]>
Commit | Line | Data |
---|---|---|
34e95e41 IPG |
1 | /* |
2 | * Ultra Wide Band | |
3 | * UWB API | |
4 | * | |
5 | * Copyright (C) 2005-2006 Intel Corporation | |
6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License version | |
10 | * 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
20 | * 02110-1301, USA. | |
21 | * | |
22 | * | |
23 | * FIXME: doc: overview of the API, different parts and pointers | |
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> | |
fec1a593 | 33 | #include <linux/wait.h> |
34e95e41 IPG |
34 | #include <linux/workqueue.h> |
35 | #include <linux/uwb/spec.h> | |
36 | ||
37 | struct uwb_dev; | |
38 | struct uwb_beca_e; | |
39 | struct uwb_rc; | |
40 | struct uwb_rsv; | |
41 | struct uwb_dbg; | |
42 | ||
43 | /** | |
44 | * struct uwb_dev - a UWB Device | |
45 | * @rc: UWB Radio Controller that discovered the device (kind of its | |
46 | * parent). | |
47 | * @bce: a beacon cache entry for this device; or NULL if the device | |
48 | * is a local radio controller. | |
49 | * @mac_addr: the EUI-48 address of this device. | |
50 | * @dev_addr: the current DevAddr used by this device. | |
51 | * @beacon_slot: the slot number the beacon is using. | |
52 | * @streams: bitmap of streams allocated to reservations targeted at | |
53 | * this device. For an RC, this is the streams allocated for | |
54 | * reservations targeted at DevAddrs. | |
55 | * | |
56 | * A UWB device may either by a neighbor or part of a local radio | |
57 | * controller. | |
58 | */ | |
59 | struct uwb_dev { | |
60 | struct mutex mutex; | |
61 | struct list_head list_node; | |
62 | struct device dev; | |
63 | struct uwb_rc *rc; /* radio controller */ | |
64 | struct uwb_beca_e *bce; /* Beacon Cache Entry */ | |
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 | }; | |
71 | #define to_uwb_dev(d) container_of(d, struct uwb_dev, dev) | |
72 | ||
73 | /** | |
74 | * UWB HWA/WHCI Radio Control {Command|Event} Block context IDs | |
75 | * | |
76 | * RC[CE]Bs have a 'context ID' field that matches the command with | |
77 | * the event received to confirm it. | |
78 | * | |
79 | * Maximum number of context IDs | |
80 | */ | |
81 | enum { UWB_RC_CTX_MAX = 256 }; | |
82 | ||
83 | ||
84 | /** Notification chain head for UWB generated events to listeners */ | |
85 | struct uwb_notifs_chain { | |
86 | struct list_head list; | |
87 | struct mutex mutex; | |
88 | }; | |
89 | ||
fec1a593 SP |
90 | /* Beacon cache list */ |
91 | struct uwb_beca { | |
92 | struct list_head list; | |
93 | size_t entries; | |
94 | struct mutex mutex; | |
95 | }; | |
96 | ||
97 | /* Event handling thread. */ | |
98 | struct uwbd { | |
99 | int pid; | |
100 | struct task_struct *task; | |
101 | wait_queue_head_t wq; | |
102 | struct list_head event_list; | |
103 | spinlock_t event_list_lock; | |
104 | }; | |
105 | ||
34e95e41 IPG |
106 | /** |
107 | * struct uwb_mas_bm - a bitmap of all MAS in a superframe | |
108 | * @bm: a bitmap of length #UWB_NUM_MAS | |
109 | */ | |
110 | struct uwb_mas_bm { | |
111 | DECLARE_BITMAP(bm, UWB_NUM_MAS); | |
112 | }; | |
113 | ||
114 | /** | |
115 | * uwb_rsv_state - UWB Reservation state. | |
116 | * | |
117 | * NONE - reservation is not active (no DRP IE being transmitted). | |
118 | * | |
119 | * Owner reservation states: | |
120 | * | |
121 | * INITIATED - owner has sent an initial DRP request. | |
122 | * PENDING - target responded with pending Reason Code. | |
123 | * MODIFIED - reservation manager is modifying an established | |
124 | * reservation with a different MAS allocation. | |
125 | * ESTABLISHED - the reservation has been successfully negotiated. | |
126 | * | |
127 | * Target reservation states: | |
128 | * | |
129 | * DENIED - request is denied. | |
130 | * ACCEPTED - request is accepted. | |
131 | * PENDING - PAL has yet to make a decision to whether to accept or | |
132 | * deny. | |
133 | * | |
134 | * FIXME: further target states TBD. | |
135 | */ | |
136 | enum uwb_rsv_state { | |
137 | UWB_RSV_STATE_NONE, | |
138 | UWB_RSV_STATE_O_INITIATED, | |
139 | UWB_RSV_STATE_O_PENDING, | |
140 | UWB_RSV_STATE_O_MODIFIED, | |
141 | UWB_RSV_STATE_O_ESTABLISHED, | |
142 | UWB_RSV_STATE_T_ACCEPTED, | |
143 | UWB_RSV_STATE_T_DENIED, | |
144 | UWB_RSV_STATE_T_PENDING, | |
145 | ||
146 | UWB_RSV_STATE_LAST, | |
147 | }; | |
148 | ||
149 | enum uwb_rsv_target_type { | |
150 | UWB_RSV_TARGET_DEV, | |
151 | UWB_RSV_TARGET_DEVADDR, | |
152 | }; | |
153 | ||
154 | /** | |
155 | * struct uwb_rsv_target - the target of a reservation. | |
156 | * | |
157 | * Reservations unicast and targeted at a single device | |
158 | * (UWB_RSV_TARGET_DEV); or (e.g., in the case of WUSB) targeted at a | |
159 | * specific (private) DevAddr (UWB_RSV_TARGET_DEVADDR). | |
160 | */ | |
161 | struct uwb_rsv_target { | |
162 | enum uwb_rsv_target_type type; | |
163 | union { | |
164 | struct uwb_dev *dev; | |
165 | struct uwb_dev_addr devaddr; | |
166 | }; | |
167 | }; | |
168 | ||
169 | /* | |
170 | * Number of streams reserved for reservations targeted at DevAddrs. | |
171 | */ | |
172 | #define UWB_NUM_GLOBAL_STREAMS 1 | |
173 | ||
174 | typedef void (*uwb_rsv_cb_f)(struct uwb_rsv *rsv); | |
175 | ||
176 | /** | |
177 | * struct uwb_rsv - a DRP reservation | |
178 | * | |
179 | * Data structure management: | |
180 | * | |
181 | * @rc: the radio controller this reservation is for | |
182 | * (as target or owner) | |
183 | * @rc_node: a list node for the RC | |
184 | * @pal_node: a list node for the PAL | |
185 | * | |
186 | * Owner and target parameters: | |
187 | * | |
188 | * @owner: the UWB device owning this reservation | |
189 | * @target: the target UWB device | |
190 | * @type: reservation type | |
191 | * | |
192 | * Owner parameters: | |
193 | * | |
194 | * @max_mas: maxiumum number of MAS | |
195 | * @min_mas: minimum number of MAS | |
196 | * @sparsity: owner selected sparsity | |
197 | * @is_multicast: true iff multicast | |
198 | * | |
199 | * @callback: callback function when the reservation completes | |
200 | * @pal_priv: private data for the PAL making the reservation | |
201 | * | |
202 | * Reservation status: | |
203 | * | |
204 | * @status: negotiation status | |
205 | * @stream: stream index allocated for this reservation | |
206 | * @mas: reserved MAS | |
207 | * @drp_ie: the DRP IE | |
208 | * @ie_valid: true iff the DRP IE matches the reservation parameters | |
209 | * | |
210 | * DRP reservations are uniquely identified by the owner, target and | |
211 | * stream index. However, when using a DevAddr as a target (e.g., for | |
212 | * a WUSB cluster reservation) the responses may be received from | |
213 | * devices with different DevAddrs. In this case, reservations are | |
214 | * uniquely identified by just the stream index. A number of stream | |
215 | * indexes (UWB_NUM_GLOBAL_STREAMS) are reserved for this. | |
216 | */ | |
217 | struct uwb_rsv { | |
218 | struct uwb_rc *rc; | |
219 | struct list_head rc_node; | |
220 | struct list_head pal_node; | |
cae1c114 | 221 | struct kref kref; |
34e95e41 IPG |
222 | |
223 | struct uwb_dev *owner; | |
224 | struct uwb_rsv_target target; | |
225 | enum uwb_drp_type type; | |
226 | int max_mas; | |
227 | int min_mas; | |
228 | int sparsity; | |
229 | bool is_multicast; | |
230 | ||
231 | uwb_rsv_cb_f callback; | |
232 | void *pal_priv; | |
233 | ||
234 | enum uwb_rsv_state state; | |
235 | u8 stream; | |
236 | struct uwb_mas_bm mas; | |
237 | struct uwb_ie_drp *drp_ie; | |
238 | bool ie_valid; | |
239 | struct timer_list timer; | |
240 | bool expired; | |
241 | }; | |
242 | ||
243 | static const | |
244 | struct uwb_mas_bm uwb_mas_bm_zero = { .bm = { 0 } }; | |
245 | ||
246 | static inline void uwb_mas_bm_copy_le(void *dst, const struct uwb_mas_bm *mas) | |
247 | { | |
248 | bitmap_copy_le(dst, mas->bm, UWB_NUM_MAS); | |
249 | } | |
250 | ||
251 | /** | |
252 | * struct uwb_drp_avail - a radio controller's view of MAS usage | |
253 | * @global: MAS unused by neighbors (excluding reservations targetted | |
254 | * or owned by the local radio controller) or the beaon period | |
255 | * @local: MAS unused by local established reservations | |
256 | * @pending: MAS unused by local pending reservations | |
257 | * @ie: DRP Availability IE to be included in the beacon | |
258 | * @ie_valid: true iff @ie is valid and does not need to regenerated from | |
259 | * @global and @local | |
260 | * | |
261 | * Each radio controller maintains a view of MAS usage or | |
262 | * availability. MAS available for a new reservation are determined | |
263 | * from the intersection of @global, @local, and @pending. | |
264 | * | |
265 | * The radio controller must transmit a DRP Availability IE that's the | |
266 | * intersection of @global and @local. | |
267 | * | |
268 | * A set bit indicates the MAS is unused and available. | |
269 | * | |
270 | * rc->rsvs_mutex should be held before accessing this data structure. | |
271 | * | |
272 | * [ECMA-368] section 17.4.3. | |
273 | */ | |
274 | struct uwb_drp_avail { | |
275 | DECLARE_BITMAP(global, UWB_NUM_MAS); | |
276 | DECLARE_BITMAP(local, UWB_NUM_MAS); | |
277 | DECLARE_BITMAP(pending, UWB_NUM_MAS); | |
278 | struct uwb_ie_drp_avail ie; | |
279 | bool ie_valid; | |
280 | }; | |
281 | ||
282 | ||
283 | const char *uwb_rsv_state_str(enum uwb_rsv_state state); | |
284 | const char *uwb_rsv_type_str(enum uwb_drp_type type); | |
285 | ||
286 | struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, | |
287 | void *pal_priv); | |
288 | void uwb_rsv_destroy(struct uwb_rsv *rsv); | |
289 | ||
290 | int uwb_rsv_establish(struct uwb_rsv *rsv); | |
291 | int uwb_rsv_modify(struct uwb_rsv *rsv, | |
292 | int max_mas, int min_mas, int sparsity); | |
293 | void uwb_rsv_terminate(struct uwb_rsv *rsv); | |
294 | ||
295 | void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv); | |
296 | ||
297 | /** | |
298 | * Radio Control Interface instance | |
299 | * | |
300 | * | |
301 | * Life cycle rules: those of the UWB Device. | |
302 | * | |
303 | * @index: an index number for this radio controller, as used in the | |
304 | * device name. | |
305 | * @version: version of protocol supported by this device | |
306 | * @priv: Backend implementation; rw with uwb_dev.dev.sem taken. | |
307 | * @cmd: Backend implementation to execute commands; rw and call | |
308 | * only with uwb_dev.dev.sem taken. | |
309 | * @reset: Hardware reset of radio controller and any PAL controllers. | |
310 | * @filter: Backend implementation to manipulate data to and from device | |
311 | * to be compliant to specification assumed by driver (WHCI | |
312 | * 0.95). | |
313 | * | |
314 | * uwb_dev.dev.mutex is used to execute commands and update | |
315 | * the corresponding structures; can't use a spinlock | |
316 | * because rc->cmd() can sleep. | |
317 | * @ies: This is a dynamically allocated array cacheing the | |
318 | * IEs (settable by the host) that the beacon of this | |
319 | * radio controller is currently sending. | |
320 | * | |
321 | * In reality, we store here the full command we set to | |
322 | * the radio controller (which is basically a command | |
323 | * prefix followed by all the IEs the beacon currently | |
324 | * contains). This way we don't have to realloc and | |
325 | * memcpy when setting it. | |
326 | * | |
327 | * We set this up in uwb_rc_ie_setup(), where we alloc | |
328 | * this struct, call get_ie() [so we know which IEs are | |
329 | * currently being sent, if any]. | |
330 | * | |
331 | * @ies_capacity:Amount of space (in bytes) allocated in @ies. The | |
332 | * amount used is given by sizeof(*ies) plus ies->wIELength | |
333 | * (which is a little endian quantity all the time). | |
334 | * @ies_mutex: protect the IE cache | |
335 | * @dbg: information for the debug interface | |
336 | */ | |
337 | struct uwb_rc { | |
338 | struct uwb_dev uwb_dev; | |
339 | int index; | |
340 | u16 version; | |
341 | ||
342 | struct module *owner; | |
343 | void *priv; | |
344 | int (*start)(struct uwb_rc *rc); | |
345 | void (*stop)(struct uwb_rc *rc); | |
346 | int (*cmd)(struct uwb_rc *, const struct uwb_rccb *, size_t); | |
347 | int (*reset)(struct uwb_rc *rc); | |
348 | int (*filter_cmd)(struct uwb_rc *, struct uwb_rccb **, size_t *); | |
349 | int (*filter_event)(struct uwb_rc *, struct uwb_rceb **, const size_t, | |
350 | size_t *, size_t *); | |
351 | ||
352 | spinlock_t neh_lock; /* protects neh_* and ctx_* */ | |
353 | struct list_head neh_list; /* Open NE handles */ | |
354 | unsigned long ctx_bm[UWB_RC_CTX_MAX / 8 / sizeof(unsigned long)]; | |
355 | u8 ctx_roll; | |
356 | ||
357 | int beaconing; /* Beaconing state [channel number] */ | |
6fae35f9 | 358 | int beaconing_forced; |
34e95e41 IPG |
359 | int scanning; |
360 | enum uwb_scan_type scan_type:3; | |
361 | unsigned ready:1; | |
362 | struct uwb_notifs_chain notifs_chain; | |
fec1a593 SP |
363 | struct uwb_beca uwb_beca; |
364 | ||
365 | struct uwbd uwbd; | |
34e95e41 IPG |
366 | |
367 | struct uwb_drp_avail drp_avail; | |
368 | struct list_head reservations; | |
369 | struct mutex rsvs_mutex; | |
370 | struct workqueue_struct *rsv_workq; | |
371 | struct work_struct rsv_update_work; | |
372 | ||
373 | struct mutex ies_mutex; | |
374 | struct uwb_rc_cmd_set_ie *ies; | |
375 | size_t ies_capacity; | |
376 | ||
34e95e41 | 377 | struct list_head pals; |
6fae35f9 | 378 | int active_pals; |
34e95e41 IPG |
379 | |
380 | struct uwb_dbg *dbg; | |
381 | }; | |
382 | ||
383 | ||
384 | /** | |
385 | * struct uwb_pal - a UWB PAL | |
6fae35f9 | 386 | * @name: descriptive name for this PAL (wusbhc, wlp, etc.). |
b60066c1 DV |
387 | * @device: a device for the PAL. Used to link the PAL and the radio |
388 | * controller in sysfs. | |
6fae35f9 DV |
389 | * @rc: the radio controller the PAL uses. |
390 | * @channel_changed: called when the channel used by the radio changes. | |
391 | * A channel of -1 means the channel has been stopped. | |
34e95e41 IPG |
392 | * @new_rsv: called when a peer requests a reservation (may be NULL if |
393 | * the PAL cannot accept reservation requests). | |
6fae35f9 DV |
394 | * @channel: channel being used by the PAL; 0 if the PAL isn't using |
395 | * the radio; -1 if the PAL wishes to use the radio but | |
396 | * cannot. | |
34e95e41 IPG |
397 | * |
398 | * A Protocol Adaptation Layer (PAL) is a user of the WiMedia UWB | |
399 | * radio platform (e.g., WUSB, WLP or Bluetooth UWB AMP). | |
400 | * | |
401 | * The PALs using a radio controller must register themselves to | |
402 | * permit the UWB stack to coordinate usage of the radio between the | |
403 | * various PALs or to allow PALs to response to certain requests from | |
404 | * peers. | |
405 | * | |
406 | * A struct uwb_pal should be embedded in a containing structure | |
407 | * belonging to the PAL and initialized with uwb_pal_init()). Fields | |
408 | * should be set appropriately by the PAL before registering the PAL | |
409 | * with uwb_pal_register(). | |
410 | */ | |
411 | struct uwb_pal { | |
412 | struct list_head node; | |
b60066c1 DV |
413 | const char *name; |
414 | struct device *device; | |
6fae35f9 DV |
415 | struct uwb_rc *rc; |
416 | ||
417 | void (*channel_changed)(struct uwb_pal *pal, int channel); | |
e17be2b2 | 418 | void (*new_rsv)(struct uwb_pal *pal, struct uwb_rsv *rsv); |
6fae35f9 DV |
419 | |
420 | int channel; | |
34e95e41 IPG |
421 | }; |
422 | ||
423 | void uwb_pal_init(struct uwb_pal *pal); | |
6fae35f9 DV |
424 | int uwb_pal_register(struct uwb_pal *pal); |
425 | void uwb_pal_unregister(struct uwb_pal *pal); | |
426 | ||
427 | int uwb_radio_start(struct uwb_pal *pal); | |
428 | void uwb_radio_stop(struct uwb_pal *pal); | |
34e95e41 IPG |
429 | |
430 | /* | |
431 | * General public API | |
432 | * | |
433 | * This API can be used by UWB device drivers or by those implementing | |
434 | * UWB Radio Controllers | |
435 | */ | |
436 | struct uwb_dev *uwb_dev_get_by_devaddr(struct uwb_rc *rc, | |
437 | const struct uwb_dev_addr *devaddr); | |
438 | struct uwb_dev *uwb_dev_get_by_rc(struct uwb_dev *, struct uwb_rc *); | |
439 | static inline void uwb_dev_get(struct uwb_dev *uwb_dev) | |
440 | { | |
441 | get_device(&uwb_dev->dev); | |
442 | } | |
443 | static inline void uwb_dev_put(struct uwb_dev *uwb_dev) | |
444 | { | |
445 | put_device(&uwb_dev->dev); | |
446 | } | |
447 | struct uwb_dev *uwb_dev_try_get(struct uwb_rc *rc, struct uwb_dev *uwb_dev); | |
448 | ||
449 | /** | |
450 | * Callback function for 'uwb_{dev,rc}_foreach()'. | |
451 | * | |
452 | * @dev: Linux device instance | |
453 | * 'uwb_dev = container_of(dev, struct uwb_dev, dev)' | |
454 | * @priv: Data passed by the caller to 'uwb_{dev,rc}_foreach()'. | |
455 | * | |
456 | * @returns: 0 to continue the iterations, any other val to stop | |
457 | * iterating and return the value to the caller of | |
458 | * _foreach(). | |
459 | */ | |
460 | typedef int (*uwb_dev_for_each_f)(struct device *dev, void *priv); | |
461 | int uwb_dev_for_each(struct uwb_rc *rc, uwb_dev_for_each_f func, void *priv); | |
462 | ||
463 | struct uwb_rc *uwb_rc_alloc(void); | |
464 | struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *); | |
465 | struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *); | |
466 | void uwb_rc_put(struct uwb_rc *rc); | |
467 | ||
468 | typedef void (*uwb_rc_cmd_cb_f)(struct uwb_rc *rc, void *arg, | |
469 | struct uwb_rceb *reply, ssize_t reply_size); | |
470 | ||
471 | int uwb_rc_cmd_async(struct uwb_rc *rc, const char *cmd_name, | |
472 | struct uwb_rccb *cmd, size_t cmd_size, | |
473 | u8 expected_type, u16 expected_event, | |
474 | uwb_rc_cmd_cb_f cb, void *arg); | |
475 | ssize_t uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name, | |
476 | struct uwb_rccb *cmd, size_t cmd_size, | |
477 | struct uwb_rceb *reply, size_t reply_size); | |
478 | ssize_t uwb_rc_vcmd(struct uwb_rc *rc, const char *cmd_name, | |
479 | struct uwb_rccb *cmd, size_t cmd_size, | |
480 | u8 expected_type, u16 expected_event, | |
481 | struct uwb_rceb **preply); | |
34e95e41 IPG |
482 | |
483 | size_t __uwb_addr_print(char *, size_t, const unsigned char *, int); | |
484 | ||
485 | int uwb_rc_dev_addr_set(struct uwb_rc *, const struct uwb_dev_addr *); | |
486 | int uwb_rc_dev_addr_get(struct uwb_rc *, struct uwb_dev_addr *); | |
487 | int uwb_rc_mac_addr_set(struct uwb_rc *, const struct uwb_mac_addr *); | |
488 | int uwb_rc_mac_addr_get(struct uwb_rc *, struct uwb_mac_addr *); | |
489 | int __uwb_mac_addr_assigned_check(struct device *, void *); | |
490 | int __uwb_dev_addr_assigned_check(struct device *, void *); | |
491 | ||
492 | /* Print in @buf a pretty repr of @addr */ | |
493 | static inline size_t uwb_dev_addr_print(char *buf, size_t buf_size, | |
494 | const struct uwb_dev_addr *addr) | |
495 | { | |
496 | return __uwb_addr_print(buf, buf_size, addr->data, 0); | |
497 | } | |
498 | ||
499 | /* Print in @buf a pretty repr of @addr */ | |
500 | static inline size_t uwb_mac_addr_print(char *buf, size_t buf_size, | |
501 | const struct uwb_mac_addr *addr) | |
502 | { | |
503 | return __uwb_addr_print(buf, buf_size, addr->data, 1); | |
504 | } | |
505 | ||
506 | /* @returns 0 if device addresses @addr2 and @addr1 are equal */ | |
507 | static inline int uwb_dev_addr_cmp(const struct uwb_dev_addr *addr1, | |
508 | const struct uwb_dev_addr *addr2) | |
509 | { | |
510 | return memcmp(addr1, addr2, sizeof(*addr1)); | |
511 | } | |
512 | ||
513 | /* @returns 0 if MAC addresses @addr2 and @addr1 are equal */ | |
514 | static inline int uwb_mac_addr_cmp(const struct uwb_mac_addr *addr1, | |
515 | const struct uwb_mac_addr *addr2) | |
516 | { | |
517 | return memcmp(addr1, addr2, sizeof(*addr1)); | |
518 | } | |
519 | ||
520 | /* @returns !0 if a MAC @addr is a broadcast address */ | |
521 | static inline int uwb_mac_addr_bcast(const struct uwb_mac_addr *addr) | |
522 | { | |
523 | struct uwb_mac_addr bcast = { | |
524 | .data = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } | |
525 | }; | |
526 | return !uwb_mac_addr_cmp(addr, &bcast); | |
527 | } | |
528 | ||
529 | /* @returns !0 if a MAC @addr is all zeroes*/ | |
530 | static inline int uwb_mac_addr_unset(const struct uwb_mac_addr *addr) | |
531 | { | |
532 | struct uwb_mac_addr unset = { | |
533 | .data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } | |
534 | }; | |
535 | return !uwb_mac_addr_cmp(addr, &unset); | |
536 | } | |
537 | ||
538 | /* @returns !0 if the address is in use. */ | |
539 | static inline unsigned __uwb_dev_addr_assigned(struct uwb_rc *rc, | |
540 | struct uwb_dev_addr *addr) | |
541 | { | |
542 | return uwb_dev_for_each(rc, __uwb_dev_addr_assigned_check, addr); | |
543 | } | |
544 | ||
545 | /* | |
546 | * UWB Radio Controller API | |
547 | * | |
548 | * This API is used (in addition to the general API) to implement UWB | |
549 | * Radio Controllers. | |
550 | */ | |
551 | void uwb_rc_init(struct uwb_rc *); | |
552 | int uwb_rc_add(struct uwb_rc *, struct device *dev, void *rc_priv); | |
553 | void uwb_rc_rm(struct uwb_rc *); | |
554 | void uwb_rc_neh_grok(struct uwb_rc *, void *, size_t); | |
555 | void uwb_rc_neh_error(struct uwb_rc *, int); | |
556 | void uwb_rc_reset_all(struct uwb_rc *rc); | |
307ba6dd DV |
557 | void uwb_rc_pre_reset(struct uwb_rc *rc); |
558 | void uwb_rc_post_reset(struct uwb_rc *rc); | |
34e95e41 IPG |
559 | |
560 | /** | |
561 | * uwb_rsv_is_owner - is the owner of this reservation the RC? | |
562 | * @rsv: the reservation | |
563 | */ | |
564 | static inline bool uwb_rsv_is_owner(struct uwb_rsv *rsv) | |
565 | { | |
566 | return rsv->owner == &rsv->rc->uwb_dev; | |
567 | } | |
568 | ||
569 | /** | |
0996e638 DV |
570 | * enum uwb_notifs - UWB events that can be passed to any listeners |
571 | * @UWB_NOTIF_ONAIR: a new neighbour has joined the beacon group. | |
572 | * @UWB_NOTIF_OFFAIR: a neighbour has left the beacon group. | |
34e95e41 IPG |
573 | * |
574 | * Higher layers can register callback functions with the radio | |
575 | * controller using uwb_notifs_register(). The radio controller | |
576 | * maintains a list of all registered handlers and will notify all | |
577 | * nodes when an event occurs. | |
578 | */ | |
579 | enum uwb_notifs { | |
34e95e41 IPG |
580 | UWB_NOTIF_ONAIR, |
581 | UWB_NOTIF_OFFAIR, | |
582 | }; | |
583 | ||
584 | /* Callback function registered with UWB */ | |
585 | struct uwb_notifs_handler { | |
586 | struct list_head list_node; | |
587 | void (*cb)(void *, struct uwb_dev *, enum uwb_notifs); | |
588 | void *data; | |
589 | }; | |
590 | ||
591 | int uwb_notifs_register(struct uwb_rc *, struct uwb_notifs_handler *); | |
592 | int uwb_notifs_deregister(struct uwb_rc *, struct uwb_notifs_handler *); | |
593 | ||
594 | ||
595 | /** | |
596 | * UWB radio controller Event Size Entry (for creating entry tables) | |
597 | * | |
598 | * WUSB and WHCI define events and notifications, and they might have | |
599 | * fixed or variable size. | |
600 | * | |
601 | * Each event/notification has a size which is not necessarily known | |
602 | * in advance based on the event code. As well, vendor specific | |
603 | * events/notifications will have a size impossible to determine | |
604 | * unless we know about the device's specific details. | |
605 | * | |
606 | * It was way too smart of the spec writers not to think that it would | |
607 | * be impossible for a generic driver to skip over vendor specific | |
608 | * events/notifications if there are no LENGTH fields in the HEADER of | |
609 | * each message...the transaction size cannot be counted on as the | |
610 | * spec does not forbid to pack more than one event in a single | |
611 | * transaction. | |
612 | * | |
613 | * Thus, we guess sizes with tables (or for events, when you know the | |
614 | * size ahead of time you can use uwb_rc_neh_extra_size*()). We | |
615 | * register tables with the known events and their sizes, and then we | |
616 | * traverse those tables. For those with variable length, we provide a | |
617 | * way to lookup the size inside the event/notification's | |
618 | * payload. This allows device-specific event size tables to be | |
619 | * registered. | |
620 | * | |
621 | * @size: Size of the payload | |
622 | * | |
623 | * @offset: if != 0, at offset @offset-1 starts a field with a length | |
624 | * that has to be added to @size. The format of the field is | |
625 | * given by @type. | |
626 | * | |
627 | * @type: Type and length of the offset field. Most common is LE 16 | |
628 | * bits (that's why that is zero); others are there mostly to | |
629 | * cover for bugs and weirdos. | |
630 | */ | |
631 | struct uwb_est_entry { | |
632 | size_t size; | |
633 | unsigned offset; | |
634 | enum { UWB_EST_16 = 0, UWB_EST_8 = 1 } type; | |
635 | }; | |
636 | ||
637 | int uwb_est_register(u8 type, u8 code_high, u16 vendor, u16 product, | |
638 | const struct uwb_est_entry *, size_t entries); | |
639 | int uwb_est_unregister(u8 type, u8 code_high, u16 vendor, u16 product, | |
640 | const struct uwb_est_entry *, size_t entries); | |
641 | ssize_t uwb_est_find_size(struct uwb_rc *rc, const struct uwb_rceb *rceb, | |
642 | size_t len); | |
643 | ||
644 | /* -- Misc */ | |
645 | ||
646 | enum { | |
647 | EDC_MAX_ERRORS = 10, | |
648 | EDC_ERROR_TIMEFRAME = HZ, | |
649 | }; | |
650 | ||
651 | /* error density counter */ | |
652 | struct edc { | |
653 | unsigned long timestart; | |
654 | u16 errorcount; | |
655 | }; | |
656 | ||
657 | static inline | |
658 | void edc_init(struct edc *edc) | |
659 | { | |
660 | edc->timestart = jiffies; | |
661 | } | |
662 | ||
663 | /* Called when an error occured. | |
664 | * This is way to determine if the number of acceptable errors per time | |
665 | * period has been exceeded. It is not accurate as there are cases in which | |
666 | * this scheme will not work, for example if there are periodic occurences | |
667 | * of errors that straddle updates to the start time. This scheme is | |
668 | * sufficient for our usage. | |
669 | * | |
670 | * @returns 1 if maximum acceptable errors per timeframe has been exceeded. | |
671 | */ | |
672 | static inline int edc_inc(struct edc *err_hist, u16 max_err, u16 timeframe) | |
673 | { | |
674 | unsigned long now; | |
675 | ||
676 | now = jiffies; | |
677 | if (now - err_hist->timestart > timeframe) { | |
678 | err_hist->errorcount = 1; | |
679 | err_hist->timestart = now; | |
680 | } else if (++err_hist->errorcount > max_err) { | |
681 | err_hist->errorcount = 0; | |
682 | err_hist->timestart = now; | |
683 | return 1; | |
684 | } | |
685 | return 0; | |
686 | } | |
687 | ||
688 | ||
689 | /* Information Element handling */ | |
690 | ||
34e95e41 | 691 | struct uwb_ie_hdr *uwb_ie_next(void **ptr, size_t *len); |
1cde7f68 DV |
692 | int uwb_rc_ie_add(struct uwb_rc *uwb_rc, const struct uwb_ie_hdr *ies, size_t size); |
693 | int uwb_rc_ie_rm(struct uwb_rc *uwb_rc, enum uwb_ie element_id); | |
34e95e41 IPG |
694 | |
695 | /* | |
696 | * Transmission statistics | |
697 | * | |
698 | * UWB uses LQI and RSSI (one byte values) for reporting radio signal | |
699 | * strength and line quality indication. We do quick and dirty | |
700 | * averages of those. They are signed values, btw. | |
701 | * | |
702 | * For 8 bit quantities, we keep the min, the max, an accumulator | |
703 | * (@sigma) and a # of samples. When @samples gets to 255, we compute | |
704 | * the average (@sigma / @samples), place it in @sigma and reset | |
705 | * @samples to 1 (so we use it as the first sample). | |
706 | * | |
707 | * Now, statistically speaking, probably I am kicking the kidneys of | |
708 | * some books I have in my shelves collecting dust, but I just want to | |
709 | * get an approx, not the Nobel. | |
710 | * | |
711 | * LOCKING: there is no locking per se, but we try to keep a lockless | |
712 | * schema. Only _add_samples() modifies the values--as long as you | |
713 | * have other locking on top that makes sure that no two calls of | |
714 | * _add_sample() happen at the same time, then we are fine. Now, for | |
715 | * resetting the values we just set @samples to 0 and that makes the | |
716 | * next _add_sample() to start with defaults. Reading the values in | |
717 | * _show() currently can race, so you need to make sure the calls are | |
718 | * under the same lock that protects calls to _add_sample(). FIXME: | |
719 | * currently unlocked (It is not ultraprecise but does the trick. Bite | |
720 | * me). | |
721 | */ | |
722 | struct stats { | |
723 | s8 min, max; | |
724 | s16 sigma; | |
725 | atomic_t samples; | |
726 | }; | |
727 | ||
728 | static inline | |
729 | void stats_init(struct stats *stats) | |
730 | { | |
731 | atomic_set(&stats->samples, 0); | |
732 | wmb(); | |
733 | } | |
734 | ||
735 | static inline | |
736 | void stats_add_sample(struct stats *stats, s8 sample) | |
737 | { | |
738 | s8 min, max; | |
739 | s16 sigma; | |
740 | unsigned samples = atomic_read(&stats->samples); | |
741 | if (samples == 0) { /* it was zero before, so we initialize */ | |
742 | min = 127; | |
743 | max = -128; | |
744 | sigma = 0; | |
745 | } else { | |
746 | min = stats->min; | |
747 | max = stats->max; | |
748 | sigma = stats->sigma; | |
749 | } | |
750 | ||
751 | if (sample < min) /* compute new values */ | |
752 | min = sample; | |
753 | else if (sample > max) | |
754 | max = sample; | |
755 | sigma += sample; | |
756 | ||
757 | stats->min = min; /* commit */ | |
758 | stats->max = max; | |
759 | stats->sigma = sigma; | |
760 | if (atomic_add_return(1, &stats->samples) > 255) { | |
761 | /* wrapped around! reset */ | |
762 | stats->sigma = sigma / 256; | |
763 | atomic_set(&stats->samples, 1); | |
764 | } | |
765 | } | |
766 | ||
767 | static inline ssize_t stats_show(struct stats *stats, char *buf) | |
768 | { | |
769 | int min, max, avg; | |
770 | int samples = atomic_read(&stats->samples); | |
771 | if (samples == 0) | |
772 | min = max = avg = 0; | |
773 | else { | |
774 | min = stats->min; | |
775 | max = stats->max; | |
776 | avg = stats->sigma / samples; | |
777 | } | |
778 | return scnprintf(buf, PAGE_SIZE, "%d %d %d\n", min, max, avg); | |
779 | } | |
780 | ||
781 | static inline ssize_t stats_store(struct stats *stats, const char *buf, | |
782 | size_t size) | |
783 | { | |
784 | stats_init(stats); | |
785 | return size; | |
786 | } | |
787 | ||
788 | #endif /* #ifndef __LINUX__UWB_H__ */ |