ohci-q.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /*
  2. * OHCI HCD (Host Controller Driver) for USB.
  3. *
  4. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  5. * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
  6. *
  7. * This file is licenced under the GPL.
  8. */
  9. #include <linux/irq.h>
  10. #include <linux/slab.h>
  11. static void urb_free_priv (struct ohci_hcd *hc, urb_priv_t *urb_priv)
  12. {
  13. int last = urb_priv->length - 1;
  14. if (last >= 0) {
  15. int i;
  16. struct td *td;
  17. for (i = 0; i <= last; i++) {
  18. td = urb_priv->td [i];
  19. if (td)
  20. td_free (hc, td);
  21. }
  22. }
  23. list_del (&urb_priv->pending);
  24. kfree (urb_priv);
  25. }
  26. /*-------------------------------------------------------------------------*/
  27. /*
  28. * URB goes back to driver, and isn't reissued.
  29. * It's completely gone from HC data structures.
  30. * PRECONDITION: ohci lock held, irqs blocked.
  31. */
  32. static void
  33. finish_urb(struct ohci_hcd *ohci, struct urb *urb, int status)
  34. __releases(ohci->lock)
  35. __acquires(ohci->lock)
  36. {
  37. struct device *dev = ohci_to_hcd(ohci)->self.controller;
  38. struct usb_host_endpoint *ep = urb->ep;
  39. struct urb_priv *urb_priv;
  40. // ASSERT (urb->hcpriv != 0);
  41. restart:
  42. urb_free_priv (ohci, urb->hcpriv);
  43. urb->hcpriv = NULL;
  44. if (likely(status == -EINPROGRESS))
  45. status = 0;
  46. switch (usb_pipetype (urb->pipe)) {
  47. case PIPE_ISOCHRONOUS:
  48. ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs--;
  49. if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0) {
  50. if (quirk_amdiso(ohci))
  51. usb_amd_quirk_pll_enable();
  52. if (quirk_amdprefetch(ohci))
  53. sb800_prefetch(dev, 0);
  54. }
  55. break;
  56. case PIPE_INTERRUPT:
  57. ohci_to_hcd(ohci)->self.bandwidth_int_reqs--;
  58. break;
  59. }
  60. /* urb->complete() can reenter this HCD */
  61. usb_hcd_unlink_urb_from_ep(ohci_to_hcd(ohci), urb);
  62. spin_unlock (&ohci->lock);
  63. usb_hcd_giveback_urb(ohci_to_hcd(ohci), urb, status);
  64. spin_lock (&ohci->lock);
  65. /* stop periodic dma if it's not needed */
  66. if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0
  67. && ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0) {
  68. ohci->hc_control &= ~(OHCI_CTRL_PLE|OHCI_CTRL_IE);
  69. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  70. }
  71. /*
  72. * An isochronous URB that is sumitted too late won't have any TDs
  73. * (marked by the fact that the td_cnt value is larger than the
  74. * actual number of TDs). If the next URB on this endpoint is like
  75. * that, give it back now.
  76. */
  77. if (!list_empty(&ep->urb_list)) {
  78. urb = list_first_entry(&ep->urb_list, struct urb, urb_list);
  79. urb_priv = urb->hcpriv;
  80. if (urb_priv->td_cnt > urb_priv->length) {
  81. status = 0;
  82. goto restart;
  83. }
  84. }
  85. }
  86. /*-------------------------------------------------------------------------*
  87. * ED handling functions
  88. *-------------------------------------------------------------------------*/
  89. /* search for the right schedule branch to use for a periodic ed.
  90. * does some load balancing; returns the branch, or negative errno.
  91. */
  92. static int balance (struct ohci_hcd *ohci, int interval, int load)
  93. {
  94. int i, branch = -ENOSPC;
  95. /* iso periods can be huge; iso tds specify frame numbers */
  96. if (interval > NUM_INTS)
  97. interval = NUM_INTS;
  98. /* search for the least loaded schedule branch of that period
  99. * that has enough bandwidth left unreserved.
  100. */
  101. for (i = 0; i < interval ; i++) {
  102. if (branch < 0 || ohci->load [branch] > ohci->load [i]) {
  103. int j;
  104. /* usb 1.1 says 90% of one frame */
  105. for (j = i; j < NUM_INTS; j += interval) {
  106. if ((ohci->load [j] + load) > 900)
  107. break;
  108. }
  109. if (j < NUM_INTS)
  110. continue;
  111. branch = i;
  112. }
  113. }
  114. return branch;
  115. }
  116. /*-------------------------------------------------------------------------*/
  117. /* both iso and interrupt requests have periods; this routine puts them
  118. * into the schedule tree in the apppropriate place. most iso devices use
  119. * 1msec periods, but that's not required.
  120. */
  121. static void periodic_link (struct ohci_hcd *ohci, struct ed *ed)
  122. {
  123. unsigned i;
  124. ohci_dbg(ohci, "link %sed %p branch %d [%dus.], interval %d\n",
  125. (ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
  126. ed, ed->branch, ed->load, ed->interval);
  127. for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
  128. struct ed **prev = &ohci->periodic [i];
  129. __hc32 *prev_p = &ohci->hcca->int_table [i];
  130. struct ed *here = *prev;
  131. /* sorting each branch by period (slow before fast)
  132. * lets us share the faster parts of the tree.
  133. * (plus maybe: put interrupt eds before iso)
  134. */
  135. while (here && ed != here) {
  136. if (ed->interval > here->interval)
  137. break;
  138. prev = &here->ed_next;
  139. prev_p = &here->hwNextED;
  140. here = *prev;
  141. }
  142. if (ed != here) {
  143. ed->ed_next = here;
  144. if (here)
  145. ed->hwNextED = *prev_p;
  146. wmb ();
  147. *prev = ed;
  148. *prev_p = cpu_to_hc32(ohci, ed->dma);
  149. wmb();
  150. }
  151. ohci->load [i] += ed->load;
  152. }
  153. ohci_to_hcd(ohci)->self.bandwidth_allocated += ed->load / ed->interval;
  154. }
  155. /* link an ed into one of the HC chains */
  156. static int ed_schedule (struct ohci_hcd *ohci, struct ed *ed)
  157. {
  158. int branch;
  159. ed->state = ED_OPER;
  160. ed->ed_prev = NULL;
  161. ed->ed_next = NULL;
  162. ed->hwNextED = 0;
  163. wmb ();
  164. /* we care about rm_list when setting CLE/BLE in case the HC was at
  165. * work on some TD when CLE/BLE was turned off, and isn't quiesced
  166. * yet. finish_unlinks() restarts as needed, some upcoming INTR_SF.
  167. *
  168. * control and bulk EDs are doubly linked (ed_next, ed_prev), but
  169. * periodic ones are singly linked (ed_next). that's because the
  170. * periodic schedule encodes a tree like figure 3-5 in the ohci
  171. * spec: each qh can have several "previous" nodes, and the tree
  172. * doesn't have unused/idle descriptors.
  173. */
  174. switch (ed->type) {
  175. case PIPE_CONTROL:
  176. if (ohci->ed_controltail == NULL) {
  177. WARN_ON (ohci->hc_control & OHCI_CTRL_CLE);
  178. ohci_writel (ohci, ed->dma,
  179. &ohci->regs->ed_controlhead);
  180. } else {
  181. ohci->ed_controltail->ed_next = ed;
  182. ohci->ed_controltail->hwNextED = cpu_to_hc32 (ohci,
  183. ed->dma);
  184. }
  185. ed->ed_prev = ohci->ed_controltail;
  186. if (!ohci->ed_controltail && !ohci->ed_rm_list) {
  187. wmb();
  188. ohci->hc_control |= OHCI_CTRL_CLE;
  189. ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
  190. ohci_writel (ohci, ohci->hc_control,
  191. &ohci->regs->control);
  192. }
  193. ohci->ed_controltail = ed;
  194. break;
  195. case PIPE_BULK:
  196. if (ohci->ed_bulktail == NULL) {
  197. WARN_ON (ohci->hc_control & OHCI_CTRL_BLE);
  198. ohci_writel (ohci, ed->dma, &ohci->regs->ed_bulkhead);
  199. } else {
  200. ohci->ed_bulktail->ed_next = ed;
  201. ohci->ed_bulktail->hwNextED = cpu_to_hc32 (ohci,
  202. ed->dma);
  203. }
  204. ed->ed_prev = ohci->ed_bulktail;
  205. if (!ohci->ed_bulktail && !ohci->ed_rm_list) {
  206. wmb();
  207. ohci->hc_control |= OHCI_CTRL_BLE;
  208. ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
  209. ohci_writel (ohci, ohci->hc_control,
  210. &ohci->regs->control);
  211. }
  212. ohci->ed_bulktail = ed;
  213. break;
  214. // case PIPE_INTERRUPT:
  215. // case PIPE_ISOCHRONOUS:
  216. default:
  217. branch = balance (ohci, ed->interval, ed->load);
  218. if (branch < 0) {
  219. ohci_dbg (ohci,
  220. "ERR %d, interval %d msecs, load %d\n",
  221. branch, ed->interval, ed->load);
  222. // FIXME if there are TDs queued, fail them!
  223. return branch;
  224. }
  225. ed->branch = branch;
  226. periodic_link (ohci, ed);
  227. }
  228. /* the HC may not see the schedule updates yet, but if it does
  229. * then they'll be properly ordered.
  230. */
  231. return 0;
  232. }
  233. /*-------------------------------------------------------------------------*/
  234. /* scan the periodic table to find and unlink this ED */
  235. static void periodic_unlink (struct ohci_hcd *ohci, struct ed *ed)
  236. {
  237. int i;
  238. for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
  239. struct ed *temp;
  240. struct ed **prev = &ohci->periodic [i];
  241. __hc32 *prev_p = &ohci->hcca->int_table [i];
  242. while (*prev && (temp = *prev) != ed) {
  243. prev_p = &temp->hwNextED;
  244. prev = &temp->ed_next;
  245. }
  246. if (*prev) {
  247. *prev_p = ed->hwNextED;
  248. *prev = ed->ed_next;
  249. }
  250. ohci->load [i] -= ed->load;
  251. }
  252. ohci_to_hcd(ohci)->self.bandwidth_allocated -= ed->load / ed->interval;
  253. ohci_dbg(ohci, "unlink %sed %p branch %d [%dus.], interval %d\n",
  254. (ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
  255. ed, ed->branch, ed->load, ed->interval);
  256. }
  257. /* unlink an ed from one of the HC chains.
  258. * just the link to the ed is unlinked.
  259. * the link from the ed still points to another operational ed or 0
  260. * so the HC can eventually finish the processing of the unlinked ed
  261. * (assuming it already started that, which needn't be true).
  262. *
  263. * ED_UNLINK is a transient state: the HC may still see this ED, but soon
  264. * it won't. ED_SKIP means the HC will finish its current transaction,
  265. * but won't start anything new. The TD queue may still grow; device
  266. * drivers don't know about this HCD-internal state.
  267. *
  268. * When the HC can't see the ED, something changes ED_UNLINK to one of:
  269. *
  270. * - ED_OPER: when there's any request queued, the ED gets rescheduled
  271. * immediately. HC should be working on them.
  272. *
  273. * - ED_IDLE: when there's no TD queue or the HC isn't running.
  274. *
  275. * When finish_unlinks() runs later, after SOF interrupt, it will often
  276. * complete one or more URB unlinks before making that state change.
  277. */
  278. static void ed_deschedule (struct ohci_hcd *ohci, struct ed *ed)
  279. {
  280. ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
  281. wmb ();
  282. ed->state = ED_UNLINK;
  283. /* To deschedule something from the control or bulk list, just
  284. * clear CLE/BLE and wait. There's no safe way to scrub out list
  285. * head/current registers until later, and "later" isn't very
  286. * tightly specified. Figure 6-5 and Section 6.4.2.2 show how
  287. * the HC is reading the ED queues (while we modify them).
  288. *
  289. * For now, ed_schedule() is "later". It might be good paranoia
  290. * to scrub those registers in finish_unlinks(), in case of bugs
  291. * that make the HC try to use them.
  292. */
  293. switch (ed->type) {
  294. case PIPE_CONTROL:
  295. /* remove ED from the HC's list: */
  296. if (ed->ed_prev == NULL) {
  297. if (!ed->hwNextED) {
  298. ohci->hc_control &= ~OHCI_CTRL_CLE;
  299. ohci_writel (ohci, ohci->hc_control,
  300. &ohci->regs->control);
  301. // a ohci_readl() later syncs CLE with the HC
  302. } else
  303. ohci_writel (ohci,
  304. hc32_to_cpup (ohci, &ed->hwNextED),
  305. &ohci->regs->ed_controlhead);
  306. } else {
  307. ed->ed_prev->ed_next = ed->ed_next;
  308. ed->ed_prev->hwNextED = ed->hwNextED;
  309. }
  310. /* remove ED from the HCD's list: */
  311. if (ohci->ed_controltail == ed) {
  312. ohci->ed_controltail = ed->ed_prev;
  313. if (ohci->ed_controltail)
  314. ohci->ed_controltail->ed_next = NULL;
  315. } else if (ed->ed_next) {
  316. ed->ed_next->ed_prev = ed->ed_prev;
  317. }
  318. break;
  319. case PIPE_BULK:
  320. /* remove ED from the HC's list: */
  321. if (ed->ed_prev == NULL) {
  322. if (!ed->hwNextED) {
  323. ohci->hc_control &= ~OHCI_CTRL_BLE;
  324. ohci_writel (ohci, ohci->hc_control,
  325. &ohci->regs->control);
  326. // a ohci_readl() later syncs BLE with the HC
  327. } else
  328. ohci_writel (ohci,
  329. hc32_to_cpup (ohci, &ed->hwNextED),
  330. &ohci->regs->ed_bulkhead);
  331. } else {
  332. ed->ed_prev->ed_next = ed->ed_next;
  333. ed->ed_prev->hwNextED = ed->hwNextED;
  334. }
  335. /* remove ED from the HCD's list: */
  336. if (ohci->ed_bulktail == ed) {
  337. ohci->ed_bulktail = ed->ed_prev;
  338. if (ohci->ed_bulktail)
  339. ohci->ed_bulktail->ed_next = NULL;
  340. } else if (ed->ed_next) {
  341. ed->ed_next->ed_prev = ed->ed_prev;
  342. }
  343. break;
  344. // case PIPE_INTERRUPT:
  345. // case PIPE_ISOCHRONOUS:
  346. default:
  347. periodic_unlink (ohci, ed);
  348. break;
  349. }
  350. }
  351. /*-------------------------------------------------------------------------*/
  352. /* get and maybe (re)init an endpoint. init _should_ be done only as part
  353. * of enumeration, usb_set_configuration() or usb_set_interface().
  354. */
  355. static struct ed *ed_get (
  356. struct ohci_hcd *ohci,
  357. struct usb_host_endpoint *ep,
  358. struct usb_device *udev,
  359. unsigned int pipe,
  360. int interval
  361. ) {
  362. struct ed *ed;
  363. unsigned long flags;
  364. spin_lock_irqsave (&ohci->lock, flags);
  365. if (!(ed = ep->hcpriv)) {
  366. struct td *td;
  367. int is_out;
  368. u32 info;
  369. ed = ed_alloc (ohci, GFP_ATOMIC);
  370. if (!ed) {
  371. /* out of memory */
  372. goto done;
  373. }
  374. /* dummy td; end of td list for ed */
  375. td = td_alloc (ohci, GFP_ATOMIC);
  376. if (!td) {
  377. /* out of memory */
  378. ed_free (ohci, ed);
  379. ed = NULL;
  380. goto done;
  381. }
  382. ed->dummy = td;
  383. ed->hwTailP = cpu_to_hc32 (ohci, td->td_dma);
  384. ed->hwHeadP = ed->hwTailP; /* ED_C, ED_H zeroed */
  385. ed->state = ED_IDLE;
  386. is_out = !(ep->desc.bEndpointAddress & USB_DIR_IN);
  387. /* FIXME usbcore changes dev->devnum before SET_ADDRESS
  388. * succeeds ... otherwise we wouldn't need "pipe".
  389. */
  390. info = usb_pipedevice (pipe);
  391. ed->type = usb_pipetype(pipe);
  392. info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << 7;
  393. info |= usb_endpoint_maxp(&ep->desc) << 16;
  394. if (udev->speed == USB_SPEED_LOW)
  395. info |= ED_LOWSPEED;
  396. /* only control transfers store pids in tds */
  397. if (ed->type != PIPE_CONTROL) {
  398. info |= is_out ? ED_OUT : ED_IN;
  399. if (ed->type != PIPE_BULK) {
  400. /* periodic transfers... */
  401. if (ed->type == PIPE_ISOCHRONOUS)
  402. info |= ED_ISO;
  403. else if (interval > 32) /* iso can be bigger */
  404. interval = 32;
  405. ed->interval = interval;
  406. ed->load = usb_calc_bus_time (
  407. udev->speed, !is_out,
  408. ed->type == PIPE_ISOCHRONOUS,
  409. usb_endpoint_maxp(&ep->desc))
  410. / 1000;
  411. }
  412. }
  413. ed->hwINFO = cpu_to_hc32(ohci, info);
  414. ep->hcpriv = ed;
  415. }
  416. done:
  417. spin_unlock_irqrestore (&ohci->lock, flags);
  418. return ed;
  419. }
  420. /*-------------------------------------------------------------------------*/
  421. /* request unlinking of an endpoint from an operational HC.
  422. * put the ep on the rm_list
  423. * real work is done at the next start frame (SF) hardware interrupt
  424. * caller guarantees HCD is running, so hardware access is safe,
  425. * and that ed->state is ED_OPER
  426. */
  427. static void start_ed_unlink (struct ohci_hcd *ohci, struct ed *ed)
  428. {
  429. ed->hwINFO |= cpu_to_hc32 (ohci, ED_DEQUEUE);
  430. ed_deschedule (ohci, ed);
  431. /* rm_list is just singly linked, for simplicity */
  432. ed->ed_next = ohci->ed_rm_list;
  433. ed->ed_prev = NULL;
  434. ohci->ed_rm_list = ed;
  435. /* enable SOF interrupt */
  436. ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
  437. ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
  438. // flush those writes, and get latest HCCA contents
  439. (void) ohci_readl (ohci, &ohci->regs->control);
  440. /* SF interrupt might get delayed; record the frame counter value that
  441. * indicates when the HC isn't looking at it, so concurrent unlinks
  442. * behave. frame_no wraps every 2^16 msec, and changes right before
  443. * SF is triggered.
  444. */
  445. ed->tick = ohci_frame_no(ohci) + 1;
  446. }
  447. /*-------------------------------------------------------------------------*
  448. * TD handling functions
  449. *-------------------------------------------------------------------------*/
  450. /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
  451. static void
  452. td_fill (struct ohci_hcd *ohci, u32 info,
  453. dma_addr_t data, int len,
  454. struct urb *urb, int index)
  455. {
  456. struct td *td, *td_pt;
  457. struct urb_priv *urb_priv = urb->hcpriv;
  458. int is_iso = info & TD_ISO;
  459. int hash;
  460. // ASSERT (index < urb_priv->length);
  461. /* aim for only one interrupt per urb. mostly applies to control
  462. * and iso; other urbs rarely need more than one TD per urb.
  463. * this way, only final tds (or ones with an error) cause IRQs.
  464. * at least immediately; use DI=6 in case any control request is
  465. * tempted to die part way through. (and to force the hc to flush
  466. * its donelist soonish, even on unlink paths.)
  467. *
  468. * NOTE: could delay interrupts even for the last TD, and get fewer
  469. * interrupts ... increasing per-urb latency by sharing interrupts.
  470. * Drivers that queue bulk urbs may request that behavior.
  471. */
  472. if (index != (urb_priv->length - 1)
  473. || (urb->transfer_flags & URB_NO_INTERRUPT))
  474. info |= TD_DI_SET (6);
  475. /* use this td as the next dummy */
  476. td_pt = urb_priv->td [index];
  477. /* fill the old dummy TD */
  478. td = urb_priv->td [index] = urb_priv->ed->dummy;
  479. urb_priv->ed->dummy = td_pt;
  480. td->ed = urb_priv->ed;
  481. td->next_dl_td = NULL;
  482. td->index = index;
  483. td->urb = urb;
  484. td->data_dma = data;
  485. if (!len)
  486. data = 0;
  487. td->hwINFO = cpu_to_hc32 (ohci, info);
  488. if (is_iso) {
  489. td->hwCBP = cpu_to_hc32 (ohci, data & 0xFFFFF000);
  490. *ohci_hwPSWp(ohci, td, 0) = cpu_to_hc16 (ohci,
  491. (data & 0x0FFF) | 0xE000);
  492. } else {
  493. td->hwCBP = cpu_to_hc32 (ohci, data);
  494. }
  495. if (data)
  496. td->hwBE = cpu_to_hc32 (ohci, data + len - 1);
  497. else
  498. td->hwBE = 0;
  499. td->hwNextTD = cpu_to_hc32 (ohci, td_pt->td_dma);
  500. /* append to queue */
  501. list_add_tail (&td->td_list, &td->ed->td_list);
  502. /* hash it for later reverse mapping */
  503. hash = TD_HASH_FUNC (td->td_dma);
  504. td->td_hash = ohci->td_hash [hash];
  505. ohci->td_hash [hash] = td;
  506. /* HC might read the TD (or cachelines) right away ... */
  507. wmb ();
  508. td->ed->hwTailP = td->hwNextTD;
  509. }
  510. /*-------------------------------------------------------------------------*/
  511. /* Prepare all TDs of a transfer, and queue them onto the ED.
  512. * Caller guarantees HC is active.
  513. * Usually the ED is already on the schedule, so TDs might be
  514. * processed as soon as they're queued.
  515. */
  516. static void td_submit_urb (
  517. struct ohci_hcd *ohci,
  518. struct urb *urb
  519. ) {
  520. struct urb_priv *urb_priv = urb->hcpriv;
  521. struct device *dev = ohci_to_hcd(ohci)->self.controller;
  522. dma_addr_t data;
  523. int data_len = urb->transfer_buffer_length;
  524. int cnt = 0;
  525. u32 info = 0;
  526. int is_out = usb_pipeout (urb->pipe);
  527. int periodic = 0;
  528. int i, this_sg_len, n;
  529. struct scatterlist *sg;
  530. /* OHCI handles the bulk/interrupt data toggles itself. We just
  531. * use the device toggle bits for resetting, and rely on the fact
  532. * that resetting toggle is meaningless if the endpoint is active.
  533. */
  534. if (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), is_out)) {
  535. usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
  536. is_out, 1);
  537. urb_priv->ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_C);
  538. }
  539. list_add (&urb_priv->pending, &ohci->pending);
  540. i = urb->num_mapped_sgs;
  541. if (data_len > 0 && i > 0) {
  542. sg = urb->sg;
  543. data = sg_dma_address(sg);
  544. /*
  545. * urb->transfer_buffer_length may be smaller than the
  546. * size of the scatterlist (or vice versa)
  547. */
  548. this_sg_len = min_t(int, sg_dma_len(sg), data_len);
  549. } else {
  550. sg = NULL;
  551. if (data_len)
  552. data = urb->transfer_dma;
  553. else
  554. data = 0;
  555. this_sg_len = data_len;
  556. }
  557. /* NOTE: TD_CC is set so we can tell which TDs the HC processed by
  558. * using TD_CC_GET, as well as by seeing them on the done list.
  559. * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
  560. */
  561. switch (urb_priv->ed->type) {
  562. /* Bulk and interrupt are identical except for where in the schedule
  563. * their EDs live.
  564. */
  565. case PIPE_INTERRUPT:
  566. /* ... and periodic urbs have extra accounting */
  567. periodic = ohci_to_hcd(ohci)->self.bandwidth_int_reqs++ == 0
  568. && ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0;
  569. /* FALLTHROUGH */
  570. case PIPE_BULK:
  571. info = is_out
  572. ? TD_T_TOGGLE | TD_CC | TD_DP_OUT
  573. : TD_T_TOGGLE | TD_CC | TD_DP_IN;
  574. /* TDs _could_ transfer up to 8K each */
  575. for (;;) {
  576. n = min(this_sg_len, 4096);
  577. /* maybe avoid ED halt on final TD short read */
  578. if (n >= data_len || (i == 1 && n >= this_sg_len)) {
  579. if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
  580. info |= TD_R;
  581. }
  582. td_fill(ohci, info, data, n, urb, cnt);
  583. this_sg_len -= n;
  584. data_len -= n;
  585. data += n;
  586. cnt++;
  587. if (this_sg_len <= 0) {
  588. if (--i <= 0 || data_len <= 0)
  589. break;
  590. sg = sg_next(sg);
  591. data = sg_dma_address(sg);
  592. this_sg_len = min_t(int, sg_dma_len(sg),
  593. data_len);
  594. }
  595. }
  596. if ((urb->transfer_flags & URB_ZERO_PACKET)
  597. && cnt < urb_priv->length) {
  598. td_fill (ohci, info, 0, 0, urb, cnt);
  599. cnt++;
  600. }
  601. /* maybe kickstart bulk list */
  602. if (urb_priv->ed->type == PIPE_BULK) {
  603. wmb ();
  604. ohci_writel (ohci, OHCI_BLF, &ohci->regs->cmdstatus);
  605. }
  606. break;
  607. /* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
  608. * any DATA phase works normally, and the STATUS ack is special.
  609. */
  610. case PIPE_CONTROL:
  611. info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
  612. td_fill (ohci, info, urb->setup_dma, 8, urb, cnt++);
  613. if (data_len > 0) {
  614. info = TD_CC | TD_R | TD_T_DATA1;
  615. info |= is_out ? TD_DP_OUT : TD_DP_IN;
  616. /* NOTE: mishandles transfers >8K, some >4K */
  617. td_fill (ohci, info, data, data_len, urb, cnt++);
  618. }
  619. info = (is_out || data_len == 0)
  620. ? TD_CC | TD_DP_IN | TD_T_DATA1
  621. : TD_CC | TD_DP_OUT | TD_T_DATA1;
  622. td_fill (ohci, info, data, 0, urb, cnt++);
  623. /* maybe kickstart control list */
  624. wmb ();
  625. ohci_writel (ohci, OHCI_CLF, &ohci->regs->cmdstatus);
  626. break;
  627. /* ISO has no retransmit, so no toggle; and it uses special TDs.
  628. * Each TD could handle multiple consecutive frames (interval 1);
  629. * we could often reduce the number of TDs here.
  630. */
  631. case PIPE_ISOCHRONOUS:
  632. for (cnt = urb_priv->td_cnt; cnt < urb->number_of_packets;
  633. cnt++) {
  634. int frame = urb->start_frame;
  635. // FIXME scheduling should handle frame counter
  636. // roll-around ... exotic case (and OHCI has
  637. // a 2^16 iso range, vs other HCs max of 2^10)
  638. frame += cnt * urb->interval;
  639. frame &= 0xffff;
  640. td_fill (ohci, TD_CC | TD_ISO | frame,
  641. data + urb->iso_frame_desc [cnt].offset,
  642. urb->iso_frame_desc [cnt].length, urb, cnt);
  643. }
  644. if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0) {
  645. if (quirk_amdiso(ohci))
  646. usb_amd_quirk_pll_disable();
  647. if (quirk_amdprefetch(ohci))
  648. sb800_prefetch(dev, 1);
  649. }
  650. periodic = ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs++ == 0
  651. && ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0;
  652. break;
  653. }
  654. /* start periodic dma if needed */
  655. if (periodic) {
  656. wmb ();
  657. ohci->hc_control |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
  658. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  659. }
  660. // ASSERT (urb_priv->length == cnt);
  661. }
  662. /*-------------------------------------------------------------------------*
  663. * Done List handling functions
  664. *-------------------------------------------------------------------------*/
  665. /* calculate transfer length/status and update the urb */
  666. static int td_done(struct ohci_hcd *ohci, struct urb *urb, struct td *td)
  667. {
  668. u32 tdINFO = hc32_to_cpup (ohci, &td->hwINFO);
  669. int cc = 0;
  670. int status = -EINPROGRESS;
  671. list_del (&td->td_list);
  672. /* ISO ... drivers see per-TD length/status */
  673. if (tdINFO & TD_ISO) {
  674. u16 tdPSW = ohci_hwPSW(ohci, td, 0);
  675. int dlen = 0;
  676. /* NOTE: assumes FC in tdINFO == 0, and that
  677. * only the first of 0..MAXPSW psws is used.
  678. */
  679. cc = (tdPSW >> 12) & 0xF;
  680. if (tdINFO & TD_CC) /* hc didn't touch? */
  681. return status;
  682. if (usb_pipeout (urb->pipe))
  683. dlen = urb->iso_frame_desc [td->index].length;
  684. else {
  685. /* short reads are always OK for ISO */
  686. if (cc == TD_DATAUNDERRUN)
  687. cc = TD_CC_NOERROR;
  688. dlen = tdPSW & 0x3ff;
  689. }
  690. urb->actual_length += dlen;
  691. urb->iso_frame_desc [td->index].actual_length = dlen;
  692. urb->iso_frame_desc [td->index].status = cc_to_error [cc];
  693. if (cc != TD_CC_NOERROR)
  694. ohci_dbg(ohci,
  695. "urb %p iso td %p (%d) len %d cc %d\n",
  696. urb, td, 1 + td->index, dlen, cc);
  697. /* BULK, INT, CONTROL ... drivers see aggregate length/status,
  698. * except that "setup" bytes aren't counted and "short" transfers
  699. * might not be reported as errors.
  700. */
  701. } else {
  702. int type = usb_pipetype (urb->pipe);
  703. u32 tdBE = hc32_to_cpup (ohci, &td->hwBE);
  704. cc = TD_CC_GET (tdINFO);
  705. /* update packet status if needed (short is normally ok) */
  706. if (cc == TD_DATAUNDERRUN
  707. && !(urb->transfer_flags & URB_SHORT_NOT_OK))
  708. cc = TD_CC_NOERROR;
  709. if (cc != TD_CC_NOERROR && cc < 0x0E)
  710. status = cc_to_error[cc];
  711. /* count all non-empty packets except control SETUP packet */
  712. if ((type != PIPE_CONTROL || td->index != 0) && tdBE != 0) {
  713. if (td->hwCBP == 0)
  714. urb->actual_length += tdBE - td->data_dma + 1;
  715. else
  716. urb->actual_length +=
  717. hc32_to_cpup (ohci, &td->hwCBP)
  718. - td->data_dma;
  719. }
  720. if (cc != TD_CC_NOERROR && cc < 0x0E)
  721. ohci_dbg(ohci,
  722. "urb %p td %p (%d) cc %d, len=%d/%d\n",
  723. urb, td, 1 + td->index, cc,
  724. urb->actual_length,
  725. urb->transfer_buffer_length);
  726. }
  727. return status;
  728. }
  729. /*-------------------------------------------------------------------------*/
  730. static void ed_halted(struct ohci_hcd *ohci, struct td *td, int cc)
  731. {
  732. struct urb *urb = td->urb;
  733. urb_priv_t *urb_priv = urb->hcpriv;
  734. struct ed *ed = td->ed;
  735. struct list_head *tmp = td->td_list.next;
  736. __hc32 toggle = ed->hwHeadP & cpu_to_hc32 (ohci, ED_C);
  737. /* clear ed halt; this is the td that caused it, but keep it inactive
  738. * until its urb->complete() has a chance to clean up.
  739. */
  740. ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
  741. wmb ();
  742. ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_H);
  743. /* Get rid of all later tds from this urb. We don't have
  744. * to be careful: no errors and nothing was transferred.
  745. * Also patch the ed so it looks as if those tds completed normally.
  746. */
  747. while (tmp != &ed->td_list) {
  748. struct td *next;
  749. next = list_entry (tmp, struct td, td_list);
  750. tmp = next->td_list.next;
  751. if (next->urb != urb)
  752. break;
  753. /* NOTE: if multi-td control DATA segments get supported,
  754. * this urb had one of them, this td wasn't the last td
  755. * in that segment (TD_R clear), this ed halted because
  756. * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
  757. * then we need to leave the control STATUS packet queued
  758. * and clear ED_SKIP.
  759. */
  760. list_del(&next->td_list);
  761. urb_priv->td_cnt++;
  762. ed->hwHeadP = next->hwNextTD | toggle;
  763. }
  764. /* help for troubleshooting: report anything that
  765. * looks odd ... that doesn't include protocol stalls
  766. * (or maybe some other things)
  767. */
  768. switch (cc) {
  769. case TD_DATAUNDERRUN:
  770. if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
  771. break;
  772. /* fallthrough */
  773. case TD_CC_STALL:
  774. if (usb_pipecontrol (urb->pipe))
  775. break;
  776. /* fallthrough */
  777. default:
  778. ohci_dbg (ohci,
  779. "urb %p path %s ep%d%s %08x cc %d --> status %d\n",
  780. urb, urb->dev->devpath,
  781. usb_pipeendpoint (urb->pipe),
  782. usb_pipein (urb->pipe) ? "in" : "out",
  783. hc32_to_cpu (ohci, td->hwINFO),
  784. cc, cc_to_error [cc]);
  785. }
  786. }
  787. /* Add a TD to the done list */
  788. static void add_to_done_list(struct ohci_hcd *ohci, struct td *td)
  789. {
  790. struct td *td2, *td_prev;
  791. struct ed *ed;
  792. if (td->next_dl_td)
  793. return; /* Already on the list */
  794. /* Add all the TDs going back until we reach one that's on the list */
  795. ed = td->ed;
  796. td2 = td_prev = td;
  797. list_for_each_entry_continue_reverse(td2, &ed->td_list, td_list) {
  798. if (td2->next_dl_td)
  799. break;
  800. td2->next_dl_td = td_prev;
  801. td_prev = td2;
  802. }
  803. if (ohci->dl_end)
  804. ohci->dl_end->next_dl_td = td_prev;
  805. else
  806. ohci->dl_start = td_prev;
  807. /*
  808. * Make td->next_dl_td point to td itself, to mark the fact
  809. * that td is on the done list.
  810. */
  811. ohci->dl_end = td->next_dl_td = td;
  812. /* Did we just add the latest pending TD? */
  813. td2 = ed->pending_td;
  814. if (td2 && td2->next_dl_td)
  815. ed->pending_td = NULL;
  816. }
  817. /* Get the entries on the hardware done queue and put them on our list */
  818. static void update_done_list(struct ohci_hcd *ohci)
  819. {
  820. u32 td_dma;
  821. struct td *td = NULL;
  822. td_dma = hc32_to_cpup (ohci, &ohci->hcca->done_head);
  823. ohci->hcca->done_head = 0;
  824. wmb();
  825. /* get TD from hc's singly linked list, and
  826. * add to ours. ed->td_list changes later.
  827. */
  828. while (td_dma) {
  829. int cc;
  830. td = dma_to_td (ohci, td_dma);
  831. if (!td) {
  832. ohci_err (ohci, "bad entry %8x\n", td_dma);
  833. break;
  834. }
  835. td->hwINFO |= cpu_to_hc32 (ohci, TD_DONE);
  836. cc = TD_CC_GET (hc32_to_cpup (ohci, &td->hwINFO));
  837. /* Non-iso endpoints can halt on error; un-halt,
  838. * and dequeue any other TDs from this urb.
  839. * No other TD could have caused the halt.
  840. */
  841. if (cc != TD_CC_NOERROR
  842. && (td->ed->hwHeadP & cpu_to_hc32 (ohci, ED_H)))
  843. ed_halted(ohci, td, cc);
  844. td_dma = hc32_to_cpup (ohci, &td->hwNextTD);
  845. add_to_done_list(ohci, td);
  846. }
  847. }
  848. /*-------------------------------------------------------------------------*/
  849. /* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
  850. static void finish_unlinks(struct ohci_hcd *ohci)
  851. {
  852. unsigned tick = ohci_frame_no(ohci);
  853. struct ed *ed, **last;
  854. rescan_all:
  855. for (last = &ohci->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
  856. struct list_head *entry, *tmp;
  857. int completed, modified;
  858. __hc32 *prev;
  859. /* Is this ED already invisible to the hardware? */
  860. if (ed->state == ED_IDLE)
  861. goto ed_idle;
  862. /* only take off EDs that the HC isn't using, accounting for
  863. * frame counter wraps and EDs with partially retired TDs
  864. */
  865. if (likely(ohci->rh_state == OHCI_RH_RUNNING) &&
  866. tick_before(tick, ed->tick)) {
  867. skip_ed:
  868. last = &ed->ed_next;
  869. continue;
  870. }
  871. if (!list_empty(&ed->td_list)) {
  872. struct td *td;
  873. u32 head;
  874. td = list_first_entry(&ed->td_list, struct td, td_list);
  875. /* INTR_WDH may need to clean up first */
  876. head = hc32_to_cpu(ohci, ed->hwHeadP) & TD_MASK;
  877. if (td->td_dma != head &&
  878. ohci->rh_state == OHCI_RH_RUNNING)
  879. goto skip_ed;
  880. /* Don't mess up anything already on the done list */
  881. if (td->next_dl_td)
  882. goto skip_ed;
  883. }
  884. /* ED's now officially unlinked, hc doesn't see */
  885. ed->state = ED_IDLE;
  886. ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_H);
  887. ed->hwNextED = 0;
  888. wmb();
  889. ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE);
  890. ed_idle:
  891. /* reentrancy: if we drop the schedule lock, someone might
  892. * have modified this list. normally it's just prepending
  893. * entries (which we'd ignore), but paranoia won't hurt.
  894. */
  895. modified = 0;
  896. /* unlink urbs as requested, but rescan the list after
  897. * we call a completion since it might have unlinked
  898. * another (earlier) urb
  899. *
  900. * When we get here, the HC doesn't see this ed. But it
  901. * must not be rescheduled until all completed URBs have
  902. * been given back to the driver.
  903. */
  904. rescan_this:
  905. completed = 0;
  906. prev = &ed->hwHeadP;
  907. list_for_each_safe (entry, tmp, &ed->td_list) {
  908. struct td *td;
  909. struct urb *urb;
  910. urb_priv_t *urb_priv;
  911. __hc32 savebits;
  912. u32 tdINFO;
  913. td = list_entry (entry, struct td, td_list);
  914. urb = td->urb;
  915. urb_priv = td->urb->hcpriv;
  916. if (!urb->unlinked) {
  917. prev = &td->hwNextTD;
  918. continue;
  919. }
  920. /* patch pointer hc uses */
  921. savebits = *prev & ~cpu_to_hc32 (ohci, TD_MASK);
  922. *prev = td->hwNextTD | savebits;
  923. /* If this was unlinked, the TD may not have been
  924. * retired ... so manually save the data toggle.
  925. * The controller ignores the value we save for
  926. * control and ISO endpoints.
  927. */
  928. tdINFO = hc32_to_cpup(ohci, &td->hwINFO);
  929. if ((tdINFO & TD_T) == TD_T_DATA0)
  930. ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_C);
  931. else if ((tdINFO & TD_T) == TD_T_DATA1)
  932. ed->hwHeadP |= cpu_to_hc32(ohci, ED_C);
  933. /* HC may have partly processed this TD */
  934. td_done (ohci, urb, td);
  935. urb_priv->td_cnt++;
  936. /* if URB is done, clean up */
  937. if (urb_priv->td_cnt >= urb_priv->length) {
  938. modified = completed = 1;
  939. finish_urb(ohci, urb, 0);
  940. }
  941. }
  942. if (completed && !list_empty (&ed->td_list))
  943. goto rescan_this;
  944. /*
  945. * If no TDs are queued, take ED off the ed_rm_list.
  946. * Otherwise, if the HC is running, reschedule.
  947. * If not, leave it on the list for further dequeues.
  948. */
  949. if (list_empty(&ed->td_list)) {
  950. *last = ed->ed_next;
  951. ed->ed_next = NULL;
  952. list_del(&ed->in_use_list);
  953. } else if (ohci->rh_state == OHCI_RH_RUNNING) {
  954. *last = ed->ed_next;
  955. ed->ed_next = NULL;
  956. ed_schedule(ohci, ed);
  957. } else {
  958. last = &ed->ed_next;
  959. }
  960. if (modified)
  961. goto rescan_all;
  962. }
  963. /* maybe reenable control and bulk lists */
  964. if (ohci->rh_state == OHCI_RH_RUNNING && !ohci->ed_rm_list) {
  965. u32 command = 0, control = 0;
  966. if (ohci->ed_controltail) {
  967. command |= OHCI_CLF;
  968. if (quirk_zfmicro(ohci))
  969. mdelay(1);
  970. if (!(ohci->hc_control & OHCI_CTRL_CLE)) {
  971. control |= OHCI_CTRL_CLE;
  972. ohci_writel (ohci, 0,
  973. &ohci->regs->ed_controlcurrent);
  974. }
  975. }
  976. if (ohci->ed_bulktail) {
  977. command |= OHCI_BLF;
  978. if (quirk_zfmicro(ohci))
  979. mdelay(1);
  980. if (!(ohci->hc_control & OHCI_CTRL_BLE)) {
  981. control |= OHCI_CTRL_BLE;
  982. ohci_writel (ohci, 0,
  983. &ohci->regs->ed_bulkcurrent);
  984. }
  985. }
  986. /* CLE/BLE to enable, CLF/BLF to (maybe) kickstart */
  987. if (control) {
  988. ohci->hc_control |= control;
  989. if (quirk_zfmicro(ohci))
  990. mdelay(1);
  991. ohci_writel (ohci, ohci->hc_control,
  992. &ohci->regs->control);
  993. }
  994. if (command) {
  995. if (quirk_zfmicro(ohci))
  996. mdelay(1);
  997. ohci_writel (ohci, command, &ohci->regs->cmdstatus);
  998. }
  999. }
  1000. }
  1001. /*-------------------------------------------------------------------------*/
  1002. /* Take back a TD from the host controller */
  1003. static void takeback_td(struct ohci_hcd *ohci, struct td *td)
  1004. {
  1005. struct urb *urb = td->urb;
  1006. urb_priv_t *urb_priv = urb->hcpriv;
  1007. struct ed *ed = td->ed;
  1008. int status;
  1009. /* update URB's length and status from TD */
  1010. status = td_done(ohci, urb, td);
  1011. urb_priv->td_cnt++;
  1012. /* If all this urb's TDs are done, call complete() */
  1013. if (urb_priv->td_cnt >= urb_priv->length)
  1014. finish_urb(ohci, urb, status);
  1015. /* clean schedule: unlink EDs that are no longer busy */
  1016. if (list_empty(&ed->td_list)) {
  1017. if (ed->state == ED_OPER)
  1018. start_ed_unlink(ohci, ed);
  1019. /* ... reenabling halted EDs only after fault cleanup */
  1020. } else if ((ed->hwINFO & cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE))
  1021. == cpu_to_hc32(ohci, ED_SKIP)) {
  1022. td = list_entry(ed->td_list.next, struct td, td_list);
  1023. if (!(td->hwINFO & cpu_to_hc32(ohci, TD_DONE))) {
  1024. ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP);
  1025. /* ... hc may need waking-up */
  1026. switch (ed->type) {
  1027. case PIPE_CONTROL:
  1028. ohci_writel(ohci, OHCI_CLF,
  1029. &ohci->regs->cmdstatus);
  1030. break;
  1031. case PIPE_BULK:
  1032. ohci_writel(ohci, OHCI_BLF,
  1033. &ohci->regs->cmdstatus);
  1034. break;
  1035. }
  1036. }
  1037. }
  1038. }
  1039. /*
  1040. * Process normal completions (error or success) and clean the schedules.
  1041. *
  1042. * This is the main path for handing urbs back to drivers. The only other
  1043. * normal path is finish_unlinks(), which unlinks URBs using ed_rm_list,
  1044. * instead of scanning the (re-reversed) donelist as this does.
  1045. */
  1046. static void process_done_list(struct ohci_hcd *ohci)
  1047. {
  1048. struct td *td;
  1049. while (ohci->dl_start) {
  1050. td = ohci->dl_start;
  1051. if (td == ohci->dl_end)
  1052. ohci->dl_start = ohci->dl_end = NULL;
  1053. else
  1054. ohci->dl_start = td->next_dl_td;
  1055. takeback_td(ohci, td);
  1056. }
  1057. }
  1058. /*
  1059. * TD takeback and URB giveback must be single-threaded.
  1060. * This routine takes care of it all.
  1061. */
  1062. static void ohci_work(struct ohci_hcd *ohci)
  1063. {
  1064. if (ohci->working) {
  1065. ohci->restart_work = 1;
  1066. return;
  1067. }
  1068. ohci->working = 1;
  1069. restart:
  1070. process_done_list(ohci);
  1071. if (ohci->ed_rm_list)
  1072. finish_unlinks(ohci);
  1073. if (ohci->restart_work) {
  1074. ohci->restart_work = 0;
  1075. goto restart;
  1076. }
  1077. ohci->working = 0;
  1078. }