blob: 327437af212269c95dc529d8921652e2f1a8f1f2 [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
39/*-------------------------------------------------------------------------*/
40
41/*
42 * periodic_next_shadow - return "next" pointer on shadow list
43 * @periodic: host pointer to qh/itd/sitd
44 * @tag: hardware tag for type of this record
45 */
46static union ehci_shadow *
Stefan Roese6dbd6822007-05-01 09:29:37 -070047periodic_next_shadow(struct ehci_hcd *ehci, union ehci_shadow *periodic,
48 __hc32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Stefan Roese6dbd6822007-05-01 09:29:37 -070050 switch (hc32_to_cpu(ehci, tag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 case Q_TYPE_QH:
52 return &periodic->qh->qh_next;
53 case Q_TYPE_FSTN:
54 return &periodic->fstn->fstn_next;
55 case Q_TYPE_ITD:
56 return &periodic->itd->itd_next;
57 // case Q_TYPE_SITD:
58 default:
59 return &periodic->sitd->sitd_next;
60 }
61}
62
Alek Du3807e262009-07-14 07:23:29 +080063static __hc32 *
64shadow_next_periodic(struct ehci_hcd *ehci, union ehci_shadow *periodic,
65 __hc32 tag)
66{
67 switch (hc32_to_cpu(ehci, tag)) {
68 /* our ehci_shadow.qh is actually software part */
69 case Q_TYPE_QH:
70 return &periodic->qh->hw->hw_next;
71 /* others are hw parts */
72 default:
73 return periodic->hw_next;
74 }
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* caller must hold ehci->lock */
78static void periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
79{
Stefan Roese6dbd6822007-05-01 09:29:37 -070080 union ehci_shadow *prev_p = &ehci->pshadow[frame];
81 __hc32 *hw_p = &ehci->periodic[frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 union ehci_shadow here = *prev_p;
83
84 /* find predecessor of "ptr"; hw and shadow lists are in sync */
85 while (here.ptr && here.ptr != ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -070086 prev_p = periodic_next_shadow(ehci, prev_p,
87 Q_NEXT_TYPE(ehci, *hw_p));
Alek Du3807e262009-07-14 07:23:29 +080088 hw_p = shadow_next_periodic(ehci, &here,
89 Q_NEXT_TYPE(ehci, *hw_p));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 here = *prev_p;
91 }
92 /* an interrupt entry (at list end) could have been shared */
93 if (!here.ptr)
94 return;
95
96 /* update shadow and hardware lists ... the old "next" pointers
97 * from ptr may still be in use, the caller updates them.
98 */
Stefan Roese6dbd6822007-05-01 09:29:37 -070099 *prev_p = *periodic_next_shadow(ehci, &here,
100 Q_NEXT_TYPE(ehci, *hw_p));
Alek Du3807e262009-07-14 07:23:29 +0800101 *hw_p = *shadow_next_periodic(ehci, &here, Q_NEXT_TYPE(ehci, *hw_p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104/* how many of the uframe's 125 usecs are allocated? */
105static unsigned short
106periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
107{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700108 __hc32 *hw_p = &ehci->periodic [frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 union ehci_shadow *q = &ehci->pshadow [frame];
110 unsigned usecs = 0;
Alek Du3807e262009-07-14 07:23:29 +0800111 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 while (q->ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700114 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800116 hw = q->qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* is it in the S-mask? */
Alek Du3807e262009-07-14 07:23:29 +0800118 if (hw->hw_info2 & cpu_to_hc32(ehci, 1 << uframe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 usecs += q->qh->usecs;
120 /* ... or C-mask? */
Alek Du3807e262009-07-14 07:23:29 +0800121 if (hw->hw_info2 & cpu_to_hc32(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700122 1 << (8 + uframe)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 usecs += q->qh->c_usecs;
Alek Du3807e262009-07-14 07:23:29 +0800124 hw_p = &hw->hw_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 q = &q->qh->qh_next;
126 break;
127 // case Q_TYPE_FSTN:
128 default:
129 /* for "save place" FSTNs, count the relevant INTR
130 * bandwidth from the previous frame
131 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700132 if (q->fstn->hw_prev != EHCI_LIST_END(ehci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 ehci_dbg (ehci, "ignoring FSTN cost ...\n");
134 }
135 hw_p = &q->fstn->hw_next;
136 q = &q->fstn->fstn_next;
137 break;
138 case Q_TYPE_ITD:
Karsten Wiese3b6fcfd2007-12-30 21:55:05 -0800139 if (q->itd->hw_transaction[uframe])
140 usecs += q->itd->stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 hw_p = &q->itd->hw_next;
142 q = &q->itd->itd_next;
143 break;
144 case Q_TYPE_SITD:
145 /* is it in the S-mask? (count SPLIT, DATA) */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700146 if (q->sitd->hw_uframe & cpu_to_hc32(ehci,
147 1 << uframe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (q->sitd->hw_fullspeed_ep &
Stefan Roese6dbd6822007-05-01 09:29:37 -0700149 cpu_to_hc32(ehci, 1<<31))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 usecs += q->sitd->stream->usecs;
151 else /* worst case for OUT start-split */
152 usecs += HS_USECS_ISO (188);
153 }
154
155 /* ... C-mask? (count CSPLIT, DATA) */
156 if (q->sitd->hw_uframe &
Stefan Roese6dbd6822007-05-01 09:29:37 -0700157 cpu_to_hc32(ehci, 1 << (8 + uframe))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* worst case for IN complete-split */
159 usecs += q->sitd->stream->c_usecs;
160 }
161
162 hw_p = &q->sitd->hw_next;
163 q = &q->sitd->sitd_next;
164 break;
165 }
166 }
167#ifdef DEBUG
168 if (usecs > 100)
169 ehci_err (ehci, "uframe %d sched overrun: %d usecs\n",
170 frame * 8 + uframe, usecs);
171#endif
172 return usecs;
173}
174
175/*-------------------------------------------------------------------------*/
176
177static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
178{
179 if (!dev1->tt || !dev2->tt)
180 return 0;
181 if (dev1->tt != dev2->tt)
182 return 0;
183 if (dev1->tt->multi)
184 return dev1->ttport == dev2->ttport;
185 else
186 return 1;
187}
188
Dan Streetmanba47f662006-05-24 09:39:16 -0700189#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
190
191/* Which uframe does the low/fullspeed transfer start in?
192 *
193 * The parameter is the mask of ssplits in "H-frame" terms
194 * and this returns the transfer start uframe in "B-frame" terms,
195 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
196 * will cause a transfer in "B-frame" uframe 0. "B-frames" lag
197 * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7.
198 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700199static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask)
Dan Streetmanba47f662006-05-24 09:39:16 -0700200{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700201 unsigned char smask = QH_SMASK & hc32_to_cpu(ehci, mask);
Dan Streetmanba47f662006-05-24 09:39:16 -0700202 if (!smask) {
203 ehci_err(ehci, "invalid empty smask!\n");
204 /* uframe 7 can't have bw so this will indicate failure */
205 return 7;
206 }
207 return ffs(smask) - 1;
208}
209
210static const unsigned char
211max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
212
213/* carryover low/fullspeed bandwidth that crosses uframe boundries */
214static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
215{
216 int i;
217 for (i=0; i<7; i++) {
218 if (max_tt_usecs[i] < tt_usecs[i]) {
219 tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i];
220 tt_usecs[i] = max_tt_usecs[i];
221 }
222 }
223}
224
225/* How many of the tt's periodic downstream 1000 usecs are allocated?
226 *
227 * While this measures the bandwidth in terms of usecs/uframe,
228 * the low/fullspeed bus has no notion of uframes, so any particular
229 * low/fullspeed transfer can "carry over" from one uframe to the next,
230 * since the TT just performs downstream transfers in sequence.
231 *
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800232 * For example two separate 100 usec transfers can start in the same uframe,
Dan Streetmanba47f662006-05-24 09:39:16 -0700233 * and the second one would "carry over" 75 usecs into the next uframe.
234 */
235static void
236periodic_tt_usecs (
237 struct ehci_hcd *ehci,
238 struct usb_device *dev,
239 unsigned frame,
240 unsigned short tt_usecs[8]
241)
242{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700243 __hc32 *hw_p = &ehci->periodic [frame];
Dan Streetmanba47f662006-05-24 09:39:16 -0700244 union ehci_shadow *q = &ehci->pshadow [frame];
245 unsigned char uf;
246
247 memset(tt_usecs, 0, 16);
248
249 while (q->ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700250 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
Dan Streetmanba47f662006-05-24 09:39:16 -0700251 case Q_TYPE_ITD:
252 hw_p = &q->itd->hw_next;
253 q = &q->itd->itd_next;
254 continue;
255 case Q_TYPE_QH:
256 if (same_tt(dev, q->qh->dev)) {
Alek Du3807e262009-07-14 07:23:29 +0800257 uf = tt_start_uframe(ehci, q->qh->hw->hw_info2);
Dan Streetmanba47f662006-05-24 09:39:16 -0700258 tt_usecs[uf] += q->qh->tt_usecs;
259 }
Alek Du3807e262009-07-14 07:23:29 +0800260 hw_p = &q->qh->hw->hw_next;
Dan Streetmanba47f662006-05-24 09:39:16 -0700261 q = &q->qh->qh_next;
262 continue;
263 case Q_TYPE_SITD:
264 if (same_tt(dev, q->sitd->urb->dev)) {
265 uf = tt_start_uframe(ehci, q->sitd->hw_uframe);
266 tt_usecs[uf] += q->sitd->stream->tt_usecs;
267 }
268 hw_p = &q->sitd->hw_next;
269 q = &q->sitd->sitd_next;
270 continue;
271 // case Q_TYPE_FSTN:
272 default:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700273 ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n",
274 frame);
Dan Streetmanba47f662006-05-24 09:39:16 -0700275 hw_p = &q->fstn->hw_next;
276 q = &q->fstn->fstn_next;
277 }
278 }
279
280 carryover_tt_bandwidth(tt_usecs);
281
282 if (max_tt_usecs[7] < tt_usecs[7])
283 ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
284 frame, tt_usecs[7] - max_tt_usecs[7]);
285}
286
287/*
288 * Return true if the device's tt's downstream bus is available for a
289 * periodic transfer of the specified length (usecs), starting at the
290 * specified frame/uframe. Note that (as summarized in section 11.19
291 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
292 * uframe.
293 *
294 * The uframe parameter is when the fullspeed/lowspeed transfer
295 * should be executed in "B-frame" terms, which is the same as the
296 * highspeed ssplit's uframe (which is in "H-frame" terms). For example
297 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
298 * See the EHCI spec sec 4.5 and fig 4.7.
299 *
300 * This checks if the full/lowspeed bus, at the specified starting uframe,
301 * has the specified bandwidth available, according to rules listed
302 * in USB 2.0 spec section 11.18.1 fig 11-60.
303 *
304 * This does not check if the transfer would exceed the max ssplit
305 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
306 * since proper scheduling limits ssplits to less than 16 per uframe.
307 */
308static int tt_available (
309 struct ehci_hcd *ehci,
310 unsigned period,
311 struct usb_device *dev,
312 unsigned frame,
313 unsigned uframe,
314 u16 usecs
315)
316{
317 if ((period == 0) || (uframe >= 7)) /* error */
318 return 0;
319
320 for (; frame < ehci->periodic_size; frame += period) {
321 unsigned short tt_usecs[8];
322
323 periodic_tt_usecs (ehci, dev, frame, tt_usecs);
324
325 ehci_vdbg(ehci, "tt frame %d check %d usecs start uframe %d in"
326 " schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
327 frame, usecs, uframe,
328 tt_usecs[0], tt_usecs[1], tt_usecs[2], tt_usecs[3],
329 tt_usecs[4], tt_usecs[5], tt_usecs[6], tt_usecs[7]);
330
331 if (max_tt_usecs[uframe] <= tt_usecs[uframe]) {
332 ehci_vdbg(ehci, "frame %d uframe %d fully scheduled\n",
333 frame, uframe);
334 return 0;
335 }
336
337 /* special case for isoc transfers larger than 125us:
338 * the first and each subsequent fully used uframe
339 * must be empty, so as to not illegally delay
340 * already scheduled transactions
341 */
342 if (125 < usecs) {
Dan Streetmanc065c602009-04-21 13:37:12 -0400343 int ufs = (usecs / 125);
Dan Streetmanba47f662006-05-24 09:39:16 -0700344 int i;
345 for (i = uframe; i < (uframe + ufs) && i < 8; i++)
346 if (0 < tt_usecs[i]) {
347 ehci_vdbg(ehci,
348 "multi-uframe xfer can't fit "
349 "in frame %d uframe %d\n",
350 frame, i);
351 return 0;
352 }
353 }
354
355 tt_usecs[uframe] += usecs;
356
357 carryover_tt_bandwidth(tt_usecs);
358
359 /* fail if the carryover pushed bw past the last uframe's limit */
360 if (max_tt_usecs[7] < tt_usecs[7]) {
361 ehci_vdbg(ehci,
362 "tt unavailable usecs %d frame %d uframe %d\n",
363 usecs, frame, uframe);
364 return 0;
365 }
366 }
367
368 return 1;
369}
370
371#else
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/* return true iff the device's transaction translator is available
374 * for a periodic transfer starting at the specified frame, using
375 * all the uframes in the mask.
376 */
377static int tt_no_collision (
378 struct ehci_hcd *ehci,
379 unsigned period,
380 struct usb_device *dev,
381 unsigned frame,
382 u32 uf_mask
383)
384{
385 if (period == 0) /* error */
386 return 0;
387
388 /* note bandwidth wastage: split never follows csplit
389 * (different dev or endpoint) until the next uframe.
390 * calling convention doesn't make that distinction.
391 */
392 for (; frame < ehci->periodic_size; frame += period) {
393 union ehci_shadow here;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700394 __hc32 type;
Alek Du3807e262009-07-14 07:23:29 +0800395 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 here = ehci->pshadow [frame];
Stefan Roese6dbd6822007-05-01 09:29:37 -0700398 type = Q_NEXT_TYPE(ehci, ehci->periodic [frame]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700400 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 case Q_TYPE_ITD:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700402 type = Q_NEXT_TYPE(ehci, here.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 here = here.itd->itd_next;
404 continue;
405 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800406 hw = here.qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (same_tt (dev, here.qh->dev)) {
408 u32 mask;
409
Stefan Roese6dbd6822007-05-01 09:29:37 -0700410 mask = hc32_to_cpu(ehci,
Alek Du3807e262009-07-14 07:23:29 +0800411 hw->hw_info2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* "knows" no gap is needed */
413 mask |= mask >> 8;
414 if (mask & uf_mask)
415 break;
416 }
Alek Du3807e262009-07-14 07:23:29 +0800417 type = Q_NEXT_TYPE(ehci, hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 here = here.qh->qh_next;
419 continue;
420 case Q_TYPE_SITD:
421 if (same_tt (dev, here.sitd->urb->dev)) {
422 u16 mask;
423
Stefan Roese6dbd6822007-05-01 09:29:37 -0700424 mask = hc32_to_cpu(ehci, here.sitd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 ->hw_uframe);
426 /* FIXME assumes no gap for IN! */
427 mask |= mask >> 8;
428 if (mask & uf_mask)
429 break;
430 }
Stefan Roese6dbd6822007-05-01 09:29:37 -0700431 type = Q_NEXT_TYPE(ehci, here.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 here = here.sitd->sitd_next;
433 continue;
434 // case Q_TYPE_FSTN:
435 default:
436 ehci_dbg (ehci,
437 "periodic frame %d bogus type %d\n",
438 frame, type);
439 }
440
441 /* collision or error */
442 return 0;
443 }
444 }
445
446 /* no collision */
447 return 1;
448}
449
Dan Streetmanba47f662006-05-24 09:39:16 -0700450#endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/*-------------------------------------------------------------------------*/
453
454static int enable_periodic (struct ehci_hcd *ehci)
455{
456 u32 cmd;
457 int status;
458
David Brownell01c17142008-08-26 23:35:04 -0700459 if (ehci->periodic_sched++)
460 return 0;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* did clearing PSE did take effect yet?
463 * takes effect only at frame boundaries...
464 */
Karsten Wiesec765d4c2008-02-16 13:44:42 -0800465 status = handshake_on_error_set_halt(ehci, &ehci->regs->status,
466 STS_PSS, 0, 9 * 125);
467 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100470 cmd = ehci_readl(ehci, &ehci->regs->command) | CMD_PSE;
471 ehci_writel(ehci, cmd, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* posted write ... PSS happens later */
473 ehci_to_hcd(ehci)->state = HC_STATE_RUNNING;
474
475 /* make sure ehci_work scans these */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100476 ehci->next_uframe = ehci_readl(ehci, &ehci->regs->frame_index)
477 % (ehci->periodic_size << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 0;
479}
480
481static int disable_periodic (struct ehci_hcd *ehci)
482{
483 u32 cmd;
484 int status;
485
David Brownell01c17142008-08-26 23:35:04 -0700486 if (--ehci->periodic_sched)
487 return 0;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* did setting PSE not take effect yet?
490 * takes effect only at frame boundaries...
491 */
Karsten Wiesec765d4c2008-02-16 13:44:42 -0800492 status = handshake_on_error_set_halt(ehci, &ehci->regs->status,
493 STS_PSS, STS_PSS, 9 * 125);
494 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100497 cmd = ehci_readl(ehci, &ehci->regs->command) & ~CMD_PSE;
498 ehci_writel(ehci, cmd, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* posted write ... */
500
501 ehci->next_uframe = -1;
502 return 0;
503}
504
505/*-------------------------------------------------------------------------*/
506
507/* periodic schedule slots have iso tds (normal or split) first, then a
508 * sparse tree for active interrupt transfers.
509 *
510 * this just links in a qh; caller guarantees uframe masks are set right.
511 * no FSTN support (yet; ehci 0.96+)
512 */
513static int qh_link_periodic (struct ehci_hcd *ehci, struct ehci_qh *qh)
514{
515 unsigned i;
516 unsigned period = qh->period;
517
518 dev_dbg (&qh->dev->dev,
519 "link qh%d-%04x/%p start %d [%d/%d us]\n",
Alek Du3807e262009-07-14 07:23:29 +0800520 period, hc32_to_cpup(ehci, &qh->hw->hw_info2)
521 & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 qh, qh->start, qh->usecs, qh->c_usecs);
523
524 /* high bandwidth, or otherwise every microframe */
525 if (period == 0)
526 period = 1;
527
528 for (i = qh->start; i < ehci->periodic_size; i += period) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700529 union ehci_shadow *prev = &ehci->pshadow[i];
530 __hc32 *hw_p = &ehci->periodic[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 union ehci_shadow here = *prev;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700532 __hc32 type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* skip the iso nodes at list head */
535 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700536 type = Q_NEXT_TYPE(ehci, *hw_p);
537 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700539 prev = periodic_next_shadow(ehci, prev, type);
Alek Du3807e262009-07-14 07:23:29 +0800540 hw_p = shadow_next_periodic(ehci, &here, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 here = *prev;
542 }
543
544 /* sorting each branch by period (slow-->fast)
545 * enables sharing interior tree nodes
546 */
547 while (here.ptr && qh != here.qh) {
548 if (qh->period > here.qh->period)
549 break;
550 prev = &here.qh->qh_next;
Alek Du3807e262009-07-14 07:23:29 +0800551 hw_p = &here.qh->hw->hw_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 here = *prev;
553 }
554 /* link in this qh, unless some earlier pass did that */
555 if (qh != here.qh) {
556 qh->qh_next = here;
557 if (here.qh)
Alek Du3807e262009-07-14 07:23:29 +0800558 qh->hw->hw_next = *hw_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 wmb ();
560 prev->qh = qh;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700561 *hw_p = QH_NEXT (ehci, qh->qh_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 }
564 qh->qh_state = QH_STATE_LINKED;
Alan Sternef4638f2009-07-31 10:41:40 -0400565 qh->xacterrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 qh_get (qh);
567
568 /* update per-qh bandwidth for usbfs */
569 ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period
570 ? ((qh->usecs + qh->c_usecs) / qh->period)
571 : (qh->usecs * 8);
572
573 /* maybe enable periodic schedule processing */
David Brownell01c17142008-08-26 23:35:04 -0700574 return enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
David Brownell01c17142008-08-26 23:35:04 -0700577static int qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 unsigned i;
580 unsigned period;
581
582 // FIXME:
583 // IF this isn't high speed
584 // and this qh is active in the current uframe
585 // (and overlay token SplitXstate is false?)
586 // THEN
Harvey Harrison551509d2009-02-11 14:11:36 -0800587 // qh->hw_info1 |= cpu_to_hc32(1 << 7 /* "ignore" */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /* high bandwidth, or otherwise part of every microframe */
590 if ((period = qh->period) == 0)
591 period = 1;
592
593 for (i = qh->start; i < ehci->periodic_size; i += period)
594 periodic_unlink (ehci, i, qh);
595
596 /* update per-qh bandwidth for usbfs */
597 ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period
598 ? ((qh->usecs + qh->c_usecs) / qh->period)
599 : (qh->usecs * 8);
600
601 dev_dbg (&qh->dev->dev,
602 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
David Brownell7dedacf2005-08-04 18:06:41 -0700603 qh->period,
Alek Du3807e262009-07-14 07:23:29 +0800604 hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 qh, qh->start, qh->usecs, qh->c_usecs);
606
607 /* qh->qh_next still "live" to HC */
608 qh->qh_state = QH_STATE_UNLINK;
609 qh->qh_next.ptr = NULL;
610 qh_put (qh);
611
612 /* maybe turn off periodic schedule */
David Brownell01c17142008-08-26 23:35:04 -0700613 return disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616static void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
617{
618 unsigned wait;
Alek Du3807e262009-07-14 07:23:29 +0800619 struct ehci_qh_hw *hw = qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 qh_unlink_periodic (ehci, qh);
622
623 /* simple/paranoid: always delay, expecting the HC needs to read
624 * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and
625 * expect khubd to clean up after any CSPLITs we won't issue.
626 * active high speed queues may need bigger delays...
627 */
628 if (list_empty (&qh->qtd_list)
Stefan Roese6dbd6822007-05-01 09:29:37 -0700629 || (cpu_to_hc32(ehci, QH_CMASK)
Alek Du3807e262009-07-14 07:23:29 +0800630 & hw->hw_info2) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 wait = 2;
632 else
633 wait = 55; /* worst case: 3 * 1024 */
634
635 udelay (wait);
636 qh->qh_state = QH_STATE_IDLE;
Alek Du3807e262009-07-14 07:23:29 +0800637 hw->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 wmb ();
639}
640
641/*-------------------------------------------------------------------------*/
642
643static int check_period (
David Brownell53bd6a62006-08-30 14:50:06 -0700644 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 unsigned frame,
646 unsigned uframe,
647 unsigned period,
648 unsigned usecs
649) {
650 int claimed;
651
652 /* complete split running into next frame?
653 * given FSTN support, we could sometimes check...
654 */
655 if (uframe >= 8)
656 return 0;
657
658 /*
659 * 80% periodic == 100 usec/uframe available
David Brownell53bd6a62006-08-30 14:50:06 -0700660 * convert "usecs we need" to "max already claimed"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
662 usecs = 100 - usecs;
663
664 /* we "know" 2 and 4 uframe intervals were rejected; so
665 * for period 0, check _every_ microframe in the schedule.
666 */
667 if (unlikely (period == 0)) {
668 do {
669 for (uframe = 0; uframe < 7; uframe++) {
670 claimed = periodic_usecs (ehci, frame, uframe);
671 if (claimed > usecs)
672 return 0;
673 }
674 } while ((frame += 1) < ehci->periodic_size);
675
676 /* just check the specified uframe, at that period */
677 } else {
678 do {
679 claimed = periodic_usecs (ehci, frame, uframe);
680 if (claimed > usecs)
681 return 0;
682 } while ((frame += period) < ehci->periodic_size);
683 }
684
685 // success!
686 return 1;
687}
688
689static int check_intr_schedule (
David Brownell53bd6a62006-08-30 14:50:06 -0700690 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 unsigned frame,
692 unsigned uframe,
693 const struct ehci_qh *qh,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700694 __hc32 *c_maskp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695)
696{
David Brownell53bd6a62006-08-30 14:50:06 -0700697 int retval = -ENOSPC;
Dan Streetmanba47f662006-05-24 09:39:16 -0700698 u8 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
701 goto done;
702
703 if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
704 goto done;
705 if (!qh->c_usecs) {
706 retval = 0;
707 *c_maskp = 0;
708 goto done;
709 }
710
Dan Streetmanba47f662006-05-24 09:39:16 -0700711#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
712 if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
713 qh->tt_usecs)) {
714 unsigned i;
715
716 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
717 for (i=uframe+1; i<8 && i<uframe+4; i++)
718 if (!check_period (ehci, frame, i,
719 qh->period, qh->c_usecs))
720 goto done;
721 else
722 mask |= 1 << i;
723
724 retval = 0;
725
Stefan Roese6dbd6822007-05-01 09:29:37 -0700726 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Dan Streetmanba47f662006-05-24 09:39:16 -0700727 }
728#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* Make sure this tt's buffer is also available for CSPLITs.
730 * We pessimize a bit; probably the typical full speed case
731 * doesn't need the second CSPLIT.
David Brownell53bd6a62006-08-30 14:50:06 -0700732 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * NOTE: both SPLIT and CSPLIT could be checked in just
734 * one smart pass...
735 */
736 mask = 0x03 << (uframe + qh->gap_uf);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700737 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 mask |= 1 << uframe;
740 if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
741 if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
742 qh->period, qh->c_usecs))
743 goto done;
744 if (!check_period (ehci, frame, uframe + qh->gap_uf,
745 qh->period, qh->c_usecs))
746 goto done;
747 retval = 0;
748 }
Dan Streetmanba47f662006-05-24 09:39:16 -0700749#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750done:
751 return retval;
752}
753
754/* "first fit" scheduling policy used the first time through,
755 * or when the previous schedule slot can't be re-used.
756 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700757static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
David Brownell53bd6a62006-08-30 14:50:06 -0700759 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 unsigned uframe;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700761 __hc32 c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
Alek Du3807e262009-07-14 07:23:29 +0800763 struct ehci_qh_hw *hw = qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 qh_refresh(ehci, qh);
Alek Du3807e262009-07-14 07:23:29 +0800766 hw->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 frame = qh->start;
768
769 /* reuse the previous schedule slots, if we can */
770 if (frame < qh->period) {
Alek Du3807e262009-07-14 07:23:29 +0800771 uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 status = check_intr_schedule (ehci, frame, --uframe,
773 qh, &c_mask);
774 } else {
775 uframe = 0;
776 c_mask = 0;
777 status = -ENOSPC;
778 }
779
780 /* else scan the schedule to find a group of slots such that all
781 * uframes have enough periodic bandwidth available.
782 */
783 if (status) {
784 /* "normal" case, uframing flexible except with splits */
785 if (qh->period) {
Alan Stern68335e82009-05-22 17:02:33 -0400786 int i;
787
788 for (i = qh->period; status && i > 0; --i) {
789 frame = ++ehci->random_frame % qh->period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 for (uframe = 0; uframe < 8; uframe++) {
791 status = check_intr_schedule (ehci,
792 frame, uframe, qh,
793 &c_mask);
794 if (status == 0)
795 break;
796 }
Alan Stern68335e82009-05-22 17:02:33 -0400797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /* qh->period == 0 means every uframe */
800 } else {
801 frame = 0;
802 status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
803 }
804 if (status)
805 goto done;
806 qh->start = frame;
807
808 /* reset S-frame and (maybe) C-frame masks */
Alek Du3807e262009-07-14 07:23:29 +0800809 hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
810 hw->hw_info2 |= qh->period
Stefan Roese6dbd6822007-05-01 09:29:37 -0700811 ? cpu_to_hc32(ehci, 1 << uframe)
812 : cpu_to_hc32(ehci, QH_SMASK);
Alek Du3807e262009-07-14 07:23:29 +0800813 hw->hw_info2 |= c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 } else
815 ehci_dbg (ehci, "reused qh %p schedule\n", qh);
816
817 /* stuff into the periodic schedule */
David Brownell53bd6a62006-08-30 14:50:06 -0700818 status = qh_link_periodic (ehci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819done:
820 return status;
821}
822
823static int intr_submit (
824 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct urb *urb,
826 struct list_head *qtd_list,
Al Viro55016f12005-10-21 03:21:58 -0400827 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828) {
829 unsigned epnum;
830 unsigned long flags;
831 struct ehci_qh *qh;
Alan Sterne9df41c2007-08-08 11:48:02 -0400832 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 struct list_head empty;
834
835 /* get endpoint and transfer/schedule data */
Alan Sterne9df41c2007-08-08 11:48:02 -0400836 epnum = urb->ep->desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 spin_lock_irqsave (&ehci->lock, flags);
839
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100840 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700841 &ehci_to_hcd(ehci)->flags))) {
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
865 /* ... update usbfs periodic stats */
866 ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
867
868done:
Alan Sterne9df41c2007-08-08 11:48:02 -0400869 if (unlikely(status))
870 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
871done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 spin_unlock_irqrestore (&ehci->lock, flags);
873 if (status)
874 qtd_list_free (ehci, urb, qtd_list);
875
876 return status;
877}
878
879/*-------------------------------------------------------------------------*/
880
881/* ehci_iso_stream ops work with both ITD and SITD */
882
883static struct ehci_iso_stream *
Al Viro55016f12005-10-21 03:21:58 -0400884iso_stream_alloc (gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 struct ehci_iso_stream *stream;
887
Pekka Enberg7b842b62005-09-06 15:18:34 -0700888 stream = kzalloc(sizeof *stream, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 INIT_LIST_HEAD(&stream->td_list);
891 INIT_LIST_HEAD(&stream->free_list);
892 stream->next_uframe = -1;
893 stream->refcount = 1;
894 }
895 return stream;
896}
897
898static void
899iso_stream_init (
900 struct ehci_hcd *ehci,
901 struct ehci_iso_stream *stream,
902 struct usb_device *dev,
903 int pipe,
904 unsigned interval
905)
906{
907 static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
908
909 u32 buf1;
910 unsigned epnum, maxp;
911 int is_input;
912 long bandwidth;
913
914 /*
915 * this might be a "high bandwidth" highspeed endpoint,
916 * as encoded in the ep descriptor's wMaxPacket field
917 */
918 epnum = usb_pipeendpoint (pipe);
919 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
920 maxp = usb_maxpacket(dev, pipe, !is_input);
921 if (is_input) {
922 buf1 = (1 << 11);
923 } else {
924 buf1 = 0;
925 }
926
927 /* knows about ITD vs SITD */
928 if (dev->speed == USB_SPEED_HIGH) {
929 unsigned multi = hb_mult(maxp);
930
931 stream->highspeed = 1;
932
933 maxp = max_packet(maxp);
934 buf1 |= maxp;
935 maxp *= multi;
936
Stefan Roese6dbd6822007-05-01 09:29:37 -0700937 stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
938 stream->buf1 = cpu_to_hc32(ehci, buf1);
939 stream->buf2 = cpu_to_hc32(ehci, multi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 /* usbfs wants to report the average usecs per frame tied up
942 * when transfers on this endpoint are scheduled ...
943 */
944 stream->usecs = HS_USECS_ISO (maxp);
945 bandwidth = stream->usecs * 8;
Alan Stern372dd6e2008-11-12 17:02:57 -0500946 bandwidth /= interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 } else {
949 u32 addr;
david-b@pacbell.netd0384202005-08-13 18:44:58 -0700950 int think_time;
Clemens Ladisch469d0222006-01-20 13:49:10 -0800951 int hs_transfers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 addr = dev->ttport << 24;
954 if (!ehci_is_TDI(ehci)
955 || (dev->tt->hub !=
956 ehci_to_hcd(ehci)->self.root_hub))
957 addr |= dev->tt->hub->devnum << 16;
958 addr |= epnum << 8;
959 addr |= dev->devnum;
960 stream->usecs = HS_USECS_ISO (maxp);
david-b@pacbell.netd0384202005-08-13 18:44:58 -0700961 think_time = dev->tt ? dev->tt->think_time : 0;
962 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
963 dev->speed, is_input, 1, maxp));
Clemens Ladisch469d0222006-01-20 13:49:10 -0800964 hs_transfers = max (1u, (maxp + 187) / 188);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (is_input) {
966 u32 tmp;
967
968 addr |= 1 << 31;
969 stream->c_usecs = stream->usecs;
970 stream->usecs = HS_USECS_ISO (1);
971 stream->raw_mask = 1;
972
Clemens Ladisch469d0222006-01-20 13:49:10 -0800973 /* c-mask as specified in USB 2.0 11.18.4 3.c */
974 tmp = (1 << (hs_transfers + 2)) - 1;
975 stream->raw_mask |= tmp << (8 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 } else
Clemens Ladisch469d0222006-01-20 13:49:10 -0800977 stream->raw_mask = smask_out [hs_transfers - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 bandwidth = stream->usecs + stream->c_usecs;
Alan Stern372dd6e2008-11-12 17:02:57 -0500979 bandwidth /= interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 /* stream->splits gets created from raw_mask later */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700982 stream->address = cpu_to_hc32(ehci, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 stream->bandwidth = bandwidth;
985
986 stream->udev = dev;
987
988 stream->bEndpointAddress = is_input | epnum;
989 stream->interval = interval;
990 stream->maxp = maxp;
991}
992
993static void
994iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
995{
996 stream->refcount--;
997
998 /* free whenever just a dev->ep reference remains.
999 * not like a QH -- no persistent state (toggle, halt)
1000 */
1001 if (stream->refcount == 1) {
1002 int is_in;
1003
1004 // BUG_ON (!list_empty(&stream->td_list));
1005
1006 while (!list_empty (&stream->free_list)) {
1007 struct list_head *entry;
1008
1009 entry = stream->free_list.next;
1010 list_del (entry);
1011
1012 /* knows about ITD vs SITD */
1013 if (stream->highspeed) {
1014 struct ehci_itd *itd;
1015
1016 itd = list_entry (entry, struct ehci_itd,
1017 itd_list);
1018 dma_pool_free (ehci->itd_pool, itd,
1019 itd->itd_dma);
1020 } else {
1021 struct ehci_sitd *sitd;
1022
1023 sitd = list_entry (entry, struct ehci_sitd,
1024 sitd_list);
1025 dma_pool_free (ehci->sitd_pool, sitd,
1026 sitd->sitd_dma);
1027 }
1028 }
1029
1030 is_in = (stream->bEndpointAddress & USB_DIR_IN) ? 0x10 : 0;
1031 stream->bEndpointAddress &= 0x0f;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001032 if (stream->ep)
1033 stream->ep->hcpriv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (stream->rescheduled) {
1036 ehci_info (ehci, "ep%d%s-iso rescheduled "
1037 "%lu times in %lu seconds\n",
1038 stream->bEndpointAddress, is_in ? "in" : "out",
1039 stream->rescheduled,
1040 ((jiffies - stream->start)/HZ)
1041 );
1042 }
1043
1044 kfree(stream);
1045 }
1046}
1047
1048static inline struct ehci_iso_stream *
1049iso_stream_get (struct ehci_iso_stream *stream)
1050{
1051 if (likely (stream != NULL))
1052 stream->refcount++;
1053 return stream;
1054}
1055
1056static struct ehci_iso_stream *
1057iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1058{
1059 unsigned epnum;
1060 struct ehci_iso_stream *stream;
1061 struct usb_host_endpoint *ep;
1062 unsigned long flags;
1063
1064 epnum = usb_pipeendpoint (urb->pipe);
1065 if (usb_pipein(urb->pipe))
1066 ep = urb->dev->ep_in[epnum];
1067 else
1068 ep = urb->dev->ep_out[epnum];
1069
1070 spin_lock_irqsave (&ehci->lock, flags);
1071 stream = ep->hcpriv;
1072
1073 if (unlikely (stream == NULL)) {
1074 stream = iso_stream_alloc(GFP_ATOMIC);
1075 if (likely (stream != NULL)) {
1076 /* dev->ep owns the initial refcount */
1077 ep->hcpriv = stream;
1078 stream->ep = ep;
1079 iso_stream_init(ehci, stream, urb->dev, urb->pipe,
1080 urb->interval);
1081 }
1082
1083 /* if dev->ep [epnum] is a QH, info1.maxpacket is nonzero */
1084 } else if (unlikely (stream->hw_info1 != 0)) {
1085 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1086 urb->dev->devpath, epnum,
1087 usb_pipein(urb->pipe) ? "in" : "out");
1088 stream = NULL;
1089 }
1090
1091 /* caller guarantees an eventual matching iso_stream_put */
1092 stream = iso_stream_get (stream);
1093
1094 spin_unlock_irqrestore (&ehci->lock, flags);
1095 return stream;
1096}
1097
1098/*-------------------------------------------------------------------------*/
1099
1100/* ehci_iso_sched ops can be ITD-only or SITD-only */
1101
1102static struct ehci_iso_sched *
Al Viro55016f12005-10-21 03:21:58 -04001103iso_sched_alloc (unsigned packets, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 struct ehci_iso_sched *iso_sched;
1106 int size = sizeof *iso_sched;
1107
1108 size += packets * sizeof (struct ehci_iso_packet);
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001109 iso_sched = kzalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (likely (iso_sched != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 INIT_LIST_HEAD (&iso_sched->td_list);
1112 }
1113 return iso_sched;
1114}
1115
1116static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001117itd_sched_init(
1118 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 struct ehci_iso_sched *iso_sched,
1120 struct ehci_iso_stream *stream,
1121 struct urb *urb
1122)
1123{
1124 unsigned i;
1125 dma_addr_t dma = urb->transfer_dma;
1126
1127 /* how many uframes are needed for these transfers */
1128 iso_sched->span = urb->number_of_packets * stream->interval;
1129
1130 /* figure out per-uframe itd fields that we'll need later
1131 * when we fit new itds into the schedule.
1132 */
1133 for (i = 0; i < urb->number_of_packets; i++) {
1134 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1135 unsigned length;
1136 dma_addr_t buf;
1137 u32 trans;
1138
1139 length = urb->iso_frame_desc [i].length;
1140 buf = dma + urb->iso_frame_desc [i].offset;
1141
1142 trans = EHCI_ISOC_ACTIVE;
1143 trans |= buf & 0x0fff;
1144 if (unlikely (((i + 1) == urb->number_of_packets))
1145 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1146 trans |= EHCI_ITD_IOC;
1147 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001148 uframe->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
David Brownell77078572005-05-28 10:46:18 -07001150 /* might need to cross a buffer page within a uframe */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 uframe->bufp = (buf & ~(u64)0x0fff);
1152 buf += length;
1153 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1154 uframe->cross = 1;
1155 }
1156}
1157
1158static void
1159iso_sched_free (
1160 struct ehci_iso_stream *stream,
1161 struct ehci_iso_sched *iso_sched
1162)
1163{
1164 if (!iso_sched)
1165 return;
1166 // caller must hold ehci->lock!
1167 list_splice (&iso_sched->td_list, &stream->free_list);
1168 kfree (iso_sched);
1169}
1170
1171static int
1172itd_urb_transaction (
1173 struct ehci_iso_stream *stream,
1174 struct ehci_hcd *ehci,
1175 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001176 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177)
1178{
1179 struct ehci_itd *itd;
1180 dma_addr_t itd_dma;
1181 int i;
1182 unsigned num_itds;
1183 struct ehci_iso_sched *sched;
1184 unsigned long flags;
1185
1186 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1187 if (unlikely (sched == NULL))
1188 return -ENOMEM;
1189
Stefan Roese6dbd6822007-05-01 09:29:37 -07001190 itd_sched_init(ehci, sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 if (urb->interval < 8)
1193 num_itds = 1 + (sched->span + 7) / 8;
1194 else
1195 num_itds = urb->number_of_packets;
1196
1197 /* allocate/init ITDs */
1198 spin_lock_irqsave (&ehci->lock, flags);
1199 for (i = 0; i < num_itds; i++) {
1200
1201 /* free_list.next might be cache-hot ... but maybe
1202 * the HC caches it too. avoid that issue for now.
1203 */
1204
1205 /* prefer previously-allocated itds */
1206 if (likely (!list_empty(&stream->free_list))) {
1207 itd = list_entry (stream->free_list.prev,
Stefan Roese6dbd6822007-05-01 09:29:37 -07001208 struct ehci_itd, itd_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 list_del (&itd->itd_list);
1210 itd_dma = itd->itd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001211 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 spin_unlock_irqrestore (&ehci->lock, flags);
1213 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1214 &itd_dma);
1215 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001216 if (!itd) {
1217 iso_sched_free(stream, sched);
1218 spin_unlock_irqrestore(&ehci->lock, flags);
1219 return -ENOMEM;
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 memset (itd, 0, sizeof *itd);
1224 itd->itd_dma = itd_dma;
1225 list_add (&itd->itd_list, &sched->td_list);
1226 }
1227 spin_unlock_irqrestore (&ehci->lock, flags);
1228
1229 /* temporarily store schedule info in hcpriv */
1230 urb->hcpriv = sched;
1231 urb->error_count = 0;
1232 return 0;
1233}
1234
1235/*-------------------------------------------------------------------------*/
1236
1237static inline int
1238itd_slot_ok (
1239 struct ehci_hcd *ehci,
1240 u32 mod,
1241 u32 uframe,
1242 u8 usecs,
1243 u32 period
1244)
1245{
1246 uframe %= period;
1247 do {
1248 /* can't commit more than 80% periodic == 100 usec */
1249 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
1250 > (100 - usecs))
1251 return 0;
1252
1253 /* we know urb->interval is 2^N uframes */
1254 uframe += period;
1255 } while (uframe < mod);
1256 return 1;
1257}
1258
1259static inline int
1260sitd_slot_ok (
1261 struct ehci_hcd *ehci,
1262 u32 mod,
1263 struct ehci_iso_stream *stream,
1264 u32 uframe,
1265 struct ehci_iso_sched *sched,
1266 u32 period_uframes
1267)
1268{
1269 u32 mask, tmp;
1270 u32 frame, uf;
1271
1272 mask = stream->raw_mask << (uframe & 7);
1273
1274 /* for IN, don't wrap CSPLIT into the next frame */
1275 if (mask & ~0xffff)
1276 return 0;
1277
1278 /* this multi-pass logic is simple, but performance may
1279 * suffer when the schedule data isn't cached.
1280 */
1281
1282 /* check bandwidth */
1283 uframe %= period_uframes;
1284 do {
1285 u32 max_used;
1286
1287 frame = uframe >> 3;
1288 uf = uframe & 7;
1289
Dan Streetmanba47f662006-05-24 09:39:16 -07001290#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1291 /* The tt's fullspeed bus bandwidth must be available.
1292 * tt_available scheduling guarantees 10+% for control/bulk.
1293 */
1294 if (!tt_available (ehci, period_uframes << 3,
1295 stream->udev, frame, uf, stream->tt_usecs))
1296 return 0;
1297#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /* tt must be idle for start(s), any gap, and csplit.
1299 * assume scheduling slop leaves 10+% for control/bulk.
1300 */
1301 if (!tt_no_collision (ehci, period_uframes << 3,
1302 stream->udev, frame, mask))
1303 return 0;
Dan Streetmanba47f662006-05-24 09:39:16 -07001304#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 /* check starts (OUT uses more than one) */
1307 max_used = 100 - stream->usecs;
1308 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
1309 if (periodic_usecs (ehci, frame, uf) > max_used)
1310 return 0;
1311 }
1312
1313 /* for IN, check CSPLIT */
1314 if (stream->c_usecs) {
Clemens Ladisch0c734622006-01-22 10:32:49 -08001315 uf = uframe & 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 max_used = 100 - stream->c_usecs;
1317 do {
1318 tmp = 1 << uf;
1319 tmp <<= 8;
1320 if ((stream->raw_mask & tmp) == 0)
1321 continue;
1322 if (periodic_usecs (ehci, frame, uf)
1323 > max_used)
1324 return 0;
1325 } while (++uf < 8);
1326 }
1327
1328 /* we know urb->interval is 2^N uframes */
1329 uframe += period_uframes;
1330 } while (uframe < mod);
1331
Stefan Roese6dbd6822007-05-01 09:29:37 -07001332 stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return 1;
1334}
1335
1336/*
1337 * This scheduler plans almost as far into the future as it has actual
1338 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1339 * "as small as possible" to be cache-friendlier.) That limits the size
1340 * transfers you can stream reliably; avoid more than 64 msec per urb.
1341 * Also avoid queue depths of less than ehci's worst irq latency (affected
1342 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1343 * and other factors); or more than about 230 msec total (for portability,
1344 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1345 */
1346
1347#define SCHEDULE_SLOP 10 /* frames */
1348
1349static int
1350iso_stream_schedule (
1351 struct ehci_hcd *ehci,
1352 struct urb *urb,
1353 struct ehci_iso_stream *stream
1354)
1355{
1356 u32 now, start, max, period;
1357 int status;
1358 unsigned mod = ehci->periodic_size << 3;
1359 struct ehci_iso_sched *sched = urb->hcpriv;
1360
1361 if (sched->span > (mod - 8 * SCHEDULE_SLOP)) {
1362 ehci_dbg (ehci, "iso request %p too long\n", urb);
1363 status = -EFBIG;
1364 goto fail;
1365 }
1366
1367 if ((stream->depth + sched->span) > mod) {
1368 ehci_dbg (ehci, "request %p would overflow (%d+%d>%d)\n",
1369 urb, stream->depth, sched->span, mod);
1370 status = -EFBIG;
1371 goto fail;
1372 }
1373
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001374 now = ehci_readl(ehci, &ehci->regs->frame_index) % mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /* when's the last uframe this urb could start? */
1377 max = now + mod;
1378
Alan Sternb40e43f2008-05-20 16:59:10 -04001379 /* Typical case: reuse current schedule, stream is still active.
1380 * Hopefully there are no gaps from the host falling behind
1381 * (irq delays etc), but if there are we'll take the next
1382 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
1384 if (likely (!list_empty (&stream->td_list))) {
1385 start = stream->next_uframe;
1386 if (start < now)
1387 start += mod;
Alan Sternb40e43f2008-05-20 16:59:10 -04001388
1389 /* Fell behind (by up to twice the slop amount)? */
1390 if (start >= max - 2 * 8 * SCHEDULE_SLOP)
1391 start += stream->interval * DIV_ROUND_UP(
1392 max - start, stream->interval) - mod;
1393
1394 /* Tried to schedule too far into the future? */
1395 if (unlikely((start + sched->span) >= max)) {
1396 status = -EFBIG;
1397 goto fail;
1398 }
1399 goto ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
1402 /* need to schedule; when's the next (u)frame we could start?
1403 * this is bigger than ehci->i_thresh allows; scheduling itself
1404 * isn't free, the slop should handle reasonably slow cpus. it
1405 * can also help high bandwidth if the dma and irq loads don't
1406 * jump until after the queue is primed.
1407 */
1408 start = SCHEDULE_SLOP * 8 + (now & ~0x07);
1409 start %= mod;
1410 stream->next_uframe = start;
1411
1412 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
1413
1414 period = urb->interval;
1415 if (!stream->highspeed)
1416 period <<= 3;
1417
1418 /* find a uframe slot with enough bandwidth */
1419 for (; start < (stream->next_uframe + period); start++) {
1420 int enough_space;
1421
1422 /* check schedule: enough space? */
1423 if (stream->highspeed)
1424 enough_space = itd_slot_ok (ehci, mod, start,
1425 stream->usecs, period);
1426 else {
1427 if ((start % 8) >= 6)
1428 continue;
1429 enough_space = sitd_slot_ok (ehci, mod, stream,
1430 start, sched, period);
1431 }
1432
1433 /* schedule it here if there's enough bandwidth */
1434 if (enough_space) {
1435 stream->next_uframe = start % mod;
1436 goto ready;
1437 }
1438 }
1439
1440 /* no room in the schedule */
1441 ehci_dbg (ehci, "iso %ssched full %p (now %d max %d)\n",
1442 list_empty (&stream->td_list) ? "" : "re",
1443 urb, now, max);
1444 status = -ENOSPC;
1445
1446fail:
1447 iso_sched_free (stream, sched);
1448 urb->hcpriv = NULL;
1449 return status;
1450
1451ready:
1452 /* report high speed start in uframes; full speed, in frames */
1453 urb->start_frame = stream->next_uframe;
1454 if (!stream->highspeed)
1455 urb->start_frame >>= 3;
1456 return 0;
1457}
1458
1459/*-------------------------------------------------------------------------*/
1460
1461static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001462itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1463 struct ehci_itd *itd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 int i;
1466
David Brownell77078572005-05-28 10:46:18 -07001467 /* it's been recently zeroed */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001468 itd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 itd->hw_bufp [0] = stream->buf0;
1470 itd->hw_bufp [1] = stream->buf1;
1471 itd->hw_bufp [2] = stream->buf2;
1472
1473 for (i = 0; i < 8; i++)
1474 itd->index[i] = -1;
1475
1476 /* All other fields are filled when scheduling */
1477}
1478
1479static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001480itd_patch(
1481 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 struct ehci_itd *itd,
1483 struct ehci_iso_sched *iso_sched,
1484 unsigned index,
David Brownell77078572005-05-28 10:46:18 -07001485 u16 uframe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486)
1487{
1488 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1489 unsigned pg = itd->pg;
1490
1491 // BUG_ON (pg == 6 && uf->cross);
1492
1493 uframe &= 0x07;
1494 itd->index [uframe] = index;
1495
Stefan Roese6dbd6822007-05-01 09:29:37 -07001496 itd->hw_transaction[uframe] = uf->transaction;
1497 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1498 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1499 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 /* iso_frame_desc[].offset must be strictly increasing */
David Brownell77078572005-05-28 10:46:18 -07001502 if (unlikely (uf->cross)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 u64 bufp = uf->bufp + 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 itd->pg = ++pg;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001506 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1507 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509}
1510
1511static inline void
1512itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1513{
1514 /* always prepend ITD/SITD ... only QH tree is order-sensitive */
1515 itd->itd_next = ehci->pshadow [frame];
1516 itd->hw_next = ehci->periodic [frame];
1517 ehci->pshadow [frame].itd = itd;
1518 itd->frame = frame;
1519 wmb ();
Stefan Roese6dbd6822007-05-01 09:29:37 -07001520 ehci->periodic[frame] = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
1523/* fit urb's itds into the selected schedule slot; activate as needed */
1524static int
1525itd_link_urb (
1526 struct ehci_hcd *ehci,
1527 struct urb *urb,
1528 unsigned mod,
1529 struct ehci_iso_stream *stream
1530)
1531{
David Brownell77078572005-05-28 10:46:18 -07001532 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 unsigned next_uframe, uframe, frame;
1534 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1535 struct ehci_itd *itd;
1536
1537 next_uframe = stream->next_uframe % mod;
1538
1539 if (unlikely (list_empty(&stream->td_list))) {
1540 ehci_to_hcd(ehci)->self.bandwidth_allocated
1541 += stream->bandwidth;
1542 ehci_vdbg (ehci,
1543 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1544 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1545 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1546 urb->interval,
1547 next_uframe >> 3, next_uframe & 0x7);
1548 stream->start = jiffies;
1549 }
1550 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1551
1552 /* fill iTDs uframe by uframe */
1553 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1554 if (itd == NULL) {
1555 /* ASSERT: we have all necessary itds */
1556 // BUG_ON (list_empty (&iso_sched->td_list));
1557
1558 /* ASSERT: no itds for this endpoint in this uframe */
1559
1560 itd = list_entry (iso_sched->td_list.next,
1561 struct ehci_itd, itd_list);
1562 list_move_tail (&itd->itd_list, &stream->td_list);
1563 itd->stream = iso_stream_get (stream);
Karsten Wiese508db8c2009-02-26 01:47:48 +01001564 itd->urb = urb;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001565 itd_init (ehci, stream, itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567
1568 uframe = next_uframe & 0x07;
1569 frame = next_uframe >> 3;
1570
Stefan Roese6dbd6822007-05-01 09:29:37 -07001571 itd_patch(ehci, itd, iso_sched, packet, uframe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 next_uframe += stream->interval;
1574 stream->depth += stream->interval;
1575 next_uframe %= mod;
1576 packet++;
1577
1578 /* link completed itds into the schedule */
1579 if (((next_uframe >> 3) != frame)
1580 || packet == urb->number_of_packets) {
1581 itd_link (ehci, frame % ehci->periodic_size, itd);
1582 itd = NULL;
1583 }
1584 }
1585 stream->next_uframe = next_uframe;
1586
1587 /* don't need that schedule data any more */
1588 iso_sched_free (stream, iso_sched);
1589 urb->hcpriv = NULL;
1590
1591 timer_action (ehci, TIMER_IO_WATCHDOG);
David Brownell01c17142008-08-26 23:35:04 -07001592 return enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1596
David Brownell30bf54e2007-12-16 22:37:40 -08001597/* Process and recycle a completed ITD. Return true iff its urb completed,
1598 * and hence its completion callback probably added things to the hardware
1599 * schedule.
1600 *
1601 * Note that we carefully avoid recycling this descriptor until after any
1602 * completion callback runs, so that it won't be reused quickly. That is,
1603 * assuming (a) no more than two urbs per frame on this endpoint, and also
1604 * (b) only this endpoint's completions submit URBs. It seems some silicon
1605 * corrupts things if you reuse completed descriptors very quickly...
1606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607static unsigned
1608itd_complete (
1609 struct ehci_hcd *ehci,
David Howells7d12e782006-10-05 14:55:46 +01001610 struct ehci_itd *itd
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611) {
1612 struct urb *urb = itd->urb;
1613 struct usb_iso_packet_descriptor *desc;
1614 u32 t;
1615 unsigned uframe;
1616 int urb_index = -1;
1617 struct ehci_iso_stream *stream = itd->stream;
1618 struct usb_device *dev;
David Brownell30bf54e2007-12-16 22:37:40 -08001619 unsigned retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 /* for each uframe with a packet */
1622 for (uframe = 0; uframe < 8; uframe++) {
1623 if (likely (itd->index[uframe] == -1))
1624 continue;
1625 urb_index = itd->index[uframe];
1626 desc = &urb->iso_frame_desc [urb_index];
1627
Stefan Roese6dbd6822007-05-01 09:29:37 -07001628 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 itd->hw_transaction [uframe] = 0;
1630 stream->depth -= stream->interval;
1631
1632 /* report transfer status */
1633 if (unlikely (t & ISO_ERRS)) {
1634 urb->error_count++;
1635 if (t & EHCI_ISOC_BUF_ERR)
1636 desc->status = usb_pipein (urb->pipe)
1637 ? -ENOSR /* hc couldn't read */
1638 : -ECOMM; /* hc couldn't write */
1639 else if (t & EHCI_ISOC_BABBLE)
1640 desc->status = -EOVERFLOW;
1641 else /* (t & EHCI_ISOC_XACTERR) */
1642 desc->status = -EPROTO;
1643
1644 /* HC need not update length with this error */
Alan Sternec6d67e2009-06-29 14:34:59 -04001645 if (!(t & EHCI_ISOC_BABBLE)) {
1646 desc->actual_length = EHCI_ITD_LENGTH(t);
1647 urb->actual_length += desc->actual_length;
1648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1650 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04001651 desc->actual_length = EHCI_ITD_LENGTH(t);
1652 urb->actual_length += desc->actual_length;
Alan Sternb40e43f2008-05-20 16:59:10 -04001653 } else {
1654 /* URB was too late */
1655 desc->status = -EXDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
1657 }
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 /* handle completion now? */
1660 if (likely ((urb_index + 1) != urb->number_of_packets))
David Brownell30bf54e2007-12-16 22:37:40 -08001661 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 /* ASSERT: it's really the last itd for this urb
1664 list_for_each_entry (itd, &stream->td_list, itd_list)
1665 BUG_ON (itd->urb == urb);
1666 */
1667
David Brownellaa16ca32007-12-30 23:45:19 -08001668 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05001669 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04001670 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08001671 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 urb = NULL;
David Brownell01c17142008-08-26 23:35:04 -07001673 (void) disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
1675
Karsten Wiese508db8c2009-02-26 01:47:48 +01001676 if (unlikely(list_is_singular(&stream->td_list))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 ehci_to_hcd(ehci)->self.bandwidth_allocated
1678 -= stream->bandwidth;
1679 ehci_vdbg (ehci,
1680 "deschedule devp %s ep%d%s-iso\n",
1681 dev->devpath, stream->bEndpointAddress & 0x0f,
1682 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1683 }
1684 iso_stream_put (ehci, stream);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001685
David Brownell30bf54e2007-12-16 22:37:40 -08001686done:
David Brownell30bf54e2007-12-16 22:37:40 -08001687 itd->urb = NULL;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001688 if (ehci->clock_frame != itd->frame || itd->index[7] != -1) {
1689 /* OK to recycle this ITD now. */
1690 itd->stream = NULL;
1691 list_move(&itd->itd_list, &stream->free_list);
1692 iso_stream_put(ehci, stream);
1693 } else {
1694 /* HW might remember this ITD, so we can't recycle it yet.
1695 * Move it to a safe place until a new frame starts.
1696 */
1697 list_move(&itd->itd_list, &ehci->cached_itd_list);
1698 if (stream->refcount == 2) {
1699 /* If iso_stream_put() were called here, stream
1700 * would be freed. Instead, just prevent reuse.
1701 */
1702 stream->ep->hcpriv = NULL;
1703 stream->ep = NULL;
1704 }
1705 }
David Brownell30bf54e2007-12-16 22:37:40 -08001706 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
1709/*-------------------------------------------------------------------------*/
1710
Olav Kongas5db539e2005-06-23 20:25:36 +03001711static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001712 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 int status = -EINVAL;
1715 unsigned long flags;
1716 struct ehci_iso_stream *stream;
1717
1718 /* Get iso_stream head */
1719 stream = iso_stream_find (ehci, urb);
1720 if (unlikely (stream == NULL)) {
1721 ehci_dbg (ehci, "can't get iso stream\n");
1722 return -ENOMEM;
1723 }
1724 if (unlikely (urb->interval != stream->interval)) {
1725 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1726 stream->interval, urb->interval);
1727 goto done;
1728 }
1729
1730#ifdef EHCI_URB_TRACE
1731 ehci_dbg (ehci,
1732 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001733 __func__, urb->dev->devpath, urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 usb_pipeendpoint (urb->pipe),
1735 usb_pipein (urb->pipe) ? "in" : "out",
1736 urb->transfer_buffer_length,
1737 urb->number_of_packets, urb->interval,
1738 stream);
1739#endif
1740
1741 /* allocate ITDs w/o locking anything */
1742 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1743 if (unlikely (status < 0)) {
1744 ehci_dbg (ehci, "can't init itds\n");
1745 goto done;
1746 }
1747
1748 /* schedule ... need to lock */
1749 spin_lock_irqsave (&ehci->lock, flags);
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001750 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
Alan Sterne9df41c2007-08-08 11:48:02 -04001751 &ehci_to_hcd(ehci)->flags))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001752 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04001753 goto done_not_linked;
1754 }
1755 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1756 if (unlikely(status))
1757 goto done_not_linked;
1758 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07001759 if (likely (status == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04001761 else
1762 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
1763done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 spin_unlock_irqrestore (&ehci->lock, flags);
1765
1766done:
1767 if (unlikely (status < 0))
1768 iso_stream_put (ehci, stream);
1769 return status;
1770}
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772/*-------------------------------------------------------------------------*/
1773
1774/*
1775 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1776 * TTs in USB 2.0 hubs. These need microframe scheduling.
1777 */
1778
1779static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001780sitd_sched_init(
1781 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 struct ehci_iso_sched *iso_sched,
1783 struct ehci_iso_stream *stream,
1784 struct urb *urb
1785)
1786{
1787 unsigned i;
1788 dma_addr_t dma = urb->transfer_dma;
1789
1790 /* how many frames are needed for these transfers */
1791 iso_sched->span = urb->number_of_packets * stream->interval;
1792
1793 /* figure out per-frame sitd fields that we'll need later
1794 * when we fit new sitds into the schedule.
1795 */
1796 for (i = 0; i < urb->number_of_packets; i++) {
1797 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1798 unsigned length;
1799 dma_addr_t buf;
1800 u32 trans;
1801
1802 length = urb->iso_frame_desc [i].length & 0x03ff;
1803 buf = dma + urb->iso_frame_desc [i].offset;
1804
1805 trans = SITD_STS_ACTIVE;
1806 if (((i + 1) == urb->number_of_packets)
1807 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1808 trans |= SITD_IOC;
1809 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001810 packet->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 /* might need to cross a buffer page within a td */
1813 packet->bufp = buf;
1814 packet->buf1 = (buf + length) & ~0x0fff;
1815 if (packet->buf1 != (buf & ~(u64)0x0fff))
1816 packet->cross = 1;
1817
David Brownell53bd6a62006-08-30 14:50:06 -07001818 /* OUT uses multiple start-splits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (stream->bEndpointAddress & USB_DIR_IN)
1820 continue;
1821 length = (length + 187) / 188;
1822 if (length > 1) /* BEGIN vs ALL */
1823 length |= 1 << 3;
1824 packet->buf1 |= length;
1825 }
1826}
1827
1828static int
1829sitd_urb_transaction (
1830 struct ehci_iso_stream *stream,
1831 struct ehci_hcd *ehci,
1832 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001833 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834)
1835{
1836 struct ehci_sitd *sitd;
1837 dma_addr_t sitd_dma;
1838 int i;
1839 struct ehci_iso_sched *iso_sched;
1840 unsigned long flags;
1841
1842 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1843 if (iso_sched == NULL)
1844 return -ENOMEM;
1845
Stefan Roese6dbd6822007-05-01 09:29:37 -07001846 sitd_sched_init(ehci, iso_sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 /* allocate/init sITDs */
1849 spin_lock_irqsave (&ehci->lock, flags);
1850 for (i = 0; i < urb->number_of_packets; i++) {
1851
1852 /* NOTE: for now, we don't try to handle wraparound cases
1853 * for IN (using sitd->hw_backpointer, like a FSTN), which
1854 * means we never need two sitds for full speed packets.
1855 */
1856
1857 /* free_list.next might be cache-hot ... but maybe
1858 * the HC caches it too. avoid that issue for now.
1859 */
1860
1861 /* prefer previously-allocated sitds */
1862 if (!list_empty(&stream->free_list)) {
1863 sitd = list_entry (stream->free_list.prev,
1864 struct ehci_sitd, sitd_list);
1865 list_del (&sitd->sitd_list);
1866 sitd_dma = sitd->sitd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001867 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 spin_unlock_irqrestore (&ehci->lock, flags);
1869 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1870 &sitd_dma);
1871 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001872 if (!sitd) {
1873 iso_sched_free(stream, iso_sched);
1874 spin_unlock_irqrestore(&ehci->lock, flags);
1875 return -ENOMEM;
1876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 memset (sitd, 0, sizeof *sitd);
1880 sitd->sitd_dma = sitd_dma;
1881 list_add (&sitd->sitd_list, &iso_sched->td_list);
1882 }
1883
1884 /* temporarily store schedule info in hcpriv */
1885 urb->hcpriv = iso_sched;
1886 urb->error_count = 0;
1887
1888 spin_unlock_irqrestore (&ehci->lock, flags);
1889 return 0;
1890}
1891
1892/*-------------------------------------------------------------------------*/
1893
1894static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001895sitd_patch(
1896 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 struct ehci_iso_stream *stream,
1898 struct ehci_sitd *sitd,
1899 struct ehci_iso_sched *iso_sched,
1900 unsigned index
1901)
1902{
1903 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1904 u64 bufp = uf->bufp;
1905
Stefan Roese6dbd6822007-05-01 09:29:37 -07001906 sitd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 sitd->hw_fullspeed_ep = stream->address;
1908 sitd->hw_uframe = stream->splits;
1909 sitd->hw_results = uf->transaction;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001910 sitd->hw_backpointer = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 bufp = uf->bufp;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001913 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
1914 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Stefan Roese6dbd6822007-05-01 09:29:37 -07001916 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (uf->cross)
1918 bufp += 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001919 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 sitd->index = index;
1921}
1922
1923static inline void
1924sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
1925{
1926 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1927 sitd->sitd_next = ehci->pshadow [frame];
1928 sitd->hw_next = ehci->periodic [frame];
1929 ehci->pshadow [frame].sitd = sitd;
1930 sitd->frame = frame;
1931 wmb ();
Stefan Roese6dbd6822007-05-01 09:29:37 -07001932 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
1935/* fit urb's sitds into the selected schedule slot; activate as needed */
1936static int
1937sitd_link_urb (
1938 struct ehci_hcd *ehci,
1939 struct urb *urb,
1940 unsigned mod,
1941 struct ehci_iso_stream *stream
1942)
1943{
1944 int packet;
1945 unsigned next_uframe;
1946 struct ehci_iso_sched *sched = urb->hcpriv;
1947 struct ehci_sitd *sitd;
1948
1949 next_uframe = stream->next_uframe;
1950
1951 if (list_empty(&stream->td_list)) {
1952 /* usbfs ignores TT bandwidth */
1953 ehci_to_hcd(ehci)->self.bandwidth_allocated
1954 += stream->bandwidth;
1955 ehci_vdbg (ehci,
1956 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
1957 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1958 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1959 (next_uframe >> 3) % ehci->periodic_size,
Stefan Roese6dbd6822007-05-01 09:29:37 -07001960 stream->interval, hc32_to_cpu(ehci, stream->splits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 stream->start = jiffies;
1962 }
1963 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1964
1965 /* fill sITDs frame by frame */
1966 for (packet = 0, sitd = NULL;
1967 packet < urb->number_of_packets;
1968 packet++) {
1969
1970 /* ASSERT: we have all necessary sitds */
1971 BUG_ON (list_empty (&sched->td_list));
1972
1973 /* ASSERT: no itds for this endpoint in this frame */
1974
1975 sitd = list_entry (sched->td_list.next,
1976 struct ehci_sitd, sitd_list);
1977 list_move_tail (&sitd->sitd_list, &stream->td_list);
1978 sitd->stream = iso_stream_get (stream);
Karsten Wiese508db8c2009-02-26 01:47:48 +01001979 sitd->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Stefan Roese6dbd6822007-05-01 09:29:37 -07001981 sitd_patch(ehci, stream, sitd, sched, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 sitd_link (ehci, (next_uframe >> 3) % ehci->periodic_size,
1983 sitd);
1984
1985 next_uframe += stream->interval << 3;
1986 stream->depth += stream->interval << 3;
1987 }
1988 stream->next_uframe = next_uframe % mod;
1989
1990 /* don't need that schedule data any more */
1991 iso_sched_free (stream, sched);
1992 urb->hcpriv = NULL;
1993
1994 timer_action (ehci, TIMER_IO_WATCHDOG);
David Brownell01c17142008-08-26 23:35:04 -07001995 return enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
1998/*-------------------------------------------------------------------------*/
1999
2000#define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
David Brownell53bd6a62006-08-30 14:50:06 -07002001 | SITD_STS_XACT | SITD_STS_MMF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
David Brownell30bf54e2007-12-16 22:37:40 -08002003/* Process and recycle a completed SITD. Return true iff its urb completed,
2004 * and hence its completion callback probably added things to the hardware
2005 * schedule.
2006 *
2007 * Note that we carefully avoid recycling this descriptor until after any
2008 * completion callback runs, so that it won't be reused quickly. That is,
2009 * assuming (a) no more than two urbs per frame on this endpoint, and also
2010 * (b) only this endpoint's completions submit URBs. It seems some silicon
2011 * corrupts things if you reuse completed descriptors very quickly...
2012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013static unsigned
2014sitd_complete (
2015 struct ehci_hcd *ehci,
David Howells7d12e782006-10-05 14:55:46 +01002016 struct ehci_sitd *sitd
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017) {
2018 struct urb *urb = sitd->urb;
2019 struct usb_iso_packet_descriptor *desc;
2020 u32 t;
2021 int urb_index = -1;
2022 struct ehci_iso_stream *stream = sitd->stream;
2023 struct usb_device *dev;
David Brownell30bf54e2007-12-16 22:37:40 -08002024 unsigned retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 urb_index = sitd->index;
2027 desc = &urb->iso_frame_desc [urb_index];
Stefan Roese6dbd6822007-05-01 09:29:37 -07002028 t = hc32_to_cpup(ehci, &sitd->hw_results);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 /* report transfer status */
2031 if (t & SITD_ERRS) {
2032 urb->error_count++;
2033 if (t & SITD_STS_DBE)
2034 desc->status = usb_pipein (urb->pipe)
2035 ? -ENOSR /* hc couldn't read */
2036 : -ECOMM; /* hc couldn't write */
2037 else if (t & SITD_STS_BABBLE)
2038 desc->status = -EOVERFLOW;
2039 else /* XACT, MMF, etc */
2040 desc->status = -EPROTO;
2041 } else {
2042 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04002043 desc->actual_length = desc->length - SITD_LENGTH(t);
2044 urb->actual_length += desc->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 stream->depth -= stream->interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /* handle completion now? */
2049 if ((urb_index + 1) != urb->number_of_packets)
David Brownell30bf54e2007-12-16 22:37:40 -08002050 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 /* ASSERT: it's really the last sitd for this urb
2053 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2054 BUG_ON (sitd->urb == urb);
2055 */
2056
David Brownellaa16ca32007-12-30 23:45:19 -08002057 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05002058 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04002059 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08002060 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 urb = NULL;
David Brownell01c17142008-08-26 23:35:04 -07002062 (void) disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
2064
Karsten Wiese508db8c2009-02-26 01:47:48 +01002065 if (list_is_singular(&stream->td_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 ehci_to_hcd(ehci)->self.bandwidth_allocated
2067 -= stream->bandwidth;
2068 ehci_vdbg (ehci,
2069 "deschedule devp %s ep%d%s-iso\n",
2070 dev->devpath, stream->bEndpointAddress & 0x0f,
2071 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
2072 }
2073 iso_stream_put (ehci, stream);
David Brownell30bf54e2007-12-16 22:37:40 -08002074 /* OK to recycle this SITD now that its completion callback ran. */
2075done:
David Brownell30bf54e2007-12-16 22:37:40 -08002076 sitd->urb = NULL;
2077 sitd->stream = NULL;
2078 list_move(&sitd->sitd_list, &stream->free_list);
2079 iso_stream_put(ehci, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
David Brownell30bf54e2007-12-16 22:37:40 -08002081 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
2084
Olav Kongas5db539e2005-06-23 20:25:36 +03002085static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04002086 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 int status = -EINVAL;
2089 unsigned long flags;
2090 struct ehci_iso_stream *stream;
2091
2092 /* Get iso_stream head */
2093 stream = iso_stream_find (ehci, urb);
2094 if (stream == NULL) {
2095 ehci_dbg (ehci, "can't get iso stream\n");
2096 return -ENOMEM;
2097 }
2098 if (urb->interval != stream->interval) {
2099 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
2100 stream->interval, urb->interval);
2101 goto done;
2102 }
2103
2104#ifdef EHCI_URB_TRACE
2105 ehci_dbg (ehci,
2106 "submit %p dev%s ep%d%s-iso len %d\n",
2107 urb, urb->dev->devpath,
2108 usb_pipeendpoint (urb->pipe),
2109 usb_pipein (urb->pipe) ? "in" : "out",
2110 urb->transfer_buffer_length);
2111#endif
2112
2113 /* allocate SITDs */
2114 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2115 if (status < 0) {
2116 ehci_dbg (ehci, "can't init sitds\n");
2117 goto done;
2118 }
2119
2120 /* schedule ... need to lock */
2121 spin_lock_irqsave (&ehci->lock, flags);
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002122 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
Alan Sterne9df41c2007-08-08 11:48:02 -04002123 &ehci_to_hcd(ehci)->flags))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002124 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04002125 goto done_not_linked;
2126 }
2127 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2128 if (unlikely(status))
2129 goto done_not_linked;
2130 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07002131 if (status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04002133 else
2134 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
2135done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 spin_unlock_irqrestore (&ehci->lock, flags);
2137
2138done:
2139 if (status < 0)
2140 iso_stream_put (ehci, stream);
2141 return status;
2142}
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144/*-------------------------------------------------------------------------*/
2145
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002146static void free_cached_itd_list(struct ehci_hcd *ehci)
2147{
2148 struct ehci_itd *itd, *n;
2149
2150 list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) {
2151 struct ehci_iso_stream *stream = itd->stream;
2152 itd->stream = NULL;
2153 list_move(&itd->itd_list, &stream->free_list);
2154 iso_stream_put(ehci, stream);
2155 }
2156}
2157
2158/*-------------------------------------------------------------------------*/
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160static void
David Howells7d12e782006-10-05 14:55:46 +01002161scan_periodic (struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
Alan Sternb40e43f2008-05-20 16:59:10 -04002163 unsigned now_uframe, frame, clock, clock_frame, mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 unsigned modified;
2165
2166 mod = ehci->periodic_size << 3;
2167
2168 /*
2169 * When running, scan from last scan point up to "now"
2170 * else clean up by scanning everything that's left.
2171 * Touches as few pages as possible: cache-friendly.
2172 */
2173 now_uframe = ehci->next_uframe;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002174 if (HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11002175 clock = ehci_readl(ehci, &ehci->regs->frame_index);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002176 clock_frame = (clock >> 3) % ehci->periodic_size;
2177 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 clock = now_uframe + mod - 1;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002179 clock_frame = -1;
2180 }
2181 if (ehci->clock_frame != clock_frame) {
2182 free_cached_itd_list(ehci);
2183 ehci->clock_frame = clock_frame;
2184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 clock %= mod;
Alan Sternb40e43f2008-05-20 16:59:10 -04002186 clock_frame = clock >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 for (;;) {
2189 union ehci_shadow q, *q_p;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002190 __hc32 type, *hw_p;
David Brownell79592b722008-01-07 00:47:42 -08002191 unsigned incomplete = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 frame = now_uframe >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195restart:
2196 /* scan each element in frame's queue for completions */
2197 q_p = &ehci->pshadow [frame];
2198 hw_p = &ehci->periodic [frame];
2199 q.ptr = q_p->ptr;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002200 type = Q_NEXT_TYPE(ehci, *hw_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 modified = 0;
2202
2203 while (q.ptr != NULL) {
2204 unsigned uf;
2205 union ehci_shadow temp;
2206 int live;
2207
2208 live = HC_IS_RUNNING (ehci_to_hcd(ehci)->state);
Stefan Roese6dbd6822007-05-01 09:29:37 -07002209 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 case Q_TYPE_QH:
2211 /* handle any completions */
2212 temp.qh = qh_get (q.qh);
Alek Du3807e262009-07-14 07:23:29 +08002213 type = Q_NEXT_TYPE(ehci, q.qh->hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 q = q.qh->qh_next;
David Howells7d12e782006-10-05 14:55:46 +01002215 modified = qh_completions (ehci, temp.qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (unlikely (list_empty (&temp.qh->qtd_list)))
2217 intr_deschedule (ehci, temp.qh);
2218 qh_put (temp.qh);
2219 break;
2220 case Q_TYPE_FSTN:
2221 /* for "save place" FSTNs, look at QH entries
2222 * in the previous frame for completions.
2223 */
Stefan Roese6dbd6822007-05-01 09:29:37 -07002224 if (q.fstn->hw_prev != EHCI_LIST_END(ehci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 dbg ("ignoring completions from FSTNs");
2226 }
Stefan Roese6dbd6822007-05-01 09:29:37 -07002227 type = Q_NEXT_TYPE(ehci, q.fstn->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 q = q.fstn->fstn_next;
2229 break;
2230 case Q_TYPE_ITD:
David Brownell79592b722008-01-07 00:47:42 -08002231 /* If this ITD is still active, leave it for
2232 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002233 * No need to check for activity unless the
2234 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002235 */
Alan Sternb40e43f2008-05-20 16:59:10 -04002236 if (frame == clock_frame && live) {
2237 rmb();
2238 for (uf = 0; uf < 8; uf++) {
2239 if (q.itd->hw_transaction[uf] &
2240 ITD_ACTIVE(ehci))
2241 break;
2242 }
2243 if (uf < 8) {
2244 incomplete = true;
2245 q_p = &q.itd->itd_next;
2246 hw_p = &q.itd->hw_next;
2247 type = Q_NEXT_TYPE(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -07002248 q.itd->hw_next);
Alan Sternb40e43f2008-05-20 16:59:10 -04002249 q = *q_p;
2250 break;
2251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
David Brownell79592b722008-01-07 00:47:42 -08002254 /* Take finished ITDs out of the schedule
2255 * and process them: recycle, maybe report
2256 * URB completion. HC won't cache the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 * pointer for much longer, if at all.
2258 */
2259 *q_p = q.itd->itd_next;
2260 *hw_p = q.itd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002261 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002263 modified = itd_complete (ehci, q.itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 q = *q_p;
2265 break;
2266 case Q_TYPE_SITD:
David Brownell79592b722008-01-07 00:47:42 -08002267 /* If this SITD is still active, leave it for
2268 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002269 * No need to check for activity unless the
2270 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002271 */
Alan Sternb40e43f2008-05-20 16:59:10 -04002272 if (frame == clock_frame && live &&
2273 (q.sitd->hw_results &
2274 SITD_ACTIVE(ehci))) {
David Brownell79592b722008-01-07 00:47:42 -08002275 incomplete = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 q_p = &q.sitd->sitd_next;
2277 hw_p = &q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002278 type = Q_NEXT_TYPE(ehci,
2279 q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 q = *q_p;
2281 break;
2282 }
David Brownell79592b722008-01-07 00:47:42 -08002283
2284 /* Take finished SITDs out of the schedule
2285 * and process them: recycle, maybe report
2286 * URB completion.
2287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 *q_p = q.sitd->sitd_next;
2289 *hw_p = q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002290 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002292 modified = sitd_complete (ehci, q.sitd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 q = *q_p;
2294 break;
2295 default:
2296 dbg ("corrupt type %d frame %d shadow %p",
2297 type, frame, q.ptr);
2298 // BUG ();
2299 q.ptr = NULL;
2300 }
2301
2302 /* assume completion callbacks modify the queue */
David Brownellaa16ca32007-12-30 23:45:19 -08002303 if (unlikely (modified)) {
2304 if (likely(ehci->periodic_sched > 0))
2305 goto restart;
David Brownell01c17142008-08-26 23:35:04 -07002306 /* short-circuit this scan */
David Brownellaa16ca32007-12-30 23:45:19 -08002307 now_uframe = clock;
2308 break;
2309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
2311
David Brownell79592b722008-01-07 00:47:42 -08002312 /* If we can tell we caught up to the hardware, stop now.
2313 * We can't advance our scan without collecting the ISO
2314 * transfers that are still pending in this frame.
2315 */
2316 if (incomplete && HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
2317 ehci->next_uframe = now_uframe;
2318 break;
2319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 // FIXME: this assumes we won't get lapped when
2322 // latencies climb; that should be rare, but...
2323 // detect it, and just go all the way around.
2324 // FLR might help detect this case, so long as latencies
2325 // don't exceed periodic_size msec (default 1.024 sec).
2326
2327 // FIXME: likewise assumes HC doesn't halt mid-scan
2328
2329 if (now_uframe == clock) {
2330 unsigned now;
2331
David Brownellaa16ca32007-12-30 23:45:19 -08002332 if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state)
2333 || ehci->periodic_sched == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 break;
2335 ehci->next_uframe = now_uframe;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11002336 now = ehci_readl(ehci, &ehci->regs->frame_index) % mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (now_uframe == now)
2338 break;
2339
2340 /* rescan the rest of this frame, then ... */
2341 clock = now;
Alan Sternb40e43f2008-05-20 16:59:10 -04002342 clock_frame = clock >> 3;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002343 if (ehci->clock_frame != clock_frame) {
2344 free_cached_itd_list(ehci);
2345 ehci->clock_frame = clock_frame;
2346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 } else {
2348 now_uframe++;
2349 now_uframe %= mod;
2350 }
David Brownell53bd6a62006-08-30 14:50:06 -07002351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}