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 usecs
+= q
->itd
->usecs
[uframe
];
123 hw_p
= &q
->itd
->hw_next
;
124 q
= &q
->itd
->itd_next
;
127 /* is it in the S-mask? (count SPLIT, DATA) */
128 if (q
->sitd
->hw_uframe
& cpu_to_hc32(ehci
,
130 if (q
->sitd
->hw_fullspeed_ep
&
131 cpu_to_hc32(ehci
, 1<<31))
132 usecs
+= q
->sitd
->stream
->usecs
;
133 else /* worst case for OUT start-split */
134 usecs
+= HS_USECS_ISO (188);
137 /* ... C-mask? (count CSPLIT, DATA) */
138 if (q
->sitd
->hw_uframe
&
139 cpu_to_hc32(ehci
, 1 << (8 + uframe
))) {
140 /* worst case for IN complete-split */
141 usecs
+= q
->sitd
->stream
->c_usecs
;
144 hw_p
= &q
->sitd
->hw_next
;
145 q
= &q
->sitd
->sitd_next
;
151 ehci_err (ehci
, "uframe %d sched overrun: %d usecs\n",
152 frame
* 8 + uframe
, usecs
);
157 /*-------------------------------------------------------------------------*/
159 static int same_tt (struct usb_device
*dev1
, struct usb_device
*dev2
)
161 if (!dev1
->tt
|| !dev2
->tt
)
163 if (dev1
->tt
!= dev2
->tt
)
166 return dev1
->ttport
== dev2
->ttport
;
171 #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
173 /* Which uframe does the low/fullspeed transfer start in?
175 * The parameter is the mask of ssplits in "H-frame" terms
176 * and this returns the transfer start uframe in "B-frame" terms,
177 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
178 * will cause a transfer in "B-frame" uframe 0. "B-frames" lag
179 * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7.
181 static inline unsigned char tt_start_uframe(struct ehci_hcd
*ehci
, __hc32 mask
)
183 unsigned char smask
= QH_SMASK
& hc32_to_cpu(ehci
, mask
);
185 ehci_err(ehci
, "invalid empty smask!\n");
186 /* uframe 7 can't have bw so this will indicate failure */
189 return ffs(smask
) - 1;
192 static const unsigned char
193 max_tt_usecs
[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
195 /* carryover low/fullspeed bandwidth that crosses uframe boundries */
196 static inline void carryover_tt_bandwidth(unsigned short tt_usecs
[8])
199 for (i
=0; i
<7; i
++) {
200 if (max_tt_usecs
[i
] < tt_usecs
[i
]) {
201 tt_usecs
[i
+1] += tt_usecs
[i
] - max_tt_usecs
[i
];
202 tt_usecs
[i
] = max_tt_usecs
[i
];
207 /* How many of the tt's periodic downstream 1000 usecs are allocated?
209 * While this measures the bandwidth in terms of usecs/uframe,
210 * the low/fullspeed bus has no notion of uframes, so any particular
211 * low/fullspeed transfer can "carry over" from one uframe to the next,
212 * since the TT just performs downstream transfers in sequence.
214 * For example two seperate 100 usec transfers can start in the same uframe,
215 * and the second one would "carry over" 75 usecs into the next uframe.
219 struct ehci_hcd
*ehci
,
220 struct usb_device
*dev
,
222 unsigned short tt_usecs
[8]
225 __hc32
*hw_p
= &ehci
->periodic
[frame
];
226 union ehci_shadow
*q
= &ehci
->pshadow
[frame
];
229 memset(tt_usecs
, 0, 16);
232 switch (hc32_to_cpu(ehci
, Q_NEXT_TYPE(ehci
, *hw_p
))) {
234 hw_p
= &q
->itd
->hw_next
;
235 q
= &q
->itd
->itd_next
;
238 if (same_tt(dev
, q
->qh
->dev
)) {
239 uf
= tt_start_uframe(ehci
, q
->qh
->hw_info2
);
240 tt_usecs
[uf
] += q
->qh
->tt_usecs
;
242 hw_p
= &q
->qh
->hw_next
;
246 if (same_tt(dev
, q
->sitd
->urb
->dev
)) {
247 uf
= tt_start_uframe(ehci
, q
->sitd
->hw_uframe
);
248 tt_usecs
[uf
] += q
->sitd
->stream
->tt_usecs
;
250 hw_p
= &q
->sitd
->hw_next
;
251 q
= &q
->sitd
->sitd_next
;
255 ehci_dbg(ehci
, "ignoring periodic frame %d FSTN\n",
257 hw_p
= &q
->fstn
->hw_next
;
258 q
= &q
->fstn
->fstn_next
;
262 carryover_tt_bandwidth(tt_usecs
);
264 if (max_tt_usecs
[7] < tt_usecs
[7])
265 ehci_err(ehci
, "frame %d tt sched overrun: %d usecs\n",
266 frame
, tt_usecs
[7] - max_tt_usecs
[7]);
270 * Return true if the device's tt's downstream bus is available for a
271 * periodic transfer of the specified length (usecs), starting at the
272 * specified frame/uframe. Note that (as summarized in section 11.19
273 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
276 * The uframe parameter is when the fullspeed/lowspeed transfer
277 * should be executed in "B-frame" terms, which is the same as the
278 * highspeed ssplit's uframe (which is in "H-frame" terms). For example
279 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
280 * See the EHCI spec sec 4.5 and fig 4.7.
282 * This checks if the full/lowspeed bus, at the specified starting uframe,
283 * has the specified bandwidth available, according to rules listed
284 * in USB 2.0 spec section 11.18.1 fig 11-60.
286 * This does not check if the transfer would exceed the max ssplit
287 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
288 * since proper scheduling limits ssplits to less than 16 per uframe.
290 static int tt_available (
291 struct ehci_hcd
*ehci
,
293 struct usb_device
*dev
,
299 if ((period
== 0) || (uframe
>= 7)) /* error */
302 for (; frame
< ehci
->periodic_size
; frame
+= period
) {
303 unsigned short tt_usecs
[8];
305 periodic_tt_usecs (ehci
, dev
, frame
, tt_usecs
);
307 ehci_vdbg(ehci
, "tt frame %d check %d usecs start uframe %d in"
308 " schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
309 frame
, usecs
, uframe
,
310 tt_usecs
[0], tt_usecs
[1], tt_usecs
[2], tt_usecs
[3],
311 tt_usecs
[4], tt_usecs
[5], tt_usecs
[6], tt_usecs
[7]);
313 if (max_tt_usecs
[uframe
] <= tt_usecs
[uframe
]) {
314 ehci_vdbg(ehci
, "frame %d uframe %d fully scheduled\n",
319 /* special case for isoc transfers larger than 125us:
320 * the first and each subsequent fully used uframe
321 * must be empty, so as to not illegally delay
322 * already scheduled transactions
325 int ufs
= (usecs
/ 125) - 1;
327 for (i
= uframe
; i
< (uframe
+ ufs
) && i
< 8; i
++)
328 if (0 < tt_usecs
[i
]) {
330 "multi-uframe xfer can't fit "
331 "in frame %d uframe %d\n",
337 tt_usecs
[uframe
] += usecs
;
339 carryover_tt_bandwidth(tt_usecs
);
341 /* fail if the carryover pushed bw past the last uframe's limit */
342 if (max_tt_usecs
[7] < tt_usecs
[7]) {
344 "tt unavailable usecs %d frame %d uframe %d\n",
345 usecs
, frame
, uframe
);
355 /* return true iff the device's transaction translator is available
356 * for a periodic transfer starting at the specified frame, using
357 * all the uframes in the mask.
359 static int tt_no_collision (
360 struct ehci_hcd
*ehci
,
362 struct usb_device
*dev
,
367 if (period
== 0) /* error */
370 /* note bandwidth wastage: split never follows csplit
371 * (different dev or endpoint) until the next uframe.
372 * calling convention doesn't make that distinction.
374 for (; frame
< ehci
->periodic_size
; frame
+= period
) {
375 union ehci_shadow here
;
378 here
= ehci
->pshadow
[frame
];
379 type
= Q_NEXT_TYPE(ehci
, ehci
->periodic
[frame
]);
381 switch (hc32_to_cpu(ehci
, type
)) {
383 type
= Q_NEXT_TYPE(ehci
, here
.itd
->hw_next
);
384 here
= here
.itd
->itd_next
;
387 if (same_tt (dev
, here
.qh
->dev
)) {
390 mask
= hc32_to_cpu(ehci
,
392 /* "knows" no gap is needed */
397 type
= Q_NEXT_TYPE(ehci
, here
.qh
->hw_next
);
398 here
= here
.qh
->qh_next
;
401 if (same_tt (dev
, here
.sitd
->urb
->dev
)) {
404 mask
= hc32_to_cpu(ehci
, here
.sitd
406 /* FIXME assumes no gap for IN! */
411 type
= Q_NEXT_TYPE(ehci
, here
.sitd
->hw_next
);
412 here
= here
.sitd
->sitd_next
;
417 "periodic frame %d bogus type %d\n",
421 /* collision or error */
430 #endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
432 /*-------------------------------------------------------------------------*/
434 static int enable_periodic (struct ehci_hcd
*ehci
)
439 /* did clearing PSE did take effect yet?
440 * takes effect only at frame boundaries...
442 status
= handshake(ehci
, &ehci
->regs
->status
, STS_PSS
, 0, 9 * 125);
444 ehci_to_hcd(ehci
)->state
= HC_STATE_HALT
;
448 cmd
= ehci_readl(ehci
, &ehci
->regs
->command
) | CMD_PSE
;
449 ehci_writel(ehci
, cmd
, &ehci
->regs
->command
);
450 /* posted write ... PSS happens later */
451 ehci_to_hcd(ehci
)->state
= HC_STATE_RUNNING
;
453 /* make sure ehci_work scans these */
454 ehci
->next_uframe
= ehci_readl(ehci
, &ehci
->regs
->frame_index
)
455 % (ehci
->periodic_size
<< 3);
459 static int disable_periodic (struct ehci_hcd
*ehci
)
464 /* did setting PSE not take effect yet?
465 * takes effect only at frame boundaries...
467 status
= handshake(ehci
, &ehci
->regs
->status
, STS_PSS
, STS_PSS
, 9 * 125);
469 ehci_to_hcd(ehci
)->state
= HC_STATE_HALT
;
473 cmd
= ehci_readl(ehci
, &ehci
->regs
->command
) & ~CMD_PSE
;
474 ehci_writel(ehci
, cmd
, &ehci
->regs
->command
);
475 /* posted write ... */
477 ehci
->next_uframe
= -1;
481 /*-------------------------------------------------------------------------*/
483 /* periodic schedule slots have iso tds (normal or split) first, then a
484 * sparse tree for active interrupt transfers.
486 * this just links in a qh; caller guarantees uframe masks are set right.
487 * no FSTN support (yet; ehci 0.96+)
489 static int qh_link_periodic (struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
492 unsigned period
= qh
->period
;
494 dev_dbg (&qh
->dev
->dev
,
495 "link qh%d-%04x/%p start %d [%d/%d us]\n",
496 period
, hc32_to_cpup(ehci
, &qh
->hw_info2
) & (QH_CMASK
| QH_SMASK
),
497 qh
, qh
->start
, qh
->usecs
, qh
->c_usecs
);
499 /* high bandwidth, or otherwise every microframe */
503 for (i
= qh
->start
; i
< ehci
->periodic_size
; i
+= period
) {
504 union ehci_shadow
*prev
= &ehci
->pshadow
[i
];
505 __hc32
*hw_p
= &ehci
->periodic
[i
];
506 union ehci_shadow here
= *prev
;
509 /* skip the iso nodes at list head */
511 type
= Q_NEXT_TYPE(ehci
, *hw_p
);
512 if (type
== cpu_to_hc32(ehci
, Q_TYPE_QH
))
514 prev
= periodic_next_shadow(ehci
, prev
, type
);
515 hw_p
= &here
.qh
->hw_next
;
519 /* sorting each branch by period (slow-->fast)
520 * enables sharing interior tree nodes
522 while (here
.ptr
&& qh
!= here
.qh
) {
523 if (qh
->period
> here
.qh
->period
)
525 prev
= &here
.qh
->qh_next
;
526 hw_p
= &here
.qh
->hw_next
;
529 /* link in this qh, unless some earlier pass did that */
536 *hw_p
= QH_NEXT (ehci
, qh
->qh_dma
);
539 qh
->qh_state
= QH_STATE_LINKED
;
542 /* update per-qh bandwidth for usbfs */
543 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
+= qh
->period
544 ? ((qh
->usecs
+ qh
->c_usecs
) / qh
->period
)
547 /* maybe enable periodic schedule processing */
548 if (!ehci
->periodic_sched
++)
549 return enable_periodic (ehci
);
554 static void qh_unlink_periodic (struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
560 // IF this isn't high speed
561 // and this qh is active in the current uframe
562 // (and overlay token SplitXstate is false?)
564 // qh->hw_info1 |= __constant_cpu_to_hc32(1 << 7 /* "ignore" */);
566 /* high bandwidth, or otherwise part of every microframe */
567 if ((period
= qh
->period
) == 0)
570 for (i
= qh
->start
; i
< ehci
->periodic_size
; i
+= period
)
571 periodic_unlink (ehci
, i
, qh
);
573 /* update per-qh bandwidth for usbfs */
574 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
-= qh
->period
575 ? ((qh
->usecs
+ qh
->c_usecs
) / qh
->period
)
578 dev_dbg (&qh
->dev
->dev
,
579 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
581 hc32_to_cpup(ehci
, &qh
->hw_info2
) & (QH_CMASK
| QH_SMASK
),
582 qh
, qh
->start
, qh
->usecs
, qh
->c_usecs
);
584 /* qh->qh_next still "live" to HC */
585 qh
->qh_state
= QH_STATE_UNLINK
;
586 qh
->qh_next
.ptr
= NULL
;
589 /* maybe turn off periodic schedule */
590 ehci
->periodic_sched
--;
591 if (!ehci
->periodic_sched
)
592 (void) 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
;
1189 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1190 itd
= dma_pool_alloc (ehci
->itd_pool
, mem_flags
,
1192 spin_lock_irqsave (&ehci
->lock
, flags
);
1195 if (unlikely (NULL
== itd
)) {
1196 iso_sched_free (stream
, sched
);
1197 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1200 memset (itd
, 0, sizeof *itd
);
1201 itd
->itd_dma
= itd_dma
;
1202 list_add (&itd
->itd_list
, &sched
->td_list
);
1204 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1206 /* temporarily store schedule info in hcpriv */
1207 urb
->hcpriv
= sched
;
1208 urb
->error_count
= 0;
1212 /*-------------------------------------------------------------------------*/
1216 struct ehci_hcd
*ehci
,
1225 /* can't commit more than 80% periodic == 100 usec */
1226 if (periodic_usecs (ehci
, uframe
>> 3, uframe
& 0x7)
1230 /* we know urb->interval is 2^N uframes */
1232 } while (uframe
< mod
);
1238 struct ehci_hcd
*ehci
,
1240 struct ehci_iso_stream
*stream
,
1242 struct ehci_iso_sched
*sched
,
1249 mask
= stream
->raw_mask
<< (uframe
& 7);
1251 /* for IN, don't wrap CSPLIT into the next frame */
1255 /* this multi-pass logic is simple, but performance may
1256 * suffer when the schedule data isn't cached.
1259 /* check bandwidth */
1260 uframe
%= period_uframes
;
1264 frame
= uframe
>> 3;
1267 #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1268 /* The tt's fullspeed bus bandwidth must be available.
1269 * tt_available scheduling guarantees 10+% for control/bulk.
1271 if (!tt_available (ehci
, period_uframes
<< 3,
1272 stream
->udev
, frame
, uf
, stream
->tt_usecs
))
1275 /* tt must be idle for start(s), any gap, and csplit.
1276 * assume scheduling slop leaves 10+% for control/bulk.
1278 if (!tt_no_collision (ehci
, period_uframes
<< 3,
1279 stream
->udev
, frame
, mask
))
1283 /* check starts (OUT uses more than one) */
1284 max_used
= 100 - stream
->usecs
;
1285 for (tmp
= stream
->raw_mask
& 0xff; tmp
; tmp
>>= 1, uf
++) {
1286 if (periodic_usecs (ehci
, frame
, uf
) > max_used
)
1290 /* for IN, check CSPLIT */
1291 if (stream
->c_usecs
) {
1293 max_used
= 100 - stream
->c_usecs
;
1297 if ((stream
->raw_mask
& tmp
) == 0)
1299 if (periodic_usecs (ehci
, frame
, uf
)
1305 /* we know urb->interval is 2^N uframes */
1306 uframe
+= period_uframes
;
1307 } while (uframe
< mod
);
1309 stream
->splits
= cpu_to_hc32(ehci
, stream
->raw_mask
<< (uframe
& 7));
1314 * This scheduler plans almost as far into the future as it has actual
1315 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1316 * "as small as possible" to be cache-friendlier.) That limits the size
1317 * transfers you can stream reliably; avoid more than 64 msec per urb.
1318 * Also avoid queue depths of less than ehci's worst irq latency (affected
1319 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1320 * and other factors); or more than about 230 msec total (for portability,
1321 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1324 #define SCHEDULE_SLOP 10 /* frames */
1327 iso_stream_schedule (
1328 struct ehci_hcd
*ehci
,
1330 struct ehci_iso_stream
*stream
1333 u32 now
, start
, max
, period
;
1335 unsigned mod
= ehci
->periodic_size
<< 3;
1336 struct ehci_iso_sched
*sched
= urb
->hcpriv
;
1338 if (sched
->span
> (mod
- 8 * SCHEDULE_SLOP
)) {
1339 ehci_dbg (ehci
, "iso request %p too long\n", urb
);
1344 if ((stream
->depth
+ sched
->span
) > mod
) {
1345 ehci_dbg (ehci
, "request %p would overflow (%d+%d>%d)\n",
1346 urb
, stream
->depth
, sched
->span
, mod
);
1351 now
= ehci_readl(ehci
, &ehci
->regs
->frame_index
) % mod
;
1353 /* when's the last uframe this urb could start? */
1356 /* typical case: reuse current schedule. stream is still active,
1357 * and no gaps from host falling behind (irq delays etc)
1359 if (likely (!list_empty (&stream
->td_list
))) {
1360 start
= stream
->next_uframe
;
1363 if (likely ((start
+ sched
->span
) < max
))
1365 /* else fell behind; someday, try to reschedule */
1370 /* need to schedule; when's the next (u)frame we could start?
1371 * this is bigger than ehci->i_thresh allows; scheduling itself
1372 * isn't free, the slop should handle reasonably slow cpus. it
1373 * can also help high bandwidth if the dma and irq loads don't
1374 * jump until after the queue is primed.
1376 start
= SCHEDULE_SLOP
* 8 + (now
& ~0x07);
1378 stream
->next_uframe
= start
;
1380 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
1382 period
= urb
->interval
;
1383 if (!stream
->highspeed
)
1386 /* find a uframe slot with enough bandwidth */
1387 for (; start
< (stream
->next_uframe
+ period
); start
++) {
1390 /* check schedule: enough space? */
1391 if (stream
->highspeed
)
1392 enough_space
= itd_slot_ok (ehci
, mod
, start
,
1393 stream
->usecs
, period
);
1395 if ((start
% 8) >= 6)
1397 enough_space
= sitd_slot_ok (ehci
, mod
, stream
,
1398 start
, sched
, period
);
1401 /* schedule it here if there's enough bandwidth */
1403 stream
->next_uframe
= start
% mod
;
1408 /* no room in the schedule */
1409 ehci_dbg (ehci
, "iso %ssched full %p (now %d max %d)\n",
1410 list_empty (&stream
->td_list
) ? "" : "re",
1415 iso_sched_free (stream
, sched
);
1420 /* report high speed start in uframes; full speed, in frames */
1421 urb
->start_frame
= stream
->next_uframe
;
1422 if (!stream
->highspeed
)
1423 urb
->start_frame
>>= 3;
1427 /*-------------------------------------------------------------------------*/
1430 itd_init(struct ehci_hcd
*ehci
, struct ehci_iso_stream
*stream
,
1431 struct ehci_itd
*itd
)
1435 /* it's been recently zeroed */
1436 itd
->hw_next
= EHCI_LIST_END(ehci
);
1437 itd
->hw_bufp
[0] = stream
->buf0
;
1438 itd
->hw_bufp
[1] = stream
->buf1
;
1439 itd
->hw_bufp
[2] = stream
->buf2
;
1441 for (i
= 0; i
< 8; i
++)
1444 /* All other fields are filled when scheduling */
1449 struct ehci_hcd
*ehci
,
1450 struct ehci_itd
*itd
,
1451 struct ehci_iso_sched
*iso_sched
,
1456 struct ehci_iso_packet
*uf
= &iso_sched
->packet
[index
];
1457 unsigned pg
= itd
->pg
;
1459 // BUG_ON (pg == 6 && uf->cross);
1462 itd
->index
[uframe
] = index
;
1464 itd
->hw_transaction
[uframe
] = uf
->transaction
;
1465 itd
->hw_transaction
[uframe
] |= cpu_to_hc32(ehci
, pg
<< 12);
1466 itd
->hw_bufp
[pg
] |= cpu_to_hc32(ehci
, uf
->bufp
& ~(u32
)0);
1467 itd
->hw_bufp_hi
[pg
] |= cpu_to_hc32(ehci
, (u32
)(uf
->bufp
>> 32));
1469 /* iso_frame_desc[].offset must be strictly increasing */
1470 if (unlikely (uf
->cross
)) {
1471 u64 bufp
= uf
->bufp
+ 4096;
1474 itd
->hw_bufp
[pg
] |= cpu_to_hc32(ehci
, bufp
& ~(u32
)0);
1475 itd
->hw_bufp_hi
[pg
] |= cpu_to_hc32(ehci
, (u32
)(bufp
>> 32));
1480 itd_link (struct ehci_hcd
*ehci
, unsigned frame
, struct ehci_itd
*itd
)
1482 /* always prepend ITD/SITD ... only QH tree is order-sensitive */
1483 itd
->itd_next
= ehci
->pshadow
[frame
];
1484 itd
->hw_next
= ehci
->periodic
[frame
];
1485 ehci
->pshadow
[frame
].itd
= itd
;
1488 ehci
->periodic
[frame
] = cpu_to_hc32(ehci
, itd
->itd_dma
| Q_TYPE_ITD
);
1491 /* fit urb's itds into the selected schedule slot; activate as needed */
1494 struct ehci_hcd
*ehci
,
1497 struct ehci_iso_stream
*stream
1501 unsigned next_uframe
, uframe
, frame
;
1502 struct ehci_iso_sched
*iso_sched
= urb
->hcpriv
;
1503 struct ehci_itd
*itd
;
1505 next_uframe
= stream
->next_uframe
% mod
;
1507 if (unlikely (list_empty(&stream
->td_list
))) {
1508 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
1509 += stream
->bandwidth
;
1511 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1512 urb
->dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
1513 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out",
1515 next_uframe
>> 3, next_uframe
& 0x7);
1516 stream
->start
= jiffies
;
1518 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
++;
1520 /* fill iTDs uframe by uframe */
1521 for (packet
= 0, itd
= NULL
; packet
< urb
->number_of_packets
; ) {
1523 /* ASSERT: we have all necessary itds */
1524 // BUG_ON (list_empty (&iso_sched->td_list));
1526 /* ASSERT: no itds for this endpoint in this uframe */
1528 itd
= list_entry (iso_sched
->td_list
.next
,
1529 struct ehci_itd
, itd_list
);
1530 list_move_tail (&itd
->itd_list
, &stream
->td_list
);
1531 itd
->stream
= iso_stream_get (stream
);
1532 itd
->urb
= usb_get_urb (urb
);
1533 itd_init (ehci
, stream
, itd
);
1536 uframe
= next_uframe
& 0x07;
1537 frame
= next_uframe
>> 3;
1539 itd
->usecs
[uframe
] = stream
->usecs
;
1540 itd_patch(ehci
, itd
, iso_sched
, packet
, uframe
);
1542 next_uframe
+= stream
->interval
;
1543 stream
->depth
+= stream
->interval
;
1547 /* link completed itds into the schedule */
1548 if (((next_uframe
>> 3) != frame
)
1549 || packet
== urb
->number_of_packets
) {
1550 itd_link (ehci
, frame
% ehci
->periodic_size
, itd
);
1554 stream
->next_uframe
= next_uframe
;
1556 /* don't need that schedule data any more */
1557 iso_sched_free (stream
, iso_sched
);
1560 timer_action (ehci
, TIMER_IO_WATCHDOG
);
1561 if (unlikely (!ehci
->periodic_sched
++))
1562 return enable_periodic (ehci
);
1566 #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1570 struct ehci_hcd
*ehci
,
1571 struct ehci_itd
*itd
1573 struct urb
*urb
= itd
->urb
;
1574 struct usb_iso_packet_descriptor
*desc
;
1578 struct ehci_iso_stream
*stream
= itd
->stream
;
1579 struct usb_device
*dev
;
1581 /* for each uframe with a packet */
1582 for (uframe
= 0; uframe
< 8; uframe
++) {
1583 if (likely (itd
->index
[uframe
] == -1))
1585 urb_index
= itd
->index
[uframe
];
1586 desc
= &urb
->iso_frame_desc
[urb_index
];
1588 t
= hc32_to_cpup(ehci
, &itd
->hw_transaction
[uframe
]);
1589 itd
->hw_transaction
[uframe
] = 0;
1590 stream
->depth
-= stream
->interval
;
1592 /* report transfer status */
1593 if (unlikely (t
& ISO_ERRS
)) {
1595 if (t
& EHCI_ISOC_BUF_ERR
)
1596 desc
->status
= usb_pipein (urb
->pipe
)
1597 ? -ENOSR
/* hc couldn't read */
1598 : -ECOMM
; /* hc couldn't write */
1599 else if (t
& EHCI_ISOC_BABBLE
)
1600 desc
->status
= -EOVERFLOW
;
1601 else /* (t & EHCI_ISOC_XACTERR) */
1602 desc
->status
= -EPROTO
;
1604 /* HC need not update length with this error */
1605 if (!(t
& EHCI_ISOC_BABBLE
))
1606 desc
->actual_length
= EHCI_ITD_LENGTH (t
);
1607 } else if (likely ((t
& EHCI_ISOC_ACTIVE
) == 0)) {
1609 desc
->actual_length
= EHCI_ITD_LENGTH (t
);
1616 list_move (&itd
->itd_list
, &stream
->free_list
);
1617 iso_stream_put (ehci
, stream
);
1619 /* handle completion now? */
1620 if (likely ((urb_index
+ 1) != urb
->number_of_packets
))
1623 /* ASSERT: it's really the last itd for this urb
1624 list_for_each_entry (itd, &stream->td_list, itd_list)
1625 BUG_ON (itd->urb == urb);
1628 /* give urb back to the driver ... can be out-of-order */
1630 ehci_urb_done(ehci
, urb
, 0);
1633 /* defer stopping schedule; completion can submit */
1634 ehci
->periodic_sched
--;
1635 if (unlikely (!ehci
->periodic_sched
))
1636 (void) disable_periodic (ehci
);
1637 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
--;
1639 if (unlikely (list_empty (&stream
->td_list
))) {
1640 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
1641 -= stream
->bandwidth
;
1643 "deschedule devp %s ep%d%s-iso\n",
1644 dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
1645 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out");
1647 iso_stream_put (ehci
, stream
);
1652 /*-------------------------------------------------------------------------*/
1654 static int itd_submit (struct ehci_hcd
*ehci
, struct urb
*urb
,
1657 int status
= -EINVAL
;
1658 unsigned long flags
;
1659 struct ehci_iso_stream
*stream
;
1661 /* Get iso_stream head */
1662 stream
= iso_stream_find (ehci
, urb
);
1663 if (unlikely (stream
== NULL
)) {
1664 ehci_dbg (ehci
, "can't get iso stream\n");
1667 if (unlikely (urb
->interval
!= stream
->interval
)) {
1668 ehci_dbg (ehci
, "can't change iso interval %d --> %d\n",
1669 stream
->interval
, urb
->interval
);
1673 #ifdef EHCI_URB_TRACE
1675 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
1676 __FUNCTION__
, urb
->dev
->devpath
, urb
,
1677 usb_pipeendpoint (urb
->pipe
),
1678 usb_pipein (urb
->pipe
) ? "in" : "out",
1679 urb
->transfer_buffer_length
,
1680 urb
->number_of_packets
, urb
->interval
,
1684 /* allocate ITDs w/o locking anything */
1685 status
= itd_urb_transaction (stream
, ehci
, urb
, mem_flags
);
1686 if (unlikely (status
< 0)) {
1687 ehci_dbg (ehci
, "can't init itds\n");
1691 /* schedule ... need to lock */
1692 spin_lock_irqsave (&ehci
->lock
, flags
);
1693 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE
,
1694 &ehci_to_hcd(ehci
)->flags
))) {
1695 status
= -ESHUTDOWN
;
1696 goto done_not_linked
;
1698 status
= usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci
), urb
);
1699 if (unlikely(status
))
1700 goto done_not_linked
;
1701 status
= iso_stream_schedule(ehci
, urb
, stream
);
1702 if (likely (status
== 0))
1703 itd_link_urb (ehci
, urb
, ehci
->periodic_size
<< 3, stream
);
1705 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci
), urb
);
1707 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1710 if (unlikely (status
< 0))
1711 iso_stream_put (ehci
, stream
);
1715 #ifdef CONFIG_USB_EHCI_SPLIT_ISO
1717 /*-------------------------------------------------------------------------*/
1720 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1721 * TTs in USB 2.0 hubs. These need microframe scheduling.
1726 struct ehci_hcd
*ehci
,
1727 struct ehci_iso_sched
*iso_sched
,
1728 struct ehci_iso_stream
*stream
,
1733 dma_addr_t dma
= urb
->transfer_dma
;
1735 /* how many frames are needed for these transfers */
1736 iso_sched
->span
= urb
->number_of_packets
* stream
->interval
;
1738 /* figure out per-frame sitd fields that we'll need later
1739 * when we fit new sitds into the schedule.
1741 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1742 struct ehci_iso_packet
*packet
= &iso_sched
->packet
[i
];
1747 length
= urb
->iso_frame_desc
[i
].length
& 0x03ff;
1748 buf
= dma
+ urb
->iso_frame_desc
[i
].offset
;
1750 trans
= SITD_STS_ACTIVE
;
1751 if (((i
+ 1) == urb
->number_of_packets
)
1752 && !(urb
->transfer_flags
& URB_NO_INTERRUPT
))
1754 trans
|= length
<< 16;
1755 packet
->transaction
= cpu_to_hc32(ehci
, trans
);
1757 /* might need to cross a buffer page within a td */
1759 packet
->buf1
= (buf
+ length
) & ~0x0fff;
1760 if (packet
->buf1
!= (buf
& ~(u64
)0x0fff))
1763 /* OUT uses multiple start-splits */
1764 if (stream
->bEndpointAddress
& USB_DIR_IN
)
1766 length
= (length
+ 187) / 188;
1767 if (length
> 1) /* BEGIN vs ALL */
1769 packet
->buf1
|= length
;
1774 sitd_urb_transaction (
1775 struct ehci_iso_stream
*stream
,
1776 struct ehci_hcd
*ehci
,
1781 struct ehci_sitd
*sitd
;
1782 dma_addr_t sitd_dma
;
1784 struct ehci_iso_sched
*iso_sched
;
1785 unsigned long flags
;
1787 iso_sched
= iso_sched_alloc (urb
->number_of_packets
, mem_flags
);
1788 if (iso_sched
== NULL
)
1791 sitd_sched_init(ehci
, iso_sched
, stream
, urb
);
1793 /* allocate/init sITDs */
1794 spin_lock_irqsave (&ehci
->lock
, flags
);
1795 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1797 /* NOTE: for now, we don't try to handle wraparound cases
1798 * for IN (using sitd->hw_backpointer, like a FSTN), which
1799 * means we never need two sitds for full speed packets.
1802 /* free_list.next might be cache-hot ... but maybe
1803 * the HC caches it too. avoid that issue for now.
1806 /* prefer previously-allocated sitds */
1807 if (!list_empty(&stream
->free_list
)) {
1808 sitd
= list_entry (stream
->free_list
.prev
,
1809 struct ehci_sitd
, sitd_list
);
1810 list_del (&sitd
->sitd_list
);
1811 sitd_dma
= sitd
->sitd_dma
;
1816 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1817 sitd
= dma_pool_alloc (ehci
->sitd_pool
, mem_flags
,
1819 spin_lock_irqsave (&ehci
->lock
, flags
);
1823 iso_sched_free (stream
, iso_sched
);
1824 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1827 memset (sitd
, 0, sizeof *sitd
);
1828 sitd
->sitd_dma
= sitd_dma
;
1829 list_add (&sitd
->sitd_list
, &iso_sched
->td_list
);
1832 /* temporarily store schedule info in hcpriv */
1833 urb
->hcpriv
= iso_sched
;
1834 urb
->error_count
= 0;
1836 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1840 /*-------------------------------------------------------------------------*/
1844 struct ehci_hcd
*ehci
,
1845 struct ehci_iso_stream
*stream
,
1846 struct ehci_sitd
*sitd
,
1847 struct ehci_iso_sched
*iso_sched
,
1851 struct ehci_iso_packet
*uf
= &iso_sched
->packet
[index
];
1852 u64 bufp
= uf
->bufp
;
1854 sitd
->hw_next
= EHCI_LIST_END(ehci
);
1855 sitd
->hw_fullspeed_ep
= stream
->address
;
1856 sitd
->hw_uframe
= stream
->splits
;
1857 sitd
->hw_results
= uf
->transaction
;
1858 sitd
->hw_backpointer
= EHCI_LIST_END(ehci
);
1861 sitd
->hw_buf
[0] = cpu_to_hc32(ehci
, bufp
);
1862 sitd
->hw_buf_hi
[0] = cpu_to_hc32(ehci
, bufp
>> 32);
1864 sitd
->hw_buf
[1] = cpu_to_hc32(ehci
, uf
->buf1
);
1867 sitd
->hw_buf_hi
[1] = cpu_to_hc32(ehci
, bufp
>> 32);
1868 sitd
->index
= index
;
1872 sitd_link (struct ehci_hcd
*ehci
, unsigned frame
, struct ehci_sitd
*sitd
)
1874 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1875 sitd
->sitd_next
= ehci
->pshadow
[frame
];
1876 sitd
->hw_next
= ehci
->periodic
[frame
];
1877 ehci
->pshadow
[frame
].sitd
= sitd
;
1878 sitd
->frame
= frame
;
1880 ehci
->periodic
[frame
] = cpu_to_hc32(ehci
, sitd
->sitd_dma
| Q_TYPE_SITD
);
1883 /* fit urb's sitds into the selected schedule slot; activate as needed */
1886 struct ehci_hcd
*ehci
,
1889 struct ehci_iso_stream
*stream
1893 unsigned next_uframe
;
1894 struct ehci_iso_sched
*sched
= urb
->hcpriv
;
1895 struct ehci_sitd
*sitd
;
1897 next_uframe
= stream
->next_uframe
;
1899 if (list_empty(&stream
->td_list
)) {
1900 /* usbfs ignores TT bandwidth */
1901 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
1902 += stream
->bandwidth
;
1904 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
1905 urb
->dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
1906 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out",
1907 (next_uframe
>> 3) % ehci
->periodic_size
,
1908 stream
->interval
, hc32_to_cpu(ehci
, stream
->splits
));
1909 stream
->start
= jiffies
;
1911 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
++;
1913 /* fill sITDs frame by frame */
1914 for (packet
= 0, sitd
= NULL
;
1915 packet
< urb
->number_of_packets
;
1918 /* ASSERT: we have all necessary sitds */
1919 BUG_ON (list_empty (&sched
->td_list
));
1921 /* ASSERT: no itds for this endpoint in this frame */
1923 sitd
= list_entry (sched
->td_list
.next
,
1924 struct ehci_sitd
, sitd_list
);
1925 list_move_tail (&sitd
->sitd_list
, &stream
->td_list
);
1926 sitd
->stream
= iso_stream_get (stream
);
1927 sitd
->urb
= usb_get_urb (urb
);
1929 sitd_patch(ehci
, stream
, sitd
, sched
, packet
);
1930 sitd_link (ehci
, (next_uframe
>> 3) % ehci
->periodic_size
,
1933 next_uframe
+= stream
->interval
<< 3;
1934 stream
->depth
+= stream
->interval
<< 3;
1936 stream
->next_uframe
= next_uframe
% mod
;
1938 /* don't need that schedule data any more */
1939 iso_sched_free (stream
, sched
);
1942 timer_action (ehci
, TIMER_IO_WATCHDOG
);
1943 if (!ehci
->periodic_sched
++)
1944 return enable_periodic (ehci
);
1948 /*-------------------------------------------------------------------------*/
1950 #define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
1951 | SITD_STS_XACT | SITD_STS_MMF)
1955 struct ehci_hcd
*ehci
,
1956 struct ehci_sitd
*sitd
1958 struct urb
*urb
= sitd
->urb
;
1959 struct usb_iso_packet_descriptor
*desc
;
1962 struct ehci_iso_stream
*stream
= sitd
->stream
;
1963 struct usb_device
*dev
;
1965 urb_index
= sitd
->index
;
1966 desc
= &urb
->iso_frame_desc
[urb_index
];
1967 t
= hc32_to_cpup(ehci
, &sitd
->hw_results
);
1969 /* report transfer status */
1970 if (t
& SITD_ERRS
) {
1972 if (t
& SITD_STS_DBE
)
1973 desc
->status
= usb_pipein (urb
->pipe
)
1974 ? -ENOSR
/* hc couldn't read */
1975 : -ECOMM
; /* hc couldn't write */
1976 else if (t
& SITD_STS_BABBLE
)
1977 desc
->status
= -EOVERFLOW
;
1978 else /* XACT, MMF, etc */
1979 desc
->status
= -EPROTO
;
1982 desc
->actual_length
= desc
->length
- SITD_LENGTH (t
);
1987 sitd
->stream
= NULL
;
1988 list_move (&sitd
->sitd_list
, &stream
->free_list
);
1989 stream
->depth
-= stream
->interval
<< 3;
1990 iso_stream_put (ehci
, stream
);
1992 /* handle completion now? */
1993 if ((urb_index
+ 1) != urb
->number_of_packets
)
1996 /* ASSERT: it's really the last sitd for this urb
1997 list_for_each_entry (sitd, &stream->td_list, sitd_list)
1998 BUG_ON (sitd->urb == urb);
2001 /* give urb back to the driver */
2003 ehci_urb_done(ehci
, urb
, 0);
2006 /* defer stopping schedule; completion can submit */
2007 ehci
->periodic_sched
--;
2008 if (!ehci
->periodic_sched
)
2009 (void) disable_periodic (ehci
);
2010 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
--;
2012 if (list_empty (&stream
->td_list
)) {
2013 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
2014 -= stream
->bandwidth
;
2016 "deschedule devp %s ep%d%s-iso\n",
2017 dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
2018 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out");
2020 iso_stream_put (ehci
, stream
);
2026 static int sitd_submit (struct ehci_hcd
*ehci
, struct urb
*urb
,
2029 int status
= -EINVAL
;
2030 unsigned long flags
;
2031 struct ehci_iso_stream
*stream
;
2033 /* Get iso_stream head */
2034 stream
= iso_stream_find (ehci
, urb
);
2035 if (stream
== NULL
) {
2036 ehci_dbg (ehci
, "can't get iso stream\n");
2039 if (urb
->interval
!= stream
->interval
) {
2040 ehci_dbg (ehci
, "can't change iso interval %d --> %d\n",
2041 stream
->interval
, urb
->interval
);
2045 #ifdef EHCI_URB_TRACE
2047 "submit %p dev%s ep%d%s-iso len %d\n",
2048 urb
, urb
->dev
->devpath
,
2049 usb_pipeendpoint (urb
->pipe
),
2050 usb_pipein (urb
->pipe
) ? "in" : "out",
2051 urb
->transfer_buffer_length
);
2054 /* allocate SITDs */
2055 status
= sitd_urb_transaction (stream
, ehci
, urb
, mem_flags
);
2057 ehci_dbg (ehci
, "can't init sitds\n");
2061 /* schedule ... need to lock */
2062 spin_lock_irqsave (&ehci
->lock
, flags
);
2063 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE
,
2064 &ehci_to_hcd(ehci
)->flags
))) {
2065 status
= -ESHUTDOWN
;
2066 goto done_not_linked
;
2068 status
= usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci
), urb
);
2069 if (unlikely(status
))
2070 goto done_not_linked
;
2071 status
= iso_stream_schedule(ehci
, urb
, stream
);
2073 sitd_link_urb (ehci
, urb
, ehci
->periodic_size
<< 3, stream
);
2075 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci
), urb
);
2077 spin_unlock_irqrestore (&ehci
->lock
, flags
);
2081 iso_stream_put (ehci
, stream
);
2088 sitd_submit (struct ehci_hcd
*ehci
, struct urb
*urb
, gfp_t mem_flags
)
2090 ehci_dbg (ehci
, "split iso support is disabled\n");
2094 static inline unsigned
2096 struct ehci_hcd
*ehci
,
2097 struct ehci_sitd
*sitd
2099 ehci_err (ehci
, "sitd_complete %p?\n", sitd
);
2103 #endif /* USB_EHCI_SPLIT_ISO */
2105 /*-------------------------------------------------------------------------*/
2108 scan_periodic (struct ehci_hcd
*ehci
)
2110 unsigned frame
, clock
, now_uframe
, mod
;
2113 mod
= ehci
->periodic_size
<< 3;
2116 * When running, scan from last scan point up to "now"
2117 * else clean up by scanning everything that's left.
2118 * Touches as few pages as possible: cache-friendly.
2120 now_uframe
= ehci
->next_uframe
;
2121 if (HC_IS_RUNNING (ehci_to_hcd(ehci
)->state
))
2122 clock
= ehci_readl(ehci
, &ehci
->regs
->frame_index
);
2124 clock
= now_uframe
+ mod
- 1;
2128 union ehci_shadow q
, *q_p
;
2132 /* don't scan past the live uframe */
2133 frame
= now_uframe
>> 3;
2134 if (frame
== (clock
>> 3))
2135 uframes
= now_uframe
& 0x07;
2137 /* safe to scan the whole frame at once */
2143 /* scan each element in frame's queue for completions */
2144 q_p
= &ehci
->pshadow
[frame
];
2145 hw_p
= &ehci
->periodic
[frame
];
2147 type
= Q_NEXT_TYPE(ehci
, *hw_p
);
2150 while (q
.ptr
!= NULL
) {
2152 union ehci_shadow temp
;
2155 live
= HC_IS_RUNNING (ehci_to_hcd(ehci
)->state
);
2156 switch (hc32_to_cpu(ehci
, type
)) {
2158 /* handle any completions */
2159 temp
.qh
= qh_get (q
.qh
);
2160 type
= Q_NEXT_TYPE(ehci
, q
.qh
->hw_next
);
2162 modified
= qh_completions (ehci
, temp
.qh
);
2163 if (unlikely (list_empty (&temp
.qh
->qtd_list
)))
2164 intr_deschedule (ehci
, temp
.qh
);
2168 /* for "save place" FSTNs, look at QH entries
2169 * in the previous frame for completions.
2171 if (q
.fstn
->hw_prev
!= EHCI_LIST_END(ehci
)) {
2172 dbg ("ignoring completions from FSTNs");
2174 type
= Q_NEXT_TYPE(ehci
, q
.fstn
->hw_next
);
2175 q
= q
.fstn
->fstn_next
;
2178 /* skip itds for later in the frame */
2180 for (uf
= live
? uframes
: 8; uf
< 8; uf
++) {
2181 if (0 == (q
.itd
->hw_transaction
[uf
]
2182 & ITD_ACTIVE(ehci
)))
2184 q_p
= &q
.itd
->itd_next
;
2185 hw_p
= &q
.itd
->hw_next
;
2186 type
= Q_NEXT_TYPE(ehci
,
2194 /* this one's ready ... HC won't cache the
2195 * pointer for much longer, if at all.
2197 *q_p
= q
.itd
->itd_next
;
2198 *hw_p
= q
.itd
->hw_next
;
2199 type
= Q_NEXT_TYPE(ehci
, q
.itd
->hw_next
);
2201 modified
= itd_complete (ehci
, q
.itd
);
2205 if ((q
.sitd
->hw_results
& SITD_ACTIVE(ehci
))
2207 q_p
= &q
.sitd
->sitd_next
;
2208 hw_p
= &q
.sitd
->hw_next
;
2209 type
= Q_NEXT_TYPE(ehci
,
2214 *q_p
= q
.sitd
->sitd_next
;
2215 *hw_p
= q
.sitd
->hw_next
;
2216 type
= Q_NEXT_TYPE(ehci
, q
.sitd
->hw_next
);
2218 modified
= sitd_complete (ehci
, q
.sitd
);
2222 dbg ("corrupt type %d frame %d shadow %p",
2223 type
, frame
, q
.ptr
);
2228 /* assume completion callbacks modify the queue */
2229 if (unlikely (modified
))
2233 /* stop when we catch up to the HC */
2235 // FIXME: this assumes we won't get lapped when
2236 // latencies climb; that should be rare, but...
2237 // detect it, and just go all the way around.
2238 // FLR might help detect this case, so long as latencies
2239 // don't exceed periodic_size msec (default 1.024 sec).
2241 // FIXME: likewise assumes HC doesn't halt mid-scan
2243 if (now_uframe
== clock
) {
2246 if (!HC_IS_RUNNING (ehci_to_hcd(ehci
)->state
))
2248 ehci
->next_uframe
= now_uframe
;
2249 now
= ehci_readl(ehci
, &ehci
->regs
->frame_index
) % mod
;
2250 if (now_uframe
== now
)
2253 /* rescan the rest of this frame, then ... */