blob: eec8446f8dedb12ba15af39bfb2421d33a90c65f [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
1048 while (!list_empty (&stream->free_list)) {
1049 struct list_head *entry;
1050
1051 entry = stream->free_list.next;
1052 list_del (entry);
1053
1054 /* knows about ITD vs SITD */
1055 if (stream->highspeed) {
1056 struct ehci_itd *itd;
1057
1058 itd = list_entry (entry, struct ehci_itd,
1059 itd_list);
1060 dma_pool_free (ehci->itd_pool, itd,
1061 itd->itd_dma);
1062 } else {
1063 struct ehci_sitd *sitd;
1064
1065 sitd = list_entry (entry, struct ehci_sitd,
1066 sitd_list);
1067 dma_pool_free (ehci->sitd_pool, sitd,
1068 sitd->sitd_dma);
1069 }
1070 }
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 stream->bEndpointAddress &= 0x0f;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001073 if (stream->ep)
1074 stream->ep->hcpriv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 kfree(stream);
1077 }
1078}
1079
1080static inline struct ehci_iso_stream *
1081iso_stream_get (struct ehci_iso_stream *stream)
1082{
1083 if (likely (stream != NULL))
1084 stream->refcount++;
1085 return stream;
1086}
1087
1088static struct ehci_iso_stream *
1089iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1090{
1091 unsigned epnum;
1092 struct ehci_iso_stream *stream;
1093 struct usb_host_endpoint *ep;
1094 unsigned long flags;
1095
1096 epnum = usb_pipeendpoint (urb->pipe);
1097 if (usb_pipein(urb->pipe))
1098 ep = urb->dev->ep_in[epnum];
1099 else
1100 ep = urb->dev->ep_out[epnum];
1101
1102 spin_lock_irqsave (&ehci->lock, flags);
1103 stream = ep->hcpriv;
1104
1105 if (unlikely (stream == NULL)) {
1106 stream = iso_stream_alloc(GFP_ATOMIC);
1107 if (likely (stream != NULL)) {
1108 /* dev->ep owns the initial refcount */
1109 ep->hcpriv = stream;
1110 stream->ep = ep;
1111 iso_stream_init(ehci, stream, urb->dev, urb->pipe,
1112 urb->interval);
1113 }
1114
Clemens Ladisch1082f572010-03-01 17:18:56 +01001115 /* if dev->ep [epnum] is a QH, hw is set */
1116 } else if (unlikely (stream->hw != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1118 urb->dev->devpath, epnum,
1119 usb_pipein(urb->pipe) ? "in" : "out");
1120 stream = NULL;
1121 }
1122
1123 /* caller guarantees an eventual matching iso_stream_put */
1124 stream = iso_stream_get (stream);
1125
1126 spin_unlock_irqrestore (&ehci->lock, flags);
1127 return stream;
1128}
1129
1130/*-------------------------------------------------------------------------*/
1131
1132/* ehci_iso_sched ops can be ITD-only or SITD-only */
1133
1134static struct ehci_iso_sched *
Al Viro55016f12005-10-21 03:21:58 -04001135iso_sched_alloc (unsigned packets, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct ehci_iso_sched *iso_sched;
1138 int size = sizeof *iso_sched;
1139
1140 size += packets * sizeof (struct ehci_iso_packet);
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001141 iso_sched = kzalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (likely (iso_sched != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 INIT_LIST_HEAD (&iso_sched->td_list);
1144 }
1145 return iso_sched;
1146}
1147
1148static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001149itd_sched_init(
1150 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 struct ehci_iso_sched *iso_sched,
1152 struct ehci_iso_stream *stream,
1153 struct urb *urb
1154)
1155{
1156 unsigned i;
1157 dma_addr_t dma = urb->transfer_dma;
1158
1159 /* how many uframes are needed for these transfers */
1160 iso_sched->span = urb->number_of_packets * stream->interval;
1161
1162 /* figure out per-uframe itd fields that we'll need later
1163 * when we fit new itds into the schedule.
1164 */
1165 for (i = 0; i < urb->number_of_packets; i++) {
1166 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1167 unsigned length;
1168 dma_addr_t buf;
1169 u32 trans;
1170
1171 length = urb->iso_frame_desc [i].length;
1172 buf = dma + urb->iso_frame_desc [i].offset;
1173
1174 trans = EHCI_ISOC_ACTIVE;
1175 trans |= buf & 0x0fff;
1176 if (unlikely (((i + 1) == urb->number_of_packets))
1177 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1178 trans |= EHCI_ITD_IOC;
1179 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001180 uframe->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
David Brownell77078572005-05-28 10:46:18 -07001182 /* might need to cross a buffer page within a uframe */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 uframe->bufp = (buf & ~(u64)0x0fff);
1184 buf += length;
1185 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1186 uframe->cross = 1;
1187 }
1188}
1189
1190static void
1191iso_sched_free (
1192 struct ehci_iso_stream *stream,
1193 struct ehci_iso_sched *iso_sched
1194)
1195{
1196 if (!iso_sched)
1197 return;
1198 // caller must hold ehci->lock!
1199 list_splice (&iso_sched->td_list, &stream->free_list);
1200 kfree (iso_sched);
1201}
1202
1203static int
1204itd_urb_transaction (
1205 struct ehci_iso_stream *stream,
1206 struct ehci_hcd *ehci,
1207 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001208 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209)
1210{
1211 struct ehci_itd *itd;
1212 dma_addr_t itd_dma;
1213 int i;
1214 unsigned num_itds;
1215 struct ehci_iso_sched *sched;
1216 unsigned long flags;
1217
1218 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1219 if (unlikely (sched == NULL))
1220 return -ENOMEM;
1221
Stefan Roese6dbd6822007-05-01 09:29:37 -07001222 itd_sched_init(ehci, sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (urb->interval < 8)
1225 num_itds = 1 + (sched->span + 7) / 8;
1226 else
1227 num_itds = urb->number_of_packets;
1228
1229 /* allocate/init ITDs */
1230 spin_lock_irqsave (&ehci->lock, flags);
1231 for (i = 0; i < num_itds; i++) {
1232
1233 /* free_list.next might be cache-hot ... but maybe
1234 * the HC caches it too. avoid that issue for now.
1235 */
1236
1237 /* prefer previously-allocated itds */
1238 if (likely (!list_empty(&stream->free_list))) {
1239 itd = list_entry (stream->free_list.prev,
Stefan Roese6dbd6822007-05-01 09:29:37 -07001240 struct ehci_itd, itd_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 list_del (&itd->itd_list);
1242 itd_dma = itd->itd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001243 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 spin_unlock_irqrestore (&ehci->lock, flags);
1245 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1246 &itd_dma);
1247 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001248 if (!itd) {
1249 iso_sched_free(stream, sched);
1250 spin_unlock_irqrestore(&ehci->lock, flags);
1251 return -ENOMEM;
1252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 memset (itd, 0, sizeof *itd);
1256 itd->itd_dma = itd_dma;
1257 list_add (&itd->itd_list, &sched->td_list);
1258 }
1259 spin_unlock_irqrestore (&ehci->lock, flags);
1260
1261 /* temporarily store schedule info in hcpriv */
1262 urb->hcpriv = sched;
1263 urb->error_count = 0;
1264 return 0;
1265}
1266
1267/*-------------------------------------------------------------------------*/
1268
1269static inline int
1270itd_slot_ok (
1271 struct ehci_hcd *ehci,
1272 u32 mod,
1273 u32 uframe,
1274 u8 usecs,
1275 u32 period
1276)
1277{
1278 uframe %= period;
1279 do {
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001280 /* can't commit more than uframe_periodic_max usec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001282 > (ehci->uframe_periodic_max - usecs))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return 0;
1284
1285 /* we know urb->interval is 2^N uframes */
1286 uframe += period;
1287 } while (uframe < mod);
1288 return 1;
1289}
1290
1291static inline int
1292sitd_slot_ok (
1293 struct ehci_hcd *ehci,
1294 u32 mod,
1295 struct ehci_iso_stream *stream,
1296 u32 uframe,
1297 struct ehci_iso_sched *sched,
1298 u32 period_uframes
1299)
1300{
1301 u32 mask, tmp;
1302 u32 frame, uf;
1303
1304 mask = stream->raw_mask << (uframe & 7);
1305
1306 /* for IN, don't wrap CSPLIT into the next frame */
1307 if (mask & ~0xffff)
1308 return 0;
1309
Alan Stern65b8e5c2012-05-14 13:47:20 -04001310 /* check bandwidth */
1311 uframe %= period_uframes;
1312 frame = uframe >> 3;
1313
1314#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1315 /* The tt's fullspeed bus bandwidth must be available.
1316 * tt_available scheduling guarantees 10+% for control/bulk.
1317 */
1318 uf = uframe & 7;
1319 if (!tt_available(ehci, period_uframes >> 3,
1320 stream->udev, frame, uf, stream->tt_usecs))
1321 return 0;
1322#else
1323 /* tt must be idle for start(s), any gap, and csplit.
1324 * assume scheduling slop leaves 10+% for control/bulk.
1325 */
1326 if (!tt_no_collision(ehci, period_uframes >> 3,
1327 stream->udev, frame, mask))
1328 return 0;
1329#endif
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /* this multi-pass logic is simple, but performance may
1332 * suffer when the schedule data isn't cached.
1333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 do {
1335 u32 max_used;
1336
1337 frame = uframe >> 3;
1338 uf = uframe & 7;
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /* check starts (OUT uses more than one) */
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001341 max_used = ehci->uframe_periodic_max - stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
1343 if (periodic_usecs (ehci, frame, uf) > max_used)
1344 return 0;
1345 }
1346
1347 /* for IN, check CSPLIT */
1348 if (stream->c_usecs) {
Clemens Ladisch0c734622006-01-22 10:32:49 -08001349 uf = uframe & 7;
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001350 max_used = ehci->uframe_periodic_max - stream->c_usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 do {
1352 tmp = 1 << uf;
1353 tmp <<= 8;
1354 if ((stream->raw_mask & tmp) == 0)
1355 continue;
1356 if (periodic_usecs (ehci, frame, uf)
1357 > max_used)
1358 return 0;
1359 } while (++uf < 8);
1360 }
1361
1362 /* we know urb->interval is 2^N uframes */
1363 uframe += period_uframes;
1364 } while (uframe < mod);
1365
Stefan Roese6dbd6822007-05-01 09:29:37 -07001366 stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return 1;
1368}
1369
1370/*
1371 * This scheduler plans almost as far into the future as it has actual
1372 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1373 * "as small as possible" to be cache-friendlier.) That limits the size
1374 * transfers you can stream reliably; avoid more than 64 msec per urb.
1375 * Also avoid queue depths of less than ehci's worst irq latency (affected
1376 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1377 * and other factors); or more than about 230 msec total (for portability,
1378 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1379 */
1380
Sarah Sharpd7e055f2009-10-27 10:54:49 -07001381#define SCHEDULE_SLOP 80 /* microframes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383static int
1384iso_stream_schedule (
1385 struct ehci_hcd *ehci,
1386 struct urb *urb,
1387 struct ehci_iso_stream *stream
1388)
1389{
Alan Sternffda0802010-07-14 11:03:46 -04001390 u32 now, next, start, period, span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int status;
1392 unsigned mod = ehci->periodic_size << 3;
1393 struct ehci_iso_sched *sched = urb->hcpriv;
1394
Alan Sternffda0802010-07-14 11:03:46 -04001395 period = urb->interval;
1396 span = sched->span;
1397 if (!stream->highspeed) {
1398 period <<= 3;
1399 span <<= 3;
1400 }
1401
1402 if (span > mod - SCHEDULE_SLOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 ehci_dbg (ehci, "iso request %p too long\n", urb);
1404 status = -EFBIG;
1405 goto fail;
1406 }
1407
Alan Stern68aa95d2011-10-12 10:39:14 -04001408 now = ehci_read_frame_index(ehci) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Alan Sternb40e43f2008-05-20 16:59:10 -04001410 /* Typical case: reuse current schedule, stream is still active.
1411 * Hopefully there are no gaps from the host falling behind
1412 * (irq delays etc), but if there are we'll take the next
1413 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
1415 if (likely (!list_empty (&stream->td_list))) {
Alan Stern1fb2e052010-07-14 11:03:53 -04001416 u32 excess;
Sarah Sharpdccd5742009-10-27 10:55:05 -07001417
1418 /* For high speed devices, allow scheduling within the
Alan Sternae68a832010-07-14 11:03:23 -04001419 * isochronous scheduling threshold. For full speed devices
1420 * and Intel PCI-based controllers, don't (work around for
1421 * Intel ICH9 bug).
Sarah Sharpdccd5742009-10-27 10:55:05 -07001422 */
Alan Sternae68a832010-07-14 11:03:23 -04001423 if (!stream->highspeed && ehci->fs_i_thresh)
Sarah Sharpdccd5742009-10-27 10:55:05 -07001424 next = now + ehci->i_thresh;
1425 else
1426 next = now;
Alan Sternb40e43f2008-05-20 16:59:10 -04001427
Alan Stern1fb2e052010-07-14 11:03:53 -04001428 /* Fell behind (by up to twice the slop amount)?
1429 * We decide based on the time of the last currently-scheduled
1430 * slot, not the time of the next available slot.
1431 */
1432 excess = (stream->next_uframe - period - next) & (mod - 1);
1433 if (excess >= mod - 2 * SCHEDULE_SLOP)
1434 start = next + excess - mod + period *
1435 DIV_ROUND_UP(mod - excess, period);
1436 else
1437 start = next + excess + period;
1438 if (start - now >= mod) {
1439 ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
1440 urb, start - now - period, period,
1441 mod);
Alan Sternb40e43f2008-05-20 16:59:10 -04001442 status = -EFBIG;
1443 goto fail;
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446
1447 /* need to schedule; when's the next (u)frame we could start?
1448 * this is bigger than ehci->i_thresh allows; scheduling itself
1449 * isn't free, the slop should handle reasonably slow cpus. it
1450 * can also help high bandwidth if the dma and irq loads don't
1451 * jump until after the queue is primed.
1452 */
Alan Stern1fb2e052010-07-14 11:03:53 -04001453 else {
Matthieu CASTETe3420902011-11-28 11:30:22 +01001454 int done = 0;
Alan Stern1fb2e052010-07-14 11:03:53 -04001455 start = SCHEDULE_SLOP + (now & ~0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Alan Stern1fb2e052010-07-14 11:03:53 -04001457 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Thomas Poussevin811c9262011-10-27 18:46:48 +02001459 /* find a uframe slot with enough bandwidth.
1460 * Early uframes are more precious because full-speed
1461 * iso IN transfers can't use late uframes,
1462 * and therefore they should be allocated last.
1463 */
1464 next = start;
1465 start += period;
1466 do {
1467 start--;
Alan Stern1fb2e052010-07-14 11:03:53 -04001468 /* check schedule: enough space? */
1469 if (stream->highspeed) {
1470 if (itd_slot_ok(ehci, mod, start,
1471 stream->usecs, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001472 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001473 } else {
1474 if ((start % 8) >= 6)
1475 continue;
1476 if (sitd_slot_ok(ehci, mod, stream,
1477 start, sched, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001478 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001479 }
Matthieu CASTETe3420902011-11-28 11:30:22 +01001480 } while (start > next && !done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Alan Stern1fb2e052010-07-14 11:03:53 -04001482 /* no room in the schedule */
Matthieu CASTETe3420902011-11-28 11:30:22 +01001483 if (!done) {
Alan Stern1fb2e052010-07-14 11:03:53 -04001484 ehci_dbg(ehci, "iso resched full %p (now %d max %d)\n",
1485 urb, now, now + mod);
1486 status = -ENOSPC;
1487 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489 }
1490
Alan Stern1fb2e052010-07-14 11:03:53 -04001491 /* Tried to schedule too far into the future? */
1492 if (unlikely(start - now + span - period
1493 >= mod - 2 * SCHEDULE_SLOP)) {
1494 ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
1495 urb, start - now, span - period,
1496 mod - 2 * SCHEDULE_SLOP);
1497 status = -EFBIG;
1498 goto fail;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Alan Stern1fb2e052010-07-14 11:03:53 -04001501 stream->next_uframe = start & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /* report high speed start in uframes; full speed, in frames */
1504 urb->start_frame = stream->next_uframe;
1505 if (!stream->highspeed)
1506 urb->start_frame >>= 3;
1507 return 0;
Alan Stern1fb2e052010-07-14 11:03:53 -04001508
1509 fail:
1510 iso_sched_free(stream, sched);
1511 urb->hcpriv = NULL;
1512 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515/*-------------------------------------------------------------------------*/
1516
1517static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001518itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1519 struct ehci_itd *itd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 int i;
1522
David Brownell77078572005-05-28 10:46:18 -07001523 /* it's been recently zeroed */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001524 itd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 itd->hw_bufp [0] = stream->buf0;
1526 itd->hw_bufp [1] = stream->buf1;
1527 itd->hw_bufp [2] = stream->buf2;
1528
1529 for (i = 0; i < 8; i++)
1530 itd->index[i] = -1;
1531
1532 /* All other fields are filled when scheduling */
1533}
1534
1535static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001536itd_patch(
1537 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 struct ehci_itd *itd,
1539 struct ehci_iso_sched *iso_sched,
1540 unsigned index,
David Brownell77078572005-05-28 10:46:18 -07001541 u16 uframe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542)
1543{
1544 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1545 unsigned pg = itd->pg;
1546
1547 // BUG_ON (pg == 6 && uf->cross);
1548
1549 uframe &= 0x07;
1550 itd->index [uframe] = index;
1551
Stefan Roese6dbd6822007-05-01 09:29:37 -07001552 itd->hw_transaction[uframe] = uf->transaction;
1553 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1554 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1555 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 /* iso_frame_desc[].offset must be strictly increasing */
David Brownell77078572005-05-28 10:46:18 -07001558 if (unlikely (uf->cross)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 u64 bufp = uf->bufp + 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 itd->pg = ++pg;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001562 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1563 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565}
1566
1567static inline void
1568itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1569{
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001570 union ehci_shadow *prev = &ehci->pshadow[frame];
1571 __hc32 *hw_p = &ehci->periodic[frame];
1572 union ehci_shadow here = *prev;
1573 __hc32 type = 0;
1574
1575 /* skip any iso nodes which might belong to previous microframes */
1576 while (here.ptr) {
1577 type = Q_NEXT_TYPE(ehci, *hw_p);
1578 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
1579 break;
1580 prev = periodic_next_shadow(ehci, prev, type);
1581 hw_p = shadow_next_periodic(ehci, &here, type);
1582 here = *prev;
1583 }
1584
1585 itd->itd_next = here;
1586 itd->hw_next = *hw_p;
1587 prev->itd = itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 itd->frame = frame;
1589 wmb ();
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001590 *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
1593/* fit urb's itds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001594static void itd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 struct ehci_hcd *ehci,
1596 struct urb *urb,
1597 unsigned mod,
1598 struct ehci_iso_stream *stream
1599)
1600{
David Brownell77078572005-05-28 10:46:18 -07001601 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 unsigned next_uframe, uframe, frame;
1603 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1604 struct ehci_itd *itd;
1605
Alan Sternbccbefa2010-07-14 11:03:36 -04001606 next_uframe = stream->next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 if (unlikely (list_empty(&stream->td_list))) {
1609 ehci_to_hcd(ehci)->self.bandwidth_allocated
1610 += stream->bandwidth;
1611 ehci_vdbg (ehci,
1612 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1613 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1614 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1615 urb->interval,
1616 next_uframe >> 3, next_uframe & 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Alex He05570292010-12-07 10:10:08 +08001618
1619 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001620 if (ehci->amd_pll_fix == 1)
1621 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08001622 }
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1625
1626 /* fill iTDs uframe by uframe */
1627 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1628 if (itd == NULL) {
1629 /* ASSERT: we have all necessary itds */
1630 // BUG_ON (list_empty (&iso_sched->td_list));
1631
1632 /* ASSERT: no itds for this endpoint in this uframe */
1633
1634 itd = list_entry (iso_sched->td_list.next,
1635 struct ehci_itd, itd_list);
1636 list_move_tail (&itd->itd_list, &stream->td_list);
1637 itd->stream = iso_stream_get (stream);
Karsten Wiese508db8c2009-02-26 01:47:48 +01001638 itd->urb = urb;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001639 itd_init (ehci, stream, itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641
1642 uframe = next_uframe & 0x07;
1643 frame = next_uframe >> 3;
1644
Stefan Roese6dbd6822007-05-01 09:29:37 -07001645 itd_patch(ehci, itd, iso_sched, packet, uframe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 next_uframe += stream->interval;
Alan Sternbccbefa2010-07-14 11:03:36 -04001648 next_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 packet++;
1650
1651 /* link completed itds into the schedule */
1652 if (((next_uframe >> 3) != frame)
1653 || packet == urb->number_of_packets) {
Alan Sternbccbefa2010-07-14 11:03:36 -04001654 itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 itd = NULL;
1656 }
1657 }
1658 stream->next_uframe = next_uframe;
1659
1660 /* don't need that schedule data any more */
1661 iso_sched_free (stream, iso_sched);
1662 urb->hcpriv = NULL;
1663
1664 timer_action (ehci, TIMER_IO_WATCHDOG);
Alan Sternb015cb72012-07-11 11:22:10 -04001665 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
1668#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1669
David Brownell30bf54e2007-12-16 22:37:40 -08001670/* Process and recycle a completed ITD. Return true iff its urb completed,
1671 * and hence its completion callback probably added things to the hardware
1672 * schedule.
1673 *
1674 * Note that we carefully avoid recycling this descriptor until after any
1675 * completion callback runs, so that it won't be reused quickly. That is,
1676 * assuming (a) no more than two urbs per frame on this endpoint, and also
1677 * (b) only this endpoint's completions submit URBs. It seems some silicon
1678 * corrupts things if you reuse completed descriptors very quickly...
1679 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680static unsigned
1681itd_complete (
1682 struct ehci_hcd *ehci,
David Howells7d12e782006-10-05 14:55:46 +01001683 struct ehci_itd *itd
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684) {
1685 struct urb *urb = itd->urb;
1686 struct usb_iso_packet_descriptor *desc;
1687 u32 t;
1688 unsigned uframe;
1689 int urb_index = -1;
1690 struct ehci_iso_stream *stream = itd->stream;
1691 struct usb_device *dev;
David Brownell30bf54e2007-12-16 22:37:40 -08001692 unsigned retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 /* for each uframe with a packet */
1695 for (uframe = 0; uframe < 8; uframe++) {
1696 if (likely (itd->index[uframe] == -1))
1697 continue;
1698 urb_index = itd->index[uframe];
1699 desc = &urb->iso_frame_desc [urb_index];
1700
Stefan Roese6dbd6822007-05-01 09:29:37 -07001701 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 itd->hw_transaction [uframe] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 /* report transfer status */
1705 if (unlikely (t & ISO_ERRS)) {
1706 urb->error_count++;
1707 if (t & EHCI_ISOC_BUF_ERR)
1708 desc->status = usb_pipein (urb->pipe)
1709 ? -ENOSR /* hc couldn't read */
1710 : -ECOMM; /* hc couldn't write */
1711 else if (t & EHCI_ISOC_BABBLE)
1712 desc->status = -EOVERFLOW;
1713 else /* (t & EHCI_ISOC_XACTERR) */
1714 desc->status = -EPROTO;
1715
1716 /* HC need not update length with this error */
Alan Sternec6d67e2009-06-29 14:34:59 -04001717 if (!(t & EHCI_ISOC_BABBLE)) {
1718 desc->actual_length = EHCI_ITD_LENGTH(t);
1719 urb->actual_length += desc->actual_length;
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1722 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04001723 desc->actual_length = EHCI_ITD_LENGTH(t);
1724 urb->actual_length += desc->actual_length;
Alan Sternb40e43f2008-05-20 16:59:10 -04001725 } else {
1726 /* URB was too late */
1727 desc->status = -EXDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729 }
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /* handle completion now? */
1732 if (likely ((urb_index + 1) != urb->number_of_packets))
David Brownell30bf54e2007-12-16 22:37:40 -08001733 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 /* ASSERT: it's really the last itd for this urb
1736 list_for_each_entry (itd, &stream->td_list, itd_list)
1737 BUG_ON (itd->urb == urb);
1738 */
1739
David Brownellaa16ca32007-12-30 23:45:19 -08001740 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05001741 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04001742 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08001743 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 urb = NULL;
Alan Sternb015cb72012-07-11 11:22:10 -04001745 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
1747
Alex He05570292010-12-07 10:10:08 +08001748 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001749 if (ehci->amd_pll_fix == 1)
1750 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08001751 }
1752
Karsten Wiese508db8c2009-02-26 01:47:48 +01001753 if (unlikely(list_is_singular(&stream->td_list))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 ehci_to_hcd(ehci)->self.bandwidth_allocated
1755 -= stream->bandwidth;
1756 ehci_vdbg (ehci,
1757 "deschedule devp %s ep%d%s-iso\n",
1758 dev->devpath, stream->bEndpointAddress & 0x0f,
1759 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1760 }
1761 iso_stream_put (ehci, stream);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001762
David Brownell30bf54e2007-12-16 22:37:40 -08001763done:
David Brownell30bf54e2007-12-16 22:37:40 -08001764 itd->urb = NULL;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001765 if (ehci->clock_frame != itd->frame || itd->index[7] != -1) {
1766 /* OK to recycle this ITD now. */
1767 itd->stream = NULL;
1768 list_move(&itd->itd_list, &stream->free_list);
1769 iso_stream_put(ehci, stream);
1770 } else {
1771 /* HW might remember this ITD, so we can't recycle it yet.
1772 * Move it to a safe place until a new frame starts.
1773 */
1774 list_move(&itd->itd_list, &ehci->cached_itd_list);
1775 if (stream->refcount == 2) {
1776 /* If iso_stream_put() were called here, stream
1777 * would be freed. Instead, just prevent reuse.
1778 */
1779 stream->ep->hcpriv = NULL;
1780 stream->ep = NULL;
1781 }
1782 }
David Brownell30bf54e2007-12-16 22:37:40 -08001783 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
1786/*-------------------------------------------------------------------------*/
1787
Olav Kongas5db539e2005-06-23 20:25:36 +03001788static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001789 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 int status = -EINVAL;
1792 unsigned long flags;
1793 struct ehci_iso_stream *stream;
1794
1795 /* Get iso_stream head */
1796 stream = iso_stream_find (ehci, urb);
1797 if (unlikely (stream == NULL)) {
1798 ehci_dbg (ehci, "can't get iso stream\n");
1799 return -ENOMEM;
1800 }
1801 if (unlikely (urb->interval != stream->interval)) {
1802 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1803 stream->interval, urb->interval);
1804 goto done;
1805 }
1806
1807#ifdef EHCI_URB_TRACE
1808 ehci_dbg (ehci,
1809 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001810 __func__, urb->dev->devpath, urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 usb_pipeendpoint (urb->pipe),
1812 usb_pipein (urb->pipe) ? "in" : "out",
1813 urb->transfer_buffer_length,
1814 urb->number_of_packets, urb->interval,
1815 stream);
1816#endif
1817
1818 /* allocate ITDs w/o locking anything */
1819 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1820 if (unlikely (status < 0)) {
1821 ehci_dbg (ehci, "can't init itds\n");
1822 goto done;
1823 }
1824
1825 /* schedule ... need to lock */
1826 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001827 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001828 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04001829 goto done_not_linked;
1830 }
1831 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1832 if (unlikely(status))
1833 goto done_not_linked;
1834 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07001835 if (likely (status == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04001837 else
1838 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
1839done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 spin_unlock_irqrestore (&ehci->lock, flags);
1841
1842done:
1843 if (unlikely (status < 0))
1844 iso_stream_put (ehci, stream);
1845 return status;
1846}
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848/*-------------------------------------------------------------------------*/
1849
1850/*
1851 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1852 * TTs in USB 2.0 hubs. These need microframe scheduling.
1853 */
1854
1855static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001856sitd_sched_init(
1857 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct ehci_iso_sched *iso_sched,
1859 struct ehci_iso_stream *stream,
1860 struct urb *urb
1861)
1862{
1863 unsigned i;
1864 dma_addr_t dma = urb->transfer_dma;
1865
1866 /* how many frames are needed for these transfers */
1867 iso_sched->span = urb->number_of_packets * stream->interval;
1868
1869 /* figure out per-frame sitd fields that we'll need later
1870 * when we fit new sitds into the schedule.
1871 */
1872 for (i = 0; i < urb->number_of_packets; i++) {
1873 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1874 unsigned length;
1875 dma_addr_t buf;
1876 u32 trans;
1877
1878 length = urb->iso_frame_desc [i].length & 0x03ff;
1879 buf = dma + urb->iso_frame_desc [i].offset;
1880
1881 trans = SITD_STS_ACTIVE;
1882 if (((i + 1) == urb->number_of_packets)
1883 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1884 trans |= SITD_IOC;
1885 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001886 packet->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 /* might need to cross a buffer page within a td */
1889 packet->bufp = buf;
1890 packet->buf1 = (buf + length) & ~0x0fff;
1891 if (packet->buf1 != (buf & ~(u64)0x0fff))
1892 packet->cross = 1;
1893
David Brownell53bd6a62006-08-30 14:50:06 -07001894 /* OUT uses multiple start-splits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (stream->bEndpointAddress & USB_DIR_IN)
1896 continue;
1897 length = (length + 187) / 188;
1898 if (length > 1) /* BEGIN vs ALL */
1899 length |= 1 << 3;
1900 packet->buf1 |= length;
1901 }
1902}
1903
1904static int
1905sitd_urb_transaction (
1906 struct ehci_iso_stream *stream,
1907 struct ehci_hcd *ehci,
1908 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001909 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910)
1911{
1912 struct ehci_sitd *sitd;
1913 dma_addr_t sitd_dma;
1914 int i;
1915 struct ehci_iso_sched *iso_sched;
1916 unsigned long flags;
1917
1918 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1919 if (iso_sched == NULL)
1920 return -ENOMEM;
1921
Stefan Roese6dbd6822007-05-01 09:29:37 -07001922 sitd_sched_init(ehci, iso_sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 /* allocate/init sITDs */
1925 spin_lock_irqsave (&ehci->lock, flags);
1926 for (i = 0; i < urb->number_of_packets; i++) {
1927
1928 /* NOTE: for now, we don't try to handle wraparound cases
1929 * for IN (using sitd->hw_backpointer, like a FSTN), which
1930 * means we never need two sitds for full speed packets.
1931 */
1932
1933 /* free_list.next might be cache-hot ... but maybe
1934 * the HC caches it too. avoid that issue for now.
1935 */
1936
1937 /* prefer previously-allocated sitds */
1938 if (!list_empty(&stream->free_list)) {
1939 sitd = list_entry (stream->free_list.prev,
1940 struct ehci_sitd, sitd_list);
1941 list_del (&sitd->sitd_list);
1942 sitd_dma = sitd->sitd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001943 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 spin_unlock_irqrestore (&ehci->lock, flags);
1945 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1946 &sitd_dma);
1947 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001948 if (!sitd) {
1949 iso_sched_free(stream, iso_sched);
1950 spin_unlock_irqrestore(&ehci->lock, flags);
1951 return -ENOMEM;
1952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 memset (sitd, 0, sizeof *sitd);
1956 sitd->sitd_dma = sitd_dma;
1957 list_add (&sitd->sitd_list, &iso_sched->td_list);
1958 }
1959
1960 /* temporarily store schedule info in hcpriv */
1961 urb->hcpriv = iso_sched;
1962 urb->error_count = 0;
1963
1964 spin_unlock_irqrestore (&ehci->lock, flags);
1965 return 0;
1966}
1967
1968/*-------------------------------------------------------------------------*/
1969
1970static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001971sitd_patch(
1972 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 struct ehci_iso_stream *stream,
1974 struct ehci_sitd *sitd,
1975 struct ehci_iso_sched *iso_sched,
1976 unsigned index
1977)
1978{
1979 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1980 u64 bufp = uf->bufp;
1981
Stefan Roese6dbd6822007-05-01 09:29:37 -07001982 sitd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 sitd->hw_fullspeed_ep = stream->address;
1984 sitd->hw_uframe = stream->splits;
1985 sitd->hw_results = uf->transaction;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001986 sitd->hw_backpointer = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 bufp = uf->bufp;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001989 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
1990 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Stefan Roese6dbd6822007-05-01 09:29:37 -07001992 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (uf->cross)
1994 bufp += 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001995 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 sitd->index = index;
1997}
1998
1999static inline void
2000sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
2001{
2002 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
2003 sitd->sitd_next = ehci->pshadow [frame];
2004 sitd->hw_next = ehci->periodic [frame];
2005 ehci->pshadow [frame].sitd = sitd;
2006 sitd->frame = frame;
2007 wmb ();
Stefan Roese6dbd6822007-05-01 09:29:37 -07002008 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
2011/* fit urb's sitds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04002012static void sitd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 struct ehci_hcd *ehci,
2014 struct urb *urb,
2015 unsigned mod,
2016 struct ehci_iso_stream *stream
2017)
2018{
2019 int packet;
2020 unsigned next_uframe;
2021 struct ehci_iso_sched *sched = urb->hcpriv;
2022 struct ehci_sitd *sitd;
2023
2024 next_uframe = stream->next_uframe;
2025
2026 if (list_empty(&stream->td_list)) {
2027 /* usbfs ignores TT bandwidth */
2028 ehci_to_hcd(ehci)->self.bandwidth_allocated
2029 += stream->bandwidth;
2030 ehci_vdbg (ehci,
2031 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
2032 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
2033 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
Alan Sternbccbefa2010-07-14 11:03:36 -04002034 (next_uframe >> 3) & (ehci->periodic_size - 1),
Stefan Roese6dbd6822007-05-01 09:29:37 -07002035 stream->interval, hc32_to_cpu(ehci, stream->splits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
Alex He05570292010-12-07 10:10:08 +08002037
2038 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002039 if (ehci->amd_pll_fix == 1)
2040 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08002041 }
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
2044
2045 /* fill sITDs frame by frame */
2046 for (packet = 0, sitd = NULL;
2047 packet < urb->number_of_packets;
2048 packet++) {
2049
2050 /* ASSERT: we have all necessary sitds */
2051 BUG_ON (list_empty (&sched->td_list));
2052
2053 /* ASSERT: no itds for this endpoint in this frame */
2054
2055 sitd = list_entry (sched->td_list.next,
2056 struct ehci_sitd, sitd_list);
2057 list_move_tail (&sitd->sitd_list, &stream->td_list);
2058 sitd->stream = iso_stream_get (stream);
Karsten Wiese508db8c2009-02-26 01:47:48 +01002059 sitd->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Stefan Roese6dbd6822007-05-01 09:29:37 -07002061 sitd_patch(ehci, stream, sitd, sched, packet);
Alan Sternbccbefa2010-07-14 11:03:36 -04002062 sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 sitd);
2064
2065 next_uframe += stream->interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
Alan Sternbccbefa2010-07-14 11:03:36 -04002067 stream->next_uframe = next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 /* don't need that schedule data any more */
2070 iso_sched_free (stream, sched);
2071 urb->hcpriv = NULL;
2072
2073 timer_action (ehci, TIMER_IO_WATCHDOG);
Alan Sternb015cb72012-07-11 11:22:10 -04002074 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
2077/*-------------------------------------------------------------------------*/
2078
2079#define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
David Brownell53bd6a62006-08-30 14:50:06 -07002080 | SITD_STS_XACT | SITD_STS_MMF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
David Brownell30bf54e2007-12-16 22:37:40 -08002082/* Process and recycle a completed SITD. Return true iff its urb completed,
2083 * and hence its completion callback probably added things to the hardware
2084 * schedule.
2085 *
2086 * Note that we carefully avoid recycling this descriptor until after any
2087 * completion callback runs, so that it won't be reused quickly. That is,
2088 * assuming (a) no more than two urbs per frame on this endpoint, and also
2089 * (b) only this endpoint's completions submit URBs. It seems some silicon
2090 * corrupts things if you reuse completed descriptors very quickly...
2091 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092static unsigned
2093sitd_complete (
2094 struct ehci_hcd *ehci,
David Howells7d12e782006-10-05 14:55:46 +01002095 struct ehci_sitd *sitd
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096) {
2097 struct urb *urb = sitd->urb;
2098 struct usb_iso_packet_descriptor *desc;
2099 u32 t;
2100 int urb_index = -1;
2101 struct ehci_iso_stream *stream = sitd->stream;
2102 struct usb_device *dev;
David Brownell30bf54e2007-12-16 22:37:40 -08002103 unsigned retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 urb_index = sitd->index;
2106 desc = &urb->iso_frame_desc [urb_index];
Stefan Roese6dbd6822007-05-01 09:29:37 -07002107 t = hc32_to_cpup(ehci, &sitd->hw_results);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 /* report transfer status */
2110 if (t & SITD_ERRS) {
2111 urb->error_count++;
2112 if (t & SITD_STS_DBE)
2113 desc->status = usb_pipein (urb->pipe)
2114 ? -ENOSR /* hc couldn't read */
2115 : -ECOMM; /* hc couldn't write */
2116 else if (t & SITD_STS_BABBLE)
2117 desc->status = -EOVERFLOW;
2118 else /* XACT, MMF, etc */
2119 desc->status = -EPROTO;
2120 } else {
2121 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04002122 desc->actual_length = desc->length - SITD_LENGTH(t);
2123 urb->actual_length += desc->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 /* handle completion now? */
2127 if ((urb_index + 1) != urb->number_of_packets)
David Brownell30bf54e2007-12-16 22:37:40 -08002128 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /* ASSERT: it's really the last sitd for this urb
2131 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2132 BUG_ON (sitd->urb == urb);
2133 */
2134
David Brownellaa16ca32007-12-30 23:45:19 -08002135 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05002136 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04002137 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08002138 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 urb = NULL;
Alan Sternb015cb72012-07-11 11:22:10 -04002140 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
2142
Alex He05570292010-12-07 10:10:08 +08002143 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002144 if (ehci->amd_pll_fix == 1)
2145 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08002146 }
2147
Karsten Wiese508db8c2009-02-26 01:47:48 +01002148 if (list_is_singular(&stream->td_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 ehci_to_hcd(ehci)->self.bandwidth_allocated
2150 -= stream->bandwidth;
2151 ehci_vdbg (ehci,
2152 "deschedule devp %s ep%d%s-iso\n",
2153 dev->devpath, stream->bEndpointAddress & 0x0f,
2154 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
2155 }
2156 iso_stream_put (ehci, stream);
Alan Stern0e5f2312010-04-08 16:56:37 -04002157
David Brownell30bf54e2007-12-16 22:37:40 -08002158done:
David Brownell30bf54e2007-12-16 22:37:40 -08002159 sitd->urb = NULL;
Alan Stern0e5f2312010-04-08 16:56:37 -04002160 if (ehci->clock_frame != sitd->frame) {
2161 /* OK to recycle this SITD now. */
2162 sitd->stream = NULL;
2163 list_move(&sitd->sitd_list, &stream->free_list);
2164 iso_stream_put(ehci, stream);
2165 } else {
2166 /* HW might remember this SITD, so we can't recycle it yet.
2167 * Move it to a safe place until a new frame starts.
2168 */
2169 list_move(&sitd->sitd_list, &ehci->cached_sitd_list);
2170 if (stream->refcount == 2) {
2171 /* If iso_stream_put() were called here, stream
2172 * would be freed. Instead, just prevent reuse.
2173 */
2174 stream->ep->hcpriv = NULL;
2175 stream->ep = NULL;
2176 }
2177 }
David Brownell30bf54e2007-12-16 22:37:40 -08002178 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181
Olav Kongas5db539e2005-06-23 20:25:36 +03002182static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04002183 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 int status = -EINVAL;
2186 unsigned long flags;
2187 struct ehci_iso_stream *stream;
2188
2189 /* Get iso_stream head */
2190 stream = iso_stream_find (ehci, urb);
2191 if (stream == NULL) {
2192 ehci_dbg (ehci, "can't get iso stream\n");
2193 return -ENOMEM;
2194 }
2195 if (urb->interval != stream->interval) {
2196 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
2197 stream->interval, urb->interval);
2198 goto done;
2199 }
2200
2201#ifdef EHCI_URB_TRACE
2202 ehci_dbg (ehci,
2203 "submit %p dev%s ep%d%s-iso len %d\n",
2204 urb, urb->dev->devpath,
2205 usb_pipeendpoint (urb->pipe),
2206 usb_pipein (urb->pipe) ? "in" : "out",
2207 urb->transfer_buffer_length);
2208#endif
2209
2210 /* allocate SITDs */
2211 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2212 if (status < 0) {
2213 ehci_dbg (ehci, "can't init sitds\n");
2214 goto done;
2215 }
2216
2217 /* schedule ... need to lock */
2218 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04002219 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002220 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04002221 goto done_not_linked;
2222 }
2223 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2224 if (unlikely(status))
2225 goto done_not_linked;
2226 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07002227 if (status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04002229 else
2230 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
2231done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 spin_unlock_irqrestore (&ehci->lock, flags);
2233
2234done:
2235 if (status < 0)
2236 iso_stream_put (ehci, stream);
2237 return status;
2238}
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240/*-------------------------------------------------------------------------*/
2241
Alan Stern0e5f2312010-04-08 16:56:37 -04002242static void free_cached_lists(struct ehci_hcd *ehci)
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002243{
2244 struct ehci_itd *itd, *n;
Alan Stern0e5f2312010-04-08 16:56:37 -04002245 struct ehci_sitd *sitd, *sn;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002246
2247 list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) {
2248 struct ehci_iso_stream *stream = itd->stream;
2249 itd->stream = NULL;
2250 list_move(&itd->itd_list, &stream->free_list);
2251 iso_stream_put(ehci, stream);
2252 }
Alan Stern0e5f2312010-04-08 16:56:37 -04002253
2254 list_for_each_entry_safe(sitd, sn, &ehci->cached_sitd_list, sitd_list) {
2255 struct ehci_iso_stream *stream = sitd->stream;
2256 sitd->stream = NULL;
2257 list_move(&sitd->sitd_list, &stream->free_list);
2258 iso_stream_put(ehci, stream);
2259 }
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002260}
2261
2262/*-------------------------------------------------------------------------*/
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264static void
David Howells7d12e782006-10-05 14:55:46 +01002265scan_periodic (struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
Alan Sternb40e43f2008-05-20 16:59:10 -04002267 unsigned now_uframe, frame, clock, clock_frame, mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 unsigned modified;
2269
2270 mod = ehci->periodic_size << 3;
2271
2272 /*
2273 * When running, scan from last scan point up to "now"
2274 * else clean up by scanning everything that's left.
2275 * Touches as few pages as possible: cache-friendly.
2276 */
2277 now_uframe = ehci->next_uframe;
Alan Sternc0c53db2012-07-11 11:21:48 -04002278 if (ehci->rh_state >= EHCI_RH_RUNNING) {
Alan Stern68aa95d2011-10-12 10:39:14 -04002279 clock = ehci_read_frame_index(ehci);
Alan Sternbccbefa2010-07-14 11:03:36 -04002280 clock_frame = (clock >> 3) & (ehci->periodic_size - 1);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002281 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 clock = now_uframe + mod - 1;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002283 clock_frame = -1;
2284 }
2285 if (ehci->clock_frame != clock_frame) {
Alan Stern0e5f2312010-04-08 16:56:37 -04002286 free_cached_lists(ehci);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002287 ehci->clock_frame = clock_frame;
2288 }
Alan Sternbccbefa2010-07-14 11:03:36 -04002289 clock &= mod - 1;
Alan Sternb40e43f2008-05-20 16:59:10 -04002290 clock_frame = clock >> 3;
Alan Stern1e12c912011-05-17 10:40:51 -04002291 ++ehci->periodic_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 for (;;) {
2294 union ehci_shadow q, *q_p;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002295 __hc32 type, *hw_p;
David Brownell79592b722008-01-07 00:47:42 -08002296 unsigned incomplete = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 frame = now_uframe >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300restart:
2301 /* scan each element in frame's queue for completions */
2302 q_p = &ehci->pshadow [frame];
2303 hw_p = &ehci->periodic [frame];
2304 q.ptr = q_p->ptr;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002305 type = Q_NEXT_TYPE(ehci, *hw_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 modified = 0;
2307
2308 while (q.ptr != NULL) {
2309 unsigned uf;
2310 union ehci_shadow temp;
2311 int live;
2312
Alan Sternc0c53db2012-07-11 11:21:48 -04002313 live = (ehci->rh_state >= EHCI_RH_RUNNING);
Stefan Roese6dbd6822007-05-01 09:29:37 -07002314 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 case Q_TYPE_QH:
2316 /* handle any completions */
Alan Sternc83e1a92012-07-11 11:21:25 -04002317 temp.qh = q.qh;
Alek Du3807e262009-07-14 07:23:29 +08002318 type = Q_NEXT_TYPE(ehci, q.qh->hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 q = q.qh->qh_next;
Alan Stern1e12c912011-05-17 10:40:51 -04002320 if (temp.qh->stamp != ehci->periodic_stamp) {
2321 modified = qh_completions(ehci, temp.qh);
2322 if (!modified)
2323 temp.qh->stamp = ehci->periodic_stamp;
2324 if (unlikely(list_empty(&temp.qh->qtd_list) ||
2325 temp.qh->needs_rescan))
Alan Sterndf202252012-07-11 11:22:26 -04002326 start_unlink_intr(ehci, temp.qh);
Alan Stern1e12c912011-05-17 10:40:51 -04002327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 break;
2329 case Q_TYPE_FSTN:
2330 /* for "save place" FSTNs, look at QH entries
2331 * in the previous frame for completions.
2332 */
Stefan Roese6dbd6822007-05-01 09:29:37 -07002333 if (q.fstn->hw_prev != EHCI_LIST_END(ehci)) {
Greg Kroah-Hartman2d0fe1b2012-05-01 21:33:36 -07002334 ehci_dbg(ehci,
2335 "ignoring completions from FSTNs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
Stefan Roese6dbd6822007-05-01 09:29:37 -07002337 type = Q_NEXT_TYPE(ehci, q.fstn->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 q = q.fstn->fstn_next;
2339 break;
2340 case Q_TYPE_ITD:
David Brownell79592b722008-01-07 00:47:42 -08002341 /* If this ITD is still active, leave it for
2342 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002343 * No need to check for activity unless the
2344 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002345 */
Alan Sternb40e43f2008-05-20 16:59:10 -04002346 if (frame == clock_frame && live) {
2347 rmb();
2348 for (uf = 0; uf < 8; uf++) {
2349 if (q.itd->hw_transaction[uf] &
2350 ITD_ACTIVE(ehci))
2351 break;
2352 }
2353 if (uf < 8) {
2354 incomplete = true;
2355 q_p = &q.itd->itd_next;
2356 hw_p = &q.itd->hw_next;
2357 type = Q_NEXT_TYPE(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -07002358 q.itd->hw_next);
Alan Sternb40e43f2008-05-20 16:59:10 -04002359 q = *q_p;
2360 break;
2361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
David Brownell79592b722008-01-07 00:47:42 -08002364 /* Take finished ITDs out of the schedule
2365 * and process them: recycle, maybe report
2366 * URB completion. HC won't cache the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 * pointer for much longer, if at all.
2368 */
2369 *q_p = q.itd->itd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002370 if (!ehci->use_dummy_qh ||
2371 q.itd->hw_next != EHCI_LIST_END(ehci))
2372 *hw_p = q.itd->hw_next;
2373 else
2374 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002375 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002377 modified = itd_complete (ehci, q.itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 q = *q_p;
2379 break;
2380 case Q_TYPE_SITD:
David Brownell79592b722008-01-07 00:47:42 -08002381 /* If this SITD is still active, leave it for
2382 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002383 * No need to check for activity unless the
2384 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002385 */
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002386 if (((frame == clock_frame) ||
Alan Sternbccbefa2010-07-14 11:03:36 -04002387 (((frame + 1) & (ehci->periodic_size - 1))
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002388 == clock_frame))
2389 && live
2390 && (q.sitd->hw_results &
2391 SITD_ACTIVE(ehci))) {
2392
David Brownell79592b722008-01-07 00:47:42 -08002393 incomplete = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 q_p = &q.sitd->sitd_next;
2395 hw_p = &q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002396 type = Q_NEXT_TYPE(ehci,
2397 q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 q = *q_p;
2399 break;
2400 }
David Brownell79592b722008-01-07 00:47:42 -08002401
2402 /* Take finished SITDs out of the schedule
2403 * and process them: recycle, maybe report
2404 * URB completion.
2405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 *q_p = q.sitd->sitd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002407 if (!ehci->use_dummy_qh ||
2408 q.sitd->hw_next != EHCI_LIST_END(ehci))
2409 *hw_p = q.sitd->hw_next;
2410 else
2411 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002412 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002414 modified = sitd_complete (ehci, q.sitd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 q = *q_p;
2416 break;
2417 default:
Greg Kroah-Hartman2d0fe1b2012-05-01 21:33:36 -07002418 ehci_dbg(ehci, "corrupt type %d frame %d shadow %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 type, frame, q.ptr);
2420 // BUG ();
2421 q.ptr = NULL;
2422 }
2423
2424 /* assume completion callbacks modify the queue */
David Brownellaa16ca32007-12-30 23:45:19 -08002425 if (unlikely (modified)) {
Alan Stern3ca9aeb2012-07-11 11:22:05 -04002426 if (likely(ehci->periodic_count > 0))
David Brownellaa16ca32007-12-30 23:45:19 -08002427 goto restart;
David Brownell01c17142008-08-26 23:35:04 -07002428 /* short-circuit this scan */
David Brownellaa16ca32007-12-30 23:45:19 -08002429 now_uframe = clock;
2430 break;
2431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 }
2433
David Brownell79592b722008-01-07 00:47:42 -08002434 /* If we can tell we caught up to the hardware, stop now.
2435 * We can't advance our scan without collecting the ISO
2436 * transfers that are still pending in this frame.
2437 */
Alan Sternc0c53db2012-07-11 11:21:48 -04002438 if (incomplete && ehci->rh_state >= EHCI_RH_RUNNING) {
David Brownell79592b722008-01-07 00:47:42 -08002439 ehci->next_uframe = now_uframe;
2440 break;
2441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 // FIXME: this assumes we won't get lapped when
2444 // latencies climb; that should be rare, but...
2445 // detect it, and just go all the way around.
2446 // FLR might help detect this case, so long as latencies
2447 // don't exceed periodic_size msec (default 1.024 sec).
2448
2449 // FIXME: likewise assumes HC doesn't halt mid-scan
2450
2451 if (now_uframe == clock) {
2452 unsigned now;
2453
Alan Sternc0c53db2012-07-11 11:21:48 -04002454 if (ehci->rh_state < EHCI_RH_RUNNING
Alan Stern3ca9aeb2012-07-11 11:22:05 -04002455 || ehci->periodic_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 break;
2457 ehci->next_uframe = now_uframe;
Alan Stern68aa95d2011-10-12 10:39:14 -04002458 now = ehci_read_frame_index(ehci) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (now_uframe == now)
2460 break;
2461
2462 /* rescan the rest of this frame, then ... */
2463 clock = now;
Alan Sternb40e43f2008-05-20 16:59:10 -04002464 clock_frame = clock >> 3;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002465 if (ehci->clock_frame != clock_frame) {
Alan Stern0e5f2312010-04-08 16:56:37 -04002466 free_cached_lists(ehci);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002467 ehci->clock_frame = clock_frame;
Alan Stern1e12c912011-05-17 10:40:51 -04002468 ++ehci->periodic_stamp;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 } else {
2471 now_uframe++;
Alan Sternbccbefa2010-07-14 11:03:36 -04002472 now_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
David Brownell53bd6a62006-08-30 14:50:06 -07002474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}