V4L/DVB (4236): Rearrange things in pvrusb2 driver in preparation for using cx2341x...
[powerpc.git] / drivers / usb / host / uhci-q.c
index 12af6fb..c9d72ac 100644 (file)
@@ -13,7 +13,7 @@
  * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
  *               support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
  * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
- * (C) Copyright 2004-2005 Alan Stern, stern@rowland.harvard.edu
+ * (C) Copyright 2004-2006 Alan Stern, stern@rowland.harvard.edu
  */
 
 
@@ -37,6 +37,60 @@ static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
        uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
 }
 
+
+/*
+ * Full-Speed Bandwidth Reclamation (FSBR).
+ * We turn on FSBR whenever a queue that wants it is advancing,
+ * and leave it on for a short time thereafter.
+ */
+static void uhci_fsbr_on(struct uhci_hcd *uhci)
+{
+       uhci->fsbr_is_on = 1;
+       uhci->skel_term_qh->link = cpu_to_le32(
+                       uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
+}
+
+static void uhci_fsbr_off(struct uhci_hcd *uhci)
+{
+       uhci->fsbr_is_on = 0;
+       uhci->skel_term_qh->link = UHCI_PTR_TERM;
+}
+
+static void uhci_add_fsbr(struct uhci_hcd *uhci, struct urb *urb)
+{
+       struct urb_priv *urbp = urb->hcpriv;
+
+       if (!(urb->transfer_flags & URB_NO_FSBR))
+               urbp->fsbr = 1;
+}
+
+static void uhci_urbp_wants_fsbr(struct uhci_hcd *uhci, struct urb_priv *urbp)
+{
+       if (urbp->fsbr) {
+               uhci->fsbr_is_wanted = 1;
+               if (!uhci->fsbr_is_on)
+                       uhci_fsbr_on(uhci);
+               else if (uhci->fsbr_expiring) {
+                       uhci->fsbr_expiring = 0;
+                       del_timer(&uhci->fsbr_timer);
+               }
+       }
+}
+
+static void uhci_fsbr_timeout(unsigned long _uhci)
+{
+       struct uhci_hcd *uhci = (struct uhci_hcd *) _uhci;
+       unsigned long flags;
+
+       spin_lock_irqsave(&uhci->lock, flags);
+       if (uhci->fsbr_expiring) {
+               uhci->fsbr_expiring = 0;
+               uhci_fsbr_off(uhci);
+       }
+       spin_unlock_irqrestore(&uhci->lock, flags);
+}
+
+
 static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
 {
        dma_addr_t dma_handle;
@@ -144,6 +198,24 @@ static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
        td->frame = -1;
 }
 
+static inline void uhci_remove_tds_from_frame(struct uhci_hcd *uhci,
+               unsigned int framenum)
+{
+       struct uhci_td *ftd, *ltd;
+
+       framenum &= (UHCI_NUMFRAMES - 1);
+
+       ftd = uhci->frame_cpu[framenum];
+       if (ftd) {
+               ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
+               uhci->frame[framenum] = ltd->link;
+               uhci->frame_cpu[framenum] = NULL;
+
+               while (!list_empty(&ftd->fl_list))
+                       list_del_init(ftd->fl_list.prev);
+       }
+}
+
 /*
  * Remove all the TDs for an Isochronous URB from the frame list
  */
@@ -154,7 +226,6 @@ static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
 
        list_for_each_entry(td, &urbp->td_list, list)
                uhci_remove_td_from_frame_list(uhci, td);
-       wmb();
 }
 
 static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
