📄 power-management.txt
字号:
Power Management for USB Alan Stern <stern@rowland.harvard.edu> October 5, 2007 What is Power Management? -------------------------Power Management (PM) is the practice of saving energy by suspendingparts of a computer system when they aren't being used. While acomponent is "suspended" it is in a nonfunctional low-power state; itmight even be turned off completely. A suspended component can be"resumed" (returned to a functional full-power state) when the kernelneeds to use it. (There also are forms of PM in which components areplaced in a less functional but still usable state instead of beingsuspended; an example would be reducing the CPU's clock rate. Thisdocument will not discuss those other forms.)When the parts being suspended include the CPU and most of the rest ofthe system, we speak of it as a "system suspend". When a particulardevice is turned off while the system as a whole remains running, wecall it a "dynamic suspend" (also known as a "runtime suspend" or"selective suspend"). This document concentrates mostly on howdynamic PM is implemented in the USB subsystem, although system PM iscovered to some extent (see Documentation/power/*.txt for moreinformation about system PM).Note: Dynamic PM support for USB is present only if the kernel wasbuilt with CONFIG_USB_SUSPEND enabled. System PM support is presentonly if the kernel was built with CONFIG_SUSPEND or CONFIG_HIBERNATIONenabled. What is Remote Wakeup? ----------------------When a device has been suspended, it generally doesn't resume untilthe computer tells it to. Likewise, if the entire computer has beensuspended, it generally doesn't resume until the user tells it to, sayby pressing a power button or opening the cover.However some devices have the capability of resuming by themselves, orasking the kernel to resume them, or even telling the entire computerto resume. This capability goes by several names such as "Wake OnLAN"; we will refer to it generically as "remote wakeup". When adevice is enabled for remote wakeup and it is suspended, it may resumeitself (or send a request to be resumed) in response to some externalevent. Examples include a suspended keyboard resuming when a key ispressed, or a suspended USB hub resuming when a device is plugged in. When is a USB device idle? --------------------------A device is idle whenever the kernel thinks it's not busy doinganything important and thus is a candidate for being suspended. Theexact definition depends on the device's driver; drivers are allowedto declare that a device isn't idle even when there's no actualcommunication taking place. (For example, a hub isn't considered idleunless all the devices plugged into that hub are already suspended.)In addition, a device isn't considered idle so long as a program keepsits usbfs file open, whether or not any I/O is going on.If a USB device has no driver, its usbfs file isn't open, and it isn'tbeing accessed through sysfs, then it definitely is idle. Forms of dynamic PM -------------------Dynamic suspends can occur in two ways: manual and automatic."Manual" means that the user has told the kernel to suspend a device,whereas "automatic" means that the kernel has decided all by itself tosuspend a device. Automatic suspend is called "autosuspend" forshort. In general, a device won't be autosuspended unless it has beenidle for some minimum period of time, the so-called idle-delay time.Of course, nothing the kernel does on its own initiative shouldprevent the computer or its devices from working properly. If adevice has been autosuspended and a program tries to use it, thekernel will automatically resume the device (autoresume). For thesame reason, an autosuspended device will usually have remote wakeupenabled, if the device supports remote wakeup.It is worth mentioning that many USB drivers don't supportautosuspend. In fact, at the time of this writing (Linux 2.6.23) theonly drivers which do support it are the hub driver, kaweth, asix,usblp, usblcd, and usb-skeleton (which doesn't count). If anon-supporting driver is bound to a device, the device won't beautosuspended. In effect, the kernel pretends the device is neveridle.We can categorize power management events in two broad classes:external and internal. External events are those triggered by someagent outside the USB stack: system suspend/resume (triggered byuserspace), manual dynamic suspend/resume (also triggered byuserspace), and remote wakeup (triggered by the device). Internalevents are those triggered within the USB stack: autosuspend andautoresume. The user interface for dynamic PM ---------------------------------The user interface for controlling dynamic PM is located in the power/subdirectory of each USB device's sysfs directory, that is, in/sys/bus/usb/devices/.../power/ where "..." is the device's ID. Therelevant attribute files are: wakeup, level, and autosuspend. power/wakeup This file is empty if the device does not support remote wakeup. Otherwise the file contains either the word "enabled" or the word "disabled", and you can write those words to the file. The setting determines whether or not remote wakeup will be enabled when the device is next suspended. (If the setting is changed while the device is suspended, the change won't take effect until the following suspend.) power/level This file contains one of three words: "on", "auto", or "suspend". You can write those words to the file to change the device's setting. "on" means that the device should be resumed and autosuspend is not allowed. (Of course, system suspends are still allowed.) "auto" is the normal state in which the kernel is allowed to autosuspend and autoresume the device. "suspend" means that the device should remain suspended, and autoresume is not allowed. (But remote wakeup may still be allowed, since it is controlled separately by the power/wakeup attribute.) power/autosuspend This file contains an integer value, which is the number of seconds the device should remain idle before the kernel will autosuspend it (the idle-delay time). The default is 2. 0 means to autosuspend as soon as the device becomes idle, and -1 means never to autosuspend. You can write a number to the file to change the autosuspend idle-delay time.Writing "-1" to power/autosuspend and writing "on" to power/level doessentially the same thing -- they both prevent the device from beingautosuspended. Yes, this is a redundancy in the API.(In 2.6.21 writing "0" to power/autosuspend would prevent the devicefrom being autosuspended; the behavior was changed in 2.6.22. Thepower/autosuspend attribute did not exist prior to 2.6.21, and thepower/level attribute did not exist prior to 2.6.22.) Changing the default idle-delay time ------------------------------------The default autosuspend idle-delay time is controlled by a moduleparameter in usbcore. You can specify the value when usbcore isloaded. For example, to set it to 5 seconds instead of 2 you woulddo: modprobe usbcore autosuspend=5Equivalently, you could add to /etc/modprobe.conf a line saying: options usbcore autosuspend=5Some distributions load the usbcore module very early during the bootprocess, by means of a program or script running from an initramfsimage. To alter the parameter value you would have to rebuild thatimage.If usbcore is compiled into the kernel rather than built as a loadablemodule, you can add usbcore.autosuspend=5to the kernel's boot command line.Finally, the parameter value can be changed while the system isrunning. If you do: echo 5 >/sys/module/usbcore/parameters/autosuspendthen each new USB device will have its autosuspend idle-delayinitialized to 5. (The idle-delay values for already existing deviceswill not be affected.)Setting the initial default idle-delay to -1 will prevent anyautosuspend of any USB device. This is a simple alternative todisabling CONFIG_USB_SUSPEND and rebuilding the kernel, and it has theadded benefit of allowing you to enable autosuspend for selecteddevices. Warnings --------The USB specification states that all USB devices must support powermanagement. Nevertheless, the sad fact is that many devices do notsupport it very well. You can suspend them all right, but when youtry to resume them they disconnect themselves from the USB bus orthey stop working entirely. This seems to be especially prevalentamong printers and scanners, but plenty of other types of device havethe same deficiency.For this reason, by default the kernel disables autosuspend (thepower/level attribute is initialized to "on") for all devices otherthan hubs. Hubs, at least, appear to be reasonably well-behaved inthis regard.(In 2.6.21 and 2.6.22 this wasn't the case. Autosuspend was enabledby default for almost all USB devices. A number of people experiencedproblems as a result.)This means that non-hub devices won't be autosuspended unless the useror a program explicitly enables it. As of this writing there aren'tany widespread programs which will do this; we hope that in the nearfuture device managers such as HAL will take on this addedresponsibility. In the meantime you can always carry out thenecessary operations by hand or add them to a udev script. You canalso change the idle-delay time; 2 seconds is not the best choice forevery device.Sometimes it turns out that even when a device does work okay withautosuspend there are still problems. For example, there areexperimental patches adding autosuspend support to the usbhid driver,which manages keyboards and mice, among other things. Tests with anumber of keyboards showed that typing on a suspended keyboard, whilecausing the keyboard to do a remote wakeup all right, wouldnonetheless frequently result in lost keystrokes. Tests with miceshowed that some of them would issue a remote-wakeup request inresponse to button presses but not to motion, and some in response toneither.The kernel will not prevent you from enabling autosuspend on devicesthat can't handle it. It is even possible in theory to damage adevice by suspending it at the wrong time -- for example, suspending aUSB hard disk might cause it to spin down without parking the heads.(Highly unlikely, but possible.) Take care. The driver interface for Power Management -----------------------------------------The requirements for a USB driver to support external power managementare pretty modest; the driver need only define .suspend .resume .reset_resumemethods in its usb_driver structure, and the reset_resume method isoptional. The methods' jobs are quite simple:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -