hwa-rc.c 28 KB

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