uwb.h 23 KB

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