74bdd5bfcc25c6c6a0dfed8261a371b052023673
[linux-2.6.git] / drivers / usb / host / ehci-sched.c
1 /*
2  * Copyright (c) 2001-2003 by David Brownell
3  * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers
4  * 
5  * 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
37 static 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  */
46 static union ehci_shadow *
47 periodic_next_shadow (union ehci_shadow *periodic, __le32 tag)
48 {
49         switch (tag) {
50         case Q_TYPE_QH:
51                 return &periodic->qh->qh_next;
52         case Q_TYPE_FSTN:
53                 return &periodic->fstn->fstn_next;
54         case Q_TYPE_ITD:
55                 return &periodic->itd->itd_next;
56         // case Q_TYPE_SITD:
57         default:
58                 return &periodic->sitd->sitd_next;
59         }
60 }
61
62 /* returns true after successful unlink */
63 /* caller must hold ehci->lock */
64 static int periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
65 {
66         union ehci_shadow       *prev_p = &ehci->pshadow [frame];
67         __le32                  *hw_p = &ehci->periodic [frame];
68         union ehci_shadow       here = *prev_p;
69         union ehci_shadow       *next_p;
70
71         /* find predecessor of "ptr"; hw and shadow lists are in sync */
72         while (here.ptr && here.ptr != ptr) {
73                 prev_p = periodic_next_shadow (prev_p, Q_NEXT_TYPE (*hw_p));
74                 hw_p = &here.qh->hw_next;
75                 here = *prev_p;
76         }
77         /* an interrupt entry (at list end) could have been shared */
78         if (!here.ptr) {
79                 dbg ("entry %p no longer on frame [%d]", ptr, frame);
80                 return 0;
81         }
82         // vdbg ("periodic unlink %p from frame %d", ptr, frame);
83
84         /* update hardware list ... HC may still know the old structure, so
85          * don't change hw_next until it'll have purged its cache
86          */
87         next_p = periodic_next_shadow (&here, Q_NEXT_TYPE (*hw_p));
88         *hw_p = here.qh->hw_next;
89
90         /* unlink from shadow list; HCD won't see old structure again */
91         *prev_p = *next_p;
92         next_p->ptr = NULL;
93
94         return 1;
95 }
96
97 /* how many of the uframe's 125 usecs are allocated? */
98 static unsigned short
99 periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
100 {
101         __le32                  *hw_p = &ehci->periodic [frame];
102         union ehci_shadow       *q = &ehci->pshadow [frame];
103         unsigned                usecs = 0;
104
105         while (q->ptr) {
106                 switch (Q_NEXT_TYPE (*hw_p)) {
107                 case Q_TYPE_QH:
108                         /* is it in the S-mask? */
109                         if (q->qh->hw_info2 & cpu_to_le32 (1 << uframe))
110                                 usecs += q->qh->usecs;
111                         /* ... or C-mask? */
112                         if (q->qh->hw_info2 & cpu_to_le32 (1 << (8 + uframe)))
113                                 usecs += q->qh->c_usecs;
114                         hw_p = &q->qh->hw_next;
115                         q = &q->qh->qh_next;
116                         break;
117                 case Q_TYPE_FSTN:
118                         /* for "save place" FSTNs, count the relevant INTR
119                          * bandwidth from the previous frame
120                          */
121                         if (q->fstn->hw_prev != EHCI_LIST_END) {
122                                 ehci_dbg (ehci, "ignoring FSTN cost ...\n");
123                         }
124                         hw_p = &q->fstn->hw_next;
125                         q = &q->fstn->fstn_next;
126                         break;
127                 case Q_TYPE_ITD:
128                         usecs += q->itd->usecs [uframe];
129                         hw_p = &q->itd->hw_next;
130                         q = &q->itd->itd_next;
131                         break;
132                 case Q_TYPE_SITD:
133                         /* is it in the S-mask?  (count SPLIT, DATA) */
134                         if (q->sitd->hw_uframe & cpu_to_le32 (1 << uframe)) {
135                                 if (q->sitd->hw_fullspeed_ep &
136                                                 __constant_cpu_to_le32 (1<<31))
137                                         usecs += q->sitd->stream->usecs;
138                                 else    /* worst case for OUT start-split */
139                                         usecs += HS_USECS_ISO (188);
140                         }
141
142                         /* ... C-mask?  (count CSPLIT, DATA) */
143                         if (q->sitd->hw_uframe &
144                                         cpu_to_le32 (1 << (8 + uframe))) {
145                                 /* worst case for IN complete-split */
146                                 usecs += q->sitd->stream->c_usecs;
147                         }
148
149                         hw_p = &q->sitd->hw_next;
150                         q = &q->sitd->sitd_next;
151                         break;
152                 default:
153                         BUG ();
154                 }
155         }
156 #ifdef  DEBUG
157         if (usecs > 100)
158                 err ("overallocated uframe %d, periodic is %d usecs",
159                         frame * 8 + uframe, usecs);
160 #endif
161         return usecs;
162 }
163
164 /*-------------------------------------------------------------------------*/
165
166 static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
167 {
168         if (!dev1->tt || !dev2->tt)
169                 return 0;
170         if (dev1->tt != dev2->tt)
171                 return 0;
172         if (dev1->tt->multi)
173                 return dev1->ttport == dev2->ttport;
174         else
175                 return 1;
176 }
177
178 /* return true iff the device's transaction translator is available
179  * for a periodic transfer starting at the specified frame, using
180  * all the uframes in the mask.
181  */
182 static int tt_no_collision (
183         struct ehci_hcd         *ehci,
184         unsigned                period,
185         struct usb_device       *dev,
186         unsigned                frame,
187         u32                     uf_mask
188 )
189 {
190         if (period == 0)        /* error */
191                 return 0;
192
193         /* note bandwidth wastage:  split never follows csplit
194          * (different dev or endpoint) until the next uframe.
195          * calling convention doesn't make that distinction.
196          */
197         for (; frame < ehci->periodic_size; frame += period) {
198                 union ehci_shadow       here;
199                 __le32                  type;
200
201                 here = ehci->pshadow [frame];
202                 type = Q_NEXT_TYPE (ehci->periodic [frame]);
203                 while (here.ptr) {
204                         switch (type) {
205                         case Q_TYPE_ITD:
206                                 type = Q_NEXT_TYPE (here.itd->hw_next);
207                                 here = here.itd->itd_next;
208                                 continue;
209                         case Q_TYPE_QH:
210                                 if (same_tt (dev, here.qh->dev)) {
211                                         u32             mask;
212
213                                         mask = le32_to_cpu (here.qh->hw_info2);
214                                         /* "knows" no gap is needed */
215                                         mask |= mask >> 8;
216                                         if (mask & uf_mask)
217                                                 break;
218                                 }
219                                 type = Q_NEXT_TYPE (here.qh->hw_next);
220                                 here = here.qh->qh_next;
221                                 continue;
222                         case Q_TYPE_SITD:
223                                 if (same_tt (dev, here.itd->urb->dev)) {
224                                         u16             mask;
225
226                                         mask = le32_to_cpu (here.sitd
227                                                                 ->hw_uframe);
228                                         /* FIXME assumes no gap for IN! */
229                                         mask |= mask >> 8;
230                                         if (mask & uf_mask)
231                                                 break;
232                                 }
233                                 type = Q_NEXT_TYPE (here.qh->hw_next);
234                                 here = here.sitd->sitd_next;
235                                 continue;
236                         // case Q_TYPE_FSTN:
237                         default:
238                                 ehci_dbg (ehci,
239                                         "periodic frame %d bogus type %d\n",
240                                         frame, type);
241                         }
242
243                         /* collision or error */
244                         return 0;
245                 }
246         }
247
248         /* no collision */
249         return 1;
250 }
251
252 /*-------------------------------------------------------------------------*/
253
254 static int enable_periodic (struct ehci_hcd *ehci)
255 {
256         u32     cmd;
257         int     status;
258
259         /* did clearing PSE did take effect yet?
260          * takes effect only at frame boundaries...
261          */
262         status = handshake (&ehci->regs->status, STS_PSS, 0, 9 * 125);
263         if (status != 0) {
264                 ehci->hcd.state = USB_STATE_HALT;
265                 return status;
266         }
267
268         cmd = readl (&ehci->regs->command) | CMD_PSE;
269         writel (cmd, &ehci->regs->command);
270         /* posted write ... PSS happens later */
271         ehci->hcd.state = USB_STATE_RUNNING;
272
273         /* make sure ehci_work scans these */
274         ehci->next_uframe = readl (&ehci->regs->frame_index)
275                                 % (ehci->periodic_size << 3);
276         return 0;
277 }
278
279 static int disable_periodic (struct ehci_hcd *ehci)
280 {
281         u32     cmd;
282         int     status;
283
284         /* did setting PSE not take effect yet?
285          * takes effect only at frame boundaries...
286          */
287         status = handshake (&ehci->regs->status, STS_PSS, STS_PSS, 9 * 125);
288         if (status != 0) {
289                 ehci->hcd.state = USB_STATE_HALT;
290                 return status;
291         }
292
293         cmd = readl (&ehci->regs->command) & ~CMD_PSE;
294         writel (cmd, &ehci->regs->command);
295         /* posted write ... */
296
297         ehci->next_uframe = -1;
298         return 0;
299 }
300
301 /*-------------------------------------------------------------------------*/
302
303 // FIXME microframe periods not yet handled
304
305 static void intr_deschedule (
306         struct ehci_hcd *ehci,
307         struct ehci_qh  *qh,
308         int             wait
309 ) {
310         int             status;
311         unsigned        frame = qh->start;
312
313         do {
314                 periodic_unlink (ehci, frame, qh);
315                 qh_put (qh);
316                 frame += qh->period;
317         } while (frame < ehci->periodic_size);
318
319         qh->qh_state = QH_STATE_UNLINK;
320         qh->qh_next.ptr = NULL;
321         ehci->periodic_sched--;
322
323         /* maybe turn off periodic schedule */
324         if (!ehci->periodic_sched)
325                 status = disable_periodic (ehci);
326         else {
327                 status = 0;
328                 ehci_vdbg (ehci, "periodic schedule still enabled\n");
329         }
330
331         /*
332          * If the hc may be looking at this qh, then delay a uframe
333          * (yeech!) to be sure it's done.
334          * No other threads may be mucking with this qh.
335          */
336         if (((ehci_get_frame (&ehci->hcd) - frame) % qh->period) == 0) {
337                 if (wait) {
338                         udelay (125);
339                         qh->hw_next = EHCI_LIST_END;
340                 } else {
341                         /* we may not be IDLE yet, but if the qh is empty
342                          * the race is very short.  then if qh also isn't
343                          * rescheduled soon, it won't matter.  otherwise...
344                          */
345                         ehci_vdbg (ehci, "intr_deschedule...\n");
346                 }
347         } else
348                 qh->hw_next = EHCI_LIST_END;
349
350         qh->qh_state = QH_STATE_IDLE;
351
352         /* update per-qh bandwidth utilization (for usbfs) */
353         hcd_to_bus (&ehci->hcd)->bandwidth_allocated -= 
354                 (qh->usecs + qh->c_usecs) / qh->period;
355
356         ehci_dbg (ehci, "descheduled qh%d/%p frame=%d count=%d, urbs=%d\n",
357                 qh->period, qh, frame,
358                 atomic_read (&qh->kref.refcount), ehci->periodic_sched);
359 }
360
361 static int check_period (
362         struct ehci_hcd *ehci, 
363         unsigned        frame,
364         unsigned        uframe,
365         unsigned        period,
366         unsigned        usecs
367 ) {
368         /* complete split running into next frame?
369          * given FSTN support, we could sometimes check...
370          */
371         if (uframe >= 8)
372                 return 0;
373
374         /*
375          * 80% periodic == 100 usec/uframe available
376          * convert "usecs we need" to "max already claimed" 
377          */
378         usecs = 100 - usecs;
379
380         do {
381                 int     claimed;
382
383 // FIXME delete when intr_submit handles non-empty queues
384 // this gives us a one intr/frame limit (vs N/uframe)
385 // ... and also lets us avoid tracking split transactions
386 // that might collide at a given TT/hub.
387                 if (ehci->pshadow [frame].ptr)
388                         return 0;
389
390                 claimed = periodic_usecs (ehci, frame, uframe);
391                 if (claimed > usecs)
392                         return 0;
393
394 // FIXME update to handle sub-frame periods
395         } while ((frame += period) < ehci->periodic_size);
396
397         // success!
398         return 1;
399 }
400
401 static int check_intr_schedule (
402         struct ehci_hcd         *ehci, 
403         unsigned                frame,
404         unsigned                uframe,
405         const struct ehci_qh    *qh,
406         __le32                  *c_maskp
407 )
408 {
409         int             retval = -ENOSPC;
410
411         if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
412                 goto done;
413         if (!qh->c_usecs) {
414                 retval = 0;
415                 *c_maskp = 0;
416                 goto done;
417         }
418
419         /* This is a split transaction; check the bandwidth available for
420          * the completion too.  Check both worst and best case gaps: worst
421          * case is SPLIT near uframe end, and CSPLIT near start ... best is
422          * vice versa.  Difference can be almost two uframe times, but we
423          * reserve unnecessary bandwidth (waste it) this way.  (Actually
424          * even better cases exist, like immediate device NAK.)
425          *
426          * FIXME don't even bother unless we know this TT is idle in that
427          * range of uframes ... for now, check_period() allows only one
428          * interrupt transfer per frame, so needn't check "TT busy" status
429          * when scheduling a split (QH, SITD, or FSTN).
430          *
431          * FIXME ehci 0.96 and above can use FSTNs
432          */
433         if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
434                                 qh->period, qh->c_usecs))
435                 goto done;
436         if (!check_period (ehci, frame, uframe + qh->gap_uf,
437                                 qh->period, qh->c_usecs))
438                 goto done;
439
440         *c_maskp = cpu_to_le32 (0x03 << (8 + uframe + qh->gap_uf));
441         retval = 0;
442 done:
443         return retval;
444 }
445
446 static int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
447 {
448         int             status;
449         unsigned        uframe;
450         __le32          c_mask;
451         unsigned        frame;          /* 0..(qh->period - 1), or NO_FRAME */
452
453         qh_refresh(ehci, qh);
454         qh->hw_next = EHCI_LIST_END;
455         frame = qh->start;
456
457         /* reuse the previous schedule slots, if we can */
458         if (frame < qh->period) {
459                 uframe = ffs (le32_to_cpup (&qh->hw_info2) & 0x00ff);
460                 status = check_intr_schedule (ehci, frame, --uframe,
461                                 qh, &c_mask);
462         } else {
463                 uframe = 0;
464                 c_mask = 0;
465                 status = -ENOSPC;
466         }
467
468         /* else scan the schedule to find a group of slots such that all
469          * uframes have enough periodic bandwidth available.
470          */
471         if (status) {
472                 frame = qh->period - 1;
473                 do {
474                         for (uframe = 0; uframe < 8; uframe++) {
475                                 status = check_intr_schedule (ehci,
476                                                 frame, uframe, qh,
477                                                 &c_mask);
478                                 if (status == 0)
479                                         break;
480                         }
481                 } while (status && frame--);
482                 if (status)
483                         goto done;
484                 qh->start = frame;
485
486                 /* reset S-frame and (maybe) C-frame masks */
487                 qh->hw_info2 &= ~__constant_cpu_to_le32(0xffff);
488                 qh->hw_info2 |= cpu_to_le32 (1 << uframe) | c_mask;
489         } else
490                 ehci_dbg (ehci, "reused qh %p schedule\n", qh);
491
492         /* stuff into the periodic schedule */
493         qh->qh_state = QH_STATE_LINKED;
494         ehci_dbg(ehci,
495                 "scheduled qh%d/%p usecs %d/%d starting %d.%d (gap %d)\n",
496                 qh->period, qh, qh->usecs, qh->c_usecs,
497                 frame, uframe, qh->gap_uf);
498         do {
499                 if (unlikely (ehci->pshadow [frame].ptr != 0)) {
500
501 // FIXME -- just link toward the end, before any qh with a shorter period,
502 // AND accommodate it already having been linked here (after some other qh)
503 // AS WELL AS updating the schedule checking logic
504
505                         BUG ();
506                 } else {
507                         ehci->pshadow [frame].qh = qh_get (qh);
508                         ehci->periodic [frame] =
509                                 QH_NEXT (qh->qh_dma);
510                 }
511                 wmb ();
512                 frame += qh->period;
513         } while (frame < ehci->periodic_size);
514
515         /* update per-qh bandwidth for usbfs */
516         hcd_to_bus (&ehci->hcd)->bandwidth_allocated += 
517                 (qh->usecs + qh->c_usecs) / qh->period;
518
519         /* maybe enable periodic schedule processing */
520         if (!ehci->periodic_sched++)
521                 status = enable_periodic (ehci);
522 done:
523         return status;
524 }
525
526 static int intr_submit (
527         struct ehci_hcd         *ehci,
528         struct urb              *urb,
529         struct list_head        *qtd_list,
530         int                     mem_flags
531 ) {
532         unsigned                epnum;
533         unsigned long           flags;
534         struct ehci_qh          *qh;
535         struct hcd_dev          *dev;
536         int                     is_input;
537         int                     status = 0;
538         struct list_head        empty;
539
540         /* get endpoint and transfer/schedule data */
541         epnum = usb_pipeendpoint (urb->pipe);
542         is_input = usb_pipein (urb->pipe);
543         if (is_input)
544                 epnum |= 0x10;
545
546         spin_lock_irqsave (&ehci->lock, flags);
547         dev = (struct hcd_dev *)urb->dev->hcpriv;
548
549         /* get qh and force any scheduling errors */
550         INIT_LIST_HEAD (&empty);
551         qh = qh_append_tds (ehci, urb, &empty, epnum, &dev->ep [epnum]);
552         if (qh == 0) {
553                 status = -ENOMEM;
554                 goto done;
555         }
556         if (qh->qh_state == QH_STATE_IDLE) {
557                 if ((status = qh_schedule (ehci, qh)) != 0)
558                         goto done;
559         }
560
561         /* then queue the urb's tds to the qh */
562         qh = qh_append_tds (ehci, urb, qtd_list, epnum, &dev->ep [epnum]);
563         BUG_ON (qh == 0);
564
565         /* ... update usbfs periodic stats */
566         hcd_to_bus (&ehci->hcd)->bandwidth_int_reqs++;
567
568 done:
569         spin_unlock_irqrestore (&ehci->lock, flags);
570         if (status)
571                 qtd_list_free (ehci, urb, qtd_list);
572
573         return status;
574 }
575
576 /*-------------------------------------------------------------------------*/
577
578 /* ehci_iso_stream ops work with both ITD and SITD */
579
580 static struct ehci_iso_stream *
581 iso_stream_alloc (int mem_flags)
582 {
583         struct ehci_iso_stream *stream;
584
585         stream = kmalloc(sizeof *stream, mem_flags);
586         if (likely (stream != 0)) {
587                 memset (stream, 0, sizeof(*stream));
588                 INIT_LIST_HEAD(&stream->td_list);
589                 INIT_LIST_HEAD(&stream->free_list);
590                 stream->next_uframe = -1;
591                 stream->refcount = 1;
592         }
593         return stream;
594 }
595
596 static void
597 iso_stream_init (
598         struct ehci_iso_stream  *stream,
599         struct usb_device       *dev,
600         int                     pipe,
601         unsigned                interval
602 )
603 {
604         static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
605
606         u32                     buf1;
607         unsigned                epnum, maxp;
608         int                     is_input;
609         long                    bandwidth;
610
611         /*
612          * this might be a "high bandwidth" highspeed endpoint,
613          * as encoded in the ep descriptor's wMaxPacket field
614          */
615         epnum = usb_pipeendpoint (pipe);
616         is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
617         if (is_input) {
618                 maxp = dev->epmaxpacketin [epnum];
619                 buf1 = (1 << 11);
620         } else {
621                 maxp = dev->epmaxpacketout [epnum];
622                 buf1 = 0;
623         }
624
625         /* knows about ITD vs SITD */
626         if (dev->speed == USB_SPEED_HIGH) {
627                 unsigned multi = hb_mult(maxp);
628
629                 stream->highspeed = 1;
630
631                 maxp = max_packet(maxp);
632                 buf1 |= maxp;
633                 maxp *= multi;
634
635                 stream->buf0 = cpu_to_le32 ((epnum << 8) | dev->devnum);
636                 stream->buf1 = cpu_to_le32 (buf1);
637                 stream->buf2 = cpu_to_le32 (multi);
638
639                 /* usbfs wants to report the average usecs per frame tied up
640                  * when transfers on this endpoint are scheduled ...
641                  */
642                 stream->usecs = HS_USECS_ISO (maxp);
643                 bandwidth = stream->usecs * 8;
644                 bandwidth /= 1 << (interval - 1);
645
646         } else {
647                 u32             addr;
648
649                 addr = dev->ttport << 24;
650                 addr |= dev->tt->hub->devnum << 16;
651                 addr |= epnum << 8;
652                 addr |= dev->devnum;
653                 stream->usecs = HS_USECS_ISO (maxp);
654                 if (is_input) {
655                         u32     tmp;
656
657                         addr |= 1 << 31;
658                         stream->c_usecs = stream->usecs;
659                         stream->usecs = HS_USECS_ISO (1);
660                         stream->raw_mask = 1;
661
662                         /* pessimistic c-mask */
663                         tmp = usb_calc_bus_time (USB_SPEED_FULL, 1, 0, maxp)
664                                         / (125 * 1000);
665                         stream->raw_mask |= 3 << (tmp + 9);
666                 } else
667                         stream->raw_mask = smask_out [maxp / 188];
668                 bandwidth = stream->usecs + stream->c_usecs;
669                 bandwidth /= 1 << (interval + 2);
670
671                 /* stream->splits gets created from raw_mask later */
672                 stream->address = cpu_to_le32 (addr);
673         }
674         stream->bandwidth = bandwidth;
675
676         stream->udev = dev;
677
678         stream->bEndpointAddress = is_input | epnum;
679         stream->interval = interval;
680         stream->maxp = maxp;
681 }
682
683 static void
684 iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
685 {
686         stream->refcount--;
687
688         /* free whenever just a dev->ep reference remains.
689          * not like a QH -- no persistent state (toggle, halt)
690          */
691         if (stream->refcount == 1) {
692                 int             is_in;
693                 struct hcd_dev  *dev = stream->udev->hcpriv;
694
695                 // BUG_ON (!list_empty(&stream->td_list));
696
697                 while (!list_empty (&stream->free_list)) {
698                         struct list_head        *entry;
699
700                         entry = stream->free_list.next;
701                         list_del (entry);
702
703                         /* knows about ITD vs SITD */
704                         if (stream->highspeed) {
705                                 struct ehci_itd         *itd;
706
707                                 itd = list_entry (entry, struct ehci_itd,
708                                                 itd_list);
709                                 dma_pool_free (ehci->itd_pool, itd,
710                                                 itd->itd_dma);
711                         } else {
712                                 struct ehci_sitd        *sitd;
713
714                                 sitd = list_entry (entry, struct ehci_sitd,
715                                                 sitd_list);
716                                 dma_pool_free (ehci->sitd_pool, sitd,
717                                                 sitd->sitd_dma);
718                         }
719                 }
720
721                 is_in = (stream->bEndpointAddress & USB_DIR_IN) ? 0x10 : 0;
722                 stream->bEndpointAddress &= 0x0f;
723                 dev->ep[is_in + stream->bEndpointAddress] = NULL;
724
725                 if (stream->rescheduled) {
726                         ehci_info (ehci, "ep%d%s-iso rescheduled "
727                                 "%lu times in %lu seconds\n",
728                                 stream->bEndpointAddress, is_in ? "in" : "out",
729                                 stream->rescheduled,
730                                 ((jiffies - stream->start)/HZ)
731                                 );
732                 }
733
734                 kfree(stream);
735         }
736 }
737
738 static inline struct ehci_iso_stream *
739 iso_stream_get (struct ehci_iso_stream *stream)
740 {
741         if (likely (stream != 0))
742                 stream->refcount++;
743         return stream;
744 }
745
746 static struct ehci_iso_stream *
747 iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
748 {
749         unsigned                epnum;
750         struct hcd_dev          *dev;
751         struct ehci_iso_stream  *stream;
752         unsigned long           flags;
753
754         epnum = usb_pipeendpoint (urb->pipe);
755         if (usb_pipein(urb->pipe))
756                 epnum += 0x10;
757
758         spin_lock_irqsave (&ehci->lock, flags);
759
760         dev = (struct hcd_dev *)urb->dev->hcpriv;
761         stream = dev->ep [epnum];
762
763         if (unlikely (stream == 0)) {
764                 stream = iso_stream_alloc(GFP_ATOMIC);
765                 if (likely (stream != 0)) {
766                         /* dev->ep owns the initial refcount */
767                         dev->ep[epnum] = stream;
768                         iso_stream_init(stream, urb->dev, urb->pipe,
769                                         urb->interval);
770                 }
771
772         /* if dev->ep [epnum] is a QH, info1.maxpacket is nonzero */
773         } else if (unlikely (stream->hw_info1 != 0)) {
774                 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
775                         urb->dev->devpath, epnum & 0x0f,
776                         (epnum & 0x10) ? "in" : "out");
777                 stream = NULL;
778         }
779
780         /* caller guarantees an eventual matching iso_stream_put */
781         stream = iso_stream_get (stream);
782
783         spin_unlock_irqrestore (&ehci->lock, flags);
784         return stream;
785 }
786
787 /*-------------------------------------------------------------------------*/
788
789 /* ehci_iso_sched ops can be shared, ITD-only, or SITD-only */
790
791 static struct ehci_iso_sched *
792 iso_sched_alloc (unsigned packets, int mem_flags)
793 {
794         struct ehci_iso_sched   *iso_sched;
795         int                     size = sizeof *iso_sched;
796
797         size += packets * sizeof (struct ehci_iso_packet);
798         iso_sched = kmalloc (size, mem_flags);
799         if (likely (iso_sched != 0)) {
800                 memset(iso_sched, 0, size);
801                 INIT_LIST_HEAD (&iso_sched->td_list);
802         }
803         return iso_sched;
804 }
805
806 static inline void
807 itd_sched_init (
808         struct ehci_iso_sched   *iso_sched,
809         struct ehci_iso_stream  *stream,
810         struct urb              *urb
811 )
812 {
813         unsigned        i;
814         dma_addr_t      dma = urb->transfer_dma;
815
816         /* how many uframes are needed for these transfers */
817         iso_sched->span = urb->number_of_packets * stream->interval;
818
819         /* figure out per-uframe itd fields that we'll need later
820          * when we fit new itds into the schedule.
821          */
822         for (i = 0; i < urb->number_of_packets; i++) {
823                 struct ehci_iso_packet  *uframe = &iso_sched->packet [i];
824                 unsigned                length;
825                 dma_addr_t              buf;
826                 u32                     trans;
827
828                 length = urb->iso_frame_desc [i].length;
829                 buf = dma + urb->iso_frame_desc [i].offset;
830
831                 trans = EHCI_ISOC_ACTIVE;
832                 trans |= buf & 0x0fff;
833                 if (unlikely (((i + 1) == urb->number_of_packets))
834                                 && !(urb->transfer_flags & URB_NO_INTERRUPT))
835                         trans |= EHCI_ITD_IOC;
836                 trans |= length << 16;
837                 uframe->transaction = cpu_to_le32 (trans);
838
839                 /* might need to cross a buffer page within a td */
840                 uframe->bufp = (buf & ~(u64)0x0fff);
841                 buf += length;
842                 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
843                         uframe->cross = 1;
844         }
845 }
846
847 static void
848 iso_sched_free (
849         struct ehci_iso_stream  *stream,
850         struct ehci_iso_sched   *iso_sched
851 )
852 {
853         if (!iso_sched)
854                 return;
855         // caller must hold ehci->lock!
856         list_splice (&iso_sched->td_list, &stream->free_list);
857         kfree (iso_sched);
858 }
859
860 static int
861 itd_urb_transaction (
862         struct ehci_iso_stream  *stream,
863         struct ehci_hcd         *ehci,
864         struct urb              *urb,
865         int                     mem_flags
866 )
867 {
868         struct ehci_itd         *itd;
869         dma_addr_t              itd_dma;
870         int                     i;
871         unsigned                num_itds;
872         struct ehci_iso_sched   *sched;
873         unsigned long           flags;
874
875         sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
876         if (unlikely (sched == 0))
877                 return -ENOMEM;
878
879         itd_sched_init (sched, stream, urb);
880
881         if (urb->interval < 8)
882                 num_itds = 1 + (sched->span + 7) / 8;
883         else
884                 num_itds = urb->number_of_packets;
885
886         /* allocate/init ITDs */
887         spin_lock_irqsave (&ehci->lock, flags);
888         for (i = 0; i < num_itds; i++) {
889
890                 /* free_list.next might be cache-hot ... but maybe
891                  * the HC caches it too. avoid that issue for now.
892                  */
893
894                 /* prefer previously-allocated itds */
895                 if (likely (!list_empty(&stream->free_list))) {
896                         itd = list_entry (stream->free_list.prev,
897                                          struct ehci_itd, itd_list);
898                         list_del (&itd->itd_list);
899                         itd_dma = itd->itd_dma;
900                 } else
901                         itd = NULL;
902
903                 if (!itd) {
904                         spin_unlock_irqrestore (&ehci->lock, flags);
905                         itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
906                                         &itd_dma);
907                         spin_lock_irqsave (&ehci->lock, flags);
908                 }
909
910                 if (unlikely (0 == itd)) {
911                         iso_sched_free (stream, sched);
912                         spin_unlock_irqrestore (&ehci->lock, flags);
913                         return -ENOMEM;
914                 }
915                 memset (itd, 0, sizeof *itd);
916                 itd->itd_dma = itd_dma;
917                 list_add (&itd->itd_list, &sched->td_list);
918         }
919         spin_unlock_irqrestore (&ehci->lock, flags);
920
921         /* temporarily store schedule info in hcpriv */
922         urb->hcpriv = sched;
923         urb->error_count = 0;
924         return 0;
925 }
926
927 /*-------------------------------------------------------------------------*/
928
929 static inline int
930 itd_slot_ok (
931         struct ehci_hcd         *ehci,
932         u32                     mod,
933         u32                     uframe,
934         u8                      usecs,
935         u32                     period
936 )
937 {
938         uframe %= period;
939         do {
940                 /* can't commit more than 80% periodic == 100 usec */
941                 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
942                                 > (100 - usecs))
943                         return 0;
944
945                 /* we know urb->interval is 2^N uframes */
946                 uframe += period;
947         } while (uframe < mod);
948         return 1;
949 }
950
951 static inline int
952 sitd_slot_ok (
953         struct ehci_hcd         *ehci,
954         u32                     mod,
955         struct ehci_iso_stream  *stream,
956         u32                     uframe,
957         struct ehci_iso_sched   *sched,
958         u32                     period_uframes
959 )
960 {
961         u32                     mask, tmp;
962         u32                     frame, uf;
963
964         mask = stream->raw_mask << (uframe & 7);
965
966         /* for IN, don't wrap CSPLIT into the next frame */
967         if (mask & ~0xffff)
968                 return 0;
969
970         /* this multi-pass logic is simple, but performance may
971          * suffer when the schedule data isn't cached.
972          */
973
974         /* check bandwidth */
975         uframe %= period_uframes;
976         do {
977                 u32             max_used;
978
979                 frame = uframe >> 3;
980                 uf = uframe & 7;
981
982                 /* tt must be idle for start(s), any gap, and csplit.
983                  * assume scheduling slop leaves 10+% for control/bulk.
984                  */
985                 if (!tt_no_collision (ehci, period_uframes << 3,
986                                 stream->udev, frame, mask))
987                         return 0;
988
989                 /* check starts (OUT uses more than one) */
990                 max_used = 100 - stream->usecs;
991                 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
992                         if (periodic_usecs (ehci, frame, uf) > max_used)
993                                 return 0;
994                 }
995
996                 /* for IN, check CSPLIT */
997                 if (stream->c_usecs) {
998                         max_used = 100 - stream->c_usecs;
999                         do {
1000                                 tmp = 1 << uf;
1001                                 tmp <<= 8;
1002                                 if ((stream->raw_mask & tmp) == 0)
1003                                         continue;
1004                                 if (periodic_usecs (ehci, frame, uf)
1005                                                 > max_used)
1006                                         return 0;
1007                         } while (++uf < 8);
1008                 }
1009
1010                 /* we know urb->interval is 2^N uframes */
1011                 uframe += period_uframes;
1012         } while (uframe < mod);
1013
1014         stream->splits = cpu_to_le32(stream->raw_mask << (uframe & 7));
1015         return 1;
1016 }
1017
1018 /*
1019  * This scheduler plans almost as far into the future as it has actual
1020  * periodic schedule slots.  (Affected by TUNE_FLS, which defaults to
1021  * "as small as possible" to be cache-friendlier.)  That limits the size
1022  * transfers you can stream reliably; avoid more than 64 msec per urb.
1023  * Also avoid queue depths of less than ehci's worst irq latency (affected
1024  * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1025  * and other factors); or more than about 230 msec total (for portability,
1026  * given EHCI_TUNE_FLS and the slop).  Or, write a smarter scheduler!
1027  */
1028
1029 #define SCHEDULE_SLOP   10      /* frames */
1030
1031 static int
1032 iso_stream_schedule (
1033         struct ehci_hcd         *ehci,
1034         struct urb              *urb,
1035         struct ehci_iso_stream  *stream
1036 )
1037 {
1038         u32                     now, start, max, period;
1039         int                     status;
1040         unsigned                mod = ehci->periodic_size << 3;
1041         struct ehci_iso_sched   *sched = urb->hcpriv;
1042
1043         if (sched->span > (mod - 8 * SCHEDULE_SLOP)) {
1044                 ehci_dbg (ehci, "iso request %p too long\n", urb);
1045                 status = -EFBIG;
1046                 goto fail;
1047         }
1048
1049         if ((stream->depth + sched->span) > mod) {
1050                 ehci_dbg (ehci, "request %p would overflow (%d+%d>%d)\n",
1051                         urb, stream->depth, sched->span, mod);
1052                 status = -EFBIG;
1053                 goto fail;
1054         }
1055
1056         now = readl (&ehci->regs->frame_index) % mod;
1057
1058         /* when's the last uframe this urb could start? */
1059         max = now + mod;
1060
1061         /* typical case: reuse current schedule. stream is still active,
1062          * and no gaps from host falling behind (irq delays etc)
1063          */
1064         if (likely (!list_empty (&stream->td_list))) {
1065                 start = stream->next_uframe;
1066                 if (start < now)
1067                         start += mod;
1068                 if (likely ((start + sched->span) < max))
1069                         goto ready;
1070                 /* else fell behind; someday, try to reschedule */
1071                 status = -EL2NSYNC;
1072                 goto fail;
1073         }
1074
1075         /* need to schedule; when's the next (u)frame we could start?
1076          * this is bigger than ehci->i_thresh allows; scheduling itself
1077          * isn't free, the slop should handle reasonably slow cpus.  it
1078          * can also help high bandwidth if the dma and irq loads don't
1079          * jump until after the queue is primed.
1080          */
1081         start = SCHEDULE_SLOP * 8 + (now & ~0x07);
1082         start %= mod;
1083         stream->next_uframe = start;
1084
1085         /* NOTE:  assumes URB_ISO_ASAP, to limit complexity/bugs */
1086
1087         period = urb->interval;
1088         if (!stream->highspeed)
1089                 period <<= 3;
1090
1091         /* find a uframe slot with enough bandwidth */
1092         for (; start < (stream->next_uframe + period); start++) {
1093                 int             enough_space;
1094
1095                 /* check schedule: enough space? */
1096                 if (stream->highspeed)
1097                         enough_space = itd_slot_ok (ehci, mod, start,
1098                                         stream->usecs, period);
1099                 else {
1100                         if ((start % 8) >= 6)
1101                                 continue;
1102                         enough_space = sitd_slot_ok (ehci, mod, stream,
1103                                         start, sched, period);
1104                 }
1105
1106                 /* schedule it here if there's enough bandwidth */
1107                 if (enough_space) {
1108                         stream->next_uframe = start % mod;
1109                         goto ready;
1110                 }
1111         }
1112
1113         /* no room in the schedule */
1114         ehci_dbg (ehci, "iso %ssched full %p (now %d max %d)\n",
1115                 list_empty (&stream->td_list) ? "" : "re",
1116                 urb, now, max);
1117         status = -ENOSPC;
1118
1119 fail:
1120         iso_sched_free (stream, sched);
1121         urb->hcpriv = NULL;
1122         return status;
1123
1124 ready:
1125         urb->start_frame = stream->next_uframe;
1126         return 0;
1127 }
1128
1129 /*-------------------------------------------------------------------------*/
1130
1131 static inline void
1132 itd_init (struct ehci_iso_stream *stream, struct ehci_itd *itd)
1133 {
1134         int i;
1135
1136         itd->hw_next = EHCI_LIST_END;
1137         itd->hw_bufp [0] = stream->buf0;
1138         itd->hw_bufp [1] = stream->buf1;
1139         itd->hw_bufp [2] = stream->buf2;
1140
1141         for (i = 0; i < 8; i++)
1142                 itd->index[i] = -1;
1143
1144         /* All other fields are filled when scheduling */
1145 }
1146
1147 static inline void
1148 itd_patch (
1149         struct ehci_itd         *itd,
1150         struct ehci_iso_sched   *iso_sched,
1151         unsigned                index,
1152         u16                     uframe,
1153         int                     first
1154 )
1155 {
1156         struct ehci_iso_packet  *uf = &iso_sched->packet [index];
1157         unsigned                pg = itd->pg;
1158
1159         // BUG_ON (pg == 6 && uf->cross);
1160
1161         uframe &= 0x07;
1162         itd->index [uframe] = index;
1163
1164         itd->hw_transaction [uframe] = uf->transaction;
1165         itd->hw_transaction [uframe] |= cpu_to_le32 (pg << 12);
1166         itd->hw_bufp [pg] |= cpu_to_le32 (uf->bufp & ~(u32)0);
1167         itd->hw_bufp_hi [pg] |= cpu_to_le32 ((u32)(uf->bufp >> 32));
1168
1169         /* iso_frame_desc[].offset must be strictly increasing */
1170         if (unlikely (!first && uf->cross)) {
1171                 u64     bufp = uf->bufp + 4096;
1172                 itd->pg = ++pg;
1173                 itd->hw_bufp [pg] |= cpu_to_le32 (bufp & ~(u32)0);
1174                 itd->hw_bufp_hi [pg] |= cpu_to_le32 ((u32)(bufp >> 32));
1175         }
1176 }
1177
1178 static inline void
1179 itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1180 {
1181         /* always prepend ITD/SITD ... only QH tree is order-sensitive */
1182         itd->itd_next = ehci->pshadow [frame];
1183         itd->hw_next = ehci->periodic [frame];
1184         ehci->pshadow [frame].itd = itd;
1185         itd->frame = frame;
1186         wmb ();
1187         ehci->periodic [frame] = cpu_to_le32 (itd->itd_dma) | Q_TYPE_ITD;
1188 }
1189
1190 /* fit urb's itds into the selected schedule slot; activate as needed */
1191 static int
1192 itd_link_urb (
1193         struct ehci_hcd         *ehci,
1194         struct urb              *urb,
1195         unsigned                mod,
1196         struct ehci_iso_stream  *stream
1197 )
1198 {
1199         int                     packet, first = 1;
1200         unsigned                next_uframe, uframe, frame;
1201         struct ehci_iso_sched   *iso_sched = urb->hcpriv;
1202         struct ehci_itd         *itd;
1203
1204         next_uframe = stream->next_uframe % mod;
1205
1206         if (unlikely (list_empty(&stream->td_list))) {
1207                 hcd_to_bus (&ehci->hcd)->bandwidth_allocated
1208                                 += stream->bandwidth;
1209                 ehci_vdbg (ehci,
1210                         "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1211                         urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1212                         (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1213                         urb->interval,
1214                         next_uframe >> 3, next_uframe & 0x7);
1215                 stream->start = jiffies;
1216         }
1217         hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs++;
1218
1219         /* fill iTDs uframe by uframe */
1220         for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1221                 if (itd == NULL) {
1222                         /* ASSERT:  we have all necessary itds */
1223                         // BUG_ON (list_empty (&iso_sched->td_list));
1224
1225                         /* ASSERT:  no itds for this endpoint in this uframe */
1226
1227                         itd = list_entry (iso_sched->td_list.next,
1228                                         struct ehci_itd, itd_list);
1229                         list_move_tail (&itd->itd_list, &stream->td_list);
1230                         itd->stream = iso_stream_get (stream);
1231                         itd->urb = usb_get_urb (urb);
1232                         first = 1;
1233                         itd_init (stream, itd);
1234                 }
1235
1236                 uframe = next_uframe & 0x07;
1237                 frame = next_uframe >> 3;
1238
1239                 itd->usecs [uframe] = stream->usecs;
1240                 itd_patch (itd, iso_sched, packet, uframe, first);
1241                 first = 0;
1242
1243                 next_uframe += stream->interval;
1244                 stream->depth += stream->interval;
1245                 next_uframe %= mod;
1246                 packet++;
1247
1248                 /* link completed itds into the schedule */
1249                 if (((next_uframe >> 3) != frame)
1250                                 || packet == urb->number_of_packets) {
1251                         itd_link (ehci, frame % ehci->periodic_size, itd);
1252                         itd = NULL;
1253                 }
1254         }
1255         stream->next_uframe = next_uframe;
1256
1257         /* don't need that schedule data any more */
1258         iso_sched_free (stream, iso_sched);
1259         urb->hcpriv = NULL;
1260
1261         timer_action (ehci, TIMER_IO_WATCHDOG);
1262         if (unlikely (!ehci->periodic_sched++))
1263                 return enable_periodic (ehci);
1264         return 0;
1265 }
1266
1267 #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1268
1269 static unsigned
1270 itd_complete (
1271         struct ehci_hcd *ehci,
1272         struct ehci_itd *itd,
1273         struct pt_regs  *regs
1274 ) {
1275         struct urb                              *urb = itd->urb;
1276         struct usb_iso_packet_descriptor        *desc;
1277         u32                                     t;
1278         unsigned                                uframe;
1279         int                                     urb_index = -1;
1280         struct ehci_iso_stream                  *stream = itd->stream;
1281         struct usb_device                       *dev;
1282
1283         /* for each uframe with a packet */
1284         for (uframe = 0; uframe < 8; uframe++) {
1285                 if (likely (itd->index[uframe] == -1))
1286                         continue;
1287                 urb_index = itd->index[uframe];
1288                 desc = &urb->iso_frame_desc [urb_index];
1289
1290                 t = le32_to_cpup (&itd->hw_transaction [uframe]);
1291                 itd->hw_transaction [uframe] = 0;
1292                 stream->depth -= stream->interval;
1293
1294                 /* report transfer status */
1295                 if (unlikely (t & ISO_ERRS)) {
1296                         urb->error_count++;
1297                         if (t & EHCI_ISOC_BUF_ERR)
1298                                 desc->status = usb_pipein (urb->pipe)
1299                                         ? -ENOSR  /* hc couldn't read */
1300                                         : -ECOMM; /* hc couldn't write */
1301                         else if (t & EHCI_ISOC_BABBLE)
1302                                 desc->status = -EOVERFLOW;
1303                         else /* (t & EHCI_ISOC_XACTERR) */
1304                                 desc->status = -EPROTO;
1305
1306                         /* HC need not update length with this error */
1307                         if (!(t & EHCI_ISOC_BABBLE))
1308                                 desc->actual_length = EHCI_ITD_LENGTH (t);
1309                 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1310                         desc->status = 0;
1311                         desc->actual_length = EHCI_ITD_LENGTH (t);
1312                 }
1313         }
1314
1315         usb_put_urb (urb);
1316         itd->urb = NULL;
1317         itd->stream = NULL;
1318         list_move (&itd->itd_list, &stream->free_list);
1319         iso_stream_put (ehci, stream);
1320
1321         /* handle completion now? */
1322         if (likely ((urb_index + 1) != urb->number_of_packets))
1323                 return 0;
1324
1325         /* ASSERT: it's really the last itd for this urb
1326         list_for_each_entry (itd, &stream->td_list, itd_list)
1327                 BUG_ON (itd->urb == urb);
1328          */
1329
1330         /* give urb back to the driver ... can be out-of-order */
1331         dev = usb_get_dev (urb->dev);
1332         ehci_urb_done (ehci, urb, regs);
1333         urb = NULL;
1334
1335         /* defer stopping schedule; completion can submit */
1336         ehci->periodic_sched--;
1337         if (unlikely (!ehci->periodic_sched))
1338                 (void) disable_periodic (ehci);
1339         hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs--;
1340
1341         if (unlikely (list_empty (&stream->td_list))) {
1342                 hcd_to_bus (&ehci->hcd)->bandwidth_allocated
1343                                 -= stream->bandwidth;
1344                 ehci_vdbg (ehci,
1345                         "deschedule devp %s ep%d%s-iso\n",
1346                         dev->devpath, stream->bEndpointAddress & 0x0f,
1347                         (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1348         }
1349         iso_stream_put (ehci, stream);
1350         usb_put_dev (dev);
1351
1352         return 1;
1353 }
1354
1355 /*-------------------------------------------------------------------------*/
1356
1357 static int itd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
1358 {
1359         int                     status = -EINVAL;
1360         unsigned long           flags;
1361         struct ehci_iso_stream  *stream;
1362
1363         /* Get iso_stream head */
1364         stream = iso_stream_find (ehci, urb);
1365         if (unlikely (stream == 0)) {
1366                 ehci_dbg (ehci, "can't get iso stream\n");
1367                 return -ENOMEM;
1368         }
1369         if (unlikely (urb->interval != stream->interval)) {
1370                 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1371                         stream->interval, urb->interval);
1372                 goto done;
1373         }
1374
1375 #ifdef EHCI_URB_TRACE
1376         ehci_dbg (ehci,
1377                 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
1378                 __FUNCTION__, urb->dev->devpath, urb,
1379                 usb_pipeendpoint (urb->pipe),
1380                 usb_pipein (urb->pipe) ? "in" : "out",
1381                 urb->transfer_buffer_length,
1382                 urb->number_of_packets, urb->interval,
1383                 stream);
1384 #endif
1385
1386         /* allocate ITDs w/o locking anything */
1387         status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1388         if (unlikely (status < 0)) {
1389                 ehci_dbg (ehci, "can't init itds\n");
1390                 goto done;
1391         }
1392
1393         /* schedule ... need to lock */
1394         spin_lock_irqsave (&ehci->lock, flags);
1395         status = iso_stream_schedule (ehci, urb, stream);
1396         if (likely (status == 0))
1397                 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
1398         spin_unlock_irqrestore (&ehci->lock, flags);
1399
1400 done:
1401         if (unlikely (status < 0))
1402                 iso_stream_put (ehci, stream);
1403         return status;
1404 }
1405
1406 #ifdef CONFIG_USB_EHCI_SPLIT_ISO
1407
1408 /*-------------------------------------------------------------------------*/
1409
1410 /*
1411  * "Split ISO TDs" ... used for USB 1.1 devices going through the
1412  * TTs in USB 2.0 hubs.  These need microframe scheduling.
1413  */
1414
1415 static inline void
1416 sitd_sched_init (
1417         struct ehci_iso_sched   *iso_sched,
1418         struct ehci_iso_stream  *stream,
1419         struct urb              *urb
1420 )
1421 {
1422         unsigned        i;
1423         dma_addr_t      dma = urb->transfer_dma;
1424
1425         /* how many frames are needed for these transfers */
1426         iso_sched->span = urb->number_of_packets * stream->interval;
1427
1428         /* figure out per-frame sitd fields that we'll need later
1429          * when we fit new sitds into the schedule.
1430          */
1431         for (i = 0; i < urb->number_of_packets; i++) {
1432                 struct ehci_iso_packet  *packet = &iso_sched->packet [i];
1433                 unsigned                length;
1434                 dma_addr_t              buf;
1435                 u32                     trans;
1436
1437                 length = urb->iso_frame_desc [i].length & 0x03ff;
1438                 buf = dma + urb->iso_frame_desc [i].offset;
1439
1440                 trans = SITD_STS_ACTIVE;
1441                 if (((i + 1) == urb->number_of_packets)
1442                                 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1443                         trans |= SITD_IOC;
1444                 trans |= length << 16;
1445                 packet->transaction = cpu_to_le32 (trans);
1446
1447                 /* might need to cross a buffer page within a td */
1448                 packet->bufp = buf;
1449                 buf += length;
1450                 packet->buf1 = buf & ~0x0fff;
1451                 if (packet->buf1 != (buf & ~(u64)0x0fff))
1452                         packet->cross = 1;
1453
1454                 /* OUT uses multiple start-splits */ 
1455                 if (stream->bEndpointAddress & USB_DIR_IN)
1456                         continue;
1457                 length = 1 + (length / 188);
1458                 packet->buf1 |= length;
1459                 if (length > 1) /* BEGIN vs ALL */
1460                         packet->buf1 |= 1 << 3;
1461         }
1462 }
1463
1464 static int
1465 sitd_urb_transaction (
1466         struct ehci_iso_stream  *stream,
1467         struct ehci_hcd         *ehci,
1468         struct urb              *urb,
1469         int                     mem_flags
1470 )
1471 {
1472         struct ehci_sitd        *sitd;
1473         dma_addr_t              sitd_dma;
1474         int                     i;
1475         struct ehci_iso_sched   *iso_sched;
1476         unsigned long           flags;
1477
1478         iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1479         if (iso_sched == 0)
1480                 return -ENOMEM;
1481
1482         sitd_sched_init (iso_sched, stream, urb);
1483
1484         /* allocate/init sITDs */
1485         spin_lock_irqsave (&ehci->lock, flags);
1486         for (i = 0; i < urb->number_of_packets; i++) {
1487
1488                 /* NOTE:  for now, we don't try to handle wraparound cases
1489                  * for IN (using sitd->hw_backpointer, like a FSTN), which
1490                  * means we never need two sitds for full speed packets.
1491                  */
1492
1493                 /* free_list.next might be cache-hot ... but maybe
1494                  * the HC caches it too. avoid that issue for now.
1495                  */
1496
1497                 /* prefer previously-allocated sitds */
1498                 if (!list_empty(&stream->free_list)) {
1499                         sitd = list_entry (stream->free_list.prev,
1500                                          struct ehci_sitd, sitd_list);
1501                         list_del (&sitd->sitd_list);
1502                         sitd_dma = sitd->sitd_dma;
1503                 } else
1504                         sitd = NULL;
1505
1506                 if (!sitd) {
1507                         spin_unlock_irqrestore (&ehci->lock, flags);
1508                         sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1509                                         &sitd_dma);
1510                         spin_lock_irqsave (&ehci->lock, flags);
1511                 }
1512
1513                 if (!sitd) {
1514                         iso_sched_free (stream, iso_sched);
1515                         spin_unlock_irqrestore (&ehci->lock, flags);
1516                         return -ENOMEM;
1517                 }
1518                 memset (sitd, 0, sizeof *sitd);
1519                 sitd->sitd_dma = sitd_dma;
1520                 list_add (&sitd->sitd_list, &iso_sched->td_list);
1521         }
1522
1523         /* temporarily store schedule info in hcpriv */
1524         urb->hcpriv = iso_sched;
1525         urb->error_count = 0;
1526
1527         spin_unlock_irqrestore (&ehci->lock, flags);
1528         return 0;
1529 }
1530
1531 /*-------------------------------------------------------------------------*/
1532
1533 static inline void
1534 sitd_patch (
1535         struct ehci_iso_stream  *stream,
1536         struct ehci_sitd        *sitd,
1537         struct ehci_iso_sched   *iso_sched,
1538         unsigned                index
1539 )
1540 {
1541         struct ehci_iso_packet  *uf = &iso_sched->packet [index];
1542         u64                     bufp = uf->bufp;
1543
1544         sitd->hw_next = EHCI_LIST_END;
1545         sitd->hw_fullspeed_ep = stream->address;
1546         sitd->hw_uframe = stream->splits;
1547         sitd->hw_results = uf->transaction;
1548         sitd->hw_backpointer = EHCI_LIST_END;
1549
1550         bufp = uf->bufp;
1551         sitd->hw_buf [0] = cpu_to_le32 (bufp);
1552         sitd->hw_buf_hi [0] = cpu_to_le32 (bufp >> 32);
1553
1554         sitd->hw_buf [1] = cpu_to_le32 (uf->buf1);
1555         if (uf->cross) {
1556                 bufp += 4096;
1557                 sitd->hw_buf_hi [1] = cpu_to_le32 (bufp >> 32);
1558         }
1559         sitd->index = index;
1560 }
1561
1562 static inline void
1563 sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
1564 {
1565         /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1566         sitd->sitd_next = ehci->pshadow [frame];
1567         sitd->hw_next = ehci->periodic [frame];
1568         ehci->pshadow [frame].sitd = sitd;
1569         sitd->frame = frame;
1570         wmb ();
1571         ehci->periodic [frame] = cpu_to_le32 (sitd->sitd_dma) | Q_TYPE_SITD;
1572 }
1573
1574 /* fit urb's sitds into the selected schedule slot; activate as needed */
1575 static int
1576 sitd_link_urb (
1577         struct ehci_hcd         *ehci,
1578         struct urb              *urb,
1579         unsigned                mod,
1580         struct ehci_iso_stream  *stream
1581 )
1582 {
1583         int                     packet;
1584         unsigned                next_uframe;
1585         struct ehci_iso_sched   *sched = urb->hcpriv;
1586         struct ehci_sitd        *sitd;
1587
1588         next_uframe = stream->next_uframe;
1589
1590         if (list_empty(&stream->td_list)) {
1591                 /* usbfs ignores TT bandwidth */
1592                 hcd_to_bus (&ehci->hcd)->bandwidth_allocated
1593                                 += stream->bandwidth;
1594                 ehci_vdbg (ehci,
1595                         "sched dev%s ep%d%s-iso [%d] %dms/%04x\n",
1596                         urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1597                         (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1598                         (next_uframe >> 3) % ehci->periodic_size,
1599                         stream->interval, le32_to_cpu (stream->splits));
1600                 stream->start = jiffies;
1601         }
1602         hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs++;
1603
1604         /* fill sITDs frame by frame */
1605         for (packet = 0, sitd = NULL;
1606                         packet < urb->number_of_packets;
1607                         packet++) {
1608
1609                 /* ASSERT:  we have all necessary sitds */
1610                 BUG_ON (list_empty (&sched->td_list));
1611
1612                 /* ASSERT:  no itds for this endpoint in this frame */
1613
1614                 sitd = list_entry (sched->td_list.next,
1615                                 struct ehci_sitd, sitd_list);
1616                 list_move_tail (&sitd->sitd_list, &stream->td_list);
1617                 sitd->stream = iso_stream_get (stream);
1618                 sitd->urb = usb_get_urb (urb);
1619
1620                 sitd_patch (stream, sitd, sched, packet);
1621                 sitd_link (ehci, (next_uframe >> 3) % ehci->periodic_size,
1622                                 sitd);
1623
1624                 next_uframe += stream->interval << 3;
1625                 stream->depth += stream->interval << 3;
1626         }
1627         stream->next_uframe = next_uframe % mod;
1628
1629         /* don't need that schedule data any more */
1630         iso_sched_free (stream, sched);
1631         urb->hcpriv = NULL;
1632
1633         timer_action (ehci, TIMER_IO_WATCHDOG);
1634         if (!ehci->periodic_sched++)
1635                 return enable_periodic (ehci);
1636         return 0;
1637 }
1638
1639 /*-------------------------------------------------------------------------*/
1640
1641 #define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
1642                         | SITD_STS_XACT | SITD_STS_MMF | SITD_STS_STS)
1643
1644 static unsigned
1645 sitd_complete (
1646         struct ehci_hcd         *ehci,
1647         struct ehci_sitd        *sitd,
1648         struct pt_regs          *regs
1649 ) {
1650         struct urb                              *urb = sitd->urb;
1651         struct usb_iso_packet_descriptor        *desc;
1652         u32                                     t;
1653         int                                     urb_index = -1;
1654         struct ehci_iso_stream                  *stream = sitd->stream;
1655         struct usb_device                       *dev;
1656
1657         urb_index = sitd->index;
1658         desc = &urb->iso_frame_desc [urb_index];
1659         t = le32_to_cpup (&sitd->hw_results);
1660
1661         /* report transfer status */
1662         if (t & SITD_ERRS) {
1663                 urb->error_count++;
1664                 if (t & SITD_STS_DBE)
1665                         desc->status = usb_pipein (urb->pipe)
1666                                 ? -ENOSR  /* hc couldn't read */
1667                                 : -ECOMM; /* hc couldn't write */
1668                 else if (t & SITD_STS_BABBLE)
1669                         desc->status = -EOVERFLOW;
1670                 else /* XACT, MMF, etc */
1671                         desc->status = -EPROTO;
1672         } else {
1673                 desc->status = 0;
1674                 desc->actual_length = desc->length - SITD_LENGTH (t);
1675         }
1676
1677         usb_put_urb (urb);
1678         sitd->urb = NULL;
1679         sitd->stream = NULL;
1680         list_move (&sitd->sitd_list, &stream->free_list);
1681         stream->depth -= stream->interval << 3;
1682         iso_stream_put (ehci, stream);
1683
1684         /* handle completion now? */
1685         if ((urb_index + 1) != urb->number_of_packets)
1686                 return 0;
1687
1688         /* ASSERT: it's really the last sitd for this urb
1689         list_for_each_entry (sitd, &stream->td_list, sitd_list)
1690                 BUG_ON (sitd->urb == urb);
1691          */
1692
1693         /* give urb back to the driver */
1694         dev = usb_get_dev (urb->dev);
1695         ehci_urb_done (ehci, urb, regs);
1696         urb = NULL;
1697
1698         /* defer stopping schedule; completion can submit */
1699         ehci->periodic_sched--;
1700         if (!ehci->periodic_sched)
1701                 (void) disable_periodic (ehci);
1702         hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs--;
1703
1704         if (list_empty (&stream->td_list)) {
1705                 hcd_to_bus (&ehci->hcd)->bandwidth_allocated
1706                                 -= stream->bandwidth;
1707                 ehci_vdbg (ehci,
1708                         "deschedule devp %s ep%d%s-iso\n",
1709                         dev->devpath, stream->bEndpointAddress & 0x0f,
1710                         (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1711         }
1712         iso_stream_put (ehci, stream);
1713         usb_put_dev (dev);
1714
1715         return 1;
1716 }
1717
1718
1719 static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
1720 {
1721         int                     status = -EINVAL;
1722         unsigned long           flags;
1723         struct ehci_iso_stream  *stream;
1724
1725         // FIXME remove when csplits behave
1726         if (usb_pipein(urb->pipe)) {
1727                 ehci_dbg (ehci, "no iso-IN split transactions yet\n");
1728                 return -ENOMEM;
1729         }
1730
1731         /* Get iso_stream head */
1732         stream = iso_stream_find (ehci, urb);
1733         if (stream == 0) {
1734                 ehci_dbg (ehci, "can't get iso stream\n");
1735                 return -ENOMEM;
1736         }
1737         if (urb->interval != stream->interval) {
1738                 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1739                         stream->interval, urb->interval);
1740                 goto done;
1741         }
1742
1743 #ifdef EHCI_URB_TRACE
1744         ehci_dbg (ehci,
1745                 "submit %p dev%s ep%d%s-iso len %d\n",
1746                 urb, urb->dev->devpath,
1747                 usb_pipeendpoint (urb->pipe),
1748                 usb_pipein (urb->pipe) ? "in" : "out",
1749                 urb->transfer_buffer_length);
1750 #endif
1751
1752         /* allocate SITDs */
1753         status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
1754         if (status < 0) {
1755                 ehci_dbg (ehci, "can't init sitds\n");
1756                 goto done;
1757         }
1758
1759         /* schedule ... need to lock */
1760         spin_lock_irqsave (&ehci->lock, flags);
1761         status = iso_stream_schedule (ehci, urb, stream);
1762         if (status == 0)
1763                 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
1764         spin_unlock_irqrestore (&ehci->lock, flags);
1765
1766 done:
1767         if (status < 0)
1768                 iso_stream_put (ehci, stream);
1769         return status;
1770 }
1771
1772 #else
1773
1774 static inline int
1775 sitd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
1776 {
1777         ehci_dbg (ehci, "split iso support is disabled\n");
1778         return -ENOSYS;
1779 }
1780
1781 static inline unsigned
1782 sitd_complete (
1783         struct ehci_hcd         *ehci,
1784         struct ehci_sitd        *sitd,
1785         struct pt_regs          *regs
1786 ) {
1787         ehci_err (ehci, "sitd_complete %p?\n", sitd);
1788         return 0;
1789 }
1790
1791 #endif /* USB_EHCI_SPLIT_ISO */
1792
1793 /*-------------------------------------------------------------------------*/
1794
1795 static void
1796 scan_periodic (struct ehci_hcd *ehci, struct pt_regs *regs)
1797 {
1798         unsigned        frame, clock, now_uframe, mod;
1799         unsigned        modified;
1800
1801         mod = ehci->periodic_size << 3;
1802
1803         /*
1804          * When running, scan from last scan point up to "now"
1805          * else clean up by scanning everything that's left.
1806          * Touches as few pages as possible:  cache-friendly.
1807          */
1808         now_uframe = ehci->next_uframe;
1809         if (HCD_IS_RUNNING (ehci->hcd.state))
1810                 clock = readl (&ehci->regs->frame_index);
1811         else
1812                 clock = now_uframe + mod - 1;
1813         clock %= mod;
1814
1815         for (;;) {
1816                 union ehci_shadow       q, *q_p;
1817                 __le32                  type, *hw_p;
1818                 unsigned                uframes;
1819
1820                 /* don't scan past the live uframe */
1821                 frame = now_uframe >> 3;
1822                 if (frame == (clock >> 3))
1823                         uframes = now_uframe & 0x07;
1824                 else {
1825                         /* safe to scan the whole frame at once */
1826                         now_uframe |= 0x07;
1827                         uframes = 8;
1828                 }
1829
1830 restart:
1831                 /* scan each element in frame's queue for completions */
1832                 q_p = &ehci->pshadow [frame];
1833                 hw_p = &ehci->periodic [frame];
1834                 q.ptr = q_p->ptr;
1835                 type = Q_NEXT_TYPE (*hw_p);
1836                 modified = 0;
1837
1838                 while (q.ptr != 0) {
1839                         unsigned                uf;
1840                         union ehci_shadow       temp;
1841                         int                     live;
1842
1843                         live = HCD_IS_RUNNING (ehci->hcd.state);
1844                         switch (type) {
1845                         case Q_TYPE_QH:
1846                                 /* handle any completions */
1847                                 temp.qh = qh_get (q.qh);
1848                                 type = Q_NEXT_TYPE (q.qh->hw_next);
1849                                 q = q.qh->qh_next;
1850                                 modified = qh_completions (ehci, temp.qh, regs);
1851                                 if (unlikely (list_empty (&temp.qh->qtd_list)))
1852                                         intr_deschedule (ehci, temp.qh, 0);
1853                                 qh_put (temp.qh);
1854                                 break;
1855                         case Q_TYPE_FSTN:
1856                                 /* for "save place" FSTNs, look at QH entries
1857                                  * in the previous frame for completions.
1858                                  */
1859                                 if (q.fstn->hw_prev != EHCI_LIST_END) {
1860                                         dbg ("ignoring completions from FSTNs");
1861                                 }
1862                                 type = Q_NEXT_TYPE (q.fstn->hw_next);
1863                                 q = q.fstn->fstn_next;
1864                                 break;
1865                         case Q_TYPE_ITD:
1866                                 /* skip itds for later in the frame */
1867                                 rmb ();
1868                                 for (uf = live ? uframes : 8; uf < 8; uf++) {
1869                                         if (0 == (q.itd->hw_transaction [uf]
1870                                                         & ITD_ACTIVE))
1871                                                 continue;
1872                                         q_p = &q.itd->itd_next;
1873                                         hw_p = &q.itd->hw_next;
1874                                         type = Q_NEXT_TYPE (q.itd->hw_next);
1875                                         q = *q_p;
1876                                         break;
1877                                 }
1878                                 if (uf != 8)
1879                                         break;
1880
1881                                 /* this one's ready ... HC won't cache the
1882                                  * pointer for much longer, if at all.
1883                                  */
1884                                 *q_p = q.itd->itd_next;
1885                                 *hw_p = q.itd->hw_next;
1886                                 type = Q_NEXT_TYPE (q.itd->hw_next);
1887                                 wmb();
1888                                 modified = itd_complete (ehci, q.itd, regs);
1889                                 q = *q_p;
1890                                 break;
1891                         case Q_TYPE_SITD:
1892                                 if ((q.sitd->hw_results & SITD_ACTIVE)
1893                                                 && live) {
1894                                         q_p = &q.sitd->sitd_next;
1895                                         hw_p = &q.sitd->hw_next;
1896                                         type = Q_NEXT_TYPE (q.sitd->hw_next);
1897                                         q = *q_p;
1898                                         break;
1899                                 }
1900                                 *q_p = q.sitd->sitd_next;
1901                                 *hw_p = q.sitd->hw_next;
1902                                 type = Q_NEXT_TYPE (q.sitd->hw_next);
1903                                 wmb();
1904                                 modified = sitd_complete (ehci, q.sitd, regs);
1905                                 q = *q_p;
1906                                 break;
1907                         default:
1908                                 dbg ("corrupt type %d frame %d shadow %p",
1909                                         type, frame, q.ptr);
1910                                 // BUG ();
1911                                 q.ptr = NULL;
1912                         }
1913
1914                         /* assume completion callbacks modify the queue */
1915                         if (unlikely (modified))
1916                                 goto restart;
1917                 }
1918
1919                 /* stop when we catch up to the HC */
1920
1921                 // FIXME:  this assumes we won't get lapped when
1922                 // latencies climb; that should be rare, but...
1923                 // detect it, and just go all the way around.
1924                 // FLR might help detect this case, so long as latencies
1925                 // don't exceed periodic_size msec (default 1.024 sec).
1926
1927                 // FIXME:  likewise assumes HC doesn't halt mid-scan
1928
1929                 if (now_uframe == clock) {
1930                         unsigned        now;
1931
1932                         if (!HCD_IS_RUNNING (ehci->hcd.state))
1933                                 break;
1934                         ehci->next_uframe = now_uframe;
1935                         now = readl (&ehci->regs->frame_index) % mod;
1936                         if (now_uframe == now)
1937                                 break;
1938
1939                         /* rescan the rest of this frame, then ... */
1940                         clock = now;
1941                 } else {
1942                         now_uframe++;
1943                         now_uframe %= mod;
1944                 }
1945         } 
1946 }