Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / usb / host / ohci-hub.c
index f6263bf..0bb972b 100644 (file)
@@ -2,7 +2,7 @@
  * OHCI HCD (Host Controller Driver) for USB.
  * 
  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
- * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
+ * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
  * 
  * This file is licenced under GPL
  */
 
 /*
  * OHCI Root Hub ... the nonsharable stuff
- *
- * Registers don't need cpu_to_le32, that happens transparently
  */
 
-/* AMD-756 (D2 rev) reports corrupt register contents in some cases.
- * The erratum (#4) description is incorrect.  AMD's workaround waits
- * till some bits (mostly reserved) are clear; ok for all revs.
- */
-#define read_roothub(hc, register, mask) ({ \
-       u32 temp = ohci_readl (&hc->regs->roothub.register); \
-       if (temp == -1) \
-               disable (hc); \
-       else if (hc->flags & OHCI_QUIRK_AMD756) \
-               while (temp & mask) \
-                       temp = ohci_readl (&hc->regs->roothub.register); \
-       temp; })
-
-static u32 roothub_a (struct ohci_hcd *hc)
-       { return read_roothub (hc, a, 0xfc0fe000); }
-static inline u32 roothub_b (struct ohci_hcd *hc)
-       { return ohci_readl (&hc->regs->roothub.b); }
-static inline u32 roothub_status (struct ohci_hcd *hc)
-       { return ohci_readl (&hc->regs->roothub.status); }
-static u32 roothub_portstatus (struct ohci_hcd *hc, int i)
-       { return read_roothub (hc, portstatus [i], 0xffe0fce0); }
-
-/*-------------------------------------------------------------------------*/
-
 #define dbg_port(hc,label,num,value) \
        ohci_dbg (hc, \
                "%s roothub.portstatus [%d] " \
@@ -62,55 +36,55 @@ static u32 roothub_portstatus (struct ohci_hcd *hc, int i)
 
 /*-------------------------------------------------------------------------*/
 
-#if    defined(CONFIG_USB_SUSPEND) || defined(CONFIG_PM)
+#ifdef CONFIG_PM
 
 #define OHCI_SCHED_ENABLES \
        (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
 
 static void dl_done_list (struct ohci_hcd *, struct pt_regs *);
 static void finish_unlinks (struct ohci_hcd *, u16 , struct pt_regs *);
+static int ohci_restart (struct ohci_hcd *ohci);
 
-static int ohci_hub_suspend (struct usb_hcd *hcd)
+static int ohci_bus_suspend (struct usb_hcd *hcd)
 {
        struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
-       struct usb_device       *root = hcd_to_bus (&ohci->hcd)->root_hub;
        int                     status = 0;
+       unsigned long           flags;
 
-       if (root->dev.power.power_state != 0)
-               return 0;
-       if (time_before (jiffies, ohci->next_statechange))
-               return -EAGAIN;
+       spin_lock_irqsave (&ohci->lock, flags);
 
-       spin_lock_irq (&ohci->lock);
+       if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
+               spin_unlock_irqrestore (&ohci->lock, flags);
+               return -ESHUTDOWN;
+       }
 
-       ohci->hc_control = ohci_readl (&ohci->regs->control);
+       ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
        switch (ohci->hc_control & OHCI_CTRL_HCFS) {
        case OHCI_USB_RESUME:
                ohci_dbg (ohci, "resume/suspend?\n");
                ohci->hc_control &= ~OHCI_CTRL_HCFS;
                ohci->hc_control |= OHCI_USB_RESET;
-               writel (ohci->hc_control, &ohci->regs->control);
-               (void) ohci_readl (&ohci->regs->control);
+               ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
+               (void) ohci_readl (ohci, &ohci->regs->control);
                /* FALL THROUGH */
        case OHCI_USB_RESET:
                status = -EBUSY;
                ohci_dbg (ohci, "needs reinit!\n");
                goto done;
        case OHCI_USB_SUSPEND:
-               ohci_dbg (ohci, "already suspended?\n");
-               goto succeed;
+               ohci_dbg (ohci, "already suspended\n");
+               goto done;
        }
        ohci_dbg (ohci, "suspend root hub\n");
 
        /* First stop any processing */
-       ohci->hcd.state = USB_STATE_QUIESCING;
        if (ohci->hc_control & OHCI_SCHED_ENABLES) {
                int             limit;
 
                ohci->hc_control &= ~OHCI_SCHED_ENABLES;
-               writel (ohci->hc_control, &ohci->regs->control);
-               ohci->hc_control = ohci_readl (&ohci->regs->control);
-               writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
+               ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
+               ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
+               ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
 
                /* sched disables take effect on the next frame,
                 * then the last WDH could take 6+ msec
@@ -120,38 +94,40 @@ static int ohci_hub_suspend (struct usb_hcd *hcd)
                while (limit > 0) {
                        udelay (250);
                        limit =- 250;
-                       if (ohci_readl (&ohci->regs->intrstatus) & OHCI_INTR_SF)
+                       if (ohci_readl (ohci, &ohci->regs->intrstatus)
+                                       & OHCI_INTR_SF)
                                break;
                }
                dl_done_list (ohci, NULL);
                mdelay (7);
        }
        dl_done_list (ohci, NULL);
-       finish_unlinks (ohci, OHCI_FRAME_NO(ohci->hcca), NULL);
-       writel (ohci_readl (&ohci->regs->intrstatus), &ohci->regs->intrstatus);
+       finish_unlinks (ohci, ohci_frame_no(ohci), NULL);
+       ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
+                       &ohci->regs->intrstatus);
 
        /* maybe resume can wake root hub */
-       if (ohci->hcd.remote_wakeup)
+       if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev))
                ohci->hc_control |= OHCI_CTRL_RWE;
        else
                ohci->hc_control &= ~OHCI_CTRL_RWE;
 
-       /* Suspend hub */
+       /* Suspend hub ... this is the "global (to this bus) suspend" mode,
+        * which doesn't imply ports will first be individually suspended.
+        */
        ohci->hc_control &= ~OHCI_CTRL_HCFS;
        ohci->hc_control |= OHCI_USB_SUSPEND;
-       writel (ohci->hc_control, &ohci->regs->control);
-       (void) ohci_readl (&ohci->regs->control);
+       ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
+       (void) ohci_readl (ohci, &ohci->regs->control);
 
        /* no resumes until devices finish suspending */
        ohci->next_statechange = jiffies + msecs_to_jiffies (5);
 
-succeed:
-       /* it's not USB_STATE_SUSPENDED unless access to this
-        * hub from the non-usb side (PCI, SOC, etc) stopped 
-        */
-       root->dev.power.power_state = 3;
 done:
-       spin_unlock_irq (&ohci->lock);
+       /* external suspend vs self autosuspend ... same effect */
+       if (status == 0)
+               usb_hcd_suspend_root_hub(hcd);
+       spin_unlock_irqrestore (&ohci->lock, flags);
        return status;
 }
 
