uwb.h 25 KB

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