usb: add runtime pm support for usb port device
authorLan Tianyu <tianyu.lan@intel.com>
Tue, 22 Jan 2013 20:26:29 +0000 (04:26 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 25 Jan 2013 18:12:19 +0000 (10:12 -0800)
This patch is to add runtime pm callback for usb port device.
Set/clear PORT_POWER feature in the resume/suspend callback.
Add portnum for struct usb_port to record port number. Do
pm_rumtime_get_sync/put(portdev) when a device is plugged/unplugged
to prevent it from being powered off when it is active.

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 29ca6ed3bea89969a267f49b7979bbdcea37a73b..7fb163365d0290634f4b6ce351afb6b48c8b30d6 100644 (file)
@@ -714,6 +714,27 @@ static void hub_tt_work(struct work_struct *work)
        spin_unlock_irqrestore (&hub->tt.lock, flags);
 }
 
+/**
+ * usb_hub_set_port_power - control hub port's power state
+ * @hdev: target hub
+ * @port1: port index
+ * @set: expected status
+ *
+ * call this function to control port's power via setting or
+ * clearing the port's PORT_POWER feature.
+ */
+int usb_hub_set_port_power(struct usb_device *hdev, int port1,
+               bool set)
+{
+       int ret;
+
+       if (set)
+               ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
+       else
+               ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
+       return ret;
+}
+
 /**
  * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
  * @urb: an URB associated with the failed or incomplete split transaction
@@ -1569,6 +1590,7 @@ static void hub_disconnect(struct usb_interface *intf)
        kfree(hub->status);
        kfree(hub->buffer);
 
+       pm_suspend_ignore_children(&intf->dev, false);
        kref_put(&hub->kref, hub_release);
 }
 
@@ -1671,6 +1693,7 @@ descriptor_error:
 
        usb_set_intfdata (intf, hub);
        intf->needs_remote_wakeup = 1;
+       pm_suspend_ignore_children(&intf->dev, true);
 
        if (hdev->speed == USB_SPEED_HIGH)
                highspeed_hubs++;
@@ -1997,6 +2020,8 @@ void usb_disconnect(struct usb_device **pdev)
 
                sysfs_remove_link(&udev->dev.kobj, "port");
                sysfs_remove_link(&port_dev->dev.kobj, "device");
+
+               pm_runtime_put(&port_dev->dev);
        }
 
        usb_remove_ep_devs(&udev->ep0);
@@ -2307,6 +2332,8 @@ int usb_new_device(struct usb_device *udev)
                        sysfs_remove_link(&udev->dev.kobj, "port");
                        goto fail;
                }
+
+               pm_runtime_get_sync(&port_dev->dev);
        }
 
        (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
index c472058f8f278aa04c02ca76122b58ad57a8c257..452e5cd7b24982f91cd99865bd3e940bb087707c 100644 (file)
@@ -79,12 +79,14 @@ struct usb_hub {
  * @dev: generic device interface
  * @port_owner: port's owner
  * @connect_type: port's connect type
+ * @portnum: port index num based one
  */
 struct usb_port {
        struct usb_device *child;
        struct device dev;
        struct dev_state *port_owner;
        enum usb_port_connect_type connect_type;
+       u8 portnum;
 };
 
 #define to_usb_port(_dev) \
@@ -94,4 +96,6 @@ extern int usb_hub_create_port_device(struct usb_hub *hub,
                int port1);
 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);
 
index 153e799e7320c6aaef522c4f1cc27d21d4a3d152..d288dfed6ccfee146088b189ae7fd81d5a1ee99e 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <linux/slab.h>
+#include <linux/pm_qos.h>
 
 #include "hub.h"
 
@@ -70,9 +71,50 @@ static void usb_port_device_release(struct device *dev)
        kfree(port_dev);
 }
 
+#ifdef CONFIG_USB_SUSPEND
+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);
+       int retval;
+
+       usb_autopm_get_interface(intf);
+       retval = usb_hub_set_port_power(hdev, port_dev->portnum, true);
+       usb_autopm_put_interface(intf);
+       return retval;
+}
+
+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);
+       int retval;
+
+       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);
+       usb_autopm_put_interface(intf);
+       return retval;
+}
+#endif
+
+static const struct dev_pm_ops usb_port_pm_ops = {
+#ifdef CONFIG_USB_SUSPEND
+       .runtime_suspend =      usb_port_runtime_suspend,
+       .runtime_resume =       usb_port_runtime_resume,
+       .runtime_idle =         pm_generic_runtime_idle,
+#endif
+};
+
 struct device_type usb_port_device_type = {
        .name =         "usb_port",
        .release =      usb_port_device_release,
+       .pm =           &usb_port_pm_ops,
 };
 
 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
@@ -87,6 +129,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->dev.parent = hub->intfdev;
        port_dev->dev.groups = port_dev_group;
        port_dev->dev.type = &usb_port_device_type;
@@ -96,6 +139,9 @@ int usb_hub_create_port_device(struct usb_hub *hub, int port1)
        if (retval)
                goto error_register;
 
+       pm_runtime_set_active(&port_dev->dev);
+       pm_runtime_enable(&port_dev->dev);
+
        retval = usb_acpi_register_power_resources(&port_dev->dev);
        if (retval && retval != -ENODEV)
                dev_warn(&port_dev->dev, "the port can't register its ACPI power resource.\n");