ohci-q.c 31 KB

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