aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/usb/power-management.txt
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/usb/power-management.txt')
-rw-r--r--Documentation/usb/power-management.txt250
1 files changed, 104 insertions, 146 deletions
diff --git a/Documentation/usb/power-management.txt b/Documentation/usb/power-management.txt
index 3bf6818c8cf5..b29d8e56cf28 100644
--- a/Documentation/usb/power-management.txt
+++ b/Documentation/usb/power-management.txt
@@ -2,7 +2,7 @@
2 2
3 Alan Stern <stern@rowland.harvard.edu> 3 Alan Stern <stern@rowland.harvard.edu>
4 4
5 November 10, 2009 5 December 11, 2009
6 6
7 7
8 8
@@ -29,9 +29,9 @@ covered to some extent (see Documentation/power/*.txt for more
29information about system PM). 29information about system PM).
30 30
31Note: Dynamic PM support for USB is present only if the kernel was 31Note: Dynamic PM support for USB is present only if the kernel was
32built with CONFIG_USB_SUSPEND enabled. System PM support is present 32built with CONFIG_USB_SUSPEND enabled (which depends on
33only if the kernel was built with CONFIG_SUSPEND or CONFIG_HIBERNATION 33CONFIG_PM_RUNTIME). System PM support is present only if the kernel
34enabled. 34was built with CONFIG_SUSPEND or CONFIG_HIBERNATION enabled.
35 35
36 36
37 What is Remote Wakeup? 37 What is Remote Wakeup?
@@ -107,7 +107,9 @@ allowed to issue dynamic suspends.
107The user interface for controlling dynamic PM is located in the power/ 107The user interface for controlling dynamic PM is located in the power/
108subdirectory of each USB device's sysfs directory, that is, in 108subdirectory of each USB device's sysfs directory, that is, in
109/sys/bus/usb/devices/.../power/ where "..." is the device's ID. The 109/sys/bus/usb/devices/.../power/ where "..." is the device's ID. The
110relevant attribute files are: wakeup, level, and autosuspend. 110relevant attribute files are: wakeup, control, and autosuspend.
111(There may also be a file named "level"; this file was deprecated
112as of the 2.6.35 kernel and replaced by the "control" file.)
111 113
112 power/wakeup 114 power/wakeup
113 115
@@ -120,7 +122,7 @@ relevant attribute files are: wakeup, level, and autosuspend.
120 while the device is suspended, the change won't take 122 while the device is suspended, the change won't take
121 effect until the following suspend.) 123 effect until the following suspend.)
122 124
123 power/level 125 power/control
124 126
125 This file contains one of two words: "on" or "auto". 127 This file contains one of two words: "on" or "auto".
126 You can write those words to the file to change the 128 You can write those words to the file to change the
@@ -148,14 +150,15 @@ relevant attribute files are: wakeup, level, and autosuspend.
148 never to autosuspend. You can write a number to the 150 never to autosuspend. You can write a number to the
149 file to change the autosuspend idle-delay time. 151 file to change the autosuspend idle-delay time.
150 152
151Writing "-1" to power/autosuspend and writing "on" to power/level do 153Writing "-1" to power/autosuspend and writing "on" to power/control do
152essentially the same thing -- they both prevent the device from being 154essentially the same thing -- they both prevent the device from being
153autosuspended. Yes, this is a redundancy in the API. 155autosuspended. Yes, this is a redundancy in the API.
154 156
155(In 2.6.21 writing "0" to power/autosuspend would prevent the device 157(In 2.6.21 writing "0" to power/autosuspend would prevent the device
156from being autosuspended; the behavior was changed in 2.6.22. The 158from being autosuspended; the behavior was changed in 2.6.22. The
157power/autosuspend attribute did not exist prior to 2.6.21, and the 159power/autosuspend attribute did not exist prior to 2.6.21, and the
158power/level attribute did not exist prior to 2.6.22.) 160power/level attribute did not exist prior to 2.6.22. power/control
161was added in 2.6.34.)
159 162
160 163
161 Changing the default idle-delay time 164 Changing the default idle-delay time
@@ -212,7 +215,7 @@ among printers and scanners, but plenty of other types of device have
212the same deficiency. 215the same deficiency.
213 216
214For this reason, by default the kernel disables autosuspend (the 217For this reason, by default the kernel disables autosuspend (the
215power/level attribute is initialized to "on") for all devices other 218power/control attribute is initialized to "on") for all devices other
216than hubs. Hubs, at least, appear to be reasonably well-behaved in 219than hubs. Hubs, at least, appear to be reasonably well-behaved in
217this regard. 220this regard.
218 221
@@ -229,6 +232,11 @@ necessary operations by hand or add them to a udev script. You can
229also change the idle-delay time; 2 seconds is not the best choice for 232also change the idle-delay time; 2 seconds is not the best choice for
230every device. 233every device.
231 234
235If a driver knows that its device has proper suspend/resume support,
236it can enable autosuspend all by itself. For example, the video
237driver for a laptop's webcam might do this, since these devices are
238rarely used and so should normally be autosuspended.
239
232Sometimes it turns out that even when a device does work okay with 240Sometimes it turns out that even when a device does work okay with
233autosuspend there are still problems. For example, there are 241autosuspend there are still problems. For example, there are
234experimental patches adding autosuspend support to the usbhid driver, 242experimental patches adding autosuspend support to the usbhid driver,
@@ -321,69 +329,81 @@ driver does so by calling these six functions:
321 void usb_autopm_get_interface_no_resume(struct usb_interface *intf); 329 void usb_autopm_get_interface_no_resume(struct usb_interface *intf);
322 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf); 330 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf);
323 331
324The functions work by maintaining a counter in the usb_interface 332The functions work by maintaining a usage counter in the
325structure. When intf->pm_usage_count is > 0 then the interface is 333usb_interface's embedded device structure. When the counter is > 0
326deemed to be busy, and the kernel will not autosuspend the interface's 334then the interface is deemed to be busy, and the kernel will not
327device. When intf->pm_usage_count is <= 0 then the interface is 335autosuspend the interface's device. When the usage counter is = 0
328considered to be idle, and the kernel may autosuspend the device. 336then the interface is considered to be idle, and the kernel may
337autosuspend the device.
329 338
330(There is a similar pm_usage_count field in struct usb_device, 339(There is a similar usage counter field in struct usb_device,
331associated with the device itself rather than any of its interfaces. 340associated with the device itself rather than any of its interfaces.
332This field is used only by the USB core.) 341This counter is used only by the USB core.)
333 342
334Drivers must not modify intf->pm_usage_count directly; its value 343Drivers need not be concerned about balancing changes to the usage
335should be changed only be using the functions listed above. Drivers 344counter; the USB core will undo any remaining "get"s when a driver
336are responsible for insuring that the overall change to pm_usage_count 345is unbound from its interface. As a corollary, drivers must not call
337during their lifetime balances out to 0 (it may be necessary for the 346any of the usb_autopm_* functions after their diconnect() routine has
338disconnect method to call usb_autopm_put_interface() one or more times 347returned.
339to fulfill this requirement). The first two routines use the PM mutex 348
340in struct usb_device for mutual exclusion; drivers using the async 349Drivers using the async routines are responsible for their own
341routines are responsible for their own synchronization and mutual 350synchronization and mutual exclusion.
342exclusion. 351
343 352 usb_autopm_get_interface() increments the usage counter and
344 usb_autopm_get_interface() increments pm_usage_count and 353 does an autoresume if the device is suspended. If the
345 attempts an autoresume if the new value is > 0 and the 354 autoresume fails, the counter is decremented back.
346 device is suspended. 355
347 356 usb_autopm_put_interface() decrements the usage counter and
348 usb_autopm_put_interface() decrements pm_usage_count and 357 attempts an autosuspend if the new value is = 0.
349 attempts an autosuspend if the new value is <= 0 and the
350 device isn't suspended.
351 358
352 usb_autopm_get_interface_async() and 359 usb_autopm_get_interface_async() and
353 usb_autopm_put_interface_async() do almost the same things as 360 usb_autopm_put_interface_async() do almost the same things as
354 their non-async counterparts. The differences are: they do 361 their non-async counterparts. The big difference is that they
355 not acquire the PM mutex, and they use a workqueue to do their 362 use a workqueue to do the resume or suspend part of their
356 jobs. As a result they can be called in an atomic context, 363 jobs. As a result they can be called in an atomic context,
357 such as an URB's completion handler, but when they return the 364 such as an URB's completion handler, but when they return the
358 device will not generally not yet be in the desired state. 365 device will generally not yet be in the desired state.
359 366
360 usb_autopm_get_interface_no_resume() and 367 usb_autopm_get_interface_no_resume() and
361 usb_autopm_put_interface_no_suspend() merely increment or 368 usb_autopm_put_interface_no_suspend() merely increment or
362 decrement the pm_usage_count value; they do not attempt to 369 decrement the usage counter; they do not attempt to carry out
363 carry out an autoresume or an autosuspend. Hence they can be 370 an autoresume or an autosuspend. Hence they can be called in
364 called in an atomic context. 371 an atomic context.
365 372
366The conventional usage pattern is that a driver calls 373The simplest usage pattern is that a driver calls
367usb_autopm_get_interface() in its open routine and 374usb_autopm_get_interface() in its open routine and
368usb_autopm_put_interface() in its close or release routine. But 375usb_autopm_put_interface() in its close or release routine. But other
369other patterns are possible. 376patterns are possible.
370 377
371The autosuspend attempts mentioned above will often fail for one 378The autosuspend attempts mentioned above will often fail for one
372reason or another. For example, the power/level attribute might be 379reason or another. For example, the power/control attribute might be
373set to "on", or another interface in the same device might not be 380set to "on", or another interface in the same device might not be
374idle. This is perfectly normal. If the reason for failure was that 381idle. This is perfectly normal. If the reason for failure was that
375the device hasn't been idle for long enough, a delayed workqueue 382the device hasn't been idle for long enough, a timer is scheduled to
376routine is automatically set up to carry out the operation when the 383carry out the operation automatically when the autosuspend idle-delay
377autosuspend idle-delay has expired. 384has expired.
378 385
379Autoresume attempts also can fail, although failure would mean that 386Autoresume attempts also can fail, although failure would mean that
380the device is no longer present or operating properly. Unlike 387the device is no longer present or operating properly. Unlike
381autosuspend, there's no delay for an autoresume. 388autosuspend, there's no idle-delay for an autoresume.
382 389
383 390
384 Other parts of the driver interface 391 Other parts of the driver interface
385 ----------------------------------- 392 -----------------------------------
386 393
394Drivers can enable autosuspend for their devices by calling
395
396 usb_enable_autosuspend(struct usb_device *udev);
397
398in their probe() routine, if they know that the device is capable of
399suspending and resuming correctly. This is exactly equivalent to
400writing "auto" to the device's power/control attribute. Likewise,
401drivers can disable autosuspend by calling
402
403 usb_disable_autosuspend(struct usb_device *udev);
404
405This is exactly the same as writing "on" to the power/control attribute.
406
387Sometimes a driver needs to make sure that remote wakeup is enabled 407Sometimes a driver needs to make sure that remote wakeup is enabled
388during autosuspend. For example, there's not much point 408during autosuspend. For example, there's not much point
389autosuspending a keyboard if the user can't cause the keyboard to do a 409autosuspending a keyboard if the user can't cause the keyboard to do a
@@ -395,26 +415,27 @@ though, setting this flag won't cause the kernel to autoresume it.
395Normally a driver would set this flag in its probe method, at which 415Normally a driver would set this flag in its probe method, at which
396time the device is guaranteed not to be autosuspended.) 416time the device is guaranteed not to be autosuspended.)
397 417
398The synchronous usb_autopm_* routines have to run in a sleepable 418If a driver does its I/O asynchronously in interrupt context, it
399process context; they must not be called from an interrupt handler or 419should call usb_autopm_get_interface_async() before starting output and
400while holding a spinlock. In fact, the entire autosuspend mechanism 420usb_autopm_put_interface_async() when the output queue drains. When
401is not well geared toward interrupt-driven operation. However there 421it receives an input event, it should call
402is one thing a driver can do in an interrupt handler:
403 422
404 usb_mark_last_busy(struct usb_device *udev); 423 usb_mark_last_busy(struct usb_device *udev);
405 424
406This sets udev->last_busy to the current time. udev->last_busy is the 425in the event handler. This sets udev->last_busy to the current time.
407field used for idle-delay calculations; updating it will cause any 426udev->last_busy is the field used for idle-delay calculations;
408pending autosuspend to be moved back. The usb_autopm_* routines will 427updating it will cause any pending autosuspend to be moved back. Most
409also set the last_busy field to the current time. 428of the usb_autopm_* routines will also set the last_busy field to the
410 429current time.
411Calling urb_mark_last_busy() from within an URB completion handler is 430
412subject to races: The kernel may have just finished deciding the 431Asynchronous operation is always subject to races. For example, a
413device has been idle for long enough but not yet gotten around to 432driver may call one of the usb_autopm_*_interface_async() routines at
414calling the driver's suspend method. The driver would have to be 433a time when the core has just finished deciding the device has been
415responsible for synchronizing its suspend method with its URB 434idle for long enough but not yet gotten around to calling the driver's
416completion handler and causing the autosuspend to fail with -EBUSY if 435suspend method. The suspend method must be responsible for
417an URB had completed too recently. 436synchronizing with the output request routine and the URB completion
437handler; it should cause autosuspends to fail with -EBUSY if the
438driver needs to use the device.
418 439
419External suspend calls should never be allowed to fail in this way, 440External suspend calls should never be allowed to fail in this way,
420only autosuspend calls. The driver can tell them apart by checking 441only autosuspend calls. The driver can tell them apart by checking
@@ -422,75 +443,23 @@ the PM_EVENT_AUTO bit in the message.event argument to the suspend
422method; this bit will be set for internal PM events (autosuspend) and 443method; this bit will be set for internal PM events (autosuspend) and
423clear for external PM events. 444clear for external PM events.
424 445
425Many of the ingredients in the autosuspend framework are oriented
426towards interfaces: The usb_interface structure contains the
427pm_usage_cnt field, and the usb_autopm_* routines take an interface
428pointer as their argument. But somewhat confusingly, a few of the
429pieces (i.e., usb_mark_last_busy()) use the usb_device structure
430instead. Drivers need to keep this straight; they can call
431interface_to_usbdev() to find the device structure for a given
432interface.
433
434 446
435 Locking requirements 447 Mutual exclusion
436 -------------------- 448 ----------------
437 449
438All three suspend/resume methods are always called while holding the 450For external events -- but not necessarily for autosuspend or
439usb_device's PM mutex. For external events -- but not necessarily for 451autoresume -- the device semaphore (udev->dev.sem) will be held when a
440autosuspend or autoresume -- the device semaphore (udev->dev.sem) will 452suspend or resume method is called. This implies that external
441also be held. This implies that external suspend/resume events are 453suspend/resume events are mutually exclusive with calls to probe,
442mutually exclusive with calls to probe, disconnect, pre_reset, and 454disconnect, pre_reset, and post_reset; the USB core guarantees that
443post_reset; the USB core guarantees that this is true of internal 455this is true of autosuspend/autoresume events as well.
444suspend/resume events as well.
445 456
446If a driver wants to block all suspend/resume calls during some 457If a driver wants to block all suspend/resume calls during some
447critical section, it can simply acquire udev->pm_mutex. Note that 458critical section, the best way is to lock the device and call
448calls to resume may be triggered indirectly. Block IO due to memory 459usb_autopm_get_interface() (and do the reverse at the end of the
449allocations can make the vm subsystem resume a device. Thus while 460critical section). Holding the device semaphore will block all
450holding this lock you must not allocate memory with GFP_KERNEL or 461external PM calls, and the usb_autopm_get_interface() will prevent any
451GFP_NOFS. 462internal PM calls, even if it fails. (Exercise: Why?)
452
453Alternatively, if the critical section might call some of the
454usb_autopm_* routines, the driver can avoid deadlock by doing:
455
456 down(&udev->dev.sem);
457 rc = usb_autopm_get_interface(intf);
458
459and at the end of the critical section:
460
461 if (!rc)
462 usb_autopm_put_interface(intf);
463 up(&udev->dev.sem);
464
465Holding the device semaphore will block all external PM calls, and the
466usb_autopm_get_interface() will prevent any internal PM calls, even if
467it fails. (Exercise: Why?)
468
469The rules for locking order are:
470
471 Never acquire any device semaphore while holding any PM mutex.
472
473 Never acquire udev->pm_mutex while holding the PM mutex for
474 a device that isn't a descendant of udev.
475
476In other words, PM mutexes should only be acquired going up the device
477tree, and they should be acquired only after locking all the device
478semaphores you need to hold. These rules don't matter to drivers very
479much; they usually affect just the USB core.
480
481Still, drivers do need to be careful. For example, many drivers use a
482private mutex to synchronize their normal I/O activities with their
483disconnect method. Now if the driver supports autosuspend then it
484must call usb_autopm_put_interface() from somewhere -- maybe from its
485close method. It should make the call while holding the private mutex,
486since a driver shouldn't call any of the usb_autopm_* functions for an
487interface from which it has been unbound.
488
489But the usb_autpm_* routines always acquire the device's PM mutex, and
490consequently the locking order has to be: private mutex first, PM
491mutex second. Since the suspend method is always called with the PM
492mutex held, it mustn't try to acquire the private mutex. It has to
493synchronize with the driver's I/O activities in some other way.
494 463
495 464
496 Interaction between dynamic PM and system PM 465 Interaction between dynamic PM and system PM
@@ -499,22 +468,11 @@ synchronize with the driver's I/O activities in some other way.
499Dynamic power management and system power management can interact in 468Dynamic power management and system power management can interact in
500a couple of ways. 469a couple of ways.
501 470
502Firstly, a device may already be manually suspended or autosuspended 471Firstly, a device may already be autosuspended when a system suspend
503when a system suspend occurs. Since system suspends are supposed to 472occurs. Since system suspends are supposed to be as transparent as
504be as transparent as possible, the device should remain suspended 473possible, the device should remain suspended following the system
505following the system resume. The 2.6.23 kernel obeys this principle 474resume. But this theory may not work out well in practice; over time
506for manually suspended devices but not for autosuspended devices; they 475the kernel's behavior in this regard has changed.
507do get resumed when the system wakes up. (Presumably they will be
508autosuspended again after their idle-delay time expires.) In later
509kernels this behavior will be fixed.
510
511(There is an exception. If a device would undergo a reset-resume
512instead of a normal resume, and the device is enabled for remote
513wakeup, then the reset-resume takes place even if the device was
514already suspended when the system suspend began. The justification is
515that a reset-resume is a kind of remote-wakeup event. Or to put it
516another way, a device which needs a reset won't be able to generate
517normal remote-wakeup signals, so it ought to be resumed immediately.)
518 476
519Secondly, a dynamic power-management event may occur as a system 477Secondly, a dynamic power-management event may occur as a system
520suspend is underway. The window for this is short, since system 478suspend is underway. The window for this is short, since system