Skip to content

Commit 9bbdf1e

Browse files
AlanSterngregkh
authored andcommitted
USB: convert to the runtime PM framework
This patch (as1329) converts the USB stack over to the PM core's runtime PM framework. This involves numerous changes throughout usbcore, especially to hub.c and driver.c. Perhaps the most notable change is that CONFIG_USB_SUSPEND now depends on CONFIG_PM_RUNTIME instead of CONFIG_PM. Several fields in the usb_device and usb_interface structures are no longer needed. Some code which used to depend on CONFIG_USB_PM now depends on CONFIG_USB_SUSPEND (requiring some rearrangement of header files). The only visible change in behavior should be that following a system sleep (resume from RAM or resume from hibernation), autosuspended USB devices will be resumed just like everything else. They won't remain suspended. But if they aren't in use then they will naturally autosuspend again in a few seconds. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 0c590e2 commit 9bbdf1e

11 files changed

Lines changed: 490 additions & 784 deletions

File tree

‎Documentation/usb/power-management.txt‎

Lines changed: 77 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Alan Stern <stern@rowland.harvard.edu>
44

5-
November 10, 2009
5+
December 11, 2009
66

77

88

@@ -29,9 +29,9 @@ covered to some extent (see Documentation/power/*.txt for more
2929
information about system PM).
3030

3131
Note: Dynamic PM support for USB is present only if the kernel was
32-
built with CONFIG_USB_SUSPEND enabled. System PM support is present
33-
only if the kernel was built with CONFIG_SUSPEND or CONFIG_HIBERNATION
34-
enabled.
32+
built with CONFIG_USB_SUSPEND enabled (which depends on
33+
CONFIG_PM_RUNTIME). System PM support is present only if the kernel
34+
was built with CONFIG_SUSPEND or CONFIG_HIBERNATION enabled.
3535

3636

3737
What is Remote Wakeup?
@@ -326,64 +326,63 @@ driver does so by calling these six functions:
326326
void usb_autopm_get_interface_no_resume(struct usb_interface *intf);
327327
void usb_autopm_put_interface_no_suspend(struct usb_interface *intf);
328328

329-
The functions work by maintaining a counter in the usb_interface
330-
structure. When intf->pm_usage_count is > 0 then the interface is
331-
deemed to be busy, and the kernel will not autosuspend the interface's
332-
device. When intf->pm_usage_count is <= 0 then the interface is
333-
considered to be idle, and the kernel may autosuspend the device.
329+
The functions work by maintaining a usage counter in the
330+
usb_interface's embedded device structure. When the counter is > 0
331+
then the interface is deemed to be busy, and the kernel will not
332+
autosuspend the interface's device. When the usage counter is = 0
333+
then the interface is considered to be idle, and the kernel may
334+
autosuspend the device.
334335

335-
(There is a similar pm_usage_count field in struct usb_device,
336+
(There is a similar usage counter field in struct usb_device,
336337
associated with the device itself rather than any of its interfaces.
337-
This field is used only by the USB core.)
338-
339-
Drivers must not modify intf->pm_usage_count directly; its value
340-
should be changed only be using the functions listed above. Drivers
341-
are responsible for insuring that the overall change to pm_usage_count
342-
during their lifetime balances out to 0 (it may be necessary for the
343-
disconnect method to call usb_autopm_put_interface() one or more times
344-
to fulfill this requirement). The first two routines use the PM mutex
345-
in struct usb_device for mutual exclusion; drivers using the async
346-
routines are responsible for their own synchronization and mutual
347-
exclusion.
348-
349-
usb_autopm_get_interface() increments pm_usage_count and
350-
attempts an autoresume if the new value is > 0 and the
351-
device is suspended.
352-
353-
usb_autopm_put_interface() decrements pm_usage_count and
354-
attempts an autosuspend if the new value is <= 0 and the
355-
device isn't suspended.
338+
This counter is used only by the USB core.)
339+
340+
Drivers need not be concerned about balancing changes to the usage
341+
counter; the USB core will undo any remaining "get"s when a driver
342+
is unbound from its interface. As a corollary, drivers must not call
343+
any of the usb_autopm_* functions after their diconnect() routine has
344+
returned.
345+
346+
Drivers using the async routines are responsible for their own
347+
synchronization and mutual exclusion.
348+
349+
usb_autopm_get_interface() increments the usage counter and
350+
does an autoresume if the device is suspended. If the
351+
autoresume fails, the counter is decremented back.
352+
353+
usb_autopm_put_interface() decrements the usage counter and
354+
attempts an autosuspend if the new value is = 0.
356355

357356
usb_autopm_get_interface_async() and
358357
usb_autopm_put_interface_async() do almost the same things as
359-
their non-async counterparts. The differences are: they do
360-
not acquire the PM mutex, and they use a workqueue to do their
358+
their non-async counterparts. The big difference is that they
359+
use a workqueue to do the resume or suspend part of their
361360
jobs. As a result they can be called in an atomic context,
362361
such as an URB's completion handler, but when they return the
363-
device will not generally not yet be in the desired state.
362+
device will generally not yet be in the desired state.
364363

365364
usb_autopm_get_interface_no_resume() and
366365
usb_autopm_put_interface_no_suspend() merely increment or
367-
decrement the pm_usage_count value; they do not attempt to
368-
carry out an autoresume or an autosuspend. Hence they can be
369-
called in an atomic context.
366+
decrement the usage counter; they do not attempt to carry out
367+
an autoresume or an autosuspend. Hence they can be called in
368+
an atomic context.
370369

371-
The conventional usage pattern is that a driver calls
370+
The simplest usage pattern is that a driver calls
372371
usb_autopm_get_interface() in its open routine and
373-
usb_autopm_put_interface() in its close or release routine. But
374-
other patterns are possible.
372+
usb_autopm_put_interface() in its close or release routine. But other
373+
patterns are possible.
375374

376375
The autosuspend attempts mentioned above will often fail for one
377376
reason or another. For example, the power/level attribute might be
378377
set to "on", or another interface in the same device might not be
379378
idle. This is perfectly normal. If the reason for failure was that
380-
the device hasn't been idle for long enough, a delayed workqueue
381-
routine is automatically set up to carry out the operation when the
382-
autosuspend idle-delay has expired.
379+
the device hasn't been idle for long enough, a timer is scheduled to
380+
carry out the operation automatically when the autosuspend idle-delay
381+
has expired.
383382

384383
Autoresume attempts also can fail, although failure would mean that
385384
the device is no longer present or operating properly. Unlike
386-
autosuspend, there's no delay for an autoresume.
385+
autosuspend, there's no idle-delay for an autoresume.
387386

388387

389388
Other parts of the driver interface
@@ -413,102 +412,51 @@ though, setting this flag won't cause the kernel to autoresume it.
413412
Normally a driver would set this flag in its probe method, at which
414413
time the device is guaranteed not to be autosuspended.)
415414

416-
The synchronous usb_autopm_* routines have to run in a sleepable
417-
process context; they must not be called from an interrupt handler or
418-
while holding a spinlock. In fact, the entire autosuspend mechanism
419-
is not well geared toward interrupt-driven operation. However there
420-
is one thing a driver can do in an interrupt handler:
415+
If a driver does its I/O asynchronously in interrupt context, it
416+
should call usb_autopm_get_interface_async() before starting output and
417+
usb_autopm_put_interface_async() when the output queue drains. When
418+
it receives an input event, it should call
421419

422420
usb_mark_last_busy(struct usb_device *udev);
423421

424-
This sets udev->last_busy to the current time. udev->last_busy is the
425-
field used for idle-delay calculations; updating it will cause any
426-
pending autosuspend to be moved back. The usb_autopm_* routines will
427-
also set the last_busy field to the current time.
428-
429-
Calling urb_mark_last_busy() from within an URB completion handler is
430-
subject to races: The kernel may have just finished deciding the
431-
device has been idle for long enough but not yet gotten around to
432-
calling the driver's suspend method. The driver would have to be
433-
responsible for synchronizing its suspend method with its URB
434-
completion handler and causing the autosuspend to fail with -EBUSY if
435-
an URB had completed too recently.
422+
in the event handler. This sets udev->last_busy to the current time.
423+
udev->last_busy is the field used for idle-delay calculations;
424+
updating it will cause any pending autosuspend to be moved back. Most
425+
of the usb_autopm_* routines will also set the last_busy field to the
426+
current time.
427+
428+
Asynchronous operation is always subject to races. For example, a
429+
driver may call one of the usb_autopm_*_interface_async() routines at
430+
a time when the core has just finished deciding the device has been
431+
idle for long enough but not yet gotten around to calling the driver's
432+
suspend method. The suspend method must be responsible for
433+
synchronizing with the output request routine and the URB completion
434+
handler; it should cause autosuspends to fail with -EBUSY if the
435+
driver needs to use the device.
436436

437437
External suspend calls should never be allowed to fail in this way,
438438
only autosuspend calls. The driver can tell them apart by checking
439439
the PM_EVENT_AUTO bit in the message.event argument to the suspend
440440
method; this bit will be set for internal PM events (autosuspend) and
441441
clear for external PM events.
442442

443-
Many of the ingredients in the autosuspend framework are oriented
444-
towards interfaces: The usb_interface structure contains the
445-
pm_usage_cnt field, and the usb_autopm_* routines take an interface
446-
pointer as their argument. But somewhat confusingly, a few of the
447-
pieces (i.e., usb_mark_last_busy()) use the usb_device structure
448-
instead. Drivers need to keep this straight; they can call
449-
interface_to_usbdev() to find the device structure for a given
450-
interface.
451443

444+
Mutual exclusion
445+
----------------
452446

453-
Locking requirements
454-
--------------------
455-
456-
All three suspend/resume methods are always called while holding the
457-
usb_device's PM mutex. For external events -- but not necessarily for
458-
autosuspend or autoresume -- the device semaphore (udev->dev.sem) will
459-
also be held. This implies that external suspend/resume events are
460-
mutually exclusive with calls to probe, disconnect, pre_reset, and
461-
post_reset; the USB core guarantees that this is true of internal
462-
suspend/resume events as well.
447+
For external events -- but not necessarily for autosuspend or
448+
autoresume -- the device semaphore (udev->dev.sem) will be held when a
449+
suspend or resume method is called. This implies that external
450+
suspend/resume events are mutually exclusive with calls to probe,
451+
disconnect, pre_reset, and post_reset; the USB core guarantees that
452+
this is true of autosuspend/autoresume events as well.
463453

464454
If a driver wants to block all suspend/resume calls during some
465-
critical section, it can simply acquire udev->pm_mutex. Note that
466-
calls to resume may be triggered indirectly. Block IO due to memory
467-
allocations can make the vm subsystem resume a device. Thus while
468-
holding this lock you must not allocate memory with GFP_KERNEL or
469-
GFP_NOFS.
470-
471-
Alternatively, if the critical section might call some of the
472-
usb_autopm_* routines, the driver can avoid deadlock by doing:
473-
474-
down(&udev->dev.sem);
475-
rc = usb_autopm_get_interface(intf);
476-
477-
and at the end of the critical section:
478-
479-
if (!rc)
480-
usb_autopm_put_interface(intf);
481-
up(&udev->dev.sem);
482-
483-
Holding the device semaphore will block all external PM calls, and the
484-
usb_autopm_get_interface() will prevent any internal PM calls, even if
485-
it fails. (Exercise: Why?)
486-
487-
The rules for locking order are:
488-
489-
Never acquire any device semaphore while holding any PM mutex.
490-
491-
Never acquire udev->pm_mutex while holding the PM mutex for
492-
a device that isn't a descendant of udev.
493-
494-
In other words, PM mutexes should only be acquired going up the device
495-
tree, and they should be acquired only after locking all the device
496-
semaphores you need to hold. These rules don't matter to drivers very
497-
much; they usually affect just the USB core.
498-
499-
Still, drivers do need to be careful. For example, many drivers use a
500-
private mutex to synchronize their normal I/O activities with their
501-
disconnect method. Now if the driver supports autosuspend then it
502-
must call usb_autopm_put_interface() from somewhere -- maybe from its
503-
close method. It should make the call while holding the private mutex,
504-
since a driver shouldn't call any of the usb_autopm_* functions for an
505-
interface from which it has been unbound.
506-
507-
But the usb_autpm_* routines always acquire the device's PM mutex, and
508-
consequently the locking order has to be: private mutex first, PM
509-
mutex second. Since the suspend method is always called with the PM
510-
mutex held, it mustn't try to acquire the private mutex. It has to
511-
synchronize with the driver's I/O activities in some other way.
455+
critical section, the best way is to lock the device and call
456+
usb_autopm_get_interface() (and do the reverse at the end of the
457+
critical section). Holding the device semaphore will block all
458+
external PM calls, and the usb_autopm_get_interface() will prevent any
459+
internal PM calls, even if it fails. (Exercise: Why?)
512460

513461

514462
Interaction between dynamic PM and system PM
@@ -517,22 +465,11 @@ synchronize with the driver's I/O activities in some other way.
517465
Dynamic power management and system power management can interact in
518466
a couple of ways.
519467

520-
Firstly, a device may already be manually suspended or autosuspended
521-
when a system suspend occurs. Since system suspends are supposed to
522-
be as transparent as possible, the device should remain suspended
523-
following the system resume. The 2.6.23 kernel obeys this principle
524-
for manually suspended devices but not for autosuspended devices; they
525-
do get resumed when the system wakes up. (Presumably they will be
526-
autosuspended again after their idle-delay time expires.) In later
527-
kernels this behavior will be fixed.
528-
529-
(There is an exception. If a device would undergo a reset-resume
530-
instead of a normal resume, and the device is enabled for remote
531-
wakeup, then the reset-resume takes place even if the device was
532-
already suspended when the system suspend began. The justification is
533-
that a reset-resume is a kind of remote-wakeup event. Or to put it
534-
another way, a device which needs a reset won't be able to generate
535-
normal remote-wakeup signals, so it ought to be resumed immediately.)
468+
Firstly, a device may already be autosuspended when a system suspend
469+
occurs. Since system suspends are supposed to be as transparent as
470+
possible, the device should remain suspended following the system
471+
resume. But this theory may not work out well in practice; over time
472+
the kernel's behavior in this regard has changed.
536473

537474
Secondly, a dynamic power-management event may occur as a system
538475
suspend is underway. The window for this is short, since system

‎drivers/usb/core/Kconfig‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ config USB_DYNAMIC_MINORS
9191
If you are unsure about this, say N here.
9292

9393
config USB_SUSPEND
94-
bool "USB selective suspend/resume and wakeup"
95-
depends on USB && PM
94+
bool "USB runtime power management (suspend/resume and wakeup)"
95+
depends on USB && PM_RUNTIME
9696
help
9797
If you say Y here, you can use driver calls or the sysfs
9898
"power/level" file to suspend or resume individual USB

0 commit comments

Comments
 (0)