blob: e7a2dbd27b1ee484a8dce46fe8aa105e0f632095 [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;
Alan Stern7bc782d2013-03-22 13:31:11 -0400542 qh->exception = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* update per-qh bandwidth for usbfs */
545 ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period
546 ? ((qh->usecs + qh->c_usecs) / qh->period)
547 : (qh->usecs * 8);
548
Alan Stern569b3942012-07-11 11:23:00 -0400549 list_add(&qh->intr_node, &ehci->intr_qh_list);
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* maybe enable periodic schedule processing */
Alan Stern569b3942012-07-11 11:23:00 -0400552 ++ehci->intr_count;
Alan Sternb015cb72012-07-11 11:22:10 -0400553 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Alan Sternb015cb72012-07-11 11:22:10 -0400556static void qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 unsigned i;
559 unsigned period;
560
Alan Sterndf202252012-07-11 11:22:26 -0400561 /*
562 * If qh is for a low/full-speed device, simply unlinking it
563 * could interfere with an ongoing split transaction. To unlink
564 * it safely would require setting the QH_INACTIVATE bit and
565 * waiting at least one frame, as described in EHCI 4.12.2.5.
566 *
567 * We won't bother with any of this. Instead, we assume that the
568 * only reason for unlinking an interrupt QH while the current URB
569 * is still active is to dequeue all the URBs (flush the whole
570 * endpoint queue).
571 *
572 * If rebalancing the periodic schedule is ever implemented, this
573 * approach will no longer be valid.
574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /* high bandwidth, or otherwise part of every microframe */
577 if ((period = qh->period) == 0)
578 period = 1;
579
580 for (i = qh->start; i < ehci->periodic_size; i += period)
581 periodic_unlink (ehci, i, qh);
582
583 /* update per-qh bandwidth for usbfs */
584 ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period
585 ? ((qh->usecs + qh->c_usecs) / qh->period)
586 : (qh->usecs * 8);
587
588 dev_dbg (&qh->dev->dev,
589 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
David Brownell7dedacf2005-08-04 18:06:41 -0700590 qh->period,
Alek Du3807e262009-07-14 07:23:29 +0800591 hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 qh, qh->start, qh->usecs, qh->c_usecs);
593
594 /* qh->qh_next still "live" to HC */
595 qh->qh_state = QH_STATE_UNLINK;
596 qh->qh_next.ptr = NULL;
Alan Stern569b3942012-07-11 11:23:00 -0400597
598 if (ehci->qh_scan_next == qh)
599 ehci->qh_scan_next = list_entry(qh->intr_node.next,
600 struct ehci_qh, intr_node);
601 list_del(&qh->intr_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Alan Sterndf202252012-07-11 11:22:26 -0400604static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Alan Stern7bc782d2013-03-22 13:31:11 -0400606 /* If the QH isn't linked then there's nothing we can do. */
607 if (qh->qh_state != QH_STATE_LINKED)
Alan Sterna448c9d2009-08-19 12:22:44 -0400608 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 qh_unlink_periodic (ehci, qh);
611
Alan Sterndf202252012-07-11 11:22:26 -0400612 /* Make sure the unlinks are visible before starting the timer */
613 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Alan Sterndf202252012-07-11 11:22:26 -0400615 /*
616 * The EHCI spec doesn't say how long it takes the controller to
617 * stop accessing an unlinked interrupt QH. The timer delay is
618 * 9 uframes; presumably that will be long enough.
619 */
620 qh->unlink_cycle = ehci->intr_unlink_cycle;
621
622 /* New entries go at the end of the intr_unlink list */
623 if (ehci->intr_unlink)
624 ehci->intr_unlink_last->unlink_next = qh;
625 else
626 ehci->intr_unlink = qh;
627 ehci->intr_unlink_last = qh;
628
629 if (ehci->intr_unlinking)
630 ; /* Avoid recursive calls */
631 else if (ehci->rh_state < EHCI_RH_RUNNING)
632 ehci_handle_intr_unlinks(ehci);
633 else if (ehci->intr_unlink == qh) {
634 ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
635 ++ehci->intr_unlink_cycle;
636 }
637}
638
639static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
640{
641 struct ehci_qh_hw *hw = qh->hw;
642 int rc;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 qh->qh_state = QH_STATE_IDLE;
Alek Du3807e262009-07-14 07:23:29 +0800645 hw->hw_next = EHCI_LIST_END(ehci);
Alan Sterna448c9d2009-08-19 12:22:44 -0400646
Alan Stern79bcf7b2013-03-22 13:30:56 -0400647 if (!list_empty(&qh->qtd_list))
648 qh_completions(ehci, qh);
Alan Sterna448c9d2009-08-19 12:22:44 -0400649
650 /* reschedule QH iff another request is queued */
Alan Sterndf202252012-07-11 11:22:26 -0400651 if (!list_empty(&qh->qtd_list) && ehci->rh_state == EHCI_RH_RUNNING) {
Alan Sterna448c9d2009-08-19 12:22:44 -0400652 rc = qh_schedule(ehci, qh);
653
654 /* An error here likely indicates handshake failure
655 * or no space left in the schedule. Neither fault
656 * should happen often ...
657 *
658 * FIXME kill the now-dysfunctional queued urbs
659 */
660 if (rc != 0)
661 ehci_err(ehci, "can't reschedule qh %p, err %d\n",
662 qh, rc);
663 }
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400664
665 /* maybe turn off periodic schedule */
Alan Stern569b3942012-07-11 11:23:00 -0400666 --ehci->intr_count;
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400667 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670/*-------------------------------------------------------------------------*/
671
672static int check_period (
David Brownell53bd6a62006-08-30 14:50:06 -0700673 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 unsigned frame,
675 unsigned uframe,
676 unsigned period,
677 unsigned usecs
678) {
679 int claimed;
680
681 /* complete split running into next frame?
682 * given FSTN support, we could sometimes check...
683 */
684 if (uframe >= 8)
685 return 0;
686
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +0400687 /* convert "usecs we need" to "max already claimed" */
688 usecs = ehci->uframe_periodic_max - usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /* we "know" 2 and 4 uframe intervals were rejected; so
691 * for period 0, check _every_ microframe in the schedule.
692 */
693 if (unlikely (period == 0)) {
694 do {
695 for (uframe = 0; uframe < 7; uframe++) {
696 claimed = periodic_usecs (ehci, frame, uframe);
697 if (claimed > usecs)
698 return 0;
699 }
700 } while ((frame += 1) < ehci->periodic_size);
701
702 /* just check the specified uframe, at that period */
703 } else {
704 do {
705 claimed = periodic_usecs (ehci, frame, uframe);
706 if (claimed > usecs)
707 return 0;
708 } while ((frame += period) < ehci->periodic_size);
709 }
710
711 // success!
712 return 1;
713}
714
715static int check_intr_schedule (
David Brownell53bd6a62006-08-30 14:50:06 -0700716 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 unsigned frame,
718 unsigned uframe,
719 const struct ehci_qh *qh,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700720 __hc32 *c_maskp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721)
722{
David Brownell53bd6a62006-08-30 14:50:06 -0700723 int retval = -ENOSPC;
Dan Streetmanba47f662006-05-24 09:39:16 -0700724 u8 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
727 goto done;
728
729 if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
730 goto done;
731 if (!qh->c_usecs) {
732 retval = 0;
733 *c_maskp = 0;
734 goto done;
735 }
736
Dan Streetmanba47f662006-05-24 09:39:16 -0700737#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
738 if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
739 qh->tt_usecs)) {
740 unsigned i;
741
742 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
743 for (i=uframe+1; i<8 && i<uframe+4; i++)
744 if (!check_period (ehci, frame, i,
745 qh->period, qh->c_usecs))
746 goto done;
747 else
748 mask |= 1 << i;
749
750 retval = 0;
751
Stefan Roese6dbd6822007-05-01 09:29:37 -0700752 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Dan Streetmanba47f662006-05-24 09:39:16 -0700753 }
754#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /* Make sure this tt's buffer is also available for CSPLITs.
756 * We pessimize a bit; probably the typical full speed case
757 * doesn't need the second CSPLIT.
David Brownell53bd6a62006-08-30 14:50:06 -0700758 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * NOTE: both SPLIT and CSPLIT could be checked in just
760 * one smart pass...
761 */
762 mask = 0x03 << (uframe + qh->gap_uf);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700763 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 mask |= 1 << uframe;
766 if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
767 if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
768 qh->period, qh->c_usecs))
769 goto done;
770 if (!check_period (ehci, frame, uframe + qh->gap_uf,
771 qh->period, qh->c_usecs))
772 goto done;
773 retval = 0;
774 }
Dan Streetmanba47f662006-05-24 09:39:16 -0700775#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776done:
777 return retval;
778}
779
780/* "first fit" scheduling policy used the first time through,
781 * or when the previous schedule slot can't be re-used.
782 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700783static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
David Brownell53bd6a62006-08-30 14:50:06 -0700785 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 unsigned uframe;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700787 __hc32 c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
Alek Du3807e262009-07-14 07:23:29 +0800789 struct ehci_qh_hw *hw = qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Alek Du3807e262009-07-14 07:23:29 +0800791 hw->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 frame = qh->start;
793
794 /* reuse the previous schedule slots, if we can */
795 if (frame < qh->period) {
Alek Du3807e262009-07-14 07:23:29 +0800796 uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 status = check_intr_schedule (ehci, frame, --uframe,
798 qh, &c_mask);
799 } else {
800 uframe = 0;
801 c_mask = 0;
802 status = -ENOSPC;
803 }
804
805 /* else scan the schedule to find a group of slots such that all
806 * uframes have enough periodic bandwidth available.
807 */
808 if (status) {
809 /* "normal" case, uframing flexible except with splits */
810 if (qh->period) {
Alan Stern68335e82009-05-22 17:02:33 -0400811 int i;
812
813 for (i = qh->period; status && i > 0; --i) {
814 frame = ++ehci->random_frame % qh->period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 for (uframe = 0; uframe < 8; uframe++) {
816 status = check_intr_schedule (ehci,
817 frame, uframe, qh,
818 &c_mask);
819 if (status == 0)
820 break;
821 }
Alan Stern68335e82009-05-22 17:02:33 -0400822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /* qh->period == 0 means every uframe */
825 } else {
826 frame = 0;
827 status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
828 }
829 if (status)
830 goto done;
831 qh->start = frame;
832
833 /* reset S-frame and (maybe) C-frame masks */
Alek Du3807e262009-07-14 07:23:29 +0800834 hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
835 hw->hw_info2 |= qh->period
Stefan Roese6dbd6822007-05-01 09:29:37 -0700836 ? cpu_to_hc32(ehci, 1 << uframe)
837 : cpu_to_hc32(ehci, QH_SMASK);
Alek Du3807e262009-07-14 07:23:29 +0800838 hw->hw_info2 |= c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 } else
840 ehci_dbg (ehci, "reused qh %p schedule\n", qh);
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842done:
843 return status;
844}
845
846static int intr_submit (
847 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct urb *urb,
849 struct list_head *qtd_list,
Al Viro55016f12005-10-21 03:21:58 -0400850 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851) {
852 unsigned epnum;
853 unsigned long flags;
854 struct ehci_qh *qh;
Alan Sterne9df41c2007-08-08 11:48:02 -0400855 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 struct list_head empty;
857
858 /* get endpoint and transfer/schedule data */
Alan Sterne9df41c2007-08-08 11:48:02 -0400859 epnum = urb->ep->desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 spin_lock_irqsave (&ehci->lock, flags);
862
Alan Stern541c7d42010-06-22 16:39:10 -0400863 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100864 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -0400865 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100866 }
Alan Sterne9df41c2007-08-08 11:48:02 -0400867 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
868 if (unlikely(status))
869 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /* get qh and force any scheduling errors */
872 INIT_LIST_HEAD (&empty);
Alan Sterne9df41c2007-08-08 11:48:02 -0400873 qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (qh == NULL) {
875 status = -ENOMEM;
876 goto done;
877 }
878 if (qh->qh_state == QH_STATE_IDLE) {
879 if ((status = qh_schedule (ehci, qh)) != 0)
880 goto done;
881 }
882
883 /* then queue the urb's tds to the qh */
Alan Sterne9df41c2007-08-08 11:48:02 -0400884 qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 BUG_ON (qh == NULL);
886
Alan Sternc1fdb682013-03-22 13:30:43 -0400887 /* stuff into the periodic schedule */
888 if (qh->qh_state == QH_STATE_IDLE) {
889 qh_refresh(ehci, qh);
890 qh_link_periodic(ehci, qh);
891 }
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* ... update usbfs periodic stats */
894 ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
895
896done:
Alan Sterne9df41c2007-08-08 11:48:02 -0400897 if (unlikely(status))
898 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
899done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 spin_unlock_irqrestore (&ehci->lock, flags);
901 if (status)
902 qtd_list_free (ehci, urb, qtd_list);
903
904 return status;
905}
906
Alan Stern569b3942012-07-11 11:23:00 -0400907static void scan_intr(struct ehci_hcd *ehci)
908{
909 struct ehci_qh *qh;
910
911 list_for_each_entry_safe(qh, ehci->qh_scan_next, &ehci->intr_qh_list,
912 intr_node) {
Alan Stern79bcf7b2013-03-22 13:30:56 -0400913
Alan Stern569b3942012-07-11 11:23:00 -0400914 /* clean any finished work for this qh */
915 if (!list_empty(&qh->qtd_list)) {
916 int temp;
917
918 /*
919 * Unlinks could happen here; completion reporting
920 * drops the lock. That's why ehci->qh_scan_next
921 * always holds the next qh to scan; if the next qh
922 * gets unlinked then ehci->qh_scan_next is adjusted
923 * in qh_unlink_periodic().
924 */
925 temp = qh_completions(ehci, qh);
Alan Stern79bcf7b2013-03-22 13:30:56 -0400926 if (unlikely(temp || (list_empty(&qh->qtd_list) &&
927 qh->qh_state == QH_STATE_LINKED)))
Alan Stern569b3942012-07-11 11:23:00 -0400928 start_unlink_intr(ehci, qh);
Alan Stern569b3942012-07-11 11:23:00 -0400929 }
930 }
931}
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933/*-------------------------------------------------------------------------*/
934
935/* ehci_iso_stream ops work with both ITD and SITD */
936
937static struct ehci_iso_stream *
Al Viro55016f12005-10-21 03:21:58 -0400938iso_stream_alloc (gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct ehci_iso_stream *stream;
941
Pekka Enberg7b842b62005-09-06 15:18:34 -0700942 stream = kzalloc(sizeof *stream, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 INIT_LIST_HEAD(&stream->td_list);
945 INIT_LIST_HEAD(&stream->free_list);
946 stream->next_uframe = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 return stream;
949}
950
951static void
952iso_stream_init (
953 struct ehci_hcd *ehci,
954 struct ehci_iso_stream *stream,
955 struct usb_device *dev,
956 int pipe,
957 unsigned interval
958)
959{
960 static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
961
962 u32 buf1;
963 unsigned epnum, maxp;
964 int is_input;
965 long bandwidth;
966
967 /*
968 * this might be a "high bandwidth" highspeed endpoint,
969 * as encoded in the ep descriptor's wMaxPacket field
970 */
971 epnum = usb_pipeendpoint (pipe);
972 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
973 maxp = usb_maxpacket(dev, pipe, !is_input);
974 if (is_input) {
975 buf1 = (1 << 11);
976 } else {
977 buf1 = 0;
978 }
979
980 /* knows about ITD vs SITD */
981 if (dev->speed == USB_SPEED_HIGH) {
982 unsigned multi = hb_mult(maxp);
983
984 stream->highspeed = 1;
985
986 maxp = max_packet(maxp);
987 buf1 |= maxp;
988 maxp *= multi;
989
Stefan Roese6dbd6822007-05-01 09:29:37 -0700990 stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
991 stream->buf1 = cpu_to_hc32(ehci, buf1);
992 stream->buf2 = cpu_to_hc32(ehci, multi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /* usbfs wants to report the average usecs per frame tied up
995 * when transfers on this endpoint are scheduled ...
996 */
997 stream->usecs = HS_USECS_ISO (maxp);
998 bandwidth = stream->usecs * 8;
Alan Stern372dd6e2008-11-12 17:02:57 -0500999 bandwidth /= interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 } else {
1002 u32 addr;
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001003 int think_time;
Clemens Ladisch469d0222006-01-20 13:49:10 -08001004 int hs_transfers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 addr = dev->ttport << 24;
1007 if (!ehci_is_TDI(ehci)
1008 || (dev->tt->hub !=
1009 ehci_to_hcd(ehci)->self.root_hub))
1010 addr |= dev->tt->hub->devnum << 16;
1011 addr |= epnum << 8;
1012 addr |= dev->devnum;
1013 stream->usecs = HS_USECS_ISO (maxp);
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001014 think_time = dev->tt ? dev->tt->think_time : 0;
1015 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
1016 dev->speed, is_input, 1, maxp));
Clemens Ladisch469d0222006-01-20 13:49:10 -08001017 hs_transfers = max (1u, (maxp + 187) / 188);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (is_input) {
1019 u32 tmp;
1020
1021 addr |= 1 << 31;
1022 stream->c_usecs = stream->usecs;
1023 stream->usecs = HS_USECS_ISO (1);
1024 stream->raw_mask = 1;
1025
Clemens Ladisch469d0222006-01-20 13:49:10 -08001026 /* c-mask as specified in USB 2.0 11.18.4 3.c */
1027 tmp = (1 << (hs_transfers + 2)) - 1;
1028 stream->raw_mask |= tmp << (8 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 } else
Clemens Ladisch469d0222006-01-20 13:49:10 -08001030 stream->raw_mask = smask_out [hs_transfers - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 bandwidth = stream->usecs + stream->c_usecs;
Alan Stern372dd6e2008-11-12 17:02:57 -05001032 bandwidth /= interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 /* stream->splits gets created from raw_mask later */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001035 stream->address = cpu_to_hc32(ehci, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037 stream->bandwidth = bandwidth;
1038
1039 stream->udev = dev;
1040
1041 stream->bEndpointAddress = is_input | epnum;
1042 stream->interval = interval;
1043 stream->maxp = maxp;
1044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static struct ehci_iso_stream *
1047iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1048{
1049 unsigned epnum;
1050 struct ehci_iso_stream *stream;
1051 struct usb_host_endpoint *ep;
1052 unsigned long flags;
1053
1054 epnum = usb_pipeendpoint (urb->pipe);
1055 if (usb_pipein(urb->pipe))
1056 ep = urb->dev->ep_in[epnum];
1057 else
1058 ep = urb->dev->ep_out[epnum];
1059
1060 spin_lock_irqsave (&ehci->lock, flags);
1061 stream = ep->hcpriv;
1062
1063 if (unlikely (stream == NULL)) {
1064 stream = iso_stream_alloc(GFP_ATOMIC);
1065 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 ep->hcpriv = stream;
1067 stream->ep = ep;
1068 iso_stream_init(ehci, stream, urb->dev, urb->pipe,
1069 urb->interval);
1070 }
1071
Clemens Ladisch1082f572010-03-01 17:18:56 +01001072 /* if dev->ep [epnum] is a QH, hw is set */
1073 } else if (unlikely (stream->hw != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1075 urb->dev->devpath, epnum,
1076 usb_pipein(urb->pipe) ? "in" : "out");
1077 stream = NULL;
1078 }
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 spin_unlock_irqrestore (&ehci->lock, flags);
1081 return stream;
1082}
1083
1084/*-------------------------------------------------------------------------*/
1085
1086/* ehci_iso_sched ops can be ITD-only or SITD-only */
1087
1088static struct ehci_iso_sched *
Al Viro55016f12005-10-21 03:21:58 -04001089iso_sched_alloc (unsigned packets, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct ehci_iso_sched *iso_sched;
1092 int size = sizeof *iso_sched;
1093
1094 size += packets * sizeof (struct ehci_iso_packet);
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001095 iso_sched = kzalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (likely (iso_sched != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 INIT_LIST_HEAD (&iso_sched->td_list);
1098 }
1099 return iso_sched;
1100}
1101
1102static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001103itd_sched_init(
1104 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 struct ehci_iso_sched *iso_sched,
1106 struct ehci_iso_stream *stream,
1107 struct urb *urb
1108)
1109{
1110 unsigned i;
1111 dma_addr_t dma = urb->transfer_dma;
1112
1113 /* how many uframes are needed for these transfers */
1114 iso_sched->span = urb->number_of_packets * stream->interval;
1115
1116 /* figure out per-uframe itd fields that we'll need later
1117 * when we fit new itds into the schedule.
1118 */
1119 for (i = 0; i < urb->number_of_packets; i++) {
1120 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1121 unsigned length;
1122 dma_addr_t buf;
1123 u32 trans;
1124
1125 length = urb->iso_frame_desc [i].length;
1126 buf = dma + urb->iso_frame_desc [i].offset;
1127
1128 trans = EHCI_ISOC_ACTIVE;
1129 trans |= buf & 0x0fff;
1130 if (unlikely (((i + 1) == urb->number_of_packets))
1131 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1132 trans |= EHCI_ITD_IOC;
1133 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001134 uframe->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
David Brownell77078572005-05-28 10:46:18 -07001136 /* might need to cross a buffer page within a uframe */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 uframe->bufp = (buf & ~(u64)0x0fff);
1138 buf += length;
1139 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1140 uframe->cross = 1;
1141 }
1142}
1143
1144static void
1145iso_sched_free (
1146 struct ehci_iso_stream *stream,
1147 struct ehci_iso_sched *iso_sched
1148)
1149{
1150 if (!iso_sched)
1151 return;
1152 // caller must hold ehci->lock!
1153 list_splice (&iso_sched->td_list, &stream->free_list);
1154 kfree (iso_sched);
1155}
1156
1157static int
1158itd_urb_transaction (
1159 struct ehci_iso_stream *stream,
1160 struct ehci_hcd *ehci,
1161 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001162 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163)
1164{
1165 struct ehci_itd *itd;
1166 dma_addr_t itd_dma;
1167 int i;
1168 unsigned num_itds;
1169 struct ehci_iso_sched *sched;
1170 unsigned long flags;
1171
1172 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1173 if (unlikely (sched == NULL))
1174 return -ENOMEM;
1175
Stefan Roese6dbd6822007-05-01 09:29:37 -07001176 itd_sched_init(ehci, sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 if (urb->interval < 8)
1179 num_itds = 1 + (sched->span + 7) / 8;
1180 else
1181 num_itds = urb->number_of_packets;
1182
1183 /* allocate/init ITDs */
1184 spin_lock_irqsave (&ehci->lock, flags);
1185 for (i = 0; i < num_itds; i++) {
1186
Alan Stern55934eb2012-07-11 11:22:35 -04001187 /*
1188 * Use iTDs from the free list, but not iTDs that may
1189 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 */
Alan Stern55934eb2012-07-11 11:22:35 -04001191 if (likely(!list_empty(&stream->free_list))) {
1192 itd = list_first_entry(&stream->free_list,
Stefan Roese6dbd6822007-05-01 09:29:37 -07001193 struct ehci_itd, itd_list);
Alan Sternf4289072012-07-11 11:23:07 -04001194 if (itd->frame == ehci->now_frame)
Alan Stern55934eb2012-07-11 11:22:35 -04001195 goto alloc_itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 list_del (&itd->itd_list);
1197 itd_dma = itd->itd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001198 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001199 alloc_itd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 spin_unlock_irqrestore (&ehci->lock, flags);
1201 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1202 &itd_dma);
1203 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001204 if (!itd) {
1205 iso_sched_free(stream, sched);
1206 spin_unlock_irqrestore(&ehci->lock, flags);
1207 return -ENOMEM;
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 memset (itd, 0, sizeof *itd);
1212 itd->itd_dma = itd_dma;
1213 list_add (&itd->itd_list, &sched->td_list);
1214 }
1215 spin_unlock_irqrestore (&ehci->lock, flags);
1216
1217 /* temporarily store schedule info in hcpriv */
1218 urb->hcpriv = sched;
1219 urb->error_count = 0;
1220 return 0;
1221}
1222
1223/*-------------------------------------------------------------------------*/
1224
1225static inline int
1226itd_slot_ok (
1227 struct ehci_hcd *ehci,
1228 u32 mod,
1229 u32 uframe,
1230 u8 usecs,
1231 u32 period
1232)
1233{
1234 uframe %= period;
1235 do {
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001236 /* can't commit more than uframe_periodic_max usec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001238 > (ehci->uframe_periodic_max - usecs))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return 0;
1240
1241 /* we know urb->interval is 2^N uframes */
1242 uframe += period;
1243 } while (uframe < mod);
1244 return 1;
1245}
1246
1247static inline int
1248sitd_slot_ok (
1249 struct ehci_hcd *ehci,
1250 u32 mod,
1251 struct ehci_iso_stream *stream,
1252 u32 uframe,
1253 struct ehci_iso_sched *sched,
1254 u32 period_uframes
1255)
1256{
1257 u32 mask, tmp;
1258 u32 frame, uf;
1259
1260 mask = stream->raw_mask << (uframe & 7);
1261
1262 /* for IN, don't wrap CSPLIT into the next frame */
1263 if (mask & ~0xffff)
1264 return 0;
1265
Alan Stern65b8e5c2012-05-14 13:47:20 -04001266 /* check bandwidth */
1267 uframe %= period_uframes;
1268 frame = uframe >> 3;
1269
1270#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1271 /* The tt's fullspeed bus bandwidth must be available.
1272 * tt_available scheduling guarantees 10+% for control/bulk.
1273 */
1274 uf = uframe & 7;
1275 if (!tt_available(ehci, period_uframes >> 3,
1276 stream->udev, frame, uf, stream->tt_usecs))
1277 return 0;
1278#else
1279 /* tt must be idle for start(s), any gap, and csplit.
1280 * assume scheduling slop leaves 10+% for control/bulk.
1281 */
1282 if (!tt_no_collision(ehci, period_uframes >> 3,
1283 stream->udev, frame, mask))
1284 return 0;
1285#endif
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /* this multi-pass logic is simple, but performance may
1288 * suffer when the schedule data isn't cached.
1289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 do {
1291 u32 max_used;
1292
1293 frame = uframe >> 3;
1294 uf = uframe & 7;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 /* check starts (OUT uses more than one) */
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001297 max_used = ehci->uframe_periodic_max - stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
1299 if (periodic_usecs (ehci, frame, uf) > max_used)
1300 return 0;
1301 }
1302
1303 /* for IN, check CSPLIT */
1304 if (stream->c_usecs) {
Clemens Ladisch0c734622006-01-22 10:32:49 -08001305 uf = uframe & 7;
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001306 max_used = ehci->uframe_periodic_max - stream->c_usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 do {
1308 tmp = 1 << uf;
1309 tmp <<= 8;
1310 if ((stream->raw_mask & tmp) == 0)
1311 continue;
1312 if (periodic_usecs (ehci, frame, uf)
1313 > max_used)
1314 return 0;
1315 } while (++uf < 8);
1316 }
1317
1318 /* we know urb->interval is 2^N uframes */
1319 uframe += period_uframes;
1320 } while (uframe < mod);
1321
Stefan Roese6dbd6822007-05-01 09:29:37 -07001322 stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return 1;
1324}
1325
1326/*
1327 * This scheduler plans almost as far into the future as it has actual
1328 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1329 * "as small as possible" to be cache-friendlier.) That limits the size
1330 * transfers you can stream reliably; avoid more than 64 msec per urb.
1331 * Also avoid queue depths of less than ehci's worst irq latency (affected
1332 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1333 * and other factors); or more than about 230 msec total (for portability,
1334 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1335 */
1336
Alan Sternc3ee9b72012-09-28 16:01:23 -04001337#define SCHEDULING_DELAY 40 /* microframes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339static int
1340iso_stream_schedule (
1341 struct ehci_hcd *ehci,
1342 struct urb *urb,
1343 struct ehci_iso_stream *stream
1344)
1345{
Alan Sternc3ee9b72012-09-28 16:01:23 -04001346 u32 now, base, next, start, period, span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int status;
1348 unsigned mod = ehci->periodic_size << 3;
1349 struct ehci_iso_sched *sched = urb->hcpriv;
1350
Alan Sternffda0802010-07-14 11:03:46 -04001351 period = urb->interval;
1352 span = sched->span;
1353 if (!stream->highspeed) {
1354 period <<= 3;
1355 span <<= 3;
1356 }
1357
Alan Stern68aa95d2011-10-12 10:39:14 -04001358 now = ehci_read_frame_index(ehci) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Alan Sternb40e43f2008-05-20 16:59:10 -04001360 /* Typical case: reuse current schedule, stream is still active.
1361 * Hopefully there are no gaps from the host falling behind
Alan Stern4005ad42012-10-01 10:32:01 -04001362 * (irq delays etc). If there are, the behavior depends on
1363 * whether URB_ISO_ASAP is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 */
1365 if (likely (!list_empty (&stream->td_list))) {
Sarah Sharpdccd5742009-10-27 10:55:05 -07001366
Alan Stern98cae422012-09-28 16:01:34 -04001367 /* Take the isochronous scheduling threshold into account */
1368 if (ehci->i_thresh)
1369 next = now + ehci->i_thresh; /* uframe cache */
Sarah Sharpdccd5742009-10-27 10:55:05 -07001370 else
Alan Stern98cae422012-09-28 16:01:34 -04001371 next = (now + 2 + 7) & ~0x07; /* full frame cache */
Alan Sternb40e43f2008-05-20 16:59:10 -04001372
Alan Sternc3ee9b72012-09-28 16:01:23 -04001373 /*
1374 * Use ehci->last_iso_frame as the base. There can't be any
1375 * TDs scheduled for earlier than that.
Alan Stern1fb2e052010-07-14 11:03:53 -04001376 */
Alan Sternc3ee9b72012-09-28 16:01:23 -04001377 base = ehci->last_iso_frame << 3;
1378 next = (next - base) & (mod - 1);
1379 start = (stream->next_uframe - base) & (mod - 1);
1380
1381 /* Is the schedule already full? */
1382 if (unlikely(start < period)) {
1383 ehci_dbg(ehci, "iso sched full %p (%u-%u < %u mod %u)\n",
1384 urb, stream->next_uframe, base,
1385 period, mod);
1386 status = -ENOSPC;
Alan Sternb40e43f2008-05-20 16:59:10 -04001387 goto fail;
1388 }
Alan Sternc3ee9b72012-09-28 16:01:23 -04001389
Alan Stern4005ad42012-10-01 10:32:01 -04001390 /* Behind the scheduling threshold? */
1391 if (unlikely(start < next)) {
1392
1393 /* USB_ISO_ASAP: Round up to the first available slot */
1394 if (urb->transfer_flags & URB_ISO_ASAP)
1395 start += (next - start + period - 1) & -period;
1396
1397 /*
1398 * Not ASAP: Use the next slot in the stream. If
1399 * the entire URB falls before the threshold, fail.
1400 */
1401 else if (start + span - period < next) {
1402 ehci_dbg(ehci, "iso urb late %p (%u+%u < %u)\n",
1403 urb, start + base,
1404 span - period, next + base);
1405 status = -EXDEV;
1406 goto fail;
1407 }
1408 }
Alan Sternc3ee9b72012-09-28 16:01:23 -04001409
1410 start += base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
1413 /* need to schedule; when's the next (u)frame we could start?
1414 * this is bigger than ehci->i_thresh allows; scheduling itself
Alan Sternc3ee9b72012-09-28 16:01:23 -04001415 * isn't free, the delay should handle reasonably slow cpus. it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * can also help high bandwidth if the dma and irq loads don't
1417 * jump until after the queue is primed.
1418 */
Alan Stern1fb2e052010-07-14 11:03:53 -04001419 else {
Matthieu CASTETe3420902011-11-28 11:30:22 +01001420 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Alan Sternc3ee9b72012-09-28 16:01:23 -04001422 base = now & ~0x07;
1423 start = base + SCHEDULING_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Thomas Poussevin811c9262011-10-27 18:46:48 +02001425 /* find a uframe slot with enough bandwidth.
1426 * Early uframes are more precious because full-speed
1427 * iso IN transfers can't use late uframes,
1428 * and therefore they should be allocated last.
1429 */
1430 next = start;
1431 start += period;
1432 do {
1433 start--;
Alan Stern1fb2e052010-07-14 11:03:53 -04001434 /* check schedule: enough space? */
1435 if (stream->highspeed) {
1436 if (itd_slot_ok(ehci, mod, start,
1437 stream->usecs, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001438 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001439 } else {
1440 if ((start % 8) >= 6)
1441 continue;
1442 if (sitd_slot_ok(ehci, mod, stream,
1443 start, sched, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001444 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001445 }
Matthieu CASTETe3420902011-11-28 11:30:22 +01001446 } while (start > next && !done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Alan Stern1fb2e052010-07-14 11:03:53 -04001448 /* no room in the schedule */
Matthieu CASTETe3420902011-11-28 11:30:22 +01001449 if (!done) {
Alan Sternc3ee9b72012-09-28 16:01:23 -04001450 ehci_dbg(ehci, "iso sched full %p", urb);
Alan Stern1fb2e052010-07-14 11:03:53 -04001451 status = -ENOSPC;
1452 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 }
1455
Alan Stern1fb2e052010-07-14 11:03:53 -04001456 /* Tried to schedule too far into the future? */
Alan Sternc3ee9b72012-09-28 16:01:23 -04001457 if (unlikely(start - base + span - period >= mod)) {
1458 ehci_dbg(ehci, "request %p would overflow (%u+%u >= %u)\n",
1459 urb, start - base, span - period, mod);
Alan Stern1fb2e052010-07-14 11:03:53 -04001460 status = -EFBIG;
1461 goto fail;
1462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Alan Stern1fb2e052010-07-14 11:03:53 -04001464 stream->next_uframe = start & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /* report high speed start in uframes; full speed, in frames */
1467 urb->start_frame = stream->next_uframe;
1468 if (!stream->highspeed)
1469 urb->start_frame >>= 3;
Alan Stern569b3942012-07-11 11:23:00 -04001470
1471 /* Make sure scan_isoc() sees these */
1472 if (ehci->isoc_count == 0)
Alan Sternc3ee9b72012-09-28 16:01:23 -04001473 ehci->last_iso_frame = now >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return 0;
Alan Stern1fb2e052010-07-14 11:03:53 -04001475
1476 fail:
1477 iso_sched_free(stream, sched);
1478 urb->hcpriv = NULL;
1479 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482/*-------------------------------------------------------------------------*/
1483
1484static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001485itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1486 struct ehci_itd *itd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 int i;
1489
David Brownell77078572005-05-28 10:46:18 -07001490 /* it's been recently zeroed */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001491 itd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 itd->hw_bufp [0] = stream->buf0;
1493 itd->hw_bufp [1] = stream->buf1;
1494 itd->hw_bufp [2] = stream->buf2;
1495
1496 for (i = 0; i < 8; i++)
1497 itd->index[i] = -1;
1498
1499 /* All other fields are filled when scheduling */
1500}
1501
1502static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001503itd_patch(
1504 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 struct ehci_itd *itd,
1506 struct ehci_iso_sched *iso_sched,
1507 unsigned index,
David Brownell77078572005-05-28 10:46:18 -07001508 u16 uframe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509)
1510{
1511 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1512 unsigned pg = itd->pg;
1513
1514 // BUG_ON (pg == 6 && uf->cross);
1515
1516 uframe &= 0x07;
1517 itd->index [uframe] = index;
1518
Stefan Roese6dbd6822007-05-01 09:29:37 -07001519 itd->hw_transaction[uframe] = uf->transaction;
1520 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1521 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1522 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /* iso_frame_desc[].offset must be strictly increasing */
David Brownell77078572005-05-28 10:46:18 -07001525 if (unlikely (uf->cross)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 u64 bufp = uf->bufp + 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 itd->pg = ++pg;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001529 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1530 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532}
1533
1534static inline void
1535itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1536{
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001537 union ehci_shadow *prev = &ehci->pshadow[frame];
1538 __hc32 *hw_p = &ehci->periodic[frame];
1539 union ehci_shadow here = *prev;
1540 __hc32 type = 0;
1541
1542 /* skip any iso nodes which might belong to previous microframes */
1543 while (here.ptr) {
1544 type = Q_NEXT_TYPE(ehci, *hw_p);
1545 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
1546 break;
1547 prev = periodic_next_shadow(ehci, prev, type);
1548 hw_p = shadow_next_periodic(ehci, &here, type);
1549 here = *prev;
1550 }
1551
1552 itd->itd_next = here;
1553 itd->hw_next = *hw_p;
1554 prev->itd = itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 itd->frame = frame;
1556 wmb ();
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001557 *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560/* fit urb's itds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001561static void itd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 struct ehci_hcd *ehci,
1563 struct urb *urb,
1564 unsigned mod,
1565 struct ehci_iso_stream *stream
1566)
1567{
David Brownell77078572005-05-28 10:46:18 -07001568 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 unsigned next_uframe, uframe, frame;
1570 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1571 struct ehci_itd *itd;
1572
Alan Sternbccbefa2010-07-14 11:03:36 -04001573 next_uframe = stream->next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 if (unlikely (list_empty(&stream->td_list))) {
1576 ehci_to_hcd(ehci)->self.bandwidth_allocated
1577 += stream->bandwidth;
1578 ehci_vdbg (ehci,
1579 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1580 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1581 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1582 urb->interval,
1583 next_uframe >> 3, next_uframe & 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Alex He05570292010-12-07 10:10:08 +08001585
1586 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001587 if (ehci->amd_pll_fix == 1)
1588 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08001589 }
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1592
1593 /* fill iTDs uframe by uframe */
1594 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1595 if (itd == NULL) {
1596 /* ASSERT: we have all necessary itds */
1597 // BUG_ON (list_empty (&iso_sched->td_list));
1598
1599 /* ASSERT: no itds for this endpoint in this uframe */
1600
1601 itd = list_entry (iso_sched->td_list.next,
1602 struct ehci_itd, itd_list);
1603 list_move_tail (&itd->itd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001604 itd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01001605 itd->urb = urb;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001606 itd_init (ehci, stream, itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
1609 uframe = next_uframe & 0x07;
1610 frame = next_uframe >> 3;
1611
Stefan Roese6dbd6822007-05-01 09:29:37 -07001612 itd_patch(ehci, itd, iso_sched, packet, uframe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 next_uframe += stream->interval;
Alan Sternbccbefa2010-07-14 11:03:36 -04001615 next_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 packet++;
1617
1618 /* link completed itds into the schedule */
1619 if (((next_uframe >> 3) != frame)
1620 || packet == urb->number_of_packets) {
Alan Sternbccbefa2010-07-14 11:03:36 -04001621 itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 itd = NULL;
1623 }
1624 }
1625 stream->next_uframe = next_uframe;
1626
1627 /* don't need that schedule data any more */
1628 iso_sched_free (stream, iso_sched);
Alan Stern2656a9a2012-11-08 10:17:01 -05001629 urb->hcpriv = stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Alan Stern569b3942012-07-11 11:23:00 -04001631 ++ehci->isoc_count;
Alan Sternb015cb72012-07-11 11:22:10 -04001632 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
1635#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1636
David Brownell30bf54e2007-12-16 22:37:40 -08001637/* Process and recycle a completed ITD. Return true iff its urb completed,
1638 * and hence its completion callback probably added things to the hardware
1639 * schedule.
1640 *
1641 * Note that we carefully avoid recycling this descriptor until after any
1642 * completion callback runs, so that it won't be reused quickly. That is,
1643 * assuming (a) no more than two urbs per frame on this endpoint, and also
1644 * (b) only this endpoint's completions submit URBs. It seems some silicon
1645 * corrupts things if you reuse completed descriptors very quickly...
1646 */
Alan Sternf4289072012-07-11 11:23:07 -04001647static bool itd_complete(struct ehci_hcd *ehci, struct ehci_itd *itd)
1648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct urb *urb = itd->urb;
1650 struct usb_iso_packet_descriptor *desc;
1651 u32 t;
1652 unsigned uframe;
1653 int urb_index = -1;
1654 struct ehci_iso_stream *stream = itd->stream;
1655 struct usb_device *dev;
Alan Sternf4289072012-07-11 11:23:07 -04001656 bool retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 /* for each uframe with a packet */
1659 for (uframe = 0; uframe < 8; uframe++) {
1660 if (likely (itd->index[uframe] == -1))
1661 continue;
1662 urb_index = itd->index[uframe];
1663 desc = &urb->iso_frame_desc [urb_index];
1664
Stefan Roese6dbd6822007-05-01 09:29:37 -07001665 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 itd->hw_transaction [uframe] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 /* report transfer status */
1669 if (unlikely (t & ISO_ERRS)) {
1670 urb->error_count++;
1671 if (t & EHCI_ISOC_BUF_ERR)
1672 desc->status = usb_pipein (urb->pipe)
1673 ? -ENOSR /* hc couldn't read */
1674 : -ECOMM; /* hc couldn't write */
1675 else if (t & EHCI_ISOC_BABBLE)
1676 desc->status = -EOVERFLOW;
1677 else /* (t & EHCI_ISOC_XACTERR) */
1678 desc->status = -EPROTO;
1679
1680 /* HC need not update length with this error */
Alan Sternec6d67e2009-06-29 14:34:59 -04001681 if (!(t & EHCI_ISOC_BABBLE)) {
1682 desc->actual_length = EHCI_ITD_LENGTH(t);
1683 urb->actual_length += desc->actual_length;
1684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1686 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04001687 desc->actual_length = EHCI_ITD_LENGTH(t);
1688 urb->actual_length += desc->actual_length;
Alan Sternb40e43f2008-05-20 16:59:10 -04001689 } else {
1690 /* URB was too late */
Alan Stern4005ad42012-10-01 10:32:01 -04001691 urb->error_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
1693 }
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /* handle completion now? */
1696 if (likely ((urb_index + 1) != urb->number_of_packets))
David Brownell30bf54e2007-12-16 22:37:40 -08001697 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 /* ASSERT: it's really the last itd for this urb
1700 list_for_each_entry (itd, &stream->td_list, itd_list)
1701 BUG_ON (itd->urb == urb);
1702 */
1703
David Brownellaa16ca32007-12-30 23:45:19 -08001704 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05001705 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04001706 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08001707 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Alan Stern569b3942012-07-11 11:23:00 -04001710 --ehci->isoc_count;
1711 disable_periodic(ehci);
1712
1713 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
Alex He05570292010-12-07 10:10:08 +08001714 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001715 if (ehci->amd_pll_fix == 1)
1716 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08001717 }
1718
Karsten Wiese508db8c2009-02-26 01:47:48 +01001719 if (unlikely(list_is_singular(&stream->td_list))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 ehci_to_hcd(ehci)->self.bandwidth_allocated
1721 -= stream->bandwidth;
1722 ehci_vdbg (ehci,
1723 "deschedule devp %s ep%d%s-iso\n",
1724 dev->devpath, stream->bEndpointAddress & 0x0f,
1725 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1726 }
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001727
David Brownell30bf54e2007-12-16 22:37:40 -08001728done:
David Brownell30bf54e2007-12-16 22:37:40 -08001729 itd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04001730
1731 /* Add to the end of the free list for later reuse */
1732 list_move_tail(&itd->itd_list, &stream->free_list);
1733
1734 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
1735 if (list_empty(&stream->td_list)) {
1736 list_splice_tail_init(&stream->free_list,
1737 &ehci->cached_itd_list);
1738 start_free_itds(ehci);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001739 }
Alan Stern55934eb2012-07-11 11:22:35 -04001740
David Brownell30bf54e2007-12-16 22:37:40 -08001741 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
1744/*-------------------------------------------------------------------------*/
1745
Olav Kongas5db539e2005-06-23 20:25:36 +03001746static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001747 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
1749 int status = -EINVAL;
1750 unsigned long flags;
1751 struct ehci_iso_stream *stream;
1752
1753 /* Get iso_stream head */
1754 stream = iso_stream_find (ehci, urb);
1755 if (unlikely (stream == NULL)) {
1756 ehci_dbg (ehci, "can't get iso stream\n");
1757 return -ENOMEM;
1758 }
1759 if (unlikely (urb->interval != stream->interval)) {
1760 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1761 stream->interval, urb->interval);
1762 goto done;
1763 }
1764
1765#ifdef EHCI_URB_TRACE
1766 ehci_dbg (ehci,
1767 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001768 __func__, urb->dev->devpath, urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 usb_pipeendpoint (urb->pipe),
1770 usb_pipein (urb->pipe) ? "in" : "out",
1771 urb->transfer_buffer_length,
1772 urb->number_of_packets, urb->interval,
1773 stream);
1774#endif
1775
1776 /* allocate ITDs w/o locking anything */
1777 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1778 if (unlikely (status < 0)) {
1779 ehci_dbg (ehci, "can't init itds\n");
1780 goto done;
1781 }
1782
1783 /* schedule ... need to lock */
1784 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001785 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001786 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04001787 goto done_not_linked;
1788 }
1789 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1790 if (unlikely(status))
1791 goto done_not_linked;
1792 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07001793 if (likely (status == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04001795 else
1796 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001797 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001799 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return status;
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803/*-------------------------------------------------------------------------*/
1804
1805/*
1806 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1807 * TTs in USB 2.0 hubs. These need microframe scheduling.
1808 */
1809
1810static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001811sitd_sched_init(
1812 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct ehci_iso_sched *iso_sched,
1814 struct ehci_iso_stream *stream,
1815 struct urb *urb
1816)
1817{
1818 unsigned i;
1819 dma_addr_t dma = urb->transfer_dma;
1820
1821 /* how many frames are needed for these transfers */
1822 iso_sched->span = urb->number_of_packets * stream->interval;
1823
1824 /* figure out per-frame sitd fields that we'll need later
1825 * when we fit new sitds into the schedule.
1826 */
1827 for (i = 0; i < urb->number_of_packets; i++) {
1828 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1829 unsigned length;
1830 dma_addr_t buf;
1831 u32 trans;
1832
1833 length = urb->iso_frame_desc [i].length & 0x03ff;
1834 buf = dma + urb->iso_frame_desc [i].offset;
1835
1836 trans = SITD_STS_ACTIVE;
1837 if (((i + 1) == urb->number_of_packets)
1838 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1839 trans |= SITD_IOC;
1840 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001841 packet->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 /* might need to cross a buffer page within a td */
1844 packet->bufp = buf;
1845 packet->buf1 = (buf + length) & ~0x0fff;
1846 if (packet->buf1 != (buf & ~(u64)0x0fff))
1847 packet->cross = 1;
1848
David Brownell53bd6a62006-08-30 14:50:06 -07001849 /* OUT uses multiple start-splits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (stream->bEndpointAddress & USB_DIR_IN)
1851 continue;
1852 length = (length + 187) / 188;
1853 if (length > 1) /* BEGIN vs ALL */
1854 length |= 1 << 3;
1855 packet->buf1 |= length;
1856 }
1857}
1858
1859static int
1860sitd_urb_transaction (
1861 struct ehci_iso_stream *stream,
1862 struct ehci_hcd *ehci,
1863 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001864 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865)
1866{
1867 struct ehci_sitd *sitd;
1868 dma_addr_t sitd_dma;
1869 int i;
1870 struct ehci_iso_sched *iso_sched;
1871 unsigned long flags;
1872
1873 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1874 if (iso_sched == NULL)
1875 return -ENOMEM;
1876
Stefan Roese6dbd6822007-05-01 09:29:37 -07001877 sitd_sched_init(ehci, iso_sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 /* allocate/init sITDs */
1880 spin_lock_irqsave (&ehci->lock, flags);
1881 for (i = 0; i < urb->number_of_packets; i++) {
1882
1883 /* NOTE: for now, we don't try to handle wraparound cases
1884 * for IN (using sitd->hw_backpointer, like a FSTN), which
1885 * means we never need two sitds for full speed packets.
1886 */
1887
Alan Stern55934eb2012-07-11 11:22:35 -04001888 /*
1889 * Use siTDs from the free list, but not siTDs that may
1890 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
Alan Stern55934eb2012-07-11 11:22:35 -04001892 if (likely(!list_empty(&stream->free_list))) {
1893 sitd = list_first_entry(&stream->free_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 struct ehci_sitd, sitd_list);
Alan Sternf4289072012-07-11 11:23:07 -04001895 if (sitd->frame == ehci->now_frame)
Alan Stern55934eb2012-07-11 11:22:35 -04001896 goto alloc_sitd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 list_del (&sitd->sitd_list);
1898 sitd_dma = sitd->sitd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001899 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001900 alloc_sitd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 spin_unlock_irqrestore (&ehci->lock, flags);
1902 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1903 &sitd_dma);
1904 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001905 if (!sitd) {
1906 iso_sched_free(stream, iso_sched);
1907 spin_unlock_irqrestore(&ehci->lock, flags);
1908 return -ENOMEM;
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 memset (sitd, 0, sizeof *sitd);
1913 sitd->sitd_dma = sitd_dma;
1914 list_add (&sitd->sitd_list, &iso_sched->td_list);
1915 }
1916
1917 /* temporarily store schedule info in hcpriv */
1918 urb->hcpriv = iso_sched;
1919 urb->error_count = 0;
1920
1921 spin_unlock_irqrestore (&ehci->lock, flags);
1922 return 0;
1923}
1924
1925/*-------------------------------------------------------------------------*/
1926
1927static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001928sitd_patch(
1929 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 struct ehci_iso_stream *stream,
1931 struct ehci_sitd *sitd,
1932 struct ehci_iso_sched *iso_sched,
1933 unsigned index
1934)
1935{
1936 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1937 u64 bufp = uf->bufp;
1938
Stefan Roese6dbd6822007-05-01 09:29:37 -07001939 sitd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 sitd->hw_fullspeed_ep = stream->address;
1941 sitd->hw_uframe = stream->splits;
1942 sitd->hw_results = uf->transaction;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001943 sitd->hw_backpointer = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 bufp = uf->bufp;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001946 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
1947 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Stefan Roese6dbd6822007-05-01 09:29:37 -07001949 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (uf->cross)
1951 bufp += 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001952 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 sitd->index = index;
1954}
1955
1956static inline void
1957sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
1958{
1959 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1960 sitd->sitd_next = ehci->pshadow [frame];
1961 sitd->hw_next = ehci->periodic [frame];
1962 ehci->pshadow [frame].sitd = sitd;
1963 sitd->frame = frame;
1964 wmb ();
Stefan Roese6dbd6822007-05-01 09:29:37 -07001965 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
1968/* fit urb's sitds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001969static void sitd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 struct ehci_hcd *ehci,
1971 struct urb *urb,
1972 unsigned mod,
1973 struct ehci_iso_stream *stream
1974)
1975{
1976 int packet;
1977 unsigned next_uframe;
1978 struct ehci_iso_sched *sched = urb->hcpriv;
1979 struct ehci_sitd *sitd;
1980
1981 next_uframe = stream->next_uframe;
1982
1983 if (list_empty(&stream->td_list)) {
1984 /* usbfs ignores TT bandwidth */
1985 ehci_to_hcd(ehci)->self.bandwidth_allocated
1986 += stream->bandwidth;
1987 ehci_vdbg (ehci,
1988 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
1989 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1990 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
Alan Sternbccbefa2010-07-14 11:03:36 -04001991 (next_uframe >> 3) & (ehci->periodic_size - 1),
Stefan Roese6dbd6822007-05-01 09:29:37 -07001992 stream->interval, hc32_to_cpu(ehci, stream->splits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
Alex He05570292010-12-07 10:10:08 +08001994
1995 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001996 if (ehci->amd_pll_fix == 1)
1997 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08001998 }
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
2001
2002 /* fill sITDs frame by frame */
2003 for (packet = 0, sitd = NULL;
2004 packet < urb->number_of_packets;
2005 packet++) {
2006
2007 /* ASSERT: we have all necessary sitds */
2008 BUG_ON (list_empty (&sched->td_list));
2009
2010 /* ASSERT: no itds for this endpoint in this frame */
2011
2012 sitd = list_entry (sched->td_list.next,
2013 struct ehci_sitd, sitd_list);
2014 list_move_tail (&sitd->sitd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002015 sitd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01002016 sitd->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Stefan Roese6dbd6822007-05-01 09:29:37 -07002018 sitd_patch(ehci, stream, sitd, sched, packet);
Alan Sternbccbefa2010-07-14 11:03:36 -04002019 sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 sitd);
2021
2022 next_uframe += stream->interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
Alan Sternbccbefa2010-07-14 11:03:36 -04002024 stream->next_uframe = next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 /* don't need that schedule data any more */
2027 iso_sched_free (stream, sched);
Alan Stern2656a9a2012-11-08 10:17:01 -05002028 urb->hcpriv = stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Alan Stern569b3942012-07-11 11:23:00 -04002030 ++ehci->isoc_count;
Alan Sternb015cb72012-07-11 11:22:10 -04002031 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034/*-------------------------------------------------------------------------*/
2035
2036#define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
David Brownell53bd6a62006-08-30 14:50:06 -07002037 | SITD_STS_XACT | SITD_STS_MMF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
David Brownell30bf54e2007-12-16 22:37:40 -08002039/* Process and recycle a completed SITD. Return true iff its urb completed,
2040 * and hence its completion callback probably added things to the hardware
2041 * schedule.
2042 *
2043 * Note that we carefully avoid recycling this descriptor until after any
2044 * completion callback runs, so that it won't be reused quickly. That is,
2045 * assuming (a) no more than two urbs per frame on this endpoint, and also
2046 * (b) only this endpoint's completions submit URBs. It seems some silicon
2047 * corrupts things if you reuse completed descriptors very quickly...
2048 */
Alan Sternf4289072012-07-11 11:23:07 -04002049static bool sitd_complete(struct ehci_hcd *ehci, struct ehci_sitd *sitd)
2050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 struct urb *urb = sitd->urb;
2052 struct usb_iso_packet_descriptor *desc;
2053 u32 t;
2054 int urb_index = -1;
2055 struct ehci_iso_stream *stream = sitd->stream;
2056 struct usb_device *dev;
Alan Sternf4289072012-07-11 11:23:07 -04002057 bool retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 urb_index = sitd->index;
2060 desc = &urb->iso_frame_desc [urb_index];
Stefan Roese6dbd6822007-05-01 09:29:37 -07002061 t = hc32_to_cpup(ehci, &sitd->hw_results);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 /* report transfer status */
Alan Stern4005ad42012-10-01 10:32:01 -04002064 if (unlikely(t & SITD_ERRS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 urb->error_count++;
2066 if (t & SITD_STS_DBE)
2067 desc->status = usb_pipein (urb->pipe)
2068 ? -ENOSR /* hc couldn't read */
2069 : -ECOMM; /* hc couldn't write */
2070 else if (t & SITD_STS_BABBLE)
2071 desc->status = -EOVERFLOW;
2072 else /* XACT, MMF, etc */
2073 desc->status = -EPROTO;
Alan Stern4005ad42012-10-01 10:32:01 -04002074 } else if (unlikely(t & SITD_STS_ACTIVE)) {
2075 /* URB was too late */
2076 urb->error_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 } else {
2078 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04002079 desc->actual_length = desc->length - SITD_LENGTH(t);
2080 urb->actual_length += desc->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 /* handle completion now? */
2084 if ((urb_index + 1) != urb->number_of_packets)
David Brownell30bf54e2007-12-16 22:37:40 -08002085 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 /* ASSERT: it's really the last sitd for this urb
2088 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2089 BUG_ON (sitd->urb == urb);
2090 */
2091
David Brownellaa16ca32007-12-30 23:45:19 -08002092 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05002093 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04002094 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08002095 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Alan Stern569b3942012-07-11 11:23:00 -04002098 --ehci->isoc_count;
2099 disable_periodic(ehci);
2100
2101 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
Alex He05570292010-12-07 10:10:08 +08002102 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002103 if (ehci->amd_pll_fix == 1)
2104 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08002105 }
2106
Karsten Wiese508db8c2009-02-26 01:47:48 +01002107 if (list_is_singular(&stream->td_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 ehci_to_hcd(ehci)->self.bandwidth_allocated
2109 -= stream->bandwidth;
2110 ehci_vdbg (ehci,
2111 "deschedule devp %s ep%d%s-iso\n",
2112 dev->devpath, stream->bEndpointAddress & 0x0f,
2113 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
2114 }
Alan Stern0e5f2312010-04-08 16:56:37 -04002115
David Brownell30bf54e2007-12-16 22:37:40 -08002116done:
David Brownell30bf54e2007-12-16 22:37:40 -08002117 sitd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04002118
2119 /* Add to the end of the free list for later reuse */
2120 list_move_tail(&sitd->sitd_list, &stream->free_list);
2121
2122 /* Recycle the siTDs when the pipeline is empty (ep no longer in use) */
2123 if (list_empty(&stream->td_list)) {
2124 list_splice_tail_init(&stream->free_list,
2125 &ehci->cached_sitd_list);
2126 start_free_itds(ehci);
Alan Stern0e5f2312010-04-08 16:56:37 -04002127 }
Alan Stern55934eb2012-07-11 11:22:35 -04002128
David Brownell30bf54e2007-12-16 22:37:40 -08002129 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
2132
Olav Kongas5db539e2005-06-23 20:25:36 +03002133static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04002134 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 int status = -EINVAL;
2137 unsigned long flags;
2138 struct ehci_iso_stream *stream;
2139
2140 /* Get iso_stream head */
2141 stream = iso_stream_find (ehci, urb);
2142 if (stream == NULL) {
2143 ehci_dbg (ehci, "can't get iso stream\n");
2144 return -ENOMEM;
2145 }
2146 if (urb->interval != stream->interval) {
2147 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
2148 stream->interval, urb->interval);
2149 goto done;
2150 }
2151
2152#ifdef EHCI_URB_TRACE
2153 ehci_dbg (ehci,
2154 "submit %p dev%s ep%d%s-iso len %d\n",
2155 urb, urb->dev->devpath,
2156 usb_pipeendpoint (urb->pipe),
2157 usb_pipein (urb->pipe) ? "in" : "out",
2158 urb->transfer_buffer_length);
2159#endif
2160
2161 /* allocate SITDs */
2162 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2163 if (status < 0) {
2164 ehci_dbg (ehci, "can't init sitds\n");
2165 goto done;
2166 }
2167
2168 /* schedule ... need to lock */
2169 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04002170 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002171 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04002172 goto done_not_linked;
2173 }
2174 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2175 if (unlikely(status))
2176 goto done_not_linked;
2177 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07002178 if (status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04002180 else
2181 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002182 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002184 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return status;
2186}
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188/*-------------------------------------------------------------------------*/
2189
Alan Stern569b3942012-07-11 11:23:00 -04002190static void scan_isoc(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Alan Sternf4289072012-07-11 11:23:07 -04002192 unsigned uf, now_frame, frame;
2193 unsigned fmask = ehci->periodic_size - 1;
2194 bool modified, live;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 /*
2197 * When running, scan from last scan point up to "now"
2198 * else clean up by scanning everything that's left.
2199 * Touches as few pages as possible: cache-friendly.
2200 */
Alan Sternc0c53db2012-07-11 11:21:48 -04002201 if (ehci->rh_state >= EHCI_RH_RUNNING) {
Alan Sternf4289072012-07-11 11:23:07 -04002202 uf = ehci_read_frame_index(ehci);
2203 now_frame = (uf >> 3) & fmask;
2204 live = true;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002205 } else {
Alan Sternc3ee9b72012-09-28 16:01:23 -04002206 now_frame = (ehci->last_iso_frame - 1) & fmask;
Alan Sternf4289072012-07-11 11:23:07 -04002207 live = false;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002208 }
Alan Sternf4289072012-07-11 11:23:07 -04002209 ehci->now_frame = now_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Alan Sternb09a61c2013-01-30 16:35:02 -05002211 frame = ehci->last_iso_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 for (;;) {
2213 union ehci_shadow q, *q_p;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002214 __hc32 type, *hw_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216restart:
2217 /* scan each element in frame's queue for completions */
2218 q_p = &ehci->pshadow [frame];
2219 hw_p = &ehci->periodic [frame];
2220 q.ptr = q_p->ptr;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002221 type = Q_NEXT_TYPE(ehci, *hw_p);
Alan Sternf4289072012-07-11 11:23:07 -04002222 modified = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 while (q.ptr != NULL) {
Stefan Roese6dbd6822007-05-01 09:29:37 -07002225 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 case Q_TYPE_ITD:
David Brownell79592b722008-01-07 00:47:42 -08002227 /* If this ITD is still active, leave it for
2228 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002229 * No need to check for activity unless the
2230 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002231 */
Alan Sternf4289072012-07-11 11:23:07 -04002232 if (frame == now_frame && live) {
Alan Sternb40e43f2008-05-20 16:59:10 -04002233 rmb();
2234 for (uf = 0; uf < 8; uf++) {
2235 if (q.itd->hw_transaction[uf] &
2236 ITD_ACTIVE(ehci))
2237 break;
2238 }
2239 if (uf < 8) {
Alan Sternb40e43f2008-05-20 16:59:10 -04002240 q_p = &q.itd->itd_next;
2241 hw_p = &q.itd->hw_next;
2242 type = Q_NEXT_TYPE(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -07002243 q.itd->hw_next);
Alan Sternb40e43f2008-05-20 16:59:10 -04002244 q = *q_p;
2245 break;
2246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
David Brownell79592b722008-01-07 00:47:42 -08002249 /* Take finished ITDs out of the schedule
2250 * and process them: recycle, maybe report
2251 * URB completion. HC won't cache the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 * pointer for much longer, if at all.
2253 */
2254 *q_p = q.itd->itd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002255 if (!ehci->use_dummy_qh ||
2256 q.itd->hw_next != EHCI_LIST_END(ehci))
2257 *hw_p = q.itd->hw_next;
2258 else
2259 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002260 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002262 modified = itd_complete (ehci, q.itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 q = *q_p;
2264 break;
2265 case Q_TYPE_SITD:
David Brownell79592b722008-01-07 00:47:42 -08002266 /* If this SITD is still active, leave it for
2267 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002268 * No need to check for activity unless the
2269 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002270 */
Alan Sternf4289072012-07-11 11:23:07 -04002271 if (((frame == now_frame) ||
2272 (((frame + 1) & fmask) == now_frame))
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002273 && live
2274 && (q.sitd->hw_results &
2275 SITD_ACTIVE(ehci))) {
2276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 q_p = &q.sitd->sitd_next;
2278 hw_p = &q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002279 type = Q_NEXT_TYPE(ehci,
2280 q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 q = *q_p;
2282 break;
2283 }
David Brownell79592b722008-01-07 00:47:42 -08002284
2285 /* Take finished SITDs out of the schedule
2286 * and process them: recycle, maybe report
2287 * URB completion.
2288 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 *q_p = q.sitd->sitd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002290 if (!ehci->use_dummy_qh ||
2291 q.sitd->hw_next != EHCI_LIST_END(ehci))
2292 *hw_p = q.sitd->hw_next;
2293 else
2294 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002295 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002297 modified = sitd_complete (ehci, q.sitd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 q = *q_p;
2299 break;
2300 default:
Greg Kroah-Hartman2d0fe1b2012-05-01 21:33:36 -07002301 ehci_dbg(ehci, "corrupt type %d frame %d shadow %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 type, frame, q.ptr);
2303 // BUG ();
Alan Stern569b3942012-07-11 11:23:00 -04002304 /* FALL THROUGH */
2305 case Q_TYPE_QH:
2306 case Q_TYPE_FSTN:
2307 /* End of the iTDs and siTDs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 q.ptr = NULL;
Alan Stern569b3942012-07-11 11:23:00 -04002309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
2311
2312 /* assume completion callbacks modify the queue */
Alan Sternf4289072012-07-11 11:23:07 -04002313 if (unlikely(modified && ehci->isoc_count > 0))
2314 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316
Alan Sternf4289072012-07-11 11:23:07 -04002317 /* Stop when we have reached the current frame */
2318 if (frame == now_frame)
David Brownell79592b722008-01-07 00:47:42 -08002319 break;
Alan Sternb09a61c2013-01-30 16:35:02 -05002320
2321 /* The last frame may still have active siTDs */
2322 ehci->last_iso_frame = frame;
2323 frame = (frame + 1) & fmask;
David Brownell53bd6a62006-08-30 14:50:06 -07002324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}