ohci-q.c 32 KB

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