ohci-q.c 31 KB

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