2 * Copyright (c) 2001-2004 by David Brownell
3 * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /* this file is part of ehci-hcd.c */
22 /*-------------------------------------------------------------------------*/
25 * EHCI scheduled transaction support: interrupt, iso, split iso
26 * These are called "periodic" transactions in the EHCI spec.
28 * Note that for interrupt transfers, the QH/QTD manipulation is shared
29 * with the "asynchronous" transaction support (control/bulk transfers).
30 * The only real difference is in how interrupt transfers are scheduled.
32 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
33 * It keeps track of every ITD (or SITD) that's linked, and holds enough
34 * pre-calculated schedule data to make appending to the queue be quick.
37 static int ehci_get_frame (struct usb_hcd
*hcd
);
39 /*-------------------------------------------------------------------------*/
42 * periodic_next_shadow - return "next" pointer on shadow list
43 * @periodic: host pointer to qh/itd/sitd
44 * @tag: hardware tag for type of this record
46 static union ehci_shadow
*
47 periodic_next_shadow(struct ehci_hcd
*ehci
, union ehci_shadow
*periodic
,
50 switch (hc32_to_cpu(ehci
, tag
)) {
52 return &periodic
->qh
->qh_next
;
54 return &periodic
->fstn
->fstn_next
;
56 return &periodic
->itd
->itd_next
;
59 return &periodic
->sitd
->sitd_next
;
63 /* caller must hold ehci->lock */
64 static void periodic_unlink (struct ehci_hcd
*ehci
, unsigned frame
, void *ptr
)
66 union ehci_shadow
*prev_p
= &ehci
->pshadow
[frame
];
67 __hc32
*hw_p
= &ehci
->periodic
[frame
];
68 union ehci_shadow here
= *prev_p
;
70 /* find predecessor of "ptr"; hw and shadow lists are in sync */
71 while (here
.ptr
&& here
.ptr
!= ptr
) {
72 prev_p
= periodic_next_shadow(ehci
, prev_p
,
73 Q_NEXT_TYPE(ehci
, *hw_p
));
77 /* an interrupt entry (at list end) could have been shared */
81 /* update shadow and hardware lists ... the old "next" pointers
82 * from ptr may still be in use, the caller updates them.
84 *prev_p
= *periodic_next_shadow(ehci
, &here
,
85 Q_NEXT_TYPE(ehci
, *hw_p
));
86 *hw_p
= *here
.hw_next
;
89 /* how many of the uframe's 125 usecs are allocated? */
91 periodic_usecs (struct ehci_hcd
*ehci
, unsigned frame
, unsigned uframe
)
93 __hc32
*hw_p
= &ehci
->periodic
[frame
];
94 union ehci_shadow
*q
= &ehci
->pshadow
[frame
];
98 switch (hc32_to_cpu(ehci
, Q_NEXT_TYPE(ehci
, *hw_p
))) {
100 /* is it in the S-mask? */
101 if (q
->qh
->hw_info2
& cpu_to_hc32(ehci
, 1 << uframe
))
102 usecs
+= q
->qh
->usecs
;
104 if (q
->qh
->hw_info2
& cpu_to_hc32(ehci
,
106 usecs
+= q
->qh
->c_usecs
;
107 hw_p
= &q
->qh
->hw_next
;
112 /* for "save place" FSTNs, count the relevant INTR
113 * bandwidth from the previous frame
115 if (q
->fstn
->hw_prev
!= EHCI_LIST_END(ehci
)) {
116 ehci_dbg (ehci
, "ignoring FSTN cost ...\n");
118 hw_p
= &q
->fstn
->hw_next
;
119 q
= &q
->fstn
->fstn_next
;
122 if (q
->itd
->hw_transaction
[uframe
])
123 usecs
+= q
->itd
->stream
->usecs
;
124 hw_p
= &q
->itd
->hw_next
;
125 q
= &q
->itd
->itd_next
;
128 /* is it in the S-mask? (count SPLIT, DATA) */
129 if (q
->sitd
->hw_uframe
& cpu_to_hc32(ehci
,
131 if (q
->sitd
->hw_fullspeed_ep
&
132 cpu_to_hc32(ehci
, 1<<31))
133 usecs
+= q
->sitd
->stream
->usecs
;
134 else /* worst case for OUT start-split */
135 usecs
+= HS_USECS_ISO (188);
138 /* ... C-mask? (count CSPLIT, DATA) */
139 if (q
->sitd
->hw_uframe
&
140 cpu_to_hc32(ehci
, 1 << (8 + uframe
))) {
141 /* worst case for IN complete-split */
142 usecs
+= q
->sitd
->stream
->c_usecs
;
145 hw_p
= &q
->sitd
->hw_next
;
146 q
= &q
->sitd
->sitd_next
;
152 ehci_err (ehci
, "uframe %d sched overrun: %d usecs\n",
153 frame
* 8 + uframe
, usecs
);
158 /*-------------------------------------------------------------------------*/
160 static int same_tt (struct usb_device
*dev1
, struct usb_device
*dev2
)
162 if (!dev1
->tt
|| !dev2
->tt
)
164 if (dev1
->tt
!= dev2
->tt
)
167 return dev1
->ttport
== dev2
->ttport
;
172 #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
174 /* Which uframe does the low/fullspeed transfer start in?
176 * The parameter is the mask of ssplits in "H-frame" terms
177 * and this returns the transfer start uframe in "B-frame" terms,
178 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
179 * will cause a transfer in "B-frame" uframe 0. "B-frames" lag
180 * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7.
182 static inline unsigned char tt_start_uframe(struct ehci_hcd
*ehci
, __hc32 mask
)
184 unsigned char smask
= QH_SMASK
& hc32_to_cpu(ehci
, mask
);
186 ehci_err(ehci
, "invalid empty smask!\n");
187 /* uframe 7 can't have bw so this will indicate failure */
190 return ffs(smask
) - 1;
193 static const unsigned char
194 max_tt_usecs
[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
196 /* carryover low/fullspeed bandwidth that crosses uframe boundries */
197 static inline void carryover_tt_bandwidth(unsigned short tt_usecs
[8])
200 for (i
=0; i
<7; i
++) {
201 if (max_tt_usecs
[i
] < tt_usecs
[i
]) {
202 tt_usecs
[i
+1] += tt_usecs
[i
] - max_tt_usecs
[i
];
203 tt_usecs
[i
] = max_tt_usecs
[i
];
208 /* How many of the tt's periodic downstream 1000 usecs are allocated?
210 * While this measures the bandwidth in terms of usecs/uframe,
211 * the low/fullspeed bus has no notion of uframes, so any particular
212 * low/fullspeed transfer can "carry over" from one uframe to the next,
213 * since the TT just performs downstream transfers in sequence.
215 * For example two separate 100 usec transfers can start in the same uframe,
216 * and the second one would "carry over" 75 usecs into the next uframe.
220 struct ehci_hcd
*ehci
,
221 struct usb_device
*dev
,
223 unsigned short tt_usecs
[8]
226 __hc32
*hw_p
= &ehci
->periodic
[frame
];
227 union ehci_shadow
*q
= &ehci
->pshadow
[frame
];
230 memset(tt_usecs
, 0, 16);
233 switch (hc32_to_cpu(ehci
, Q_NEXT_TYPE(ehci
, *hw_p
))) {
235 hw_p
= &q
->itd
->hw_next
;
236 q
= &q
->itd
->itd_next
;
239 if (same_tt(dev
, q
->qh
->dev
)) {
240 uf
= tt_start_uframe(ehci
, q
->qh
->hw_info2
);
241 tt_usecs
[uf
] += q
->qh
->tt_usecs
;
243 hw_p
= &q
->qh
->hw_next
;
247 if (same_tt(dev
, q
->sitd
->urb
->dev
)) {
248 uf
= tt_start_uframe(ehci
, q
->sitd
->hw_uframe
);
249 tt_usecs
[uf
] += q
->sitd
->stream
->tt_usecs
;
251 hw_p
= &q
->sitd
->hw_next
;
252 q
= &q
->sitd
->sitd_next
;
256 ehci_dbg(ehci
, "ignoring periodic frame %d FSTN\n",
258 hw_p
= &q
->fstn
->hw_next
;
259 q
= &q
->fstn
->fstn_next
;
263 carryover_tt_bandwidth(tt_usecs
);
265 if (max_tt_usecs
[7] < tt_usecs
[7])
266 ehci_err(ehci
, "frame %d tt sched overrun: %d usecs\n",
267 frame
, tt_usecs
[7] - max_tt_usecs
[7]);
271 * Return true if the device's tt's downstream bus is available for a
272 * periodic transfer of the specified length (usecs), starting at the
273 * specified frame/uframe. Note that (as summarized in section 11.19
274 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
277 * The uframe parameter is when the fullspeed/lowspeed transfer
278 * should be executed in "B-frame" terms, which is the same as the
279 * highspeed ssplit's uframe (which is in "H-frame" terms). For example
280 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
281 * See the EHCI spec sec 4.5 and fig 4.7.
283 * This checks if the full/lowspeed bus, at the specified starting uframe,
284 * has the specified bandwidth available, according to rules listed
285 * in USB 2.0 spec section 11.18.1 fig 11-60.
287 * This does not check if the transfer would exceed the max ssplit
288 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
289 * since proper scheduling limits ssplits to less than 16 per uframe.
291 static int tt_available (
292 struct ehci_hcd
*ehci
,
294 struct usb_device
*dev
,
300 if ((period
== 0) || (uframe
>= 7)) /* error */
303 for (; frame
< ehci
->periodic_size
; frame
+= period
) {
304 unsigned short tt_usecs
[8];
306 periodic_tt_usecs (ehci
, dev
, frame
, tt_usecs
);
308 ehci_vdbg(ehci
, "tt frame %d check %d usecs start uframe %d in"
309 " schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
310 frame
, usecs
, uframe
,
311 tt_usecs
[0], tt_usecs
[1], tt_usecs
[2], tt_usecs
[3],
312 tt_usecs
[4], tt_usecs
[5], tt_usecs
[6], tt_usecs
[7]);
314 if (max_tt_usecs
[uframe
] <= tt_usecs
[uframe
]) {
315 ehci_vdbg(ehci
, "frame %d uframe %d fully scheduled\n",
320 /* special case for isoc transfers larger than 125us:
321 * the first and each subsequent fully used uframe
322 * must be empty, so as to not illegally delay
323 * already scheduled transactions
326 int ufs
= (usecs
/ 125) - 1;
328 for (i
= uframe
; i
< (uframe
+ ufs
) && i
< 8; i
++)
329 if (0 < tt_usecs
[i
]) {
331 "multi-uframe xfer can't fit "
332 "in frame %d uframe %d\n",
338 tt_usecs
[uframe
] += usecs
;
340 carryover_tt_bandwidth(tt_usecs
);
342 /* fail if the carryover pushed bw past the last uframe's limit */
343 if (max_tt_usecs
[7] < tt_usecs
[7]) {
345 "tt unavailable usecs %d frame %d uframe %d\n",
346 usecs
, frame
, uframe
);
356 /* return true iff the device's transaction translator is available
357 * for a periodic transfer starting at the specified frame, using
358 * all the uframes in the mask.
360 static int tt_no_collision (
361 struct ehci_hcd
*ehci
,
363 struct usb_device
*dev
,
368 if (period
== 0) /* error */
371 /* note bandwidth wastage: split never follows csplit
372 * (different dev or endpoint) until the next uframe.
373 * calling convention doesn't make that distinction.
375 for (; frame
< ehci
->periodic_size
; frame
+= period
) {
376 union ehci_shadow here
;
379 here
= ehci
->pshadow
[frame
];
380 type
= Q_NEXT_TYPE(ehci
, ehci
->periodic
[frame
]);
382 switch (hc32_to_cpu(ehci
, type
)) {
384 type
= Q_NEXT_TYPE(ehci
, here
.itd
->hw_next
);
385 here
= here
.itd
->itd_next
;
388 if (same_tt (dev
, here
.qh
->dev
)) {
391 mask
= hc32_to_cpu(ehci
,
393 /* "knows" no gap is needed */
398 type
= Q_NEXT_TYPE(ehci
, here
.qh
->hw_next
);
399 here
= here
.qh
->qh_next
;
402 if (same_tt (dev
, here
.sitd
->urb
->dev
)) {
405 mask
= hc32_to_cpu(ehci
, here
.sitd
407 /* FIXME assumes no gap for IN! */
412 type
= Q_NEXT_TYPE(ehci
, here
.sitd
->hw_next
);
413 here
= here
.sitd
->sitd_next
;
418 "periodic frame %d bogus type %d\n",
422 /* collision or error */
431 #endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
433 /*-------------------------------------------------------------------------*/
435 static int enable_periodic (struct ehci_hcd
*ehci
)
440 /* did clearing PSE did take effect yet?
441 * takes effect only at frame boundaries...
443 status
= handshake_on_error_set_halt(ehci
, &ehci
->regs
->status
,
444 STS_PSS
, 0, 9 * 125);
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_on_error_set_halt(ehci
, &ehci
->regs
->status
,
468 STS_PSS
, STS_PSS
, 9 * 125);
472 cmd
= ehci_readl(ehci
, &ehci
->regs
->command
) & ~CMD_PSE
;
473 ehci_writel(ehci
, cmd
, &ehci
->regs
->command
);
474 /* posted write ... */
476 ehci
->next_uframe
= -1;
480 /*-------------------------------------------------------------------------*/
482 /* periodic schedule slots have iso tds (normal or split) first, then a
483 * sparse tree for active interrupt transfers.
485 * this just links in a qh; caller guarantees uframe masks are set right.
486 * no FSTN support (yet; ehci 0.96+)
488 static int qh_link_periodic (struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
491 unsigned period
= qh
->period
;
493 dev_dbg (&qh
->dev
->dev
,
494 "link qh%d-%04x/%p start %d [%d/%d us]\n",
495 period
, hc32_to_cpup(ehci
, &qh
->hw_info2
) & (QH_CMASK
| QH_SMASK
),
496 qh
, qh
->start
, qh
->usecs
, qh
->c_usecs
);
498 /* high bandwidth, or otherwise every microframe */
502 for (i
= qh
->start
; i
< ehci
->periodic_size
; i
+= period
) {
503 union ehci_shadow
*prev
= &ehci
->pshadow
[i
];
504 __hc32
*hw_p
= &ehci
->periodic
[i
];
505 union ehci_shadow here
= *prev
;
508 /* skip the iso nodes at list head */
510 type
= Q_NEXT_TYPE(ehci
, *hw_p
);
511 if (type
== cpu_to_hc32(ehci
, Q_TYPE_QH
))
513 prev
= periodic_next_shadow(ehci
, prev
, type
);
514 hw_p
= &here
.qh
->hw_next
;
518 /* sorting each branch by period (slow-->fast)
519 * enables sharing interior tree nodes
521 while (here
.ptr
&& qh
!= here
.qh
) {
522 if (qh
->period
> here
.qh
->period
)
524 prev
= &here
.qh
->qh_next
;
525 hw_p
= &here
.qh
->hw_next
;
528 /* link in this qh, unless some earlier pass did that */
535 *hw_p
= QH_NEXT (ehci
, qh
->qh_dma
);
538 qh
->qh_state
= QH_STATE_LINKED
;
541 /* update per-qh bandwidth for usbfs */
542 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
+= qh
->period
543 ? ((qh
->usecs
+ qh
->c_usecs
) / qh
->period
)
546 /* maybe enable periodic schedule processing */
547 if (!ehci
->periodic_sched
++)
548 return enable_periodic (ehci
);
553 static void qh_unlink_periodic (struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
559 // IF this isn't high speed
560 // and this qh is active in the current uframe
561 // (and overlay token SplitXstate is false?)
563 // qh->hw_info1 |= __constant_cpu_to_hc32(1 << 7 /* "ignore" */);
565 /* high bandwidth, or otherwise part of every microframe */
566 if ((period
= qh
->period
) == 0)
569 for (i
= qh
->start
; i
< ehci
->periodic_size
; i
+= period
)
570 periodic_unlink (ehci
, i
, qh
);
572 /* update per-qh bandwidth for usbfs */
573 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
-= qh
->period
574 ? ((qh
->usecs
+ qh
->c_usecs
) / qh
->period
)
577 dev_dbg (&qh
->dev
->dev
,
578 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
580 hc32_to_cpup(ehci
, &qh
->hw_info2
) & (QH_CMASK
| QH_SMASK
),
581 qh
, qh
->start
, qh
->usecs
, qh
->c_usecs
);
583 /* qh->qh_next still "live" to HC */
584 qh
->qh_state
= QH_STATE_UNLINK
;
585 qh
->qh_next
.ptr
= NULL
;
588 /* maybe turn off periodic schedule */
589 ehci
->periodic_sched
--;
590 if (!ehci
->periodic_sched
)
591 (void) disable_periodic (ehci
);
594 static void intr_deschedule (struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
598 qh_unlink_periodic (ehci
, qh
);
600 /* simple/paranoid: always delay, expecting the HC needs to read
601 * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and
602 * expect khubd to clean up after any CSPLITs we won't issue.
603 * active high speed queues may need bigger delays...
605 if (list_empty (&qh
->qtd_list
)
606 || (cpu_to_hc32(ehci
, QH_CMASK
)
607 & qh
->hw_info2
) != 0)
610 wait
= 55; /* worst case: 3 * 1024 */
613 qh
->qh_state
= QH_STATE_IDLE
;
614 qh
->hw_next
= EHCI_LIST_END(ehci
);
618 /*-------------------------------------------------------------------------*/
620 static int check_period (
621 struct ehci_hcd
*ehci
,
629 /* complete split running into next frame?
630 * given FSTN support, we could sometimes check...
636 * 80% periodic == 100 usec/uframe available
637 * convert "usecs we need" to "max already claimed"
641 /* we "know" 2 and 4 uframe intervals were rejected; so
642 * for period 0, check _every_ microframe in the schedule.
644 if (unlikely (period
== 0)) {
646 for (uframe
= 0; uframe
< 7; uframe
++) {
647 claimed
= periodic_usecs (ehci
, frame
, uframe
);
651 } while ((frame
+= 1) < ehci
->periodic_size
);
653 /* just check the specified uframe, at that period */
656 claimed
= periodic_usecs (ehci
, frame
, uframe
);
659 } while ((frame
+= period
) < ehci
->periodic_size
);
666 static int check_intr_schedule (
667 struct ehci_hcd
*ehci
,
670 const struct ehci_qh
*qh
,
674 int retval
= -ENOSPC
;
677 if (qh
->c_usecs
&& uframe
>= 6) /* FSTN territory? */
680 if (!check_period (ehci
, frame
, uframe
, qh
->period
, qh
->usecs
))
688 #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
689 if (tt_available (ehci
, qh
->period
, qh
->dev
, frame
, uframe
,
693 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
694 for (i
=uframe
+1; i
<8 && i
<uframe
+4; i
++)
695 if (!check_period (ehci
, frame
, i
,
696 qh
->period
, qh
->c_usecs
))
703 *c_maskp
= cpu_to_hc32(ehci
, mask
<< 8);
706 /* Make sure this tt's buffer is also available for CSPLITs.
707 * We pessimize a bit; probably the typical full speed case
708 * doesn't need the second CSPLIT.
710 * NOTE: both SPLIT and CSPLIT could be checked in just
713 mask
= 0x03 << (uframe
+ qh
->gap_uf
);
714 *c_maskp
= cpu_to_hc32(ehci
, mask
<< 8);
717 if (tt_no_collision (ehci
, qh
->period
, qh
->dev
, frame
, mask
)) {
718 if (!check_period (ehci
, frame
, uframe
+ qh
->gap_uf
+ 1,
719 qh
->period
, qh
->c_usecs
))
721 if (!check_period (ehci
, frame
, uframe
+ qh
->gap_uf
,
722 qh
->period
, qh
->c_usecs
))
731 /* "first fit" scheduling policy used the first time through,
732 * or when the previous schedule slot can't be re-used.
734 static int qh_schedule(struct ehci_hcd
*ehci
, struct ehci_qh
*qh
)
739 unsigned frame
; /* 0..(qh->period - 1), or NO_FRAME */
741 qh_refresh(ehci
, qh
);
742 qh
->hw_next
= EHCI_LIST_END(ehci
);
745 /* reuse the previous schedule slots, if we can */
746 if (frame
< qh
->period
) {
747 uframe
= ffs(hc32_to_cpup(ehci
, &qh
->hw_info2
) & QH_SMASK
);
748 status
= check_intr_schedule (ehci
, frame
, --uframe
,
756 /* else scan the schedule to find a group of slots such that all
757 * uframes have enough periodic bandwidth available.
760 /* "normal" case, uframing flexible except with splits */
762 frame
= qh
->period
- 1;
764 for (uframe
= 0; uframe
< 8; uframe
++) {
765 status
= check_intr_schedule (ehci
,
771 } while (status
&& frame
--);
773 /* qh->period == 0 means every uframe */
776 status
= check_intr_schedule (ehci
, 0, 0, qh
, &c_mask
);
782 /* reset S-frame and (maybe) C-frame masks */
783 qh
->hw_info2
&= cpu_to_hc32(ehci
, ~(QH_CMASK
| QH_SMASK
));
784 qh
->hw_info2
|= qh
->period
785 ? cpu_to_hc32(ehci
, 1 << uframe
)
786 : cpu_to_hc32(ehci
, QH_SMASK
);
787 qh
->hw_info2
|= c_mask
;
789 ehci_dbg (ehci
, "reused qh %p schedule\n", qh
);
791 /* stuff into the periodic schedule */
792 status
= qh_link_periodic (ehci
, qh
);
797 static int intr_submit (
798 struct ehci_hcd
*ehci
,
800 struct list_head
*qtd_list
,
807 struct list_head empty
;
809 /* get endpoint and transfer/schedule data */
810 epnum
= urb
->ep
->desc
.bEndpointAddress
;
812 spin_lock_irqsave (&ehci
->lock
, flags
);
814 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE
,
815 &ehci_to_hcd(ehci
)->flags
))) {
817 goto done_not_linked
;
819 status
= usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci
), urb
);
820 if (unlikely(status
))
821 goto done_not_linked
;
823 /* get qh and force any scheduling errors */
824 INIT_LIST_HEAD (&empty
);
825 qh
= qh_append_tds(ehci
, urb
, &empty
, epnum
, &urb
->ep
->hcpriv
);
830 if (qh
->qh_state
== QH_STATE_IDLE
) {
831 if ((status
= qh_schedule (ehci
, qh
)) != 0)
835 /* then queue the urb's tds to the qh */
836 qh
= qh_append_tds(ehci
, urb
, qtd_list
, epnum
, &urb
->ep
->hcpriv
);
839 /* ... update usbfs periodic stats */
840 ehci_to_hcd(ehci
)->self
.bandwidth_int_reqs
++;
843 if (unlikely(status
))
844 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci
), urb
);
846 spin_unlock_irqrestore (&ehci
->lock
, flags
);
848 qtd_list_free (ehci
, urb
, qtd_list
);
853 /*-------------------------------------------------------------------------*/
855 /* ehci_iso_stream ops work with both ITD and SITD */
857 static struct ehci_iso_stream
*
858 iso_stream_alloc (gfp_t mem_flags
)
860 struct ehci_iso_stream
*stream
;
862 stream
= kzalloc(sizeof *stream
, mem_flags
);
863 if (likely (stream
!= NULL
)) {
864 INIT_LIST_HEAD(&stream
->td_list
);
865 INIT_LIST_HEAD(&stream
->free_list
);
866 stream
->next_uframe
= -1;
867 stream
->refcount
= 1;
874 struct ehci_hcd
*ehci
,
875 struct ehci_iso_stream
*stream
,
876 struct usb_device
*dev
,
881 static const u8 smask_out
[] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
884 unsigned epnum
, maxp
;
889 * this might be a "high bandwidth" highspeed endpoint,
890 * as encoded in the ep descriptor's wMaxPacket field
892 epnum
= usb_pipeendpoint (pipe
);
893 is_input
= usb_pipein (pipe
) ? USB_DIR_IN
: 0;
894 maxp
= usb_maxpacket(dev
, pipe
, !is_input
);
901 /* knows about ITD vs SITD */
902 if (dev
->speed
== USB_SPEED_HIGH
) {
903 unsigned multi
= hb_mult(maxp
);
905 stream
->highspeed
= 1;
907 maxp
= max_packet(maxp
);
911 stream
->buf0
= cpu_to_hc32(ehci
, (epnum
<< 8) | dev
->devnum
);
912 stream
->buf1
= cpu_to_hc32(ehci
, buf1
);
913 stream
->buf2
= cpu_to_hc32(ehci
, multi
);
915 /* usbfs wants to report the average usecs per frame tied up
916 * when transfers on this endpoint are scheduled ...
918 stream
->usecs
= HS_USECS_ISO (maxp
);
919 bandwidth
= stream
->usecs
* 8;
920 bandwidth
/= 1 << (interval
- 1);
927 addr
= dev
->ttport
<< 24;
928 if (!ehci_is_TDI(ehci
)
930 ehci_to_hcd(ehci
)->self
.root_hub
))
931 addr
|= dev
->tt
->hub
->devnum
<< 16;
934 stream
->usecs
= HS_USECS_ISO (maxp
);
935 think_time
= dev
->tt
? dev
->tt
->think_time
: 0;
936 stream
->tt_usecs
= NS_TO_US (think_time
+ usb_calc_bus_time (
937 dev
->speed
, is_input
, 1, maxp
));
938 hs_transfers
= max (1u, (maxp
+ 187) / 188);
943 stream
->c_usecs
= stream
->usecs
;
944 stream
->usecs
= HS_USECS_ISO (1);
945 stream
->raw_mask
= 1;
947 /* c-mask as specified in USB 2.0 11.18.4 3.c */
948 tmp
= (1 << (hs_transfers
+ 2)) - 1;
949 stream
->raw_mask
|= tmp
<< (8 + 2);
951 stream
->raw_mask
= smask_out
[hs_transfers
- 1];
952 bandwidth
= stream
->usecs
+ stream
->c_usecs
;
953 bandwidth
/= 1 << (interval
+ 2);
955 /* stream->splits gets created from raw_mask later */
956 stream
->address
= cpu_to_hc32(ehci
, addr
);
958 stream
->bandwidth
= bandwidth
;
962 stream
->bEndpointAddress
= is_input
| epnum
;
963 stream
->interval
= interval
;
968 iso_stream_put(struct ehci_hcd
*ehci
, struct ehci_iso_stream
*stream
)
972 /* free whenever just a dev->ep reference remains.
973 * not like a QH -- no persistent state (toggle, halt)
975 if (stream
->refcount
== 1) {
978 // BUG_ON (!list_empty(&stream->td_list));
980 while (!list_empty (&stream
->free_list
)) {
981 struct list_head
*entry
;
983 entry
= stream
->free_list
.next
;
986 /* knows about ITD vs SITD */
987 if (stream
->highspeed
) {
988 struct ehci_itd
*itd
;
990 itd
= list_entry (entry
, struct ehci_itd
,
992 dma_pool_free (ehci
->itd_pool
, itd
,
995 struct ehci_sitd
*sitd
;
997 sitd
= list_entry (entry
, struct ehci_sitd
,
999 dma_pool_free (ehci
->sitd_pool
, sitd
,
1004 is_in
= (stream
->bEndpointAddress
& USB_DIR_IN
) ? 0x10 : 0;
1005 stream
->bEndpointAddress
&= 0x0f;
1006 stream
->ep
->hcpriv
= NULL
;
1008 if (stream
->rescheduled
) {
1009 ehci_info (ehci
, "ep%d%s-iso rescheduled "
1010 "%lu times in %lu seconds\n",
1011 stream
->bEndpointAddress
, is_in
? "in" : "out",
1012 stream
->rescheduled
,
1013 ((jiffies
- stream
->start
)/HZ
)
1021 static inline struct ehci_iso_stream
*
1022 iso_stream_get (struct ehci_iso_stream
*stream
)
1024 if (likely (stream
!= NULL
))
1029 static struct ehci_iso_stream
*
1030 iso_stream_find (struct ehci_hcd
*ehci
, struct urb
*urb
)
1033 struct ehci_iso_stream
*stream
;
1034 struct usb_host_endpoint
*ep
;
1035 unsigned long flags
;
1037 epnum
= usb_pipeendpoint (urb
->pipe
);
1038 if (usb_pipein(urb
->pipe
))
1039 ep
= urb
->dev
->ep_in
[epnum
];
1041 ep
= urb
->dev
->ep_out
[epnum
];
1043 spin_lock_irqsave (&ehci
->lock
, flags
);
1044 stream
= ep
->hcpriv
;
1046 if (unlikely (stream
== NULL
)) {
1047 stream
= iso_stream_alloc(GFP_ATOMIC
);
1048 if (likely (stream
!= NULL
)) {
1049 /* dev->ep owns the initial refcount */
1050 ep
->hcpriv
= stream
;
1052 iso_stream_init(ehci
, stream
, urb
->dev
, urb
->pipe
,
1056 /* if dev->ep [epnum] is a QH, info1.maxpacket is nonzero */
1057 } else if (unlikely (stream
->hw_info1
!= 0)) {
1058 ehci_dbg (ehci
, "dev %s ep%d%s, not iso??\n",
1059 urb
->dev
->devpath
, epnum
,
1060 usb_pipein(urb
->pipe
) ? "in" : "out");
1064 /* caller guarantees an eventual matching iso_stream_put */
1065 stream
= iso_stream_get (stream
);
1067 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1071 /*-------------------------------------------------------------------------*/
1073 /* ehci_iso_sched ops can be ITD-only or SITD-only */
1075 static struct ehci_iso_sched
*
1076 iso_sched_alloc (unsigned packets
, gfp_t mem_flags
)
1078 struct ehci_iso_sched
*iso_sched
;
1079 int size
= sizeof *iso_sched
;
1081 size
+= packets
* sizeof (struct ehci_iso_packet
);
1082 iso_sched
= kzalloc(size
, mem_flags
);
1083 if (likely (iso_sched
!= NULL
)) {
1084 INIT_LIST_HEAD (&iso_sched
->td_list
);
1091 struct ehci_hcd
*ehci
,
1092 struct ehci_iso_sched
*iso_sched
,
1093 struct ehci_iso_stream
*stream
,
1098 dma_addr_t dma
= urb
->transfer_dma
;
1100 /* how many uframes are needed for these transfers */
1101 iso_sched
->span
= urb
->number_of_packets
* stream
->interval
;
1103 /* figure out per-uframe itd fields that we'll need later
1104 * when we fit new itds into the schedule.
1106 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1107 struct ehci_iso_packet
*uframe
= &iso_sched
->packet
[i
];
1112 length
= urb
->iso_frame_desc
[i
].length
;
1113 buf
= dma
+ urb
->iso_frame_desc
[i
].offset
;
1115 trans
= EHCI_ISOC_ACTIVE
;
1116 trans
|= buf
& 0x0fff;
1117 if (unlikely (((i
+ 1) == urb
->number_of_packets
))
1118 && !(urb
->transfer_flags
& URB_NO_INTERRUPT
))
1119 trans
|= EHCI_ITD_IOC
;
1120 trans
|= length
<< 16;
1121 uframe
->transaction
= cpu_to_hc32(ehci
, trans
);
1123 /* might need to cross a buffer page within a uframe */
1124 uframe
->bufp
= (buf
& ~(u64
)0x0fff);
1126 if (unlikely ((uframe
->bufp
!= (buf
& ~(u64
)0x0fff))))
1133 struct ehci_iso_stream
*stream
,
1134 struct ehci_iso_sched
*iso_sched
1139 // caller must hold ehci->lock!
1140 list_splice (&iso_sched
->td_list
, &stream
->free_list
);
1145 itd_urb_transaction (
1146 struct ehci_iso_stream
*stream
,
1147 struct ehci_hcd
*ehci
,
1152 struct ehci_itd
*itd
;
1156 struct ehci_iso_sched
*sched
;
1157 unsigned long flags
;
1159 sched
= iso_sched_alloc (urb
->number_of_packets
, mem_flags
);
1160 if (unlikely (sched
== NULL
))
1163 itd_sched_init(ehci
, sched
, stream
, urb
);
1165 if (urb
->interval
< 8)
1166 num_itds
= 1 + (sched
->span
+ 7) / 8;
1168 num_itds
= urb
->number_of_packets
;
1170 /* allocate/init ITDs */
1171 spin_lock_irqsave (&ehci
->lock
, flags
);
1172 for (i
= 0; i
< num_itds
; i
++) {
1174 /* free_list.next might be cache-hot ... but maybe
1175 * the HC caches it too. avoid that issue for now.
1178 /* prefer previously-allocated itds */
1179 if (likely (!list_empty(&stream
->free_list
))) {
1180 itd
= list_entry (stream
->free_list
.prev
,
1181 struct ehci_itd
, itd_list
);
1182 list_del (&itd
->itd_list
);
1183 itd_dma
= itd
->itd_dma
;
1185 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1186 itd
= dma_pool_alloc (ehci
->itd_pool
, mem_flags
,
1188 spin_lock_irqsave (&ehci
->lock
, flags
);
1190 iso_sched_free(stream
, sched
);
1191 spin_unlock_irqrestore(&ehci
->lock
, flags
);
1196 memset (itd
, 0, sizeof *itd
);
1197 itd
->itd_dma
= itd_dma
;
1198 list_add (&itd
->itd_list
, &sched
->td_list
);
1200 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1202 /* temporarily store schedule info in hcpriv */
1203 urb
->hcpriv
= sched
;
1204 urb
->error_count
= 0;
1208 /*-------------------------------------------------------------------------*/
1212 struct ehci_hcd
*ehci
,
1221 /* can't commit more than 80% periodic == 100 usec */
1222 if (periodic_usecs (ehci
, uframe
>> 3, uframe
& 0x7)
1226 /* we know urb->interval is 2^N uframes */
1228 } while (uframe
< mod
);
1234 struct ehci_hcd
*ehci
,
1236 struct ehci_iso_stream
*stream
,
1238 struct ehci_iso_sched
*sched
,
1245 mask
= stream
->raw_mask
<< (uframe
& 7);
1247 /* for IN, don't wrap CSPLIT into the next frame */
1251 /* this multi-pass logic is simple, but performance may
1252 * suffer when the schedule data isn't cached.
1255 /* check bandwidth */
1256 uframe
%= period_uframes
;
1260 frame
= uframe
>> 3;
1263 #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1264 /* The tt's fullspeed bus bandwidth must be available.
1265 * tt_available scheduling guarantees 10+% for control/bulk.
1267 if (!tt_available (ehci
, period_uframes
<< 3,
1268 stream
->udev
, frame
, uf
, stream
->tt_usecs
))
1271 /* tt must be idle for start(s), any gap, and csplit.
1272 * assume scheduling slop leaves 10+% for control/bulk.
1274 if (!tt_no_collision (ehci
, period_uframes
<< 3,
1275 stream
->udev
, frame
, mask
))
1279 /* check starts (OUT uses more than one) */
1280 max_used
= 100 - stream
->usecs
;
1281 for (tmp
= stream
->raw_mask
& 0xff; tmp
; tmp
>>= 1, uf
++) {
1282 if (periodic_usecs (ehci
, frame
, uf
) > max_used
)
1286 /* for IN, check CSPLIT */
1287 if (stream
->c_usecs
) {
1289 max_used
= 100 - stream
->c_usecs
;
1293 if ((stream
->raw_mask
& tmp
) == 0)
1295 if (periodic_usecs (ehci
, frame
, uf
)
1301 /* we know urb->interval is 2^N uframes */
1302 uframe
+= period_uframes
;
1303 } while (uframe
< mod
);
1305 stream
->splits
= cpu_to_hc32(ehci
, stream
->raw_mask
<< (uframe
& 7));
1310 * This scheduler plans almost as far into the future as it has actual
1311 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1312 * "as small as possible" to be cache-friendlier.) That limits the size
1313 * transfers you can stream reliably; avoid more than 64 msec per urb.
1314 * Also avoid queue depths of less than ehci's worst irq latency (affected
1315 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1316 * and other factors); or more than about 230 msec total (for portability,
1317 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1320 #define SCHEDULE_SLOP 10 /* frames */
1323 iso_stream_schedule (
1324 struct ehci_hcd
*ehci
,
1326 struct ehci_iso_stream
*stream
1329 u32 now
, start
, max
, period
;
1331 unsigned mod
= ehci
->periodic_size
<< 3;
1332 struct ehci_iso_sched
*sched
= urb
->hcpriv
;
1334 if (sched
->span
> (mod
- 8 * SCHEDULE_SLOP
)) {
1335 ehci_dbg (ehci
, "iso request %p too long\n", urb
);
1340 if ((stream
->depth
+ sched
->span
) > mod
) {
1341 ehci_dbg (ehci
, "request %p would overflow (%d+%d>%d)\n",
1342 urb
, stream
->depth
, sched
->span
, mod
);
1347 now
= ehci_readl(ehci
, &ehci
->regs
->frame_index
) % mod
;
1349 /* when's the last uframe this urb could start? */
1352 /* Typical case: reuse current schedule, stream is still active.
1353 * Hopefully there are no gaps from the host falling behind
1354 * (irq delays etc), but if there are we'll take the next
1355 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
1357 if (likely (!list_empty (&stream
->td_list
))) {
1358 start
= stream
->next_uframe
;
1362 /* Fell behind (by up to twice the slop amount)? */
1363 if (start
>= max
- 2 * 8 * SCHEDULE_SLOP
)
1364 start
+= stream
->interval
* DIV_ROUND_UP(
1365 max
- start
, stream
->interval
) - mod
;
1367 /* Tried to schedule too far into the future? */
1368 if (unlikely((start
+ sched
->span
) >= max
)) {
1375 /* need to schedule; when's the next (u)frame we could start?
1376 * this is bigger than ehci->i_thresh allows; scheduling itself
1377 * isn't free, the slop should handle reasonably slow cpus. it
1378 * can also help high bandwidth if the dma and irq loads don't
1379 * jump until after the queue is primed.
1381 start
= SCHEDULE_SLOP
* 8 + (now
& ~0x07);
1383 stream
->next_uframe
= start
;
1385 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
1387 period
= urb
->interval
;
1388 if (!stream
->highspeed
)
1391 /* find a uframe slot with enough bandwidth */
1392 for (; start
< (stream
->next_uframe
+ period
); start
++) {
1395 /* check schedule: enough space? */
1396 if (stream
->highspeed
)
1397 enough_space
= itd_slot_ok (ehci
, mod
, start
,
1398 stream
->usecs
, period
);
1400 if ((start
% 8) >= 6)
1402 enough_space
= sitd_slot_ok (ehci
, mod
, stream
,
1403 start
, sched
, period
);
1406 /* schedule it here if there's enough bandwidth */
1408 stream
->next_uframe
= start
% mod
;
1413 /* no room in the schedule */
1414 ehci_dbg (ehci
, "iso %ssched full %p (now %d max %d)\n",
1415 list_empty (&stream
->td_list
) ? "" : "re",
1420 iso_sched_free (stream
, sched
);
1425 /* report high speed start in uframes; full speed, in frames */
1426 urb
->start_frame
= stream
->next_uframe
;
1427 if (!stream
->highspeed
)
1428 urb
->start_frame
>>= 3;
1432 /*-------------------------------------------------------------------------*/
1435 itd_init(struct ehci_hcd
*ehci
, struct ehci_iso_stream
*stream
,
1436 struct ehci_itd
*itd
)
1440 /* it's been recently zeroed */
1441 itd
->hw_next
= EHCI_LIST_END(ehci
);
1442 itd
->hw_bufp
[0] = stream
->buf0
;
1443 itd
->hw_bufp
[1] = stream
->buf1
;
1444 itd
->hw_bufp
[2] = stream
->buf2
;
1446 for (i
= 0; i
< 8; i
++)
1449 /* All other fields are filled when scheduling */
1454 struct ehci_hcd
*ehci
,
1455 struct ehci_itd
*itd
,
1456 struct ehci_iso_sched
*iso_sched
,
1461 struct ehci_iso_packet
*uf
= &iso_sched
->packet
[index
];
1462 unsigned pg
= itd
->pg
;
1464 // BUG_ON (pg == 6 && uf->cross);
1467 itd
->index
[uframe
] = index
;
1469 itd
->hw_transaction
[uframe
] = uf
->transaction
;
1470 itd
->hw_transaction
[uframe
] |= cpu_to_hc32(ehci
, pg
<< 12);
1471 itd
->hw_bufp
[pg
] |= cpu_to_hc32(ehci
, uf
->bufp
& ~(u32
)0);
1472 itd
->hw_bufp_hi
[pg
] |= cpu_to_hc32(ehci
, (u32
)(uf
->bufp
>> 32));
1474 /* iso_frame_desc[].offset must be strictly increasing */
1475 if (unlikely (uf
->cross
)) {
1476 u64 bufp
= uf
->bufp
+ 4096;
1479 itd
->hw_bufp
[pg
] |= cpu_to_hc32(ehci
, bufp
& ~(u32
)0);
1480 itd
->hw_bufp_hi
[pg
] |= cpu_to_hc32(ehci
, (u32
)(bufp
>> 32));
1485 itd_link (struct ehci_hcd
*ehci
, unsigned frame
, struct ehci_itd
*itd
)
1487 /* always prepend ITD/SITD ... only QH tree is order-sensitive */
1488 itd
->itd_next
= ehci
->pshadow
[frame
];
1489 itd
->hw_next
= ehci
->periodic
[frame
];
1490 ehci
->pshadow
[frame
].itd
= itd
;
1493 ehci
->periodic
[frame
] = cpu_to_hc32(ehci
, itd
->itd_dma
| Q_TYPE_ITD
);
1496 /* fit urb's itds into the selected schedule slot; activate as needed */
1499 struct ehci_hcd
*ehci
,
1502 struct ehci_iso_stream
*stream
1506 unsigned next_uframe
, uframe
, frame
;
1507 struct ehci_iso_sched
*iso_sched
= urb
->hcpriv
;
1508 struct ehci_itd
*itd
;
1510 next_uframe
= stream
->next_uframe
% mod
;
1512 if (unlikely (list_empty(&stream
->td_list
))) {
1513 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
1514 += stream
->bandwidth
;
1516 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1517 urb
->dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
1518 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out",
1520 next_uframe
>> 3, next_uframe
& 0x7);
1521 stream
->start
= jiffies
;
1523 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
++;
1525 /* fill iTDs uframe by uframe */
1526 for (packet
= 0, itd
= NULL
; packet
< urb
->number_of_packets
; ) {
1528 /* ASSERT: we have all necessary itds */
1529 // BUG_ON (list_empty (&iso_sched->td_list));
1531 /* ASSERT: no itds for this endpoint in this uframe */
1533 itd
= list_entry (iso_sched
->td_list
.next
,
1534 struct ehci_itd
, itd_list
);
1535 list_move_tail (&itd
->itd_list
, &stream
->td_list
);
1536 itd
->stream
= iso_stream_get (stream
);
1537 itd
->urb
= usb_get_urb (urb
);
1538 itd_init (ehci
, stream
, itd
);
1541 uframe
= next_uframe
& 0x07;
1542 frame
= next_uframe
>> 3;
1544 itd_patch(ehci
, itd
, iso_sched
, packet
, uframe
);
1546 next_uframe
+= stream
->interval
;
1547 stream
->depth
+= stream
->interval
;
1551 /* link completed itds into the schedule */
1552 if (((next_uframe
>> 3) != frame
)
1553 || packet
== urb
->number_of_packets
) {
1554 itd_link (ehci
, frame
% ehci
->periodic_size
, itd
);
1558 stream
->next_uframe
= next_uframe
;
1560 /* don't need that schedule data any more */
1561 iso_sched_free (stream
, iso_sched
);
1564 timer_action (ehci
, TIMER_IO_WATCHDOG
);
1565 if (unlikely (!ehci
->periodic_sched
++))
1566 return enable_periodic (ehci
);
1570 #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1572 /* Process and recycle a completed ITD. Return true iff its urb completed,
1573 * and hence its completion callback probably added things to the hardware
1576 * Note that we carefully avoid recycling this descriptor until after any
1577 * completion callback runs, so that it won't be reused quickly. That is,
1578 * assuming (a) no more than two urbs per frame on this endpoint, and also
1579 * (b) only this endpoint's completions submit URBs. It seems some silicon
1580 * corrupts things if you reuse completed descriptors very quickly...
1584 struct ehci_hcd
*ehci
,
1585 struct ehci_itd
*itd
1587 struct urb
*urb
= itd
->urb
;
1588 struct usb_iso_packet_descriptor
*desc
;
1592 struct ehci_iso_stream
*stream
= itd
->stream
;
1593 struct usb_device
*dev
;
1594 unsigned retval
= false;
1596 /* for each uframe with a packet */
1597 for (uframe
= 0; uframe
< 8; uframe
++) {
1598 if (likely (itd
->index
[uframe
] == -1))
1600 urb_index
= itd
->index
[uframe
];
1601 desc
= &urb
->iso_frame_desc
[urb_index
];
1603 t
= hc32_to_cpup(ehci
, &itd
->hw_transaction
[uframe
]);
1604 itd
->hw_transaction
[uframe
] = 0;
1605 stream
->depth
-= stream
->interval
;
1607 /* report transfer status */
1608 if (unlikely (t
& ISO_ERRS
)) {
1610 if (t
& EHCI_ISOC_BUF_ERR
)
1611 desc
->status
= usb_pipein (urb
->pipe
)
1612 ? -ENOSR
/* hc couldn't read */
1613 : -ECOMM
; /* hc couldn't write */
1614 else if (t
& EHCI_ISOC_BABBLE
)
1615 desc
->status
= -EOVERFLOW
;
1616 else /* (t & EHCI_ISOC_XACTERR) */
1617 desc
->status
= -EPROTO
;
1619 /* HC need not update length with this error */
1620 if (!(t
& EHCI_ISOC_BABBLE
))
1621 desc
->actual_length
= EHCI_ITD_LENGTH (t
);
1622 } else if (likely ((t
& EHCI_ISOC_ACTIVE
) == 0)) {
1624 desc
->actual_length
= EHCI_ITD_LENGTH (t
);
1626 /* URB was too late */
1627 desc
->status
= -EXDEV
;
1631 /* handle completion now? */
1632 if (likely ((urb_index
+ 1) != urb
->number_of_packets
))
1635 /* ASSERT: it's really the last itd for this urb
1636 list_for_each_entry (itd, &stream->td_list, itd_list)
1637 BUG_ON (itd->urb == urb);
1640 /* give urb back to the driver; completion often (re)submits */
1642 ehci_urb_done(ehci
, urb
, 0);
1645 ehci
->periodic_sched
--;
1646 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
--;
1648 if (unlikely (list_empty (&stream
->td_list
))) {
1649 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
1650 -= stream
->bandwidth
;
1652 "deschedule devp %s ep%d%s-iso\n",
1653 dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
1654 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out");
1656 iso_stream_put (ehci
, stream
);
1657 /* OK to recycle this ITD now that its completion callback ran. */
1662 list_move(&itd
->itd_list
, &stream
->free_list
);
1663 iso_stream_put(ehci
, stream
);
1668 /*-------------------------------------------------------------------------*/
1670 static int itd_submit (struct ehci_hcd
*ehci
, struct urb
*urb
,
1673 int status
= -EINVAL
;
1674 unsigned long flags
;
1675 struct ehci_iso_stream
*stream
;
1677 /* Get iso_stream head */
1678 stream
= iso_stream_find (ehci
, urb
);
1679 if (unlikely (stream
== NULL
)) {
1680 ehci_dbg (ehci
, "can't get iso stream\n");
1683 if (unlikely (urb
->interval
!= stream
->interval
)) {
1684 ehci_dbg (ehci
, "can't change iso interval %d --> %d\n",
1685 stream
->interval
, urb
->interval
);
1689 #ifdef EHCI_URB_TRACE
1691 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
1692 __func__
, urb
->dev
->devpath
, urb
,
1693 usb_pipeendpoint (urb
->pipe
),
1694 usb_pipein (urb
->pipe
) ? "in" : "out",
1695 urb
->transfer_buffer_length
,
1696 urb
->number_of_packets
, urb
->interval
,
1700 /* allocate ITDs w/o locking anything */
1701 status
= itd_urb_transaction (stream
, ehci
, urb
, mem_flags
);
1702 if (unlikely (status
< 0)) {
1703 ehci_dbg (ehci
, "can't init itds\n");
1707 /* schedule ... need to lock */
1708 spin_lock_irqsave (&ehci
->lock
, flags
);
1709 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE
,
1710 &ehci_to_hcd(ehci
)->flags
))) {
1711 status
= -ESHUTDOWN
;
1712 goto done_not_linked
;
1714 status
= usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci
), urb
);
1715 if (unlikely(status
))
1716 goto done_not_linked
;
1717 status
= iso_stream_schedule(ehci
, urb
, stream
);
1718 if (likely (status
== 0))
1719 itd_link_urb (ehci
, urb
, ehci
->periodic_size
<< 3, stream
);
1721 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci
), urb
);
1723 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1726 if (unlikely (status
< 0))
1727 iso_stream_put (ehci
, stream
);
1731 /*-------------------------------------------------------------------------*/
1734 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1735 * TTs in USB 2.0 hubs. These need microframe scheduling.
1740 struct ehci_hcd
*ehci
,
1741 struct ehci_iso_sched
*iso_sched
,
1742 struct ehci_iso_stream
*stream
,
1747 dma_addr_t dma
= urb
->transfer_dma
;
1749 /* how many frames are needed for these transfers */
1750 iso_sched
->span
= urb
->number_of_packets
* stream
->interval
;
1752 /* figure out per-frame sitd fields that we'll need later
1753 * when we fit new sitds into the schedule.
1755 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1756 struct ehci_iso_packet
*packet
= &iso_sched
->packet
[i
];
1761 length
= urb
->iso_frame_desc
[i
].length
& 0x03ff;
1762 buf
= dma
+ urb
->iso_frame_desc
[i
].offset
;
1764 trans
= SITD_STS_ACTIVE
;
1765 if (((i
+ 1) == urb
->number_of_packets
)
1766 && !(urb
->transfer_flags
& URB_NO_INTERRUPT
))
1768 trans
|= length
<< 16;
1769 packet
->transaction
= cpu_to_hc32(ehci
, trans
);
1771 /* might need to cross a buffer page within a td */
1773 packet
->buf1
= (buf
+ length
) & ~0x0fff;
1774 if (packet
->buf1
!= (buf
& ~(u64
)0x0fff))
1777 /* OUT uses multiple start-splits */
1778 if (stream
->bEndpointAddress
& USB_DIR_IN
)
1780 length
= (length
+ 187) / 188;
1781 if (length
> 1) /* BEGIN vs ALL */
1783 packet
->buf1
|= length
;
1788 sitd_urb_transaction (
1789 struct ehci_iso_stream
*stream
,
1790 struct ehci_hcd
*ehci
,
1795 struct ehci_sitd
*sitd
;
1796 dma_addr_t sitd_dma
;
1798 struct ehci_iso_sched
*iso_sched
;
1799 unsigned long flags
;
1801 iso_sched
= iso_sched_alloc (urb
->number_of_packets
, mem_flags
);
1802 if (iso_sched
== NULL
)
1805 sitd_sched_init(ehci
, iso_sched
, stream
, urb
);
1807 /* allocate/init sITDs */
1808 spin_lock_irqsave (&ehci
->lock
, flags
);
1809 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1811 /* NOTE: for now, we don't try to handle wraparound cases
1812 * for IN (using sitd->hw_backpointer, like a FSTN), which
1813 * means we never need two sitds for full speed packets.
1816 /* free_list.next might be cache-hot ... but maybe
1817 * the HC caches it too. avoid that issue for now.
1820 /* prefer previously-allocated sitds */
1821 if (!list_empty(&stream
->free_list
)) {
1822 sitd
= list_entry (stream
->free_list
.prev
,
1823 struct ehci_sitd
, sitd_list
);
1824 list_del (&sitd
->sitd_list
);
1825 sitd_dma
= sitd
->sitd_dma
;
1827 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1828 sitd
= dma_pool_alloc (ehci
->sitd_pool
, mem_flags
,
1830 spin_lock_irqsave (&ehci
->lock
, flags
);
1832 iso_sched_free(stream
, iso_sched
);
1833 spin_unlock_irqrestore(&ehci
->lock
, flags
);
1838 memset (sitd
, 0, sizeof *sitd
);
1839 sitd
->sitd_dma
= sitd_dma
;
1840 list_add (&sitd
->sitd_list
, &iso_sched
->td_list
);
1843 /* temporarily store schedule info in hcpriv */
1844 urb
->hcpriv
= iso_sched
;
1845 urb
->error_count
= 0;
1847 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1851 /*-------------------------------------------------------------------------*/
1855 struct ehci_hcd
*ehci
,
1856 struct ehci_iso_stream
*stream
,
1857 struct ehci_sitd
*sitd
,
1858 struct ehci_iso_sched
*iso_sched
,
1862 struct ehci_iso_packet
*uf
= &iso_sched
->packet
[index
];
1863 u64 bufp
= uf
->bufp
;
1865 sitd
->hw_next
= EHCI_LIST_END(ehci
);
1866 sitd
->hw_fullspeed_ep
= stream
->address
;
1867 sitd
->hw_uframe
= stream
->splits
;
1868 sitd
->hw_results
= uf
->transaction
;
1869 sitd
->hw_backpointer
= EHCI_LIST_END(ehci
);
1872 sitd
->hw_buf
[0] = cpu_to_hc32(ehci
, bufp
);
1873 sitd
->hw_buf_hi
[0] = cpu_to_hc32(ehci
, bufp
>> 32);
1875 sitd
->hw_buf
[1] = cpu_to_hc32(ehci
, uf
->buf1
);
1878 sitd
->hw_buf_hi
[1] = cpu_to_hc32(ehci
, bufp
>> 32);
1879 sitd
->index
= index
;
1883 sitd_link (struct ehci_hcd
*ehci
, unsigned frame
, struct ehci_sitd
*sitd
)
1885 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1886 sitd
->sitd_next
= ehci
->pshadow
[frame
];
1887 sitd
->hw_next
= ehci
->periodic
[frame
];
1888 ehci
->pshadow
[frame
].sitd
= sitd
;
1889 sitd
->frame
= frame
;
1891 ehci
->periodic
[frame
] = cpu_to_hc32(ehci
, sitd
->sitd_dma
| Q_TYPE_SITD
);
1894 /* fit urb's sitds into the selected schedule slot; activate as needed */
1897 struct ehci_hcd
*ehci
,
1900 struct ehci_iso_stream
*stream
1904 unsigned next_uframe
;
1905 struct ehci_iso_sched
*sched
= urb
->hcpriv
;
1906 struct ehci_sitd
*sitd
;
1908 next_uframe
= stream
->next_uframe
;
1910 if (list_empty(&stream
->td_list
)) {
1911 /* usbfs ignores TT bandwidth */
1912 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
1913 += stream
->bandwidth
;
1915 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
1916 urb
->dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
1917 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out",
1918 (next_uframe
>> 3) % ehci
->periodic_size
,
1919 stream
->interval
, hc32_to_cpu(ehci
, stream
->splits
));
1920 stream
->start
= jiffies
;
1922 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
++;
1924 /* fill sITDs frame by frame */
1925 for (packet
= 0, sitd
= NULL
;
1926 packet
< urb
->number_of_packets
;
1929 /* ASSERT: we have all necessary sitds */
1930 BUG_ON (list_empty (&sched
->td_list
));
1932 /* ASSERT: no itds for this endpoint in this frame */
1934 sitd
= list_entry (sched
->td_list
.next
,
1935 struct ehci_sitd
, sitd_list
);
1936 list_move_tail (&sitd
->sitd_list
, &stream
->td_list
);
1937 sitd
->stream
= iso_stream_get (stream
);
1938 sitd
->urb
= usb_get_urb (urb
);
1940 sitd_patch(ehci
, stream
, sitd
, sched
, packet
);
1941 sitd_link (ehci
, (next_uframe
>> 3) % ehci
->periodic_size
,
1944 next_uframe
+= stream
->interval
<< 3;
1945 stream
->depth
+= stream
->interval
<< 3;
1947 stream
->next_uframe
= next_uframe
% mod
;
1949 /* don't need that schedule data any more */
1950 iso_sched_free (stream
, sched
);
1953 timer_action (ehci
, TIMER_IO_WATCHDOG
);
1954 if (!ehci
->periodic_sched
++)
1955 return enable_periodic (ehci
);
1959 /*-------------------------------------------------------------------------*/
1961 #define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
1962 | SITD_STS_XACT | SITD_STS_MMF)
1964 /* Process and recycle a completed SITD. Return true iff its urb completed,
1965 * and hence its completion callback probably added things to the hardware
1968 * Note that we carefully avoid recycling this descriptor until after any
1969 * completion callback runs, so that it won't be reused quickly. That is,
1970 * assuming (a) no more than two urbs per frame on this endpoint, and also
1971 * (b) only this endpoint's completions submit URBs. It seems some silicon
1972 * corrupts things if you reuse completed descriptors very quickly...
1976 struct ehci_hcd
*ehci
,
1977 struct ehci_sitd
*sitd
1979 struct urb
*urb
= sitd
->urb
;
1980 struct usb_iso_packet_descriptor
*desc
;
1983 struct ehci_iso_stream
*stream
= sitd
->stream
;
1984 struct usb_device
*dev
;
1985 unsigned retval
= false;
1987 urb_index
= sitd
->index
;
1988 desc
= &urb
->iso_frame_desc
[urb_index
];
1989 t
= hc32_to_cpup(ehci
, &sitd
->hw_results
);
1991 /* report transfer status */
1992 if (t
& SITD_ERRS
) {
1994 if (t
& SITD_STS_DBE
)
1995 desc
->status
= usb_pipein (urb
->pipe
)
1996 ? -ENOSR
/* hc couldn't read */
1997 : -ECOMM
; /* hc couldn't write */
1998 else if (t
& SITD_STS_BABBLE
)
1999 desc
->status
= -EOVERFLOW
;
2000 else /* XACT, MMF, etc */
2001 desc
->status
= -EPROTO
;
2004 desc
->actual_length
= desc
->length
- SITD_LENGTH (t
);
2006 stream
->depth
-= stream
->interval
<< 3;
2008 /* handle completion now? */
2009 if ((urb_index
+ 1) != urb
->number_of_packets
)
2012 /* ASSERT: it's really the last sitd for this urb
2013 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2014 BUG_ON (sitd->urb == urb);
2017 /* give urb back to the driver; completion often (re)submits */
2019 ehci_urb_done(ehci
, urb
, 0);
2022 ehci
->periodic_sched
--;
2023 ehci_to_hcd(ehci
)->self
.bandwidth_isoc_reqs
--;
2025 if (list_empty (&stream
->td_list
)) {
2026 ehci_to_hcd(ehci
)->self
.bandwidth_allocated
2027 -= stream
->bandwidth
;
2029 "deschedule devp %s ep%d%s-iso\n",
2030 dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
2031 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out");
2033 iso_stream_put (ehci
, stream
);
2034 /* OK to recycle this SITD now that its completion callback ran. */
2038 sitd
->stream
= NULL
;
2039 list_move(&sitd
->sitd_list
, &stream
->free_list
);
2040 iso_stream_put(ehci
, stream
);
2046 static int sitd_submit (struct ehci_hcd
*ehci
, struct urb
*urb
,
2049 int status
= -EINVAL
;
2050 unsigned long flags
;
2051 struct ehci_iso_stream
*stream
;
2053 /* Get iso_stream head */
2054 stream
= iso_stream_find (ehci
, urb
);
2055 if (stream
== NULL
) {
2056 ehci_dbg (ehci
, "can't get iso stream\n");
2059 if (urb
->interval
!= stream
->interval
) {
2060 ehci_dbg (ehci
, "can't change iso interval %d --> %d\n",
2061 stream
->interval
, urb
->interval
);
2065 #ifdef EHCI_URB_TRACE
2067 "submit %p dev%s ep%d%s-iso len %d\n",
2068 urb
, urb
->dev
->devpath
,
2069 usb_pipeendpoint (urb
->pipe
),
2070 usb_pipein (urb
->pipe
) ? "in" : "out",
2071 urb
->transfer_buffer_length
);
2074 /* allocate SITDs */
2075 status
= sitd_urb_transaction (stream
, ehci
, urb
, mem_flags
);
2077 ehci_dbg (ehci
, "can't init sitds\n");
2081 /* schedule ... need to lock */
2082 spin_lock_irqsave (&ehci
->lock
, flags
);
2083 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE
,
2084 &ehci_to_hcd(ehci
)->flags
))) {
2085 status
= -ESHUTDOWN
;
2086 goto done_not_linked
;
2088 status
= usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci
), urb
);
2089 if (unlikely(status
))
2090 goto done_not_linked
;
2091 status
= iso_stream_schedule(ehci
, urb
, stream
);
2093 sitd_link_urb (ehci
, urb
, ehci
->periodic_size
<< 3, stream
);
2095 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci
), urb
);
2097 spin_unlock_irqrestore (&ehci
->lock
, flags
);
2101 iso_stream_put (ehci
, stream
);
2105 /*-------------------------------------------------------------------------*/
2108 scan_periodic (struct ehci_hcd
*ehci
)
2110 unsigned now_uframe
, frame
, clock
, clock_frame
, 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;
2126 clock_frame
= clock
>> 3;
2129 union ehci_shadow q
, *q_p
;
2131 unsigned incomplete
= false;
2133 frame
= now_uframe
>> 3;
2136 /* scan each element in frame's queue for completions */
2137 q_p
= &ehci
->pshadow
[frame
];
2138 hw_p
= &ehci
->periodic
[frame
];
2140 type
= Q_NEXT_TYPE(ehci
, *hw_p
);
2143 while (q
.ptr
!= NULL
) {
2145 union ehci_shadow temp
;
2148 live
= HC_IS_RUNNING (ehci_to_hcd(ehci
)->state
);
2149 switch (hc32_to_cpu(ehci
, type
)) {
2151 /* handle any completions */
2152 temp
.qh
= qh_get (q
.qh
);
2153 type
= Q_NEXT_TYPE(ehci
, q
.qh
->hw_next
);
2155 modified
= qh_completions (ehci
, temp
.qh
);
2156 if (unlikely (list_empty (&temp
.qh
->qtd_list
)))
2157 intr_deschedule (ehci
, temp
.qh
);
2161 /* for "save place" FSTNs, look at QH entries
2162 * in the previous frame for completions.
2164 if (q
.fstn
->hw_prev
!= EHCI_LIST_END(ehci
)) {
2165 dbg ("ignoring completions from FSTNs");
2167 type
= Q_NEXT_TYPE(ehci
, q
.fstn
->hw_next
);
2168 q
= q
.fstn
->fstn_next
;
2171 /* If this ITD is still active, leave it for
2172 * later processing ... check the next entry.
2173 * No need to check for activity unless the
2176 if (frame
== clock_frame
&& live
) {
2178 for (uf
= 0; uf
< 8; uf
++) {
2179 if (q
.itd
->hw_transaction
[uf
] &
2185 q_p
= &q
.itd
->itd_next
;
2186 hw_p
= &q
.itd
->hw_next
;
2187 type
= Q_NEXT_TYPE(ehci
,
2194 /* Take finished ITDs out of the schedule
2195 * and process them: recycle, maybe report
2196 * URB completion. HC won't cache the
2197 * pointer for much longer, if at all.
2199 *q_p
= q
.itd
->itd_next
;
2200 *hw_p
= q
.itd
->hw_next
;
2201 type
= Q_NEXT_TYPE(ehci
, q
.itd
->hw_next
);
2203 modified
= itd_complete (ehci
, q
.itd
);
2207 /* If this SITD is still active, leave it for
2208 * later processing ... check the next entry.
2209 * No need to check for activity unless the
2212 if (frame
== clock_frame
&& live
&&
2213 (q
.sitd
->hw_results
&
2214 SITD_ACTIVE(ehci
))) {
2216 q_p
= &q
.sitd
->sitd_next
;
2217 hw_p
= &q
.sitd
->hw_next
;
2218 type
= Q_NEXT_TYPE(ehci
,
2224 /* Take finished SITDs out of the schedule
2225 * and process them: recycle, maybe report
2228 *q_p
= q
.sitd
->sitd_next
;
2229 *hw_p
= q
.sitd
->hw_next
;
2230 type
= Q_NEXT_TYPE(ehci
, q
.sitd
->hw_next
);
2232 modified
= sitd_complete (ehci
, q
.sitd
);
2236 dbg ("corrupt type %d frame %d shadow %p",
2237 type
, frame
, q
.ptr
);
2242 /* assume completion callbacks modify the queue */
2243 if (unlikely (modified
)) {
2244 if (likely(ehci
->periodic_sched
> 0))
2246 /* maybe we can short-circuit this scan! */
2247 disable_periodic(ehci
);
2253 /* If we can tell we caught up to the hardware, stop now.
2254 * We can't advance our scan without collecting the ISO
2255 * transfers that are still pending in this frame.
2257 if (incomplete
&& HC_IS_RUNNING(ehci_to_hcd(ehci
)->state
)) {
2258 ehci
->next_uframe
= now_uframe
;
2262 // FIXME: this assumes we won't get lapped when
2263 // latencies climb; that should be rare, but...
2264 // detect it, and just go all the way around.
2265 // FLR might help detect this case, so long as latencies
2266 // don't exceed periodic_size msec (default 1.024 sec).
2268 // FIXME: likewise assumes HC doesn't halt mid-scan
2270 if (now_uframe
== clock
) {
2273 if (!HC_IS_RUNNING (ehci_to_hcd(ehci
)->state
)
2274 || ehci
->periodic_sched
== 0)
2276 ehci
->next_uframe
= now_uframe
;
2277 now
= ehci_readl(ehci
, &ehci
->regs
->frame_index
) % mod
;
2278 if (now_uframe
== now
)
2281 /* rescan the rest of this frame, then ... */
2283 clock_frame
= clock
>> 3;