]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - drivers/usb/core/hub.c
usb: hub_handle_remote_wakeup() only exists for CONFIG_PM=y
[karo-tx-linux.git] / drivers / usb / core / hub.c
index 5a909ba6fb676f8784cc65a94f9bf400d1057d25..db6287025c06148adcd86608a0f7c38743be7a36 100644 (file)
 #define USB_VENDOR_GENESYS_LOGIC               0x05e3
 #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND       0x01
 
-static inline int hub_is_superspeed(struct usb_device *hdev)
-{
-       return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
-}
-
 /* Protect struct usb_device->state and ->children members
  * Note: Both are also protected by ->dev.sem, except that ->state can
  * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
@@ -751,16 +746,20 @@ int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
                           int port1, bool set)
 {
        int ret;
-       struct usb_port *port_dev = hub->ports[port1 - 1];
 
        if (set)
                ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
        else
                ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
 
-       if (!ret)
-               port_dev->power_is_on = set;
-       return ret;
+       if (ret)
+               return ret;
+
+       if (set)
+               set_bit(port1, hub->power_bits);
+       else
+               clear_bit(port1, hub->power_bits);
+       return 0;
 }
 
 /**
@@ -818,14 +817,9 @@ int usb_hub_clear_tt_buffer(struct urb *urb)
 }
 EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
 
-/* If do_delay is false, return the number of milliseconds the caller
- * needs to delay.
- */
-static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
+static void hub_power_on(struct usb_hub *hub, bool do_delay)
 {
        int port1;
-       unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
-       unsigned delay;
 
        /* Enable power on each port.  Some hubs have reserved values
         * of LPSM (> 2) in their descriptors, even though they are
@@ -839,17 +833,13 @@ static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
                dev_dbg(hub->intfdev, "trying to enable port power on "
                                "non-switchable hub\n");
        for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
-               if (hub->ports[port1 - 1]->power_is_on)
+               if (test_bit(port1, hub->power_bits))
                        set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
                else
                        usb_clear_port_feature(hub->hdev, port1,
                                                USB_PORT_FEAT_POWER);
-
-       /* Wait at least 100 msec for power to become stable */
-       delay = max(pgood_delay, (unsigned) 100);
        if (do_delay)
-               msleep(delay);
-       return delay;
+               msleep(hub_power_on_good_delay(hub));
 }
 
 static int hub_hub_status(struct usb_hub *hub,
@@ -1053,7 +1043,9 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
                 * for HUB_POST_RESET, but it's easier not to.
                 */
                if (type == HUB_INIT) {
-                       delay = hub_power_on(hub, false);
+                       unsigned delay = hub_power_on_good_delay(hub);
+
+                       hub_power_on(hub, false);
                        INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
                        queue_delayed_work(system_power_efficient_wq,
                                        &hub->init_work,
@@ -1180,15 +1172,13 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
                                set_bit(port1, hub->change_bits);
 
                } else if (udev->persist_enabled) {
-                       struct usb_port *port_dev = hub->ports[port1 - 1];
-
 #ifdef CONFIG_PM
                        udev->reset_resume = 1;
 #endif
                        /* Don't set the change_bits when the device
                         * was powered off.
                         */
-                       if (port_dev->power_is_on)
+                       if (test_bit(port1, hub->power_bits))
                                set_bit(port1, hub->change_bits);
 
                } else {
@@ -2049,6 +2039,18 @@ static void hub_free_dev(struct usb_device *udev)
                hcd->driver->free_dev(hcd, udev);
 }
 
+static void hub_disconnect_children(struct usb_device *udev)
+{
+       struct usb_hub *hub = usb_hub_to_struct_hub(udev);
+       int i;
+
+       /* Free up all the children before we remove this device */
+       for (i = 0; i < udev->maxchild; i++) {
+               if (hub->ports[i]->child)
+                       usb_disconnect(&hub->ports[i]->child);
+       }
+}
+
 /**
  * usb_disconnect - disconnect a device (usbcore-internal)
  * @pdev: pointer to device being disconnected
@@ -2067,9 +2069,10 @@ static void hub_free_dev(struct usb_device *udev)
  */
 void usb_disconnect(struct usb_device **pdev)
 {
-       struct usb_device       *udev = *pdev;
-       struct usb_hub          *hub = usb_hub_to_struct_hub(udev);
-       int                     i;
+       struct usb_port *port_dev = NULL;
+       struct usb_device *udev = *pdev;
+       struct usb_hub *hub;
+       int port1;
 
        /* mark the device as inactive, so any further urb submissions for
         * this device (and any of its children) will fail immediately.
@@ -2081,11 +2084,7 @@ void usb_disconnect(struct usb_device **pdev)
 
        usb_lock_device(udev);
 
-       /* Free up all the children before we remove this device */
-       for (i = 0; i < udev->maxchild; i++) {
-               if (hub->ports[i]->child)
-                       usb_disconnect(&hub->ports[i]->child);
-       }
+       hub_disconnect_children(udev);
 
        /* deallocate hcd/hardware state ... nuking all pending urbs and
         * cleaning up all state associated with the current configuration
@@ -2096,16 +2095,19 @@ void usb_disconnect(struct usb_device **pdev)
        usb_hcd_synchronize_unlinks(udev);
 
        if (udev->parent) {
-               struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
-               struct usb_port *port_dev = hub->ports[udev->portnum - 1];
+               port1 = udev->portnum;
+               hub = usb_hub_to_struct_hub(udev->parent);
+               port_dev = hub->ports[port1 - 1];
 
                sysfs_remove_link(&udev->dev.kobj, "port");
                sysfs_remove_link(&port_dev->dev.kobj, "device");
 
-               if (!port_dev->did_runtime_put)
-                       pm_runtime_put(&port_dev->dev);
-               else
-                       port_dev->did_runtime_put = false;
+               /*
+                * As usb_port_runtime_resume() de-references udev, make
+                * sure no resumes occur during removal
+                */
+               if (!test_and_set_bit(port1, hub->child_usage_bits))
+                       pm_runtime_get_sync(&port_dev->dev);
        }
 
        usb_remove_ep_devs(&udev->ep0);
@@ -2127,6 +2129,9 @@ void usb_disconnect(struct usb_device **pdev)
        *pdev = NULL;
        spin_unlock_irq(&device_state_lock);
 
+       if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
+               pm_runtime_put(&port_dev->dev);
+
        hub_free_dev(udev);
 
        put_device(&udev->dev);
@@ -2416,7 +2421,8 @@ int usb_new_device(struct usb_device *udev)
        /* Create link files between child device and usb port device. */
        if (udev->parent) {
                struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
-               struct usb_port *port_dev = hub->ports[udev->portnum - 1];
+               int port1 = udev->portnum;
+               struct usb_port *port_dev = hub->ports[port1 - 1];
 
                err = sysfs_create_link(&udev->dev.kobj,
                                &port_dev->dev.kobj, "port");
@@ -2430,7 +2436,8 @@ int usb_new_device(struct usb_device *udev)
                        goto fail;
                }
 
-               pm_runtime_get_sync(&port_dev->dev);
+               if (!test_and_set_bit(port1, hub->child_usage_bits))
+                       pm_runtime_get_sync(&port_dev->dev);
        }
 
        (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
@@ -2790,6 +2797,20 @@ static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
        return ret;
 }
 
+static void usb_lock_port(struct usb_port *port_dev)
+               __acquires(&port_dev->status_lock)
+{
+       mutex_lock(&port_dev->status_lock);
+       __acquire(&port_dev->status_lock);
+}
+
+static void usb_unlock_port(struct usb_port *port_dev)
+               __releases(&port_dev->status_lock)
+{
+       mutex_unlock(&port_dev->status_lock);
+       __release(&port_dev->status_lock);
+}
+
 #ifdef CONFIG_PM
 
 /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
@@ -3012,6 +3033,8 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
        int             status;
        bool            really_suspend = true;
 
+       usb_lock_port(port_dev);
+
        /* enable remote wakeup when appropriate; this lets the device
         * wake up the upstream hub (including maybe the root hub).
         *
@@ -3100,12 +3123,13 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
                usb_set_device_state(udev, USB_STATE_SUSPENDED);
        }
 
-       if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled) {
+       if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
+                       && test_and_clear_bit(port1, hub->child_usage_bits))
                pm_runtime_put_sync(&port_dev->dev);
-               port_dev->did_runtime_put = true;
-       }
 
        usb_mark_last_busy(hub->hdev);
+
+       usb_unlock_port(port_dev);
        return status;
 }
 
@@ -3245,9 +3269,8 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
        int             status;
        u16             portchange, portstatus;
 
-       if (port_dev->did_runtime_put) {
+       if (!test_and_set_bit(port1, hub->child_usage_bits)) {
                status = pm_runtime_get_sync(&port_dev->dev);
-               port_dev->did_runtime_put = false;
                if (status < 0) {
                        dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
                                        status);
@@ -3255,13 +3278,13 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
                }
        }
 
+       usb_lock_port(port_dev);
+
        /* Skip the initial Clear-Suspend step for a remote wakeup */
        status = hub_port_status(hub, port1, &portstatus, &portchange);
        if (status == 0 && !port_is_suspended(hub, portstatus))
                goto SuspendCleared;
 
-       set_bit(port1, hub->busy_bits);
-
        /* see 7.1.7.7; affects power usage, but not budgeting */
        if (hub_is_superspeed(hub->hdev))
                status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
@@ -3300,8 +3323,6 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
                }
        }
 
