ohci-q.c 33 KB

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