asl.c 9.5 KB

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