usb: add usb port auto power off mechanism
authorLan Tianyu <tianyu.lan@intel.com>
Tue, 22 Jan 2013 20:26:30 +0000 (04:26 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 25 Jan 2013 18:14:20 +0000 (10:14 -0800)
This patch is to add usb port auto power off mechanism.
When usb device is suspending, usb core will suspend usb port and
usb port runtime pm callback will clear PORT_POWER feature to
power off port if all conditions were met. These conditions are
remote wakeup disable, pm qos NO_POWER_OFF flag clear and persist
enable. When it resumes, power on port again.

Add did_runtime_put in the struct usb_port to ensure
pm_runtime_get/put(portdev) to be called pairedly.  Set did_runtime_put
to true when call pm_runtime_put(portdev) during suspending. The
pm_runtime_get(portdev) only will be called when did_runtime_put
is set to true during resuming. Set did_runtime_put to false after
calling pm_runtime_get(portdev).

Make clear_port_feature() and hdev_to_hub() as global symbol.
Rename clear_port_feature() to usb_clear_port_feature() and
hdev_to_hub() to usb_hub_to_struct_hub().

Extend hub_port_debounce() with the fuction of debouncing to
be connected. Add two wraps: hub_port_debounce_be_connected()
and hub_port_debouce_be_stable().

Increase HUB_DEBOUNCE_TIMEOUT to 2000 because some usb ssds
needs around 1.5 or more to make the hub port status to be
connected steadily after being powered off and powered on.

Acked-by: Alan Stern <stern@rowland.harvard.edu>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Lan Tianyu <tianyu.lan@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/core/hub.c
drivers/usb/core/hub.h
drivers/usb/core/port.c

index 7fb163365d0290634f4b6ce351afb6b48c8b30d6..0883364e73474893fb0268905cd373e07d3cc0bc 100644 (file)
@@ -26,6 +26,7 @@
 #include <linux/mutex.h>
 #include <linux/freezer.h>
 #include <linux/random.h>
+#include <linux/pm_qos.h>
 
 #include <asm/uaccess.h>
 #include <asm/byteorder.h>
@@ -108,7 +109,7 @@ MODULE_PARM_DESC(use_both_schemes,
 DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
 EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
 
-#define HUB_DEBOUNCE_TIMEOUT   1500
+#define HUB_DEBOUNCE_TIMEOUT   2000
 #define HUB_DEBOUNCE_STEP        25
 #define HUB_DEBOUNCE_STABLE     100
 
@@ -127,7 +128,7 @@ static inline char *portspeed(struct usb_hub *hub, int portstatus)
 }
 
 /* Note that hdev or one of its children must be locked! */
-static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
+struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
 {
        if (!hdev || !hdev->actconfig || !hdev->maxchild)
                return NULL;
@@ -301,7 +302,7 @@ static void usb_set_lpm_parameters(struct usb_device *udev)
        if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
                return;
 
-       hub = hdev_to_hub(udev->parent);
+       hub = usb_hub_to_struct_hub(udev->parent);
        /* It doesn't take time to transition the roothub into U0, since it
         * doesn't have an upstream link.
         */
@@ -393,7 +394,7 @@ static int clear_hub_feature(struct usb_device *hdev, int feature)
 /*
  * USB 2.0 spec Section 11.24.2.2
  */
-static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
+int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
 {
        return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
                USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
@@ -586,7 +587,7 @@ static void kick_khubd(struct usb_hub *hub)
 
 void usb_kick_khubd(struct usb_device *hdev)
 {
-       struct usb_hub *hub = hdev_to_hub(hdev);
+       struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
 
        if (hub)
                kick_khubd(hub);
@@ -608,7 +609,7 @@ void usb_wakeup_notification(struct usb_device *hdev,
        if (!hdev)
                return;
 
-       hub = hdev_to_hub(hdev);
+       hub = usb_hub_to_struct_hub(hdev);
        if (hub) {
                set_bit(portnum, hub->wakeup_bits);
                kick_khubd(hub);
@@ -727,11 +728,16 @@ int usb_hub_set_port_power(struct usb_device *hdev, int port1,
                bool set)
 {
        int ret;
+       struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
+       struct usb_port *port_dev = hub->ports[port1 - 1];
 
        if (set)
                ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
        else
-               ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
+               ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
+
+       if (!ret)
+               port_dev->power_is_on = set;
        return ret;
 }
 
@@ -811,7 +817,11 @@ 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->descriptor->bNbrPorts; port1++)
-               set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
+               if (hub->ports[port1 - 1]->power_is_on)
+                       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);
@@ -905,7 +915,7 @@ static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
                if (hub_is_superspeed(hub->hdev))
                        ret = hub_usb3_port_disable(hub, port1);
                else
-                       ret = clear_port_feature(hdev, port1,
+                       ret = usb_clear_port_feature(hdev, port1,
                                        USB_PORT_FEAT_ENABLE);
        }
        if (ret)
@@ -954,7 +964,7 @@ int usb_remove_device(struct usb_device *udev)
 
        if (!udev->parent)      /* Can't remove a root hub */
                return -EINVAL;
-       hub = hdev_to_hub(udev->parent);
+       hub = usb_hub_to_struct_hub(udev->parent);
        intf = to_usb_interface(hub->intfdev);
 
        usb_autopm_get_interface(intf);
@@ -1086,7 +1096,7 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
                         * Do not disable USB3 protocol ports.
                         */
                        if (!hub_is_superspeed(hdev)) {
-                               clear_port_feature(hdev, port1,
+                               usb_clear_port_feature(hdev, port1,
                                                   USB_PORT_FEAT_ENABLE);
                                portstatus &= ~USB_PORT_STAT_ENABLE;
                        } else {
@@ -1098,18 +1108,18 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
                /* Clear status-change flags; we'll debounce later */
                if (portchange & USB_PORT_STAT_C_CONNECTION) {
                        need_debounce_delay = true;
-                       clear_port_feature(hub->hdev, port1,
+                       usb_clear_port_feature(hub->hdev, port1,
                                        USB_PORT_FEAT_C_CONNECTION);
                }
                if (portchange & USB_PORT_STAT_C_ENABLE) {
                        need_debounce_delay = true;
-                       clear_port_feature(hub->hdev, port1,
+                       usb_clear_port_feature(hub->hdev, port1,
                                        USB_PORT_FEAT_C_ENABLE);
                }
                if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
                                hub_is_superspeed(hub->hdev)) {
                        need_debounce_delay = true;
-                       clear_port_feature(hub->hdev, port1,
+                       usb_clear_port_feature(hub->hdev, port1,
                                        USB_PORT_FEAT_C_BH_PORT_RESET);
                }
                /* We can forget about a "removed" device when there's a
@@ -1143,10 +1153,16 @@ 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
-                       set_bit(port1, hub->change_bits);
+                       /* Don't set the change_bits when the device
+                        * was powered off.
+                        */
+                       if (port_dev->power_is_on)
+                               set_bit(port1, hub->change_bits);
 
                } else {
                        /* The power session is gone; tell khubd */
@@ -1712,7 +1728,7 @@ static int
 hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
 {
        struct usb_device *hdev = interface_to_usbdev (intf);
-       struct usb_hub *hub = hdev_to_hub(hdev);
+       struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
 
        /* assert ifno == 0 (part of hub spec) */
        switch (code) {
@@ -1758,7 +1774,7 @@ static int find_port_owner(struct usb_device *hdev, unsigned port1,
        /* This assumes that devices not managed by the hub driver
         * will always have maxchild equal to 0.
         */
-       *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
+       *ppowner = &(usb_hub_to_struct_hub(hdev)->ports[port1 - 1]->port_owner);
        return 0;
 }
 
@@ -1795,7 +1811,7 @@ int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
 
 void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
 {
-       struct usb_hub *hub = hdev_to_hub(hdev);
+       struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
        int n;
 
        for (n = 0; n < hdev->maxchild; n++) {
@@ -1812,13 +1828,13 @@ bool usb_device_is_owned(struct usb_device *udev)
 
        if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
                return false;
-       hub = hdev_to_hub(udev->parent);
+       hub = usb_hub_to_struct_hub(udev->parent);
        return !!hub->ports[udev->portnum - 1]->port_owner;
 }
 
 static void recursively_mark_NOTATTACHED(struct usb_device *udev)
 {
-       struct usb_hub *hub = hdev_to_hub(udev);
+       struct usb_hub *hub = usb_hub_to_struct_hub(udev);
        int i;
 
        for (i = 0; i < udev->maxchild; ++i) {
@@ -1987,7 +2003,7 @@ 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 = hdev_to_hub(udev);
+       struct usb_hub          *hub = usb_hub_to_struct_hub(udev);
        int                     i;
 
        /* mark the device as inactive, so any further urb submissions for
@@ -2015,13 +2031,16 @@ void usb_disconnect(struct usb_device **pdev)
        usb_hcd_synchronize_unlinks(udev);
 
        if (udev->parent) {
-               struct usb_port *port_dev =
-                       hdev_to_hub(udev->parent)->ports[udev->portnum - 1];
+               struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
+               struct usb_port *port_dev = hub->ports[udev->portnum - 1];
 
                sysfs_remove_link(&udev->dev.kobj, "port");
                sysfs_remove_link(&port_dev->dev.kobj, "device");
 
-               pm_runtime_put(&port_dev->dev);
+               if (!port_dev->did_runtime_put)
+                       pm_runtime_put(&port_dev->dev);
+               else
+                       port_dev->did_runtime_put = false;
        }
 
        usb_remove_ep_devs(&udev->ep0);
@@ -2210,7 +2229,7 @@ static void set_usb_port_removable(struct usb_device *udev)
        if (!hdev)
                return;
 
-       hub = hdev_to_hub(udev->parent);
+       hub = usb_hub_to_struct_hub(udev->parent);
 
        wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
 
@@ -2318,8 +2337,8 @@ int usb_new_device(struct usb_device *udev)
 
        /* Create link files between child device and usb port device. */
        if (udev->parent) {
-               struct usb_port *port_dev =
-                       hdev_to_hub(udev->parent)->ports[udev->portnum - 1];
+               struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
+               struct usb_port *port_dev = hub->ports[udev->portnum - 1];
 
                err = sysfs_create_link(&udev->dev.kobj,
                                &port_dev->dev.kobj, "port");
@@ -2567,14 +2586,14 @@ static void hub_port_finish_reset(struct usb_hub *hub, int port1,
                /* FALL THROUGH */
        case -ENOTCONN:
        case -ENODEV:
-               clear_port_feature(hub->hdev,
+               usb_clear_port_feature(hub->hdev,
                                port1, USB_PORT_FEAT_C_RESET);
                if (hub_is_superspeed(hub->hdev)) {
-                       clear_port_feature(hub->hdev, port1,
+                       usb_clear_port_feature(hub->hdev, port1,
                                        USB_PORT_FEAT_C_BH_PORT_RESET);
-                       clear_port_feature(hub->hdev, port1,
+                       usb_clear_port_feature(hub->hdev, port1,
                                        USB_PORT_FEAT_C_PORT_LINK_STATE);
-                       clear_port_feature(hub->hdev, port1,
+                       usb_clear_port_feature(hub->hdev, port1,
                                        USB_PORT_FEAT_C_CONNECTION);
                }
                if (udev)
@@ -2748,10 +2767,10 @@ static int check_port_resume_type(struct usb_device *udev,
 
                /* Late port handoff can set status-change bits */
                if (portchange & USB_PORT_STAT_C_CONNECTION)
-                       clear_port_feature(hub->hdev, port1,
+                       usb_clear_port_feature(hub->hdev, port1,
                                        USB_PORT_FEAT_C_CONNECTION);
                if (portchange & USB_PORT_STAT_C_ENABLE)
-                       clear_port_feature(hub->hdev, port1,
+                       usb_clear_port_feature(hub->hdev, port1,
                                        USB_PORT_FEAT_C_ENABLE);
        }
 
@@ -2852,7 +2871,9 @@ EXPORT_SYMBOL_GPL(usb_enable_ltm);
  */
 int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
 {
-       struct usb_hub  *hub = hdev_to_hub(udev->parent);
+       struct usb_hub  *hub = usb_hub_to_struct_hub(udev->parent);
+       struct usb_port *port_dev = hub->ports[udev->portnum - 1];
+       enum pm_qos_flags_status pm_qos_stat;
        int             port1 = udev->portnum;
        int             status;
 
@@ -2945,6 +2966,21 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
                udev->port_is_suspended = 1;
                msleep(10);
        }
+
+       /*
+        * Check whether current status meets the requirement of
+        * usb port power off mechanism
+        */
+       pm_qos_stat = dev_pm_qos_flags(&port_dev->dev,
+                       PM_QOS_FLAG_NO_POWER_OFF);
+       if (!udev->do_remote_wakeup
+                       && pm_qos_stat != PM_QOS_FLAGS_ALL
+                       && udev->persist_enabled
+                       && !status) {
+               pm_runtime_put_sync(&port_dev->dev);
+               port_dev->did_runtime_put = true;
+       }
+
        usb_mark_last_busy(hub->hdev);
        return status;
 }
@@ -3070,11 +3106,22 @@ static int finish_port_resume(struct usb_device *udev)
  */
 int usb_port_resume(struct usb_device *udev, pm_message_t msg)
 {
-       struct usb_hub  *hub = hdev_to_hub(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;
        int             status;
        u16             portchange, portstatus;
 
+       if (port_dev->did_runtime_put) {
+               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);
+                       return status;
+               }
+       }
+
        /* 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))
@@ -3088,7 +3135,7 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
        if (hub_is_superspeed(hub->hdev))
                status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
        else
-               status = clear_port_feature(hub->hdev,
+               status = usb_clear_port_feature(hub->hdev,
                                port1, USB_PORT_FEAT_SUSPEND);
        if (status) {
                dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
@@ -3114,11 +3161,11 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
                udev->port_is_suspended = 0;
                if (hub_is_superspeed(hub->hdev)) {
                        if (portchange & USB_PORT_STAT_C_LINK_STATE)
-                               clear_port_feature(hub->hdev, port1,
+                               usb_clear_port_feature(hub->hdev, port1,
                                        USB_PORT_FEAT_C_PORT_LINK_STATE);
                } else {
                        if (portchange & USB_PORT_STAT_C_SUSPEND)
-                               clear_port_feature(hub->hdev, port1,
+                               usb_clear_port_feature(hub->hdev, port1,
                                                USB_PORT_FEAT_C_SUSPEND);
                }
        }
@@ -3174,7 +3221,7 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
 
 int usb_port_resume(struct usb_device *udev, pm_message_t msg)
 {
-       struct usb_hub  *hub = hdev_to_hub(udev->parent);
+       struct usb_hub  *hub = usb_hub_to_struct_hub(udev->parent);
        int             port1 = udev->portnum;
        int             status;
        u16             portchange, portstatus;
@@ -3753,7 +3800,7 @@ EXPORT_SYMBOL_GPL(usb_enable_ltm);
  * every 25ms for transient disconnects.  When the port status has been
  * unchanged for 100ms it returns the port status.
  */
-static int hub_port_debounce(struct usb_hub *hub, int port1)
+int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
 {
        int ret;
        int total_time, stable_time = 0;
@@ -3767,7 +3814,9 @@ static int hub_port_debounce(struct usb_hub *hub, int port1)
 
                if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
                     (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
-                       stable_time += HUB_DEBOUNCE_STEP;
+                       if (!must_be_connected ||
+                            (connection == USB_PORT_STAT_CONNECTION))
+                               stable_time += HUB_DEBOUNCE_STEP;
                        if (stable_time >= HUB_DEBOUNCE_STABLE)
                                break;
                } else {
@@ -3776,7 +3825,7 @@ static int hub_port_debounce(struct usb_hub *hub, int port1)
                }
 
                if (portchange & USB_PORT_STAT_C_CONNECTION) {
-                       clear_port_feature(hub->hdev, port1,
+                       usb_clear_port_feature(hub->hdev, port1,
                                        USB_PORT_FEAT_C_CONNECTION);
                }
 
@@ -4288,7 +4337,7 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
 
        if (portchange & (USB_PORT_STAT_C_CONNECTION |
                                USB_PORT_STAT_C_ENABLE)) {
-               status = hub_port_debounce(hub, port1);
+               status = hub_port_debounce_be_stable(hub, port1);
                if (status < 0) {
                        if (printk_ratelimit())
                                dev_err(hub_dev, "connect-debounce failed, "
@@ -4467,7 +4516,7 @@ static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
        if (!hub_is_superspeed(hdev)) {
                if (!(portchange & USB_PORT_STAT_C_SUSPEND))
                        return 0;
-               clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
+               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) !=
@@ -4595,7 +4644,7 @@ static void hub_events(void)
                                continue;
 
                        if (portchange & USB_PORT_STAT_C_CONNECTION) {
-                               clear_port_feature(hdev, i,
+                               usb_clear_port_feature(hdev, i,
                                        USB_PORT_FEAT_C_CONNECTION);
                                connect_change = 1;
                        }
@@ -4606,7 +4655,7 @@ static void hub_events(void)
                                                "port %d enable change, "
                                                "status %08x\n",
                                                i, portstatus);
-                               clear_port_feature(hdev, i,
+                               usb_clear_port_feature(hdev, i,
                                        USB_PORT_FEAT_C_ENABLE);
 
                                /*
@@ -4637,7 +4686,7 @@ static void hub_events(void)
 
                                dev_dbg(hub_dev, "over-current change on port "
                                        "%d\n", i);
-                               clear_port_feature(hdev, i,
+                               usb_clear_port_feature(hdev, i,
                                        USB_PORT_FEAT_C_OVER_CURRENT);
                                msleep(100);    /* Cool down */
                                hub_power_on(hub, true);
@@ -4651,7 +4700,7 @@ static void hub_events(void)
                                dev_dbg (hub_dev,
                                        "reset change on port %d\n",
                                        i);
-                               clear_port_feature(hdev, i,
+                               usb_clear_port_feature(hdev, i,
                                        USB_PORT_FEAT_C_RESET);
                        }
                        if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
@@ -4659,18 +4708,18 @@ static void hub_events(void)
                                dev_dbg(hub_dev,
                                        "warm reset change on port %d\n",
                                        i);
-                               clear_port_feature(hdev, i,
+                               usb_clear_port_feature(hdev, i,
                                        USB_PORT_FEAT_C_BH_PORT_RESET);
                        }
                        if (portchange & USB_PORT_STAT_C_LINK_STATE) {
-                               clear_port_feature(hub->hdev, i,
+                               usb_clear_port_feature(hub->hdev, i,
                                                USB_PORT_FEAT_C_PORT_LINK_STATE);
                        }
                        if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
                                dev_warn(hub_dev,
                                        "config error on port %d\n",
                                        i);
-                               clear_port_feature(hub->hdev, i,
+                               usb_clear_port_feature(hub->hdev, i,
                                                USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
                        }
 
@@ -4954,7 +5003,7 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
                dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
                return -EISDIR;
        }
-       parent_hub = hdev_to_hub(parent_hdev);
+       parent_hub = usb_hub_to_struct_hub(parent_hdev);
 
        /* Disable LPM and LTM while we reset the device and reinstall the alt
         * settings.  Device-initiated LPM settings, and system exit latency
@@ -5210,7 +5259,7 @@ EXPORT_SYMBOL_GPL(usb_queue_reset_device);
 struct usb_device *usb_hub_find_child(struct usb_device *hdev,
                int port1)
 {
-       struct usb_hub *hub = hdev_to_hub(hdev);
+       struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
 
        if (port1 < 1 || port1 > hdev->maxchild)
                return NULL;
@@ -5227,7 +5276,7 @@ EXPORT_SYMBOL_GPL(usb_hub_find_child);
 void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
        enum usb_port_connect_type type)
 {
-       struct usb_hub *hub = hdev_to_hub(hdev);
+       struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
 
        hub->ports[port1 - 1]->connect_type = type;
 }
@@ -5243,7 +5292,7 @@ void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
 enum usb_port_connect_type
 usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
 {
-       struct usb_hub *hub = hdev_to_hub(hdev);
+       struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
 
        return hub->ports[port1 - 1]->connect_type;
 }
@@ -5301,7 +5350,7 @@ void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
 acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
        int port1)
 {
-       struct usb_hub *hub = hdev_to_hub(hdev);
+       struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
 
        return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
 }
index 452e5cd7b24982f91cd99865bd3e940bb087707c..80ab9ee070171a795a93758a271f96767156e68f 100644 (file)
@@ -80,6 +80,8 @@ struct usb_hub {
  * @port_owner: port's owner
  * @connect_type: port's connect type
  * @portnum: port index num based one
+ * @power_is_on: port's power state
+ * @did_runtime_put: port has done pm_runtime_put().
  */
 struct usb_port {
        struct usb_device *child;
@@ -87,6 +89,8 @@ struct usb_port {
        struct dev_state *port_owner;
        enum usb_port_connect_type connect_type;
        u8 portnum;
+       unsigned power_is_on:1;
+       unsigned did_runtime_put:1;
 };
 
 #define to_usb_port(_dev) \
@@ -98,4 +102,21 @@ extern void usb_hub_remove_port_device(struct usb_hub *hub,
                int port1);
 extern int usb_hub_set_port_power(struct usb_device *hdev,
                int port1, bool set);
+extern struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev);
+extern int hub_port_debounce(struct usb_hub *hub, int port1,
+               bool must_be_connected);
+extern int usb_clear_port_feature(struct usb_device *hdev,
+               int port1, int feature);
+
+static inline int hub_port_debounce_be_connected(struct usb_hub *hub,
+               int port1)
+{
+       return hub_port_debounce(hub, port1, true);
+}
+
+static inline int hub_port_debounce_be_stable(struct usb_hub *hub,
+               int port1)
+{
+       return hub_port_debounce(hub, port1, false);
+}
 
index d288dfed6ccfee146088b189ae7fd81d5a1ee99e..280433d808871f799782d5ce1d7b53993275fc1c 100644 (file)
@@ -77,10 +77,36 @@ static int usb_port_runtime_resume(struct device *dev)
        struct usb_port *port_dev = to_usb_port(dev);
        struct usb_device *hdev = to_usb_device(dev->parent->parent);
        struct usb_interface *intf = to_usb_interface(dev->parent);
+       struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
+       int port1 = port_dev->portnum;
        int retval;
 
+       if (!hub)
+               return -EINVAL;
+
        usb_autopm_get_interface(intf);
-       retval = usb_hub_set_port_power(hdev, port_dev->portnum, true);
+       set_bit(port1, hub->busy_bits);
+
+       retval = usb_hub_set_port_power(hdev, port1, true);
+       if (port_dev->child && !retval) {
+               /*
+                * Wait for usb hub port to be reconnected in order to make
+                * the resume procedure successful.
+                */
+               retval = hub_port_debounce_be_connected(hub, port1);
+               if (retval < 0) {
+                       dev_dbg(&port_dev->dev, "can't get reconnection after setting port  power on, status %d\n",
+                                       retval);
+                       goto out;
+               }
+               usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
+
+               /* Set return value to 0 if debounce successful */
+               retval = 0;
+       }
+
+out:
+       clear_bit(port1, hub->busy_bits);
        usb_autopm_put_interface(intf);
        return retval;
 }
@@ -90,14 +116,23 @@ static int usb_port_runtime_suspend(struct device *dev)
        struct usb_port *port_dev = to_usb_port(dev);
        struct usb_device *hdev = to_usb_device(dev->parent->parent);
        struct usb_interface *intf = to_usb_interface(dev->parent);
+       struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
+       int port1 = port_dev->portnum;
        int retval;
 
+       if (!hub)
+               return -EINVAL;
+
        if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
                        == PM_QOS_FLAGS_ALL)
                return -EAGAIN;
 
        usb_autopm_get_interface(intf);
-       retval = usb_hub_set_port_power(hdev, port_dev->portnum, false);
+       set_bit(port1, hub->busy_bits);
+       retval = usb_hub_set_port_power(hdev, port1, false);
+       usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
+       usb_clear_port_feature(hdev, port1,     USB_PORT_FEAT_C_ENABLE);
+       clear_bit(port1, hub->busy_bits);
        usb_autopm_put_interface(intf);
        return retval;
 }
@@ -130,6 +165,7 @@ int usb_hub_create_port_device(struct usb_hub *hub, int port1)
 
        hub->ports[port1 - 1] = port_dev;
        port_dev->portnum = port1;
+       port_dev->power_is_on = true;
        port_dev->dev.parent = hub->intfdev;
        port_dev->dev.groups = port_dev_group;
        port_dev->dev.type = &usb_port_device_type;