uwb.h 23 KB

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