ohci-q.c 31 KB

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