hcd.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Wireless Host Controller (WHC) driver.
  3. *
  4. * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/uwb/umc.h>
  21. #include "../../wusbcore/wusbhc.h"
  22. #include "whcd.h"
  23. /*
  24. * One time initialization.
  25. *
  26. * Nothing to do here.
  27. */
  28. static int whc_reset(struct usb_hcd *usb_hcd)
  29. {
  30. return 0;
  31. }
  32. /*
  33. * Start the wireless host controller.
  34. *
  35. * Start device notification.
  36. *
  37. * Put hc into run state, set DNTS parameters.
  38. */
  39. static int whc_start(struct usb_hcd *usb_hcd)
  40. {
  41. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  42. struct whc *whc = wusbhc_to_whc(wusbhc);
  43. u8 bcid;
  44. int ret;
  45. mutex_lock(&wusbhc->mutex);
  46. le_writel(WUSBINTR_GEN_CMD_DONE
  47. | WUSBINTR_HOST_ERR
  48. | WUSBINTR_ASYNC_SCHED_SYNCED
  49. | WUSBINTR_DNTS_INT
  50. | WUSBINTR_ERR_INT
  51. | WUSBINTR_INT,
  52. whc->base + WUSBINTR);
  53. /* set cluster ID */
  54. bcid = wusb_cluster_id_get();
  55. ret = whc_set_cluster_id(whc, bcid);
  56. if (ret < 0)
  57. goto out;
  58. wusbhc->cluster_id = bcid;
  59. /* start HC */
  60. whc_write_wusbcmd(whc, WUSBCMD_RUN, WUSBCMD_RUN);
  61. usb_hcd->uses_new_polling = 1;
  62. usb_hcd->poll_rh = 1;
  63. usb_hcd->state = HC_STATE_RUNNING;
  64. out:
  65. mutex_unlock(&wusbhc->mutex);
  66. return ret;
  67. }
  68. /*
  69. * Stop the wireless host controller.
  70. *
  71. * Stop device notification.
  72. *
  73. * Wait for pending transfer to stop? Put hc into stop state?
  74. */
  75. static void whc_stop(struct usb_hcd *usb_hcd)
  76. {
  77. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  78. struct whc *whc = wusbhc_to_whc(wusbhc);
  79. mutex_lock(&wusbhc->mutex);
  80. /* stop HC */
  81. le_writel(0, whc->base + WUSBINTR);
  82. whc_write_wusbcmd(whc, WUSBCMD_RUN, 0);
  83. whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
  84. WUSBSTS_HCHALTED, WUSBSTS_HCHALTED,
  85. 100, "HC to halt");
  86. wusb_cluster_id_put(wusbhc->cluster_id);
  87. mutex_unlock(&wusbhc->mutex);
  88. }
  89. static int whc_get_frame_number(struct usb_hcd *usb_hcd)
  90. {
  91. /* Frame numbers are not applicable to WUSB. */
  92. return -ENOSYS;
  93. }
  94. /*
  95. * Queue an URB to the ASL or PZL
  96. */
  97. static int whc_urb_enqueue(struct usb_hcd *usb_hcd, struct urb *urb,
  98. gfp_t mem_flags)
  99. {
  100. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  101. struct whc *whc = wusbhc_to_whc(wusbhc);
  102. int ret;
  103. switch (usb_pipetype(urb->pipe)) {
  104. case PIPE_INTERRUPT:
  105. ret = pzl_urb_enqueue(whc, urb, mem_flags);
  106. break;
  107. case PIPE_ISOCHRONOUS:
  108. dev_err(&whc->umc->dev, "isochronous transfers unsupported\n");
  109. ret = -ENOTSUPP;
  110. break;
  111. case PIPE_CONTROL:
  112. case PIPE_BULK:
  113. default:
  114. ret = asl_urb_enqueue(whc, urb, mem_flags);
  115. break;
  116. };
  117. return ret;
  118. }
  119. /*
  120. * Remove a queued URB from the ASL or PZL.
  121. */
  122. static int whc_urb_dequeue(struct usb_hcd *usb_hcd, struct urb *urb, int status)
  123. {
  124. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  125. struct whc *whc = wusbhc_to_whc(wusbhc);
  126. int ret;
  127. switch (usb_pipetype(urb->pipe)) {
  128. case PIPE_INTERRUPT:
  129. ret = pzl_urb_dequeue(whc, urb, status);
  130. break;
  131. case PIPE_ISOCHRONOUS:
  132. ret = -ENOTSUPP;
  133. break;
  134. case PIPE_CONTROL:
  135. case PIPE_BULK:
  136. default:
  137. ret = asl_urb_dequeue(whc, urb, status);
  138. break;
  139. };
  140. return ret;
  141. }
  142. /*
  143. * Wait for all URBs to the endpoint to be completed, then delete the
  144. * qset.
  145. */
  146. static void whc_endpoint_disable(struct usb_hcd *usb_hcd,
  147. struct usb_host_endpoint *ep)
  148. {
  149. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  150. struct whc *whc = wusbhc_to_whc(wusbhc);
  151. struct whc_qset *qset;
  152. qset = ep->hcpriv;
  153. if (qset) {
  154. ep->hcpriv = NULL;
  155. if (usb_endpoint_xfer_bulk(&ep->desc)
  156. || usb_endpoint_xfer_control(&ep->desc))
  157. asl_qset_delete(whc, qset);
  158. else
  159. pzl_qset_delete(whc, qset);
  160. }
  161. }
  162. static void whc_endpoint_reset(struct usb_hcd *usb_hcd,
  163. struct usb_host_endpoint *ep)
  164. {
  165. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  166. struct whc *whc = wusbhc_to_whc(wusbhc);
  167. struct whc_qset *qset;
  168. qset = ep->hcpriv;
  169. if (qset) {
  170. qset->remove = 1;
  171. if (usb_endpoint_xfer_bulk(&ep->desc)
  172. || usb_endpoint_xfer_control(&ep->desc))
  173. queue_work(whc->workqueue, &whc->async_work);
  174. else
  175. queue_work(whc->workqueue, &whc->periodic_work);
  176. qset_reset(whc, qset);
  177. }
  178. }
  179. static struct hc_driver whc_hc_driver = {
  180. .description = "whci-hcd",
  181. .product_desc = "Wireless host controller",
  182. .hcd_priv_size = sizeof(struct whc) - sizeof(struct usb_hcd),
  183. .irq = whc_int_handler,
  184. .flags = HCD_USB2,
  185. .reset = whc_reset,
  186. .start = whc_start,
  187. .stop = whc_stop,
  188. .get_frame_number = whc_get_frame_number,
  189. .urb_enqueue = whc_urb_enqueue,
  190. .urb_dequeue = whc_urb_dequeue,
  191. .endpoint_disable = whc_endpoint_disable,
  192. .endpoint_reset = whc_endpoint_reset,
  193. .hub_status_data = wusbhc_rh_status_data,
  194. .hub_control = wusbhc_rh_control,
  195. .bus_suspend = wusbhc_rh_suspend,
  196. .bus_resume = wusbhc_rh_resume,
  197. .start_port_reset = wusbhc_rh_start_port_reset,
  198. };
  199. static int whc_probe(struct umc_dev *umc)
  200. {
  201. int ret = -ENOMEM;
  202. struct usb_hcd *usb_hcd;
  203. struct wusbhc *wusbhc = NULL;
  204. struct whc *whc = NULL;
  205. struct device *dev = &umc->dev;
  206. usb_hcd = usb_create_hcd(&whc_hc_driver, dev, "whci");
  207. if (usb_hcd == NULL) {
  208. dev_err(dev, "unable to create hcd\n");
  209. goto error;
  210. }
  211. usb_hcd->wireless = 1;
  212. wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  213. whc = wusbhc_to_whc(wusbhc);
  214. whc->umc = umc;
  215. ret = whc_init(whc);
  216. if (ret)
  217. goto error;
  218. wusbhc->dev = dev;
  219. wusbhc->uwb_rc = uwb_rc_get_by_grandpa(umc->dev.parent);
  220. if (!wusbhc->uwb_rc) {
  221. ret = -ENODEV;
  222. dev_err(dev, "cannot get radio controller\n");
  223. goto error;
  224. }
  225. if (whc->n_devices > USB_MAXCHILDREN) {
  226. dev_warn(dev, "USB_MAXCHILDREN too low for WUSB adapter (%u ports)\n",
  227. whc->n_devices);
  228. wusbhc->ports_max = USB_MAXCHILDREN;
  229. } else
  230. wusbhc->ports_max = whc->n_devices;
  231. wusbhc->mmcies_max = whc->n_mmc_ies;
  232. wusbhc->start = whc_wusbhc_start;
  233. wusbhc->stop = whc_wusbhc_stop;
  234. wusbhc->mmcie_add = whc_mmcie_add;
  235. wusbhc->mmcie_rm = whc_mmcie_rm;
  236. wusbhc->dev_info_set = whc_dev_info_set;
  237. wusbhc->bwa_set = whc_bwa_set;
  238. wusbhc->set_num_dnts = whc_set_num_dnts;
  239. wusbhc->set_ptk = whc_set_ptk;
  240. wusbhc->set_gtk = whc_set_gtk;
  241. ret = wusbhc_create(wusbhc);
  242. if (ret)
  243. goto error_wusbhc_create;
  244. ret = usb_add_hcd(usb_hcd, whc->umc->irq, IRQF_SHARED);
  245. if (ret) {
  246. dev_err(dev, "cannot add HCD: %d\n", ret);
  247. goto error_usb_add_hcd;
  248. }
  249. ret = wusbhc_b_create(wusbhc);
  250. if (ret) {
  251. dev_err(dev, "WUSBHC phase B setup failed: %d\n", ret);
  252. goto error_wusbhc_b_create;
  253. }
  254. whc_dbg_init(whc);
  255. return 0;
  256. error_wusbhc_b_create:
  257. usb_remove_hcd(usb_hcd);
  258. error_usb_add_hcd:
  259. wusbhc_destroy(wusbhc);
  260. error_wusbhc_create:
  261. uwb_rc_put(wusbhc->uwb_rc);
  262. error:
  263. whc_clean_up(whc);
  264. if (usb_hcd)
  265. usb_put_hcd(usb_hcd);
  266. return ret;
  267. }
  268. static void whc_remove(struct umc_dev *umc)
  269. {
  270. struct usb_hcd *usb_hcd = dev_get_drvdata(&umc->dev);
  271. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  272. struct whc *whc = wusbhc_to_whc(wusbhc);
  273. if (usb_hcd) {
  274. whc_dbg_clean_up(whc);
  275. wusbhc_b_destroy(wusbhc);
  276. usb_remove_hcd(usb_hcd);
  277. wusbhc_destroy(wusbhc);
  278. uwb_rc_put(wusbhc->uwb_rc);
  279. whc_clean_up(whc);
  280. usb_put_hcd(usb_hcd);
  281. }
  282. }
  283. static struct umc_driver whci_hc_driver = {
  284. .name = "whci-hcd",
  285. .cap_id = UMC_CAP_ID_WHCI_WUSB_HC,
  286. .probe = whc_probe,
  287. .remove = whc_remove,
  288. };
  289. static int __init whci_hc_driver_init(void)
  290. {
  291. return umc_driver_register(&whci_hc_driver);
  292. }
  293. module_init(whci_hc_driver_init);
  294. static void __exit whci_hc_driver_exit(void)
  295. {
  296. umc_driver_unregister(&whci_hc_driver);
  297. }
  298. module_exit(whci_hc_driver_exit);
  299. /* PCI device ID's that we handle (so it gets loaded) */
  300. static struct pci_device_id whci_hcd_id_table[] = {
  301. { PCI_DEVICE_CLASS(PCI_CLASS_WIRELESS_WHCI, ~0) },
  302. { /* empty last entry */ }
  303. };
  304. MODULE_DEVICE_TABLE(pci, whci_hcd_id_table);
  305. MODULE_DESCRIPTION("WHCI Wireless USB host controller driver");
  306. MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
  307. MODULE_LICENSE("GPL");