blob: 66259dc7822e90145f7e286ca8ff5213a131829f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2001-2004 by David Brownell
3 * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers
David Brownell53bd6a62006-08-30 14:50:06 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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.
9 *
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
13 * for more details.
14 *
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.
18 */
19
20/* this file is part of ehci-hcd.c */
21
22/*-------------------------------------------------------------------------*/
23
24/*
25 * EHCI scheduled transaction support: interrupt, iso, split iso
26 * These are called "periodic" transactions in the EHCI spec.
27 *
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.
31 *
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.
35 */
36
37static int ehci_get_frame (struct usb_hcd *hcd);
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * periodic_next_shadow - return "next" pointer on shadow list
41 * @periodic: host pointer to qh/itd/sitd
42 * @tag: hardware tag for type of this record
43 */
44static union ehci_shadow *
Stefan Roese6dbd6822007-05-01 09:29:37 -070045periodic_next_shadow(struct ehci_hcd *ehci, union ehci_shadow *periodic,
46 __hc32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Stefan Roese6dbd6822007-05-01 09:29:37 -070048 switch (hc32_to_cpu(ehci, tag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 case Q_TYPE_QH:
50 return &periodic->qh->qh_next;
51 case Q_TYPE_FSTN:
52 return &periodic->fstn->fstn_next;
53 case Q_TYPE_ITD:
54 return &periodic->itd->itd_next;
55 // case Q_TYPE_SITD:
56 default:
57 return &periodic->sitd->sitd_next;
58 }
59}
60
Alek Du3807e262009-07-14 07:23:29 +080061static __hc32 *
62shadow_next_periodic(struct ehci_hcd *ehci, union ehci_shadow *periodic,
63 __hc32 tag)
64{
65 switch (hc32_to_cpu(ehci, tag)) {
66 /* our ehci_shadow.qh is actually software part */
67 case Q_TYPE_QH:
68 return &periodic->qh->hw->hw_next;
69 /* others are hw parts */
70 default:
71 return periodic->hw_next;
72 }
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* caller must hold ehci->lock */
76static void periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
77{
Stefan Roese6dbd6822007-05-01 09:29:37 -070078 union ehci_shadow *prev_p = &ehci->pshadow[frame];
79 __hc32 *hw_p = &ehci->periodic[frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 union ehci_shadow here = *prev_p;
81
82 /* find predecessor of "ptr"; hw and shadow lists are in sync */
83 while (here.ptr && here.ptr != ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -070084 prev_p = periodic_next_shadow(ehci, prev_p,
85 Q_NEXT_TYPE(ehci, *hw_p));
Alek Du3807e262009-07-14 07:23:29 +080086 hw_p = shadow_next_periodic(ehci, &here,
87 Q_NEXT_TYPE(ehci, *hw_p));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 here = *prev_p;
89 }
90 /* an interrupt entry (at list end) could have been shared */
91 if (!here.ptr)
92 return;
93
94 /* update shadow and hardware lists ... the old "next" pointers
95 * from ptr may still be in use, the caller updates them.
96 */
Stefan Roese6dbd6822007-05-01 09:29:37 -070097 *prev_p = *periodic_next_shadow(ehci, &here,
98 Q_NEXT_TYPE(ehci, *hw_p));
Andiry Xu3d091a62010-11-08 17:58:35 +080099
100 if (!ehci->use_dummy_qh ||
101 *shadow_next_periodic(ehci, &here, Q_NEXT_TYPE(ehci, *hw_p))
102 != EHCI_LIST_END(ehci))
103 *hw_p = *shadow_next_periodic(ehci, &here,
104 Q_NEXT_TYPE(ehci, *hw_p));
105 else
106 *hw_p = ehci->dummy->qh_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/* how many of the uframe's 125 usecs are allocated? */
110static unsigned short
111periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
112{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700113 __hc32 *hw_p = &ehci->periodic [frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 union ehci_shadow *q = &ehci->pshadow [frame];
115 unsigned usecs = 0;
Alek Du3807e262009-07-14 07:23:29 +0800116 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 while (q->ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700119 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800121 hw = q->qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* is it in the S-mask? */
Alek Du3807e262009-07-14 07:23:29 +0800123 if (hw->hw_info2 & cpu_to_hc32(ehci, 1 << uframe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 usecs += q->qh->usecs;
125 /* ... or C-mask? */
Alek Du3807e262009-07-14 07:23:29 +0800126 if (hw->hw_info2 & cpu_to_hc32(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700127 1 << (8 + uframe)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 usecs += q->qh->c_usecs;
Alek Du3807e262009-07-14 07:23:29 +0800129 hw_p = &hw->hw_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 q = &q->qh->qh_next;
131 break;
132 // case Q_TYPE_FSTN:
133 default:
134 /* for "save place" FSTNs, count the relevant INTR
135 * bandwidth from the previous frame
136 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700137 if (q->fstn->hw_prev != EHCI_LIST_END(ehci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 ehci_dbg (ehci, "ignoring FSTN cost ...\n");
139 }
140 hw_p = &q->fstn->hw_next;
141 q = &q->fstn->fstn_next;
142 break;
143 case Q_TYPE_ITD:
Karsten Wiese3b6fcfd2007-12-30 21:55:05 -0800144 if (q->itd->hw_transaction[uframe])
145 usecs += q->itd->stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 hw_p = &q->itd->hw_next;
147 q = &q->itd->itd_next;
148 break;
149 case Q_TYPE_SITD:
150 /* is it in the S-mask? (count SPLIT, DATA) */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700151 if (q->sitd->hw_uframe & cpu_to_hc32(ehci,
152 1 << uframe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (q->sitd->hw_fullspeed_ep &
Stefan Roese6dbd6822007-05-01 09:29:37 -0700154 cpu_to_hc32(ehci, 1<<31))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 usecs += q->sitd->stream->usecs;
156 else /* worst case for OUT start-split */
157 usecs += HS_USECS_ISO (188);
158 }
159
160 /* ... C-mask? (count CSPLIT, DATA) */
161 if (q->sitd->hw_uframe &
Stefan Roese6dbd6822007-05-01 09:29:37 -0700162 cpu_to_hc32(ehci, 1 << (8 + uframe))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* worst case for IN complete-split */
164 usecs += q->sitd->stream->c_usecs;
165 }
166
167 hw_p = &q->sitd->hw_next;
168 q = &q->sitd->sitd_next;
169 break;
170 }
171 }
172#ifdef DEBUG
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +0400173 if (usecs > ehci->uframe_periodic_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 ehci_err (ehci, "uframe %d sched overrun: %d usecs\n",
175 frame * 8 + uframe, usecs);
176#endif
177 return usecs;
178}
179
180/*-------------------------------------------------------------------------*/
181
182static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
183{
184 if (!dev1->tt || !dev2->tt)
185 return 0;
186 if (dev1->tt != dev2->tt)
187 return 0;
188 if (dev1->tt->multi)
189 return dev1->ttport == dev2->ttport;
190 else
191 return 1;
192}
193
Dan Streetmanba47f662006-05-24 09:39:16 -0700194#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
195
196/* Which uframe does the low/fullspeed transfer start in?
197 *
198 * The parameter is the mask of ssplits in "H-frame" terms
199 * and this returns the transfer start uframe in "B-frame" terms,
200 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
201 * will cause a transfer in "B-frame" uframe 0. "B-frames" lag
202 * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7.
203 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700204static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask)
Dan Streetmanba47f662006-05-24 09:39:16 -0700205{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700206 unsigned char smask = QH_SMASK & hc32_to_cpu(ehci, mask);
Dan Streetmanba47f662006-05-24 09:39:16 -0700207 if (!smask) {
208 ehci_err(ehci, "invalid empty smask!\n");
209 /* uframe 7 can't have bw so this will indicate failure */
210 return 7;
211 }
212 return ffs(smask) - 1;
213}
214
215static const unsigned char
Alan Stern3e619d02013-01-30 16:36:40 -0500216max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 125, 25 };
Dan Streetmanba47f662006-05-24 09:39:16 -0700217
218/* carryover low/fullspeed bandwidth that crosses uframe boundries */
219static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
220{
221 int i;
222 for (i=0; i<7; i++) {
223 if (max_tt_usecs[i] < tt_usecs[i]) {
224 tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i];
225 tt_usecs[i] = max_tt_usecs[i];
226 }
227 }
228}
229
230/* How many of the tt's periodic downstream 1000 usecs are allocated?
231 *
232 * While this measures the bandwidth in terms of usecs/uframe,
233 * the low/fullspeed bus has no notion of uframes, so any particular
234 * low/fullspeed transfer can "carry over" from one uframe to the next,
235 * since the TT just performs downstream transfers in sequence.
236 *
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800237 * For example two separate 100 usec transfers can start in the same uframe,
Dan Streetmanba47f662006-05-24 09:39:16 -0700238 * and the second one would "carry over" 75 usecs into the next uframe.
239 */
240static void
241periodic_tt_usecs (
242 struct ehci_hcd *ehci,
243 struct usb_device *dev,
244 unsigned frame,
245 unsigned short tt_usecs[8]
246)
247{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700248 __hc32 *hw_p = &ehci->periodic [frame];
Dan Streetmanba47f662006-05-24 09:39:16 -0700249 union ehci_shadow *q = &ehci->pshadow [frame];
250 unsigned char uf;
251
252 memset(tt_usecs, 0, 16);
253
254 while (q->ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700255 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
Dan Streetmanba47f662006-05-24 09:39:16 -0700256 case Q_TYPE_ITD:
257 hw_p = &q->itd->hw_next;
258 q = &q->itd->itd_next;
259 continue;
260 case Q_TYPE_QH:
261 if (same_tt(dev, q->qh->dev)) {
Alek Du3807e262009-07-14 07:23:29 +0800262 uf = tt_start_uframe(ehci, q->qh->hw->hw_info2);
Dan Streetmanba47f662006-05-24 09:39:16 -0700263 tt_usecs[uf] += q->qh->tt_usecs;
264 }
Alek Du3807e262009-07-14 07:23:29 +0800265 hw_p = &q->qh->hw->hw_next;
Dan Streetmanba47f662006-05-24 09:39:16 -0700266 q = &q->qh->qh_next;
267 continue;
268 case Q_TYPE_SITD:
269 if (same_tt(dev, q->sitd->urb->dev)) {
270 uf = tt_start_uframe(ehci, q->sitd->hw_uframe);
271 tt_usecs[uf] += q->sitd->stream->tt_usecs;
272 }
273 hw_p = &q->sitd->hw_next;
274 q = &q->sitd->sitd_next;
275 continue;
276 // case Q_TYPE_FSTN:
277 default:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700278 ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n",
279 frame);
Dan Streetmanba47f662006-05-24 09:39:16 -0700280 hw_p = &q->fstn->hw_next;
281 q = &q->fstn->fstn_next;
282 }
283 }
284
285 carryover_tt_bandwidth(tt_usecs);
286
287 if (max_tt_usecs[7] < tt_usecs[7])
288 ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
289 frame, tt_usecs[7] - max_tt_usecs[7]);
290}
291
292/*
293 * Return true if the device's tt's downstream bus is available for a
294 * periodic transfer of the specified length (usecs), starting at the
295 * specified frame/uframe. Note that (as summarized in section 11.19
296 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
297 * uframe.
298 *
299 * The uframe parameter is when the fullspeed/lowspeed transfer
300 * should be executed in "B-frame" terms, which is the same as the
301 * highspeed ssplit's uframe (which is in "H-frame" terms). For example
302 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
303 * See the EHCI spec sec 4.5 and fig 4.7.
304 *
305 * This checks if the full/lowspeed bus, at the specified starting uframe,
306 * has the specified bandwidth available, according to rules listed
307 * in USB 2.0 spec section 11.18.1 fig 11-60.
308 *
309 * This does not check if the transfer would exceed the max ssplit
310 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
311 * since proper scheduling limits ssplits to less than 16 per uframe.
312 */
313static int tt_available (
314 struct ehci_hcd *ehci,
315 unsigned period,
316 struct usb_device *dev,
317 unsigned frame,
318 unsigned uframe,
319 u16 usecs
320)
321{
322 if ((period == 0) || (uframe >= 7)) /* error */
323 return 0;
324
325 for (; frame < ehci->periodic_size; frame += period) {
326 unsigned short tt_usecs[8];
327
328 periodic_tt_usecs (ehci, dev, frame, tt_usecs);
329
330 ehci_vdbg(ehci, "tt frame %d check %d usecs start uframe %d in"
331 " schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
332 frame, usecs, uframe,
333 tt_usecs[0], tt_usecs[1], tt_usecs[2], tt_usecs[3],
334 tt_usecs[4], tt_usecs[5], tt_usecs[6], tt_usecs[7]);
335
336 if (max_tt_usecs[uframe] <= tt_usecs[uframe]) {
337 ehci_vdbg(ehci, "frame %d uframe %d fully scheduled\n",
338 frame, uframe);
339 return 0;
340 }
341
342 /* special case for isoc transfers larger than 125us:
343 * the first and each subsequent fully used uframe
344 * must be empty, so as to not illegally delay
345 * already scheduled transactions
346 */
347 if (125 < usecs) {
Dan Streetmanc065c602009-04-21 13:37:12 -0400348 int ufs = (usecs / 125);
Dan Streetmanba47f662006-05-24 09:39:16 -0700349 int i;
350 for (i = uframe; i < (uframe + ufs) && i < 8; i++)
351 if (0 < tt_usecs[i]) {
352 ehci_vdbg(ehci,
353 "multi-uframe xfer can't fit "
354 "in frame %d uframe %d\n",
355 frame, i);
356 return 0;
357 }
358 }
359
360 tt_usecs[uframe] += usecs;
361
362 carryover_tt_bandwidth(tt_usecs);
363
364 /* fail if the carryover pushed bw past the last uframe's limit */
365 if (max_tt_usecs[7] < tt_usecs[7]) {
366 ehci_vdbg(ehci,
367 "tt unavailable usecs %d frame %d uframe %d\n",
368 usecs, frame, uframe);
369 return 0;
370 }
371 }
372
373 return 1;
374}
375
376#else
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/* return true iff the device's transaction translator is available
379 * for a periodic transfer starting at the specified frame, using
380 * all the uframes in the mask.
381 */
382static int tt_no_collision (
383 struct ehci_hcd *ehci,
384 unsigned period,
385 struct usb_device *dev,
386 unsigned frame,
387 u32 uf_mask
388)
389{
390 if (period == 0) /* error */
391 return 0;
392
393 /* note bandwidth wastage: split never follows csplit
394 * (different dev or endpoint) until the next uframe.
395 * calling convention doesn't make that distinction.
396 */
397 for (; frame < ehci->periodic_size; frame += period) {
398 union ehci_shadow here;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700399 __hc32 type;
Alek Du3807e262009-07-14 07:23:29 +0800400 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 here = ehci->pshadow [frame];
Stefan Roese6dbd6822007-05-01 09:29:37 -0700403 type = Q_NEXT_TYPE(ehci, ehci->periodic [frame]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700405 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 case Q_TYPE_ITD:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700407 type = Q_NEXT_TYPE(ehci, here.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 here = here.itd->itd_next;
409 continue;
410 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800411 hw = here.qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (same_tt (dev, here.qh->dev)) {
413 u32 mask;
414
Stefan Roese6dbd6822007-05-01 09:29:37 -0700415 mask = hc32_to_cpu(ehci,
Alek Du3807e262009-07-14 07:23:29 +0800416 hw->hw_info2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* "knows" no gap is needed */
418 mask |= mask >> 8;
419 if (mask & uf_mask)
420 break;
421 }
Alek Du3807e262009-07-14 07:23:29 +0800422 type = Q_NEXT_TYPE(ehci, hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 here = here.qh->qh_next;
424 continue;
425 case Q_TYPE_SITD:
426 if (same_tt (dev, here.sitd->urb->dev)) {
427 u16 mask;
428
Stefan Roese6dbd6822007-05-01 09:29:37 -0700429 mask = hc32_to_cpu(ehci, here.sitd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ->hw_uframe);
431 /* FIXME assumes no gap for IN! */
432 mask |= mask >> 8;
433 if (mask & uf_mask)
434 break;
435 }
Stefan Roese6dbd6822007-05-01 09:29:37 -0700436 type = Q_NEXT_TYPE(ehci, here.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 here = here.sitd->sitd_next;
438 continue;
439 // case Q_TYPE_FSTN:
440 default:
441 ehci_dbg (ehci,
442 "periodic frame %d bogus type %d\n",
443 frame, type);
444 }
445
446 /* collision or error */
447 return 0;
448 }
449 }
450
451 /* no collision */
452 return 1;
453}
454
Dan Streetmanba47f662006-05-24 09:39:16 -0700455#endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*-------------------------------------------------------------------------*/
458
Alan Sternb015cb72012-07-11 11:22:10 -0400459static void enable_periodic(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400461 if (ehci->periodic_count++)
Alan Sternb015cb72012-07-11 11:22:10 -0400462 return;
David Brownell01c17142008-08-26 23:35:04 -0700463
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400464 /* Stop waiting to turn off the periodic schedule */
465 ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_DISABLE_PERIODIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400467 /* Don't start the schedule until PSS is 0 */
468 ehci_poll_PSS(ehci);
Alan Stern18aafe62012-07-11 11:23:04 -0400469 turn_on_io_watchdog(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Alan Sternb015cb72012-07-11 11:22:10 -0400472static void disable_periodic(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400474 if (--ehci->periodic_count)
Alan Sternb015cb72012-07-11 11:22:10 -0400475 return;
David Brownell01c17142008-08-26 23:35:04 -0700476
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400477 /* Don't turn off the schedule until PSS is 1 */
478 ehci_poll_PSS(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481/*-------------------------------------------------------------------------*/
482
483/* periodic schedule slots have iso tds (normal or split) first, then a
484 * sparse tree for active interrupt transfers.
485 *
486 * this just links in a qh; caller guarantees uframe masks are set right.
487 * no FSTN support (yet; ehci 0.96+)
488 */
Alan Sternb015cb72012-07-11 11:22:10 -0400489static void qh_link_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 unsigned i;
492 unsigned period = qh->period;
493
494 dev_dbg (&qh->dev->dev,
495 "link qh%d-%04x/%p start %d [%d/%d us]\n",
Alek Du3807e262009-07-14 07:23:29 +0800496 period, hc32_to_cpup(ehci, &qh->hw->hw_info2)
497 & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 qh, qh->start, qh->usecs, qh->c_usecs);
499
500 /* high bandwidth, or otherwise every microframe */
501 if (period == 0)
502 period = 1;
503
504 for (i = qh->start; i < ehci->periodic_size; i += period) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700505 union ehci_shadow *prev = &ehci->pshadow[i];
506 __hc32 *hw_p = &ehci->periodic[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 union ehci_shadow here = *prev;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700508 __hc32 type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* skip the iso nodes at list head */
511 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700512 type = Q_NEXT_TYPE(ehci, *hw_p);
513 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700515 prev = periodic_next_shadow(ehci, prev, type);
Alek Du3807e262009-07-14 07:23:29 +0800516 hw_p = shadow_next_periodic(ehci, &here, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 here = *prev;
518 }
519
520 /* sorting each branch by period (slow-->fast)
521 * enables sharing interior tree nodes
522 */
523 while (here.ptr && qh != here.qh) {
524 if (qh->period > here.qh->period)
525 break;
526 prev = &here.qh->qh_next;
Alek Du3807e262009-07-14 07:23:29 +0800527 hw_p = &here.qh->hw->hw_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 here = *prev;
529 }
530 /* link in this qh, unless some earlier pass did that */
531 if (qh != here.qh) {
532 qh->qh_next = here;
533 if (here.qh)
Alek Du3807e262009-07-14 07:23:29 +0800534 qh->hw->hw_next = *hw_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 wmb ();
536 prev->qh = qh;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700537 *hw_p = QH_NEXT (ehci, qh->qh_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 }
540 qh->qh_state = QH_STATE_LINKED;
Alan Sternef4638f2009-07-31 10:41:40 -0400541 qh->xacterrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* update per-qh bandwidth for usbfs */
544 ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period
545 ? ((qh->usecs + qh->c_usecs) / qh->period)
546 : (qh->usecs * 8);
547
Alan Stern569b3942012-07-11 11:23:00 -0400548 list_add(&qh->intr_node, &ehci->intr_qh_list);
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* maybe enable periodic schedule processing */
Alan Stern569b3942012-07-11 11:23:00 -0400551 ++ehci->intr_count;
Alan Sternb015cb72012-07-11 11:22:10 -0400552 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Alan Sternb015cb72012-07-11 11:22:10 -0400555static void qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 unsigned i;
558 unsigned period;
559
Alan Sterndf202252012-07-11 11:22:26 -0400560 /*
561 * If qh is for a low/full-speed device, simply unlinking it
562 * could interfere with an ongoing split transaction. To unlink
563 * it safely would require setting the QH_INACTIVATE bit and
564 * waiting at least one frame, as described in EHCI 4.12.2.5.
565 *
566 * We won't bother with any of this. Instead, we assume that the
567 * only reason for unlinking an interrupt QH while the current URB
568 * is still active is to dequeue all the URBs (flush the whole
569 * endpoint queue).
570 *
571 * If rebalancing the periodic schedule is ever implemented, this
572 * approach will no longer be valid.
573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /* high bandwidth, or otherwise part of every microframe */
576 if ((period = qh->period) == 0)
577 period = 1;
578
579 for (i = qh->start; i < ehci->periodic_size; i += period)
580 periodic_unlink (ehci, i, qh);
581
582 /* update per-qh bandwidth for usbfs */
583 ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period
584 ? ((qh->usecs + qh->c_usecs) / qh->period)
585 : (qh->usecs * 8);
586
587 dev_dbg (&qh->dev->dev,
588 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
David Brownell7dedacf2005-08-04 18:06:41 -0700589 qh->period,
Alek Du3807e262009-07-14 07:23:29 +0800590 hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 qh, qh->start, qh->usecs, qh->c_usecs);
592
593 /* qh->qh_next still "live" to HC */
594 qh->qh_state = QH_STATE_UNLINK;
595 qh->qh_next.ptr = NULL;
Alan Stern569b3942012-07-11 11:23:00 -0400596
597 if (ehci->qh_scan_next == qh)
598 ehci->qh_scan_next = list_entry(qh->intr_node.next,
599 struct ehci_qh, intr_node);
600 list_del(&qh->intr_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Alan Sterndf202252012-07-11 11:22:26 -0400603static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Alan Sterna448c9d2009-08-19 12:22:44 -0400605 /* If the QH isn't linked then there's nothing we can do
606 * unless we were called during a giveback, in which case
607 * qh_completions() has to deal with it.
608 */
609 if (qh->qh_state != QH_STATE_LINKED) {
610 if (qh->qh_state == QH_STATE_COMPLETING)
611 qh->needs_rescan = 1;
612 return;
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 qh_unlink_periodic (ehci, qh);
616
Alan Sterndf202252012-07-11 11:22:26 -0400617 /* Make sure the unlinks are visible before starting the timer */
618 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Alan Sterndf202252012-07-11 11:22:26 -0400620 /*
621 * The EHCI spec doesn't say how long it takes the controller to
622 * stop accessing an unlinked interrupt QH. The timer delay is
623 * 9 uframes; presumably that will be long enough.
624 */
625 qh->unlink_cycle = ehci->intr_unlink_cycle;
626
627 /* New entries go at the end of the intr_unlink list */
628 if (ehci->intr_unlink)
629 ehci->intr_unlink_last->unlink_next = qh;
630 else
631 ehci->intr_unlink = qh;
632 ehci->intr_unlink_last = qh;
633
634 if (ehci->intr_unlinking)
635 ; /* Avoid recursive calls */
636 else if (ehci->rh_state < EHCI_RH_RUNNING)
637 ehci_handle_intr_unlinks(ehci);
638 else if (ehci->intr_unlink == qh) {
639 ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
640 ++ehci->intr_unlink_cycle;
641 }
642}
643
644static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
645{
646 struct ehci_qh_hw *hw = qh->hw;
647 int rc;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 qh->qh_state = QH_STATE_IDLE;
Alek Du3807e262009-07-14 07:23:29 +0800650 hw->hw_next = EHCI_LIST_END(ehci);
Alan Sterna448c9d2009-08-19 12:22:44 -0400651
652 qh_completions(ehci, qh);
653
654 /* reschedule QH iff another request is queued */
Alan Sterndf202252012-07-11 11:22:26 -0400655 if (!list_empty(&qh->qtd_list) && ehci->rh_state == EHCI_RH_RUNNING) {
Alan Sterna448c9d2009-08-19 12:22:44 -0400656 rc = qh_schedule(ehci, qh);
657
658 /* An error here likely indicates handshake failure
659 * or no space left in the schedule. Neither fault
660 * should happen often ...
661 *
662 * FIXME kill the now-dysfunctional queued urbs
663 */
664 if (rc != 0)
665 ehci_err(ehci, "can't reschedule qh %p, err %d\n",
666 qh, rc);
667 }
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400668
669 /* maybe turn off periodic schedule */
Alan Stern569b3942012-07-11 11:23:00 -0400670 --ehci->intr_count;
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400671 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/*-------------------------------------------------------------------------*/
675
676static int check_period (
David Brownell53bd6a62006-08-30 14:50:06 -0700677 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned frame,
679 unsigned uframe,
680 unsigned period,
681 unsigned usecs
682) {
683 int claimed;
684
685 /* complete split running into next frame?
686 * given FSTN support, we could sometimes check...
687 */
688 if (uframe >= 8)
689 return 0;
690
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +0400691 /* convert "usecs we need" to "max already claimed" */
692 usecs = ehci->uframe_periodic_max - usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* we "know" 2 and 4 uframe intervals were rejected; so
695 * for period 0, check _every_ microframe in the schedule.
696 */
697 if (unlikely (period == 0)) {
698 do {
699 for (uframe = 0; uframe < 7; uframe++) {
700 claimed = periodic_usecs (ehci, frame, uframe);
701 if (claimed > usecs)
702 return 0;
703 }
704 } while ((frame += 1) < ehci->periodic_size);
705
706 /* just check the specified uframe, at that period */
707 } else {
708 do {
709 claimed = periodic_usecs (ehci, frame, uframe);
710 if (claimed > usecs)
711 return 0;
712 } while ((frame += period) < ehci->periodic_size);
713 }
714
715 // success!
716 return 1;
717}
718
719static int check_intr_schedule (
David Brownell53bd6a62006-08-30 14:50:06 -0700720 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 unsigned frame,
722 unsigned uframe,
723 const struct ehci_qh *qh,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700724 __hc32 *c_maskp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725)
726{
David Brownell53bd6a62006-08-30 14:50:06 -0700727 int retval = -ENOSPC;
Dan Streetmanba47f662006-05-24 09:39:16 -0700728 u8 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
731 goto done;
732
733 if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
734 goto done;
735 if (!qh->c_usecs) {
736 retval = 0;
737 *c_maskp = 0;
738 goto done;
739 }
740
Dan Streetmanba47f662006-05-24 09:39:16 -0700741#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
742 if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
743 qh->tt_usecs)) {
744 unsigned i;
745
746 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
747 for (i=uframe+1; i<8 && i<uframe+4; i++)
748 if (!check_period (ehci, frame, i,
749 qh->period, qh->c_usecs))
750 goto done;
751 else
752 mask |= 1 << i;
753
754 retval = 0;
755
Stefan Roese6dbd6822007-05-01 09:29:37 -0700756 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Dan Streetmanba47f662006-05-24 09:39:16 -0700757 }
758#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /* Make sure this tt's buffer is also available for CSPLITs.
760 * We pessimize a bit; probably the typical full speed case
761 * doesn't need the second CSPLIT.
David Brownell53bd6a62006-08-30 14:50:06 -0700762 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * NOTE: both SPLIT and CSPLIT could be checked in just
764 * one smart pass...
765 */
766 mask = 0x03 << (uframe + qh->gap_uf);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700767 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 mask |= 1 << uframe;
770 if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
771 if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
772 qh->period, qh->c_usecs))
773 goto done;
774 if (!check_period (ehci, frame, uframe + qh->gap_uf,
775 qh->period, qh->c_usecs))
776 goto done;
777 retval = 0;
778 }
Dan Streetmanba47f662006-05-24 09:39:16 -0700779#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780done:
781 return retval;
782}
783
784/* "first fit" scheduling policy used the first time through,
785 * or when the previous schedule slot can't be re-used.
786 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700787static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
David Brownell53bd6a62006-08-30 14:50:06 -0700789 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 unsigned uframe;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700791 __hc32 c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
Alek Du3807e262009-07-14 07:23:29 +0800793 struct ehci_qh_hw *hw = qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Alek Du3807e262009-07-14 07:23:29 +0800795 hw->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 frame = qh->start;
797
798 /* reuse the previous schedule slots, if we can */
799 if (frame < qh->period) {
Alek Du3807e262009-07-14 07:23:29 +0800800 uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 status = check_intr_schedule (ehci, frame, --uframe,
802 qh, &c_mask);
803 } else {
804 uframe = 0;
805 c_mask = 0;
806 status = -ENOSPC;
807 }
808
809 /* else scan the schedule to find a group of slots such that all
810 * uframes have enough periodic bandwidth available.
811 */
812 if (status) {
813 /* "normal" case, uframing flexible except with splits */
814 if (qh->period) {
Alan Stern68335e82009-05-22 17:02:33 -0400815 int i;
816
817 for (i = qh->period; status && i > 0; --i) {
818 frame = ++ehci->random_frame % qh->period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 for (uframe = 0; uframe < 8; uframe++) {
820 status = check_intr_schedule (ehci,
821 frame, uframe, qh,
822 &c_mask);
823 if (status == 0)
824 break;
825 }
Alan Stern68335e82009-05-22 17:02:33 -0400826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 /* qh->period == 0 means every uframe */
829 } else {
830 frame = 0;
831 status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
832 }
833 if (status)
834 goto done;
835 qh->start = frame;
836
837 /* reset S-frame and (maybe) C-frame masks */
Alek Du3807e262009-07-14 07:23:29 +0800838 hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
839 hw->hw_info2 |= qh->period
Stefan Roese6dbd6822007-05-01 09:29:37 -0700840 ? cpu_to_hc32(ehci, 1 << uframe)
841 : cpu_to_hc32(ehci, QH_SMASK);
Alek Du3807e262009-07-14 07:23:29 +0800842 hw->hw_info2 |= c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 } else
844 ehci_dbg (ehci, "reused qh %p schedule\n", qh);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846done:
847 return status;
848}
849
850static int intr_submit (
851 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct urb *urb,
853 struct list_head *qtd_list,
Al Viro55016f12005-10-21 03:21:58 -0400854 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855) {
856 unsigned epnum;
857 unsigned long flags;
858 struct ehci_qh *qh;
Alan Sterne9df41c2007-08-08 11:48:02 -0400859 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct list_head empty;
861
862 /* get endpoint and transfer/schedule data */
Alan Sterne9df41c2007-08-08 11:48:02 -0400863 epnum = urb->ep->desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 spin_lock_irqsave (&ehci->lock, flags);
866
Alan Stern541c7d42010-06-22 16:39:10 -0400867 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100868 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -0400869 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100870 }
Alan Sterne9df41c2007-08-08 11:48:02 -0400871 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
872 if (unlikely(status))
873 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* get qh and force any scheduling errors */
876 INIT_LIST_HEAD (&empty);
Alan Sterne9df41c2007-08-08 11:48:02 -0400877 qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (qh == NULL) {
879 status = -ENOMEM;
880 goto done;
881 }
882 if (qh->qh_state == QH_STATE_IDLE) {
883 if ((status = qh_schedule (ehci, qh)) != 0)
884 goto done;
885 }
886
887 /* then queue the urb's tds to the qh */
Alan Sterne9df41c2007-08-08 11:48:02 -0400888 qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 BUG_ON (qh == NULL);
890
Alan Sternc1fdb682013-03-22 13:30:43 -0400891 /* stuff into the periodic schedule */
892 if (qh->qh_state == QH_STATE_IDLE) {
893 qh_refresh(ehci, qh);
894 qh_link_periodic(ehci, qh);
895 }
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* ... update usbfs periodic stats */
898 ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
899
900done:
Alan Sterne9df41c2007-08-08 11:48:02 -0400901 if (unlikely(status))
902 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
903done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 spin_unlock_irqrestore (&ehci->lock, flags);
905 if (status)
906 qtd_list_free (ehci, urb, qtd_list);
907
908 return status;
909}
910
Alan Stern569b3942012-07-11 11:23:00 -0400911static void scan_intr(struct ehci_hcd *ehci)
912{
913 struct ehci_qh *qh;
914
915 list_for_each_entry_safe(qh, ehci->qh_scan_next, &ehci->intr_qh_list,
916 intr_node) {
917 rescan:
918 /* clean any finished work for this qh */
919 if (!list_empty(&qh->qtd_list)) {
920 int temp;
921
922 /*
923 * Unlinks could happen here; completion reporting
924 * drops the lock. That's why ehci->qh_scan_next
925 * always holds the next qh to scan; if the next qh
926 * gets unlinked then ehci->qh_scan_next is adjusted
927 * in qh_unlink_periodic().
928 */
929 temp = qh_completions(ehci, qh);
930 if (unlikely(qh->needs_rescan ||
931 (list_empty(&qh->qtd_list) &&
932 qh->qh_state == QH_STATE_LINKED)))
933 start_unlink_intr(ehci, qh);
934 else if (temp != 0)
935 goto rescan;
936 }
937 }
938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/*-------------------------------------------------------------------------*/
941
942/* ehci_iso_stream ops work with both ITD and SITD */
943
944static struct ehci_iso_stream *
Al Viro55016f12005-10-21 03:21:58 -0400945iso_stream_alloc (gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct ehci_iso_stream *stream;
948
Pekka Enberg7b842b62005-09-06 15:18:34 -0700949 stream = kzalloc(sizeof *stream, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 INIT_LIST_HEAD(&stream->td_list);
952 INIT_LIST_HEAD(&stream->free_list);
953 stream->next_uframe = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955 return stream;
956}
957
958static void
959iso_stream_init (
960 struct ehci_hcd *ehci,
961 struct ehci_iso_stream *stream,
962 struct usb_device *dev,
963 int pipe,
964 unsigned interval
965)
966{
967 static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
968
969 u32 buf1;
970 unsigned epnum, maxp;
971 int is_input;
972 long bandwidth;
973
974 /*
975 * this might be a "high bandwidth" highspeed endpoint,
976 * as encoded in the ep descriptor's wMaxPacket field
977 */
978 epnum = usb_pipeendpoint (pipe);
979 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
980 maxp = usb_maxpacket(dev, pipe, !is_input);
981 if (is_input) {
982 buf1 = (1 << 11);
983 } else {
984 buf1 = 0;
985 }
986
987 /* knows about ITD vs SITD */
988 if (dev->speed == USB_SPEED_HIGH) {
989 unsigned multi = hb_mult(maxp);
990
991 stream->highspeed = 1;
992
993 maxp = max_packet(maxp);
994 buf1 |= maxp;
995 maxp *= multi;
996
Stefan Roese6dbd6822007-05-01 09:29:37 -0700997 stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
998 stream->buf1 = cpu_to_hc32(ehci, buf1);
999 stream->buf2 = cpu_to_hc32(ehci, multi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 /* usbfs wants to report the average usecs per frame tied up
1002 * when transfers on this endpoint are scheduled ...
1003 */
1004 stream->usecs = HS_USECS_ISO (maxp);
1005 bandwidth = stream->usecs * 8;
Alan Stern372dd6e2008-11-12 17:02:57 -05001006 bandwidth /= interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 } else {
1009 u32 addr;
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001010 int think_time;
Clemens Ladisch469d0222006-01-20 13:49:10 -08001011 int hs_transfers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 addr = dev->ttport << 24;
1014 if (!ehci_is_TDI(ehci)
1015 || (dev->tt->hub !=
1016 ehci_to_hcd(ehci)->self.root_hub))
1017 addr |= dev->tt->hub->devnum << 16;
1018 addr |= epnum << 8;
1019 addr |= dev->devnum;
1020 stream->usecs = HS_USECS_ISO (maxp);
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001021 think_time = dev->tt ? dev->tt->think_time : 0;
1022 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
1023 dev->speed, is_input, 1, maxp));
Clemens Ladisch469d0222006-01-20 13:49:10 -08001024 hs_transfers = max (1u, (maxp + 187) / 188);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (is_input) {
1026 u32 tmp;
1027
1028 addr |= 1 << 31;
1029 stream->c_usecs = stream->usecs;
1030 stream->usecs = HS_USECS_ISO (1);
1031 stream->raw_mask = 1;
1032
Clemens Ladisch469d0222006-01-20 13:49:10 -08001033 /* c-mask as specified in USB 2.0 11.18.4 3.c */
1034 tmp = (1 << (hs_transfers + 2)) - 1;
1035 stream->raw_mask |= tmp << (8 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 } else
Clemens Ladisch469d0222006-01-20 13:49:10 -08001037 stream->raw_mask = smask_out [hs_transfers - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 bandwidth = stream->usecs + stream->c_usecs;
Alan Stern372dd6e2008-11-12 17:02:57 -05001039 bandwidth /= interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 /* stream->splits gets created from raw_mask later */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001042 stream->address = cpu_to_hc32(ehci, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044 stream->bandwidth = bandwidth;
1045
1046 stream->udev = dev;
1047
1048 stream->bEndpointAddress = is_input | epnum;
1049 stream->interval = interval;
1050 stream->maxp = maxp;
1051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053static struct ehci_iso_stream *
1054iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1055{
1056 unsigned epnum;
1057 struct ehci_iso_stream *stream;
1058 struct usb_host_endpoint *ep;
1059 unsigned long flags;
1060
1061 epnum = usb_pipeendpoint (urb->pipe);
1062 if (usb_pipein(urb->pipe))
1063 ep = urb->dev->ep_in[epnum];
1064 else
1065 ep = urb->dev->ep_out[epnum];
1066
1067 spin_lock_irqsave (&ehci->lock, flags);
1068 stream = ep->hcpriv;
1069
1070 if (unlikely (stream == NULL)) {
1071 stream = iso_stream_alloc(GFP_ATOMIC);
1072 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ep->hcpriv = stream;
1074 stream->ep = ep;
1075 iso_stream_init(ehci, stream, urb->dev, urb->pipe,
1076 urb->interval);
1077 }
1078
Clemens Ladisch1082f572010-03-01 17:18:56 +01001079 /* if dev->ep [epnum] is a QH, hw is set */
1080 } else if (unlikely (stream->hw != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1082 urb->dev->devpath, epnum,
1083 usb_pipein(urb->pipe) ? "in" : "out");
1084 stream = NULL;
1085 }
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 spin_unlock_irqrestore (&ehci->lock, flags);
1088 return stream;
1089}
1090
1091/*-------------------------------------------------------------------------*/
1092
1093/* ehci_iso_sched ops can be ITD-only or SITD-only */
1094
1095static struct ehci_iso_sched *
Al Viro55016f12005-10-21 03:21:58 -04001096iso_sched_alloc (unsigned packets, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 struct ehci_iso_sched *iso_sched;
1099 int size = sizeof *iso_sched;
1100
1101 size += packets * sizeof (struct ehci_iso_packet);
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001102 iso_sched = kzalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (likely (iso_sched != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 INIT_LIST_HEAD (&iso_sched->td_list);
1105 }
1106 return iso_sched;
1107}
1108
1109static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001110itd_sched_init(
1111 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 struct ehci_iso_sched *iso_sched,
1113 struct ehci_iso_stream *stream,
1114 struct urb *urb
1115)
1116{
1117 unsigned i;
1118 dma_addr_t dma = urb->transfer_dma;
1119
1120 /* how many uframes are needed for these transfers */
1121 iso_sched->span = urb->number_of_packets * stream->interval;
1122
1123 /* figure out per-uframe itd fields that we'll need later
1124 * when we fit new itds into the schedule.
1125 */
1126 for (i = 0; i < urb->number_of_packets; i++) {
1127 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1128 unsigned length;
1129 dma_addr_t buf;
1130 u32 trans;
1131
1132 length = urb->iso_frame_desc [i].length;
1133 buf = dma + urb->iso_frame_desc [i].offset;
1134
1135 trans = EHCI_ISOC_ACTIVE;
1136 trans |= buf & 0x0fff;
1137 if (unlikely (((i + 1) == urb->number_of_packets))
1138 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1139 trans |= EHCI_ITD_IOC;
1140 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001141 uframe->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
David Brownell77078572005-05-28 10:46:18 -07001143 /* might need to cross a buffer page within a uframe */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 uframe->bufp = (buf & ~(u64)0x0fff);
1145 buf += length;
1146 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1147 uframe->cross = 1;
1148 }
1149}
1150
1151static void
1152iso_sched_free (
1153 struct ehci_iso_stream *stream,
1154 struct ehci_iso_sched *iso_sched
1155)
1156{
1157 if (!iso_sched)
1158 return;
1159 // caller must hold ehci->lock!
1160 list_splice (&iso_sched->td_list, &stream->free_list);
1161 kfree (iso_sched);
1162}
1163
1164static int
1165itd_urb_transaction (
1166 struct ehci_iso_stream *stream,
1167 struct ehci_hcd *ehci,
1168 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001169 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170)
1171{
1172 struct ehci_itd *itd;
1173 dma_addr_t itd_dma;
1174 int i;
1175 unsigned num_itds;
1176 struct ehci_iso_sched *sched;
1177 unsigned long flags;
1178
1179 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1180 if (unlikely (sched == NULL))
1181 return -ENOMEM;
1182
Stefan Roese6dbd6822007-05-01 09:29:37 -07001183 itd_sched_init(ehci, sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (urb->interval < 8)
1186 num_itds = 1 + (sched->span + 7) / 8;
1187 else
1188 num_itds = urb->number_of_packets;
1189
1190 /* allocate/init ITDs */
1191 spin_lock_irqsave (&ehci->lock, flags);
1192 for (i = 0; i < num_itds; i++) {
1193
Alan Stern55934eb2012-07-11 11:22:35 -04001194 /*
1195 * Use iTDs from the free list, but not iTDs that may
1196 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 */
Alan Stern55934eb2012-07-11 11:22:35 -04001198 if (likely(!list_empty(&stream->free_list))) {
1199 itd = list_first_entry(&stream->free_list,
Stefan Roese6dbd6822007-05-01 09:29:37 -07001200 struct ehci_itd, itd_list);
Alan Sternf4289072012-07-11 11:23:07 -04001201 if (itd->frame == ehci->now_frame)
Alan Stern55934eb2012-07-11 11:22:35 -04001202 goto alloc_itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 list_del (&itd->itd_list);
1204 itd_dma = itd->itd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001205 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001206 alloc_itd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 spin_unlock_irqrestore (&ehci->lock, flags);
1208 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1209 &itd_dma);
1210 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001211 if (!itd) {
1212 iso_sched_free(stream, sched);
1213 spin_unlock_irqrestore(&ehci->lock, flags);
1214 return -ENOMEM;
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 memset (itd, 0, sizeof *itd);
1219 itd->itd_dma = itd_dma;
1220 list_add (&itd->itd_list, &sched->td_list);
1221 }
1222 spin_unlock_irqrestore (&ehci->lock, flags);
1223
1224 /* temporarily store schedule info in hcpriv */
1225 urb->hcpriv = sched;
1226 urb->error_count = 0;
1227 return 0;
1228}
1229
1230/*-------------------------------------------------------------------------*/
1231
1232static inline int
1233itd_slot_ok (
1234 struct ehci_hcd *ehci,
1235 u32 mod,
1236 u32 uframe,
1237 u8 usecs,
1238 u32 period
1239)
1240{
1241 uframe %= period;
1242 do {
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001243 /* can't commit more than uframe_periodic_max usec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001245 > (ehci->uframe_periodic_max - usecs))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return 0;
1247
1248 /* we know urb->interval is 2^N uframes */
1249 uframe += period;
1250 } while (uframe < mod);
1251 return 1;
1252}
1253
1254static inline int
1255sitd_slot_ok (
1256 struct ehci_hcd *ehci,
1257 u32 mod,
1258 struct ehci_iso_stream *stream,
1259 u32 uframe,
1260 struct ehci_iso_sched *sched,
1261 u32 period_uframes
1262)
1263{
1264 u32 mask, tmp;
1265 u32 frame, uf;
1266
1267 mask = stream->raw_mask << (uframe & 7);
1268
1269 /* for IN, don't wrap CSPLIT into the next frame */
1270 if (mask & ~0xffff)
1271 return 0;
1272
Alan Stern65b8e5c2012-05-14 13:47:20 -04001273 /* check bandwidth */
1274 uframe %= period_uframes;
1275 frame = uframe >> 3;
1276
1277#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1278 /* The tt's fullspeed bus bandwidth must be available.
1279 * tt_available scheduling guarantees 10+% for control/bulk.
1280 */
1281 uf = uframe & 7;
1282 if (!tt_available(ehci, period_uframes >> 3,
1283 stream->udev, frame, uf, stream->tt_usecs))
1284 return 0;
1285#else
1286 /* tt must be idle for start(s), any gap, and csplit.
1287 * assume scheduling slop leaves 10+% for control/bulk.
1288 */
1289 if (!tt_no_collision(ehci, period_uframes >> 3,
1290 stream->udev, frame, mask))
1291 return 0;
1292#endif
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* this multi-pass logic is simple, but performance may
1295 * suffer when the schedule data isn't cached.
1296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 do {
1298 u32 max_used;
1299
1300 frame = uframe >> 3;
1301 uf = uframe & 7;
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /* check starts (OUT uses more than one) */
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001304 max_used = ehci->uframe_periodic_max - stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
1306 if (periodic_usecs (ehci, frame, uf) > max_used)
1307 return 0;
1308 }
1309
1310 /* for IN, check CSPLIT */
1311 if (stream->c_usecs) {
Clemens Ladisch0c734622006-01-22 10:32:49 -08001312 uf = uframe & 7;
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001313 max_used = ehci->uframe_periodic_max - stream->c_usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 do {
1315 tmp = 1 << uf;
1316 tmp <<= 8;
1317 if ((stream->raw_mask & tmp) == 0)
1318 continue;
1319 if (periodic_usecs (ehci, frame, uf)
1320 > max_used)
1321 return 0;
1322 } while (++uf < 8);
1323 }
1324
1325 /* we know urb->interval is 2^N uframes */
1326 uframe += period_uframes;
1327 } while (uframe < mod);
1328
Stefan Roese6dbd6822007-05-01 09:29:37 -07001329 stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return 1;
1331}
1332
1333/*
1334 * This scheduler plans almost as far into the future as it has actual
1335 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1336 * "as small as possible" to be cache-friendlier.) That limits the size
1337 * transfers you can stream reliably; avoid more than 64 msec per urb.
1338 * Also avoid queue depths of less than ehci's worst irq latency (affected
1339 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1340 * and other factors); or more than about 230 msec total (for portability,
1341 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1342 */
1343
Alan Sternc3ee9b72012-09-28 16:01:23 -04001344#define SCHEDULING_DELAY 40 /* microframes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346static int
1347iso_stream_schedule (
1348 struct ehci_hcd *ehci,
1349 struct urb *urb,
1350 struct ehci_iso_stream *stream
1351)
1352{
Alan Sternc3ee9b72012-09-28 16:01:23 -04001353 u32 now, base, next, start, period, span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int status;
1355 unsigned mod = ehci->periodic_size << 3;
1356 struct ehci_iso_sched *sched = urb->hcpriv;
1357
Alan Sternffda0802010-07-14 11:03:46 -04001358 period = urb->interval;
1359 span = sched->span;
1360 if (!stream->highspeed) {
1361 period <<= 3;
1362 span <<= 3;
1363 }
1364
Alan Stern68aa95d2011-10-12 10:39:14 -04001365 now = ehci_read_frame_index(ehci) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Alan Sternb40e43f2008-05-20 16:59:10 -04001367 /* Typical case: reuse current schedule, stream is still active.
1368 * Hopefully there are no gaps from the host falling behind
Alan Stern4005ad42012-10-01 10:32:01 -04001369 * (irq delays etc). If there are, the behavior depends on
1370 * whether URB_ISO_ASAP is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 */
1372 if (likely (!list_empty (&stream->td_list))) {
Sarah Sharpdccd5742009-10-27 10:55:05 -07001373
Alan Stern98cae422012-09-28 16:01:34 -04001374 /* Take the isochronous scheduling threshold into account */
1375 if (ehci->i_thresh)
1376 next = now + ehci->i_thresh; /* uframe cache */
Sarah Sharpdccd5742009-10-27 10:55:05 -07001377 else
Alan Stern98cae422012-09-28 16:01:34 -04001378 next = (now + 2 + 7) & ~0x07; /* full frame cache */
Alan Sternb40e43f2008-05-20 16:59:10 -04001379
Alan Sternc3ee9b72012-09-28 16:01:23 -04001380 /*
1381 * Use ehci->last_iso_frame as the base. There can't be any
1382 * TDs scheduled for earlier than that.
Alan Stern1fb2e052010-07-14 11:03:53 -04001383 */
Alan Sternc3ee9b72012-09-28 16:01:23 -04001384 base = ehci->last_iso_frame << 3;
1385 next = (next - base) & (mod - 1);
1386 start = (stream->next_uframe - base) & (mod - 1);
1387
1388 /* Is the schedule already full? */
1389 if (unlikely(start < period)) {
1390 ehci_dbg(ehci, "iso sched full %p (%u-%u < %u mod %u)\n",
1391 urb, stream->next_uframe, base,
1392 period, mod);
1393 status = -ENOSPC;
Alan Sternb40e43f2008-05-20 16:59:10 -04001394 goto fail;
1395 }
Alan Sternc3ee9b72012-09-28 16:01:23 -04001396
Alan Stern4005ad42012-10-01 10:32:01 -04001397 /* Behind the scheduling threshold? */
1398 if (unlikely(start < next)) {
1399
1400 /* USB_ISO_ASAP: Round up to the first available slot */
1401 if (urb->transfer_flags & URB_ISO_ASAP)
1402 start += (next - start + period - 1) & -period;
1403
1404 /*
1405 * Not ASAP: Use the next slot in the stream. If
1406 * the entire URB falls before the threshold, fail.
1407 */
1408 else if (start + span - period < next) {
1409 ehci_dbg(ehci, "iso urb late %p (%u+%u < %u)\n",
1410 urb, start + base,
1411 span - period, next + base);
1412 status = -EXDEV;
1413 goto fail;
1414 }
1415 }
Alan Sternc3ee9b72012-09-28 16:01:23 -04001416
1417 start += base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419
1420 /* need to schedule; when's the next (u)frame we could start?
1421 * this is bigger than ehci->i_thresh allows; scheduling itself
Alan Sternc3ee9b72012-09-28 16:01:23 -04001422 * isn't free, the delay should handle reasonably slow cpus. it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * can also help high bandwidth if the dma and irq loads don't
1424 * jump until after the queue is primed.
1425 */
Alan Stern1fb2e052010-07-14 11:03:53 -04001426 else {
Matthieu CASTETe3420902011-11-28 11:30:22 +01001427 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Alan Sternc3ee9b72012-09-28 16:01:23 -04001429 base = now & ~0x07;
1430 start = base + SCHEDULING_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Thomas Poussevin811c9262011-10-27 18:46:48 +02001432 /* find a uframe slot with enough bandwidth.
1433 * Early uframes are more precious because full-speed
1434 * iso IN transfers can't use late uframes,
1435 * and therefore they should be allocated last.
1436 */
1437 next = start;
1438 start += period;
1439 do {
1440 start--;
Alan Stern1fb2e052010-07-14 11:03:53 -04001441 /* check schedule: enough space? */
1442 if (stream->highspeed) {
1443 if (itd_slot_ok(ehci, mod, start,
1444 stream->usecs, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001445 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001446 } else {
1447 if ((start % 8) >= 6)
1448 continue;
1449 if (sitd_slot_ok(ehci, mod, stream,
1450 start, sched, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001451 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001452 }
Matthieu CASTETe3420902011-11-28 11:30:22 +01001453 } while (start > next && !done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Alan Stern1fb2e052010-07-14 11:03:53 -04001455 /* no room in the schedule */
Matthieu CASTETe3420902011-11-28 11:30:22 +01001456 if (!done) {
Alan Sternc3ee9b72012-09-28 16:01:23 -04001457 ehci_dbg(ehci, "iso sched full %p", urb);
Alan Stern1fb2e052010-07-14 11:03:53 -04001458 status = -ENOSPC;
1459 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461 }
1462
Alan Stern1fb2e052010-07-14 11:03:53 -04001463 /* Tried to schedule too far into the future? */
Alan Sternc3ee9b72012-09-28 16:01:23 -04001464 if (unlikely(start - base + span - period >= mod)) {
1465 ehci_dbg(ehci, "request %p would overflow (%u+%u >= %u)\n",
1466 urb, start - base, span - period, mod);
Alan Stern1fb2e052010-07-14 11:03:53 -04001467 status = -EFBIG;
1468 goto fail;
1469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Alan Stern1fb2e052010-07-14 11:03:53 -04001471 stream->next_uframe = start & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /* report high speed start in uframes; full speed, in frames */
1474 urb->start_frame = stream->next_uframe;
1475 if (!stream->highspeed)
1476 urb->start_frame >>= 3;
Alan Stern569b3942012-07-11 11:23:00 -04001477
1478 /* Make sure scan_isoc() sees these */
1479 if (ehci->isoc_count == 0)
Alan Sternc3ee9b72012-09-28 16:01:23 -04001480 ehci->last_iso_frame = now >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return 0;
Alan Stern1fb2e052010-07-14 11:03:53 -04001482
1483 fail:
1484 iso_sched_free(stream, sched);
1485 urb->hcpriv = NULL;
1486 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
1489/*-------------------------------------------------------------------------*/
1490
1491static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001492itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1493 struct ehci_itd *itd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 int i;
1496
David Brownell77078572005-05-28 10:46:18 -07001497 /* it's been recently zeroed */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001498 itd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 itd->hw_bufp [0] = stream->buf0;
1500 itd->hw_bufp [1] = stream->buf1;
1501 itd->hw_bufp [2] = stream->buf2;
1502
1503 for (i = 0; i < 8; i++)
1504 itd->index[i] = -1;
1505
1506 /* All other fields are filled when scheduling */
1507}
1508
1509static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001510itd_patch(
1511 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 struct ehci_itd *itd,
1513 struct ehci_iso_sched *iso_sched,
1514 unsigned index,
David Brownell77078572005-05-28 10:46:18 -07001515 u16 uframe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516)
1517{
1518 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1519 unsigned pg = itd->pg;
1520
1521 // BUG_ON (pg == 6 && uf->cross);
1522
1523 uframe &= 0x07;
1524 itd->index [uframe] = index;
1525
Stefan Roese6dbd6822007-05-01 09:29:37 -07001526 itd->hw_transaction[uframe] = uf->transaction;
1527 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1528 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1529 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 /* iso_frame_desc[].offset must be strictly increasing */
David Brownell77078572005-05-28 10:46:18 -07001532 if (unlikely (uf->cross)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 u64 bufp = uf->bufp + 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 itd->pg = ++pg;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001536 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1537 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539}
1540
1541static inline void
1542itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1543{
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001544 union ehci_shadow *prev = &ehci->pshadow[frame];
1545 __hc32 *hw_p = &ehci->periodic[frame];
1546 union ehci_shadow here = *prev;
1547 __hc32 type = 0;
1548
1549 /* skip any iso nodes which might belong to previous microframes */
1550 while (here.ptr) {
1551 type = Q_NEXT_TYPE(ehci, *hw_p);
1552 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
1553 break;
1554 prev = periodic_next_shadow(ehci, prev, type);
1555 hw_p = shadow_next_periodic(ehci, &here, type);
1556 here = *prev;
1557 }
1558
1559 itd->itd_next = here;
1560 itd->hw_next = *hw_p;
1561 prev->itd = itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 itd->frame = frame;
1563 wmb ();
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001564 *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/* fit urb's itds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001568static void itd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 struct ehci_hcd *ehci,
1570 struct urb *urb,
1571 unsigned mod,
1572 struct ehci_iso_stream *stream
1573)
1574{
David Brownell77078572005-05-28 10:46:18 -07001575 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 unsigned next_uframe, uframe, frame;
1577 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1578 struct ehci_itd *itd;
1579
Alan Sternbccbefa2010-07-14 11:03:36 -04001580 next_uframe = stream->next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 if (unlikely (list_empty(&stream->td_list))) {
1583 ehci_to_hcd(ehci)->self.bandwidth_allocated
1584 += stream->bandwidth;
1585 ehci_vdbg (ehci,
1586 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1587 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1588 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1589 urb->interval,
1590 next_uframe >> 3, next_uframe & 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Alex He05570292010-12-07 10:10:08 +08001592
1593 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001594 if (ehci->amd_pll_fix == 1)
1595 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08001596 }
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1599
1600 /* fill iTDs uframe by uframe */
1601 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1602 if (itd == NULL) {
1603 /* ASSERT: we have all necessary itds */
1604 // BUG_ON (list_empty (&iso_sched->td_list));
1605
1606 /* ASSERT: no itds for this endpoint in this uframe */
1607
1608 itd = list_entry (iso_sched->td_list.next,
1609 struct ehci_itd, itd_list);
1610 list_move_tail (&itd->itd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001611 itd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01001612 itd->urb = urb;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001613 itd_init (ehci, stream, itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
1615
1616 uframe = next_uframe & 0x07;
1617 frame = next_uframe >> 3;
1618
Stefan Roese6dbd6822007-05-01 09:29:37 -07001619 itd_patch(ehci, itd, iso_sched, packet, uframe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 next_uframe += stream->interval;
Alan Sternbccbefa2010-07-14 11:03:36 -04001622 next_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 packet++;
1624
1625 /* link completed itds into the schedule */
1626 if (((next_uframe >> 3) != frame)
1627 || packet == urb->number_of_packets) {
Alan Sternbccbefa2010-07-14 11:03:36 -04001628 itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 itd = NULL;
1630 }
1631 }
1632 stream->next_uframe = next_uframe;
1633
1634 /* don't need that schedule data any more */
1635 iso_sched_free (stream, iso_sched);
Alan Stern2656a9a2012-11-08 10:17:01 -05001636 urb->hcpriv = stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Alan Stern569b3942012-07-11 11:23:00 -04001638 ++ehci->isoc_count;
Alan Sternb015cb72012-07-11 11:22:10 -04001639 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
1642#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1643
David Brownell30bf54e2007-12-16 22:37:40 -08001644/* Process and recycle a completed ITD. Return true iff its urb completed,
1645 * and hence its completion callback probably added things to the hardware
1646 * schedule.
1647 *
1648 * Note that we carefully avoid recycling this descriptor until after any
1649 * completion callback runs, so that it won't be reused quickly. That is,
1650 * assuming (a) no more than two urbs per frame on this endpoint, and also
1651 * (b) only this endpoint's completions submit URBs. It seems some silicon
1652 * corrupts things if you reuse completed descriptors very quickly...
1653 */
Alan Sternf4289072012-07-11 11:23:07 -04001654static bool itd_complete(struct ehci_hcd *ehci, struct ehci_itd *itd)
1655{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 struct urb *urb = itd->urb;
1657 struct usb_iso_packet_descriptor *desc;
1658 u32 t;
1659 unsigned uframe;
1660 int urb_index = -1;
1661 struct ehci_iso_stream *stream = itd->stream;
1662 struct usb_device *dev;
Alan Sternf4289072012-07-11 11:23:07 -04001663 bool retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 /* for each uframe with a packet */
1666 for (uframe = 0; uframe < 8; uframe++) {
1667 if (likely (itd->index[uframe] == -1))
1668 continue;
1669 urb_index = itd->index[uframe];
1670 desc = &urb->iso_frame_desc [urb_index];
1671
Stefan Roese6dbd6822007-05-01 09:29:37 -07001672 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 itd->hw_transaction [uframe] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 /* report transfer status */
1676 if (unlikely (t & ISO_ERRS)) {
1677 urb->error_count++;
1678 if (t & EHCI_ISOC_BUF_ERR)
1679 desc->status = usb_pipein (urb->pipe)
1680 ? -ENOSR /* hc couldn't read */
1681 : -ECOMM; /* hc couldn't write */
1682 else if (t & EHCI_ISOC_BABBLE)
1683 desc->status = -EOVERFLOW;
1684 else /* (t & EHCI_ISOC_XACTERR) */
1685 desc->status = -EPROTO;
1686
1687 /* HC need not update length with this error */
Alan Sternec6d67e2009-06-29 14:34:59 -04001688 if (!(t & EHCI_ISOC_BABBLE)) {
1689 desc->actual_length = EHCI_ITD_LENGTH(t);
1690 urb->actual_length += desc->actual_length;
1691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1693 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04001694 desc->actual_length = EHCI_ITD_LENGTH(t);
1695 urb->actual_length += desc->actual_length;
Alan Sternb40e43f2008-05-20 16:59:10 -04001696 } else {
1697 /* URB was too late */
Alan Stern4005ad42012-10-01 10:32:01 -04001698 urb->error_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 }
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /* handle completion now? */
1703 if (likely ((urb_index + 1) != urb->number_of_packets))
David Brownell30bf54e2007-12-16 22:37:40 -08001704 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 /* ASSERT: it's really the last itd for this urb
1707 list_for_each_entry (itd, &stream->td_list, itd_list)
1708 BUG_ON (itd->urb == urb);
1709 */
1710
David Brownellaa16ca32007-12-30 23:45:19 -08001711 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05001712 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04001713 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08001714 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Alan Stern569b3942012-07-11 11:23:00 -04001717 --ehci->isoc_count;
1718 disable_periodic(ehci);
1719
1720 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
Alex He05570292010-12-07 10:10:08 +08001721 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001722 if (ehci->amd_pll_fix == 1)
1723 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08001724 }
1725
Karsten Wiese508db8c2009-02-26 01:47:48 +01001726 if (unlikely(list_is_singular(&stream->td_list))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 ehci_to_hcd(ehci)->self.bandwidth_allocated
1728 -= stream->bandwidth;
1729 ehci_vdbg (ehci,
1730 "deschedule devp %s ep%d%s-iso\n",
1731 dev->devpath, stream->bEndpointAddress & 0x0f,
1732 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1733 }
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001734
David Brownell30bf54e2007-12-16 22:37:40 -08001735done:
David Brownell30bf54e2007-12-16 22:37:40 -08001736 itd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04001737
1738 /* Add to the end of the free list for later reuse */
1739 list_move_tail(&itd->itd_list, &stream->free_list);
1740
1741 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
1742 if (list_empty(&stream->td_list)) {
1743 list_splice_tail_init(&stream->free_list,
1744 &ehci->cached_itd_list);
1745 start_free_itds(ehci);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001746 }
Alan Stern55934eb2012-07-11 11:22:35 -04001747
David Brownell30bf54e2007-12-16 22:37:40 -08001748 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
1751/*-------------------------------------------------------------------------*/
1752
Olav Kongas5db539e2005-06-23 20:25:36 +03001753static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001754 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 int status = -EINVAL;
1757 unsigned long flags;
1758 struct ehci_iso_stream *stream;
1759
1760 /* Get iso_stream head */
1761 stream = iso_stream_find (ehci, urb);
1762 if (unlikely (stream == NULL)) {
1763 ehci_dbg (ehci, "can't get iso stream\n");
1764 return -ENOMEM;
1765 }
1766 if (unlikely (urb->interval != stream->interval)) {
1767 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1768 stream->interval, urb->interval);
1769 goto done;
1770 }
1771
1772#ifdef EHCI_URB_TRACE
1773 ehci_dbg (ehci,
1774 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001775 __func__, urb->dev->devpath, urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 usb_pipeendpoint (urb->pipe),
1777 usb_pipein (urb->pipe) ? "in" : "out",
1778 urb->transfer_buffer_length,
1779 urb->number_of_packets, urb->interval,
1780 stream);
1781#endif
1782
1783 /* allocate ITDs w/o locking anything */
1784 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1785 if (unlikely (status < 0)) {
1786 ehci_dbg (ehci, "can't init itds\n");
1787 goto done;
1788 }
1789
1790 /* schedule ... need to lock */
1791 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001792 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001793 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04001794 goto done_not_linked;
1795 }
1796 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1797 if (unlikely(status))
1798 goto done_not_linked;
1799 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07001800 if (likely (status == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04001802 else
1803 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001804 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001806 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return status;
1808}
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810/*-------------------------------------------------------------------------*/
1811
1812/*
1813 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1814 * TTs in USB 2.0 hubs. These need microframe scheduling.
1815 */
1816
1817static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001818sitd_sched_init(
1819 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 struct ehci_iso_sched *iso_sched,
1821 struct ehci_iso_stream *stream,
1822 struct urb *urb
1823)
1824{
1825 unsigned i;
1826 dma_addr_t dma = urb->transfer_dma;
1827
1828 /* how many frames are needed for these transfers */
1829 iso_sched->span = urb->number_of_packets * stream->interval;
1830
1831 /* figure out per-frame sitd fields that we'll need later
1832 * when we fit new sitds into the schedule.
1833 */
1834 for (i = 0; i < urb->number_of_packets; i++) {
1835 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1836 unsigned length;
1837 dma_addr_t buf;
1838 u32 trans;
1839
1840 length = urb->iso_frame_desc [i].length & 0x03ff;
1841 buf = dma + urb->iso_frame_desc [i].offset;
1842
1843 trans = SITD_STS_ACTIVE;
1844 if (((i + 1) == urb->number_of_packets)
1845 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1846 trans |= SITD_IOC;
1847 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001848 packet->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 /* might need to cross a buffer page within a td */
1851 packet->bufp = buf;
1852 packet->buf1 = (buf + length) & ~0x0fff;
1853 if (packet->buf1 != (buf & ~(u64)0x0fff))
1854 packet->cross = 1;
1855
David Brownell53bd6a62006-08-30 14:50:06 -07001856 /* OUT uses multiple start-splits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (stream->bEndpointAddress & USB_DIR_IN)
1858 continue;
1859 length = (length + 187) / 188;
1860 if (length > 1) /* BEGIN vs ALL */
1861 length |= 1 << 3;
1862 packet->buf1 |= length;
1863 }
1864}
1865
1866static int
1867sitd_urb_transaction (
1868 struct ehci_iso_stream *stream,
1869 struct ehci_hcd *ehci,
1870 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001871 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872)
1873{
1874 struct ehci_sitd *sitd;
1875 dma_addr_t sitd_dma;
1876 int i;
1877 struct ehci_iso_sched *iso_sched;
1878 unsigned long flags;
1879
1880 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1881 if (iso_sched == NULL)
1882 return -ENOMEM;
1883
Stefan Roese6dbd6822007-05-01 09:29:37 -07001884 sitd_sched_init(ehci, iso_sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 /* allocate/init sITDs */
1887 spin_lock_irqsave (&ehci->lock, flags);
1888 for (i = 0; i < urb->number_of_packets; i++) {
1889
1890 /* NOTE: for now, we don't try to handle wraparound cases
1891 * for IN (using sitd->hw_backpointer, like a FSTN), which
1892 * means we never need two sitds for full speed packets.
1893 */
1894
Alan Stern55934eb2012-07-11 11:22:35 -04001895 /*
1896 * Use siTDs from the free list, but not siTDs that may
1897 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 */
Alan Stern55934eb2012-07-11 11:22:35 -04001899 if (likely(!list_empty(&stream->free_list))) {
1900 sitd = list_first_entry(&stream->free_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct ehci_sitd, sitd_list);
Alan Sternf4289072012-07-11 11:23:07 -04001902 if (sitd->frame == ehci->now_frame)
Alan Stern55934eb2012-07-11 11:22:35 -04001903 goto alloc_sitd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 list_del (&sitd->sitd_list);
1905 sitd_dma = sitd->sitd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001906 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001907 alloc_sitd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 spin_unlock_irqrestore (&ehci->lock, flags);
1909 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1910 &sitd_dma);
1911 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001912 if (!sitd) {
1913 iso_sched_free(stream, iso_sched);
1914 spin_unlock_irqrestore(&ehci->lock, flags);
1915 return -ENOMEM;
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 memset (sitd, 0, sizeof *sitd);
1920 sitd->sitd_dma = sitd_dma;
1921 list_add (&sitd->sitd_list, &iso_sched->td_list);
1922 }
1923
1924 /* temporarily store schedule info in hcpriv */
1925 urb->hcpriv = iso_sched;
1926 urb->error_count = 0;
1927
1928 spin_unlock_irqrestore (&ehci->lock, flags);
1929 return 0;
1930}
1931
1932/*-------------------------------------------------------------------------*/
1933
1934static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001935sitd_patch(
1936 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 struct ehci_iso_stream *stream,
1938 struct ehci_sitd *sitd,
1939 struct ehci_iso_sched *iso_sched,
1940 unsigned index
1941)
1942{
1943 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1944 u64 bufp = uf->bufp;
1945
Stefan Roese6dbd6822007-05-01 09:29:37 -07001946 sitd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 sitd->hw_fullspeed_ep = stream->address;
1948 sitd->hw_uframe = stream->splits;
1949 sitd->hw_results = uf->transaction;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001950 sitd->hw_backpointer = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 bufp = uf->bufp;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001953 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
1954 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Stefan Roese6dbd6822007-05-01 09:29:37 -07001956 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (uf->cross)
1958 bufp += 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001959 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 sitd->index = index;
1961}
1962
1963static inline void
1964sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
1965{
1966 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1967 sitd->sitd_next = ehci->pshadow [frame];
1968 sitd->hw_next = ehci->periodic [frame];
1969 ehci->pshadow [frame].sitd = sitd;
1970 sitd->frame = frame;
1971 wmb ();
Stefan Roese6dbd6822007-05-01 09:29:37 -07001972 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
1975/* fit urb's sitds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001976static void sitd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 struct ehci_hcd *ehci,
1978 struct urb *urb,
1979 unsigned mod,
1980 struct ehci_iso_stream *stream
1981)
1982{
1983 int packet;
1984 unsigned next_uframe;
1985 struct ehci_iso_sched *sched = urb->hcpriv;
1986 struct ehci_sitd *sitd;
1987
1988 next_uframe = stream->next_uframe;
1989
1990 if (list_empty(&stream->td_list)) {
1991 /* usbfs ignores TT bandwidth */
1992 ehci_to_hcd(ehci)->self.bandwidth_allocated
1993 += stream->bandwidth;
1994 ehci_vdbg (ehci,
1995 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
1996 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1997 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
Alan Sternbccbefa2010-07-14 11:03:36 -04001998 (next_uframe >> 3) & (ehci->periodic_size - 1),
Stefan Roese6dbd6822007-05-01 09:29:37 -07001999 stream->interval, hc32_to_cpu(ehci, stream->splits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
Alex He05570292010-12-07 10:10:08 +08002001
2002 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002003 if (ehci->amd_pll_fix == 1)
2004 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08002005 }
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
2008
2009 /* fill sITDs frame by frame */
2010 for (packet = 0, sitd = NULL;
2011 packet < urb->number_of_packets;
2012 packet++) {
2013
2014 /* ASSERT: we have all necessary sitds */
2015 BUG_ON (list_empty (&sched->td_list));
2016
2017 /* ASSERT: no itds for this endpoint in this frame */
2018
2019 sitd = list_entry (sched->td_list.next,
2020 struct ehci_sitd, sitd_list);
2021 list_move_tail (&sitd->sitd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002022 sitd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01002023 sitd->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Stefan Roese6dbd6822007-05-01 09:29:37 -07002025 sitd_patch(ehci, stream, sitd, sched, packet);
Alan Sternbccbefa2010-07-14 11:03:36 -04002026 sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 sitd);
2028
2029 next_uframe += stream->interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
Alan Sternbccbefa2010-07-14 11:03:36 -04002031 stream->next_uframe = next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 /* don't need that schedule data any more */
2034 iso_sched_free (stream, sched);
Alan Stern2656a9a2012-11-08 10:17:01 -05002035 urb->hcpriv = stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Alan Stern569b3942012-07-11 11:23:00 -04002037 ++ehci->isoc_count;
Alan Sternb015cb72012-07-11 11:22:10 -04002038 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
2041/*-------------------------------------------------------------------------*/
2042
2043#define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
David Brownell53bd6a62006-08-30 14:50:06 -07002044 | SITD_STS_XACT | SITD_STS_MMF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
David Brownell30bf54e2007-12-16 22:37:40 -08002046/* Process and recycle a completed SITD. Return true iff its urb completed,
2047 * and hence its completion callback probably added things to the hardware
2048 * schedule.
2049 *
2050 * Note that we carefully avoid recycling this descriptor until after any
2051 * completion callback runs, so that it won't be reused quickly. That is,
2052 * assuming (a) no more than two urbs per frame on this endpoint, and also
2053 * (b) only this endpoint's completions submit URBs. It seems some silicon
2054 * corrupts things if you reuse completed descriptors very quickly...
2055 */
Alan Sternf4289072012-07-11 11:23:07 -04002056static bool sitd_complete(struct ehci_hcd *ehci, struct ehci_sitd *sitd)
2057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 struct urb *urb = sitd->urb;
2059 struct usb_iso_packet_descriptor *desc;
2060 u32 t;
2061 int urb_index = -1;
2062 struct ehci_iso_stream *stream = sitd->stream;
2063 struct usb_device *dev;
Alan Sternf4289072012-07-11 11:23:07 -04002064 bool retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 urb_index = sitd->index;
2067 desc = &urb->iso_frame_desc [urb_index];
Stefan Roese6dbd6822007-05-01 09:29:37 -07002068 t = hc32_to_cpup(ehci, &sitd->hw_results);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 /* report transfer status */
Alan Stern4005ad42012-10-01 10:32:01 -04002071 if (unlikely(t & SITD_ERRS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 urb->error_count++;
2073 if (t & SITD_STS_DBE)
2074 desc->status = usb_pipein (urb->pipe)
2075 ? -ENOSR /* hc couldn't read */
2076 : -ECOMM; /* hc couldn't write */
2077 else if (t & SITD_STS_BABBLE)
2078 desc->status = -EOVERFLOW;
2079 else /* XACT, MMF, etc */
2080 desc->status = -EPROTO;
Alan Stern4005ad42012-10-01 10:32:01 -04002081 } else if (unlikely(t & SITD_STS_ACTIVE)) {
2082 /* URB was too late */
2083 urb->error_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 } else {
2085 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04002086 desc->actual_length = desc->length - SITD_LENGTH(t);
2087 urb->actual_length += desc->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 /* handle completion now? */
2091 if ((urb_index + 1) != urb->number_of_packets)
David Brownell30bf54e2007-12-16 22:37:40 -08002092 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 /* ASSERT: it's really the last sitd for this urb
2095 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2096 BUG_ON (sitd->urb == urb);
2097 */
2098
David Brownellaa16ca32007-12-30 23:45:19 -08002099 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05002100 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04002101 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08002102 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Alan Stern569b3942012-07-11 11:23:00 -04002105 --ehci->isoc_count;
2106 disable_periodic(ehci);
2107
2108 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
Alex He05570292010-12-07 10:10:08 +08002109 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002110 if (ehci->amd_pll_fix == 1)
2111 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08002112 }
2113
Karsten Wiese508db8c2009-02-26 01:47:48 +01002114 if (list_is_singular(&stream->td_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 ehci_to_hcd(ehci)->self.bandwidth_allocated
2116 -= stream->bandwidth;
2117 ehci_vdbg (ehci,
2118 "deschedule devp %s ep%d%s-iso\n",
2119 dev->devpath, stream->bEndpointAddress & 0x0f,
2120 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
2121 }
Alan Stern0e5f2312010-04-08 16:56:37 -04002122
David Brownell30bf54e2007-12-16 22:37:40 -08002123done:
David Brownell30bf54e2007-12-16 22:37:40 -08002124 sitd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04002125
2126 /* Add to the end of the free list for later reuse */
2127 list_move_tail(&sitd->sitd_list, &stream->free_list);
2128
2129 /* Recycle the siTDs when the pipeline is empty (ep no longer in use) */
2130 if (list_empty(&stream->td_list)) {
2131 list_splice_tail_init(&stream->free_list,
2132 &ehci->cached_sitd_list);
2133 start_free_itds(ehci);
Alan Stern0e5f2312010-04-08 16:56:37 -04002134 }
Alan Stern55934eb2012-07-11 11:22:35 -04002135
David Brownell30bf54e2007-12-16 22:37:40 -08002136 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
2139
Olav Kongas5db539e2005-06-23 20:25:36 +03002140static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04002141 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 int status = -EINVAL;
2144 unsigned long flags;
2145 struct ehci_iso_stream *stream;
2146
2147 /* Get iso_stream head */
2148 stream = iso_stream_find (ehci, urb);
2149 if (stream == NULL) {
2150 ehci_dbg (ehci, "can't get iso stream\n");
2151 return -ENOMEM;
2152 }
2153 if (urb->interval != stream->interval) {
2154 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
2155 stream->interval, urb->interval);
2156 goto done;
2157 }
2158
2159#ifdef EHCI_URB_TRACE
2160 ehci_dbg (ehci,
2161 "submit %p dev%s ep%d%s-iso len %d\n",
2162 urb, urb->dev->devpath,
2163 usb_pipeendpoint (urb->pipe),
2164 usb_pipein (urb->pipe) ? "in" : "out",
2165 urb->transfer_buffer_length);
2166#endif
2167
2168 /* allocate SITDs */
2169 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2170 if (status < 0) {
2171 ehci_dbg (ehci, "can't init sitds\n");
2172 goto done;
2173 }
2174
2175 /* schedule ... need to lock */
2176 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04002177 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002178 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04002179 goto done_not_linked;
2180 }
2181 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2182 if (unlikely(status))
2183 goto done_not_linked;
2184 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07002185 if (status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04002187 else
2188 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002189 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002191 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return status;
2193}
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195/*-------------------------------------------------------------------------*/
2196
Alan Stern569b3942012-07-11 11:23:00 -04002197static void scan_isoc(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Alan Sternf4289072012-07-11 11:23:07 -04002199 unsigned uf, now_frame, frame;
2200 unsigned fmask = ehci->periodic_size - 1;
2201 bool modified, live;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /*
2204 * When running, scan from last scan point up to "now"
2205 * else clean up by scanning everything that's left.
2206 * Touches as few pages as possible: cache-friendly.
2207 */
Alan Sternc0c53db2012-07-11 11:21:48 -04002208 if (ehci->rh_state >= EHCI_RH_RUNNING) {
Alan Sternf4289072012-07-11 11:23:07 -04002209 uf = ehci_read_frame_index(ehci);
2210 now_frame = (uf >> 3) & fmask;
2211 live = true;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002212 } else {
Alan Sternc3ee9b72012-09-28 16:01:23 -04002213 now_frame = (ehci->last_iso_frame - 1) & fmask;
Alan Sternf4289072012-07-11 11:23:07 -04002214 live = false;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002215 }
Alan Sternf4289072012-07-11 11:23:07 -04002216 ehci->now_frame = now_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Alan Sternb09a61c2013-01-30 16:35:02 -05002218 frame = ehci->last_iso_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 for (;;) {
2220 union ehci_shadow q, *q_p;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002221 __hc32 type, *hw_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223restart:
2224 /* scan each element in frame's queue for completions */
2225 q_p = &ehci->pshadow [frame];
2226 hw_p = &ehci->periodic [frame];
2227 q.ptr = q_p->ptr;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002228 type = Q_NEXT_TYPE(ehci, *hw_p);
Alan Sternf4289072012-07-11 11:23:07 -04002229 modified = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 while (q.ptr != NULL) {
Stefan Roese6dbd6822007-05-01 09:29:37 -07002232 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 case Q_TYPE_ITD:
David Brownell79592b722008-01-07 00:47:42 -08002234 /* If this ITD is still active, leave it for
2235 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002236 * No need to check for activity unless the
2237 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002238 */
Alan Sternf4289072012-07-11 11:23:07 -04002239 if (frame == now_frame && live) {
Alan Sternb40e43f2008-05-20 16:59:10 -04002240 rmb();
2241 for (uf = 0; uf < 8; uf++) {
2242 if (q.itd->hw_transaction[uf] &
2243 ITD_ACTIVE(ehci))
2244 break;
2245 }
2246 if (uf < 8) {
Alan Sternb40e43f2008-05-20 16:59:10 -04002247 q_p = &q.itd->itd_next;
2248 hw_p = &q.itd->hw_next;
2249 type = Q_NEXT_TYPE(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -07002250 q.itd->hw_next);
Alan Sternb40e43f2008-05-20 16:59:10 -04002251 q = *q_p;
2252 break;
2253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
David Brownell79592b722008-01-07 00:47:42 -08002256 /* Take finished ITDs out of the schedule
2257 * and process them: recycle, maybe report
2258 * URB completion. HC won't cache the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 * pointer for much longer, if at all.
2260 */
2261 *q_p = q.itd->itd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002262 if (!ehci->use_dummy_qh ||
2263 q.itd->hw_next != EHCI_LIST_END(ehci))
2264 *hw_p = q.itd->hw_next;
2265 else
2266 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002267 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002269 modified = itd_complete (ehci, q.itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 q = *q_p;
2271 break;
2272 case Q_TYPE_SITD:
David Brownell79592b722008-01-07 00:47:42 -08002273 /* If this SITD is still active, leave it for
2274 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002275 * No need to check for activity unless the
2276 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002277 */
Alan Sternf4289072012-07-11 11:23:07 -04002278 if (((frame == now_frame) ||
2279 (((frame + 1) & fmask) == now_frame))
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002280 && live
2281 && (q.sitd->hw_results &
2282 SITD_ACTIVE(ehci))) {
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 q_p = &q.sitd->sitd_next;
2285 hw_p = &q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002286 type = Q_NEXT_TYPE(ehci,
2287 q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 q = *q_p;
2289 break;
2290 }
David Brownell79592b722008-01-07 00:47:42 -08002291
2292 /* Take finished SITDs out of the schedule
2293 * and process them: recycle, maybe report
2294 * URB completion.
2295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 *q_p = q.sitd->sitd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002297 if (!ehci->use_dummy_qh ||
2298 q.sitd->hw_next != EHCI_LIST_END(ehci))
2299 *hw_p = q.sitd->hw_next;
2300 else
2301 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002302 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002304 modified = sitd_complete (ehci, q.sitd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 q = *q_p;
2306 break;
2307 default:
Greg Kroah-Hartman2d0fe1b2012-05-01 21:33:36 -07002308 ehci_dbg(ehci, "corrupt type %d frame %d shadow %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 type, frame, q.ptr);
2310 // BUG ();
Alan Stern569b3942012-07-11 11:23:00 -04002311 /* FALL THROUGH */
2312 case Q_TYPE_QH:
2313 case Q_TYPE_FSTN:
2314 /* End of the iTDs and siTDs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 q.ptr = NULL;
Alan Stern569b3942012-07-11 11:23:00 -04002316 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318
2319 /* assume completion callbacks modify the queue */
Alan Sternf4289072012-07-11 11:23:07 -04002320 if (unlikely(modified && ehci->isoc_count > 0))
2321 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323
Alan Sternf4289072012-07-11 11:23:07 -04002324 /* Stop when we have reached the current frame */
2325 if (frame == now_frame)
David Brownell79592b722008-01-07 00:47:42 -08002326 break;
Alan Sternb09a61c2013-01-30 16:35:02 -05002327
2328 /* The last frame may still have active siTDs */
2329 ehci->last_iso_frame = frame;
2330 frame = (frame + 1) & fmask;
David Brownell53bd6a62006-08-30 14:50:06 -07002331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}