uwb.h 24 KB

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