Commit 645daaab authored by Alan Stern's avatar Alan Stern Committed by Greg Kroah-Hartman

usbcore: add autosuspend/autoresume infrastructure

This patch (as739) adds the basic infrastructure for USB autosuspend
and autoresume.  The main features are:

	PM usage counters added to struct usb_device and struct
	usb_interface, indicating whether it's okay to autosuspend
	them or they are currently in use.

	Flag added to usb_device indicating whether the current
	suspend/resume operation originated from outside or as an
	autosuspend/autoresume.

	Flag added to usb_driver indicating whether the driver
	supports autosuspend.  If not, no device bound to the driver
	will be autosuspended.

	Mutex added to usb_device for protecting PM operations.
	Unlike the device semaphore, the locking rule for the pm_mutex
	is that you must acquire the locks going _up_ the device tree.

	New routines handling autosuspend/autoresume requests for
	interfaces and devices.

	Suspend and resume requests are propagated up the device tree
	(but not outside the USB subsystem).

	work_struct added to usb_device, for carrying out delayed
	autosuspend requests.

	Autoresume added (and autosuspend prevented) during probe and
	disconnect.
Signed-off-by: default avatarAlan Stern <stern@rowland.harvard.edu>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent b6956ffa
This diff is collapsed.
...@@ -1017,19 +1017,22 @@ void usb_set_device_state(struct usb_device *udev, ...@@ -1017,19 +1017,22 @@ void usb_set_device_state(struct usb_device *udev,
if (udev->state == USB_STATE_NOTATTACHED) if (udev->state == USB_STATE_NOTATTACHED)
; /* do nothing */ ; /* do nothing */
else if (new_state != USB_STATE_NOTATTACHED) { else if (new_state != USB_STATE_NOTATTACHED) {
udev->state = new_state;
/* root hub wakeup capabilities are managed out-of-band /* root hub wakeup capabilities are managed out-of-band
* and may involve silicon errata ... ignore them here. * and may involve silicon errata ... ignore them here.
*/ */
if (udev->parent) { if (udev->parent) {
if (new_state == USB_STATE_CONFIGURED) if (udev->state == USB_STATE_SUSPENDED
|| new_state == USB_STATE_SUSPENDED)
; /* No change to wakeup settings */
else if (new_state == USB_STATE_CONFIGURED)
device_init_wakeup(&udev->dev, device_init_wakeup(&udev->dev,
(udev->actconfig->desc.bmAttributes (udev->actconfig->desc.bmAttributes
& USB_CONFIG_ATT_WAKEUP)); & USB_CONFIG_ATT_WAKEUP));
else if (new_state != USB_STATE_SUSPENDED) else
device_init_wakeup(&udev->dev, 0); device_init_wakeup(&udev->dev, 0);
} }
udev->state = new_state;
} else } else
recursively_mark_NOTATTACHED(udev); recursively_mark_NOTATTACHED(udev);
spin_unlock_irqrestore(&device_state_lock, flags); spin_unlock_irqrestore(&device_state_lock, flags);
...@@ -1507,7 +1510,7 @@ static int hub_port_suspend(struct usb_hub *hub, int port1, ...@@ -1507,7 +1510,7 @@ static int hub_port_suspend(struct usb_hub *hub, int port1,
* NOTE: OTG devices may issue remote wakeup (or SRP) even when * NOTE: OTG devices may issue remote wakeup (or SRP) even when
* we don't explicitly enable it here. * we don't explicitly enable it here.
*/ */
if (device_may_wakeup(&udev->dev)) { if (udev->do_remote_wakeup) {
status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
USB_DEVICE_REMOTE_WAKEUP, 0, USB_DEVICE_REMOTE_WAKEUP, 0,
...@@ -1533,7 +1536,8 @@ static int hub_port_suspend(struct usb_hub *hub, int port1, ...@@ -1533,7 +1536,8 @@ static int hub_port_suspend(struct usb_hub *hub, int port1,
USB_CTRL_SET_TIMEOUT); USB_CTRL_SET_TIMEOUT);
} else { } else {
/* device has up to 10 msec to fully suspend */ /* device has up to 10 msec to fully suspend */
dev_dbg(&udev->dev, "usb suspend\n"); dev_dbg(&udev->dev, "usb %ssuspend\n",
udev->auto_pm ? "auto-" : "");
usb_set_device_state(udev, USB_STATE_SUSPENDED); usb_set_device_state(udev, USB_STATE_SUSPENDED);
msleep(10); msleep(10);
} }
...@@ -1573,7 +1577,8 @@ static int __usb_port_suspend (struct usb_device *udev, int port1) ...@@ -1573,7 +1577,8 @@ static int __usb_port_suspend (struct usb_device *udev, int port1)
status = hub_port_suspend(hdev_to_hub(udev->parent), port1, status = hub_port_suspend(hdev_to_hub(udev->parent), port1,
udev); udev);
else { else {
dev_dbg(&udev->dev, "usb suspend\n"); dev_dbg(&udev->dev, "usb %ssuspend\n",
udev->auto_pm ? "auto-" : "");
usb_set_device_state(udev, USB_STATE_SUSPENDED); usb_set_device_state(udev, USB_STATE_SUSPENDED);
} }
return status; return status;
...@@ -1687,7 +1692,8 @@ hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev) ...@@ -1687,7 +1692,8 @@ hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
/* drive resume for at least 20 msec */ /* drive resume for at least 20 msec */
if (udev) if (udev)
dev_dbg(&udev->dev, "RESUME\n"); dev_dbg(&udev->dev, "usb %sresume\n",
udev->auto_pm ? "auto-" : "");
msleep(25); msleep(25);
#define LIVE_FLAGS ( USB_PORT_STAT_POWER \ #define LIVE_FLAGS ( USB_PORT_STAT_POWER \
...@@ -1754,8 +1760,11 @@ int usb_port_resume(struct usb_device *udev) ...@@ -1754,8 +1760,11 @@ int usb_port_resume(struct usb_device *udev)
// NOTE this fails if parent is also suspended... // NOTE this fails if parent is also suspended...
status = hub_port_resume(hdev_to_hub(udev->parent), status = hub_port_resume(hdev_to_hub(udev->parent),
udev->portnum, udev); udev->portnum, udev);
} else } else {
dev_dbg(&udev->dev, "usb %sresume\n",
udev->auto_pm ? "auto-" : "");
status = finish_port_resume(udev); status = finish_port_resume(udev);
}
if (status < 0) if (status < 0)
dev_dbg(&udev->dev, "can't resume, status %d\n", status); dev_dbg(&udev->dev, "can't resume, status %d\n", status);
return status; return status;
...@@ -1765,19 +1774,23 @@ static int remote_wakeup(struct usb_device *udev) ...@@ -1765,19 +1774,23 @@ static int remote_wakeup(struct usb_device *udev)
{ {
int status = 0; int status = 0;
/* don't repeat RESUME sequence if this device /* All this just to avoid sending a port-resume message
* was already woken up by some other task * to the parent hub! */
*/
usb_lock_device(udev); usb_lock_device(udev);
mutex_lock_nested(&udev->pm_mutex, udev->level);
if (udev->state == USB_STATE_SUSPENDED) { if (udev->state == USB_STATE_SUSPENDED) {
dev_dbg(&udev->dev, "RESUME (wakeup)\n"); dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
/* TRSMRCY = 10 msec */ /* TRSMRCY = 10 msec */
msleep(10); msleep(10);
status = finish_port_resume(udev); status = finish_port_resume(udev);
if (status == 0)
udev->dev.power.power_state.event = PM_EVENT_ON;
} }
mutex_unlock(&udev->pm_mutex);
if (status == 0) if (status == 0)
usb_resume_both(udev); usb_autoresume_device(udev, 0);
usb_unlock_device(udev); usb_unlock_device(udev);
return status; return status;
} }
...@@ -1834,7 +1847,9 @@ static int hub_suspend(struct usb_interface *intf, pm_message_t msg) ...@@ -1834,7 +1847,9 @@ static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
== PM_EVENT_ON == PM_EVENT_ON
#endif #endif
) { ) {
dev_dbg(&intf->dev, "port %d nyet suspended\n", port1); if (!hdev->auto_pm)
dev_dbg(&intf->dev, "port %d nyet suspended\n",
port1);
return -EBUSY; return -EBUSY;
} }
} }
...@@ -2587,7 +2602,7 @@ static void hub_events(void) ...@@ -2587,7 +2602,7 @@ static void hub_events(void)
* stub "device" node was never suspended. * stub "device" node was never suspended.
*/ */
if (i) if (i)
usb_resume_both(hdev); usb_autoresume_device(hdev, 0);
/* If this is an inactive or suspended hub, do nothing */ /* If this is an inactive or suspended hub, do nothing */
if (hub->quiescing) if (hub->quiescing)
...@@ -2993,6 +3008,9 @@ int usb_reset_composite_device(struct usb_device *udev, ...@@ -2993,6 +3008,9 @@ int usb_reset_composite_device(struct usb_device *udev,
return -EINVAL; return -EINVAL;
} }
/* Prevent autosuspend during the reset */
usb_autoresume_device(udev, 1);
if (iface && iface->condition != USB_INTERFACE_BINDING) if (iface && iface->condition != USB_INTERFACE_BINDING)
iface = NULL; iface = NULL;
...@@ -3034,6 +3052,7 @@ int usb_reset_composite_device(struct usb_device *udev, ...@@ -3034,6 +3052,7 @@ int usb_reset_composite_device(struct usb_device *udev,
} }
} }
usb_autosuspend_device(udev, 1);
return ret; return ret;
} }
EXPORT_SYMBOL(usb_reset_composite_device); EXPORT_SYMBOL(usb_reset_composite_device);
...@@ -168,6 +168,10 @@ static void usb_release_dev(struct device *dev) ...@@ -168,6 +168,10 @@ static void usb_release_dev(struct device *dev)
udev = to_usb_device(dev); udev = to_usb_device(dev);
#ifdef CONFIG_PM
cancel_delayed_work(&udev->autosuspend);
flush_scheduled_work();
#endif
usb_destroy_configuration(udev); usb_destroy_configuration(udev);
usb_put_hcd(bus_to_hcd(udev->bus)); usb_put_hcd(bus_to_hcd(udev->bus));
kfree(udev->product); kfree(udev->product);
...@@ -176,6 +180,21 @@ static void usb_release_dev(struct device *dev) ...@@ -176,6 +180,21 @@ static void usb_release_dev(struct device *dev)
kfree(udev); kfree(udev);
} }
#ifdef CONFIG_PM
/* usb_autosuspend_work - callback routine to autosuspend a USB device */
static void usb_autosuspend_work(void *_udev)
{
struct usb_device *udev = _udev;
mutex_lock_nested(&udev->pm_mutex, udev->level);
udev->auto_pm = 1;
usb_suspend_both(udev, PMSG_SUSPEND);
mutex_unlock(&udev->pm_mutex);
}
#endif
/** /**
* usb_alloc_dev - usb device constructor (usbcore-internal) * usb_alloc_dev - usb device constructor (usbcore-internal)
* @parent: hub to which device is connected; null to allocate a root hub * @parent: hub to which device is connected; null to allocate a root hub
...@@ -251,6 +270,10 @@ usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1) ...@@ -251,6 +270,10 @@ usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
dev->parent = parent; dev->parent = parent;
INIT_LIST_HEAD(&dev->filelist); INIT_LIST_HEAD(&dev->filelist);
#ifdef CONFIG_PM
mutex_init(&dev->pm_mutex);
INIT_WORK(&dev->autosuspend, usb_autosuspend_work, dev);
#endif
return dev; return dev;
} }
......
...@@ -49,6 +49,20 @@ static inline int usb_resume_both(struct usb_device *udev) ...@@ -49,6 +49,20 @@ static inline int usb_resume_both(struct usb_device *udev)
#endif #endif
#ifdef CONFIG_USB_SUSPEND
#define USB_AUTOSUSPEND_DELAY (HZ*2)
extern void usb_autosuspend_device(struct usb_device *udev, int dec_busy_cnt);
extern int usb_autoresume_device(struct usb_device *udev, int inc_busy_cnt);
#else
#define usb_autosuspend_device(udev, dec_busy_cnt) do {} while (0)
#define usb_autoresume_device(udev, inc_busy_cnt) 0
#endif
extern struct bus_type usb_bus_type; extern struct bus_type usb_bus_type;
extern struct usb_device_driver usb_generic_driver; extern struct usb_device_driver usb_generic_driver;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <linux/fs.h> /* for struct file_operations */ #include <linux/fs.h> /* for struct file_operations */
#include <linux/completion.h> /* for struct completion */ #include <linux/completion.h> /* for struct completion */
#include <linux/sched.h> /* for current && schedule_timeout */ #include <linux/sched.h> /* for current && schedule_timeout */
#include <linux/mutex.h> /* for struct mutex */
struct usb_device; struct usb_device;
struct usb_driver; struct usb_driver;
...@@ -103,8 +104,12 @@ enum usb_interface_condition { ...@@ -103,8 +104,12 @@ enum usb_interface_condition {
* @condition: binding state of the interface: not bound, binding * @condition: binding state of the interface: not bound, binding
* (in probe()), bound to a driver, or unbinding (in disconnect()) * (in probe()), bound to a driver, or unbinding (in disconnect())
* @is_active: flag set when the interface is bound and not suspended. * @is_active: flag set when the interface is bound and not suspended.
* @needs_remote_wakeup: flag set when the driver requires remote-wakeup
* capability during autosuspend.
* @dev: driver model's view of this device * @dev: driver model's view of this device
* @class_dev: driver model's class view of this device. * @class_dev: driver model's class view of this device.
* @pm_usage_cnt: PM usage counter for this interface; autosuspend is not
* allowed unless the counter is 0.
* *
* USB device drivers attach to interfaces on a physical device. Each * USB device drivers attach to interfaces on a physical device. Each
* interface encapsulates a single high level function, such as feeding * interface encapsulates a single high level function, such as feeding
...@@ -144,9 +149,11 @@ struct usb_interface { ...@@ -144,9 +149,11 @@ struct usb_interface {
* bound to */ * bound to */
enum usb_interface_condition condition; /* state of binding */ enum usb_interface_condition condition; /* state of binding */
unsigned is_active:1; /* the interface is not suspended */ unsigned is_active:1; /* the interface is not suspended */
unsigned needs_remote_wakeup:1; /* driver requires remote wakeup */
struct device dev; /* interface specific device info */ struct device dev; /* interface specific device info */
struct class_device *class_dev; struct class_device *class_dev;
int pm_usage_cnt; /* usage counter for autosuspend */
}; };
#define to_usb_interface(d) container_of(d, struct usb_interface, dev) #define to_usb_interface(d) container_of(d, struct usb_interface, dev)
#define interface_to_usbdev(intf) \ #define interface_to_usbdev(intf) \
...@@ -372,6 +379,15 @@ struct usb_device { ...@@ -372,6 +379,15 @@ struct usb_device {
int maxchild; /* Number of ports if hub */ int maxchild; /* Number of ports if hub */
struct usb_device *children[USB_MAXCHILDREN]; struct usb_device *children[USB_MAXCHILDREN];
#ifdef CONFIG_PM
struct work_struct autosuspend; /* for delayed autosuspends */
struct mutex pm_mutex; /* protects PM operations */
int pm_usage_cnt; /* usage counter for autosuspend */
unsigned auto_pm:1; /* autosuspend/resume in progress */
unsigned do_remote_wakeup:1; /* remote wakeup should be enabled */
#endif
}; };
#define to_usb_device(d) container_of(d, struct usb_device, dev) #define to_usb_device(d) container_of(d, struct usb_device, dev)
...@@ -392,6 +408,17 @@ extern int usb_reset_composite_device(struct usb_device *dev, ...@@ -392,6 +408,17 @@ extern int usb_reset_composite_device(struct usb_device *dev,
extern struct usb_device *usb_find_device(u16 vendor_id, u16 product_id); extern struct usb_device *usb_find_device(u16 vendor_id, u16 product_id);
/* USB autosuspend and autoresume */
#ifdef CONFIG_USB_SUSPEND
extern int usb_autopm_get_interface(struct usb_interface *intf);
extern void usb_autopm_put_interface(struct usb_interface *intf);
#else
#define usb_autopm_get_interface(intf) 0
#define usb_autopm_put_interface(intf) do {} while (0)
#endif
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
/* for drivers using iso endpoints */ /* for drivers using iso endpoints */
...@@ -593,6 +620,8 @@ struct usbdrv_wrap { ...@@ -593,6 +620,8 @@ struct usbdrv_wrap {
* @drvwrap: Driver-model core structure wrapper. * @drvwrap: Driver-model core structure wrapper.
* @no_dynamic_id: if set to 1, the USB core will not allow dynamic ids to be * @no_dynamic_id: if set to 1, the USB core will not allow dynamic ids to be
* added to this driver by preventing the sysfs file from being created. * added to this driver by preventing the sysfs file from being created.
* @supports_autosuspend: if set to 0, the USB core will not allow autosuspend
* for interfaces bound to this driver.
* *
* USB interface drivers must provide a name, probe() and disconnect() * USB interface drivers must provide a name, probe() and disconnect()
* methods, and an id_table. Other driver fields are optional. * methods, and an id_table. Other driver fields are optional.
...@@ -631,6 +660,7 @@ struct usb_driver { ...@@ -631,6 +660,7 @@ struct usb_driver {
struct usb_dynids dynids; struct usb_dynids dynids;
struct usbdrv_wrap drvwrap; struct usbdrv_wrap drvwrap;
unsigned int no_dynamic_id:1; unsigned int no_dynamic_id:1;
unsigned int supports_autosuspend:1;
}; };
#define to_usb_driver(d) container_of(d, struct usb_driver, drvwrap.driver) #define to_usb_driver(d) container_of(d, struct usb_driver, drvwrap.driver)
...@@ -648,6 +678,8 @@ struct usb_driver { ...@@ -648,6 +678,8 @@ struct usb_driver {
* @suspend: Called when the device is going to be suspended by the system. * @suspend: Called when the device is going to be suspended by the system.
* @resume: Called when the device is being resumed by the system. * @resume: Called when the device is being resumed by the system.
* @drvwrap: Driver-model core structure wrapper. * @drvwrap: Driver-model core structure wrapper.
* @supports_autosuspend: if set to 0, the USB core will not allow autosuspend
* for devices bound to this driver.
* *
* USB drivers must provide all the fields listed above except drvwrap. * USB drivers must provide all the fields listed above except drvwrap.
*/ */
...@@ -660,6 +692,7 @@ struct usb_device_driver { ...@@ -660,6 +692,7 @@ struct usb_device_driver {
int (*suspend) (struct usb_device *udev, pm_message_t message); int (*suspend) (struct usb_device *udev, pm_message_t message);
int (*resume) (struct usb_device *udev); int (*resume) (struct usb_device *udev);
struct usbdrv_wrap drvwrap; struct usbdrv_wrap drvwrap;
unsigned int supports_autosuspend:1;
}; };
#define to_usb_device_driver(d) container_of(d, struct usb_device_driver, \ #define to_usb_device_driver(d) container_of(d, struct usb_device_driver, \
drvwrap.driver) drvwrap.driver)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment