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