@@ -213,17 +284,25 @@ static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
  * When a queue is stopped and a dequeued URB is given back, adjust
  * the previous TD link (if the URB isn't first on the queue) or
  * save its toggle value (if it is first and is currently executing).
+ *
+ * Returns 0 if the URB should not yet be given back, 1 otherwise.
  */
-static void uhci_cleanup_queue(struct uhci_qh *qh,
+static int uhci_cleanup_queue(struct uhci_hcd *uhci, struct uhci_qh *qh,
                struct urb *urb)
 {
        struct urb_priv *urbp = urb->hcpriv;
        struct uhci_td *td;
+       int ret = 1;
 
        /* Isochronous pipes don't use toggles and their TD link pointers
-        * get adjusted during uhci_urb_dequeue(). */
-       if (qh->type == USB_ENDPOINT_XFER_ISOC)
-               return;
+        * get adjusted during uhci_urb_dequeue().  But since their queues
+        * cannot truly be stopped, we have to watch out for dequeues
+        * occurring after the nominal unlink frame. */
+       if (qh->type == USB_ENDPOINT_XFER_ISOC) {
+               ret = (uhci->frame_number + uhci->is_stopped !=
+                               qh->unlink_frame);
+               goto done;
+       }
 
        /* If the URB isn't first on its queue, adjust the link pointer
         * of the last TD in the previous URB.  The toggle doesn't need
@@ -239,24 +318,27 @@ static void uhci_cleanup_queue(struct uhci_qh *qh,
                td = list_entry(urbp->td_list.prev, struct uhci_td,
                                list);
                ptd->link = td->link;
-               return;
+               goto done;
        }
 
        /* If the QH element pointer is UHCI_PTR_TERM then then currently
         * executing URB has already been unlinked, so this one isn't it. */
        if (qh_element(qh) == UHCI_PTR_TERM)
-               return;
+               goto done;
        qh->element = UHCI_PTR_TERM;
 
        /* Control pipes have to worry about toggles */
        if (qh->type == USB_ENDPOINT_XFER_CONTROL)
-               return;
+               goto done;
 
        /* Save the next toggle value */
        WARN_ON(list_empty(&urbp->td_list));
        td = list_entry(urbp->td_list.next, struct uhci_td, list);
        qh->needs_fixup = 1;
        qh->initial_toggle = uhci_toggle(td_token(td));
+
+done:
+       return ret;
 }
 
 /*
@@ -331,6 +413,10 @@ static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
                qh->element = cpu_to_le32(td->dma_handle);
        }
 
+       /* Treat the queue as if it has just advanced */
+       qh->wait_expired = 0;
+       qh->advance_jiffies = jiffies;
+
        if (qh->state == QH_STATE_ACTIVE)
                return;
        qh->state = QH_STATE_ACTIVE;
@@ -445,28 +531,6 @@ static void uhci_free_urb_priv(struct uhci_hcd *uhci,
        kmem_cache_free(uhci_up_cachep, urbp);
 }
 
-static void uhci_inc_fsbr(struct uhci_hcd *uhci, struct urb *urb)
-{
-       struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
-
-       if ((!(urb->transfer_flags & URB_NO_FSBR)) && !urbp->fsbr) {
-               urbp->fsbr = 1;
-               if (!uhci->fsbr++ && !uhci->fsbrtimeout)
-                       uhci->skel_term_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH;
-       }
-}
-
-static void uhci_dec_fsbr(struct uhci_hcd *uhci, struct urb *urb)
-{
-       struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
-
-       if ((!(urb->transfer_flags & URB_NO_FSBR)) && urbp->fsbr) {
-               urbp->fsbr = 0;
-               if (!--uhci->fsbr)
-                       uhci->fsbrtimeout = jiffies + FSBR_DELAY;
-       }
-}
-
 /*
  * Map status to standard result codes
  *
@@ -493,7 +557,6 @@ static int uhci_map_status(int status, int dir_out)
                return -ENOSR;
        if (status & TD_CTRL_STALLED)                   /* Stalled */
                return -EPIPE;
-       WARN_ON(status & TD_CTRL_ACTIVE);               /* Active */
        return 0;
 }
 
@@ -613,7 +676,7 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
                qh->skel = uhci->skel_ls_control_qh;
        else {
                qh->skel = uhci->skel_fs_control_qh;
-               uhci_inc_fsbr(uhci, urb);
+               uhci_add_fsbr(uhci, urb);
        }
 
        urb->actual_length = -8;        /* Account for the SETUP packet */
@@ -733,6 +796,7 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
        wmb();
        qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
        qh->dummy_td = td;
+       qh->period = urb->interval;
 
        usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
                        usb_pipeout(urb->pipe), toggle);
@@ -756,18 +820,34 @@ static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
        qh->skel = uhci->skel_bulk_qh;
        ret = uhci_submit_common(uhci, urb, qh);
        if (ret == 0)
-               uhci_inc_fsbr(uhci, urb);
+               uhci_add_fsbr(uhci, urb);
        return ret;
 }
 