@@ -163,29 +139,44 @@ static inline struct ed *find_head (struct ed *ed)
        return ed;
 }
 
-static int hc_restart (struct ohci_hcd *ohci);
-
-/* caller owns root->serialize */
-static int ohci_hub_resume (struct usb_hcd *hcd)
+/* caller has locked the root hub */
+static int ohci_bus_resume (struct usb_hcd *hcd)
 {
        struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
-       struct usb_device       *root = hcd_to_bus (&ohci->hcd)->root_hub;
        u32                     temp, enables;
        int                     status = -EINPROGRESS;
+       unsigned long           flags;
 
-       if (!root->dev.power.power_state)
-               return 0;
        if (time_before (jiffies, ohci->next_statechange))
-               return -EAGAIN;
+               msleep(5);
 
-       spin_lock_irq (&ohci->lock);
-       ohci->hc_control = ohci_readl (&ohci->regs->control);
-       switch (ohci->hc_control & OHCI_CTRL_HCFS) {
+       spin_lock_irqsave (&ohci->lock, flags);
+
+       if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
+               spin_unlock_irqrestore (&ohci->lock, flags);
+               return -ESHUTDOWN;
+       }
+
+
+       ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
+
+       if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
+               /* this can happen after resuming a swsusp snapshot */
+               if (hcd->state == HC_STATE_RESUMING) {
+                       ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
+                                       ohci->hc_control);
+                       status = -EBUSY;
+               /* this happens when pmcore resumes HC then root */
+               } else {
+                       ohci_dbg (ohci, "duplicate resume\n");
+                       status = 0;
+               }
+       } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
        case OHCI_USB_SUSPEND:
                ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
                ohci->hc_control |= OHCI_USB_RESUME;