-       clear_bit(port1, hub->busy_bits);
-
        status = check_port_resume_type(udev,
                        hub, port1, status, portchange, portstatus);
        if (status == 0)
@@ -3319,16 +3340,18 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
                usb_unlocked_enable_lpm(udev);
        }
 
+       usb_unlock_port(port_dev);
+
        return status;
 }
 
 #ifdef CONFIG_PM_RUNTIME
 
-/* caller has locked udev */
 int usb_remote_wakeup(struct usb_device *udev)
 {
        int     status = 0;
 
+       usb_lock_device(udev);
        if (udev->state == USB_STATE_SUSPENDED) {
                dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
                status = usb_autoresume_device(udev);
@@ -3337,9 +3360,59 @@ int usb_remote_wakeup(struct usb_device *udev)
                        usb_autosuspend_device(udev);
                }
        }
+       usb_unlock_device(udev);
        return status;
 }
 
+/* Returns 1 if there was a remote wakeup and a connect status change. */
+static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
+               u16 portstatus, u16 portchange)
+               __must_hold(&port_dev->status_lock)
+{
+       struct usb_port *port_dev = hub->ports[port - 1];
+       struct usb_device *hdev;
+       struct usb_device *udev;
+       int connect_change = 0;
+       int ret;
+
+       hdev = hub->hdev;
+       udev = port_dev->child;
+       if (!hub_is_superspeed(hdev)) {
+               if (!(portchange & USB_PORT_STAT_C_SUSPEND))
+                       return 0;
+               usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
+       } else {
+               if (!udev || udev->state != USB_STATE_SUSPENDED ||
+                                (portstatus & USB_PORT_STAT_LINK_STATE) !=
+                                USB_SS_PORT_LS_U0)
+                       return 0;
+       }
+
+       if (udev) {
+               /* TRSMRCY = 10 msec */
+               msleep(10);
+
+               usb_unlock_port(port_dev);
+               ret = usb_remote_wakeup(udev);
+               usb_lock_port(port_dev);
+               if (ret < 0)
+                       connect_change = 1;
+       } else {
+               ret = -ENODEV;
+               hub_port_disable(hub, port, 1);
+       }
+       dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
+       return connect_change;
+}
+
+#else
+
+static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
+               u16 portstatus, u16 portchange)
+{
+       return 0;
+}
+
 #endif
 
 static int check_ports_changed(struct usb_hub *hub)
