asl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Wireless Host Controller (WHC) asynchronous schedule management.
  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/dma-mapping.h>
  20. #include <linux/uwb/umc.h>
  21. #include <linux/usb.h>
  22. #include "../../wusbcore/wusbhc.h"
  23. #include "whcd.h"
  24. static void qset_get_next_prev(struct whc *whc, struct whc_qset *qset,
  25. struct whc_qset **next, struct whc_qset **prev)
  26. {
  27. struct list_head *n, *p;
  28. BUG_ON(list_empty(&whc->async_list));
  29. n = qset->list_node.next;
  30. if (n == &whc->async_list)
  31. n = n->next;
  32. p = qset->list_node.prev;
  33. if (p == &whc->async_list)
  34. p = p->prev;
  35. *next = container_of(n, struct whc_qset, list_node);
  36. *prev = container_of(p, struct whc_qset, list_node);
  37. }
  38. static void asl_qset_insert_begin(struct whc *whc, struct whc_qset *qset)
  39. {
  40. list_move(&qset->list_node, &whc->async_list);
  41. qset->in_sw_list = true;
  42. }
  43. static void asl_qset_insert(struct whc *whc, struct whc_qset *qset)
  44. {
  45. struct whc_qset *next, *prev;
  46. qset_clear(whc, qset);
  47. /* Link into ASL. */
  48. qset_get_next_prev(whc, qset, &next, &prev);
  49. whc_qset_set_link_ptr(&qset->qh.link, next->qset_dma);
  50. whc_qset_set_link_ptr(&prev->qh.link, qset->qset_dma);
  51. qset->in_hw_list = true;
  52. }
  53. static void asl_qset_remove(struct whc *whc, struct whc_qset *qset)
  54. {
  55. struct whc_qset *prev, *next;
  56. qset_get_next_prev(whc, qset, &next, &prev);
  57. list_move(&qset->list_node, &whc->async_removed_list);
  58. qset->in_sw_list = false;
  59. /*
  60. * No more qsets in the ASL? The caller must stop the ASL as
  61. * it's no longer valid.
  62. */
  63. if (list_empty(&whc->async_list))
  64. return;
  65. /* Remove from ASL. */
  66. whc_qset_set_link_ptr(&prev->qh.link, next->qset_dma);
  67. qset->in_hw_list = false;
  68. }
  69. /**
  70. * process_qset - process any recently inactivated or halted qTDs in a
  71. * qset.
  72. *
  73. * After inactive qTDs are removed, new qTDs can be added if the
  74. * urb queue still contains URBs.
  75. *
  76. * Returns any additional WUSBCMD bits for the ASL sync command (i.e.,
  77. * WUSBCMD_ASYNC_QSET_RM if a halted qset was removed).
  78. */
  79. static uint32_t process_qset(struct whc *whc, struct whc_qset *qset)
  80. {
  81. enum whc_update update = 0;
  82. uint32_t status = 0;
  83. while (qset->ntds) {
  84. struct whc_qtd *td;
  85. int t;
  86. t = qset->td_start;
  87. td = &qset->qtd[qset->td_start];
  88. status = le32_to_cpu(td->status);
  89. /*
  90. * Nothing to do with a still active qTD.
  91. */
  92. if (status & QTD_STS_ACTIVE)
  93. break;
  94. if (status & QTD_STS_HALTED) {
  95. /* Ug, an error. */
  96. process_halted_qtd(whc, qset, td);
  97. /* A halted qTD always triggers an update
  98. because the qset was either removed or
  99. reactivated. */
  100. update |= WHC_UPDATE_UPDATED;
  101. goto done;
  102. }
  103. /* Mmm, a completed qTD. */
  104. process_inactive_qtd(whc, qset, td);
  105. }
  106. if (!qset->remove)
  107. update |= qset_add_qtds(whc, qset);
  108. done:
  109. /*
  110. * Remove this qset from the ASL if requested, but only if has
  111. * no qTDs.
  112. */
  113. if (qset->remove && qset->ntds == 0) {
  114. asl_qset_remove(whc, qset);
  115. update |= WHC_UPDATE_REMOVED;
  116. }
  117. return update;
  118. }
  119. void asl_start(struct whc *whc)
  120. {
  121. struct whc_qset *qset;
  122. qset = list_first_entry(&whc->async_list, struct whc_qset, list_node);
  123. le_writeq(qset->qset_dma | QH_LINK_NTDS(8), whc->base + WUSBASYNCLISTADDR);
  124. whc_write_wusbcmd(whc, WUSBCMD_ASYNC_EN, WUSBCMD_ASYNC_EN);
  125. whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
  126. WUSBSTS_ASYNC_SCHED, WUSBSTS_ASYNC_SCHED,
  127. 1000, "start ASL");
  128. }
  129. void asl_stop(struct whc *whc)
  130. {
  131. whc_write_wusbcmd(whc, WUSBCMD_ASYNC_EN, 0);
  132. whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
  133. WUSBSTS_ASYNC_SCHED, 0,
  134. 1000, "stop ASL");
  135. }
  136. /**
  137. * asl_update - request an ASL update and wait for the hardware to be synced
  138. * @whc: the WHCI HC
  139. * @wusbcmd: WUSBCMD value to start the update.
  140. *
  141. * If the WUSB HC is inactive (i.e., the ASL is stopped) then the
  142. * update must be skipped as the hardware may not respond to update
  143. * requests.
  144. */
  145. void asl_update(struct whc *whc, uint32_t wusbcmd)
  146. {
  147. struct wusbhc *wusbhc = &whc->wusbhc;
  148. long t;
  149. mutex_lock(&wusbhc->mutex);
  150. if (wusbhc->active) {
  151. whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
  152. t = wait_event_timeout(
  153. whc->async_list_wq,
  154. (le_readl(whc->base + WUSBCMD) & WUSBCMD_ASYNC_UPDATED) == 0,
  155. msecs_to_jiffies(1000));
  156. if (t == 0)
  157. whc_hw_error(whc, "ASL update timeout");
  158. }
  159. mutex_unlock(&wusbhc->mutex);
  160. }
  161. /**
  162. * scan_async_work - scan the ASL for qsets to process.
  163. *
  164. * Process each qset in the ASL in turn and then signal the WHC that
  165. * the ASL has been updated.
  166. *
  167. * Then start, stop or update the asynchronous schedule as required.
  168. */
  169. void scan_async_work(struct work_struct *work)
  170. {
  171. struct whc *whc = container_of(work, struct whc, async_work);
  172. struct whc_qset *qset, *t;
  173. enum whc_update update = 0;
  174. spin_lock_irq(&whc->lock);
  175. /*
  176. * Transerve the software list backwards so new qsets can be
  177. * safely inserted into the ASL without making it non-circular.
  178. */
  179. list_for_each_entry_safe_reverse(qset, t, &whc->async_list, list_node) {
  180. if (!qset->in_hw_list) {
  181. asl_qset_insert(whc, qset);
  182. update |= WHC_UPDATE_ADDED;
  183. }
  184. update |= process_qset(whc, qset);
  185. }
  186. spin_unlock_irq(&whc->lock);
  187. if (update) {
  188. uint32_t wusbcmd = WUSBCMD_ASYNC_UPDATED | WUSBCMD_ASYNC_SYNCED_DB;
  189. if (update & WHC_UPDATE_REMOVED)
  190. wusbcmd |= WUSBCMD_ASYNC_QSET_RM;
  191. asl_update(whc, wusbcmd);
  192. }
  193. /*
  194. * Now that the ASL is updated, complete the removal of any
  195. * removed qsets.
  196. *
  197. * If the qset was to be reset, do so and reinsert it into the
  198. * ASL if it has pending transfers.
  199. */
  200. spin_lock_irq(&whc->lock);
  201. list_for_each_entry_safe(qset, t, &whc->async_removed_list, list_node) {
  202. qset_remove_complete(whc, qset);
  203. if (qset->reset) {
  204. qset_reset(whc, qset);
  205. if (!list_empty(&qset->stds)) {
  206. asl_qset_insert_begin(whc, qset);
  207. queue_work(whc->workqueue, &whc->async_work);
  208. }
  209. }
  210. }
  211. spin_unlock_irq(&whc->lock);
  212. }
  213. /**
  214. * asl_urb_enqueue - queue an URB onto the asynchronous list (ASL).
  215. * @whc: the WHCI host controller
  216. * @urb: the URB to enqueue
  217. * @mem_flags: flags for any memory allocations
  218. *
  219. * The qset for the endpoint is obtained and the urb queued on to it.
  220. *
  221. * Work is scheduled to update the hardware's view of the ASL.
  222. */
  223. int asl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
  224. {
  225. struct whc_qset *qset;
  226. int err;
  227. unsigned long flags;
  228. spin_lock_irqsave(&whc->lock, flags);
  229. err = usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
  230. if (err < 0) {
  231. spin_unlock_irqrestore(&whc->lock, flags);
  232. return err;
  233. }
  234. qset = get_qset(whc, urb, GFP_ATOMIC);
  235. if (qset == NULL)
  236. err = -ENOMEM;
  237. else
  238. err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
  239. if (!err) {
  240. if (!qset->in_sw_list && !qset->remove)
  241. asl_qset_insert_begin(whc, qset);
  242. } else
  243. usb_hcd_unlink_urb_from_ep(&whc->wusbhc.usb_hcd, urb);
  244. spin_unlock_irqrestore(&whc->lock, flags);
  245. if (!err)
  246. queue_work(whc->workqueue, &whc->async_work);
  247. return err;
  248. }
  249. /**
  250. * asl_urb_dequeue - remove an URB (qset) from the async list.
  251. * @whc: the WHCI host controller
  252. * @urb: the URB to dequeue
  253. * @status: the current status of the URB
  254. *
  255. * URBs that do yet have qTDs can simply be removed from the software
  256. * queue, otherwise the qset must be removed from the ASL so the qTDs
  257. * can be removed.
  258. */
  259. int asl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
  260. {
  261. struct whc_urb *wurb = urb->hcpriv;
  262. struct whc_qset *qset = wurb->qset;
  263. struct whc_std *std, *t;
  264. bool has_qtd = false;
  265. int ret;
  266. unsigned long flags;
  267. spin_lock_irqsave(&whc->lock, flags);
  268. ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
  269. if (ret < 0)
  270. goto out;
  271. list_for_each_entry_safe(std, t, &qset->stds, list_node) {
  272. if (std->urb == urb) {
  273. if (std->qtd)
  274. has_qtd = true;
  275. qset_free_std(whc, std);
  276. } else
  277. std->qtd = NULL; /* so this std is re-added when the qset is */
  278. }
  279. if (has_qtd) {
  280. asl_qset_remove(whc, qset);
  281. wurb->status = status;
  282. wurb->is_async = true;
  283. queue_work(whc->workqueue, &wurb->dequeue_work);
  284. } else
  285. qset_remove_urb(whc, qset, urb, status);
  286. out:
  287. spin_unlock_irqrestore(&whc->lock, flags);
  288. return ret;
  289. }
  290. /**
  291. * asl_qset_delete - delete a qset from the ASL
  292. */
  293. void asl_qset_delete(struct whc *whc, struct whc_qset *qset)
  294. {
  295. qset->remove = 1;
  296. queue_work(whc->workqueue, &whc->async_work);
  297. qset_delete(whc, qset);
  298. }
  299. /**
  300. * asl_init - initialize the asynchronous schedule list
  301. *
  302. * A dummy qset with no qTDs is added to the ASL to simplify removing
  303. * qsets (no need to stop the ASL when the last qset is removed).
  304. */
  305. int asl_init(struct whc *whc)
  306. {
  307. struct whc_qset *qset;
  308. qset = qset_alloc(whc, GFP_KERNEL);
  309. if (qset == NULL)
  310. return -ENOMEM;
  311. asl_qset_insert_begin(whc, qset);
  312. asl_qset_insert(whc, qset);
  313. return 0;
  314. }
  315. /**
  316. * asl_clean_up - free ASL resources
  317. *
  318. * The ASL is stopped and empty except for the dummy qset.
  319. */
  320. void asl_clean_up(struct whc *whc)
  321. {
  322. struct whc_qset *qset;
  323. if (!list_empty(&whc->async_list)) {
  324. qset = list_first_entry(&whc->async_list, struct whc_qset, list_node);
  325. list_del(&qset->list_node);
  326. qset_free(whc, qset);
  327. }
  328. }