uhci-q.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. /*
  2. * Universal Host Controller Interface driver for USB.
  3. *
  4. * Maintainer: Alan Stern <stern@rowland.harvard.edu>
  5. *
  6. * (C) Copyright 1999 Linus Torvalds
  7. * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
  8. * (C) Copyright 1999 Randy Dunlap
  9. * (C) Copyright 1999 Georg Acher, acher@in.tum.de
  10. * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
  11. * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
  12. * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
  13. * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
  14. * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
  15. * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
  16. * (C) Copyright 2004-2005 Alan Stern, stern@rowland.harvard.edu
  17. */
  18. static void uhci_free_pending_tds(struct uhci_hcd *uhci);
  19. /*
  20. * Technically, updating td->status here is a race, but it's not really a
  21. * problem. The worst that can happen is that we set the IOC bit again
  22. * generating a spurious interrupt. We could fix this by creating another
  23. * QH and leaving the IOC bit always set, but then we would have to play
  24. * games with the FSBR code to make sure we get the correct order in all
  25. * the cases. I don't think it's worth the effort
  26. */
  27. static void uhci_set_next_interrupt(struct uhci_hcd *uhci)
  28. {
  29. if (uhci->is_stopped)
  30. mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
  31. uhci->term_td->status |= cpu_to_le32(TD_CTRL_IOC);
  32. }
  33. static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
  34. {
  35. uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
  36. }
  37. static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
  38. {
  39. dma_addr_t dma_handle;
  40. struct uhci_td *td;
  41. td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle);
  42. if (!td)
  43. return NULL;
  44. td->dma_handle = dma_handle;
  45. td->frame = -1;
  46. INIT_LIST_HEAD(&td->list);
  47. INIT_LIST_HEAD(&td->remove_list);
  48. INIT_LIST_HEAD(&td->fl_list);
  49. return td;
  50. }
  51. static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
  52. {
  53. if (!list_empty(&td->list))
  54. dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
  55. if (!list_empty(&td->remove_list))
  56. dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td);
  57. if (!list_empty(&td->fl_list))
  58. dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
  59. dma_pool_free(uhci->td_pool, td, td->dma_handle);
  60. }
  61. static inline void uhci_fill_td(struct uhci_td *td, u32 status,
  62. u32 token, u32 buffer)
  63. {
  64. td->status = cpu_to_le32(status);
  65. td->token = cpu_to_le32(token);
  66. td->buffer = cpu_to_le32(buffer);
  67. }
  68. /*
  69. * We insert Isochronous URBs directly into the frame list at the beginning
  70. */
  71. static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
  72. struct uhci_td *td, unsigned framenum)
  73. {
  74. framenum &= (UHCI_NUMFRAMES - 1);
  75. td->frame = framenum;
  76. /* Is there a TD already mapped there? */
  77. if (uhci->frame_cpu[framenum]) {
  78. struct uhci_td *ftd, *ltd;
  79. ftd = uhci->frame_cpu[framenum];
  80. ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
  81. list_add_tail(&td->fl_list, &ftd->fl_list);
  82. td->link = ltd->link;
  83. wmb();
  84. ltd->link = cpu_to_le32(td->dma_handle);
  85. } else {
  86. td->link = uhci->frame[framenum];
  87. wmb();
  88. uhci->frame[framenum] = cpu_to_le32(td->dma_handle);
  89. uhci->frame_cpu[framenum] = td;
  90. }
  91. }
  92. static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
  93. struct uhci_td *td)
  94. {
  95. /* If it's not inserted, don't remove it */
  96. if (td->frame == -1) {
  97. WARN_ON(!list_empty(&td->fl_list));
  98. return;
  99. }
  100. if (uhci->frame_cpu[td->frame] == td) {
  101. if (list_empty(&td->fl_list)) {
  102. uhci->frame[td->frame] = td->link;
  103. uhci->frame_cpu[td->frame] = NULL;
  104. } else {
  105. struct uhci_td *ntd;
  106. ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
  107. uhci->frame[td->frame] = cpu_to_le32(ntd->dma_handle);
  108. uhci->frame_cpu[td->frame] = ntd;
  109. }
  110. } else {
  111. struct uhci_td *ptd;
  112. ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list);
  113. ptd->link = td->link;
  114. }
  115. list_del_init(&td->fl_list);
  116. td->frame = -1;
  117. }
  118. /*
  119. * Remove all the TDs for an Isochronous URB from the frame list
  120. */
  121. static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
  122. {
  123. struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
  124. struct uhci_td *td;
  125. list_for_each_entry(td, &urbp->td_list, list)
  126. uhci_remove_td_from_frame_list(uhci, td);
  127. wmb();
  128. }
  129. /*
  130. * Remove an URB's TDs from the hardware schedule
  131. */
  132. static void uhci_remove_tds_from_schedule(struct uhci_hcd *uhci,
  133. struct urb *urb, int status)
  134. {
  135. struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
  136. /* Isochronous TDs get unlinked directly from the frame list */
  137. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  138. uhci_unlink_isochronous_tds(uhci, urb);
  139. return;
  140. }
  141. /* If the URB isn't first on its queue, adjust the link pointer
  142. * of the last TD in the previous URB. */
  143. if (urbp->node.prev != &urbp->qh->queue) {
  144. struct urb_priv *purbp;
  145. struct uhci_td *ptd, *ltd;
  146. if (status == -EINPROGRESS)
  147. status = 0;
  148. purbp = list_entry(urbp->node.prev, struct urb_priv, node);
  149. ptd = list_entry(purbp->td_list.prev, struct uhci_td,
  150. list);
  151. ltd = list_entry(urbp->td_list.prev, struct uhci_td,
  152. list);
  153. ptd->link = ltd->link;
  154. }
  155. /* If the URB completed with an error, then the QH element certainly
  156. * points to one of the URB's TDs. If it completed normally then
  157. * the QH element has certainly moved on to the next URB. And if
  158. * the URB is still in progress then it must have been dequeued.
  159. * The QH element either hasn't reached it yet or is somewhere in
  160. * the middle. If the URB wasn't first we can assume that it
  161. * hasn't started yet (see above): Otherwise all the preceding URBs
  162. * would have completed and been removed from the queue, so this one
  163. * _would_ be first.
  164. *
  165. * If the QH element is inside this URB, clear it. It will be
  166. * set properly when the QH is activated.
  167. */
  168. if (status < 0)
  169. urbp->qh->element = UHCI_PTR_TERM;
  170. }
  171. static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
  172. struct usb_device *udev, struct usb_host_endpoint *hep)
  173. {
  174. dma_addr_t dma_handle;
  175. struct uhci_qh *qh;
  176. qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle);
  177. if (!qh)
  178. return NULL;
  179. qh->dma_handle = dma_handle;
  180. qh->element = UHCI_PTR_TERM;
  181. qh->link = UHCI_PTR_TERM;
  182. INIT_LIST_HEAD(&qh->queue);
  183. INIT_LIST_HEAD(&qh->node);
  184. if (udev) { /* Normal QH */
  185. qh->dummy_td = uhci_alloc_td(uhci);
  186. if (!qh->dummy_td) {
  187. dma_pool_free(uhci->qh_pool, qh, dma_handle);
  188. return NULL;
  189. }
  190. qh->state = QH_STATE_IDLE;
  191. qh->hep = hep;
  192. qh->udev = udev;
  193. hep->hcpriv = qh;
  194. usb_get_dev(udev);
  195. } else { /* Skeleton QH */
  196. qh->state = QH_STATE_ACTIVE;
  197. qh->udev = NULL;
  198. }
  199. return qh;
  200. }
  201. static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
  202. {
  203. WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
  204. if (!list_empty(&qh->queue))
  205. dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
  206. list_del(&qh->node);
  207. if (qh->udev) {
  208. qh->hep->hcpriv = NULL;
  209. usb_put_dev(qh->udev);
  210. uhci_free_td(uhci, qh->dummy_td);
  211. }
  212. dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
  213. }
  214. /*
  215. * Put a QH on the schedule in both hardware and software
  216. */
  217. static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
  218. {
  219. struct uhci_qh *pqh;
  220. WARN_ON(list_empty(&qh->queue));
  221. /* Set the element pointer if it isn't set already.
  222. * This isn't needed for Isochronous queues, but it doesn't hurt. */
  223. if (qh_element(qh) == UHCI_PTR_TERM) {
  224. struct urb_priv *urbp = list_entry(qh->queue.next,
  225. struct urb_priv, node);
  226. struct uhci_td *td = list_entry(urbp->td_list.next,
  227. struct uhci_td, list);
  228. qh->element = cpu_to_le32(td->dma_handle);
  229. }
  230. if (qh->state == QH_STATE_ACTIVE)
  231. return;
  232. qh->state = QH_STATE_ACTIVE;
  233. /* Move the QH from its old list to the end of the appropriate
  234. * skeleton's list */
  235. list_move_tail(&qh->node, &qh->skel->node);
  236. /* Link it into the schedule */
  237. pqh = list_entry(qh->node.prev, struct uhci_qh, node);
  238. qh->link = pqh->link;
  239. wmb();
  240. pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
  241. }
  242. /*
  243. * Take a QH off the hardware schedule
  244. */
  245. static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
  246. {
  247. struct uhci_qh *pqh;
  248. if (qh->state == QH_STATE_UNLINKING)
  249. return;
  250. WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
  251. qh->state = QH_STATE_UNLINKING;
  252. /* Unlink the QH from the schedule and record when we did it */
  253. pqh = list_entry(qh->node.prev, struct uhci_qh, node);
  254. pqh->link = qh->link;
  255. mb();
  256. uhci_get_current_frame_number(uhci);
  257. qh->unlink_frame = uhci->frame_number;
  258. /* Force an interrupt so we know when the QH is fully unlinked */
  259. if (list_empty(&uhci->skel_unlink_qh->node))
  260. uhci_set_next_interrupt(uhci);
  261. /* Move the QH from its old list to the end of the unlinking list */
  262. list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
  263. }
  264. /*
  265. * When we and the controller are through with a QH, it becomes IDLE.
  266. * This happens when a QH has been off the schedule (on the unlinking
  267. * list) for more than one frame, or when an error occurs while adding
  268. * the first URB onto a new QH.
  269. */
  270. static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
  271. {
  272. WARN_ON(qh->state == QH_STATE_ACTIVE);
  273. list_move(&qh->node, &uhci->idle_qh_list);
  274. qh->state = QH_STATE_IDLE;
  275. /* If anyone is waiting for a QH to become idle, wake them up */
  276. if (uhci->num_waiting)
  277. wake_up_all(&uhci->waitqh);
  278. }
  279. static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
  280. struct urb *urb)
  281. {
  282. struct urb_priv *urbp;
  283. urbp = kmem_cache_alloc(uhci_up_cachep, SLAB_ATOMIC);
  284. if (!urbp)
  285. return NULL;
  286. memset((void *)urbp, 0, sizeof(*urbp));
  287. urbp->urb = urb;
  288. urb->hcpriv = urbp;
  289. urbp->fsbrtime = jiffies;
  290. INIT_LIST_HEAD(&urbp->node);
  291. INIT_LIST_HEAD(&urbp->td_list);
  292. INIT_LIST_HEAD(&urbp->urb_list);
  293. return urbp;
  294. }
  295. static void uhci_add_td_to_urb(struct urb *urb, struct uhci_td *td)
  296. {
  297. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  298. list_add_tail(&td->list, &urbp->td_list);
  299. }
  300. static void uhci_remove_td_from_urb(struct uhci_td *td)
  301. {
  302. if (list_empty(&td->list))
  303. return;
  304. list_del_init(&td->list);
  305. }
  306. static void uhci_free_urb_priv(struct uhci_hcd *uhci,
  307. struct urb_priv *urbp)
  308. {
  309. struct uhci_td *td, *tmp;
  310. if (!list_empty(&urbp->urb_list))
  311. dev_warn(uhci_dev(uhci), "urb %p still on uhci->urb_list!\n",
  312. urbp->urb);
  313. if (!list_empty(&urbp->node))
  314. dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
  315. urbp->urb);
  316. uhci_get_current_frame_number(uhci);
  317. if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) {
  318. uhci_free_pending_tds(uhci);
  319. uhci->td_remove_age = uhci->frame_number;
  320. }
  321. /* Check to see if the remove list is empty. Set the IOC bit */
  322. /* to force an interrupt so we can remove the TDs. */
  323. if (list_empty(&uhci->td_remove_list))
  324. uhci_set_next_interrupt(uhci);
  325. list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
  326. uhci_remove_td_from_urb(td);
  327. list_add(&td->remove_list, &uhci->td_remove_list);
  328. }
  329. urbp->urb->hcpriv = NULL;
  330. kmem_cache_free(uhci_up_cachep, urbp);
  331. }
  332. static void uhci_inc_fsbr(struct uhci_hcd *uhci, struct urb *urb)
  333. {
  334. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  335. if ((!(urb->transfer_flags & URB_NO_FSBR)) && !urbp->fsbr) {
  336. urbp->fsbr = 1;
  337. if (!uhci->fsbr++ && !uhci->fsbrtimeout)
  338. uhci->skel_term_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
  339. }
  340. }
  341. static void uhci_dec_fsbr(struct uhci_hcd *uhci, struct urb *urb)
  342. {
  343. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  344. if ((!(urb->transfer_flags & URB_NO_FSBR)) && urbp->fsbr) {
  345. urbp->fsbr = 0;
  346. if (!--uhci->fsbr)
  347. uhci->fsbrtimeout = jiffies + FSBR_DELAY;
  348. }
  349. }
  350. /*
  351. * Map status to standard result codes
  352. *
  353. * <status> is (td_status(td) & 0xF60000), a.k.a.
  354. * uhci_status_bits(td_status(td)).
  355. * Note: <status> does not include the TD_CTRL_NAK bit.
  356. * <dir_out> is True for output TDs and False for input TDs.
  357. */
  358. static int uhci_map_status(int status, int dir_out)
  359. {
  360. if (!status)
  361. return 0;
  362. if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
  363. return -EPROTO;
  364. if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
  365. if (dir_out)
  366. return -EPROTO;
  367. else
  368. return -EILSEQ;
  369. }
  370. if (status & TD_CTRL_BABBLE) /* Babble */
  371. return -EOVERFLOW;
  372. if (status & TD_CTRL_DBUFERR) /* Buffer error */
  373. return -ENOSR;
  374. if (status & TD_CTRL_STALLED) /* Stalled */
  375. return -EPIPE;
  376. WARN_ON(status & TD_CTRL_ACTIVE); /* Active */
  377. return 0;
  378. }
  379. /*
  380. * Fix up the data toggles for URBs in a queue, when one of them
  381. * terminates early (short transfer, error, or dequeued).
  382. */
  383. static void uhci_fixup_toggles(struct urb *urb)
  384. {
  385. struct list_head *head;
  386. struct uhci_td *td;
  387. struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
  388. int prevactive = 0;
  389. unsigned int toggle = 0;
  390. struct urb_priv *turbp, *list_end;
  391. /*
  392. * We need to find out what the last successful toggle was so
  393. * we can update the data toggles for the following transfers.
  394. *
  395. * There are 2 ways the last successful completed TD is found:
  396. *
  397. * 1) The TD is NOT active and the actual length < expected length
  398. * 2) The TD is NOT active and it's the last TD in the chain
  399. *
  400. * and a third way the first uncompleted TD is found:
  401. *
  402. * 3) The TD is active and the previous TD is NOT active
  403. */
  404. head = &urbp->td_list;
  405. list_for_each_entry(td, head, list) {
  406. unsigned int ctrlstat = td_status(td);
  407. if (!(ctrlstat & TD_CTRL_ACTIVE) &&
  408. (uhci_actual_length(ctrlstat) <
  409. uhci_expected_length(td_token(td)) ||
  410. td->list.next == head))
  411. toggle = uhci_toggle(td_token(td)) ^ 1;
  412. else if ((ctrlstat & TD_CTRL_ACTIVE) && !prevactive)
  413. toggle = uhci_toggle(td_token(td));
  414. prevactive = ctrlstat & TD_CTRL_ACTIVE;
  415. }
  416. /*
  417. * Fix up the toggle for the following URBs in the queue.
  418. *
  419. * We can stop as soon as we find an URB with toggles set correctly,
  420. * because then all the following URBs will be correct also.
  421. */
  422. list_end = list_entry(&urbp->qh->queue, struct urb_priv, node);
  423. turbp = urbp;
  424. while ((turbp = list_entry(turbp->node.next, struct urb_priv, node))
  425. != list_end) {
  426. td = list_entry(turbp->td_list.next, struct uhci_td, list);
  427. if (uhci_toggle(td_token(td)) == toggle)
  428. return;
  429. list_for_each_entry(td, &turbp->td_list, list) {
  430. td->token ^= __constant_cpu_to_le32(TD_TOKEN_TOGGLE);
  431. toggle ^= 1;
  432. }
  433. }
  434. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  435. usb_pipeout(urb->pipe), toggle);
  436. }
  437. /*
  438. * Control transfers
  439. */
  440. static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
  441. struct uhci_qh *qh)
  442. {
  443. struct uhci_td *td;
  444. unsigned long destination, status;
  445. int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
  446. int len = urb->transfer_buffer_length;
  447. dma_addr_t data = urb->transfer_dma;
  448. __le32 *plink;
  449. /* The "pipe" thing contains the destination in bits 8--18 */
  450. destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
  451. /* 3 errors, dummy TD remains inactive */
  452. status = uhci_maxerr(3);
  453. if (urb->dev->speed == USB_SPEED_LOW)
  454. status |= TD_CTRL_LS;
  455. /*
  456. * Build the TD for the control request setup packet
  457. */
  458. td = qh->dummy_td;
  459. uhci_add_td_to_urb(urb, td);
  460. uhci_fill_td(td, status, destination | uhci_explen(8),
  461. urb->setup_dma);
  462. plink = &td->link;
  463. status |= TD_CTRL_ACTIVE;
  464. /*
  465. * If direction is "send", change the packet ID from SETUP (0x2D)
  466. * to OUT (0xE1). Else change it from SETUP to IN (0x69) and
  467. * set Short Packet Detect (SPD) for all data packets.
  468. */
  469. if (usb_pipeout(urb->pipe))
  470. destination ^= (USB_PID_SETUP ^ USB_PID_OUT);
  471. else {
  472. destination ^= (USB_PID_SETUP ^ USB_PID_IN);
  473. status |= TD_CTRL_SPD;
  474. }
  475. /*
  476. * Build the DATA TDs
  477. */
  478. while (len > 0) {
  479. int pktsze = min(len, maxsze);
  480. td = uhci_alloc_td(uhci);
  481. if (!td)
  482. goto nomem;
  483. *plink = cpu_to_le32(td->dma_handle);
  484. /* Alternate Data0/1 (start with Data1) */
  485. destination ^= TD_TOKEN_TOGGLE;
  486. uhci_add_td_to_urb(urb, td);
  487. uhci_fill_td(td, status, destination | uhci_explen(pktsze),
  488. data);
  489. plink = &td->link;
  490. data += pktsze;
  491. len -= pktsze;
  492. }
  493. /*
  494. * Build the final TD for control status
  495. */
  496. td = uhci_alloc_td(uhci);
  497. if (!td)
  498. goto nomem;
  499. *plink = cpu_to_le32(td->dma_handle);
  500. /*
  501. * It's IN if the pipe is an output pipe or we're not expecting
  502. * data back.
  503. */
  504. destination &= ~TD_TOKEN_PID_MASK;
  505. if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
  506. destination |= USB_PID_IN;
  507. else
  508. destination |= USB_PID_OUT;
  509. destination |= TD_TOKEN_TOGGLE; /* End in Data1 */
  510. status &= ~TD_CTRL_SPD;
  511. uhci_add_td_to_urb(urb, td);
  512. uhci_fill_td(td, status | TD_CTRL_IOC,
  513. destination | uhci_explen(0), 0);
  514. plink = &td->link;
  515. /*
  516. * Build the new dummy TD and activate the old one
  517. */
  518. td = uhci_alloc_td(uhci);
  519. if (!td)
  520. goto nomem;
  521. *plink = cpu_to_le32(td->dma_handle);
  522. uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
  523. wmb();
  524. qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
  525. qh->dummy_td = td;
  526. /* Low-speed transfers get a different queue, and won't hog the bus.
  527. * Also, some devices enumerate better without FSBR; the easiest way
  528. * to do that is to put URBs on the low-speed queue while the device
  529. * isn't in the CONFIGURED state. */
  530. if (urb->dev->speed == USB_SPEED_LOW ||
  531. urb->dev->state != USB_STATE_CONFIGURED)
  532. qh->skel = uhci->skel_ls_control_qh;
  533. else {
  534. qh->skel = uhci->skel_fs_control_qh;
  535. uhci_inc_fsbr(uhci, urb);
  536. }
  537. return 0;
  538. nomem:
  539. /* Remove the dummy TD from the td_list so it doesn't get freed */
  540. uhci_remove_td_from_urb(qh->dummy_td);
  541. return -ENOMEM;
  542. }
  543. /*
  544. * If control-IN transfer was short, the status packet wasn't sent.
  545. * This routine changes the element pointer in the QH to point at the
  546. * status TD. It's safe to do this even while the QH is live, because
  547. * the hardware only updates the element pointer following a successful
  548. * transfer. The inactive TD for the short packet won't cause an update,
  549. * so the pointer won't get overwritten. The next time the controller
  550. * sees this QH, it will send the status packet.
  551. */
  552. static int usb_control_retrigger_status(struct uhci_hcd *uhci, struct urb *urb)
  553. {
  554. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  555. struct uhci_td *td;
  556. urbp->short_transfer = 1;
  557. td = list_entry(urbp->td_list.prev, struct uhci_td, list);
  558. urbp->qh->element = cpu_to_le32(td->dma_handle);
  559. return -EINPROGRESS;
  560. }
  561. static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
  562. {
  563. struct list_head *tmp, *head;
  564. struct urb_priv *urbp = urb->hcpriv;
  565. struct uhci_td *td;
  566. unsigned int status;
  567. int ret = 0;
  568. head = &urbp->td_list;
  569. if (urbp->short_transfer) {
  570. tmp = head->prev;
  571. goto status_stage;
  572. }
  573. urb->actual_length = 0;
  574. tmp = head->next;
  575. td = list_entry(tmp, struct uhci_td, list);
  576. /* The first TD is the SETUP stage, check the status, but skip */
  577. /* the count */
  578. status = uhci_status_bits(td_status(td));
  579. if (status & TD_CTRL_ACTIVE)
  580. return -EINPROGRESS;
  581. if (status)
  582. goto td_error;
  583. /* The rest of the TDs (but the last) are data */
  584. tmp = tmp->next;
  585. while (tmp != head && tmp->next != head) {
  586. unsigned int ctrlstat;
  587. td = list_entry(tmp, struct uhci_td, list);
  588. tmp = tmp->next;
  589. ctrlstat = td_status(td);
  590. status = uhci_status_bits(ctrlstat);
  591. if (status & TD_CTRL_ACTIVE)
  592. return -EINPROGRESS;
  593. urb->actual_length += uhci_actual_length(ctrlstat);
  594. if (status)
  595. goto td_error;
  596. /* Check to see if we received a short packet */
  597. if (uhci_actual_length(ctrlstat) <
  598. uhci_expected_length(td_token(td))) {
  599. if (urb->transfer_flags & URB_SHORT_NOT_OK) {
  600. ret = -EREMOTEIO;
  601. goto err;
  602. }
  603. return usb_control_retrigger_status(uhci, urb);
  604. }
  605. }
  606. status_stage:
  607. td = list_entry(tmp, struct uhci_td, list);
  608. /* Control status stage */
  609. status = td_status(td);
  610. #ifdef I_HAVE_BUGGY_APC_BACKUPS
  611. /* APC BackUPS Pro kludge */
  612. /* It tries to send all of the descriptor instead of the amount */
  613. /* we requested */
  614. if (status & TD_CTRL_IOC && /* IOC is masked out by uhci_status_bits */
  615. status & TD_CTRL_ACTIVE &&
  616. status & TD_CTRL_NAK)
  617. return 0;
  618. #endif
  619. status = uhci_status_bits(status);
  620. if (status & TD_CTRL_ACTIVE)
  621. return -EINPROGRESS;
  622. if (status)
  623. goto td_error;
  624. return 0;
  625. td_error:
  626. ret = uhci_map_status(status, uhci_packetout(td_token(td)));
  627. err:
  628. if ((debug == 1 && ret != -EPIPE) || debug > 1) {
  629. /* Some debugging code */
  630. dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n",
  631. __FUNCTION__, status);
  632. if (errbuf) {
  633. /* Print the chain for debugging purposes */
  634. uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
  635. lprintk(errbuf);
  636. }
  637. }
  638. return ret;
  639. }
  640. /*
  641. * Common submit for bulk and interrupt
  642. */
  643. static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
  644. struct uhci_qh *qh)
  645. {
  646. struct uhci_td *td;
  647. unsigned long destination, status;
  648. int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
  649. int len = urb->transfer_buffer_length;
  650. dma_addr_t data = urb->transfer_dma;
  651. __le32 *plink;
  652. unsigned int toggle;
  653. if (len < 0)
  654. return -EINVAL;
  655. /* The "pipe" thing contains the destination in bits 8--18 */
  656. destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
  657. toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  658. usb_pipeout(urb->pipe));
  659. /* 3 errors, dummy TD remains inactive */
  660. status = uhci_maxerr(3);
  661. if (urb->dev->speed == USB_SPEED_LOW)
  662. status |= TD_CTRL_LS;
  663. if (usb_pipein(urb->pipe))
  664. status |= TD_CTRL_SPD;
  665. /*
  666. * Build the DATA TDs
  667. */
  668. plink = NULL;
  669. td = qh->dummy_td;
  670. do { /* Allow zero length packets */
  671. int pktsze = maxsze;
  672. if (len <= pktsze) { /* The last packet */
  673. pktsze = len;
  674. if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
  675. status &= ~TD_CTRL_SPD;
  676. }
  677. if (plink) {
  678. td = uhci_alloc_td(uhci);
  679. if (!td)
  680. goto nomem;
  681. *plink = cpu_to_le32(td->dma_handle);
  682. }
  683. uhci_add_td_to_urb(urb, td);
  684. uhci_fill_td(td, status,
  685. destination | uhci_explen(pktsze) |
  686. (toggle << TD_TOKEN_TOGGLE_SHIFT),
  687. data);
  688. plink = &td->link;
  689. status |= TD_CTRL_ACTIVE;
  690. data += pktsze;
  691. len -= maxsze;
  692. toggle ^= 1;
  693. } while (len > 0);
  694. /*
  695. * URB_ZERO_PACKET means adding a 0-length packet, if direction
  696. * is OUT and the transfer_length was an exact multiple of maxsze,
  697. * hence (len = transfer_length - N * maxsze) == 0
  698. * however, if transfer_length == 0, the zero packet was already
  699. * prepared above.
  700. */
  701. if ((urb->transfer_flags & URB_ZERO_PACKET) &&
  702. usb_pipeout(urb->pipe) && len == 0 &&
  703. urb->transfer_buffer_length > 0) {
  704. td = uhci_alloc_td(uhci);
  705. if (!td)
  706. goto nomem;
  707. *plink = cpu_to_le32(td->dma_handle);
  708. uhci_add_td_to_urb(urb, td);
  709. uhci_fill_td(td, status,
  710. destination | uhci_explen(0) |
  711. (toggle << TD_TOKEN_TOGGLE_SHIFT),
  712. data);
  713. plink = &td->link;
  714. toggle ^= 1;
  715. }
  716. /* Set the interrupt-on-completion flag on the last packet.
  717. * A more-or-less typical 4 KB URB (= size of one memory page)
  718. * will require about 3 ms to transfer; that's a little on the
  719. * fast side but not enough to justify delaying an interrupt
  720. * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
  721. * flag setting. */
  722. td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
  723. /*
  724. * Build the new dummy TD and activate the old one
  725. */
  726. td = uhci_alloc_td(uhci);
  727. if (!td)
  728. goto nomem;
  729. *plink = cpu_to_le32(td->dma_handle);
  730. uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
  731. wmb();
  732. qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
  733. qh->dummy_td = td;
  734. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  735. usb_pipeout(urb->pipe), toggle);
  736. return 0;
  737. nomem:
  738. /* Remove the dummy TD from the td_list so it doesn't get freed */
  739. uhci_remove_td_from_urb(qh->dummy_td);
  740. return -ENOMEM;
  741. }
  742. /*
  743. * Common result for bulk and interrupt
  744. */
  745. static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
  746. {
  747. struct urb_priv *urbp = urb->hcpriv;
  748. struct uhci_td *td;
  749. unsigned int status = 0;
  750. int ret = 0;
  751. urb->actual_length = 0;
  752. list_for_each_entry(td, &urbp->td_list, list) {
  753. unsigned int ctrlstat = td_status(td);
  754. status = uhci_status_bits(ctrlstat);
  755. if (status & TD_CTRL_ACTIVE)
  756. return -EINPROGRESS;
  757. urb->actual_length += uhci_actual_length(ctrlstat);
  758. if (status)
  759. goto td_error;
  760. if (uhci_actual_length(ctrlstat) <
  761. uhci_expected_length(td_token(td))) {
  762. if (urb->transfer_flags & URB_SHORT_NOT_OK) {
  763. ret = -EREMOTEIO;
  764. goto err;
  765. }
  766. /*
  767. * This URB stopped short of its end. We have to
  768. * fix up the toggles of the following URBs on the
  769. * queue and restart the queue.
  770. *
  771. * Do this only the first time we encounter the
  772. * short URB.
  773. */
  774. if (!urbp->short_transfer) {
  775. urbp->short_transfer = 1;
  776. uhci_fixup_toggles(urb);
  777. td = list_entry(urbp->td_list.prev,
  778. struct uhci_td, list);
  779. urbp->qh->element = td->link;
  780. }
  781. break;
  782. }
  783. }
  784. return 0;
  785. td_error:
  786. ret = uhci_map_status(status, uhci_packetout(td_token(td)));
  787. err:
  788. /*
  789. * Enable this chunk of code if you want to see some more debugging.
  790. * But be careful, it has the tendancy to starve out khubd and prevent
  791. * disconnects from happening successfully if you have a slow debug
  792. * log interface (like a serial console.
  793. */
  794. #if 0
  795. if ((debug == 1 && ret != -EPIPE) || debug > 1) {
  796. /* Some debugging code */
  797. dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n",
  798. __FUNCTION__, status);
  799. if (errbuf) {
  800. /* Print the chain for debugging purposes */
  801. uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
  802. lprintk(errbuf);
  803. }
  804. }
  805. #endif
  806. return ret;
  807. }
  808. static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
  809. struct uhci_qh *qh)
  810. {
  811. int ret;
  812. /* Can't have low-speed bulk transfers */
  813. if (urb->dev->speed == USB_SPEED_LOW)
  814. return -EINVAL;
  815. qh->skel = uhci->skel_bulk_qh;
  816. ret = uhci_submit_common(uhci, urb, qh);
  817. if (ret == 0)
  818. uhci_inc_fsbr(uhci, urb);
  819. return ret;
  820. }
  821. static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
  822. struct uhci_qh *qh)
  823. {
  824. /* USB 1.1 interrupt transfers only involve one packet per interval.
  825. * Drivers can submit URBs of any length, but longer ones will need
  826. * multiple intervals to complete.
  827. */
  828. qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
  829. return uhci_submit_common(uhci, urb, qh);
  830. }
  831. /*
  832. * Isochronous transfers
  833. */
  834. static int isochronous_find_limits(struct uhci_hcd *uhci, struct urb *urb, unsigned int *start, unsigned int *end)
  835. {
  836. struct urb *last_urb = NULL;
  837. struct urb_priv *up;
  838. int ret = 0;
  839. list_for_each_entry(up, &uhci->urb_list, urb_list) {
  840. struct urb *u = up->urb;
  841. /* look for pending URBs with identical pipe handle */
  842. if ((urb->pipe == u->pipe) && (urb->dev == u->dev) &&
  843. (u->status == -EINPROGRESS) && (u != urb)) {
  844. if (!last_urb)
  845. *start = u->start_frame;
  846. last_urb = u;
  847. }
  848. }
  849. if (last_urb) {
  850. *end = (last_urb->start_frame + last_urb->number_of_packets *
  851. last_urb->interval) & (UHCI_NUMFRAMES-1);
  852. ret = 0;
  853. } else
  854. ret = -1; /* no previous urb found */
  855. return ret;
  856. }
  857. static int isochronous_find_start(struct uhci_hcd *uhci, struct urb *urb)
  858. {
  859. int limits;
  860. unsigned int start = 0, end = 0;
  861. if (urb->number_of_packets > 900) /* 900? Why? */
  862. return -EFBIG;
  863. limits = isochronous_find_limits(uhci, urb, &start, &end);
  864. if (urb->transfer_flags & URB_ISO_ASAP) {
  865. if (limits) {
  866. uhci_get_current_frame_number(uhci);
  867. urb->start_frame = (uhci->frame_number + 10)
  868. & (UHCI_NUMFRAMES - 1);
  869. } else
  870. urb->start_frame = end;
  871. } else {
  872. urb->start_frame &= (UHCI_NUMFRAMES - 1);
  873. /* FIXME: Sanity check */
  874. }
  875. return 0;
  876. }
  877. /*
  878. * Isochronous transfers
  879. */
  880. static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
  881. struct uhci_qh *qh)
  882. {
  883. struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
  884. int i, ret, frame;
  885. unsigned long destination, status;
  886. struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
  887. status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
  888. destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
  889. ret = isochronous_find_start(uhci, urb);
  890. if (ret)
  891. return ret;
  892. for (i = 0; i < urb->number_of_packets; i++) {
  893. td = uhci_alloc_td(uhci);
  894. if (!td)
  895. return -ENOMEM;
  896. uhci_add_td_to_urb(urb, td);
  897. uhci_fill_td(td, status, destination |
  898. uhci_explen(urb->iso_frame_desc[i].length),
  899. urb->transfer_dma +
  900. urb->iso_frame_desc[i].offset);
  901. }
  902. /* Set the interrupt-on-completion flag on the last packet. */
  903. td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
  904. qh->skel = uhci->skel_iso_qh;
  905. /* Add the TDs to the frame list */
  906. frame = urb->start_frame;
  907. list_for_each_entry(td, &urbp->td_list, list) {
  908. uhci_insert_td_in_frame_list(uhci, td, frame);
  909. frame += urb->interval;
  910. }
  911. return 0;
  912. }
  913. static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
  914. {
  915. struct uhci_td *td;
  916. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  917. int status;
  918. int i, ret = 0;
  919. urb->actual_length = urb->error_count = 0;
  920. i = 0;
  921. list_for_each_entry(td, &urbp->td_list, list) {
  922. int actlength;
  923. unsigned int ctrlstat = td_status(td);
  924. if (ctrlstat & TD_CTRL_ACTIVE)
  925. return -EINPROGRESS;
  926. actlength = uhci_actual_length(ctrlstat);
  927. urb->iso_frame_desc[i].actual_length = actlength;
  928. urb->actual_length += actlength;
  929. status = uhci_map_status(uhci_status_bits(ctrlstat),
  930. usb_pipeout(urb->pipe));
  931. urb->iso_frame_desc[i].status = status;
  932. if (status) {
  933. urb->error_count++;
  934. ret = status;
  935. }
  936. i++;
  937. }
  938. return ret;
  939. }
  940. static int uhci_urb_enqueue(struct usb_hcd *hcd,
  941. struct usb_host_endpoint *hep,
  942. struct urb *urb, gfp_t mem_flags)
  943. {
  944. int ret;
  945. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  946. unsigned long flags;
  947. struct urb_priv *urbp;
  948. struct uhci_qh *qh;
  949. int bustime;
  950. spin_lock_irqsave(&uhci->lock, flags);
  951. ret = urb->status;
  952. if (ret != -EINPROGRESS) /* URB already unlinked! */
  953. goto done;
  954. ret = -ENOMEM;
  955. urbp = uhci_alloc_urb_priv(uhci, urb);
  956. if (!urbp)
  957. goto done;
  958. if (hep->hcpriv)
  959. qh = (struct uhci_qh *) hep->hcpriv;
  960. else {
  961. qh = uhci_alloc_qh(uhci, urb->dev, hep);
  962. if (!qh)
  963. goto err_no_qh;
  964. }
  965. urbp->qh = qh;
  966. switch (usb_pipetype(urb->pipe)) {
  967. case PIPE_CONTROL:
  968. ret = uhci_submit_control(uhci, urb, qh);
  969. break;
  970. case PIPE_BULK:
  971. ret = uhci_submit_bulk(uhci, urb, qh);
  972. break;
  973. case PIPE_INTERRUPT:
  974. if (list_empty(&qh->queue)) {
  975. bustime = usb_check_bandwidth(urb->dev, urb);
  976. if (bustime < 0)
  977. ret = bustime;
  978. else {
  979. ret = uhci_submit_interrupt(uhci, urb, qh);
  980. if (ret == 0)
  981. usb_claim_bandwidth(urb->dev, urb, bustime, 0);
  982. }
  983. } else { /* inherit from parent */
  984. struct urb_priv *eurbp;
  985. eurbp = list_entry(qh->queue.prev, struct urb_priv,
  986. node);
  987. urb->bandwidth = eurbp->urb->bandwidth;
  988. ret = uhci_submit_interrupt(uhci, urb, qh);
  989. }
  990. break;
  991. case PIPE_ISOCHRONOUS:
  992. bustime = usb_check_bandwidth(urb->dev, urb);
  993. if (bustime < 0) {
  994. ret = bustime;
  995. break;
  996. }
  997. ret = uhci_submit_isochronous(uhci, urb, qh);
  998. if (ret == 0)
  999. usb_claim_bandwidth(urb->dev, urb, bustime, 1);
  1000. break;
  1001. }
  1002. if (ret != 0)
  1003. goto err_submit_failed;
  1004. /* Add this URB to the QH */
  1005. urbp->qh = qh;
  1006. list_add_tail(&urbp->node, &qh->queue);
  1007. list_add_tail(&urbp->urb_list, &uhci->urb_list);
  1008. /* If the new URB is the first and only one on this QH then either
  1009. * the QH is new and idle or else it's unlinked and waiting to
  1010. * become idle, so we can activate it right away. */
  1011. if (qh->queue.next == &urbp->node)
  1012. uhci_activate_qh(uhci, qh);
  1013. goto done;
  1014. err_submit_failed:
  1015. if (qh->state == QH_STATE_IDLE)
  1016. uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
  1017. err_no_qh:
  1018. uhci_free_urb_priv(uhci, urbp);
  1019. done:
  1020. spin_unlock_irqrestore(&uhci->lock, flags);
  1021. return ret;
  1022. }
  1023. /*
  1024. * Return the result of a transfer
  1025. */
  1026. static void uhci_transfer_result(struct uhci_hcd *uhci, struct urb *urb)
  1027. {
  1028. int status;
  1029. int okay_to_giveback = 0;
  1030. struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
  1031. switch (usb_pipetype(urb->pipe)) {
  1032. case PIPE_CONTROL:
  1033. status = uhci_result_control(uhci, urb);
  1034. break;
  1035. case PIPE_ISOCHRONOUS:
  1036. status = uhci_result_isochronous(uhci, urb);
  1037. break;
  1038. default: /* PIPE_BULK or PIPE_INTERRUPT */
  1039. status = uhci_result_common(uhci, urb);
  1040. break;
  1041. }
  1042. spin_lock(&urb->lock);
  1043. if (urb->status == -EINPROGRESS) { /* Not yet dequeued */
  1044. if (status != -EINPROGRESS) { /* URB has completed */
  1045. urb->status = status;
  1046. /* If the URB got a real error (as opposed to
  1047. * simply being dequeued), we don't have to
  1048. * unlink the QH. Fix this later... */
  1049. if (status < 0)
  1050. uhci_unlink_qh(uhci, urbp->qh);
  1051. else
  1052. okay_to_giveback = 1;
  1053. }
  1054. } else { /* Already dequeued */
  1055. if (urbp->qh->state == QH_STATE_UNLINKING &&
  1056. uhci->frame_number + uhci->is_stopped !=
  1057. urbp->qh->unlink_frame)
  1058. okay_to_giveback = 1;
  1059. }
  1060. spin_unlock(&urb->lock);
  1061. if (!okay_to_giveback)
  1062. return;
  1063. switch (usb_pipetype(urb->pipe)) {
  1064. case PIPE_ISOCHRONOUS:
  1065. /* Release bandwidth for Interrupt or Isoc. transfers */
  1066. if (urb->bandwidth)
  1067. usb_release_bandwidth(urb->dev, urb, 1);
  1068. break;
  1069. case PIPE_INTERRUPT:
  1070. /* Release bandwidth for Interrupt or Isoc. transfers */
  1071. /* Make sure we don't release if we have a queued URB */
  1072. if (list_empty(&urbp->qh->queue) && urb->bandwidth)
  1073. usb_release_bandwidth(urb->dev, urb, 0);
  1074. else
  1075. /* bandwidth was passed on to queued URB, */
  1076. /* so don't let usb_unlink_urb() release it */
  1077. urb->bandwidth = 0;
  1078. /* Falls through */
  1079. case PIPE_BULK:
  1080. if (status < 0)
  1081. uhci_fixup_toggles(urb);
  1082. break;
  1083. default: /* PIPE_CONTROL */
  1084. break;
  1085. }
  1086. /* Take the URB's TDs off the hardware schedule */
  1087. uhci_remove_tds_from_schedule(uhci, urb, status);
  1088. /* Take the URB off the QH's queue and see if the QH is now unused */
  1089. list_del_init(&urbp->node);
  1090. if (list_empty(&urbp->qh->queue))
  1091. uhci_unlink_qh(uhci, urbp->qh);
  1092. uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
  1093. /* Queue it for giving back */
  1094. list_move_tail(&urbp->urb_list, &uhci->complete_list);
  1095. }
  1096. /*
  1097. * Check out the QHs waiting to be fully unlinked
  1098. */
  1099. static void uhci_scan_unlinking_qhs(struct uhci_hcd *uhci)
  1100. {
  1101. struct uhci_qh *qh, *tmp;
  1102. list_for_each_entry_safe(qh, tmp, &uhci->skel_unlink_qh->node, node) {
  1103. /* If the queue is empty and the QH is fully unlinked then
  1104. * it can become IDLE. */
  1105. if (list_empty(&qh->queue)) {
  1106. if (uhci->frame_number + uhci->is_stopped !=
  1107. qh->unlink_frame)
  1108. uhci_make_qh_idle(uhci, qh);
  1109. /* If none of the QH's URBs have been dequeued then the QH
  1110. * should be re-activated. */
  1111. } else {
  1112. struct urb_priv *urbp;
  1113. int any_dequeued = 0;
  1114. list_for_each_entry(urbp, &qh->queue, node) {
  1115. if (urbp->urb->status != -EINPROGRESS) {
  1116. any_dequeued = 1;
  1117. break;
  1118. }
  1119. }
  1120. if (!any_dequeued)
  1121. uhci_activate_qh(uhci, qh);
  1122. }
  1123. }
  1124. }
  1125. static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  1126. {
  1127. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  1128. unsigned long flags;
  1129. struct urb_priv *urbp;
  1130. spin_lock_irqsave(&uhci->lock, flags);
  1131. urbp = urb->hcpriv;
  1132. if (!urbp) /* URB was never linked! */
  1133. goto done;
  1134. /* Remove Isochronous TDs from the frame list ASAP */
  1135. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  1136. uhci_unlink_isochronous_tds(uhci, urb);
  1137. uhci_unlink_qh(uhci, urbp->qh);
  1138. done:
  1139. spin_unlock_irqrestore(&uhci->lock, flags);
  1140. return 0;
  1141. }
  1142. static int uhci_fsbr_timeout(struct uhci_hcd *uhci, struct urb *urb)
  1143. {
  1144. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  1145. struct list_head *head;
  1146. struct uhci_td *td;
  1147. int count = 0;
  1148. uhci_dec_fsbr(uhci, urb);
  1149. urbp->fsbr_timeout = 1;
  1150. /*
  1151. * Ideally we would want to fix qh->element as well, but it's
  1152. * read/write by the HC, so that can introduce a race. It's not
  1153. * really worth the hassle
  1154. */
  1155. head = &urbp->td_list;
  1156. list_for_each_entry(td, head, list) {
  1157. /*
  1158. * Make sure we don't do the last one (since it'll have the
  1159. * TERM bit set) as well as we skip every so many TDs to
  1160. * make sure it doesn't hog the bandwidth
  1161. */
  1162. if (td->list.next != head && (count % DEPTH_INTERVAL) ==
  1163. (DEPTH_INTERVAL - 1))
  1164. td->link |= UHCI_PTR_DEPTH;
  1165. count++;
  1166. }
  1167. return 0;
  1168. }
  1169. static void uhci_free_pending_tds(struct uhci_hcd *uhci)
  1170. {
  1171. struct uhci_td *td, *tmp;
  1172. list_for_each_entry_safe(td, tmp, &uhci->td_remove_list, remove_list) {
  1173. list_del_init(&td->remove_list);
  1174. uhci_free_td(uhci, td);
  1175. }
  1176. }
  1177. static void
  1178. uhci_finish_urb(struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
  1179. __releases(uhci->lock)
  1180. __acquires(uhci->lock)
  1181. {
  1182. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  1183. uhci_free_urb_priv(uhci, (struct urb_priv *) (urb->hcpriv));
  1184. spin_unlock(&uhci->lock);
  1185. usb_hcd_giveback_urb(hcd, urb, regs);
  1186. spin_lock(&uhci->lock);
  1187. }
  1188. static void uhci_finish_completion(struct uhci_hcd *uhci, struct pt_regs *regs)
  1189. {
  1190. struct urb_priv *urbp, *tmp;
  1191. list_for_each_entry_safe(urbp, tmp, &uhci->complete_list, urb_list) {
  1192. struct urb *urb = urbp->urb;
  1193. list_del_init(&urbp->urb_list);
  1194. uhci_finish_urb(uhci_to_hcd(uhci), urb, regs);
  1195. }
  1196. }
  1197. /* Process events in the schedule, but only in one thread at a time */
  1198. static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
  1199. {
  1200. struct urb_priv *urbp, *tmp;
  1201. /* Don't allow re-entrant calls */
  1202. if (uhci->scan_in_progress) {
  1203. uhci->need_rescan = 1;
  1204. return;
  1205. }
  1206. uhci->scan_in_progress = 1;
  1207. rescan:
  1208. uhci->need_rescan = 0;
  1209. uhci_clear_next_interrupt(uhci);
  1210. uhci_get_current_frame_number(uhci);
  1211. if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age)
  1212. uhci_free_pending_tds(uhci);
  1213. /* Walk the list of pending URBs to see which ones completed
  1214. * (must be _safe because uhci_transfer_result() dequeues URBs) */
  1215. list_for_each_entry_safe(urbp, tmp, &uhci->urb_list, urb_list) {
  1216. struct urb *urb = urbp->urb;
  1217. /* Checks the status and does all of the magic necessary */
  1218. uhci_transfer_result(uhci, urb);
  1219. }
  1220. uhci_finish_completion(uhci, regs);
  1221. /* If the controller is stopped, we can finish these off right now */
  1222. if (uhci->is_stopped)
  1223. uhci_free_pending_tds(uhci);
  1224. if (uhci->need_rescan)
  1225. goto rescan;
  1226. uhci->scan_in_progress = 0;
  1227. /* Check out the QHs waiting for unlinking */
  1228. uhci_scan_unlinking_qhs(uhci);
  1229. if (list_empty(&uhci->td_remove_list) &&
  1230. list_empty(&uhci->skel_unlink_qh->node))
  1231. uhci_clear_next_interrupt(uhci);
  1232. else
  1233. uhci_set_next_interrupt(uhci);
  1234. }
  1235. static void check_fsbr(struct uhci_hcd *uhci)
  1236. {
  1237. struct urb_priv *up;
  1238. list_for_each_entry(up, &uhci->urb_list, urb_list) {
  1239. struct urb *u = up->urb;
  1240. spin_lock(&u->lock);
  1241. /* Check if the FSBR timed out */
  1242. if (up->fsbr && !up->fsbr_timeout && time_after_eq(jiffies, up->fsbrtime + IDLE_TIMEOUT))
  1243. uhci_fsbr_timeout(uhci, u);
  1244. spin_unlock(&u->lock);
  1245. }
  1246. /* Really disable FSBR */
  1247. if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) {
  1248. uhci->fsbrtimeout = 0;
  1249. uhci->skel_term_qh->link = UHCI_PTR_TERM;
  1250. }
  1251. }