@@ -3893,6 +3966,12 @@ EXPORT_SYMBOL_GPL(usb_disable_ltm);
 void usb_enable_ltm(struct usb_device *udev) { }
 EXPORT_SYMBOL_GPL(usb_enable_ltm);
 
+static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
+               u16 portstatus, u16 portchange)
+{
+       return 0;
+}
+
 #endif /* CONFIG_PM */
 
 
@@ -4041,9 +4120,10 @@ static int hub_enable_device(struct usb_device *udev)
  * Returns device in USB_STATE_ADDRESS, except on error.
  *
  * If this is called for an already-existing device (as part of
- * usb_reset_and_verify_device), the caller must own the device lock.  For a
- * newly detected device that is not accessible through any global
- * pointers, it's not necessary to lock the device.
+ * usb_reset_and_verify_device), the caller must own the device lock and
+ * the port lock.  For a newly detected device that is not accessible
+ * through any global pointers, it's not necessary to lock the device,
+ * but it is still necessary to lock the port.
  */
 static int
 hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
@@ -4424,66 +4504,15 @@ hub_power_remaining (struct usb_hub *hub)
        return remaining;
 }
 
-/* Handle physical or logical connection change events.
- * This routine is called when:
- *     a port connection-change occurs;
- *     a port enable-change occurs (often caused by EMI);
- *     usb_reset_and_verify_device() encounters changed descriptors (as from
- *             a firmware download)
- * caller already locked the hub
- */
-static void hub_port_connect_change(struct usb_hub *hub, int port1,
-                                       u16 portstatus, u16 portchange)
+static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
+               u16 portchange)
 {
+       int status, i;
+       unsigned unit_load;
        struct usb_device *hdev = hub->hdev;
        struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
        struct usb_port *port_dev = hub->ports[port1 - 1];
-       struct usb_device *udev;
-       int status, i;
-       unsigned unit_load;
-
-       dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n",
-                       portstatus, portchange, portspeed(hub, portstatus));
-
-       if (hub->has_indicators) {
-               set_port_led(hub, port1, HUB_LED_AUTO);
-               hub->indicator[port1-1] = INDICATOR_AUTO;
-       }
-
-#ifdef CONFIG_USB_OTG
-       /* during HNP, don't repeat the debounce */
-       if (hdev->bus->is_b_host)
-               portchange &= ~(USB_PORT_STAT_C_CONNECTION |
-                               USB_PORT_STAT_C_ENABLE);
-#endif
-
-       /* Try to resuscitate an existing device */
-       udev = port_dev->child;
-       if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
-                       udev->state != USB_STATE_NOTATTACHED) {
-               usb_lock_device(udev);
-               if (portstatus & USB_PORT_STAT_ENABLE) {
-                       status = 0;             /* Nothing to do */
-
-#ifdef CONFIG_PM_RUNTIME
-               } else if (udev->state == USB_STATE_SUSPENDED &&
-                               udev->persist_enabled) {
-                       /* For a suspended device, treat this as a
-                        * remote wakeup event.
-                        */
-                       status = usb_remote_wakeup(udev);
-#endif
-
-               } else {
-                       status = -ENODEV;       /* Don't resuscitate */
-               }
-               usb_unlock_device(udev);
-
-               if (status == 0) {
-                       clear_bit(port1, hub->change_bits);
-                       return;
-               }
-       }
+       struct usb_device *udev = port_dev->child;
 
        /* Disconnect any existing devices under this port */
        if (udev) {
@@ -4492,7 +4521,6 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
                        usb_phy_notify_disconnect(hcd->phy, udev->speed);
                usb_disconnect(&port_dev->child);
        }
