hwa-hc.c 25 KB

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