blob: 1e4f13c11b6a705f57f5eb3f72efe4c181d59712 [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;
937 stream->refcount = 1;
938 }
939 return stream;
940}
941
942static void
943iso_stream_init (
944 struct ehci_hcd *ehci,
945 struct ehci_iso_stream *stream,
946 struct usb_device *dev,
947 int pipe,
948 unsigned interval
949)
950{
951 static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
952
953 u32 buf1;
954 unsigned epnum, maxp;
955 int is_input;
956 long bandwidth;
957
958 /*
959 * this might be a "high bandwidth" highspeed endpoint,
960 * as encoded in the ep descriptor's wMaxPacket field
961 */
962 epnum = usb_pipeendpoint (pipe);
963 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
964 maxp = usb_maxpacket(dev, pipe, !is_input);
965 if (is_input) {
966 buf1 = (1 << 11);
967 } else {
968 buf1 = 0;
969 }
970
971 /* knows about ITD vs SITD */
972 if (dev->speed == USB_SPEED_HIGH) {
973 unsigned multi = hb_mult(maxp);
974
975 stream->highspeed = 1;
976
977 maxp = max_packet(maxp);
978 buf1 |= maxp;
979 maxp *= multi;
980
Stefan Roese6dbd6822007-05-01 09:29:37 -0700981 stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
982 stream->buf1 = cpu_to_hc32(ehci, buf1);
983 stream->buf2 = cpu_to_hc32(ehci, multi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 /* usbfs wants to report the average usecs per frame tied up
986 * when transfers on this endpoint are scheduled ...
987 */
988 stream->usecs = HS_USECS_ISO (maxp);
989 bandwidth = stream->usecs * 8;
Alan Stern372dd6e2008-11-12 17:02:57 -0500990 bandwidth /= interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 } else {
993 u32 addr;
david-b@pacbell.netd0384202005-08-13 18:44:58 -0700994 int think_time;
Clemens Ladisch469d0222006-01-20 13:49:10 -0800995 int hs_transfers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 addr = dev->ttport << 24;
998 if (!ehci_is_TDI(ehci)
999 || (dev->tt->hub !=
1000 ehci_to_hcd(ehci)->self.root_hub))
1001 addr |= dev->tt->hub->devnum << 16;
1002 addr |= epnum << 8;
1003 addr |= dev->devnum;
1004 stream->usecs = HS_USECS_ISO (maxp);
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001005 think_time = dev->tt ? dev->tt->think_time : 0;
1006 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
1007 dev->speed, is_input, 1, maxp));
Clemens Ladisch469d0222006-01-20 13:49:10 -08001008 hs_transfers = max (1u, (maxp + 187) / 188);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (is_input) {
1010 u32 tmp;
1011
1012 addr |= 1 << 31;
1013 stream->c_usecs = stream->usecs;
1014 stream->usecs = HS_USECS_ISO (1);
1015 stream->raw_mask = 1;
1016
Clemens Ladisch469d0222006-01-20 13:49:10 -08001017 /* c-mask as specified in USB 2.0 11.18.4 3.c */
1018 tmp = (1 << (hs_transfers + 2)) - 1;
1019 stream->raw_mask |= tmp << (8 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 } else
Clemens Ladisch469d0222006-01-20 13:49:10 -08001021 stream->raw_mask = smask_out [hs_transfers - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 bandwidth = stream->usecs + stream->c_usecs;
Alan Stern372dd6e2008-11-12 17:02:57 -05001023 bandwidth /= interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 /* stream->splits gets created from raw_mask later */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001026 stream->address = cpu_to_hc32(ehci, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028 stream->bandwidth = bandwidth;
1029
1030 stream->udev = dev;
1031
1032 stream->bEndpointAddress = is_input | epnum;
1033 stream->interval = interval;
1034 stream->maxp = maxp;
1035}
1036
1037static void
1038iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
1039{
1040 stream->refcount--;
1041
1042 /* free whenever just a dev->ep reference remains.
1043 * not like a QH -- no persistent state (toggle, halt)
1044 */
1045 if (stream->refcount == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 // BUG_ON (!list_empty(&stream->td_list));
1047
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001048 if (stream->ep)
1049 stream->ep->hcpriv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 kfree(stream);
1052 }
1053}
1054
1055static inline struct ehci_iso_stream *
1056iso_stream_get (struct ehci_iso_stream *stream)
1057{
1058 if (likely (stream != NULL))
1059 stream->refcount++;
1060 return stream;
1061}
1062
1063static struct ehci_iso_stream *
1064iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1065{
1066 unsigned epnum;
1067 struct ehci_iso_stream *stream;
1068 struct usb_host_endpoint *ep;
1069 unsigned long flags;
1070
1071 epnum = usb_pipeendpoint (urb->pipe);
1072 if (usb_pipein(urb->pipe))
1073 ep = urb->dev->ep_in[epnum];
1074 else
1075 ep = urb->dev->ep_out[epnum];
1076
1077 spin_lock_irqsave (&ehci->lock, flags);
1078 stream = ep->hcpriv;
1079
1080 if (unlikely (stream == NULL)) {
1081 stream = iso_stream_alloc(GFP_ATOMIC);
1082 if (likely (stream != NULL)) {
1083 /* dev->ep owns the initial refcount */
1084 ep->hcpriv = stream;
1085 stream->ep = ep;
1086 iso_stream_init(ehci, stream, urb->dev, urb->pipe,
1087 urb->interval);
1088 }
1089
Clemens Ladisch1082f572010-03-01 17:18:56 +01001090 /* if dev->ep [epnum] is a QH, hw is set */
1091 } else if (unlikely (stream->hw != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1093 urb->dev->devpath, epnum,
1094 usb_pipein(urb->pipe) ? "in" : "out");
1095 stream = NULL;
1096 }
1097
1098 /* caller guarantees an eventual matching iso_stream_put */
1099 stream = iso_stream_get (stream);
1100
1101 spin_unlock_irqrestore (&ehci->lock, flags);
1102 return stream;
1103}
1104
1105/*-------------------------------------------------------------------------*/
1106
1107/* ehci_iso_sched ops can be ITD-only or SITD-only */
1108
1109static struct ehci_iso_sched *
Al Viro55016f12005-10-21 03:21:58 -04001110iso_sched_alloc (unsigned packets, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 struct ehci_iso_sched *iso_sched;
1113 int size = sizeof *iso_sched;
1114
1115 size += packets * sizeof (struct ehci_iso_packet);
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001116 iso_sched = kzalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (likely (iso_sched != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 INIT_LIST_HEAD (&iso_sched->td_list);
1119 }
1120 return iso_sched;
1121}
1122
1123static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001124itd_sched_init(
1125 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct ehci_iso_sched *iso_sched,
1127 struct ehci_iso_stream *stream,
1128 struct urb *urb
1129)
1130{
1131 unsigned i;
1132 dma_addr_t dma = urb->transfer_dma;
1133
1134 /* how many uframes are needed for these transfers */
1135 iso_sched->span = urb->number_of_packets * stream->interval;
1136
1137 /* figure out per-uframe itd fields that we'll need later
1138 * when we fit new itds into the schedule.
1139 */
1140 for (i = 0; i < urb->number_of_packets; i++) {
1141 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1142 unsigned length;
1143 dma_addr_t buf;
1144 u32 trans;
1145
1146 length = urb->iso_frame_desc [i].length;
1147 buf = dma + urb->iso_frame_desc [i].offset;
1148
1149 trans = EHCI_ISOC_ACTIVE;
1150 trans |= buf & 0x0fff;
1151 if (unlikely (((i + 1) == urb->number_of_packets))
1152 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1153 trans |= EHCI_ITD_IOC;
1154 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001155 uframe->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
David Brownell77078572005-05-28 10:46:18 -07001157 /* might need to cross a buffer page within a uframe */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 uframe->bufp = (buf & ~(u64)0x0fff);
1159 buf += length;
1160 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1161 uframe->cross = 1;
1162 }
1163}
1164
1165static void
1166iso_sched_free (
1167 struct ehci_iso_stream *stream,
1168 struct ehci_iso_sched *iso_sched
1169)
1170{
1171 if (!iso_sched)
1172 return;
1173 // caller must hold ehci->lock!
1174 list_splice (&iso_sched->td_list, &stream->free_list);
1175 kfree (iso_sched);
1176}
1177
1178static int
1179itd_urb_transaction (
1180 struct ehci_iso_stream *stream,
1181 struct ehci_hcd *ehci,
1182 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001183 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184)
1185{
1186 struct ehci_itd *itd;
1187 dma_addr_t itd_dma;
1188 int i;
1189 unsigned num_itds;
1190 struct ehci_iso_sched *sched;
1191 unsigned long flags;
1192
1193 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1194 if (unlikely (sched == NULL))
1195 return -ENOMEM;
1196
Stefan Roese6dbd6822007-05-01 09:29:37 -07001197 itd_sched_init(ehci, sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 if (urb->interval < 8)
1200 num_itds = 1 + (sched->span + 7) / 8;
1201 else
1202 num_itds = urb->number_of_packets;
1203
1204 /* allocate/init ITDs */
1205 spin_lock_irqsave (&ehci->lock, flags);
1206 for (i = 0; i < num_itds; i++) {
1207
Alan Stern55934eb2012-07-11 11:22:35 -04001208 /*
1209 * Use iTDs from the free list, but not iTDs that may
1210 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 */
Alan Stern55934eb2012-07-11 11:22:35 -04001212 if (likely(!list_empty(&stream->free_list))) {
1213 itd = list_first_entry(&stream->free_list,
Stefan Roese6dbd6822007-05-01 09:29:37 -07001214 struct ehci_itd, itd_list);
Alan Stern55934eb2012-07-11 11:22:35 -04001215 if (itd->frame == ehci->clock_frame)
1216 goto alloc_itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 list_del (&itd->itd_list);
1218 itd_dma = itd->itd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001219 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001220 alloc_itd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 spin_unlock_irqrestore (&ehci->lock, flags);
1222 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1223 &itd_dma);
1224 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001225 if (!itd) {
1226 iso_sched_free(stream, sched);
1227 spin_unlock_irqrestore(&ehci->lock, flags);
1228 return -ENOMEM;
1229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 memset (itd, 0, sizeof *itd);
1233 itd->itd_dma = itd_dma;
1234 list_add (&itd->itd_list, &sched->td_list);
1235 }
1236 spin_unlock_irqrestore (&ehci->lock, flags);
1237
1238 /* temporarily store schedule info in hcpriv */
1239 urb->hcpriv = sched;
1240 urb->error_count = 0;
1241 return 0;
1242}
1243
1244/*-------------------------------------------------------------------------*/
1245
1246static inline int
1247itd_slot_ok (
1248 struct ehci_hcd *ehci,
1249 u32 mod,
1250 u32 uframe,
1251 u8 usecs,
1252 u32 period
1253)
1254{
1255 uframe %= period;
1256 do {
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001257 /* can't commit more than uframe_periodic_max usec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001259 > (ehci->uframe_periodic_max - usecs))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return 0;
1261
1262 /* we know urb->interval is 2^N uframes */
1263 uframe += period;
1264 } while (uframe < mod);
1265 return 1;
1266}
1267
1268static inline int
1269sitd_slot_ok (
1270 struct ehci_hcd *ehci,
1271 u32 mod,
1272 struct ehci_iso_stream *stream,
1273 u32 uframe,
1274 struct ehci_iso_sched *sched,
1275 u32 period_uframes
1276)
1277{
1278 u32 mask, tmp;
1279 u32 frame, uf;
1280
1281 mask = stream->raw_mask << (uframe & 7);
1282
1283 /* for IN, don't wrap CSPLIT into the next frame */
1284 if (mask & ~0xffff)
1285 return 0;
1286
Alan Stern65b8e5c2012-05-14 13:47:20 -04001287 /* check bandwidth */
1288 uframe %= period_uframes;
1289 frame = uframe >> 3;
1290
1291#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1292 /* The tt's fullspeed bus bandwidth must be available.
1293 * tt_available scheduling guarantees 10+% for control/bulk.
1294 */
1295 uf = uframe & 7;
1296 if (!tt_available(ehci, period_uframes >> 3,
1297 stream->udev, frame, uf, stream->tt_usecs))
1298 return 0;
1299#else
1300 /* tt must be idle for start(s), any gap, and csplit.
1301 * assume scheduling slop leaves 10+% for control/bulk.
1302 */
1303 if (!tt_no_collision(ehci, period_uframes >> 3,
1304 stream->udev, frame, mask))
1305 return 0;
1306#endif
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /* this multi-pass logic is simple, but performance may
1309 * suffer when the schedule data isn't cached.
1310 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 do {
1312 u32 max_used;
1313
1314 frame = uframe >> 3;
1315 uf = uframe & 7;
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /* check starts (OUT uses more than one) */
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001318 max_used = ehci->uframe_periodic_max - stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
1320 if (periodic_usecs (ehci, frame, uf) > max_used)
1321 return 0;
1322 }
1323
1324 /* for IN, check CSPLIT */
1325 if (stream->c_usecs) {
Clemens Ladisch0c734622006-01-22 10:32:49 -08001326 uf = uframe & 7;
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001327 max_used = ehci->uframe_periodic_max - stream->c_usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 do {
1329 tmp = 1 << uf;
1330 tmp <<= 8;
1331 if ((stream->raw_mask & tmp) == 0)
1332 continue;
1333 if (periodic_usecs (ehci, frame, uf)
1334 > max_used)
1335 return 0;
1336 } while (++uf < 8);
1337 }
1338
1339 /* we know urb->interval is 2^N uframes */
1340 uframe += period_uframes;
1341 } while (uframe < mod);
1342
Stefan Roese6dbd6822007-05-01 09:29:37 -07001343 stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return 1;
1345}
1346
1347/*
1348 * This scheduler plans almost as far into the future as it has actual
1349 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1350 * "as small as possible" to be cache-friendlier.) That limits the size
1351 * transfers you can stream reliably; avoid more than 64 msec per urb.
1352 * Also avoid queue depths of less than ehci's worst irq latency (affected
1353 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1354 * and other factors); or more than about 230 msec total (for portability,
1355 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1356 */
1357
Sarah Sharpd7e055f2009-10-27 10:54:49 -07001358#define SCHEDULE_SLOP 80 /* microframes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360static int
1361iso_stream_schedule (
1362 struct ehci_hcd *ehci,
1363 struct urb *urb,
1364 struct ehci_iso_stream *stream
1365)
1366{
Alan Sternffda0802010-07-14 11:03:46 -04001367 u32 now, next, start, period, span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 int status;
1369 unsigned mod = ehci->periodic_size << 3;
1370 struct ehci_iso_sched *sched = urb->hcpriv;
1371
Alan Sternffda0802010-07-14 11:03:46 -04001372 period = urb->interval;
1373 span = sched->span;
1374 if (!stream->highspeed) {
1375 period <<= 3;
1376 span <<= 3;
1377 }
1378
1379 if (span > mod - SCHEDULE_SLOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 ehci_dbg (ehci, "iso request %p too long\n", urb);
1381 status = -EFBIG;
1382 goto fail;
1383 }
1384
Alan Stern68aa95d2011-10-12 10:39:14 -04001385 now = ehci_read_frame_index(ehci) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Alan Sternb40e43f2008-05-20 16:59:10 -04001387 /* Typical case: reuse current schedule, stream is still active.
1388 * Hopefully there are no gaps from the host falling behind
1389 * (irq delays etc), but if there are we'll take the next
1390 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 */
1392 if (likely (!list_empty (&stream->td_list))) {
Alan Stern1fb2e052010-07-14 11:03:53 -04001393 u32 excess;
Sarah Sharpdccd5742009-10-27 10:55:05 -07001394
1395 /* For high speed devices, allow scheduling within the
Alan Sternae68a832010-07-14 11:03:23 -04001396 * isochronous scheduling threshold. For full speed devices
1397 * and Intel PCI-based controllers, don't (work around for
1398 * Intel ICH9 bug).
Sarah Sharpdccd5742009-10-27 10:55:05 -07001399 */
Alan Sternae68a832010-07-14 11:03:23 -04001400 if (!stream->highspeed && ehci->fs_i_thresh)
Sarah Sharpdccd5742009-10-27 10:55:05 -07001401 next = now + ehci->i_thresh;
1402 else
1403 next = now;
Alan Sternb40e43f2008-05-20 16:59:10 -04001404
Alan Stern1fb2e052010-07-14 11:03:53 -04001405 /* Fell behind (by up to twice the slop amount)?
1406 * We decide based on the time of the last currently-scheduled
1407 * slot, not the time of the next available slot.
1408 */
1409 excess = (stream->next_uframe - period - next) & (mod - 1);
1410 if (excess >= mod - 2 * SCHEDULE_SLOP)
1411 start = next + excess - mod + period *
1412 DIV_ROUND_UP(mod - excess, period);
1413 else
1414 start = next + excess + period;
1415 if (start - now >= mod) {
1416 ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
1417 urb, start - now - period, period,
1418 mod);
Alan Sternb40e43f2008-05-20 16:59:10 -04001419 status = -EFBIG;
1420 goto fail;
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
1424 /* need to schedule; when's the next (u)frame we could start?
1425 * this is bigger than ehci->i_thresh allows; scheduling itself
1426 * isn't free, the slop should handle reasonably slow cpus. it
1427 * can also help high bandwidth if the dma and irq loads don't
1428 * jump until after the queue is primed.
1429 */
Alan Stern1fb2e052010-07-14 11:03:53 -04001430 else {
Matthieu CASTETe3420902011-11-28 11:30:22 +01001431 int done = 0;
Alan Stern1fb2e052010-07-14 11:03:53 -04001432 start = SCHEDULE_SLOP + (now & ~0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Alan Stern1fb2e052010-07-14 11:03:53 -04001434 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Thomas Poussevin811c9262011-10-27 18:46:48 +02001436 /* find a uframe slot with enough bandwidth.
1437 * Early uframes are more precious because full-speed
1438 * iso IN transfers can't use late uframes,
1439 * and therefore they should be allocated last.
1440 */
1441 next = start;
1442 start += period;
1443 do {
1444 start--;
Alan Stern1fb2e052010-07-14 11:03:53 -04001445 /* check schedule: enough space? */
1446 if (stream->highspeed) {
1447 if (itd_slot_ok(ehci, mod, start,
1448 stream->usecs, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001449 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001450 } else {
1451 if ((start % 8) >= 6)
1452 continue;
1453 if (sitd_slot_ok(ehci, mod, stream,
1454 start, sched, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001455 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001456 }
Matthieu CASTETe3420902011-11-28 11:30:22 +01001457 } while (start > next && !done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Alan Stern1fb2e052010-07-14 11:03:53 -04001459 /* no room in the schedule */
Matthieu CASTETe3420902011-11-28 11:30:22 +01001460 if (!done) {
Alan Stern1fb2e052010-07-14 11:03:53 -04001461 ehci_dbg(ehci, "iso resched full %p (now %d max %d)\n",
1462 urb, now, now + mod);
1463 status = -ENOSPC;
1464 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466 }
1467
Alan Stern1fb2e052010-07-14 11:03:53 -04001468 /* Tried to schedule too far into the future? */
1469 if (unlikely(start - now + span - period
1470 >= mod - 2 * SCHEDULE_SLOP)) {
1471 ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
1472 urb, start - now, span - period,
1473 mod - 2 * SCHEDULE_SLOP);
1474 status = -EFBIG;
1475 goto fail;
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Alan Stern1fb2e052010-07-14 11:03:53 -04001478 stream->next_uframe = start & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /* report high speed start in uframes; full speed, in frames */
1481 urb->start_frame = stream->next_uframe;
1482 if (!stream->highspeed)
1483 urb->start_frame >>= 3;
1484 return 0;
Alan Stern1fb2e052010-07-14 11:03:53 -04001485
1486 fail:
1487 iso_sched_free(stream, sched);
1488 urb->hcpriv = NULL;
1489 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492/*-------------------------------------------------------------------------*/
1493
1494static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001495itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1496 struct ehci_itd *itd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 int i;
1499
David Brownell77078572005-05-28 10:46:18 -07001500 /* it's been recently zeroed */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001501 itd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 itd->hw_bufp [0] = stream->buf0;
1503 itd->hw_bufp [1] = stream->buf1;
1504 itd->hw_bufp [2] = stream->buf2;
1505
1506 for (i = 0; i < 8; i++)
1507 itd->index[i] = -1;
1508
1509 /* All other fields are filled when scheduling */
1510}
1511
1512static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001513itd_patch(
1514 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 struct ehci_itd *itd,
1516 struct ehci_iso_sched *iso_sched,
1517 unsigned index,
David Brownell77078572005-05-28 10:46:18 -07001518 u16 uframe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519)
1520{
1521 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1522 unsigned pg = itd->pg;
1523
1524 // BUG_ON (pg == 6 && uf->cross);
1525
1526 uframe &= 0x07;
1527 itd->index [uframe] = index;
1528
Stefan Roese6dbd6822007-05-01 09:29:37 -07001529 itd->hw_transaction[uframe] = uf->transaction;
1530 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1531 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1532 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 /* iso_frame_desc[].offset must be strictly increasing */
David Brownell77078572005-05-28 10:46:18 -07001535 if (unlikely (uf->cross)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 u64 bufp = uf->bufp + 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 itd->pg = ++pg;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001539 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1540 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542}
1543
1544static inline void
1545itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1546{
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001547 union ehci_shadow *prev = &ehci->pshadow[frame];
1548 __hc32 *hw_p = &ehci->periodic[frame];
1549 union ehci_shadow here = *prev;
1550 __hc32 type = 0;
1551
1552 /* skip any iso nodes which might belong to previous microframes */
1553 while (here.ptr) {
1554 type = Q_NEXT_TYPE(ehci, *hw_p);
1555 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
1556 break;
1557 prev = periodic_next_shadow(ehci, prev, type);
1558 hw_p = shadow_next_periodic(ehci, &here, type);
1559 here = *prev;
1560 }
1561
1562 itd->itd_next = here;
1563 itd->hw_next = *hw_p;
1564 prev->itd = itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 itd->frame = frame;
1566 wmb ();
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001567 *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570/* fit urb's itds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001571static void itd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 struct ehci_hcd *ehci,
1573 struct urb *urb,
1574 unsigned mod,
1575 struct ehci_iso_stream *stream
1576)
1577{
David Brownell77078572005-05-28 10:46:18 -07001578 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 unsigned next_uframe, uframe, frame;
1580 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1581 struct ehci_itd *itd;
1582
Alan Sternbccbefa2010-07-14 11:03:36 -04001583 next_uframe = stream->next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 if (unlikely (list_empty(&stream->td_list))) {
1586 ehci_to_hcd(ehci)->self.bandwidth_allocated
1587 += stream->bandwidth;
1588 ehci_vdbg (ehci,
1589 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1590 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1591 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1592 urb->interval,
1593 next_uframe >> 3, next_uframe & 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Alex He05570292010-12-07 10:10:08 +08001595
1596 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001597 if (ehci->amd_pll_fix == 1)
1598 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08001599 }
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1602
1603 /* fill iTDs uframe by uframe */
1604 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1605 if (itd == NULL) {
1606 /* ASSERT: we have all necessary itds */
1607 // BUG_ON (list_empty (&iso_sched->td_list));
1608
1609 /* ASSERT: no itds for this endpoint in this uframe */
1610
1611 itd = list_entry (iso_sched->td_list.next,
1612 struct ehci_itd, itd_list);
1613 list_move_tail (&itd->itd_list, &stream->td_list);
1614 itd->stream = iso_stream_get (stream);
Karsten Wiese508db8c2009-02-26 01:47:48 +01001615 itd->urb = urb;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001616 itd_init (ehci, stream, itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618
1619 uframe = next_uframe & 0x07;
1620 frame = next_uframe >> 3;
1621
Stefan Roese6dbd6822007-05-01 09:29:37 -07001622 itd_patch(ehci, itd, iso_sched, packet, uframe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 next_uframe += stream->interval;
Alan Sternbccbefa2010-07-14 11:03:36 -04001625 next_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 packet++;
1627
1628 /* link completed itds into the schedule */
1629 if (((next_uframe >> 3) != frame)
1630 || packet == urb->number_of_packets) {
Alan Sternbccbefa2010-07-14 11:03:36 -04001631 itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 itd = NULL;
1633 }
1634 }
1635 stream->next_uframe = next_uframe;
1636
1637 /* don't need that schedule data any more */
1638 iso_sched_free (stream, iso_sched);
1639 urb->hcpriv = NULL;
1640
1641 timer_action (ehci, TIMER_IO_WATCHDOG);
Alan Sternb015cb72012-07-11 11:22:10 -04001642 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
1645#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1646
David Brownell30bf54e2007-12-16 22:37:40 -08001647/* Process and recycle a completed ITD. Return true iff its urb completed,
1648 * and hence its completion callback probably added things to the hardware
1649 * schedule.
1650 *
1651 * Note that we carefully avoid recycling this descriptor until after any
1652 * completion callback runs, so that it won't be reused quickly. That is,
1653 * assuming (a) no more than two urbs per frame on this endpoint, and also
1654 * (b) only this endpoint's completions submit URBs. It seems some silicon
1655 * corrupts things if you reuse completed descriptors very quickly...
1656 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657static unsigned
1658itd_complete (
1659 struct ehci_hcd *ehci,
David Howells7d12e782006-10-05 14:55:46 +01001660 struct ehci_itd *itd
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661) {
1662 struct urb *urb = itd->urb;
1663 struct usb_iso_packet_descriptor *desc;
1664 u32 t;
1665 unsigned uframe;
1666 int urb_index = -1;
1667 struct ehci_iso_stream *stream = itd->stream;
1668 struct usb_device *dev;
David Brownell30bf54e2007-12-16 22:37:40 -08001669 unsigned retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 /* for each uframe with a packet */
1672 for (uframe = 0; uframe < 8; uframe++) {
1673 if (likely (itd->index[uframe] == -1))
1674 continue;
1675 urb_index = itd->index[uframe];
1676 desc = &urb->iso_frame_desc [urb_index];
1677
Stefan Roese6dbd6822007-05-01 09:29:37 -07001678 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 itd->hw_transaction [uframe] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /* report transfer status */
1682 if (unlikely (t & ISO_ERRS)) {
1683 urb->error_count++;
1684 if (t & EHCI_ISOC_BUF_ERR)
1685 desc->status = usb_pipein (urb->pipe)
1686 ? -ENOSR /* hc couldn't read */
1687 : -ECOMM; /* hc couldn't write */
1688 else if (t & EHCI_ISOC_BABBLE)
1689 desc->status = -EOVERFLOW;
1690 else /* (t & EHCI_ISOC_XACTERR) */
1691 desc->status = -EPROTO;
1692
1693 /* HC need not update length with this error */
Alan Sternec6d67e2009-06-29 14:34:59 -04001694 if (!(t & EHCI_ISOC_BABBLE)) {
1695 desc->actual_length = EHCI_ITD_LENGTH(t);
1696 urb->actual_length += desc->actual_length;
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1699 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04001700 desc->actual_length = EHCI_ITD_LENGTH(t);
1701 urb->actual_length += desc->actual_length;
Alan Sternb40e43f2008-05-20 16:59:10 -04001702 } else {
1703 /* URB was too late */
1704 desc->status = -EXDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706 }
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 /* handle completion now? */
1709 if (likely ((urb_index + 1) != urb->number_of_packets))
David Brownell30bf54e2007-12-16 22:37:40 -08001710 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 /* ASSERT: it's really the last itd for this urb
1713 list_for_each_entry (itd, &stream->td_list, itd_list)
1714 BUG_ON (itd->urb == urb);
1715 */
1716
David Brownellaa16ca32007-12-30 23:45:19 -08001717 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05001718 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04001719 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08001720 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 urb = NULL;
Alan Sternb015cb72012-07-11 11:22:10 -04001722 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
1724
Alex He05570292010-12-07 10:10:08 +08001725 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001726 if (ehci->amd_pll_fix == 1)
1727 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08001728 }
1729
Karsten Wiese508db8c2009-02-26 01:47:48 +01001730 if (unlikely(list_is_singular(&stream->td_list))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 ehci_to_hcd(ehci)->self.bandwidth_allocated
1732 -= stream->bandwidth;
1733 ehci_vdbg (ehci,
1734 "deschedule devp %s ep%d%s-iso\n",
1735 dev->devpath, stream->bEndpointAddress & 0x0f,
1736 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1737 }
1738 iso_stream_put (ehci, stream);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001739
David Brownell30bf54e2007-12-16 22:37:40 -08001740done:
David Brownell30bf54e2007-12-16 22:37:40 -08001741 itd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04001742
1743 /* Add to the end of the free list for later reuse */
1744 list_move_tail(&itd->itd_list, &stream->free_list);
1745
1746 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
1747 if (list_empty(&stream->td_list)) {
1748 list_splice_tail_init(&stream->free_list,
1749 &ehci->cached_itd_list);
1750 start_free_itds(ehci);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001751 }
Alan Stern55934eb2012-07-11 11:22:35 -04001752
1753 iso_stream_put(ehci, stream);
David Brownell30bf54e2007-12-16 22:37:40 -08001754 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
1757/*-------------------------------------------------------------------------*/
1758
Olav Kongas5db539e2005-06-23 20:25:36 +03001759static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001760 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 int status = -EINVAL;
1763 unsigned long flags;
1764 struct ehci_iso_stream *stream;
1765
1766 /* Get iso_stream head */
1767 stream = iso_stream_find (ehci, urb);
1768 if (unlikely (stream == NULL)) {
1769 ehci_dbg (ehci, "can't get iso stream\n");
1770 return -ENOMEM;
1771 }
1772 if (unlikely (urb->interval != stream->interval)) {
1773 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1774 stream->interval, urb->interval);
1775 goto done;
1776 }
1777
1778#ifdef EHCI_URB_TRACE
1779 ehci_dbg (ehci,
1780 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001781 __func__, urb->dev->devpath, urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 usb_pipeendpoint (urb->pipe),
1783 usb_pipein (urb->pipe) ? "in" : "out",
1784 urb->transfer_buffer_length,
1785 urb->number_of_packets, urb->interval,
1786 stream);
1787#endif
1788
1789 /* allocate ITDs w/o locking anything */
1790 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1791 if (unlikely (status < 0)) {
1792 ehci_dbg (ehci, "can't init itds\n");
1793 goto done;
1794 }
1795
1796 /* schedule ... need to lock */
1797 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001798 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001799 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04001800 goto done_not_linked;
1801 }
1802 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1803 if (unlikely(status))
1804 goto done_not_linked;
1805 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07001806 if (likely (status == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04001808 else
1809 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
1810done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 spin_unlock_irqrestore (&ehci->lock, flags);
1812
1813done:
1814 if (unlikely (status < 0))
1815 iso_stream_put (ehci, stream);
1816 return status;
1817}
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819/*-------------------------------------------------------------------------*/
1820
1821/*
1822 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1823 * TTs in USB 2.0 hubs. These need microframe scheduling.
1824 */
1825
1826static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001827sitd_sched_init(
1828 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct ehci_iso_sched *iso_sched,
1830 struct ehci_iso_stream *stream,
1831 struct urb *urb
1832)
1833{
1834 unsigned i;
1835 dma_addr_t dma = urb->transfer_dma;
1836
1837 /* how many frames are needed for these transfers */
1838 iso_sched->span = urb->number_of_packets * stream->interval;
1839
1840 /* figure out per-frame sitd fields that we'll need later
1841 * when we fit new sitds into the schedule.
1842 */
1843 for (i = 0; i < urb->number_of_packets; i++) {
1844 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1845 unsigned length;
1846 dma_addr_t buf;
1847 u32 trans;
1848
1849 length = urb->iso_frame_desc [i].length & 0x03ff;
1850 buf = dma + urb->iso_frame_desc [i].offset;
1851
1852 trans = SITD_STS_ACTIVE;
1853 if (((i + 1) == urb->number_of_packets)
1854 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1855 trans |= SITD_IOC;
1856 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001857 packet->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 /* might need to cross a buffer page within a td */
1860 packet->bufp = buf;
1861 packet->buf1 = (buf + length) & ~0x0fff;
1862 if (packet->buf1 != (buf & ~(u64)0x0fff))
1863 packet->cross = 1;
1864
David Brownell53bd6a62006-08-30 14:50:06 -07001865 /* OUT uses multiple start-splits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (stream->bEndpointAddress & USB_DIR_IN)
1867 continue;
1868 length = (length + 187) / 188;
1869 if (length > 1) /* BEGIN vs ALL */
1870 length |= 1 << 3;
1871 packet->buf1 |= length;
1872 }
1873}
1874
1875static int
1876sitd_urb_transaction (
1877 struct ehci_iso_stream *stream,
1878 struct ehci_hcd *ehci,
1879 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001880 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881)
1882{
1883 struct ehci_sitd *sitd;
1884 dma_addr_t sitd_dma;
1885 int i;
1886 struct ehci_iso_sched *iso_sched;
1887 unsigned long flags;
1888
1889 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1890 if (iso_sched == NULL)
1891 return -ENOMEM;
1892
Stefan Roese6dbd6822007-05-01 09:29:37 -07001893 sitd_sched_init(ehci, iso_sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 /* allocate/init sITDs */
1896 spin_lock_irqsave (&ehci->lock, flags);
1897 for (i = 0; i < urb->number_of_packets; i++) {
1898
1899 /* NOTE: for now, we don't try to handle wraparound cases
1900 * for IN (using sitd->hw_backpointer, like a FSTN), which
1901 * means we never need two sitds for full speed packets.
1902 */
1903
Alan Stern55934eb2012-07-11 11:22:35 -04001904 /*
1905 * Use siTDs from the free list, but not siTDs that may
1906 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 */
Alan Stern55934eb2012-07-11 11:22:35 -04001908 if (likely(!list_empty(&stream->free_list))) {
1909 sitd = list_first_entry(&stream->free_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 struct ehci_sitd, sitd_list);
Alan Stern55934eb2012-07-11 11:22:35 -04001911 if (sitd->frame == ehci->clock_frame)
1912 goto alloc_sitd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 list_del (&sitd->sitd_list);
1914 sitd_dma = sitd->sitd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001915 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001916 alloc_sitd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 spin_unlock_irqrestore (&ehci->lock, flags);
1918 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1919 &sitd_dma);
1920 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001921 if (!sitd) {
1922 iso_sched_free(stream, iso_sched);
1923 spin_unlock_irqrestore(&ehci->lock, flags);
1924 return -ENOMEM;
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 memset (sitd, 0, sizeof *sitd);
1929 sitd->sitd_dma = sitd_dma;
1930 list_add (&sitd->sitd_list, &iso_sched->td_list);
1931 }
1932
1933 /* temporarily store schedule info in hcpriv */
1934 urb->hcpriv = iso_sched;
1935 urb->error_count = 0;
1936
1937 spin_unlock_irqrestore (&ehci->lock, flags);
1938 return 0;
1939}
1940
1941/*-------------------------------------------------------------------------*/
1942
1943static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001944sitd_patch(
1945 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 struct ehci_iso_stream *stream,
1947 struct ehci_sitd *sitd,
1948 struct ehci_iso_sched *iso_sched,
1949 unsigned index
1950)
1951{
1952 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1953 u64 bufp = uf->bufp;
1954
Stefan Roese6dbd6822007-05-01 09:29:37 -07001955 sitd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 sitd->hw_fullspeed_ep = stream->address;
1957 sitd->hw_uframe = stream->splits;
1958 sitd->hw_results = uf->transaction;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001959 sitd->hw_backpointer = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 bufp = uf->bufp;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001962 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
1963 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Stefan Roese6dbd6822007-05-01 09:29:37 -07001965 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (uf->cross)
1967 bufp += 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001968 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 sitd->index = index;
1970}
1971
1972static inline void
1973sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
1974{
1975 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1976 sitd->sitd_next = ehci->pshadow [frame];
1977 sitd->hw_next = ehci->periodic [frame];
1978 ehci->pshadow [frame].sitd = sitd;
1979 sitd->frame = frame;
1980 wmb ();
Stefan Roese6dbd6822007-05-01 09:29:37 -07001981 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
1984/* fit urb's sitds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001985static void sitd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 struct ehci_hcd *ehci,
1987 struct urb *urb,
1988 unsigned mod,
1989 struct ehci_iso_stream *stream
1990)
1991{
1992 int packet;
1993 unsigned next_uframe;
1994 struct ehci_iso_sched *sched = urb->hcpriv;
1995 struct ehci_sitd *sitd;
1996
1997 next_uframe = stream->next_uframe;
1998
1999 if (list_empty(&stream->td_list)) {
2000 /* usbfs ignores TT bandwidth */
2001 ehci_to_hcd(ehci)->self.bandwidth_allocated
2002 += stream->bandwidth;
2003 ehci_vdbg (ehci,
2004 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
2005 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
2006 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
Alan Sternbccbefa2010-07-14 11:03:36 -04002007 (next_uframe >> 3) & (ehci->periodic_size - 1),
Stefan Roese6dbd6822007-05-01 09:29:37 -07002008 stream->interval, hc32_to_cpu(ehci, stream->splits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Alex He05570292010-12-07 10:10:08 +08002010
2011 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002012 if (ehci->amd_pll_fix == 1)
2013 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08002014 }
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
2017
2018 /* fill sITDs frame by frame */
2019 for (packet = 0, sitd = NULL;
2020 packet < urb->number_of_packets;
2021 packet++) {
2022
2023 /* ASSERT: we have all necessary sitds */
2024 BUG_ON (list_empty (&sched->td_list));
2025
2026 /* ASSERT: no itds for this endpoint in this frame */
2027
2028 sitd = list_entry (sched->td_list.next,
2029 struct ehci_sitd, sitd_list);
2030 list_move_tail (&sitd->sitd_list, &stream->td_list);
2031 sitd->stream = iso_stream_get (stream);
Karsten Wiese508db8c2009-02-26 01:47:48 +01002032 sitd->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Stefan Roese6dbd6822007-05-01 09:29:37 -07002034 sitd_patch(ehci, stream, sitd, sched, packet);
Alan Sternbccbefa2010-07-14 11:03:36 -04002035 sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 sitd);
2037
2038 next_uframe += stream->interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
Alan Sternbccbefa2010-07-14 11:03:36 -04002040 stream->next_uframe = next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 /* don't need that schedule data any more */
2043 iso_sched_free (stream, sched);
2044 urb->hcpriv = NULL;
2045
2046 timer_action (ehci, TIMER_IO_WATCHDOG);
Alan Sternb015cb72012-07-11 11:22:10 -04002047 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
2050/*-------------------------------------------------------------------------*/
2051
2052#define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
David Brownell53bd6a62006-08-30 14:50:06 -07002053 | SITD_STS_XACT | SITD_STS_MMF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
David Brownell30bf54e2007-12-16 22:37:40 -08002055/* Process and recycle a completed SITD. Return true iff its urb completed,
2056 * and hence its completion callback probably added things to the hardware
2057 * schedule.
2058 *
2059 * Note that we carefully avoid recycling this descriptor until after any
2060 * completion callback runs, so that it won't be reused quickly. That is,
2061 * assuming (a) no more than two urbs per frame on this endpoint, and also
2062 * (b) only this endpoint's completions submit URBs. It seems some silicon
2063 * corrupts things if you reuse completed descriptors very quickly...
2064 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065static unsigned
2066sitd_complete (
2067 struct ehci_hcd *ehci,
David Howells7d12e782006-10-05 14:55:46 +01002068 struct ehci_sitd *sitd
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069) {
2070 struct urb *urb = sitd->urb;
2071 struct usb_iso_packet_descriptor *desc;
2072 u32 t;
2073 int urb_index = -1;
2074 struct ehci_iso_stream *stream = sitd->stream;
2075 struct usb_device *dev;
David Brownell30bf54e2007-12-16 22:37:40 -08002076 unsigned retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 urb_index = sitd->index;
2079 desc = &urb->iso_frame_desc [urb_index];
Stefan Roese6dbd6822007-05-01 09:29:37 -07002080 t = hc32_to_cpup(ehci, &sitd->hw_results);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /* report transfer status */
2083 if (t & SITD_ERRS) {
2084 urb->error_count++;
2085 if (t & SITD_STS_DBE)
2086 desc->status = usb_pipein (urb->pipe)
2087 ? -ENOSR /* hc couldn't read */
2088 : -ECOMM; /* hc couldn't write */
2089 else if (t & SITD_STS_BABBLE)
2090 desc->status = -EOVERFLOW;
2091 else /* XACT, MMF, etc */
2092 desc->status = -EPROTO;
2093 } else {
2094 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04002095 desc->actual_length = desc->length - SITD_LENGTH(t);
2096 urb->actual_length += desc->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 /* handle completion now? */
2100 if ((urb_index + 1) != urb->number_of_packets)
David Brownell30bf54e2007-12-16 22:37:40 -08002101 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 /* ASSERT: it's really the last sitd for this urb
2104 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2105 BUG_ON (sitd->urb == urb);
2106 */
2107
David Brownellaa16ca32007-12-30 23:45:19 -08002108 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05002109 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04002110 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08002111 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 urb = NULL;
Alan Sternb015cb72012-07-11 11:22:10 -04002113 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
2115
Alex He05570292010-12-07 10:10:08 +08002116 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002117 if (ehci->amd_pll_fix == 1)
2118 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08002119 }
2120
Karsten Wiese508db8c2009-02-26 01:47:48 +01002121 if (list_is_singular(&stream->td_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 ehci_to_hcd(ehci)->self.bandwidth_allocated
2123 -= stream->bandwidth;
2124 ehci_vdbg (ehci,
2125 "deschedule devp %s ep%d%s-iso\n",
2126 dev->devpath, stream->bEndpointAddress & 0x0f,
2127 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
2128 }
2129 iso_stream_put (ehci, stream);
Alan Stern0e5f2312010-04-08 16:56:37 -04002130
David Brownell30bf54e2007-12-16 22:37:40 -08002131done:
David Brownell30bf54e2007-12-16 22:37:40 -08002132 sitd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04002133
2134 /* Add to the end of the free list for later reuse */
2135 list_move_tail(&sitd->sitd_list, &stream->free_list);
2136
2137 /* Recycle the siTDs when the pipeline is empty (ep no longer in use) */
2138 if (list_empty(&stream->td_list)) {
2139 list_splice_tail_init(&stream->free_list,
2140 &ehci->cached_sitd_list);
2141 start_free_itds(ehci);
Alan Stern0e5f2312010-04-08 16:56:37 -04002142 }
Alan Stern55934eb2012-07-11 11:22:35 -04002143
2144 iso_stream_put(ehci, stream);
David Brownell30bf54e2007-12-16 22:37:40 -08002145 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148
Olav Kongas5db539e2005-06-23 20:25:36 +03002149static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04002150 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 int status = -EINVAL;
2153 unsigned long flags;
2154 struct ehci_iso_stream *stream;
2155
2156 /* Get iso_stream head */
2157 stream = iso_stream_find (ehci, urb);
2158 if (stream == NULL) {
2159 ehci_dbg (ehci, "can't get iso stream\n");
2160 return -ENOMEM;
2161 }
2162 if (urb->interval != stream->interval) {
2163 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
2164 stream->interval, urb->interval);
2165 goto done;
2166 }
2167
2168#ifdef EHCI_URB_TRACE
2169 ehci_dbg (ehci,
2170 "submit %p dev%s ep%d%s-iso len %d\n",
2171 urb, urb->dev->devpath,
2172 usb_pipeendpoint (urb->pipe),
2173 usb_pipein (urb->pipe) ? "in" : "out",
2174 urb->transfer_buffer_length);
2175#endif
2176
2177 /* allocate SITDs */
2178 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2179 if (status < 0) {
2180 ehci_dbg (ehci, "can't init sitds\n");
2181 goto done;
2182 }
2183
2184 /* schedule ... need to lock */
2185 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04002186 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002187 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04002188 goto done_not_linked;
2189 }
2190 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2191 if (unlikely(status))
2192 goto done_not_linked;
2193 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07002194 if (status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04002196 else
2197 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
2198done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 spin_unlock_irqrestore (&ehci->lock, flags);
2200
2201done:
2202 if (status < 0)
2203 iso_stream_put (ehci, stream);
2204 return status;
2205}
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/*-------------------------------------------------------------------------*/
2208
2209static void
David Howells7d12e782006-10-05 14:55:46 +01002210scan_periodic (struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Alan Sternb40e43f2008-05-20 16:59:10 -04002212 unsigned now_uframe, frame, clock, clock_frame, mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 unsigned modified;
2214
2215 mod = ehci->periodic_size << 3;
2216
2217 /*
2218 * When running, scan from last scan point up to "now"
2219 * else clean up by scanning everything that's left.
2220 * Touches as few pages as possible: cache-friendly.
2221 */
2222 now_uframe = ehci->next_uframe;
Alan Sternc0c53db2012-07-11 11:21:48 -04002223 if (ehci->rh_state >= EHCI_RH_RUNNING) {
Alan Stern68aa95d2011-10-12 10:39:14 -04002224 clock = ehci_read_frame_index(ehci);
Alan Sternbccbefa2010-07-14 11:03:36 -04002225 clock_frame = (clock >> 3) & (ehci->periodic_size - 1);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002226 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 clock = now_uframe + mod - 1;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002228 clock_frame = -1;
2229 }
Alan Stern55934eb2012-07-11 11:22:35 -04002230 ehci->clock_frame = clock_frame;
Alan Sternbccbefa2010-07-14 11:03:36 -04002231 clock &= mod - 1;
Alan Sternb40e43f2008-05-20 16:59:10 -04002232 clock_frame = clock >> 3;
Alan Stern1e12c912011-05-17 10:40:51 -04002233 ++ehci->periodic_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 for (;;) {
2236 union ehci_shadow q, *q_p;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002237 __hc32 type, *hw_p;
David Brownell79592b722008-01-07 00:47:42 -08002238 unsigned incomplete = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 frame = now_uframe >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242restart:
2243 /* scan each element in frame's queue for completions */
2244 q_p = &ehci->pshadow [frame];
2245 hw_p = &ehci->periodic [frame];
2246 q.ptr = q_p->ptr;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002247 type = Q_NEXT_TYPE(ehci, *hw_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 modified = 0;
2249
2250 while (q.ptr != NULL) {
2251 unsigned uf;
2252 union ehci_shadow temp;
2253 int live;
2254
Alan Sternc0c53db2012-07-11 11:21:48 -04002255 live = (ehci->rh_state >= EHCI_RH_RUNNING);
Stefan Roese6dbd6822007-05-01 09:29:37 -07002256 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 case Q_TYPE_QH:
2258 /* handle any completions */
Alan Sternc83e1a92012-07-11 11:21:25 -04002259 temp.qh = q.qh;
Alek Du3807e262009-07-14 07:23:29 +08002260 type = Q_NEXT_TYPE(ehci, q.qh->hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 q = q.qh->qh_next;
Alan Stern1e12c912011-05-17 10:40:51 -04002262 if (temp.qh->stamp != ehci->periodic_stamp) {
2263 modified = qh_completions(ehci, temp.qh);
2264 if (!modified)
2265 temp.qh->stamp = ehci->periodic_stamp;
2266 if (unlikely(list_empty(&temp.qh->qtd_list) ||
2267 temp.qh->needs_rescan))
Alan Sterndf202252012-07-11 11:22:26 -04002268 start_unlink_intr(ehci, temp.qh);
Alan Stern1e12c912011-05-17 10:40:51 -04002269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 break;
2271 case Q_TYPE_FSTN:
2272 /* for "save place" FSTNs, look at QH entries
2273 * in the previous frame for completions.
2274 */
Stefan Roese6dbd6822007-05-01 09:29:37 -07002275 if (q.fstn->hw_prev != EHCI_LIST_END(ehci)) {
Greg Kroah-Hartman2d0fe1b2012-05-01 21:33:36 -07002276 ehci_dbg(ehci,
2277 "ignoring completions from FSTNs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
Stefan Roese6dbd6822007-05-01 09:29:37 -07002279 type = Q_NEXT_TYPE(ehci, q.fstn->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 q = q.fstn->fstn_next;
2281 break;
2282 case Q_TYPE_ITD:
David Brownell79592b722008-01-07 00:47:42 -08002283 /* If this ITD is still active, leave it for
2284 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002285 * No need to check for activity unless the
2286 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002287 */
Alan Sternb40e43f2008-05-20 16:59:10 -04002288 if (frame == clock_frame && live) {
2289 rmb();
2290 for (uf = 0; uf < 8; uf++) {
2291 if (q.itd->hw_transaction[uf] &
2292 ITD_ACTIVE(ehci))
2293 break;
2294 }
2295 if (uf < 8) {
2296 incomplete = true;
2297 q_p = &q.itd->itd_next;
2298 hw_p = &q.itd->hw_next;
2299 type = Q_NEXT_TYPE(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -07002300 q.itd->hw_next);
Alan Sternb40e43f2008-05-20 16:59:10 -04002301 q = *q_p;
2302 break;
2303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
David Brownell79592b722008-01-07 00:47:42 -08002306 /* Take finished ITDs out of the schedule
2307 * and process them: recycle, maybe report
2308 * URB completion. HC won't cache the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 * pointer for much longer, if at all.
2310 */
2311 *q_p = q.itd->itd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002312 if (!ehci->use_dummy_qh ||
2313 q.itd->hw_next != EHCI_LIST_END(ehci))
2314 *hw_p = q.itd->hw_next;
2315 else
2316 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002317 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002319 modified = itd_complete (ehci, q.itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 q = *q_p;
2321 break;
2322 case Q_TYPE_SITD:
David Brownell79592b722008-01-07 00:47:42 -08002323 /* If this SITD is still active, leave it for
2324 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002325 * No need to check for activity unless the
2326 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002327 */
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002328 if (((frame == clock_frame) ||
Alan Sternbccbefa2010-07-14 11:03:36 -04002329 (((frame + 1) & (ehci->periodic_size - 1))
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002330 == clock_frame))
2331 && live
2332 && (q.sitd->hw_results &
2333 SITD_ACTIVE(ehci))) {
2334
David Brownell79592b722008-01-07 00:47:42 -08002335 incomplete = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 q_p = &q.sitd->sitd_next;
2337 hw_p = &q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002338 type = Q_NEXT_TYPE(ehci,
2339 q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 q = *q_p;
2341 break;
2342 }
David Brownell79592b722008-01-07 00:47:42 -08002343
2344 /* Take finished SITDs out of the schedule
2345 * and process them: recycle, maybe report
2346 * URB completion.
2347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 *q_p = q.sitd->sitd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002349 if (!ehci->use_dummy_qh ||
2350 q.sitd->hw_next != EHCI_LIST_END(ehci))
2351 *hw_p = q.sitd->hw_next;
2352 else
2353 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002354 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002356 modified = sitd_complete (ehci, q.sitd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 q = *q_p;
2358 break;
2359 default:
Greg Kroah-Hartman2d0fe1b2012-05-01 21:33:36 -07002360 ehci_dbg(ehci, "corrupt type %d frame %d shadow %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 type, frame, q.ptr);
2362 // BUG ();
2363 q.ptr = NULL;
2364 }
2365
2366 /* assume completion callbacks modify the queue */
David Brownellaa16ca32007-12-30 23:45:19 -08002367 if (unlikely (modified)) {
Alan Stern3ca9aeb2012-07-11 11:22:05 -04002368 if (likely(ehci->periodic_count > 0))
David Brownellaa16ca32007-12-30 23:45:19 -08002369 goto restart;
David Brownell01c17142008-08-26 23:35:04 -07002370 /* short-circuit this scan */
David Brownellaa16ca32007-12-30 23:45:19 -08002371 now_uframe = clock;
2372 break;
2373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375
David Brownell79592b722008-01-07 00:47:42 -08002376 /* If we can tell we caught up to the hardware, stop now.
2377 * We can't advance our scan without collecting the ISO
2378 * transfers that are still pending in this frame.
2379 */
Alan Sternc0c53db2012-07-11 11:21:48 -04002380 if (incomplete && ehci->rh_state >= EHCI_RH_RUNNING) {
David Brownell79592b722008-01-07 00:47:42 -08002381 ehci->next_uframe = now_uframe;
2382 break;
2383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 // FIXME: this assumes we won't get lapped when
2386 // latencies climb; that should be rare, but...
2387 // detect it, and just go all the way around.
2388 // FLR might help detect this case, so long as latencies
2389 // don't exceed periodic_size msec (default 1.024 sec).
2390
2391 // FIXME: likewise assumes HC doesn't halt mid-scan
2392
2393 if (now_uframe == clock) {
2394 unsigned now;
2395
Alan Sternc0c53db2012-07-11 11:21:48 -04002396 if (ehci->rh_state < EHCI_RH_RUNNING
Alan Stern3ca9aeb2012-07-11 11:22:05 -04002397 || ehci->periodic_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 break;
2399 ehci->next_uframe = now_uframe;
Alan Stern68aa95d2011-10-12 10:39:14 -04002400 now = ehci_read_frame_index(ehci) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (now_uframe == now)
2402 break;
2403
2404 /* rescan the rest of this frame, then ... */
2405 clock = now;
Alan Sternb40e43f2008-05-20 16:59:10 -04002406 clock_frame = clock >> 3;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002407 if (ehci->clock_frame != clock_frame) {
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002408 ehci->clock_frame = clock_frame;
Alan Stern1e12c912011-05-17 10:40:51 -04002409 ++ehci->periodic_stamp;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 } else {
2412 now_uframe++;
Alan Sternbccbefa2010-07-14 11:03:36 -04002413 now_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
David Brownell53bd6a62006-08-30 14:50:06 -07002415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}