uwb: order IEs by element ID
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / include / linux / uwb.h
blob6d93f54b8879953834fb6c14178d5d432c239c99
1 /*
2 * Ultra Wide Band
3 * UWB API
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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.
23 * FIXME: doc: overview of the API, different parts and pointers
26 #ifndef __LINUX__UWB_H__
27 #define __LINUX__UWB_H__
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>
36 struct uwb_dev;
37 struct uwb_beca_e;
38 struct uwb_rc;
39 struct uwb_rsv;
40 struct uwb_dbg;
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.
55 * A UWB device may either by a neighbor or part of a local radio
56 * controller.
58 struct 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 */
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);
70 #define to_uwb_dev(d) container_of(d, struct uwb_dev, dev)
72 /**
73 * UWB HWA/WHCI Radio Control {Command|Event} Block context IDs
75 * RC[CE]Bs have a 'context ID' field that matches the command with
76 * the event received to confirm it.
78 * Maximum number of context IDs
80 enum { UWB_RC_CTX_MAX = 256 };
83 /** Notification chain head for UWB generated events to listeners */
84 struct uwb_notifs_chain {
85 struct list_head list;
86 struct mutex mutex;
89 /**
90 * struct uwb_mas_bm - a bitmap of all MAS in a superframe
91 * @bm: a bitmap of length #UWB_NUM_MAS
93 struct uwb_mas_bm {
94 DECLARE_BITMAP(bm, UWB_NUM_MAS);
97 /**
98 * uwb_rsv_state - UWB Reservation state.
100 * NONE - reservation is not active (no DRP IE being transmitted).
102 * Owner reservation states:
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.
110 * Target reservation states:
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.
117 * FIXME: further target states TBD.
119 enum 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,
129 UWB_RSV_STATE_LAST,
132 enum uwb_rsv_target_type {
133 UWB_RSV_TARGET_DEV,
134 UWB_RSV_TARGET_DEVADDR,
138 * struct uwb_rsv_target - the target of a reservation.
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).
144 struct uwb_rsv_target {
145 enum uwb_rsv_target_type type;
146 union {
147 struct uwb_dev *dev;
148 struct uwb_dev_addr devaddr;
153 * Number of streams reserved for reservations targeted at DevAddrs.
155 #define UWB_NUM_GLOBAL_STREAMS 1
157 typedef void (*uwb_rsv_cb_f)(struct uwb_rsv *rsv);
160 * struct uwb_rsv - a DRP reservation
162 * Data structure management:
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
169 * Owner and target parameters:
171 * @owner: the UWB device owning this reservation
172 * @target: the target UWB device
173 * @type: reservation type
175 * Owner parameters:
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
182 * @callback: callback function when the reservation completes
183 * @pal_priv: private data for the PAL making the reservation
185 * Reservation status:
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
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.
200 struct uwb_rsv {
201 struct uwb_rc *rc;
202 struct list_head rc_node;
203 struct list_head pal_node;
204 struct kref kref;
206 struct uwb_dev *owner;
207 struct uwb_rsv_target target;
208 enum uwb_drp_type type;
209 int max_mas;
210 int min_mas;
211 int sparsity;
212 bool is_multicast;
214 uwb_rsv_cb_f callback;
215 void *pal_priv;
217 enum uwb_rsv_state state;
218 u8 stream;
219 struct uwb_mas_bm mas;
220 struct uwb_ie_drp *drp_ie;
221 bool ie_valid;
222 struct timer_list timer;
223 bool expired;
226 static const
227 struct uwb_mas_bm uwb_mas_bm_zero = { .bm = { 0 } };
229 static inline void uwb_mas_bm_copy_le(void *dst, const struct uwb_mas_bm *mas)
231 bitmap_copy_le(dst, mas->bm, UWB_NUM_MAS);
235 * struct uwb_drp_avail - a radio controller's view of MAS usage
236 * @global: MAS unused by neighbors (excluding reservations targetted
237 * or owned by the local radio controller) or the beaon period
238 * @local: MAS unused by local established reservations
239 * @pending: MAS unused by local pending reservations
240 * @ie: DRP Availability IE to be included in the beacon
241 * @ie_valid: true iff @ie is valid and does not need to regenerated from
242 * @global and @local
244 * Each radio controller maintains a view of MAS usage or
245 * availability. MAS available for a new reservation are determined
246 * from the intersection of @global, @local, and @pending.
248 * The radio controller must transmit a DRP Availability IE that's the
249 * intersection of @global and @local.
251 * A set bit indicates the MAS is unused and available.
253 * rc->rsvs_mutex should be held before accessing this data structure.
255 * [ECMA-368] section 17.4.3.
257 struct uwb_drp_avail {
258 DECLARE_BITMAP(global, UWB_NUM_MAS);
259 DECLARE_BITMAP(local, UWB_NUM_MAS);
260 DECLARE_BITMAP(pending, UWB_NUM_MAS);
261 struct uwb_ie_drp_avail ie;
262 bool ie_valid;
266 const char *uwb_rsv_state_str(enum uwb_rsv_state state);
267 const char *uwb_rsv_type_str(enum uwb_drp_type type);
269 struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb,
270 void *pal_priv);
271 void uwb_rsv_destroy(struct uwb_rsv *rsv);
273 int uwb_rsv_establish(struct uwb_rsv *rsv);
274 int uwb_rsv_modify(struct uwb_rsv *rsv,
275 int max_mas, int min_mas, int sparsity);
276 void uwb_rsv_terminate(struct uwb_rsv *rsv);
278 void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv);
281 * Radio Control Interface instance
284 * Life cycle rules: those of the UWB Device.
286 * @index: an index number for this radio controller, as used in the
287 * device name.
288 * @version: version of protocol supported by this device
289 * @priv: Backend implementation; rw with uwb_dev.dev.sem taken.
290 * @cmd: Backend implementation to execute commands; rw and call
291 * only with uwb_dev.dev.sem taken.
292 * @reset: Hardware reset of radio controller and any PAL controllers.
293 * @filter: Backend implementation to manipulate data to and from device
294 * to be compliant to specification assumed by driver (WHCI
295 * 0.95).
297 * uwb_dev.dev.mutex is used to execute commands and update
298 * the corresponding structures; can't use a spinlock
299 * because rc->cmd() can sleep.
300 * @ies: This is a dynamically allocated array cacheing the
301 * IEs (settable by the host) that the beacon of this
302 * radio controller is currently sending.
304 * In reality, we store here the full command we set to
305 * the radio controller (which is basically a command
306 * prefix followed by all the IEs the beacon currently
307 * contains). This way we don't have to realloc and
308 * memcpy when setting it.
310 * We set this up in uwb_rc_ie_setup(), where we alloc
311 * this struct, call get_ie() [so we know which IEs are
312 * currently being sent, if any].
314 * @ies_capacity:Amount of space (in bytes) allocated in @ies. The
315 * amount used is given by sizeof(*ies) plus ies->wIELength
316 * (which is a little endian quantity all the time).
317 * @ies_mutex: protect the IE cache
318 * @dbg: information for the debug interface
320 struct uwb_rc {
321 struct uwb_dev uwb_dev;
322 int index;
323 u16 version;
325 struct module *owner;
326 void *priv;
327 int (*start)(struct uwb_rc *rc);
328 void (*stop)(struct uwb_rc *rc);
329 int (*cmd)(struct uwb_rc *, const struct uwb_rccb *, size_t);
330 int (*reset)(struct uwb_rc *rc);
331 int (*filter_cmd)(struct uwb_rc *, struct uwb_rccb **, size_t *);
332 int (*filter_event)(struct uwb_rc *, struct uwb_rceb **, const size_t,
333 size_t *, size_t *);
335 spinlock_t neh_lock; /* protects neh_* and ctx_* */
336 struct list_head neh_list; /* Open NE handles */
337 unsigned long ctx_bm[UWB_RC_CTX_MAX / 8 / sizeof(unsigned long)];
338 u8 ctx_roll;
340 int beaconing; /* Beaconing state [channel number] */
341 int scanning;
342 enum uwb_scan_type scan_type:3;
343 unsigned ready:1;
344 struct uwb_notifs_chain notifs_chain;
346 struct uwb_drp_avail drp_avail;
347 struct list_head reservations;
348 struct mutex rsvs_mutex;
349 struct workqueue_struct *rsv_workq;
350 struct work_struct rsv_update_work;
352 struct mutex ies_mutex;
353 struct uwb_rc_cmd_set_ie *ies;
354 size_t ies_capacity;
356 spinlock_t pal_lock;
357 struct list_head pals;
359 struct uwb_dbg *dbg;
364 * struct uwb_pal - a UWB PAL
365 * @name: descriptive name for this PAL (wushc, wlp, etc.).
366 * @device: a device for the PAL. Used to link the PAL and the radio
367 * controller in sysfs.
368 * @new_rsv: called when a peer requests a reservation (may be NULL if
369 * the PAL cannot accept reservation requests).
371 * A Protocol Adaptation Layer (PAL) is a user of the WiMedia UWB
372 * radio platform (e.g., WUSB, WLP or Bluetooth UWB AMP).
374 * The PALs using a radio controller must register themselves to
375 * permit the UWB stack to coordinate usage of the radio between the
376 * various PALs or to allow PALs to response to certain requests from
377 * peers.
379 * A struct uwb_pal should be embedded in a containing structure
380 * belonging to the PAL and initialized with uwb_pal_init()). Fields
381 * should be set appropriately by the PAL before registering the PAL
382 * with uwb_pal_register().
384 struct uwb_pal {
385 struct list_head node;
386 const char *name;
387 struct device *device;
388 void (*new_rsv)(struct uwb_rsv *rsv);
391 void uwb_pal_init(struct uwb_pal *pal);
392 int uwb_pal_register(struct uwb_rc *rc, struct uwb_pal *pal);
393 void uwb_pal_unregister(struct uwb_rc *rc, struct uwb_pal *pal);
396 * General public API
398 * This API can be used by UWB device drivers or by those implementing
399 * UWB Radio Controllers
401 struct uwb_dev *uwb_dev_get_by_devaddr(struct uwb_rc *rc,
402 const struct uwb_dev_addr *devaddr);
403 struct uwb_dev *uwb_dev_get_by_rc(struct uwb_dev *, struct uwb_rc *);
404 static inline void uwb_dev_get(struct uwb_dev *uwb_dev)
406 get_device(&uwb_dev->dev);
408 static inline void uwb_dev_put(struct uwb_dev *uwb_dev)
410 put_device(&uwb_dev->dev);
412 struct uwb_dev *uwb_dev_try_get(struct uwb_rc *rc, struct uwb_dev *uwb_dev);
415 * Callback function for 'uwb_{dev,rc}_foreach()'.
417 * @dev: Linux device instance
418 * 'uwb_dev = container_of(dev, struct uwb_dev, dev)'
419 * @priv: Data passed by the caller to 'uwb_{dev,rc}_foreach()'.
421 * @returns: 0 to continue the iterations, any other val to stop
422 * iterating and return the value to the caller of
423 * _foreach().
425 typedef int (*uwb_dev_for_each_f)(struct device *dev, void *priv);
426 int uwb_dev_for_each(struct uwb_rc *rc, uwb_dev_for_each_f func, void *priv);
428 struct uwb_rc *uwb_rc_alloc(void);
429 struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *);
430 struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *);
431 void uwb_rc_put(struct uwb_rc *rc);
433 typedef void (*uwb_rc_cmd_cb_f)(struct uwb_rc *rc, void *arg,
434 struct uwb_rceb *reply, ssize_t reply_size);
436 int uwb_rc_cmd_async(struct uwb_rc *rc, const char *cmd_name,
437 struct uwb_rccb *cmd, size_t cmd_size,
438 u8 expected_type, u16 expected_event,
439 uwb_rc_cmd_cb_f cb, void *arg);
440 ssize_t uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name,
441 struct uwb_rccb *cmd, size_t cmd_size,
442 struct uwb_rceb *reply, size_t reply_size);
443 ssize_t uwb_rc_vcmd(struct uwb_rc *rc, const char *cmd_name,
444 struct uwb_rccb *cmd, size_t cmd_size,
445 u8 expected_type, u16 expected_event,
446 struct uwb_rceb **preply);
447 int uwb_bg_joined(struct uwb_rc *rc);
449 size_t __uwb_addr_print(char *, size_t, const unsigned char *, int);
451 int uwb_rc_dev_addr_set(struct uwb_rc *, const struct uwb_dev_addr *);
452 int uwb_rc_dev_addr_get(struct uwb_rc *, struct uwb_dev_addr *);
453 int uwb_rc_mac_addr_set(struct uwb_rc *, const struct uwb_mac_addr *);
454 int uwb_rc_mac_addr_get(struct uwb_rc *, struct uwb_mac_addr *);
455 int __uwb_mac_addr_assigned_check(struct device *, void *);
456 int __uwb_dev_addr_assigned_check(struct device *, void *);
458 /* Print in @buf a pretty repr of @addr */
459 static inline size_t uwb_dev_addr_print(char *buf, size_t buf_size,
460 const struct uwb_dev_addr *addr)
462 return __uwb_addr_print(buf, buf_size, addr->data, 0);
465 /* Print in @buf a pretty repr of @addr */
466 static inline size_t uwb_mac_addr_print(char *buf, size_t buf_size,
467 const struct uwb_mac_addr *addr)
469 return __uwb_addr_print(buf, buf_size, addr->data, 1);
472 /* @returns 0 if device addresses @addr2 and @addr1 are equal */
473 static inline int uwb_dev_addr_cmp(const struct uwb_dev_addr *addr1,
474 const struct uwb_dev_addr *addr2)
476 return memcmp(addr1, addr2, sizeof(*addr1));
479 /* @returns 0 if MAC addresses @addr2 and @addr1 are equal */
480 static inline int uwb_mac_addr_cmp(const struct uwb_mac_addr *addr1,
481 const struct uwb_mac_addr *addr2)
483 return memcmp(addr1, addr2, sizeof(*addr1));
486 /* @returns !0 if a MAC @addr is a broadcast address */
487 static inline int uwb_mac_addr_bcast(const struct uwb_mac_addr *addr)
489 struct uwb_mac_addr bcast = {
490 .data = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
492 return !uwb_mac_addr_cmp(addr, &bcast);
495 /* @returns !0 if a MAC @addr is all zeroes*/
496 static inline int uwb_mac_addr_unset(const struct uwb_mac_addr *addr)
498 struct uwb_mac_addr unset = {
499 .data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
501 return !uwb_mac_addr_cmp(addr, &unset);
504 /* @returns !0 if the address is in use. */
505 static inline unsigned __uwb_dev_addr_assigned(struct uwb_rc *rc,
506 struct uwb_dev_addr *addr)
508 return uwb_dev_for_each(rc, __uwb_dev_addr_assigned_check, addr);
512 * UWB Radio Controller API
514 * This API is used (in addition to the general API) to implement UWB
515 * Radio Controllers.
517 void uwb_rc_init(struct uwb_rc *);
518 int uwb_rc_add(struct uwb_rc *, struct device *dev, void *rc_priv);
519 void uwb_rc_rm(struct uwb_rc *);
520 void uwb_rc_neh_grok(struct uwb_rc *, void *, size_t);
521 void uwb_rc_neh_error(struct uwb_rc *, int);
522 void uwb_rc_reset_all(struct uwb_rc *rc);
525 * uwb_rsv_is_owner - is the owner of this reservation the RC?
526 * @rsv: the reservation
528 static inline bool uwb_rsv_is_owner(struct uwb_rsv *rsv)
530 return rsv->owner == &rsv->rc->uwb_dev;
534 * Events generated by UWB that can be passed to any listeners
536 * Higher layers can register callback functions with the radio
537 * controller using uwb_notifs_register(). The radio controller
538 * maintains a list of all registered handlers and will notify all
539 * nodes when an event occurs.
541 enum uwb_notifs {
542 UWB_NOTIF_BG_JOIN = 0, /* radio controller joined a beacon group */
543 UWB_NOTIF_BG_LEAVE = 1, /* radio controller left a beacon group */
544 UWB_NOTIF_ONAIR,
545 UWB_NOTIF_OFFAIR,
548 /* Callback function registered with UWB */
549 struct uwb_notifs_handler {
550 struct list_head list_node;
551 void (*cb)(void *, struct uwb_dev *, enum uwb_notifs);
552 void *data;
555 int uwb_notifs_register(struct uwb_rc *, struct uwb_notifs_handler *);
556 int uwb_notifs_deregister(struct uwb_rc *, struct uwb_notifs_handler *);
560 * UWB radio controller Event Size Entry (for creating entry tables)
562 * WUSB and WHCI define events and notifications, and they might have
563 * fixed or variable size.
565 * Each event/notification has a size which is not necessarily known
566 * in advance based on the event code. As well, vendor specific
567 * events/notifications will have a size impossible to determine
568 * unless we know about the device's specific details.
570 * It was way too smart of the spec writers not to think that it would
571 * be impossible for a generic driver to skip over vendor specific
572 * events/notifications if there are no LENGTH fields in the HEADER of
573 * each message...the transaction size cannot be counted on as the
574 * spec does not forbid to pack more than one event in a single
575 * transaction.
577 * Thus, we guess sizes with tables (or for events, when you know the
578 * size ahead of time you can use uwb_rc_neh_extra_size*()). We
579 * register tables with the known events and their sizes, and then we
580 * traverse those tables. For those with variable length, we provide a
581 * way to lookup the size inside the event/notification's
582 * payload. This allows device-specific event size tables to be
583 * registered.
585 * @size: Size of the payload
587 * @offset: if != 0, at offset @offset-1 starts a field with a length
588 * that has to be added to @size. The format of the field is
589 * given by @type.
591 * @type: Type and length of the offset field. Most common is LE 16
592 * bits (that's why that is zero); others are there mostly to
593 * cover for bugs and weirdos.
595 struct uwb_est_entry {
596 size_t size;
597 unsigned offset;
598 enum { UWB_EST_16 = 0, UWB_EST_8 = 1 } type;
601 int uwb_est_register(u8 type, u8 code_high, u16 vendor, u16 product,
602 const struct uwb_est_entry *, size_t entries);
603 int uwb_est_unregister(u8 type, u8 code_high, u16 vendor, u16 product,
604 const struct uwb_est_entry *, size_t entries);
605 ssize_t uwb_est_find_size(struct uwb_rc *rc, const struct uwb_rceb *rceb,
606 size_t len);
608 /* -- Misc */
610 enum {
611 EDC_MAX_ERRORS = 10,
612 EDC_ERROR_TIMEFRAME = HZ,
615 /* error density counter */
616 struct edc {
617 unsigned long timestart;
618 u16 errorcount;
621 static inline
622 void edc_init(struct edc *edc)
624 edc->timestart = jiffies;
627 /* Called when an error occured.
628 * This is way to determine if the number of acceptable errors per time
629 * period has been exceeded. It is not accurate as there are cases in which
630 * this scheme will not work, for example if there are periodic occurences
631 * of errors that straddle updates to the start time. This scheme is
632 * sufficient for our usage.
634 * @returns 1 if maximum acceptable errors per timeframe has been exceeded.
636 static inline int edc_inc(struct edc *err_hist, u16 max_err, u16 timeframe)
638 unsigned long now;
640 now = jiffies;
641 if (now - err_hist->timestart > timeframe) {
642 err_hist->errorcount = 1;
643 err_hist->timestart = now;
644 } else if (++err_hist->errorcount > max_err) {
645 err_hist->errorcount = 0;
646 err_hist->timestart = now;
647 return 1;
649 return 0;
653 /* Information Element handling */
655 struct uwb_ie_hdr *uwb_ie_next(void **ptr, size_t *len);
656 int uwb_rc_ie_add(struct uwb_rc *uwb_rc, const struct uwb_ie_hdr *ies, size_t size);
657 int uwb_rc_ie_rm(struct uwb_rc *uwb_rc, enum uwb_ie element_id);
660 * Transmission statistics
662 * UWB uses LQI and RSSI (one byte values) for reporting radio signal
663 * strength and line quality indication. We do quick and dirty
664 * averages of those. They are signed values, btw.
666 * For 8 bit quantities, we keep the min, the max, an accumulator
667 * (@sigma) and a # of samples. When @samples gets to 255, we compute
668 * the average (@sigma / @samples), place it in @sigma and reset
669 * @samples to 1 (so we use it as the first sample).
671 * Now, statistically speaking, probably I am kicking the kidneys of
672 * some books I have in my shelves collecting dust, but I just want to
673 * get an approx, not the Nobel.
675 * LOCKING: there is no locking per se, but we try to keep a lockless
676 * schema. Only _add_samples() modifies the values--as long as you
677 * have other locking on top that makes sure that no two calls of
678 * _add_sample() happen at the same time, then we are fine. Now, for
679 * resetting the values we just set @samples to 0 and that makes the
680 * next _add_sample() to start with defaults. Reading the values in
681 * _show() currently can race, so you need to make sure the calls are
682 * under the same lock that protects calls to _add_sample(). FIXME:
683 * currently unlocked (It is not ultraprecise but does the trick. Bite
684 * me).
686 struct stats {
687 s8 min, max;
688 s16 sigma;
689 atomic_t samples;
692 static inline
693 void stats_init(struct stats *stats)
695 atomic_set(&stats->samples, 0);
696 wmb();
699 static inline
700 void stats_add_sample(struct stats *stats, s8 sample)
702 s8 min, max;
703 s16 sigma;
704 unsigned samples = atomic_read(&stats->samples);
705 if (samples == 0) { /* it was zero before, so we initialize */
706 min = 127;
707 max = -128;
708 sigma = 0;
709 } else {
710 min = stats->min;
711 max = stats->max;
712 sigma = stats->sigma;
715 if (sample < min) /* compute new values */
716 min = sample;
717 else if (sample > max)
718 max = sample;
719 sigma += sample;
721 stats->min = min; /* commit */
722 stats->max = max;
723 stats->sigma = sigma;
724 if (atomic_add_return(1, &stats->samples) > 255) {
725 /* wrapped around! reset */
726 stats->sigma = sigma / 256;
727 atomic_set(&stats->samples, 1);
731 static inline ssize_t stats_show(struct stats *stats, char *buf)
733 int min, max, avg;
734 int samples = atomic_read(&stats->samples);
735 if (samples == 0)
736 min = max = avg = 0;
737 else {
738 min = stats->min;
739 max = stats->max;
740 avg = stats->sigma / samples;
742 return scnprintf(buf, PAGE_SIZE, "%d %d %d\n", min, max, avg);
745 static inline ssize_t stats_store(struct stats *stats, const char *buf,
746 size_t size)
748 stats_init(stats);
749 return size;
752 #endif /* #ifndef __LINUX__UWB_H__ */