-               writel (ohci->hc_control, &ohci->regs->control);
-               (void) ohci_readl (&ohci->regs->control);
+               ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
+               (void) ohci_readl (ohci, &ohci->regs->control);
                ohci_dbg (ohci, "resume root hub\n");
                break;
        case OHCI_USB_RESUME:
@@ -193,36 +184,39 @@ static int ohci_hub_resume (struct usb_hcd *hcd)
                ohci_info (ohci, "wakeup\n");
                break;
        case OHCI_USB_OPER:
-               ohci_dbg (ohci, "odd resume\n");
-               root->dev.power.power_state = 0;
-               status = 0;
+               /* this can happen after resuming a swsusp snapshot */
+               ohci_dbg (ohci, "snapshot resume? reinit\n");
+               status = -EBUSY;
                break;
        default:                /* RESET, we lost power */
-               ohci_dbg (ohci, "root hub hardware reset\n");
+               ohci_dbg (ohci, "lost power\n");
                status = -EBUSY;
        }
-       spin_unlock_irq (&ohci->lock);
-       if (status == -EBUSY)
-               return hc_restart (ohci);
+       spin_unlock_irqrestore (&ohci->lock, flags);
+       if (status == -EBUSY) {
+               (void) ohci_init (ohci);
+               return ohci_restart (ohci);
+       }
        if (status != -EINPROGRESS)
                return status;
 
-       temp = roothub_a (ohci) & RH_A_NDP;
+       temp = ohci->num_ports;
        enables = 0;
        while (temp--) {
-               u32 stat = ohci_readl (&ohci->regs->roothub.portstatus [temp]);
+               u32 stat = ohci_readl (ohci,
+                                      &ohci->regs->roothub.portstatus [temp]);
 
                /* force global, not selective, resume */
                if (!(stat & RH_PS_PSS))
                        continue;
-               writel (RH_PS_POCI, &ohci->regs->roothub.portstatus [temp]);
+               ohci_writel (ohci, RH_PS_POCI,
+                               &ohci->regs->roothub.portstatus [temp]);
        }
 
-       /* Some controllers (lucent) need extra-long delays */
-       ohci->hcd.state = USB_STATE_RESUMING;
-       mdelay (20 /* usb 11.5.1.10 */ + 15);
+       /* Some controllers (lucent erratum) need extra-long delays */
+       msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
 
-       temp = ohci_readl (&ohci->regs->control);
+       temp = ohci_readl (ohci, &ohci->regs->control);
        temp &= OHCI_CTRL_HCFS;
        if (temp != OHCI_USB_RESUME) {
                ohci_err (ohci, "controller won't resume\n");
@@ -230,36 +224,37 @@ static int ohci_hub_resume (struct usb_hcd *hcd)
        }
 
        /* disable old schedule state, reinit from scratch */
-       writel (0, &ohci->regs->ed_controlhead);
-       writel (0, &ohci->regs->ed_controlcurrent);
-       writel (0, &ohci->regs->ed_bulkhead);
-       writel (0, &ohci->regs->ed_bulkcurrent);
-       writel (0, &ohci->regs->ed_periodcurrent);
-       writel ((u32) ohci->hcca_dma, &ohci->regs->hcca);
-
+       ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
+       ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
+       ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
+       ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
+       ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
+       ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
+
+       /* Sometimes PCI D3 suspend trashes frame timings ... */
        periodic_reinit (ohci);
 
        /* interrupts might have been disabled */
-       writel (OHCI_INTR_INIT, &ohci->regs->intrenable);
+       ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
        if (ohci->ed_rm_list)
-               writel (OHCI_INTR_SF, &ohci->regs->intrenable);
-       writel (ohci_readl (&ohci->regs->intrstatus), &ohci->regs->intrstatus);
+               ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
+       ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
+                       &ohci->regs->intrstatus);
 
        /* Then re-enable operations */
-       writel (OHCI_USB_OPER, &ohci->regs->control);
-       (void) ohci_readl (&ohci->regs->control);
+       ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
+       (void) ohci_readl (ohci, &ohci->regs->control);
        msleep (3);
 
-       temp = OHCI_CONTROL_INIT | OHCI_USB_OPER;
-       if (ohci->hcd.can_wakeup)
-               temp |= OHCI_CTRL_RWC;
+       temp = ohci->hc_control;
+       temp &= OHCI_CTRL_RWC;
+       temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
        ohci->hc_control = temp;
