hwa-hc.c 27 KB

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