ohci-q.c 32 KB

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