ehci-sched.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:32k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Copyright (c) 2001-2002 by David Brownell
  3.  * 
  4.  * This program is free software; you can redistribute it and/or modify it
  5.  * under the terms of the GNU General Public License as published by the
  6.  * Free Software Foundation; either version 2 of the License, or (at your
  7.  * option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful, but
  10.  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11.  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12.  * for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software Foundation,
  16.  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  */
  18. /* this file is part of ehci-hcd.c */
  19. /*-------------------------------------------------------------------------*/
  20. /*
  21.  * EHCI scheduled transaction support:  interrupt, iso, split iso
  22.  * These are called "periodic" transactions in the EHCI spec.
  23.  *
  24.  * Note that for interrupt transfers, the QH/QTD manipulation is shared
  25.  * with the "asynchronous" transaction support (control/bulk transfers).
  26.  * The only real difference is in how interrupt transfers are scheduled.
  27.  * We get some funky API restrictions from the current URB model, which
  28.  * works notably better for reading transfers than for writing.  (And
  29.  * which accordingly needs to change before it'll work inside devices,
  30.  * or with "USB On The Go" additions to USB 2.0 ...)
  31.  */
  32. /*
  33.  * Ceiling microseconds (typical) for that many bytes at high speed
  34.  * ISO is a bit less, no ACK ... from USB 2.0 spec, 5.11.3 (and needed
  35.  * to preallocate bandwidth)
  36.  */
  37. #define EHCI_HOST_DELAY 5 /* nsec, guess */
  38. #define HS_USECS(bytes) NS_TO_US ( ((55 * 8 * 2083)/1000) 
  39. + ((2083UL * (3167 + BitTime (bytes)))/1000) 
  40. + EHCI_HOST_DELAY)
  41. #define HS_USECS_ISO(bytes) NS_TO_US ( ((long)(38 * 8 * 2.083)) 
  42. + ((2083UL * (3167 + BitTime (bytes)))/1000) 
  43. + EHCI_HOST_DELAY)
  44. static int ehci_get_frame (struct usb_hcd *hcd);
  45. /*-------------------------------------------------------------------------*/
  46. /*
  47.  * periodic_next_shadow - return "next" pointer on shadow list
  48.  * @periodic: host pointer to qh/itd/sitd
  49.  * @tag: hardware tag for type of this record
  50.  */
  51. static union ehci_shadow *
  52. periodic_next_shadow (union ehci_shadow *periodic, int tag)
  53. {
  54. switch (tag) {
  55. case Q_TYPE_QH:
  56. return &periodic->qh->qh_next;
  57. case Q_TYPE_FSTN:
  58. return &periodic->fstn->fstn_next;
  59. case Q_TYPE_ITD:
  60. return &periodic->itd->itd_next;
  61. #ifdef have_split_iso
  62. case Q_TYPE_SITD:
  63. return &periodic->sitd->sitd_next;
  64. #endif /* have_split_iso */
  65. }
  66. dbg ("BAD shadow %p tag %d", periodic->ptr, tag);
  67. // BUG ();
  68. return 0;
  69. }
  70. /* returns true after successful unlink */
  71. /* caller must hold ehci->lock */
  72. static int periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
  73. {
  74. union ehci_shadow *prev_p = &ehci->pshadow [frame];
  75. u32 *hw_p = &ehci->periodic [frame];
  76. union ehci_shadow here = *prev_p;
  77. union ehci_shadow *next_p;
  78. /* find predecessor of "ptr"; hw and shadow lists are in sync */
  79. while (here.ptr && here.ptr != ptr) {
  80. prev_p = periodic_next_shadow (prev_p, Q_NEXT_TYPE (*hw_p));
  81. hw_p = &here.qh->hw_next;
  82. here = *prev_p;
  83. }
  84. /* an interrupt entry (at list end) could have been shared */
  85. if (!here.ptr) {
  86. dbg ("entry %p no longer on frame [%d]", ptr, frame);
  87. return 0;
  88. }
  89. // vdbg ("periodic unlink %p from frame %d", ptr, frame);
  90. /* update hardware list ... HC may still know the old structure, so
  91.  * don't change hw_next until it'll have purged its cache
  92.  */
  93. next_p = periodic_next_shadow (&here, Q_NEXT_TYPE (*hw_p));
  94. *hw_p = here.qh->hw_next;
  95. /* unlink from shadow list; HCD won't see old structure again */
  96. *prev_p = *next_p;
  97. next_p->ptr = 0;
  98. return 1;
  99. }
  100. /* how many of the uframe's 125 usecs are allocated? */
  101. static unsigned short
  102. periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
  103. {
  104. u32 *hw_p = &ehci->periodic [frame];
  105. union ehci_shadow *q = &ehci->pshadow [frame];
  106. unsigned usecs = 0;
  107. while (q->ptr) {
  108. switch (Q_NEXT_TYPE (*hw_p)) {
  109. case Q_TYPE_QH:
  110. /* is it in the S-mask? */
  111. if (q->qh->hw_info2 & cpu_to_le32 (1 << uframe))
  112. usecs += q->qh->usecs;
  113. q = &q->qh->qh_next;
  114. break;
  115. case Q_TYPE_FSTN:
  116. /* for "save place" FSTNs, count the relevant INTR
  117.  * bandwidth from the previous frame
  118.  */
  119. if (q->fstn->hw_prev != EHCI_LIST_END) {
  120. dbg ("not counting FSTN bandwidth yet ...");
  121. }
  122. q = &q->fstn->fstn_next;
  123. break;
  124. case Q_TYPE_ITD:
  125. /* NOTE the "one uframe per itd" policy */
  126. if (q->itd->hw_transaction [uframe] != 0)
  127. usecs += q->itd->usecs;
  128. q = &q->itd->itd_next;
  129. break;
  130. #ifdef have_split_iso
  131. case Q_TYPE_SITD:
  132. temp = q->sitd->hw_fullspeed_ep &
  133. __constant_cpu_to_le32 (1 << 31);
  134. // FIXME:  this doesn't count data bytes right...
  135. /* is it in the S-mask?  (count SPLIT, DATA) */
  136. if (q->sitd->hw_uframe & cpu_to_le32 (1 << uframe)) {
  137. if (temp)
  138. usecs += HS_USECS (188);
  139. else
  140. usecs += HS_USECS (1);
  141. }
  142. /* ... C-mask?  (count CSPLIT, DATA) */
  143. if (q->sitd->hw_uframe &
  144. cpu_to_le32 (1 << (8 + uframe))) {
  145. if (temp)
  146. usecs += HS_USECS (0);
  147. else
  148. usecs += HS_USECS (188);
  149. }
  150. q = &q->sitd->sitd_next;
  151. break;
  152. #endif /* have_split_iso */
  153. default:
  154. BUG ();
  155. }
  156. }
  157. #ifdef DEBUG
  158. if (usecs > 100)
  159. err ("overallocated uframe %d, periodic is %d usecs",
  160. frame * 8 + uframe, usecs);
  161. #endif
  162. return usecs;
  163. }
  164. /*-------------------------------------------------------------------------*/
  165. static void enable_periodic (struct ehci_hcd *ehci)
  166. {
  167. u32 cmd;
  168. /* did clearing PSE did take effect yet?
  169.  * takes effect only at frame boundaries...
  170.  */
  171. while (readl (&ehci->regs->status) & STS_PSS)
  172. udelay (20);
  173. cmd = readl (&ehci->regs->command) | CMD_PSE;
  174. writel (cmd, &ehci->regs->command);
  175. /* posted write ... PSS happens later */
  176. ehci->hcd.state = USB_STATE_RUNNING;
  177. /* make sure tasklet scans these */
  178. ehci->next_uframe = readl (&ehci->regs->frame_index)
  179. % (ehci->periodic_size << 3);
  180. }
  181. static void disable_periodic (struct ehci_hcd *ehci)
  182. {
  183. u32 cmd;
  184. /* did setting PSE not take effect yet?
  185.  * takes effect only at frame boundaries...
  186.  */
  187. while (!(readl (&ehci->regs->status) & STS_PSS))
  188. udelay (20);
  189. cmd = readl (&ehci->regs->command) & ~CMD_PSE;
  190. writel (cmd, &ehci->regs->command);
  191. /* posted write ... */
  192. ehci->next_uframe = -1;
  193. }
  194. /*-------------------------------------------------------------------------*/
  195. static void intr_deschedule (
  196. struct ehci_hcd *ehci,
  197. unsigned frame,
  198. struct ehci_qh *qh,
  199. unsigned period
  200. ) {
  201. unsigned long flags;
  202. period >>= 3; // FIXME microframe periods not handled yet
  203. spin_lock_irqsave (&ehci->lock, flags);
  204. do {
  205. periodic_unlink (ehci, frame, qh);
  206. qh_put (ehci, qh);
  207. frame += period;
  208. } while (frame < ehci->periodic_size);
  209. qh->qh_state = QH_STATE_UNLINK;
  210. qh->qh_next.ptr = 0;
  211. ehci->periodic_urbs--;
  212. /* maybe turn off periodic schedule */
  213. if (!ehci->periodic_urbs)
  214. disable_periodic (ehci);
  215. else
  216. vdbg ("periodic schedule still enabled");
  217. spin_unlock_irqrestore (&ehci->lock, flags);
  218. /*
  219.  * If the hc may be looking at this qh, then delay a uframe
  220.  * (yeech!) to be sure it's done.
  221.  * No other threads may be mucking with this qh.
  222.  */
  223. if (((ehci_get_frame (&ehci->hcd) - frame) % period) == 0)
  224. udelay (125);
  225. qh->qh_state = QH_STATE_IDLE;
  226. qh->hw_next = EHCI_LIST_END;
  227. vdbg ("descheduled qh %p, per = %d frame = %d count = %d, urbs = %d",
  228. qh, period, frame,
  229. atomic_read (&qh->refcount), ehci->periodic_urbs);
  230. }
  231. static int check_period (
  232. struct ehci_hcd *ehci, 
  233. unsigned frame,
  234. int uframe,
  235. unsigned period,
  236. unsigned usecs
  237. ) {
  238. /*
  239.  * 80% periodic == 100 usec/uframe available
  240.  * convert "usecs we need" to "max already claimed" 
  241.  */
  242. usecs = 100 - usecs;
  243. do {
  244. int claimed;
  245. // FIXME delete when intr_submit handles non-empty queues
  246. // this gives us a one intr/frame limit (vs N/uframe)
  247. if (ehci->pshadow [frame].ptr)
  248. return 0;
  249. claimed = periodic_usecs (ehci, frame, uframe);
  250. if (claimed > usecs)
  251. return 0;
  252. // FIXME update to handle sub-frame periods
  253. } while ((frame += period) < ehci->periodic_size);
  254. // success!
  255. return 1;
  256. }
  257. static int intr_submit (
  258. struct ehci_hcd *ehci,
  259. struct urb *urb,
  260. struct list_head *qtd_list,
  261. int mem_flags
  262. ) {
  263. unsigned epnum, period;
  264. unsigned short usecs;
  265. unsigned long flags;
  266. struct ehci_qh *qh;
  267. struct hcd_dev *dev;
  268. int status = 0;
  269. /* get endpoint and transfer data */
  270. epnum = usb_pipeendpoint (urb->pipe);
  271. if (usb_pipein (urb->pipe))
  272. epnum |= 0x10;
  273. if (urb->dev->speed != USB_SPEED_HIGH) {
  274. dbg ("no intr/tt scheduling yet"); 
  275. status = -ENOSYS;
  276. goto done;
  277. }
  278. /*
  279.  * NOTE: current completion/restart logic doesn't handle more than
  280.  * one qtd in a periodic qh ... 16-20 KB/urb is pretty big for this.
  281.  * such big requests need many periods to transfer.
  282.  *
  283.  * FIXME want to change hcd core submit model to expect queuing
  284.  * for all transfer types ... not just ISO and (with flag) BULK.
  285.  * that means: getting rid of this check; handling the "interrupt
  286.  * urb already queued" case below like bulk queuing is handled (no
  287.  * errors possible!); and completly getting rid of that annoying
  288.  * qh restart logic.  simpler/smaller overall, and more flexible.
  289.  */
  290. if (unlikely (qtd_list->next != qtd_list->prev)) {
  291. dbg ("only one intr qtd per urb allowed"); 
  292. status = -EINVAL;
  293. goto done;
  294. }
  295. usecs = HS_USECS (urb->transfer_buffer_length);
  296. /* FIXME handle HS periods of less than 1 frame. */
  297. period = urb->interval >> 3;
  298. if (period < 1) {
  299. dbg ("intr period %d uframes, NYET!", urb->interval);
  300. status = -EINVAL;
  301. goto done;
  302. }
  303. spin_lock_irqsave (&ehci->lock, flags);
  304. /* get the qh (must be empty and idle) */
  305. dev = (struct hcd_dev *)urb->dev->hcpriv;
  306. qh = (struct ehci_qh *) dev->ep [epnum];
  307. if (qh) {
  308. /* only allow one queued interrupt urb per EP */
  309. if (unlikely (qh->qh_state != QH_STATE_IDLE
  310. || !list_empty (&qh->qtd_list))) {
  311. dbg ("interrupt urb already queued");
  312. status = -EBUSY;
  313. } else {
  314. /* maybe reset hardware's data toggle in the qh */
  315. if (unlikely (!usb_gettoggle (urb->dev, epnum & 0x0f,
  316. !(epnum & 0x10)))) {
  317. qh->hw_token |=
  318. __constant_cpu_to_le32 (QTD_TOGGLE);
  319. usb_settoggle (urb->dev, epnum & 0x0f,
  320. !(epnum & 0x10), 1);
  321. }
  322. /* trust the QH was set up as interrupt ... */
  323. list_splice (qtd_list, &qh->qtd_list);
  324. qh_update (qh, list_entry (qtd_list->next,
  325. struct ehci_qtd, qtd_list));
  326. qtd_list = &qh->qtd_list;
  327. }
  328. } else {
  329. /* can't sleep here, we have ehci->lock... */
  330. qh = ehci_qh_make (ehci, urb, qtd_list, SLAB_ATOMIC);
  331. if (likely (qh != 0)) {
  332. // dbg ("new INTR qh %p", qh);
  333. dev->ep [epnum] = qh;
  334. qtd_list = &qh->qtd_list;
  335. } else
  336. status = -ENOMEM;
  337. }
  338. /* Schedule this periodic QH. */
  339. if (likely (status == 0)) {
  340. unsigned frame = period;
  341. qh->hw_next = EHCI_LIST_END;
  342. qh->usecs = usecs;
  343. urb->hcpriv = qh_get (qh);
  344. status = -ENOSPC;
  345. /* pick a set of schedule slots, link the QH into them */
  346. do {
  347. int uframe;
  348. /* pick a set of slots such that all uframes have
  349.  * enough periodic bandwidth available.
  350.  *
  351.  * FIXME for TT splits, need uframes for start and end.
  352.  * FSTNs can put end into next frame (uframes 0 or 1).
  353.  */
  354. frame--;
  355. for (uframe = 0; uframe < 8; uframe++) {
  356. if (check_period (ehci, frame, uframe,
  357. period, usecs) != 0)
  358. break;
  359. }
  360. if (uframe == 8)
  361. continue;
  362. /* QH will run once each period, starting there  */
  363. urb->start_frame = frame;
  364. status = 0;
  365. /* set S-frame mask */
  366. qh->hw_info2 |= cpu_to_le32 (1 << uframe);
  367. // dbg_qh ("Schedule INTR qh", ehci, qh);
  368. /* stuff into the periodic schedule */
  369. qh->qh_state = QH_STATE_LINKED;
  370. vdbg ("qh %p usecs %d period %d starting %d.%d",
  371. qh, qh->usecs, period, frame, uframe);
  372. do {
  373. if (unlikely (ehci->pshadow [frame].ptr != 0)) {
  374. // FIXME -- just link toward the end, before any qh with a shorter period,
  375. // AND handle it already being (implicitly) linked into this frame
  376. // AS WELL AS updating the check_period() logic
  377. BUG ();
  378. } else {
  379. ehci->pshadow [frame].qh = qh_get (qh);
  380. ehci->periodic [frame] =
  381. QH_NEXT (qh->qh_dma);
  382. }
  383. wmb ();
  384. frame += period;
  385. } while (frame < ehci->periodic_size);
  386. /* update bandwidth utilization records (for usbfs) */
  387. usb_claim_bandwidth (urb->dev, urb, usecs/period, 0);
  388. /* maybe enable periodic schedule processing */
  389. if (!ehci->periodic_urbs++)
  390. enable_periodic (ehci);
  391. break;
  392. } while (frame);
  393. }
  394. spin_unlock_irqrestore (&ehci->lock, flags);
  395. done:
  396. if (status)
  397. qtd_list_free (ehci, urb, qtd_list);
  398. return status;
  399. }
  400. static unsigned long
  401. intr_complete (
  402. struct ehci_hcd *ehci,
  403. unsigned frame,
  404. struct ehci_qh *qh,
  405. unsigned long flags /* caller owns ehci->lock ... */
  406. ) {
  407. struct ehci_qtd *qtd;
  408. struct urb *urb;
  409. int unlinking;
  410. /* nothing to report? */
  411. if (likely ((qh->hw_token & __constant_cpu_to_le32 (QTD_STS_ACTIVE))
  412. != 0))
  413. return flags;
  414. if (unlikely (list_empty (&qh->qtd_list))) {
  415. dbg ("intr qh %p no TDs?", qh);
  416. return flags;
  417. }
  418. qtd = list_entry (qh->qtd_list.next, struct ehci_qtd, qtd_list);
  419. urb = qtd->urb;
  420. unlinking = (urb->status == -ENOENT) || (urb->status == -ECONNRESET);
  421. /* call any completions, after patching for reactivation */
  422. spin_unlock_irqrestore (&ehci->lock, flags);
  423. /* NOTE:  currently restricted to one qtd per qh! */
  424. if (qh_completions (ehci, qh, 0) == 0)
  425. urb = 0;
  426. spin_lock_irqsave (&ehci->lock, flags);
  427. /* never reactivate requests that were unlinked ... */
  428. if (likely (urb != 0)) {
  429. if (unlinking
  430. || urb->status == -ECONNRESET
  431. || urb->status == -ENOENT
  432. // || (urb->dev == null)
  433. || ehci->hcd.state == USB_STATE_HALT)
  434. urb = 0;
  435. // FIXME look at all those unlink cases ... we always
  436. // need exactly one completion that reports unlink.
  437. // the one above might not have been it!
  438. }
  439. /* normally reactivate */
  440. if (likely (urb != 0)) {
  441. if (usb_pipeout (urb->pipe))
  442. pci_dma_sync_single (ehci->hcd.pdev,
  443. qtd->buf_dma,
  444. urb->transfer_buffer_length,
  445. PCI_DMA_TODEVICE);
  446. urb->status = -EINPROGRESS;
  447. urb->actual_length = 0;
  448. /* patch qh and restart */
  449. qh_update (qh, qtd);
  450. }
  451. return flags;
  452. }
  453. /*-------------------------------------------------------------------------*/
  454. static void
  455. itd_free_list (struct ehci_hcd *ehci, struct urb *urb)
  456. {
  457. struct ehci_itd *first_itd = urb->hcpriv;
  458. pci_unmap_single (ehci->hcd.pdev,
  459. first_itd->buf_dma, urb->transfer_buffer_length,
  460. usb_pipein (urb->pipe)
  461.     ? PCI_DMA_FROMDEVICE
  462.     : PCI_DMA_TODEVICE);
  463. while (!list_empty (&first_itd->itd_list)) {
  464. struct ehci_itd *itd;
  465. itd = list_entry (
  466. first_itd->itd_list.next,
  467. struct ehci_itd, itd_list);
  468. list_del (&itd->itd_list);
  469. pci_pool_free (ehci->itd_pool, itd, itd->itd_dma);
  470. }
  471. pci_pool_free (ehci->itd_pool, first_itd, first_itd->itd_dma);
  472. urb->hcpriv = 0;
  473. }
  474. static int
  475. itd_fill (
  476. struct ehci_hcd *ehci,
  477. struct ehci_itd *itd,
  478. struct urb *urb,
  479. unsigned index, // urb->iso_frame_desc [index]
  480. dma_addr_t dma // mapped transfer buffer
  481. ) {
  482. u64 temp;
  483. u32 buf1;
  484. unsigned i, epnum, maxp, multi;
  485. unsigned length;
  486. itd->hw_next = EHCI_LIST_END;
  487. itd->urb = urb;
  488. itd->index = index;
  489. /* tell itd about its transfer buffer, max 2 pages */
  490. length = urb->iso_frame_desc [index].length;
  491. dma += urb->iso_frame_desc [index].offset;
  492. temp = dma & ~0x0fff;
  493. for (i = 0; i < 2; i++) {
  494. itd->hw_bufp [i] = cpu_to_le32 ((u32) temp);
  495. itd->hw_bufp_hi [i] = cpu_to_le32 ((u32)(temp >> 32));
  496. temp += 0x1000;
  497. }
  498. itd->buf_dma = dma;
  499. /*
  500.  * this might be a "high bandwidth" highspeed endpoint,
  501.  * as encoded in the ep descriptor's maxpacket field
  502.  */
  503. epnum = usb_pipeendpoint (urb->pipe);
  504. if (usb_pipein (urb->pipe)) {
  505. maxp = urb->dev->epmaxpacketin [epnum];
  506. buf1 = (1 << 11);
  507. } else {
  508. maxp = urb->dev->epmaxpacketout [epnum];
  509. buf1 = 0;
  510. }
  511. buf1 |= (maxp & 0x03ff);
  512. multi = 1;
  513. multi += (maxp >> 11) & 0x03;
  514. maxp &= 0x03ff;
  515. maxp *= multi;
  516. /* transfer can't fit in any uframe? */ 
  517. if (length < 0 || maxp < length) {
  518. dbg ("BAD iso packet: %d bytes, max %d, urb %p [%d] (of %d)",
  519. length, maxp, urb, index,
  520. urb->iso_frame_desc [index].length);
  521. return -ENOSPC;
  522. }
  523. itd->usecs = HS_USECS_ISO (length);
  524. /* "plus" info in low order bits of buffer pointers */
  525. itd->hw_bufp [0] |= cpu_to_le32 ((epnum << 8) | urb->dev->devnum);
  526. itd->hw_bufp [1] |= cpu_to_le32 (buf1);
  527. itd->hw_bufp [2] |= cpu_to_le32 (multi);
  528. /* figure hw_transaction[] value (it's scheduled later) */
  529. itd->transaction = EHCI_ISOC_ACTIVE;
  530. itd->transaction |= dma & 0x0fff; /* offset; buffer=0 */
  531. if ((index + 1) == urb->number_of_packets)
  532. itd->transaction |= EHCI_ITD_IOC;  /* end-of-urb irq */
  533. itd->transaction |= length << 16;
  534. cpu_to_le32s (&itd->transaction);
  535. return 0;
  536. }
  537. static int
  538. itd_urb_transaction (
  539. struct ehci_hcd *ehci,
  540. struct urb *urb,
  541. int mem_flags
  542. ) {
  543. int frame_index;
  544. struct ehci_itd *first_itd, *itd;
  545. int status;
  546. dma_addr_t buf_dma, itd_dma;
  547. /* set up one dma mapping for this urb */
  548. buf_dma = pci_map_single (ehci->hcd.pdev,
  549. urb->transfer_buffer, urb->transfer_buffer_length,
  550. usb_pipein (urb->pipe)
  551.     ? PCI_DMA_FROMDEVICE
  552.     : PCI_DMA_TODEVICE);
  553. if (buf_dma == 0)
  554. return -ENOMEM;
  555. /* allocate/init ITDs */
  556. for (frame_index = 0, first_itd = 0;
  557. frame_index < urb->number_of_packets;
  558. frame_index++) {
  559. itd = pci_pool_alloc (ehci->itd_pool, mem_flags, &itd_dma);
  560. if (!itd) {
  561. status = -ENOMEM;
  562. goto fail;
  563. }
  564. memset (itd, 0, sizeof *itd);
  565. itd->itd_dma = itd_dma;
  566. status = itd_fill (ehci, itd, urb, frame_index, buf_dma);
  567. if (status != 0)
  568. goto fail;
  569. if (first_itd)
  570. list_add_tail (&itd->itd_list,
  571. &first_itd->itd_list);
  572. else {
  573. INIT_LIST_HEAD (&itd->itd_list);
  574. urb->hcpriv = first_itd = itd;
  575. }
  576. }
  577. urb->error_count = 0;
  578. return 0;
  579. fail:
  580. if (urb->hcpriv)
  581. itd_free_list (ehci, urb);
  582. return status;
  583. }
  584. /*-------------------------------------------------------------------------*/
  585. static inline void
  586. itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
  587. {
  588. /* always prepend ITD/SITD ... only QH tree is order-sensitive */
  589. itd->itd_next = ehci->pshadow [frame];
  590. itd->hw_next = ehci->periodic [frame];
  591. ehci->pshadow [frame].itd = itd;
  592. ehci->periodic [frame] = cpu_to_le32 (itd->itd_dma) | Q_TYPE_ITD;
  593. }
  594. /*
  595.  * return zero on success, else -errno
  596.  * - start holds first uframe to start scheduling into
  597.  * - max is the first uframe it's NOT (!) OK to start scheduling into
  598.  * math to be done modulo "mod" (ehci->periodic_size << 3)
  599.  */
  600. static int get_iso_range (
  601. struct ehci_hcd *ehci,
  602. struct urb *urb,
  603. unsigned *start,
  604. unsigned *max,
  605. unsigned mod
  606. ) {
  607. struct list_head *lh;
  608. struct hcd_dev *dev = urb->dev->hcpriv;
  609. int last = -1;
  610. unsigned now, span, end;
  611. span = urb->interval * urb->number_of_packets;
  612. /* first see if we know when the next transfer SHOULD happen */
  613. list_for_each (lh, &dev->urb_list) {
  614. struct urb *u;
  615. struct ehci_itd *itd;
  616. unsigned s;
  617. u = list_entry (lh, struct urb, urb_list);
  618. if (u == urb || u->pipe != urb->pipe)
  619. continue;
  620. if (u->interval != urb->interval) { /* must not change! */ 
  621. dbg ("urb %p interval %d ... != %p interval %d",
  622. u, u->interval, urb, urb->interval);
  623. return -EINVAL;
  624. }
  625. /* URB for this endpoint... covers through when?  */
  626. itd = urb->hcpriv;
  627. s = itd->uframe + u->interval * u->number_of_packets;
  628. if (last < 0)
  629. last = s;
  630. else {
  631. /*
  632.  * So far we can only queue two ISO URBs...
  633.  *
  634.  * FIXME do interval math, figure out whether
  635.  * this URB is "before" or not ... also, handle
  636.  * the case where the URB might have completed,
  637.  * but hasn't yet been processed.
  638.  */
  639. dbg ("NYET: queue >2 URBs per ISO endpoint");
  640. return -EDOM;
  641. }
  642. }
  643. /* calculate the legal range [start,max) */
  644. now = readl (&ehci->regs->frame_index) + 1; /* next uframe */
  645. if (!ehci->periodic_urbs)
  646. now += 8; /* startup delay */
  647. now %= mod;
  648. end = now + mod;
  649. if (last < 0) {
  650. *start = now + ehci->i_thresh + /* paranoia */ 1;
  651. *max = end - span;
  652. if (*max < *start + 1)
  653. *max = *start + 1;
  654. } else {
  655. *start = last % mod;
  656. *max = (last + 1) % mod;
  657. }
  658. /* explicit start frame? */
  659. if (!(urb->transfer_flags & USB_ISO_ASAP)) {
  660. unsigned temp;
  661. /* sanity check: must be in range */
  662. urb->start_frame %= ehci->periodic_size;
  663. temp = urb->start_frame << 3;
  664. if (temp < *start)
  665. temp += mod;
  666. if (temp > *max)
  667. return -EDOM;
  668. /* use that explicit start frame */
  669. *start = urb->start_frame << 3;
  670. temp += 8;
  671. if (temp < *max)
  672. *max = temp;
  673. }
  674. // FIXME minimize wraparound to "now" ... insist max+span
  675. // (and start+span) remains a few frames short of "end"
  676. *max %= ehci->periodic_size;
  677. if ((*start + span) < end)
  678. return 0;
  679. return -EFBIG;
  680. }
  681. static int
  682. itd_schedule (struct ehci_hcd *ehci, struct urb *urb)
  683. {
  684. unsigned start, max, i;
  685. int status;
  686. unsigned mod = ehci->periodic_size << 3;
  687. for (i = 0; i < urb->number_of_packets; i++) {
  688. urb->iso_frame_desc [i].status = -EINPROGRESS;
  689. urb->iso_frame_desc [i].actual_length = 0;
  690. }
  691. if ((status = get_iso_range (ehci, urb, &start, &max, mod)) != 0)
  692. return status;
  693. do {
  694. unsigned uframe;
  695. unsigned usecs;
  696. struct ehci_itd *itd;
  697. /* check schedule: enough space? */
  698. itd = urb->hcpriv;
  699. uframe = start;
  700. for (i = 0, uframe = start;
  701. i < urb->number_of_packets;
  702. i++, uframe += urb->interval) {
  703. uframe %= mod;
  704. /* can't commit more than 80% periodic == 100 usec */
  705. if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
  706. > (100 - itd->usecs)) {
  707. itd = 0;
  708. break;
  709. }
  710. itd = list_entry (itd->itd_list.next,
  711. struct ehci_itd, itd_list);
  712. }
  713. if (!itd)
  714. continue;
  715. /* that's where we'll schedule this! */
  716. itd = urb->hcpriv;
  717. urb->start_frame = start >> 3;
  718. vdbg ("ISO urb %p (%d packets period %d) starting %d.%d",
  719. urb, urb->number_of_packets, urb->interval,
  720. urb->start_frame, start & 0x7);
  721. for (i = 0, uframe = start, usecs = 0;
  722. i < urb->number_of_packets;
  723. i++, uframe += urb->interval) {
  724. uframe %= mod;
  725. itd->uframe = uframe;
  726. itd->hw_transaction [uframe & 0x07] = itd->transaction;
  727. itd_link (ehci, (uframe >> 3) % ehci->periodic_size,
  728. itd);
  729. wmb ();
  730. usecs += itd->usecs;
  731. itd = list_entry (itd->itd_list.next,
  732. struct ehci_itd, itd_list);
  733. }
  734. /* update bandwidth utilization records (for usbfs)
  735.  *
  736.  * FIXME This claims each URB queued to an endpoint, as if
  737.  * transfers were concurrent, not sequential.  So bandwidth
  738.  * typically gets double-billed ... comes from tying it to
  739.  * URBs rather than endpoints in the schedule.  Luckily we
  740.  * don't use this usbfs data for serious decision making.
  741.  */
  742. usecs /= urb->number_of_packets;
  743. usecs /= urb->interval;
  744. usecs >>= 3;
  745. if (usecs < 1)
  746. usecs = 1;
  747. usb_claim_bandwidth (urb->dev, urb, usecs, 1);
  748. /* maybe enable periodic schedule processing */
  749. if (!ehci->periodic_urbs++)
  750. enable_periodic (ehci);
  751. return 0;
  752. } while ((start = ++start % mod) != max);
  753. /* no room in the schedule */
  754. dbg ("urb %p, CAN'T SCHEDULE", urb);
  755. return -ENOSPC;
  756. }
  757. /*-------------------------------------------------------------------------*/
  758. #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
  759. static unsigned long
  760. itd_complete (
  761. struct ehci_hcd *ehci,
  762. struct ehci_itd *itd,
  763. unsigned uframe,
  764. unsigned long flags
  765. ) {
  766. struct urb *urb = itd->urb;
  767. struct iso_packet_descriptor *desc;
  768. u32 t;
  769. /* update status for this uframe's transfers */
  770. desc = &urb->iso_frame_desc [itd->index];
  771. t = itd->hw_transaction [uframe];
  772. itd->hw_transaction [uframe] = 0;
  773. if (t & EHCI_ISOC_ACTIVE)
  774. desc->status = -EXDEV;
  775. else if (t & ISO_ERRS) {
  776. urb->error_count++;
  777. if (t & EHCI_ISOC_BUF_ERR)
  778. desc->status = usb_pipein (urb->pipe)
  779. ? -ENOSR  /* couldn't read */
  780. : -ECOMM; /* couldn't write */
  781. else if (t & EHCI_ISOC_BABBLE)
  782. desc->status = -EOVERFLOW;
  783. else /* (t & EHCI_ISOC_XACTERR) */
  784. desc->status = -EPROTO;
  785. /* HC need not update length with this error */
  786. if (!(t & EHCI_ISOC_BABBLE))
  787. desc->actual_length += EHCI_ITD_LENGTH (t);
  788. } else {
  789. desc->status = 0;
  790. desc->actual_length += EHCI_ITD_LENGTH (t);
  791. }
  792. vdbg ("itd %p urb %p packet %d/%d trans %x status %d len %d",
  793. itd, urb, itd->index + 1, urb->number_of_packets,
  794. t, desc->status, desc->actual_length);
  795. /* handle completion now? */
  796. if ((itd->index + 1) != urb->number_of_packets)
  797. return flags;
  798. /*
  799.  * Always give the urb back to the driver ... expect it to submit
  800.  * a new urb (or resubmit this), and to have another already queued
  801.  * when un-interrupted transfers are needed.
  802.  *
  803.  * NOTE that for now we don't accelerate ISO unlinks; they just
  804.  * happen according to the current schedule.  Means a delay of
  805.  * up to about a second (max).
  806.  */
  807. itd_free_list (ehci, urb);
  808. if (urb->status == -EINPROGRESS)
  809. urb->status = 0;
  810. spin_unlock_irqrestore (&ehci->lock, flags);
  811. usb_hcd_giveback_urb (&ehci->hcd, urb);
  812. spin_lock_irqsave (&ehci->lock, flags);
  813. /* defer stopping schedule; completion can submit */
  814. ehci->periodic_urbs--;
  815. if (!ehci->periodic_urbs)
  816. disable_periodic (ehci);
  817. return flags;
  818. }
  819. /*-------------------------------------------------------------------------*/
  820. static int itd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
  821. {
  822. int status;
  823. unsigned long flags;
  824. dbg ("itd_submit urb %p", urb);
  825. /* NOTE DMA mapping assumes this ... */
  826. if (urb->iso_frame_desc [0].offset != 0)
  827. return -EINVAL;
  828. /* allocate ITDs w/o locking anything */
  829. status = itd_urb_transaction (ehci, urb, mem_flags);
  830. if (status < 0)
  831. return status;
  832. /* schedule ... need to lock */
  833. spin_lock_irqsave (&ehci->lock, flags);
  834. status = itd_schedule (ehci, urb);
  835. spin_unlock_irqrestore (&ehci->lock, flags);
  836. if (status < 0)
  837. itd_free_list (ehci, urb);
  838. return status;
  839. }
  840. #ifdef have_split_iso
  841. /*-------------------------------------------------------------------------*/
  842. /*
  843.  * "Split ISO TDs" ... used for USB 1.1 devices going through
  844.  * the TTs in USB 2.0 hubs.
  845.  */
  846. static void
  847. sitd_free (struct ehci_hcd *ehci, struct ehci_sitd *sitd)
  848. {
  849. pci_pool_free (ehci->sitd_pool, sitd, sitd->sitd_dma);
  850. }
  851. static struct ehci_sitd *
  852. sitd_make (
  853. struct ehci_hcd *ehci,
  854. struct urb *urb,
  855. unsigned index, // urb->iso_frame_desc [index]
  856. unsigned uframe, // scheduled start
  857. dma_addr_t dma, // mapped transfer buffer
  858. int mem_flags
  859. ) {
  860. struct ehci_sitd *sitd;
  861. unsigned length;
  862. sitd = pci_pool_alloc (ehci->sitd_pool, mem_flags, &dma);
  863. if (!sitd)
  864. return sitd;
  865. sitd->urb = urb;
  866. length = urb->iso_frame_desc [index].length;
  867. dma += urb->iso_frame_desc [index].offset;
  868. #if 0
  869. // FIXME:  do the rest!
  870. #else
  871. sitd_free (ehci, sitd);
  872. return 0;
  873. #endif
  874. }
  875. static void
  876. sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
  877. {
  878. u32 ptr;
  879. ptr = cpu_to_le32 (sitd->sitd_dma | 2); // type 2 == sitd
  880. if (ehci->pshadow [frame].ptr) {
  881. if (!sitd->sitd_next.ptr) {
  882. sitd->sitd_next = ehci->pshadow [frame];
  883. sitd->hw_next = ehci->periodic [frame];
  884. } else if (sitd->sitd_next.ptr != ehci->pshadow [frame].ptr) {
  885. dbg ("frame %d sitd link goof", frame);
  886. BUG ();
  887. }
  888. }
  889. ehci->pshadow [frame].sitd = sitd;
  890. ehci->periodic [frame] = ptr;
  891. }
  892. static unsigned long
  893. sitd_complete (
  894. struct ehci_hcd *ehci,
  895. struct ehci_sitd *sitd,
  896. unsigned long flags
  897. ) {
  898. // FIXME -- implement!
  899. dbg ("NYI -- sitd_complete");
  900. return flags;
  901. }
  902. /*-------------------------------------------------------------------------*/
  903. static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
  904. {
  905. // struct ehci_sitd *first_sitd = 0;
  906. unsigned frame_index;
  907. dma_addr_t dma;
  908. dbg ("NYI -- sitd_submit");
  909. // FIXME -- implement!
  910. // FIXME:  setup one big dma mapping
  911. dma = 0;
  912. for (frame_index = 0;
  913. frame_index < urb->number_of_packets;
  914. frame_index++) {
  915. struct ehci_sitd *sitd;
  916. unsigned uframe;
  917. // FIXME:  use real arguments, schedule this!
  918. uframe = -1;
  919. sitd = sitd_make (ehci, urb, frame_index,
  920. uframe, dma, mem_flags);
  921. if (sitd) {
  922.     /*
  923. if (first_sitd)
  924. list_add_tail (&sitd->sitd_list,
  925. &first_sitd->sitd_list);
  926. else
  927. first_sitd = sitd;
  928.     */
  929. } else {
  930. // FIXME:  clean everything up
  931. }
  932. }
  933. // if we have a first sitd, then
  934. // store them all into the periodic schedule!
  935. // urb->hcpriv = first sitd in sitd_list
  936. return -ENOSYS;
  937. }
  938. #endif /* have_split_iso */
  939. /*-------------------------------------------------------------------------*/
  940. static void scan_periodic (struct ehci_hcd *ehci)
  941. {
  942. unsigned frame, clock, now_uframe, mod;
  943. unsigned long flags;
  944. mod = ehci->periodic_size << 3;
  945. spin_lock_irqsave (&ehci->lock, flags);
  946. /*
  947.  * When running, scan from last scan point up to "now"
  948.  * else clean up by scanning everything that's left.
  949.  * Touches as few pages as possible:  cache-friendly.
  950.  * Don't scan ISO entries more than once, though.
  951.  */
  952. frame = ehci->next_uframe >> 3;
  953. if (HCD_IS_RUNNING (ehci->hcd.state))
  954. now_uframe = readl (&ehci->regs->frame_index);
  955. else
  956. now_uframe = (frame << 3) - 1;
  957. now_uframe %= mod;
  958. clock = now_uframe >> 3;
  959. for (;;) {
  960. union ehci_shadow q, *q_p;
  961. u32 type, *hw_p;
  962. unsigned uframes;
  963. restart:
  964. /* scan schedule to _before_ current frame index */
  965. if (frame == clock)
  966. uframes = now_uframe & 0x07;
  967. else
  968. uframes = 8;
  969. q_p = &ehci->pshadow [frame];
  970. hw_p = &ehci->periodic [frame];
  971. q.ptr = q_p->ptr;
  972. type = Q_NEXT_TYPE (*hw_p);
  973. /* scan each element in frame's queue for completions */
  974. while (q.ptr != 0) {
  975. int last;
  976. unsigned uf;
  977. union ehci_shadow temp;
  978. switch (type) {
  979. case Q_TYPE_QH:
  980. last = (q.qh->hw_next == EHCI_LIST_END);
  981. temp = q.qh->qh_next;
  982. type = Q_NEXT_TYPE (q.qh->hw_next);
  983. flags = intr_complete (ehci, frame,
  984. qh_get (q.qh), flags);
  985. qh_put (ehci, q.qh);
  986. q = temp;
  987. break;
  988. case Q_TYPE_FSTN:
  989. last = (q.fstn->hw_next == EHCI_LIST_END);
  990. /* for "save place" FSTNs, look at QH entries
  991.  * in the previous frame for completions.
  992.  */
  993. if (q.fstn->hw_prev != EHCI_LIST_END) {
  994. dbg ("ignoring completions from FSTNs");
  995. }
  996. type = Q_NEXT_TYPE (q.fstn->hw_next);
  997. q = q.fstn->fstn_next;
  998. break;
  999. case Q_TYPE_ITD:
  1000. last = (q.itd->hw_next == EHCI_LIST_END);
  1001. /* Unlink each (S)ITD we see, since the ISO
  1002.  * URB model forces constant rescheduling.
  1003.  * That complicates sharing uframes in ITDs,
  1004.  * and means we need to skip uframes the HC
  1005.  * hasn't yet processed.
  1006.  */
  1007. for (uf = 0; uf < uframes; uf++) {
  1008. if (q.itd->hw_transaction [uf] != 0) {
  1009. temp = q;
  1010. *q_p = q.itd->itd_next;
  1011. *hw_p = q.itd->hw_next;
  1012. type = Q_NEXT_TYPE (*hw_p);
  1013. /* might free q.itd ... */
  1014. flags = itd_complete (ehci,
  1015. temp.itd, uf, flags);
  1016. break;
  1017. }
  1018. }
  1019. /* we might skip this ITD's uframe ... */
  1020. if (uf == uframes) {
  1021. q_p = &q.itd->itd_next;
  1022. hw_p = &q.itd->hw_next;
  1023. type = Q_NEXT_TYPE (q.itd->hw_next);
  1024. }
  1025. q = *q_p;
  1026. break;
  1027. #ifdef have_split_iso
  1028. case Q_TYPE_SITD:
  1029. last = (q.sitd->hw_next == EHCI_LIST_END);
  1030. flags = sitd_complete (ehci, q.sitd, flags);
  1031. type = Q_NEXT_TYPE (q.sitd->hw_next);
  1032. // FIXME unlink SITD after split completes
  1033. q = q.sitd->sitd_next;
  1034. break;
  1035. #endif /* have_split_iso */
  1036. default:
  1037. dbg ("corrupt type %d frame %d shadow %p",
  1038. type, frame, q.ptr);
  1039. // BUG ();
  1040. last = 1;
  1041. q.ptr = 0;
  1042. }
  1043. /* did completion remove an interior q entry? */
  1044. if (unlikely (q.ptr == 0 && !last))
  1045. goto restart;
  1046. }
  1047. /* stop when we catch up to the HC */
  1048. // FIXME:  this assumes we won't get lapped when
  1049. // latencies climb; that should be rare, but...
  1050. // detect it, and just go all the way around.
  1051. // FLR might help detect this case, so long as latencies
  1052. // don't exceed periodic_size msec (default 1.024 sec).
  1053. // FIXME:  likewise assumes HC doesn't halt mid-scan
  1054. if (frame == clock) {
  1055. unsigned now;
  1056. if (!HCD_IS_RUNNING (ehci->hcd.state))
  1057. break;
  1058. ehci->next_uframe = now_uframe;
  1059. now = readl (&ehci->regs->frame_index) % mod;
  1060. if (now_uframe == now)
  1061. break;
  1062. /* rescan the rest of this frame, then ... */
  1063. now_uframe = now;
  1064. clock = now_uframe >> 3;
  1065. } else
  1066. frame = (frame + 1) % ehci->periodic_size;
  1067. spin_unlock_irqrestore (&ehci->lock, flags);
  1068. }