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