ohci-q.c 31 KB

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