ohci-q.c 31 KB

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