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
);
40 * periodic_next_shadow - return "next" pointer on shadow list
41 * @periodic: host pointer to qh/itd/sitd
42 * @tag: hardware tag for type of this record
44 static union ehci_shadow
*
45 periodic_next_shadow(struct ehci_hcd
*ehci
, union ehci_shadow
*periodic
,
48 switch (hc32_to_cpu(ehci
, tag
)) {
50 return &periodic
->qh
->qh_next
;
52 return &periodic
->fstn
->fstn_next
;
54 return &periodic
->itd
->itd_next
;
57 return &periodic
->sitd
->sitd_next
;
62 shadow_next_periodic(struct ehci_hcd
*ehci
, union ehci_shadow
*periodic
,
65 switch (hc32_to_cpu(ehci
, tag
)) {
66 /* our ehci_shadow.qh is actually software part */
68 return &periodic
->qh
->hw
->hw_next
;
69 /* others are hw parts */
71 return periodic
->hw_next
;
75 /* caller must hold ehci->lock */
76 static void periodic_unlink (struct ehci_hcd
*ehci
, unsigned frame
, void *ptr
)
78 union ehci_shadow
*prev_p
= &ehci
->pshadow
[frame
];
79 __hc32
*hw_p
= &ehci
->periodic
[frame
];
80 union ehci_shadow here
= *prev_p
;
82 /* find predecessor of "ptr"; hw and shadow lists are in sync */
83 while (here
.ptr
&& here
.ptr
!= ptr
) {
84 prev_p
= periodic_next_shadow(ehci
, prev_p
,
85 Q_NEXT_TYPE(ehci
, *hw_p
));
86 hw_p
= shadow_next_periodic(ehci
, &here
,
87 Q_NEXT_TYPE(ehci
, *hw_p
));
90 /* an interrupt entry (at list end) could have been shared */
94 /* update shadow and hardware lists ... the old "next" pointers
95 * from ptr may still be in use, the caller updates them.
97 *prev_p
= *periodic_next_shadow(ehci
, &here
,
98 Q_NEXT_TYPE(ehci
, *hw_p
));
100 if (!ehci
->use_dummy_qh
||
101 *shadow_next_periodic(ehci
, &here
, Q_NEXT_TYPE(ehci
, *hw_p
))
102 != EHCI_LIST_END(ehci
))
103 *hw_p
= *shadow_next_periodic(ehci
, &here
,
104 Q_NEXT_TYPE(ehci
, *hw_p
));
106 *hw_p
= ehci
->dummy
->qh_dma
;
109 /* how many of the uframe's 125 usecs are allocated? */
110 static unsigned short
111 periodic_usecs (struct ehci_hcd
*ehci
, unsigned frame
, unsigned uframe
)
113 __hc32
*hw_p
= &ehci
->periodic
[frame
];
114 union ehci_shadow
*q
= &ehci
->pshadow
[frame
];
116 struct ehci_qh_hw
*hw
;
119 switch (hc32_to_cpu(ehci
, Q_NEXT_TYPE(ehci
, *hw_p
))) {
122 /* is it in the S-mask? */
123 if (hw
->hw_info2
& cpu_to_hc32(ehci
, 1 << uframe
))
124 usecs
+= q
->qh
->usecs
;
126 if (hw
->hw_info2
& cpu_to_hc32(ehci
,
128 usecs
+= q
->qh
->c_usecs
;
134 /* for "save place" FSTNs, count the relevant INTR
135 * bandwidth from the previous frame
137 if (q
->fstn
->hw_prev
!= EHCI_LIST_END(ehci
)) {
138 ehci_dbg (ehci
, "ignoring FSTN cost ...\n");
140 hw_p
= &q
->fstn
->hw_next
;
141 q
= &q
->fstn
->fstn_next
;
144 if (q
->itd
->hw_transaction
[uframe
])
145 usecs
+= q
->itd
->stream
->usecs
;
146 hw_p
= &q
->itd
->hw_next
;
147 q
= &q
->itd
->itd_next
;
150 /* is it in the S-mask? (count SPLIT, DATA) */
151 if (q
->sitd
->hw_uframe
& cpu_to_hc32(ehci
,
153 if (q
->sitd
->hw_fullspeed_ep
&
154 cpu_to_hc32(ehci
, 1<<31))
155 usecs
+= q
->sitd
->stream
->usecs
;
156 else /* worst case for OUT start-split */
157 usecs
+= HS_USECS_ISO (188);
160 /* ... C-mask? (count CSPLIT, DATA) */
161 if (q
->sitd
->hw_uframe
&
162 cpu_to_hc32(ehci
, 1 << (8 + uframe
))) {
163 /* worst case for IN complete-split */
164 usecs
+= q
->sitd
->stream
->c_usecs
;
167 hw_p
= &q
->sitd
->hw_next
;
168 q
= &q
->sitd
->sitd_next
;
172 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
173 if (usecs
> ehci
->uframe_periodic_max
)
174 ehci_err (ehci
, "uframe %d sched overrun: %d usecs\n",
175 frame
* 8 + uframe
, usecs
);
180 /*-------------------------------------------------------------------------*/
182 static int same_tt (struct usb_device
*dev1
, struct usb_device
*dev2
)
184 if (!dev1
->tt
|| !dev2
->tt
)
186 if (dev1
->tt
!= dev2
->tt
)
189 return dev1
->ttport
== dev2
->ttport
;
194 #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
196 /* Which uframe does the low/fullspeed transfer start in?
198 * The parameter is the mask of ssplits in "H-frame" terms
199 * and this returns the transfer start uframe in "B-frame" terms,
200 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
201 * will cause a transfer in "B-frame" uframe 0. "B-frames" lag
202 * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7.
204 static inline unsigned char tt_start_uframe(struct ehci_hcd
*ehci
, __hc32 mask
)
206 unsigned char smask
= QH_SMASK
& hc32_to_cpu(ehci
, mask
);
208 ehci_err(ehci
, "invalid empty smask!\n");
209 /* uframe 7 can't have bw so this will indicate failure */
212 return ffs(smask
) - 1;
215 static const unsigned char
216 max_tt_usecs
[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
218 /* carryover low/fullspeed bandwidth that crosses uframe boundries */
219 static inline void carryover_tt_bandwidth(unsigned short tt_usecs
[8])
222 for (i
=0; i
<7; i
++) {
223 if (max_tt_usecs
[i
] < tt_usecs
[i
]) {
224 tt_usecs
[i
+1] += tt_usecs
[i
] - max_tt_usecs
[i
];
225 tt_usecs
[i
] = max_tt_usecs
[i
];
230 /* How many of the tt's periodic downstream 1000 usecs are allocated?
232 * While this measures the bandwidth in terms of usecs/uframe,
233 * the low/fullspeed bus has no notion of uframes, so any particular
234 * low/fullspeed transfer can "carry over" from one uframe to the next,
235 * since the TT just performs downstream transfers in sequence.
237 * For example two separate 100 usec transfers can start in the same uframe,
238 * and the second one would "carry over" 75 usecs into the next uframe.
242 struct ehci_hcd
*ehci
,
243 struct usb_device
*dev
,
245 unsigned short tt_usecs
[8]
248 __hc32
*hw_p
= &ehci
->periodic
[frame
];
249 union ehci_shadow
*q
= &ehci
->pshadow
[frame
];
252 memset(tt_usecs
, 0, 16);
255 switch (hc32_to_cpu(ehci
, Q_NEXT_TYPE(ehci
, *hw_p
))) {
257 hw_p
= &q
->itd
->hw_next
;
258 q
= &q
->itd
->itd_next
;
261 if (same_tt(dev
, q
->qh
->dev
)) {
262 uf
= tt_start_uframe(ehci
, q
->qh
->hw
->hw_info2
);
263 tt_usecs
[uf
] += q
->qh
->tt_usecs
;
265 hw_p
= &q
->qh
->hw
->hw_next
;
269 if (same_tt(dev
, q
->sitd
->urb
->dev
)) {
270 uf
= tt_start_uframe(ehci
, q
->sitd
->hw_uframe
);
271 tt_usecs
[uf
] += q
->sitd
->stream
->tt_usecs
;
273 hw_p
= &q
->sitd
->hw_next
;
274 q
= &q
->sitd
->sitd_next
;
278 ehci_dbg(ehci
, "ignoring periodic frame %d FSTN\n",
280 hw_p
= &q
->fstn
->hw_next
;
281 q
= &q
->fstn
->fstn_next
;
285 carryover_tt_bandwidth(tt_usecs
);
287 if (max_tt_usecs
[7] < tt_usecs
[7])
288 ehci_err(ehci
, "frame %d tt sched overrun: %d usecs\n",
289 frame
, tt_usecs
[7] - max_tt_usecs
[7]);
293 * Return true if the device's tt's downstream bus is available for a
294 * periodic transfer of the specified length (usecs), starting at the
295 * specified frame/uframe. Note that (as summarized in section 11.19
296 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
299 * The uframe parameter is when the fullspeed/lowspeed transfer
300 * should be executed in "B-frame" terms, which is the same as the
301 * highspeed ssplit's uframe (which is in "H-frame" terms). For example
302 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
303 * See the EHCI spec sec 4.5 and fig 4.7.
305 * This checks if the full/lowspeed bus, at the specified starting uframe,
306 * has the specified bandwidth available, according to rules listed
307 * in USB 2.0 spec section 11.18.1 fig 11-60.
309 * This does not check if the transfer would exceed the max ssplit
310 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
311 * since proper scheduling limits ssplits to less than 16 per uframe.
313 static int tt_available (
314 struct ehci_hcd
*ehci
,
316 struct usb_device
*dev
,
322 if ((period
== 0) || (uframe
>= 7)) /* error */
325 for (; frame
< ehci
->periodic_size
; frame
+= period
) {
326 unsigned short tt_usecs
[8];
328 periodic_tt_usecs (ehci
, dev
, frame
, tt_usecs
);
330 if (max_tt_usecs
[uframe
] <= tt_usecs
[uframe
])
333 /* special case for isoc transfers larger than 125us:
334 * the first and each subsequent fully used uframe
335 * must be empty, so as to not illegally delay
336 * already scheduled transactions
339 int ufs
= (usecs
/ 125);
341 for (i
= uframe
; i
< (uframe
+ ufs
) && i
< 8; i
++)
346 tt_usecs
[uframe
] += usecs
;
348 carryover_tt_bandwidth(tt_usecs
);
350 /* fail if the carryover pushed bw past the last uframe's limit */
351 if (max_tt_usecs
[7] < tt_usecs
[7])
360 /* return true iff the device's transaction translator is available
361 * for a periodic transfer starting at the specified frame, using
362 * all the uframes in the mask.
364 static int tt_no_collision (
365 struct ehci_hcd
*ehci
,
367 struct usb_device
*dev
,
372 if (period
== 0) /* error */
375 /* note bandwidth wastage: split never follows csplit
376 * (different dev or endpoint) until the next uframe.
377 * calling convention doesn't make that distinction.
379 for (; frame
< ehci
->periodic_size
; frame
+= period
) {
380 union ehci_shadow here
;
382 struct ehci_qh_hw
*hw
;
384 here
= ehci
->pshadow
[frame
];
385 type
= Q_NEXT_TYPE(ehci
, ehci
->periodic
[frame
]);
387 switch (hc32_to_cpu(ehci
, type
)) {
389 type
= Q_NEXT_TYPE(ehci
, here
.itd
->hw_next
);
390 here
= here
.itd
->itd_next
;
394 if (same_tt (dev
, here
.qh
->dev
)) {
397 mask
= hc32_to_cpu(ehci
,
399 /* "knows" no gap is needed */
404 type
= Q_NEXT_TYPE(ehci
, hw
->hw_next
);
405 here
= here
.qh
->qh_next
;
408 if (same_tt (dev
, here
.sitd
->urb
->dev
)) {
411 mask
= hc32_to_cpu(ehci
, here
.sitd
413 /* FIXME assumes no gap for IN! */
418 type
= Q_NEXT_TYPE(ehci
, here
.sitd
->hw_next
);
419 here
= here
.sitd
->sitd_next
;
424 "periodic frame %d bogus type %d\n",
428 /* collision or error */
437 #endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
439 /*-------------------------------------------------------------------------*/
441 static void enable_periodic(struct ehci_hcd
*ehci
)
443 if (ehci
->periodic_count
++)
446 /* Stop waiting to turn off the periodic schedule */
447 ehci
->enabled_hrtimer_events
&= ~BIT(EHCI_HRTIMER_DISABLE_PERIODIC
);
449 /* Don't start the schedule until PSS is 0 */
451 turn_on_io_watchdog(ehci
);
454 static void disable_periodic(struct ehci_hcd
*ehci
)
456 if (--ehci
->periodic_count
)
459 /* Don't turn off the schedule until PSS is 1 */
463 /*-------------------------------------------------------------------------*/
465 /* periodic schedule slots have iso tds (normal or split) first, then a
466 * sparse tree for active interrupt transfers.
468 * this just links in a qh; caller guarantees uframe masks are set right.
469 * no FSTN support (yet; ehci 0.96+)
471 static void qh_link_periodic(struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
474 unsigned period
= qh
->period
;
476 dev_dbg (&qh
->dev
->dev
,
477 "link qh%d-%04x/%p start %d [%d/%d us]\n",
478 period
, hc32_to_cpup(ehci
, &qh
->hw
->hw_info2
)
479 & (QH_CMASK
| QH_SMASK
),
480 qh
, qh
->start
, qh
->usecs
, qh
->c_usecs
);
482 /* high bandwidth, or otherwise every microframe */
486 for (i
= qh
->start
; i
< ehci
->periodic_size
; i
+= period
) {
487 union ehci_shadow
*prev
= &ehci
->pshadow
[i
];
488 __hc32
*hw_p
= &ehci
->periodic
[i
];
489 union ehci_shadow here
= *prev
;
492 /* skip the iso nodes at list head */
494 type
= Q_NEXT_TYPE(ehci
, *hw_p
);
495 if (type
== cpu_to_hc32(ehci
, Q_TYPE_QH
))
497 prev
= periodic_next_shadow(ehci
, prev
, type
);
498 hw_p
= shadow_next_periodic(ehci
, &here
, type
);
502 /* sorting each branch by period (slow-->fast)
503 * enables sharing interior tree nodes
505 while (here
.ptr
&& qh
!= here
.qh
) {
506 if (qh
->period
> here
.qh
->period
)
508 prev
= &here
.qh
->qh_next
;
509 hw_p
= &here
.qh
->hw
->hw_next
;
512 /* link in this qh, unless some earlier pass did that */
516 qh
->hw
->hw_next
= *hw_p
;
519 *hw_p
= QH_NEXT (ehci
, qh
->qh_dma
);
522 qh
->qh_state
= QH_STATE_LINKED
;
526 /* update per-qh bandwidth for usbfs */
527 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
+= qh
->period
528 ? ((qh
->usecs
+ qh
->c_usecs
) / qh
->period
)
531 list_add(&qh
->intr_node
, &ehci
->intr_qh_list
);
533 /* maybe enable periodic schedule processing */
535 enable_periodic(ehci
);
538 static void qh_unlink_periodic(struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
544 * If qh is for a low/full-speed device, simply unlinking it
545 * could interfere with an ongoing split transaction. To unlink
546 * it safely would require setting the QH_INACTIVATE bit and
547 * waiting at least one frame, as described in EHCI 4.12.2.5.
549 * We won't bother with any of this. Instead, we assume that the
550 * only reason for unlinking an interrupt QH while the current URB
551 * is still active is to dequeue all the URBs (flush the whole
554 * If rebalancing the periodic schedule is ever implemented, this
555 * approach will no longer be valid.
558 /* high bandwidth, or otherwise part of every microframe */
559 if ((period
= qh
->period
) == 0)
562 for (i
= qh
->start
; i
< ehci
->periodic_size
; i
+= period
)
563 periodic_unlink (ehci
, i
, qh
);
565 /* update per-qh bandwidth for usbfs */
566 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
-= qh
->period
567 ? ((qh
->usecs
+ qh
->c_usecs
) / qh
->period
)
570 dev_dbg (&qh
->dev
->dev
,
571 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
573 hc32_to_cpup(ehci
, &qh
->hw
->hw_info2
) & (QH_CMASK
| QH_SMASK
),
574 qh
, qh
->start
, qh
->usecs
, qh
->c_usecs
);
576 /* qh->qh_next still "live" to HC */
577 qh
->qh_state
= QH_STATE_UNLINK
;
578 qh
->qh_next
.ptr
= NULL
;
580 if (ehci
->qh_scan_next
== qh
)
581 ehci
->qh_scan_next
= list_entry(qh
->intr_node
.next
,
582 struct ehci_qh
, intr_node
);
583 list_del(&qh
->intr_node
);
586 static void cancel_unlink_wait_intr(struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
588 if (qh
->qh_state
!= QH_STATE_LINKED
||
589 list_empty(&qh
->unlink_node
))
592 list_del_init(&qh
->unlink_node
);
595 * TODO: disable the event of EHCI_HRTIMER_START_UNLINK_INTR for
596 * avoiding unnecessary CPU wakeup
600 static void start_unlink_intr(struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
602 /* If the QH isn't linked then there's nothing we can do. */
603 if (qh
->qh_state
!= QH_STATE_LINKED
)
606 /* if the qh is waiting for unlink, cancel it now */
607 cancel_unlink_wait_intr(ehci
, qh
);
609 qh_unlink_periodic (ehci
, qh
);
611 /* Make sure the unlinks are visible before starting the timer */
615 * The EHCI spec doesn't say how long it takes the controller to
616 * stop accessing an unlinked interrupt QH. The timer delay is
617 * 9 uframes; presumably that will be long enough.
619 qh
->unlink_cycle
= ehci
->intr_unlink_cycle
;
621 /* New entries go at the end of the intr_unlink list */
622 list_add_tail(&qh
->unlink_node
, &ehci
->intr_unlink
);
624 if (ehci
->intr_unlinking
)
625 ; /* Avoid recursive calls */
626 else if (ehci
->rh_state
< EHCI_RH_RUNNING
)
627 ehci_handle_intr_unlinks(ehci
);
628 else if (ehci
->intr_unlink
.next
== &qh
->unlink_node
) {
629 ehci_enable_event(ehci
, EHCI_HRTIMER_UNLINK_INTR
, true);
630 ++ehci
->intr_unlink_cycle
;
635 * It is common only one intr URB is scheduled on one qh, and
636 * given complete() is run in tasklet context, introduce a bit
637 * delay to avoid unlink qh too early.
639 static void start_unlink_intr_wait(struct ehci_hcd
*ehci
,
642 qh
->unlink_cycle
= ehci
->intr_unlink_wait_cycle
;
644 /* New entries go at the end of the intr_unlink_wait list */
645 list_add_tail(&qh
->unlink_node
, &ehci
->intr_unlink_wait
);
647 if (ehci
->rh_state
< EHCI_RH_RUNNING
)
648 ehci_handle_start_intr_unlinks(ehci
);
649 else if (ehci
->intr_unlink_wait
.next
== &qh
->unlink_node
) {
650 ehci_enable_event(ehci
, EHCI_HRTIMER_START_UNLINK_INTR
, true);
651 ++ehci
->intr_unlink_wait_cycle
;
655 static void end_unlink_intr(struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
657 struct ehci_qh_hw
*hw
= qh
->hw
;
660 qh
->qh_state
= QH_STATE_IDLE
;
661 hw
->hw_next
= EHCI_LIST_END(ehci
);
663 if (!list_empty(&qh
->qtd_list
))
664 qh_completions(ehci
, qh
);
666 /* reschedule QH iff another request is queued */
667 if (!list_empty(&qh
->qtd_list
) && ehci
->rh_state
== EHCI_RH_RUNNING
) {
668 rc
= qh_schedule(ehci
, qh
);
670 qh_refresh(ehci
, qh
);
671 qh_link_periodic(ehci
, qh
);
674 /* An error here likely indicates handshake failure
675 * or no space left in the schedule. Neither fault
676 * should happen often ...
678 * FIXME kill the now-dysfunctional queued urbs
681 ehci_err(ehci
, "can't reschedule qh %p, err %d\n",
686 /* maybe turn off periodic schedule */
688 disable_periodic(ehci
);
691 /*-------------------------------------------------------------------------*/
693 static int check_period (
694 struct ehci_hcd
*ehci
,
702 /* complete split running into next frame?
703 * given FSTN support, we could sometimes check...
708 /* convert "usecs we need" to "max already claimed" */
709 usecs
= ehci
->uframe_periodic_max
- usecs
;
711 /* we "know" 2 and 4 uframe intervals were rejected; so
712 * for period 0, check _every_ microframe in the schedule.
714 if (unlikely (period
== 0)) {
716 for (uframe
= 0; uframe
< 7; uframe
++) {
717 claimed
= periodic_usecs (ehci
, frame
, uframe
);
721 } while ((frame
+= 1) < ehci
->periodic_size
);
723 /* just check the specified uframe, at that period */
726 claimed
= periodic_usecs (ehci
, frame
, uframe
);
729 } while ((frame
+= period
) < ehci
->periodic_size
);
736 static int check_intr_schedule (
737 struct ehci_hcd
*ehci
,
740 const struct ehci_qh
*qh
,
744 int retval
= -ENOSPC
;
747 if (qh
->c_usecs
&& uframe
>= 6) /* FSTN territory? */
750 if (!check_period (ehci
, frame
, uframe
, qh
->period
, qh
->usecs
))
758 #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
759 if (tt_available (ehci
, qh
->period
, qh
->dev
, frame
, uframe
,
763 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
764 for (i
=uframe
+1; i
<8 && i
<uframe
+4; i
++)
765 if (!check_period (ehci
, frame
, i
,
766 qh
->period
, qh
->c_usecs
))
773 *c_maskp
= cpu_to_hc32(ehci
, mask
<< 8);
776 /* Make sure this tt's buffer is also available for CSPLITs.
777 * We pessimize a bit; probably the typical full speed case
778 * doesn't need the second CSPLIT.
780 * NOTE: both SPLIT and CSPLIT could be checked in just
783 mask
= 0x03 << (uframe
+ qh
->gap_uf
);
784 *c_maskp
= cpu_to_hc32(ehci
, mask
<< 8);
787 if (tt_no_collision (ehci
, qh
->period
, qh
->dev
, frame
, mask
)) {
788 if (!check_period (ehci
, frame
, uframe
+ qh
->gap_uf
+ 1,
789 qh
->period
, qh
->c_usecs
))
791 if (!check_period (ehci
, frame
, uframe
+ qh
->gap_uf
,
792 qh
->period
, qh
->c_usecs
))
801 /* "first fit" scheduling policy used the first time through,
802 * or when the previous schedule slot can't be re-used.
804 static int qh_schedule(struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
809 unsigned frame
; /* 0..(qh->period - 1), or NO_FRAME */
810 struct ehci_qh_hw
*hw
= qh
->hw
;
812 hw
->hw_next
= EHCI_LIST_END(ehci
);
815 /* reuse the previous schedule slots, if we can */
816 if (frame
< qh
->period
) {
817 uframe
= ffs(hc32_to_cpup(ehci
, &hw
->hw_info2
) & QH_SMASK
);
818 status
= check_intr_schedule (ehci
, frame
, --uframe
,
826 /* else scan the schedule to find a group of slots such that all
827 * uframes have enough periodic bandwidth available.
830 /* "normal" case, uframing flexible except with splits */
834 for (i
= qh
->period
; status
&& i
> 0; --i
) {
835 frame
= ++ehci
->random_frame
% qh
->period
;
836 for (uframe
= 0; uframe
< 8; uframe
++) {
837 status
= check_intr_schedule (ehci
,
845 /* qh->period == 0 means every uframe */
848 status
= check_intr_schedule (ehci
, 0, 0, qh
, &c_mask
);
854 /* reset S-frame and (maybe) C-frame masks */
855 hw
->hw_info2
&= cpu_to_hc32(ehci
, ~(QH_CMASK
| QH_SMASK
));
856 hw
->hw_info2
|= qh
->period
857 ? cpu_to_hc32(ehci
, 1 << uframe
)
858 : cpu_to_hc32(ehci
, QH_SMASK
);
859 hw
->hw_info2
|= c_mask
;
861 ehci_dbg (ehci
, "reused qh %p schedule\n", qh
);
867 static int intr_submit (
868 struct ehci_hcd
*ehci
,
870 struct list_head
*qtd_list
,
877 struct list_head empty
;
879 /* get endpoint and transfer/schedule data */
880 epnum
= urb
->ep
->desc
.bEndpointAddress
;
882 spin_lock_irqsave (&ehci
->lock
, flags
);
884 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci
)))) {
886 goto done_not_linked
;
888 status
= usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci
), urb
);
889 if (unlikely(status
))
890 goto done_not_linked
;
892 /* get qh and force any scheduling errors */
893 INIT_LIST_HEAD (&empty
);
894 qh
= qh_append_tds(ehci
, urb
, &empty
, epnum
, &urb
->ep
->hcpriv
);
899 if (qh
->qh_state
== QH_STATE_IDLE
) {
900 if ((status
= qh_schedule (ehci
, qh
)) != 0)
904 /* then queue the urb's tds to the qh */
905 qh
= qh_append_tds(ehci
, urb
, qtd_list
, epnum
, &urb
->ep
->hcpriv
);
908 /* stuff into the periodic schedule */
909 if (qh
->qh_state
== QH_STATE_IDLE
) {
910 qh_refresh(ehci
, qh
);
911 qh_link_periodic(ehci
, qh
);
913 /* cancel unlink wait for the qh */
914 cancel_unlink_wait_intr(ehci
, qh
);
917 /* ... update usbfs periodic stats */
918 ehci_to_hcd(ehci
)->self
.bandwidth_int_reqs
++;
921 if (unlikely(status
))
922 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci
), urb
);
924 spin_unlock_irqrestore (&ehci
->lock
, flags
);
926 qtd_list_free (ehci
, urb
, qtd_list
);
931 static void scan_intr(struct ehci_hcd
*ehci
)
935 list_for_each_entry_safe(qh
, ehci
->qh_scan_next
, &ehci
->intr_qh_list
,
938 /* clean any finished work for this qh */
939 if (!list_empty(&qh
->qtd_list
)) {
943 * Unlinks could happen here; completion reporting
944 * drops the lock. That's why ehci->qh_scan_next
945 * always holds the next qh to scan; if the next qh
946 * gets unlinked then ehci->qh_scan_next is adjusted
947 * in qh_unlink_periodic().
949 temp
= qh_completions(ehci
, qh
);
951 start_unlink_intr(ehci
, qh
);
952 else if (unlikely(list_empty(&qh
->qtd_list
) &&
953 qh
->qh_state
== QH_STATE_LINKED
))
954 start_unlink_intr_wait(ehci
, qh
);
959 /*-------------------------------------------------------------------------*/
961 /* ehci_iso_stream ops work with both ITD and SITD */
963 static struct ehci_iso_stream
*
964 iso_stream_alloc (gfp_t mem_flags
)
966 struct ehci_iso_stream
*stream
;
968 stream
= kzalloc(sizeof *stream
, mem_flags
);
969 if (likely (stream
!= NULL
)) {
970 INIT_LIST_HEAD(&stream
->td_list
);
971 INIT_LIST_HEAD(&stream
->free_list
);
972 stream
->next_uframe
= -1;
979 struct ehci_hcd
*ehci
,
980 struct ehci_iso_stream
*stream
,
981 struct usb_device
*dev
,
986 static const u8 smask_out
[] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
989 unsigned epnum
, maxp
;
994 * this might be a "high bandwidth" highspeed endpoint,
995 * as encoded in the ep descriptor's wMaxPacket field
997 epnum
= usb_pipeendpoint (pipe
);
998 is_input
= usb_pipein (pipe
) ? USB_DIR_IN
: 0;
999 maxp
= usb_maxpacket(dev
, pipe
, !is_input
);
1006 /* knows about ITD vs SITD */
1007 if (dev
->speed
== USB_SPEED_HIGH
) {
1008 unsigned multi
= hb_mult(maxp
);
1010 stream
->highspeed
= 1;
1012 maxp
= max_packet(maxp
);
1016 stream
->buf0
= cpu_to_hc32(ehci
, (epnum
<< 8) | dev
->devnum
);
1017 stream
->buf1
= cpu_to_hc32(ehci
, buf1
);
1018 stream
->buf2
= cpu_to_hc32(ehci
, multi
);
1020 /* usbfs wants to report the average usecs per frame tied up
1021 * when transfers on this endpoint are scheduled ...
1023 stream
->usecs
= HS_USECS_ISO (maxp
);
1024 bandwidth
= stream
->usecs
* 8;
1025 bandwidth
/= interval
;
1032 addr
= dev
->ttport
<< 24;
1033 if (!ehci_is_TDI(ehci
)
1035 ehci_to_hcd(ehci
)->self
.root_hub
))
1036 addr
|= dev
->tt
->hub
->devnum
<< 16;
1038 addr
|= dev
->devnum
;
1039 stream
->usecs
= HS_USECS_ISO (maxp
);
1040 think_time
= dev
->tt
? dev
->tt
->think_time
: 0;
1041 stream
->tt_usecs
= NS_TO_US (think_time
+ usb_calc_bus_time (
1042 dev
->speed
, is_input
, 1, maxp
));
1043 hs_transfers
= max (1u, (maxp
+ 187) / 188);
1048 stream
->c_usecs
= stream
->usecs
;
1049 stream
->usecs
= HS_USECS_ISO (1);
1050 stream
->raw_mask
= 1;
1052 /* c-mask as specified in USB 2.0 11.18.4 3.c */
1053 tmp
= (1 << (hs_transfers
+ 2)) - 1;
1054 stream
->raw_mask
|= tmp
<< (8 + 2);
1056 stream
->raw_mask
= smask_out
[hs_transfers
- 1];
1057 bandwidth
= stream
->usecs
+ stream
->c_usecs
;
1058 bandwidth
/= interval
<< 3;
1060 /* stream->splits gets created from raw_mask later */
1061 stream
->address
= cpu_to_hc32(ehci
, addr
);
1063 stream
->bandwidth
= bandwidth
;
1067 stream
->bEndpointAddress
= is_input
| epnum
;
1068 stream
->interval
= interval
;
1069 stream
->maxp
= maxp
;
1072 static struct ehci_iso_stream
*
1073 iso_stream_find (struct ehci_hcd
*ehci
, struct urb
*urb
)
1076 struct ehci_iso_stream
*stream
;
1077 struct usb_host_endpoint
*ep
;
1078 unsigned long flags
;
1080 epnum
= usb_pipeendpoint (urb
->pipe
);
1081 if (usb_pipein(urb
->pipe
))
1082 ep
= urb
->dev
->ep_in
[epnum
];
1084 ep
= urb
->dev
->ep_out
[epnum
];
1086 spin_lock_irqsave (&ehci
->lock
, flags
);
1087 stream
= ep
->hcpriv
;
1089 if (unlikely (stream
== NULL
)) {
1090 stream
= iso_stream_alloc(GFP_ATOMIC
);
1091 if (likely (stream
!= NULL
)) {
1092 ep
->hcpriv
= stream
;
1094 iso_stream_init(ehci
, stream
, urb
->dev
, urb
->pipe
,
1098 /* if dev->ep [epnum] is a QH, hw is set */
1099 } else if (unlikely (stream
->hw
!= NULL
)) {
1100 ehci_dbg (ehci
, "dev %s ep%d%s, not iso??\n",
1101 urb
->dev
->devpath
, epnum
,
1102 usb_pipein(urb
->pipe
) ? "in" : "out");
1106 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1110 /*-------------------------------------------------------------------------*/
1112 /* ehci_iso_sched ops can be ITD-only or SITD-only */
1114 static struct ehci_iso_sched
*
1115 iso_sched_alloc (unsigned packets
, gfp_t mem_flags
)
1117 struct ehci_iso_sched
*iso_sched
;
1118 int size
= sizeof *iso_sched
;
1120 size
+= packets
* sizeof (struct ehci_iso_packet
);
1121 iso_sched
= kzalloc(size
, mem_flags
);
1122 if (likely (iso_sched
!= NULL
)) {
1123 INIT_LIST_HEAD (&iso_sched
->td_list
);
1130 struct ehci_hcd
*ehci
,
1131 struct ehci_iso_sched
*iso_sched
,
1132 struct ehci_iso_stream
*stream
,
1137 dma_addr_t dma
= urb
->transfer_dma
;
1139 /* how many uframes are needed for these transfers */
1140 iso_sched
->span
= urb
->number_of_packets
* stream
->interval
;
1142 /* figure out per-uframe itd fields that we'll need later
1143 * when we fit new itds into the schedule.
1145 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1146 struct ehci_iso_packet
*uframe
= &iso_sched
->packet
[i
];
1151 length
= urb
->iso_frame_desc
[i
].length
;
1152 buf
= dma
+ urb
->iso_frame_desc
[i
].offset
;
1154 trans
= EHCI_ISOC_ACTIVE
;
1155 trans
|= buf
& 0x0fff;
1156 if (unlikely (((i
+ 1) == urb
->number_of_packets
))
1157 && !(urb
->transfer_flags
& URB_NO_INTERRUPT
))
1158 trans
|= EHCI_ITD_IOC
;
1159 trans
|= length
<< 16;
1160 uframe
->transaction
= cpu_to_hc32(ehci
, trans
);
1162 /* might need to cross a buffer page within a uframe */
1163 uframe
->bufp
= (buf
& ~(u64
)0x0fff);
1165 if (unlikely ((uframe
->bufp
!= (buf
& ~(u64
)0x0fff))))
1172 struct ehci_iso_stream
*stream
,
1173 struct ehci_iso_sched
*iso_sched
1178 // caller must hold ehci->lock!
1179 list_splice (&iso_sched
->td_list
, &stream
->free_list
);
1184 itd_urb_transaction (
1185 struct ehci_iso_stream
*stream
,
1186 struct ehci_hcd
*ehci
,
1191 struct ehci_itd
*itd
;
1195 struct ehci_iso_sched
*sched
;
1196 unsigned long flags
;
1198 sched
= iso_sched_alloc (urb
->number_of_packets
, mem_flags
);
1199 if (unlikely (sched
== NULL
))
1202 itd_sched_init(ehci
, sched
, stream
, urb
);
1204 if (urb
->interval
< 8)
1205 num_itds
= 1 + (sched
->span
+ 7) / 8;
1207 num_itds
= urb
->number_of_packets
;
1209 /* allocate/init ITDs */
1210 spin_lock_irqsave (&ehci
->lock
, flags
);
1211 for (i
= 0; i
< num_itds
; i
++) {
1214 * Use iTDs from the free list, but not iTDs that may
1215 * still be in use by the hardware.
1217 if (likely(!list_empty(&stream
->free_list
))) {
1218 itd
= list_first_entry(&stream
->free_list
,
1219 struct ehci_itd
, itd_list
);
1220 if (itd
->frame
== ehci
->now_frame
)
1222 list_del (&itd
->itd_list
);
1223 itd_dma
= itd
->itd_dma
;
1226 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1227 itd
= dma_pool_alloc (ehci
->itd_pool
, mem_flags
,
1229 spin_lock_irqsave (&ehci
->lock
, flags
);
1231 iso_sched_free(stream
, sched
);
1232 spin_unlock_irqrestore(&ehci
->lock
, flags
);
1237 memset (itd
, 0, sizeof *itd
);
1238 itd
->itd_dma
= itd_dma
;
1239 itd
->frame
= 9999; /* an invalid value */
1240 list_add (&itd
->itd_list
, &sched
->td_list
);
1242 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1244 /* temporarily store schedule info in hcpriv */
1245 urb
->hcpriv
= sched
;
1246 urb
->error_count
= 0;
1250 /*-------------------------------------------------------------------------*/
1254 struct ehci_hcd
*ehci
,
1263 /* can't commit more than uframe_periodic_max usec */
1264 if (periodic_usecs (ehci
, uframe
>> 3, uframe
& 0x7)
1265 > (ehci
->uframe_periodic_max
- usecs
))
1268 /* we know urb->interval is 2^N uframes */
1270 } while (uframe
< mod
);
1276 struct ehci_hcd
*ehci
,
1278 struct ehci_iso_stream
*stream
,
1280 struct ehci_iso_sched
*sched
,
1287 mask
= stream
->raw_mask
<< (uframe
& 7);
1289 /* for IN, don't wrap CSPLIT into the next frame */
1293 /* check bandwidth */
1294 uframe
%= period_uframes
;
1295 frame
= uframe
>> 3;
1297 #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1298 /* The tt's fullspeed bus bandwidth must be available.
1299 * tt_available scheduling guarantees 10+% for control/bulk.
1302 if (!tt_available(ehci
, period_uframes
>> 3,
1303 stream
->udev
, frame
, uf
, stream
->tt_usecs
))
1306 /* tt must be idle for start(s), any gap, and csplit.
1307 * assume scheduling slop leaves 10+% for control/bulk.
1309 if (!tt_no_collision(ehci
, period_uframes
>> 3,
1310 stream
->udev
, frame
, mask
))
1314 /* this multi-pass logic is simple, but performance may
1315 * suffer when the schedule data isn't cached.
1320 frame
= uframe
>> 3;
1323 /* check starts (OUT uses more than one) */
1324 max_used
= ehci
->uframe_periodic_max
- stream
->usecs
;
1325 for (tmp
= stream
->raw_mask
& 0xff; tmp
; tmp
>>= 1, uf
++) {
1326 if (periodic_usecs (ehci
, frame
, uf
) > max_used
)
1330 /* for IN, check CSPLIT */
1331 if (stream
->c_usecs
) {
1333 max_used
= ehci
->uframe_periodic_max
- stream
->c_usecs
;
1337 if ((stream
->raw_mask
& tmp
) == 0)
1339 if (periodic_usecs (ehci
, frame
, uf
)
1345 /* we know urb->interval is 2^N uframes */
1346 uframe
+= period_uframes
;
1347 } while (uframe
< mod
);
1349 stream
->splits
= cpu_to_hc32(ehci
, stream
->raw_mask
<< (uframe
& 7));
1354 * This scheduler plans almost as far into the future as it has actual
1355 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1356 * "as small as possible" to be cache-friendlier.) That limits the size
1357 * transfers you can stream reliably; avoid more than 64 msec per urb.
1358 * Also avoid queue depths of less than ehci's worst irq latency (affected
1359 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1360 * and other factors); or more than about 230 msec total (for portability,
1361 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1364 #define SCHEDULING_DELAY 40 /* microframes */
1367 iso_stream_schedule (
1368 struct ehci_hcd
*ehci
,
1370 struct ehci_iso_stream
*stream
1373 u32 now
, base
, next
, start
, period
, span
;
1375 unsigned mod
= ehci
->periodic_size
<< 3;
1376 struct ehci_iso_sched
*sched
= urb
->hcpriv
;
1378 period
= urb
->interval
;
1380 if (!stream
->highspeed
) {
1385 now
= ehci_read_frame_index(ehci
) & (mod
- 1);
1387 /* Typical case: reuse current schedule, stream is still active.
1388 * Hopefully there are no gaps from the host falling behind
1389 * (irq delays etc). If there are, the behavior depends on
1390 * whether URB_ISO_ASAP is set.
1392 if (likely (!list_empty (&stream
->td_list
))) {
1394 /* Take the isochronous scheduling threshold into account */
1396 next
= now
+ ehci
->i_thresh
; /* uframe cache */
1398 next
= (now
+ 2 + 7) & ~0x07; /* full frame cache */
1401 * Use ehci->last_iso_frame as the base. There can't be any
1402 * TDs scheduled for earlier than that.
1404 base
= ehci
->last_iso_frame
<< 3;
1405 next
= (next
- base
) & (mod
- 1);
1406 start
= (stream
->next_uframe
- base
) & (mod
- 1);
1408 /* Is the schedule already full? */
1409 if (unlikely(start
< period
)) {
1410 ehci_dbg(ehci
, "iso sched full %p (%u-%u < %u mod %u)\n",
1411 urb
, stream
->next_uframe
, base
,
1417 /* Behind the scheduling threshold? */
1418 if (unlikely(start
< next
)) {
1419 unsigned now2
= (now
- base
) & (mod
- 1);
1421 /* USB_ISO_ASAP: Round up to the first available slot */
1422 if (urb
->transfer_flags
& URB_ISO_ASAP
)
1423 start
+= (next
- start
+ period
- 1) & -period
;
1426 * Not ASAP: Use the next slot in the stream,
1429 else if (start
+ span
- period
< now2
) {
1430 ehci_dbg(ehci
, "iso underrun %p (%u+%u < %u)\n",
1432 span
- period
, now2
+ base
);
1439 /* need to schedule; when's the next (u)frame we could start?
1440 * this is bigger than ehci->i_thresh allows; scheduling itself
1441 * isn't free, the delay should handle reasonably slow cpus. it
1442 * can also help high bandwidth if the dma and irq loads don't
1443 * jump until after the queue is primed.
1449 start
= base
+ SCHEDULING_DELAY
;
1451 /* find a uframe slot with enough bandwidth.
1452 * Early uframes are more precious because full-speed
1453 * iso IN transfers can't use late uframes,
1454 * and therefore they should be allocated last.
1460 /* check schedule: enough space? */
1461 if (stream
->highspeed
) {
1462 if (itd_slot_ok(ehci
, mod
, start
,
1463 stream
->usecs
, period
))
1466 if ((start
% 8) >= 6)
1468 if (sitd_slot_ok(ehci
, mod
, stream
,
1469 start
, sched
, period
))
1472 } while (start
> next
&& !done
);
1474 /* no room in the schedule */
1476 ehci_dbg(ehci
, "iso sched full %p", urb
);
1482 /* Tried to schedule too far into the future? */
1483 if (unlikely(start
- base
+ span
- period
>= mod
)) {
1484 ehci_dbg(ehci
, "request %p would overflow (%u+%u >= %u)\n",
1485 urb
, start
- base
, span
- period
, mod
);
1490 stream
->next_uframe
= start
& (mod
- 1);
1492 /* report high speed start in uframes; full speed, in frames */
1493 urb
->start_frame
= stream
->next_uframe
;
1494 if (!stream
->highspeed
)
1495 urb
->start_frame
>>= 3;
1497 /* Make sure scan_isoc() sees these */
1498 if (ehci
->isoc_count
== 0)
1499 ehci
->last_iso_frame
= now
>> 3;
1503 iso_sched_free(stream
, sched
);
1508 /*-------------------------------------------------------------------------*/
1511 itd_init(struct ehci_hcd
*ehci
, struct ehci_iso_stream
*stream
,
1512 struct ehci_itd
*itd
)
1516 /* it's been recently zeroed */
1517 itd
->hw_next
= EHCI_LIST_END(ehci
);
1518 itd
->hw_bufp
[0] = stream
->buf0
;
1519 itd
->hw_bufp
[1] = stream
->buf1
;
1520 itd
->hw_bufp
[2] = stream
->buf2
;
1522 for (i
= 0; i
< 8; i
++)
1525 /* All other fields are filled when scheduling */
1530 struct ehci_hcd
*ehci
,
1531 struct ehci_itd
*itd
,
1532 struct ehci_iso_sched
*iso_sched
,
1537 struct ehci_iso_packet
*uf
= &iso_sched
->packet
[index
];
1538 unsigned pg
= itd
->pg
;
1540 // BUG_ON (pg == 6 && uf->cross);
1543 itd
->index
[uframe
] = index
;
1545 itd
->hw_transaction
[uframe
] = uf
->transaction
;
1546 itd
->hw_transaction
[uframe
] |= cpu_to_hc32(ehci
, pg
<< 12);
1547 itd
->hw_bufp
[pg
] |= cpu_to_hc32(ehci
, uf
->bufp
& ~(u32
)0);
1548 itd
->hw_bufp_hi
[pg
] |= cpu_to_hc32(ehci
, (u32
)(uf
->bufp
>> 32));
1550 /* iso_frame_desc[].offset must be strictly increasing */
1551 if (unlikely (uf
->cross
)) {
1552 u64 bufp
= uf
->bufp
+ 4096;
1555 itd
->hw_bufp
[pg
] |= cpu_to_hc32(ehci
, bufp
& ~(u32
)0);
1556 itd
->hw_bufp_hi
[pg
] |= cpu_to_hc32(ehci
, (u32
)(bufp
>> 32));
1561 itd_link (struct ehci_hcd
*ehci
, unsigned frame
, struct ehci_itd
*itd
)
1563 union ehci_shadow
*prev
= &ehci
->pshadow
[frame
];
1564 __hc32
*hw_p
= &ehci
->periodic
[frame
];
1565 union ehci_shadow here
= *prev
;
1568 /* skip any iso nodes which might belong to previous microframes */
1570 type
= Q_NEXT_TYPE(ehci
, *hw_p
);
1571 if (type
== cpu_to_hc32(ehci
, Q_TYPE_QH
))
1573 prev
= periodic_next_shadow(ehci
, prev
, type
);
1574 hw_p
= shadow_next_periodic(ehci
, &here
, type
);
1578 itd
->itd_next
= here
;
1579 itd
->hw_next
= *hw_p
;
1583 *hw_p
= cpu_to_hc32(ehci
, itd
->itd_dma
| Q_TYPE_ITD
);
1586 /* fit urb's itds into the selected schedule slot; activate as needed */
1587 static void itd_link_urb(
1588 struct ehci_hcd
*ehci
,
1591 struct ehci_iso_stream
*stream
1595 unsigned next_uframe
, uframe
, frame
;
1596 struct ehci_iso_sched
*iso_sched
= urb
->hcpriv
;
1597 struct ehci_itd
*itd
;
1599 next_uframe
= stream
->next_uframe
& (mod
- 1);
1601 if (unlikely (list_empty(&stream
->td_list
)))
1602 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
1603 += stream
->bandwidth
;
1605 if (ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
== 0) {
1606 if (ehci
->amd_pll_fix
== 1)
1607 usb_amd_quirk_pll_disable();
1610 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
++;
1612 /* fill iTDs uframe by uframe */
1613 for (packet
= 0, itd
= NULL
; packet
< urb
->number_of_packets
; ) {
1615 /* ASSERT: we have all necessary itds */
1616 // BUG_ON (list_empty (&iso_sched->td_list));
1618 /* ASSERT: no itds for this endpoint in this uframe */
1620 itd
= list_entry (iso_sched
->td_list
.next
,
1621 struct ehci_itd
, itd_list
);
1622 list_move_tail (&itd
->itd_list
, &stream
->td_list
);
1623 itd
->stream
= stream
;
1625 itd_init (ehci
, stream
, itd
);
1628 uframe
= next_uframe
& 0x07;
1629 frame
= next_uframe
>> 3;
1631 itd_patch(ehci
, itd
, iso_sched
, packet
, uframe
);
1633 next_uframe
+= stream
->interval
;
1634 next_uframe
&= mod
- 1;
1637 /* link completed itds into the schedule */
1638 if (((next_uframe
>> 3) != frame
)
1639 || packet
== urb
->number_of_packets
) {
1640 itd_link(ehci
, frame
& (ehci
->periodic_size
- 1), itd
);
1644 stream
->next_uframe
= next_uframe
;
1646 /* don't need that schedule data any more */
1647 iso_sched_free (stream
, iso_sched
);
1648 urb
->hcpriv
= stream
;
1651 enable_periodic(ehci
);
1654 #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1656 /* Process and recycle a completed ITD. Return true iff its urb completed,
1657 * and hence its completion callback probably added things to the hardware
1660 * Note that we carefully avoid recycling this descriptor until after any
1661 * completion callback runs, so that it won't be reused quickly. That is,
1662 * assuming (a) no more than two urbs per frame on this endpoint, and also
1663 * (b) only this endpoint's completions submit URBs. It seems some silicon
1664 * corrupts things if you reuse completed descriptors very quickly...
1666 static bool itd_complete(struct ehci_hcd
*ehci
, struct ehci_itd
*itd
)
1668 struct urb
*urb
= itd
->urb
;
1669 struct usb_iso_packet_descriptor
*desc
;
1673 struct ehci_iso_stream
*stream
= itd
->stream
;
1674 struct usb_device
*dev
;
1675 bool retval
= false;
1677 /* for each uframe with a packet */
1678 for (uframe
= 0; uframe
< 8; uframe
++) {
1679 if (likely (itd
->index
[uframe
] == -1))
1681 urb_index
= itd
->index
[uframe
];
1682 desc
= &urb
->iso_frame_desc
[urb_index
];
1684 t
= hc32_to_cpup(ehci
, &itd
->hw_transaction
[uframe
]);
1685 itd
->hw_transaction
[uframe
] = 0;
1687 /* report transfer status */
1688 if (unlikely (t
& ISO_ERRS
)) {
1690 if (t
& EHCI_ISOC_BUF_ERR
)
1691 desc
->status
= usb_pipein (urb
->pipe
)
1692 ? -ENOSR
/* hc couldn't read */
1693 : -ECOMM
; /* hc couldn't write */
1694 else if (t
& EHCI_ISOC_BABBLE
)
1695 desc
->status
= -EOVERFLOW
;
1696 else /* (t & EHCI_ISOC_XACTERR) */
1697 desc
->status
= -EPROTO
;
1699 /* HC need not update length with this error */
1700 if (!(t
& EHCI_ISOC_BABBLE
)) {
1701 desc
->actual_length
= EHCI_ITD_LENGTH(t
);
1702 urb
->actual_length
+= desc
->actual_length
;
1704 } else if (likely ((t
& EHCI_ISOC_ACTIVE
) == 0)) {
1706 desc
->actual_length
= EHCI_ITD_LENGTH(t
);
1707 urb
->actual_length
+= desc
->actual_length
;
1709 /* URB was too late */
1714 /* handle completion now? */
1715 if (likely ((urb_index
+ 1) != urb
->number_of_packets
))
1718 /* ASSERT: it's really the last itd for this urb
1719 list_for_each_entry (itd, &stream->td_list, itd_list)
1720 BUG_ON (itd->urb == urb);
1723 /* give urb back to the driver; completion often (re)submits */
1725 ehci_urb_done(ehci
, urb
, 0);
1730 disable_periodic(ehci
);
1732 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
--;
1733 if (ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
== 0) {
1734 if (ehci
->amd_pll_fix
== 1)
1735 usb_amd_quirk_pll_enable();
1738 if (unlikely(list_is_singular(&stream
->td_list
)))
1739 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
1740 -= stream
->bandwidth
;
1745 /* Add to the end of the free list for later reuse */
1746 list_move_tail(&itd
->itd_list
, &stream
->free_list
);
1748 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
1749 if (list_empty(&stream
->td_list
)) {
1750 list_splice_tail_init(&stream
->free_list
,
1751 &ehci
->cached_itd_list
);
1752 start_free_itds(ehci
);
1758 /*-------------------------------------------------------------------------*/
1760 static int itd_submit (struct ehci_hcd
*ehci
, struct urb
*urb
,
1763 int status
= -EINVAL
;
1764 unsigned long flags
;
1765 struct ehci_iso_stream
*stream
;
1767 /* Get iso_stream head */
1768 stream
= iso_stream_find (ehci
, urb
);
1769 if (unlikely (stream
== NULL
)) {
1770 ehci_dbg (ehci
, "can't get iso stream\n");
1773 if (unlikely (urb
->interval
!= stream
->interval
)) {
1774 ehci_dbg (ehci
, "can't change iso interval %d --> %d\n",
1775 stream
->interval
, urb
->interval
);
1779 #ifdef EHCI_URB_TRACE
1781 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
1782 __func__
, urb
->dev
->devpath
, urb
,
1783 usb_pipeendpoint (urb
->pipe
),
1784 usb_pipein (urb
->pipe
) ? "in" : "out",
1785 urb
->transfer_buffer_length
,
1786 urb
->number_of_packets
, urb
->interval
,
1790 /* allocate ITDs w/o locking anything */
1791 status
= itd_urb_transaction (stream
, ehci
, urb
, mem_flags
);
1792 if (unlikely (status
< 0)) {
1793 ehci_dbg (ehci
, "can't init itds\n");
1797 /* schedule ... need to lock */
1798 spin_lock_irqsave (&ehci
->lock
, flags
);
1799 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci
)))) {
1800 status
= -ESHUTDOWN
;
1801 goto done_not_linked
;
1803 status
= usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci
), urb
);
1804 if (unlikely(status
))
1805 goto done_not_linked
;
1806 status
= iso_stream_schedule(ehci
, urb
, stream
);
1807 if (likely (status
== 0))
1808 itd_link_urb (ehci
, urb
, ehci
->periodic_size
<< 3, stream
);
1810 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci
), urb
);
1812 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1817 /*-------------------------------------------------------------------------*/
1820 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1821 * TTs in USB 2.0 hubs. These need microframe scheduling.
1826 struct ehci_hcd
*ehci
,
1827 struct ehci_iso_sched
*iso_sched
,
1828 struct ehci_iso_stream
*stream
,
1833 dma_addr_t dma
= urb
->transfer_dma
;
1835 /* how many frames are needed for these transfers */
1836 iso_sched
->span
= urb
->number_of_packets
* stream
->interval
;
1838 /* figure out per-frame sitd fields that we'll need later
1839 * when we fit new sitds into the schedule.
1841 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1842 struct ehci_iso_packet
*packet
= &iso_sched
->packet
[i
];
1847 length
= urb
->iso_frame_desc
[i
].length
& 0x03ff;
1848 buf
= dma
+ urb
->iso_frame_desc
[i
].offset
;
1850 trans
= SITD_STS_ACTIVE
;
1851 if (((i
+ 1) == urb
->number_of_packets
)
1852 && !(urb
->transfer_flags
& URB_NO_INTERRUPT
))
1854 trans
|= length
<< 16;
1855 packet
->transaction
= cpu_to_hc32(ehci
, trans
);
1857 /* might need to cross a buffer page within a td */
1859 packet
->buf1
= (buf
+ length
) & ~0x0fff;
1860 if (packet
->buf1
!= (buf
& ~(u64
)0x0fff))
1863 /* OUT uses multiple start-splits */
1864 if (stream
->bEndpointAddress
& USB_DIR_IN
)
1866 length
= (length
+ 187) / 188;
1867 if (length
> 1) /* BEGIN vs ALL */
1869 packet
->buf1
|= length
;
1874 sitd_urb_transaction (
1875 struct ehci_iso_stream
*stream
,
1876 struct ehci_hcd
*ehci
,
1881 struct ehci_sitd
*sitd
;
1882 dma_addr_t sitd_dma
;
1884 struct ehci_iso_sched
*iso_sched
;
1885 unsigned long flags
;
1887 iso_sched
= iso_sched_alloc (urb
->number_of_packets
, mem_flags
);
1888 if (iso_sched
== NULL
)
1891 sitd_sched_init(ehci
, iso_sched
, stream
, urb
);
1893 /* allocate/init sITDs */
1894 spin_lock_irqsave (&ehci
->lock
, flags
);
1895 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1897 /* NOTE: for now, we don't try to handle wraparound cases
1898 * for IN (using sitd->hw_backpointer, like a FSTN), which
1899 * means we never need two sitds for full speed packets.
1903 * Use siTDs from the free list, but not siTDs that may
1904 * still be in use by the hardware.
1906 if (likely(!list_empty(&stream
->free_list
))) {
1907 sitd
= list_first_entry(&stream
->free_list
,
1908 struct ehci_sitd
, sitd_list
);
1909 if (sitd
->frame
== ehci
->now_frame
)
1911 list_del (&sitd
->sitd_list
);
1912 sitd_dma
= sitd
->sitd_dma
;
1915 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1916 sitd
= dma_pool_alloc (ehci
->sitd_pool
, mem_flags
,
1918 spin_lock_irqsave (&ehci
->lock
, flags
);
1920 iso_sched_free(stream
, iso_sched
);
1921 spin_unlock_irqrestore(&ehci
->lock
, flags
);
1926 memset (sitd
, 0, sizeof *sitd
);
1927 sitd
->sitd_dma
= sitd_dma
;
1928 sitd
->frame
= 9999; /* an invalid value */
1929 list_add (&sitd
->sitd_list
, &iso_sched
->td_list
);
1932 /* temporarily store schedule info in hcpriv */
1933 urb
->hcpriv
= iso_sched
;
1934 urb
->error_count
= 0;
1936 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1940 /*-------------------------------------------------------------------------*/
1944 struct ehci_hcd
*ehci
,
1945 struct ehci_iso_stream
*stream
,
1946 struct ehci_sitd
*sitd
,
1947 struct ehci_iso_sched
*iso_sched
,
1951 struct ehci_iso_packet
*uf
= &iso_sched
->packet
[index
];
1952 u64 bufp
= uf
->bufp
;
1954 sitd
->hw_next
= EHCI_LIST_END(ehci
);
1955 sitd
->hw_fullspeed_ep
= stream
->address
;
1956 sitd
->hw_uframe
= stream
->splits
;
1957 sitd
->hw_results
= uf
->transaction
;
1958 sitd
->hw_backpointer
= EHCI_LIST_END(ehci
);
1961 sitd
->hw_buf
[0] = cpu_to_hc32(ehci
, bufp
);
1962 sitd
->hw_buf_hi
[0] = cpu_to_hc32(ehci
, bufp
>> 32);
1964 sitd
->hw_buf
[1] = cpu_to_hc32(ehci
, uf
->buf1
);
1967 sitd
->hw_buf_hi
[1] = cpu_to_hc32(ehci
, bufp
>> 32);
1968 sitd
->index
= index
;
1972 sitd_link (struct ehci_hcd
*ehci
, unsigned frame
, struct ehci_sitd
*sitd
)
1974 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1975 sitd
->sitd_next
= ehci
->pshadow
[frame
];
1976 sitd
->hw_next
= ehci
->periodic
[frame
];
1977 ehci
->pshadow
[frame
].sitd
= sitd
;
1978 sitd
->frame
= frame
;
1980 ehci
->periodic
[frame
] = cpu_to_hc32(ehci
, sitd
->sitd_dma
| Q_TYPE_SITD
);
1983 /* fit urb's sitds into the selected schedule slot; activate as needed */
1984 static void sitd_link_urb(
1985 struct ehci_hcd
*ehci
,
1988 struct ehci_iso_stream
*stream
1992 unsigned next_uframe
;
1993 struct ehci_iso_sched
*sched
= urb
->hcpriv
;
1994 struct ehci_sitd
*sitd
;
1996 next_uframe
= stream
->next_uframe
;
1998 if (list_empty(&stream
->td_list
))
1999 /* usbfs ignores TT bandwidth */
2000 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
2001 += stream
->bandwidth
;
2003 if (ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
== 0) {
2004 if (ehci
->amd_pll_fix
== 1)
2005 usb_amd_quirk_pll_disable();
2008 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
++;
2010 /* fill sITDs frame by frame */
2011 for (packet
= 0, sitd
= NULL
;
2012 packet
< urb
->number_of_packets
;
2015 /* ASSERT: we have all necessary sitds */
2016 BUG_ON (list_empty (&sched
->td_list
));
2018 /* ASSERT: no itds for this endpoint in this frame */
2020 sitd
= list_entry (sched
->td_list
.next
,
2021 struct ehci_sitd
, sitd_list
);
2022 list_move_tail (&sitd
->sitd_list
, &stream
->td_list
);
2023 sitd
->stream
= stream
;
2026 sitd_patch(ehci
, stream
, sitd
, sched
, packet
);
2027 sitd_link(ehci
, (next_uframe
>> 3) & (ehci
->periodic_size
- 1),
2030 next_uframe
+= stream
->interval
<< 3;
2032 stream
->next_uframe
= next_uframe
& (mod
- 1);
2034 /* don't need that schedule data any more */
2035 iso_sched_free (stream
, sched
);
2036 urb
->hcpriv
= stream
;
2039 enable_periodic(ehci
);
2042 /*-------------------------------------------------------------------------*/
2044 #define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
2045 | SITD_STS_XACT | SITD_STS_MMF)
2047 /* Process and recycle a completed SITD. Return true iff its urb completed,
2048 * and hence its completion callback probably added things to the hardware
2051 * Note that we carefully avoid recycling this descriptor until after any
2052 * completion callback runs, so that it won't be reused quickly. That is,
2053 * assuming (a) no more than two urbs per frame on this endpoint, and also
2054 * (b) only this endpoint's completions submit URBs. It seems some silicon
2055 * corrupts things if you reuse completed descriptors very quickly...
2057 static bool sitd_complete(struct ehci_hcd
*ehci
, struct ehci_sitd
*sitd
)
2059 struct urb
*urb
= sitd
->urb
;
2060 struct usb_iso_packet_descriptor
*desc
;
2063 struct ehci_iso_stream
*stream
= sitd
->stream
;
2064 struct usb_device
*dev
;
2065 bool retval
= false;
2067 urb_index
= sitd
->index
;
2068 desc
= &urb
->iso_frame_desc
[urb_index
];
2069 t
= hc32_to_cpup(ehci
, &sitd
->hw_results
);
2071 /* report transfer status */
2072 if (unlikely(t
& SITD_ERRS
)) {
2074 if (t
& SITD_STS_DBE
)
2075 desc
->status
= usb_pipein (urb
->pipe
)
2076 ? -ENOSR
/* hc couldn't read */
2077 : -ECOMM
; /* hc couldn't write */
2078 else if (t
& SITD_STS_BABBLE
)
2079 desc
->status
= -EOVERFLOW
;
2080 else /* XACT, MMF, etc */
2081 desc
->status
= -EPROTO
;
2082 } else if (unlikely(t
& SITD_STS_ACTIVE
)) {
2083 /* URB was too late */
2087 desc
->actual_length
= desc
->length
- SITD_LENGTH(t
);
2088 urb
->actual_length
+= desc
->actual_length
;
2091 /* handle completion now? */
2092 if ((urb_index
+ 1) != urb
->number_of_packets
)
2095 /* ASSERT: it's really the last sitd for this urb
2096 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2097 BUG_ON (sitd->urb == urb);
2100 /* give urb back to the driver; completion often (re)submits */
2102 ehci_urb_done(ehci
, urb
, 0);
2107 disable_periodic(ehci
);
2109 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
--;
2110 if (ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
== 0) {
2111 if (ehci
->amd_pll_fix
== 1)
2112 usb_amd_quirk_pll_enable();
2115 if (list_is_singular(&stream
->td_list
))
2116 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
2117 -= stream
->bandwidth
;
2122 /* Add to the end of the free list for later reuse */
2123 list_move_tail(&sitd
->sitd_list
, &stream
->free_list
);
2125 /* Recycle the siTDs when the pipeline is empty (ep no longer in use) */
2126 if (list_empty(&stream
->td_list
)) {
2127 list_splice_tail_init(&stream
->free_list
,
2128 &ehci
->cached_sitd_list
);
2129 start_free_itds(ehci
);
2136 static int sitd_submit (struct ehci_hcd
*ehci
, struct urb
*urb
,
2139 int status
= -EINVAL
;
2140 unsigned long flags
;
2141 struct ehci_iso_stream
*stream
;
2143 /* Get iso_stream head */
2144 stream
= iso_stream_find (ehci
, urb
);
2145 if (stream
== NULL
) {
2146 ehci_dbg (ehci
, "can't get iso stream\n");
2149 if (urb
->interval
!= stream
->interval
) {
2150 ehci_dbg (ehci
, "can't change iso interval %d --> %d\n",
2151 stream
->interval
, urb
->interval
);
2155 #ifdef EHCI_URB_TRACE
2157 "submit %p dev%s ep%d%s-iso len %d\n",
2158 urb
, urb
->dev
->devpath
,
2159 usb_pipeendpoint (urb
->pipe
),
2160 usb_pipein (urb
->pipe
) ? "in" : "out",
2161 urb
->transfer_buffer_length
);
2164 /* allocate SITDs */
2165 status
= sitd_urb_transaction (stream
, ehci
, urb
, mem_flags
);
2167 ehci_dbg (ehci
, "can't init sitds\n");
2171 /* schedule ... need to lock */
2172 spin_lock_irqsave (&ehci
->lock
, flags
);
2173 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci
)))) {
2174 status
= -ESHUTDOWN
;
2175 goto done_not_linked
;
2177 status
= usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci
), urb
);
2178 if (unlikely(status
))
2179 goto done_not_linked
;
2180 status
= iso_stream_schedule(ehci
, urb
, stream
);
2182 sitd_link_urb (ehci
, urb
, ehci
->periodic_size
<< 3, stream
);
2184 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci
), urb
);
2186 spin_unlock_irqrestore (&ehci
->lock
, flags
);
2191 /*-------------------------------------------------------------------------*/
2193 static void scan_isoc(struct ehci_hcd
*ehci
)
2195 unsigned uf
, now_frame
, frame
;
2196 unsigned fmask
= ehci
->periodic_size
- 1;
2197 bool modified
, live
;
2200 * When running, scan from last scan point up to "now"
2201 * else clean up by scanning everything that's left.
2202 * Touches as few pages as possible: cache-friendly.
2204 if (ehci
->rh_state
>= EHCI_RH_RUNNING
) {
2205 uf
= ehci_read_frame_index(ehci
);
2206 now_frame
= (uf
>> 3) & fmask
;
2209 now_frame
= (ehci
->last_iso_frame
- 1) & fmask
;
2212 ehci
->now_frame
= now_frame
;
2214 frame
= ehci
->last_iso_frame
;
2216 union ehci_shadow q
, *q_p
;
2220 /* scan each element in frame's queue for completions */
2221 q_p
= &ehci
->pshadow
[frame
];
2222 hw_p
= &ehci
->periodic
[frame
];
2224 type
= Q_NEXT_TYPE(ehci
, *hw_p
);
2227 while (q
.ptr
!= NULL
) {
2228 switch (hc32_to_cpu(ehci
, type
)) {
2230 /* If this ITD is still active, leave it for
2231 * later processing ... check the next entry.
2232 * No need to check for activity unless the
2235 if (frame
== now_frame
&& live
) {
2237 for (uf
= 0; uf
< 8; uf
++) {
2238 if (q
.itd
->hw_transaction
[uf
] &
2243 q_p
= &q
.itd
->itd_next
;
2244 hw_p
= &q
.itd
->hw_next
;
2245 type
= Q_NEXT_TYPE(ehci
,
2252 /* Take finished ITDs out of the schedule
2253 * and process them: recycle, maybe report
2254 * URB completion. HC won't cache the
2255 * pointer for much longer, if at all.
2257 *q_p
= q
.itd
->itd_next
;
2258 if (!ehci
->use_dummy_qh
||
2259 q
.itd
->hw_next
!= EHCI_LIST_END(ehci
))
2260 *hw_p
= q
.itd
->hw_next
;
2262 *hw_p
= ehci
->dummy
->qh_dma
;
2263 type
= Q_NEXT_TYPE(ehci
, q
.itd
->hw_next
);
2265 modified
= itd_complete (ehci
, q
.itd
);
2269 /* If this SITD is still active, leave it for
2270 * later processing ... check the next entry.
2271 * No need to check for activity unless the
2274 if (((frame
== now_frame
) ||
2275 (((frame
+ 1) & fmask
) == now_frame
))
2277 && (q
.sitd
->hw_results
&
2278 SITD_ACTIVE(ehci
))) {
2280 q_p
= &q
.sitd
->sitd_next
;
2281 hw_p
= &q
.sitd
->hw_next
;
2282 type
= Q_NEXT_TYPE(ehci
,
2288 /* Take finished SITDs out of the schedule
2289 * and process them: recycle, maybe report
2292 *q_p
= q
.sitd
->sitd_next
;
2293 if (!ehci
->use_dummy_qh
||
2294 q
.sitd
->hw_next
!= EHCI_LIST_END(ehci
))
2295 *hw_p
= q
.sitd
->hw_next
;
2297 *hw_p
= ehci
->dummy
->qh_dma
;
2298 type
= Q_NEXT_TYPE(ehci
, q
.sitd
->hw_next
);
2300 modified
= sitd_complete (ehci
, q
.sitd
);
2304 ehci_dbg(ehci
, "corrupt type %d frame %d shadow %p\n",
2305 type
, frame
, q
.ptr
);
2310 /* End of the iTDs and siTDs */
2315 /* assume completion callbacks modify the queue */
2316 if (unlikely(modified
&& ehci
->isoc_count
> 0))
2320 /* Stop when we have reached the current frame */
2321 if (frame
== now_frame
)
2324 /* The last frame may still have active siTDs */
2325 ehci
->last_iso_frame
= frame
;
2326 frame
= (frame
+ 1) & fmask
;