blob: 11b2f21d7ac169ceedca9f2bdb80e493f3cfe416 [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
Alan Stern68aa95d2011-10-12 10:39:14 -040039#ifdef CONFIG_PCI
40
41static unsigned ehci_read_frame_index(struct ehci_hcd *ehci)
42{
43 unsigned uf;
44
45 /*
46 * The MosChip MCS9990 controller updates its microframe counter
47 * a little before the frame counter, and occasionally we will read
48 * the invalid intermediate value. Avoid problems by checking the
49 * microframe number (the low-order 3 bits); if they are 0 then
50 * re-read the register to get the correct value.
51 */
52 uf = ehci_readl(ehci, &ehci->regs->frame_index);
53 if (unlikely(ehci->frame_index_bug && ((uf & 7) == 0)))
54 uf = ehci_readl(ehci, &ehci->regs->frame_index);
55 return uf;
56}
57
58#endif
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*-------------------------------------------------------------------------*/
61
62/*
63 * periodic_next_shadow - return "next" pointer on shadow list
64 * @periodic: host pointer to qh/itd/sitd
65 * @tag: hardware tag for type of this record
66 */
67static union ehci_shadow *
Stefan Roese6dbd6822007-05-01 09:29:37 -070068periodic_next_shadow(struct ehci_hcd *ehci, union ehci_shadow *periodic,
69 __hc32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Stefan Roese6dbd6822007-05-01 09:29:37 -070071 switch (hc32_to_cpu(ehci, tag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 case Q_TYPE_QH:
73 return &periodic->qh->qh_next;
74 case Q_TYPE_FSTN:
75 return &periodic->fstn->fstn_next;
76 case Q_TYPE_ITD:
77 return &periodic->itd->itd_next;
78 // case Q_TYPE_SITD:
79 default:
80 return &periodic->sitd->sitd_next;
81 }
82}
83
Alek Du3807e262009-07-14 07:23:29 +080084static __hc32 *
85shadow_next_periodic(struct ehci_hcd *ehci, union ehci_shadow *periodic,
86 __hc32 tag)
87{
88 switch (hc32_to_cpu(ehci, tag)) {
89 /* our ehci_shadow.qh is actually software part */
90 case Q_TYPE_QH:
91 return &periodic->qh->hw->hw_next;
92 /* others are hw parts */
93 default:
94 return periodic->hw_next;
95 }
96}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* caller must hold ehci->lock */
99static void periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
100{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700101 union ehci_shadow *prev_p = &ehci->pshadow[frame];
102 __hc32 *hw_p = &ehci->periodic[frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 union ehci_shadow here = *prev_p;
104
105 /* find predecessor of "ptr"; hw and shadow lists are in sync */
106 while (here.ptr && here.ptr != ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700107 prev_p = periodic_next_shadow(ehci, prev_p,
108 Q_NEXT_TYPE(ehci, *hw_p));
Alek Du3807e262009-07-14 07:23:29 +0800109 hw_p = shadow_next_periodic(ehci, &here,
110 Q_NEXT_TYPE(ehci, *hw_p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 here = *prev_p;
112 }
113 /* an interrupt entry (at list end) could have been shared */
114 if (!here.ptr)
115 return;
116
117 /* update shadow and hardware lists ... the old "next" pointers
118 * from ptr may still be in use, the caller updates them.
119 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700120 *prev_p = *periodic_next_shadow(ehci, &here,
121 Q_NEXT_TYPE(ehci, *hw_p));
Andiry Xu3d091a62010-11-08 17:58:35 +0800122
123 if (!ehci->use_dummy_qh ||
124 *shadow_next_periodic(ehci, &here, Q_NEXT_TYPE(ehci, *hw_p))
125 != EHCI_LIST_END(ehci))
126 *hw_p = *shadow_next_periodic(ehci, &here,
127 Q_NEXT_TYPE(ehci, *hw_p));
128 else
129 *hw_p = ehci->dummy->qh_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/* how many of the uframe's 125 usecs are allocated? */
133static unsigned short
134periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
135{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700136 __hc32 *hw_p = &ehci->periodic [frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 union ehci_shadow *q = &ehci->pshadow [frame];
138 unsigned usecs = 0;
Alek Du3807e262009-07-14 07:23:29 +0800139 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 while (q->ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700142 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800144 hw = q->qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* is it in the S-mask? */
Alek Du3807e262009-07-14 07:23:29 +0800146 if (hw->hw_info2 & cpu_to_hc32(ehci, 1 << uframe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 usecs += q->qh->usecs;
148 /* ... or C-mask? */
Alek Du3807e262009-07-14 07:23:29 +0800149 if (hw->hw_info2 & cpu_to_hc32(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700150 1 << (8 + uframe)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 usecs += q->qh->c_usecs;
Alek Du3807e262009-07-14 07:23:29 +0800152 hw_p = &hw->hw_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 q = &q->qh->qh_next;
154 break;
155 // case Q_TYPE_FSTN:
156 default:
157 /* for "save place" FSTNs, count the relevant INTR
158 * bandwidth from the previous frame
159 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700160 if (q->fstn->hw_prev != EHCI_LIST_END(ehci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 ehci_dbg (ehci, "ignoring FSTN cost ...\n");
162 }
163 hw_p = &q->fstn->hw_next;
164 q = &q->fstn->fstn_next;
165 break;
166 case Q_TYPE_ITD:
Karsten Wiese3b6fcfd2007-12-30 21:55:05 -0800167 if (q->itd->hw_transaction[uframe])
168 usecs += q->itd->stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 hw_p = &q->itd->hw_next;
170 q = &q->itd->itd_next;
171 break;
172 case Q_TYPE_SITD:
173 /* is it in the S-mask? (count SPLIT, DATA) */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700174 if (q->sitd->hw_uframe & cpu_to_hc32(ehci,
175 1 << uframe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (q->sitd->hw_fullspeed_ep &
Stefan Roese6dbd6822007-05-01 09:29:37 -0700177 cpu_to_hc32(ehci, 1<<31))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 usecs += q->sitd->stream->usecs;
179 else /* worst case for OUT start-split */
180 usecs += HS_USECS_ISO (188);
181 }
182
183 /* ... C-mask? (count CSPLIT, DATA) */
184 if (q->sitd->hw_uframe &
Stefan Roese6dbd6822007-05-01 09:29:37 -0700185 cpu_to_hc32(ehci, 1 << (8 + uframe))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* worst case for IN complete-split */
187 usecs += q->sitd->stream->c_usecs;
188 }
189
190 hw_p = &q->sitd->hw_next;
191 q = &q->sitd->sitd_next;
192 break;
193 }
194 }
195#ifdef DEBUG
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +0400196 if (usecs > ehci->uframe_periodic_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 ehci_err (ehci, "uframe %d sched overrun: %d usecs\n",
198 frame * 8 + uframe, usecs);
199#endif
200 return usecs;
201}
202
203/*-------------------------------------------------------------------------*/
204
205static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
206{
207 if (!dev1->tt || !dev2->tt)
208 return 0;
209 if (dev1->tt != dev2->tt)
210 return 0;
211 if (dev1->tt->multi)
212 return dev1->ttport == dev2->ttport;
213 else
214 return 1;
215}
216
Dan Streetmanba47f662006-05-24 09:39:16 -0700217#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
218
219/* Which uframe does the low/fullspeed transfer start in?
220 *
221 * The parameter is the mask of ssplits in "H-frame" terms
222 * and this returns the transfer start uframe in "B-frame" terms,
223 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
224 * will cause a transfer in "B-frame" uframe 0. "B-frames" lag
225 * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7.
226 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700227static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask)
Dan Streetmanba47f662006-05-24 09:39:16 -0700228{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700229 unsigned char smask = QH_SMASK & hc32_to_cpu(ehci, mask);
Dan Streetmanba47f662006-05-24 09:39:16 -0700230 if (!smask) {
231 ehci_err(ehci, "invalid empty smask!\n");
232 /* uframe 7 can't have bw so this will indicate failure */
233 return 7;
234 }
235 return ffs(smask) - 1;
236}
237
238static const unsigned char
239max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
240
241/* carryover low/fullspeed bandwidth that crosses uframe boundries */
242static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
243{
244 int i;
245 for (i=0; i<7; i++) {
246 if (max_tt_usecs[i] < tt_usecs[i]) {
247 tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i];
248 tt_usecs[i] = max_tt_usecs[i];
249 }
250 }
251}
252
253/* How many of the tt's periodic downstream 1000 usecs are allocated?
254 *
255 * While this measures the bandwidth in terms of usecs/uframe,
256 * the low/fullspeed bus has no notion of uframes, so any particular
257 * low/fullspeed transfer can "carry over" from one uframe to the next,
258 * since the TT just performs downstream transfers in sequence.
259 *
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800260 * For example two separate 100 usec transfers can start in the same uframe,
Dan Streetmanba47f662006-05-24 09:39:16 -0700261 * and the second one would "carry over" 75 usecs into the next uframe.
262 */
263static void
264periodic_tt_usecs (
265 struct ehci_hcd *ehci,
266 struct usb_device *dev,
267 unsigned frame,
268 unsigned short tt_usecs[8]
269)
270{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700271 __hc32 *hw_p = &ehci->periodic [frame];
Dan Streetmanba47f662006-05-24 09:39:16 -0700272 union ehci_shadow *q = &ehci->pshadow [frame];
273 unsigned char uf;
274
275 memset(tt_usecs, 0, 16);
276
277 while (q->ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700278 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
Dan Streetmanba47f662006-05-24 09:39:16 -0700279 case Q_TYPE_ITD:
280 hw_p = &q->itd->hw_next;
281 q = &q->itd->itd_next;
282 continue;
283 case Q_TYPE_QH:
284 if (same_tt(dev, q->qh->dev)) {
Alek Du3807e262009-07-14 07:23:29 +0800285 uf = tt_start_uframe(ehci, q->qh->hw->hw_info2);
Dan Streetmanba47f662006-05-24 09:39:16 -0700286 tt_usecs[uf] += q->qh->tt_usecs;
287 }
Alek Du3807e262009-07-14 07:23:29 +0800288 hw_p = &q->qh->hw->hw_next;
Dan Streetmanba47f662006-05-24 09:39:16 -0700289 q = &q->qh->qh_next;
290 continue;
291 case Q_TYPE_SITD:
292 if (same_tt(dev, q->sitd->urb->dev)) {
293 uf = tt_start_uframe(ehci, q->sitd->hw_uframe);
294 tt_usecs[uf] += q->sitd->stream->tt_usecs;
295 }
296 hw_p = &q->sitd->hw_next;
297 q = &q->sitd->sitd_next;
298 continue;
299 // case Q_TYPE_FSTN:
300 default:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700301 ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n",
302 frame);
Dan Streetmanba47f662006-05-24 09:39:16 -0700303 hw_p = &q->fstn->hw_next;
304 q = &q->fstn->fstn_next;
305 }
306 }
307
308 carryover_tt_bandwidth(tt_usecs);
309
310 if (max_tt_usecs[7] < tt_usecs[7])
311 ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
312 frame, tt_usecs[7] - max_tt_usecs[7]);
313}
314
315/*
316 * Return true if the device's tt's downstream bus is available for a
317 * periodic transfer of the specified length (usecs), starting at the
318 * specified frame/uframe. Note that (as summarized in section 11.19
319 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
320 * uframe.
321 *
322 * The uframe parameter is when the fullspeed/lowspeed transfer
323 * should be executed in "B-frame" terms, which is the same as the
324 * highspeed ssplit's uframe (which is in "H-frame" terms). For example
325 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
326 * See the EHCI spec sec 4.5 and fig 4.7.
327 *
328 * This checks if the full/lowspeed bus, at the specified starting uframe,
329 * has the specified bandwidth available, according to rules listed
330 * in USB 2.0 spec section 11.18.1 fig 11-60.
331 *
332 * This does not check if the transfer would exceed the max ssplit
333 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
334 * since proper scheduling limits ssplits to less than 16 per uframe.
335 */
336static int tt_available (
337 struct ehci_hcd *ehci,
338 unsigned period,
339 struct usb_device *dev,
340 unsigned frame,
341 unsigned uframe,
342 u16 usecs
343)
344{
345 if ((period == 0) || (uframe >= 7)) /* error */
346 return 0;
347
348 for (; frame < ehci->periodic_size; frame += period) {
349 unsigned short tt_usecs[8];
350
351 periodic_tt_usecs (ehci, dev, frame, tt_usecs);
352
353 ehci_vdbg(ehci, "tt frame %d check %d usecs start uframe %d in"
354 " schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
355 frame, usecs, uframe,
356 tt_usecs[0], tt_usecs[1], tt_usecs[2], tt_usecs[3],
357 tt_usecs[4], tt_usecs[5], tt_usecs[6], tt_usecs[7]);
358
359 if (max_tt_usecs[uframe] <= tt_usecs[uframe]) {
360 ehci_vdbg(ehci, "frame %d uframe %d fully scheduled\n",
361 frame, uframe);
362 return 0;
363 }
364
365 /* special case for isoc transfers larger than 125us:
366 * the first and each subsequent fully used uframe
367 * must be empty, so as to not illegally delay
368 * already scheduled transactions
369 */
370 if (125 < usecs) {
Dan Streetmanc065c602009-04-21 13:37:12 -0400371 int ufs = (usecs / 125);
Dan Streetmanba47f662006-05-24 09:39:16 -0700372 int i;
373 for (i = uframe; i < (uframe + ufs) && i < 8; i++)
374 if (0 < tt_usecs[i]) {
375 ehci_vdbg(ehci,
376 "multi-uframe xfer can't fit "
377 "in frame %d uframe %d\n",
378 frame, i);
379 return 0;
380 }
381 }
382
383 tt_usecs[uframe] += usecs;
384
385 carryover_tt_bandwidth(tt_usecs);
386
387 /* fail if the carryover pushed bw past the last uframe's limit */
388 if (max_tt_usecs[7] < tt_usecs[7]) {
389 ehci_vdbg(ehci,
390 "tt unavailable usecs %d frame %d uframe %d\n",
391 usecs, frame, uframe);
392 return 0;
393 }
394 }
395
396 return 1;
397}
398
399#else
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/* return true iff the device's transaction translator is available
402 * for a periodic transfer starting at the specified frame, using
403 * all the uframes in the mask.
404 */
405static int tt_no_collision (
406 struct ehci_hcd *ehci,
407 unsigned period,
408 struct usb_device *dev,
409 unsigned frame,
410 u32 uf_mask
411)
412{
413 if (period == 0) /* error */
414 return 0;
415
416 /* note bandwidth wastage: split never follows csplit
417 * (different dev or endpoint) until the next uframe.
418 * calling convention doesn't make that distinction.
419 */
420 for (; frame < ehci->periodic_size; frame += period) {
421 union ehci_shadow here;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700422 __hc32 type;
Alek Du3807e262009-07-14 07:23:29 +0800423 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 here = ehci->pshadow [frame];
Stefan Roese6dbd6822007-05-01 09:29:37 -0700426 type = Q_NEXT_TYPE(ehci, ehci->periodic [frame]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700428 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 case Q_TYPE_ITD:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700430 type = Q_NEXT_TYPE(ehci, here.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 here = here.itd->itd_next;
432 continue;
433 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800434 hw = here.qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (same_tt (dev, here.qh->dev)) {
436 u32 mask;
437
Stefan Roese6dbd6822007-05-01 09:29:37 -0700438 mask = hc32_to_cpu(ehci,
Alek Du3807e262009-07-14 07:23:29 +0800439 hw->hw_info2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* "knows" no gap is needed */
441 mask |= mask >> 8;
442 if (mask & uf_mask)
443 break;
444 }
Alek Du3807e262009-07-14 07:23:29 +0800445 type = Q_NEXT_TYPE(ehci, hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 here = here.qh->qh_next;
447 continue;
448 case Q_TYPE_SITD:
449 if (same_tt (dev, here.sitd->urb->dev)) {
450 u16 mask;
451
Stefan Roese6dbd6822007-05-01 09:29:37 -0700452 mask = hc32_to_cpu(ehci, here.sitd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 ->hw_uframe);
454 /* FIXME assumes no gap for IN! */
455 mask |= mask >> 8;
456 if (mask & uf_mask)
457 break;
458 }
Stefan Roese6dbd6822007-05-01 09:29:37 -0700459 type = Q_NEXT_TYPE(ehci, here.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 here = here.sitd->sitd_next;
461 continue;
462 // case Q_TYPE_FSTN:
463 default:
464 ehci_dbg (ehci,
465 "periodic frame %d bogus type %d\n",
466 frame, type);
467 }
468
469 /* collision or error */
470 return 0;
471 }
472 }
473
474 /* no collision */
475 return 1;
476}
477
Dan Streetmanba47f662006-05-24 09:39:16 -0700478#endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*-------------------------------------------------------------------------*/
481
Alan Sternb015cb72012-07-11 11:22:10 -0400482static void enable_periodic(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400484 if (ehci->periodic_count++)
Alan Sternb015cb72012-07-11 11:22:10 -0400485 return;
David Brownell01c17142008-08-26 23:35:04 -0700486
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400487 /* Stop waiting to turn off the periodic schedule */
488 ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_DISABLE_PERIODIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400490 /* Don't start the schedule until PSS is 0 */
491 ehci_poll_PSS(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Alan Sternb015cb72012-07-11 11:22:10 -0400494static void disable_periodic(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400496 if (--ehci->periodic_count)
Alan Sternb015cb72012-07-11 11:22:10 -0400497 return;
David Brownell01c17142008-08-26 23:35:04 -0700498
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400499 ehci->next_uframe = -1; /* the periodic schedule is empty */
Oliver Neukumee4ecb82009-11-27 15:17:59 +0100500
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400501 /* Don't turn off the schedule until PSS is 1 */
502 ehci_poll_PSS(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/*-------------------------------------------------------------------------*/
506
507/* periodic schedule slots have iso tds (normal or split) first, then a
508 * sparse tree for active interrupt transfers.
509 *
510 * this just links in a qh; caller guarantees uframe masks are set right.
511 * no FSTN support (yet; ehci 0.96+)
512 */
Alan Sternb015cb72012-07-11 11:22:10 -0400513static void qh_link_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 unsigned i;
516 unsigned period = qh->period;
517
518 dev_dbg (&qh->dev->dev,
519 "link qh%d-%04x/%p start %d [%d/%d us]\n",
Alek Du3807e262009-07-14 07:23:29 +0800520 period, hc32_to_cpup(ehci, &qh->hw->hw_info2)
521 & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 qh, qh->start, qh->usecs, qh->c_usecs);
523
524 /* high bandwidth, or otherwise every microframe */
525 if (period == 0)
526 period = 1;
527
528 for (i = qh->start; i < ehci->periodic_size; i += period) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700529 union ehci_shadow *prev = &ehci->pshadow[i];
530 __hc32 *hw_p = &ehci->periodic[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 union ehci_shadow here = *prev;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700532 __hc32 type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* skip the iso nodes at list head */
535 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700536 type = Q_NEXT_TYPE(ehci, *hw_p);
537 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700539 prev = periodic_next_shadow(ehci, prev, type);
Alek Du3807e262009-07-14 07:23:29 +0800540 hw_p = shadow_next_periodic(ehci, &here, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 here = *prev;
542 }
543
544 /* sorting each branch by period (slow-->fast)
545 * enables sharing interior tree nodes
546 */
547 while (here.ptr && qh != here.qh) {
548 if (qh->period > here.qh->period)
549 break;
550 prev = &here.qh->qh_next;
Alek Du3807e262009-07-14 07:23:29 +0800551 hw_p = &here.qh->hw->hw_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 here = *prev;
553 }
554 /* link in this qh, unless some earlier pass did that */
555 if (qh != here.qh) {
556 qh->qh_next = here;
557 if (here.qh)
Alek Du3807e262009-07-14 07:23:29 +0800558 qh->hw->hw_next = *hw_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 wmb ();
560 prev->qh = qh;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700561 *hw_p = QH_NEXT (ehci, qh->qh_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 }
564 qh->qh_state = QH_STATE_LINKED;
Alan Sternef4638f2009-07-31 10:41:40 -0400565 qh->xacterrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* update per-qh bandwidth for usbfs */
568 ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period
569 ? ((qh->usecs + qh->c_usecs) / qh->period)
570 : (qh->usecs * 8);
571
572 /* maybe enable periodic schedule processing */
Alan Sternb015cb72012-07-11 11:22:10 -0400573 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Alan Sternb015cb72012-07-11 11:22:10 -0400576static void qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 unsigned i;
579 unsigned period;
580
Alan Sterndf202252012-07-11 11:22:26 -0400581 /*
582 * If qh is for a low/full-speed device, simply unlinking it
583 * could interfere with an ongoing split transaction. To unlink
584 * it safely would require setting the QH_INACTIVATE bit and
585 * waiting at least one frame, as described in EHCI 4.12.2.5.
586 *
587 * We won't bother with any of this. Instead, we assume that the
588 * only reason for unlinking an interrupt QH while the current URB
589 * is still active is to dequeue all the URBs (flush the whole
590 * endpoint queue).
591 *
592 * If rebalancing the periodic schedule is ever implemented, this
593 * approach will no longer be valid.
594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /* high bandwidth, or otherwise part of every microframe */
597 if ((period = qh->period) == 0)
598 period = 1;
599
600 for (i = qh->start; i < ehci->periodic_size; i += period)
601 periodic_unlink (ehci, i, qh);
602
603 /* update per-qh bandwidth for usbfs */
604 ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period
605 ? ((qh->usecs + qh->c_usecs) / qh->period)
606 : (qh->usecs * 8);
607
608 dev_dbg (&qh->dev->dev,
609 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
David Brownell7dedacf2005-08-04 18:06:41 -0700610 qh->period,
Alek Du3807e262009-07-14 07:23:29 +0800611 hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 qh, qh->start, qh->usecs, qh->c_usecs);
613
614 /* qh->qh_next still "live" to HC */
615 qh->qh_state = QH_STATE_UNLINK;
616 qh->qh_next.ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Alan Sterndf202252012-07-11 11:22:26 -0400619static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Alan Sterna448c9d2009-08-19 12:22:44 -0400621 /* If the QH isn't linked then there's nothing we can do
622 * unless we were called during a giveback, in which case
623 * qh_completions() has to deal with it.
624 */
625 if (qh->qh_state != QH_STATE_LINKED) {
626 if (qh->qh_state == QH_STATE_COMPLETING)
627 qh->needs_rescan = 1;
628 return;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 qh_unlink_periodic (ehci, qh);
632
Alan Sterndf202252012-07-11 11:22:26 -0400633 /* Make sure the unlinks are visible before starting the timer */
634 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Alan Sterndf202252012-07-11 11:22:26 -0400636 /*
637 * The EHCI spec doesn't say how long it takes the controller to
638 * stop accessing an unlinked interrupt QH. The timer delay is
639 * 9 uframes; presumably that will be long enough.
640 */
641 qh->unlink_cycle = ehci->intr_unlink_cycle;
642
643 /* New entries go at the end of the intr_unlink list */
644 if (ehci->intr_unlink)
645 ehci->intr_unlink_last->unlink_next = qh;
646 else
647 ehci->intr_unlink = qh;
648 ehci->intr_unlink_last = qh;
649
650 if (ehci->intr_unlinking)
651 ; /* Avoid recursive calls */
652 else if (ehci->rh_state < EHCI_RH_RUNNING)
653 ehci_handle_intr_unlinks(ehci);
654 else if (ehci->intr_unlink == qh) {
655 ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
656 ++ehci->intr_unlink_cycle;
657 }
658}
659
660static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
661{
662 struct ehci_qh_hw *hw = qh->hw;
663 int rc;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 qh->qh_state = QH_STATE_IDLE;
Alek Du3807e262009-07-14 07:23:29 +0800666 hw->hw_next = EHCI_LIST_END(ehci);
Alan Sterna448c9d2009-08-19 12:22:44 -0400667
668 qh_completions(ehci, qh);
669
670 /* reschedule QH iff another request is queued */
Alan Sterndf202252012-07-11 11:22:26 -0400671 if (!list_empty(&qh->qtd_list) && ehci->rh_state == EHCI_RH_RUNNING) {
Alan Sterna448c9d2009-08-19 12:22:44 -0400672 rc = qh_schedule(ehci, qh);
673
674 /* An error here likely indicates handshake failure
675 * or no space left in the schedule. Neither fault
676 * should happen often ...
677 *
678 * FIXME kill the now-dysfunctional queued urbs
679 */
680 if (rc != 0)
681 ehci_err(ehci, "can't reschedule qh %p, err %d\n",
682 qh, rc);
683 }
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400684
685 /* maybe turn off periodic schedule */
686 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689/*-------------------------------------------------------------------------*/
690
691static int check_period (
David Brownell53bd6a62006-08-30 14:50:06 -0700692 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 unsigned frame,
694 unsigned uframe,
695 unsigned period,
696 unsigned usecs
697) {
698 int claimed;
699
700 /* complete split running into next frame?
701 * given FSTN support, we could sometimes check...
702 */
703 if (uframe >= 8)
704 return 0;
705
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +0400706 /* convert "usecs we need" to "max already claimed" */
707 usecs = ehci->uframe_periodic_max - usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* we "know" 2 and 4 uframe intervals were rejected; so
710 * for period 0, check _every_ microframe in the schedule.
711 */
712 if (unlikely (period == 0)) {
713 do {
714 for (uframe = 0; uframe < 7; uframe++) {
715 claimed = periodic_usecs (ehci, frame, uframe);
716 if (claimed > usecs)
717 return 0;
718 }
719 } while ((frame += 1) < ehci->periodic_size);
720
721 /* just check the specified uframe, at that period */
722 } else {
723 do {
724 claimed = periodic_usecs (ehci, frame, uframe);
725 if (claimed > usecs)
726 return 0;
727 } while ((frame += period) < ehci->periodic_size);
728 }
729
730 // success!
731 return 1;
732}
733
734static int check_intr_schedule (
David Brownell53bd6a62006-08-30 14:50:06 -0700735 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 unsigned frame,
737 unsigned uframe,
738 const struct ehci_qh *qh,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700739 __hc32 *c_maskp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740)
741{
David Brownell53bd6a62006-08-30 14:50:06 -0700742 int retval = -ENOSPC;
Dan Streetmanba47f662006-05-24 09:39:16 -0700743 u8 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
746 goto done;
747
748 if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
749 goto done;
750 if (!qh->c_usecs) {
751 retval = 0;
752 *c_maskp = 0;
753 goto done;
754 }
755
Dan Streetmanba47f662006-05-24 09:39:16 -0700756#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
757 if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
758 qh->tt_usecs)) {
759 unsigned i;
760
761 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
762 for (i=uframe+1; i<8 && i<uframe+4; i++)
763 if (!check_period (ehci, frame, i,
764 qh->period, qh->c_usecs))
765 goto done;
766 else
767 mask |= 1 << i;
768
769 retval = 0;
770
Stefan Roese6dbd6822007-05-01 09:29:37 -0700771 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Dan Streetmanba47f662006-05-24 09:39:16 -0700772 }
773#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* Make sure this tt's buffer is also available for CSPLITs.
775 * We pessimize a bit; probably the typical full speed case
776 * doesn't need the second CSPLIT.
David Brownell53bd6a62006-08-30 14:50:06 -0700777 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * NOTE: both SPLIT and CSPLIT could be checked in just
779 * one smart pass...
780 */
781 mask = 0x03 << (uframe + qh->gap_uf);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700782 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 mask |= 1 << uframe;
785 if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
786 if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
787 qh->period, qh->c_usecs))
788 goto done;
789 if (!check_period (ehci, frame, uframe + qh->gap_uf,
790 qh->period, qh->c_usecs))
791 goto done;
792 retval = 0;
793 }
Dan Streetmanba47f662006-05-24 09:39:16 -0700794#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795done:
796 return retval;
797}
798
799/* "first fit" scheduling policy used the first time through,
800 * or when the previous schedule slot can't be re-used.
801 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700802static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
David Brownell53bd6a62006-08-30 14:50:06 -0700804 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 unsigned uframe;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700806 __hc32 c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
Alek Du3807e262009-07-14 07:23:29 +0800808 struct ehci_qh_hw *hw = qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 qh_refresh(ehci, qh);
Alek Du3807e262009-07-14 07:23:29 +0800811 hw->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 frame = qh->start;
813
814 /* reuse the previous schedule slots, if we can */
815 if (frame < qh->period) {
Alek Du3807e262009-07-14 07:23:29 +0800816 uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 status = check_intr_schedule (ehci, frame, --uframe,
818 qh, &c_mask);
819 } else {
820 uframe = 0;
821 c_mask = 0;
822 status = -ENOSPC;
823 }
824
825 /* else scan the schedule to find a group of slots such that all
826 * uframes have enough periodic bandwidth available.
827 */
828 if (status) {
829 /* "normal" case, uframing flexible except with splits */
830 if (qh->period) {
Alan Stern68335e82009-05-22 17:02:33 -0400831 int i;
832
833 for (i = qh->period; status && i > 0; --i) {
834 frame = ++ehci->random_frame % qh->period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 for (uframe = 0; uframe < 8; uframe++) {
836 status = check_intr_schedule (ehci,
837 frame, uframe, qh,
838 &c_mask);
839 if (status == 0)
840 break;
841 }
Alan Stern68335e82009-05-22 17:02:33 -0400842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* qh->period == 0 means every uframe */
845 } else {
846 frame = 0;
847 status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
848 }
849 if (status)
850 goto done;
851 qh->start = frame;
852
853 /* reset S-frame and (maybe) C-frame masks */
Alek Du3807e262009-07-14 07:23:29 +0800854 hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
855 hw->hw_info2 |= qh->period
Stefan Roese6dbd6822007-05-01 09:29:37 -0700856 ? cpu_to_hc32(ehci, 1 << uframe)
857 : cpu_to_hc32(ehci, QH_SMASK);
Alek Du3807e262009-07-14 07:23:29 +0800858 hw->hw_info2 |= c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 } else
860 ehci_dbg (ehci, "reused qh %p schedule\n", qh);
861
862 /* stuff into the periodic schedule */
Alan Sternb015cb72012-07-11 11:22:10 -0400863 qh_link_periodic(ehci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864done:
865 return status;
866}
867
868static int intr_submit (
869 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct urb *urb,
871 struct list_head *qtd_list,
Al Viro55016f12005-10-21 03:21:58 -0400872 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873) {
874 unsigned epnum;
875 unsigned long flags;
876 struct ehci_qh *qh;
Alan Sterne9df41c2007-08-08 11:48:02 -0400877 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct list_head empty;
879
880 /* get endpoint and transfer/schedule data */
Alan Sterne9df41c2007-08-08 11:48:02 -0400881 epnum = urb->ep->desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 spin_lock_irqsave (&ehci->lock, flags);
884
Alan Stern541c7d42010-06-22 16:39:10 -0400885 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100886 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -0400887 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100888 }
Alan Sterne9df41c2007-08-08 11:48:02 -0400889 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
890 if (unlikely(status))
891 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* get qh and force any scheduling errors */
894 INIT_LIST_HEAD (&empty);
Alan Sterne9df41c2007-08-08 11:48:02 -0400895 qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (qh == NULL) {
897 status = -ENOMEM;
898 goto done;
899 }
900 if (qh->qh_state == QH_STATE_IDLE) {
901 if ((status = qh_schedule (ehci, qh)) != 0)
902 goto done;
903 }
904
905 /* then queue the urb's tds to the qh */
Alan Sterne9df41c2007-08-08 11:48:02 -0400906 qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 BUG_ON (qh == NULL);
908
909 /* ... update usbfs periodic stats */
910 ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
911
912done:
Alan Sterne9df41c2007-08-08 11:48:02 -0400913 if (unlikely(status))
914 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
915done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 spin_unlock_irqrestore (&ehci->lock, flags);
917 if (status)
918 qtd_list_free (ehci, urb, qtd_list);
919
920 return status;
921}
922
923/*-------------------------------------------------------------------------*/
924
925/* ehci_iso_stream ops work with both ITD and SITD */
926
927static struct ehci_iso_stream *
Al Viro55016f12005-10-21 03:21:58 -0400928iso_stream_alloc (gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 struct ehci_iso_stream *stream;
931
Pekka Enberg7b842b62005-09-06 15:18:34 -0700932 stream = kzalloc(sizeof *stream, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 INIT_LIST_HEAD(&stream->td_list);
935 INIT_LIST_HEAD(&stream->free_list);
936 stream->next_uframe = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938 return stream;
939}
940
941static void
942iso_stream_init (
943 struct ehci_hcd *ehci,
944 struct ehci_iso_stream *stream,
945 struct usb_device *dev,
946 int pipe,
947 unsigned interval
948)
949{
950 static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
951
952 u32 buf1;
953 unsigned epnum, maxp;
954 int is_input;
955 long bandwidth;
956
957 /*
958 * this might be a "high bandwidth" highspeed endpoint,
959 * as encoded in the ep descriptor's wMaxPacket field
960 */
961 epnum = usb_pipeendpoint (pipe);
962 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
963 maxp = usb_maxpacket(dev, pipe, !is_input);
964 if (is_input) {
965 buf1 = (1 << 11);
966 } else {
967 buf1 = 0;
968 }
969
970 /* knows about ITD vs SITD */
971 if (dev->speed == USB_SPEED_HIGH) {
972 unsigned multi = hb_mult(maxp);
973
974 stream->highspeed = 1;
975
976 maxp = max_packet(maxp);
977 buf1 |= maxp;
978 maxp *= multi;
979
Stefan Roese6dbd6822007-05-01 09:29:37 -0700980 stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
981 stream->buf1 = cpu_to_hc32(ehci, buf1);
982 stream->buf2 = cpu_to_hc32(ehci, multi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 /* usbfs wants to report the average usecs per frame tied up
985 * when transfers on this endpoint are scheduled ...
986 */
987 stream->usecs = HS_USECS_ISO (maxp);
988 bandwidth = stream->usecs * 8;
Alan Stern372dd6e2008-11-12 17:02:57 -0500989 bandwidth /= interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 } else {
992 u32 addr;
david-b@pacbell.netd0384202005-08-13 18:44:58 -0700993 int think_time;
Clemens Ladisch469d0222006-01-20 13:49:10 -0800994 int hs_transfers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 addr = dev->ttport << 24;
997 if (!ehci_is_TDI(ehci)
998 || (dev->tt->hub !=
999 ehci_to_hcd(ehci)->self.root_hub))
1000 addr |= dev->tt->hub->devnum << 16;
1001 addr |= epnum << 8;
1002 addr |= dev->devnum;
1003 stream->usecs = HS_USECS_ISO (maxp);
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001004 think_time = dev->tt ? dev->tt->think_time : 0;
1005 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
1006 dev->speed, is_input, 1, maxp));
Clemens Ladisch469d0222006-01-20 13:49:10 -08001007 hs_transfers = max (1u, (maxp + 187) / 188);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (is_input) {
1009 u32 tmp;
1010
1011 addr |= 1 << 31;
1012 stream->c_usecs = stream->usecs;
1013 stream->usecs = HS_USECS_ISO (1);
1014 stream->raw_mask = 1;
1015
Clemens Ladisch469d0222006-01-20 13:49:10 -08001016 /* c-mask as specified in USB 2.0 11.18.4 3.c */
1017 tmp = (1 << (hs_transfers + 2)) - 1;
1018 stream->raw_mask |= tmp << (8 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 } else
Clemens Ladisch469d0222006-01-20 13:49:10 -08001020 stream->raw_mask = smask_out [hs_transfers - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 bandwidth = stream->usecs + stream->c_usecs;
Alan Stern372dd6e2008-11-12 17:02:57 -05001022 bandwidth /= interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /* stream->splits gets created from raw_mask later */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001025 stream->address = cpu_to_hc32(ehci, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 stream->bandwidth = bandwidth;
1028
1029 stream->udev = dev;
1030
1031 stream->bEndpointAddress = is_input | epnum;
1032 stream->interval = interval;
1033 stream->maxp = maxp;
1034}
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036static struct ehci_iso_stream *
1037iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1038{
1039 unsigned epnum;
1040 struct ehci_iso_stream *stream;
1041 struct usb_host_endpoint *ep;
1042 unsigned long flags;
1043
1044 epnum = usb_pipeendpoint (urb->pipe);
1045 if (usb_pipein(urb->pipe))
1046 ep = urb->dev->ep_in[epnum];
1047 else
1048 ep = urb->dev->ep_out[epnum];
1049
1050 spin_lock_irqsave (&ehci->lock, flags);
1051 stream = ep->hcpriv;
1052
1053 if (unlikely (stream == NULL)) {
1054 stream = iso_stream_alloc(GFP_ATOMIC);
1055 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 ep->hcpriv = stream;
1057 stream->ep = ep;
1058 iso_stream_init(ehci, stream, urb->dev, urb->pipe,
1059 urb->interval);
1060 }
1061
Clemens Ladisch1082f572010-03-01 17:18:56 +01001062 /* if dev->ep [epnum] is a QH, hw is set */
1063 } else if (unlikely (stream->hw != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1065 urb->dev->devpath, epnum,
1066 usb_pipein(urb->pipe) ? "in" : "out");
1067 stream = NULL;
1068 }
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 spin_unlock_irqrestore (&ehci->lock, flags);
1071 return stream;
1072}
1073
1074/*-------------------------------------------------------------------------*/
1075
1076/* ehci_iso_sched ops can be ITD-only or SITD-only */
1077
1078static struct ehci_iso_sched *
Al Viro55016f12005-10-21 03:21:58 -04001079iso_sched_alloc (unsigned packets, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct ehci_iso_sched *iso_sched;
1082 int size = sizeof *iso_sched;
1083
1084 size += packets * sizeof (struct ehci_iso_packet);
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001085 iso_sched = kzalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (likely (iso_sched != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 INIT_LIST_HEAD (&iso_sched->td_list);
1088 }
1089 return iso_sched;
1090}
1091
1092static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001093itd_sched_init(
1094 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct ehci_iso_sched *iso_sched,
1096 struct ehci_iso_stream *stream,
1097 struct urb *urb
1098)
1099{
1100 unsigned i;
1101 dma_addr_t dma = urb->transfer_dma;
1102
1103 /* how many uframes are needed for these transfers */
1104 iso_sched->span = urb->number_of_packets * stream->interval;
1105
1106 /* figure out per-uframe itd fields that we'll need later
1107 * when we fit new itds into the schedule.
1108 */
1109 for (i = 0; i < urb->number_of_packets; i++) {
1110 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1111 unsigned length;
1112 dma_addr_t buf;
1113 u32 trans;
1114
1115 length = urb->iso_frame_desc [i].length;
1116 buf = dma + urb->iso_frame_desc [i].offset;
1117
1118 trans = EHCI_ISOC_ACTIVE;
1119 trans |= buf & 0x0fff;
1120 if (unlikely (((i + 1) == urb->number_of_packets))
1121 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1122 trans |= EHCI_ITD_IOC;
1123 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001124 uframe->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
David Brownell77078572005-05-28 10:46:18 -07001126 /* might need to cross a buffer page within a uframe */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 uframe->bufp = (buf & ~(u64)0x0fff);
1128 buf += length;
1129 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1130 uframe->cross = 1;
1131 }
1132}
1133
1134static void
1135iso_sched_free (
1136 struct ehci_iso_stream *stream,
1137 struct ehci_iso_sched *iso_sched
1138)
1139{
1140 if (!iso_sched)
1141 return;
1142 // caller must hold ehci->lock!
1143 list_splice (&iso_sched->td_list, &stream->free_list);
1144 kfree (iso_sched);
1145}
1146
1147static int
1148itd_urb_transaction (
1149 struct ehci_iso_stream *stream,
1150 struct ehci_hcd *ehci,
1151 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001152 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153)
1154{
1155 struct ehci_itd *itd;
1156 dma_addr_t itd_dma;
1157 int i;
1158 unsigned num_itds;
1159 struct ehci_iso_sched *sched;
1160 unsigned long flags;
1161
1162 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1163 if (unlikely (sched == NULL))
1164 return -ENOMEM;
1165
Stefan Roese6dbd6822007-05-01 09:29:37 -07001166 itd_sched_init(ehci, sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 if (urb->interval < 8)
1169 num_itds = 1 + (sched->span + 7) / 8;
1170 else
1171 num_itds = urb->number_of_packets;
1172
1173 /* allocate/init ITDs */
1174 spin_lock_irqsave (&ehci->lock, flags);
1175 for (i = 0; i < num_itds; i++) {
1176
Alan Stern55934eb2012-07-11 11:22:35 -04001177 /*
1178 * Use iTDs from the free list, but not iTDs that may
1179 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
Alan Stern55934eb2012-07-11 11:22:35 -04001181 if (likely(!list_empty(&stream->free_list))) {
1182 itd = list_first_entry(&stream->free_list,
Stefan Roese6dbd6822007-05-01 09:29:37 -07001183 struct ehci_itd, itd_list);
Alan Stern55934eb2012-07-11 11:22:35 -04001184 if (itd->frame == ehci->clock_frame)
1185 goto alloc_itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 list_del (&itd->itd_list);
1187 itd_dma = itd->itd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001188 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001189 alloc_itd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 spin_unlock_irqrestore (&ehci->lock, flags);
1191 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1192 &itd_dma);
1193 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001194 if (!itd) {
1195 iso_sched_free(stream, sched);
1196 spin_unlock_irqrestore(&ehci->lock, flags);
1197 return -ENOMEM;
1198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 memset (itd, 0, sizeof *itd);
1202 itd->itd_dma = itd_dma;
1203 list_add (&itd->itd_list, &sched->td_list);
1204 }
1205 spin_unlock_irqrestore (&ehci->lock, flags);
1206
1207 /* temporarily store schedule info in hcpriv */
1208 urb->hcpriv = sched;
1209 urb->error_count = 0;
1210 return 0;
1211}
1212
1213/*-------------------------------------------------------------------------*/
1214
1215static inline int
1216itd_slot_ok (
1217 struct ehci_hcd *ehci,
1218 u32 mod,
1219 u32 uframe,
1220 u8 usecs,
1221 u32 period
1222)
1223{
1224 uframe %= period;
1225 do {
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001226 /* can't commit more than uframe_periodic_max usec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001228 > (ehci->uframe_periodic_max - usecs))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return 0;
1230
1231 /* we know urb->interval is 2^N uframes */
1232 uframe += period;
1233 } while (uframe < mod);
1234 return 1;
1235}
1236
1237static inline int
1238sitd_slot_ok (
1239 struct ehci_hcd *ehci,
1240 u32 mod,
1241 struct ehci_iso_stream *stream,
1242 u32 uframe,
1243 struct ehci_iso_sched *sched,
1244 u32 period_uframes
1245)
1246{
1247 u32 mask, tmp;
1248 u32 frame, uf;
1249
1250 mask = stream->raw_mask << (uframe & 7);
1251
1252 /* for IN, don't wrap CSPLIT into the next frame */
1253 if (mask & ~0xffff)
1254 return 0;
1255
Alan Stern65b8e5c2012-05-14 13:47:20 -04001256 /* check bandwidth */
1257 uframe %= period_uframes;
1258 frame = uframe >> 3;
1259
1260#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1261 /* The tt's fullspeed bus bandwidth must be available.
1262 * tt_available scheduling guarantees 10+% for control/bulk.
1263 */
1264 uf = uframe & 7;
1265 if (!tt_available(ehci, period_uframes >> 3,
1266 stream->udev, frame, uf, stream->tt_usecs))
1267 return 0;
1268#else
1269 /* tt must be idle for start(s), any gap, and csplit.
1270 * assume scheduling slop leaves 10+% for control/bulk.
1271 */
1272 if (!tt_no_collision(ehci, period_uframes >> 3,
1273 stream->udev, frame, mask))
1274 return 0;
1275#endif
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* this multi-pass logic is simple, but performance may
1278 * suffer when the schedule data isn't cached.
1279 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 do {
1281 u32 max_used;
1282
1283 frame = uframe >> 3;
1284 uf = uframe & 7;
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /* check starts (OUT uses more than one) */
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001287 max_used = ehci->uframe_periodic_max - stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
1289 if (periodic_usecs (ehci, frame, uf) > max_used)
1290 return 0;
1291 }
1292
1293 /* for IN, check CSPLIT */
1294 if (stream->c_usecs) {
Clemens Ladisch0c734622006-01-22 10:32:49 -08001295 uf = uframe & 7;
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001296 max_used = ehci->uframe_periodic_max - stream->c_usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 do {
1298 tmp = 1 << uf;
1299 tmp <<= 8;
1300 if ((stream->raw_mask & tmp) == 0)
1301 continue;
1302 if (periodic_usecs (ehci, frame, uf)
1303 > max_used)
1304 return 0;
1305 } while (++uf < 8);
1306 }
1307
1308 /* we know urb->interval is 2^N uframes */
1309 uframe += period_uframes;
1310 } while (uframe < mod);
1311
Stefan Roese6dbd6822007-05-01 09:29:37 -07001312 stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return 1;
1314}
1315
1316/*
1317 * This scheduler plans almost as far into the future as it has actual
1318 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1319 * "as small as possible" to be cache-friendlier.) That limits the size
1320 * transfers you can stream reliably; avoid more than 64 msec per urb.
1321 * Also avoid queue depths of less than ehci's worst irq latency (affected
1322 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1323 * and other factors); or more than about 230 msec total (for portability,
1324 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1325 */
1326
Sarah Sharpd7e055f2009-10-27 10:54:49 -07001327#define SCHEDULE_SLOP 80 /* microframes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329static int
1330iso_stream_schedule (
1331 struct ehci_hcd *ehci,
1332 struct urb *urb,
1333 struct ehci_iso_stream *stream
1334)
1335{
Alan Sternffda0802010-07-14 11:03:46 -04001336 u32 now, next, start, period, span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 int status;
1338 unsigned mod = ehci->periodic_size << 3;
1339 struct ehci_iso_sched *sched = urb->hcpriv;
1340
Alan Sternffda0802010-07-14 11:03:46 -04001341 period = urb->interval;
1342 span = sched->span;
1343 if (!stream->highspeed) {
1344 period <<= 3;
1345 span <<= 3;
1346 }
1347
1348 if (span > mod - SCHEDULE_SLOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 ehci_dbg (ehci, "iso request %p too long\n", urb);
1350 status = -EFBIG;
1351 goto fail;
1352 }
1353
Alan Stern68aa95d2011-10-12 10:39:14 -04001354 now = ehci_read_frame_index(ehci) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Alan Sternb40e43f2008-05-20 16:59:10 -04001356 /* Typical case: reuse current schedule, stream is still active.
1357 * Hopefully there are no gaps from the host falling behind
1358 * (irq delays etc), but if there are we'll take the next
1359 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
1361 if (likely (!list_empty (&stream->td_list))) {
Alan Stern1fb2e052010-07-14 11:03:53 -04001362 u32 excess;
Sarah Sharpdccd5742009-10-27 10:55:05 -07001363
1364 /* For high speed devices, allow scheduling within the
Alan Sternae68a832010-07-14 11:03:23 -04001365 * isochronous scheduling threshold. For full speed devices
1366 * and Intel PCI-based controllers, don't (work around for
1367 * Intel ICH9 bug).
Sarah Sharpdccd5742009-10-27 10:55:05 -07001368 */
Alan Sternae68a832010-07-14 11:03:23 -04001369 if (!stream->highspeed && ehci->fs_i_thresh)
Sarah Sharpdccd5742009-10-27 10:55:05 -07001370 next = now + ehci->i_thresh;
1371 else
1372 next = now;
Alan Sternb40e43f2008-05-20 16:59:10 -04001373
Alan Stern1fb2e052010-07-14 11:03:53 -04001374 /* Fell behind (by up to twice the slop amount)?
1375 * We decide based on the time of the last currently-scheduled
1376 * slot, not the time of the next available slot.
1377 */
1378 excess = (stream->next_uframe - period - next) & (mod - 1);
1379 if (excess >= mod - 2 * SCHEDULE_SLOP)
1380 start = next + excess - mod + period *
1381 DIV_ROUND_UP(mod - excess, period);
1382 else
1383 start = next + excess + period;
1384 if (start - now >= mod) {
1385 ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
1386 urb, start - now - period, period,
1387 mod);
Alan Sternb40e43f2008-05-20 16:59:10 -04001388 status = -EFBIG;
1389 goto fail;
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
1393 /* need to schedule; when's the next (u)frame we could start?
1394 * this is bigger than ehci->i_thresh allows; scheduling itself
1395 * isn't free, the slop should handle reasonably slow cpus. it
1396 * can also help high bandwidth if the dma and irq loads don't
1397 * jump until after the queue is primed.
1398 */
Alan Stern1fb2e052010-07-14 11:03:53 -04001399 else {
Matthieu CASTETe3420902011-11-28 11:30:22 +01001400 int done = 0;
Alan Stern1fb2e052010-07-14 11:03:53 -04001401 start = SCHEDULE_SLOP + (now & ~0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Alan Stern1fb2e052010-07-14 11:03:53 -04001403 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Thomas Poussevin811c9262011-10-27 18:46:48 +02001405 /* find a uframe slot with enough bandwidth.
1406 * Early uframes are more precious because full-speed
1407 * iso IN transfers can't use late uframes,
1408 * and therefore they should be allocated last.
1409 */
1410 next = start;
1411 start += period;
1412 do {
1413 start--;
Alan Stern1fb2e052010-07-14 11:03:53 -04001414 /* check schedule: enough space? */
1415 if (stream->highspeed) {
1416 if (itd_slot_ok(ehci, mod, start,
1417 stream->usecs, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001418 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001419 } else {
1420 if ((start % 8) >= 6)
1421 continue;
1422 if (sitd_slot_ok(ehci, mod, stream,
1423 start, sched, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001424 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001425 }
Matthieu CASTETe3420902011-11-28 11:30:22 +01001426 } while (start > next && !done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Alan Stern1fb2e052010-07-14 11:03:53 -04001428 /* no room in the schedule */
Matthieu CASTETe3420902011-11-28 11:30:22 +01001429 if (!done) {
Alan Stern1fb2e052010-07-14 11:03:53 -04001430 ehci_dbg(ehci, "iso resched full %p (now %d max %d)\n",
1431 urb, now, now + mod);
1432 status = -ENOSPC;
1433 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435 }
1436
Alan Stern1fb2e052010-07-14 11:03:53 -04001437 /* Tried to schedule too far into the future? */
1438 if (unlikely(start - now + span - period
1439 >= mod - 2 * SCHEDULE_SLOP)) {
1440 ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
1441 urb, start - now, span - period,
1442 mod - 2 * SCHEDULE_SLOP);
1443 status = -EFBIG;
1444 goto fail;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Alan Stern1fb2e052010-07-14 11:03:53 -04001447 stream->next_uframe = start & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /* report high speed start in uframes; full speed, in frames */
1450 urb->start_frame = stream->next_uframe;
1451 if (!stream->highspeed)
1452 urb->start_frame >>= 3;
1453 return 0;
Alan Stern1fb2e052010-07-14 11:03:53 -04001454
1455 fail:
1456 iso_sched_free(stream, sched);
1457 urb->hcpriv = NULL;
1458 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461/*-------------------------------------------------------------------------*/
1462
1463static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001464itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1465 struct ehci_itd *itd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 int i;
1468
David Brownell77078572005-05-28 10:46:18 -07001469 /* it's been recently zeroed */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001470 itd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 itd->hw_bufp [0] = stream->buf0;
1472 itd->hw_bufp [1] = stream->buf1;
1473 itd->hw_bufp [2] = stream->buf2;
1474
1475 for (i = 0; i < 8; i++)
1476 itd->index[i] = -1;
1477
1478 /* All other fields are filled when scheduling */
1479}
1480
1481static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001482itd_patch(
1483 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 struct ehci_itd *itd,
1485 struct ehci_iso_sched *iso_sched,
1486 unsigned index,
David Brownell77078572005-05-28 10:46:18 -07001487 u16 uframe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488)
1489{
1490 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1491 unsigned pg = itd->pg;
1492
1493 // BUG_ON (pg == 6 && uf->cross);
1494
1495 uframe &= 0x07;
1496 itd->index [uframe] = index;
1497
Stefan Roese6dbd6822007-05-01 09:29:37 -07001498 itd->hw_transaction[uframe] = uf->transaction;
1499 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1500 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1501 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 /* iso_frame_desc[].offset must be strictly increasing */
David Brownell77078572005-05-28 10:46:18 -07001504 if (unlikely (uf->cross)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 u64 bufp = uf->bufp + 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 itd->pg = ++pg;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001508 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1509 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511}
1512
1513static inline void
1514itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1515{
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001516 union ehci_shadow *prev = &ehci->pshadow[frame];
1517 __hc32 *hw_p = &ehci->periodic[frame];
1518 union ehci_shadow here = *prev;
1519 __hc32 type = 0;
1520
1521 /* skip any iso nodes which might belong to previous microframes */
1522 while (here.ptr) {
1523 type = Q_NEXT_TYPE(ehci, *hw_p);
1524 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
1525 break;
1526 prev = periodic_next_shadow(ehci, prev, type);
1527 hw_p = shadow_next_periodic(ehci, &here, type);
1528 here = *prev;
1529 }
1530
1531 itd->itd_next = here;
1532 itd->hw_next = *hw_p;
1533 prev->itd = itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 itd->frame = frame;
1535 wmb ();
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001536 *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539/* fit urb's itds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001540static void itd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 struct ehci_hcd *ehci,
1542 struct urb *urb,
1543 unsigned mod,
1544 struct ehci_iso_stream *stream
1545)
1546{
David Brownell77078572005-05-28 10:46:18 -07001547 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 unsigned next_uframe, uframe, frame;
1549 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1550 struct ehci_itd *itd;
1551
Alan Sternbccbefa2010-07-14 11:03:36 -04001552 next_uframe = stream->next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 if (unlikely (list_empty(&stream->td_list))) {
1555 ehci_to_hcd(ehci)->self.bandwidth_allocated
1556 += stream->bandwidth;
1557 ehci_vdbg (ehci,
1558 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1559 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1560 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1561 urb->interval,
1562 next_uframe >> 3, next_uframe & 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
Alex He05570292010-12-07 10:10:08 +08001564
1565 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001566 if (ehci->amd_pll_fix == 1)
1567 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08001568 }
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1571
1572 /* fill iTDs uframe by uframe */
1573 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1574 if (itd == NULL) {
1575 /* ASSERT: we have all necessary itds */
1576 // BUG_ON (list_empty (&iso_sched->td_list));
1577
1578 /* ASSERT: no itds for this endpoint in this uframe */
1579
1580 itd = list_entry (iso_sched->td_list.next,
1581 struct ehci_itd, itd_list);
1582 list_move_tail (&itd->itd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001583 itd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01001584 itd->urb = urb;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001585 itd_init (ehci, stream, itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587
1588 uframe = next_uframe & 0x07;
1589 frame = next_uframe >> 3;
1590
Stefan Roese6dbd6822007-05-01 09:29:37 -07001591 itd_patch(ehci, itd, iso_sched, packet, uframe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 next_uframe += stream->interval;
Alan Sternbccbefa2010-07-14 11:03:36 -04001594 next_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 packet++;
1596
1597 /* link completed itds into the schedule */
1598 if (((next_uframe >> 3) != frame)
1599 || packet == urb->number_of_packets) {
Alan Sternbccbefa2010-07-14 11:03:36 -04001600 itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 itd = NULL;
1602 }
1603 }
1604 stream->next_uframe = next_uframe;
1605
1606 /* don't need that schedule data any more */
1607 iso_sched_free (stream, iso_sched);
1608 urb->hcpriv = NULL;
1609
1610 timer_action (ehci, TIMER_IO_WATCHDOG);
Alan Sternb015cb72012-07-11 11:22:10 -04001611 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
1614#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1615
David Brownell30bf54e2007-12-16 22:37:40 -08001616/* Process and recycle a completed ITD. Return true iff its urb completed,
1617 * and hence its completion callback probably added things to the hardware
1618 * schedule.
1619 *
1620 * Note that we carefully avoid recycling this descriptor until after any
1621 * completion callback runs, so that it won't be reused quickly. That is,
1622 * assuming (a) no more than two urbs per frame on this endpoint, and also
1623 * (b) only this endpoint's completions submit URBs. It seems some silicon
1624 * corrupts things if you reuse completed descriptors very quickly...
1625 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626static unsigned
1627itd_complete (
1628 struct ehci_hcd *ehci,
David Howells7d12e782006-10-05 14:55:46 +01001629 struct ehci_itd *itd
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630) {
1631 struct urb *urb = itd->urb;
1632 struct usb_iso_packet_descriptor *desc;
1633 u32 t;
1634 unsigned uframe;
1635 int urb_index = -1;
1636 struct ehci_iso_stream *stream = itd->stream;
1637 struct usb_device *dev;
David Brownell30bf54e2007-12-16 22:37:40 -08001638 unsigned retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 /* for each uframe with a packet */
1641 for (uframe = 0; uframe < 8; uframe++) {
1642 if (likely (itd->index[uframe] == -1))
1643 continue;
1644 urb_index = itd->index[uframe];
1645 desc = &urb->iso_frame_desc [urb_index];
1646
Stefan Roese6dbd6822007-05-01 09:29:37 -07001647 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 itd->hw_transaction [uframe] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 /* report transfer status */
1651 if (unlikely (t & ISO_ERRS)) {
1652 urb->error_count++;
1653 if (t & EHCI_ISOC_BUF_ERR)
1654 desc->status = usb_pipein (urb->pipe)
1655 ? -ENOSR /* hc couldn't read */
1656 : -ECOMM; /* hc couldn't write */
1657 else if (t & EHCI_ISOC_BABBLE)
1658 desc->status = -EOVERFLOW;
1659 else /* (t & EHCI_ISOC_XACTERR) */
1660 desc->status = -EPROTO;
1661
1662 /* HC need not update length with this error */
Alan Sternec6d67e2009-06-29 14:34:59 -04001663 if (!(t & EHCI_ISOC_BABBLE)) {
1664 desc->actual_length = EHCI_ITD_LENGTH(t);
1665 urb->actual_length += desc->actual_length;
1666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1668 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04001669 desc->actual_length = EHCI_ITD_LENGTH(t);
1670 urb->actual_length += desc->actual_length;
Alan Sternb40e43f2008-05-20 16:59:10 -04001671 } else {
1672 /* URB was too late */
1673 desc->status = -EXDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675 }
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /* handle completion now? */
1678 if (likely ((urb_index + 1) != urb->number_of_packets))
David Brownell30bf54e2007-12-16 22:37:40 -08001679 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /* ASSERT: it's really the last itd for this urb
1682 list_for_each_entry (itd, &stream->td_list, itd_list)
1683 BUG_ON (itd->urb == urb);
1684 */
1685
David Brownellaa16ca32007-12-30 23:45:19 -08001686 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05001687 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04001688 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08001689 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 urb = NULL;
Alan Sternb015cb72012-07-11 11:22:10 -04001691 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
1693
Alex He05570292010-12-07 10:10:08 +08001694 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001695 if (ehci->amd_pll_fix == 1)
1696 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08001697 }
1698
Karsten Wiese508db8c2009-02-26 01:47:48 +01001699 if (unlikely(list_is_singular(&stream->td_list))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 ehci_to_hcd(ehci)->self.bandwidth_allocated
1701 -= stream->bandwidth;
1702 ehci_vdbg (ehci,
1703 "deschedule devp %s ep%d%s-iso\n",
1704 dev->devpath, stream->bEndpointAddress & 0x0f,
1705 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1706 }
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001707
David Brownell30bf54e2007-12-16 22:37:40 -08001708done:
David Brownell30bf54e2007-12-16 22:37:40 -08001709 itd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04001710
1711 /* Add to the end of the free list for later reuse */
1712 list_move_tail(&itd->itd_list, &stream->free_list);
1713
1714 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
1715 if (list_empty(&stream->td_list)) {
1716 list_splice_tail_init(&stream->free_list,
1717 &ehci->cached_itd_list);
1718 start_free_itds(ehci);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001719 }
Alan Stern55934eb2012-07-11 11:22:35 -04001720
David Brownell30bf54e2007-12-16 22:37:40 -08001721 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
1724/*-------------------------------------------------------------------------*/
1725
Olav Kongas5db539e2005-06-23 20:25:36 +03001726static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001727 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 int status = -EINVAL;
1730 unsigned long flags;
1731 struct ehci_iso_stream *stream;
1732
1733 /* Get iso_stream head */
1734 stream = iso_stream_find (ehci, urb);
1735 if (unlikely (stream == NULL)) {
1736 ehci_dbg (ehci, "can't get iso stream\n");
1737 return -ENOMEM;
1738 }
1739 if (unlikely (urb->interval != stream->interval)) {
1740 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1741 stream->interval, urb->interval);
1742 goto done;
1743 }
1744
1745#ifdef EHCI_URB_TRACE
1746 ehci_dbg (ehci,
1747 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001748 __func__, urb->dev->devpath, urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 usb_pipeendpoint (urb->pipe),
1750 usb_pipein (urb->pipe) ? "in" : "out",
1751 urb->transfer_buffer_length,
1752 urb->number_of_packets, urb->interval,
1753 stream);
1754#endif
1755
1756 /* allocate ITDs w/o locking anything */
1757 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1758 if (unlikely (status < 0)) {
1759 ehci_dbg (ehci, "can't init itds\n");
1760 goto done;
1761 }
1762
1763 /* schedule ... need to lock */
1764 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001765 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001766 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04001767 goto done_not_linked;
1768 }
1769 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1770 if (unlikely(status))
1771 goto done_not_linked;
1772 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07001773 if (likely (status == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04001775 else
1776 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001777 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001779 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return status;
1781}
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783/*-------------------------------------------------------------------------*/
1784
1785/*
1786 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1787 * TTs in USB 2.0 hubs. These need microframe scheduling.
1788 */
1789
1790static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001791sitd_sched_init(
1792 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 struct ehci_iso_sched *iso_sched,
1794 struct ehci_iso_stream *stream,
1795 struct urb *urb
1796)
1797{
1798 unsigned i;
1799 dma_addr_t dma = urb->transfer_dma;
1800
1801 /* how many frames are needed for these transfers */
1802 iso_sched->span = urb->number_of_packets * stream->interval;
1803
1804 /* figure out per-frame sitd fields that we'll need later
1805 * when we fit new sitds into the schedule.
1806 */
1807 for (i = 0; i < urb->number_of_packets; i++) {
1808 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1809 unsigned length;
1810 dma_addr_t buf;
1811 u32 trans;
1812
1813 length = urb->iso_frame_desc [i].length & 0x03ff;
1814 buf = dma + urb->iso_frame_desc [i].offset;
1815
1816 trans = SITD_STS_ACTIVE;
1817 if (((i + 1) == urb->number_of_packets)
1818 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1819 trans |= SITD_IOC;
1820 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001821 packet->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 /* might need to cross a buffer page within a td */
1824 packet->bufp = buf;
1825 packet->buf1 = (buf + length) & ~0x0fff;
1826 if (packet->buf1 != (buf & ~(u64)0x0fff))
1827 packet->cross = 1;
1828
David Brownell53bd6a62006-08-30 14:50:06 -07001829 /* OUT uses multiple start-splits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (stream->bEndpointAddress & USB_DIR_IN)
1831 continue;
1832 length = (length + 187) / 188;
1833 if (length > 1) /* BEGIN vs ALL */
1834 length |= 1 << 3;
1835 packet->buf1 |= length;
1836 }
1837}
1838
1839static int
1840sitd_urb_transaction (
1841 struct ehci_iso_stream *stream,
1842 struct ehci_hcd *ehci,
1843 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001844 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845)
1846{
1847 struct ehci_sitd *sitd;
1848 dma_addr_t sitd_dma;
1849 int i;
1850 struct ehci_iso_sched *iso_sched;
1851 unsigned long flags;
1852
1853 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1854 if (iso_sched == NULL)
1855 return -ENOMEM;
1856
Stefan Roese6dbd6822007-05-01 09:29:37 -07001857 sitd_sched_init(ehci, iso_sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 /* allocate/init sITDs */
1860 spin_lock_irqsave (&ehci->lock, flags);
1861 for (i = 0; i < urb->number_of_packets; i++) {
1862
1863 /* NOTE: for now, we don't try to handle wraparound cases
1864 * for IN (using sitd->hw_backpointer, like a FSTN), which
1865 * means we never need two sitds for full speed packets.
1866 */
1867
Alan Stern55934eb2012-07-11 11:22:35 -04001868 /*
1869 * Use siTDs from the free list, but not siTDs that may
1870 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 */
Alan Stern55934eb2012-07-11 11:22:35 -04001872 if (likely(!list_empty(&stream->free_list))) {
1873 sitd = list_first_entry(&stream->free_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 struct ehci_sitd, sitd_list);
Alan Stern55934eb2012-07-11 11:22:35 -04001875 if (sitd->frame == ehci->clock_frame)
1876 goto alloc_sitd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 list_del (&sitd->sitd_list);
1878 sitd_dma = sitd->sitd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001879 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001880 alloc_sitd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 spin_unlock_irqrestore (&ehci->lock, flags);
1882 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1883 &sitd_dma);
1884 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001885 if (!sitd) {
1886 iso_sched_free(stream, iso_sched);
1887 spin_unlock_irqrestore(&ehci->lock, flags);
1888 return -ENOMEM;
1889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 memset (sitd, 0, sizeof *sitd);
1893 sitd->sitd_dma = sitd_dma;
1894 list_add (&sitd->sitd_list, &iso_sched->td_list);
1895 }
1896
1897 /* temporarily store schedule info in hcpriv */
1898 urb->hcpriv = iso_sched;
1899 urb->error_count = 0;
1900
1901 spin_unlock_irqrestore (&ehci->lock, flags);
1902 return 0;
1903}
1904
1905/*-------------------------------------------------------------------------*/
1906
1907static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001908sitd_patch(
1909 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 struct ehci_iso_stream *stream,
1911 struct ehci_sitd *sitd,
1912 struct ehci_iso_sched *iso_sched,
1913 unsigned index
1914)
1915{
1916 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1917 u64 bufp = uf->bufp;
1918
Stefan Roese6dbd6822007-05-01 09:29:37 -07001919 sitd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 sitd->hw_fullspeed_ep = stream->address;
1921 sitd->hw_uframe = stream->splits;
1922 sitd->hw_results = uf->transaction;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001923 sitd->hw_backpointer = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 bufp = uf->bufp;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001926 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
1927 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Stefan Roese6dbd6822007-05-01 09:29:37 -07001929 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (uf->cross)
1931 bufp += 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001932 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 sitd->index = index;
1934}
1935
1936static inline void
1937sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
1938{
1939 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1940 sitd->sitd_next = ehci->pshadow [frame];
1941 sitd->hw_next = ehci->periodic [frame];
1942 ehci->pshadow [frame].sitd = sitd;
1943 sitd->frame = frame;
1944 wmb ();
Stefan Roese6dbd6822007-05-01 09:29:37 -07001945 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
1948/* fit urb's sitds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001949static void sitd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 struct ehci_hcd *ehci,
1951 struct urb *urb,
1952 unsigned mod,
1953 struct ehci_iso_stream *stream
1954)
1955{
1956 int packet;
1957 unsigned next_uframe;
1958 struct ehci_iso_sched *sched = urb->hcpriv;
1959 struct ehci_sitd *sitd;
1960
1961 next_uframe = stream->next_uframe;
1962
1963 if (list_empty(&stream->td_list)) {
1964 /* usbfs ignores TT bandwidth */
1965 ehci_to_hcd(ehci)->self.bandwidth_allocated
1966 += stream->bandwidth;
1967 ehci_vdbg (ehci,
1968 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
1969 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1970 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
Alan Sternbccbefa2010-07-14 11:03:36 -04001971 (next_uframe >> 3) & (ehci->periodic_size - 1),
Stefan Roese6dbd6822007-05-01 09:29:37 -07001972 stream->interval, hc32_to_cpu(ehci, stream->splits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
Alex He05570292010-12-07 10:10:08 +08001974
1975 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001976 if (ehci->amd_pll_fix == 1)
1977 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08001978 }
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1981
1982 /* fill sITDs frame by frame */
1983 for (packet = 0, sitd = NULL;
1984 packet < urb->number_of_packets;
1985 packet++) {
1986
1987 /* ASSERT: we have all necessary sitds */
1988 BUG_ON (list_empty (&sched->td_list));
1989
1990 /* ASSERT: no itds for this endpoint in this frame */
1991
1992 sitd = list_entry (sched->td_list.next,
1993 struct ehci_sitd, sitd_list);
1994 list_move_tail (&sitd->sitd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001995 sitd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01001996 sitd->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Stefan Roese6dbd6822007-05-01 09:29:37 -07001998 sitd_patch(ehci, stream, sitd, sched, packet);
Alan Sternbccbefa2010-07-14 11:03:36 -04001999 sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 sitd);
2001
2002 next_uframe += stream->interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
Alan Sternbccbefa2010-07-14 11:03:36 -04002004 stream->next_uframe = next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 /* don't need that schedule data any more */
2007 iso_sched_free (stream, sched);
2008 urb->hcpriv = NULL;
2009
2010 timer_action (ehci, TIMER_IO_WATCHDOG);
Alan Sternb015cb72012-07-11 11:22:10 -04002011 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
2014/*-------------------------------------------------------------------------*/
2015
2016#define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
David Brownell53bd6a62006-08-30 14:50:06 -07002017 | SITD_STS_XACT | SITD_STS_MMF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
David Brownell30bf54e2007-12-16 22:37:40 -08002019/* Process and recycle a completed SITD. Return true iff its urb completed,
2020 * and hence its completion callback probably added things to the hardware
2021 * schedule.
2022 *
2023 * Note that we carefully avoid recycling this descriptor until after any
2024 * completion callback runs, so that it won't be reused quickly. That is,
2025 * assuming (a) no more than two urbs per frame on this endpoint, and also
2026 * (b) only this endpoint's completions submit URBs. It seems some silicon
2027 * corrupts things if you reuse completed descriptors very quickly...
2028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029static unsigned
2030sitd_complete (
2031 struct ehci_hcd *ehci,
David Howells7d12e782006-10-05 14:55:46 +01002032 struct ehci_sitd *sitd
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033) {
2034 struct urb *urb = sitd->urb;
2035 struct usb_iso_packet_descriptor *desc;
2036 u32 t;
2037 int urb_index = -1;
2038 struct ehci_iso_stream *stream = sitd->stream;
2039 struct usb_device *dev;
David Brownell30bf54e2007-12-16 22:37:40 -08002040 unsigned retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 urb_index = sitd->index;
2043 desc = &urb->iso_frame_desc [urb_index];
Stefan Roese6dbd6822007-05-01 09:29:37 -07002044 t = hc32_to_cpup(ehci, &sitd->hw_results);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 /* report transfer status */
2047 if (t & SITD_ERRS) {
2048 urb->error_count++;
2049 if (t & SITD_STS_DBE)
2050 desc->status = usb_pipein (urb->pipe)
2051 ? -ENOSR /* hc couldn't read */
2052 : -ECOMM; /* hc couldn't write */
2053 else if (t & SITD_STS_BABBLE)
2054 desc->status = -EOVERFLOW;
2055 else /* XACT, MMF, etc */
2056 desc->status = -EPROTO;
2057 } else {
2058 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04002059 desc->actual_length = desc->length - SITD_LENGTH(t);
2060 urb->actual_length += desc->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 /* handle completion now? */
2064 if ((urb_index + 1) != urb->number_of_packets)
David Brownell30bf54e2007-12-16 22:37:40 -08002065 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 /* ASSERT: it's really the last sitd for this urb
2068 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2069 BUG_ON (sitd->urb == urb);
2070 */
2071
David Brownellaa16ca32007-12-30 23:45:19 -08002072 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05002073 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04002074 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08002075 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 urb = NULL;
Alan Sternb015cb72012-07-11 11:22:10 -04002077 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
2079
Alex He05570292010-12-07 10:10:08 +08002080 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002081 if (ehci->amd_pll_fix == 1)
2082 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08002083 }
2084
Karsten Wiese508db8c2009-02-26 01:47:48 +01002085 if (list_is_singular(&stream->td_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 ehci_to_hcd(ehci)->self.bandwidth_allocated
2087 -= stream->bandwidth;
2088 ehci_vdbg (ehci,
2089 "deschedule devp %s ep%d%s-iso\n",
2090 dev->devpath, stream->bEndpointAddress & 0x0f,
2091 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
2092 }
Alan Stern0e5f2312010-04-08 16:56:37 -04002093
David Brownell30bf54e2007-12-16 22:37:40 -08002094done:
David Brownell30bf54e2007-12-16 22:37:40 -08002095 sitd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04002096
2097 /* Add to the end of the free list for later reuse */
2098 list_move_tail(&sitd->sitd_list, &stream->free_list);
2099
2100 /* Recycle the siTDs when the pipeline is empty (ep no longer in use) */
2101 if (list_empty(&stream->td_list)) {
2102 list_splice_tail_init(&stream->free_list,
2103 &ehci->cached_sitd_list);
2104 start_free_itds(ehci);
Alan Stern0e5f2312010-04-08 16:56:37 -04002105 }
Alan Stern55934eb2012-07-11 11:22:35 -04002106
David Brownell30bf54e2007-12-16 22:37:40 -08002107 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
2110
Olav Kongas5db539e2005-06-23 20:25:36 +03002111static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04002112 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 int status = -EINVAL;
2115 unsigned long flags;
2116 struct ehci_iso_stream *stream;
2117
2118 /* Get iso_stream head */
2119 stream = iso_stream_find (ehci, urb);
2120 if (stream == NULL) {
2121 ehci_dbg (ehci, "can't get iso stream\n");
2122 return -ENOMEM;
2123 }
2124 if (urb->interval != stream->interval) {
2125 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
2126 stream->interval, urb->interval);
2127 goto done;
2128 }
2129
2130#ifdef EHCI_URB_TRACE
2131 ehci_dbg (ehci,
2132 "submit %p dev%s ep%d%s-iso len %d\n",
2133 urb, urb->dev->devpath,
2134 usb_pipeendpoint (urb->pipe),
2135 usb_pipein (urb->pipe) ? "in" : "out",
2136 urb->transfer_buffer_length);
2137#endif
2138
2139 /* allocate SITDs */
2140 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2141 if (status < 0) {
2142 ehci_dbg (ehci, "can't init sitds\n");
2143 goto done;
2144 }
2145
2146 /* schedule ... need to lock */
2147 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04002148 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002149 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04002150 goto done_not_linked;
2151 }
2152 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2153 if (unlikely(status))
2154 goto done_not_linked;
2155 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07002156 if (status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04002158 else
2159 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002160 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002162 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return status;
2164}
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166/*-------------------------------------------------------------------------*/
2167
2168static void
David Howells7d12e782006-10-05 14:55:46 +01002169scan_periodic (struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Alan Sternb40e43f2008-05-20 16:59:10 -04002171 unsigned now_uframe, frame, clock, clock_frame, mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 unsigned modified;
2173
2174 mod = ehci->periodic_size << 3;
2175
2176 /*
2177 * When running, scan from last scan point up to "now"
2178 * else clean up by scanning everything that's left.
2179 * Touches as few pages as possible: cache-friendly.
2180 */
2181 now_uframe = ehci->next_uframe;
Alan Sternc0c53db2012-07-11 11:21:48 -04002182 if (ehci->rh_state >= EHCI_RH_RUNNING) {
Alan Stern68aa95d2011-10-12 10:39:14 -04002183 clock = ehci_read_frame_index(ehci);
Alan Sternbccbefa2010-07-14 11:03:36 -04002184 clock_frame = (clock >> 3) & (ehci->periodic_size - 1);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002185 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 clock = now_uframe + mod - 1;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002187 clock_frame = -1;
2188 }
Alan Stern55934eb2012-07-11 11:22:35 -04002189 ehci->clock_frame = clock_frame;
Alan Sternbccbefa2010-07-14 11:03:36 -04002190 clock &= mod - 1;
Alan Sternb40e43f2008-05-20 16:59:10 -04002191 clock_frame = clock >> 3;
Alan Stern1e12c912011-05-17 10:40:51 -04002192 ++ehci->periodic_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 for (;;) {
2195 union ehci_shadow q, *q_p;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002196 __hc32 type, *hw_p;
David Brownell79592b722008-01-07 00:47:42 -08002197 unsigned incomplete = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 frame = now_uframe >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201restart:
2202 /* scan each element in frame's queue for completions */
2203 q_p = &ehci->pshadow [frame];
2204 hw_p = &ehci->periodic [frame];
2205 q.ptr = q_p->ptr;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002206 type = Q_NEXT_TYPE(ehci, *hw_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 modified = 0;
2208
2209 while (q.ptr != NULL) {
2210 unsigned uf;
2211 union ehci_shadow temp;
2212 int live;
2213
Alan Sternc0c53db2012-07-11 11:21:48 -04002214 live = (ehci->rh_state >= EHCI_RH_RUNNING);
Stefan Roese6dbd6822007-05-01 09:29:37 -07002215 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 case Q_TYPE_QH:
2217 /* handle any completions */
Alan Sternc83e1a92012-07-11 11:21:25 -04002218 temp.qh = q.qh;
Alek Du3807e262009-07-14 07:23:29 +08002219 type = Q_NEXT_TYPE(ehci, q.qh->hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 q = q.qh->qh_next;
Alan Stern1e12c912011-05-17 10:40:51 -04002221 if (temp.qh->stamp != ehci->periodic_stamp) {
2222 modified = qh_completions(ehci, temp.qh);
2223 if (!modified)
2224 temp.qh->stamp = ehci->periodic_stamp;
2225 if (unlikely(list_empty(&temp.qh->qtd_list) ||
2226 temp.qh->needs_rescan))
Alan Sterndf202252012-07-11 11:22:26 -04002227 start_unlink_intr(ehci, temp.qh);
Alan Stern1e12c912011-05-17 10:40:51 -04002228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 break;
2230 case Q_TYPE_FSTN:
2231 /* for "save place" FSTNs, look at QH entries
2232 * in the previous frame for completions.
2233 */
Stefan Roese6dbd6822007-05-01 09:29:37 -07002234 if (q.fstn->hw_prev != EHCI_LIST_END(ehci)) {
Greg Kroah-Hartman2d0fe1b2012-05-01 21:33:36 -07002235 ehci_dbg(ehci,
2236 "ignoring completions from FSTNs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Stefan Roese6dbd6822007-05-01 09:29:37 -07002238 type = Q_NEXT_TYPE(ehci, q.fstn->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 q = q.fstn->fstn_next;
2240 break;
2241 case Q_TYPE_ITD:
David Brownell79592b722008-01-07 00:47:42 -08002242 /* If this ITD is still active, leave it for
2243 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002244 * No need to check for activity unless the
2245 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002246 */
Alan Sternb40e43f2008-05-20 16:59:10 -04002247 if (frame == clock_frame && live) {
2248 rmb();
2249 for (uf = 0; uf < 8; uf++) {
2250 if (q.itd->hw_transaction[uf] &
2251 ITD_ACTIVE(ehci))
2252 break;
2253 }
2254 if (uf < 8) {
2255 incomplete = true;
2256 q_p = &q.itd->itd_next;
2257 hw_p = &q.itd->hw_next;
2258 type = Q_NEXT_TYPE(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -07002259 q.itd->hw_next);
Alan Sternb40e43f2008-05-20 16:59:10 -04002260 q = *q_p;
2261 break;
2262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
David Brownell79592b722008-01-07 00:47:42 -08002265 /* Take finished ITDs out of the schedule
2266 * and process them: recycle, maybe report
2267 * URB completion. HC won't cache the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 * pointer for much longer, if at all.
2269 */
2270 *q_p = q.itd->itd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002271 if (!ehci->use_dummy_qh ||
2272 q.itd->hw_next != EHCI_LIST_END(ehci))
2273 *hw_p = q.itd->hw_next;
2274 else
2275 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002276 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002278 modified = itd_complete (ehci, q.itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 q = *q_p;
2280 break;
2281 case Q_TYPE_SITD:
David Brownell79592b722008-01-07 00:47:42 -08002282 /* If this SITD is still active, leave it for
2283 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002284 * No need to check for activity unless the
2285 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002286 */
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002287 if (((frame == clock_frame) ||
Alan Sternbccbefa2010-07-14 11:03:36 -04002288 (((frame + 1) & (ehci->periodic_size - 1))
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002289 == clock_frame))
2290 && live
2291 && (q.sitd->hw_results &
2292 SITD_ACTIVE(ehci))) {
2293
David Brownell79592b722008-01-07 00:47:42 -08002294 incomplete = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 q_p = &q.sitd->sitd_next;
2296 hw_p = &q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002297 type = Q_NEXT_TYPE(ehci,
2298 q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 q = *q_p;
2300 break;
2301 }
David Brownell79592b722008-01-07 00:47:42 -08002302
2303 /* Take finished SITDs out of the schedule
2304 * and process them: recycle, maybe report
2305 * URB completion.
2306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 *q_p = q.sitd->sitd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002308 if (!ehci->use_dummy_qh ||
2309 q.sitd->hw_next != EHCI_LIST_END(ehci))
2310 *hw_p = q.sitd->hw_next;
2311 else
2312 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002313 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002315 modified = sitd_complete (ehci, q.sitd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 q = *q_p;
2317 break;
2318 default:
Greg Kroah-Hartman2d0fe1b2012-05-01 21:33:36 -07002319 ehci_dbg(ehci, "corrupt type %d frame %d shadow %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 type, frame, q.ptr);
2321 // BUG ();
2322 q.ptr = NULL;
2323 }
2324
2325 /* assume completion callbacks modify the queue */
David Brownellaa16ca32007-12-30 23:45:19 -08002326 if (unlikely (modified)) {
Alan Stern3ca9aeb2012-07-11 11:22:05 -04002327 if (likely(ehci->periodic_count > 0))
David Brownellaa16ca32007-12-30 23:45:19 -08002328 goto restart;
David Brownell01c17142008-08-26 23:35:04 -07002329 /* short-circuit this scan */
David Brownellaa16ca32007-12-30 23:45:19 -08002330 now_uframe = clock;
2331 break;
2332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
2334
David Brownell79592b722008-01-07 00:47:42 -08002335 /* If we can tell we caught up to the hardware, stop now.
2336 * We can't advance our scan without collecting the ISO
2337 * transfers that are still pending in this frame.
2338 */
Alan Sternc0c53db2012-07-11 11:21:48 -04002339 if (incomplete && ehci->rh_state >= EHCI_RH_RUNNING) {
David Brownell79592b722008-01-07 00:47:42 -08002340 ehci->next_uframe = now_uframe;
2341 break;
2342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 // FIXME: this assumes we won't get lapped when
2345 // latencies climb; that should be rare, but...
2346 // detect it, and just go all the way around.
2347 // FLR might help detect this case, so long as latencies
2348 // don't exceed periodic_size msec (default 1.024 sec).
2349
2350 // FIXME: likewise assumes HC doesn't halt mid-scan
2351
2352 if (now_uframe == clock) {
2353 unsigned now;
2354
Alan Sternc0c53db2012-07-11 11:21:48 -04002355 if (ehci->rh_state < EHCI_RH_RUNNING
Alan Stern3ca9aeb2012-07-11 11:22:05 -04002356 || ehci->periodic_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 break;
2358 ehci->next_uframe = now_uframe;
Alan Stern68aa95d2011-10-12 10:39:14 -04002359 now = ehci_read_frame_index(ehci) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (now_uframe == now)
2361 break;
2362
2363 /* rescan the rest of this frame, then ... */
2364 clock = now;
Alan Sternb40e43f2008-05-20 16:59:10 -04002365 clock_frame = clock >> 3;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002366 if (ehci->clock_frame != clock_frame) {
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002367 ehci->clock_frame = clock_frame;
Alan Stern1e12c912011-05-17 10:40:51 -04002368 ++ehci->periodic_stamp;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 } else {
2371 now_uframe++;
Alan Sternbccbefa2010-07-14 11:03:36 -04002372 now_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
David Brownell53bd6a62006-08-30 14:50:06 -07002374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}