-static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
+static int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
                struct uhci_qh *qh)
 {
+       int exponent;
+
        /* USB 1.1 interrupt transfers only involve one packet per interval.
         * Drivers can submit URBs of any length, but longer ones will need
         * multiple intervals to complete.
         */
-       qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
+
+       /* Figure out which power-of-two queue to use */
+       for (exponent = 7; exponent >= 0; --exponent) {
+               if ((1 << exponent) <= urb->interval)
+                       break;
+       }
+       if (exponent < 0)
+               return -EINVAL;
+       urb->interval = 1 << exponent;
+
+       if (qh->period == 0)
+               qh->skel = uhci->skelqh[UHCI_SKEL_INDEX(exponent)];
+       else if (qh->period != urb->interval)
+               return -EINVAL;         /* Can't change the period */
+
        return uhci_submit_common(uhci, urb, qh);
 }
 
@@ -846,7 +926,7 @@ static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
                                        uhci_packetout(td_token(td)));
                        if ((debug == 1 && ret != -EPIPE) || debug > 1) {
                                /* Some debugging code */
-                               dev_dbg(uhci_dev(uhci),
+                               dev_dbg(&urb->dev->dev,
                                                "%s: failed with status %x\n",
                                                __FUNCTION__, status);
 
@@ -907,31 +987,50 @@ static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
        unsigned long destination, status;
        struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
 
-       if (urb->number_of_packets > 900)       /* 900? Why? */
+       /* Values must not be too big (could overflow below) */
+       if (urb->interval >= UHCI_NUMFRAMES ||
+                       urb->number_of_packets >= UHCI_NUMFRAMES)
                return -EFBIG;
 
-       status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
-       destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
-
-       /* Figure out the starting frame number */
-       if (urb->transfer_flags & URB_ISO_ASAP) {
-               if (list_empty(&qh->queue)) {
+       /* Check the period and figure out the starting frame number */
+       if (qh->period == 0) {
+               if (urb->transfer_flags & URB_ISO_ASAP) {
                        uhci_get_current_frame_number(uhci);
-                       urb->start_frame = (uhci->frame_number + 10);
+                       urb->start_frame = uhci->frame_number + 10;
+               } else {
+                       i = urb->start_frame - uhci->last_iso_frame;
+                       if (i <= 0 || i >= UHCI_NUMFRAMES)
+                               return -EINVAL;
+               }
+       } else if (qh->period != urb->interval) {
+               return -EINVAL;         /* Can't change the period */
 
-               } else {                /* Go right after the last one */
-                       struct urb *last_urb;
+       } else {        /* Pick up where the last URB leaves off */
+               if (list_empty(&qh->queue)) {
+                       frame = qh->iso_frame;
+               } else {
+                       struct urb *lurb;
 
-                       last_urb = list_entry(qh->queue.prev,
+                       lurb = list_entry(qh->queue.prev,
                                        struct urb_priv, node)->urb;
-                       urb->start_frame = (last_urb->start_frame +
-                                       last_urb->number_of_packets *
-                                       last_urb->interval);
+                       frame = lurb->start_frame +
+                                       lurb->number_of_packets *
+                                       lurb->interval;
                }
-       } else {
-               /* FIXME: Sanity check */
+               if (urb->transfer_flags & URB_ISO_ASAP)
+                       urb->start_frame = frame;
+               else if (urb->start_frame != frame)
+                       return -EINVAL;
        }
-       urb->start_frame &= (UHCI_NUMFRAMES - 1);
+
+       /* Make sure we won't have to go too far into the future */
+       if (uhci_frame_before_eq(uhci->last_iso_frame + UHCI_NUMFRAMES,
+                       urb->start_frame + urb->number_of_packets *
+                               urb->interval))
+               return -EFBIG;
+
+       status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
+       destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
 
        for (i = 0; i < urb->number_of_packets; i++) {
                td = uhci_alloc_td(uhci);
@@ -949,12 +1048,19 @@ static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
        td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
 
        qh->skel = uhci->skel_iso_qh;
+       qh->period = urb->interval;
 
        /* Add the TDs to the frame list */
        frame = urb->start_frame;
        list_for_each_entry(td, &urbp->td_list, list) {
                uhci_insert_td_in_frame_list(uhci, td, frame);
-               frame += urb->interval;
+               frame += qh->period;
+       }
+
+       if (list_empty(&qh->queue)) {
+               qh->iso_packet_desc = &urb->iso_frame_desc[0];
+               qh->iso_frame = urb->start_frame;
+               qh->iso_status = 0;
        }
 
        return 0;
@@ -962,37 +1068,44 @@ static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
 
 static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
 {
-       struct uhci_td *td;
-       struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
-       int status;
-       int i, ret = 0;
-
-       urb->actual_length = urb->error_count = 0;
+       struct uhci_td *td, *tmp;
+       struct urb_priv *urbp = urb->hcpriv;
+       struct uhci_qh *qh = urbp->qh;
 
-       i = 0;
-       list_for_each_entry(td, &urbp->td_list, list) {
+       list_for_each_entry_safe(td, tmp, &urbp->td_list, list) {
+               unsigned int ctrlstat;
+               int status;
                int actlength;
-               unsigned int ctrlstat = td_status(td);
 
-               if (ctrlstat & TD_CTRL_ACTIVE)
+               if (uhci_frame_before_eq(uhci->cur_iso_frame, qh->iso_frame))
                        return -EINPROGRESS;
 
-               actlength = uhci_actual_length(ctrlstat);
-               urb->iso_frame_desc[i].actual_length = actlength;
-               urb->actual_length += actlength;
+               uhci_remove_tds_from_frame(uhci, qh->iso_frame);
+
+               ctrlstat = td_status(td);
+               if (ctrlstat & TD_CTRL_ACTIVE) {
+                       status = -EXDEV;        /* TD was added too late? */
+               } else {
+                       status = uhci_map_status(uhci_status_bits(ctrlstat),
+                                       usb_pipeout(urb->pipe));
+                       actlength = uhci_actual_length(ctrlstat);
+
+                       urb->actual_length += actlength;
+                       qh->iso_packet_desc->actual_length = actlength;
+                       qh->iso_packet_desc->status = status;
+               }
 
-               status = uhci_map_status(uhci_status_bits(ctrlstat),
-                               usb_pipeout(urb->pipe));
-               urb->iso_frame_desc[i].status = status;
                if (status) {
                        urb->error_count++;
-                       ret = status;
+                       qh->iso_status = status;
                }
 
-               i++;
+               uhci_remove_td_from_urbp(td);
+               uhci_free_td(uhci, td);
+               qh->iso_frame += qh->period;
+               ++qh->iso_packet_desc;
        }
-
-       return ret;
+       return qh->iso_status;
 }
 
 static int uhci_urb_enqueue(struct usb_hcd *hcd,
@@ -1053,6 +1166,7 @@ static int uhci_urb_enqueue(struct usb_hcd *hcd,
                }
                break;
        case USB_ENDPOINT_XFER_ISOC:
+               urb->error_count = 0;
                bustime = usb_check_bandwidth(urb->dev, urb);
                if (bustime < 0) {
                        ret = bustime;
@@ -1075,8 +1189,10 @@ static int uhci_urb_enqueue(struct usb_hcd *hcd,
         * the QH is new and idle or else it's unlinked and waiting to
         * become idle, so we can activate it right away.  But only if the
         * queue isn't stopped. */
-       if (qh->queue.next == &urbp->node && !qh->is_stopped)
+       if (qh->queue.next == &urbp->node && !qh->is_stopped) {
                uhci_activate_qh(uhci, qh);
+               uhci_urbp_wants_fsbr(uhci, urbp);
+       }
        goto done;
 
 err_submit_failed:
@@ -1096,16 +1212,26 @@ static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
        struct uhci_hcd *uhci = hcd_to_uhci(hcd);
        unsigned long flags;
        struct urb_priv *urbp;
+       struct uhci_qh *qh;
 
        spin_lock_irqsave(&uhci->lock, flags);
        urbp = urb->hcpriv;
        if (!urbp)                      /* URB was never linked! */
                goto done;
+       qh = urbp->qh;
 
        /* Remove Isochronous TDs from the frame list ASAP */
-       if (urbp->qh->type == USB_ENDPOINT_XFER_ISOC)
+       if (qh->type == USB_ENDPOINT_XFER_ISOC) {
                uhci_unlink_isochronous_tds(uhci, urb);
-       uhci_unlink_qh(uhci, urbp->qh);
+               mb();
+
+               /* If the URB has already started, update the QH unlink time */
+               uhci_get_current_frame_number(uhci);
+               if (uhci_frame_before_eq(urb->start_frame, uhci->frame_number))
+                       qh->unlink_frame = uhci->frame_number;
+       }
+
+       uhci_unlink_qh(uhci, qh);
 
 done:
        spin_unlock_irqrestore(&uhci->lock, flags);
@@ -1122,9 +1248,18 @@ __acquires(uhci->lock)
 {
        struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
 
-       /* Isochronous TDs get unlinked directly from the frame list */
-       if (qh->type == USB_ENDPOINT_XFER_ISOC)
-               uhci_unlink_isochronous_tds(uhci, urb);
+       /* When giving back the first URB in an Isochronous queue,
+        * reinitialize the QH's iso-related members for the next URB. */
+       if (qh->type == USB_ENDPOINT_XFER_ISOC &&
+                       urbp->node.prev == &qh->queue &&
+                       urbp->node.next != &qh->queue) {
+               struct urb *nurb = list_entry(urbp->node.next,
+                               struct urb_priv, node)->urb;
+
+               qh->iso_packet_desc = &nurb->iso_frame_desc[0];
+               qh->iso_frame = nurb->start_frame;
+               qh->iso_status = 0;
+       }
 
        /* Take the URB off the QH's queue.  If the queue is now empty,
         * this is a perfect time for a toggle fixup. */
@@ -1135,7 +1270,6 @@ __acquires(uhci->lock)
                qh->needs_fixup = 0;
        }
 
-       uhci_dec_fsbr(uhci, urb);       /* Safe since it checks */
        uhci_free_urb_priv(uhci, urbp);
 
        switch (qh->type) {
@@ -1166,6 +1300,7 @@ __acquires(uhci->lock)
                uhci_unlink_qh(uhci, qh);
 
                /* Bandwidth stuff not yet implemented */
+               qh->period = 0;
        }
 }
 
@@ -1227,7 +1362,14 @@ restart:
        list_for_each_entry(urbp, &qh->queue, node) {
                urb = urbp->urb;
                if (urb->status != -EINPROGRESS) {
-                       uhci_cleanup_queue(qh, urb);
+
+                       /* Fix up the TD links and save the toggles for
+                        * non-Isochronous queues.  For Isochronous queues,
+                        * test for too-recent dequeues. */
+                       if (!uhci_cleanup_queue(uhci, qh, urb)) {
+                               qh->is_stopped = 0;
+                               return;
+                       }
                        uhci_giveback_urb(uhci, qh, urb, regs);
                        goto restart;
                }
@@ -1239,6 +1381,18 @@ restart:
        if (!list_empty(&qh->queue)) {
                if (qh->needs_fixup)
                        uhci_fixup_toggles(qh, 0);
+
+               /* If the first URB on the queue wants FSBR but its time
+                * limit has expired, set the next TD to interrupt on
+                * completion before reactivating the QH. */
+               urbp = list_entry(qh->queue.next, struct urb_priv, node);
+               if (urbp->fsbr && qh->wait_expired) {
+                       struct uhci_td *td = list_entry(urbp->td_list.next,
+                                       struct uhci_td, list);
+
+                       td->status |= __cpu_to_le32(TD_CTRL_IOC);
+               }
+
                uhci_activate_qh(uhci, qh);
        }
 
@@ -1248,6 +1402,86 @@ restart:
                uhci_make_qh_idle(uhci, qh);
 }
 
+/*
+ * Check for queues that have made some forward progress.
+ * Returns 0 if the queue is not Isochronous, is ACTIVE, and
+ * has not advanced since last examined; 1 otherwise.
+ *
+ * Early Intel controllers have a bug which causes qh->element sometimes
+ * not to advance when a TD completes successfully.  The queue remains
+ * stuck on the inactive completed TD.  We detect such cases and advance
+ * the element pointer by hand.
+ */
+static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh)
+{
+       struct urb_priv *urbp = NULL;
+       struct uhci_td *td;
+       int ret = 1;
+       unsigned status;
+
+       if (qh->type == USB_ENDPOINT_XFER_ISOC)
+               goto done;
+
+       /* Treat an UNLINKING queue as though it hasn't advanced.
+        * This is okay because reactivation will treat it as though
+        * it has advanced, and if it is going to become IDLE then
+        * this doesn't matter anyway.  Furthermore it's possible
+        * for an UNLINKING queue not to have any URBs at all, or
+        * for its first URB not to have any TDs (if it was dequeued
+        * just as it completed).  So it's not easy in any case to
+        * test whether such queues have advanced. */
+       if (qh->state != QH_STATE_ACTIVE) {
+               urbp = NULL;
+               status = 0;
+
+       } else {
+               urbp = list_entry(qh->queue.next, struct urb_priv, node);
+               td = list_entry(urbp->td_list.next, struct uhci_td, list);
+               status = td_status(td);
+               if (!(status & TD_CTRL_ACTIVE)) {
+
+                       /* We're okay, the queue has advanced */
+                       qh->wait_expired = 0;
+                       qh->advance_jiffies = jiffies;
+                       goto done;
+               }
+               ret = 0;
+       }
+
+       /* The queue hasn't advanced; check for timeout */
+       if (qh->wait_expired)
+               goto done;
+
+       if (time_after(jiffies, qh->advance_jiffies + QH_WAIT_TIMEOUT)) {
+
+               /* Detect the Intel bug and work around it */
+               if (qh->post_td && qh_element(qh) ==
+                               cpu_to_le32(qh->post_td->dma_handle)) {
+                       qh->element = qh->post_td->link;
+                       qh->advance_jiffies = jiffies;
+                       ret = 1;
+                       goto done;
+               }
+
+               qh->wait_expired = 1;
+
+               /* If the current URB wants FSBR, unlink it temporarily
+                * so that we can safely set the next TD to interrupt on
+                * completion.  That way we'll know as soon as the queue
+                * starts moving again. */
+               if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC))
+                       uhci_unlink_qh(uhci, qh);
+
+       } else {
+               /* Unmoving but not-yet-expired queues keep FSBR alive */
+               if (urbp)
+                       uhci_urbp_wants_fsbr(uhci, urbp);
+       }
+
+done:
+       return ret;
+}
+
 /*
  * Process events in the schedule, but only in one thread at a time
  */
@@ -1262,11 +1496,13 @@ static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
                return;
        }
        uhci->scan_in_progress = 1;
- rescan:
+rescan:
        uhci->need_rescan = 0;
+       uhci->fsbr_is_wanted = 0;
 
        uhci_clear_next_interrupt(uhci);
        uhci_get_current_frame_number(uhci);
+       uhci->cur_iso_frame = uhci->frame_number;
 
        /* Go through all the QH queues and process the URBs in each one */
        for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
@@ -1275,28 +1511,30 @@ static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
                while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
                        uhci->next_qh = list_entry(qh->node.next,
                                        struct uhci_qh, node);
-                       uhci_scan_qh(uhci, qh, regs);
+
+                       if (uhci_advance_check(uhci, qh)) {
+                               uhci_scan_qh(uhci, qh, regs);
+                               if (qh->state == QH_STATE_ACTIVE) {
+                                       uhci_urbp_wants_fsbr(uhci,
+       list_entry(qh->queue.next, struct urb_priv, node));
+                               }
+                       }
                }
        }
 
+       uhci->last_iso_frame = uhci->cur_iso_frame;
        if (uhci->need_rescan)
                goto rescan;
        uhci->scan_in_progress = 0;
 
+       if (uhci->fsbr_is_on && !uhci->fsbr_is_wanted &&
+                       !uhci->fsbr_expiring) {
+               uhci->fsbr_expiring = 1;
+               mod_timer(&uhci->fsbr_timer, jiffies + FSBR_OFF_DELAY);
+       }
+
        if (list_empty(&uhci->skel_unlink_qh->node))
                uhci_clear_next_interrupt(uhci);
        else
                uhci_set_next_interrupt(uhci);
 }
-
-static void check_fsbr(struct uhci_hcd *uhci)
-{
-       /* For now, don't scan URBs for FSBR timeouts.
-        * Add it back in later... */
-
-       /* Really disable FSBR */
-       if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) {
-               uhci->fsbrtimeout = 0;
-               uhci->skel_term_qh->link = UHCI_PTR_TERM;
-       }
-}