-       clear_bit(port1, hub->change_bits);
 
        /* We can forget about a "removed" device when there's a physical
         * disconnect or the connect status changes.
@@ -4565,7 +4593,9 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
                }
 
                /* reset (non-USB 3.0 devices) and get descriptor */
+               usb_lock_port(port_dev);
                status = hub_port_init(hub, udev, port1, i);
+               usb_unlock_port(port_dev);
                if (status < 0)
                        goto loop;
 
@@ -4674,48 +4704,192 @@ done:
        hub_port_disable(hub, port1, 1);
        if (hcd->driver->relinquish_port && !hub->hdev->parent)
                hcd->driver->relinquish_port(hcd, port1);
+
 }
 
-/* Returns 1 if there was a remote wakeup and a connect status change. */
-static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
-               u16 portstatus, u16 portchange)
+/* Handle physical or logical connection change events.
+ * This routine is called when:
+ *     a port connection-change occurs;
+ *     a port enable-change occurs (often caused by EMI);
+ *     usb_reset_and_verify_device() encounters changed descriptors (as from
+ *             a firmware download)
+ * caller already locked the hub
+ */
+static void hub_port_connect_change(struct usb_hub *hub, int port1,
+                                       u16 portstatus, u16 portchange)
+               __must_hold(&port_dev->status_lock)
 {
-       struct usb_port *port_dev = hub->ports[port - 1];
-       struct usb_device *hdev;
-       struct usb_device *udev;
-       int connect_change = 0;
-       int ret;
+       struct usb_port *port_dev = hub->ports[port1 - 1];
+       struct usb_device *udev = port_dev->child;
+       int status = -ENODEV;
 
-       hdev = hub->hdev;
-       udev = port_dev->child;
-       if (!hub_is_superspeed(hdev)) {
-               if (!(portchange & USB_PORT_STAT_C_SUSPEND))
-                       return 0;
-               usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
-       } else {
-               if (!udev || udev->state != USB_STATE_SUSPENDED ||
-                                (portstatus & USB_PORT_STAT_LINK_STATE) !=
-                                USB_SS_PORT_LS_U0)
-                       return 0;
+       dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
+                       portchange, portspeed(hub, portstatus));
+
+       if (hub->has_indicators) {
+               set_port_led(hub, port1, HUB_LED_AUTO);
+               hub->indicator[port1-1] = INDICATOR_AUTO;
        }
 
-       if (udev) {
-               /* TRSMRCY = 10 msec */
-               msleep(10);
+#ifdef CONFIG_USB_OTG
+       /* during HNP, don't repeat the debounce */
+       if (hub->hdev->bus->is_b_host)
+               portchange &= ~(USB_PORT_STAT_C_CONNECTION |
+                               USB_PORT_STAT_C_ENABLE);
+#endif
+
+       /* Try to resuscitate an existing device */
+       if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
+                       udev->state != USB_STATE_NOTATTACHED) {
+               if (portstatus & USB_PORT_STAT_ENABLE) {
+                       status = 0;             /* Nothing to do */
+#ifdef CONFIG_PM_RUNTIME
+               } else if (udev->state == USB_STATE_SUSPENDED &&
+                               udev->persist_enabled) {
+                       /* For a suspended device, treat this as a
+                        * remote wakeup event.
+                        */
+                       usb_unlock_port(port_dev);
+                       status = usb_remote_wakeup(udev);
+                       usb_lock_port(port_dev);
+#endif
+               } else {
+                       /* Don't resuscitate */;
+               }
+       }
+       clear_bit(port1, hub->change_bits);
+
+       /* successfully revalidated the connection */
+       if (status == 0)
+               return;
+
+       usb_unlock_port(port_dev);
+       hub_port_connect(hub, port1, portstatus, portchange);
+       usb_lock_port(port_dev);
+}
+
+static void port_event(struct usb_hub *hub, int port1)
+               __must_hold(&port_dev->status_lock)
+{
+       int connect_change, reset_device = 0;
+       struct usb_port *port_dev = hub->ports[port1 - 1];
+       struct usb_device *udev = port_dev->child;
+       struct usb_device *hdev = hub->hdev;
+       u16 portstatus, portchange;
+
+       connect_change = test_bit(port1, hub->change_bits);
+       clear_bit(port1, hub->event_bits);
+       clear_bit(port1, hub->wakeup_bits);
+
+       if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
+               return;
+
+       if (portchange & USB_PORT_STAT_C_CONNECTION) {
+               usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
+               connect_change = 1;
+       }
+
+       if (portchange & USB_PORT_STAT_C_ENABLE) {
+               if (!connect_change)
+                       dev_dbg(&port_dev->dev, "enable change, status %08x\n",
+                                       portstatus);
+               usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
+
+               /*
+                * EM interference sometimes causes badly shielded USB devices
+                * to be shutdown by the hub, this hack enables them again.
+                * Works at least with mouse driver.
+                */
+               if (!(portstatus & USB_PORT_STAT_ENABLE)
+                   && !connect_change && udev) {
+                       dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
+                       connect_change = 1;
+               }
+       }
+
+       if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
+               u16 status = 0, unused;
 
+               dev_dbg(&port_dev->dev, "over-current change\n");
+               usb_clear_port_feature(hdev, port1,
+                               USB_PORT_FEAT_C_OVER_CURRENT);
+               msleep(100);    /* Cool down */
+               hub_power_on(hub, true);
+               hub_port_status(hub, port1, &status, &unused);
+               if (status & USB_PORT_STAT_OVERCURRENT)
+                       dev_err(&port_dev->dev, "over-current condition\n");
+       }
+
+       if (portchange & USB_PORT_STAT_C_RESET) {
+               dev_dbg(&port_dev->dev, "reset change\n");
+               usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
+       }
+       if ((portchange & USB_PORT_STAT_C_BH_RESET)
+           && hub_is_superspeed(hdev)) {
+               dev_dbg(&port_dev->dev, "warm reset change\n");
+               usb_clear_port_feature(hdev, port1,
+                               USB_PORT_FEAT_C_BH_PORT_RESET);
+       }
+       if (portchange & USB_PORT_STAT_C_LINK_STATE) {
+               dev_dbg(&port_dev->dev, "link state change\n");
+               usb_clear_port_feature(hdev, port1,
+                               USB_PORT_FEAT_C_PORT_LINK_STATE);
+       }
+       if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
+               dev_warn(&port_dev->dev, "config error\n");
+               usb_clear_port_feature(hdev, port1,
+                               USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
+       }
+
+       /* skip port actions that require the port to be powered on */
+       if (!pm_runtime_active(&port_dev->dev))
+               return;
+
+       if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
+               connect_change = 1;
+
+       /*
+        * Warm reset a USB3 protocol port if it's in
+        * SS.Inactive state.
+        */
+       if (hub_port_warm_reset_required(hub, portstatus)) {
+               dev_dbg(&port_dev->dev, "do warm reset\n");
+               if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
+                               || udev->state == USB_STATE_NOTATTACHED) {
+                       if (hub_port_reset(hub, port1, NULL,
+                                       HUB_BH_RESET_TIME, true) < 0)
+                               hub_port_disable(hub, port1, 1);
+               } else
+                       reset_device = 1;
+       }
+
+       /*
+        * On disconnect USB3 protocol ports transit from U0 to
+        * SS.Inactive to Rx.Detect. If this happens a warm-
+        * reset is not needed, but a (re)connect may happen
+        * before khubd runs and sees the disconnect, and the
+        * device may be an unknown state.
+        *
+        * If the port went through SS.Inactive without khubd
+        * seeing it the C_LINK_STATE change flag will be set,
+        * and we reset the dev to put it in a known state.
+        */
+       if (reset_device || (udev && hub_is_superspeed(hub->hdev)
+                               && (portchange & USB_PORT_STAT_C_LINK_STATE)
+                               && (portstatus & USB_PORT_STAT_CONNECTION))) {
+               usb_unlock_port(port_dev);
                usb_lock_device(udev);
-               ret = usb_remote_wakeup(udev);
+               usb_reset_device(udev);
                usb_unlock_device(udev);
-               if (ret < 0)
-                       connect_change = 1;
-       } else {
-               ret = -ENODEV;
-               hub_port_disable(hub, port, 1);
+               usb_lock_port(port_dev);
+               connect_change = 0;
        }
