ohci-q.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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->ed_prev = NULL;
  160. ed->ed_next = NULL;
  161. ed->hwNextED = 0;
  162. wmb ();
  163. /* we care about rm_list when setting CLE/BLE in case the HC was at
  164. * work on some TD when CLE/BLE was turned off, and isn't quiesced
  165. * yet. finish_unlinks() restarts as needed, some upcoming INTR_SF.
  166. *
  167. * control and bulk EDs are doubly linked (ed_next, ed_prev), but
  168. * periodic ones are singly linked (ed_next). that's because the
  169. * periodic schedule encodes a tree like figure 3-5 in the ohci
  170. * spec: each qh can have several "previous" nodes, and the tree
  171. * doesn't have unused/idle descriptors.
  172. */
  173. switch (ed->type) {
  174. case PIPE_CONTROL:
  175. if (ohci->ed_controltail == NULL) {
  176. WARN_ON (ohci->hc_control & OHCI_CTRL_CLE);
  177. ohci_writel (ohci, ed->dma,
  178. &ohci->regs->ed_controlhead);
  179. } else {
  180. ohci->ed_controltail->ed_next = ed;
  181. ohci->ed_controltail->hwNextED = cpu_to_hc32 (ohci,
  182. ed->dma);
  183. }
  184. ed->ed_prev = ohci->ed_controltail;
  185. if (!ohci->ed_controltail && !ohci->ed_rm_list) {
  186. wmb();
  187. ohci->hc_control |= OHCI_CTRL_CLE;
  188. ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
  189. ohci_writel (ohci, ohci->hc_control,
  190. &ohci->regs->control);
  191. }
  192. ohci->ed_controltail = ed;
  193. break;
  194. case PIPE_BULK:
  195. if (ohci->ed_bulktail == NULL) {
  196. WARN_ON (ohci->hc_control & OHCI_CTRL_BLE);
  197. ohci_writel (ohci, ed->dma, &ohci->regs->ed_bulkhead);
  198. } else {
  199. ohci->ed_bulktail->ed_next = ed;
  200. ohci->ed_bulktail->hwNextED = cpu_to_hc32 (ohci,
  201. ed->dma);
  202. }
  203. ed->ed_prev = ohci->ed_bulktail;
  204. if (!ohci->ed_bulktail && !ohci->ed_rm_list) {
  205. wmb();
  206. ohci->hc_control |= OHCI_CTRL_BLE;
  207. ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
  208. ohci_writel (ohci, ohci->hc_control,
  209. &ohci->regs->control);
  210. }
  211. ohci->ed_bulktail = ed;
  212. break;
  213. // case PIPE_INTERRUPT:
  214. // case PIPE_ISOCHRONOUS:
  215. default:
  216. branch = balance (ohci, ed->interval, ed->load);
  217. if (branch < 0) {
  218. ohci_dbg (ohci,
  219. "ERR %d, interval %d msecs, load %d\n",
  220. branch, ed->interval, ed->load);
  221. // FIXME if there are TDs queued, fail them!
  222. return branch;
  223. }
  224. ed->branch = branch;
  225. periodic_link (ohci, ed);
  226. }
  227. /* the HC may not see the schedule updates yet, but if it does
  228. * then they'll be properly ordered.
  229. */
  230. ed->state = ED_OPER;
  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. ed = ep->hcpriv;
  366. if (!ed) {
  367. struct td *td;
  368. int is_out;
  369. u32 info;
  370. ed = ed_alloc (ohci, GFP_ATOMIC);
  371. if (!ed) {
  372. /* out of memory */
  373. goto done;
  374. }
  375. /* dummy td; end of td list for ed */
  376. td = td_alloc (ohci, GFP_ATOMIC);
  377. if (!td) {
  378. /* out of memory */
  379. ed_free (ohci, ed);
  380. ed = NULL;
  381. goto done;
  382. }
  383. ed->dummy = td;
  384. ed->hwTailP = cpu_to_hc32 (ohci, td->td_dma);
  385. ed->hwHeadP = ed->hwTailP; /* ED_C, ED_H zeroed */
  386. ed->state = ED_IDLE;
  387. is_out = !(ep->desc.bEndpointAddress & USB_DIR_IN);
  388. /* FIXME usbcore changes dev->devnum before SET_ADDRESS
  389. * succeeds ... otherwise we wouldn't need "pipe".
  390. */
  391. info = usb_pipedevice (pipe);
  392. ed->type = usb_pipetype(pipe);
  393. info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << 7;
  394. info |= usb_endpoint_maxp(&ep->desc) << 16;
  395. if (udev->speed == USB_SPEED_LOW)
  396. info |= ED_LOWSPEED;
  397. /* only control transfers store pids in tds */
  398. if (ed->type != PIPE_CONTROL) {
  399. info |= is_out ? ED_OUT : ED_IN;
  400. if (ed->type != PIPE_BULK) {
  401. /* periodic transfers... */
  402. if (ed->type == PIPE_ISOCHRONOUS)
  403. info |= ED_ISO;
  404. else if (interval > 32) /* iso can be bigger */
  405. interval = 32;
  406. ed->interval = interval;
  407. ed->load = usb_calc_bus_time (
  408. udev->speed, !is_out,
  409. ed->type == PIPE_ISOCHRONOUS,
  410. usb_endpoint_maxp(&ep->desc))
  411. / 1000;
  412. }
  413. }
  414. ed->hwINFO = cpu_to_hc32(ohci, info);
  415. ep->hcpriv = ed;
  416. }
  417. done:
  418. spin_unlock_irqrestore (&ohci->lock, flags);
  419. return ed;
  420. }
  421. /*-------------------------------------------------------------------------*/
  422. /* request unlinking of an endpoint from an operational HC.
  423. * put the ep on the rm_list
  424. * real work is done at the next start frame (SF) hardware interrupt
  425. * caller guarantees HCD is running, so hardware access is safe,
  426. * and that ed->state is ED_OPER
  427. */
  428. static void start_ed_unlink (struct ohci_hcd *ohci, struct ed *ed)
  429. {
  430. ed->hwINFO |= cpu_to_hc32 (ohci, ED_DEQUEUE);
  431. ed_deschedule (ohci, ed);
  432. /* rm_list is just singly linked, for simplicity */
  433. ed->ed_next = ohci->ed_rm_list;
  434. ed->ed_prev = NULL;
  435. ohci->ed_rm_list = ed;
  436. /* enable SOF interrupt */
  437. ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
  438. ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
  439. // flush those writes, and get latest HCCA contents
  440. (void) ohci_readl (ohci, &ohci->regs->control);
  441. /* SF interrupt might get delayed; record the frame counter value that
  442. * indicates when the HC isn't looking at it, so concurrent unlinks
  443. * behave. frame_no wraps every 2^16 msec, and changes right before
  444. * SF is triggered.
  445. */
  446. ed->tick = ohci_frame_no(ohci) + 1;
  447. }
  448. /*-------------------------------------------------------------------------*
  449. * TD handling functions
  450. *-------------------------------------------------------------------------*/
  451. /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
  452. static void
  453. td_fill (struct ohci_hcd *ohci, u32 info,
  454. dma_addr_t data, int len,
  455. struct urb *urb, int index)
  456. {
  457. struct td *td, *td_pt;
  458. struct urb_priv *urb_priv = urb->hcpriv;
  459. int is_iso = info & TD_ISO;
  460. int hash;
  461. // ASSERT (index < urb_priv->length);
  462. /* aim for only one interrupt per urb. mostly applies to control
  463. * and iso; other urbs rarely need more than one TD per urb.
  464. * this way, only final tds (or ones with an error) cause IRQs.
  465. * at least immediately; use DI=6 in case any control request is
  466. * tempted to die part way through. (and to force the hc to flush
  467. * its donelist soonish, even on unlink paths.)
  468. *
  469. * NOTE: could delay interrupts even for the last TD, and get fewer
  470. * interrupts ... increasing per-urb latency by sharing interrupts.
  471. * Drivers that queue bulk urbs may request that behavior.
  472. */
  473. if (index != (urb_priv->length - 1)
  474. || (urb->transfer_flags & URB_NO_INTERRUPT))
  475. info |= TD_DI_SET (6);
  476. /* use this td as the next dummy */
  477. td_pt = urb_priv->td [index];
  478. /* fill the old dummy TD */
  479. td = urb_priv->td [index] = urb_priv->ed->dummy;
  480. urb_priv->ed->dummy = td_pt;
  481. td->ed = urb_priv->ed;
  482. td->next_dl_td = NULL;
  483. td->index = index;
  484. td->urb = urb;
  485. td->data_dma = data;
  486. if (!len)
  487. data = 0;
  488. td->hwINFO = cpu_to_hc32 (ohci, info);
  489. if (is_iso) {
  490. td->hwCBP = cpu_to_hc32 (ohci, data & 0xFFFFF000);
  491. *ohci_hwPSWp(ohci, td, 0) = cpu_to_hc16 (ohci,
  492. (data & 0x0FFF) | 0xE000);
  493. } else {
  494. td->hwCBP = cpu_to_hc32 (ohci, data);
  495. }
  496. if (data)
  497. td->hwBE = cpu_to_hc32 (ohci, data + len - 1);
  498. else
  499. td->hwBE = 0;
  500. td->hwNextTD = cpu_to_hc32 (ohci, td_pt->td_dma);
  501. /* append to queue */
  502. list_add_tail (&td->td_list, &td->ed->td_list);
  503. /* hash it for later reverse mapping */
  504. hash = TD_HASH_FUNC (td->td_dma);
  505. td->td_hash = ohci->td_hash [hash];
  506. ohci->td_hash [hash] = td;
  507. /* HC might read the TD (or cachelines) right away ... */
  508. wmb ();
  509. td->ed->hwTailP = td->hwNextTD;
  510. }
  511. /*-------------------------------------------------------------------------*/
  512. /* Prepare all TDs of a transfer, and queue them onto the ED.
  513. * Caller guarantees HC is active.
  514. * Usually the ED is already on the schedule, so TDs might be
  515. * processed as soon as they're queued.
  516. */
  517. static void td_submit_urb (
  518. struct ohci_hcd *ohci,
  519. struct urb *urb
  520. ) {
  521. struct urb_priv *urb_priv = urb->hcpriv;
  522. struct device *dev = ohci_to_hcd(ohci)->self.controller;
  523. dma_addr_t data;
  524. int data_len = urb->transfer_buffer_length;
  525. int cnt = 0;
  526. u32 info = 0;
  527. int is_out = usb_pipeout (urb->pipe);
  528. int periodic = 0;
  529. int i, this_sg_len, n;
  530. struct scatterlist *sg;
  531. /* OHCI handles the bulk/interrupt data toggles itself. We just
  532. * use the device toggle bits for resetting, and rely on the fact
  533. * that resetting toggle is meaningless if the endpoint is active.
  534. */
  535. if (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), is_out)) {
  536. usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
  537. is_out, 1);
  538. urb_priv->ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_C);
  539. }
  540. list_add (&urb_priv->pending, &ohci->pending);
  541. i = urb->num_mapped_sgs;
  542. if (data_len > 0 && i > 0) {
  543. sg = urb->sg;
  544. data = sg_dma_address(sg);
  545. /*
  546. * urb->transfer_buffer_length may be smaller than the
  547. * size of the scatterlist (or vice versa)
  548. */
  549. this_sg_len = min_t(int, sg_dma_len(sg), data_len);
  550. } else {
  551. sg = NULL;
  552. if (data_len)
  553. data = urb->transfer_dma;
  554. else
  555. data = 0;
  556. this_sg_len = data_len;
  557. }
  558. /* NOTE: TD_CC is set so we can tell which TDs the HC processed by
  559. * using TD_CC_GET, as well as by seeing them on the done list.
  560. * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
  561. */
  562. switch (urb_priv->ed->type) {
  563. /* Bulk and interrupt are identical except for where in the schedule
  564. * their EDs live.
  565. */
  566. case PIPE_INTERRUPT:
  567. /* ... and periodic urbs have extra accounting */
  568. periodic = ohci_to_hcd(ohci)->self.bandwidth_int_reqs++ == 0
  569. && ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0;
  570. /* FALLTHROUGH */
  571. case PIPE_BULK:
  572. info = is_out
  573. ? TD_T_TOGGLE | TD_CC | TD_DP_OUT
  574. : TD_T_TOGGLE | TD_CC | TD_DP_IN;
  575. /* TDs _could_ transfer up to 8K each */
  576. for (;;) {
  577. n = min(this_sg_len, 4096);
  578. /* maybe avoid ED halt on final TD short read */
  579. if (n >= data_len || (i == 1 && n >= this_sg_len)) {
  580. if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
  581. info |= TD_R;
  582. }
  583. td_fill(ohci, info, data, n, urb, cnt);
  584. this_sg_len -= n;
  585. data_len -= n;
  586. data += n;
  587. cnt++;
  588. if (this_sg_len <= 0) {
  589. if (--i <= 0 || data_len <= 0)
  590. break;
  591. sg = sg_next(sg);
  592. data = sg_dma_address(sg);
  593. this_sg_len = min_t(int, sg_dma_len(sg),
  594. data_len);
  595. }
  596. }
  597. if ((urb->transfer_flags & URB_ZERO_PACKET)
  598. && cnt < urb_priv->length) {
  599. td_fill (ohci, info, 0, 0, urb, cnt);
  600. cnt++;
  601. }
  602. /* maybe kickstart bulk list */
  603. if (urb_priv->ed->type == PIPE_BULK) {
  604. wmb ();
  605. ohci_writel (ohci, OHCI_BLF, &ohci->regs->cmdstatus);
  606. }
  607. break;
  608. /* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
  609. * any DATA phase works normally, and the STATUS ack is special.
  610. */
  611. case PIPE_CONTROL:
  612. info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
  613. td_fill (ohci, info, urb->setup_dma, 8, urb, cnt++);
  614. if (data_len > 0) {
  615. info = TD_CC | TD_R | TD_T_DATA1;
  616. info |= is_out ? TD_DP_OUT : TD_DP_IN;
  617. /* NOTE: mishandles transfers >8K, some >4K */
  618. td_fill (ohci, info, data, data_len, urb, cnt++);
  619. }
  620. info = (is_out || data_len == 0)
  621. ? TD_CC | TD_DP_IN | TD_T_DATA1
  622. : TD_CC | TD_DP_OUT | TD_T_DATA1;
  623. td_fill (ohci, info, data, 0, urb, cnt++);
  624. /* maybe kickstart control list */
  625. wmb ();
  626. ohci_writel (ohci, OHCI_CLF, &ohci->regs->cmdstatus);
  627. break;
  628. /* ISO has no retransmit, so no toggle; and it uses special TDs.
  629. * Each TD could handle multiple consecutive frames (interval 1);
  630. * we could often reduce the number of TDs here.
  631. */
  632. case PIPE_ISOCHRONOUS:
  633. for (cnt = urb_priv->td_cnt; cnt < urb->number_of_packets;
  634. cnt++) {
  635. int frame = urb->start_frame;
  636. // FIXME scheduling should handle frame counter
  637. // roll-around ... exotic case (and OHCI has
  638. // a 2^16 iso range, vs other HCs max of 2^10)
  639. frame += cnt * urb->interval;
  640. frame &= 0xffff;
  641. td_fill (ohci, TD_CC | TD_ISO | frame,
  642. data + urb->iso_frame_desc [cnt].offset,
  643. urb->iso_frame_desc [cnt].length, urb, cnt);
  644. }
  645. if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0) {
  646. if (quirk_amdiso(ohci))
  647. usb_amd_quirk_pll_disable();
  648. if (quirk_amdprefetch(ohci))
  649. sb800_prefetch(dev, 1);
  650. }
  651. periodic = ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs++ == 0
  652. && ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0;
  653. break;
  654. }
  655. /* start periodic dma if needed */
  656. if (periodic) {
  657. wmb ();
  658. ohci->hc_control |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
  659. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  660. }
  661. // ASSERT (urb_priv->length == cnt);
  662. }
  663. /*-------------------------------------------------------------------------*
  664. * Done List handling functions
  665. *-------------------------------------------------------------------------*/
  666. /* calculate transfer length/status and update the urb */
  667. static int td_done(struct ohci_hcd *ohci, struct urb *urb, struct td *td)
  668. {
  669. u32 tdINFO = hc32_to_cpup (ohci, &td->hwINFO);
  670. int cc = 0;
  671. int status = -EINPROGRESS;
  672. list_del (&td->td_list);
  673. /* ISO ... drivers see per-TD length/status */
  674. if (tdINFO & TD_ISO) {
  675. u16 tdPSW = ohci_hwPSW(ohci, td, 0);
  676. int dlen = 0;
  677. /* NOTE: assumes FC in tdINFO == 0, and that
  678. * only the first of 0..MAXPSW psws is used.
  679. */
  680. cc = (tdPSW >> 12) & 0xF;
  681. if (tdINFO & TD_CC) /* hc didn't touch? */
  682. return status;
  683. if (usb_pipeout (urb->pipe))
  684. dlen = urb->iso_frame_desc [td->index].length;
  685. else {
  686. /* short reads are always OK for ISO */
  687. if (cc == TD_DATAUNDERRUN)
  688. cc = TD_CC_NOERROR;
  689. dlen = tdPSW & 0x3ff;
  690. }
  691. urb->actual_length += dlen;
  692. urb->iso_frame_desc [td->index].actual_length = dlen;
  693. urb->iso_frame_desc [td->index].status = cc_to_error [cc];
  694. if (cc != TD_CC_NOERROR)
  695. ohci_dbg(ohci,
  696. "urb %p iso td %p (%d) len %d cc %d\n",
  697. urb, td, 1 + td->index, dlen, cc);
  698. /* BULK, INT, CONTROL ... drivers see aggregate length/status,
  699. * except that "setup" bytes aren't counted and "short" transfers
  700. * might not be reported as errors.
  701. */
  702. } else {
  703. int type = usb_pipetype (urb->pipe);
  704. u32 tdBE = hc32_to_cpup (ohci, &td->hwBE);
  705. cc = TD_CC_GET (tdINFO);
  706. /* update packet status if needed (short is normally ok) */
  707. if (cc == TD_DATAUNDERRUN
  708. && !(urb->transfer_flags & URB_SHORT_NOT_OK))
  709. cc = TD_CC_NOERROR;
  710. if (cc != TD_CC_NOERROR && cc < 0x0E)
  711. status = cc_to_error[cc];
  712. /* count all non-empty packets except control SETUP packet */
  713. if ((type != PIPE_CONTROL || td->index != 0) && tdBE != 0) {
  714. if (td->hwCBP == 0)
  715. urb->actual_length += tdBE - td->data_dma + 1;
  716. else
  717. urb->actual_length +=
  718. hc32_to_cpup (ohci, &td->hwCBP)
  719. - td->data_dma;
  720. }
  721. if (cc != TD_CC_NOERROR && cc < 0x0E)
  722. ohci_dbg(ohci,
  723. "urb %p td %p (%d) cc %d, len=%d/%d\n",
  724. urb, td, 1 + td->index, cc,
  725. urb->actual_length,
  726. urb->transfer_buffer_length);
  727. }
  728. return status;
  729. }
  730. /*-------------------------------------------------------------------------*/
  731. static void ed_halted(struct ohci_hcd *ohci, struct td *td, int cc)
  732. {
  733. struct urb *urb = td->urb;
  734. urb_priv_t *urb_priv = urb->hcpriv;
  735. struct ed *ed = td->ed;
  736. struct list_head *tmp = td->td_list.next;
  737. __hc32 toggle = ed->hwHeadP & cpu_to_hc32 (ohci, ED_C);
  738. /* clear ed halt; this is the td that caused it, but keep it inactive
  739. * until its urb->complete() has a chance to clean up.
  740. */
  741. ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
  742. wmb ();
  743. ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_H);
  744. /* Get rid of all later tds from this urb. We don't have
  745. * to be careful: no errors and nothing was transferred.
  746. * Also patch the ed so it looks as if those tds completed normally.
  747. */
  748. while (tmp != &ed->td_list) {
  749. struct td *next;
  750. next = list_entry (tmp, struct td, td_list);
  751. tmp = next->td_list.next;
  752. if (next->urb != urb)
  753. break;
  754. /* NOTE: if multi-td control DATA segments get supported,
  755. * this urb had one of them, this td wasn't the last td
  756. * in that segment (TD_R clear), this ed halted because
  757. * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
  758. * then we need to leave the control STATUS packet queued
  759. * and clear ED_SKIP.
  760. */
  761. list_del(&next->td_list);
  762. urb_priv->td_cnt++;
  763. ed->hwHeadP = next->hwNextTD | toggle;
  764. }
  765. /* help for troubleshooting: report anything that
  766. * looks odd ... that doesn't include protocol stalls
  767. * (or maybe some other things)
  768. */
  769. switch (cc) {
  770. case TD_DATAUNDERRUN:
  771. if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
  772. break;
  773. /* fallthrough */
  774. case TD_CC_STALL:
  775. if (usb_pipecontrol (urb->pipe))
  776. break;
  777. /* fallthrough */
  778. default:
  779. ohci_dbg (ohci,
  780. "urb %p path %s ep%d%s %08x cc %d --> status %d\n",
  781. urb, urb->dev->devpath,
  782. usb_pipeendpoint (urb->pipe),
  783. usb_pipein (urb->pipe) ? "in" : "out",
  784. hc32_to_cpu (ohci, td->hwINFO),
  785. cc, cc_to_error [cc]);
  786. }
  787. }
  788. /* Add a TD to the done list */
  789. static void add_to_done_list(struct ohci_hcd *ohci, struct td *td)
  790. {
  791. struct td *td2, *td_prev;
  792. struct ed *ed;
  793. if (td->next_dl_td)
  794. return; /* Already on the list */
  795. /* Add all the TDs going back until we reach one that's on the list */
  796. ed = td->ed;
  797. td2 = td_prev = td;
  798. list_for_each_entry_continue_reverse(td2, &ed->td_list, td_list) {
  799. if (td2->next_dl_td)
  800. break;
  801. td2->next_dl_td = td_prev;
  802. td_prev = td2;
  803. }
  804. if (ohci->dl_end)
  805. ohci->dl_end->next_dl_td = td_prev;
  806. else
  807. ohci->dl_start = td_prev;
  808. /*
  809. * Make td->next_dl_td point to td itself, to mark the fact
  810. * that td is on the done list.
  811. */
  812. ohci->dl_end = td->next_dl_td = td;
  813. /* Did we just add the latest pending TD? */
  814. td2 = ed->pending_td;
  815. if (td2 && td2->next_dl_td)
  816. ed->pending_td = NULL;
  817. }
  818. /* Get the entries on the hardware done queue and put them on our list */
  819. static void update_done_list(struct ohci_hcd *ohci)
  820. {
  821. u32 td_dma;
  822. struct td *td = NULL;
  823. td_dma = hc32_to_cpup (ohci, &ohci->hcca->done_head);
  824. ohci->hcca->done_head = 0;
  825. wmb();
  826. /* get TD from hc's singly linked list, and
  827. * add to ours. ed->td_list changes later.
  828. */
  829. while (td_dma) {
  830. int cc;
  831. td = dma_to_td (ohci, td_dma);
  832. if (!td) {
  833. ohci_err (ohci, "bad entry %8x\n", td_dma);
  834. break;
  835. }
  836. td->hwINFO |= cpu_to_hc32 (ohci, TD_DONE);
  837. cc = TD_CC_GET (hc32_to_cpup (ohci, &td->hwINFO));
  838. /* Non-iso endpoints can halt on error; un-halt,
  839. * and dequeue any other TDs from this urb.
  840. * No other TD could have caused the halt.
  841. */
  842. if (cc != TD_CC_NOERROR
  843. && (td->ed->hwHeadP & cpu_to_hc32 (ohci, ED_H)))
  844. ed_halted(ohci, td, cc);
  845. td_dma = hc32_to_cpup (ohci, &td->hwNextTD);
  846. add_to_done_list(ohci, td);
  847. }
  848. }
  849. /*-------------------------------------------------------------------------*/
  850. /* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
  851. static void finish_unlinks(struct ohci_hcd *ohci)
  852. {
  853. unsigned tick = ohci_frame_no(ohci);
  854. struct ed *ed, **last;
  855. rescan_all:
  856. for (last = &ohci->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
  857. struct list_head *entry, *tmp;
  858. int completed, modified;
  859. __hc32 *prev;
  860. /* only take off EDs that the HC isn't using, accounting for
  861. * frame counter wraps and EDs with partially retired TDs
  862. */
  863. if (likely(ohci->rh_state == OHCI_RH_RUNNING) &&
  864. tick_before(tick, ed->tick)) {
  865. skip_ed:
  866. last = &ed->ed_next;
  867. continue;
  868. }
  869. if (!list_empty(&ed->td_list)) {
  870. struct td *td;
  871. u32 head;
  872. td = list_first_entry(&ed->td_list, struct td, td_list);
  873. /* INTR_WDH may need to clean up first */
  874. head = hc32_to_cpu(ohci, ed->hwHeadP) & TD_MASK;
  875. if (td->td_dma != head &&
  876. ohci->rh_state == OHCI_RH_RUNNING)
  877. goto skip_ed;
  878. /* Don't mess up anything already on the done list */
  879. if (td->next_dl_td)
  880. goto skip_ed;
  881. }
  882. /* ED's now officially unlinked, hc doesn't see */
  883. ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_H);
  884. ed->hwNextED = 0;
  885. wmb();
  886. ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE);
  887. /* reentrancy: if we drop the schedule lock, someone might
  888. * have modified this list. normally it's just prepending
  889. * entries (which we'd ignore), but paranoia won't hurt.
  890. */
  891. *last = ed->ed_next;
  892. ed->ed_next = NULL;
  893. modified = 0;
  894. /* unlink urbs as requested, but rescan the list after
  895. * we call a completion since it might have unlinked
  896. * another (earlier) urb
  897. *
  898. * When we get here, the HC doesn't see this ed. But it
  899. * must not be rescheduled until all completed URBs have
  900. * been given back to the driver.
  901. */
  902. rescan_this:
  903. completed = 0;
  904. prev = &ed->hwHeadP;
  905. list_for_each_safe (entry, tmp, &ed->td_list) {
  906. struct td *td;
  907. struct urb *urb;
  908. urb_priv_t *urb_priv;
  909. __hc32 savebits;
  910. u32 tdINFO;
  911. td = list_entry (entry, struct td, td_list);
  912. urb = td->urb;
  913. urb_priv = td->urb->hcpriv;
  914. if (!urb->unlinked) {
  915. prev = &td->hwNextTD;
  916. continue;
  917. }
  918. /* patch pointer hc uses */
  919. savebits = *prev & ~cpu_to_hc32 (ohci, TD_MASK);
  920. *prev = td->hwNextTD | savebits;
  921. /* If this was unlinked, the TD may not have been
  922. * retired ... so manually save the data toggle.
  923. * The controller ignores the value we save for
  924. * control and ISO endpoints.
  925. */
  926. tdINFO = hc32_to_cpup(ohci, &td->hwINFO);
  927. if ((tdINFO & TD_T) == TD_T_DATA0)
  928. ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_C);
  929. else if ((tdINFO & TD_T) == TD_T_DATA1)
  930. ed->hwHeadP |= cpu_to_hc32(ohci, ED_C);
  931. /* HC may have partly processed this TD */
  932. td_done (ohci, urb, td);
  933. urb_priv->td_cnt++;
  934. /* if URB is done, clean up */
  935. if (urb_priv->td_cnt >= urb_priv->length) {
  936. modified = completed = 1;
  937. finish_urb(ohci, urb, 0);
  938. }
  939. }
  940. if (completed && !list_empty (&ed->td_list))
  941. goto rescan_this;
  942. /*
  943. * If no TDs are queued, ED is now idle.
  944. * Otherwise, if the HC is running, reschedule.
  945. * If the HC isn't running, add ED back to the
  946. * start of the list for later processing.
  947. */
  948. if (list_empty(&ed->td_list)) {
  949. ed->state = ED_IDLE;
  950. list_del(&ed->in_use_list);
  951. } else if (ohci->rh_state == OHCI_RH_RUNNING) {
  952. ed_schedule(ohci, ed);
  953. } else {
  954. ed->ed_next = ohci->ed_rm_list;
  955. ohci->ed_rm_list = ed;
  956. /* Don't loop on the same ED */
  957. if (last == &ohci->ed_rm_list)
  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. }