uwb.h 25 KB

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