-       writel (temp, &ohci->regs->control);
-       (void) ohci_readl (&ohci->regs->control);
+       ohci_writel (ohci, temp, &ohci->regs->control);
+       (void) ohci_readl (ohci, &ohci->regs->control);
 
        /* TRSMRCY */
        msleep (10);
-       root->dev.power.power_state = 0;
 
        /* keep it alive for ~5x suspend + resume costs */
        ohci->next_statechange = jiffies + msecs_to_jiffies (250);
@@ -269,52 +264,34 @@ static int ohci_hub_resume (struct usb_hcd *hcd)
        temp = 0;
        if (!ohci->ed_rm_list) {
                if (ohci->ed_controltail) {
-                       writel (find_head (ohci->ed_controltail)->dma,
-                               &ohci->regs->ed_controlhead);
+                       ohci_writel (ohci,
+                                       find_head (ohci->ed_controltail)->dma,
+                                       &ohci->regs->ed_controlhead);
                        enables |= OHCI_CTRL_CLE;
                        temp |= OHCI_CLF;
                }
                if (ohci->ed_bulktail) {
-                       writel (find_head (ohci->ed_bulktail)->dma,
+                       ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
                                &ohci->regs->ed_bulkhead);
                        enables |= OHCI_CTRL_BLE;
                        temp |= OHCI_BLF;
                }
        }
-       if (hcd_to_bus (&ohci->hcd)->bandwidth_isoc_reqs
-                       || hcd_to_bus (&ohci->hcd)->bandwidth_int_reqs)
+       if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
                enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
        if (enables) {
                ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
                ohci->hc_control |= enables;
-               writel (ohci->hc_control, &ohci->regs->control);
+               ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
                if (temp)
-                       writel (status, &ohci->regs->cmdstatus);
-               (void) ohci_readl (&ohci->regs->control);
+                       ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
+               (void) ohci_readl (ohci, &ohci->regs->control);
        }
 
-       ohci->hcd.state = USB_STATE_RUNNING;
        return 0;
 }
 
-static void ohci_rh_resume (void *_hcd)
-{
-       struct usb_hcd  *hcd = _hcd;
-
-       down (&hcd->self.root_hub->serialize);
-       (void) ohci_hub_resume (hcd);
-       up (&hcd->self.root_hub->serialize);
-}
-
-#else
-
-static void ohci_rh_resume (void *_hcd)
-{
-       struct ohci_hcd *ohci = hcd_to_ohci (_hcd);
-       ohci_dbg(ohci, "rh_resume ??\n");
-}
-
-#endif /* CONFIG_USB_SUSPEND || CONFIG_PM */
+#endif /* CONFIG_PM */
 
 /*-------------------------------------------------------------------------*/
 
