hwa-rc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. * WUSB Host Wire Adapter: Radio Control Interface (WUSB[8.6])
  3. * Radio Control command/event transport
  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. * Initialize the Radio Control interface Driver.
  24. *
  25. * For each device probed, creates an 'struct hwarc' which contains
  26. * just the representation of the UWB Radio Controller, and the logic
  27. * for reading notifications and passing them to the UWB Core.
  28. *
  29. * So we initialize all of those, register the UWB Radio Controller
  30. * and setup the notification/event handle to pipe the notifications
  31. * to the UWB management Daemon.
  32. *
  33. * Command and event filtering.
  34. *
  35. * This is the driver for the Radio Control Interface described in WUSB
  36. * 1.0. The core UWB module assumes that all drivers are compliant to the
  37. * WHCI 0.95 specification. We thus create a filter that parses all
  38. * incoming messages from the (WUSB 1.0) device and manipulate them to
  39. * conform to the WHCI 0.95 specification. Similarly, outgoing messages
  40. * are parsed and manipulated to conform to the WUSB 1.0 compliant messages
  41. * that the device expects. Only a few messages are affected:
  42. * Affected events:
  43. * UWB_RC_EVT_BEACON
  44. * UWB_RC_EVT_BP_SLOT_CHANGE
  45. * UWB_RC_EVT_DRP_AVAIL
  46. * UWB_RC_EVT_DRP
  47. * Affected commands:
  48. * UWB_RC_CMD_SCAN
  49. * UWB_RC_CMD_SET_DRP_IE
  50. *
  51. *
  52. *
  53. */
  54. #include <linux/init.h>
  55. #include <linux/module.h>
  56. #include <linux/slab.h>
  57. #include <linux/usb.h>
  58. #include <linux/usb/wusb.h>
  59. #include <linux/usb/wusb-wa.h>
  60. #include <linux/uwb.h>
  61. #include "uwb-internal.h"
  62. /* The device uses commands and events from the WHCI specification, although
  63. * reporting itself as WUSB compliant. */
  64. #define WUSB_QUIRK_WHCI_CMD_EVT 0x01
  65. /**
  66. * Descriptor for an instance of the UWB Radio Control Driver that
  67. * attaches to the RCI interface of the Host Wired Adapter.
  68. *
  69. * Unless there is a lock specific to the 'data members', all access
  70. * is protected by uwb_rc->mutex.
  71. *
  72. * The NEEP (Notification/Event EndPoint) URB (@neep_urb) writes to
  73. * @rd_buffer. Note there is no locking because it is perfectly (heh!)
  74. * serialized--probe() submits an URB, callback is called, processes
  75. * the data (synchronously), submits another URB, and so on. There is
  76. * no concurrent access to the buffer.
  77. */
  78. struct hwarc {
  79. struct usb_device *usb_dev;
  80. struct usb_interface *usb_iface;
  81. struct uwb_rc *uwb_rc; /* UWB host controller */
  82. struct urb *neep_urb; /* Notification endpoint handling */
  83. struct edc neep_edc;
  84. void *rd_buffer; /* NEEP read buffer */
  85. };
  86. /* Beacon received notification (WUSB 1.0 [8.6.3.2]) */
  87. struct uwb_rc_evt_beacon_WUSB_0100 {
  88. struct uwb_rceb rceb;
  89. u8 bChannelNumber;
  90. __le16 wBPSTOffset;
  91. u8 bLQI;
  92. u8 bRSSI;
  93. __le16 wBeaconInfoLength;
  94. u8 BeaconInfo[];
  95. } __attribute__((packed));
  96. /**
  97. * Filter WUSB 1.0 BEACON RCV notification to be WHCI 0.95
  98. *
  99. * @header: the incoming event
  100. * @buf_size: size of buffer containing incoming event
  101. * @new_size: size of event after filtering completed
  102. *
  103. * The WHCI 0.95 spec has a "Beacon Type" field. This value is unknown at
  104. * the time we receive the beacon from WUSB so we just set it to
  105. * UWB_RC_BEACON_TYPE_NEIGHBOR as a default.
  106. * The solution below allocates memory upon receipt of every beacon from a
  107. * WUSB device. This will deteriorate performance. What is the right way to
  108. * do this?
  109. */
  110. static
  111. int hwarc_filter_evt_beacon_WUSB_0100(struct uwb_rc *rc,
  112. struct uwb_rceb **header,
  113. const size_t buf_size,
  114. size_t *new_size)
  115. {
  116. struct uwb_rc_evt_beacon_WUSB_0100 *be;
  117. struct uwb_rc_evt_beacon *newbe;
  118. size_t bytes_left, ielength;
  119. struct device *dev = &rc->uwb_dev.dev;
  120. be = container_of(*header, struct uwb_rc_evt_beacon_WUSB_0100, rceb);
  121. bytes_left = buf_size;
  122. if (bytes_left < sizeof(*be)) {
  123. dev_err(dev, "Beacon Received Notification: Not enough data "
  124. "to decode for filtering (%zu vs %zu bytes needed)\n",
  125. bytes_left, sizeof(*be));
  126. return -EINVAL;
  127. }
  128. bytes_left -= sizeof(*be);
  129. ielength = le16_to_cpu(be->wBeaconInfoLength);
  130. if (bytes_left < ielength) {
  131. dev_err(dev, "Beacon Received Notification: Not enough data "
  132. "to decode IEs (%zu vs %zu bytes needed)\n",
  133. bytes_left, ielength);
  134. return -EINVAL;
  135. }
  136. newbe = kzalloc(sizeof(*newbe) + ielength, GFP_ATOMIC);
  137. if (newbe == NULL)
  138. return -ENOMEM;
  139. newbe->rceb = be->rceb;
  140. newbe->bChannelNumber = be->bChannelNumber;
  141. newbe->bBeaconType = UWB_RC_BEACON_TYPE_NEIGHBOR;
  142. newbe->wBPSTOffset = be->wBPSTOffset;
  143. newbe->bLQI = be->bLQI;
  144. newbe->bRSSI = be->bRSSI;
  145. newbe->wBeaconInfoLength = be->wBeaconInfoLength;
  146. memcpy(newbe->BeaconInfo, be->BeaconInfo, ielength);
  147. *header = &newbe->rceb;
  148. *new_size = sizeof(*newbe) + ielength;
  149. return 1; /* calling function will free memory */
  150. }
  151. /* DRP Availability change notification (WUSB 1.0 [8.6.3.8]) */
  152. struct uwb_rc_evt_drp_avail_WUSB_0100 {
  153. struct uwb_rceb rceb;
  154. __le16 wIELength;
  155. u8 IEData[];
  156. } __attribute__((packed));
  157. /**
  158. * Filter WUSB 1.0 DRP AVAILABILITY CHANGE notification to be WHCI 0.95
  159. *
  160. * @header: the incoming event
  161. * @buf_size: size of buffer containing incoming event
  162. * @new_size: size of event after filtering completed
  163. */
  164. static
  165. int hwarc_filter_evt_drp_avail_WUSB_0100(struct uwb_rc *rc,
  166. struct uwb_rceb **header,
  167. const size_t buf_size,
  168. size_t *new_size)
  169. {
  170. struct uwb_rc_evt_drp_avail_WUSB_0100 *da;
  171. struct uwb_rc_evt_drp_avail *newda;
  172. struct uwb_ie_hdr *ie_hdr;
  173. size_t bytes_left, ielength;
  174. struct device *dev = &rc->uwb_dev.dev;
  175. da = container_of(*header, struct uwb_rc_evt_drp_avail_WUSB_0100, rceb);
  176. bytes_left = buf_size;
  177. if (bytes_left < sizeof(*da)) {
  178. dev_err(dev, "Not enough data to decode DRP Avail "
  179. "Notification for filtering. Expected %zu, "
  180. "received %zu.\n", (size_t)sizeof(*da), bytes_left);
  181. return -EINVAL;
  182. }
  183. bytes_left -= sizeof(*da);
  184. ielength = le16_to_cpu(da->wIELength);
  185. if (bytes_left < ielength) {
  186. dev_err(dev, "DRP Avail Notification filter: IE length "
  187. "[%zu bytes] does not match actual length "
  188. "[%zu bytes].\n", ielength, bytes_left);
  189. return -EINVAL;
  190. }
  191. if (ielength < sizeof(*ie_hdr)) {
  192. dev_err(dev, "DRP Avail Notification filter: Not enough "
  193. "data to decode IE [%zu bytes, %zu needed]\n",
  194. ielength, sizeof(*ie_hdr));
  195. return -EINVAL;
  196. }
  197. ie_hdr = (void *) da->IEData;
  198. if (ie_hdr->length > 32) {
  199. dev_err(dev, "DRP Availability Change event has unexpected "
  200. "length for filtering. Expected < 32 bytes, "
  201. "got %zu bytes.\n", (size_t)ie_hdr->length);
  202. return -EINVAL;
  203. }
  204. newda = kzalloc(sizeof(*newda), GFP_ATOMIC);
  205. if (newda == NULL)
  206. return -ENOMEM;
  207. newda->rceb = da->rceb;
  208. memcpy(newda->bmp, (u8 *) ie_hdr + sizeof(*ie_hdr), ie_hdr->length);
  209. *header = &newda->rceb;
  210. *new_size = sizeof(*newda);
  211. return 1; /* calling function will free memory */
  212. }
  213. /* DRP notification (WUSB 1.0 [8.6.3.9]) */
  214. struct uwb_rc_evt_drp_WUSB_0100 {
  215. struct uwb_rceb rceb;
  216. struct uwb_dev_addr wSrcAddr;
  217. u8 bExplicit;
  218. __le16 wIELength;
  219. u8 IEData[];
  220. } __attribute__((packed));
  221. /**
  222. * Filter WUSB 1.0 DRP Notification to be WHCI 0.95
  223. *
  224. * @header: the incoming event
  225. * @buf_size: size of buffer containing incoming event
  226. * @new_size: size of event after filtering completed
  227. *
  228. * It is hard to manage DRP reservations without having a Reason code.
  229. * Unfortunately there is none in the WUSB spec. We just set the default to
  230. * DRP IE RECEIVED.
  231. * We do not currently use the bBeaconSlotNumber value, so we set this to
  232. * zero for now.
  233. */
  234. static
  235. int hwarc_filter_evt_drp_WUSB_0100(struct uwb_rc *rc,
  236. struct uwb_rceb **header,
  237. const size_t buf_size,
  238. size_t *new_size)
  239. {
  240. struct uwb_rc_evt_drp_WUSB_0100 *drpev;
  241. struct uwb_rc_evt_drp *newdrpev;
  242. size_t bytes_left, ielength;
  243. struct device *dev = &rc->uwb_dev.dev;
  244. drpev = container_of(*header, struct uwb_rc_evt_drp_WUSB_0100, rceb);
  245. bytes_left = buf_size;
  246. if (bytes_left < sizeof(*drpev)) {
  247. dev_err(dev, "Not enough data to decode DRP Notification "
  248. "for filtering. Expected %zu, received %zu.\n",
  249. (size_t)sizeof(*drpev), bytes_left);
  250. return -EINVAL;
  251. }
  252. ielength = le16_to_cpu(drpev->wIELength);
  253. bytes_left -= sizeof(*drpev);
  254. if (bytes_left < ielength) {
  255. dev_err(dev, "DRP Notification filter: header length [%zu "
  256. "bytes] does not match actual length [%zu "
  257. "bytes].\n", ielength, bytes_left);
  258. return -EINVAL;
  259. }
  260. newdrpev = kzalloc(sizeof(*newdrpev) + ielength, GFP_ATOMIC);
  261. if (newdrpev == NULL)
  262. return -ENOMEM;
  263. newdrpev->rceb = drpev->rceb;
  264. newdrpev->src_addr = drpev->wSrcAddr;
  265. newdrpev->reason = UWB_DRP_NOTIF_DRP_IE_RCVD;
  266. newdrpev->beacon_slot_number = 0;
  267. newdrpev->ie_length = drpev->wIELength;
  268. memcpy(newdrpev->ie_data, drpev->IEData, ielength);
  269. *header = &newdrpev->rceb;
  270. *new_size = sizeof(*newdrpev) + ielength;
  271. return 1; /* calling function will free memory */
  272. }
  273. /* Scan Command (WUSB 1.0 [8.6.2.5]) */
  274. struct uwb_rc_cmd_scan_WUSB_0100 {
  275. struct uwb_rccb rccb;
  276. u8 bChannelNumber;
  277. u8 bScanState;
  278. } __attribute__((packed));
  279. /**
  280. * Filter WHCI 0.95 SCAN command to be WUSB 1.0 SCAN command
  281. *
  282. * @header: command sent to device (compliant to WHCI 0.95)
  283. * @size: size of command sent to device
  284. *
  285. * We only reduce the size by two bytes because the WUSB 1.0 scan command
  286. * does not have the last field (wStarttime). Also, make sure we don't send
  287. * the device an unexpected scan type.
  288. */
  289. static
  290. int hwarc_filter_cmd_scan_WUSB_0100(struct uwb_rc *rc,
  291. struct uwb_rccb **header,
  292. size_t *size)
  293. {
  294. struct uwb_rc_cmd_scan *sc;
  295. sc = container_of(*header, struct uwb_rc_cmd_scan, rccb);
  296. if (sc->bScanState == UWB_SCAN_ONLY_STARTTIME)
  297. sc->bScanState = UWB_SCAN_ONLY;
  298. /* Don't send the last two bytes. */
  299. *size -= 2;
  300. return 0;
  301. }
  302. /* SET DRP IE command (WUSB 1.0 [8.6.2.7]) */
  303. struct uwb_rc_cmd_set_drp_ie_WUSB_0100 {
  304. struct uwb_rccb rccb;
  305. u8 bExplicit;
  306. __le16 wIELength;
  307. struct uwb_ie_drp IEData[];
  308. } __attribute__((packed));
  309. /**
  310. * Filter WHCI 0.95 SET DRP IE command to be WUSB 1.0 SET DRP IE command
  311. *
  312. * @header: command sent to device (compliant to WHCI 0.95)
  313. * @size: size of command sent to device
  314. *
  315. * WUSB has an extra bExplicit field - we assume always explicit
  316. * negotiation so this field is set. The command expected by the device is
  317. * thus larger than the one prepared by the driver so we need to
  318. * reallocate memory to accommodate this.
  319. * We trust the driver to send us the correct data so no checking is done
  320. * on incoming data - evn though it is variable length.
  321. */
  322. static
  323. int hwarc_filter_cmd_set_drp_ie_WUSB_0100(struct uwb_rc *rc,
  324. struct uwb_rccb **header,
  325. size_t *size)
  326. {
  327. struct uwb_rc_cmd_set_drp_ie *orgcmd;
  328. struct uwb_rc_cmd_set_drp_ie_WUSB_0100 *cmd;
  329. size_t ielength;
  330. orgcmd = container_of(*header, struct uwb_rc_cmd_set_drp_ie, rccb);
  331. ielength = le16_to_cpu(orgcmd->wIELength);
  332. cmd = kzalloc(sizeof(*cmd) + ielength, GFP_KERNEL);
  333. if (cmd == NULL)
  334. return -ENOMEM;
  335. cmd->rccb = orgcmd->rccb;
  336. cmd->bExplicit = 0;
  337. cmd->wIELength = orgcmd->wIELength;
  338. memcpy(cmd->IEData, orgcmd->IEData, ielength);
  339. *header = &cmd->rccb;
  340. *size = sizeof(*cmd) + ielength;
  341. return 1; /* calling function will free memory */
  342. }
  343. /**
  344. * Filter data from WHCI driver to WUSB device
  345. *
  346. * @header: WHCI 0.95 compliant command from driver
  347. * @size: length of command
  348. *
  349. * The routine managing commands to the device (uwb_rc_cmd()) will call the
  350. * filtering function pointer (if it exists) before it passes any data to
  351. * the device. At this time the command has been formatted according to
  352. * WHCI 0.95 and is ready to be sent to the device.
  353. *
  354. * The filter function will be provided with the current command and its
  355. * length. The function will manipulate the command if necessary and
  356. * potentially reallocate memory for a command that needed more memory that
  357. * the given command. If new memory was created the function will return 1
  358. * to indicate to the calling function that the memory need to be freed
  359. * when not needed any more. The size will contain the new length of the
  360. * command.
  361. * If memory has not been allocated we rely on the original mechanisms to
  362. * free the memory of the command - even when we reduce the value of size.
  363. */
  364. static
  365. int hwarc_filter_cmd_WUSB_0100(struct uwb_rc *rc, struct uwb_rccb **header,
  366. size_t *size)
  367. {
  368. int result;
  369. struct uwb_rccb *rccb = *header;
  370. int cmd = le16_to_cpu(rccb->wCommand);
  371. switch (cmd) {
  372. case UWB_RC_CMD_SCAN:
  373. result = hwarc_filter_cmd_scan_WUSB_0100(rc, header, size);
  374. break;
  375. case UWB_RC_CMD_SET_DRP_IE:
  376. result = hwarc_filter_cmd_set_drp_ie_WUSB_0100(rc, header, size);
  377. break;
  378. default:
  379. result = -ENOANO;
  380. break;
  381. }
  382. return result;
  383. }
  384. /**
  385. * Filter data from WHCI driver to WUSB device
  386. *
  387. * @header: WHCI 0.95 compliant command from driver
  388. * @size: length of command
  389. *
  390. * Filter commands based on which protocol the device supports. The WUSB
  391. * errata should be the same as WHCI 0.95 so we do not filter that here -
  392. * only WUSB 1.0.
  393. */
  394. static
  395. int hwarc_filter_cmd(struct uwb_rc *rc, struct uwb_rccb **header,
  396. size_t *size)
  397. {
  398. int result = -ENOANO;
  399. if (rc->version == 0x0100)
  400. result = hwarc_filter_cmd_WUSB_0100(rc, header, size);
  401. return result;
  402. }
  403. /**
  404. * Compute return value as sum of incoming value and value at given offset
  405. *
  406. * @rceb: event for which we compute the size, it contains a variable
  407. * length field.
  408. * @core_size: size of the "non variable" part of the event
  409. * @offset: place in event where the length of the variable part is stored
  410. * @buf_size: total length of buffer in which event arrived - we need to make
  411. * sure we read the offset in memory that is still part of the event
  412. */
  413. static
  414. ssize_t hwarc_get_event_size(struct uwb_rc *rc, const struct uwb_rceb *rceb,
  415. size_t core_size, size_t offset,
  416. const size_t buf_size)
  417. {
  418. ssize_t size = -ENOSPC;
  419. const void *ptr = rceb;
  420. size_t type_size = sizeof(__le16);
  421. struct device *dev = &rc->uwb_dev.dev;
  422. if (offset + type_size >= buf_size) {
  423. dev_err(dev, "Not enough data to read extra size of event "
  424. "0x%02x/%04x/%02x, only got %zu bytes.\n",
  425. rceb->bEventType, le16_to_cpu(rceb->wEvent),
  426. rceb->bEventContext, buf_size);
  427. goto out;
  428. }
  429. ptr += offset;
  430. size = core_size + le16_to_cpu(*(__le16 *)ptr);
  431. out:
  432. return size;
  433. }
  434. /* Beacon slot change notification (WUSB 1.0 [8.6.3.5]) */
  435. struct uwb_rc_evt_bp_slot_change_WUSB_0100 {
  436. struct uwb_rceb rceb;
  437. u8 bSlotNumber;
  438. } __attribute__((packed));
  439. /**
  440. * Filter data from WUSB device to WHCI driver
  441. *
  442. * @header: incoming event
  443. * @buf_size: size of buffer in which event arrived
  444. * @_event_size: actual size of event in the buffer
  445. * @new_size: size of event after filtered
  446. *
  447. * We don't know how the buffer is constructed - there may be more than one
  448. * event in it so buffer length does not determine event length. We first
  449. * determine the expected size of the incoming event. This value is passed
  450. * back only if the actual filtering succeeded (so we know the computed
  451. * expected size is correct). This value will be zero if
  452. * the event did not need any filtering.
  453. *
  454. * WHCI interprets the BP Slot Change event's data differently than
  455. * WUSB. The event sizes are exactly the same. The data field
  456. * indicates the new beacon slot in which a RC is transmitting its
  457. * beacon. The maximum value of this is 96 (wMacBPLength ECMA-368
  458. * 17.16 (Table 117)). We thus know that the WUSB value will not set
  459. * the bit bNoSlot, so we don't really do anything (placeholder).
  460. */
  461. static
  462. int hwarc_filter_event_WUSB_0100(struct uwb_rc *rc, struct uwb_rceb **header,
  463. const size_t buf_size, size_t *_real_size,
  464. size_t *_new_size)
  465. {
  466. int result = -ENOANO;
  467. struct uwb_rceb *rceb = *header;
  468. int event = le16_to_cpu(rceb->wEvent);
  469. ssize_t event_size;
  470. size_t core_size, offset;
  471. if (rceb->bEventType != UWB_RC_CET_GENERAL)
  472. goto out;
  473. switch (event) {
  474. case UWB_RC_EVT_BEACON:
  475. core_size = sizeof(struct uwb_rc_evt_beacon_WUSB_0100);
  476. offset = offsetof(struct uwb_rc_evt_beacon_WUSB_0100,
  477. wBeaconInfoLength);
  478. event_size = hwarc_get_event_size(rc, rceb, core_size,
  479. offset, buf_size);
  480. if (event_size < 0)
  481. goto out;
  482. *_real_size = event_size;
  483. result = hwarc_filter_evt_beacon_WUSB_0100(rc, header,
  484. buf_size, _new_size);
  485. break;
  486. case UWB_RC_EVT_BP_SLOT_CHANGE:
  487. *_new_size = *_real_size =
  488. sizeof(struct uwb_rc_evt_bp_slot_change_WUSB_0100);
  489. result = 0;
  490. break;
  491. case UWB_RC_EVT_DRP_AVAIL:
  492. core_size = sizeof(struct uwb_rc_evt_drp_avail_WUSB_0100);
  493. offset = offsetof(struct uwb_rc_evt_drp_avail_WUSB_0100,
  494. wIELength);
  495. event_size = hwarc_get_event_size(rc, rceb, core_size,
  496. offset, buf_size);
  497. if (event_size < 0)
  498. goto out;
  499. *_real_size = event_size;
  500. result = hwarc_filter_evt_drp_avail_WUSB_0100(
  501. rc, header, buf_size, _new_size);
  502. break;
  503. case UWB_RC_EVT_DRP:
  504. core_size = sizeof(struct uwb_rc_evt_drp_WUSB_0100);
  505. offset = offsetof(struct uwb_rc_evt_drp_WUSB_0100, wIELength);
  506. event_size = hwarc_get_event_size(rc, rceb, core_size,
  507. offset, buf_size);
  508. if (event_size < 0)
  509. goto out;
  510. *_real_size = event_size;
  511. result = hwarc_filter_evt_drp_WUSB_0100(rc, header,
  512. buf_size, _new_size);
  513. break;
  514. default:
  515. break;
  516. }
  517. out:
  518. return result;
  519. }
  520. /**
  521. * Filter data from WUSB device to WHCI driver
  522. *
  523. * @header: incoming event
  524. * @buf_size: size of buffer in which event arrived
  525. * @_event_size: actual size of event in the buffer
  526. * @_new_size: size of event after filtered
  527. *
  528. * Filter events based on which protocol the device supports. The WUSB
  529. * errata should be the same as WHCI 0.95 so we do not filter that here -
  530. * only WUSB 1.0.
  531. *
  532. * If we don't handle it, we return -ENOANO (why the weird error code?
  533. * well, so if I get it, I can pinpoint in the code that raised
  534. * it...after all, not too many places use the higher error codes).
  535. */
  536. static
  537. int hwarc_filter_event(struct uwb_rc *rc, struct uwb_rceb **header,
  538. const size_t buf_size, size_t *_real_size,
  539. size_t *_new_size)
  540. {
  541. int result = -ENOANO;
  542. if (rc->version == 0x0100)
  543. result = hwarc_filter_event_WUSB_0100(
  544. rc, header, buf_size, _real_size, _new_size);
  545. return result;
  546. }
  547. /**
  548. * Execute an UWB RC command on HWA
  549. *
  550. * @rc: Instance of a Radio Controller that is a HWA
  551. * @cmd: Buffer containing the RCCB and payload to execute
  552. * @cmd_size: Size of the command buffer.
  553. *
  554. * NOTE: rc's mutex has to be locked
  555. */
  556. static
  557. int hwarc_cmd(struct uwb_rc *uwb_rc, const struct uwb_rccb *cmd, size_t cmd_size)
  558. {
  559. struct hwarc *hwarc = uwb_rc->priv;
  560. return usb_control_msg(
  561. hwarc->usb_dev, usb_sndctrlpipe(hwarc->usb_dev, 0),
  562. WA_EXEC_RC_CMD, USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  563. 0, hwarc->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  564. (void *) cmd, cmd_size, 100 /* FIXME: this is totally arbitrary */);
  565. }
  566. static
  567. int hwarc_reset(struct uwb_rc *uwb_rc)
  568. {
  569. struct hwarc *hwarc = uwb_rc->priv;
  570. int result;
  571. /* device lock must be held when calling usb_reset_device. */
  572. result = usb_lock_device_for_reset(hwarc->usb_dev, NULL);
  573. if (result >= 0) {
  574. result = usb_reset_device(hwarc->usb_dev);
  575. usb_unlock_device(hwarc->usb_dev);
  576. }
  577. return result;
  578. }
  579. /**
  580. * Callback for the notification and event endpoint
  581. *
  582. * Check's that everything is fine and then passes the read data to
  583. * the notification/event handling mechanism (neh).
  584. */
  585. static
  586. void hwarc_neep_cb(struct urb *urb)
  587. {
  588. struct hwarc *hwarc = urb->context;
  589. struct usb_interface *usb_iface = hwarc->usb_iface;
  590. struct device *dev = &usb_iface->dev;
  591. int result;
  592. switch (result = urb->status) {
  593. case 0:
  594. uwb_rc_neh_grok(hwarc->uwb_rc, urb->transfer_buffer,
  595. urb->actual_length);
  596. break;
  597. case -ECONNRESET: /* Not an error, but a controlled situation; */
  598. case -ENOENT: /* (we killed the URB)...so, no broadcast */
  599. goto out;
  600. case -ESHUTDOWN: /* going away! */
  601. goto out;
  602. default: /* On general errors, retry unless it gets ugly */
  603. if (edc_inc(&hwarc->neep_edc, EDC_MAX_ERRORS,
  604. EDC_ERROR_TIMEFRAME))
  605. goto error_exceeded;
  606. dev_err(dev, "NEEP: URB error %d\n", urb->status);
  607. }
  608. result = usb_submit_urb(urb, GFP_ATOMIC);
  609. if (result < 0 && result != -ENODEV && result != -EPERM) {
  610. /* ignoring unrecoverable errors */
  611. dev_err(dev, "NEEP: Can't resubmit URB (%d) resetting device\n",
  612. result);
  613. goto error;
  614. }
  615. out:
  616. return;
  617. error_exceeded:
  618. dev_err(dev, "NEEP: URB max acceptable errors "
  619. "exceeded, resetting device\n");
  620. error:
  621. uwb_rc_neh_error(hwarc->uwb_rc, result);
  622. uwb_rc_reset_all(hwarc->uwb_rc);
  623. return;
  624. }
  625. static void hwarc_init(struct hwarc *hwarc)
  626. {
  627. edc_init(&hwarc->neep_edc);
  628. }
  629. /**
  630. * Initialize the notification/event endpoint stuff
  631. *
  632. * Note this is effectively a parallel thread; it knows that
  633. * hwarc->uwb_rc always exists because the existence of a 'hwarc'
  634. * means that there is a reverence on the hwarc->uwb_rc (see
  635. * _probe()), and thus _neep_cb() can execute safely.
  636. */
  637. static int hwarc_neep_init(struct uwb_rc *rc)
  638. {
  639. struct hwarc *hwarc = rc->priv;
  640. struct usb_interface *iface = hwarc->usb_iface;
  641. struct usb_device *usb_dev = interface_to_usbdev(iface);
  642. struct device *dev = &iface->dev;
  643. int result;
  644. struct usb_endpoint_descriptor *epd;
  645. epd = &iface->cur_altsetting->endpoint[0].desc;
  646. hwarc->rd_buffer = (void *) __get_free_page(GFP_KERNEL);
  647. if (hwarc->rd_buffer == NULL) {
  648. dev_err(dev, "Unable to allocate notification's read buffer\n");
  649. goto error_rd_buffer;
  650. }
  651. hwarc->neep_urb = usb_alloc_urb(0, GFP_KERNEL);
  652. if (hwarc->neep_urb == NULL) {
  653. dev_err(dev, "Unable to allocate notification URB\n");
  654. goto error_urb_alloc;
  655. }
  656. usb_fill_int_urb(hwarc->neep_urb, usb_dev,
  657. usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
  658. hwarc->rd_buffer, PAGE_SIZE,
  659. hwarc_neep_cb, hwarc, epd->bInterval);
  660. result = usb_submit_urb(hwarc->neep_urb, GFP_ATOMIC);
  661. if (result < 0) {
  662. dev_err(dev, "Cannot submit notification URB: %d\n", result);
  663. goto error_neep_submit;
  664. }
  665. return 0;
  666. error_neep_submit:
  667. usb_free_urb(hwarc->neep_urb);
  668. hwarc->neep_urb = NULL;
  669. error_urb_alloc:
  670. free_page((unsigned long)hwarc->rd_buffer);
  671. hwarc->rd_buffer = NULL;
  672. error_rd_buffer:
  673. return -ENOMEM;
  674. }
  675. /** Clean up all the notification endpoint resources */
  676. static void hwarc_neep_release(struct uwb_rc *rc)
  677. {
  678. struct hwarc *hwarc = rc->priv;
  679. usb_kill_urb(hwarc->neep_urb);
  680. usb_free_urb(hwarc->neep_urb);
  681. hwarc->neep_urb = NULL;
  682. free_page((unsigned long)hwarc->rd_buffer);
  683. hwarc->rd_buffer = NULL;
  684. }
  685. /**
  686. * Get the version from class-specific descriptor
  687. *
  688. * NOTE: this descriptor comes with the big bundled configuration
  689. * descriptor that includes the interfaces' and endpoints', so
  690. * we just look for it in the cached copy kept by the USB stack.
  691. *
  692. * NOTE2: We convert LE fields to CPU order.
  693. */
  694. static int hwarc_get_version(struct uwb_rc *rc)
  695. {
  696. int result;
  697. struct hwarc *hwarc = rc->priv;
  698. struct uwb_rc_control_intf_class_desc *descr;
  699. struct device *dev = &rc->uwb_dev.dev;
  700. struct usb_device *usb_dev = hwarc->usb_dev;
  701. char *itr;
  702. struct usb_descriptor_header *hdr;
  703. size_t itr_size, actconfig_idx;
  704. u16 version;
  705. actconfig_idx = (usb_dev->actconfig - usb_dev->config) /
  706. sizeof(usb_dev->config[0]);
  707. itr = usb_dev->rawdescriptors[actconfig_idx];
  708. itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
  709. while (itr_size >= sizeof(*hdr)) {
  710. hdr = (struct usb_descriptor_header *) itr;
  711. dev_dbg(dev, "Extra device descriptor: "
  712. "type %02x/%u bytes @ %zu (%zu left)\n",
  713. hdr->bDescriptorType, hdr->bLength,
  714. (itr - usb_dev->rawdescriptors[actconfig_idx]),
  715. itr_size);
  716. if (hdr->bDescriptorType == USB_DT_CS_RADIO_CONTROL)
  717. goto found;
  718. itr += hdr->bLength;
  719. itr_size -= hdr->bLength;
  720. }
  721. dev_err(dev, "cannot find Radio Control Interface Class descriptor\n");
  722. return -ENODEV;
  723. found:
  724. result = -EINVAL;
  725. if (hdr->bLength > itr_size) { /* is it available? */
  726. dev_err(dev, "incomplete Radio Control Interface Class "
  727. "descriptor (%zu bytes left, %u needed)\n",
  728. itr_size, hdr->bLength);
  729. goto error;
  730. }
  731. if (hdr->bLength < sizeof(*descr)) {
  732. dev_err(dev, "short Radio Control Interface Class "
  733. "descriptor\n");
  734. goto error;
  735. }
  736. descr = (struct uwb_rc_control_intf_class_desc *) hdr;
  737. /* Make LE fields CPU order */
  738. version = __le16_to_cpu(descr->bcdRCIVersion);
  739. if (version != 0x0100) {
  740. dev_err(dev, "Device reports protocol version 0x%04x. We "
  741. "do not support that. \n", version);
  742. result = -EINVAL;
  743. goto error;
  744. }
  745. rc->version = version;
  746. dev_dbg(dev, "Device supports WUSB protocol version 0x%04x \n", rc->version);
  747. result = 0;
  748. error:
  749. return result;
  750. }
  751. /*
  752. * By creating a 'uwb_rc', we have a reference on it -- that reference
  753. * is the one we drop when we disconnect.
  754. *
  755. * No need to switch altsettings; according to WUSB1.0[8.6.1.1], there
  756. * is only one altsetting allowed.
  757. */
  758. static int hwarc_probe(struct usb_interface *iface,
  759. const struct usb_device_id *id)
  760. {
  761. int result;
  762. struct uwb_rc *uwb_rc;
  763. struct hwarc *hwarc;
  764. struct device *dev = &iface->dev;
  765. if (iface->cur_altsetting->desc.bNumEndpoints < 1)
  766. return -ENODEV;
  767. if (!usb_endpoint_xfer_int(&iface->cur_altsetting->endpoint[0].desc))
  768. return -ENODEV;
  769. result = -ENOMEM;
  770. uwb_rc = uwb_rc_alloc();
  771. if (uwb_rc == NULL) {
  772. dev_err(dev, "unable to allocate RC instance\n");
  773. goto error_rc_alloc;
  774. }
  775. hwarc = kzalloc(sizeof(*hwarc), GFP_KERNEL);
  776. if (hwarc == NULL) {
  777. dev_err(dev, "unable to allocate HWA RC instance\n");
  778. goto error_alloc;
  779. }
  780. hwarc_init(hwarc);
  781. hwarc->usb_dev = usb_get_dev(interface_to_usbdev(iface));
  782. hwarc->usb_iface = usb_get_intf(iface);
  783. hwarc->uwb_rc = uwb_rc;
  784. uwb_rc->owner = THIS_MODULE;
  785. uwb_rc->start = hwarc_neep_init;
  786. uwb_rc->stop = hwarc_neep_release;
  787. uwb_rc->cmd = hwarc_cmd;
  788. uwb_rc->reset = hwarc_reset;
  789. if (id->driver_info & WUSB_QUIRK_WHCI_CMD_EVT) {
  790. uwb_rc->filter_cmd = NULL;
  791. uwb_rc->filter_event = NULL;
  792. } else {
  793. uwb_rc->filter_cmd = hwarc_filter_cmd;
  794. uwb_rc->filter_event = hwarc_filter_event;
  795. }
  796. result = uwb_rc_add(uwb_rc, dev, hwarc);
  797. if (result < 0)
  798. goto error_rc_add;
  799. result = hwarc_get_version(uwb_rc);
  800. if (result < 0) {
  801. dev_err(dev, "cannot retrieve version of RC \n");
  802. goto error_get_version;
  803. }
  804. usb_set_intfdata(iface, hwarc);
  805. return 0;
  806. error_get_version:
  807. uwb_rc_rm(uwb_rc);
  808. error_rc_add:
  809. usb_put_intf(iface);
  810. usb_put_dev(hwarc->usb_dev);
  811. kfree(hwarc);
  812. error_alloc:
  813. uwb_rc_put(uwb_rc);
  814. error_rc_alloc:
  815. return result;
  816. }
  817. static void hwarc_disconnect(struct usb_interface *iface)
  818. {
  819. struct hwarc *hwarc = usb_get_intfdata(iface);
  820. struct uwb_rc *uwb_rc = hwarc->uwb_rc;
  821. usb_set_intfdata(hwarc->usb_iface, NULL);
  822. uwb_rc_rm(uwb_rc);
  823. usb_put_intf(hwarc->usb_iface);
  824. usb_put_dev(hwarc->usb_dev);
  825. kfree(hwarc);
  826. uwb_rc_put(uwb_rc); /* when creating the device, refcount = 1 */
  827. }
  828. static int hwarc_pre_reset(struct usb_interface *iface)
  829. {
  830. struct hwarc *hwarc = usb_get_intfdata(iface);
  831. struct uwb_rc *uwb_rc = hwarc->uwb_rc;
  832. uwb_rc_pre_reset(uwb_rc);
  833. return 0;
  834. }
  835. static int hwarc_post_reset(struct usb_interface *iface)
  836. {
  837. struct hwarc *hwarc = usb_get_intfdata(iface);
  838. struct uwb_rc *uwb_rc = hwarc->uwb_rc;
  839. return uwb_rc_post_reset(uwb_rc);
  840. }
  841. /** USB device ID's that we handle */
  842. static const struct usb_device_id hwarc_id_table[] = {
  843. /* D-Link DUB-1210 */
  844. { USB_DEVICE_AND_INTERFACE_INFO(0x07d1, 0x3d02, 0xe0, 0x01, 0x02),
  845. .driver_info = WUSB_QUIRK_WHCI_CMD_EVT },
  846. /* Intel i1480 (using firmware 1.3PA2-20070828) */
  847. { USB_DEVICE_AND_INTERFACE_INFO(0x8086, 0x0c3b, 0xe0, 0x01, 0x02),
  848. .driver_info = WUSB_QUIRK_WHCI_CMD_EVT },
  849. /* Alereon 5310 */
  850. { USB_DEVICE_AND_INTERFACE_INFO(0x13dc, 0x5310, 0xe0, 0x01, 0x02),
  851. .driver_info = WUSB_QUIRK_WHCI_CMD_EVT },
  852. /* Alereon 5611 */
  853. { USB_DEVICE_AND_INTERFACE_INFO(0x13dc, 0x5611, 0xe0, 0x01, 0x02),
  854. .driver_info = WUSB_QUIRK_WHCI_CMD_EVT },
  855. /* Generic match for the Radio Control interface */
  856. { USB_INTERFACE_INFO(0xe0, 0x01, 0x02), },
  857. { },
  858. };
  859. MODULE_DEVICE_TABLE(usb, hwarc_id_table);
  860. static struct usb_driver hwarc_driver = {
  861. .name = "hwa-rc",
  862. .id_table = hwarc_id_table,
  863. .probe = hwarc_probe,
  864. .disconnect = hwarc_disconnect,
  865. .pre_reset = hwarc_pre_reset,
  866. .post_reset = hwarc_post_reset,
  867. };
  868. module_usb_driver(hwarc_driver);
  869. MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
  870. MODULE_DESCRIPTION("Host Wireless Adapter Radio Control Driver");
  871. MODULE_LICENSE("GPL");