ohci-q.c 32 KB

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