hcd.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/module.h>
  21. #include <linux/uwb/umc.h>
  22. #include "../../wusbcore/wusbhc.h"
  23. #include "whcd.h"
  24. /*
  25. * One time initialization.
  26. *
  27. * Nothing to do here.
  28. */
  29. static int whc_reset(struct usb_hcd *usb_hcd)
  30. {
  31. return 0;
  32. }
  33. /*
  34. * Start the wireless host controller.
  35. *
  36. * Start device notification.
  37. *
  38. * Put hc into run state, set DNTS parameters.
  39. */
  40. static int whc_start(struct usb_hcd *usb_hcd)
  41. {
  42. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  43. struct whc *whc = wusbhc_to_whc(wusbhc);
  44. u8 bcid;
  45. int ret;
  46. mutex_lock(&wusbhc->mutex);
  47. le_writel(WUSBINTR_GEN_CMD_DONE
  48. | WUSBINTR_HOST_ERR
  49. | WUSBINTR_ASYNC_SCHED_SYNCED
  50. | WUSBINTR_DNTS_INT
  51. | WUSBINTR_ERR_INT
  52. | WUSBINTR_INT,
  53. whc->base + WUSBINTR);
  54. /* set cluster ID */
  55. bcid = wusb_cluster_id_get();
  56. ret = whc_set_cluster_id(whc, bcid);
  57. if (ret < 0)
  58. goto out;
  59. wusbhc->cluster_id = bcid;
  60. /* start HC */
  61. whc_write_wusbcmd(whc, WUSBCMD_RUN, WUSBCMD_RUN);
  62. usb_hcd->uses_new_polling = 1;
  63. set_bit(HCD_FLAG_POLL_RH, &usb_hcd->flags);
  64. usb_hcd->state = HC_STATE_RUNNING;
  65. out:
  66. mutex_unlock(&wusbhc->mutex);
  67. return ret;
  68. }
  69. /*
  70. * Stop the wireless host controller.
  71. *
  72. * Stop device notification.
  73. *
  74. * Wait for pending transfer to stop? Put hc into stop state?
  75. */
  76. static void whc_stop(struct usb_hcd *usb_hcd)
  77. {
  78. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  79. struct whc *whc = wusbhc_to_whc(wusbhc);
  80. mutex_lock(&wusbhc->mutex);
  81. /* stop HC */
  82. le_writel(0, whc->base + WUSBINTR);
  83. whc_write_wusbcmd(whc, WUSBCMD_RUN, 0);
  84. whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
  85. WUSBSTS_HCHALTED, WUSBSTS_HCHALTED,
  86. 100, "HC to halt");
  87. wusb_cluster_id_put(wusbhc->cluster_id);
  88. mutex_unlock(&wusbhc->mutex);
  89. }
  90. static int whc_get_frame_number(struct usb_hcd *usb_hcd)
  91. {
  92. /* Frame numbers are not applicable to WUSB. */
  93. return -ENOSYS;
  94. }
  95. /*
  96. * Queue an URB to the ASL or PZL
  97. */
  98. static int whc_urb_enqueue(struct usb_hcd *usb_hcd, struct urb *urb,
  99. gfp_t mem_flags)
  100. {
  101. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  102. struct whc *whc = wusbhc_to_whc(wusbhc);
  103. int ret;
  104. switch (usb_pipetype(urb->pipe)) {
  105. case PIPE_INTERRUPT:
  106. ret = pzl_urb_enqueue(whc, urb, mem_flags);
  107. break;
  108. case PIPE_ISOCHRONOUS:
  109. dev_err(&whc->umc->dev, "isochronous transfers unsupported\n");
  110. ret = -ENOTSUPP;
  111. break;
  112. case PIPE_CONTROL:
  113. case PIPE_BULK:
  114. default:
  115. ret = asl_urb_enqueue(whc, urb, mem_flags);
  116. break;
  117. }
  118. return ret;
  119. }
  120. /*
  121. * Remove a queued URB from the ASL or PZL.
  122. */
  123. static int whc_urb_dequeue(struct usb_hcd *usb_hcd, struct urb *urb, int status)
  124. {
  125. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  126. struct whc *whc = wusbhc_to_whc(wusbhc);
  127. int ret;
  128. switch (usb_pipetype(urb->pipe)) {
  129. case PIPE_INTERRUPT:
  130. ret = pzl_urb_dequeue(whc, urb, status);
  131. break;
  132. case PIPE_ISOCHRONOUS:
  133. ret = -ENOTSUPP;
  134. break;
  135. case PIPE_CONTROL:
  136. case PIPE_BULK:
  137. default:
  138. ret = asl_urb_dequeue(whc, urb, status);
  139. break;
  140. }
  141. return ret;
  142. }
  143. /*
  144. * Wait for all URBs to the endpoint to be completed, then delete the
  145. * qset.
  146. */
  147. static void whc_endpoint_disable(struct usb_hcd *usb_hcd,
  148. struct usb_host_endpoint *ep)
  149. {
  150. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  151. struct whc *whc = wusbhc_to_whc(wusbhc);
  152. struct whc_qset *qset;
  153. qset = ep->hcpriv;
  154. if (qset) {
  155. ep->hcpriv = NULL;
  156. if (usb_endpoint_xfer_bulk(&ep->desc)
  157. || usb_endpoint_xfer_control(&ep->desc))
  158. asl_qset_delete(whc, qset);
  159. else
  160. pzl_qset_delete(whc, qset);
  161. }
  162. }
  163. static void whc_endpoint_reset(struct usb_hcd *usb_hcd,
  164. struct usb_host_endpoint *ep)
  165. {
  166. struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  167. struct whc *whc = wusbhc_to_whc(wusbhc);
  168. struct whc_qset *qset;
  169. unsigned long flags;
  170. spin_lock_irqsave(&whc->lock, flags);
  171. qset = ep->hcpriv;
  172. if (qset) {
  173. qset->remove = 1;
  174. qset->reset = 1;
  175. if (usb_endpoint_xfer_bulk(&ep->desc)
  176. || usb_endpoint_xfer_control(&ep->desc))
  177. queue_work(whc->workqueue, &whc->async_work);
  178. else
  179. queue_work(whc->workqueue, &whc->periodic_work);
  180. }
  181. spin_unlock_irqrestore(&whc->lock, flags);
  182. }
  183. static struct hc_driver whc_hc_driver = {
  184. .description = "whci-hcd",
  185. .product_desc = "Wireless host controller",
  186. .hcd_priv_size = sizeof(struct whc) - sizeof(struct usb_hcd),
  187. .irq = whc_int_handler,
  188. .flags = HCD_USB2,
  189. .reset = whc_reset,
  190. .start = whc_start,
  191. .stop = whc_stop,
  192. .get_frame_number = whc_get_frame_number,
  193. .urb_enqueue = whc_urb_enqueue,
  194. .urb_dequeue = whc_urb_dequeue,
  195. .endpoint_disable = whc_endpoint_disable,
  196. .endpoint_reset = whc_endpoint_reset,
  197. .hub_status_data = wusbhc_rh_status_data,
  198. .hub_control = wusbhc_rh_control,
  199. .start_port_reset = wusbhc_rh_start_port_reset,
  200. };
  201. static int whc_probe(struct umc_dev *umc)
  202. {
  203. int ret;
  204. struct usb_hcd *usb_hcd;
  205. struct wusbhc *wusbhc;
  206. struct whc *whc;
  207. struct device *dev = &umc->dev;
  208. usb_hcd = usb_create_hcd(&whc_hc_driver, dev, "whci");
  209. if (usb_hcd == NULL) {
  210. dev_err(dev, "unable to create hcd\n");
  211. return -ENOMEM;
  212. }
  213. usb_hcd->wireless = 1;
  214. usb_hcd->self.sg_tablesize = 2048; /* somewhat arbitrary */
  215. wusbhc = usb_hcd_to_wusbhc(usb_hcd);
  216. whc = wusbhc_to_whc(wusbhc);
  217. whc->umc = umc;
  218. ret = whc_init(whc);
  219. if (ret)
  220. goto error_whc_init;
  221. wusbhc->dev = dev;
  222. wusbhc->uwb_rc = uwb_rc_get_by_grandpa(umc->dev.parent);
  223. if (!wusbhc->uwb_rc) {
  224. ret = -ENODEV;
  225. dev_err(dev, "cannot get radio controller\n");
  226. goto error_uwb_rc;
  227. }
  228. if (whc->n_devices > USB_MAXCHILDREN) {
  229. dev_warn(dev, "USB_MAXCHILDREN too low for WUSB adapter (%u ports)\n",
  230. whc->n_devices);
  231. wusbhc->ports_max = USB_MAXCHILDREN;
  232. } else
  233. wusbhc->ports_max = whc->n_devices;
  234. wusbhc->mmcies_max = whc->n_mmc_ies;
  235. wusbhc->start = whc_wusbhc_start;
  236. wusbhc->stop = whc_wusbhc_stop;
  237. wusbhc->mmcie_add = whc_mmcie_add;
  238. wusbhc->mmcie_rm = whc_mmcie_rm;
  239. wusbhc->dev_info_set = whc_dev_info_set;
  240. wusbhc->bwa_set = whc_bwa_set;
  241. wusbhc->set_num_dnts = whc_set_num_dnts;
  242. wusbhc->set_ptk = whc_set_ptk;
  243. wusbhc->set_gtk = whc_set_gtk;
  244. ret = wusbhc_create(wusbhc);
  245. if (ret)
  246. goto error_wusbhc_create;
  247. ret = usb_add_hcd(usb_hcd, whc->umc->irq, IRQF_SHARED);
  248. if (ret) {
  249. dev_err(dev, "cannot add HCD: %d\n", ret);
  250. goto error_usb_add_hcd;
  251. }
  252. device_wakeup_enable(usb_hcd->self.controller);
  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_uwb_rc:
  267. whc_clean_up(whc);
  268. error_whc_init:
  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 __used 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");