2 * Copyright (c) 2001-2004 by David Brownell
3 * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /* this file is part of ehci-hcd.c */
22 /*-------------------------------------------------------------------------*/
25 * EHCI scheduled transaction support: interrupt, iso, split iso
26 * These are called "periodic" transactions in the EHCI spec.
28 * Note that for interrupt transfers, the QH/QTD manipulation is shared
29 * with the "asynchronous" transaction support (control/bulk transfers).
30 * The only real difference is in how interrupt transfers are scheduled.
32 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
33 * It keeps track of every ITD (or SITD) that's linked, and holds enough
34 * pre-calculated schedule data to make appending to the queue be quick.
37 static int ehci_get_frame (struct usb_hcd *hcd);
39 /*-------------------------------------------------------------------------*/
42 * periodic_next_shadow - return "next" pointer on shadow list
43 * @periodic: host pointer to qh/itd/sitd
44 * @tag: hardware tag for type of this record
46 static union ehci_shadow *
47 periodic_next_shadow(struct ehci_hcd *ehci, union ehci_shadow *periodic,
50 switch (hc32_to_cpu(ehci, tag)) {
52 return &periodic->qh->qh_next;
54 return &periodic->fstn->fstn_next;
56 return &periodic->itd->itd_next;
59 return &periodic->sitd->sitd_next;
63 /* caller must hold ehci->lock */
64 static void periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
66 union ehci_shadow *prev_p = &ehci->pshadow[frame];
67 __hc32 *hw_p = &ehci->periodic[frame];
68 union ehci_shadow here = *prev_p;
70 /* find predecessor of "ptr"; hw and shadow lists are in sync */
71 while (here.ptr && here.ptr != ptr) {
72 prev_p = periodic_next_shadow(ehci, prev_p,
73 Q_NEXT_TYPE(ehci, *hw_p));
77 /* an interrupt entry (at list end) could have been shared */
81 /* update shadow and hardware lists ... the old "next" pointers
82 * from ptr may still be in use, the caller updates them.
84 *prev_p = *periodic_next_shadow(ehci, &here,
85 Q_NEXT_TYPE(ehci, *hw_p));
86 *hw_p = *here.hw_next;
89 /* how many of the uframe's 125 usecs are allocated? */
91 periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
93 __hc32 *hw_p = &ehci->periodic [frame];
94 union ehci_shadow *q = &ehci->pshadow [frame];
98 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
100 /* is it in the S-mask? */
101 if (q->qh->hw_info2 & cpu_to_hc32(ehci, 1 << uframe))
102 usecs += q->qh->usecs;
104 if (q->qh->hw_info2 & cpu_to_hc32(ehci,
106 usecs += q->qh->c_usecs;
107 hw_p = &q->qh->hw_next;
112 /* for "save place" FSTNs, count the relevant INTR
113 * bandwidth from the previous frame
115 if (q->fstn->hw_prev != EHCI_LIST_END(ehci)) {
116 ehci_dbg (ehci, "ignoring FSTN cost ...\n");
118 hw_p = &q->fstn->hw_next;
119 q = &q->fstn->fstn_next;
122 if (q->itd->hw_transaction[uframe])
123 usecs += q->itd->stream->usecs;
124 hw_p = &q->itd->hw_next;
125 q = &q->itd->itd_next;
128 /* is it in the S-mask? (count SPLIT, DATA) */
129 if (q->sitd->hw_uframe & cpu_to_hc32(ehci,
131 if (q->sitd->hw_fullspeed_ep &
132 cpu_to_hc32(ehci, 1<<31))
133 usecs += q->sitd->stream->usecs;
134 else /* worst case for OUT start-split */
135 usecs += HS_USECS_ISO (188);
138 /* ... C-mask? (count CSPLIT, DATA) */
139 if (q->sitd->hw_uframe &
140 cpu_to_hc32(ehci, 1 << (8 + uframe))) {
141 /* worst case for IN complete-split */
142 usecs += q->sitd->stream->c_usecs;
145 hw_p = &q->sitd->hw_next;
146 q = &q->sitd->sitd_next;
152 ehci_err (ehci, "uframe %d sched overrun: %d usecs\n",
153 frame * 8 + uframe, usecs);
158 /*-------------------------------------------------------------------------*/
160 static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
162 if (!dev1->tt || !dev2->tt)
164 if (dev1->tt != dev2->tt)
167 return dev1->ttport == dev2->ttport;
172 #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
174 /* Which uframe does the low/fullspeed transfer start in?
176 * The parameter is the mask of ssplits in "H-frame" terms
177 * and this returns the transfer start uframe in "B-frame" terms,
178 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
179 * will cause a transfer in "B-frame" uframe 0. "B-frames" lag
180 * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7.
182 static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask)
184 unsigned char smask = QH_SMASK & hc32_to_cpu(ehci, mask);
186 ehci_err(ehci, "invalid empty smask!\n");
187 /* uframe 7 can't have bw so this will indicate failure */
190 return ffs(smask) - 1;
193 static const unsigned char
194 max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
196 /* carryover low/fullspeed bandwidth that crosses uframe boundries */
197 static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
200 for (i=0; i<7; i++) {
201 if (max_tt_usecs[i] < tt_usecs[i]) {
202 tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i];
203 tt_usecs[i] = max_tt_usecs[i];
208 /* How many of the tt's periodic downstream 1000 usecs are allocated?
210 * While this measures the bandwidth in terms of usecs/uframe,
211 * the low/fullspeed bus has no notion of uframes, so any particular
212 * low/fullspeed transfer can "carry over" from one uframe to the next,
213 * since the TT just performs downstream transfers in sequence.
215 * For example two separate 100 usec transfers can start in the same uframe,
216 * and the second one would "carry over" 75 usecs into the next uframe.
220 struct ehci_hcd *ehci,
221 struct usb_device *dev,
223 unsigned short tt_usecs[8]
226 __hc32 *hw_p = &ehci->periodic [frame];
227 union ehci_shadow *q = &ehci->pshadow [frame];
230 memset(tt_usecs, 0, 16);
233 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
235 hw_p = &q->itd->hw_next;
236 q = &q->itd->itd_next;
239 if (same_tt(dev, q->qh->dev)) {
240 uf = tt_start_uframe(ehci, q->qh->hw_info2);
241 tt_usecs[uf] += q->qh->tt_usecs;
243 hw_p = &q->qh->hw_next;
247 if (same_tt(dev, q->sitd->urb->dev)) {
248 uf = tt_start_uframe(ehci, q->sitd->hw_uframe);
249 tt_usecs[uf] += q->sitd->stream->tt_usecs;
251 hw_p = &q->sitd->hw_next;
252 q = &q->sitd->sitd_next;
256 ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n",
258 hw_p = &q->fstn->hw_next;
259 q = &q->fstn->fstn_next;
263 carryover_tt_bandwidth(tt_usecs);
265 if (max_tt_usecs[7] < tt_usecs[7])
266 ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
267 frame, tt_usecs[7] - max_tt_usecs[7]);
271 * Return true if the device's tt's downstream bus is available for a
272 * periodic transfer of the specified length (usecs), starting at the
273 * specified frame/uframe. Note that (as summarized in section 11.19
274 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
277 * The uframe parameter is when the fullspeed/lowspeed transfer
278 * should be executed in "B-frame" terms, which is the same as the
279 * highspeed ssplit's uframe (which is in "H-frame" terms). For example
280 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
281 * See the EHCI spec sec 4.5 and fig 4.7.
283 * This checks if the full/lowspeed bus, at the specified starting uframe,
284 * has the specified bandwidth available, according to rules listed
285 * in USB 2.0 spec section 11.18.1 fig 11-60.
287 * This does not check if the transfer would exceed the max ssplit
288 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
289 * since proper scheduling limits ssplits to less than 16 per uframe.
291 static int tt_available (
292 struct ehci_hcd *ehci,
294 struct usb_device *dev,
300 if ((period == 0) || (uframe >= 7)) /* error */
303 for (; frame < ehci->periodic_size; frame += period) {
304 unsigned short tt_usecs[8];
306 periodic_tt_usecs (ehci, dev, frame, tt_usecs);
308 ehci_vdbg(ehci, "tt frame %d check %d usecs start uframe %d in"
309 " schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
310 frame, usecs, uframe,
311 tt_usecs[0], tt_usecs[1], tt_usecs[2], tt_usecs[3],
312 tt_usecs[4], tt_usecs[5], tt_usecs[6], tt_usecs[7]);
314 if (max_tt_usecs[uframe] <= tt_usecs[uframe]) {
315 ehci_vdbg(ehci, "frame %d uframe %d fully scheduled\n",
320 /* special case for isoc transfers larger than 125us:
321 * the first and each subsequent fully used uframe
322 * must be empty, so as to not illegally delay
323 * already scheduled transactions
326 int ufs = (usecs / 125) - 1;
328 for (i = uframe; i < (uframe + ufs) && i < 8; i++)
329 if (0 < tt_usecs[i]) {
331 "multi-uframe xfer can't fit "
332 "in frame %d uframe %d\n",
338 tt_usecs[uframe] += usecs;
340 carryover_tt_bandwidth(tt_usecs);
342 /* fail if the carryover pushed bw past the last uframe's limit */
343 if (max_tt_usecs[7] < tt_usecs[7]) {
345 "tt unavailable usecs %d frame %d uframe %d\n",
346 usecs, frame, uframe);
356 /* return true iff the device's transaction translator is available
357 * for a periodic transfer starting at the specified frame, using
358 * all the uframes in the mask.
360 static int tt_no_collision (
361 struct ehci_hcd *ehci,
363 struct usb_device *dev,
368 if (period == 0) /* error */
371 /* note bandwidth wastage: split never follows csplit
372 * (different dev or endpoint) until the next uframe.
373 * calling convention doesn't make that distinction.
375 for (; frame < ehci->periodic_size; frame += period) {
376 union ehci_shadow here;
379 here = ehci->pshadow [frame];
380 type = Q_NEXT_TYPE(ehci, ehci->periodic [frame]);
382 switch (hc32_to_cpu(ehci, type)) {
384 type = Q_NEXT_TYPE(ehci, here.itd->hw_next);
385 here = here.itd->itd_next;
388 if (same_tt (dev, here.qh->dev)) {
391 mask = hc32_to_cpu(ehci,
393 /* "knows" no gap is needed */
398 type = Q_NEXT_TYPE(ehci, here.qh->hw_next);
399 here = here.qh->qh_next;
402 if (same_tt (dev, here.sitd->urb->dev)) {
405 mask = hc32_to_cpu(ehci, here.sitd
407 /* FIXME assumes no gap for IN! */
412 type = Q_NEXT_TYPE(ehci, here.sitd->hw_next);
413 here = here.sitd->sitd_next;
418 "periodic frame %d bogus type %d\n",
422 /* collision or error */
431 #endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
433 /*-------------------------------------------------------------------------*/
435 static int enable_periodic (struct ehci_hcd *ehci)
440 if (ehci->periodic_sched++)
443 /* did clearing PSE did take effect yet?
444 * takes effect only at frame boundaries...
446 status = handshake_on_error_set_halt(ehci, &ehci->regs->status,
447 STS_PSS, 0, 9 * 125);
451 cmd = ehci_readl(ehci, &ehci->regs->command) | CMD_PSE;
452 ehci_writel(ehci, cmd, &ehci->regs->command);
453 /* posted write ... PSS happens later */
454 ehci_to_hcd(ehci)->state = HC_STATE_RUNNING;
456 /* make sure ehci_work scans these */
457 ehci->next_uframe = ehci_readl(ehci, &ehci->regs->frame_index)
458 % (ehci->periodic_size << 3);
462 static int disable_periodic (struct ehci_hcd *ehci)
467 if (--ehci->periodic_sched)
470 /* did setting PSE not take effect yet?
471 * takes effect only at frame boundaries...
473 status = handshake_on_error_set_halt(ehci, &ehci->regs->status,
474 STS_PSS, STS_PSS, 9 * 125);
478 cmd = ehci_readl(ehci, &ehci->regs->command) & ~CMD_PSE;
479 ehci_writel(ehci, cmd, &ehci->regs->command);
480 /* posted write ... */
482 ehci->next_uframe = -1;
486 /*-------------------------------------------------------------------------*/
488 /* periodic schedule slots have iso tds (normal or split) first, then a
489 * sparse tree for active interrupt transfers.
491 * this just links in a qh; caller guarantees uframe masks are set right.
492 * no FSTN support (yet; ehci 0.96+)
494 static int qh_link_periodic (struct ehci_hcd *ehci, struct ehci_qh *qh)
497 unsigned period = qh->period;
499 dev_dbg (&qh->dev->dev,
500 "link qh%d-%04x/%p start %d [%d/%d us]\n",
501 period, hc32_to_cpup(ehci, &qh->hw_info2) & (QH_CMASK | QH_SMASK),
502 qh, qh->start, qh->usecs, qh->c_usecs);
504 /* high bandwidth, or otherwise every microframe */
508 for (i = qh->start; i < ehci->periodic_size; i += period) {
509 union ehci_shadow *prev = &ehci->pshadow[i];
510 __hc32 *hw_p = &ehci->periodic[i];
511 union ehci_shadow here = *prev;
514 /* skip the iso nodes at list head */
516 type = Q_NEXT_TYPE(ehci, *hw_p);
517 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
519 prev = periodic_next_shadow(ehci, prev, type);
520 hw_p = &here.qh->hw_next;
524 /* sorting each branch by period (slow-->fast)
525 * enables sharing interior tree nodes
527 while (here.ptr && qh != here.qh) {
528 if (qh->period > here.qh->period)
530 prev = &here.qh->qh_next;
531 hw_p = &here.qh->hw_next;
534 /* link in this qh, unless some earlier pass did that */
541 *hw_p = QH_NEXT (ehci, qh->qh_dma);
544 qh->qh_state = QH_STATE_LINKED;
547 /* update per-qh bandwidth for usbfs */
548 ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period
549 ? ((qh->usecs + qh->c_usecs) / qh->period)
552 /* maybe enable periodic schedule processing */
553 return enable_periodic(ehci);
556 static int qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
562 // IF this isn't high speed
563 // and this qh is active in the current uframe
564 // (and overlay token SplitXstate is false?)
566 // qh->hw_info1 |= __constant_cpu_to_hc32(1 << 7 /* "ignore" */);
568 /* high bandwidth, or otherwise part of every microframe */
569 if ((period = qh->period) == 0)
572 for (i = qh->start; i < ehci->periodic_size; i += period)
573 periodic_unlink (ehci, i, qh);
575 /* update per-qh bandwidth for usbfs */
576 ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period
577 ? ((qh->usecs + qh->c_usecs) / qh->period)
580 dev_dbg (&qh->dev->dev,
581 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
583 hc32_to_cpup(ehci, &qh->hw_info2) & (QH_CMASK | QH_SMASK),
584 qh, qh->start, qh->usecs, qh->c_usecs);
586 /* qh->qh_next still "live" to HC */
587 qh->qh_state = QH_STATE_UNLINK;
588 qh->qh_next.ptr = NULL;
591 /* maybe turn off periodic schedule */
592 return disable_periodic(ehci);
595 static void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
599 qh_unlink_periodic (ehci, qh);
601 /* simple/paranoid: always delay, expecting the HC needs to read
602 * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and
603 * expect khubd to clean up after any CSPLITs we won't issue.
604 * active high speed queues may need bigger delays...
606 if (list_empty (&qh->qtd_list)
607 || (cpu_to_hc32(ehci, QH_CMASK)
608 & qh->hw_info2) != 0)
611 wait = 55; /* worst case: 3 * 1024 */
614 qh->qh_state = QH_STATE_IDLE;
615 qh->hw_next = EHCI_LIST_END(ehci);
619 /*-------------------------------------------------------------------------*/
621 static int check_period (
622 struct ehci_hcd *ehci,
630 /* complete split running into next frame?
631 * given FSTN support, we could sometimes check...
637 * 80% periodic == 100 usec/uframe available
638 * convert "usecs we need" to "max already claimed"
642 /* we "know" 2 and 4 uframe intervals were rejected; so
643 * for period 0, check _every_ microframe in the schedule.
645 if (unlikely (period == 0)) {
647 for (uframe = 0; uframe < 7; uframe++) {
648 claimed = periodic_usecs (ehci, frame, uframe);
652 } while ((frame += 1) < ehci->periodic_size);
654 /* just check the specified uframe, at that period */
657 claimed = periodic_usecs (ehci, frame, uframe);
660 } while ((frame += period) < ehci->periodic_size);
667 static int check_intr_schedule (
668 struct ehci_hcd *ehci,
671 const struct ehci_qh *qh,
675 int retval = -ENOSPC;
678 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
681 if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
689 #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
690 if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
694 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
695 for (i=uframe+1; i<8 && i<uframe+4; i++)
696 if (!check_period (ehci, frame, i,
697 qh->period, qh->c_usecs))
704 *c_maskp = cpu_to_hc32(ehci, mask << 8);
707 /* Make sure this tt's buffer is also available for CSPLITs.
708 * We pessimize a bit; probably the typical full speed case
709 * doesn't need the second CSPLIT.
711 * NOTE: both SPLIT and CSPLIT could be checked in just
714 mask = 0x03 << (uframe + qh->gap_uf);
715 *c_maskp = cpu_to_hc32(ehci, mask << 8);
718 if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
719 if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
720 qh->period, qh->c_usecs))
722 if (!check_period (ehci, frame, uframe + qh->gap_uf,
723 qh->period, qh->c_usecs))
732 /* "first fit" scheduling policy used the first time through,
733 * or when the previous schedule slot can't be re-used.
735 static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
740 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
742 qh_refresh(ehci, qh);
743 qh->hw_next = EHCI_LIST_END(ehci);
746 /* reuse the previous schedule slots, if we can */
747 if (frame < qh->period) {
748 uframe = ffs(hc32_to_cpup(ehci, &qh->hw_info2) & QH_SMASK);
749 status = check_intr_schedule (ehci, frame, --uframe,
757 /* else scan the schedule to find a group of slots such that all
758 * uframes have enough periodic bandwidth available.
761 /* "normal" case, uframing flexible except with splits */
763 frame = qh->period - 1;
765 for (uframe = 0; uframe < 8; uframe++) {
766 status = check_intr_schedule (ehci,
772 } while (status && frame--);
774 /* qh->period == 0 means every uframe */
777 status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
783 /* reset S-frame and (maybe) C-frame masks */
784 qh->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
785 qh->hw_info2 |= qh->period
786 ? cpu_to_hc32(ehci, 1 << uframe)
787 : cpu_to_hc32(ehci, QH_SMASK);
788 qh->hw_info2 |= c_mask;
790 ehci_dbg (ehci, "reused qh %p schedule\n", qh);
792 /* stuff into the periodic schedule */
793 status = qh_link_periodic (ehci, qh);
798 static int intr_submit (
799 struct ehci_hcd *ehci,
801 struct list_head *qtd_list,
808 struct list_head empty;
810 /* get endpoint and transfer/schedule data */
811 epnum = urb->ep->desc.bEndpointAddress;
813 spin_lock_irqsave (&ehci->lock, flags);
815 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
816 &ehci_to_hcd(ehci)->flags))) {
818 goto done_not_linked;
820 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
821 if (unlikely(status))
822 goto done_not_linked;
824 /* get qh and force any scheduling errors */
825 INIT_LIST_HEAD (&empty);
826 qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
831 if (qh->qh_state == QH_STATE_IDLE) {
832 if ((status = qh_schedule (ehci, qh)) != 0)
836 /* then queue the urb's tds to the qh */
837 qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
840 /* ... update usbfs periodic stats */
841 ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
844 if (unlikely(status))
845 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
847 spin_unlock_irqrestore (&ehci->lock, flags);
849 qtd_list_free (ehci, urb, qtd_list);
854 /*-------------------------------------------------------------------------*/
856 /* ehci_iso_stream ops work with both ITD and SITD */
858 static struct ehci_iso_stream *
859 iso_stream_alloc (gfp_t mem_flags)
861 struct ehci_iso_stream *stream;
863 stream = kzalloc(sizeof *stream, mem_flags);
864 if (likely (stream != NULL)) {
865 INIT_LIST_HEAD(&stream->td_list);
866 INIT_LIST_HEAD(&stream->free_list);
867 stream->next_uframe = -1;
868 stream->refcount = 1;
875 struct ehci_hcd *ehci,
876 struct ehci_iso_stream *stream,
877 struct usb_device *dev,
882 static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
885 unsigned epnum, maxp;
890 * this might be a "high bandwidth" highspeed endpoint,
891 * as encoded in the ep descriptor's wMaxPacket field
893 epnum = usb_pipeendpoint (pipe);
894 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
895 maxp = usb_maxpacket(dev, pipe, !is_input);
902 /* knows about ITD vs SITD */
903 if (dev->speed == USB_SPEED_HIGH) {
904 unsigned multi = hb_mult(maxp);
906 stream->highspeed = 1;
908 maxp = max_packet(maxp);
912 stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
913 stream->buf1 = cpu_to_hc32(ehci, buf1);
914 stream->buf2 = cpu_to_hc32(ehci, multi);
916 /* usbfs wants to report the average usecs per frame tied up
917 * when transfers on this endpoint are scheduled ...
919 stream->usecs = HS_USECS_ISO (maxp);
920 bandwidth = stream->usecs * 8;
921 bandwidth /= 1 << (interval - 1);
928 addr = dev->ttport << 24;
929 if (!ehci_is_TDI(ehci)
931 ehci_to_hcd(ehci)->self.root_hub))
932 addr |= dev->tt->hub->devnum << 16;
935 stream->usecs = HS_USECS_ISO (maxp);
936 think_time = dev->tt ? dev->tt->think_time : 0;
937 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
938 dev->speed, is_input, 1, maxp));
939 hs_transfers = max (1u, (maxp + 187) / 188);
944 stream->c_usecs = stream->usecs;
945 stream->usecs = HS_USECS_ISO (1);
946 stream->raw_mask = 1;
948 /* c-mask as specified in USB 2.0 11.18.4 3.c */
949 tmp = (1 << (hs_transfers + 2)) - 1;
950 stream->raw_mask |= tmp << (8 + 2);
952 stream->raw_mask = smask_out [hs_transfers - 1];
953 bandwidth = stream->usecs + stream->c_usecs;
954 bandwidth /= 1 << (interval + 2);
956 /* stream->splits gets created from raw_mask later */
957 stream->address = cpu_to_hc32(ehci, addr);
959 stream->bandwidth = bandwidth;
963 stream->bEndpointAddress = is_input | epnum;
964 stream->interval = interval;
969 iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
973 /* free whenever just a dev->ep reference remains.
974 * not like a QH -- no persistent state (toggle, halt)
976 if (stream->refcount == 1) {
979 // BUG_ON (!list_empty(&stream->td_list));
981 while (!list_empty (&stream->free_list)) {
982 struct list_head *entry;
984 entry = stream->free_list.next;
987 /* knows about ITD vs SITD */
988 if (stream->highspeed) {
989 struct ehci_itd *itd;
991 itd = list_entry (entry, struct ehci_itd,
993 dma_pool_free (ehci->itd_pool, itd,
996 struct ehci_sitd *sitd;
998 sitd = list_entry (entry, struct ehci_sitd,
1000 dma_pool_free (ehci->sitd_pool, sitd,
1005 is_in = (stream->bEndpointAddress & USB_DIR_IN) ? 0x10 : 0;
1006 stream->bEndpointAddress &= 0x0f;
1007 stream->ep->hcpriv = NULL;
1009 if (stream->rescheduled) {
1010 ehci_info (ehci, "ep%d%s-iso rescheduled "
1011 "%lu times in %lu seconds\n",
1012 stream->bEndpointAddress, is_in ? "in" : "out",
1013 stream->rescheduled,
1014 ((jiffies - stream->start)/HZ)
1022 static inline struct ehci_iso_stream *
1023 iso_stream_get (struct ehci_iso_stream *stream)
1025 if (likely (stream != NULL))
1030 static struct ehci_iso_stream *
1031 iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1034 struct ehci_iso_stream *stream;
1035 struct usb_host_endpoint *ep;
1036 unsigned long flags;
1038 epnum = usb_pipeendpoint (urb->pipe);
1039 if (usb_pipein(urb->pipe))
1040 ep = urb->dev->ep_in[epnum];
1042 ep = urb->dev->ep_out[epnum];
1044 spin_lock_irqsave (&ehci->lock, flags);
1045 stream = ep->hcpriv;
1047 if (unlikely (stream == NULL)) {
1048 stream = iso_stream_alloc(GFP_ATOMIC);
1049 if (likely (stream != NULL)) {
1050 /* dev->ep owns the initial refcount */
1051 ep->hcpriv = stream;
1053 iso_stream_init(ehci, stream, urb->dev, urb->pipe,
1057 /* if dev->ep [epnum] is a QH, info1.maxpacket is nonzero */
1058 } else if (unlikely (stream->hw_info1 != 0)) {
1059 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1060 urb->dev->devpath, epnum,
1061 usb_pipein(urb->pipe) ? "in" : "out");
1065 /* caller guarantees an eventual matching iso_stream_put */
1066 stream = iso_stream_get (stream);
1068 spin_unlock_irqrestore (&ehci->lock, flags);
1072 /*-------------------------------------------------------------------------*/
1074 /* ehci_iso_sched ops can be ITD-only or SITD-only */
1076 static struct ehci_iso_sched *
1077 iso_sched_alloc (unsigned packets, gfp_t mem_flags)
1079 struct ehci_iso_sched *iso_sched;
1080 int size = sizeof *iso_sched;
1082 size += packets * sizeof (struct ehci_iso_packet);
1083 iso_sched = kzalloc(size, mem_flags);
1084 if (likely (iso_sched != NULL)) {
1085 INIT_LIST_HEAD (&iso_sched->td_list);
1092 struct ehci_hcd *ehci,
1093 struct ehci_iso_sched *iso_sched,
1094 struct ehci_iso_stream *stream,
1099 dma_addr_t dma = urb->transfer_dma;
1101 /* how many uframes are needed for these transfers */
1102 iso_sched->span = urb->number_of_packets * stream->interval;
1104 /* figure out per-uframe itd fields that we'll need later
1105 * when we fit new itds into the schedule.
1107 for (i = 0; i < urb->number_of_packets; i++) {
1108 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1113 length = urb->iso_frame_desc [i].length;
1114 buf = dma + urb->iso_frame_desc [i].offset;
1116 trans = EHCI_ISOC_ACTIVE;
1117 trans |= buf & 0x0fff;
1118 if (unlikely (((i + 1) == urb->number_of_packets))
1119 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1120 trans |= EHCI_ITD_IOC;
1121 trans |= length << 16;
1122 uframe->transaction = cpu_to_hc32(ehci, trans);
1124 /* might need to cross a buffer page within a uframe */
1125 uframe->bufp = (buf & ~(u64)0x0fff);
1127 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1134 struct ehci_iso_stream *stream,
1135 struct ehci_iso_sched *iso_sched
1140 // caller must hold ehci->lock!
1141 list_splice (&iso_sched->td_list, &stream->free_list);
1146 itd_urb_transaction (
1147 struct ehci_iso_stream *stream,
1148 struct ehci_hcd *ehci,
1153 struct ehci_itd *itd;
1157 struct ehci_iso_sched *sched;
1158 unsigned long flags;
1160 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1161 if (unlikely (sched == NULL))
1164 itd_sched_init(ehci, sched, stream, urb);
1166 if (urb->interval < 8)
1167 num_itds = 1 + (sched->span + 7) / 8;
1169 num_itds = urb->number_of_packets;
1171 /* allocate/init ITDs */
1172 spin_lock_irqsave (&ehci->lock, flags);
1173 for (i = 0; i < num_itds; i++) {
1175 /* free_list.next might be cache-hot ... but maybe
1176 * the HC caches it too. avoid that issue for now.
1179 /* prefer previously-allocated itds */
1180 if (likely (!list_empty(&stream->free_list))) {
1181 itd = list_entry (stream->free_list.prev,
1182 struct ehci_itd, itd_list);
1183 list_del (&itd->itd_list);
1184 itd_dma = itd->itd_dma;
1186 spin_unlock_irqrestore (&ehci->lock, flags);
1187 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1189 spin_lock_irqsave (&ehci->lock, flags);
1191 iso_sched_free(stream, sched);
1192 spin_unlock_irqrestore(&ehci->lock, flags);
1197 memset (itd, 0, sizeof *itd);
1198 itd->itd_dma = itd_dma;
1199 list_add (&itd->itd_list, &sched->td_list);
1201 spin_unlock_irqrestore (&ehci->lock, flags);
1203 /* temporarily store schedule info in hcpriv */
1204 urb->hcpriv = sched;
1205 urb->error_count = 0;
1209 /*-------------------------------------------------------------------------*/
1213 struct ehci_hcd *ehci,
1222 /* can't commit more than 80% periodic == 100 usec */
1223 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
1227 /* we know urb->interval is 2^N uframes */
1229 } while (uframe < mod);
1235 struct ehci_hcd *ehci,
1237 struct ehci_iso_stream *stream,
1239 struct ehci_iso_sched *sched,
1246 mask = stream->raw_mask << (uframe & 7);
1248 /* for IN, don't wrap CSPLIT into the next frame */
1252 /* this multi-pass logic is simple, but performance may
1253 * suffer when the schedule data isn't cached.
1256 /* check bandwidth */
1257 uframe %= period_uframes;
1261 frame = uframe >> 3;
1264 #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1265 /* The tt's fullspeed bus bandwidth must be available.
1266 * tt_available scheduling guarantees 10+% for control/bulk.
1268 if (!tt_available (ehci, period_uframes << 3,
1269 stream->udev, frame, uf, stream->tt_usecs))
1272 /* tt must be idle for start(s), any gap, and csplit.
1273 * assume scheduling slop leaves 10+% for control/bulk.
1275 if (!tt_no_collision (ehci, period_uframes << 3,
1276 stream->udev, frame, mask))
1280 /* check starts (OUT uses more than one) */
1281 max_used = 100 - stream->usecs;
1282 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
1283 if (periodic_usecs (ehci, frame, uf) > max_used)
1287 /* for IN, check CSPLIT */
1288 if (stream->c_usecs) {
1290 max_used = 100 - stream->c_usecs;
1294 if ((stream->raw_mask & tmp) == 0)
1296 if (periodic_usecs (ehci, frame, uf)
1302 /* we know urb->interval is 2^N uframes */
1303 uframe += period_uframes;
1304 } while (uframe < mod);
1306 stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
1311 * This scheduler plans almost as far into the future as it has actual
1312 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1313 * "as small as possible" to be cache-friendlier.) That limits the size
1314 * transfers you can stream reliably; avoid more than 64 msec per urb.
1315 * Also avoid queue depths of less than ehci's worst irq latency (affected
1316 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1317 * and other factors); or more than about 230 msec total (for portability,
1318 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1321 #define SCHEDULE_SLOP 10 /* frames */
1324 iso_stream_schedule (
1325 struct ehci_hcd *ehci,
1327 struct ehci_iso_stream *stream
1330 u32 now, start, max, period;
1332 unsigned mod = ehci->periodic_size << 3;
1333 struct ehci_iso_sched *sched = urb->hcpriv;
1335 if (sched->span > (mod - 8 * SCHEDULE_SLOP)) {
1336 ehci_dbg (ehci, "iso request %p too long\n", urb);
1341 if ((stream->depth + sched->span) > mod) {
1342 ehci_dbg (ehci, "request %p would overflow (%d+%d>%d)\n",
1343 urb, stream->depth, sched->span, mod);
1348 now = ehci_readl(ehci, &ehci->regs->frame_index) % mod;
1350 /* when's the last uframe this urb could start? */
1353 /* Typical case: reuse current schedule, stream is still active.
1354 * Hopefully there are no gaps from the host falling behind
1355 * (irq delays etc), but if there are we'll take the next
1356 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
1358 if (likely (!list_empty (&stream->td_list))) {
1359 start = stream->next_uframe;
1363 /* Fell behind (by up to twice the slop amount)? */
1364 if (start >= max - 2 * 8 * SCHEDULE_SLOP)
1365 start += stream->interval * DIV_ROUND_UP(
1366 max - start, stream->interval) - mod;
1368 /* Tried to schedule too far into the future? */
1369 if (unlikely((start + sched->span) >= max)) {
1376 /* need to schedule; when's the next (u)frame we could start?
1377 * this is bigger than ehci->i_thresh allows; scheduling itself
1378 * isn't free, the slop should handle reasonably slow cpus. it
1379 * can also help high bandwidth if the dma and irq loads don't
1380 * jump until after the queue is primed.
1382 start = SCHEDULE_SLOP * 8 + (now & ~0x07);
1384 stream->next_uframe = start;
1386 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
1388 period = urb->interval;
1389 if (!stream->highspeed)
1392 /* find a uframe slot with enough bandwidth */
1393 for (; start < (stream->next_uframe + period); start++) {
1396 /* check schedule: enough space? */
1397 if (stream->highspeed)
1398 enough_space = itd_slot_ok (ehci, mod, start,
1399 stream->usecs, period);
1401 if ((start % 8) >= 6)
1403 enough_space = sitd_slot_ok (ehci, mod, stream,
1404 start, sched, period);
1407 /* schedule it here if there's enough bandwidth */
1409 stream->next_uframe = start % mod;
1414 /* no room in the schedule */
1415 ehci_dbg (ehci, "iso %ssched full %p (now %d max %d)\n",
1416 list_empty (&stream->td_list) ? "" : "re",
1421 iso_sched_free (stream, sched);
1426 /* report high speed start in uframes; full speed, in frames */
1427 urb->start_frame = stream->next_uframe;
1428 if (!stream->highspeed)
1429 urb->start_frame >>= 3;
1433 /*-------------------------------------------------------------------------*/
1436 itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1437 struct ehci_itd *itd)
1441 /* it's been recently zeroed */
1442 itd->hw_next = EHCI_LIST_END(ehci);
1443 itd->hw_bufp [0] = stream->buf0;
1444 itd->hw_bufp [1] = stream->buf1;
1445 itd->hw_bufp [2] = stream->buf2;
1447 for (i = 0; i < 8; i++)
1450 /* All other fields are filled when scheduling */
1455 struct ehci_hcd *ehci,
1456 struct ehci_itd *itd,
1457 struct ehci_iso_sched *iso_sched,
1462 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1463 unsigned pg = itd->pg;
1465 // BUG_ON (pg == 6 && uf->cross);
1468 itd->index [uframe] = index;
1470 itd->hw_transaction[uframe] = uf->transaction;
1471 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1472 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1473 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
1475 /* iso_frame_desc[].offset must be strictly increasing */
1476 if (unlikely (uf->cross)) {
1477 u64 bufp = uf->bufp + 4096;
1480 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1481 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
1486 itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1488 /* always prepend ITD/SITD ... only QH tree is order-sensitive */
1489 itd->itd_next = ehci->pshadow [frame];
1490 itd->hw_next = ehci->periodic [frame];
1491 ehci->pshadow [frame].itd = itd;
1494 ehci->periodic[frame] = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
1497 /* fit urb's itds into the selected schedule slot; activate as needed */
1500 struct ehci_hcd *ehci,
1503 struct ehci_iso_stream *stream
1507 unsigned next_uframe, uframe, frame;
1508 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1509 struct ehci_itd *itd;
1511 next_uframe = stream->next_uframe % mod;
1513 if (unlikely (list_empty(&stream->td_list))) {
1514 ehci_to_hcd(ehci)->self.bandwidth_allocated
1515 += stream->bandwidth;
1517 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1518 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1519 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1521 next_uframe >> 3, next_uframe & 0x7);
1522 stream->start = jiffies;
1524 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1526 /* fill iTDs uframe by uframe */
1527 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1529 /* ASSERT: we have all necessary itds */
1530 // BUG_ON (list_empty (&iso_sched->td_list));
1532 /* ASSERT: no itds for this endpoint in this uframe */
1534 itd = list_entry (iso_sched->td_list.next,
1535 struct ehci_itd, itd_list);
1536 list_move_tail (&itd->itd_list, &stream->td_list);
1537 itd->stream = iso_stream_get (stream);
1538 itd->urb = usb_get_urb (urb);
1539 itd_init (ehci, stream, itd);
1542 uframe = next_uframe & 0x07;
1543 frame = next_uframe >> 3;
1545 itd_patch(ehci, itd, iso_sched, packet, uframe);
1547 next_uframe += stream->interval;
1548 stream->depth += stream->interval;
1552 /* link completed itds into the schedule */
1553 if (((next_uframe >> 3) != frame)
1554 || packet == urb->number_of_packets) {
1555 itd_link (ehci, frame % ehci->periodic_size, itd);
1559 stream->next_uframe = next_uframe;
1561 /* don't need that schedule data any more */
1562 iso_sched_free (stream, iso_sched);
1565 timer_action (ehci, TIMER_IO_WATCHDOG);
1566 return enable_periodic(ehci);
1569 #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1571 /* Process and recycle a completed ITD. Return true iff its urb completed,
1572 * and hence its completion callback probably added things to the hardware
1575 * Note that we carefully avoid recycling this descriptor until after any
1576 * completion callback runs, so that it won't be reused quickly. That is,
1577 * assuming (a) no more than two urbs per frame on this endpoint, and also
1578 * (b) only this endpoint's completions submit URBs. It seems some silicon
1579 * corrupts things if you reuse completed descriptors very quickly...
1583 struct ehci_hcd *ehci,
1584 struct ehci_itd *itd
1586 struct urb *urb = itd->urb;
1587 struct usb_iso_packet_descriptor *desc;
1591 struct ehci_iso_stream *stream = itd->stream;
1592 struct usb_device *dev;
1593 unsigned retval = false;
1595 /* for each uframe with a packet */
1596 for (uframe = 0; uframe < 8; uframe++) {
1597 if (likely (itd->index[uframe] == -1))
1599 urb_index = itd->index[uframe];
1600 desc = &urb->iso_frame_desc [urb_index];
1602 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
1603 itd->hw_transaction [uframe] = 0;
1604 stream->depth -= stream->interval;
1606 /* report transfer status */
1607 if (unlikely (t & ISO_ERRS)) {
1609 if (t & EHCI_ISOC_BUF_ERR)
1610 desc->status = usb_pipein (urb->pipe)
1611 ? -ENOSR /* hc couldn't read */
1612 : -ECOMM; /* hc couldn't write */
1613 else if (t & EHCI_ISOC_BABBLE)
1614 desc->status = -EOVERFLOW;
1615 else /* (t & EHCI_ISOC_XACTERR) */
1616 desc->status = -EPROTO;
1618 /* HC need not update length with this error */
1619 if (!(t & EHCI_ISOC_BABBLE))
1620 desc->actual_length = EHCI_ITD_LENGTH (t);
1621 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1623 desc->actual_length = EHCI_ITD_LENGTH (t);
1625 /* URB was too late */
1626 desc->status = -EXDEV;
1630 /* handle completion now? */
1631 if (likely ((urb_index + 1) != urb->number_of_packets))
1634 /* ASSERT: it's really the last itd for this urb
1635 list_for_each_entry (itd, &stream->td_list, itd_list)
1636 BUG_ON (itd->urb == urb);
1639 /* give urb back to the driver; completion often (re)submits */
1641 ehci_urb_done(ehci, urb, 0);
1644 (void) disable_periodic(ehci);
1645 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
1647 if (unlikely (list_empty (&stream->td_list))) {
1648 ehci_to_hcd(ehci)->self.bandwidth_allocated
1649 -= stream->bandwidth;
1651 "deschedule devp %s ep%d%s-iso\n",
1652 dev->devpath, stream->bEndpointAddress & 0x0f,
1653 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1655 iso_stream_put (ehci, stream);
1656 /* OK to recycle this ITD now that its completion callback ran. */
1661 list_move(&itd->itd_list, &stream->free_list);
1662 iso_stream_put(ehci, stream);
1667 /*-------------------------------------------------------------------------*/
1669 static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
1672 int status = -EINVAL;
1673 unsigned long flags;
1674 struct ehci_iso_stream *stream;
1676 /* Get iso_stream head */
1677 stream = iso_stream_find (ehci, urb);
1678 if (unlikely (stream == NULL)) {
1679 ehci_dbg (ehci, "can't get iso stream\n");
1682 if (unlikely (urb->interval != stream->interval)) {
1683 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1684 stream->interval, urb->interval);
1688 #ifdef EHCI_URB_TRACE
1690 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
1691 __func__, urb->dev->devpath, urb,
1692 usb_pipeendpoint (urb->pipe),
1693 usb_pipein (urb->pipe) ? "in" : "out",
1694 urb->transfer_buffer_length,
1695 urb->number_of_packets, urb->interval,
1699 /* allocate ITDs w/o locking anything */
1700 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1701 if (unlikely (status < 0)) {
1702 ehci_dbg (ehci, "can't init itds\n");
1706 /* schedule ... need to lock */
1707 spin_lock_irqsave (&ehci->lock, flags);
1708 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
1709 &ehci_to_hcd(ehci)->flags))) {
1710 status = -ESHUTDOWN;
1711 goto done_not_linked;
1713 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1714 if (unlikely(status))
1715 goto done_not_linked;
1716 status = iso_stream_schedule(ehci, urb, stream);
1717 if (likely (status == 0))
1718 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
1720 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
1722 spin_unlock_irqrestore (&ehci->lock, flags);
1725 if (unlikely (status < 0))
1726 iso_stream_put (ehci, stream);
1730 /*-------------------------------------------------------------------------*/
1733 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1734 * TTs in USB 2.0 hubs. These need microframe scheduling.
1739 struct ehci_hcd *ehci,
1740 struct ehci_iso_sched *iso_sched,
1741 struct ehci_iso_stream *stream,
1746 dma_addr_t dma = urb->transfer_dma;
1748 /* how many frames are needed for these transfers */
1749 iso_sched->span = urb->number_of_packets * stream->interval;
1751 /* figure out per-frame sitd fields that we'll need later
1752 * when we fit new sitds into the schedule.
1754 for (i = 0; i < urb->number_of_packets; i++) {
1755 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1760 length = urb->iso_frame_desc [i].length & 0x03ff;
1761 buf = dma + urb->iso_frame_desc [i].offset;
1763 trans = SITD_STS_ACTIVE;
1764 if (((i + 1) == urb->number_of_packets)
1765 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1767 trans |= length << 16;
1768 packet->transaction = cpu_to_hc32(ehci, trans);
1770 /* might need to cross a buffer page within a td */
1772 packet->buf1 = (buf + length) & ~0x0fff;
1773 if (packet->buf1 != (buf & ~(u64)0x0fff))
1776 /* OUT uses multiple start-splits */
1777 if (stream->bEndpointAddress & USB_DIR_IN)
1779 length = (length + 187) / 188;
1780 if (length > 1) /* BEGIN vs ALL */
1782 packet->buf1 |= length;
1787 sitd_urb_transaction (
1788 struct ehci_iso_stream *stream,
1789 struct ehci_hcd *ehci,
1794 struct ehci_sitd *sitd;
1795 dma_addr_t sitd_dma;
1797 struct ehci_iso_sched *iso_sched;
1798 unsigned long flags;
1800 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1801 if (iso_sched == NULL)
1804 sitd_sched_init(ehci, iso_sched, stream, urb);
1806 /* allocate/init sITDs */
1807 spin_lock_irqsave (&ehci->lock, flags);
1808 for (i = 0; i < urb->number_of_packets; i++) {
1810 /* NOTE: for now, we don't try to handle wraparound cases
1811 * for IN (using sitd->hw_backpointer, like a FSTN), which
1812 * means we never need two sitds for full speed packets.
1815 /* free_list.next might be cache-hot ... but maybe
1816 * the HC caches it too. avoid that issue for now.
1819 /* prefer previously-allocated sitds */
1820 if (!list_empty(&stream->free_list)) {
1821 sitd = list_entry (stream->free_list.prev,
1822 struct ehci_sitd, sitd_list);
1823 list_del (&sitd->sitd_list);
1824 sitd_dma = sitd->sitd_dma;
1826 spin_unlock_irqrestore (&ehci->lock, flags);
1827 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1829 spin_lock_irqsave (&ehci->lock, flags);
1831 iso_sched_free(stream, iso_sched);
1832 spin_unlock_irqrestore(&ehci->lock, flags);
1837 memset (sitd, 0, sizeof *sitd);
1838 sitd->sitd_dma = sitd_dma;
1839 list_add (&sitd->sitd_list, &iso_sched->td_list);
1842 /* temporarily store schedule info in hcpriv */
1843 urb->hcpriv = iso_sched;
1844 urb->error_count = 0;
1846 spin_unlock_irqrestore (&ehci->lock, flags);
1850 /*-------------------------------------------------------------------------*/
1854 struct ehci_hcd *ehci,
1855 struct ehci_iso_stream *stream,
1856 struct ehci_sitd *sitd,
1857 struct ehci_iso_sched *iso_sched,
1861 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1862 u64 bufp = uf->bufp;
1864 sitd->hw_next = EHCI_LIST_END(ehci);
1865 sitd->hw_fullspeed_ep = stream->address;
1866 sitd->hw_uframe = stream->splits;
1867 sitd->hw_results = uf->transaction;
1868 sitd->hw_backpointer = EHCI_LIST_END(ehci);
1871 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
1872 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
1874 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
1877 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
1878 sitd->index = index;
1882 sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
1884 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1885 sitd->sitd_next = ehci->pshadow [frame];
1886 sitd->hw_next = ehci->periodic [frame];
1887 ehci->pshadow [frame].sitd = sitd;
1888 sitd->frame = frame;
1890 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
1893 /* fit urb's sitds into the selected schedule slot; activate as needed */
1896 struct ehci_hcd *ehci,
1899 struct ehci_iso_stream *stream
1903 unsigned next_uframe;
1904 struct ehci_iso_sched *sched = urb->hcpriv;
1905 struct ehci_sitd *sitd;
1907 next_uframe = stream->next_uframe;
1909 if (list_empty(&stream->td_list)) {
1910 /* usbfs ignores TT bandwidth */
1911 ehci_to_hcd(ehci)->self.bandwidth_allocated
1912 += stream->bandwidth;
1914 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
1915 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1916 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1917 (next_uframe >> 3) % ehci->periodic_size,
1918 stream->interval, hc32_to_cpu(ehci, stream->splits));
1919 stream->start = jiffies;
1921 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1923 /* fill sITDs frame by frame */
1924 for (packet = 0, sitd = NULL;
1925 packet < urb->number_of_packets;
1928 /* ASSERT: we have all necessary sitds */
1929 BUG_ON (list_empty (&sched->td_list));
1931 /* ASSERT: no itds for this endpoint in this frame */
1933 sitd = list_entry (sched->td_list.next,
1934 struct ehci_sitd, sitd_list);
1935 list_move_tail (&sitd->sitd_list, &stream->td_list);
1936 sitd->stream = iso_stream_get (stream);
1937 sitd->urb = usb_get_urb (urb);
1939 sitd_patch(ehci, stream, sitd, sched, packet);
1940 sitd_link (ehci, (next_uframe >> 3) % ehci->periodic_size,
1943 next_uframe += stream->interval << 3;
1944 stream->depth += stream->interval << 3;
1946 stream->next_uframe = next_uframe % mod;
1948 /* don't need that schedule data any more */
1949 iso_sched_free (stream, sched);
1952 timer_action (ehci, TIMER_IO_WATCHDOG);
1953 return enable_periodic(ehci);
1956 /*-------------------------------------------------------------------------*/
1958 #define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
1959 | SITD_STS_XACT | SITD_STS_MMF)
1961 /* Process and recycle a completed SITD. Return true iff its urb completed,
1962 * and hence its completion callback probably added things to the hardware
1965 * Note that we carefully avoid recycling this descriptor until after any
1966 * completion callback runs, so that it won't be reused quickly. That is,
1967 * assuming (a) no more than two urbs per frame on this endpoint, and also
1968 * (b) only this endpoint's completions submit URBs. It seems some silicon
1969 * corrupts things if you reuse completed descriptors very quickly...
1973 struct ehci_hcd *ehci,
1974 struct ehci_sitd *sitd
1976 struct urb *urb = sitd->urb;
1977 struct usb_iso_packet_descriptor *desc;
1980 struct ehci_iso_stream *stream = sitd->stream;
1981 struct usb_device *dev;
1982 unsigned retval = false;
1984 urb_index = sitd->index;
1985 desc = &urb->iso_frame_desc [urb_index];
1986 t = hc32_to_cpup(ehci, &sitd->hw_results);
1988 /* report transfer status */
1989 if (t & SITD_ERRS) {
1991 if (t & SITD_STS_DBE)
1992 desc->status = usb_pipein (urb->pipe)
1993 ? -ENOSR /* hc couldn't read */
1994 : -ECOMM; /* hc couldn't write */
1995 else if (t & SITD_STS_BABBLE)
1996 desc->status = -EOVERFLOW;
1997 else /* XACT, MMF, etc */
1998 desc->status = -EPROTO;
2001 desc->actual_length = desc->length - SITD_LENGTH (t);
2003 stream->depth -= stream->interval << 3;
2005 /* handle completion now? */
2006 if ((urb_index + 1) != urb->number_of_packets)
2009 /* ASSERT: it's really the last sitd for this urb
2010 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2011 BUG_ON (sitd->urb == urb);
2014 /* give urb back to the driver; completion often (re)submits */
2016 ehci_urb_done(ehci, urb, 0);
2019 (void) disable_periodic(ehci);
2020 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
2022 if (list_empty (&stream->td_list)) {
2023 ehci_to_hcd(ehci)->self.bandwidth_allocated
2024 -= stream->bandwidth;
2026 "deschedule devp %s ep%d%s-iso\n",
2027 dev->devpath, stream->bEndpointAddress & 0x0f,
2028 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
2030 iso_stream_put (ehci, stream);
2031 /* OK to recycle this SITD now that its completion callback ran. */
2035 sitd->stream = NULL;
2036 list_move(&sitd->sitd_list, &stream->free_list);
2037 iso_stream_put(ehci, stream);
2043 static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
2046 int status = -EINVAL;
2047 unsigned long flags;
2048 struct ehci_iso_stream *stream;
2050 /* Get iso_stream head */
2051 stream = iso_stream_find (ehci, urb);
2052 if (stream == NULL) {
2053 ehci_dbg (ehci, "can't get iso stream\n");
2056 if (urb->interval != stream->interval) {
2057 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
2058 stream->interval, urb->interval);
2062 #ifdef EHCI_URB_TRACE
2064 "submit %p dev%s ep%d%s-iso len %d\n",
2065 urb, urb->dev->devpath,
2066 usb_pipeendpoint (urb->pipe),
2067 usb_pipein (urb->pipe) ? "in" : "out",
2068 urb->transfer_buffer_length);
2071 /* allocate SITDs */
2072 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2074 ehci_dbg (ehci, "can't init sitds\n");
2078 /* schedule ... need to lock */
2079 spin_lock_irqsave (&ehci->lock, flags);
2080 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
2081 &ehci_to_hcd(ehci)->flags))) {
2082 status = -ESHUTDOWN;
2083 goto done_not_linked;
2085 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2086 if (unlikely(status))
2087 goto done_not_linked;
2088 status = iso_stream_schedule(ehci, urb, stream);
2090 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
2092 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
2094 spin_unlock_irqrestore (&ehci->lock, flags);
2098 iso_stream_put (ehci, stream);
2102 /*-------------------------------------------------------------------------*/
2105 scan_periodic (struct ehci_hcd *ehci)
2107 unsigned now_uframe, frame, clock, clock_frame, mod;
2110 mod = ehci->periodic_size << 3;
2113 * When running, scan from last scan point up to "now"
2114 * else clean up by scanning everything that's left.
2115 * Touches as few pages as possible: cache-friendly.
2117 now_uframe = ehci->next_uframe;
2118 if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
2119 clock = ehci_readl(ehci, &ehci->regs->frame_index);
2121 clock = now_uframe + mod - 1;
2123 clock_frame = clock >> 3;
2126 union ehci_shadow q, *q_p;
2128 unsigned incomplete = false;
2130 frame = now_uframe >> 3;
2133 /* scan each element in frame's queue for completions */
2134 q_p = &ehci->pshadow [frame];
2135 hw_p = &ehci->periodic [frame];
2137 type = Q_NEXT_TYPE(ehci, *hw_p);
2140 while (q.ptr != NULL) {
2142 union ehci_shadow temp;
2145 live = HC_IS_RUNNING (ehci_to_hcd(ehci)->state);
2146 switch (hc32_to_cpu(ehci, type)) {
2148 /* handle any completions */
2149 temp.qh = qh_get (q.qh);
2150 type = Q_NEXT_TYPE(ehci, q.qh->hw_next);
2152 modified = qh_completions (ehci, temp.qh);
2153 if (unlikely (list_empty (&temp.qh->qtd_list)))
2154 intr_deschedule (ehci, temp.qh);
2158 /* for "save place" FSTNs, look at QH entries
2159 * in the previous frame for completions.
2161 if (q.fstn->hw_prev != EHCI_LIST_END(ehci)) {
2162 dbg ("ignoring completions from FSTNs");
2164 type = Q_NEXT_TYPE(ehci, q.fstn->hw_next);
2165 q = q.fstn->fstn_next;
2168 /* If this ITD is still active, leave it for
2169 * later processing ... check the next entry.
2170 * No need to check for activity unless the
2173 if (frame == clock_frame && live) {
2175 for (uf = 0; uf < 8; uf++) {
2176 if (q.itd->hw_transaction[uf] &
2182 q_p = &q.itd->itd_next;
2183 hw_p = &q.itd->hw_next;
2184 type = Q_NEXT_TYPE(ehci,
2191 /* Take finished ITDs out of the schedule
2192 * and process them: recycle, maybe report
2193 * URB completion. HC won't cache the
2194 * pointer for much longer, if at all.
2196 *q_p = q.itd->itd_next;
2197 *hw_p = q.itd->hw_next;
2198 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
2200 modified = itd_complete (ehci, q.itd);
2204 /* If this SITD is still active, leave it for
2205 * later processing ... check the next entry.
2206 * No need to check for activity unless the
2209 if (frame == clock_frame && live &&
2210 (q.sitd->hw_results &
2211 SITD_ACTIVE(ehci))) {
2213 q_p = &q.sitd->sitd_next;
2214 hw_p = &q.sitd->hw_next;
2215 type = Q_NEXT_TYPE(ehci,
2221 /* Take finished SITDs out of the schedule
2222 * and process them: recycle, maybe report
2225 *q_p = q.sitd->sitd_next;
2226 *hw_p = q.sitd->hw_next;
2227 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
2229 modified = sitd_complete (ehci, q.sitd);
2233 dbg ("corrupt type %d frame %d shadow %p",
2234 type, frame, q.ptr);
2239 /* assume completion callbacks modify the queue */
2240 if (unlikely (modified)) {
2241 if (likely(ehci->periodic_sched > 0))
2243 /* short-circuit this scan */
2249 /* If we can tell we caught up to the hardware, stop now.
2250 * We can't advance our scan without collecting the ISO
2251 * transfers that are still pending in this frame.
2253 if (incomplete && HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
2254 ehci->next_uframe = now_uframe;
2258 // FIXME: this assumes we won't get lapped when
2259 // latencies climb; that should be rare, but...
2260 // detect it, and just go all the way around.
2261 // FLR might help detect this case, so long as latencies
2262 // don't exceed periodic_size msec (default 1.024 sec).
2264 // FIXME: likewise assumes HC doesn't halt mid-scan
2266 if (now_uframe == clock) {
2269 if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state)
2270 || ehci->periodic_sched == 0)
2272 ehci->next_uframe = now_uframe;
2273 now = ehci_readl(ehci, &ehci->regs->frame_index) % mod;
2274 if (now_uframe == now)
2277 /* rescan the rest of this frame, then ... */
2279 clock_frame = clock >> 3;