-       dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
-       return connect_change;
+
+       if (connect_change)
+               hub_port_connect_change(hub, port1, portstatus, portchange);
 }
 
+
 static void hub_events(void)
 {
        struct list_head *tmp;
@@ -4725,10 +4899,7 @@ static void hub_events(void)
        struct device *hub_dev;
        u16 hubstatus;
        u16 hubchange;
-       u16 portstatus;
-       u16 portchange;
        int i, ret;
-       int connect_change, wakeup_change;
 
        /*
         *  We restart the list every time to avoid a deadlock with
@@ -4803,134 +4974,27 @@ static void hub_events(void)
                /* deal with port status changes */
                for (i = 1; i <= hdev->maxchild; i++) {
                        struct usb_port *port_dev = hub->ports[i - 1];
-                       struct usb_device *udev = port_dev->child;
-
-                       if (test_bit(i, hub->busy_bits))
-                               continue;
-                       connect_change = test_bit(i, hub->change_bits);
-                       wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
-                       if (!test_and_clear_bit(i, hub->event_bits) &&
-                                       !connect_change && !wakeup_change)
-                               continue;
-
-                       ret = hub_port_status(hub, i,
-                                       &portstatus, &portchange);
-                       if (ret < 0)
-                               continue;
-
-                       if (portchange & USB_PORT_STAT_C_CONNECTION) {
-                               usb_clear_port_feature(hdev, i,
-                                       USB_PORT_FEAT_C_CONNECTION);
-                               connect_change = 1;
-                       }
-
-                       if (portchange & USB_PORT_STAT_C_ENABLE) {
-                               if (!connect_change)
-                                       dev_dbg(&port_dev->dev,
-                                                       "enable change, status %08x\n",
-                                                        portstatus);
-                               usb_clear_port_feature(hdev, i,
-                                       USB_PORT_FEAT_C_ENABLE);
 
+                       if (test_bit(i, hub->event_bits)
+                                       || test_bit(i, hub->change_bits)
+                                       || test_bit(i, hub->wakeup_bits)) {
                                /*
-                                * EM interference sometimes causes badly
-                                * shielded USB devices to be shutdown by
-                                * the hub, this hack enables them again.
-                                * Works at least with mouse driver.
+                                * The get_noresume and barrier ensure that if
+                                * the port was in the process of resuming, we
+                                * flush that work and keep the port active for
+                                * the duration of the port_event().  However,
+                                * if the port is runtime pm suspended
+                                * (powered-off), we leave it in that state, run
+                                * an abbreviated port_event(), and move on.
                                 */
-                               if (!(portstatus & USB_PORT_STAT_ENABLE)
-                                   && !connect_change && udev) {
-                                       dev_err(&port_dev->dev,
-                                                       "disabled by hub (EMI?), re-enabling...\n");
-                                       connect_change = 1;
-                               }
-                       }
-
-                       if (hub_handle_remote_wakeup(hub, i,
-                                               portstatus, portchange))
-                               connect_change = 1;
-
-                       if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
-                               u16 status = 0;
-                               u16 unused;
-
-                               dev_dbg(&port_dev->dev, "over-current change\n");
-                               usb_clear_port_feature(hdev, i,
-                                       USB_PORT_FEAT_C_OVER_CURRENT);
-                               msleep(100);    /* Cool down */
-                               hub_power_on(hub, true);
-                               hub_port_status(hub, i, &status, &unused);
-                               if (status & USB_PORT_STAT_OVERCURRENT)
-                                       dev_err(&port_dev->dev,
-                                                       "over-current condition\n");
-                       }
-
-                       if (portchange & USB_PORT_STAT_C_RESET) {
-                               dev_dbg(&port_dev->dev, "reset change\n");
-                               usb_clear_port_feature(hdev, i,
-                                       USB_PORT_FEAT_C_RESET);
-                       }
-                       if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
-                                       hub_is_superspeed(hub->hdev)) {
-                               dev_dbg(&port_dev->dev, "warm reset change\n");
-                               usb_clear_port_feature(hdev, i,
-                                       USB_PORT_FEAT_C_BH_PORT_RESET);
-                       }
-                       if (portchange & USB_PORT_STAT_C_LINK_STATE) {
-                               usb_clear_port_feature(hub->hdev, i,
-                                               USB_PORT_FEAT_C_PORT_LINK_STATE);
-                       }
-                       if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
-                               dev_warn(&port_dev->dev, "config error\n");
-                               usb_clear_port_feature(hub->hdev, i,
-                                               USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
-                       }
-
-                       /* Warm reset a USB3 protocol port if it's in
-                        * SS.Inactive state.
-                        */
-                       if (hub_port_warm_reset_required(hub, portstatus)) {
-                               int status;
-
-                               dev_dbg(&port_dev->dev, "warm reset\n");
-                               if (!udev ||
-                                   !(portstatus & USB_PORT_STAT_CONNECTION) ||
-                                   udev->state == USB_STATE_NOTATTACHED) {
-                                       status = hub_port_reset(hub, i,
-                                                       NULL, HUB_BH_RESET_TIME,
-                                                       true);
-                                       if (status < 0)
-                                               hub_port_disable(hub, i, 1);
-                               } else {
-                                       usb_lock_device(udev);
-                                       status = usb_reset_device(udev);
-                                       usb_unlock_device(udev);
-                                       connect_change = 0;
-                               }
-                       /*
-                        * On disconnect USB3 protocol ports transit from U0 to
-                        * SS.Inactive to Rx.Detect. If this happens a warm-
-                        * reset is not needed, but a (re)connect may happen
-                        * before khubd runs and sees the disconnect, and the
-                        * device may be an unknown state.
-                        *
-                        * If the port went through SS.Inactive without khubd
-                        * seeing it the C_LINK_STATE change flag will be set,
-                        * and we reset the dev to put it in a known state.
-                        */
-                       } else if (udev && hub_is_superspeed(hub->hdev) &&
-                                  (portchange & USB_PORT_STAT_C_LINK_STATE) &&
-                                  (portstatus & USB_PORT_STAT_CONNECTION)) {
-                               usb_lock_device(udev);
-                               usb_reset_device(udev);
-                               usb_unlock_device(udev);
-                               connect_change = 0;
+                               pm_runtime_get_noresume(&port_dev->dev);
+                               pm_runtime_barrier(&port_dev->dev);
+                               usb_lock_port(port_dev);
+                               port_event(hub, i);
+                               usb_unlock_port(port_dev);
+                               pm_runtime_put_sync(&port_dev->dev);
                        }
-
-                       if (connect_change)
-                               hub_port_connect_change(hub, i,
-                                               portstatus, portchange);
-               } /* end for i */
+               }
 
                /* deal with hub status changes */
                if (test_and_clear_bit(0, hub->event_bits) == 0)
