1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#ifndef __LINUX__UWB_H__
27#define __LINUX__UWB_H__
28
29#include <linux/limits.h>
30#include <linux/device.h>
31#include <linux/mutex.h>
32#include <linux/timer.h>
33#include <linux/wait.h>
34#include <linux/workqueue.h>
35#include <linux/uwb/spec.h>
36#include <asm/page.h>
37
38struct uwb_dev;
39struct uwb_beca_e;
40struct uwb_rc;
41struct uwb_rsv;
42struct uwb_dbg;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60struct uwb_dev {
61 struct mutex mutex;
62 struct list_head list_node;
63 struct device dev;
64 struct uwb_rc *rc;
65 struct uwb_beca_e *bce;
66
67 struct uwb_mac_addr mac_addr;
68 struct uwb_dev_addr dev_addr;
69 int beacon_slot;
70 DECLARE_BITMAP(streams, UWB_NUM_STREAMS);
71 DECLARE_BITMAP(last_availability_bm, UWB_NUM_MAS);
72};
73#define to_uwb_dev(d) container_of(d, struct uwb_dev, dev)
74
75
76
77
78
79
80
81
82
83enum { UWB_RC_CTX_MAX = 256 };
84
85
86
87struct uwb_notifs_chain {
88 struct list_head list;
89 struct mutex mutex;
90};
91
92
93struct uwb_beca {
94 struct list_head list;
95 size_t entries;
96 struct mutex mutex;
97};
98
99
100struct uwbd {
101 int pid;
102 struct task_struct *task;
103 wait_queue_head_t wq;
104 struct list_head event_list;
105 spinlock_t event_list_lock;
106};
107
108
109
110
111
112struct uwb_mas_bm {
113 DECLARE_BITMAP(bm, UWB_NUM_MAS);
114 DECLARE_BITMAP(unsafe_bm, UWB_NUM_MAS);
115 int safe;
116 int unsafe;
117};
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141enum uwb_rsv_state {
142 UWB_RSV_STATE_NONE = 0,
143 UWB_RSV_STATE_O_INITIATED,
144 UWB_RSV_STATE_O_PENDING,
145 UWB_RSV_STATE_O_MODIFIED,
146 UWB_RSV_STATE_O_ESTABLISHED,
147 UWB_RSV_STATE_O_TO_BE_MOVED,
148 UWB_RSV_STATE_O_MOVE_EXPANDING,
149 UWB_RSV_STATE_O_MOVE_COMBINING,
150 UWB_RSV_STATE_O_MOVE_REDUCING,
151 UWB_RSV_STATE_T_ACCEPTED,
152 UWB_RSV_STATE_T_DENIED,
153 UWB_RSV_STATE_T_CONFLICT,
154 UWB_RSV_STATE_T_PENDING,
155 UWB_RSV_STATE_T_EXPANDING_ACCEPTED,
156 UWB_RSV_STATE_T_EXPANDING_CONFLICT,
157 UWB_RSV_STATE_T_EXPANDING_PENDING,
158 UWB_RSV_STATE_T_EXPANDING_DENIED,
159 UWB_RSV_STATE_T_RESIZED,
160
161 UWB_RSV_STATE_LAST,
162};
163
164enum uwb_rsv_target_type {
165 UWB_RSV_TARGET_DEV,
166 UWB_RSV_TARGET_DEVADDR,
167};
168
169
170
171
172
173
174
175
176struct uwb_rsv_target {
177 enum uwb_rsv_target_type type;
178 union {
179 struct uwb_dev *dev;
180 struct uwb_dev_addr devaddr;
181 };
182};
183
184struct uwb_rsv_move {
185 struct uwb_mas_bm final_mas;
186 struct uwb_ie_drp *companion_drp_ie;
187 struct uwb_mas_bm companion_mas;
188};
189
190
191
192
193#define UWB_NUM_GLOBAL_STREAMS 1
194
195typedef void (*uwb_rsv_cb_f)(struct uwb_rsv *rsv);
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239struct uwb_rsv {
240 struct uwb_rc *rc;
241 struct list_head rc_node;
242 struct list_head pal_node;
243 struct kref kref;
244
245 struct uwb_dev *owner;
246 struct uwb_rsv_target target;
247 enum uwb_drp_type type;
248 int max_mas;
249 int min_mas;
250 int max_interval;
251 bool is_multicast;
252
253 uwb_rsv_cb_f callback;
254 void *pal_priv;
255
256 enum uwb_rsv_state state;
257 bool needs_release_companion_mas;
258 u8 stream;
259 u8 tiebreaker;
260 struct uwb_mas_bm mas;
261 struct uwb_ie_drp *drp_ie;
262 struct uwb_rsv_move mv;
263 bool ie_valid;
264 struct timer_list timer;
265 struct work_struct handle_timeout_work;
266};
267
268static const
269struct uwb_mas_bm uwb_mas_bm_zero = { .bm = { 0 } };
270
271static inline void uwb_mas_bm_copy_le(void *dst, const struct uwb_mas_bm *mas)
272{
273 bitmap_copy_le(dst, mas->bm, UWB_NUM_MAS);
274}
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299struct uwb_drp_avail {
300 DECLARE_BITMAP(global, UWB_NUM_MAS);
301 DECLARE_BITMAP(local, UWB_NUM_MAS);
302 DECLARE_BITMAP(pending, UWB_NUM_MAS);
303 struct uwb_ie_drp_avail ie;
304 bool ie_valid;
305};
306
307struct uwb_drp_backoff_win {
308 u8 window;
309 u8 n;
310 int total_expired;
311 struct timer_list timer;
312 bool can_reserve_extra_mases;
313};
314
315const char *uwb_rsv_state_str(enum uwb_rsv_state state);
316const char *uwb_rsv_type_str(enum uwb_drp_type type);
317
318struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb,
319 void *pal_priv);
320void uwb_rsv_destroy(struct uwb_rsv *rsv);
321
322int uwb_rsv_establish(struct uwb_rsv *rsv);
323int uwb_rsv_modify(struct uwb_rsv *rsv,
324 int max_mas, int min_mas, int sparsity);
325void uwb_rsv_terminate(struct uwb_rsv *rsv);
326
327void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv);
328
329void uwb_rsv_get_usable_mas(struct uwb_rsv *orig_rsv, struct uwb_mas_bm *mas);
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371struct uwb_rc {
372 struct uwb_dev uwb_dev;
373 int index;
374 u16 version;
375
376 struct module *owner;
377 void *priv;
378 int (*start)(struct uwb_rc *rc);
379 void (*stop)(struct uwb_rc *rc);
380 int (*cmd)(struct uwb_rc *, const struct uwb_rccb *, size_t);
381 int (*reset)(struct uwb_rc *rc);
382 int (*filter_cmd)(struct uwb_rc *, struct uwb_rccb **, size_t *);
383 int (*filter_event)(struct uwb_rc *, struct uwb_rceb **, const size_t,
384 size_t *, size_t *);
385
386 spinlock_t neh_lock;
387 struct list_head neh_list;
388 unsigned long ctx_bm[UWB_RC_CTX_MAX / 8 / sizeof(unsigned long)];
389 u8 ctx_roll;
390
391 int beaconing;
392 int beaconing_forced;
393 int scanning;
394 enum uwb_scan_type scan_type:3;
395 unsigned ready:1;
396 struct uwb_notifs_chain notifs_chain;
397 struct uwb_beca uwb_beca;
398
399 struct uwbd uwbd;
400
401 struct uwb_drp_backoff_win bow;
402 struct uwb_drp_avail drp_avail;
403 struct list_head reservations;
404 struct list_head cnflt_alien_list;
405 struct uwb_mas_bm cnflt_alien_bitmap;
406 struct mutex rsvs_mutex;
407 spinlock_t rsvs_lock;
408 struct workqueue_struct *rsv_workq;
409
410 struct delayed_work rsv_update_work;
411 struct delayed_work rsv_alien_bp_work;
412 int set_drp_ie_pending;
413 struct mutex ies_mutex;
414 struct uwb_rc_cmd_set_ie *ies;
415 size_t ies_capacity;
416
417 struct list_head pals;
418 int active_pals;
419
420 struct uwb_dbg *dbg;
421};
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453struct uwb_pal {
454 struct list_head node;
455 const char *name;
456 struct device *device;
457 struct uwb_rc *rc;
458
459 void (*channel_changed)(struct uwb_pal *pal, int channel);
460 void (*new_rsv)(struct uwb_pal *pal, struct uwb_rsv *rsv);
461
462 int channel;
463 struct dentry *debugfs_dir;
464};
465
466void uwb_pal_init(struct uwb_pal *pal);
467int uwb_pal_register(struct uwb_pal *pal);
468void uwb_pal_unregister(struct uwb_pal *pal);
469
470int uwb_radio_start(struct uwb_pal *pal);
471void uwb_radio_stop(struct uwb_pal *pal);
472
473
474
475
476
477
478
479struct uwb_dev *uwb_dev_get_by_devaddr(struct uwb_rc *rc,
480 const struct uwb_dev_addr *devaddr);
481struct uwb_dev *uwb_dev_get_by_rc(struct uwb_dev *, struct uwb_rc *);
482static inline void uwb_dev_get(struct uwb_dev *uwb_dev)
483{
484 get_device(&uwb_dev->dev);
485}
486static inline void uwb_dev_put(struct uwb_dev *uwb_dev)
487{
488 put_device(&uwb_dev->dev);
489}
490struct uwb_dev *uwb_dev_try_get(struct uwb_rc *rc, struct uwb_dev *uwb_dev);
491
492
493
494
495
496
497
498
499
500
501
502
503typedef int (*uwb_dev_for_each_f)(struct device *dev, void *priv);
504int uwb_dev_for_each(struct uwb_rc *rc, uwb_dev_for_each_f func, void *priv);
505
506struct uwb_rc *uwb_rc_alloc(void);
507struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *);
508struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *);
509void uwb_rc_put(struct uwb_rc *rc);
510
511typedef void (*uwb_rc_cmd_cb_f)(struct uwb_rc *rc, void *arg,
512 struct uwb_rceb *reply, ssize_t reply_size);
513
514int uwb_rc_cmd_async(struct uwb_rc *rc, const char *cmd_name,
515 struct uwb_rccb *cmd, size_t cmd_size,
516 u8 expected_type, u16 expected_event,
517 uwb_rc_cmd_cb_f cb, void *arg);
518ssize_t uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name,
519 struct uwb_rccb *cmd, size_t cmd_size,
520 struct uwb_rceb *reply, size_t reply_size);
521ssize_t uwb_rc_vcmd(struct uwb_rc *rc, const char *cmd_name,
522 struct uwb_rccb *cmd, size_t cmd_size,
523 u8 expected_type, u16 expected_event,
524 struct uwb_rceb **preply);
525
526size_t __uwb_addr_print(char *, size_t, const unsigned char *, int);
527
528int uwb_rc_dev_addr_set(struct uwb_rc *, const struct uwb_dev_addr *);
529int uwb_rc_dev_addr_get(struct uwb_rc *, struct uwb_dev_addr *);
530int uwb_rc_mac_addr_set(struct uwb_rc *, const struct uwb_mac_addr *);
531int uwb_rc_mac_addr_get(struct uwb_rc *, struct uwb_mac_addr *);
532int __uwb_mac_addr_assigned_check(struct device *, void *);
533int __uwb_dev_addr_assigned_check(struct device *, void *);
534
535
536static inline size_t uwb_dev_addr_print(char *buf, size_t buf_size,
537 const struct uwb_dev_addr *addr)
538{
539 return __uwb_addr_print(buf, buf_size, addr->data, 0);
540}
541
542
543static inline size_t uwb_mac_addr_print(char *buf, size_t buf_size,
544 const struct uwb_mac_addr *addr)
545{
546 return __uwb_addr_print(buf, buf_size, addr->data, 1);
547}
548
549
550static inline int uwb_dev_addr_cmp(const struct uwb_dev_addr *addr1,
551 const struct uwb_dev_addr *addr2)
552{
553 return memcmp(addr1, addr2, sizeof(*addr1));
554}
555
556
557static inline int uwb_mac_addr_cmp(const struct uwb_mac_addr *addr1,
558 const struct uwb_mac_addr *addr2)
559{
560 return memcmp(addr1, addr2, sizeof(*addr1));
561}
562
563
564static inline int uwb_mac_addr_bcast(const struct uwb_mac_addr *addr)
565{
566 struct uwb_mac_addr bcast = {
567 .data = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
568 };
569 return !uwb_mac_addr_cmp(addr, &bcast);
570}
571
572
573static inline int uwb_mac_addr_unset(const struct uwb_mac_addr *addr)
574{
575 struct uwb_mac_addr unset = {
576 .data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
577 };
578 return !uwb_mac_addr_cmp(addr, &unset);
579}
580
581
582static inline unsigned __uwb_dev_addr_assigned(struct uwb_rc *rc,
583 struct uwb_dev_addr *addr)
584{
585 return uwb_dev_for_each(rc, __uwb_dev_addr_assigned_check, addr);
586}
587
588
589
590
591
592
593
594void uwb_rc_init(struct uwb_rc *);
595int uwb_rc_add(struct uwb_rc *, struct device *dev, void *rc_priv);
596void uwb_rc_rm(struct uwb_rc *);
597void uwb_rc_neh_grok(struct uwb_rc *, void *, size_t);
598void uwb_rc_neh_error(struct uwb_rc *, int);
599void uwb_rc_reset_all(struct uwb_rc *rc);
600void uwb_rc_pre_reset(struct uwb_rc *rc);
601int uwb_rc_post_reset(struct uwb_rc *rc);
602
603
604
605
606
607static inline bool uwb_rsv_is_owner(struct uwb_rsv *rsv)
608{
609 return rsv->owner == &rsv->rc->uwb_dev;
610}
611
612
613
614
615
616
617
618
619
620
621
622enum uwb_notifs {
623 UWB_NOTIF_ONAIR,
624 UWB_NOTIF_OFFAIR,
625};
626
627
628struct uwb_notifs_handler {
629 struct list_head list_node;
630 void (*cb)(void *, struct uwb_dev *, enum uwb_notifs);
631 void *data;
632};
633
634int uwb_notifs_register(struct uwb_rc *, struct uwb_notifs_handler *);
635int uwb_notifs_deregister(struct uwb_rc *, struct uwb_notifs_handler *);
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674struct uwb_est_entry {
675 size_t size;
676 unsigned offset;
677 enum { UWB_EST_16 = 0, UWB_EST_8 = 1 } type;
678};
679
680int uwb_est_register(u8 type, u8 code_high, u16 vendor, u16 product,
681 const struct uwb_est_entry *, size_t entries);
682int uwb_est_unregister(u8 type, u8 code_high, u16 vendor, u16 product,
683 const struct uwb_est_entry *, size_t entries);
684ssize_t uwb_est_find_size(struct uwb_rc *rc, const struct uwb_rceb *rceb,
685 size_t len);
686
687
688
689enum {
690 EDC_MAX_ERRORS = 10,
691 EDC_ERROR_TIMEFRAME = HZ,
692};
693
694
695struct edc {
696 unsigned long timestart;
697 u16 errorcount;
698};
699
700static inline
701void edc_init(struct edc *edc)
702{
703 edc->timestart = jiffies;
704}
705
706
707
708
709
710
711
712
713
714
715static inline int edc_inc(struct edc *err_hist, u16 max_err, u16 timeframe)
716{
717 unsigned long now;
718
719 now = jiffies;
720 if (now - err_hist->timestart > timeframe) {
721 err_hist->errorcount = 1;
722 err_hist->timestart = now;
723 } else if (++err_hist->errorcount > max_err) {
724 err_hist->errorcount = 0;
725 err_hist->timestart = now;
726 return 1;
727 }
728 return 0;
729}
730
731
732
733
734struct uwb_ie_hdr *uwb_ie_next(void **ptr, size_t *len);
735int uwb_rc_ie_add(struct uwb_rc *uwb_rc, const struct uwb_ie_hdr *ies, size_t size);
736int uwb_rc_ie_rm(struct uwb_rc *uwb_rc, enum uwb_ie element_id);
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765struct stats {
766 s8 min, max;
767 s16 sigma;
768 atomic_t samples;
769};
770
771static inline
772void stats_init(struct stats *stats)
773{
774 atomic_set(&stats->samples, 0);
775 wmb();
776}
777
778static inline
779void stats_add_sample(struct stats *stats, s8 sample)
780{
781 s8 min, max;
782 s16 sigma;
783 unsigned samples = atomic_read(&stats->samples);
784 if (samples == 0) {
785 min = 127;
786 max = -128;
787 sigma = 0;
788 } else {
789 min = stats->min;
790 max = stats->max;
791 sigma = stats->sigma;
792 }
793
794 if (sample < min)
795 min = sample;
796 else if (sample > max)
797 max = sample;
798 sigma += sample;
799
800 stats->min = min;
801 stats->max = max;
802 stats->sigma = sigma;
803 if (atomic_add_return(1, &stats->samples) > 255) {
804
805 stats->sigma = sigma / 256;
806 atomic_set(&stats->samples, 1);
807 }
808}
809
810static inline ssize_t stats_show(struct stats *stats, char *buf)
811{
812 int min, max, avg;
813 int samples = atomic_read(&stats->samples);
814 if (samples == 0)
815 min = max = avg = 0;
816 else {
817 min = stats->min;
818 max = stats->max;
819 avg = stats->sigma / samples;
820 }
821 return scnprintf(buf, PAGE_SIZE, "%d %d %d\n", min, max, avg);
822}
823
824static inline ssize_t stats_store(struct stats *stats, const char *buf,
825 size_t size)
826{
827 stats_init(stats);
828 return size;
829}
830
831#endif
832