ohci-q.c 31 KB

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