est.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Ultra Wide Band Radio Control
  3. * Event Size Tables management
  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: docs
  24. *
  25. * Infrastructure, code and data tables for guessing the size of
  26. * events received on the notification endpoints of UWB radio
  27. * controllers.
  28. *
  29. * You define a table of events and for each, its size and how to get
  30. * the extra size.
  31. *
  32. * ENTRY POINTS:
  33. *
  34. * uwb_est_{init/destroy}(): To initialize/release the EST subsystem.
  35. *
  36. * uwb_est_[u]register(): To un/register event size tables
  37. * uwb_est_grow()
  38. *
  39. * uwb_est_find_size(): Get the size of an event
  40. * uwb_est_get_size()
  41. */
  42. #include <linux/spinlock.h>
  43. #include "uwb-internal.h"
  44. struct uwb_est {
  45. u16 type_event_high;
  46. u16 vendor, product;
  47. u8 entries;
  48. const struct uwb_est_entry *entry;
  49. };
  50. static struct uwb_est *uwb_est;
  51. static u8 uwb_est_size;
  52. static u8 uwb_est_used;
  53. static DEFINE_RWLOCK(uwb_est_lock);
  54. /**
  55. * WUSB Standard Event Size Table, HWA-RC interface
  56. *
  57. * Sizes for events and notifications type 0 (general), high nibble 0.
  58. */
  59. static
  60. struct uwb_est_entry uwb_est_00_00xx[] = {
  61. [UWB_RC_EVT_IE_RCV] = {
  62. .size = sizeof(struct uwb_rc_evt_ie_rcv),
  63. .offset = 1 + offsetof(struct uwb_rc_evt_ie_rcv, wIELength),
  64. },
  65. [UWB_RC_EVT_BEACON] = {
  66. .size = sizeof(struct uwb_rc_evt_beacon),
  67. .offset = 1 + offsetof(struct uwb_rc_evt_beacon, wBeaconInfoLength),
  68. },
  69. [UWB_RC_EVT_BEACON_SIZE] = {
  70. .size = sizeof(struct uwb_rc_evt_beacon_size),
  71. },
  72. [UWB_RC_EVT_BPOIE_CHANGE] = {
  73. .size = sizeof(struct uwb_rc_evt_bpoie_change),
  74. .offset = 1 + offsetof(struct uwb_rc_evt_bpoie_change,
  75. wBPOIELength),
  76. },
  77. [UWB_RC_EVT_BP_SLOT_CHANGE] = {
  78. .size = sizeof(struct uwb_rc_evt_bp_slot_change),
  79. },
  80. [UWB_RC_EVT_BP_SWITCH_IE_RCV] = {
  81. .size = sizeof(struct uwb_rc_evt_bp_switch_ie_rcv),
  82. .offset = 1 + offsetof(struct uwb_rc_evt_bp_switch_ie_rcv, wIELength),
  83. },
  84. [UWB_RC_EVT_DEV_ADDR_CONFLICT] = {
  85. .size = sizeof(struct uwb_rc_evt_dev_addr_conflict),
  86. },
  87. [UWB_RC_EVT_DRP_AVAIL] = {
  88. .size = sizeof(struct uwb_rc_evt_drp_avail)
  89. },
  90. [UWB_RC_EVT_DRP] = {
  91. .size = sizeof(struct uwb_rc_evt_drp),
  92. .offset = 1 + offsetof(struct uwb_rc_evt_drp, ie_length),
  93. },
  94. [UWB_RC_EVT_BP_SWITCH_STATUS] = {
  95. .size = sizeof(struct uwb_rc_evt_bp_switch_status),
  96. },
  97. [UWB_RC_EVT_CMD_FRAME_RCV] = {
  98. .size = sizeof(struct uwb_rc_evt_cmd_frame_rcv),
  99. .offset = 1 + offsetof(struct uwb_rc_evt_cmd_frame_rcv, dataLength),
  100. },
  101. [UWB_RC_EVT_CHANNEL_CHANGE_IE_RCV] = {
  102. .size = sizeof(struct uwb_rc_evt_channel_change_ie_rcv),
  103. .offset = 1 + offsetof(struct uwb_rc_evt_channel_change_ie_rcv, wIELength),
  104. },
  105. [UWB_RC_CMD_CHANNEL_CHANGE] = {
  106. .size = sizeof(struct uwb_rc_evt_confirm),
  107. },
  108. [UWB_RC_CMD_DEV_ADDR_MGMT] = {
  109. .size = sizeof(struct uwb_rc_evt_dev_addr_mgmt) },
  110. [UWB_RC_CMD_GET_IE] = {
  111. .size = sizeof(struct uwb_rc_evt_get_ie),
  112. .offset = 1 + offsetof(struct uwb_rc_evt_get_ie, wIELength),
  113. },
  114. [UWB_RC_CMD_RESET] = {
  115. .size = sizeof(struct uwb_rc_evt_confirm),
  116. },
  117. [UWB_RC_CMD_SCAN] = {
  118. .size = sizeof(struct uwb_rc_evt_confirm),
  119. },
  120. [UWB_RC_CMD_SET_BEACON_FILTER] = {
  121. .size = sizeof(struct uwb_rc_evt_confirm),
  122. },
  123. [UWB_RC_CMD_SET_DRP_IE] = {
  124. .size = sizeof(struct uwb_rc_evt_set_drp_ie),
  125. },
  126. [UWB_RC_CMD_SET_IE] = {
  127. .size = sizeof(struct uwb_rc_evt_set_ie),
  128. },
  129. [UWB_RC_CMD_SET_NOTIFICATION_FILTER] = {
  130. .size = sizeof(struct uwb_rc_evt_confirm),
  131. },
  132. [UWB_RC_CMD_SET_TX_POWER] = {
  133. .size = sizeof(struct uwb_rc_evt_confirm),
  134. },
  135. [UWB_RC_CMD_SLEEP] = {
  136. .size = sizeof(struct uwb_rc_evt_confirm),
  137. },
  138. [UWB_RC_CMD_START_BEACON] = {
  139. .size = sizeof(struct uwb_rc_evt_confirm),
  140. },
  141. [UWB_RC_CMD_STOP_BEACON] = {
  142. .size = sizeof(struct uwb_rc_evt_confirm),
  143. },
  144. [UWB_RC_CMD_BP_MERGE] = {
  145. .size = sizeof(struct uwb_rc_evt_confirm),
  146. },
  147. [UWB_RC_CMD_SEND_COMMAND_FRAME] = {
  148. .size = sizeof(struct uwb_rc_evt_confirm),
  149. },
  150. [UWB_RC_CMD_SET_ASIE_NOTIF] = {
  151. .size = sizeof(struct uwb_rc_evt_confirm),
  152. },
  153. };
  154. static
  155. struct uwb_est_entry uwb_est_01_00xx[] = {
  156. [UWB_RC_DAA_ENERGY_DETECTED] = {
  157. .size = sizeof(struct uwb_rc_evt_daa_energy_detected),
  158. },
  159. [UWB_RC_SET_DAA_ENERGY_MASK] = {
  160. .size = sizeof(struct uwb_rc_evt_set_daa_energy_mask),
  161. },
  162. [UWB_RC_SET_NOTIFICATION_FILTER_EX] = {
  163. .size = sizeof(struct uwb_rc_evt_set_notification_filter_ex),
  164. },
  165. };
  166. /**
  167. * Initialize the EST subsystem
  168. *
  169. * Register the standard tables also.
  170. *
  171. * FIXME: tag init
  172. */
  173. int uwb_est_create(void)
  174. {
  175. int result;
  176. uwb_est_size = 2;
  177. uwb_est_used = 0;
  178. uwb_est = kzalloc(uwb_est_size * sizeof(uwb_est[0]), GFP_KERNEL);
  179. if (uwb_est == NULL)
  180. return -ENOMEM;
  181. result = uwb_est_register(UWB_RC_CET_GENERAL, 0, 0xffff, 0xffff,
  182. uwb_est_00_00xx, ARRAY_SIZE(uwb_est_00_00xx));
  183. if (result < 0)
  184. goto out;
  185. result = uwb_est_register(UWB_RC_CET_EX_TYPE_1, 0, 0xffff, 0xffff,
  186. uwb_est_01_00xx, ARRAY_SIZE(uwb_est_01_00xx));
  187. out:
  188. return result;
  189. }
  190. /** Clean it up */
  191. void uwb_est_destroy(void)
  192. {
  193. kfree(uwb_est);
  194. uwb_est = NULL;
  195. uwb_est_size = uwb_est_used = 0;
  196. }
  197. /**
  198. * Double the capacity of the EST table
  199. *
  200. * @returns 0 if ok, < 0 errno no error.
  201. */
  202. static
  203. int uwb_est_grow(void)
  204. {
  205. size_t actual_size = uwb_est_size * sizeof(uwb_est[0]);
  206. void *new = kmalloc(2 * actual_size, GFP_ATOMIC);
  207. if (new == NULL)
  208. return -ENOMEM;
  209. memcpy(new, uwb_est, actual_size);
  210. memset(new + actual_size, 0, actual_size);
  211. kfree(uwb_est);
  212. uwb_est = new;
  213. uwb_est_size *= 2;
  214. return 0;
  215. }
  216. /**
  217. * Register an event size table
  218. *
  219. * Makes room for it if the table is full, and then inserts it in the
  220. * right position (entries are sorted by type, event_high, vendor and
  221. * then product).
  222. *
  223. * @vendor: vendor code for matching against the device (0x0000 and
  224. * 0xffff mean any); use 0x0000 to force all to match without
  225. * checking possible vendor specific ones, 0xfffff to match
  226. * after checking vendor specific ones.
  227. *
  228. * @product: product code from that vendor; same matching rules, use
  229. * 0x0000 for not allowing vendor specific matches, 0xffff
  230. * for allowing.
  231. *
  232. * This arragement just makes the tables sort differenty. Because the
  233. * table is sorted by growing type-event_high-vendor-product, a zero
  234. * vendor will match before than a 0x456a vendor, that will match
  235. * before a 0xfffff vendor.
  236. *
  237. * @returns 0 if ok, < 0 errno on error (-ENOENT if not found).
  238. */
  239. /* FIXME: add bus type to vendor/product code */
  240. int uwb_est_register(u8 type, u8 event_high, u16 vendor, u16 product,
  241. const struct uwb_est_entry *entry, size_t entries)
  242. {
  243. unsigned long flags;
  244. unsigned itr;
  245. u16 type_event_high;
  246. int result = 0;
  247. write_lock_irqsave(&uwb_est_lock, flags);
  248. if (uwb_est_used == uwb_est_size) {
  249. result = uwb_est_grow();
  250. if (result < 0)
  251. goto out;
  252. }
  253. /* Find the right spot to insert it in */
  254. type_event_high = type << 8 | event_high;
  255. for (itr = 0; itr < uwb_est_used; itr++)
  256. if (uwb_est[itr].type_event_high < type
  257. && uwb_est[itr].vendor < vendor
  258. && uwb_est[itr].product < product)
  259. break;
  260. /* Shift others to make room for the new one? */
  261. if (itr < uwb_est_used)
  262. memmove(&uwb_est[itr+1], &uwb_est[itr], uwb_est_used - itr);
  263. uwb_est[itr].type_event_high = type << 8 | event_high;
  264. uwb_est[itr].vendor = vendor;
  265. uwb_est[itr].product = product;
  266. uwb_est[itr].entry = entry;
  267. uwb_est[itr].entries = entries;
  268. uwb_est_used++;
  269. out:
  270. write_unlock_irqrestore(&uwb_est_lock, flags);
  271. return result;
  272. }
  273. EXPORT_SYMBOL_GPL(uwb_est_register);
  274. /**
  275. * Unregister an event size table
  276. *
  277. * This just removes the specified entry and moves the ones after it
  278. * to fill in the gap. This is needed to keep the list sorted; no
  279. * reallocation is done to reduce the size of the table.
  280. *
  281. * We unregister by all the data we used to register instead of by
  282. * pointer to the @entry array because we might have used the same
  283. * table for a bunch of IDs (for example).
  284. *
  285. * @returns 0 if ok, < 0 errno on error (-ENOENT if not found).
  286. */
  287. int uwb_est_unregister(u8 type, u8 event_high, u16 vendor, u16 product,
  288. const struct uwb_est_entry *entry, size_t entries)
  289. {
  290. unsigned long flags;
  291. unsigned itr;
  292. struct uwb_est est_cmp = {
  293. .type_event_high = type << 8 | event_high,
  294. .vendor = vendor,
  295. .product = product,
  296. .entry = entry,
  297. .entries = entries
  298. };
  299. write_lock_irqsave(&uwb_est_lock, flags);
  300. for (itr = 0; itr < uwb_est_used; itr++)
  301. if (!memcmp(&uwb_est[itr], &est_cmp, sizeof(est_cmp)))
  302. goto found;
  303. write_unlock_irqrestore(&uwb_est_lock, flags);
  304. return -ENOENT;
  305. found:
  306. if (itr < uwb_est_used - 1) /* Not last one? move ones above */
  307. memmove(&uwb_est[itr], &uwb_est[itr+1], uwb_est_used - itr - 1);
  308. uwb_est_used--;
  309. write_unlock_irqrestore(&uwb_est_lock, flags);
  310. return 0;
  311. }
  312. EXPORT_SYMBOL_GPL(uwb_est_unregister);
  313. /**
  314. * Get the size of an event from a table
  315. *
  316. * @rceb: pointer to the buffer with the event
  317. * @rceb_size: size of the area pointed to by @rceb in bytes.
  318. * @returns: > 0 Size of the event
  319. * -ENOSPC An area big enough was not provided to look
  320. * ahead into the event's guts and guess the size.
  321. * -EINVAL Unknown event code (wEvent).
  322. *
  323. * This will look at the received RCEB and guess what is the total
  324. * size. For variable sized events, it will look further ahead into
  325. * their length field to see how much data should be read.
  326. *
  327. * Note this size is *not* final--the neh (Notification/Event Handle)
  328. * might specificy an extra size to add.
  329. */
  330. static
  331. ssize_t uwb_est_get_size(struct uwb_rc *uwb_rc, struct uwb_est *est,
  332. u8 event_low, const struct uwb_rceb *rceb,
  333. size_t rceb_size)
  334. {
  335. unsigned offset;
  336. ssize_t size;
  337. struct device *dev = &uwb_rc->uwb_dev.dev;
  338. const struct uwb_est_entry *entry;
  339. size = -ENOENT;
  340. if (event_low >= est->entries) { /* in range? */
  341. dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: event %u out of range\n",
  342. est, est->type_event_high, est->vendor, est->product,
  343. est->entries, event_low);
  344. goto out;
  345. }
  346. size = -ENOENT;
  347. entry = &est->entry[event_low];
  348. if (entry->size == 0 && entry->offset == 0) { /* unknown? */
  349. dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: event %u unknown\n",
  350. est, est->type_event_high, est->vendor, est->product,
  351. est->entries, event_low);
  352. goto out;
  353. }
  354. offset = entry->offset; /* extra fries with that? */
  355. if (offset == 0)
  356. size = entry->size;
  357. else {
  358. /* Ops, got an extra size field at 'offset'--read it */
  359. const void *ptr = rceb;
  360. size_t type_size = 0;
  361. offset--;
  362. size = -ENOSPC; /* enough data for more? */
  363. switch (entry->type) {
  364. case UWB_EST_16: type_size = sizeof(__le16); break;
  365. case UWB_EST_8: type_size = sizeof(u8); break;
  366. default: BUG();
  367. }
  368. if (offset + type_size > rceb_size) {
  369. dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: "
  370. "not enough data to read extra size\n",
  371. est, est->type_event_high, est->vendor,
  372. est->product, est->entries);
  373. goto out;
  374. }
  375. size = entry->size;
  376. ptr += offset;
  377. switch (entry->type) {
  378. case UWB_EST_16: size += le16_to_cpu(*(__le16 *)ptr); break;
  379. case UWB_EST_8: size += *(u8 *)ptr; break;
  380. default: BUG();
  381. }
  382. }
  383. out:
  384. return size;
  385. }
  386. /**
  387. * Guesses the size of a WA event
  388. *
  389. * @rceb: pointer to the buffer with the event
  390. * @rceb_size: size of the area pointed to by @rceb in bytes.
  391. * @returns: > 0 Size of the event
  392. * -ENOSPC An area big enough was not provided to look
  393. * ahead into the event's guts and guess the size.
  394. * -EINVAL Unknown event code (wEvent).
  395. *
  396. * This will look at the received RCEB and guess what is the total
  397. * size by checking all the tables registered with
  398. * uwb_est_register(). For variable sized events, it will look further
  399. * ahead into their length field to see how much data should be read.
  400. *
  401. * Note this size is *not* final--the neh (Notification/Event Handle)
  402. * might specificy an extra size to add or replace.
  403. */
  404. ssize_t uwb_est_find_size(struct uwb_rc *rc, const struct uwb_rceb *rceb,
  405. size_t rceb_size)
  406. {
  407. /* FIXME: add vendor/product data */
  408. ssize_t size;
  409. struct device *dev = &rc->uwb_dev.dev;
  410. unsigned long flags;
  411. unsigned itr;
  412. u16 type_event_high, event;
  413. u8 *ptr = (u8 *) rceb;
  414. read_lock_irqsave(&uwb_est_lock, flags);
  415. size = -ENOSPC;
  416. if (rceb_size < sizeof(*rceb))
  417. goto out;
  418. event = le16_to_cpu(rceb->wEvent);
  419. type_event_high = rceb->bEventType << 8 | (event & 0xff00) >> 8;
  420. for (itr = 0; itr < uwb_est_used; itr++) {
  421. if (uwb_est[itr].type_event_high != type_event_high)
  422. continue;
  423. size = uwb_est_get_size(rc, &uwb_est[itr],
  424. event & 0x00ff, rceb, rceb_size);
  425. /* try more tables that might handle the same type */
  426. if (size != -ENOENT)
  427. goto out;
  428. }
  429. dev_dbg(dev, "event 0x%02x/%04x/%02x: no handlers available; "
  430. "RCEB %02x %02x %02x %02x\n",
  431. (unsigned) rceb->bEventType,
  432. (unsigned) le16_to_cpu(rceb->wEvent),
  433. (unsigned) rceb->bEventContext,
  434. ptr[0], ptr[1], ptr[2], ptr[3]);
  435. size = -ENOENT;
  436. out:
  437. read_unlock_irqrestore(&uwb_est_lock, flags);
  438. return size;
  439. }
  440. EXPORT_SYMBOL_GPL(uwb_est_find_size);