hwa-hc.c 28 KB

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