fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / usb / host / ehci-sched.c
index 3cdfb95..65c402a 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2001-2004 by David Brownell
  * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers
- * 
+ *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
  * Free Software Foundation; either version 2 of the License, or (at your
@@ -163,6 +163,190 @@ static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
                return 1;
 }
 
+#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
+
+/* Which uframe does the low/fullspeed transfer start in?
+ *
+ * The parameter is the mask of ssplits in "H-frame" terms
+ * and this returns the transfer start uframe in "B-frame" terms,
+ * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
+ * will cause a transfer in "B-frame" uframe 0.  "B-frames" lag
+ * "H-frames" by 1 uframe.  See the EHCI spec sec 4.5 and figure 4.7.
+ */
+static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __le32 mask)
+{
+       unsigned char smask = QH_SMASK & le32_to_cpu(mask);
+       if (!smask) {
+               ehci_err(ehci, "invalid empty smask!\n");
+               /* uframe 7 can't have bw so this will indicate failure */
+               return 7;
+       }
+       return ffs(smask) - 1;
+}
+
+static const unsigned char
+max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
+
+/* carryover low/fullspeed bandwidth that crosses uframe boundries */
+static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
+{
+       int i;
+       for (i=0; i<7; i++) {
+               if (max_tt_usecs[i] < tt_usecs[i]) {
+                       tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i];
+                       tt_usecs[i] = max_tt_usecs[i];
+               }
+       }
+}
+
+/* How many of the tt's periodic downstream 1000 usecs are allocated?
+ *
+ * While this measures the bandwidth in terms of usecs/uframe,
+ * the low/fullspeed bus has no notion of uframes, so any particular
+ * low/fullspeed transfer can "carry over" from one uframe to the next,
+ * since the TT just performs downstream transfers in sequence.
+ *
+ * For example two seperate 100 usec transfers can start in the same uframe,
+ * and the second one would "carry over" 75 usecs into the next uframe.
+ */
+static void
+periodic_tt_usecs (
+       struct ehci_hcd *ehci,
+       struct usb_device *dev,
+       unsigned frame,
+       unsigned short tt_usecs[8]
+)
+{
+       __le32                  *hw_p = &ehci->periodic [frame];
+       union ehci_shadow       *q = &ehci->pshadow [frame];
+       unsigned char           uf;
+
+       memset(tt_usecs, 0, 16);
+
+       while (q->ptr) {
+               switch (Q_NEXT_TYPE(*hw_p)) {
+               case Q_TYPE_ITD:
+                       hw_p = &q->itd->hw_next;
+                       q = &q->itd->itd_next;
+                       continue;
+               case Q_TYPE_QH:
+                       if (same_tt(dev, q->qh->dev)) {
+                               uf = tt_start_uframe(ehci, q->qh->hw_info2);
+                               tt_usecs[uf] += q->qh->tt_usecs;
+                       }
+                       hw_p = &q->qh->hw_next;
+                       q = &q->qh->qh_next;
+                       continue;
+               case Q_TYPE_SITD:
+                       if (same_tt(dev, q->sitd->urb->dev)) {
+                               uf = tt_start_uframe(ehci, q->sitd->hw_uframe);
+                               tt_usecs[uf] += q->sitd->stream->tt_usecs;
+                       }
+                       hw_p = &q->sitd->hw_next;
+                       q = &q->sitd->sitd_next;
+                       continue;
+               // case Q_TYPE_FSTN:
+               default:
+                       ehci_dbg(ehci,
+                                 "ignoring periodic frame %d FSTN\n", frame);
+                       hw_p = &q->fstn->hw_next;
+                       q = &q->fstn->fstn_next;
+               }
+       }
+
+       carryover_tt_bandwidth(tt_usecs);
+
+       if (max_tt_usecs[7] < tt_usecs[7])
+               ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
+                       frame, tt_usecs[7] - max_tt_usecs[7]);
+}
+
+/*
+ * Return true if the device's tt's downstream bus is available for a
+ * periodic transfer of the specified length (usecs), starting at the
+ * specified frame/uframe.  Note that (as summarized in section 11.19
+ * of the usb 2.0 spec) TTs can buffer multiple transactions for each
+ * uframe.
+ *
+ * The uframe parameter is when the fullspeed/lowspeed transfer
+ * should be executed in "B-frame" terms, which is the same as the
+ * highspeed ssplit's uframe (which is in "H-frame" terms).  For example
+ * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
+ * See the EHCI spec sec 4.5 and fig 4.7.
+ *
+ * This checks if the full/lowspeed bus, at the specified starting uframe,
+ * has the specified bandwidth available, according to rules listed
+ * in USB 2.0 spec section 11.18.1 fig 11-60.
+ *
+ * This does not check if the transfer would exceed the max ssplit
+ * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
+ * since proper scheduling limits ssplits to less than 16 per uframe.
+ */
+static int tt_available (
+       struct ehci_hcd         *ehci,
+       unsigned                period,
+       struct usb_device       *dev,
+       unsigned                frame,
+       unsigned                uframe,
+       u16                     usecs
+)
+{
+       if ((period == 0) || (uframe >= 7))     /* error */
+               return 0;
+
+       for (; frame < ehci->periodic_size; frame += period) {
+               unsigned short tt_usecs[8];
+
+               periodic_tt_usecs (ehci, dev, frame, tt_usecs);
+
+               ehci_vdbg(ehci, "tt frame %d check %d usecs start uframe %d in"
+                       " schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
+                       frame, usecs, uframe,
+                       tt_usecs[0], tt_usecs[1], tt_usecs[2], tt_usecs[3],
+                       tt_usecs[4], tt_usecs[5], tt_usecs[6], tt_usecs[7]);
+
+               if (max_tt_usecs[uframe] <= tt_usecs[uframe]) {
+                       ehci_vdbg(ehci, "frame %d uframe %d fully scheduled\n",
+                               frame, uframe);
+                       return 0;
+               }
+
+               /* special case for isoc transfers larger than 125us:
+                * the first and each subsequent fully used uframe
+                * must be empty, so as to not illegally delay
+                * already scheduled transactions
+                */
+               if (125 < usecs) {
+                       int ufs = (usecs / 125) - 1;
+                       int i;
+                       for (i = uframe; i < (uframe + ufs) && i < 8; i++)
+                               if (0 < tt_usecs[i]) {
+                                       ehci_vdbg(ehci,
+                                               "multi-uframe xfer can't fit "
+                                               "in frame %d uframe %d\n",
+                                               frame, i);
+                                       return 0;
+                               }
+               }
+
+               tt_usecs[uframe] += usecs;
+
+               carryover_tt_bandwidth(tt_usecs);
+
+               /* fail if the carryover pushed bw past the last uframe's limit */
+               if (max_tt_usecs[7] < tt_usecs[7]) {
+                       ehci_vdbg(ehci,
+                               "tt unavailable usecs %d frame %d uframe %d\n",
+                               usecs, frame, uframe);
+                       return 0;
+               }
+       }
+
+       return 1;
+}
+
+#else
+
 /* return true iff the device's transaction translator is available
  * for a periodic transfer starting at the specified frame, using
  * all the uframes in the mask.
@@ -208,7 +392,7 @@ static int tt_no_collision (
                                here = here.qh->qh_next;
                                continue;
                        case Q_TYPE_SITD:
-                               if (same_tt (dev, here.itd->urb->dev)) {
+                               if (same_tt (dev, here.sitd->urb->dev)) {
                                        u16             mask;
 
                                        mask = le32_to_cpu (here.sitd
@@ -218,7 +402,7 @@ static int tt_no_collision (
                                        if (mask & uf_mask)
                                                break;
                                }
-                               type = Q_NEXT_TYPE (here.qh->hw_next);
+                               type = Q_NEXT_TYPE (here.sitd->hw_next);
                                here = here.sitd->sitd_next;
                                continue;
                        // case Q_TYPE_FSTN:
@@ -237,6 +421,8 @@ static int tt_no_collision (
        return 1;
 }
 
+#endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
+
 /*-------------------------------------------------------------------------*/
 
 static int enable_periodic (struct ehci_hcd *ehci)
