ohci-q.c 31 KB

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