@@ -324,17 +301,28 @@ static int
 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
 {
        struct ohci_hcd *ohci = hcd_to_ohci (hcd);
-       int             ports, i, changed = 0, length = 1;
-       int             can_suspend = 1;
-
-       ports = roothub_a (ohci) & RH_A_NDP; 
-       if (ports > MAX_ROOT_PORTS) {
-               if (!HCD_IS_RUNNING(ohci->hcd.state))
-                       return -ESHUTDOWN;
-               ohci_err (ohci, "bogus NDP=%d, rereads as NDP=%d\n",
-                       ports, ohci_readl (&ohci->regs->roothub.a) & RH_A_NDP);
+       int             i, changed = 0, length = 1;
+       int             can_suspend = device_may_wakeup(&hcd->self.root_hub->dev);
+       unsigned long   flags;
+
+       spin_lock_irqsave (&ohci->lock, flags);
+
+       /* handle autosuspended root:  finish resuming before
+        * letting khubd or root hub timer see state changes.
+        */
+       if (unlikely((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER
+                    || !HC_IS_RUNNING(hcd->state))) {
+               can_suspend = 0;
+               goto done;
+       }
+
+       /* undocumented erratum seen on at least rev D */
+       if ((ohci->flags & OHCI_QUIRK_AMD756)
+                       && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
+               ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
+                         ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
                /* retry later; "should not happen" */
-               return 0;
+               goto done;
        }
 
        /* init status */
@@ -342,13 +330,13 @@ ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
                buf [0] = changed = 1;
        else
                buf [0] = 0;
-       if (ports > 7) {
+       if (ohci->num_ports > 7) {
                buf [1] = 0;
                length++;
        }
 
        /* look at each port */
-       for (i = 0; i < ports; i++) {
+       for (i = 0; i < ohci->num_ports; i++) {
                u32     status = roothub_portstatus (ohci, i);
 
                if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
@@ -366,10 +354,12 @@ ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
                 */
                if (!(status & RH_PS_CCS))
                        continue;
-               if ((status & RH_PS_PSS) && ohci->hcd.remote_wakeup)
+               if ((status & RH_PS_PSS) && can_suspend)
                        continue;
                can_suspend = 0;
        }
+done:
+       spin_unlock_irqrestore (&ohci->lock, flags);
 
 #ifdef CONFIG_PM
        /* save power by suspending idle root hubs;
@@ -381,12 +371,12 @@ ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
                        && ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES)
                                        & ohci->hc_control)
                                == OHCI_USB_OPER
-                       && down_trylock (&hcd->self.root_hub->serialize) == 0
+                       && time_after (jiffies, ohci->next_statechange)
+                       && usb_trylock_device (hcd->self.root_hub) == 0
                        ) {
                ohci_vdbg (ohci, "autosuspend\n");
-               (void) ohci_hub_suspend (&ohci->hcd);
-               ohci->hcd.state = USB_STATE_RUNNING;
-               up (&hcd->self.root_hub->serialize);
+               (void) ohci_bus_suspend (hcd);
+               usb_unlock_device (hcd->self.root_hub);
        }
 #endif
 
@@ -401,15 +391,14 @@ ohci_hub_descriptor (
        struct usb_hub_descriptor       *desc
 ) {
        u32             rh = roothub_a (ohci);
-       int             ports = rh & RH_A_NDP; 
        u16             temp;
 
        desc->bDescriptorType = 0x29;
        desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
        desc->bHubContrCurrent = 0;
 
-       desc->bNbrPorts = ports;
-       temp = 1 + (ports / 8);
+       desc->bNbrPorts = ohci->num_ports;
+       temp = 1 + (ohci->num_ports / 8);
        desc->bDescLength = 7 + 2 * temp;
 
        temp = 0;
@@ -421,20 +410,104 @@ ohci_hub_descriptor (
            temp |= 0x0010;
        else if (rh & RH_A_OCPM)        /* per-port overcurrent reporting? */
            temp |= 0x0008;
-       desc->wHubCharacteristics = cpu_to_le16 (temp);
+       desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
 
        /* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
        rh = roothub_b (ohci);
+       memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
        desc->bitmap [0] = rh & RH_B_DR;
-       if (ports > 7) {
+       if (ohci->num_ports > 7) {
                desc->bitmap [1] = (rh & RH_B_DR) >> 8;
-               desc->bitmap [2] = desc->bitmap [3] = 0xff;
+               desc->bitmap [2] = 0xff;
        } else
                desc->bitmap [1] = 0xff;
 }
 
 /*-------------------------------------------------------------------------*/
 
+#ifdef CONFIG_USB_OTG
+
+static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
+{
+       struct ohci_hcd *ohci = hcd_to_ohci (hcd);
+       u32                     status;
+
+       if (!port)
+               return -EINVAL;
+       port--;
+
+       /* start port reset before HNP protocol times out */
+       status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
+       if (!(status & RH_PS_CCS))
+               return -ENODEV;
+
+       /* khubd will finish the reset later */
+       ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
+       return 0;
+}
+
+static void start_hnp(struct ohci_hcd *ohci);
+
+#else
+
+#define        ohci_start_port_reset           NULL
+
+#endif
+
+/*-------------------------------------------------------------------------*/
+
+
+/* See usb 7.1.7.5:  root hubs must issue at least 50 msec reset signaling,
+ * not necessarily continuous ... to guard against resume signaling.
+ * The short timeout is safe for non-root hubs, and is backward-compatible
+ * with earlier Linux hosts.
+ */
+#ifdef CONFIG_USB_SUSPEND
+#define        PORT_RESET_MSEC         50
+#else
+#define        PORT_RESET_MSEC         10
+#endif
+
+/* this timer value might be vendor-specific ... */
+#define        PORT_RESET_HW_MSEC      10
+
+/* wrap-aware logic morphed from <linux/jiffies.h> */
+#define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
+
+/* called from some task, normally khubd */
+static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
+{
+       __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
+       u32     temp;
+       u16     now = ohci_readl(ohci, &ohci->regs->fmnumber);
+       u16     reset_done = now + PORT_RESET_MSEC;
+
+       /* build a "continuous enough" reset signal, with up to
+        * 3msec gap between pulses.  scheduler HZ==100 must work;
+        * this might need to be deadline-scheduled.
+        */
+       do {
+               /* spin until any current reset finishes */
+               for (;;) {
+                       temp = ohci_readl (ohci, portstat);
+                       if (!(temp & RH_PS_PRS))
+                               break;
+                       udelay (500);
+               } 
+
+               if (!(temp & RH_PS_CCS))
+                       break;
+               if (temp & RH_PS_PRSC)
+                       ohci_writel (ohci, RH_PS_PRSC, portstat);
+
+               /* start the next reset, sleep till it's probably done */
+               ohci_writel (ohci, RH_PS_PRS, portstat);
+               msleep(PORT_RESET_HW_MSEC);
+               now = ohci_readl(ohci, &ohci->regs->fmnumber);
+       } while (tick_before(now, reset_done));
+       /* caller synchronizes using PRSC */
+}
+
 static int ohci_hub_control (
        struct usb_hcd  *hcd,
        u16             typeReq,
@@ -448,11 +521,15 @@ static int ohci_hub_control (
        u32             temp;
        int             retval = 0;
 
+       if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
+               return -ESHUTDOWN;
+
        switch (typeReq) {
        case ClearHubFeature:
                switch (wValue) {
                case C_HUB_OVER_CURRENT:
-                       writel (RH_HS_OCIC, &ohci->regs->roothub.status);
+                       ohci_writel (ohci, RH_HS_OCIC,
+                                       &ohci->regs->roothub.status);
                case C_HUB_LOCAL_POWER:
                        break;
                default:
@@ -475,7 +552,7 @@ static int ohci_hub_control (
                        temp = RH_PS_POCI;
                        if ((ohci->hc_control & OHCI_CTRL_HCFS)
                                        != OHCI_USB_OPER)
-                               schedule_work (&ohci->rh_resume);
+                               usb_hcd_resume_root_hub(hcd);
                        break;
                case USB_PORT_FEAT_C_SUSPEND:
                        temp = RH_PS_PSSC;
@@ -495,22 +572,23 @@ static int ohci_hub_control (
                default:
                        goto error;
                }
-               writel (temp, &ohci->regs->roothub.portstatus [wIndex]);
-               // ohci_readl (&ohci->regs->roothub.portstatus [wIndex]);
+               ohci_writel (ohci, temp,
+                               &ohci->regs->roothub.portstatus [wIndex]);
+               // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
                break;
        case GetHubDescriptor:
                ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
                break;
        case GetHubStatus:
                temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
-               *(u32 *) buf = cpu_to_le32 (temp);
+               *(__le32 *) buf = cpu_to_le32 (temp);
                break;
        case GetPortStatus:
                if (!wIndex || wIndex > ports)
                        goto error;
                wIndex--;
                temp = roothub_portstatus (ohci, wIndex);
-               *(u32 *) buf = cpu_to_le32 (temp);
+               *(__le32 *) buf = cpu_to_le32 (temp);
 
 #ifndef        OHCI_VERBOSE_DEBUG
        if (*(u16*)(buf+2))     /* only if wPortChange is interesting */
@@ -533,18 +611,21 @@ static int ohci_hub_control (
                wIndex--;
                switch (wValue) {
                case USB_PORT_FEAT_SUSPEND:
-                       writel (RH_PS_PSS,
+#ifdef CONFIG_USB_OTG
+                       if (hcd->self.otg_port == (wIndex + 1)
+                                       && hcd->self.b_hnp_enable)
+                               start_hnp(ohci);
+                       else
+#endif
+                       ohci_writel (ohci, RH_PS_PSS,
                                &ohci->regs->roothub.portstatus [wIndex]);
                        break;
                case USB_PORT_FEAT_POWER:
-                       writel (RH_PS_PPS,
+                       ohci_writel (ohci, RH_PS_PPS,
                                &ohci->regs->roothub.portstatus [wIndex]);
                        break;
                case USB_PORT_FEAT_RESET:
-                       temp = ohci_readl (&ohci->regs->roothub.portstatus [wIndex]);
-                       if (temp & RH_PS_CCS)
-                               writel (RH_PS_PRS,
-                                   &ohci->regs->roothub.portstatus [wIndex]);
+                       root_port_reset (ohci, wIndex);
                        break;
                default:
                        goto error;