blob: 790a64c0da5c2f0cb5ef5a246534b36cd11e6a4f [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
Alan Sternd0ce5c62013-10-11 11:29:13 -0400109static void bandwidth_dbg(struct ehci_hcd *ehci, int sign, char *type,
110 struct ehci_per_sched *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Alan Sternd0ce5c62013-10-11 11:29:13 -0400112 dev_dbg(&ps->udev->dev,
113 "ep %02x: %s %s @ %u+%u (%u.%u+%u) [%u/%u us] mask %04x\n",
114 ps->ep->desc.bEndpointAddress,
115 (sign >= 0 ? "reserve" : "release"), type,
116 (ps->bw_phase << 3) + ps->phase_uf, ps->bw_uperiod,
117 ps->phase, ps->phase_uf, ps->period,
118 ps->usecs, ps->c_usecs, ps->cs_mask);
119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Alan Sternd0ce5c62013-10-11 11:29:13 -0400121static void reserve_release_intr_bandwidth(struct ehci_hcd *ehci,
122 struct ehci_qh *qh, int sign)
123{
124 unsigned start_uf;
125 unsigned i, j, m;
126 int usecs = qh->ps.usecs;
127 int c_usecs = qh->ps.c_usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Alan Sternd0ce5c62013-10-11 11:29:13 -0400129 if (qh->ps.phase == NO_FRAME) /* Bandwidth wasn't reserved */
130 return;
131 start_uf = qh->ps.bw_phase << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Alan Sternd0ce5c62013-10-11 11:29:13 -0400133 bandwidth_dbg(ehci, sign, "intr", &qh->ps);
134
135 if (sign < 0) { /* Release bandwidth */
136 usecs = -usecs;
137 c_usecs = -c_usecs;
138 }
139
140 /* Entire transaction (high speed) or start-split (full/low speed) */
141 for (i = start_uf + qh->ps.phase_uf; i < EHCI_BANDWIDTH_SIZE;
142 i += qh->ps.bw_uperiod)
143 ehci->bandwidth[i] += usecs;
144
145 /* Complete-split (full/low speed) */
146 if (qh->ps.c_usecs) {
147 /* NOTE: adjustments needed for FSTN */
148 for (i = start_uf; i < EHCI_BANDWIDTH_SIZE;
149 i += qh->ps.bw_uperiod) {
150 for ((j = 2, m = 1 << (j+8)); j < 8; (++j, m <<= 1)) {
151 if (qh->ps.cs_mask & m)
152 ehci->bandwidth[i+j] += c_usecs;
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/*-------------------------------------------------------------------------*/
159
160static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
161{
162 if (!dev1->tt || !dev2->tt)
163 return 0;
164 if (dev1->tt != dev2->tt)
165 return 0;
166 if (dev1->tt->multi)
167 return dev1->ttport == dev2->ttport;
168 else
169 return 1;
170}
171
Dan Streetmanba47f662006-05-24 09:39:16 -0700172#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
173
174/* Which uframe does the low/fullspeed transfer start in?
175 *
176 * The parameter is the mask of ssplits in "H-frame" terms
177 * and this returns the transfer start uframe in "B-frame" terms,
178 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
179 * will cause a transfer in "B-frame" uframe 0. "B-frames" lag
180 * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7.
181 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700182static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask)
Dan Streetmanba47f662006-05-24 09:39:16 -0700183{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700184 unsigned char smask = QH_SMASK & hc32_to_cpu(ehci, mask);
Dan Streetmanba47f662006-05-24 09:39:16 -0700185 if (!smask) {
186 ehci_err(ehci, "invalid empty smask!\n");
187 /* uframe 7 can't have bw so this will indicate failure */
188 return 7;
189 }
190 return ffs(smask) - 1;
191}
192
193static const unsigned char
Alan Sternfdc034382013-05-28 14:03:10 -0400194max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
Dan Streetmanba47f662006-05-24 09:39:16 -0700195
196/* carryover low/fullspeed bandwidth that crosses uframe boundries */
197static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
198{
199 int i;
200 for (i=0; i<7; i++) {
201 if (max_tt_usecs[i] < tt_usecs[i]) {
202 tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i];
203 tt_usecs[i] = max_tt_usecs[i];
204 }
205 }
206}
207
208/* How many of the tt's periodic downstream 1000 usecs are allocated?
209 *
210 * While this measures the bandwidth in terms of usecs/uframe,
211 * the low/fullspeed bus has no notion of uframes, so any particular
212 * low/fullspeed transfer can "carry over" from one uframe to the next,
213 * since the TT just performs downstream transfers in sequence.
214 *
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800215 * For example two separate 100 usec transfers can start in the same uframe,
Dan Streetmanba47f662006-05-24 09:39:16 -0700216 * and the second one would "carry over" 75 usecs into the next uframe.
217 */
218static void
219periodic_tt_usecs (
220 struct ehci_hcd *ehci,
221 struct usb_device *dev,
222 unsigned frame,
223 unsigned short tt_usecs[8]
224)
225{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700226 __hc32 *hw_p = &ehci->periodic [frame];
Dan Streetmanba47f662006-05-24 09:39:16 -0700227 union ehci_shadow *q = &ehci->pshadow [frame];
228 unsigned char uf;
229
230 memset(tt_usecs, 0, 16);
231
232 while (q->ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700233 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
Dan Streetmanba47f662006-05-24 09:39:16 -0700234 case Q_TYPE_ITD:
235 hw_p = &q->itd->hw_next;
236 q = &q->itd->itd_next;
237 continue;
238 case Q_TYPE_QH:
Alan Sternffa02482013-10-11 11:29:03 -0400239 if (same_tt(dev, q->qh->ps.udev)) {
Alek Du3807e262009-07-14 07:23:29 +0800240 uf = tt_start_uframe(ehci, q->qh->hw->hw_info2);
Alan Sternffa02482013-10-11 11:29:03 -0400241 tt_usecs[uf] += q->qh->ps.tt_usecs;
Dan Streetmanba47f662006-05-24 09:39:16 -0700242 }
Alek Du3807e262009-07-14 07:23:29 +0800243 hw_p = &q->qh->hw->hw_next;
Dan Streetmanba47f662006-05-24 09:39:16 -0700244 q = &q->qh->qh_next;
245 continue;
246 case Q_TYPE_SITD:
247 if (same_tt(dev, q->sitd->urb->dev)) {
248 uf = tt_start_uframe(ehci, q->sitd->hw_uframe);
Alan Sternffa02482013-10-11 11:29:03 -0400249 tt_usecs[uf] += q->sitd->stream->ps.tt_usecs;
Dan Streetmanba47f662006-05-24 09:39:16 -0700250 }
251 hw_p = &q->sitd->hw_next;
252 q = &q->sitd->sitd_next;
253 continue;
254 // case Q_TYPE_FSTN:
255 default:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700256 ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n",
257 frame);
Dan Streetmanba47f662006-05-24 09:39:16 -0700258 hw_p = &q->fstn->hw_next;
259 q = &q->fstn->fstn_next;
260 }
261 }
262
263 carryover_tt_bandwidth(tt_usecs);
264
265 if (max_tt_usecs[7] < tt_usecs[7])
266 ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
267 frame, tt_usecs[7] - max_tt_usecs[7]);
268}
269
270/*
271 * Return true if the device's tt's downstream bus is available for a
272 * periodic transfer of the specified length (usecs), starting at the
273 * specified frame/uframe. Note that (as summarized in section 11.19
274 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
275 * uframe.
276 *
277 * The uframe parameter is when the fullspeed/lowspeed transfer
278 * should be executed in "B-frame" terms, which is the same as the
279 * highspeed ssplit's uframe (which is in "H-frame" terms). For example
280 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
281 * See the EHCI spec sec 4.5 and fig 4.7.
282 *
283 * This checks if the full/lowspeed bus, at the specified starting uframe,
284 * has the specified bandwidth available, according to rules listed
285 * in USB 2.0 spec section 11.18.1 fig 11-60.
286 *
287 * This does not check if the transfer would exceed the max ssplit
288 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
289 * since proper scheduling limits ssplits to less than 16 per uframe.
290 */
291static int tt_available (
292 struct ehci_hcd *ehci,
293 unsigned period,
294 struct usb_device *dev,
295 unsigned frame,
296 unsigned uframe,
297 u16 usecs
298)
299{
300 if ((period == 0) || (uframe >= 7)) /* error */
301 return 0;
302
303 for (; frame < ehci->periodic_size; frame += period) {
304 unsigned short tt_usecs[8];
305
306 periodic_tt_usecs (ehci, dev, frame, tt_usecs);
307
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +0300308 if (max_tt_usecs[uframe] <= tt_usecs[uframe])
Dan Streetmanba47f662006-05-24 09:39:16 -0700309 return 0;
Dan Streetmanba47f662006-05-24 09:39:16 -0700310
311 /* special case for isoc transfers larger than 125us:
312 * the first and each subsequent fully used uframe
313 * must be empty, so as to not illegally delay
314 * already scheduled transactions
315 */
316 if (125 < usecs) {
Dan Streetmanc065c602009-04-21 13:37:12 -0400317 int ufs = (usecs / 125);
Dan Streetmanba47f662006-05-24 09:39:16 -0700318 int i;
319 for (i = uframe; i < (uframe + ufs) && i < 8; i++)
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +0300320 if (0 < tt_usecs[i])
Dan Streetmanba47f662006-05-24 09:39:16 -0700321 return 0;
Dan Streetmanba47f662006-05-24 09:39:16 -0700322 }
323
324 tt_usecs[uframe] += usecs;
325
326 carryover_tt_bandwidth(tt_usecs);
327
328 /* fail if the carryover pushed bw past the last uframe's limit */
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +0300329 if (max_tt_usecs[7] < tt_usecs[7])
Dan Streetmanba47f662006-05-24 09:39:16 -0700330 return 0;
Dan Streetmanba47f662006-05-24 09:39:16 -0700331 }
332
333 return 1;
334}
335
336#else
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/* return true iff the device's transaction translator is available
339 * for a periodic transfer starting at the specified frame, using
340 * all the uframes in the mask.
341 */
342static int tt_no_collision (
343 struct ehci_hcd *ehci,
344 unsigned period,
345 struct usb_device *dev,
346 unsigned frame,
347 u32 uf_mask
348)
349{
350 if (period == 0) /* error */
351 return 0;
352
353 /* note bandwidth wastage: split never follows csplit
354 * (different dev or endpoint) until the next uframe.
355 * calling convention doesn't make that distinction.
356 */
357 for (; frame < ehci->periodic_size; frame += period) {
358 union ehci_shadow here;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700359 __hc32 type;
Alek Du3807e262009-07-14 07:23:29 +0800360 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 here = ehci->pshadow [frame];
Stefan Roese6dbd6822007-05-01 09:29:37 -0700363 type = Q_NEXT_TYPE(ehci, ehci->periodic [frame]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700365 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 case Q_TYPE_ITD:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700367 type = Q_NEXT_TYPE(ehci, here.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 here = here.itd->itd_next;
369 continue;
370 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800371 hw = here.qh->hw;
Alan Sternffa02482013-10-11 11:29:03 -0400372 if (same_tt(dev, here.qh->ps.udev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 u32 mask;
374
Stefan Roese6dbd6822007-05-01 09:29:37 -0700375 mask = hc32_to_cpu(ehci,
Alek Du3807e262009-07-14 07:23:29 +0800376 hw->hw_info2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* "knows" no gap is needed */
378 mask |= mask >> 8;
379 if (mask & uf_mask)
380 break;
381 }
Alek Du3807e262009-07-14 07:23:29 +0800382 type = Q_NEXT_TYPE(ehci, hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 here = here.qh->qh_next;
384 continue;
385 case Q_TYPE_SITD:
386 if (same_tt (dev, here.sitd->urb->dev)) {
387 u16 mask;
388
Stefan Roese6dbd6822007-05-01 09:29:37 -0700389 mask = hc32_to_cpu(ehci, here.sitd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 ->hw_uframe);
391 /* FIXME assumes no gap for IN! */
392 mask |= mask >> 8;
393 if (mask & uf_mask)
394 break;
395 }
Stefan Roese6dbd6822007-05-01 09:29:37 -0700396 type = Q_NEXT_TYPE(ehci, here.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 here = here.sitd->sitd_next;
398 continue;
399 // case Q_TYPE_FSTN:
400 default:
401 ehci_dbg (ehci,
402 "periodic frame %d bogus type %d\n",
403 frame, type);
404 }
405
406 /* collision or error */
407 return 0;
408 }
409 }
410
411 /* no collision */
412 return 1;
413}
414
Dan Streetmanba47f662006-05-24 09:39:16 -0700415#endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/*-------------------------------------------------------------------------*/
418
Alan Sternb015cb72012-07-11 11:22:10 -0400419static void enable_periodic(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400421 if (ehci->periodic_count++)
Alan Sternb015cb72012-07-11 11:22:10 -0400422 return;
David Brownell01c17142008-08-26 23:35:04 -0700423
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400424 /* Stop waiting to turn off the periodic schedule */
425 ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_DISABLE_PERIODIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400427 /* Don't start the schedule until PSS is 0 */
428 ehci_poll_PSS(ehci);
Alan Stern18aafe62012-07-11 11:23:04 -0400429 turn_on_io_watchdog(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Alan Sternb015cb72012-07-11 11:22:10 -0400432static void disable_periodic(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400434 if (--ehci->periodic_count)
Alan Sternb015cb72012-07-11 11:22:10 -0400435 return;
David Brownell01c17142008-08-26 23:35:04 -0700436
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400437 /* Don't turn off the schedule until PSS is 1 */
438 ehci_poll_PSS(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441/*-------------------------------------------------------------------------*/
442
443/* periodic schedule slots have iso tds (normal or split) first, then a
444 * sparse tree for active interrupt transfers.
445 *
446 * this just links in a qh; caller guarantees uframe masks are set right.
447 * no FSTN support (yet; ehci 0.96+)
448 */
Alan Sternb015cb72012-07-11 11:22:10 -0400449static void qh_link_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 unsigned i;
Alan Sternffa02482013-10-11 11:29:03 -0400452 unsigned period = qh->ps.period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Alan Sternffa02482013-10-11 11:29:03 -0400454 dev_dbg(&qh->ps.udev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 "link qh%d-%04x/%p start %d [%d/%d us]\n",
Alek Du3807e262009-07-14 07:23:29 +0800456 period, hc32_to_cpup(ehci, &qh->hw->hw_info2)
457 & (QH_CMASK | QH_SMASK),
Alan Sternffa02482013-10-11 11:29:03 -0400458 qh, qh->ps.phase, qh->ps.usecs, qh->ps.c_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /* high bandwidth, or otherwise every microframe */
461 if (period == 0)
462 period = 1;
463
Alan Sternffa02482013-10-11 11:29:03 -0400464 for (i = qh->ps.phase; i < ehci->periodic_size; i += period) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700465 union ehci_shadow *prev = &ehci->pshadow[i];
466 __hc32 *hw_p = &ehci->periodic[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 union ehci_shadow here = *prev;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700468 __hc32 type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /* skip the iso nodes at list head */
471 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700472 type = Q_NEXT_TYPE(ehci, *hw_p);
473 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700475 prev = periodic_next_shadow(ehci, prev, type);
Alek Du3807e262009-07-14 07:23:29 +0800476 hw_p = shadow_next_periodic(ehci, &here, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 here = *prev;
478 }
479
480 /* sorting each branch by period (slow-->fast)
481 * enables sharing interior tree nodes
482 */
483 while (here.ptr && qh != here.qh) {
Alan Sternffa02482013-10-11 11:29:03 -0400484 if (qh->ps.period > here.qh->ps.period)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486 prev = &here.qh->qh_next;
Alek Du3807e262009-07-14 07:23:29 +0800487 hw_p = &here.qh->hw->hw_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 here = *prev;
489 }
490 /* link in this qh, unless some earlier pass did that */
491 if (qh != here.qh) {
492 qh->qh_next = here;
493 if (here.qh)
Alek Du3807e262009-07-14 07:23:29 +0800494 qh->hw->hw_next = *hw_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 wmb ();
496 prev->qh = qh;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700497 *hw_p = QH_NEXT (ehci, qh->qh_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 }
500 qh->qh_state = QH_STATE_LINKED;
Alan Sternef4638f2009-07-31 10:41:40 -0400501 qh->xacterrs = 0;
Alan Stern7bc782d2013-03-22 13:31:11 -0400502 qh->exception = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Alan Sternffa02482013-10-11 11:29:03 -0400504 /* update per-qh bandwidth for debugfs */
Alan Sternd0ce5c62013-10-11 11:29:13 -0400505 ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->ps.bw_period
506 ? ((qh->ps.usecs + qh->ps.c_usecs) / qh->ps.bw_period)
Alan Sternffa02482013-10-11 11:29:03 -0400507 : (qh->ps.usecs * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Alan Stern569b3942012-07-11 11:23:00 -0400509 list_add(&qh->intr_node, &ehci->intr_qh_list);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* maybe enable periodic schedule processing */
Alan Stern569b3942012-07-11 11:23:00 -0400512 ++ehci->intr_count;
Alan Sternb015cb72012-07-11 11:22:10 -0400513 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Alan Sternb015cb72012-07-11 11:22:10 -0400516static void qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 unsigned i;
519 unsigned period;
520
Alan Sterndf202252012-07-11 11:22:26 -0400521 /*
522 * If qh is for a low/full-speed device, simply unlinking it
523 * could interfere with an ongoing split transaction. To unlink
524 * it safely would require setting the QH_INACTIVATE bit and
525 * waiting at least one frame, as described in EHCI 4.12.2.5.
526 *
527 * We won't bother with any of this. Instead, we assume that the
528 * only reason for unlinking an interrupt QH while the current URB
529 * is still active is to dequeue all the URBs (flush the whole
530 * endpoint queue).
531 *
532 * If rebalancing the periodic schedule is ever implemented, this
533 * approach will no longer be valid.
534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* high bandwidth, or otherwise part of every microframe */
Alan Sternffa02482013-10-11 11:29:03 -0400537 period = qh->ps.period ? : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Alan Sternffa02482013-10-11 11:29:03 -0400539 for (i = qh->ps.phase; i < ehci->periodic_size; i += period)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 periodic_unlink (ehci, i, qh);
541
Alan Sternffa02482013-10-11 11:29:03 -0400542 /* update per-qh bandwidth for debugfs */
Alan Sternd0ce5c62013-10-11 11:29:13 -0400543 ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->ps.bw_period
544 ? ((qh->ps.usecs + qh->ps.c_usecs) / qh->ps.bw_period)
Alan Sternffa02482013-10-11 11:29:03 -0400545 : (qh->ps.usecs * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Alan Sternffa02482013-10-11 11:29:03 -0400547 dev_dbg(&qh->ps.udev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
Alan Sternffa02482013-10-11 11:29:03 -0400549 qh->ps.period,
Alek Du3807e262009-07-14 07:23:29 +0800550 hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
Alan Sternffa02482013-10-11 11:29:03 -0400551 qh, qh->ps.phase, qh->ps.usecs, qh->ps.c_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /* qh->qh_next still "live" to HC */
554 qh->qh_state = QH_STATE_UNLINK;
555 qh->qh_next.ptr = NULL;
Alan Stern569b3942012-07-11 11:23:00 -0400556
557 if (ehci->qh_scan_next == qh)
558 ehci->qh_scan_next = list_entry(qh->intr_node.next,
559 struct ehci_qh, intr_node);
560 list_del(&qh->intr_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Ming Lei9118f9e2013-07-03 22:53:10 +0800563static void cancel_unlink_wait_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
564{
565 if (qh->qh_state != QH_STATE_LINKED ||
566 list_empty(&qh->unlink_node))
567 return;
568
569 list_del_init(&qh->unlink_node);
570
571 /*
572 * TODO: disable the event of EHCI_HRTIMER_START_UNLINK_INTR for
573 * avoiding unnecessary CPU wakeup
574 */
575}
576
Alan Sterndf202252012-07-11 11:22:26 -0400577static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Alan Stern7bc782d2013-03-22 13:31:11 -0400579 /* If the QH isn't linked then there's nothing we can do. */
580 if (qh->qh_state != QH_STATE_LINKED)
Alan Sterna448c9d2009-08-19 12:22:44 -0400581 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Ming Lei9118f9e2013-07-03 22:53:10 +0800583 /* if the qh is waiting for unlink, cancel it now */
584 cancel_unlink_wait_intr(ehci, qh);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 qh_unlink_periodic (ehci, qh);
587
Alan Sterndf202252012-07-11 11:22:26 -0400588 /* Make sure the unlinks are visible before starting the timer */
589 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Alan Sterndf202252012-07-11 11:22:26 -0400591 /*
592 * The EHCI spec doesn't say how long it takes the controller to
593 * stop accessing an unlinked interrupt QH. The timer delay is
594 * 9 uframes; presumably that will be long enough.
595 */
596 qh->unlink_cycle = ehci->intr_unlink_cycle;
597
598 /* New entries go at the end of the intr_unlink list */
Alan Stern6e018752013-03-22 13:31:45 -0400599 list_add_tail(&qh->unlink_node, &ehci->intr_unlink);
Alan Sterndf202252012-07-11 11:22:26 -0400600
601 if (ehci->intr_unlinking)
602 ; /* Avoid recursive calls */
603 else if (ehci->rh_state < EHCI_RH_RUNNING)
604 ehci_handle_intr_unlinks(ehci);
Alan Stern6e018752013-03-22 13:31:45 -0400605 else if (ehci->intr_unlink.next == &qh->unlink_node) {
Alan Sterndf202252012-07-11 11:22:26 -0400606 ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
607 ++ehci->intr_unlink_cycle;
608 }
609}
610
Ming Lei9118f9e2013-07-03 22:53:10 +0800611/*
612 * It is common only one intr URB is scheduled on one qh, and
613 * given complete() is run in tasklet context, introduce a bit
614 * delay to avoid unlink qh too early.
615 */
616static void start_unlink_intr_wait(struct ehci_hcd *ehci,
617 struct ehci_qh *qh)
618{
619 qh->unlink_cycle = ehci->intr_unlink_wait_cycle;
620
621 /* New entries go at the end of the intr_unlink_wait list */
622 list_add_tail(&qh->unlink_node, &ehci->intr_unlink_wait);
623
624 if (ehci->rh_state < EHCI_RH_RUNNING)
625 ehci_handle_start_intr_unlinks(ehci);
626 else if (ehci->intr_unlink_wait.next == &qh->unlink_node) {
627 ehci_enable_event(ehci, EHCI_HRTIMER_START_UNLINK_INTR, true);
628 ++ehci->intr_unlink_wait_cycle;
629 }
630}
631
Alan Sterndf202252012-07-11 11:22:26 -0400632static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
633{
634 struct ehci_qh_hw *hw = qh->hw;
635 int rc;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 qh->qh_state = QH_STATE_IDLE;
Alek Du3807e262009-07-14 07:23:29 +0800638 hw->hw_next = EHCI_LIST_END(ehci);
Alan Sterna448c9d2009-08-19 12:22:44 -0400639
Alan Stern79bcf7b2013-03-22 13:30:56 -0400640 if (!list_empty(&qh->qtd_list))
641 qh_completions(ehci, qh);
Alan Sterna448c9d2009-08-19 12:22:44 -0400642
643 /* reschedule QH iff another request is queued */
Alan Sterndf202252012-07-11 11:22:26 -0400644 if (!list_empty(&qh->qtd_list) && ehci->rh_state == EHCI_RH_RUNNING) {
Alan Sterna448c9d2009-08-19 12:22:44 -0400645 rc = qh_schedule(ehci, qh);
Alan Stern077f5f12013-05-29 11:33:52 -0400646 if (rc == 0) {
647 qh_refresh(ehci, qh);
648 qh_link_periodic(ehci, qh);
649 }
Alan Sterna448c9d2009-08-19 12:22:44 -0400650
651 /* An error here likely indicates handshake failure
652 * or no space left in the schedule. Neither fault
653 * should happen often ...
654 *
655 * FIXME kill the now-dysfunctional queued urbs
656 */
Alan Stern077f5f12013-05-29 11:33:52 -0400657 else {
Alan Sterna448c9d2009-08-19 12:22:44 -0400658 ehci_err(ehci, "can't reschedule qh %p, err %d\n",
659 qh, rc);
Alan Stern077f5f12013-05-29 11:33:52 -0400660 }
Alan Sterna448c9d2009-08-19 12:22:44 -0400661 }
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400662
663 /* maybe turn off periodic schedule */
Alan Stern569b3942012-07-11 11:23:00 -0400664 --ehci->intr_count;
Alan Stern3ca9aeb2012-07-11 11:22:05 -0400665 disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668/*-------------------------------------------------------------------------*/
669
670static int check_period (
David Brownell53bd6a62006-08-30 14:50:06 -0700671 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 unsigned frame,
673 unsigned uframe,
Alan Sternd0ce5c62013-10-11 11:29:13 -0400674 unsigned uperiod,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 unsigned usecs
676) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* complete split running into next frame?
678 * given FSTN support, we could sometimes check...
679 */
680 if (uframe >= 8)
681 return 0;
682
Kirill Smelkovcc62a7e2011-07-03 20:36:57 +0400683 /* convert "usecs we need" to "max already claimed" */
684 usecs = ehci->uframe_periodic_max - usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Alan Sternd0ce5c62013-10-11 11:29:13 -0400686 for (uframe += frame << 3; uframe < EHCI_BANDWIDTH_SIZE;
687 uframe += uperiod) {
688 if (ehci->bandwidth[uframe] > usecs)
689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
692 // success!
693 return 1;
694}
695
696static int check_intr_schedule (
David Brownell53bd6a62006-08-30 14:50:06 -0700697 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 unsigned frame,
699 unsigned uframe,
700 const struct ehci_qh *qh,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700701 __hc32 *c_maskp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702)
703{
David Brownell53bd6a62006-08-30 14:50:06 -0700704 int retval = -ENOSPC;
Dan Streetmanba47f662006-05-24 09:39:16 -0700705 u8 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Alan Sternffa02482013-10-11 11:29:03 -0400707 if (qh->ps.c_usecs && uframe >= 6) /* FSTN territory? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 goto done;
709
Alan Sternd0ce5c62013-10-11 11:29:13 -0400710 if (!check_period(ehci, frame, uframe, qh->ps.bw_uperiod, qh->ps.usecs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 goto done;
Alan Sternffa02482013-10-11 11:29:03 -0400712 if (!qh->ps.c_usecs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 retval = 0;
714 *c_maskp = 0;
715 goto done;
716 }
717
Dan Streetmanba47f662006-05-24 09:39:16 -0700718#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
Alan Sternd0ce5c62013-10-11 11:29:13 -0400719 if (tt_available(ehci, qh->ps.bw_period, qh->ps.udev, frame, uframe,
Alan Sternffa02482013-10-11 11:29:03 -0400720 qh->ps.tt_usecs)) {
Dan Streetmanba47f662006-05-24 09:39:16 -0700721 unsigned i;
722
723 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
Alan Sterne24371a2013-10-11 11:28:12 -0400724 for (i = uframe+2; i < 8 && i <= uframe+4; i++)
Alan Sternffa02482013-10-11 11:29:03 -0400725 if (!check_period(ehci, frame, i,
Alan Sternd0ce5c62013-10-11 11:29:13 -0400726 qh->ps.bw_uperiod, qh->ps.c_usecs))
Dan Streetmanba47f662006-05-24 09:39:16 -0700727 goto done;
728 else
729 mask |= 1 << i;
730
731 retval = 0;
732
Alan Sternd0ce5c62013-10-11 11:29:13 -0400733 *c_maskp = mask;
Dan Streetmanba47f662006-05-24 09:39:16 -0700734 }
735#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* Make sure this tt's buffer is also available for CSPLITs.
737 * We pessimize a bit; probably the typical full speed case
738 * doesn't need the second CSPLIT.
David Brownell53bd6a62006-08-30 14:50:06 -0700739 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * NOTE: both SPLIT and CSPLIT could be checked in just
741 * one smart pass...
742 */
743 mask = 0x03 << (uframe + qh->gap_uf);
Alan Sternd0ce5c62013-10-11 11:29:13 -0400744 *c_maskp = mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 mask |= 1 << uframe;
Alan Sternd0ce5c62013-10-11 11:29:13 -0400747 if (tt_no_collision(ehci, qh->ps.bw_period, qh->ps.udev, frame, mask)) {
Alan Sternffa02482013-10-11 11:29:03 -0400748 if (!check_period(ehci, frame, uframe + qh->gap_uf + 1,
Alan Sternd0ce5c62013-10-11 11:29:13 -0400749 qh->ps.bw_uperiod, qh->ps.c_usecs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 goto done;
Alan Sternffa02482013-10-11 11:29:03 -0400751 if (!check_period(ehci, frame, uframe + qh->gap_uf,
Alan Sternd0ce5c62013-10-11 11:29:13 -0400752 qh->ps.bw_uperiod, qh->ps.c_usecs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 goto done;
754 retval = 0;
755 }
Dan Streetmanba47f662006-05-24 09:39:16 -0700756#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757done:
758 return retval;
759}
760
761/* "first fit" scheduling policy used the first time through,
762 * or when the previous schedule slot can't be re-used.
763 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700764static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
David Brownell53bd6a62006-08-30 14:50:06 -0700766 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 unsigned uframe;
Alan Sternd0ce5c62013-10-11 11:29:13 -0400768 unsigned c_mask;
Alek Du3807e262009-07-14 07:23:29 +0800769 struct ehci_qh_hw *hw = qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Alek Du3807e262009-07-14 07:23:29 +0800771 hw->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /* reuse the previous schedule slots, if we can */
Alan Sternd0ce5c62013-10-11 11:29:13 -0400774 if (qh->ps.phase != NO_FRAME) {
775 ehci_dbg(ehci, "reused qh %p schedule\n", qh);
776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778
Alan Sternd0ce5c62013-10-11 11:29:13 -0400779 uframe = 0;
780 c_mask = 0;
781 status = -ENOSPC;
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* else scan the schedule to find a group of slots such that all
784 * uframes have enough periodic bandwidth available.
785 */
Alan Sternd0ce5c62013-10-11 11:29:13 -0400786 /* "normal" case, uframing flexible except with splits */
787 if (qh->ps.bw_period) {
788 int i;
789 unsigned frame;
Alan Stern68335e82009-05-22 17:02:33 -0400790
Alan Sternd0ce5c62013-10-11 11:29:13 -0400791 for (i = qh->ps.bw_period; status && i > 0; --i) {
792 frame = ++ehci->random_frame & (qh->ps.bw_period - 1);
793 for (uframe = 0; uframe < 8; uframe++) {
794 status = check_intr_schedule(ehci,
795 frame, uframe, qh, &c_mask);
796 if (status == 0)
797 break;
Alan Stern68335e82009-05-22 17:02:33 -0400798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Alan Sternd0ce5c62013-10-11 11:29:13 -0400801 /* qh->ps.bw_period == 0 means every uframe */
802 } else {
803 status = check_intr_schedule(ehci, 0, 0, qh, &c_mask);
804 }
805 if (status)
806 goto done;
807 qh->ps.phase = (qh->ps.period ? ehci->random_frame &
808 (qh->ps.period - 1) : 0);
809 qh->ps.bw_phase = qh->ps.phase & (qh->ps.bw_period - 1);
810 qh->ps.phase_uf = uframe;
811 qh->ps.cs_mask = qh->ps.period ?
812 (c_mask << 8) | (1 << uframe) :
813 QH_SMASK;
814
815 /* reset S-frame and (maybe) C-frame masks */
816 hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
817 hw->hw_info2 |= cpu_to_hc32(ehci, qh->ps.cs_mask);
818 reserve_release_intr_bandwidth(ehci, qh, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820done:
821 return status;
822}
823
824static int intr_submit (
825 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct urb *urb,
827 struct list_head *qtd_list,
Al Viro55016f12005-10-21 03:21:58 -0400828 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829) {
830 unsigned epnum;
831 unsigned long flags;
832 struct ehci_qh *qh;
Alan Sterne9df41c2007-08-08 11:48:02 -0400833 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 struct list_head empty;
835
836 /* get endpoint and transfer/schedule data */
Alan Sterne9df41c2007-08-08 11:48:02 -0400837 epnum = urb->ep->desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 spin_lock_irqsave (&ehci->lock, flags);
840
Alan Stern541c7d42010-06-22 16:39:10 -0400841 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100842 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -0400843 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100844 }
Alan Sterne9df41c2007-08-08 11:48:02 -0400845 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
846 if (unlikely(status))
847 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* get qh and force any scheduling errors */
850 INIT_LIST_HEAD (&empty);
Alan Sterne9df41c2007-08-08 11:48:02 -0400851 qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (qh == NULL) {
853 status = -ENOMEM;
854 goto done;
855 }
856 if (qh->qh_state == QH_STATE_IDLE) {
857 if ((status = qh_schedule (ehci, qh)) != 0)
858 goto done;
859 }
860
861 /* then queue the urb's tds to the qh */
Alan Sterne9df41c2007-08-08 11:48:02 -0400862 qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 BUG_ON (qh == NULL);
864
Alan Sternc1fdb682013-03-22 13:30:43 -0400865 /* stuff into the periodic schedule */
866 if (qh->qh_state == QH_STATE_IDLE) {
867 qh_refresh(ehci, qh);
868 qh_link_periodic(ehci, qh);
Ming Lei9118f9e2013-07-03 22:53:10 +0800869 } else {
870 /* cancel unlink wait for the qh */
871 cancel_unlink_wait_intr(ehci, qh);
Alan Sternc1fdb682013-03-22 13:30:43 -0400872 }
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /* ... update usbfs periodic stats */
875 ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
876
877done:
Alan Sterne9df41c2007-08-08 11:48:02 -0400878 if (unlikely(status))
879 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
880done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 spin_unlock_irqrestore (&ehci->lock, flags);
882 if (status)
883 qtd_list_free (ehci, urb, qtd_list);
884
885 return status;
886}
887
Alan Stern569b3942012-07-11 11:23:00 -0400888static void scan_intr(struct ehci_hcd *ehci)
889{
890 struct ehci_qh *qh;
891
892 list_for_each_entry_safe(qh, ehci->qh_scan_next, &ehci->intr_qh_list,
893 intr_node) {
Alan Stern79bcf7b2013-03-22 13:30:56 -0400894
Alan Stern569b3942012-07-11 11:23:00 -0400895 /* clean any finished work for this qh */
896 if (!list_empty(&qh->qtd_list)) {
897 int temp;
898
899 /*
900 * Unlinks could happen here; completion reporting
901 * drops the lock. That's why ehci->qh_scan_next
902 * always holds the next qh to scan; if the next qh
903 * gets unlinked then ehci->qh_scan_next is adjusted
904 * in qh_unlink_periodic().
905 */
906 temp = qh_completions(ehci, qh);
Ming Lei9118f9e2013-07-03 22:53:10 +0800907 if (unlikely(temp))
Alan Stern569b3942012-07-11 11:23:00 -0400908 start_unlink_intr(ehci, qh);
Ming Lei9118f9e2013-07-03 22:53:10 +0800909 else if (unlikely(list_empty(&qh->qtd_list) &&
910 qh->qh_state == QH_STATE_LINKED))
911 start_unlink_intr_wait(ehci, qh);
Alan Stern569b3942012-07-11 11:23:00 -0400912 }
913 }
914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/*-------------------------------------------------------------------------*/
917
918/* ehci_iso_stream ops work with both ITD and SITD */
919
920static struct ehci_iso_stream *
Al Viro55016f12005-10-21 03:21:58 -0400921iso_stream_alloc (gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct ehci_iso_stream *stream;
924
Pekka Enberg7b842b62005-09-06 15:18:34 -0700925 stream = kzalloc(sizeof *stream, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 INIT_LIST_HEAD(&stream->td_list);
928 INIT_LIST_HEAD(&stream->free_list);
Alan Stern91a99b52013-10-11 11:28:52 -0400929 stream->next_uframe = NO_FRAME;
Alan Sternd0ce5c62013-10-11 11:29:13 -0400930 stream->ps.phase = NO_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 return stream;
933}
934
935static void
936iso_stream_init (
937 struct ehci_hcd *ehci,
938 struct ehci_iso_stream *stream,
Alan Sternffa02482013-10-11 11:29:03 -0400939 struct urb *urb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940)
941{
942 static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
943
Alan Sternffa02482013-10-11 11:29:03 -0400944 struct usb_device *dev = urb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 u32 buf1;
946 unsigned epnum, maxp;
947 int is_input;
Alan Sternd0ce5c62013-10-11 11:29:13 -0400948 unsigned tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /*
951 * this might be a "high bandwidth" highspeed endpoint,
952 * as encoded in the ep descriptor's wMaxPacket field
953 */
Alan Sternffa02482013-10-11 11:29:03 -0400954 epnum = usb_pipeendpoint(urb->pipe);
955 is_input = usb_pipein(urb->pipe) ? USB_DIR_IN : 0;
956 maxp = usb_endpoint_maxp(&urb->ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (is_input) {
958 buf1 = (1 << 11);
959 } else {
960 buf1 = 0;
961 }
962
963 /* knows about ITD vs SITD */
964 if (dev->speed == USB_SPEED_HIGH) {
965 unsigned multi = hb_mult(maxp);
966
967 stream->highspeed = 1;
968
969 maxp = max_packet(maxp);
970 buf1 |= maxp;
971 maxp *= multi;
972
Stefan Roese6dbd6822007-05-01 09:29:37 -0700973 stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
974 stream->buf1 = cpu_to_hc32(ehci, buf1);
975 stream->buf2 = cpu_to_hc32(ehci, multi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /* usbfs wants to report the average usecs per frame tied up
978 * when transfers on this endpoint are scheduled ...
979 */
Alan Sternffa02482013-10-11 11:29:03 -0400980 stream->ps.usecs = HS_USECS_ISO(maxp);
981
Alan Sternd0ce5c62013-10-11 11:29:13 -0400982 /* period for bandwidth allocation */
983 tmp = min_t(unsigned, EHCI_BANDWIDTH_SIZE,
984 1 << (urb->ep->desc.bInterval - 1));
985
986 /* Allow urb->interval to override */
987 stream->ps.bw_uperiod = min_t(unsigned, tmp, urb->interval);
988
989 stream->uperiod = urb->interval;
990 stream->ps.period = urb->interval >> 3;
991 stream->bandwidth = stream->ps.usecs * 8 /
992 stream->ps.bw_uperiod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 } else {
995 u32 addr;
david-b@pacbell.netd0384202005-08-13 18:44:58 -0700996 int think_time;
Clemens Ladisch469d0222006-01-20 13:49:10 -0800997 int hs_transfers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 addr = dev->ttport << 24;
1000 if (!ehci_is_TDI(ehci)
1001 || (dev->tt->hub !=
1002 ehci_to_hcd(ehci)->self.root_hub))
1003 addr |= dev->tt->hub->devnum << 16;
1004 addr |= epnum << 8;
1005 addr |= dev->devnum;
Alan Sternffa02482013-10-11 11:29:03 -04001006 stream->ps.usecs = HS_USECS_ISO(maxp);
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001007 think_time = dev->tt ? dev->tt->think_time : 0;
Alan Sternffa02482013-10-11 11:29:03 -04001008 stream->ps.tt_usecs = NS_TO_US(think_time + usb_calc_bus_time(
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001009 dev->speed, is_input, 1, maxp));
Clemens Ladisch469d0222006-01-20 13:49:10 -08001010 hs_transfers = max (1u, (maxp + 187) / 188);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (is_input) {
1012 u32 tmp;
1013
1014 addr |= 1 << 31;
Alan Sternffa02482013-10-11 11:29:03 -04001015 stream->ps.c_usecs = stream->ps.usecs;
1016 stream->ps.usecs = HS_USECS_ISO(1);
Alan Sternd0ce5c62013-10-11 11:29:13 -04001017 stream->ps.cs_mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Clemens Ladisch469d0222006-01-20 13:49:10 -08001019 /* c-mask as specified in USB 2.0 11.18.4 3.c */
1020 tmp = (1 << (hs_transfers + 2)) - 1;
Alan Sternd0ce5c62013-10-11 11:29:13 -04001021 stream->ps.cs_mask |= tmp << (8 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } else
Alan Sternd0ce5c62013-10-11 11:29:13 -04001023 stream->ps.cs_mask = smask_out[hs_transfers - 1];
Alan Sternffa02482013-10-11 11:29:03 -04001024
Alan Sternd0ce5c62013-10-11 11:29:13 -04001025 /* period for bandwidth allocation */
1026 tmp = min_t(unsigned, EHCI_BANDWIDTH_FRAMES,
1027 1 << (urb->ep->desc.bInterval - 1));
1028
1029 /* Allow urb->interval to override */
1030 stream->ps.bw_period = min_t(unsigned, tmp, urb->interval);
1031 stream->ps.bw_uperiod = stream->ps.bw_period << 3;
1032
1033 stream->ps.period = urb->interval;
1034 stream->uperiod = urb->interval << 3;
Alan Sternffa02482013-10-11 11:29:03 -04001035 stream->bandwidth = (stream->ps.usecs + stream->ps.c_usecs) /
Alan Sternd0ce5c62013-10-11 11:29:13 -04001036 stream->ps.bw_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Alan Sternd0ce5c62013-10-11 11:29:13 -04001038 /* stream->splits gets created from cs_mask later */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001039 stream->address = cpu_to_hc32(ehci, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Alan Sternffa02482013-10-11 11:29:03 -04001042 stream->ps.udev = dev;
1043 stream->ps.ep = urb->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 stream->bEndpointAddress = is_input | epnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 stream->maxp = maxp;
1047}
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049static struct ehci_iso_stream *
1050iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1051{
1052 unsigned epnum;
1053 struct ehci_iso_stream *stream;
1054 struct usb_host_endpoint *ep;
1055 unsigned long flags;
1056
1057 epnum = usb_pipeendpoint (urb->pipe);
1058 if (usb_pipein(urb->pipe))
1059 ep = urb->dev->ep_in[epnum];
1060 else
1061 ep = urb->dev->ep_out[epnum];
1062
1063 spin_lock_irqsave (&ehci->lock, flags);
1064 stream = ep->hcpriv;
1065
1066 if (unlikely (stream == NULL)) {
1067 stream = iso_stream_alloc(GFP_ATOMIC);
1068 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ep->hcpriv = stream;
Alan Sternffa02482013-10-11 11:29:03 -04001070 iso_stream_init(ehci, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072
Clemens Ladisch1082f572010-03-01 17:18:56 +01001073 /* if dev->ep [epnum] is a QH, hw is set */
1074 } else if (unlikely (stream->hw != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1076 urb->dev->devpath, epnum,
1077 usb_pipein(urb->pipe) ? "in" : "out");
1078 stream = NULL;
1079 }
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 spin_unlock_irqrestore (&ehci->lock, flags);
1082 return stream;
1083}
1084
1085/*-------------------------------------------------------------------------*/
1086
1087/* ehci_iso_sched ops can be ITD-only or SITD-only */
1088
1089static struct ehci_iso_sched *
Al Viro55016f12005-10-21 03:21:58 -04001090iso_sched_alloc (unsigned packets, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 struct ehci_iso_sched *iso_sched;
1093 int size = sizeof *iso_sched;
1094
1095 size += packets * sizeof (struct ehci_iso_packet);
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001096 iso_sched = kzalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (likely (iso_sched != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 INIT_LIST_HEAD (&iso_sched->td_list);
1099 }
1100 return iso_sched;
1101}
1102
1103static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001104itd_sched_init(
1105 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct ehci_iso_sched *iso_sched,
1107 struct ehci_iso_stream *stream,
1108 struct urb *urb
1109)
1110{
1111 unsigned i;
1112 dma_addr_t dma = urb->transfer_dma;
1113
1114 /* how many uframes are needed for these transfers */
Alan Sternffa02482013-10-11 11:29:03 -04001115 iso_sched->span = urb->number_of_packets * stream->uperiod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 /* figure out per-uframe itd fields that we'll need later
1118 * when we fit new itds into the schedule.
1119 */
1120 for (i = 0; i < urb->number_of_packets; i++) {
1121 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1122 unsigned length;
1123 dma_addr_t buf;
1124 u32 trans;
1125
1126 length = urb->iso_frame_desc [i].length;
1127 buf = dma + urb->iso_frame_desc [i].offset;
1128
1129 trans = EHCI_ISOC_ACTIVE;
1130 trans |= buf & 0x0fff;
1131 if (unlikely (((i + 1) == urb->number_of_packets))
1132 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1133 trans |= EHCI_ITD_IOC;
1134 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001135 uframe->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
David Brownell77078572005-05-28 10:46:18 -07001137 /* might need to cross a buffer page within a uframe */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 uframe->bufp = (buf & ~(u64)0x0fff);
1139 buf += length;
1140 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1141 uframe->cross = 1;
1142 }
1143}
1144
1145static void
1146iso_sched_free (
1147 struct ehci_iso_stream *stream,
1148 struct ehci_iso_sched *iso_sched
1149)
1150{
1151 if (!iso_sched)
1152 return;
1153 // caller must hold ehci->lock!
1154 list_splice (&iso_sched->td_list, &stream->free_list);
1155 kfree (iso_sched);
1156}
1157
1158static int
1159itd_urb_transaction (
1160 struct ehci_iso_stream *stream,
1161 struct ehci_hcd *ehci,
1162 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001163 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164)
1165{
1166 struct ehci_itd *itd;
1167 dma_addr_t itd_dma;
1168 int i;
1169 unsigned num_itds;
1170 struct ehci_iso_sched *sched;
1171 unsigned long flags;
1172
1173 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1174 if (unlikely (sched == NULL))
1175 return -ENOMEM;
1176
Stefan Roese6dbd6822007-05-01 09:29:37 -07001177 itd_sched_init(ehci, sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (urb->interval < 8)
1180 num_itds = 1 + (sched->span + 7) / 8;
1181 else
1182 num_itds = urb->number_of_packets;
1183
1184 /* allocate/init ITDs */
1185 spin_lock_irqsave (&ehci->lock, flags);
1186 for (i = 0; i < num_itds; i++) {
1187
Alan Stern55934eb2012-07-11 11:22:35 -04001188 /*
1189 * Use iTDs from the free list, but not iTDs that may
1190 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 */
Alan Stern55934eb2012-07-11 11:22:35 -04001192 if (likely(!list_empty(&stream->free_list))) {
1193 itd = list_first_entry(&stream->free_list,
Stefan Roese6dbd6822007-05-01 09:29:37 -07001194 struct ehci_itd, itd_list);
Alan Sternf4289072012-07-11 11:23:07 -04001195 if (itd->frame == ehci->now_frame)
Alan Stern55934eb2012-07-11 11:22:35 -04001196 goto alloc_itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 list_del (&itd->itd_list);
1198 itd_dma = itd->itd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001199 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001200 alloc_itd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 spin_unlock_irqrestore (&ehci->lock, flags);
1202 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1203 &itd_dma);
1204 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001205 if (!itd) {
1206 iso_sched_free(stream, sched);
1207 spin_unlock_irqrestore(&ehci->lock, flags);
1208 return -ENOMEM;
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 memset (itd, 0, sizeof *itd);
1213 itd->itd_dma = itd_dma;
Alan Stern91a99b52013-10-11 11:28:52 -04001214 itd->frame = NO_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 list_add (&itd->itd_list, &sched->td_list);
1216 }
1217 spin_unlock_irqrestore (&ehci->lock, flags);
1218
1219 /* temporarily store schedule info in hcpriv */
1220 urb->hcpriv = sched;
1221 urb->error_count = 0;
1222 return 0;
1223}
1224
1225/*-------------------------------------------------------------------------*/
1226
Alan Sternd0ce5c62013-10-11 11:29:13 -04001227static void reserve_release_iso_bandwidth(struct ehci_hcd *ehci,
1228 struct ehci_iso_stream *stream, int sign)
1229{
1230 unsigned uframe;
1231 unsigned i, j;
1232 unsigned s_mask, c_mask, m;
1233 int usecs = stream->ps.usecs;
1234 int c_usecs = stream->ps.c_usecs;
1235
1236 if (stream->ps.phase == NO_FRAME) /* Bandwidth wasn't reserved */
1237 return;
1238 uframe = stream->ps.bw_phase << 3;
1239
1240 bandwidth_dbg(ehci, sign, "iso", &stream->ps);
1241
1242 if (sign < 0) { /* Release bandwidth */
1243 usecs = -usecs;
1244 c_usecs = -c_usecs;
1245 }
1246
1247 if (!stream->splits) { /* High speed */
1248 for (i = uframe + stream->ps.phase_uf; i < EHCI_BANDWIDTH_SIZE;
1249 i += stream->ps.bw_uperiod)
1250 ehci->bandwidth[i] += usecs;
1251
1252 } else { /* Full speed */
1253 s_mask = stream->ps.cs_mask;
1254 c_mask = s_mask >> 8;
1255
1256 /* NOTE: adjustment needed for frame overflow */
1257 for (i = uframe; i < EHCI_BANDWIDTH_SIZE;
1258 i += stream->ps.bw_uperiod) {
1259 for ((j = stream->ps.phase_uf, m = 1 << j); j < 8;
1260 (++j, m <<= 1)) {
1261 if (s_mask & m)
1262 ehci->bandwidth[i+j] += usecs;
1263 else if (c_mask & m)
1264 ehci->bandwidth[i+j] += c_usecs;
1265 }
1266 }
1267 }
1268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270static inline int
1271itd_slot_ok (
1272 struct ehci_hcd *ehci,
Alan Sternd0ce5c62013-10-11 11:29:13 -04001273 struct ehci_iso_stream *stream,
1274 unsigned uframe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275)
1276{
Alan Sternd0ce5c62013-10-11 11:29:13 -04001277 unsigned usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Alan Sternd0ce5c62013-10-11 11:29:13 -04001279 /* convert "usecs we need" to "max already claimed" */
1280 usecs = ehci->uframe_periodic_max - stream->ps.usecs;
1281
1282 for (uframe &= stream->ps.bw_uperiod - 1; uframe < EHCI_BANDWIDTH_SIZE;
1283 uframe += stream->ps.bw_uperiod) {
1284 if (ehci->bandwidth[uframe] > usecs)
1285 return 0;
1286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return 1;
1288}
1289
1290static inline int
1291sitd_slot_ok (
1292 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct ehci_iso_stream *stream,
Alan Sternd0ce5c62013-10-11 11:29:13 -04001294 unsigned uframe,
1295 struct ehci_iso_sched *sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296)
1297{
Alan Sternd0ce5c62013-10-11 11:29:13 -04001298 unsigned mask, tmp;
1299 unsigned frame, uf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Alan Sternd0ce5c62013-10-11 11:29:13 -04001301 mask = stream->ps.cs_mask << (uframe & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Alan Stern8c05dc52013-10-11 11:28:31 -04001303 /* for OUT, don't wrap SSPLIT into H-microframe 7 */
Alan Sternd0ce5c62013-10-11 11:29:13 -04001304 if (((stream->ps.cs_mask & 0xff) << (uframe & 7)) >= (1 << 7))
Alan Stern8c05dc52013-10-11 11:28:31 -04001305 return 0;
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /* for IN, don't wrap CSPLIT into the next frame */
1308 if (mask & ~0xffff)
1309 return 0;
1310
Alan Stern65b8e5c2012-05-14 13:47:20 -04001311 /* check bandwidth */
Alan Sternd0ce5c62013-10-11 11:29:13 -04001312 uframe &= stream->ps.bw_uperiod - 1;
Alan Stern65b8e5c2012-05-14 13:47:20 -04001313 frame = uframe >> 3;
1314
1315#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1316 /* The tt's fullspeed bus bandwidth must be available.
1317 * tt_available scheduling guarantees 10+% for control/bulk.
1318 */
1319 uf = uframe & 7;
Alan Sternd0ce5c62013-10-11 11:29:13 -04001320 if (!tt_available(ehci, stream->ps.bw_period,
Alan Sternffa02482013-10-11 11:29:03 -04001321 stream->ps.udev, frame, uf, stream->ps.tt_usecs))
Alan Stern65b8e5c2012-05-14 13:47:20 -04001322 return 0;
1323#else
1324 /* tt must be idle for start(s), any gap, and csplit.
1325 * assume scheduling slop leaves 10+% for control/bulk.
1326 */
Alan Sternd0ce5c62013-10-11 11:29:13 -04001327 if (!tt_no_collision(ehci, stream->ps.bw_period,
Alan Sternffa02482013-10-11 11:29:03 -04001328 stream->ps.udev, frame, mask))
Alan Stern65b8e5c2012-05-14 13:47:20 -04001329 return 0;
1330#endif
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 do {
Alan Sternd0ce5c62013-10-11 11:29:13 -04001333 unsigned max_used;
1334 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 /* check starts (OUT uses more than one) */
Alan Sternd0ce5c62013-10-11 11:29:13 -04001337 uf = uframe;
Alan Sternffa02482013-10-11 11:29:03 -04001338 max_used = ehci->uframe_periodic_max - stream->ps.usecs;
Alan Sternd0ce5c62013-10-11 11:29:13 -04001339 for (tmp = stream->ps.cs_mask & 0xff; tmp; tmp >>= 1, uf++) {
1340 if (ehci->bandwidth[uf] > max_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return 0;
1342 }
1343
1344 /* for IN, check CSPLIT */
Alan Sternffa02482013-10-11 11:29:03 -04001345 if (stream->ps.c_usecs) {
Alan Sternffa02482013-10-11 11:29:03 -04001346 max_used = ehci->uframe_periodic_max -
1347 stream->ps.c_usecs;
Alan Sternd0ce5c62013-10-11 11:29:13 -04001348 uf = uframe & ~7;
1349 tmp = 1 << (2+8);
1350 for (i = (uframe & 7) + 2; i < 8; (++i, tmp <<= 1)) {
1351 if ((stream->ps.cs_mask & tmp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 continue;
Alan Sternd0ce5c62013-10-11 11:29:13 -04001353 if (ehci->bandwidth[uf+i] > max_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return 0;
Alan Sternd0ce5c62013-10-11 11:29:13 -04001355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357
Alan Sternd0ce5c62013-10-11 11:29:13 -04001358 uframe += stream->ps.bw_uperiod;
1359 } while (uframe < EHCI_BANDWIDTH_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Alan Sternd0ce5c62013-10-11 11:29:13 -04001361 stream->ps.cs_mask <<= uframe & 7;
1362 stream->splits = cpu_to_hc32(ehci, stream->ps.cs_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return 1;
1364}
1365
1366/*
1367 * This scheduler plans almost as far into the future as it has actual
1368 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1369 * "as small as possible" to be cache-friendlier.) That limits the size
1370 * transfers you can stream reliably; avoid more than 64 msec per urb.
1371 * Also avoid queue depths of less than ehci's worst irq latency (affected
1372 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1373 * and other factors); or more than about 230 msec total (for portability,
1374 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1375 */
1376
Alan Sternc3ee9b72012-09-28 16:01:23 -04001377#define SCHEDULING_DELAY 40 /* microframes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379static int
1380iso_stream_schedule (
1381 struct ehci_hcd *ehci,
1382 struct urb *urb,
1383 struct ehci_iso_stream *stream
1384)
1385{
Alan Stern46c73d12013-09-03 13:59:03 -04001386 u32 now, base, next, start, period, span, now2;
1387 u32 wrap = 0, skip = 0;
1388 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 unsigned mod = ehci->periodic_size << 3;
1390 struct ehci_iso_sched *sched = urb->hcpriv;
Alan Stern46c73d12013-09-03 13:59:03 -04001391 bool empty = list_empty(&stream->td_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Alan Sternd0ce5c62013-10-11 11:29:13 -04001393 period = stream->uperiod;
Alan Sternffda0802010-07-14 11:03:46 -04001394 span = sched->span;
Alan Sternd0ce5c62013-10-11 11:29:13 -04001395 if (!stream->highspeed)
Alan Sternffda0802010-07-14 11:03:46 -04001396 span <<= 3;
Alan Sternffda0802010-07-14 11:03:46 -04001397
Alan Stern68aa95d2011-10-12 10:39:14 -04001398 now = ehci_read_frame_index(ehci) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Alan Stern46c73d12013-09-03 13:59:03 -04001400 /* Take the isochronous scheduling threshold into account */
1401 if (ehci->i_thresh)
1402 next = now + ehci->i_thresh; /* uframe cache */
1403 else
1404 next = (now + 2 + 7) & ~0x07; /* full frame cache */
1405
1406 /*
1407 * Use ehci->last_iso_frame as the base. There can't be any
1408 * TDs scheduled for earlier than that.
1409 */
1410 base = ehci->last_iso_frame << 3;
1411 next = (next - base) & (mod - 1);
1412
Alan Sternd0ce5c62013-10-11 11:29:13 -04001413 /* Start a new isochronous stream? */
Alan Stern46c73d12013-09-03 13:59:03 -04001414 if (unlikely(empty && !hcd_periodic_completion_in_progress(
1415 ehci_to_hcd(ehci), urb->ep))) {
Alan Sterne4e18cb2013-09-03 13:58:59 -04001416
Alan Sternd0ce5c62013-10-11 11:29:13 -04001417 /* Schedule the endpoint */
1418 if (stream->ps.phase == NO_FRAME) {
1419 int done = 0;
Alan Sterne4e18cb2013-09-03 13:58:59 -04001420
Alan Sternd0ce5c62013-10-11 11:29:13 -04001421 start = (now & ~0x07) + SCHEDULING_DELAY;
1422
1423 /* find a uframe slot with enough bandwidth.
1424 * Early uframes are more precious because full-speed
1425 * iso IN transfers can't use late uframes,
1426 * and therefore they should be allocated last.
1427 */
1428 next = start;
1429 start += period;
1430 do {
1431 start--;
1432 /* check schedule: enough space? */
1433 if (stream->highspeed) {
1434 if (itd_slot_ok(ehci, stream, start))
1435 done = 1;
1436 } else {
1437 if ((start % 8) >= 6)
1438 continue;
1439 if (sitd_slot_ok(ehci, stream, start,
1440 sched))
1441 done = 1;
1442 }
1443 } while (start > next && !done);
1444
1445 /* no room in the schedule */
1446 if (!done) {
1447 ehci_dbg(ehci, "iso sched full %p", urb);
1448 status = -ENOSPC;
1449 goto fail;
Alan Sterne4e18cb2013-09-03 13:58:59 -04001450 }
Alan Sternd0ce5c62013-10-11 11:29:13 -04001451 stream->ps.phase = (start >> 3) &
1452 (stream->ps.period - 1);
1453 stream->ps.bw_phase = stream->ps.phase &
1454 (stream->ps.bw_period - 1);
1455 stream->ps.phase_uf = start & 7;
1456 reserve_release_iso_bandwidth(ehci, stream, 1);
1457 }
Alan Sterne4e18cb2013-09-03 13:58:59 -04001458
Alan Sternd0ce5c62013-10-11 11:29:13 -04001459 /* New stream is already scheduled; use the upcoming slot */
1460 else {
1461 start = (stream->ps.phase << 3) + stream->ps.phase_uf;
Alan Sterne4e18cb2013-09-03 13:58:59 -04001462 }
Alan Stern46c73d12013-09-03 13:59:03 -04001463
1464 start = (start - base) & (mod - 1);
1465 goto use_start;
Alan Sterne4e18cb2013-09-03 13:58:59 -04001466 }
1467
1468 /*
Alan Sternd0ce5c62013-10-11 11:29:13 -04001469 * Typical case: reuse current schedule, stream may still be active.
Alan Sternb40e43f2008-05-20 16:59:10 -04001470 * Hopefully there are no gaps from the host falling behind
Alan Stern4005ad42012-10-01 10:32:01 -04001471 * (irq delays etc). If there are, the behavior depends on
1472 * whether URB_ISO_ASAP is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
Alan Stern46c73d12013-09-03 13:59:03 -04001474 start = (stream->next_uframe - base) & (mod - 1);
1475 now2 = (now - base) & (mod - 1);
Sarah Sharpdccd5742009-10-27 10:55:05 -07001476
Alan Stern46c73d12013-09-03 13:59:03 -04001477 /* Is the schedule already full? */
1478 if (unlikely(!empty && start < period)) {
1479 ehci_dbg(ehci, "iso sched full %p (%u-%u < %u mod %u)\n",
1480 urb, stream->next_uframe, base, period, mod);
1481 status = -ENOSPC;
1482 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484
Alan Stern46c73d12013-09-03 13:59:03 -04001485 /* Is the next packet scheduled after the base time? */
1486 if (likely(!empty || start <= now2 + period)) {
1487
1488 /* URB_ISO_ASAP: make sure that start >= next */
1489 if (unlikely(start < next &&
1490 (urb->transfer_flags & URB_ISO_ASAP)))
1491 goto do_ASAP;
1492
1493 /* Otherwise use start, if it's not in the past */
1494 if (likely(start >= now2))
1495 goto use_start;
1496
1497 /* Otherwise we got an underrun while the queue was empty */
1498 } else {
1499 if (urb->transfer_flags & URB_ISO_ASAP)
1500 goto do_ASAP;
1501 wrap = mod;
1502 now2 += mod;
1503 }
1504
1505 /* How many uframes and packets do we need to skip? */
1506 skip = (now2 - start + period - 1) & -period;
1507 if (skip >= span) { /* Entirely in the past? */
1508 ehci_dbg(ehci, "iso underrun %p (%u+%u < %u) [%u]\n",
1509 urb, start + base, span - period, now2 + base,
1510 base);
1511
1512 /* Try to keep the last TD intact for scanning later */
1513 skip = span - period;
1514
1515 /* Will it come before the current scan position? */
1516 if (empty) {
1517 skip = span; /* Skip the entire URB */
1518 status = 1; /* and give it back immediately */
1519 iso_sched_free(stream, sched);
1520 sched = NULL;
1521 }
1522 }
1523 urb->error_count = skip / period;
1524 if (sched)
1525 sched->first_packet = urb->error_count;
1526 goto use_start;
1527
1528 do_ASAP:
1529 /* Use the first slot after "next" */
1530 start = next + ((start - next) & (period - 1));
1531
1532 use_start:
Alan Stern1fb2e052010-07-14 11:03:53 -04001533 /* Tried to schedule too far into the future? */
Alan Stern46c73d12013-09-03 13:59:03 -04001534 if (unlikely(start + span - period >= mod + wrap)) {
Alan Sternc3ee9b72012-09-28 16:01:23 -04001535 ehci_dbg(ehci, "request %p would overflow (%u+%u >= %u)\n",
Alan Stern46c73d12013-09-03 13:59:03 -04001536 urb, start, span - period, mod + wrap);
Alan Stern1fb2e052010-07-14 11:03:53 -04001537 status = -EFBIG;
1538 goto fail;
1539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Alan Stern46c73d12013-09-03 13:59:03 -04001541 start += base;
1542 stream->next_uframe = (start + skip) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* report high speed start in uframes; full speed, in frames */
Alan Stern46c73d12013-09-03 13:59:03 -04001545 urb->start_frame = start & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (!stream->highspeed)
1547 urb->start_frame >>= 3;
Alan Stern569b3942012-07-11 11:23:00 -04001548
1549 /* Make sure scan_isoc() sees these */
1550 if (ehci->isoc_count == 0)
Alan Sternc3ee9b72012-09-28 16:01:23 -04001551 ehci->last_iso_frame = now >> 3;
Alan Stern46c73d12013-09-03 13:59:03 -04001552 return status;
Alan Stern1fb2e052010-07-14 11:03:53 -04001553
1554 fail:
1555 iso_sched_free(stream, sched);
1556 urb->hcpriv = NULL;
1557 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560/*-------------------------------------------------------------------------*/
1561
1562static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001563itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1564 struct ehci_itd *itd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 int i;
1567
David Brownell77078572005-05-28 10:46:18 -07001568 /* it's been recently zeroed */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001569 itd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 itd->hw_bufp [0] = stream->buf0;
1571 itd->hw_bufp [1] = stream->buf1;
1572 itd->hw_bufp [2] = stream->buf2;
1573
1574 for (i = 0; i < 8; i++)
1575 itd->index[i] = -1;
1576
1577 /* All other fields are filled when scheduling */
1578}
1579
1580static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001581itd_patch(
1582 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct ehci_itd *itd,
1584 struct ehci_iso_sched *iso_sched,
1585 unsigned index,
David Brownell77078572005-05-28 10:46:18 -07001586 u16 uframe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587)
1588{
1589 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1590 unsigned pg = itd->pg;
1591
1592 // BUG_ON (pg == 6 && uf->cross);
1593
1594 uframe &= 0x07;
1595 itd->index [uframe] = index;
1596
Stefan Roese6dbd6822007-05-01 09:29:37 -07001597 itd->hw_transaction[uframe] = uf->transaction;
1598 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1599 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1600 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 /* iso_frame_desc[].offset must be strictly increasing */
David Brownell77078572005-05-28 10:46:18 -07001603 if (unlikely (uf->cross)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 u64 bufp = uf->bufp + 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 itd->pg = ++pg;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001607 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1608 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610}
1611
1612static inline void
1613itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1614{
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001615 union ehci_shadow *prev = &ehci->pshadow[frame];
1616 __hc32 *hw_p = &ehci->periodic[frame];
1617 union ehci_shadow here = *prev;
1618 __hc32 type = 0;
1619
1620 /* skip any iso nodes which might belong to previous microframes */
1621 while (here.ptr) {
1622 type = Q_NEXT_TYPE(ehci, *hw_p);
1623 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
1624 break;
1625 prev = periodic_next_shadow(ehci, prev, type);
1626 hw_p = shadow_next_periodic(ehci, &here, type);
1627 here = *prev;
1628 }
1629
1630 itd->itd_next = here;
1631 itd->hw_next = *hw_p;
1632 prev->itd = itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 itd->frame = frame;
1634 wmb ();
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001635 *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
1638/* fit urb's itds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04001639static void itd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 struct ehci_hcd *ehci,
1641 struct urb *urb,
1642 unsigned mod,
1643 struct ehci_iso_stream *stream
1644)
1645{
David Brownell77078572005-05-28 10:46:18 -07001646 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 unsigned next_uframe, uframe, frame;
1648 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1649 struct ehci_itd *itd;
1650
Alan Sternbccbefa2010-07-14 11:03:36 -04001651 next_uframe = stream->next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +03001653 if (unlikely (list_empty(&stream->td_list)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 ehci_to_hcd(ehci)->self.bandwidth_allocated
1655 += stream->bandwidth;
Alex He05570292010-12-07 10:10:08 +08001656
1657 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001658 if (ehci->amd_pll_fix == 1)
1659 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08001660 }
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1663
1664 /* fill iTDs uframe by uframe */
Alan Stern46c73d12013-09-03 13:59:03 -04001665 for (packet = iso_sched->first_packet, itd = NULL;
1666 packet < urb->number_of_packets;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (itd == NULL) {
1668 /* ASSERT: we have all necessary itds */
1669 // BUG_ON (list_empty (&iso_sched->td_list));
1670
1671 /* ASSERT: no itds for this endpoint in this uframe */
1672
1673 itd = list_entry (iso_sched->td_list.next,
1674 struct ehci_itd, itd_list);
1675 list_move_tail (&itd->itd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001676 itd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01001677 itd->urb = urb;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001678 itd_init (ehci, stream, itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
1681 uframe = next_uframe & 0x07;
1682 frame = next_uframe >> 3;
1683
Stefan Roese6dbd6822007-05-01 09:29:37 -07001684 itd_patch(ehci, itd, iso_sched, packet, uframe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Alan Sternffa02482013-10-11 11:29:03 -04001686 next_uframe += stream->uperiod;
Alan Sternbccbefa2010-07-14 11:03:36 -04001687 next_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 packet++;
1689
1690 /* link completed itds into the schedule */
1691 if (((next_uframe >> 3) != frame)
1692 || packet == urb->number_of_packets) {
Alan Sternbccbefa2010-07-14 11:03:36 -04001693 itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 itd = NULL;
1695 }
1696 }
1697 stream->next_uframe = next_uframe;
1698
1699 /* don't need that schedule data any more */
1700 iso_sched_free (stream, iso_sched);
Alan Stern2656a9a2012-11-08 10:17:01 -05001701 urb->hcpriv = stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Alan Stern569b3942012-07-11 11:23:00 -04001703 ++ehci->isoc_count;
Alan Sternb015cb72012-07-11 11:22:10 -04001704 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
1707#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1708
David Brownell30bf54e2007-12-16 22:37:40 -08001709/* Process and recycle a completed ITD. Return true iff its urb completed,
1710 * and hence its completion callback probably added things to the hardware
1711 * schedule.
1712 *
1713 * Note that we carefully avoid recycling this descriptor until after any
1714 * completion callback runs, so that it won't be reused quickly. That is,
1715 * assuming (a) no more than two urbs per frame on this endpoint, and also
1716 * (b) only this endpoint's completions submit URBs. It seems some silicon
1717 * corrupts things if you reuse completed descriptors very quickly...
1718 */
Alan Sternf4289072012-07-11 11:23:07 -04001719static bool itd_complete(struct ehci_hcd *ehci, struct ehci_itd *itd)
1720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 struct urb *urb = itd->urb;
1722 struct usb_iso_packet_descriptor *desc;
1723 u32 t;
1724 unsigned uframe;
1725 int urb_index = -1;
1726 struct ehci_iso_stream *stream = itd->stream;
1727 struct usb_device *dev;
Alan Sternf4289072012-07-11 11:23:07 -04001728 bool retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 /* for each uframe with a packet */
1731 for (uframe = 0; uframe < 8; uframe++) {
1732 if (likely (itd->index[uframe] == -1))
1733 continue;
1734 urb_index = itd->index[uframe];
1735 desc = &urb->iso_frame_desc [urb_index];
1736
Stefan Roese6dbd6822007-05-01 09:29:37 -07001737 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 itd->hw_transaction [uframe] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 /* report transfer status */
1741 if (unlikely (t & ISO_ERRS)) {
1742 urb->error_count++;
1743 if (t & EHCI_ISOC_BUF_ERR)
1744 desc->status = usb_pipein (urb->pipe)
1745 ? -ENOSR /* hc couldn't read */
1746 : -ECOMM; /* hc couldn't write */
1747 else if (t & EHCI_ISOC_BABBLE)
1748 desc->status = -EOVERFLOW;
1749 else /* (t & EHCI_ISOC_XACTERR) */
1750 desc->status = -EPROTO;
1751
1752 /* HC need not update length with this error */
Alan Sternec6d67e2009-06-29 14:34:59 -04001753 if (!(t & EHCI_ISOC_BABBLE)) {
1754 desc->actual_length = EHCI_ITD_LENGTH(t);
1755 urb->actual_length += desc->actual_length;
1756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1758 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04001759 desc->actual_length = EHCI_ITD_LENGTH(t);
1760 urb->actual_length += desc->actual_length;
Alan Sternb40e43f2008-05-20 16:59:10 -04001761 } else {
1762 /* URB was too late */
Alan Stern4005ad42012-10-01 10:32:01 -04001763 urb->error_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765 }
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /* handle completion now? */
1768 if (likely ((urb_index + 1) != urb->number_of_packets))
David Brownell30bf54e2007-12-16 22:37:40 -08001769 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 /* ASSERT: it's really the last itd for this urb
1772 list_for_each_entry (itd, &stream->td_list, itd_list)
1773 BUG_ON (itd->urb == urb);
1774 */
1775
David Brownellaa16ca32007-12-30 23:45:19 -08001776 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05001777 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04001778 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08001779 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Alan Stern569b3942012-07-11 11:23:00 -04001782 --ehci->isoc_count;
1783 disable_periodic(ehci);
1784
1785 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
Alex He05570292010-12-07 10:10:08 +08001786 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08001787 if (ehci->amd_pll_fix == 1)
1788 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08001789 }
1790
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +03001791 if (unlikely(list_is_singular(&stream->td_list)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 ehci_to_hcd(ehci)->self.bandwidth_allocated
1793 -= stream->bandwidth;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001794
David Brownell30bf54e2007-12-16 22:37:40 -08001795done:
David Brownell30bf54e2007-12-16 22:37:40 -08001796 itd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04001797
1798 /* Add to the end of the free list for later reuse */
1799 list_move_tail(&itd->itd_list, &stream->free_list);
1800
1801 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
1802 if (list_empty(&stream->td_list)) {
1803 list_splice_tail_init(&stream->free_list,
1804 &ehci->cached_itd_list);
1805 start_free_itds(ehci);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001806 }
Alan Stern55934eb2012-07-11 11:22:35 -04001807
David Brownell30bf54e2007-12-16 22:37:40 -08001808 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
1811/*-------------------------------------------------------------------------*/
1812
Olav Kongas5db539e2005-06-23 20:25:36 +03001813static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001814 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
1816 int status = -EINVAL;
1817 unsigned long flags;
1818 struct ehci_iso_stream *stream;
1819
1820 /* Get iso_stream head */
1821 stream = iso_stream_find (ehci, urb);
1822 if (unlikely (stream == NULL)) {
1823 ehci_dbg (ehci, "can't get iso stream\n");
1824 return -ENOMEM;
1825 }
Alan Sternffa02482013-10-11 11:29:03 -04001826 if (unlikely(urb->interval != stream->uperiod)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
Alan Sternffa02482013-10-11 11:29:03 -04001828 stream->uperiod, urb->interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 goto done;
1830 }
1831
1832#ifdef EHCI_URB_TRACE
1833 ehci_dbg (ehci,
1834 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001835 __func__, urb->dev->devpath, urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 usb_pipeendpoint (urb->pipe),
1837 usb_pipein (urb->pipe) ? "in" : "out",
1838 urb->transfer_buffer_length,
1839 urb->number_of_packets, urb->interval,
1840 stream);
1841#endif
1842
1843 /* allocate ITDs w/o locking anything */
1844 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1845 if (unlikely (status < 0)) {
1846 ehci_dbg (ehci, "can't init itds\n");
1847 goto done;
1848 }
1849
1850 /* schedule ... need to lock */
1851 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001852 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001853 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04001854 goto done_not_linked;
1855 }
1856 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1857 if (unlikely(status))
1858 goto done_not_linked;
1859 status = iso_stream_schedule(ehci, urb, stream);
Alan Stern46c73d12013-09-03 13:59:03 -04001860 if (likely(status == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Stern46c73d12013-09-03 13:59:03 -04001862 } else if (status > 0) {
1863 status = 0;
1864 ehci_urb_done(ehci, urb, 0);
1865 } else {
Alan Sterne9df41c2007-08-08 11:48:02 -04001866 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern46c73d12013-09-03 13:59:03 -04001867 }
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001868 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04001870 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return status;
1872}
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874/*-------------------------------------------------------------------------*/
1875
1876/*
1877 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1878 * TTs in USB 2.0 hubs. These need microframe scheduling.
1879 */
1880
1881static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001882sitd_sched_init(
1883 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 struct ehci_iso_sched *iso_sched,
1885 struct ehci_iso_stream *stream,
1886 struct urb *urb
1887)
1888{
1889 unsigned i;
1890 dma_addr_t dma = urb->transfer_dma;
1891
1892 /* how many frames are needed for these transfers */
Alan Sternffa02482013-10-11 11:29:03 -04001893 iso_sched->span = urb->number_of_packets * stream->ps.period;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 /* figure out per-frame sitd fields that we'll need later
1896 * when we fit new sitds into the schedule.
1897 */
1898 for (i = 0; i < urb->number_of_packets; i++) {
1899 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1900 unsigned length;
1901 dma_addr_t buf;
1902 u32 trans;
1903
1904 length = urb->iso_frame_desc [i].length & 0x03ff;
1905 buf = dma + urb->iso_frame_desc [i].offset;
1906
1907 trans = SITD_STS_ACTIVE;
1908 if (((i + 1) == urb->number_of_packets)
1909 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1910 trans |= SITD_IOC;
1911 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001912 packet->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /* might need to cross a buffer page within a td */
1915 packet->bufp = buf;
1916 packet->buf1 = (buf + length) & ~0x0fff;
1917 if (packet->buf1 != (buf & ~(u64)0x0fff))
1918 packet->cross = 1;
1919
David Brownell53bd6a62006-08-30 14:50:06 -07001920 /* OUT uses multiple start-splits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (stream->bEndpointAddress & USB_DIR_IN)
1922 continue;
1923 length = (length + 187) / 188;
1924 if (length > 1) /* BEGIN vs ALL */
1925 length |= 1 << 3;
1926 packet->buf1 |= length;
1927 }
1928}
1929
1930static int
1931sitd_urb_transaction (
1932 struct ehci_iso_stream *stream,
1933 struct ehci_hcd *ehci,
1934 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001935 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936)
1937{
1938 struct ehci_sitd *sitd;
1939 dma_addr_t sitd_dma;
1940 int i;
1941 struct ehci_iso_sched *iso_sched;
1942 unsigned long flags;
1943
1944 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1945 if (iso_sched == NULL)
1946 return -ENOMEM;
1947
Stefan Roese6dbd6822007-05-01 09:29:37 -07001948 sitd_sched_init(ehci, iso_sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 /* allocate/init sITDs */
1951 spin_lock_irqsave (&ehci->lock, flags);
1952 for (i = 0; i < urb->number_of_packets; i++) {
1953
1954 /* NOTE: for now, we don't try to handle wraparound cases
1955 * for IN (using sitd->hw_backpointer, like a FSTN), which
1956 * means we never need two sitds for full speed packets.
1957 */
1958
Alan Stern55934eb2012-07-11 11:22:35 -04001959 /*
1960 * Use siTDs from the free list, but not siTDs that may
1961 * still be in use by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
Alan Stern55934eb2012-07-11 11:22:35 -04001963 if (likely(!list_empty(&stream->free_list))) {
1964 sitd = list_first_entry(&stream->free_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct ehci_sitd, sitd_list);
Alan Sternf4289072012-07-11 11:23:07 -04001966 if (sitd->frame == ehci->now_frame)
Alan Stern55934eb2012-07-11 11:22:35 -04001967 goto alloc_sitd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 list_del (&sitd->sitd_list);
1969 sitd_dma = sitd->sitd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001970 } else {
Alan Stern55934eb2012-07-11 11:22:35 -04001971 alloc_sitd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 spin_unlock_irqrestore (&ehci->lock, flags);
1973 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1974 &sitd_dma);
1975 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001976 if (!sitd) {
1977 iso_sched_free(stream, iso_sched);
1978 spin_unlock_irqrestore(&ehci->lock, flags);
1979 return -ENOMEM;
1980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 memset (sitd, 0, sizeof *sitd);
1984 sitd->sitd_dma = sitd_dma;
Alan Stern91a99b52013-10-11 11:28:52 -04001985 sitd->frame = NO_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 list_add (&sitd->sitd_list, &iso_sched->td_list);
1987 }
1988
1989 /* temporarily store schedule info in hcpriv */
1990 urb->hcpriv = iso_sched;
1991 urb->error_count = 0;
1992
1993 spin_unlock_irqrestore (&ehci->lock, flags);
1994 return 0;
1995}
1996
1997/*-------------------------------------------------------------------------*/
1998
1999static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07002000sitd_patch(
2001 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 struct ehci_iso_stream *stream,
2003 struct ehci_sitd *sitd,
2004 struct ehci_iso_sched *iso_sched,
2005 unsigned index
2006)
2007{
2008 struct ehci_iso_packet *uf = &iso_sched->packet [index];
2009 u64 bufp = uf->bufp;
2010
Stefan Roese6dbd6822007-05-01 09:29:37 -07002011 sitd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 sitd->hw_fullspeed_ep = stream->address;
2013 sitd->hw_uframe = stream->splits;
2014 sitd->hw_results = uf->transaction;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002015 sitd->hw_backpointer = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 bufp = uf->bufp;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002018 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
2019 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Stefan Roese6dbd6822007-05-01 09:29:37 -07002021 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (uf->cross)
2023 bufp += 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002024 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 sitd->index = index;
2026}
2027
2028static inline void
2029sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
2030{
2031 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
2032 sitd->sitd_next = ehci->pshadow [frame];
2033 sitd->hw_next = ehci->periodic [frame];
2034 ehci->pshadow [frame].sitd = sitd;
2035 sitd->frame = frame;
2036 wmb ();
Stefan Roese6dbd6822007-05-01 09:29:37 -07002037 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
2040/* fit urb's sitds into the selected schedule slot; activate as needed */
Alan Sternb015cb72012-07-11 11:22:10 -04002041static void sitd_link_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 struct ehci_hcd *ehci,
2043 struct urb *urb,
2044 unsigned mod,
2045 struct ehci_iso_stream *stream
2046)
2047{
2048 int packet;
2049 unsigned next_uframe;
2050 struct ehci_iso_sched *sched = urb->hcpriv;
2051 struct ehci_sitd *sitd;
2052
2053 next_uframe = stream->next_uframe;
2054
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +03002055 if (list_empty(&stream->td_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /* usbfs ignores TT bandwidth */
2057 ehci_to_hcd(ehci)->self.bandwidth_allocated
2058 += stream->bandwidth;
Alex He05570292010-12-07 10:10:08 +08002059
2060 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002061 if (ehci->amd_pll_fix == 1)
2062 usb_amd_quirk_pll_disable();
Alex He05570292010-12-07 10:10:08 +08002063 }
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
2066
2067 /* fill sITDs frame by frame */
Alan Stern46c73d12013-09-03 13:59:03 -04002068 for (packet = sched->first_packet, sitd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 packet < urb->number_of_packets;
2070 packet++) {
2071
2072 /* ASSERT: we have all necessary sitds */
2073 BUG_ON (list_empty (&sched->td_list));
2074
2075 /* ASSERT: no itds for this endpoint in this frame */
2076
2077 sitd = list_entry (sched->td_list.next,
2078 struct ehci_sitd, sitd_list);
2079 list_move_tail (&sitd->sitd_list, &stream->td_list);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002080 sitd->stream = stream;
Karsten Wiese508db8c2009-02-26 01:47:48 +01002081 sitd->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Stefan Roese6dbd6822007-05-01 09:29:37 -07002083 sitd_patch(ehci, stream, sitd, sched, packet);
Alan Sternbccbefa2010-07-14 11:03:36 -04002084 sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 sitd);
2086
Alan Sternffa02482013-10-11 11:29:03 -04002087 next_uframe += stream->uperiod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Alan Sternbccbefa2010-07-14 11:03:36 -04002089 stream->next_uframe = next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 /* don't need that schedule data any more */
2092 iso_sched_free (stream, sched);
Alan Stern2656a9a2012-11-08 10:17:01 -05002093 urb->hcpriv = stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Alan Stern569b3942012-07-11 11:23:00 -04002095 ++ehci->isoc_count;
Alan Sternb015cb72012-07-11 11:22:10 -04002096 enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
2099/*-------------------------------------------------------------------------*/
2100
2101#define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
David Brownell53bd6a62006-08-30 14:50:06 -07002102 | SITD_STS_XACT | SITD_STS_MMF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
David Brownell30bf54e2007-12-16 22:37:40 -08002104/* Process and recycle a completed SITD. Return true iff its urb completed,
2105 * and hence its completion callback probably added things to the hardware
2106 * schedule.
2107 *
2108 * Note that we carefully avoid recycling this descriptor until after any
2109 * completion callback runs, so that it won't be reused quickly. That is,
2110 * assuming (a) no more than two urbs per frame on this endpoint, and also
2111 * (b) only this endpoint's completions submit URBs. It seems some silicon
2112 * corrupts things if you reuse completed descriptors very quickly...
2113 */
Alan Sternf4289072012-07-11 11:23:07 -04002114static bool sitd_complete(struct ehci_hcd *ehci, struct ehci_sitd *sitd)
2115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 struct urb *urb = sitd->urb;
2117 struct usb_iso_packet_descriptor *desc;
2118 u32 t;
2119 int urb_index = -1;
2120 struct ehci_iso_stream *stream = sitd->stream;
2121 struct usb_device *dev;
Alan Sternf4289072012-07-11 11:23:07 -04002122 bool retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 urb_index = sitd->index;
2125 desc = &urb->iso_frame_desc [urb_index];
Stefan Roese6dbd6822007-05-01 09:29:37 -07002126 t = hc32_to_cpup(ehci, &sitd->hw_results);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 /* report transfer status */
Alan Stern4005ad42012-10-01 10:32:01 -04002129 if (unlikely(t & SITD_ERRS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 urb->error_count++;
2131 if (t & SITD_STS_DBE)
2132 desc->status = usb_pipein (urb->pipe)
2133 ? -ENOSR /* hc couldn't read */
2134 : -ECOMM; /* hc couldn't write */
2135 else if (t & SITD_STS_BABBLE)
2136 desc->status = -EOVERFLOW;
2137 else /* XACT, MMF, etc */
2138 desc->status = -EPROTO;
Alan Stern4005ad42012-10-01 10:32:01 -04002139 } else if (unlikely(t & SITD_STS_ACTIVE)) {
2140 /* URB was too late */
2141 urb->error_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 } else {
2143 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04002144 desc->actual_length = desc->length - SITD_LENGTH(t);
2145 urb->actual_length += desc->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 /* handle completion now? */
2149 if ((urb_index + 1) != urb->number_of_packets)
David Brownell30bf54e2007-12-16 22:37:40 -08002150 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 /* ASSERT: it's really the last sitd for this urb
2153 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2154 BUG_ON (sitd->urb == urb);
2155 */
2156
David Brownellaa16ca32007-12-30 23:45:19 -08002157 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05002158 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04002159 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08002160 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Alan Stern569b3942012-07-11 11:23:00 -04002163 --ehci->isoc_count;
2164 disable_periodic(ehci);
2165
2166 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
Alex He05570292010-12-07 10:10:08 +08002167 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
Andiry Xuad935622011-03-01 14:57:05 +08002168 if (ehci->amd_pll_fix == 1)
2169 usb_amd_quirk_pll_enable();
Alex He05570292010-12-07 10:10:08 +08002170 }
2171
Xenia Ragiadakoufea26ef2013-08-29 11:45:10 +03002172 if (list_is_singular(&stream->td_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 ehci_to_hcd(ehci)->self.bandwidth_allocated
2174 -= stream->bandwidth;
Alan Stern0e5f2312010-04-08 16:56:37 -04002175
David Brownell30bf54e2007-12-16 22:37:40 -08002176done:
David Brownell30bf54e2007-12-16 22:37:40 -08002177 sitd->urb = NULL;
Alan Stern55934eb2012-07-11 11:22:35 -04002178
2179 /* Add to the end of the free list for later reuse */
2180 list_move_tail(&sitd->sitd_list, &stream->free_list);
2181
2182 /* Recycle the siTDs when the pipeline is empty (ep no longer in use) */
2183 if (list_empty(&stream->td_list)) {
2184 list_splice_tail_init(&stream->free_list,
2185 &ehci->cached_sitd_list);
2186 start_free_itds(ehci);
Alan Stern0e5f2312010-04-08 16:56:37 -04002187 }
Alan Stern55934eb2012-07-11 11:22:35 -04002188
David Brownell30bf54e2007-12-16 22:37:40 -08002189 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
2192
Olav Kongas5db539e2005-06-23 20:25:36 +03002193static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04002194 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 int status = -EINVAL;
2197 unsigned long flags;
2198 struct ehci_iso_stream *stream;
2199
2200 /* Get iso_stream head */
2201 stream = iso_stream_find (ehci, urb);
2202 if (stream == NULL) {
2203 ehci_dbg (ehci, "can't get iso stream\n");
2204 return -ENOMEM;
2205 }
Alan Sternffa02482013-10-11 11:29:03 -04002206 if (urb->interval != stream->ps.period) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
Alan Sternffa02482013-10-11 11:29:03 -04002208 stream->ps.period, urb->interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 goto done;
2210 }
2211
2212#ifdef EHCI_URB_TRACE
2213 ehci_dbg (ehci,
2214 "submit %p dev%s ep%d%s-iso len %d\n",
2215 urb, urb->dev->devpath,
2216 usb_pipeendpoint (urb->pipe),
2217 usb_pipein (urb->pipe) ? "in" : "out",
2218 urb->transfer_buffer_length);
2219#endif
2220
2221 /* allocate SITDs */
2222 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2223 if (status < 0) {
2224 ehci_dbg (ehci, "can't init sitds\n");
2225 goto done;
2226 }
2227
2228 /* schedule ... need to lock */
2229 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04002230 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002231 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04002232 goto done_not_linked;
2233 }
2234 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2235 if (unlikely(status))
2236 goto done_not_linked;
2237 status = iso_stream_schedule(ehci, urb, stream);
Alan Stern46c73d12013-09-03 13:59:03 -04002238 if (likely(status == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Stern46c73d12013-09-03 13:59:03 -04002240 } else if (status > 0) {
2241 status = 0;
2242 ehci_urb_done(ehci, urb, 0);
2243 } else {
Alan Sterne9df41c2007-08-08 11:48:02 -04002244 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
Alan Stern46c73d12013-09-03 13:59:03 -04002245 }
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002246 done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 spin_unlock_irqrestore (&ehci->lock, flags);
Alan Stern8c5bf7b2012-07-11 11:22:39 -04002248 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return status;
2250}
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252/*-------------------------------------------------------------------------*/
2253
Alan Stern569b3942012-07-11 11:23:00 -04002254static void scan_isoc(struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Alan Sternf4289072012-07-11 11:23:07 -04002256 unsigned uf, now_frame, frame;
2257 unsigned fmask = ehci->periodic_size - 1;
2258 bool modified, live;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 /*
2261 * When running, scan from last scan point up to "now"
2262 * else clean up by scanning everything that's left.
2263 * Touches as few pages as possible: cache-friendly.
2264 */
Alan Sternc0c53db2012-07-11 11:21:48 -04002265 if (ehci->rh_state >= EHCI_RH_RUNNING) {
Alan Sternf4289072012-07-11 11:23:07 -04002266 uf = ehci_read_frame_index(ehci);
2267 now_frame = (uf >> 3) & fmask;
2268 live = true;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002269 } else {
Alan Sternc3ee9b72012-09-28 16:01:23 -04002270 now_frame = (ehci->last_iso_frame - 1) & fmask;
Alan Sternf4289072012-07-11 11:23:07 -04002271 live = false;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002272 }
Alan Sternf4289072012-07-11 11:23:07 -04002273 ehci->now_frame = now_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Alan Sternb09a61c2013-01-30 16:35:02 -05002275 frame = ehci->last_iso_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 for (;;) {
2277 union ehci_shadow q, *q_p;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002278 __hc32 type, *hw_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280restart:
2281 /* scan each element in frame's queue for completions */
2282 q_p = &ehci->pshadow [frame];
2283 hw_p = &ehci->periodic [frame];
2284 q.ptr = q_p->ptr;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002285 type = Q_NEXT_TYPE(ehci, *hw_p);
Alan Sternf4289072012-07-11 11:23:07 -04002286 modified = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 while (q.ptr != NULL) {
Stefan Roese6dbd6822007-05-01 09:29:37 -07002289 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 case Q_TYPE_ITD:
David Brownell79592b722008-01-07 00:47:42 -08002291 /* If this ITD is still active, leave it for
2292 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002293 * No need to check for activity unless the
2294 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002295 */
Alan Sternf4289072012-07-11 11:23:07 -04002296 if (frame == now_frame && live) {
Alan Sternb40e43f2008-05-20 16:59:10 -04002297 rmb();
2298 for (uf = 0; uf < 8; uf++) {
2299 if (q.itd->hw_transaction[uf] &
2300 ITD_ACTIVE(ehci))
2301 break;
2302 }
2303 if (uf < 8) {
Alan Sternb40e43f2008-05-20 16:59:10 -04002304 q_p = &q.itd->itd_next;
2305 hw_p = &q.itd->hw_next;
2306 type = Q_NEXT_TYPE(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -07002307 q.itd->hw_next);
Alan Sternb40e43f2008-05-20 16:59:10 -04002308 q = *q_p;
2309 break;
2310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
David Brownell79592b722008-01-07 00:47:42 -08002313 /* Take finished ITDs out of the schedule
2314 * and process them: recycle, maybe report
2315 * URB completion. HC won't cache the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 * pointer for much longer, if at all.
2317 */
2318 *q_p = q.itd->itd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002319 if (!ehci->use_dummy_qh ||
2320 q.itd->hw_next != EHCI_LIST_END(ehci))
2321 *hw_p = q.itd->hw_next;
2322 else
2323 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002324 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002326 modified = itd_complete (ehci, q.itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 q = *q_p;
2328 break;
2329 case Q_TYPE_SITD:
David Brownell79592b722008-01-07 00:47:42 -08002330 /* If this SITD is still active, leave it for
2331 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002332 * No need to check for activity unless the
2333 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002334 */
Alan Sternf4289072012-07-11 11:23:07 -04002335 if (((frame == now_frame) ||
2336 (((frame + 1) & fmask) == now_frame))
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002337 && live
2338 && (q.sitd->hw_results &
2339 SITD_ACTIVE(ehci))) {
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 q_p = &q.sitd->sitd_next;
2342 hw_p = &q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002343 type = Q_NEXT_TYPE(ehci,
2344 q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 q = *q_p;
2346 break;
2347 }
David Brownell79592b722008-01-07 00:47:42 -08002348
2349 /* Take finished SITDs out of the schedule
2350 * and process them: recycle, maybe report
2351 * URB completion.
2352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 *q_p = q.sitd->sitd_next;
Andiry Xu3d091a62010-11-08 17:58:35 +08002354 if (!ehci->use_dummy_qh ||
2355 q.sitd->hw_next != EHCI_LIST_END(ehci))
2356 *hw_p = q.sitd->hw_next;
2357 else
2358 *hw_p = ehci->dummy->qh_dma;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002359 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002361 modified = sitd_complete (ehci, q.sitd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 q = *q_p;
2363 break;
2364 default:
Greg Kroah-Hartman2d0fe1b2012-05-01 21:33:36 -07002365 ehci_dbg(ehci, "corrupt type %d frame %d shadow %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 type, frame, q.ptr);
2367 // BUG ();
Alan Stern569b3942012-07-11 11:23:00 -04002368 /* FALL THROUGH */
2369 case Q_TYPE_QH:
2370 case Q_TYPE_FSTN:
2371 /* End of the iTDs and siTDs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 q.ptr = NULL;
Alan Stern569b3942012-07-11 11:23:00 -04002373 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375
2376 /* assume completion callbacks modify the queue */
Alan Sternf4289072012-07-11 11:23:07 -04002377 if (unlikely(modified && ehci->isoc_count > 0))
2378 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380
Alan Sternf4289072012-07-11 11:23:07 -04002381 /* Stop when we have reached the current frame */
2382 if (frame == now_frame)
David Brownell79592b722008-01-07 00:47:42 -08002383 break;
Alan Sternb09a61c2013-01-30 16:35:02 -05002384
2385 /* The last frame may still have active siTDs */
2386 ehci->last_iso_frame = frame;
2387 frame = (frame + 1) & fmask;
David Brownell53bd6a62006-08-30 14:50:06 -07002388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}