This patch is to add runtime pm callback for usb port device. Set/clear PORT_POWER feature in the resume/suspend callbak. 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 power off when it is active. Acked-by: Rafael J. Wysocki <rafael.j.wysocki@xxxxxxxxx> Signed-off-by: Lan Tianyu <tianyu.lan@xxxxxxxxx> --- drivers/usb/core/hub.c | 20 ++++++++++++++++++++ drivers/usb/core/hub.h | 5 +++++ drivers/usb/core/port.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index dd0694c..3e69d66 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c @@ -694,6 +694,20 @@ static void hub_tt_work(struct work_struct *work) spin_unlock_irqrestore (&hub->tt.lock, flags); } +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 @@ -1922,6 +1936,9 @@ void usb_disconnect(struct usb_device **pdev) sysfs_remove_link(&udev->dev.kobj, "port"); sysfs_remove_link(&port_dev->dev.kobj, "device"); + + /* put the related port to idle when device is unplugged */ + pm_runtime_put(&port_dev->dev); } usb_remove_ep_devs(&udev->ep0); @@ -2232,6 +2249,9 @@ int usb_new_device(struct usb_device *udev) sysfs_remove_link(&udev->dev.kobj, "port"); goto fail; } + + /* resume the related port device */ + pm_runtime_get_sync(&port_dev->dev); } (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); diff --git a/drivers/usb/core/hub.h b/drivers/usb/core/hub.h index b028b6b..1b574a2 100644 --- a/drivers/usb/core/hub.h +++ b/drivers/usb/core/hub.h @@ -38,6 +38,7 @@ #include <linux/freezer.h> #include <linux/random.h> #include <linux/uaccess.h> +#include <linux/pm_qos.h> #include <asm/byteorder.h> @@ -98,12 +99,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) \ @@ -113,4 +116,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); diff --git a/drivers/usb/core/port.c b/drivers/usb/core/port.c index 187046a..d44d8d3 100644 --- a/drivers/usb/core/port.c +++ b/drivers/usb/core/port.c @@ -66,9 +66,49 @@ static void usb_port_device_release(struct device *dev) kfree(port_dev); } +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); + + return usb_hub_set_port_power(hdev, port_dev->portnum, + true); +} + +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); + + if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF) + == PM_QOS_FLAGS_ALL) + return -EAGAIN; + + return usb_hub_set_port_power(hdev, port_dev->portnum, + false); +} + +static int usb_port_runtime_idle(struct device *dev) +{ + struct usb_port *port_dev = to_usb_port(dev); + + return pm_runtime_suspend(&port_dev->dev); +} + +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 = usb_port_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) @@ -83,6 +123,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; @@ -92,6 +133,8 @@ 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); usb_acpi_register_power_resources(&port_dev->dev); return 0; -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html