ohci-q.c 32 KB

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