@@ -249,14 +435,14 @@ static int enable_periodic (struct ehci_hcd *ehci)
         */
        status = handshake (&ehci->regs->status, STS_PSS, 0, 9 * 125);
        if (status != 0) {
-               ehci_to_hcd(ehci)->state = USB_STATE_HALT;
+               ehci_to_hcd(ehci)->state = HC_STATE_HALT;
                return status;
        }
 
        cmd = readl (&ehci->regs->command) | CMD_PSE;
        writel (cmd, &ehci->regs->command);
        /* posted write ... PSS happens later */
-       ehci_to_hcd(ehci)->state = USB_STATE_RUNNING;
+       ehci_to_hcd(ehci)->state = HC_STATE_RUNNING;
 
        /* make sure ehci_work scans these */
        ehci->next_uframe = readl (&ehci->regs->frame_index)
@@ -274,7 +460,7 @@ static int disable_periodic (struct ehci_hcd *ehci)
         */
        status = handshake (&ehci->regs->status, STS_PSS, STS_PSS, 9 * 125);
        if (status != 0) {
-               ehci_to_hcd(ehci)->state = USB_STATE_HALT;
+               ehci_to_hcd(ehci)->state = HC_STATE_HALT;
                return status;
        }
 
@@ -301,7 +487,7 @@ static int qh_link_periodic (struct ehci_hcd *ehci, struct ehci_qh *qh)
 
        dev_dbg (&qh->dev->dev,
                "link qh%d-%04x/%p start %d [%d/%d us]\n",
-               period, le32_to_cpup (&qh->hw_info2) & 0xffff,
+               period, le32_to_cpup (&qh->hw_info2) & (QH_CMASK | QH_SMASK),
                qh, qh->start, qh->usecs, qh->c_usecs);
 
        /* high bandwidth, or otherwise every microframe */
