blob: 5c82bbab9a48976cf1ba27dfadb0f774f44e2a1f [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
Alan Stern79bcf7b2013-03-22 13:30:56 -0400652 if (!list_empty(&qh->qtd_list))
653 qh_completions(ehci, qh);
Alan Sterna448c9d2009-08-19 12:22:44 -0400654
655 /* reschedule QH iff another request is queued */
Alan Sterndf202252012-07-11 11:22:26 -0400656 if (!list_empty(&qh->qtd_list) && ehci->rh_state == EHCI_RH_RUNNING) {
Alan Sterna448c9d2009-08-19 12:22:44 -0400657 rc = qh_schedule(ehci, qh);
658
659 /* An error here likely indicates handshake failure
660 * or no space left in the schedule. Neither fault
661 * should happen often ...
662 *
663 * FIXME kill the now-dysfunctional queued urbs
664 */
665 if (rc != 0)
666 ehci_err(ehci, "can't reschedule qh %p, err %d\n",
667 qh, rc);
668 }
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400669
670 /* maybe turn off periodic schedule */
Alan Stern569b3942012-07-11 11:23:00 -0400671 --ehci->intr_count;
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400672 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/*-------------------------------------------------------------------------*/
676
677static int check_period (
David Brownell53bd6a62006-08-30 14:50:06 -0700678 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 unsigned frame,
680 unsigned uframe,
681 unsigned period,
682 unsigned usecs
683) {
684 int claimed;
685
686 /* complete split running into next frame?
687 * given FSTN support, we could sometimes check...
688 */
689 if (uframe >= 8)
690 return 0;
691
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +0400692 /* convert "usecs we need" to "max already claimed" */
693 usecs = ehci->uframe_periodic_max - usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* we "know" 2 and 4 uframe intervals were rejected; so
696 * for period 0, check _every_ microframe in the schedule.
697 */
698 if (unlikely (period == 0)) {
699 do {
700 for (uframe = 0; uframe < 7; uframe++) {
701 claimed = periodic_usecs (ehci, frame, uframe);
702 if (claimed > usecs)
703 return 0;
704 }
705 } while ((frame += 1) < ehci->periodic_size);
706
707 /* just check the specified uframe, at that period */
708 } else {
709 do {
710 claimed = periodic_usecs (ehci, frame, uframe);
711 if (claimed > usecs)
712 return 0;
713 } while ((frame += period) < ehci->periodic_size);
714 }
715
716 // success!
717 return 1;
718}
719
720static int check_intr_schedule (
David Brownell53bd6a62006-08-30 14:50:06 -0700721 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unsigned frame,
723 unsigned uframe,
724 const struct ehci_qh *qh,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700725 __hc32 *c_maskp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726)
727{
David Brownell53bd6a62006-08-30 14:50:06 -0700728 int retval = -ENOSPC;
Dan Streetmanba47f662006-05-24 09:39:16 -0700729 u8 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
732 goto done;
733
734 if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
735 goto done;
736 if (!qh->c_usecs) {
737 retval = 0;
738 *c_maskp = 0;
739 goto done;
740 }
741
Dan Streetmanba47f662006-05-24 09:39:16 -0700742#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
743 if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
744 qh->tt_usecs)) {
745 unsigned i;
746
747 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
748 for (i=uframe+1; i<8 && i<uframe+4; i++)
749 if (!check_period (ehci, frame, i,
750 qh->period, qh->c_usecs))
751 goto done;
752 else
753 mask |= 1 << i;
754
755 retval = 0;
756
Stefan Roese6dbd6822007-05-01 09:29:37 -0700757 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Dan Streetmanba47f662006-05-24 09:39:16 -0700758 }
759#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* Make sure this tt's buffer is also available for CSPLITs.
761 * We pessimize a bit; probably the typical full speed case
762 * doesn't need the second CSPLIT.
David Brownell53bd6a62006-08-30 14:50:06 -0700763 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * NOTE: both SPLIT and CSPLIT could be checked in just
765 * one smart pass...
766 */
767 mask = 0x03 << (uframe + qh->gap_uf);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700768 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 mask |= 1 << uframe;
771 if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
772 if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
773 qh->period, qh->c_usecs))
774 goto done;
775 if (!check_period (ehci, frame, uframe + qh->gap_uf,
776 qh->period, qh->c_usecs))
777 goto done;
778 retval = 0;
779 }
Dan Streetmanba47f662006-05-24 09:39:16 -0700780#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781done:
782 return retval;
783}
784
785/* "first fit" scheduling policy used the first time through,
786 * or when the previous schedule slot can't be re-used.
787 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700788static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
David Brownell53bd6a62006-08-30 14:50:06 -0700790 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 unsigned uframe;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700792 __hc32 c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
Alek Du3807e262009-07-14 07:23:29 +0800794 struct ehci_qh_hw *hw = qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Alek Du3807e262009-07-14 07:23:29 +0800796 hw->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 frame = qh->start;
798
799 /* reuse the previous schedule slots, if we can */
800 if (frame < qh->period) {
Alek Du3807e262009-07-14 07:23:29 +0800801 uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 status = check_intr_schedule (ehci, frame, --uframe,
803 qh, &c_mask);
804 } else {
805 uframe = 0;
806 c_mask = 0;
807 status = -ENOSPC;
808 }
809
810 /* else scan the schedule to find a group of slots such that all
811 * uframes have enough periodic bandwidth available.
812 */
813 if (status) {
814 /* "normal" case, uframing flexible except with splits */
815 if (qh->period) {
Alan Stern68335e82009-05-22 17:02:33 -0400816 int i;
817
818 for (i = qh->period; status && i > 0; --i) {
819 frame = ++ehci->random_frame % qh->period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 for (uframe = 0; uframe < 8; uframe++) {
821 status = check_intr_schedule (ehci,
822 frame, uframe, qh,
823 &c_mask);
824 if (status == 0)
825 break;
826 }
Alan Stern68335e82009-05-22 17:02:33 -0400827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /* qh->period == 0 means every uframe */
830 } else {
831 frame = 0;
832 status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
833 }
834 if (status)
835 goto done;
836 qh->start = frame;
837
838 /* reset S-frame and (maybe) C-frame masks */
Alek Du3807e262009-07-14 07:23:29 +0800839 hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
840 hw->hw_info2 |= qh->period
Stefan Roese6dbd6822007-05-01 09:29:37 -0700841 ? cpu_to_hc32(ehci, 1 << uframe)
842 : cpu_to_hc32(ehci, QH_SMASK);
Alek Du3807e262009-07-14 07:23:29 +0800843 hw->hw_info2 |= c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 } else
845 ehci_dbg (ehci, "reused qh %p schedule\n", qh);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847done:
848 return status;
849}
850
851static int intr_submit (
852 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct urb *urb,
854 struct list_head *qtd_list,
Al Viro55016f12005-10-21 03:21:58 -0400855 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856) {
857 unsigned epnum;
858 unsigned long flags;
859 struct ehci_qh *qh;
Alan Sterne9df41c2007-08-08 11:48:02 -0400860 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct list_head empty;
862
863 /* get endpoint and transfer/schedule data */
Alan Sterne9df41c2007-08-08 11:48:02 -0400864 epnum = urb->ep->desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 spin_lock_irqsave (&ehci->lock, flags);
867
Alan Stern541c7d42010-06-22 16:39:10 -0400868 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100869 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -0400870 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100871 }
Alan Sterne9df41c2007-08-08 11:48:02 -0400872 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
873 if (unlikely(status))
874 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /* get qh and force any scheduling errors */
877 INIT_LIST_HEAD (&empty);
Alan Sterne9df41c2007-08-08 11:48:02 -0400878 qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (qh == NULL) {
880 status = -ENOMEM;
881 goto done;
882 }
883 if (qh->qh_state == QH_STATE_IDLE) {
884 if ((status = qh_schedule (ehci, qh)) != 0)
885 goto done;
886 }
887
888 /* then queue the urb's tds to the qh */
Alan Sterne9df41c2007-08-08 11:48:02 -0400889 qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 BUG_ON (qh == NULL);
891
Alan Sternc1fdb682013-03-22 13:30:43 -0400892 /* stuff into the periodic schedule */
893 if (qh->qh_state == QH_STATE_IDLE) {
894 qh_refresh(ehci, qh);
895 qh_link_periodic(ehci, qh);
896 }
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /* ... update usbfs periodic stats */
899 ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
900
901done:
Alan Sterne9df41c2007-08-08 11:48:02 -0400902 if (unlikely(status))
903 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
904done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 spin_unlock_irqrestore (&ehci->lock, flags);
906 if (status)
907 qtd_list_free (ehci, urb, qtd_list);
908
909 return status;
910}
911
Alan Stern569b3942012-07-11 11:23:00 -0400912static void scan_intr(struct ehci_hcd *ehci)
913{
914 struct ehci_qh *qh;
915
916 list_for_each_entry_safe(qh, ehci->qh_scan_next, &ehci->intr_qh_list,
917 intr_node) {
Alan Stern79bcf7b2013-03-22 13:30:56 -0400918
Alan Stern569b3942012-07-11 11:23:00 -0400919 /* clean any finished work for this qh */
920 if (!list_empty(&qh->qtd_list)) {
921 int temp;
922
923 /*
924 * Unlinks could happen here; completion reporting
925 * drops the lock. That's why ehci->qh_scan_next
926 * always holds the next qh to scan; if the next qh
927 * gets unlinked then ehci->qh_scan_next is adjusted
928 * in qh_unlink_periodic().
929 */
930 temp = qh_completions(ehci, qh);
Alan Stern79bcf7b2013-03-22 13:30:56 -0400931 if (unlikely(temp || (list_empty(&qh->qtd_list) &&
932 qh->qh_state == QH_STATE_LINKED)))
Alan Stern569b3942012-07-11 11:23:00 -0400933 start_unlink_intr(ehci, qh);
Alan Stern569b3942012-07-11 11:23:00 -0400934 }
935 }
936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/*-------------------------------------------------------------------------*/
939
940/* ehci_iso_stream ops work with both ITD and SITD */
941
942static struct ehci_iso_stream *
Al Viro55016f12005-10-21 03:21:58 -0400943iso_stream_alloc (gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 struct ehci_iso_stream *stream;
946
Pekka Enberg7b842b62005-09-06 15:18:34 -0700947 stream = kzalloc(sizeof *stream, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 INIT_LIST_HEAD(&stream->td_list);
950 INIT_LIST_HEAD(&stream->free_list);
951 stream->next_uframe = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953 return stream;
954}
955
956static void
957iso_stream_init (
958 struct ehci_hcd *ehci,
959 struct ehci_iso_stream *stream,
960 struct usb_device *dev,
961 int pipe,
962 unsigned interval
963)
964{
965 static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
966
967 u32 buf1;
968 unsigned epnum, maxp;
969 int is_input;
970 long bandwidth;
971
972 /*
973 * this might be a "high bandwidth" highspeed endpoint,
974 * as encoded in the ep descriptor's wMaxPacket field
975 */
976 epnum = usb_pipeendpoint (pipe);
977 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
978 maxp = usb_maxpacket(dev, pipe, !is_input);
979 if (is_input) {
980 buf1 = (1 << 11);
981 } else {
982 buf1 = 0;
983 }
984
985 /* knows about ITD vs SITD */
986 if (dev->speed == USB_SPEED_HIGH) {
987 unsigned multi = hb_mult(maxp);
988
989 stream->highspeed = 1;
990
991 maxp = max_packet(maxp);
992 buf1 |= maxp;
993 maxp *= multi;
994
Stefan Roese6dbd6822007-05-01 09:29:37 -0700995 stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
996 stream->buf1 = cpu_to_hc32(ehci, buf1);
997 stream->buf2 = cpu_to_hc32(ehci, multi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /* usbfs wants to report the average usecs per frame tied up
1000 * when transfers on this endpoint are scheduled ...
1001 */
1002 stream->usecs = HS_USECS_ISO (maxp);
1003 bandwidth = stream->usecs * 8;
Alan Stern372dd6e2008-11-12 17:02:57 -05001004 bandwidth /= interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 } else {
1007 u32 addr;
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001008 int think_time;
Clemens Ladisch469d0222006-01-20 13:49:10 -08001009 int hs_transfers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 addr = dev->ttport << 24;
1012 if (!ehci_is_TDI(ehci)
1013 || (dev->tt->hub !=
1014 ehci_to_hcd(ehci)->self.root_hub))
1015 addr |= dev->tt->hub->devnum << 16;
1016 addr |= epnum << 8;
1017 addr |= dev->devnum;
1018 stream->usecs = HS_USECS_ISO (maxp);
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001019 think_time = dev->tt ? dev->tt->think_time : 0;
1020 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
1021 dev->speed, is_input, 1, maxp));
Clemens Ladisch469d0222006-01-20 13:49:10 -08001022 hs_transfers = max (1u, (maxp + 187) / 188);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (is_input) {
1024 u32 tmp;
1025
1026 addr |= 1 << 31;
1027 stream->c_usecs = stream->usecs;
1028 stream->usecs = HS_USECS_ISO (1);
1029 stream->raw_mask = 1;
1030
Clemens Ladisch469d0222006-01-20 13:49:10 -08001031 /* c-mask as specified in USB 2.0 11.18.4 3.c */
1032 tmp = (1 << (hs_transfers + 2)) - 1;
1033 stream->raw_mask |= tmp << (8 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 } else
Clemens Ladisch469d0222006-01-20 13:49:10 -08001035 stream->raw_mask = smask_out [hs_transfers - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 bandwidth = stream->usecs + stream->c_usecs;
Alan Stern372dd6e2008-11-12 17:02:57 -05001037 bandwidth /= interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 /* stream->splits gets created from raw_mask later */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001040 stream->address = cpu_to_hc32(ehci, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042 stream->bandwidth = bandwidth;
1043
1044 stream->udev = dev;
1045
1046 stream->bEndpointAddress = is_input | epnum;
1047 stream->interval = interval;
1048 stream->maxp = maxp;
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051static struct ehci_iso_stream *
1052iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1053{
1054 unsigned epnum;
1055 struct ehci_iso_stream *stream;
1056 struct usb_host_endpoint *ep;
1057 unsigned long flags;
1058
1059 epnum = usb_pipeendpoint (urb->pipe);
1060 if (usb_pipein(urb->pipe))
1061 ep = urb->dev->ep_in[epnum];
1062 else
1063 ep = urb->dev->ep_out[epnum];
1064
1065 spin_lock_irqsave (&ehci->lock, flags);
1066 stream = ep->hcpriv;
1067
1068 if (unlikely (stream == NULL)) {
1069 stream = iso_stream_alloc(GFP_ATOMIC);
1070 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 ep->hcpriv = stream;
1072 stream->ep = ep;
1073 iso_stream_init(ehci, stream, urb->dev, urb->pipe,
1074 urb->interval);
1075 }
1076
Clemens Ladisch1082f572010-03-01 17:18:56 +01001077 /* if dev->ep [epnum] is a QH, hw is set */
1078 } else if (unlikely (stream->hw != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1080 urb->dev->devpath, epnum,
1081 usb_pipein(urb->pipe) ? "in" : "out");
1082 stream = NULL;
1083 }
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 spin_unlock_irqrestore (&ehci->lock, flags);
1086 return stream;
1087}
1088
1089/*-------------------------------------------------------------------------*/
1090
1091/* ehci_iso_sched ops can be ITD-only or SITD-only */
1092
1093static struct ehci_iso_sched *
Al Viro55016f12005-10-21 03:21:58 -04001094iso_sched_alloc (unsigned packets, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 struct ehci_iso_sched *iso_sched;
1097 int size = sizeof *iso_sched;
1098
1099 size += packets * sizeof (struct ehci_iso_packet);
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001100 iso_sched = kzalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (likely (iso_sched != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 INIT_LIST_HEAD (&iso_sched->td_list);
1103 }
1104 return iso_sched;
1105}
1106
1107static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001108itd_sched_init(
1109 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct ehci_iso_sched *iso_sched,
1111 struct ehci_iso_stream *stream,
1112 struct urb *urb
1113)
1114{
1115 unsigned i;
1116 dma_addr_t dma = urb->transfer_dma;
1117
1118 /* how many uframes are needed for these transfers */
1119 iso_sched->span = urb->number_of_packets * stream->interval;
1120
1121 /* figure out per-uframe itd fields that we'll need later
1122 * when we fit new itds into the schedule.
1123 */
1124 for (i = 0; i < urb->number_of_packets; i++) {
1125 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1126 unsigned length;
1127 dma_addr_t buf;
1128 u32 trans;
1129
1130 length = urb->iso_frame_desc [i].length;
1131 buf = dma + urb->iso_frame_desc [i].offset;
1132
1133 trans = EHCI_ISOC_ACTIVE;
1134 trans |= buf & 0x0fff;
1135 if (unlikely (((i + 1) == urb->number_of_packets))
1136 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1137 trans |= EHCI_ITD_IOC;
1138 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001139 uframe->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
David Brownell77078572005-05-28 10:46:18 -07001141 /* might need to cross a buffer page within a uframe */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 uframe->bufp = (buf & ~(u64)0x0fff);
1143 buf += length;
1144 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1145 uframe->cross = 1;
1146 }
1147}
1148
1149static void
1150iso_sched_free (
1151 struct ehci_iso_stream *stream,
1152 struct ehci_iso_sched *iso_sched
1153)
1154{
1155 if (!iso_sched)
1156 return;
1157 // caller must hold ehci->lock!
1158 list_splice (&iso_sched->td_list, &stream->free_list);
1159 kfree (iso_sched);
1160}
1161
1162static int
1163itd_urb_transaction (
1164 struct ehci_iso_stream *stream,
1165 struct ehci_hcd *ehci,
1166 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001167 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168)
1169{
1170 struct ehci_itd *itd;
1171 dma_addr_t itd_dma;
1172 int i;
1173 unsigned num_itds;
1174 struct ehci_iso_sched *sched;
1175 unsigned long flags;
1176
1177 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1178 if (unlikely (sched == NULL))
1179 return -ENOMEM;
1180
Stefan Roese6dbd6822007-05-01 09:29:37 -07001181 itd_sched_init(ehci, sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 if (urb->interval < 8)
1184 num_itds = 1 + (sched->span + 7) / 8;
1185 else
1186 num_itds = urb->number_of_packets;
1187
1188 /* allocate/init ITDs */
1189 spin_lock_irqsave (&ehci->lock, flags);
1190 for (i = 0; i < num_itds; i++) {
1191
Alan Stern55934eb2012-07-11 11:22:35 -04001192 /*
1193 * Use iTDs from the free list, but not iTDs that may
1194 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
Alan Stern55934eb2012-07-11 11:22:35 -04001196 if (likely(!list_empty(&stream->free_list))) {
1197 itd = list_first_entry(&stream->free_list,
Stefan Roese6dbd6822007-05-01 09:29:37 -07001198 struct ehci_itd, itd_list);
Alan Sternf4289072012-07-11 11:23:07 -04001199 if (itd->frame == ehci->now_frame)
Alan Stern55934eb2012-07-11 11:22:35 -04001200 goto alloc_itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 list_del (&itd->itd_list);
1202 itd_dma = itd->itd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001203 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001204 alloc_itd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 spin_unlock_irqrestore (&ehci->lock, flags);
1206 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1207 &itd_dma);
1208 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001209 if (!itd) {
1210 iso_sched_free(stream, sched);
1211 spin_unlock_irqrestore(&ehci->lock, flags);
1212 return -ENOMEM;
1213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 memset (itd, 0, sizeof *itd);
1217 itd->itd_dma = itd_dma;
1218 list_add (&itd->itd_list, &sched->td_list);
1219 }
1220 spin_unlock_irqrestore (&ehci->lock, flags);
1221
1222 /* temporarily store schedule info in hcpriv */
1223 urb->hcpriv = sched;
1224 urb->error_count = 0;
1225 return 0;
1226}
1227
1228/*-------------------------------------------------------------------------*/
1229
1230static inline int
1231itd_slot_ok (
1232 struct ehci_hcd *ehci,
1233 u32 mod,
1234 u32 uframe,
1235 u8 usecs,
1236 u32 period
1237)
1238{
1239 uframe %= period;
1240 do {
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001241 /* can't commit more than uframe_periodic_max usec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001243 > (ehci->uframe_periodic_max - usecs))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return 0;
1245
1246 /* we know urb->interval is 2^N uframes */
1247 uframe += period;
1248 } while (uframe < mod);
1249 return 1;
1250}
1251
1252static inline int
1253sitd_slot_ok (
1254 struct ehci_hcd *ehci,
1255 u32 mod,
1256 struct ehci_iso_stream *stream,
1257 u32 uframe,
1258 struct ehci_iso_sched *sched,
1259 u32 period_uframes
1260)
1261{
1262 u32 mask, tmp;
1263 u32 frame, uf;
1264
1265 mask = stream->raw_mask << (uframe & 7);
1266
1267 /* for IN, don't wrap CSPLIT into the next frame */
1268 if (mask & ~0xffff)
1269 return 0;
1270
Alan Stern65b8e5c2012-05-14 13:47:20 -04001271 /* check bandwidth */
1272 uframe %= period_uframes;
1273 frame = uframe >> 3;
1274
1275#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1276 /* The tt's fullspeed bus bandwidth must be available.
1277 * tt_available scheduling guarantees 10+% for control/bulk.
1278 */
1279 uf = uframe & 7;
1280 if (!tt_available(ehci, period_uframes >> 3,
1281 stream->udev, frame, uf, stream->tt_usecs))
1282 return 0;
1283#else
1284 /* tt must be idle for start(s), any gap, and csplit.
1285 * assume scheduling slop leaves 10+% for control/bulk.
1286 */
1287 if (!tt_no_collision(ehci, period_uframes >> 3,
1288 stream->udev, frame, mask))
1289 return 0;
1290#endif
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 /* this multi-pass logic is simple, but performance may
1293 * suffer when the schedule data isn't cached.
1294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 do {
1296 u32 max_used;
1297
1298 frame = uframe >> 3;
1299 uf = uframe & 7;
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /* check starts (OUT uses more than one) */
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001302 max_used = ehci->uframe_periodic_max - stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
1304 if (periodic_usecs (ehci, frame, uf) > max_used)
1305 return 0;
1306 }
1307
1308 /* for IN, check CSPLIT */
1309 if (stream->c_usecs) {
Clemens Ladisch0c734622006-01-22 10:32:49 -08001310 uf = uframe & 7;
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001311 max_used = ehci->uframe_periodic_max - stream->c_usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 do {
1313 tmp = 1 << uf;
1314 tmp <<= 8;
1315 if ((stream->raw_mask & tmp) == 0)
1316 continue;
1317 if (periodic_usecs (ehci, frame, uf)
1318 > max_used)
1319 return 0;
1320 } while (++uf < 8);
1321 }
1322
1323 /* we know urb->interval is 2^N uframes */
1324 uframe += period_uframes;
1325 } while (uframe < mod);
1326
Stefan Roese6dbd6822007-05-01 09:29:37 -07001327 stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return 1;
1329}
1330
1331/*
1332 * This scheduler plans almost as far into the future as it has actual
1333 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1334 * "as small as possible" to be cache-friendlier.) That limits the size
1335 * transfers you can stream reliably; avoid more than 64 msec per urb.
1336 * Also avoid queue depths of less than ehci's worst irq latency (affected
1337 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1338 * and other factors); or more than about 230 msec total (for portability,
1339 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1340 */
1341
Alan Sternc3ee9b72012-09-28 16:01:23 -04001342#define SCHEDULING_DELAY 40 /* microframes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344static int
1345iso_stream_schedule (
1346 struct ehci_hcd *ehci,
1347 struct urb *urb,
1348 struct ehci_iso_stream *stream
1349)
1350{
Alan Sternc3ee9b72012-09-28 16:01:23 -04001351 u32 now, base, next, start, period, span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 int status;
1353 unsigned mod = ehci->periodic_size << 3;
1354 struct ehci_iso_sched *sched = urb->hcpriv;
1355
Alan Sternffda0802010-07-14 11:03:46 -04001356 period = urb->interval;
1357 span = sched->span;
1358 if (!stream->highspeed) {
1359 period <<= 3;
1360 span <<= 3;
1361 }
1362
Alan Stern68aa95d2011-10-12 10:39:14 -04001363 now = ehci_read_frame_index(ehci) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Alan Sternb40e43f2008-05-20 16:59:10 -04001365 /* Typical case: reuse current schedule, stream is still active.
1366 * Hopefully there are no gaps from the host falling behind
Alan Stern4005ad42012-10-01 10:32:01 -04001367 * (irq delays etc). If there are, the behavior depends on
1368 * whether URB_ISO_ASAP is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
1370 if (likely (!list_empty (&stream->td_list))) {
Sarah Sharpdccd5742009-10-27 10:55:05 -07001371
Alan Stern98cae422012-09-28 16:01:34 -04001372 /* Take the isochronous scheduling threshold into account */
1373 if (ehci->i_thresh)
1374 next = now + ehci->i_thresh; /* uframe cache */
Sarah Sharpdccd5742009-10-27 10:55:05 -07001375 else
Alan Stern98cae422012-09-28 16:01:34 -04001376 next = (now + 2 + 7) & ~0x07; /* full frame cache */
Alan Sternb40e43f2008-05-20 16:59:10 -04001377
Alan Sternc3ee9b72012-09-28 16:01:23 -04001378 /*
1379 * Use ehci->last_iso_frame as the base. There can't be any
1380 * TDs scheduled for earlier than that.
Alan Stern1fb2e052010-07-14 11:03:53 -04001381 */
Alan Sternc3ee9b72012-09-28 16:01:23 -04001382 base = ehci->last_iso_frame << 3;
1383 next = (next - base) & (mod - 1);
1384 start = (stream->next_uframe - base) & (mod - 1);
1385
1386 /* Is the schedule already full? */
1387 if (unlikely(start < period)) {
1388 ehci_dbg(ehci, "iso sched full %p (%u-%u < %u mod %u)\n",
1389 urb, stream->next_uframe, base,
1390 period, mod);
1391 status = -ENOSPC;
Alan Sternb40e43f2008-05-20 16:59:10 -04001392 goto fail;
1393 }
Alan Sternc3ee9b72012-09-28 16:01:23 -04001394
Alan Stern4005ad42012-10-01 10:32:01 -04001395 /* Behind the scheduling threshold? */
1396 if (unlikely(start < next)) {
1397
1398 /* USB_ISO_ASAP: Round up to the first available slot */
1399 if (urb->transfer_flags & URB_ISO_ASAP)
1400 start += (next - start + period - 1) & -period;
1401
1402 /*
1403 * Not ASAP: Use the next slot in the stream. If
1404 * the entire URB falls before the threshold, fail.
1405 */
1406 else if (start + span - period < next) {
1407 ehci_dbg(ehci, "iso urb late %p (%u+%u < %u)\n",
1408 urb, start + base,
1409 span - period, next + base);
1410 status = -EXDEV;
1411 goto fail;
1412 }
1413 }
Alan Sternc3ee9b72012-09-28 16:01:23 -04001414
1415 start += base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
1418 /* need to schedule; when's the next (u)frame we could start?
1419 * this is bigger than ehci->i_thresh allows; scheduling itself
Alan Sternc3ee9b72012-09-28 16:01:23 -04001420 * isn't free, the delay should handle reasonably slow cpus. it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * can also help high bandwidth if the dma and irq loads don't
1422 * jump until after the queue is primed.
1423 */
Alan Stern1fb2e052010-07-14 11:03:53 -04001424 else {
Matthieu CASTETe3420902011-11-28 11:30:22 +01001425 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Alan Sternc3ee9b72012-09-28 16:01:23 -04001427 base = now & ~0x07;
1428 start = base + SCHEDULING_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Thomas Poussevin811c9262011-10-27 18:46:48 +02001430 /* find a uframe slot with enough bandwidth.
1431 * Early uframes are more precious because full-speed
1432 * iso IN transfers can't use late uframes,
1433 * and therefore they should be allocated last.
1434 */
1435 next = start;
1436 start += period;
1437 do {
1438 start--;
Alan Stern1fb2e052010-07-14 11:03:53 -04001439 /* check schedule: enough space? */
1440 if (stream->highspeed) {
1441 if (itd_slot_ok(ehci, mod, start,
1442 stream->usecs, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001443 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001444 } else {
1445 if ((start % 8) >= 6)
1446 continue;
1447 if (sitd_slot_ok(ehci, mod, stream,
1448 start, sched, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001449 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001450 }
Matthieu CASTETe3420902011-11-28 11:30:22 +01001451 } while (start > next && !done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Alan Stern1fb2e052010-07-14 11:03:53 -04001453 /* no room in the schedule */
Matthieu CASTETe3420902011-11-28 11:30:22 +01001454 if (!done) {
Alan Sternc3ee9b72012-09-28 16:01:23 -04001455 ehci_dbg(ehci, "iso sched full %p", urb);
Alan Stern1fb2e052010-07-14 11:03:53 -04001456 status = -ENOSPC;
1457 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 }
1460
Alan Stern1fb2e052010-07-14 11:03:53 -04001461 /* Tried to schedule too far into the future? */
Alan Sternc3ee9b72012-09-28 16:01:23 -04001462 if (unlikely(start - base + span - period >= mod)) {
1463 ehci_dbg(ehci, "request %p would overflow (%u+%u >= %u)\n",
1464 urb, start - base, span - period, mod);
Alan Stern1fb2e052010-07-14 11:03:53 -04001465 status = -EFBIG;
1466 goto fail;
1467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Alan Stern1fb2e052010-07-14 11:03:53 -04001469 stream->next_uframe = start & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /* report high speed start in uframes; full speed, in frames */
1472 urb->start_frame = stream->next_uframe;
1473 if (!stream->highspeed)
1474 urb->start_frame >>= 3;
Alan Stern569b3942012-07-11 11:23:00 -04001475
1476 /* Make sure scan_isoc() sees these */
1477 if (ehci->isoc_count == 0)
Alan Sternc3ee9b72012-09-28 16:01:23 -04001478 ehci->last_iso_frame = now >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
Alan Stern1fb2e052010-07-14 11:03:53 -04001480
1481 fail:
1482 iso_sched_free(stream, sched);
1483 urb->hcpriv = NULL;
1484 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487/*-------------------------------------------------------------------------*/
1488
1489static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001490itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1491 struct ehci_itd *itd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 int i;
1494
David Brownell77078572005-05-28 10:46:18 -07001495 /* it's been recently zeroed */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001496 itd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 itd->hw_bufp [0] = stream->buf0;
1498 itd->hw_bufp [1] = stream->buf1;
1499 itd->hw_bufp [2] = stream->buf2;
1500
1501 for (i = 0; i < 8; i++)
1502 itd->index[i] = -1;
1503
1504 /* All other fields are filled when scheduling */
1505}
1506
1507static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001508itd_patch(
1509 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 struct ehci_itd *itd,
1511 struct ehci_iso_sched *iso_sched,
1512 unsigned index,
David Brownell77078572005-05-28 10:46:18 -07001513 u16 uframe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514)
1515{
1516 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1517 unsigned pg = itd->pg;
1518
1519 // BUG_ON (pg == 6 && uf->cross);
1520
1521 uframe &= 0x07;
1522 itd->index [uframe] = index;
1523
Stefan Roese6dbd6822007-05-01 09:29:37 -07001524 itd->hw_transaction[uframe] = uf->transaction;
1525 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1526 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1527 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 /* iso_frame_desc[].offset must be strictly increasing */
David Brownell77078572005-05-28 10:46:18 -07001530 if (unlikely (uf->cross)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 u64 bufp = uf->bufp + 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 itd->pg = ++pg;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001534 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1535 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537}
1538
1539static inline void
1540itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1541{
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001542 union ehci_shadow *prev = &ehci->pshadow[frame];
1543 __hc32 *hw_p = &ehci->periodic[frame];
1544 union ehci_shadow here = *prev;
1545 __hc32 type = 0;
1546
1547 /* skip any iso nodes which might belong to previous microframes */
1548 while (here.ptr) {
1549 type = Q_NEXT_TYPE(ehci, *hw_p);
1550 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
1551 break;
1552 prev = periodic_next_shadow(ehci, prev, type);
1553 hw_p = shadow_next_periodic(ehci, &here, type);
1554 here = *prev;
1555 }
1556
1557 itd->itd_next = here;
1558 itd->hw_next = *hw_p;
1559 prev->itd = itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 itd->frame = frame;
1561 wmb ();
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001562 *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565/* fit urb's itds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001566static void itd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 struct ehci_hcd *ehci,
1568 struct urb *urb,
1569 unsigned mod,
1570 struct ehci_iso_stream *stream
1571)
1572{
David Brownell77078572005-05-28 10:46:18 -07001573 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 unsigned next_uframe, uframe, frame;
1575 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1576 struct ehci_itd *itd;
1577
Alan Sternbccbefa2010-07-14 11:03:36 -04001578 next_uframe = stream->next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 if (unlikely (list_empty(&stream->td_list))) {
1581 ehci_to_hcd(ehci)->self.bandwidth_allocated
1582 += stream->bandwidth;
1583 ehci_vdbg (ehci,
1584 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1585 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1586 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1587 urb->interval,
1588 next_uframe >> 3, next_uframe & 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Alex He05570292010-12-07 10:10:08 +08001590
1591 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001592 if (ehci->amd_pll_fix == 1)
1593 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08001594 }
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1597
1598 /* fill iTDs uframe by uframe */
1599 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1600 if (itd == NULL) {
1601 /* ASSERT: we have all necessary itds */
1602 // BUG_ON (list_empty (&iso_sched->td_list));
1603
1604 /* ASSERT: no itds for this endpoint in this uframe */
1605
1606 itd = list_entry (iso_sched->td_list.next,
1607 struct ehci_itd, itd_list);
1608 list_move_tail (&itd->itd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001609 itd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01001610 itd->urb = urb;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001611 itd_init (ehci, stream, itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613
1614 uframe = next_uframe & 0x07;
1615 frame = next_uframe >> 3;
1616
Stefan Roese6dbd6822007-05-01 09:29:37 -07001617 itd_patch(ehci, itd, iso_sched, packet, uframe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 next_uframe += stream->interval;
Alan Sternbccbefa2010-07-14 11:03:36 -04001620 next_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 packet++;
1622
1623 /* link completed itds into the schedule */
1624 if (((next_uframe >> 3) != frame)
1625 || packet == urb->number_of_packets) {
Alan Sternbccbefa2010-07-14 11:03:36 -04001626 itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 itd = NULL;
1628 }
1629 }
1630 stream->next_uframe = next_uframe;
1631
1632 /* don't need that schedule data any more */
1633 iso_sched_free (stream, iso_sched);
Alan Stern2656a9a2012-11-08 10:17:01 -05001634 urb->hcpriv = stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Alan Stern569b3942012-07-11 11:23:00 -04001636 ++ehci->isoc_count;
Alan Sternb015cb72012-07-11 11:22:10 -04001637 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
1640#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1641
David Brownell30bf54e2007-12-16 22:37:40 -08001642/* Process and recycle a completed ITD. Return true iff its urb completed,
1643 * and hence its completion callback probably added things to the hardware
1644 * schedule.
1645 *
1646 * Note that we carefully avoid recycling this descriptor until after any
1647 * completion callback runs, so that it won't be reused quickly. That is,
1648 * assuming (a) no more than two urbs per frame on this endpoint, and also
1649 * (b) only this endpoint's completions submit URBs. It seems some silicon
1650 * corrupts things if you reuse completed descriptors very quickly...
1651 */
Alan Sternf4289072012-07-11 11:23:07 -04001652static bool itd_complete(struct ehci_hcd *ehci, struct ehci_itd *itd)
1653{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct urb *urb = itd->urb;
1655 struct usb_iso_packet_descriptor *desc;
1656 u32 t;
1657 unsigned uframe;
1658 int urb_index = -1;
1659 struct ehci_iso_stream *stream = itd->stream;
1660 struct usb_device *dev;
Alan Sternf4289072012-07-11 11:23:07 -04001661 bool retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 /* for each uframe with a packet */
1664 for (uframe = 0; uframe < 8; uframe++) {
1665 if (likely (itd->index[uframe] == -1))
1666 continue;
1667 urb_index = itd->index[uframe];
1668 desc = &urb->iso_frame_desc [urb_index];
1669
Stefan Roese6dbd6822007-05-01 09:29:37 -07001670 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 itd->hw_transaction [uframe] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 /* report transfer status */
1674 if (unlikely (t & ISO_ERRS)) {
1675 urb->error_count++;
1676 if (t & EHCI_ISOC_BUF_ERR)
1677 desc->status = usb_pipein (urb->pipe)
1678 ? -ENOSR /* hc couldn't read */
1679 : -ECOMM; /* hc couldn't write */
1680 else if (t & EHCI_ISOC_BABBLE)
1681 desc->status = -EOVERFLOW;
1682 else /* (t & EHCI_ISOC_XACTERR) */
1683 desc->status = -EPROTO;
1684
1685 /* HC need not update length with this error */
Alan Sternec6d67e2009-06-29 14:34:59 -04001686 if (!(t & EHCI_ISOC_BABBLE)) {
1687 desc->actual_length = EHCI_ITD_LENGTH(t);
1688 urb->actual_length += desc->actual_length;
1689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1691 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04001692 desc->actual_length = EHCI_ITD_LENGTH(t);
1693 urb->actual_length += desc->actual_length;
Alan Sternb40e43f2008-05-20 16:59:10 -04001694 } else {
1695 /* URB was too late */
Alan Stern4005ad42012-10-01 10:32:01 -04001696 urb->error_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698 }
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /* handle completion now? */
1701 if (likely ((urb_index + 1) != urb->number_of_packets))
David Brownell30bf54e2007-12-16 22:37:40 -08001702 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 /* ASSERT: it's really the last itd for this urb
1705 list_for_each_entry (itd, &stream->td_list, itd_list)
1706 BUG_ON (itd->urb == urb);
1707 */
1708
David Brownellaa16ca32007-12-30 23:45:19 -08001709 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05001710 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04001711 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08001712 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Alan Stern569b3942012-07-11 11:23:00 -04001715 --ehci->isoc_count;
1716 disable_periodic(ehci);
1717
1718 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
Alex He05570292010-12-07 10:10:08 +08001719 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001720 if (ehci->amd_pll_fix == 1)
1721 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08001722 }
1723
Karsten Wiese508db8c2009-02-26 01:47:48 +01001724 if (unlikely(list_is_singular(&stream->td_list))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 ehci_to_hcd(ehci)->self.bandwidth_allocated
1726 -= stream->bandwidth;
1727 ehci_vdbg (ehci,
1728 "deschedule devp %s ep%d%s-iso\n",
1729 dev->devpath, stream->bEndpointAddress & 0x0f,
1730 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1731 }
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001732
David Brownell30bf54e2007-12-16 22:37:40 -08001733done:
David Brownell30bf54e2007-12-16 22:37:40 -08001734 itd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04001735
1736 /* Add to the end of the free list for later reuse */
1737 list_move_tail(&itd->itd_list, &stream->free_list);
1738
1739 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
1740 if (list_empty(&stream->td_list)) {
1741 list_splice_tail_init(&stream->free_list,
1742 &ehci->cached_itd_list);
1743 start_free_itds(ehci);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001744 }
Alan Stern55934eb2012-07-11 11:22:35 -04001745
David Brownell30bf54e2007-12-16 22:37:40 -08001746 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
1749/*-------------------------------------------------------------------------*/
1750
Olav Kongas5db539e2005-06-23 20:25:36 +03001751static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001752 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
1754 int status = -EINVAL;
1755 unsigned long flags;
1756 struct ehci_iso_stream *stream;
1757
1758 /* Get iso_stream head */
1759 stream = iso_stream_find (ehci, urb);
1760 if (unlikely (stream == NULL)) {
1761 ehci_dbg (ehci, "can't get iso stream\n");
1762 return -ENOMEM;
1763 }
1764 if (unlikely (urb->interval != stream->interval)) {
1765 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1766 stream->interval, urb->interval);
1767 goto done;
1768 }
1769
1770#ifdef EHCI_URB_TRACE
1771 ehci_dbg (ehci,
1772 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001773 __func__, urb->dev->devpath, urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 usb_pipeendpoint (urb->pipe),
1775 usb_pipein (urb->pipe) ? "in" : "out",
1776 urb->transfer_buffer_length,
1777 urb->number_of_packets, urb->interval,
1778 stream);
1779#endif
1780
1781 /* allocate ITDs w/o locking anything */
1782 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1783 if (unlikely (status < 0)) {
1784 ehci_dbg (ehci, "can't init itds\n");
1785 goto done;
1786 }
1787
1788 /* schedule ... need to lock */
1789 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001790 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001791 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04001792 goto done_not_linked;
1793 }
1794 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1795 if (unlikely(status))
1796 goto done_not_linked;
1797 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07001798 if (likely (status == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04001800 else
1801 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001802 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001804 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return status;
1806}
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/*-------------------------------------------------------------------------*/
1809
1810/*
1811 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1812 * TTs in USB 2.0 hubs. These need microframe scheduling.
1813 */
1814
1815static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001816sitd_sched_init(
1817 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 struct ehci_iso_sched *iso_sched,
1819 struct ehci_iso_stream *stream,
1820 struct urb *urb
1821)
1822{
1823 unsigned i;
1824 dma_addr_t dma = urb->transfer_dma;
1825
1826 /* how many frames are needed for these transfers */
1827 iso_sched->span = urb->number_of_packets * stream->interval;
1828
1829 /* figure out per-frame sitd fields that we'll need later
1830 * when we fit new sitds into the schedule.
1831 */
1832 for (i = 0; i < urb->number_of_packets; i++) {
1833 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1834 unsigned length;
1835 dma_addr_t buf;
1836 u32 trans;
1837
1838 length = urb->iso_frame_desc [i].length & 0x03ff;
1839 buf = dma + urb->iso_frame_desc [i].offset;
1840
1841 trans = SITD_STS_ACTIVE;
1842 if (((i + 1) == urb->number_of_packets)
1843 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1844 trans |= SITD_IOC;
1845 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001846 packet->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 /* might need to cross a buffer page within a td */
1849 packet->bufp = buf;
1850 packet->buf1 = (buf + length) & ~0x0fff;
1851 if (packet->buf1 != (buf & ~(u64)0x0fff))
1852 packet->cross = 1;
1853
David Brownell53bd6a62006-08-30 14:50:06 -07001854 /* OUT uses multiple start-splits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (stream->bEndpointAddress & USB_DIR_IN)
1856 continue;
1857 length = (length + 187) / 188;
1858 if (length > 1) /* BEGIN vs ALL */
1859 length |= 1 << 3;
1860 packet->buf1 |= length;
1861 }
1862}
1863
1864static int
1865sitd_urb_transaction (
1866 struct ehci_iso_stream *stream,
1867 struct ehci_hcd *ehci,
1868 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001869 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870)
1871{
1872 struct ehci_sitd *sitd;
1873 dma_addr_t sitd_dma;
1874 int i;
1875 struct ehci_iso_sched *iso_sched;
1876 unsigned long flags;
1877
1878 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1879 if (iso_sched == NULL)
1880 return -ENOMEM;
1881
Stefan Roese6dbd6822007-05-01 09:29:37 -07001882 sitd_sched_init(ehci, iso_sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 /* allocate/init sITDs */
1885 spin_lock_irqsave (&ehci->lock, flags);
1886 for (i = 0; i < urb->number_of_packets; i++) {
1887
1888 /* NOTE: for now, we don't try to handle wraparound cases
1889 * for IN (using sitd->hw_backpointer, like a FSTN), which
1890 * means we never need two sitds for full speed packets.
1891 */
1892
Alan Stern55934eb2012-07-11 11:22:35 -04001893 /*
1894 * Use siTDs from the free list, but not siTDs that may
1895 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 */
Alan Stern55934eb2012-07-11 11:22:35 -04001897 if (likely(!list_empty(&stream->free_list))) {
1898 sitd = list_first_entry(&stream->free_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct ehci_sitd, sitd_list);
Alan Sternf4289072012-07-11 11:23:07 -04001900 if (sitd->frame == ehci->now_frame)
Alan Stern55934eb2012-07-11 11:22:35 -04001901 goto alloc_sitd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 list_del (&sitd->sitd_list);
1903 sitd_dma = sitd->sitd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001904 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001905 alloc_sitd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 spin_unlock_irqrestore (&ehci->lock, flags);
1907 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1908 &sitd_dma);
1909 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001910 if (!sitd) {
1911 iso_sched_free(stream, iso_sched);
1912 spin_unlock_irqrestore(&ehci->lock, flags);
1913 return -ENOMEM;
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 memset (sitd, 0, sizeof *sitd);
1918 sitd->sitd_dma = sitd_dma;
1919 list_add (&sitd->sitd_list, &iso_sched->td_list);
1920 }
1921
1922 /* temporarily store schedule info in hcpriv */
1923 urb->hcpriv = iso_sched;
1924 urb->error_count = 0;
1925
1926 spin_unlock_irqrestore (&ehci->lock, flags);
1927 return 0;
1928}
1929
1930/*-------------------------------------------------------------------------*/
1931
1932static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001933sitd_patch(
1934 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 struct ehci_iso_stream *stream,
1936 struct ehci_sitd *sitd,
1937 struct ehci_iso_sched *iso_sched,
1938 unsigned index
1939)
1940{
1941 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1942 u64 bufp = uf->bufp;
1943
Stefan Roese6dbd6822007-05-01 09:29:37 -07001944 sitd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 sitd->hw_fullspeed_ep = stream->address;
1946 sitd->hw_uframe = stream->splits;
1947 sitd->hw_results = uf->transaction;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001948 sitd->hw_backpointer = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 bufp = uf->bufp;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001951 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
1952 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Stefan Roese6dbd6822007-05-01 09:29:37 -07001954 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (uf->cross)
1956 bufp += 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001957 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 sitd->index = index;
1959}
1960
1961static inline void
1962sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
1963{
1964 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1965 sitd->sitd_next = ehci->pshadow [frame];
1966 sitd->hw_next = ehci->periodic [frame];
1967 ehci->pshadow [frame].sitd = sitd;
1968 sitd->frame = frame;
1969 wmb ();
Stefan Roese6dbd6822007-05-01 09:29:37 -07001970 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973/* fit urb's sitds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001974static void sitd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 struct ehci_hcd *ehci,
1976 struct urb *urb,
1977 unsigned mod,
1978 struct ehci_iso_stream *stream
1979)
1980{
1981 int packet;
1982 unsigned next_uframe;
1983 struct ehci_iso_sched *sched = urb->hcpriv;
1984 struct ehci_sitd *sitd;
1985
1986 next_uframe = stream->next_uframe;
1987
1988 if (list_empty(&stream->td_list)) {
1989 /* usbfs ignores TT bandwidth */
1990 ehci_to_hcd(ehci)->self.bandwidth_allocated
1991 += stream->bandwidth;
1992 ehci_vdbg (ehci,
1993 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
1994 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1995 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
Alan Sternbccbefa2010-07-14 11:03:36 -04001996 (next_uframe >> 3) & (ehci->periodic_size - 1),
Stefan Roese6dbd6822007-05-01 09:29:37 -07001997 stream->interval, hc32_to_cpu(ehci, stream->splits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
Alex He05570292010-12-07 10:10:08 +08001999
2000 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002001 if (ehci->amd_pll_fix == 1)
2002 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08002003 }
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
2006
2007 /* fill sITDs frame by frame */
2008 for (packet = 0, sitd = NULL;
2009 packet < urb->number_of_packets;
2010 packet++) {
2011
2012 /* ASSERT: we have all necessary sitds */
2013 BUG_ON (list_empty (&sched->td_list));
2014
2015 /* ASSERT: no itds for this endpoint in this frame */
2016
2017 sitd = list_entry (sched->td_list.next,
2018 struct ehci_sitd, sitd_list);
2019 list_move_tail (&sitd->sitd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002020 sitd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01002021 sitd->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Stefan Roese6dbd6822007-05-01 09:29:37 -07002023 sitd_patch(ehci, stream, sitd, sched, packet);
Alan Sternbccbefa2010-07-14 11:03:36 -04002024 sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 sitd);
2026
2027 next_uframe += stream->interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
Alan Sternbccbefa2010-07-14 11:03:36 -04002029 stream->next_uframe = next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 /* don't need that schedule data any more */
2032 iso_sched_free (stream, sched);
Alan Stern2656a9a2012-11-08 10:17:01 -05002033 urb->hcpriv = stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Alan Stern569b3942012-07-11 11:23:00 -04002035 ++ehci->isoc_count;
Alan Sternb015cb72012-07-11 11:22:10 -04002036 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
2039/*-------------------------------------------------------------------------*/
2040
2041#define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
David Brownell53bd6a62006-08-30 14:50:06 -07002042 | SITD_STS_XACT | SITD_STS_MMF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
David Brownell30bf54e2007-12-16 22:37:40 -08002044/* Process and recycle a completed SITD. Return true iff its urb completed,
2045 * and hence its completion callback probably added things to the hardware
2046 * schedule.
2047 *
2048 * Note that we carefully avoid recycling this descriptor until after any
2049 * completion callback runs, so that it won't be reused quickly. That is,
2050 * assuming (a) no more than two urbs per frame on this endpoint, and also
2051 * (b) only this endpoint's completions submit URBs. It seems some silicon
2052 * corrupts things if you reuse completed descriptors very quickly...
2053 */
Alan Sternf4289072012-07-11 11:23:07 -04002054static bool sitd_complete(struct ehci_hcd *ehci, struct ehci_sitd *sitd)
2055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 struct urb *urb = sitd->urb;
2057 struct usb_iso_packet_descriptor *desc;
2058 u32 t;
2059 int urb_index = -1;
2060 struct ehci_iso_stream *stream = sitd->stream;
2061 struct usb_device *dev;
Alan Sternf4289072012-07-11 11:23:07 -04002062 bool retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 urb_index = sitd->index;
2065 desc = &urb->iso_frame_desc [urb_index];
Stefan Roese6dbd6822007-05-01 09:29:37 -07002066 t = hc32_to_cpup(ehci, &sitd->hw_results);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 /* report transfer status */
Alan Stern4005ad42012-10-01 10:32:01 -04002069 if (unlikely(t & SITD_ERRS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 urb->error_count++;
2071 if (t & SITD_STS_DBE)
2072 desc->status = usb_pipein (urb->pipe)
2073 ? -ENOSR /* hc couldn't read */
2074 : -ECOMM; /* hc couldn't write */
2075 else if (t & SITD_STS_BABBLE)
2076 desc->status = -EOVERFLOW;
2077 else /* XACT, MMF, etc */
2078 desc->status = -EPROTO;
Alan Stern4005ad42012-10-01 10:32:01 -04002079 } else if (unlikely(t & SITD_STS_ACTIVE)) {
2080 /* URB was too late */
2081 urb->error_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 } else {
2083 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04002084 desc->actual_length = desc->length - SITD_LENGTH(t);
2085 urb->actual_length += desc->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 /* handle completion now? */
2089 if ((urb_index + 1) != urb->number_of_packets)
David Brownell30bf54e2007-12-16 22:37:40 -08002090 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 /* ASSERT: it's really the last sitd for this urb
2093 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2094 BUG_ON (sitd->urb == urb);
2095 */
2096
David Brownellaa16ca32007-12-30 23:45:19 -08002097 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05002098 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04002099 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08002100 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Alan Stern569b3942012-07-11 11:23:00 -04002103 --ehci->isoc_count;
2104 disable_periodic(ehci);
2105
2106 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
Alex He05570292010-12-07 10:10:08 +08002107 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002108 if (ehci->amd_pll_fix == 1)
2109 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08002110 }
2111
Karsten Wiese508db8c2009-02-26 01:47:48 +01002112 if (list_is_singular(&stream->td_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 ehci_to_hcd(ehci)->self.bandwidth_allocated
2114 -= stream->bandwidth;
2115 ehci_vdbg (ehci,
2116 "deschedule devp %s ep%d%s-iso\n",
2117 dev->devpath, stream->bEndpointAddress & 0x0f,
2118 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
2119 }
Alan Stern0e5f2312010-04-08 16:56:37 -04002120
David Brownell30bf54e2007-12-16 22:37:40 -08002121done:
David Brownell30bf54e2007-12-16 22:37:40 -08002122 sitd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04002123
2124 /* Add to the end of the free list for later reuse */
2125 list_move_tail(&sitd->sitd_list, &stream->free_list);
2126
2127 /* Recycle the siTDs when the pipeline is empty (ep no longer in use) */
2128 if (list_empty(&stream->td_list)) {
2129 list_splice_tail_init(&stream->free_list,
2130 &ehci->cached_sitd_list);
2131 start_free_itds(ehci);
Alan Stern0e5f2312010-04-08 16:56:37 -04002132 }
Alan Stern55934eb2012-07-11 11:22:35 -04002133
David Brownell30bf54e2007-12-16 22:37:40 -08002134 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
2137
Olav Kongas5db539e2005-06-23 20:25:36 +03002138static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04002139 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 int status = -EINVAL;
2142 unsigned long flags;
2143 struct ehci_iso_stream *stream;
2144
2145 /* Get iso_stream head */
2146 stream = iso_stream_find (ehci, urb);
2147 if (stream == NULL) {
2148 ehci_dbg (ehci, "can't get iso stream\n");
2149 return -ENOMEM;
2150 }
2151 if (urb->interval != stream->interval) {
2152 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
2153 stream->interval, urb->interval);
2154 goto done;
2155 }
2156
2157#ifdef EHCI_URB_TRACE
2158 ehci_dbg (ehci,
2159 "submit %p dev%s ep%d%s-iso len %d\n",
2160 urb, urb->dev->devpath,
2161 usb_pipeendpoint (urb->pipe),
2162 usb_pipein (urb->pipe) ? "in" : "out",
2163 urb->transfer_buffer_length);
2164#endif
2165
2166 /* allocate SITDs */
2167 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2168 if (status < 0) {
2169 ehci_dbg (ehci, "can't init sitds\n");
2170 goto done;
2171 }
2172
2173 /* schedule ... need to lock */
2174 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04002175 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002176 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04002177 goto done_not_linked;
2178 }
2179 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2180 if (unlikely(status))
2181 goto done_not_linked;
2182 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07002183 if (status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04002185 else
2186 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002187 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002189 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return status;
2191}
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193/*-------------------------------------------------------------------------*/
2194
Alan Stern569b3942012-07-11 11:23:00 -04002195static void scan_isoc(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Alan Sternf4289072012-07-11 11:23:07 -04002197 unsigned uf, now_frame, frame;
2198 unsigned fmask = ehci->periodic_size - 1;
2199 bool modified, live;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 /*
2202 * When running, scan from last scan point up to "now"
2203 * else clean up by scanning everything that's left.
2204 * Touches as few pages as possible: cache-friendly.
2205 */
Alan Sternc0c53db2012-07-11 11:21:48 -04002206 if (ehci->rh_state >= EHCI_RH_RUNNING) {
Alan Sternf4289072012-07-11 11:23:07 -04002207 uf = ehci_read_frame_index(ehci);
2208 now_frame = (uf >> 3) & fmask;
2209 live = true;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002210 } else {
Alan Sternc3ee9b72012-09-28 16:01:23 -04002211 now_frame = (ehci->last_iso_frame - 1) & fmask;
Alan Sternf4289072012-07-11 11:23:07 -04002212 live = false;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002213 }
Alan Sternf4289072012-07-11 11:23:07 -04002214 ehci->now_frame = now_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Alan Sternb09a61c2013-01-30 16:35:02 -05002216 frame = ehci->last_iso_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 for (;;) {
2218 union ehci_shadow q, *q_p;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002219 __hc32 type, *hw_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221restart:
2222 /* scan each element in frame's queue for completions */
2223 q_p = &ehci->pshadow [frame];
2224 hw_p = &ehci->periodic [frame];
2225 q.ptr = q_p->ptr;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002226 type = Q_NEXT_TYPE(ehci, *hw_p);
Alan Sternf4289072012-07-11 11:23:07 -04002227 modified = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 while (q.ptr != NULL) {
Stefan Roese6dbd6822007-05-01 09:29:37 -07002230 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 case Q_TYPE_ITD:
David Brownell79592b722008-01-07 00:47:42 -08002232 /* If this ITD is still active, leave it for
2233 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002234 * No need to check for activity unless the
2235 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002236 */
Alan Sternf4289072012-07-11 11:23:07 -04002237 if (frame == now_frame && live) {
Alan Sternb40e43f2008-05-20 16:59:10 -04002238 rmb();
2239 for (uf = 0; uf < 8; uf++) {
2240 if (q.itd->hw_transaction[uf] &
2241 ITD_ACTIVE(ehci))
2242 break;
2243 }
2244 if (uf < 8) {
Alan Sternb40e43f2008-05-20 16:59:10 -04002245 q_p = &q.itd->itd_next;
2246 hw_p = &q.itd->hw_next;
2247 type = Q_NEXT_TYPE(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -07002248 q.itd->hw_next);
Alan Sternb40e43f2008-05-20 16:59:10 -04002249 q = *q_p;
2250 break;
2251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
David Brownell79592b722008-01-07 00:47:42 -08002254 /* Take finished ITDs out of the schedule
2255 * and process them: recycle, maybe report
2256 * URB completion. HC won't cache the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 * pointer for much longer, if at all.
2258 */
2259 *q_p = q.itd->itd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002260 if (!ehci->use_dummy_qh ||
2261 q.itd->hw_next != EHCI_LIST_END(ehci))
2262 *hw_p = q.itd->hw_next;
2263 else
2264 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002265 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002267 modified = itd_complete (ehci, q.itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 q = *q_p;
2269 break;
2270 case Q_TYPE_SITD:
David Brownell79592b722008-01-07 00:47:42 -08002271 /* If this SITD is still active, leave it for
2272 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002273 * No need to check for activity unless the
2274 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002275 */
Alan Sternf4289072012-07-11 11:23:07 -04002276 if (((frame == now_frame) ||
2277 (((frame + 1) & fmask) == now_frame))
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002278 && live
2279 && (q.sitd->hw_results &
2280 SITD_ACTIVE(ehci))) {
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 q_p = &q.sitd->sitd_next;
2283 hw_p = &q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002284 type = Q_NEXT_TYPE(ehci,
2285 q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 q = *q_p;
2287 break;
2288 }
David Brownell79592b722008-01-07 00:47:42 -08002289
2290 /* Take finished SITDs out of the schedule
2291 * and process them: recycle, maybe report
2292 * URB completion.
2293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 *q_p = q.sitd->sitd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002295 if (!ehci->use_dummy_qh ||
2296 q.sitd->hw_next != EHCI_LIST_END(ehci))
2297 *hw_p = q.sitd->hw_next;
2298 else
2299 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002300 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002302 modified = sitd_complete (ehci, q.sitd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 q = *q_p;
2304 break;
2305 default:
Greg Kroah-Hartman2d0fe1b2012-05-01 21:33:36 -07002306 ehci_dbg(ehci, "corrupt type %d frame %d shadow %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 type, frame, q.ptr);
2308 // BUG ();
Alan Stern569b3942012-07-11 11:23:00 -04002309 /* FALL THROUGH */
2310 case Q_TYPE_QH:
2311 case Q_TYPE_FSTN:
2312 /* End of the iTDs and siTDs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 q.ptr = NULL;
Alan Stern569b3942012-07-11 11:23:00 -04002314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316
2317 /* assume completion callbacks modify the queue */
Alan Sternf4289072012-07-11 11:23:07 -04002318 if (unlikely(modified && ehci->isoc_count > 0))
2319 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321
Alan Sternf4289072012-07-11 11:23:07 -04002322 /* Stop when we have reached the current frame */
2323 if (frame == now_frame)
David Brownell79592b722008-01-07 00:47:42 -08002324 break;
Alan Sternb09a61c2013-01-30 16:35:02 -05002325
2326 /* The last frame may still have active siTDs */
2327 ehci->last_iso_frame = frame;
2328 frame = (frame + 1) & fmask;
David Brownell53bd6a62006-08-30 14:50:06 -07002329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}