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