@@ -310,9 +496,9 @@ static int qh_link_periodic (struct ehci_hcd *ehci, struct ehci_qh *qh)
 
        for (i = qh->start; i < ehci->periodic_size; i += period) {
                union ehci_shadow       *prev = &ehci->pshadow [i];
-               u32                     *hw_p = &ehci->periodic [i];
+               __le32                  *hw_p = &ehci->periodic [i];
                union ehci_shadow       here = *prev;
-               u32                     type = 0;
+               __le32                  type = 0;
 
                /* skip the iso nodes at list head */
                while (here.ptr) {
@@ -385,7 +571,8 @@ static void qh_unlink_periodic (struct ehci_hcd *ehci, struct ehci_qh *qh)
 
        dev_dbg (&qh->dev->dev,
                "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
-               qh->period, le32_to_cpup (&qh->hw_info2) & 0xffff,
+               qh->period,
+               le32_to_cpup (&qh->hw_info2) & (QH_CMASK | QH_SMASK),
                qh, qh->start, qh->usecs, qh->c_usecs);
 
        /* qh->qh_next still "live" to HC */
@@ -411,7 +598,7 @@ static void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
         * active high speed queues may need bigger delays...
         */
        if (list_empty (&qh->qtd_list)
-                       || (__constant_cpu_to_le32 (0x0ff << 8)
+                       || (__constant_cpu_to_le32 (QH_CMASK)
                                        & qh->hw_info2) != 0)
                wait = 2;
        else
@@ -426,7 +613,7 @@ static void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
 /*-------------------------------------------------------------------------*/
 
 static int check_period (
-       struct ehci_hcd *ehci, 
+       struct ehci_hcd *ehci,
        unsigned        frame,
        unsigned        uframe,
        unsigned        period,
@@ -442,7 +629,7 @@ static int check_period (
 
        /*
         * 80% periodic == 100 usec/uframe available
-        * convert "usecs we need" to "max already claimed" 
+        * convert "usecs we need" to "max already claimed"
         */
        usecs = 100 - usecs;
 
@@ -472,15 +659,15 @@ static int check_period (
 }
 
 static int check_intr_schedule (
-       struct ehci_hcd         *ehci, 
+       struct ehci_hcd         *ehci,
        unsigned                frame,
        unsigned                uframe,
        const struct ehci_qh    *qh,
        __le32                  *c_maskp
 )
 {
-       int             retval = -ENOSPC;
-       u8              mask;
+       int             retval = -ENOSPC;
+       u8              mask = 0;
 
        if (qh->c_usecs && uframe >= 6)         /* FSTN territory? */
                goto done;
@@ -493,10 +680,28 @@ static int check_intr_schedule (
                goto done;
        }
 
+#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
+       if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
+                               qh->tt_usecs)) {
+               unsigned i;
+
+               /* TODO : this may need FSTN for SSPLIT in uframe 5. */
+               for (i=uframe+1; i<8 && i<uframe+4; i++)
+                       if (!check_period (ehci, frame, i,
+                                               qh->period, qh->c_usecs))
+                               goto done;
+                       else
+                               mask |= 1 << i;
+
+               retval = 0;
+
+               *c_maskp = cpu_to_le32 (mask << 8);
+       }
+#else
        /* Make sure this tt's buffer is also available for CSPLITs.
         * We pessimize a bit; probably the typical full speed case
         * doesn't need the second CSPLIT.
-        * 
+        *
         * NOTE:  both SPLIT and CSPLIT could be checked in just
         * one smart pass...
         */
@@ -513,6 +718,7 @@ static int check_intr_schedule (
                        goto done;
                retval = 0;
        }
+#endif
 done:
        return retval;
 }
@@ -522,7 +728,7 @@ done:
  */
 static int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
 {
-       int             status;
+       int             status;
        unsigned        uframe;
        __le32          c_mask;
        unsigned        frame;          /* 0..(qh->period - 1), or NO_FRAME */
@@ -533,7 +739,7 @@ static int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
 
        /* reuse the previous schedule slots, if we can */
        if (frame < qh->period) {
-               uframe = ffs (le32_to_cpup (&qh->hw_info2) & 0x00ff);
+               uframe = ffs (le32_to_cpup (&qh->hw_info2) & QH_SMASK);
                status = check_intr_schedule (ehci, frame, --uframe,
                                qh, &c_mask);
        } else {
@@ -569,16 +775,16 @@ static int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
                qh->start = frame;
 
                /* reset S-frame and (maybe) C-frame masks */
-               qh->hw_info2 &= __constant_cpu_to_le32 (~0xffff);
+               qh->hw_info2 &= __constant_cpu_to_le32(~(QH_CMASK | QH_SMASK));
                qh->hw_info2 |= qh->period
                        ? cpu_to_le32 (1 << uframe)
-                       : __constant_cpu_to_le32 (0xff);
+                       : __constant_cpu_to_le32 (QH_SMASK);
                qh->hw_info2 |= c_mask;
        } else
                ehci_dbg (ehci, "reused qh %p schedule\n", qh);
 
        /* stuff into the periodic schedule */
-       status = qh_link_periodic (ehci, qh);
+       status = qh_link_periodic (ehci, qh);
 done:
        return status;
 }
@@ -588,7 +794,7 @@ static int intr_submit (
        struct usb_host_endpoint *ep,
        struct urb              *urb,
        struct list_head        *qtd_list,
-       int                     mem_flags
+       gfp_t                   mem_flags
 ) {
        unsigned                epnum;
        unsigned long           flags;
@@ -601,6 +807,12 @@ static int intr_submit (
 
        spin_lock_irqsave (&ehci->lock, flags);
 
+       if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
+                              &ehci_to_hcd(ehci)->flags))) {
+               status = -ESHUTDOWN;
+               goto done;
+       }
+
        /* get qh and force any scheduling errors */
        INIT_LIST_HEAD (&empty);
        qh = qh_append_tds (ehci, urb, &empty, epnum, &ep->hcpriv);
@@ -633,13 +845,12 @@ done:
 /* ehci_iso_stream ops work with both ITD and SITD */
 
 static struct ehci_iso_stream *
-iso_stream_alloc (int mem_flags)
+iso_stream_alloc (gfp_t mem_flags)
 {
        struct ehci_iso_stream *stream;
 
-       stream = kmalloc(sizeof *stream, mem_flags);
+       stream = kzalloc(sizeof *stream, mem_flags);
        if (likely (stream != NULL)) {
-               memset (stream, 0, sizeof(*stream));
                INIT_LIST_HEAD(&stream->td_list);
                INIT_LIST_HEAD(&stream->free_list);
                stream->next_uframe = -1;
@@ -650,6 +861,7 @@ iso_stream_alloc (int mem_flags)
 
 static void
 iso_stream_init (
+       struct ehci_hcd         *ehci,
        struct ehci_iso_stream  *stream,
        struct usb_device       *dev,
        int                     pipe,
@@ -699,12 +911,21 @@ iso_stream_init (
 
        } else {
                u32             addr;
+               int             think_time;
+               int             hs_transfers;
 
                addr = dev->ttport << 24;
-               addr |= dev->tt->hub->devnum << 16;
+               if (!ehci_is_TDI(ehci)
+                               || (dev->tt->hub !=
+                                       ehci_to_hcd(ehci)->self.root_hub))
+                       addr |= dev->tt->hub->devnum << 16;
                addr |= epnum << 8;
                addr |= dev->devnum;
                stream->usecs = HS_USECS_ISO (maxp);
+               think_time = dev->tt ? dev->tt->think_time : 0;
+               stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
+                               dev->speed, is_input, 1, maxp));
+               hs_transfers = max (1u, (maxp + 187) / 188);
                if (is_input) {
                        u32     tmp;
 
@@ -713,12 +934,11 @@ iso_stream_init (
                        stream->usecs = HS_USECS_ISO (1);
                        stream->raw_mask = 1;
 
-                       /* pessimistic c-mask */
-                       tmp = usb_calc_bus_time (USB_SPEED_FULL, 1, 0, maxp)
-                                       / (125 * 1000);
-                       stream->raw_mask |= 3 << (tmp + 9);
+                       /* c-mask as specified in USB 2.0 11.18.4 3.c */
+                       tmp = (1 << (hs_transfers + 2)) - 1;
+                       stream->raw_mask |= tmp << (8 + 2);
                } else
-                       stream->raw_mask = smask_out [maxp / 188];
+                       stream->raw_mask = smask_out [hs_transfers - 1];
                bandwidth = stream->usecs + stream->c_usecs;
                bandwidth /= 1 << (interval + 2);
 
@@ -819,7 +1039,7 @@ iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
                        /* dev->ep owns the initial refcount */
                        ep->hcpriv = stream;
                        stream->ep = ep;
-                       iso_stream_init(stream, urb->dev, urb->pipe,
+                       iso_stream_init(ehci, stream, urb->dev, urb->pipe,
                                        urb->interval);
                }
 
@@ -843,15 +1063,14 @@ iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
 /* ehci_iso_sched ops can be ITD-only or SITD-only */
 
 static struct ehci_iso_sched *
-iso_sched_alloc (unsigned packets, int mem_flags)
+iso_sched_alloc (unsigned packets, gfp_t mem_flags)
 {
        struct ehci_iso_sched   *iso_sched;
        int                     size = sizeof *iso_sched;
 
        size += packets * sizeof (struct ehci_iso_packet);
-       iso_sched = kmalloc (size, mem_flags);
+       iso_sched = kzalloc(size, mem_flags);
        if (likely (iso_sched != NULL)) {
-               memset(iso_sched, 0, size);
                INIT_LIST_HEAD (&iso_sched->td_list);
        }
        return iso_sched;
@@ -890,7 +1109,7 @@ itd_sched_init (
                trans |= length << 16;
                uframe->transaction = cpu_to_le32 (trans);
 
-               /* might need to cross a buffer page within a td */
+               /* might need to cross a buffer page within a uframe */
                uframe->bufp = (buf & ~(u64)0x0fff);
                buf += length;
                if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
@@ -916,7 +1135,7 @@ itd_urb_transaction (
        struct ehci_iso_stream  *stream,
        struct ehci_hcd         *ehci,
        struct urb              *urb,
-       int                     mem_flags
+       gfp_t                   mem_flags
 )
 {
        struct ehci_itd         *itd;
@@ -1033,12 +1252,21 @@ sitd_slot_ok (
                frame = uframe >> 3;
                uf = uframe & 7;
 
+#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
+               /* The tt's fullspeed bus bandwidth must be available.
+                * tt_available scheduling guarantees 10+% for control/bulk.
+                */
+               if (!tt_available (ehci, period_uframes << 3,
+                               stream->udev, frame, uf, stream->tt_usecs))
+                       return 0;
+#else
                /* tt must be idle for start(s), any gap, and csplit.
                 * assume scheduling slop leaves 10+% for control/bulk.
                 */
                if (!tt_no_collision (ehci, period_uframes << 3,
                                stream->udev, frame, mask))
                        return 0;
+#endif
 
                /* check starts (OUT uses more than one) */
                max_used = 100 - stream->usecs;
@@ -1049,6 +1277,7 @@ sitd_slot_ok (
 
                /* for IN, check CSPLIT */
                if (stream->c_usecs) {
+                       uf = uframe & 7;
                        max_used = 100 - stream->c_usecs;
                        do {
                                tmp = 1 << uf;
@@ -1176,7 +1405,10 @@ fail:
        return status;
 
 ready:
+       /* report high speed start in uframes; full speed, in frames */
        urb->start_frame = stream->next_uframe;
+       if (!stream->highspeed)
+               urb->start_frame >>= 3;
        return 0;
 }
 
@@ -1187,6 +1419,7 @@ itd_init (struct ehci_iso_stream *stream, struct ehci_itd *itd)
 {
        int i;
 
+       /* it's been recently zeroed */
        itd->hw_next = EHCI_LIST_END;
        itd->hw_bufp [0] = stream->buf0;
        itd->hw_bufp [1] = stream->buf1;
@@ -1203,8 +1436,7 @@ itd_patch (
        struct ehci_itd         *itd,
        struct ehci_iso_sched   *iso_sched,
        unsigned                index,
-       u16                     uframe,
-       int                     first
+       u16                     uframe
 )
 {
        struct ehci_iso_packet  *uf = &iso_sched->packet [index];
@@ -1221,7 +1453,7 @@ itd_patch (
        itd->hw_bufp_hi [pg] |= cpu_to_le32 ((u32)(uf->bufp >> 32));
 
        /* iso_frame_desc[].offset must be strictly increasing */
-       if (unlikely (!first && uf->cross)) {
+       if (unlikely (uf->cross)) {
                u64     bufp = uf->bufp + 4096;
                itd->pg = ++pg;
                itd->hw_bufp [pg] |= cpu_to_le32 (bufp & ~(u32)0);
@@ -1250,7 +1482,7 @@ itd_link_urb (
        struct ehci_iso_stream  *stream
 )
 {
-       int                     packet, first = 1;
+       int                     packet;
        unsigned                next_uframe, uframe, frame;
        struct ehci_iso_sched   *iso_sched = urb->hcpriv;
        struct ehci_itd         *itd;
@@ -1283,7 +1515,6 @@ itd_link_urb (
                        list_move_tail (&itd->itd_list, &stream->td_list);
                        itd->stream = iso_stream_get (stream);
                        itd->urb = usb_get_urb (urb);
-                       first = 1;
                        itd_init (stream, itd);
                }
 
@@ -1291,8 +1522,7 @@ itd_link_urb (
                frame = next_uframe >> 3;
 
                itd->usecs [uframe] = stream->usecs;
-               itd_patch (itd, iso_sched, packet, uframe, first);
-               first = 0;
+               itd_patch (itd, iso_sched, packet, uframe);
 
                next_uframe += stream->interval;
                stream->depth += stream->interval;
@@ -1323,8 +1553,7 @@ itd_link_urb (
 static unsigned
 itd_complete (
        struct ehci_hcd *ehci,
-       struct ehci_itd *itd,
-       struct pt_regs  *regs
+       struct ehci_itd *itd
 ) {
        struct urb                              *urb = itd->urb;
        struct usb_iso_packet_descriptor        *desc;
@@ -1382,8 +1611,8 @@ itd_complete (
         */
 
        /* give urb back to the driver ... can be out-of-order */
-       dev = usb_get_dev (urb->dev);
-       ehci_urb_done (ehci, urb, regs);
+       dev = urb->dev;
+       ehci_urb_done (ehci, urb);
        urb = NULL;
 
        /* defer stopping schedule; completion can submit */
@@ -1401,14 +1630,14 @@ itd_complete (
                        (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
        }
        iso_stream_put (ehci, stream);
-       usb_put_dev (dev);
 
        return 1;
 }
 
 /*-------------------------------------------------------------------------*/
 
-static int itd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
+static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
+       gfp_t mem_flags)
 {
        int                     status = -EINVAL;
        unsigned long           flags;
@@ -1446,8 +1675,12 @@ static int itd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
 
        /* schedule ... need to lock */
        spin_lock_irqsave (&ehci->lock, flags);
-       status = iso_stream_schedule (ehci, urb, stream);
-       if (likely (status == 0))
+       if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
+                              &ehci_to_hcd(ehci)->flags)))
+               status = -ESHUTDOWN;
+       else
+               status = iso_stream_schedule (ehci, urb, stream);
+       if (likely (status == 0))
                itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
        spin_unlock_irqrestore (&ehci->lock, flags);
 
@@ -1500,18 +1733,17 @@ sitd_sched_init (
 
                /* might need to cross a buffer page within a td */
                packet->bufp = buf;
-               buf += length;
-               packet->buf1 = buf & ~0x0fff;
+               packet->buf1 = (buf + length) & ~0x0fff;
                if (packet->buf1 != (buf & ~(u64)0x0fff))
                        packet->cross = 1;
 
-               /* OUT uses multiple start-splits */ 
+               /* OUT uses multiple start-splits */
                if (stream->bEndpointAddress & USB_DIR_IN)
                        continue;
-               length = 1 + (length / 188);
-               packet->buf1 |= length;
+               length = (length + 187) / 188;
                if (length > 1) /* BEGIN vs ALL */
-                       packet->buf1 |= 1 << 3;
+                       length |= 1 << 3;
+               packet->buf1 |= length;
        }
 }
 
@@ -1520,7 +1752,7 @@ sitd_urb_transaction (
        struct ehci_iso_stream  *stream,
        struct ehci_hcd         *ehci,
        struct urb              *urb,
-       int                     mem_flags
+       gfp_t                   mem_flags
 )
 {
        struct ehci_sitd        *sitd;
@@ -1606,10 +1838,9 @@ sitd_patch (
        sitd->hw_buf_hi [0] = cpu_to_le32 (bufp >> 32);
 
        sitd->hw_buf [1] = cpu_to_le32 (uf->buf1);
-       if (uf->cross) {
+       if (uf->cross)
                bufp += 4096;
-               sitd->hw_buf_hi [1] = cpu_to_le32 (bufp >> 32);
-       }
+       sitd->hw_buf_hi [1] = cpu_to_le32 (bufp >> 32);
        sitd->index = index;
 }
 
@@ -1646,7 +1877,7 @@ sitd_link_urb (
                ehci_to_hcd(ehci)->self.bandwidth_allocated
                                += stream->bandwidth;
                ehci_vdbg (ehci,
-                       "sched dev%s ep%d%s-iso [%d] %dms/%04x\n",
+                       "sched dev%s ep%d%s-iso [%d] %dms/%04x\n",
                        urb->dev->devpath, stream->bEndpointAddress & 0x0f,
                        (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
                        (next_uframe >> 3) % ehci->periodic_size,
@@ -1693,13 +1924,12 @@ sitd_link_urb (
 /*-------------------------------------------------------------------------*/
 
 #define        SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
-                       | SITD_STS_XACT | SITD_STS_MMF | SITD_STS_STS)
+                               | SITD_STS_XACT | SITD_STS_MMF)
 
 static unsigned
 sitd_complete (
        struct ehci_hcd         *ehci,
-       struct ehci_sitd        *sitd,
-       struct pt_regs          *regs
+       struct ehci_sitd        *sitd
 ) {
        struct urb                              *urb = sitd->urb;
        struct usb_iso_packet_descriptor        *desc;
@@ -1745,8 +1975,8 @@ sitd_complete (
         */
 
        /* give urb back to the driver */
-       dev = usb_get_dev (urb->dev);
-       ehci_urb_done (ehci, urb, regs);
+       dev = urb->dev;
+       ehci_urb_done (ehci, urb);
        urb = NULL;
 
        /* defer stopping schedule; completion can submit */
@@ -1764,24 +1994,18 @@ sitd_complete (
                        (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
        }
        iso_stream_put (ehci, stream);
-       usb_put_dev (dev);
 
        return 1;
 }
 
 
-static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
+static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
+       gfp_t mem_flags)
 {
        int                     status = -EINVAL;
        unsigned long           flags;
        struct ehci_iso_stream  *stream;
 
-       // FIXME remove when csplits behave
-       if (usb_pipein(urb->pipe)) {
-               ehci_dbg (ehci, "no iso-IN split transactions yet\n");
-               return -ENOMEM;
-       }
-
        /* Get iso_stream head */
        stream = iso_stream_find (ehci, urb);
        if (stream == NULL) {
@@ -1812,8 +2036,12 @@ static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
 
        /* schedule ... need to lock */
        spin_lock_irqsave (&ehci->lock, flags);
-       status = iso_stream_schedule (ehci, urb, stream);
-       if (status == 0)
+       if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
+                              &ehci_to_hcd(ehci)->flags)))
+               status = -ESHUTDOWN;
+       else
+               status = iso_stream_schedule (ehci, urb, stream);
+       if (status == 0)
                sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
        spin_unlock_irqrestore (&ehci->lock, flags);
 
@@ -1826,7 +2054,7 @@ done:
 #else
 
 static inline int
-sitd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
+sitd_submit (struct ehci_hcd *ehci, struct urb *urb, gfp_t mem_flags)
 {
        ehci_dbg (ehci, "split iso support is disabled\n");
        return -ENOSYS;
@@ -1835,8 +2063,7 @@ sitd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
 static inline unsigned
 sitd_complete (
        struct ehci_hcd         *ehci,
-       struct ehci_sitd        *sitd,
-       struct pt_regs          *regs
+       struct ehci_sitd        *sitd
 ) {
        ehci_err (ehci, "sitd_complete %p?\n", sitd);
        return 0;
@@ -1847,7 +2074,7 @@ sitd_complete (
 /*-------------------------------------------------------------------------*/
 
 static void
-scan_periodic (struct ehci_hcd *ehci, struct pt_regs *regs)
+scan_periodic (struct ehci_hcd *ehci)
 {
        unsigned        frame, clock, now_uframe, mod;
        unsigned        modified;
@@ -1860,7 +2087,7 @@ scan_periodic (struct ehci_hcd *ehci, struct pt_regs *regs)
         * Touches as few pages as possible:  cache-friendly.
         */
        now_uframe = ehci->next_uframe;
-       if (HCD_IS_RUNNING (ehci_to_hcd(ehci)->state))
+       if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
                clock = readl (&ehci->regs->frame_index);
        else
                clock = now_uframe + mod - 1;
@@ -1894,14 +2121,14 @@ restart:
                        union ehci_shadow       temp;
                        int                     live;
 
-                       live = HCD_IS_RUNNING (ehci_to_hcd(ehci)->state);
+                       live = HC_IS_RUNNING (ehci_to_hcd(ehci)->state);
                        switch (type) {
                        case Q_TYPE_QH:
                                /* handle any completions */
                                temp.qh = qh_get (q.qh);
                                type = Q_NEXT_TYPE (q.qh->hw_next);
                                q = q.qh->qh_next;
-                               modified = qh_completions (ehci, temp.qh, regs);
+                               modified = qh_completions (ehci, temp.qh);
                                if (unlikely (list_empty (&temp.qh->qtd_list)))
                                        intr_deschedule (ehci, temp.qh);
                                qh_put (temp.qh);
@@ -1939,7 +2166,7 @@ restart:
                                *hw_p = q.itd->hw_next;
                                type = Q_NEXT_TYPE (q.itd->hw_next);
                                wmb();
-                               modified = itd_complete (ehci, q.itd, regs);
+                               modified = itd_complete (ehci, q.itd);
                                q = *q_p;
                                break;
                        case Q_TYPE_SITD:
@@ -1955,7 +2182,7 @@ restart:
                                *hw_p = q.sitd->hw_next;
                                type = Q_NEXT_TYPE (q.sitd->hw_next);
                                wmb();
-                               modified = sitd_complete (ehci, q.sitd, regs);
+                               modified = sitd_complete (ehci, q.sitd);
                                q = *q_p;
                                break;
                        default:
@@ -1983,7 +2210,7 @@ restart:
                if (now_uframe == clock) {
                        unsigned        now;
 
-                       if (!HCD_IS_RUNNING (ehci_to_hcd(ehci)->state))
+                       if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
                                break;
                        ehci->next_uframe = now_uframe;
                        now = readl (&ehci->regs->frame_index) % mod;
@@ -1996,5 +2223,5 @@ restart:
                        now_uframe++;
                        now_uframe %= mod;
                }
-       } 
+       }
 }