@@ -5165,15 +5229,18 @@ static int descriptors_changed(struct usb_device *udev,
  * if the reset wasn't even attempted.
  *
  * Note:
- * The caller must own the device lock.  For example, it's safe to use
- * this from a driver probe() routine after downloading new firmware.
- * For calls that might not occur during probe(), drivers should lock
- * the device using usb_lock_device_for_reset().
+ * The caller must own the device lock and the port lock, the latter is
+ * taken by usb_reset_device().  For example, it's safe to use
+ * usb_reset_device() from a driver probe() routine after downloading
+ * new firmware.  For calls that might not occur during probe(), drivers
+ * should lock the device using usb_lock_device_for_reset().
  *
  * Locking exception: This routine may also be called from within an
  * autoresume handler.  Such usage won't conflict with other tasks
  * holding the device lock because these tasks should always call
- * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
+ * usb_autopm_resume_device(), thereby preventing any unwanted
+ * autoresume.  The autoresume handler is expected to have already
+ * acquired the port lock before calling this routine.
  */
 static int usb_reset_and_verify_device(struct usb_device *udev)
 {
@@ -5192,11 +5259,9 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
                return -EINVAL;
        }
 
-       if (!parent_hdev) {
-               /* this requires hcd-specific logic; see ohci_restart() */
-               dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
+       if (!parent_hdev)
                return -EISDIR;
-       }
+
        parent_hub = usb_hub_to_struct_hub(parent_hdev);
 
        /* Disable USB2 hardware LPM.
@@ -5225,7 +5290,6 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
                goto re_enumerate;
        }
 
-       set_bit(port1, parent_hub->busy_bits);
        for (i = 0; i < SET_CONFIG_TRIES; ++i) {
 
                /* ep0 maxpacket size may change; let the HCD know about it.
@@ -5235,7 +5299,6 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
                if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
                        break;
        }
-       clear_bit(port1, parent_hub->busy_bits);
 
        if (ret < 0)
                goto re_enumerate;
@@ -5356,7 +5419,9 @@ int usb_reset_device(struct usb_device *udev)
        int ret;
        int i;
        unsigned int noio_flag;
+       struct usb_port *port_dev;
        struct usb_host_config *config = udev->actconfig;
+       struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
 
        if (udev->state == USB_STATE_NOTATTACHED ||
                        udev->state == USB_STATE_SUSPENDED) {
@@ -5365,6 +5430,14 @@ int usb_reset_device(struct usb_device *udev)
                return -EINVAL;
        }
 
+       if (!udev->parent) {
+               /* this requires hcd-specific logic; see ohci_restart() */
+               dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
+               return -EISDIR;
+       }
+
+       port_dev = hub->ports[udev->portnum - 1];
+
        /*
         * Don't allocate memory with GFP_KERNEL in current
         * context to avoid possible deadlock if usb mass
@@ -5398,7 +5471,9 @@ int usb_reset_device(struct usb_device *udev)
                }
        }
 
+       usb_lock_port(port_dev);
        ret = usb_reset_and_verify_device(udev);
+       usb_unlock_port(port_dev);
 
        if (config) {
                for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {