If a port is powered-off, or in the process of being powered-off, prevent
khubd from operating on it.  Otherwise, the following sequence of events
leading to an unintended disconnect may occur:

Events:
(0) <set pm_qos_no_poweroff to '0' for port1>
(1) hub 2-2:1.0: hub_resume
(2) hub 2-2:1.0: port 1: status 0301 change 0000
(3) hub 2-2:1.0: state 7 ports 4 chg 0002 evt 0000
(4) hub 2-2:1.0: port 1, power off status 0000, change 0000, 12 Mb/s
(5) usb 2-2.1: USB disconnect, device number 5

Description:
(1) hub is resumed before sending a ClearPortFeature request
(2) hub_activate() notices the port is connected and sets
    hub->change_bits for the port
(3) hub_events() starts, but at the same time the port suspends
(4) hub_connect_change() sees the disabled port and triggers disconnect

Signed-off-by: Dan Williams <dan.j.willi...@intel.com>
---
 drivers/usb/core/hub.c |   38 +++++++++++++++++++++++++++++++++-----
 1 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index d86548edcc36..0fa13a52b52a 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -4654,6 +4654,35 @@ static int hub_handle_remote_wakeup(struct usb_hub *hub, 
unsigned int port,
        return connect_change;
 }
 
+static void put_port(struct usb_port *port_dev, int *port1)
+{
+       if (!port_dev)
+               return;
+
+       pm_runtime_put_sync(&port_dev->dev);
+       (*port1)++;
+}
+
+static struct usb_port *next_active_port(struct usb_hub *hub, int *port1)
+{
+       struct usb_device *hdev = hub->hdev;
+
+       while (*port1 <= hdev->maxchild) {
+               struct usb_port *port_dev = hub->ports[*port1 - 1];
+
+               pm_runtime_get_noresume(&port_dev->dev);
+               pm_runtime_barrier(&port_dev->dev);
+               if (pm_runtime_active(&port_dev->dev))
+                       return port_dev;
+               put_port(port_dev, port1);
+       }
+
+       return NULL;
+}
+
+#define for_each_pm_active_port(i, p, h) \
+       for (i = 1; (p = next_active_port(h, &i)); put_port(p, &i))
+
 static void hub_events(void)
 {
        struct list_head *tmp;
@@ -4661,6 +4690,7 @@ static void hub_events(void)
        struct usb_interface *intf;
        struct usb_hub *hub;
        struct device *hub_dev;
+       struct usb_port *port_dev;
        u16 hubstatus;
        u16 hubchange;
        u16 portstatus;
@@ -4739,7 +4769,7 @@ static void hub_events(void)
                }
 
                /* deal with port status changes */
-               for (i = 1; i <= hdev->maxchild; i++) {
+               for_each_pm_active_port(i, port_dev, hub) {
                        if (test_bit(i, hub->busy_bits))
                                continue;
                        connect_change = test_bit(i, hub->change_bits);
@@ -4775,8 +4805,7 @@ static void hub_events(void)
                                 * Works at least with mouse driver.
                                 */
                                if (!(portstatus & USB_PORT_STAT_ENABLE)
-                                   && !connect_change
-                                   && hub->ports[i - 1]->child) {
+                                   && !connect_change && port_dev->child) {
                                        dev_err (hub_dev,
                                            "port %i "
                                            "disabled by hub (EMI?), "
@@ -4838,8 +4867,7 @@ static void hub_events(void)
                         */
                        if (hub_port_warm_reset_required(hub, portstatus)) {
                                int status;
-                               struct usb_device *udev =
-                                       hub->ports[i - 1]->child;
+                               struct usb_device *udev = port_dev->child;
 
                                dev_dbg(hub_dev, "warm reset port %d\n", i);
                                if (!udev ||

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to