6aa4c4f89c77dda1999c2dff1278308ccdfc7a97
[linux-flexiantxendom0-3.2.10.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, int 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 #ifdef have_split_iso
57         case Q_TYPE_SITD:
58                 return &periodic->sitd->sitd_next;
59 #endif /* have_split_iso */
60         }
61         dbg ("BAD shadow %p tag %d", periodic->ptr, tag);
62         // BUG ();
63         return 0;
64 }
65
66 /* returns true after successful unlink */
67 /* caller must hold ehci->lock */
68 static int periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
69 {
70         union ehci_shadow       *prev_p = &ehci->pshadow [frame];
71         u32                     *hw_p = &ehci->periodic [frame];
72         union ehci_shadow       here = *prev_p;
73         union ehci_shadow       *next_p;
74
75         /* find predecessor of "ptr"; hw and shadow lists are in sync */
76         while (here.ptr && here.ptr != ptr) {
77                 prev_p = periodic_next_shadow (prev_p, Q_NEXT_TYPE (*hw_p));
78                 hw_p = &here.qh->hw_next;
79                 here = *prev_p;
80         }
81         /* an interrupt entry (at list end) could have been shared */
82         if (!here.ptr) {
83                 dbg ("entry %p no longer on frame [%d]", ptr, frame);
84                 return 0;
85         }
86         // vdbg ("periodic unlink %p from frame %d", ptr, frame);
87
88         /* update hardware list ... HC may still know the old structure, so
89          * don't change hw_next until it'll have purged its cache
90          */
91         next_p = periodic_next_shadow (&here, Q_NEXT_TYPE (*hw_p));
92         *hw_p = here.qh->hw_next;
93
94         /* unlink from shadow list; HCD won't see old structure again */
95         *prev_p = *next_p;
96         next_p->ptr = 0;
97
98         return 1;
99 }
100
101 /* how many of the uframe's 125 usecs are allocated? */
102 static unsigned short
103 periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
104 {
105         u32                     *hw_p = &ehci->periodic [frame];
106         union ehci_shadow       *q = &ehci->pshadow [frame];
107         unsigned                usecs = 0;
108
109         while (q->ptr) {
110                 switch (Q_NEXT_TYPE (*hw_p)) {
111                 case Q_TYPE_QH:
112                         /* is it in the S-mask? */
113                         if (q->qh->hw_info2 & cpu_to_le32 (1 << uframe))
114                                 usecs += q->qh->usecs;
115                         /* ... or C-mask? */
116                         if (q->qh->hw_info2 & cpu_to_le32 (1 << (8 + uframe)))
117                                 usecs += q->qh->c_usecs;
118                         q = &q->qh->qh_next;
119                         break;
120                 case Q_TYPE_FSTN:
121                         /* for "save place" FSTNs, count the relevant INTR
122                          * bandwidth from the previous frame
123                          */
124                         if (q->fstn->hw_prev != EHCI_LIST_END) {
125                                 dbg ("not counting FSTN bandwidth yet ...");
126                         }
127                         q = &q->fstn->fstn_next;
128                         break;
129                 case Q_TYPE_ITD:
130                         usecs += q->itd->usecs [uframe];
131                         q = &q->itd->itd_next;
132                         break;
133 #ifdef have_split_iso
134                 case Q_TYPE_SITD:
135                         temp = q->sitd->hw_fullspeed_ep &
136                                 __constant_cpu_to_le32 (1 << 31);
137
138                         // FIXME:  this doesn't count data bytes right...
139
140                         /* is it in the S-mask?  (count SPLIT, DATA) */
141                         if (q->sitd->hw_uframe & cpu_to_le32 (1 << uframe)) {
142                                 if (temp)
143                                         usecs += HS_USECS (188);
144                                 else
145                                         usecs += HS_USECS (1);
146                         }
147
148                         /* ... C-mask?  (count CSPLIT, DATA) */
149                         if (q->sitd->hw_uframe &
150                                         cpu_to_le32 (1 << (8 + uframe))) {
151                                 if (temp)
152                                         usecs += HS_USECS (0);
153                                 else
154                                         usecs += HS_USECS (188);
155                         }
156                         q = &q->sitd->sitd_next;
157                         break;
158 #endif /* have_split_iso */
159                 default:
160                         BUG ();
161                 }
162         }
163 #ifdef  DEBUG
164         if (usecs > 100)
165                 err ("overallocated uframe %d, periodic is %d usecs",
166                         frame * 8 + uframe, usecs);
167 #endif
168         return usecs;
169 }
170
171 /*-------------------------------------------------------------------------*/
172
173 static int enable_periodic (struct ehci_hcd *ehci)
174 {
175         u32     cmd;
176         int     status;
177
178         /* did clearing PSE did take effect yet?
179          * takes effect only at frame boundaries...
180          */
181         status = handshake (&ehci->regs->status, STS_PSS, 0, 9 * 125);
182         if (status != 0) {
183                 ehci->hcd.state = USB_STATE_HALT;
184                 return status;
185         }
186
187         cmd = readl (&ehci->regs->command) | CMD_PSE;
188         writel (cmd, &ehci->regs->command);
189         /* posted write ... PSS happens later */
190         ehci->hcd.state = USB_STATE_RUNNING;
191
192         /* make sure ehci_work scans these */
193         ehci->next_uframe = readl (&ehci->regs->frame_index)
194                                 % (ehci->periodic_size << 3);
195         return 0;
196 }
197
198 static int disable_periodic (struct ehci_hcd *ehci)
199 {
200         u32     cmd;
201         int     status;
202
203         /* did setting PSE not take effect yet?
204          * takes effect only at frame boundaries...
205          */
206         status = handshake (&ehci->regs->status, STS_PSS, STS_PSS, 9 * 125);
207         if (status != 0) {
208                 ehci->hcd.state = USB_STATE_HALT;
209                 return status;
210         }
211
212         cmd = readl (&ehci->regs->command) & ~CMD_PSE;
213         writel (cmd, &ehci->regs->command);
214         /* posted write ... */
215
216         ehci->next_uframe = -1;
217         return 0;
218 }
219
220 /*-------------------------------------------------------------------------*/
221
222 // FIXME microframe periods not yet handled
223
224 static void intr_deschedule (
225         struct ehci_hcd *ehci,
226         struct ehci_qh  *qh,
227         int             wait
228 ) {
229         int             status;
230         unsigned        frame = qh->start;
231
232         do {
233                 periodic_unlink (ehci, frame, qh);
234                 qh_put (ehci, qh);
235                 frame += qh->period;
236         } while (frame < ehci->periodic_size);
237
238         qh->qh_state = QH_STATE_UNLINK;
239         qh->qh_next.ptr = 0;
240         ehci->periodic_sched--;
241
242         /* maybe turn off periodic schedule */
243         if (!ehci->periodic_sched)
244                 status = disable_periodic (ehci);
245         else {
246                 status = 0;
247                 vdbg ("periodic schedule still enabled");
248         }
249
250         /*
251          * If the hc may be looking at this qh, then delay a uframe
252          * (yeech!) to be sure it's done.
253          * No other threads may be mucking with this qh.
254          */
255         if (((ehci_get_frame (&ehci->hcd) - frame) % qh->period) == 0) {
256                 if (wait) {
257                         udelay (125);
258                         qh->hw_next = EHCI_LIST_END;
259                 } else {
260                         /* we may not be IDLE yet, but if the qh is empty
261                          * the race is very short.  then if qh also isn't
262                          * rescheduled soon, it won't matter.  otherwise...
263                          */
264                         vdbg ("intr_deschedule...");
265                 }
266         } else
267                 qh->hw_next = EHCI_LIST_END;
268
269         qh->qh_state = QH_STATE_IDLE;
270
271         /* update per-qh bandwidth utilization (for usbfs) */
272         hcd_to_bus (&ehci->hcd)->bandwidth_allocated -= 
273                 (qh->usecs + qh->c_usecs) / qh->period;
274
275         dbg ("descheduled qh %p, period = %d frame = %d count = %d, urbs = %d",
276                 qh, qh->period, frame,
277                 atomic_read (&qh->refcount), ehci->periodic_sched);
278 }
279
280 static int check_period (
281         struct ehci_hcd *ehci, 
282         unsigned        frame,
283         unsigned        uframe,
284         unsigned        period,
285         unsigned        usecs
286 ) {
287         /* complete split running into next frame?
288          * given FSTN support, we could sometimes check...
289          */
290         if (uframe >= 8)
291                 return 0;
292
293         /*
294          * 80% periodic == 100 usec/uframe available
295          * convert "usecs we need" to "max already claimed" 
296          */
297         usecs = 100 - usecs;
298
299         do {
300                 int     claimed;
301
302 // FIXME delete when intr_submit handles non-empty queues
303 // this gives us a one intr/frame limit (vs N/uframe)
304 // ... and also lets us avoid tracking split transactions
305 // that might collide at a given TT/hub.
306                 if (ehci->pshadow [frame].ptr)
307                         return 0;
308
309                 claimed = periodic_usecs (ehci, frame, uframe);
310                 if (claimed > usecs)
311                         return 0;
312
313 // FIXME update to handle sub-frame periods
314         } while ((frame += period) < ehci->periodic_size);
315
316         // success!
317         return 1;
318 }
319
320 static int check_intr_schedule (
321         struct ehci_hcd         *ehci, 
322         unsigned                frame,
323         unsigned                uframe,
324         const struct ehci_qh    *qh,
325         u32                     *c_maskp
326 )
327 {
328         int             retval = -ENOSPC;
329
330         if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
331                 goto done;
332         if (!qh->c_usecs) {
333                 retval = 0;
334                 *c_maskp = cpu_to_le32 (0);
335                 goto done;
336         }
337
338         /* This is a split transaction; check the bandwidth available for
339          * the completion too.  Check both worst and best case gaps: worst
340          * case is SPLIT near uframe end, and CSPLIT near start ... best is
341          * vice versa.  Difference can be almost two uframe times, but we
342          * reserve unnecessary bandwidth (waste it) this way.  (Actually
343          * even better cases exist, like immediate device NAK.)
344          *
345          * FIXME don't even bother unless we know this TT is idle in that
346          * range of uframes ... for now, check_period() allows only one
347          * interrupt transfer per frame, so needn't check "TT busy" status
348          * when scheduling a split (QH, SITD, or FSTN).
349          *
350          * FIXME ehci 0.96 and above can use FSTNs
351          */
352         if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
353                                 qh->period, qh->c_usecs))
354                 goto done;
355         if (!check_period (ehci, frame, uframe + qh->gap_uf,
356                                 qh->period, qh->c_usecs))
357                 goto done;
358
359         *c_maskp = cpu_to_le32 (0x03 << (8 + uframe + qh->gap_uf));
360         retval = 0;
361 done:
362         return retval;
363 }
364
365 static int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
366 {
367         int             status;
368         unsigned        uframe;
369         u32             c_mask;
370         unsigned        frame;          /* 0..(qh->period - 1), or NO_FRAME */
371
372         qh->hw_next = EHCI_LIST_END;
373         frame = qh->start;
374
375         /* reuse the previous schedule slots, if we can */
376         if (frame < qh->period) {
377                 uframe = ffs (le32_to_cpup (&qh->hw_info2) & 0x00ff);
378                 status = check_intr_schedule (ehci, frame, --uframe,
379                                 qh, &c_mask);
380         } else {
381                 uframe = 0;
382                 c_mask = 0;
383                 status = -ENOSPC;
384         }
385
386         /* else scan the schedule to find a group of slots such that all
387          * uframes have enough periodic bandwidth available.
388          */
389         if (status) {
390                 frame = qh->period - 1;
391                 do {
392                         for (uframe = 0; uframe < 8; uframe++) {
393                                 status = check_intr_schedule (ehci,
394                                                 frame, uframe, qh,
395                                                 &c_mask);
396                                 if (status == 0)
397                                         break;
398                         }
399                 } while (status && frame--);
400                 if (status)
401                         goto done;
402                 qh->start = frame;
403
404                 /* reset S-frame and (maybe) C-frame masks */
405                 qh->hw_info2 &= ~0xffff;
406                 qh->hw_info2 |= cpu_to_le32 (1 << uframe) | c_mask;
407         } else
408                 dbg ("reused previous qh %p schedule", qh);
409
410         /* stuff into the periodic schedule */
411         qh->qh_state = QH_STATE_LINKED;
412         dbg ("scheduled qh %p usecs %d/%d period %d.0 starting %d.%d (gap %d)",
413                 qh, qh->usecs, qh->c_usecs,
414                 qh->period, frame, uframe, qh->gap_uf);
415         do {
416                 if (unlikely (ehci->pshadow [frame].ptr != 0)) {
417
418 // FIXME -- just link toward the end, before any qh with a shorter period,
419 // AND accommodate it already having been linked here (after some other qh)
420 // AS WELL AS updating the schedule checking logic
421
422                         BUG ();
423                 } else {
424                         ehci->pshadow [frame].qh = qh_get (qh);
425                         ehci->periodic [frame] =
426                                 QH_NEXT (qh->qh_dma);
427                 }
428                 wmb ();
429                 frame += qh->period;
430         } while (frame < ehci->periodic_size);
431
432         /* update per-qh bandwidth for usbfs */
433         hcd_to_bus (&ehci->hcd)->bandwidth_allocated += 
434                 (qh->usecs + qh->c_usecs) / qh->period;
435
436         /* maybe enable periodic schedule processing */
437         if (!ehci->periodic_sched++)
438                 status = enable_periodic (ehci);
439 done:
440         return status;
441 }
442
443 static int intr_submit (
444         struct ehci_hcd         *ehci,
445         struct urb              *urb,
446         struct list_head        *qtd_list,
447         int                     mem_flags
448 ) {
449         unsigned                epnum;
450         unsigned long           flags;
451         struct ehci_qh          *qh;
452         struct hcd_dev          *dev;
453         int                     is_input;
454         int                     status = 0;
455         struct list_head        empty;
456
457         /* get endpoint and transfer/schedule data */
458         epnum = usb_pipeendpoint (urb->pipe);
459         is_input = usb_pipein (urb->pipe);
460         if (is_input)
461                 epnum |= 0x10;
462
463         spin_lock_irqsave (&ehci->lock, flags);
464         dev = (struct hcd_dev *)urb->dev->hcpriv;
465
466         /* get qh and force any scheduling errors */
467         INIT_LIST_HEAD (&empty);
468         qh = qh_append_tds (ehci, urb, &empty, epnum, &dev->ep [epnum]);
469         if (qh == 0) {
470                 status = -ENOMEM;
471                 goto done;
472         }
473         if (qh->qh_state == QH_STATE_IDLE) {
474                 if ((status = qh_schedule (ehci, qh)) != 0)
475                         goto done;
476         }
477
478         /* then queue the urb's tds to the qh */
479         qh = qh_append_tds (ehci, urb, qtd_list, epnum, &dev->ep [epnum]);
480         BUG_ON (qh == 0);
481
482         /* ... update usbfs periodic stats */
483         hcd_to_bus (&ehci->hcd)->bandwidth_int_reqs++;
484
485 done:
486         spin_unlock_irqrestore (&ehci->lock, flags);
487         if (status)
488                 qtd_list_free (ehci, urb, qtd_list);
489
490         return status;
491 }
492
493 static unsigned
494 intr_complete (
495         struct ehci_hcd *ehci,
496         unsigned        frame,
497         struct ehci_qh  *qh,
498         struct pt_regs  *regs
499 ) {
500         unsigned        count;
501
502         /* nothing to report? */
503         if (likely ((qh->hw_token & __constant_cpu_to_le32 (QTD_STS_ACTIVE))
504                         != 0))
505                 return 0;
506         if (unlikely (list_empty (&qh->qtd_list))) {
507                 dbg ("intr qh %p no TDs?", qh);
508                 return 0;
509         }
510         
511         /* handle any completions */
512         count = qh_completions (ehci, qh, regs);
513
514         if (unlikely (list_empty (&qh->qtd_list)))
515                 intr_deschedule (ehci, qh, 0);
516
517         return count;
518 }
519
520 /*-------------------------------------------------------------------------*/
521
522 static inline struct ehci_iso_stream *
523 iso_stream_alloc (int mem_flags)
524 {
525         struct ehci_iso_stream *stream;
526
527         stream = kmalloc(sizeof *stream, mem_flags);
528         if (likely (stream != 0)) {
529                 memset (stream, 0, sizeof(*stream));
530                 INIT_LIST_HEAD(&stream->itd_list);
531                 INIT_LIST_HEAD(&stream->free_itd_list);
532                 stream->next_uframe = -1;
533                 stream->refcount = 1;
534         }
535         return stream;
536 }
537
538 static inline void
539 iso_stream_init (
540         struct ehci_iso_stream  *stream,
541         struct usb_device       *dev,
542         int                     pipe,
543         unsigned                interval
544 )
545 {
546         u32                     buf1;
547         unsigned                epnum, maxp, multi;
548         int                     is_input;
549         long                    bandwidth;
550
551         /*
552          * this might be a "high bandwidth" highspeed endpoint,
553          * as encoded in the ep descriptor's wMaxPacket field
554          */
555         epnum = usb_pipeendpoint (pipe);
556         is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
557         if (is_input) {
558                 maxp = dev->epmaxpacketin [epnum];
559                 buf1 = (1 << 11);
560         } else {
561                 maxp = dev->epmaxpacketout [epnum];
562                 buf1 = 0;
563         }
564
565         multi = hb_mult(maxp);
566         maxp = max_packet(maxp);
567         buf1 |= maxp;
568         maxp *= multi;
569
570         stream->dev = (struct hcd_dev *)dev->hcpriv;
571
572         stream->bEndpointAddress = is_input | epnum;
573         stream->interval = interval;
574         stream->maxp = maxp;
575
576         stream->buf0 = cpu_to_le32 ((epnum << 8) | dev->devnum);
577         stream->buf1 = cpu_to_le32 (buf1);
578         stream->buf2 = cpu_to_le32 (multi);
579
580         /* usbfs wants to report the average usecs per frame tied up
581          * when transfers on this endpoint are scheduled ...
582          */
583         stream->usecs = HS_USECS_ISO (maxp);
584         bandwidth = stream->usecs * 8;
585         bandwidth /= 1 << (interval - 1);
586         stream->bandwidth = bandwidth;
587 }
588
589 static void
590 iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
591 {
592         stream->refcount--;
593
594         /* free whenever just a dev->ep reference remains.
595          * not like a QH -- no persistent state (toggle, halt)
596          */
597         if (stream->refcount == 1) {
598                 int is_in;
599
600                 // BUG_ON (!list_empty(&stream->itd_list));
601
602                 while (!list_empty (&stream->free_itd_list)) {
603                         struct ehci_itd *itd;
604
605                         itd = list_entry (stream->free_itd_list.next,
606                                 struct ehci_itd, itd_list);
607                         list_del (&itd->itd_list);
608                         pci_pool_free (ehci->itd_pool, itd, itd->itd_dma);
609                 }
610
611                 is_in = (stream->bEndpointAddress & USB_DIR_IN) ? 0x10 : 0;
612                 stream->bEndpointAddress &= 0x0f;
613                 stream->dev->ep [is_in + stream->bEndpointAddress] = 0;
614
615                 if (stream->rescheduled) {
616                         ehci_info (ehci, "ep%d%s-iso rescheduled "
617                                 "%lu times in %lu seconds\n",
618                                 stream->bEndpointAddress, is_in ? "in" : "out",
619                                 stream->rescheduled,
620                                 ((jiffies - stream->start)/HZ)
621                                 );
622                 }
623
624                 kfree(stream);
625         }
626 }
627
628 static inline struct ehci_iso_stream *
629 iso_stream_get (struct ehci_iso_stream *stream)
630 {
631         if (likely (stream != 0))
632                 stream->refcount++;
633         return stream;
634 }
635
636 static struct ehci_iso_stream *
637 iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
638 {
639         unsigned                epnum;
640         struct hcd_dev          *dev;
641         struct ehci_iso_stream  *stream;
642         unsigned long           flags;
643
644         epnum = usb_pipeendpoint (urb->pipe);
645         if (usb_pipein(urb->pipe))
646                 epnum += 0x10;
647
648         spin_lock_irqsave (&ehci->lock, flags);
649
650         dev = (struct hcd_dev *)urb->dev->hcpriv;
651         stream = dev->ep [epnum];
652
653         if (unlikely (stream == 0)) {
654                 stream = iso_stream_alloc(GFP_ATOMIC);
655                 if (likely (stream != 0)) {
656                         /* dev->ep owns the initial refcount */
657                         dev->ep[epnum] = stream;
658                         iso_stream_init(stream, urb->dev, urb->pipe,
659                                         urb->interval);
660                 }
661
662         /* if dev->ep [epnum] is a QH, info1.maxpacket is nonzero */
663         } else if (unlikely (stream->hw_info1 != 0)) {
664                 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
665                         urb->dev->devpath, epnum & 0x0f,
666                         (epnum & 0x10) ? "in" : "out");
667                 stream = 0;
668         }
669
670         /* caller guarantees an eventual matching iso_stream_put */
671         stream = iso_stream_get (stream);
672
673         spin_unlock_irqrestore (&ehci->lock, flags);
674         return stream;
675 }
676
677 /*-------------------------------------------------------------------------*/
678
679 static inline struct ehci_itd_sched *
680 itd_sched_alloc (unsigned packets, int mem_flags)
681 {
682         struct ehci_itd_sched   *itd_sched;
683         int                     size = sizeof *itd_sched;
684
685         size += packets * sizeof (struct ehci_iso_uframe);
686         itd_sched = kmalloc (size, mem_flags);
687         if (likely (itd_sched != 0)) {
688                 memset(itd_sched, 0, size);
689                 INIT_LIST_HEAD (&itd_sched->itd_list);
690         }
691         return itd_sched;
692 }
693
694 static int
695 itd_sched_init (
696         struct ehci_itd_sched   *itd_sched,
697         struct ehci_iso_stream  *stream,
698         struct urb              *urb
699 )
700 {
701         unsigned        i;
702         dma_addr_t      dma = urb->transfer_dma;
703
704         /* how many uframes are needed for these transfers */
705         itd_sched->span = urb->number_of_packets * stream->interval;
706
707         /* figure out per-uframe itd fields that we'll need later
708          * when we fit new itds into the schedule.
709          */
710         for (i = 0; i < urb->number_of_packets; i++) {
711                 struct ehci_iso_uframe  *uframe = &itd_sched->packet [i];
712                 unsigned                length;
713                 dma_addr_t              buf;
714                 u32                     trans;
715
716                 length = urb->iso_frame_desc [i].length;
717                 buf = dma + urb->iso_frame_desc [i].offset;
718
719                 trans = EHCI_ISOC_ACTIVE;
720                 trans |= buf & 0x0fff;
721                 if (unlikely ((i + 1) == urb->number_of_packets))
722                         trans |= EHCI_ITD_IOC;
723                 trans |= length << 16;
724                 uframe->transaction = cpu_to_le32 (trans);
725
726                 /* might need to cross a buffer page within a td */
727                 uframe->bufp = (buf & ~(u64)0x0fff);
728                 buf += length;
729                 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
730                         uframe->cross = 1;
731         }
732         return 0;
733 }
734
735 static void
736 itd_sched_free (
737         struct ehci_iso_stream  *stream,
738         struct ehci_itd_sched   *itd_sched
739 )
740 {
741         list_splice (&itd_sched->itd_list, &stream->free_itd_list);
742         kfree (itd_sched);
743 }
744
745 static int
746 itd_urb_transaction (
747         struct ehci_iso_stream  *stream,
748         struct ehci_hcd         *ehci,
749         struct urb              *urb,
750         int                     mem_flags
751 )
752 {
753         struct ehci_itd         *itd;
754         int                     status;
755         dma_addr_t              itd_dma;
756         int                     i;
757         unsigned                num_itds;
758         struct ehci_itd_sched   *itd_sched;
759
760         itd_sched = itd_sched_alloc (urb->number_of_packets, mem_flags);
761         if (unlikely (itd_sched == 0))
762                 return -ENOMEM;
763
764         status = itd_sched_init (itd_sched, stream, urb);
765         if (unlikely (status != 0))  {
766                 itd_sched_free (stream, itd_sched);
767                 return status;
768         }
769
770         if (urb->interval < 8)
771                 num_itds = 1 + (itd_sched->span + 7) / 8;
772         else
773                 num_itds = urb->number_of_packets;
774
775         /* allocate/init ITDs */
776         for (i = 0; i < num_itds; i++) {
777
778                 /* free_itd_list.next might be cache-hot ... but maybe
779                  * the HC caches it too. avoid that issue for now.
780                  */
781
782                 /* prefer previously-allocated itds */
783                 if (likely (!list_empty(&stream->free_itd_list))) {
784                         itd = list_entry (stream->free_itd_list.prev,
785                                          struct ehci_itd, itd_list);
786                         list_del (&itd->itd_list);
787                         itd_dma = itd->itd_dma;
788                 } else
789                         itd = pci_pool_alloc (ehci->itd_pool, mem_flags,
790                                         &itd_dma);
791
792                 if (unlikely (0 == itd)) {
793                         itd_sched_free (stream, itd_sched);
794                         return -ENOMEM;
795                 }
796                 memset (itd, 0, sizeof *itd);
797                 itd->itd_dma = itd_dma;
798                 list_add (&itd->itd_list, &itd_sched->itd_list);
799         }
800
801         /* temporarily store schedule info in hcpriv */
802         urb->hcpriv = itd_sched;
803         urb->error_count = 0;
804         return 0;
805 }
806
807 /*
808  * This scheduler plans almost as far into the future as it has actual
809  * periodic schedule slots.  (Affected by TUNE_FLS, which defaults to
810  * "as small as possible" to be cache-friendlier.)  That limits the size
811  * transfers you can stream reliably; avoid more than 64 msec per urb.
812  * Also avoid queue depths of less than the system's worst irq latency.
813  */
814
815 #define SCHEDULE_SLOP   10      /* frames */
816
817 static int
818 itd_stream_schedule (
819         struct ehci_hcd         *ehci,
820         struct urb              *urb,
821         struct ehci_iso_stream  *stream
822 )
823 {
824         u32                     now, start, end, max;
825         int                     status;
826         unsigned                mod = ehci->periodic_size << 3;
827         struct ehci_itd_sched   *itd_sched = urb->hcpriv;
828
829         if (unlikely (itd_sched->span > (mod - 8 * SCHEDULE_SLOP))) {
830                 ehci_dbg (ehci, "iso request %p too long\n", urb);
831                 status = -EFBIG;
832                 goto fail;
833         }
834
835         now = readl (&ehci->regs->frame_index) % mod;
836
837         /* when's the last uframe this urb could start? */
838         max = now + mod;
839         max -= itd_sched->span;
840         max -= 8 * SCHEDULE_SLOP;
841
842         /* typical case: reuse current schedule. stream is still active,
843          * and no gaps from host falling behind (irq delays etc)
844          */
845         if (likely (!list_empty (&stream->itd_list))) {
846
847                 start = stream->next_uframe;
848                 if (start < now)
849                         start += mod;
850                 if (likely (start < max))
851                         goto ready;
852
853                 /* two cases:
854                  * (a) we missed some uframes ... can reschedule
855                  * (b) trying to overcommit the schedule
856                  * FIXME (b) should be a hard failure
857                  */
858         }
859
860         /* need to schedule; when's the next (u)frame we could start?
861          * this is bigger than ehci->i_thresh allows; scheduling itself
862          * isn't free, the slop should handle reasonably slow cpus.  it
863          * can also help high bandwidth if the dma and irq loads don't
864          * jump until after the queue is primed.
865          */
866         start = SCHEDULE_SLOP * 8 + (now & ~0x07);
867         end = start;
868
869         ehci_vdbg (ehci, "%s schedule from %d (%d..%d), was %d\n",
870                         __FUNCTION__, now, start, max,
871                         stream->next_uframe);
872
873         /* NOTE:  assumes URB_ISO_ASAP, to limit complexity/bugs */
874
875         if (likely (max > (start + urb->interval)))
876                 max = start + urb->interval;
877
878         /* hack:  account for itds already scheduled to this endpoint */
879         if (unlikely (list_empty (&stream->itd_list)))
880                 end = max;
881
882         /* within [start..max] find a uframe slot with enough bandwidth */
883         end %= mod;
884         do {
885                 unsigned        uframe;
886                 int             enough_space = 1;
887
888                 /* check schedule: enough space? */
889                 uframe = start;
890                 do {
891                         uframe %= mod;
892
893                         /* can't commit more than 80% periodic == 100 usec */
894                         if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
895                                         > (100 - stream->usecs)) {
896                                 enough_space = 0;
897                                 break;
898                         }
899
900                         /* we know urb->interval is 2^N uframes */
901                         uframe += urb->interval;
902                 } while (uframe != end);
903
904                 /* (re)schedule it here if there's enough bandwidth */
905                 if (enough_space) {
906                         start %= mod;
907                         if (unlikely (!list_empty (&stream->itd_list))) {
908                                 /* host fell behind ... maybe irq latencies
909                                  * delayed this request queue for too long.
910                                  */
911                                 stream->rescheduled++;
912                                 dev_dbg (&urb->dev->dev,
913                                         "iso%d%s %d.%d skip %d.%d\n",
914                                         stream->bEndpointAddress & 0x0f,
915                                         (stream->bEndpointAddress & USB_DIR_IN)
916                                                 ? "in" : "out",
917                                         stream->next_uframe >> 3,
918                                         stream->next_uframe & 0x7,
919                                         start >> 3, start & 0x7);
920                         }
921                         stream->next_uframe = start;
922                         goto ready;
923                 }
924
925         } while (++start < max);
926
927         /* no room in the schedule */
928         ehci_dbg (ehci, "iso %ssched full %p (now %d end %d max %d)\n",
929                 list_empty (&stream->itd_list) ? "" : "re",
930                 urb, now, end, max);
931         status = -ENOSPC;
932
933 fail:
934         itd_sched_free (stream, itd_sched);
935         urb->hcpriv = 0;
936         return status;
937
938 ready:
939         urb->start_frame = stream->next_uframe;
940         return 0;
941 }
942
943 /*-------------------------------------------------------------------------*/
944
945 static inline void
946 itd_init (struct ehci_iso_stream *stream, struct ehci_itd *itd)
947 {
948         int i;
949
950         itd->hw_next = EHCI_LIST_END;
951         itd->hw_bufp [0] = stream->buf0;
952         itd->hw_bufp [1] = stream->buf1;
953         itd->hw_bufp [2] = stream->buf2;
954
955         for (i = 0; i < 8; i++)
956                 itd->index[i] = -1;
957
958         /* All other fields are filled when scheduling */
959 }
960
961 static inline void
962 itd_patch (
963         struct ehci_itd         *itd,
964         struct ehci_itd_sched   *itd_sched,
965         unsigned                index,
966         u16                     uframe,
967         int                     first
968 )
969 {
970         struct ehci_iso_uframe  *uf = &itd_sched->packet [index];
971         unsigned                pg = itd->pg;
972
973         // BUG_ON (pg == 6 && uf->cross);
974
975         uframe &= 0x07;
976         itd->index [uframe] = index;
977
978         itd->hw_transaction [uframe] = uf->transaction;
979         itd->hw_transaction [uframe] |= cpu_to_le32 (pg << 12);
980         itd->hw_bufp [pg] |= cpu_to_le32 (uf->bufp & ~(u32)0);
981         itd->hw_bufp_hi [pg] |= cpu_to_le32 ((u32)(uf->bufp >> 32));
982
983         /* iso_frame_desc[].offset must be strictly increasing */
984         if (unlikely (!first && uf->cross)) {
985                 u64     bufp = uf->bufp + 4096;
986                 itd->pg = ++pg;
987                 itd->hw_bufp [pg] |= cpu_to_le32 (bufp & ~(u32)0);
988                 itd->hw_bufp_hi [pg] |= cpu_to_le32 ((u32)(bufp >> 32));
989         }
990 }
991
992 static inline void
993 itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
994 {
995         /* always prepend ITD/SITD ... only QH tree is order-sensitive */
996         itd->itd_next = ehci->pshadow [frame];
997         itd->hw_next = ehci->periodic [frame];
998         ehci->pshadow [frame].itd = itd;
999         itd->frame = frame;
1000         wmb ();
1001         ehci->periodic [frame] = cpu_to_le32 (itd->itd_dma) | Q_TYPE_ITD;
1002 }
1003
1004 /* fit urb's itds into the selected schedule slot; activate as needed */
1005 static int
1006 itd_link_urb (
1007         struct ehci_hcd         *ehci,
1008         struct urb              *urb,
1009         unsigned                mod,
1010         struct ehci_iso_stream  *stream
1011 )
1012 {
1013         int                     packet, first = 1;
1014         unsigned                next_uframe, uframe, frame;
1015         struct ehci_itd_sched   *itd_sched = urb->hcpriv;
1016         struct ehci_itd         *itd;
1017
1018         next_uframe = stream->next_uframe % mod;
1019
1020         if (unlikely (list_empty(&stream->itd_list))) {
1021                 hcd_to_bus (&ehci->hcd)->bandwidth_allocated
1022                                 += stream->bandwidth;
1023                 ehci_vdbg (ehci,
1024                         "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1025                         urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1026                         (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1027                         urb->interval,
1028                         next_uframe >> 3, next_uframe & 0x7);
1029                 stream->start = jiffies;
1030         }
1031         hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs++;
1032
1033         /* fill iTDs uframe by uframe */
1034         for (packet = 0, itd = 0; packet < urb->number_of_packets; ) {
1035                 if (itd == 0) {
1036                         /* ASSERT:  we have all necessary itds */
1037                         // BUG_ON (list_empty (&itd_sched->itd_list));
1038
1039                         /* ASSERT:  no itds for this endpoint in this uframe */
1040
1041                         itd = list_entry (itd_sched->itd_list.next,
1042                                         struct ehci_itd, itd_list);
1043                         list_move_tail (&itd->itd_list, &stream->itd_list);
1044                         itd->stream = iso_stream_get (stream);
1045                         itd->urb = usb_get_urb (urb);
1046                         first = 1;
1047                         itd_init (stream, itd);
1048                 }
1049
1050                 uframe = next_uframe & 0x07;
1051                 frame = next_uframe >> 3;
1052
1053                 itd->usecs [uframe] = stream->usecs;
1054                 itd_patch (itd, itd_sched, packet, uframe, first);
1055                 first = 0;
1056
1057                 next_uframe += stream->interval;
1058                 next_uframe %= mod;
1059                 packet++;
1060
1061                 /* link completed itds into the schedule */
1062                 if (((next_uframe >> 3) != frame)
1063                                 || packet == urb->number_of_packets) {
1064                         itd_link (ehci, frame % ehci->periodic_size, itd);
1065                         itd = 0;
1066                 }
1067         }
1068         stream->next_uframe = next_uframe;
1069
1070         /* don't need that schedule data any more */
1071         itd_sched_free (stream, itd_sched);
1072         urb->hcpriv = 0;
1073
1074         if (unlikely (!ehci->periodic_sched++))
1075                 return enable_periodic (ehci);
1076         return 0;
1077 }
1078
1079 #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1080
1081 static unsigned
1082 itd_complete (
1083         struct ehci_hcd *ehci,
1084         struct ehci_itd *itd,
1085         struct pt_regs  *regs
1086 ) {
1087         struct urb                              *urb = itd->urb;
1088         struct usb_iso_packet_descriptor        *desc;
1089         u32                                     t;
1090         unsigned                                uframe;
1091         int                                     urb_index = -1;
1092         struct ehci_iso_stream                  *stream = itd->stream;
1093         struct usb_device                       *dev;
1094
1095         /* for each uframe with a packet */
1096         for (uframe = 0; uframe < 8; uframe++) {
1097                 if (likely (itd->index[uframe] == -1))
1098                         continue;
1099                 urb_index = itd->index[uframe];
1100                 desc = &urb->iso_frame_desc [urb_index];
1101
1102                 t = le32_to_cpup (&itd->hw_transaction [uframe]);
1103                 itd->hw_transaction [uframe] = 0;
1104
1105                 /* report transfer status */
1106                 if (unlikely (t & ISO_ERRS)) {
1107                         urb->error_count++;
1108                         if (t & EHCI_ISOC_BUF_ERR)
1109                                 desc->status = usb_pipein (urb->pipe)
1110                                         ? -ENOSR  /* hc couldn't read */
1111                                         : -ECOMM; /* hc couldn't write */
1112                         else if (t & EHCI_ISOC_BABBLE)
1113                                 desc->status = -EOVERFLOW;
1114                         else /* (t & EHCI_ISOC_XACTERR) */
1115                                 desc->status = -EPROTO;
1116
1117                         /* HC need not update length with this error */
1118                         if (!(t & EHCI_ISOC_BABBLE))
1119                                 desc->actual_length = EHCI_ITD_LENGTH (t);
1120                 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1121                         desc->status = 0;
1122                         desc->actual_length = EHCI_ITD_LENGTH (t);
1123                 }
1124         }
1125
1126         usb_put_urb (urb);
1127         itd->urb = 0;
1128         itd->stream = 0;
1129         list_move (&itd->itd_list, &stream->free_itd_list);
1130         iso_stream_put (ehci, stream);
1131
1132         /* handle completion now? */
1133         if (likely ((urb_index + 1) != urb->number_of_packets))
1134                 return 0;
1135
1136         /* ASSERT: it's really the last itd for this urb
1137         list_for_each_entry (itd, &stream->itd_list, itd_list)
1138                 BUG_ON (itd->urb == urb);
1139          */
1140
1141         /* give urb back to the driver ... can be out-of-order */
1142         dev = usb_get_dev (urb->dev);
1143         ehci_urb_done (ehci, urb, regs);
1144         urb = 0;
1145
1146         /* defer stopping schedule; completion can submit */
1147         ehci->periodic_sched--;
1148         if (unlikely (!ehci->periodic_sched))
1149                 (void) disable_periodic (ehci);
1150         hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs--;
1151
1152         if (unlikely (list_empty (&stream->itd_list))) {
1153                 hcd_to_bus (&ehci->hcd)->bandwidth_allocated
1154                                 -= stream->bandwidth;
1155                 ehci_vdbg (ehci,
1156                         "deschedule devp %s ep%d%s-iso\n",
1157                         dev->devpath, stream->bEndpointAddress & 0x0f,
1158                         (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1159         }
1160         iso_stream_put (ehci, stream);
1161         usb_put_dev (dev);
1162
1163         return 1;
1164 }
1165
1166 /*-------------------------------------------------------------------------*/
1167
1168 static int itd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
1169 {
1170         int                     status = -EINVAL;
1171         unsigned long           flags;
1172         struct ehci_iso_stream  *stream;
1173
1174         /* Get iso_stream head */
1175         stream = iso_stream_find (ehci, urb);
1176         if (unlikely (stream == 0)) {
1177                 ehci_dbg (ehci, "can't get iso stream\n");
1178                 return -ENOMEM;
1179         }
1180         if (unlikely (urb->interval != stream->interval)) {
1181                 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1182                         stream->interval, urb->interval);
1183                 goto done;
1184         }
1185
1186 #ifdef EHCI_URB_TRACE
1187         ehci_dbg (ehci,
1188                 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
1189                 __FUNCTION__, urb->dev->devpath, urb,
1190                 usb_pipeendpoint (urb->pipe),
1191                 usb_pipein (urb->pipe) ? "in" : "out",
1192                 urb->transfer_buffer_length,
1193                 urb->number_of_packets, urb->interval,
1194                 stream);
1195 #endif
1196
1197         /* allocate ITDs w/o locking anything */
1198         status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1199         if (unlikely (status < 0)) {
1200                 ehci_dbg (ehci, "can't init itds\n");
1201                 goto done;
1202         }
1203
1204         /* schedule ... need to lock */
1205         spin_lock_irqsave (&ehci->lock, flags);
1206         status = itd_stream_schedule (ehci, urb, stream);
1207         if (likely (status == 0))
1208                 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
1209         spin_unlock_irqrestore (&ehci->lock, flags);
1210
1211 done:
1212         if (unlikely (status < 0))
1213                 iso_stream_put (ehci, stream);
1214         return status;
1215 }
1216
1217 #ifdef have_split_iso
1218
1219 /*-------------------------------------------------------------------------*/
1220
1221 /*
1222  * "Split ISO TDs" ... used for USB 1.1 devices going through
1223  * the TTs in USB 2.0 hubs.
1224  *
1225  * FIXME not yet implemented
1226  */
1227
1228 #endif /* have_split_iso */
1229
1230 /*-------------------------------------------------------------------------*/
1231
1232 static void
1233 scan_periodic (struct ehci_hcd *ehci, struct pt_regs *regs)
1234 {
1235         unsigned        frame, clock, now_uframe, mod;
1236         unsigned        count = 0;
1237
1238         mod = ehci->periodic_size << 3;
1239
1240         /*
1241          * When running, scan from last scan point up to "now"
1242          * else clean up by scanning everything that's left.
1243          * Touches as few pages as possible:  cache-friendly.
1244          */
1245         now_uframe = ehci->next_uframe;
1246         if (HCD_IS_RUNNING (ehci->hcd.state))
1247                 clock = readl (&ehci->regs->frame_index) % mod;
1248         else
1249                 clock = now_uframe + mod - 1;
1250
1251         for (;;) {
1252                 union ehci_shadow       q, *q_p;
1253                 u32                     type, *hw_p;
1254                 unsigned                uframes;
1255
1256                 frame = now_uframe >> 3;
1257 restart:
1258                 /* scan schedule to _before_ current frame index */
1259                 if ((frame == (clock >> 3))
1260                                 && HCD_IS_RUNNING (ehci->hcd.state))
1261                         uframes = now_uframe & 0x07;
1262                 else
1263                         uframes = 8;
1264
1265                 q_p = &ehci->pshadow [frame];
1266                 hw_p = &ehci->periodic [frame];
1267                 q.ptr = q_p->ptr;
1268                 type = Q_NEXT_TYPE (*hw_p);
1269
1270                 /* scan each element in frame's queue for completions */
1271                 while (q.ptr != 0) {
1272                         int                     last;
1273                         unsigned                uf;
1274                         union ehci_shadow       temp;
1275
1276                         switch (type) {
1277                         case Q_TYPE_QH:
1278                                 last = (q.qh->hw_next == EHCI_LIST_END);
1279                                 temp = q.qh->qh_next;
1280                                 type = Q_NEXT_TYPE (q.qh->hw_next);
1281                                 count += intr_complete (ehci, frame,
1282                                                 qh_get (q.qh), regs);
1283                                 qh_put (ehci, q.qh);
1284                                 q = temp;
1285                                 break;
1286                         case Q_TYPE_FSTN:
1287                                 last = (q.fstn->hw_next == EHCI_LIST_END);
1288                                 /* for "save place" FSTNs, look at QH entries
1289                                  * in the previous frame for completions.
1290                                  */
1291                                 if (q.fstn->hw_prev != EHCI_LIST_END) {
1292                                         dbg ("ignoring completions from FSTNs");
1293                                 }
1294                                 type = Q_NEXT_TYPE (q.fstn->hw_next);
1295                                 q = q.fstn->fstn_next;
1296                                 break;
1297                         case Q_TYPE_ITD:
1298                                 last = (q.itd->hw_next == EHCI_LIST_END);
1299
1300                                 /* skip itds for later in the frame */
1301                                 rmb ();
1302                                 for (uf = uframes; uf < 8; uf++) {
1303                                         if (0 == (q.itd->hw_transaction [uf]
1304                                                         & ISO_ACTIVE))
1305                                                 continue;
1306                                         q_p = &q.itd->itd_next;
1307                                         hw_p = &q.itd->hw_next;
1308                                         type = Q_NEXT_TYPE (q.itd->hw_next);
1309                                         q = *q_p;
1310                                         break;
1311                                 }
1312                                 if (uf != 8)
1313                                         break;
1314
1315                                 /* this one's ready ... HC won't cache the
1316                                  * pointer for much longer, if at all.
1317                                  */
1318                                 *q_p = q.itd->itd_next;
1319                                 *hw_p = q.itd->hw_next;
1320                                 wmb();
1321
1322                                 /* always rescan here; simpler */
1323                                 count += itd_complete (ehci, q.itd, regs);
1324                                 goto restart;
1325 #ifdef have_split_iso
1326                         case Q_TYPE_SITD:
1327                                 last = (q.sitd->hw_next == EHCI_LIST_END);
1328                                 sitd_complete (ehci, q.sitd);
1329                                 type = Q_NEXT_TYPE (q.sitd->hw_next);
1330
1331                                 // FIXME unlink SITD after split completes
1332                                 q = q.sitd->sitd_next;
1333                                 break;
1334 #endif /* have_split_iso */
1335                         default:
1336                                 dbg ("corrupt type %d frame %d shadow %p",
1337                                         type, frame, q.ptr);
1338                                 // BUG ();
1339                                 last = 1;
1340                                 q.ptr = 0;
1341                         }
1342
1343                         /* did completion remove an interior q entry? */
1344                         if (unlikely (q.ptr == 0 && !last))
1345                                 goto restart;
1346                 }
1347
1348                 /* stop when we catch up to the HC */
1349
1350                 // FIXME:  this assumes we won't get lapped when
1351                 // latencies climb; that should be rare, but...
1352                 // detect it, and just go all the way around.
1353                 // FLR might help detect this case, so long as latencies
1354                 // don't exceed periodic_size msec (default 1.024 sec).
1355
1356                 // FIXME:  likewise assumes HC doesn't halt mid-scan
1357
1358                 if (now_uframe == clock) {
1359                         unsigned        now;
1360
1361                         if (!HCD_IS_RUNNING (ehci->hcd.state))
1362                                 break;
1363                         ehci->next_uframe = now_uframe;
1364                         now = readl (&ehci->regs->frame_index) % mod;
1365                         if (now_uframe == now)
1366                                 break;
1367
1368                         /* rescan the rest of this frame, then ... */
1369                         clock = now;
1370                 } else {
1371                         /* FIXME sometimes we can scan the next frame
1372                          * right away, not always inching up on it ...
1373                          */
1374                         now_uframe++;
1375                         now_uframe %= mod;
1376                 }
1377         } 
1378 }