hcd.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. unsigned long flags;
  169. spin_lock_irqsave(&whc->lock, flags);
  170. qset = ep->hcpriv;
  171. if (qset) {
  172. qset->remove = 1;
  173. qset->reset = 1;
  174. if (usb_endpoint_xfer_bulk(&ep->desc)
  175. || usb_endpoint_xfer_control(&ep->desc))
  176. queue_work(whc->workqueue, &whc->async_work);
  177. else
  178. queue_work(whc->workqueue, &whc->periodic_work);
  179. }
  180. spin_unlock_irqrestore(&whc->lock, flags);
  181. }
  182. static struct hc_driver whc_hc_driver = {
  183. .description = "whci-hcd",
  184. .product_desc = "Wireless host controller",
  185. .hcd_priv_size = sizeof(struct whc) - sizeof(struct usb_hcd),
  186. .irq = whc_int_handler,
  187. .flags = HCD_USB2,
  188. .reset = whc_reset,
  189. .start = whc_start,
  190. .stop = whc_stop,
  191. .get_frame_number = whc_get_frame_number,
  192. .urb_enqueue = whc_urb_enqueue,
  193. .urb_dequeue = whc_urb_dequeue,
  194. .endpoint_disable = whc_endpoint_disable,
  195. .endpoint_reset = whc_endpoint_reset,
  196. .hub_status_data = wusbhc_rh_status_data,
  197. .hub_control = wusbhc_rh_control,
  198. .bus_suspend = wusbhc_rh_suspend,
  199. .bus_resume = wusbhc_rh_resume,
  200. .start_port_reset = wusbhc_rh_start_port_reset,
  201. };
  202. static int whc_probe(struct umc_dev *umc)
  203. {
  204. int ret = -ENOMEM;
  205. struct usb_hcd *usb_hcd;
  206. struct wusbhc *wusbhc = NULL;
  207. struct whc *whc = NULL;
  208. struct device *dev = &umc->dev;
  209. usb_hcd = usb_create_hcd(&whc_hc_driver, dev, "whci");
  210. if (usb_hcd == NULL) {
  211. dev_err(dev, "unable to create hcd\n");
  212. goto error;
  213. }
  214. usb_hcd->wireless = 1;
  215. usb_hcd->self.sg_tablesize = 2048; /* somewhat arbitrary */
  216. wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  217. whc = wusbhc_to_whc(wusbhc);
  218. whc->umc = umc;
  219. ret = whc_init(whc);
  220. if (ret)
  221. goto error;
  222. wusbhc->dev = dev;
  223. wusbhc->uwb_rc = uwb_rc_get_by_grandpa(umc->dev.parent);
  224. if (!wusbhc->uwb_rc) {
  225. ret = -ENODEV;
  226. dev_err(dev, "cannot get radio controller\n");
  227. goto error;
  228. }
  229. if (whc->n_devices > USB_MAXCHILDREN) {
  230. dev_warn(dev, "USB_MAXCHILDREN too low for WUSB adapter (%u ports)\n",
  231. whc->n_devices);
  232. wusbhc->ports_max = USB_MAXCHILDREN;
  233. } else
  234. wusbhc->ports_max = whc->n_devices;
  235. wusbhc->mmcies_max = whc->n_mmc_ies;
  236. wusbhc->start = whc_wusbhc_start;
  237. wusbhc->stop = whc_wusbhc_stop;
  238. wusbhc->mmcie_add = whc_mmcie_add;
  239. wusbhc->mmcie_rm = whc_mmcie_rm;
  240. wusbhc->dev_info_set = whc_dev_info_set;
  241. wusbhc->bwa_set = whc_bwa_set;
  242. wusbhc->set_num_dnts = whc_set_num_dnts;
  243. wusbhc->set_ptk = whc_set_ptk;
  244. wusbhc->set_gtk = whc_set_gtk;
  245. ret = wusbhc_create(wusbhc);
  246. if (ret)
  247. goto error_wusbhc_create;
  248. ret = usb_add_hcd(usb_hcd, whc->umc->irq, IRQF_SHARED);
  249. if (ret) {
  250. dev_err(dev, "cannot add HCD: %d\n", ret);
  251. goto error_usb_add_hcd;
  252. }
  253. ret = wusbhc_b_create(wusbhc);
  254. if (ret) {
  255. dev_err(dev, "WUSBHC phase B setup failed: %d\n", ret);
  256. goto error_wusbhc_b_create;
  257. }
  258. whc_dbg_init(whc);
  259. return 0;
  260. error_wusbhc_b_create:
  261. usb_remove_hcd(usb_hcd);
  262. error_usb_add_hcd:
  263. wusbhc_destroy(wusbhc);
  264. error_wusbhc_create:
  265. uwb_rc_put(wusbhc->uwb_rc);
  266. error:
  267. whc_clean_up(whc);
  268. if (usb_hcd)
  269. usb_put_hcd(usb_hcd);
  270. return ret;
  271. }
  272. static void whc_remove(struct umc_dev *umc)
  273. {
  274. struct usb_hcd *usb_hcd = dev_get_drvdata(&umc->dev);
  275. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  276. struct whc *whc = wusbhc_to_whc(wusbhc);
  277. if (usb_hcd) {
  278. whc_dbg_clean_up(whc);
  279. wusbhc_b_destroy(wusbhc);
  280. usb_remove_hcd(usb_hcd);
  281. wusbhc_destroy(wusbhc);
  282. uwb_rc_put(wusbhc->uwb_rc);
  283. whc_clean_up(whc);
  284. usb_put_hcd(usb_hcd);
  285. }
  286. }
  287. static struct umc_driver whci_hc_driver = {
  288. .name = "whci-hcd",
  289. .cap_id = UMC_CAP_ID_WHCI_WUSB_HC,
  290. .probe = whc_probe,
  291. .remove = whc_remove,
  292. };
  293. static int __init whci_hc_driver_init(void)
  294. {
  295. return umc_driver_register(&whci_hc_driver);
  296. }
  297. module_init(whci_hc_driver_init);
  298. static void __exit whci_hc_driver_exit(void)
  299. {
  300. umc_driver_unregister(&whci_hc_driver);
  301. }
  302. module_exit(whci_hc_driver_exit);
  303. /* PCI device ID's that we handle (so it gets loaded) */
  304. static struct pci_device_id whci_hcd_id_table[] = {
  305. { PCI_DEVICE_CLASS(PCI_CLASS_WIRELESS_WHCI, ~0) },
  306. { /* empty last entry */ }
  307. };
  308. MODULE_DEVICE_TABLE(pci, whci_hcd_id_table);
  309. MODULE_DESCRIPTION("WHCI Wireless USB host controller driver");
  310. MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
  311. MODULE_LICENSE("GPL");