hwa-rc.c 28 KB

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