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