hwa-hc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Host Wire Adapter:
  4. * Driver glue, HWA-specific functions, bridges to WAHC and WUSBHC
  5. *
  6. * Copyright (C) 2005-2006 Intel Corporation
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  8. *
  9. * The HWA driver is a simple layer that forwards requests to the WAHC
  10. * (Wire Adater Host Controller) or WUSBHC (Wireless USB Host
  11. * Controller) layers.
  12. *
  13. * Host Wire Adapter is the 'WUSB 1.0 standard' name for Wireless-USB
  14. * Host Controller that is connected to your system via USB (a USB
  15. * dongle that implements a USB host...). There is also a Device Wired
  16. * Adaptor, DWA (Wireless USB hub) that uses the same mechanism for
  17. * transferring data (it is after all a USB host connected via
  18. * Wireless USB), we have a common layer called Wire Adapter Host
  19. * Controller that does all the hard work. The WUSBHC (Wireless USB
  20. * Host Controller) is the part common to WUSB Host Controllers, the
  21. * HWA and the PCI-based one, that is implemented following the WHCI
  22. * spec. All these layers are implemented in ../wusbcore.
  23. *
  24. * The main functions are hwahc_op_urb_{en,de}queue(), that pass the
  25. * job of converting a URB to a Wire Adapter
  26. *
  27. * Entry points:
  28. *
  29. * hwahc_driver_*() Driver initialization, registration and
  30. * teardown.
  31. *
  32. * hwahc_probe() New device came up, create an instance for
  33. * it [from device enumeration].
  34. *
  35. * hwahc_disconnect() Remove device instance [from device
  36. * enumeration].
  37. *
  38. * [__]hwahc_op_*() Host-Wire-Adaptor specific functions for
  39. * starting/stopping/etc (some might be made also
  40. * DWA).
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/slab.h>
  44. #include <linux/module.h>
  45. #include <linux/workqueue.h>
  46. #include <linux/wait.h>
  47. #include <linux/completion.h>
  48. #include "../wusbcore/wa-hc.h"
  49. #include "../wusbcore/wusbhc.h"
  50. struct hwahc {
  51. struct wusbhc wusbhc; /* has to be 1st */
  52. struct wahc wa;
  53. };
  54. /*
  55. * FIXME should be wusbhc
  56. *
  57. * NOTE: we need to cache the Cluster ID because later...there is no
  58. * way to get it :)
  59. */
  60. static int __hwahc_set_cluster_id(struct hwahc *hwahc, u8 cluster_id)
  61. {
  62. int result;
  63. struct wusbhc *wusbhc = &hwahc->wusbhc;
  64. struct wahc *wa = &hwahc->wa;
  65. struct device *dev = &wa->usb_iface->dev;
  66. result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  67. WUSB_REQ_SET_CLUSTER_ID,
  68. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  69. cluster_id,
  70. wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  71. NULL, 0, USB_CTRL_SET_TIMEOUT);
  72. if (result < 0)
  73. dev_err(dev, "Cannot set WUSB Cluster ID to 0x%02x: %d\n",
  74. cluster_id, result);
  75. else
  76. wusbhc->cluster_id = cluster_id;
  77. dev_info(dev, "Wireless USB Cluster ID set to 0x%02x\n", cluster_id);
  78. return result;
  79. }
  80. static int __hwahc_op_set_num_dnts(struct wusbhc *wusbhc, u8 interval, u8 slots)
  81. {
  82. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  83. struct wahc *wa = &hwahc->wa;
  84. return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  85. WUSB_REQ_SET_NUM_DNTS,
  86. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  87. interval << 8 | slots,
  88. wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  89. NULL, 0, USB_CTRL_SET_TIMEOUT);
  90. }
  91. /*
  92. * Reset a WUSB host controller and wait for it to complete doing it.
  93. *
  94. * @usb_hcd: Pointer to WUSB Host Controller instance.
  95. *
  96. */
  97. static int hwahc_op_reset(struct usb_hcd *usb_hcd)
  98. {
  99. int result;
  100. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  101. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  102. struct device *dev = &hwahc->wa.usb_iface->dev;
  103. mutex_lock(&wusbhc->mutex);
  104. wa_nep_disarm(&hwahc->wa);
  105. result = __wa_set_feature(&hwahc->wa, WA_RESET);
  106. if (result < 0) {
  107. dev_err(dev, "error commanding HC to reset: %d\n", result);
  108. goto error_unlock;
  109. }
  110. result = __wa_wait_status(&hwahc->wa, WA_STATUS_RESETTING, 0);
  111. if (result < 0) {
  112. dev_err(dev, "error waiting for HC to reset: %d\n", result);
  113. goto error_unlock;
  114. }
  115. error_unlock:
  116. mutex_unlock(&wusbhc->mutex);
  117. return result;
  118. }
  119. /*
  120. * FIXME: break this function up
  121. */
  122. static int hwahc_op_start(struct usb_hcd *usb_hcd)
  123. {
  124. u8 addr;
  125. int result;
  126. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  127. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  128. result = -ENOSPC;
  129. mutex_lock(&wusbhc->mutex);
  130. addr = wusb_cluster_id_get();
  131. if (addr == 0)
  132. goto error_cluster_id_get;
  133. result = __hwahc_set_cluster_id(hwahc, addr);
  134. if (result < 0)
  135. goto error_set_cluster_id;
  136. usb_hcd->uses_new_polling = 1;
  137. set_bit(HCD_FLAG_POLL_RH, &usb_hcd->flags);
  138. usb_hcd->state = HC_STATE_RUNNING;
  139. /*
  140. * prevent USB core from suspending the root hub since
  141. * bus_suspend and bus_resume are not yet supported.
  142. */
  143. pm_runtime_get_noresume(&usb_hcd->self.root_hub->dev);
  144. result = 0;
  145. out:
  146. mutex_unlock(&wusbhc->mutex);
  147. return result;
  148. error_set_cluster_id:
  149. wusb_cluster_id_put(addr);
  150. error_cluster_id_get:
  151. goto out;
  152. }
  153. /*
  154. * No need to abort pipes, as when this is called, all the children
  155. * has been disconnected and that has done it [through
  156. * usb_disable_interface() -> usb_disable_endpoint() ->
  157. * hwahc_op_ep_disable() - >rpipe_ep_disable()].
  158. */
  159. static void hwahc_op_stop(struct usb_hcd *usb_hcd)
  160. {
  161. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  162. mutex_lock(&wusbhc->mutex);
  163. wusb_cluster_id_put(wusbhc->cluster_id);
  164. mutex_unlock(&wusbhc->mutex);
  165. }
  166. static int hwahc_op_get_frame_number(struct usb_hcd *usb_hcd)
  167. {
  168. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  169. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  170. struct wahc *wa = &hwahc->wa;
  171. /*
  172. * We cannot query the HWA for the WUSB time since that requires sending
  173. * a synchronous URB and this function can be called in_interrupt.
  174. * Instead, query the USB frame number for our parent and use that.
  175. */
  176. return usb_get_current_frame_number(wa->usb_dev);
  177. }
  178. static int hwahc_op_urb_enqueue(struct usb_hcd *usb_hcd, struct urb *urb,
  179. gfp_t gfp)
  180. {
  181. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  182. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  183. return wa_urb_enqueue(&hwahc->wa, urb->ep, urb, gfp);
  184. }
  185. static int hwahc_op_urb_dequeue(struct usb_hcd *usb_hcd, struct urb *urb,
  186. int status)
  187. {
  188. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  189. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  190. return wa_urb_dequeue(&hwahc->wa, urb, status);
  191. }
  192. /*
  193. * Release resources allocated for an endpoint
  194. *
  195. * If there is an associated rpipe to this endpoint, go ahead and put it.
  196. */
  197. static void hwahc_op_endpoint_disable(struct usb_hcd *usb_hcd,
  198. struct usb_host_endpoint *ep)
  199. {
  200. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  201. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  202. rpipe_ep_disable(&hwahc->wa, ep);
  203. }
  204. static int __hwahc_op_wusbhc_start(struct wusbhc *wusbhc)
  205. {
  206. int result;
  207. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  208. struct device *dev = &hwahc->wa.usb_iface->dev;
  209. result = __wa_set_feature(&hwahc->wa, WA_ENABLE);
  210. if (result < 0) {
  211. dev_err(dev, "error commanding HC to start: %d\n", result);
  212. goto error_stop;
  213. }
  214. result = __wa_wait_status(&hwahc->wa, WA_ENABLE, WA_ENABLE);
  215. if (result < 0) {
  216. dev_err(dev, "error waiting for HC to start: %d\n", result);
  217. goto error_stop;
  218. }
  219. result = wa_nep_arm(&hwahc->wa, GFP_KERNEL);
  220. if (result < 0) {
  221. dev_err(dev, "cannot listen to notifications: %d\n", result);
  222. goto error_stop;
  223. }
  224. /*
  225. * If WUSB_QUIRK_ALEREON_HWA_DISABLE_XFER_NOTIFICATIONS is set,
  226. * disable transfer notifications.
  227. */
  228. if (hwahc->wa.quirks &
  229. WUSB_QUIRK_ALEREON_HWA_DISABLE_XFER_NOTIFICATIONS) {
  230. struct usb_host_interface *cur_altsetting =
  231. hwahc->wa.usb_iface->cur_altsetting;
  232. result = usb_control_msg(hwahc->wa.usb_dev,
  233. usb_sndctrlpipe(hwahc->wa.usb_dev, 0),
  234. WA_REQ_ALEREON_DISABLE_XFER_NOTIFICATIONS,
  235. USB_DIR_OUT | USB_TYPE_VENDOR |
  236. USB_RECIP_INTERFACE,
  237. WA_REQ_ALEREON_FEATURE_SET,
  238. cur_altsetting->desc.bInterfaceNumber,
  239. NULL, 0,
  240. USB_CTRL_SET_TIMEOUT);
  241. /*
  242. * If we successfully sent the control message, start DTI here
  243. * because no transfer notifications will be received which is
  244. * where DTI is normally started.
  245. */
  246. if (result == 0)
  247. result = wa_dti_start(&hwahc->wa);
  248. else
  249. result = 0; /* OK. Continue normally. */
  250. if (result < 0) {
  251. dev_err(dev, "cannot start DTI: %d\n", result);
  252. goto error_dti_start;
  253. }
  254. }
  255. return result;
  256. error_dti_start:
  257. wa_nep_disarm(&hwahc->wa);
  258. error_stop:
  259. __wa_clear_feature(&hwahc->wa, WA_ENABLE);
  260. return result;
  261. }
  262. static void __hwahc_op_wusbhc_stop(struct wusbhc *wusbhc, int delay)
  263. {
  264. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  265. struct wahc *wa = &hwahc->wa;
  266. u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
  267. int ret;
  268. ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  269. WUSB_REQ_CHAN_STOP,
  270. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  271. delay * 1000,
  272. iface_no,
  273. NULL, 0, USB_CTRL_SET_TIMEOUT);
  274. if (ret == 0)
  275. msleep(delay);
  276. wa_nep_disarm(&hwahc->wa);
  277. __wa_stop(&hwahc->wa);
  278. }
  279. /*
  280. * Set the UWB MAS allocation for the WUSB cluster
  281. *
  282. * @stream_index: stream to use (-1 for cancelling the allocation)
  283. * @mas: mas bitmap to use
  284. */
  285. static int __hwahc_op_bwa_set(struct wusbhc *wusbhc, s8 stream_index,
  286. const struct uwb_mas_bm *mas)
  287. {
  288. int result;
  289. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  290. struct wahc *wa = &hwahc->wa;
  291. struct device *dev = &wa->usb_iface->dev;
  292. u8 mas_le[UWB_NUM_MAS/8];
  293. /* Set the stream index */
  294. result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  295. WUSB_REQ_SET_STREAM_IDX,
  296. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  297. stream_index,
  298. wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  299. NULL, 0, USB_CTRL_SET_TIMEOUT);
  300. if (result < 0) {
  301. dev_err(dev, "Cannot set WUSB stream index: %d\n", result);
  302. goto out;
  303. }
  304. uwb_mas_bm_copy_le(mas_le, mas);
  305. /* Set the MAS allocation */
  306. result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  307. WUSB_REQ_SET_WUSB_MAS,
  308. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  309. 0, wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  310. mas_le, 32, USB_CTRL_SET_TIMEOUT);
  311. if (result < 0)
  312. dev_err(dev, "Cannot set WUSB MAS allocation: %d\n", result);
  313. out:
  314. return result;
  315. }
  316. /*
  317. * Add an IE to the host's MMC
  318. *
  319. * @interval: See WUSB1.0[8.5.3.1]
  320. * @repeat_cnt: See WUSB1.0[8.5.3.1]
  321. * @handle: See WUSB1.0[8.5.3.1]
  322. * @wuie: Pointer to the header of the WUSB IE data to add.
  323. * MUST BE allocated in a kmalloc buffer (no stack or
  324. * vmalloc).
  325. *
  326. * NOTE: the format of the WUSB IEs for MMCs are different to the
  327. * normal MBOA MAC IEs (IE Id + Length in MBOA MAC vs. Length +
  328. * Id in WUSB IEs). Standards...you gotta love'em.
  329. */
  330. static int __hwahc_op_mmcie_add(struct wusbhc *wusbhc, u8 interval,
  331. u8 repeat_cnt, u8 handle,
  332. struct wuie_hdr *wuie)
  333. {
  334. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  335. struct wahc *wa = &hwahc->wa;
  336. u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
  337. return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  338. WUSB_REQ_ADD_MMC_IE,
  339. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  340. interval << 8 | repeat_cnt,
  341. handle << 8 | iface_no,
  342. wuie, wuie->bLength, USB_CTRL_SET_TIMEOUT);
  343. }
  344. /*
  345. * Remove an IE to the host's MMC
  346. *
  347. * @handle: See WUSB1.0[8.5.3.1]
  348. */
  349. static int __hwahc_op_mmcie_rm(struct wusbhc *wusbhc, u8 handle)
  350. {
  351. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  352. struct wahc *wa = &hwahc->wa;
  353. u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
  354. return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  355. WUSB_REQ_REMOVE_MMC_IE,
  356. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  357. 0, handle << 8 | iface_no,
  358. NULL, 0, USB_CTRL_SET_TIMEOUT);
  359. }
  360. /*
  361. * Update device information for a given fake port
  362. *
  363. * @port_idx: Fake port to which device is connected (wusbhc index, not
  364. * USB port number).
  365. */
  366. static int __hwahc_op_dev_info_set(struct wusbhc *wusbhc,
  367. struct wusb_dev *wusb_dev)
  368. {
  369. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  370. struct wahc *wa = &hwahc->wa;
  371. u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
  372. struct hwa_dev_info *dev_info;
  373. int ret;
  374. /* fill out the Device Info buffer and send it */
  375. dev_info = kzalloc(sizeof(struct hwa_dev_info), GFP_KERNEL);
  376. if (!dev_info)
  377. return -ENOMEM;
  378. uwb_mas_bm_copy_le(dev_info->bmDeviceAvailability,
  379. &wusb_dev->availability);
  380. dev_info->bDeviceAddress = wusb_dev->addr;
  381. /*
  382. * If the descriptors haven't been read yet, use a default PHY
  383. * rate of 53.3 Mbit/s only. The correct value will be used
  384. * when this will be called again as part of the
  385. * authentication process (which occurs after the descriptors
  386. * have been read).
  387. */
  388. if (wusb_dev->wusb_cap_descr)
  389. dev_info->wPHYRates = wusb_dev->wusb_cap_descr->wPHYRates;
  390. else
  391. dev_info->wPHYRates = cpu_to_le16(USB_WIRELESS_PHY_53);
  392. ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  393. WUSB_REQ_SET_DEV_INFO,
  394. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  395. 0, wusb_dev->port_idx << 8 | iface_no,
  396. dev_info, sizeof(struct hwa_dev_info),
  397. USB_CTRL_SET_TIMEOUT);
  398. kfree(dev_info);
  399. return ret;
  400. }
  401. /*
  402. * Set host's idea of which encryption (and key) method to use when
  403. * talking to ad evice on a given port.
  404. *
  405. * If key is NULL, it means disable encryption for that "virtual port"
  406. * (used when we disconnect).
  407. */
  408. static int __hwahc_dev_set_key(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
  409. const void *key, size_t key_size,
  410. u8 key_idx)
  411. {
  412. int result = -ENOMEM;
  413. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  414. struct wahc *wa = &hwahc->wa;
  415. u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
  416. struct usb_key_descriptor *keyd;
  417. size_t keyd_len;
  418. keyd_len = sizeof(*keyd) + key_size;
  419. keyd = kzalloc(keyd_len, GFP_KERNEL);
  420. if (keyd == NULL)
  421. return -ENOMEM;
  422. keyd->bLength = keyd_len;
  423. keyd->bDescriptorType = USB_DT_KEY;
  424. keyd->tTKID[0] = (tkid >> 0) & 0xff;
  425. keyd->tTKID[1] = (tkid >> 8) & 0xff;
  426. keyd->tTKID[2] = (tkid >> 16) & 0xff;
  427. memcpy(keyd->bKeyData, key, key_size);
  428. result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  429. USB_REQ_SET_DESCRIPTOR,
  430. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  431. USB_DT_KEY << 8 | key_idx,
  432. port_idx << 8 | iface_no,
  433. keyd, keyd_len, USB_CTRL_SET_TIMEOUT);
  434. kzfree(keyd); /* clear keys etc. */
  435. return result;
  436. }
  437. /*
  438. * Set host's idea of which encryption (and key) method to use when
  439. * talking to ad evice on a given port.
  440. *
  441. * If key is NULL, it means disable encryption for that "virtual port"
  442. * (used when we disconnect).
  443. */
  444. static int __hwahc_op_set_ptk(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
  445. const void *key, size_t key_size)
  446. {
  447. int result = -ENOMEM;
  448. struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  449. struct wahc *wa = &hwahc->wa;
  450. u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
  451. u8 encryption_value;
  452. /* Tell the host which key to use to talk to the device */
  453. if (key) {
  454. u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_PTK,
  455. WUSB_KEY_INDEX_ORIGINATOR_HOST);
  456. result = __hwahc_dev_set_key(wusbhc, port_idx, tkid,
  457. key, key_size, key_idx);
  458. if (result < 0)
  459. goto error_set_key;
  460. encryption_value = wusbhc->ccm1_etd->bEncryptionValue;
  461. } else {
  462. /* FIXME: this should come from wusbhc->etd[UNSECURE].value */
  463. encryption_value = 0;
  464. }
  465. /* Set the encryption type for communicating with the device */
  466. result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  467. USB_REQ_SET_ENCRYPTION,
  468. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  469. encryption_value, port_idx << 8 | iface_no,
  470. NULL, 0, USB_CTRL_SET_TIMEOUT);
  471. if (result < 0)
  472. dev_err(wusbhc->dev, "Can't set host's WUSB encryption for "
  473. "port index %u to %s (value %d): %d\n", port_idx,
  474. wusb_et_name(wusbhc->ccm1_etd->bEncryptionType),
  475. wusbhc->ccm1_etd->bEncryptionValue, result);
  476. error_set_key:
  477. return result;
  478. }
  479. /*
  480. * Set host's GTK key
  481. */
  482. static int __hwahc_op_set_gtk(struct wusbhc *wusbhc, u32 tkid,
  483. const void *key, size_t key_size)
  484. {
  485. u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_GTK,
  486. WUSB_KEY_INDEX_ORIGINATOR_HOST);
  487. return __hwahc_dev_set_key(wusbhc, 0, tkid, key, key_size, key_idx);
  488. }
  489. /*
  490. * Get the Wire Adapter class-specific descriptor
  491. *
  492. * NOTE: this descriptor comes with the big bundled configuration
  493. * descriptor that includes the interfaces' and endpoints', so
  494. * we just look for it in the cached copy kept by the USB stack.
  495. *
  496. * NOTE2: We convert LE fields to CPU order.
  497. */
  498. static int wa_fill_descr(struct wahc *wa)
  499. {
  500. int result;
  501. struct device *dev = &wa->usb_iface->dev;
  502. char *itr;
  503. struct usb_device *usb_dev = wa->usb_dev;
  504. struct usb_descriptor_header *hdr;
  505. struct usb_wa_descriptor *wa_descr;
  506. size_t itr_size, actconfig_idx;
  507. actconfig_idx = (usb_dev->actconfig - usb_dev->config) /
  508. sizeof(usb_dev->config[0]);
  509. itr = usb_dev->rawdescriptors[actconfig_idx];
  510. itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
  511. while (itr_size >= sizeof(*hdr)) {
  512. hdr = (struct usb_descriptor_header *) itr;
  513. dev_dbg(dev, "Extra device descriptor: "
  514. "type %02x/%u bytes @ %zu (%zu left)\n",
  515. hdr->bDescriptorType, hdr->bLength,
  516. (itr - usb_dev->rawdescriptors[actconfig_idx]),
  517. itr_size);
  518. if (hdr->bDescriptorType == USB_DT_WIRE_ADAPTER)
  519. goto found;
  520. itr += hdr->bLength;
  521. itr_size -= hdr->bLength;
  522. }
  523. dev_err(dev, "cannot find Wire Adapter Class descriptor\n");
  524. return -ENODEV;
  525. found:
  526. result = -EINVAL;
  527. if (hdr->bLength > itr_size) { /* is it available? */
  528. dev_err(dev, "incomplete Wire Adapter Class descriptor "
  529. "(%zu bytes left, %u needed)\n",
  530. itr_size, hdr->bLength);
  531. goto error;
  532. }
  533. if (hdr->bLength < sizeof(*wa->wa_descr)) {
  534. dev_err(dev, "short Wire Adapter Class descriptor\n");
  535. goto error;
  536. }
  537. wa->wa_descr = wa_descr = (struct usb_wa_descriptor *) hdr;
  538. if (le16_to_cpu(wa_descr->bcdWAVersion) > 0x0100)
  539. dev_warn(dev, "Wire Adapter v%d.%d newer than groked v1.0\n",
  540. (le16_to_cpu(wa_descr->bcdWAVersion) & 0xff00) >> 8,
  541. le16_to_cpu(wa_descr->bcdWAVersion) & 0x00ff);
  542. result = 0;
  543. error:
  544. return result;
  545. }
  546. static const struct hc_driver hwahc_hc_driver = {
  547. .description = "hwa-hcd",
  548. .product_desc = "Wireless USB HWA host controller",
  549. .hcd_priv_size = sizeof(struct hwahc) - sizeof(struct usb_hcd),
  550. .irq = NULL, /* FIXME */
  551. .flags = HCD_USB25,
  552. .reset = hwahc_op_reset,
  553. .start = hwahc_op_start,
  554. .stop = hwahc_op_stop,
  555. .get_frame_number = hwahc_op_get_frame_number,
  556. .urb_enqueue = hwahc_op_urb_enqueue,
  557. .urb_dequeue = hwahc_op_urb_dequeue,
  558. .endpoint_disable = hwahc_op_endpoint_disable,
  559. .hub_status_data = wusbhc_rh_status_data,
  560. .hub_control = wusbhc_rh_control,
  561. .start_port_reset = wusbhc_rh_start_port_reset,
  562. };
  563. static int hwahc_security_create(struct hwahc *hwahc)
  564. {
  565. int result;
  566. struct wusbhc *wusbhc = &hwahc->wusbhc;
  567. struct usb_device *usb_dev = hwahc->wa.usb_dev;
  568. struct device *dev = &usb_dev->dev;
  569. struct usb_security_descriptor *secd;
  570. struct usb_encryption_descriptor *etd;
  571. void *itr, *top;
  572. size_t itr_size, needed, bytes;
  573. u8 index;
  574. char buf[64];
  575. /* Find the host's security descriptors in the config descr bundle */
  576. index = (usb_dev->actconfig - usb_dev->config) /
  577. sizeof(usb_dev->config[0]);
  578. itr = usb_dev->rawdescriptors[index];
  579. itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
  580. top = itr + itr_size;
  581. result = __usb_get_extra_descriptor(usb_dev->rawdescriptors[index],
  582. le16_to_cpu(usb_dev->actconfig->desc.wTotalLength),
  583. USB_DT_SECURITY, (void **) &secd, sizeof(*secd));
  584. if (result == -1) {
  585. dev_warn(dev, "BUG? WUSB host has no security descriptors\n");
  586. return 0;
  587. }
  588. needed = sizeof(*secd);
  589. if (top - (void *)secd < needed) {
  590. dev_err(dev, "BUG? Not enough data to process security "
  591. "descriptor header (%zu bytes left vs %zu needed)\n",
  592. top - (void *) secd, needed);
  593. return 0;
  594. }
  595. needed = le16_to_cpu(secd->wTotalLength);
  596. if (top - (void *)secd < needed) {
  597. dev_err(dev, "BUG? Not enough data to process security "
  598. "descriptors (%zu bytes left vs %zu needed)\n",
  599. top - (void *) secd, needed);
  600. return 0;
  601. }
  602. /* Walk over the sec descriptors and store CCM1's on wusbhc */
  603. itr = (void *) secd + sizeof(*secd);
  604. top = (void *) secd + le16_to_cpu(secd->wTotalLength);
  605. index = 0;
  606. bytes = 0;
  607. while (itr < top) {
  608. etd = itr;
  609. if (top - itr < sizeof(*etd)) {
  610. dev_err(dev, "BUG: bad host security descriptor; "
  611. "not enough data (%zu vs %zu left)\n",
  612. top - itr, sizeof(*etd));
  613. break;
  614. }
  615. if (etd->bLength < sizeof(*etd)) {
  616. dev_err(dev, "BUG: bad host encryption descriptor; "
  617. "descriptor is too short "
  618. "(%zu vs %zu needed)\n",
  619. (size_t)etd->bLength, sizeof(*etd));
  620. break;
  621. }
  622. itr += etd->bLength;
  623. bytes += snprintf(buf + bytes, sizeof(buf) - bytes,
  624. "%s (0x%02x) ",
  625. wusb_et_name(etd->bEncryptionType),
  626. etd->bEncryptionValue);
  627. wusbhc->ccm1_etd = etd;
  628. }
  629. dev_info(dev, "supported encryption types: %s\n", buf);
  630. if (wusbhc->ccm1_etd == NULL) {
  631. dev_err(dev, "E: host doesn't support CCM-1 crypto\n");
  632. return 0;
  633. }
  634. /* Pretty print what we support */
  635. return 0;
  636. }
  637. static void hwahc_security_release(struct hwahc *hwahc)
  638. {
  639. /* nothing to do here so far... */
  640. }
  641. static int hwahc_create(struct hwahc *hwahc, struct usb_interface *iface,
  642. kernel_ulong_t quirks)
  643. {
  644. int result;
  645. struct device *dev = &iface->dev;
  646. struct wusbhc *wusbhc = &hwahc->wusbhc;
  647. struct wahc *wa = &hwahc->wa;
  648. struct usb_device *usb_dev = interface_to_usbdev(iface);
  649. wa->usb_dev = usb_get_dev(usb_dev); /* bind the USB device */
  650. wa->usb_iface = usb_get_intf(iface);
  651. wusbhc->dev = dev;
  652. /* defer getting the uwb_rc handle until it is needed since it
  653. * may not have been registered by the hwa_rc driver yet. */
  654. wusbhc->uwb_rc = NULL;
  655. result = wa_fill_descr(wa); /* Get the device descriptor */
  656. if (result < 0)
  657. goto error_fill_descriptor;
  658. if (wa->wa_descr->bNumPorts > USB_MAXCHILDREN) {
  659. dev_err(dev, "FIXME: USB_MAXCHILDREN too low for WUSB "
  660. "adapter (%u ports)\n", wa->wa_descr->bNumPorts);
  661. wusbhc->ports_max = USB_MAXCHILDREN;
  662. } else {
  663. wusbhc->ports_max = wa->wa_descr->bNumPorts;
  664. }
  665. wusbhc->mmcies_max = wa->wa_descr->bNumMMCIEs;
  666. wusbhc->start = __hwahc_op_wusbhc_start;
  667. wusbhc->stop = __hwahc_op_wusbhc_stop;
  668. wusbhc->mmcie_add = __hwahc_op_mmcie_add;
  669. wusbhc->mmcie_rm = __hwahc_op_mmcie_rm;
  670. wusbhc->dev_info_set = __hwahc_op_dev_info_set;
  671. wusbhc->bwa_set = __hwahc_op_bwa_set;
  672. wusbhc->set_num_dnts = __hwahc_op_set_num_dnts;
  673. wusbhc->set_ptk = __hwahc_op_set_ptk;
  674. wusbhc->set_gtk = __hwahc_op_set_gtk;
  675. result = hwahc_security_create(hwahc);
  676. if (result < 0) {
  677. dev_err(dev, "Can't initialize security: %d\n", result);
  678. goto error_security_create;
  679. }
  680. wa->wusb = wusbhc; /* FIXME: ugly, need to fix */
  681. result = wusbhc_create(&hwahc->wusbhc);
  682. if (result < 0) {
  683. dev_err(dev, "Can't create WUSB HC structures: %d\n", result);
  684. goto error_wusbhc_create;
  685. }
  686. result = wa_create(&hwahc->wa, iface, quirks);
  687. if (result < 0)
  688. goto error_wa_create;
  689. return 0;
  690. error_wa_create:
  691. wusbhc_destroy(&hwahc->wusbhc);
  692. error_wusbhc_create:
  693. /* WA Descr fill allocs no resources */
  694. error_security_create:
  695. error_fill_descriptor:
  696. usb_put_intf(iface);
  697. usb_put_dev(usb_dev);
  698. return result;
  699. }
  700. static void hwahc_destroy(struct hwahc *hwahc)
  701. {
  702. struct wusbhc *wusbhc = &hwahc->wusbhc;
  703. mutex_lock(&wusbhc->mutex);
  704. __wa_destroy(&hwahc->wa);
  705. wusbhc_destroy(&hwahc->wusbhc);
  706. hwahc_security_release(hwahc);
  707. hwahc->wusbhc.dev = NULL;
  708. uwb_rc_put(wusbhc->uwb_rc);
  709. usb_put_intf(hwahc->wa.usb_iface);
  710. usb_put_dev(hwahc->wa.usb_dev);
  711. mutex_unlock(&wusbhc->mutex);
  712. }
  713. static void hwahc_init(struct hwahc *hwahc)
  714. {
  715. wa_init(&hwahc->wa);
  716. }
  717. static int hwahc_probe(struct usb_interface *usb_iface,
  718. const struct usb_device_id *id)
  719. {
  720. int result;
  721. struct usb_hcd *usb_hcd;
  722. struct wusbhc *wusbhc;
  723. struct hwahc *hwahc;
  724. struct device *dev = &usb_iface->dev;
  725. result = -ENOMEM;
  726. usb_hcd = usb_create_hcd(&hwahc_hc_driver, &usb_iface->dev, "wusb-hwa");
  727. if (usb_hcd == NULL) {
  728. dev_err(dev, "unable to allocate instance\n");
  729. goto error_alloc;
  730. }
  731. usb_hcd->wireless = 1;
  732. usb_hcd->self.sg_tablesize = ~0;
  733. wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  734. hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  735. hwahc_init(hwahc);
  736. result = hwahc_create(hwahc, usb_iface, id->driver_info);
  737. if (result < 0) {
  738. dev_err(dev, "Cannot initialize internals: %d\n", result);
  739. goto error_hwahc_create;
  740. }
  741. result = usb_add_hcd(usb_hcd, 0, 0);
  742. if (result < 0) {
  743. dev_err(dev, "Cannot add HCD: %d\n", result);
  744. goto error_add_hcd;
  745. }
  746. device_wakeup_enable(usb_hcd->self.controller);
  747. result = wusbhc_b_create(&hwahc->wusbhc);
  748. if (result < 0) {
  749. dev_err(dev, "Cannot setup phase B of WUSBHC: %d\n", result);
  750. goto error_wusbhc_b_create;
  751. }
  752. return 0;
  753. error_wusbhc_b_create:
  754. usb_remove_hcd(usb_hcd);
  755. error_add_hcd:
  756. hwahc_destroy(hwahc);
  757. error_hwahc_create:
  758. usb_put_hcd(usb_hcd);
  759. error_alloc:
  760. return result;
  761. }
  762. static void hwahc_disconnect(struct usb_interface *usb_iface)
  763. {
  764. struct usb_hcd *usb_hcd;
  765. struct wusbhc *wusbhc;
  766. struct hwahc *hwahc;
  767. usb_hcd = usb_get_intfdata(usb_iface);
  768. wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  769. hwahc = container_of(wusbhc, struct hwahc, wusbhc);
  770. wusbhc_b_destroy(&hwahc->wusbhc);
  771. usb_remove_hcd(usb_hcd);
  772. hwahc_destroy(hwahc);
  773. usb_put_hcd(usb_hcd);
  774. }
  775. static const struct usb_device_id hwahc_id_table[] = {
  776. /* Alereon 5310 */
  777. { USB_DEVICE_AND_INTERFACE_INFO(0x13dc, 0x5310, 0xe0, 0x02, 0x01),
  778. .driver_info = WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC |
  779. WUSB_QUIRK_ALEREON_HWA_DISABLE_XFER_NOTIFICATIONS },
  780. /* Alereon 5611 */
  781. { USB_DEVICE_AND_INTERFACE_INFO(0x13dc, 0x5611, 0xe0, 0x02, 0x01),
  782. .driver_info = WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC |
  783. WUSB_QUIRK_ALEREON_HWA_DISABLE_XFER_NOTIFICATIONS },
  784. /* FIXME: use class labels for this */
  785. { USB_INTERFACE_INFO(0xe0, 0x02, 0x01), },
  786. {},
  787. };
  788. MODULE_DEVICE_TABLE(usb, hwahc_id_table);
  789. static struct usb_driver hwahc_driver = {
  790. .name = "hwa-hc",
  791. .probe = hwahc_probe,
  792. .disconnect = hwahc_disconnect,
  793. .id_table = hwahc_id_table,
  794. };
  795. module_usb_driver(hwahc_driver);
  796. MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
  797. MODULE_DESCRIPTION("Host Wired Adapter USB Host Control Driver");
  798. MODULE_LICENSE("GPL");