est.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 <linux/slab.h>
  44. #include <linux/export.h>
  45. #include "uwb-internal.h"
  46. struct uwb_est {
  47. u16 type_event_high;
  48. u16 vendor, product;
  49. u8 entries;
  50. const struct uwb_est_entry *entry;
  51. };
  52. static struct uwb_est *uwb_est;
  53. static u8 uwb_est_size;
  54. static u8 uwb_est_used;
  55. static DEFINE_RWLOCK(uwb_est_lock);
  56. /**
  57. * WUSB Standard Event Size Table, HWA-RC interface
  58. *
  59. * Sizes for events and notifications type 0 (general), high nibble 0.
  60. */
  61. static
  62. struct uwb_est_entry uwb_est_00_00xx[] = {
  63. [UWB_RC_EVT_IE_RCV] = {
  64. .size = sizeof(struct uwb_rc_evt_ie_rcv),
  65. .offset = 1 + offsetof(struct uwb_rc_evt_ie_rcv, wIELength),
  66. },
  67. [UWB_RC_EVT_BEACON] = {
  68. .size = sizeof(struct uwb_rc_evt_beacon),
  69. .offset = 1 + offsetof(struct uwb_rc_evt_beacon, wBeaconInfoLength),
  70. },
  71. [UWB_RC_EVT_BEACON_SIZE] = {
  72. .size = sizeof(struct uwb_rc_evt_beacon_size),
  73. },
  74. [UWB_RC_EVT_BPOIE_CHANGE] = {
  75. .size = sizeof(struct uwb_rc_evt_bpoie_change),
  76. .offset = 1 + offsetof(struct uwb_rc_evt_bpoie_change,
  77. wBPOIELength),
  78. },
  79. [UWB_RC_EVT_BP_SLOT_CHANGE] = {
  80. .size = sizeof(struct uwb_rc_evt_bp_slot_change),
  81. },
  82. [UWB_RC_EVT_BP_SWITCH_IE_RCV] = {
  83. .size = sizeof(struct uwb_rc_evt_bp_switch_ie_rcv),
  84. .offset = 1 + offsetof(struct uwb_rc_evt_bp_switch_ie_rcv, wIELength),
  85. },
  86. [UWB_RC_EVT_DEV_ADDR_CONFLICT] = {
  87. .size = sizeof(struct uwb_rc_evt_dev_addr_conflict),
  88. },
  89. [UWB_RC_EVT_DRP_AVAIL] = {
  90. .size = sizeof(struct uwb_rc_evt_drp_avail)
  91. },
  92. [UWB_RC_EVT_DRP] = {
  93. .size = sizeof(struct uwb_rc_evt_drp),
  94. .offset = 1 + offsetof(struct uwb_rc_evt_drp, ie_length),
  95. },
  96. [UWB_RC_EVT_BP_SWITCH_STATUS] = {
  97. .size = sizeof(struct uwb_rc_evt_bp_switch_status),
  98. },
  99. [UWB_RC_EVT_CMD_FRAME_RCV] = {
  100. .size = sizeof(struct uwb_rc_evt_cmd_frame_rcv),
  101. .offset = 1 + offsetof(struct uwb_rc_evt_cmd_frame_rcv, dataLength),
  102. },
  103. [UWB_RC_EVT_CHANNEL_CHANGE_IE_RCV] = {
  104. .size = sizeof(struct uwb_rc_evt_channel_change_ie_rcv),
  105. .offset = 1 + offsetof(struct uwb_rc_evt_channel_change_ie_rcv, wIELength),
  106. },
  107. [UWB_RC_CMD_CHANNEL_CHANGE] = {
  108. .size = sizeof(struct uwb_rc_evt_confirm),
  109. },
  110. [UWB_RC_CMD_DEV_ADDR_MGMT] = {
  111. .size = sizeof(struct uwb_rc_evt_dev_addr_mgmt) },
  112. [UWB_RC_CMD_GET_IE] = {
  113. .size = sizeof(struct uwb_rc_evt_get_ie),
  114. .offset = 1 + offsetof(struct uwb_rc_evt_get_ie, wIELength),
  115. },
  116. [UWB_RC_CMD_RESET] = {
  117. .size = sizeof(struct uwb_rc_evt_confirm),
  118. },
  119. [UWB_RC_CMD_SCAN] = {
  120. .size = sizeof(struct uwb_rc_evt_confirm),
  121. },
  122. [UWB_RC_CMD_SET_BEACON_FILTER] = {
  123. .size = sizeof(struct uwb_rc_evt_confirm),
  124. },
  125. [UWB_RC_CMD_SET_DRP_IE] = {
  126. .size = sizeof(struct uwb_rc_evt_set_drp_ie),
  127. },
  128. [UWB_RC_CMD_SET_IE] = {
  129. .size = sizeof(struct uwb_rc_evt_set_ie),
  130. },
  131. [UWB_RC_CMD_SET_NOTIFICATION_FILTER] = {
  132. .size = sizeof(struct uwb_rc_evt_confirm),
  133. },
  134. [UWB_RC_CMD_SET_TX_POWER] = {
  135. .size = sizeof(struct uwb_rc_evt_confirm),
  136. },
  137. [UWB_RC_CMD_SLEEP] = {
  138. .size = sizeof(struct uwb_rc_evt_confirm),
  139. },
  140. [UWB_RC_CMD_START_BEACON] = {
  141. .size = sizeof(struct uwb_rc_evt_confirm),
  142. },
  143. [UWB_RC_CMD_STOP_BEACON] = {
  144. .size = sizeof(struct uwb_rc_evt_confirm),
  145. },
  146. [UWB_RC_CMD_BP_MERGE] = {
  147. .size = sizeof(struct uwb_rc_evt_confirm),
  148. },
  149. [UWB_RC_CMD_SEND_COMMAND_FRAME] = {
  150. .size = sizeof(struct uwb_rc_evt_confirm),
  151. },
  152. [UWB_RC_CMD_SET_ASIE_NOTIF] = {
  153. .size = sizeof(struct uwb_rc_evt_confirm),
  154. },
  155. };
  156. static
  157. struct uwb_est_entry uwb_est_01_00xx[] = {
  158. [UWB_RC_DAA_ENERGY_DETECTED] = {
  159. .size = sizeof(struct uwb_rc_evt_daa_energy_detected),
  160. },
  161. [UWB_RC_SET_DAA_ENERGY_MASK] = {
  162. .size = sizeof(struct uwb_rc_evt_set_daa_energy_mask),
  163. },
  164. [UWB_RC_SET_NOTIFICATION_FILTER_EX] = {
  165. .size = sizeof(struct uwb_rc_evt_set_notification_filter_ex),
  166. },
  167. };
  168. /**
  169. * Initialize the EST subsystem
  170. *
  171. * Register the standard tables also.
  172. *
  173. * FIXME: tag init
  174. */
  175. int uwb_est_create(void)
  176. {
  177. int result;
  178. uwb_est_size = 2;
  179. uwb_est_used = 0;
  180. uwb_est = kcalloc(uwb_est_size, sizeof(uwb_est[0]), GFP_KERNEL);
  181. if (uwb_est == NULL)
  182. return -ENOMEM;
  183. result = uwb_est_register(UWB_RC_CET_GENERAL, 0, 0xffff, 0xffff,
  184. uwb_est_00_00xx, ARRAY_SIZE(uwb_est_00_00xx));
  185. if (result < 0)
  186. goto out;
  187. result = uwb_est_register(UWB_RC_CET_EX_TYPE_1, 0, 0xffff, 0xffff,
  188. uwb_est_01_00xx, ARRAY_SIZE(uwb_est_01_00xx));
  189. out:
  190. return result;
  191. }
  192. /** Clean it up */
  193. void uwb_est_destroy(void)
  194. {
  195. kfree(uwb_est);
  196. uwb_est = NULL;
  197. uwb_est_size = uwb_est_used = 0;
  198. }
  199. /**
  200. * Double the capacity of the EST table
  201. *
  202. * @returns 0 if ok, < 0 errno no error.
  203. */
  204. static
  205. int uwb_est_grow(void)
  206. {
  207. size_t actual_size = uwb_est_size * sizeof(uwb_est[0]);
  208. void *new = kmalloc(2 * actual_size, GFP_ATOMIC);
  209. if (new == NULL)
  210. return -ENOMEM;
  211. memcpy(new, uwb_est, actual_size);
  212. memset(new + actual_size, 0, actual_size);
  213. kfree(uwb_est);
  214. uwb_est = new;
  215. uwb_est_size *= 2;
  216. return 0;
  217. }
  218. /**
  219. * Register an event size table
  220. *
  221. * Makes room for it if the table is full, and then inserts it in the
  222. * right position (entries are sorted by type, event_high, vendor and
  223. * then product).
  224. *
  225. * @vendor: vendor code for matching against the device (0x0000 and
  226. * 0xffff mean any); use 0x0000 to force all to match without
  227. * checking possible vendor specific ones, 0xfffff to match
  228. * after checking vendor specific ones.
  229. *
  230. * @product: product code from that vendor; same matching rules, use
  231. * 0x0000 for not allowing vendor specific matches, 0xffff
  232. * for allowing.
  233. *
  234. * This arragement just makes the tables sort differenty. Because the
  235. * table is sorted by growing type-event_high-vendor-product, a zero
  236. * vendor will match before than a 0x456a vendor, that will match
  237. * before a 0xfffff vendor.
  238. *
  239. * @returns 0 if ok, < 0 errno on error (-ENOENT if not found).
  240. */
  241. /* FIXME: add bus type to vendor/product code */
  242. int uwb_est_register(u8 type, u8 event_high, u16 vendor, u16 product,
  243. const struct uwb_est_entry *entry, size_t entries)
  244. {
  245. unsigned long flags;
  246. unsigned itr;
  247. int result = 0;
  248. write_lock_irqsave(&uwb_est_lock, flags);
  249. if (uwb_est_used == uwb_est_size) {
  250. result = uwb_est_grow();
  251. if (result < 0)
  252. goto out;
  253. }
  254. /* Find the right spot to insert it in */
  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. read_lock_irqsave(&uwb_est_lock, flags);
  414. size = -ENOSPC;
  415. if (rceb_size < sizeof(*rceb))
  416. goto out;
  417. event = le16_to_cpu(rceb->wEvent);
  418. type_event_high = rceb->bEventType << 8 | (event & 0xff00) >> 8;
  419. for (itr = 0; itr < uwb_est_used; itr++) {
  420. if (uwb_est[itr].type_event_high != type_event_high)
  421. continue;
  422. size = uwb_est_get_size(rc, &uwb_est[itr],
  423. event & 0x00ff, rceb, rceb_size);
  424. /* try more tables that might handle the same type */
  425. if (size != -ENOENT)
  426. goto out;
  427. }
  428. dev_dbg(dev,
  429. "event 0x%02x/%04x/%02x: no handlers available; RCEB %4ph\n",
  430. (unsigned) rceb->bEventType,
  431. (unsigned) le16_to_cpu(rceb->wEvent),
  432. (unsigned) rceb->bEventContext,
  433. rceb);
  434. size = -ENOENT;
  435. out:
  436. read_unlock_irqrestore(&uwb_est_lock, flags);
  437. return size;
  438. }
  439. EXPORT_SYMBOL_GPL(uwb_est_find_size);