hwa-hc.c 26 KB

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