blob: 85dd24ed97a6d6cf6700405797da29d313e76958 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2001-2004 by David Brownell
3 * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers
David Brownell53bd6a62006-08-30 14:50:06 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20/* this file is part of ehci-hcd.c */
21
22/*-------------------------------------------------------------------------*/
23
24/*
25 * EHCI scheduled transaction support: interrupt, iso, split iso
26 * These are called "periodic" transactions in the EHCI spec.
27 *
28 * Note that for interrupt transfers, the QH/QTD manipulation is shared
29 * with the "asynchronous" transaction support (control/bulk transfers).
30 * The only real difference is in how interrupt transfers are scheduled.
31 *
32 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
33 * It keeps track of every ITD (or SITD) that's linked, and holds enough
34 * pre-calculated schedule data to make appending to the queue be quick.
35 */
36
37static int ehci_get_frame (struct usb_hcd *hcd);
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * periodic_next_shadow - return "next" pointer on shadow list
41 * @periodic: host pointer to qh/itd/sitd
42 * @tag: hardware tag for type of this record
43 */
44static union ehci_shadow *
Stefan Roese6dbd6822007-05-01 09:29:37 -070045periodic_next_shadow(struct ehci_hcd *ehci, union ehci_shadow *periodic,
46 __hc32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Stefan Roese6dbd6822007-05-01 09:29:37 -070048 switch (hc32_to_cpu(ehci, tag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 case Q_TYPE_QH:
50 return &periodic->qh->qh_next;
51 case Q_TYPE_FSTN:
52 return &periodic->fstn->fstn_next;
53 case Q_TYPE_ITD:
54 return &periodic->itd->itd_next;
55 // case Q_TYPE_SITD:
56 default:
57 return &periodic->sitd->sitd_next;
58 }
59}
60
Alek Du3807e262009-07-14 07:23:29 +080061static __hc32 *
62shadow_next_periodic(struct ehci_hcd *ehci, union ehci_shadow *periodic,
63 __hc32 tag)
64{
65 switch (hc32_to_cpu(ehci, tag)) {
66 /* our ehci_shadow.qh is actually software part */
67 case Q_TYPE_QH:
68 return &periodic->qh->hw->hw_next;
69 /* others are hw parts */
70 default:
71 return periodic->hw_next;
72 }
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* caller must hold ehci->lock */
76static void periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
77{
Stefan Roese6dbd6822007-05-01 09:29:37 -070078 union ehci_shadow *prev_p = &ehci->pshadow[frame];
79 __hc32 *hw_p = &ehci->periodic[frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 union ehci_shadow here = *prev_p;
81
82 /* find predecessor of "ptr"; hw and shadow lists are in sync */
83 while (here.ptr && here.ptr != ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -070084 prev_p = periodic_next_shadow(ehci, prev_p,
85 Q_NEXT_TYPE(ehci, *hw_p));
Alek Du3807e262009-07-14 07:23:29 +080086 hw_p = shadow_next_periodic(ehci, &here,
87 Q_NEXT_TYPE(ehci, *hw_p));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 here = *prev_p;
89 }
90 /* an interrupt entry (at list end) could have been shared */
91 if (!here.ptr)
92 return;
93
94 /* update shadow and hardware lists ... the old "next" pointers
95 * from ptr may still be in use, the caller updates them.
96 */
Stefan Roese6dbd6822007-05-01 09:29:37 -070097 *prev_p = *periodic_next_shadow(ehci, &here,
98 Q_NEXT_TYPE(ehci, *hw_p));
Andiry Xu3d091a62010-11-08 17:58:35 +080099
100 if (!ehci->use_dummy_qh ||
101 *shadow_next_periodic(ehci, &here, Q_NEXT_TYPE(ehci, *hw_p))
102 != EHCI_LIST_END(ehci))
103 *hw_p = *shadow_next_periodic(ehci, &here,
104 Q_NEXT_TYPE(ehci, *hw_p));
105 else
106 *hw_p = ehci->dummy->qh_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/* how many of the uframe's 125 usecs are allocated? */
110static unsigned short
111periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
112{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700113 __hc32 *hw_p = &ehci->periodic [frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 union ehci_shadow *q = &ehci->pshadow [frame];
115 unsigned usecs = 0;
Alek Du3807e262009-07-14 07:23:29 +0800116 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 while (q->ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700119 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800121 hw = q->qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* is it in the S-mask? */
Alek Du3807e262009-07-14 07:23:29 +0800123 if (hw->hw_info2 & cpu_to_hc32(ehci, 1 << uframe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 usecs += q->qh->usecs;
125 /* ... or C-mask? */
Alek Du3807e262009-07-14 07:23:29 +0800126 if (hw->hw_info2 & cpu_to_hc32(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700127 1 << (8 + uframe)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 usecs += q->qh->c_usecs;
Alek Du3807e262009-07-14 07:23:29 +0800129 hw_p = &hw->hw_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 q = &q->qh->qh_next;
131 break;
132 // case Q_TYPE_FSTN:
133 default:
134 /* for "save place" FSTNs, count the relevant INTR
135 * bandwidth from the previous frame
136 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700137 if (q->fstn->hw_prev != EHCI_LIST_END(ehci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 ehci_dbg (ehci, "ignoring FSTN cost ...\n");
139 }
140 hw_p = &q->fstn->hw_next;
141 q = &q->fstn->fstn_next;
142 break;
143 case Q_TYPE_ITD:
Karsten Wiese3b6fcfd2007-12-30 21:55:05 -0800144 if (q->itd->hw_transaction[uframe])
145 usecs += q->itd->stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 hw_p = &q->itd->hw_next;
147 q = &q->itd->itd_next;
148 break;
149 case Q_TYPE_SITD:
150 /* is it in the S-mask? (count SPLIT, DATA) */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700151 if (q->sitd->hw_uframe & cpu_to_hc32(ehci,
152 1 << uframe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (q->sitd->hw_fullspeed_ep &
Stefan Roese6dbd6822007-05-01 09:29:37 -0700154 cpu_to_hc32(ehci, 1<<31))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 usecs += q->sitd->stream->usecs;
156 else /* worst case for OUT start-split */
157 usecs += HS_USECS_ISO (188);
158 }
159
160 /* ... C-mask? (count CSPLIT, DATA) */
161 if (q->sitd->hw_uframe &
Stefan Roese6dbd6822007-05-01 09:29:37 -0700162 cpu_to_hc32(ehci, 1 << (8 + uframe))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* worst case for IN complete-split */
164 usecs += q->sitd->stream->c_usecs;
165 }
166
167 hw_p = &q->sitd->hw_next;
168 q = &q->sitd->sitd_next;
169 break;
170 }
171 }
Xenia Ragiadakou1512c912013-08-29 11:45:13 +0300172#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +0400173 if (usecs > ehci->uframe_periodic_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 ehci_err (ehci, "uframe %d sched overrun: %d usecs\n",
175 frame * 8 + uframe, usecs);
176#endif
177 return usecs;
178}
179
180/*-------------------------------------------------------------------------*/
181
182static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
183{
184 if (!dev1->tt || !dev2->tt)
185 return 0;
186 if (dev1->tt != dev2->tt)
187 return 0;
188 if (dev1->tt->multi)
189 return dev1->ttport == dev2->ttport;
190 else
191 return 1;
192}
193
Dan Streetmanba47f662006-05-24 09:39:16 -0700194#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
195
196/* Which uframe does the low/fullspeed transfer start in?
197 *
198 * The parameter is the mask of ssplits in "H-frame" terms
199 * and this returns the transfer start uframe in "B-frame" terms,
200 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
201 * will cause a transfer in "B-frame" uframe 0. "B-frames" lag
202 * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7.
203 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700204static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask)
Dan Streetmanba47f662006-05-24 09:39:16 -0700205{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700206 unsigned char smask = QH_SMASK & hc32_to_cpu(ehci, mask);
Dan Streetmanba47f662006-05-24 09:39:16 -0700207 if (!smask) {
208 ehci_err(ehci, "invalid empty smask!\n");
209 /* uframe 7 can't have bw so this will indicate failure */
210 return 7;
211 }
212 return ffs(smask) - 1;
213}
214
215static const unsigned char
Alan Sternfdc034382013-05-28 14:03:10 -0400216max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
Dan Streetmanba47f662006-05-24 09:39:16 -0700217
218/* carryover low/fullspeed bandwidth that crosses uframe boundries */
219static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
220{
221 int i;
222 for (i=0; i<7; i++) {
223 if (max_tt_usecs[i] < tt_usecs[i]) {
224 tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i];
225 tt_usecs[i] = max_tt_usecs[i];
226 }
227 }
228}
229
230/* How many of the tt's periodic downstream 1000 usecs are allocated?
231 *
232 * While this measures the bandwidth in terms of usecs/uframe,
233 * the low/fullspeed bus has no notion of uframes, so any particular
234 * low/fullspeed transfer can "carry over" from one uframe to the next,
235 * since the TT just performs downstream transfers in sequence.
236 *
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800237 * For example two separate 100 usec transfers can start in the same uframe,
Dan Streetmanba47f662006-05-24 09:39:16 -0700238 * and the second one would "carry over" 75 usecs into the next uframe.
239 */
240static void
241periodic_tt_usecs (
242 struct ehci_hcd *ehci,
243 struct usb_device *dev,
244 unsigned frame,
245 unsigned short tt_usecs[8]
246)
247{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700248 __hc32 *hw_p = &ehci->periodic [frame];
Dan Streetmanba47f662006-05-24 09:39:16 -0700249 union ehci_shadow *q = &ehci->pshadow [frame];
250 unsigned char uf;
251
252 memset(tt_usecs, 0, 16);
253
254 while (q->ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700255 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
Dan Streetmanba47f662006-05-24 09:39:16 -0700256 case Q_TYPE_ITD:
257 hw_p = &q->itd->hw_next;
258 q = &q->itd->itd_next;
259 continue;
260 case Q_TYPE_QH:
261 if (same_tt(dev, q->qh->dev)) {
Alek Du3807e262009-07-14 07:23:29 +0800262 uf = tt_start_uframe(ehci, q->qh->hw->hw_info2);
Dan Streetmanba47f662006-05-24 09:39:16 -0700263 tt_usecs[uf] += q->qh->tt_usecs;
264 }
Alek Du3807e262009-07-14 07:23:29 +0800265 hw_p = &q->qh->hw->hw_next;
Dan Streetmanba47f662006-05-24 09:39:16 -0700266 q = &q->qh->qh_next;
267 continue;
268 case Q_TYPE_SITD:
269 if (same_tt(dev, q->sitd->urb->dev)) {
270 uf = tt_start_uframe(ehci, q->sitd->hw_uframe);
271 tt_usecs[uf] += q->sitd->stream->tt_usecs;
272 }
273 hw_p = &q->sitd->hw_next;
274 q = &q->sitd->sitd_next;
275 continue;
276 // case Q_TYPE_FSTN:
277 default:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700278 ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n",
279 frame);
Dan Streetmanba47f662006-05-24 09:39:16 -0700280 hw_p = &q->fstn->hw_next;
281 q = &q->fstn->fstn_next;
282 }
283 }
284
285 carryover_tt_bandwidth(tt_usecs);
286
287 if (max_tt_usecs[7] < tt_usecs[7])
288 ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
289 frame, tt_usecs[7] - max_tt_usecs[7]);
290}
291
292/*
293 * Return true if the device's tt's downstream bus is available for a
294 * periodic transfer of the specified length (usecs), starting at the
295 * specified frame/uframe. Note that (as summarized in section 11.19
296 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
297 * uframe.
298 *
299 * The uframe parameter is when the fullspeed/lowspeed transfer
300 * should be executed in "B-frame" terms, which is the same as the
301 * highspeed ssplit's uframe (which is in "H-frame" terms). For example
302 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
303 * See the EHCI spec sec 4.5 and fig 4.7.
304 *
305 * This checks if the full/lowspeed bus, at the specified starting uframe,
306 * has the specified bandwidth available, according to rules listed
307 * in USB 2.0 spec section 11.18.1 fig 11-60.
308 *
309 * This does not check if the transfer would exceed the max ssplit
310 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
311 * since proper scheduling limits ssplits to less than 16 per uframe.
312 */
313static int tt_available (
314 struct ehci_hcd *ehci,
315 unsigned period,
316 struct usb_device *dev,
317 unsigned frame,
318 unsigned uframe,
319 u16 usecs
320)
321{
322 if ((period == 0) || (uframe >= 7)) /* error */
323 return 0;
324
325 for (; frame < ehci->periodic_size; frame += period) {
326 unsigned short tt_usecs[8];
327
328 periodic_tt_usecs (ehci, dev, frame, tt_usecs);
329
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +0300330 if (max_tt_usecs[uframe] <= tt_usecs[uframe])
Dan Streetmanba47f662006-05-24 09:39:16 -0700331 return 0;
Dan Streetmanba47f662006-05-24 09:39:16 -0700332
333 /* special case for isoc transfers larger than 125us:
334 * the first and each subsequent fully used uframe
335 * must be empty, so as to not illegally delay
336 * already scheduled transactions
337 */
338 if (125 < usecs) {
Dan Streetmanc065c602009-04-21 13:37:12 -0400339 int ufs = (usecs / 125);
Dan Streetmanba47f662006-05-24 09:39:16 -0700340 int i;
341 for (i = uframe; i < (uframe + ufs) && i < 8; i++)
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +0300342 if (0 < tt_usecs[i])
Dan Streetmanba47f662006-05-24 09:39:16 -0700343 return 0;
Dan Streetmanba47f662006-05-24 09:39:16 -0700344 }
345
346 tt_usecs[uframe] += usecs;
347
348 carryover_tt_bandwidth(tt_usecs);
349
350 /* fail if the carryover pushed bw past the last uframe's limit */
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +0300351 if (max_tt_usecs[7] < tt_usecs[7])
Dan Streetmanba47f662006-05-24 09:39:16 -0700352 return 0;
Dan Streetmanba47f662006-05-24 09:39:16 -0700353 }
354
355 return 1;
356}
357
358#else
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/* return true iff the device's transaction translator is available
361 * for a periodic transfer starting at the specified frame, using
362 * all the uframes in the mask.
363 */
364static int tt_no_collision (
365 struct ehci_hcd *ehci,
366 unsigned period,
367 struct usb_device *dev,
368 unsigned frame,
369 u32 uf_mask
370)
371{
372 if (period == 0) /* error */
373 return 0;
374
375 /* note bandwidth wastage: split never follows csplit
376 * (different dev or endpoint) until the next uframe.
377 * calling convention doesn't make that distinction.
378 */
379 for (; frame < ehci->periodic_size; frame += period) {
380 union ehci_shadow here;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700381 __hc32 type;
Alek Du3807e262009-07-14 07:23:29 +0800382 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 here = ehci->pshadow [frame];
Stefan Roese6dbd6822007-05-01 09:29:37 -0700385 type = Q_NEXT_TYPE(ehci, ehci->periodic [frame]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700387 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 case Q_TYPE_ITD:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700389 type = Q_NEXT_TYPE(ehci, here.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 here = here.itd->itd_next;
391 continue;
392 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800393 hw = here.qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (same_tt (dev, here.qh->dev)) {
395 u32 mask;
396
Stefan Roese6dbd6822007-05-01 09:29:37 -0700397 mask = hc32_to_cpu(ehci,
Alek Du3807e262009-07-14 07:23:29 +0800398 hw->hw_info2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* "knows" no gap is needed */
400 mask |= mask >> 8;
401 if (mask & uf_mask)
402 break;
403 }
Alek Du3807e262009-07-14 07:23:29 +0800404 type = Q_NEXT_TYPE(ehci, hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 here = here.qh->qh_next;
406 continue;
407 case Q_TYPE_SITD:
408 if (same_tt (dev, here.sitd->urb->dev)) {
409 u16 mask;
410
Stefan Roese6dbd6822007-05-01 09:29:37 -0700411 mask = hc32_to_cpu(ehci, here.sitd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 ->hw_uframe);
413 /* FIXME assumes no gap for IN! */
414 mask |= mask >> 8;
415 if (mask & uf_mask)
416 break;
417 }
Stefan Roese6dbd6822007-05-01 09:29:37 -0700418 type = Q_NEXT_TYPE(ehci, here.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 here = here.sitd->sitd_next;
420 continue;
421 // case Q_TYPE_FSTN:
422 default:
423 ehci_dbg (ehci,
424 "periodic frame %d bogus type %d\n",
425 frame, type);
426 }
427
428 /* collision or error */
429 return 0;
430 }
431 }
432
433 /* no collision */
434 return 1;
435}
436
Dan Streetmanba47f662006-05-24 09:39:16 -0700437#endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/*-------------------------------------------------------------------------*/
440
Alan Sternb015cb72012-07-11 11:22:10 -0400441static void enable_periodic(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400443 if (ehci->periodic_count++)
Alan Sternb015cb72012-07-11 11:22:10 -0400444 return;
David Brownell01c17142008-08-26 23:35:04 -0700445
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400446 /* Stop waiting to turn off the periodic schedule */
447 ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_DISABLE_PERIODIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400449 /* Don't start the schedule until PSS is 0 */
450 ehci_poll_PSS(ehci);
Alan Stern18aafe62012-07-11 11:23:04 -0400451 turn_on_io_watchdog(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Alan Sternb015cb72012-07-11 11:22:10 -0400454static void disable_periodic(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400456 if (--ehci->periodic_count)
Alan Sternb015cb72012-07-11 11:22:10 -0400457 return;
David Brownell01c17142008-08-26 23:35:04 -0700458
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400459 /* Don't turn off the schedule until PSS is 1 */
460 ehci_poll_PSS(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463/*-------------------------------------------------------------------------*/
464
465/* periodic schedule slots have iso tds (normal or split) first, then a
466 * sparse tree for active interrupt transfers.
467 *
468 * this just links in a qh; caller guarantees uframe masks are set right.
469 * no FSTN support (yet; ehci 0.96+)
470 */
Alan Sternb015cb72012-07-11 11:22:10 -0400471static void qh_link_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 unsigned i;
474 unsigned period = qh->period;
475
476 dev_dbg (&qh->dev->dev,
477 "link qh%d-%04x/%p start %d [%d/%d us]\n",
Alek Du3807e262009-07-14 07:23:29 +0800478 period, hc32_to_cpup(ehci, &qh->hw->hw_info2)
479 & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 qh, qh->start, qh->usecs, qh->c_usecs);
481
482 /* high bandwidth, or otherwise every microframe */
483 if (period == 0)
484 period = 1;
485
486 for (i = qh->start; i < ehci->periodic_size; i += period) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700487 union ehci_shadow *prev = &ehci->pshadow[i];
488 __hc32 *hw_p = &ehci->periodic[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 union ehci_shadow here = *prev;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700490 __hc32 type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* skip the iso nodes at list head */
493 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700494 type = Q_NEXT_TYPE(ehci, *hw_p);
495 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 break;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700497 prev = periodic_next_shadow(ehci, prev, type);
Alek Du3807e262009-07-14 07:23:29 +0800498 hw_p = shadow_next_periodic(ehci, &here, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 here = *prev;
500 }
501
502 /* sorting each branch by period (slow-->fast)
503 * enables sharing interior tree nodes
504 */
505 while (here.ptr && qh != here.qh) {
506 if (qh->period > here.qh->period)
507 break;
508 prev = &here.qh->qh_next;
Alek Du3807e262009-07-14 07:23:29 +0800509 hw_p = &here.qh->hw->hw_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 here = *prev;
511 }
512 /* link in this qh, unless some earlier pass did that */
513 if (qh != here.qh) {
514 qh->qh_next = here;
515 if (here.qh)
Alek Du3807e262009-07-14 07:23:29 +0800516 qh->hw->hw_next = *hw_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 wmb ();
518 prev->qh = qh;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700519 *hw_p = QH_NEXT (ehci, qh->qh_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 }
522 qh->qh_state = QH_STATE_LINKED;
Alan Sternef4638f2009-07-31 10:41:40 -0400523 qh->xacterrs = 0;
Alan Stern7bc782d2013-03-22 13:31:11 -0400524 qh->exception = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 /* update per-qh bandwidth for usbfs */
527 ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period
528 ? ((qh->usecs + qh->c_usecs) / qh->period)
529 : (qh->usecs * 8);
530
Alan Stern569b3942012-07-11 11:23:00 -0400531 list_add(&qh->intr_node, &ehci->intr_qh_list);
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* maybe enable periodic schedule processing */
Alan Stern569b3942012-07-11 11:23:00 -0400534 ++ehci->intr_count;
Alan Sternb015cb72012-07-11 11:22:10 -0400535 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Alan Sternb015cb72012-07-11 11:22:10 -0400538static void qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 unsigned i;
541 unsigned period;
542
Alan Sterndf202252012-07-11 11:22:26 -0400543 /*
544 * If qh is for a low/full-speed device, simply unlinking it
545 * could interfere with an ongoing split transaction. To unlink
546 * it safely would require setting the QH_INACTIVATE bit and
547 * waiting at least one frame, as described in EHCI 4.12.2.5.
548 *
549 * We won't bother with any of this. Instead, we assume that the
550 * only reason for unlinking an interrupt QH while the current URB
551 * is still active is to dequeue all the URBs (flush the whole
552 * endpoint queue).
553 *
554 * If rebalancing the periodic schedule is ever implemented, this
555 * approach will no longer be valid.
556 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* high bandwidth, or otherwise part of every microframe */
559 if ((period = qh->period) == 0)
560 period = 1;
561
562 for (i = qh->start; i < ehci->periodic_size; i += period)
563 periodic_unlink (ehci, i, qh);
564
565 /* update per-qh bandwidth for usbfs */
566 ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period
567 ? ((qh->usecs + qh->c_usecs) / qh->period)
568 : (qh->usecs * 8);
569
570 dev_dbg (&qh->dev->dev,
571 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
David Brownell7dedacf2005-08-04 18:06:41 -0700572 qh->period,
Alek Du3807e262009-07-14 07:23:29 +0800573 hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 qh, qh->start, qh->usecs, qh->c_usecs);
575
576 /* qh->qh_next still "live" to HC */
577 qh->qh_state = QH_STATE_UNLINK;
578 qh->qh_next.ptr = NULL;
Alan Stern569b3942012-07-11 11:23:00 -0400579
580 if (ehci->qh_scan_next == qh)
581 ehci->qh_scan_next = list_entry(qh->intr_node.next,
582 struct ehci_qh, intr_node);
583 list_del(&qh->intr_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Ming Lei9118f9e2013-07-03 22:53:10 +0800586static void cancel_unlink_wait_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
587{
588 if (qh->qh_state != QH_STATE_LINKED ||
589 list_empty(&qh->unlink_node))
590 return;
591
592 list_del_init(&qh->unlink_node);
593
594 /*
595 * TODO: disable the event of EHCI_HRTIMER_START_UNLINK_INTR for
596 * avoiding unnecessary CPU wakeup
597 */
598}
599
Alan Sterndf202252012-07-11 11:22:26 -0400600static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Alan Stern7bc782d2013-03-22 13:31:11 -0400602 /* If the QH isn't linked then there's nothing we can do. */
603 if (qh->qh_state != QH_STATE_LINKED)
Alan Sterna448c9d2009-08-19 12:22:44 -0400604 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Ming Lei9118f9e2013-07-03 22:53:10 +0800606 /* if the qh is waiting for unlink, cancel it now */
607 cancel_unlink_wait_intr(ehci, qh);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 qh_unlink_periodic (ehci, qh);
610
Alan Sterndf202252012-07-11 11:22:26 -0400611 /* Make sure the unlinks are visible before starting the timer */
612 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Alan Sterndf202252012-07-11 11:22:26 -0400614 /*
615 * The EHCI spec doesn't say how long it takes the controller to
616 * stop accessing an unlinked interrupt QH. The timer delay is
617 * 9 uframes; presumably that will be long enough.
618 */
619 qh->unlink_cycle = ehci->intr_unlink_cycle;
620
621 /* New entries go at the end of the intr_unlink list */
Alan Stern6e018752013-03-22 13:31:45 -0400622 list_add_tail(&qh->unlink_node, &ehci->intr_unlink);
Alan Sterndf202252012-07-11 11:22:26 -0400623
624 if (ehci->intr_unlinking)
625 ; /* Avoid recursive calls */
626 else if (ehci->rh_state < EHCI_RH_RUNNING)
627 ehci_handle_intr_unlinks(ehci);
Alan Stern6e018752013-03-22 13:31:45 -0400628 else if (ehci->intr_unlink.next == &qh->unlink_node) {
Alan Sterndf202252012-07-11 11:22:26 -0400629 ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
630 ++ehci->intr_unlink_cycle;
631 }
632}
633
Ming Lei9118f9e2013-07-03 22:53:10 +0800634/*
635 * It is common only one intr URB is scheduled on one qh, and
636 * given complete() is run in tasklet context, introduce a bit
637 * delay to avoid unlink qh too early.
638 */
639static void start_unlink_intr_wait(struct ehci_hcd *ehci,
640 struct ehci_qh *qh)
641{
642 qh->unlink_cycle = ehci->intr_unlink_wait_cycle;
643
644 /* New entries go at the end of the intr_unlink_wait list */
645 list_add_tail(&qh->unlink_node, &ehci->intr_unlink_wait);
646
647 if (ehci->rh_state < EHCI_RH_RUNNING)
648 ehci_handle_start_intr_unlinks(ehci);
649 else if (ehci->intr_unlink_wait.next == &qh->unlink_node) {
650 ehci_enable_event(ehci, EHCI_HRTIMER_START_UNLINK_INTR, true);
651 ++ehci->intr_unlink_wait_cycle;
652 }
653}
654
Alan Sterndf202252012-07-11 11:22:26 -0400655static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
656{
657 struct ehci_qh_hw *hw = qh->hw;
658 int rc;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 qh->qh_state = QH_STATE_IDLE;
Alek Du3807e262009-07-14 07:23:29 +0800661 hw->hw_next = EHCI_LIST_END(ehci);
Alan Sterna448c9d2009-08-19 12:22:44 -0400662
Alan Stern79bcf7b2013-03-22 13:30:56 -0400663 if (!list_empty(&qh->qtd_list))
664 qh_completions(ehci, qh);
Alan Sterna448c9d2009-08-19 12:22:44 -0400665
666 /* reschedule QH iff another request is queued */
Alan Sterndf202252012-07-11 11:22:26 -0400667 if (!list_empty(&qh->qtd_list) && ehci->rh_state == EHCI_RH_RUNNING) {
Alan Sterna448c9d2009-08-19 12:22:44 -0400668 rc = qh_schedule(ehci, qh);
Alan Stern077f5f12013-05-29 11:33:52 -0400669 if (rc == 0) {
670 qh_refresh(ehci, qh);
671 qh_link_periodic(ehci, qh);
672 }
Alan Sterna448c9d2009-08-19 12:22:44 -0400673
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 */
Alan Stern077f5f12013-05-29 11:33:52 -0400680 else {
Alan Sterna448c9d2009-08-19 12:22:44 -0400681 ehci_err(ehci, "can't reschedule qh %p, err %d\n",
682 qh, rc);
Alan Stern077f5f12013-05-29 11:33:52 -0400683 }
Alan Sterna448c9d2009-08-19 12:22:44 -0400684 }
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400685
686 /* maybe turn off periodic schedule */
Alan Stern569b3942012-07-11 11:23:00 -0400687 --ehci->intr_count;
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400688 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691/*-------------------------------------------------------------------------*/
692
693static int check_period (
David Brownell53bd6a62006-08-30 14:50:06 -0700694 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 unsigned frame,
696 unsigned uframe,
697 unsigned period,
698 unsigned usecs
699) {
700 int claimed;
701
702 /* complete split running into next frame?
703 * given FSTN support, we could sometimes check...
704 */
705 if (uframe >= 8)
706 return 0;
707
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +0400708 /* convert "usecs we need" to "max already claimed" */
709 usecs = ehci->uframe_periodic_max - usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 /* we "know" 2 and 4 uframe intervals were rejected; so
712 * for period 0, check _every_ microframe in the schedule.
713 */
714 if (unlikely (period == 0)) {
715 do {
716 for (uframe = 0; uframe < 7; uframe++) {
717 claimed = periodic_usecs (ehci, frame, uframe);
718 if (claimed > usecs)
719 return 0;
720 }
721 } while ((frame += 1) < ehci->periodic_size);
722
723 /* just check the specified uframe, at that period */
724 } else {
725 do {
726 claimed = periodic_usecs (ehci, frame, uframe);
727 if (claimed > usecs)
728 return 0;
729 } while ((frame += period) < ehci->periodic_size);
730 }
731
732 // success!
733 return 1;
734}
735
736static int check_intr_schedule (
David Brownell53bd6a62006-08-30 14:50:06 -0700737 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 unsigned frame,
739 unsigned uframe,
740 const struct ehci_qh *qh,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700741 __hc32 *c_maskp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742)
743{
David Brownell53bd6a62006-08-30 14:50:06 -0700744 int retval = -ENOSPC;
Dan Streetmanba47f662006-05-24 09:39:16 -0700745 u8 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
748 goto done;
749
750 if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
751 goto done;
752 if (!qh->c_usecs) {
753 retval = 0;
754 *c_maskp = 0;
755 goto done;
756 }
757
Dan Streetmanba47f662006-05-24 09:39:16 -0700758#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
759 if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
760 qh->tt_usecs)) {
761 unsigned i;
762
763 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
764 for (i=uframe+1; i<8 && i<uframe+4; i++)
765 if (!check_period (ehci, frame, i,
766 qh->period, qh->c_usecs))
767 goto done;
768 else
769 mask |= 1 << i;
770
771 retval = 0;
772
Stefan Roese6dbd6822007-05-01 09:29:37 -0700773 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Dan Streetmanba47f662006-05-24 09:39:16 -0700774 }
775#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* Make sure this tt's buffer is also available for CSPLITs.
777 * We pessimize a bit; probably the typical full speed case
778 * doesn't need the second CSPLIT.
David Brownell53bd6a62006-08-30 14:50:06 -0700779 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * NOTE: both SPLIT and CSPLIT could be checked in just
781 * one smart pass...
782 */
783 mask = 0x03 << (uframe + qh->gap_uf);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700784 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 mask |= 1 << uframe;
787 if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
788 if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
789 qh->period, qh->c_usecs))
790 goto done;
791 if (!check_period (ehci, frame, uframe + qh->gap_uf,
792 qh->period, qh->c_usecs))
793 goto done;
794 retval = 0;
795 }
Dan Streetmanba47f662006-05-24 09:39:16 -0700796#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797done:
798 return retval;
799}
800
801/* "first fit" scheduling policy used the first time through,
802 * or when the previous schedule slot can't be re-used.
803 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700804static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
David Brownell53bd6a62006-08-30 14:50:06 -0700806 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 unsigned uframe;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700808 __hc32 c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
Alek Du3807e262009-07-14 07:23:29 +0800810 struct ehci_qh_hw *hw = qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Alek Du3807e262009-07-14 07:23:29 +0800812 hw->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 frame = qh->start;
814
815 /* reuse the previous schedule slots, if we can */
816 if (frame < qh->period) {
Alek Du3807e262009-07-14 07:23:29 +0800817 uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 status = check_intr_schedule (ehci, frame, --uframe,
819 qh, &c_mask);
820 } else {
821 uframe = 0;
822 c_mask = 0;
823 status = -ENOSPC;
824 }
825
826 /* else scan the schedule to find a group of slots such that all
827 * uframes have enough periodic bandwidth available.
828 */
829 if (status) {
830 /* "normal" case, uframing flexible except with splits */
831 if (qh->period) {
Alan Stern68335e82009-05-22 17:02:33 -0400832 int i;
833
834 for (i = qh->period; status && i > 0; --i) {
835 frame = ++ehci->random_frame % qh->period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 for (uframe = 0; uframe < 8; uframe++) {
837 status = check_intr_schedule (ehci,
838 frame, uframe, qh,
839 &c_mask);
840 if (status == 0)
841 break;
842 }
Alan Stern68335e82009-05-22 17:02:33 -0400843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /* qh->period == 0 means every uframe */
846 } else {
847 frame = 0;
848 status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
849 }
850 if (status)
851 goto done;
852 qh->start = frame;
853
854 /* reset S-frame and (maybe) C-frame masks */
Alek Du3807e262009-07-14 07:23:29 +0800855 hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
856 hw->hw_info2 |= qh->period
Stefan Roese6dbd6822007-05-01 09:29:37 -0700857 ? cpu_to_hc32(ehci, 1 << uframe)
858 : cpu_to_hc32(ehci, QH_SMASK);
Alek Du3807e262009-07-14 07:23:29 +0800859 hw->hw_info2 |= c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 } else
861 ehci_dbg (ehci, "reused qh %p schedule\n", qh);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863done:
864 return status;
865}
866
867static int intr_submit (
868 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct urb *urb,
870 struct list_head *qtd_list,
Al Viro55016f12005-10-21 03:21:58 -0400871 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872) {
873 unsigned epnum;
874 unsigned long flags;
875 struct ehci_qh *qh;
Alan Sterne9df41c2007-08-08 11:48:02 -0400876 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 struct list_head empty;
878
879 /* get endpoint and transfer/schedule data */
Alan Sterne9df41c2007-08-08 11:48:02 -0400880 epnum = urb->ep->desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 spin_lock_irqsave (&ehci->lock, flags);
883
Alan Stern541c7d42010-06-22 16:39:10 -0400884 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100885 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -0400886 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100887 }
Alan Sterne9df41c2007-08-08 11:48:02 -0400888 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
889 if (unlikely(status))
890 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /* get qh and force any scheduling errors */
893 INIT_LIST_HEAD (&empty);
Alan Sterne9df41c2007-08-08 11:48:02 -0400894 qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (qh == NULL) {
896 status = -ENOMEM;
897 goto done;
898 }
899 if (qh->qh_state == QH_STATE_IDLE) {
900 if ((status = qh_schedule (ehci, qh)) != 0)
901 goto done;
902 }
903
904 /* then queue the urb's tds to the qh */
Alan Sterne9df41c2007-08-08 11:48:02 -0400905 qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 BUG_ON (qh == NULL);
907
Alan Sternc1fdb682013-03-22 13:30:43 -0400908 /* stuff into the periodic schedule */
909 if (qh->qh_state == QH_STATE_IDLE) {
910 qh_refresh(ehci, qh);
911 qh_link_periodic(ehci, qh);
Ming Lei9118f9e2013-07-03 22:53:10 +0800912 } else {
913 /* cancel unlink wait for the qh */
914 cancel_unlink_wait_intr(ehci, qh);
Alan Sternc1fdb682013-03-22 13:30:43 -0400915 }
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* ... update usbfs periodic stats */
918 ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
919
920done:
Alan Sterne9df41c2007-08-08 11:48:02 -0400921 if (unlikely(status))
922 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
923done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 spin_unlock_irqrestore (&ehci->lock, flags);
925 if (status)
926 qtd_list_free (ehci, urb, qtd_list);
927
928 return status;
929}
930
Alan Stern569b3942012-07-11 11:23:00 -0400931static void scan_intr(struct ehci_hcd *ehci)
932{
933 struct ehci_qh *qh;
934
935 list_for_each_entry_safe(qh, ehci->qh_scan_next, &ehci->intr_qh_list,
936 intr_node) {
Alan Stern79bcf7b2013-03-22 13:30:56 -0400937
Alan Stern569b3942012-07-11 11:23:00 -0400938 /* clean any finished work for this qh */
939 if (!list_empty(&qh->qtd_list)) {
940 int temp;
941
942 /*
943 * Unlinks could happen here; completion reporting
944 * drops the lock. That's why ehci->qh_scan_next
945 * always holds the next qh to scan; if the next qh
946 * gets unlinked then ehci->qh_scan_next is adjusted
947 * in qh_unlink_periodic().
948 */
949 temp = qh_completions(ehci, qh);
Ming Lei9118f9e2013-07-03 22:53:10 +0800950 if (unlikely(temp))
Alan Stern569b3942012-07-11 11:23:00 -0400951 start_unlink_intr(ehci, qh);
Ming Lei9118f9e2013-07-03 22:53:10 +0800952 else if (unlikely(list_empty(&qh->qtd_list) &&
953 qh->qh_state == QH_STATE_LINKED))
954 start_unlink_intr_wait(ehci, qh);
Alan Stern569b3942012-07-11 11:23:00 -0400955 }
956 }
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/*-------------------------------------------------------------------------*/
960
961/* ehci_iso_stream ops work with both ITD and SITD */
962
963static struct ehci_iso_stream *
Al Viro55016f12005-10-21 03:21:58 -0400964iso_stream_alloc (gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct ehci_iso_stream *stream;
967
Pekka Enberg7b842b62005-09-06 15:18:34 -0700968 stream = kzalloc(sizeof *stream, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 INIT_LIST_HEAD(&stream->td_list);
971 INIT_LIST_HEAD(&stream->free_list);
972 stream->next_uframe = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974 return stream;
975}
976
977static void
978iso_stream_init (
979 struct ehci_hcd *ehci,
980 struct ehci_iso_stream *stream,
981 struct usb_device *dev,
982 int pipe,
983 unsigned interval
984)
985{
986 static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
987
988 u32 buf1;
989 unsigned epnum, maxp;
990 int is_input;
991 long bandwidth;
992
993 /*
994 * this might be a "high bandwidth" highspeed endpoint,
995 * as encoded in the ep descriptor's wMaxPacket field
996 */
997 epnum = usb_pipeendpoint (pipe);
998 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
999 maxp = usb_maxpacket(dev, pipe, !is_input);
1000 if (is_input) {
1001 buf1 = (1 << 11);
1002 } else {
1003 buf1 = 0;
1004 }
1005
1006 /* knows about ITD vs SITD */
1007 if (dev->speed == USB_SPEED_HIGH) {
1008 unsigned multi = hb_mult(maxp);
1009
1010 stream->highspeed = 1;
1011
1012 maxp = max_packet(maxp);
1013 buf1 |= maxp;
1014 maxp *= multi;
1015
Stefan Roese6dbd6822007-05-01 09:29:37 -07001016 stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
1017 stream->buf1 = cpu_to_hc32(ehci, buf1);
1018 stream->buf2 = cpu_to_hc32(ehci, multi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /* usbfs wants to report the average usecs per frame tied up
1021 * when transfers on this endpoint are scheduled ...
1022 */
1023 stream->usecs = HS_USECS_ISO (maxp);
1024 bandwidth = stream->usecs * 8;
Alan Stern372dd6e2008-11-12 17:02:57 -05001025 bandwidth /= interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 } else {
1028 u32 addr;
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001029 int think_time;
Clemens Ladisch469d0222006-01-20 13:49:10 -08001030 int hs_transfers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 addr = dev->ttport << 24;
1033 if (!ehci_is_TDI(ehci)
1034 || (dev->tt->hub !=
1035 ehci_to_hcd(ehci)->self.root_hub))
1036 addr |= dev->tt->hub->devnum << 16;
1037 addr |= epnum << 8;
1038 addr |= dev->devnum;
1039 stream->usecs = HS_USECS_ISO (maxp);
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001040 think_time = dev->tt ? dev->tt->think_time : 0;
1041 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
1042 dev->speed, is_input, 1, maxp));
Clemens Ladisch469d0222006-01-20 13:49:10 -08001043 hs_transfers = max (1u, (maxp + 187) / 188);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (is_input) {
1045 u32 tmp;
1046
1047 addr |= 1 << 31;
1048 stream->c_usecs = stream->usecs;
1049 stream->usecs = HS_USECS_ISO (1);
1050 stream->raw_mask = 1;
1051
Clemens Ladisch469d0222006-01-20 13:49:10 -08001052 /* c-mask as specified in USB 2.0 11.18.4 3.c */
1053 tmp = (1 << (hs_transfers + 2)) - 1;
1054 stream->raw_mask |= tmp << (8 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 } else
Clemens Ladisch469d0222006-01-20 13:49:10 -08001056 stream->raw_mask = smask_out [hs_transfers - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 bandwidth = stream->usecs + stream->c_usecs;
Alan Stern372dd6e2008-11-12 17:02:57 -05001058 bandwidth /= interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 /* stream->splits gets created from raw_mask later */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001061 stream->address = cpu_to_hc32(ehci, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063 stream->bandwidth = bandwidth;
1064
1065 stream->udev = dev;
1066
1067 stream->bEndpointAddress = is_input | epnum;
1068 stream->interval = interval;
1069 stream->maxp = maxp;
1070}
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072static struct ehci_iso_stream *
1073iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1074{
1075 unsigned epnum;
1076 struct ehci_iso_stream *stream;
1077 struct usb_host_endpoint *ep;
1078 unsigned long flags;
1079
1080 epnum = usb_pipeendpoint (urb->pipe);
1081 if (usb_pipein(urb->pipe))
1082 ep = urb->dev->ep_in[epnum];
1083 else
1084 ep = urb->dev->ep_out[epnum];
1085
1086 spin_lock_irqsave (&ehci->lock, flags);
1087 stream = ep->hcpriv;
1088
1089 if (unlikely (stream == NULL)) {
1090 stream = iso_stream_alloc(GFP_ATOMIC);
1091 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 ep->hcpriv = stream;
1093 stream->ep = ep;
1094 iso_stream_init(ehci, stream, urb->dev, urb->pipe,
1095 urb->interval);
1096 }
1097
Clemens Ladisch1082f572010-03-01 17:18:56 +01001098 /* if dev->ep [epnum] is a QH, hw is set */
1099 } else if (unlikely (stream->hw != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1101 urb->dev->devpath, epnum,
1102 usb_pipein(urb->pipe) ? "in" : "out");
1103 stream = NULL;
1104 }
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 spin_unlock_irqrestore (&ehci->lock, flags);
1107 return stream;
1108}
1109
1110/*-------------------------------------------------------------------------*/
1111
1112/* ehci_iso_sched ops can be ITD-only or SITD-only */
1113
1114static struct ehci_iso_sched *
Al Viro55016f12005-10-21 03:21:58 -04001115iso_sched_alloc (unsigned packets, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 struct ehci_iso_sched *iso_sched;
1118 int size = sizeof *iso_sched;
1119
1120 size += packets * sizeof (struct ehci_iso_packet);
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001121 iso_sched = kzalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (likely (iso_sched != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 INIT_LIST_HEAD (&iso_sched->td_list);
1124 }
1125 return iso_sched;
1126}
1127
1128static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001129itd_sched_init(
1130 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 struct ehci_iso_sched *iso_sched,
1132 struct ehci_iso_stream *stream,
1133 struct urb *urb
1134)
1135{
1136 unsigned i;
1137 dma_addr_t dma = urb->transfer_dma;
1138
1139 /* how many uframes are needed for these transfers */
1140 iso_sched->span = urb->number_of_packets * stream->interval;
1141
1142 /* figure out per-uframe itd fields that we'll need later
1143 * when we fit new itds into the schedule.
1144 */
1145 for (i = 0; i < urb->number_of_packets; i++) {
1146 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1147 unsigned length;
1148 dma_addr_t buf;
1149 u32 trans;
1150
1151 length = urb->iso_frame_desc [i].length;
1152 buf = dma + urb->iso_frame_desc [i].offset;
1153
1154 trans = EHCI_ISOC_ACTIVE;
1155 trans |= buf & 0x0fff;
1156 if (unlikely (((i + 1) == urb->number_of_packets))
1157 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1158 trans |= EHCI_ITD_IOC;
1159 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001160 uframe->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
David Brownell77078572005-05-28 10:46:18 -07001162 /* might need to cross a buffer page within a uframe */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 uframe->bufp = (buf & ~(u64)0x0fff);
1164 buf += length;
1165 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1166 uframe->cross = 1;
1167 }
1168}
1169
1170static void
1171iso_sched_free (
1172 struct ehci_iso_stream *stream,
1173 struct ehci_iso_sched *iso_sched
1174)
1175{
1176 if (!iso_sched)
1177 return;
1178 // caller must hold ehci->lock!
1179 list_splice (&iso_sched->td_list, &stream->free_list);
1180 kfree (iso_sched);
1181}
1182
1183static int
1184itd_urb_transaction (
1185 struct ehci_iso_stream *stream,
1186 struct ehci_hcd *ehci,
1187 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001188 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189)
1190{
1191 struct ehci_itd *itd;
1192 dma_addr_t itd_dma;
1193 int i;
1194 unsigned num_itds;
1195 struct ehci_iso_sched *sched;
1196 unsigned long flags;
1197
1198 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1199 if (unlikely (sched == NULL))
1200 return -ENOMEM;
1201
Stefan Roese6dbd6822007-05-01 09:29:37 -07001202 itd_sched_init(ehci, sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 if (urb->interval < 8)
1205 num_itds = 1 + (sched->span + 7) / 8;
1206 else
1207 num_itds = urb->number_of_packets;
1208
1209 /* allocate/init ITDs */
1210 spin_lock_irqsave (&ehci->lock, flags);
1211 for (i = 0; i < num_itds; i++) {
1212
Alan Stern55934eb2012-07-11 11:22:35 -04001213 /*
1214 * Use iTDs from the free list, but not iTDs that may
1215 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Alan Stern55934eb2012-07-11 11:22:35 -04001217 if (likely(!list_empty(&stream->free_list))) {
1218 itd = list_first_entry(&stream->free_list,
Stefan Roese6dbd6822007-05-01 09:29:37 -07001219 struct ehci_itd, itd_list);
Alan Sternf4289072012-07-11 11:23:07 -04001220 if (itd->frame == ehci->now_frame)
Alan Stern55934eb2012-07-11 11:22:35 -04001221 goto alloc_itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 list_del (&itd->itd_list);
1223 itd_dma = itd->itd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001224 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001225 alloc_itd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 spin_unlock_irqrestore (&ehci->lock, flags);
1227 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1228 &itd_dma);
1229 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001230 if (!itd) {
1231 iso_sched_free(stream, sched);
1232 spin_unlock_irqrestore(&ehci->lock, flags);
1233 return -ENOMEM;
1234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 memset (itd, 0, sizeof *itd);
1238 itd->itd_dma = itd_dma;
Soeren Moch85ecd032013-03-22 12:16:52 -04001239 itd->frame = 9999; /* an invalid value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 list_add (&itd->itd_list, &sched->td_list);
1241 }
1242 spin_unlock_irqrestore (&ehci->lock, flags);
1243
1244 /* temporarily store schedule info in hcpriv */
1245 urb->hcpriv = sched;
1246 urb->error_count = 0;
1247 return 0;
1248}
1249
1250/*-------------------------------------------------------------------------*/
1251
1252static inline int
1253itd_slot_ok (
1254 struct ehci_hcd *ehci,
1255 u32 mod,
1256 u32 uframe,
1257 u8 usecs,
1258 u32 period
1259)
1260{
1261 uframe %= period;
1262 do {
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001263 /* can't commit more than uframe_periodic_max usec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001265 > (ehci->uframe_periodic_max - usecs))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return 0;
1267
1268 /* we know urb->interval is 2^N uframes */
1269 uframe += period;
1270 } while (uframe < mod);
1271 return 1;
1272}
1273
1274static inline int
1275sitd_slot_ok (
1276 struct ehci_hcd *ehci,
1277 u32 mod,
1278 struct ehci_iso_stream *stream,
1279 u32 uframe,
1280 struct ehci_iso_sched *sched,
1281 u32 period_uframes
1282)
1283{
1284 u32 mask, tmp;
1285 u32 frame, uf;
1286
1287 mask = stream->raw_mask << (uframe & 7);
1288
1289 /* for IN, don't wrap CSPLIT into the next frame */
1290 if (mask & ~0xffff)
1291 return 0;
1292
Alan Stern65b8e5c2012-05-14 13:47:20 -04001293 /* check bandwidth */
1294 uframe %= period_uframes;
1295 frame = uframe >> 3;
1296
1297#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1298 /* The tt's fullspeed bus bandwidth must be available.
1299 * tt_available scheduling guarantees 10+% for control/bulk.
1300 */
1301 uf = uframe & 7;
1302 if (!tt_available(ehci, period_uframes >> 3,
1303 stream->udev, frame, uf, stream->tt_usecs))
1304 return 0;
1305#else
1306 /* tt must be idle for start(s), any gap, and csplit.
1307 * assume scheduling slop leaves 10+% for control/bulk.
1308 */
1309 if (!tt_no_collision(ehci, period_uframes >> 3,
1310 stream->udev, frame, mask))
1311 return 0;
1312#endif
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /* this multi-pass logic is simple, but performance may
1315 * suffer when the schedule data isn't cached.
1316 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 do {
1318 u32 max_used;
1319
1320 frame = uframe >> 3;
1321 uf = uframe & 7;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /* check starts (OUT uses more than one) */
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001324 max_used = ehci->uframe_periodic_max - stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
1326 if (periodic_usecs (ehci, frame, uf) > max_used)
1327 return 0;
1328 }
1329
1330 /* for IN, check CSPLIT */
1331 if (stream->c_usecs) {
Clemens Ladisch0c734622006-01-22 10:32:49 -08001332 uf = uframe & 7;
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +04001333 max_used = ehci->uframe_periodic_max - stream->c_usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 do {
1335 tmp = 1 << uf;
1336 tmp <<= 8;
1337 if ((stream->raw_mask & tmp) == 0)
1338 continue;
1339 if (periodic_usecs (ehci, frame, uf)
1340 > max_used)
1341 return 0;
1342 } while (++uf < 8);
1343 }
1344
1345 /* we know urb->interval is 2^N uframes */
1346 uframe += period_uframes;
1347 } while (uframe < mod);
1348
Stefan Roese6dbd6822007-05-01 09:29:37 -07001349 stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return 1;
1351}
1352
1353/*
1354 * This scheduler plans almost as far into the future as it has actual
1355 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1356 * "as small as possible" to be cache-friendlier.) That limits the size
1357 * transfers you can stream reliably; avoid more than 64 msec per urb.
1358 * Also avoid queue depths of less than ehci's worst irq latency (affected
1359 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1360 * and other factors); or more than about 230 msec total (for portability,
1361 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1362 */
1363
Alan Sternc3ee9b72012-09-28 16:01:23 -04001364#define SCHEDULING_DELAY 40 /* microframes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366static int
1367iso_stream_schedule (
1368 struct ehci_hcd *ehci,
1369 struct urb *urb,
1370 struct ehci_iso_stream *stream
1371)
1372{
Alan Sternc3ee9b72012-09-28 16:01:23 -04001373 u32 now, base, next, start, period, span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 int status;
1375 unsigned mod = ehci->periodic_size << 3;
1376 struct ehci_iso_sched *sched = urb->hcpriv;
1377
Alan Sternffda0802010-07-14 11:03:46 -04001378 period = urb->interval;
1379 span = sched->span;
1380 if (!stream->highspeed) {
1381 period <<= 3;
1382 span <<= 3;
1383 }
1384
Alan Stern68aa95d2011-10-12 10:39:14 -04001385 now = ehci_read_frame_index(ehci) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Alan Sternb40e43f2008-05-20 16:59:10 -04001387 /* Typical case: reuse current schedule, stream is still active.
1388 * Hopefully there are no gaps from the host falling behind
Alan Stern4005ad42012-10-01 10:32:01 -04001389 * (irq delays etc). If there are, the behavior depends on
1390 * whether URB_ISO_ASAP is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 */
1392 if (likely (!list_empty (&stream->td_list))) {
Sarah Sharpdccd5742009-10-27 10:55:05 -07001393
Alan Stern98cae422012-09-28 16:01:34 -04001394 /* Take the isochronous scheduling threshold into account */
1395 if (ehci->i_thresh)
1396 next = now + ehci->i_thresh; /* uframe cache */
Sarah Sharpdccd5742009-10-27 10:55:05 -07001397 else
Alan Stern98cae422012-09-28 16:01:34 -04001398 next = (now + 2 + 7) & ~0x07; /* full frame cache */
Alan Sternb40e43f2008-05-20 16:59:10 -04001399
Alan Sternc3ee9b72012-09-28 16:01:23 -04001400 /*
1401 * Use ehci->last_iso_frame as the base. There can't be any
1402 * TDs scheduled for earlier than that.
Alan Stern1fb2e052010-07-14 11:03:53 -04001403 */
Alan Sternc3ee9b72012-09-28 16:01:23 -04001404 base = ehci->last_iso_frame << 3;
1405 next = (next - base) & (mod - 1);
1406 start = (stream->next_uframe - base) & (mod - 1);
1407
1408 /* Is the schedule already full? */
1409 if (unlikely(start < period)) {
1410 ehci_dbg(ehci, "iso sched full %p (%u-%u < %u mod %u)\n",
1411 urb, stream->next_uframe, base,
1412 period, mod);
1413 status = -ENOSPC;
Alan Sternb40e43f2008-05-20 16:59:10 -04001414 goto fail;
1415 }
Alan Sternc3ee9b72012-09-28 16:01:23 -04001416
Alan Stern4005ad42012-10-01 10:32:01 -04001417 /* Behind the scheduling threshold? */
1418 if (unlikely(start < next)) {
Alan Stern24f53132013-08-07 10:58:05 -04001419 unsigned now2 = (now - base) & (mod - 1);
Alan Stern4005ad42012-10-01 10:32:01 -04001420
1421 /* USB_ISO_ASAP: Round up to the first available slot */
1422 if (urb->transfer_flags & URB_ISO_ASAP)
1423 start += (next - start + period - 1) & -period;
1424
1425 /*
Alan Stern24f53132013-08-07 10:58:05 -04001426 * Not ASAP: Use the next slot in the stream,
1427 * no matter what.
Alan Stern4005ad42012-10-01 10:32:01 -04001428 */
Alan Stern24f53132013-08-07 10:58:05 -04001429 else if (start + span - period < now2) {
1430 ehci_dbg(ehci, "iso underrun %p (%u+%u < %u)\n",
Alan Stern4005ad42012-10-01 10:32:01 -04001431 urb, start + base,
Alan Stern24f53132013-08-07 10:58:05 -04001432 span - period, now2 + base);
Alan Stern4005ad42012-10-01 10:32:01 -04001433 }
1434 }
Alan Sternc3ee9b72012-09-28 16:01:23 -04001435
1436 start += base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438
1439 /* need to schedule; when's the next (u)frame we could start?
1440 * this is bigger than ehci->i_thresh allows; scheduling itself
Alan Sternc3ee9b72012-09-28 16:01:23 -04001441 * isn't free, the delay should handle reasonably slow cpus. it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 * can also help high bandwidth if the dma and irq loads don't
1443 * jump until after the queue is primed.
1444 */
Alan Stern1fb2e052010-07-14 11:03:53 -04001445 else {
Matthieu CASTETe3420902011-11-28 11:30:22 +01001446 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Alan Sternc3ee9b72012-09-28 16:01:23 -04001448 base = now & ~0x07;
1449 start = base + SCHEDULING_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Thomas Poussevin811c9262011-10-27 18:46:48 +02001451 /* find a uframe slot with enough bandwidth.
1452 * Early uframes are more precious because full-speed
1453 * iso IN transfers can't use late uframes,
1454 * and therefore they should be allocated last.
1455 */
1456 next = start;
1457 start += period;
1458 do {
1459 start--;
Alan Stern1fb2e052010-07-14 11:03:53 -04001460 /* check schedule: enough space? */
1461 if (stream->highspeed) {
1462 if (itd_slot_ok(ehci, mod, start,
1463 stream->usecs, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001464 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001465 } else {
1466 if ((start % 8) >= 6)
1467 continue;
1468 if (sitd_slot_ok(ehci, mod, stream,
1469 start, sched, period))
Matthieu CASTETe3420902011-11-28 11:30:22 +01001470 done = 1;
Alan Stern1fb2e052010-07-14 11:03:53 -04001471 }
Matthieu CASTETe3420902011-11-28 11:30:22 +01001472 } while (start > next && !done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Alan Stern1fb2e052010-07-14 11:03:53 -04001474 /* no room in the schedule */
Matthieu CASTETe3420902011-11-28 11:30:22 +01001475 if (!done) {
Alan Sternc3ee9b72012-09-28 16:01:23 -04001476 ehci_dbg(ehci, "iso sched full %p", urb);
Alan Stern1fb2e052010-07-14 11:03:53 -04001477 status = -ENOSPC;
1478 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480 }
1481
Alan Stern1fb2e052010-07-14 11:03:53 -04001482 /* Tried to schedule too far into the future? */
Alan Sternc3ee9b72012-09-28 16:01:23 -04001483 if (unlikely(start - base + span - period >= mod)) {
1484 ehci_dbg(ehci, "request %p would overflow (%u+%u >= %u)\n",
1485 urb, start - base, span - period, mod);
Alan Stern1fb2e052010-07-14 11:03:53 -04001486 status = -EFBIG;
1487 goto fail;
1488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Alan Stern1fb2e052010-07-14 11:03:53 -04001490 stream->next_uframe = start & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /* report high speed start in uframes; full speed, in frames */
1493 urb->start_frame = stream->next_uframe;
1494 if (!stream->highspeed)
1495 urb->start_frame >>= 3;
Alan Stern569b3942012-07-11 11:23:00 -04001496
1497 /* Make sure scan_isoc() sees these */
1498 if (ehci->isoc_count == 0)
Alan Sternc3ee9b72012-09-28 16:01:23 -04001499 ehci->last_iso_frame = now >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return 0;
Alan Stern1fb2e052010-07-14 11:03:53 -04001501
1502 fail:
1503 iso_sched_free(stream, sched);
1504 urb->hcpriv = NULL;
1505 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
1508/*-------------------------------------------------------------------------*/
1509
1510static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001511itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1512 struct ehci_itd *itd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 int i;
1515
David Brownell77078572005-05-28 10:46:18 -07001516 /* it's been recently zeroed */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001517 itd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 itd->hw_bufp [0] = stream->buf0;
1519 itd->hw_bufp [1] = stream->buf1;
1520 itd->hw_bufp [2] = stream->buf2;
1521
1522 for (i = 0; i < 8; i++)
1523 itd->index[i] = -1;
1524
1525 /* All other fields are filled when scheduling */
1526}
1527
1528static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001529itd_patch(
1530 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 struct ehci_itd *itd,
1532 struct ehci_iso_sched *iso_sched,
1533 unsigned index,
David Brownell77078572005-05-28 10:46:18 -07001534 u16 uframe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535)
1536{
1537 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1538 unsigned pg = itd->pg;
1539
1540 // BUG_ON (pg == 6 && uf->cross);
1541
1542 uframe &= 0x07;
1543 itd->index [uframe] = index;
1544
Stefan Roese6dbd6822007-05-01 09:29:37 -07001545 itd->hw_transaction[uframe] = uf->transaction;
1546 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1547 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1548 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 /* iso_frame_desc[].offset must be strictly increasing */
David Brownell77078572005-05-28 10:46:18 -07001551 if (unlikely (uf->cross)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 u64 bufp = uf->bufp + 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 itd->pg = ++pg;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001555 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1556 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558}
1559
1560static inline void
1561itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1562{
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001563 union ehci_shadow *prev = &ehci->pshadow[frame];
1564 __hc32 *hw_p = &ehci->periodic[frame];
1565 union ehci_shadow here = *prev;
1566 __hc32 type = 0;
1567
1568 /* skip any iso nodes which might belong to previous microframes */
1569 while (here.ptr) {
1570 type = Q_NEXT_TYPE(ehci, *hw_p);
1571 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
1572 break;
1573 prev = periodic_next_shadow(ehci, prev, type);
1574 hw_p = shadow_next_periodic(ehci, &here, type);
1575 here = *prev;
1576 }
1577
1578 itd->itd_next = here;
1579 itd->hw_next = *hw_p;
1580 prev->itd = itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 itd->frame = frame;
1582 wmb ();
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001583 *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586/* fit urb's itds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001587static void itd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 struct ehci_hcd *ehci,
1589 struct urb *urb,
1590 unsigned mod,
1591 struct ehci_iso_stream *stream
1592)
1593{
David Brownell77078572005-05-28 10:46:18 -07001594 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 unsigned next_uframe, uframe, frame;
1596 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1597 struct ehci_itd *itd;
1598
Alan Sternbccbefa2010-07-14 11:03:36 -04001599 next_uframe = stream->next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +03001601 if (unlikely (list_empty(&stream->td_list)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 ehci_to_hcd(ehci)->self.bandwidth_allocated
1603 += stream->bandwidth;
Alex He05570292010-12-07 10:10:08 +08001604
1605 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001606 if (ehci->amd_pll_fix == 1)
1607 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08001608 }
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1611
1612 /* fill iTDs uframe by uframe */
1613 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1614 if (itd == NULL) {
1615 /* ASSERT: we have all necessary itds */
1616 // BUG_ON (list_empty (&iso_sched->td_list));
1617
1618 /* ASSERT: no itds for this endpoint in this uframe */
1619
1620 itd = list_entry (iso_sched->td_list.next,
1621 struct ehci_itd, itd_list);
1622 list_move_tail (&itd->itd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001623 itd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01001624 itd->urb = urb;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001625 itd_init (ehci, stream, itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627
1628 uframe = next_uframe & 0x07;
1629 frame = next_uframe >> 3;
1630
Stefan Roese6dbd6822007-05-01 09:29:37 -07001631 itd_patch(ehci, itd, iso_sched, packet, uframe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 next_uframe += stream->interval;
Alan Sternbccbefa2010-07-14 11:03:36 -04001634 next_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 packet++;
1636
1637 /* link completed itds into the schedule */
1638 if (((next_uframe >> 3) != frame)
1639 || packet == urb->number_of_packets) {
Alan Sternbccbefa2010-07-14 11:03:36 -04001640 itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 itd = NULL;
1642 }
1643 }
1644 stream->next_uframe = next_uframe;
1645
1646 /* don't need that schedule data any more */
1647 iso_sched_free (stream, iso_sched);
Alan Stern2656a9a2012-11-08 10:17:01 -05001648 urb->hcpriv = stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Alan Stern569b3942012-07-11 11:23:00 -04001650 ++ehci->isoc_count;
Alan Sternb015cb72012-07-11 11:22:10 -04001651 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1655
David Brownell30bf54e2007-12-16 22:37:40 -08001656/* Process and recycle a completed ITD. Return true iff its urb completed,
1657 * and hence its completion callback probably added things to the hardware
1658 * schedule.
1659 *
1660 * Note that we carefully avoid recycling this descriptor until after any
1661 * completion callback runs, so that it won't be reused quickly. That is,
1662 * assuming (a) no more than two urbs per frame on this endpoint, and also
1663 * (b) only this endpoint's completions submit URBs. It seems some silicon
1664 * corrupts things if you reuse completed descriptors very quickly...
1665 */
Alan Sternf4289072012-07-11 11:23:07 -04001666static bool itd_complete(struct ehci_hcd *ehci, struct ehci_itd *itd)
1667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 struct urb *urb = itd->urb;
1669 struct usb_iso_packet_descriptor *desc;
1670 u32 t;
1671 unsigned uframe;
1672 int urb_index = -1;
1673 struct ehci_iso_stream *stream = itd->stream;
1674 struct usb_device *dev;
Alan Sternf4289072012-07-11 11:23:07 -04001675 bool retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /* for each uframe with a packet */
1678 for (uframe = 0; uframe < 8; uframe++) {
1679 if (likely (itd->index[uframe] == -1))
1680 continue;
1681 urb_index = itd->index[uframe];
1682 desc = &urb->iso_frame_desc [urb_index];
1683
Stefan Roese6dbd6822007-05-01 09:29:37 -07001684 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 itd->hw_transaction [uframe] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 /* report transfer status */
1688 if (unlikely (t & ISO_ERRS)) {
1689 urb->error_count++;
1690 if (t & EHCI_ISOC_BUF_ERR)
1691 desc->status = usb_pipein (urb->pipe)
1692 ? -ENOSR /* hc couldn't read */
1693 : -ECOMM; /* hc couldn't write */
1694 else if (t & EHCI_ISOC_BABBLE)
1695 desc->status = -EOVERFLOW;
1696 else /* (t & EHCI_ISOC_XACTERR) */
1697 desc->status = -EPROTO;
1698
1699 /* HC need not update length with this error */
Alan Sternec6d67e2009-06-29 14:34:59 -04001700 if (!(t & EHCI_ISOC_BABBLE)) {
1701 desc->actual_length = EHCI_ITD_LENGTH(t);
1702 urb->actual_length += desc->actual_length;
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1705 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04001706 desc->actual_length = EHCI_ITD_LENGTH(t);
1707 urb->actual_length += desc->actual_length;
Alan Sternb40e43f2008-05-20 16:59:10 -04001708 } else {
1709 /* URB was too late */
Alan Stern4005ad42012-10-01 10:32:01 -04001710 urb->error_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712 }
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /* handle completion now? */
1715 if (likely ((urb_index + 1) != urb->number_of_packets))
David Brownell30bf54e2007-12-16 22:37:40 -08001716 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 /* ASSERT: it's really the last itd for this urb
1719 list_for_each_entry (itd, &stream->td_list, itd_list)
1720 BUG_ON (itd->urb == urb);
1721 */
1722
David Brownellaa16ca32007-12-30 23:45:19 -08001723 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05001724 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04001725 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08001726 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Alan Stern569b3942012-07-11 11:23:00 -04001729 --ehci->isoc_count;
1730 disable_periodic(ehci);
1731
1732 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
Alex He05570292010-12-07 10:10:08 +08001733 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001734 if (ehci->amd_pll_fix == 1)
1735 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08001736 }
1737
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +03001738 if (unlikely(list_is_singular(&stream->td_list)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 ehci_to_hcd(ehci)->self.bandwidth_allocated
1740 -= stream->bandwidth;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001741
David Brownell30bf54e2007-12-16 22:37:40 -08001742done:
David Brownell30bf54e2007-12-16 22:37:40 -08001743 itd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04001744
1745 /* Add to the end of the free list for later reuse */
1746 list_move_tail(&itd->itd_list, &stream->free_list);
1747
1748 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
1749 if (list_empty(&stream->td_list)) {
1750 list_splice_tail_init(&stream->free_list,
1751 &ehci->cached_itd_list);
1752 start_free_itds(ehci);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001753 }
Alan Stern55934eb2012-07-11 11:22:35 -04001754
David Brownell30bf54e2007-12-16 22:37:40 -08001755 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
1758/*-------------------------------------------------------------------------*/
1759
Olav Kongas5db539e2005-06-23 20:25:36 +03001760static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001761 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
1763 int status = -EINVAL;
1764 unsigned long flags;
1765 struct ehci_iso_stream *stream;
1766
1767 /* Get iso_stream head */
1768 stream = iso_stream_find (ehci, urb);
1769 if (unlikely (stream == NULL)) {
1770 ehci_dbg (ehci, "can't get iso stream\n");
1771 return -ENOMEM;
1772 }
1773 if (unlikely (urb->interval != stream->interval)) {
1774 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1775 stream->interval, urb->interval);
1776 goto done;
1777 }
1778
1779#ifdef EHCI_URB_TRACE
1780 ehci_dbg (ehci,
1781 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001782 __func__, urb->dev->devpath, urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 usb_pipeendpoint (urb->pipe),
1784 usb_pipein (urb->pipe) ? "in" : "out",
1785 urb->transfer_buffer_length,
1786 urb->number_of_packets, urb->interval,
1787 stream);
1788#endif
1789
1790 /* allocate ITDs w/o locking anything */
1791 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1792 if (unlikely (status < 0)) {
1793 ehci_dbg (ehci, "can't init itds\n");
1794 goto done;
1795 }
1796
1797 /* schedule ... need to lock */
1798 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001799 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001800 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04001801 goto done_not_linked;
1802 }
1803 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1804 if (unlikely(status))
1805 goto done_not_linked;
1806 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07001807 if (likely (status == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04001809 else
1810 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001811 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001813 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return status;
1815}
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/*-------------------------------------------------------------------------*/
1818
1819/*
1820 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1821 * TTs in USB 2.0 hubs. These need microframe scheduling.
1822 */
1823
1824static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001825sitd_sched_init(
1826 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 struct ehci_iso_sched *iso_sched,
1828 struct ehci_iso_stream *stream,
1829 struct urb *urb
1830)
1831{
1832 unsigned i;
1833 dma_addr_t dma = urb->transfer_dma;
1834
1835 /* how many frames are needed for these transfers */
1836 iso_sched->span = urb->number_of_packets * stream->interval;
1837
1838 /* figure out per-frame sitd fields that we'll need later
1839 * when we fit new sitds into the schedule.
1840 */
1841 for (i = 0; i < urb->number_of_packets; i++) {
1842 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1843 unsigned length;
1844 dma_addr_t buf;
1845 u32 trans;
1846
1847 length = urb->iso_frame_desc [i].length & 0x03ff;
1848 buf = dma + urb->iso_frame_desc [i].offset;
1849
1850 trans = SITD_STS_ACTIVE;
1851 if (((i + 1) == urb->number_of_packets)
1852 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1853 trans |= SITD_IOC;
1854 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001855 packet->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 /* might need to cross a buffer page within a td */
1858 packet->bufp = buf;
1859 packet->buf1 = (buf + length) & ~0x0fff;
1860 if (packet->buf1 != (buf & ~(u64)0x0fff))
1861 packet->cross = 1;
1862
David Brownell53bd6a62006-08-30 14:50:06 -07001863 /* OUT uses multiple start-splits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (stream->bEndpointAddress & USB_DIR_IN)
1865 continue;
1866 length = (length + 187) / 188;
1867 if (length > 1) /* BEGIN vs ALL */
1868 length |= 1 << 3;
1869 packet->buf1 |= length;
1870 }
1871}
1872
1873static int
1874sitd_urb_transaction (
1875 struct ehci_iso_stream *stream,
1876 struct ehci_hcd *ehci,
1877 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001878 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879)
1880{
1881 struct ehci_sitd *sitd;
1882 dma_addr_t sitd_dma;
1883 int i;
1884 struct ehci_iso_sched *iso_sched;
1885 unsigned long flags;
1886
1887 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1888 if (iso_sched == NULL)
1889 return -ENOMEM;
1890
Stefan Roese6dbd6822007-05-01 09:29:37 -07001891 sitd_sched_init(ehci, iso_sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 /* allocate/init sITDs */
1894 spin_lock_irqsave (&ehci->lock, flags);
1895 for (i = 0; i < urb->number_of_packets; i++) {
1896
1897 /* NOTE: for now, we don't try to handle wraparound cases
1898 * for IN (using sitd->hw_backpointer, like a FSTN), which
1899 * means we never need two sitds for full speed packets.
1900 */
1901
Alan Stern55934eb2012-07-11 11:22:35 -04001902 /*
1903 * Use siTDs from the free list, but not siTDs that may
1904 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 */
Alan Stern55934eb2012-07-11 11:22:35 -04001906 if (likely(!list_empty(&stream->free_list))) {
1907 sitd = list_first_entry(&stream->free_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 struct ehci_sitd, sitd_list);
Alan Sternf4289072012-07-11 11:23:07 -04001909 if (sitd->frame == ehci->now_frame)
Alan Stern55934eb2012-07-11 11:22:35 -04001910 goto alloc_sitd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 list_del (&sitd->sitd_list);
1912 sitd_dma = sitd->sitd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001913 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001914 alloc_sitd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 spin_unlock_irqrestore (&ehci->lock, flags);
1916 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1917 &sitd_dma);
1918 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001919 if (!sitd) {
1920 iso_sched_free(stream, iso_sched);
1921 spin_unlock_irqrestore(&ehci->lock, flags);
1922 return -ENOMEM;
1923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 memset (sitd, 0, sizeof *sitd);
1927 sitd->sitd_dma = sitd_dma;
Soeren Moch85ecd032013-03-22 12:16:52 -04001928 sitd->frame = 9999; /* an invalid value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 list_add (&sitd->sitd_list, &iso_sched->td_list);
1930 }
1931
1932 /* temporarily store schedule info in hcpriv */
1933 urb->hcpriv = iso_sched;
1934 urb->error_count = 0;
1935
1936 spin_unlock_irqrestore (&ehci->lock, flags);
1937 return 0;
1938}
1939
1940/*-------------------------------------------------------------------------*/
1941
1942static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001943sitd_patch(
1944 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 struct ehci_iso_stream *stream,
1946 struct ehci_sitd *sitd,
1947 struct ehci_iso_sched *iso_sched,
1948 unsigned index
1949)
1950{
1951 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1952 u64 bufp = uf->bufp;
1953
Stefan Roese6dbd6822007-05-01 09:29:37 -07001954 sitd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 sitd->hw_fullspeed_ep = stream->address;
1956 sitd->hw_uframe = stream->splits;
1957 sitd->hw_results = uf->transaction;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001958 sitd->hw_backpointer = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 bufp = uf->bufp;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001961 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
1962 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Stefan Roese6dbd6822007-05-01 09:29:37 -07001964 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (uf->cross)
1966 bufp += 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001967 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 sitd->index = index;
1969}
1970
1971static inline void
1972sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
1973{
1974 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1975 sitd->sitd_next = ehci->pshadow [frame];
1976 sitd->hw_next = ehci->periodic [frame];
1977 ehci->pshadow [frame].sitd = sitd;
1978 sitd->frame = frame;
1979 wmb ();
Stefan Roese6dbd6822007-05-01 09:29:37 -07001980 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
1983/* fit urb's sitds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001984static void sitd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 struct ehci_hcd *ehci,
1986 struct urb *urb,
1987 unsigned mod,
1988 struct ehci_iso_stream *stream
1989)
1990{
1991 int packet;
1992 unsigned next_uframe;
1993 struct ehci_iso_sched *sched = urb->hcpriv;
1994 struct ehci_sitd *sitd;
1995
1996 next_uframe = stream->next_uframe;
1997
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +03001998 if (list_empty(&stream->td_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /* usbfs ignores TT bandwidth */
2000 ehci_to_hcd(ehci)->self.bandwidth_allocated
2001 += stream->bandwidth;
Alex He05570292010-12-07 10:10:08 +08002002
2003 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002004 if (ehci->amd_pll_fix == 1)
2005 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08002006 }
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
2009
2010 /* fill sITDs frame by frame */
2011 for (packet = 0, sitd = NULL;
2012 packet < urb->number_of_packets;
2013 packet++) {
2014
2015 /* ASSERT: we have all necessary sitds */
2016 BUG_ON (list_empty (&sched->td_list));
2017
2018 /* ASSERT: no itds for this endpoint in this frame */
2019
2020 sitd = list_entry (sched->td_list.next,
2021 struct ehci_sitd, sitd_list);
2022 list_move_tail (&sitd->sitd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002023 sitd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01002024 sitd->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Stefan Roese6dbd6822007-05-01 09:29:37 -07002026 sitd_patch(ehci, stream, sitd, sched, packet);
Alan Sternbccbefa2010-07-14 11:03:36 -04002027 sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 sitd);
2029
2030 next_uframe += stream->interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
Alan Sternbccbefa2010-07-14 11:03:36 -04002032 stream->next_uframe = next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 /* don't need that schedule data any more */
2035 iso_sched_free (stream, sched);
Alan Stern2656a9a2012-11-08 10:17:01 -05002036 urb->hcpriv = stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Alan Stern569b3942012-07-11 11:23:00 -04002038 ++ehci->isoc_count;
Alan Sternb015cb72012-07-11 11:22:10 -04002039 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
2042/*-------------------------------------------------------------------------*/
2043
2044#define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
David Brownell53bd6a62006-08-30 14:50:06 -07002045 | SITD_STS_XACT | SITD_STS_MMF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
David Brownell30bf54e2007-12-16 22:37:40 -08002047/* Process and recycle a completed SITD. Return true iff its urb completed,
2048 * and hence its completion callback probably added things to the hardware
2049 * schedule.
2050 *
2051 * Note that we carefully avoid recycling this descriptor until after any
2052 * completion callback runs, so that it won't be reused quickly. That is,
2053 * assuming (a) no more than two urbs per frame on this endpoint, and also
2054 * (b) only this endpoint's completions submit URBs. It seems some silicon
2055 * corrupts things if you reuse completed descriptors very quickly...
2056 */
Alan Sternf4289072012-07-11 11:23:07 -04002057static bool sitd_complete(struct ehci_hcd *ehci, struct ehci_sitd *sitd)
2058{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct urb *urb = sitd->urb;
2060 struct usb_iso_packet_descriptor *desc;
2061 u32 t;
2062 int urb_index = -1;
2063 struct ehci_iso_stream *stream = sitd->stream;
2064 struct usb_device *dev;
Alan Sternf4289072012-07-11 11:23:07 -04002065 bool retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 urb_index = sitd->index;
2068 desc = &urb->iso_frame_desc [urb_index];
Stefan Roese6dbd6822007-05-01 09:29:37 -07002069 t = hc32_to_cpup(ehci, &sitd->hw_results);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 /* report transfer status */
Alan Stern4005ad42012-10-01 10:32:01 -04002072 if (unlikely(t & SITD_ERRS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 urb->error_count++;
2074 if (t & SITD_STS_DBE)
2075 desc->status = usb_pipein (urb->pipe)
2076 ? -ENOSR /* hc couldn't read */
2077 : -ECOMM; /* hc couldn't write */
2078 else if (t & SITD_STS_BABBLE)
2079 desc->status = -EOVERFLOW;
2080 else /* XACT, MMF, etc */
2081 desc->status = -EPROTO;
Alan Stern4005ad42012-10-01 10:32:01 -04002082 } else if (unlikely(t & SITD_STS_ACTIVE)) {
2083 /* URB was too late */
2084 urb->error_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 } else {
2086 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04002087 desc->actual_length = desc->length - SITD_LENGTH(t);
2088 urb->actual_length += desc->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 /* handle completion now? */
2092 if ((urb_index + 1) != urb->number_of_packets)
David Brownell30bf54e2007-12-16 22:37:40 -08002093 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 /* ASSERT: it's really the last sitd for this urb
2096 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2097 BUG_ON (sitd->urb == urb);
2098 */
2099
David Brownellaa16ca32007-12-30 23:45:19 -08002100 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05002101 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04002102 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08002103 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Alan Stern569b3942012-07-11 11:23:00 -04002106 --ehci->isoc_count;
2107 disable_periodic(ehci);
2108
2109 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
Alex He05570292010-12-07 10:10:08 +08002110 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002111 if (ehci->amd_pll_fix == 1)
2112 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08002113 }
2114
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +03002115 if (list_is_singular(&stream->td_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 ehci_to_hcd(ehci)->self.bandwidth_allocated
2117 -= stream->bandwidth;
Alan Stern0e5f2312010-04-08 16:56:37 -04002118
David Brownell30bf54e2007-12-16 22:37:40 -08002119done:
David Brownell30bf54e2007-12-16 22:37:40 -08002120 sitd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04002121
2122 /* Add to the end of the free list for later reuse */
2123 list_move_tail(&sitd->sitd_list, &stream->free_list);
2124
2125 /* Recycle the siTDs when the pipeline is empty (ep no longer in use) */
2126 if (list_empty(&stream->td_list)) {
2127 list_splice_tail_init(&stream->free_list,
2128 &ehci->cached_sitd_list);
2129 start_free_itds(ehci);
Alan Stern0e5f2312010-04-08 16:56:37 -04002130 }
Alan Stern55934eb2012-07-11 11:22:35 -04002131
David Brownell30bf54e2007-12-16 22:37:40 -08002132 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
2135
Olav Kongas5db539e2005-06-23 20:25:36 +03002136static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04002137 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 int status = -EINVAL;
2140 unsigned long flags;
2141 struct ehci_iso_stream *stream;
2142
2143 /* Get iso_stream head */
2144 stream = iso_stream_find (ehci, urb);
2145 if (stream == NULL) {
2146 ehci_dbg (ehci, "can't get iso stream\n");
2147 return -ENOMEM;
2148 }
2149 if (urb->interval != stream->interval) {
2150 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
2151 stream->interval, urb->interval);
2152 goto done;
2153 }
2154
2155#ifdef EHCI_URB_TRACE
2156 ehci_dbg (ehci,
2157 "submit %p dev%s ep%d%s-iso len %d\n",
2158 urb, urb->dev->devpath,
2159 usb_pipeendpoint (urb->pipe),
2160 usb_pipein (urb->pipe) ? "in" : "out",
2161 urb->transfer_buffer_length);
2162#endif
2163
2164 /* allocate SITDs */
2165 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2166 if (status < 0) {
2167 ehci_dbg (ehci, "can't init sitds\n");
2168 goto done;
2169 }
2170
2171 /* schedule ... need to lock */
2172 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04002173 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002174 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04002175 goto done_not_linked;
2176 }
2177 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2178 if (unlikely(status))
2179 goto done_not_linked;
2180 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07002181 if (status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04002183 else
2184 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002185 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002187 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return status;
2189}
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191/*-------------------------------------------------------------------------*/
2192
Alan Stern569b3942012-07-11 11:23:00 -04002193static void scan_isoc(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Alan Sternf4289072012-07-11 11:23:07 -04002195 unsigned uf, now_frame, frame;
2196 unsigned fmask = ehci->periodic_size - 1;
2197 bool modified, live;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 /*
2200 * When running, scan from last scan point up to "now"
2201 * else clean up by scanning everything that's left.
2202 * Touches as few pages as possible: cache-friendly.
2203 */
Alan Sternc0c53db2012-07-11 11:21:48 -04002204 if (ehci->rh_state >= EHCI_RH_RUNNING) {
Alan Sternf4289072012-07-11 11:23:07 -04002205 uf = ehci_read_frame_index(ehci);
2206 now_frame = (uf >> 3) & fmask;
2207 live = true;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002208 } else {
Alan Sternc3ee9b72012-09-28 16:01:23 -04002209 now_frame = (ehci->last_iso_frame - 1) & fmask;
Alan Sternf4289072012-07-11 11:23:07 -04002210 live = false;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002211 }
Alan Sternf4289072012-07-11 11:23:07 -04002212 ehci->now_frame = now_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Alan Sternb09a61c2013-01-30 16:35:02 -05002214 frame = ehci->last_iso_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 for (;;) {
2216 union ehci_shadow q, *q_p;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002217 __hc32 type, *hw_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219restart:
2220 /* scan each element in frame's queue for completions */
2221 q_p = &ehci->pshadow [frame];
2222 hw_p = &ehci->periodic [frame];
2223 q.ptr = q_p->ptr;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002224 type = Q_NEXT_TYPE(ehci, *hw_p);
Alan Sternf4289072012-07-11 11:23:07 -04002225 modified = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 while (q.ptr != NULL) {
Stefan Roese6dbd6822007-05-01 09:29:37 -07002228 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 case Q_TYPE_ITD:
David Brownell79592b722008-01-07 00:47:42 -08002230 /* If this ITD is still active, leave it for
2231 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002232 * No need to check for activity unless the
2233 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002234 */
Alan Sternf4289072012-07-11 11:23:07 -04002235 if (frame == now_frame && live) {
Alan Sternb40e43f2008-05-20 16:59:10 -04002236 rmb();
2237 for (uf = 0; uf < 8; uf++) {
2238 if (q.itd->hw_transaction[uf] &
2239 ITD_ACTIVE(ehci))
2240 break;
2241 }
2242 if (uf < 8) {
Alan Sternb40e43f2008-05-20 16:59:10 -04002243 q_p = &q.itd->itd_next;
2244 hw_p = &q.itd->hw_next;
2245 type = Q_NEXT_TYPE(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -07002246 q.itd->hw_next);
Alan Sternb40e43f2008-05-20 16:59:10 -04002247 q = *q_p;
2248 break;
2249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
David Brownell79592b722008-01-07 00:47:42 -08002252 /* Take finished ITDs out of the schedule
2253 * and process them: recycle, maybe report
2254 * URB completion. HC won't cache the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 * pointer for much longer, if at all.
2256 */
2257 *q_p = q.itd->itd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002258 if (!ehci->use_dummy_qh ||
2259 q.itd->hw_next != EHCI_LIST_END(ehci))
2260 *hw_p = q.itd->hw_next;
2261 else
2262 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002263 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002265 modified = itd_complete (ehci, q.itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 q = *q_p;
2267 break;
2268 case Q_TYPE_SITD:
David Brownell79592b722008-01-07 00:47:42 -08002269 /* If this SITD is still active, leave it for
2270 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002271 * No need to check for activity unless the
2272 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002273 */
Alan Sternf4289072012-07-11 11:23:07 -04002274 if (((frame == now_frame) ||
2275 (((frame + 1) & fmask) == now_frame))
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002276 && live
2277 && (q.sitd->hw_results &
2278 SITD_ACTIVE(ehci))) {
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 q_p = &q.sitd->sitd_next;
2281 hw_p = &q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002282 type = Q_NEXT_TYPE(ehci,
2283 q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 q = *q_p;
2285 break;
2286 }
David Brownell79592b722008-01-07 00:47:42 -08002287
2288 /* Take finished SITDs out of the schedule
2289 * and process them: recycle, maybe report
2290 * URB completion.
2291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 *q_p = q.sitd->sitd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002293 if (!ehci->use_dummy_qh ||
2294 q.sitd->hw_next != EHCI_LIST_END(ehci))
2295 *hw_p = q.sitd->hw_next;
2296 else
2297 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002298 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002300 modified = sitd_complete (ehci, q.sitd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 q = *q_p;
2302 break;
2303 default:
Greg Kroah-Hartman2d0fe1b2012-05-01 21:33:36 -07002304 ehci_dbg(ehci, "corrupt type %d frame %d shadow %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 type, frame, q.ptr);
2306 // BUG ();
Alan Stern569b3942012-07-11 11:23:00 -04002307 /* FALL THROUGH */
2308 case Q_TYPE_QH:
2309 case Q_TYPE_FSTN:
2310 /* End of the iTDs and siTDs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 q.ptr = NULL;
Alan Stern569b3942012-07-11 11:23:00 -04002312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
2314
2315 /* assume completion callbacks modify the queue */
Alan Sternf4289072012-07-11 11:23:07 -04002316 if (unlikely(modified && ehci->isoc_count > 0))
2317 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
2319
Alan Sternf4289072012-07-11 11:23:07 -04002320 /* Stop when we have reached the current frame */
2321 if (frame == now_frame)
David Brownell79592b722008-01-07 00:47:42 -08002322 break;
Alan Sternb09a61c2013-01-30 16:35:02 -05002323
2324 /* The last frame may still have active siTDs */
2325 ehci->last_iso_frame = frame;
2326 frame = (frame + 1) & fmask;
David Brownell53bd6a62006-08-30 14:50:06 -07002327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}