Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 1 | .. _usb-power-management: |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 2 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 3 | Power Management for USB |
| 4 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 5 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 6 | :Author: Alan Stern <stern@rowland.harvard.edu> |
| 7 | :Date: Last-updated: February 2014 |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 8 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 9 | .. |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 10 | Contents: |
| 11 | --------- |
| 12 | * What is Power Management? |
| 13 | * What is Remote Wakeup? |
| 14 | * When is a USB device idle? |
| 15 | * Forms of dynamic PM |
| 16 | * The user interface for dynamic PM |
| 17 | * Changing the default idle-delay time |
| 18 | * Warnings |
| 19 | * The driver interface for Power Management |
| 20 | * The driver interface for autosuspend and autoresume |
| 21 | * Other parts of the driver interface |
| 22 | * Mutual exclusion |
| 23 | * Interaction between dynamic PM and system PM |
| 24 | * xHCI hardware link PM |
| 25 | * USB Port Power Control |
| 26 | * User Interface for Port Power Control |
| 27 | * Suggested Userspace Port Power Policy |
| 28 | |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 29 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 30 | What is Power Management? |
| 31 | ------------------------- |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 32 | |
| 33 | Power Management (PM) is the practice of saving energy by suspending |
| 34 | parts of a computer system when they aren't being used. While a |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 35 | component is ``suspended`` it is in a nonfunctional low-power state; it |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 36 | might even be turned off completely. A suspended component can be |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 37 | ``resumed`` (returned to a functional full-power state) when the kernel |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 38 | needs to use it. (There also are forms of PM in which components are |
| 39 | placed in a less functional but still usable state instead of being |
| 40 | suspended; an example would be reducing the CPU's clock rate. This |
| 41 | document will not discuss those other forms.) |
| 42 | |
| 43 | When the parts being suspended include the CPU and most of the rest of |
| 44 | the system, we speak of it as a "system suspend". When a particular |
| 45 | device is turned off while the system as a whole remains running, we |
| 46 | call it a "dynamic suspend" (also known as a "runtime suspend" or |
| 47 | "selective suspend"). This document concentrates mostly on how |
| 48 | dynamic PM is implemented in the USB subsystem, although system PM is |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 49 | covered to some extent (see ``Documentation/power/*.txt`` for more |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 50 | information about system PM). |
| 51 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 52 | System PM support is present only if the kernel was built with |
| 53 | ``CONFIG_SUSPEND`` or ``CONFIG_HIBERNATION`` enabled. Dynamic PM support |
| 54 | |
| 55 | for USB is present whenever |
| 56 | the kernel was built with ``CONFIG_PM`` enabled. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 57 | |
Rafael J. Wysocki | ceb6c9c | 2014-11-29 23:47:05 +0100 | [diff] [blame] | 58 | [Historically, dynamic PM support for USB was present only if the |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 59 | kernel had been built with ``CONFIG_USB_SUSPEND`` enabled (which depended on |
| 60 | ``CONFIG_PM_RUNTIME``). Starting with the 3.10 kernel release, dynamic PM |
| 61 | support for USB was present whenever the kernel was built with |
| 62 | ``CONFIG_PM_RUNTIME`` enabled. The ``CONFIG_USB_SUSPEND`` option had been |
| 63 | eliminated.] |
Alan Stern | 4e9c8e5 | 2013-03-28 15:31:13 -0400 | [diff] [blame] | 64 | |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 65 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 66 | What is Remote Wakeup? |
| 67 | ---------------------- |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 68 | |
| 69 | When a device has been suspended, it generally doesn't resume until |
| 70 | the computer tells it to. Likewise, if the entire computer has been |
| 71 | suspended, it generally doesn't resume until the user tells it to, say |
| 72 | by pressing a power button or opening the cover. |
| 73 | |
| 74 | However some devices have the capability of resuming by themselves, or |
| 75 | asking the kernel to resume them, or even telling the entire computer |
| 76 | to resume. This capability goes by several names such as "Wake On |
| 77 | LAN"; we will refer to it generically as "remote wakeup". When a |
| 78 | device is enabled for remote wakeup and it is suspended, it may resume |
| 79 | itself (or send a request to be resumed) in response to some external |
| 80 | event. Examples include a suspended keyboard resuming when a key is |
| 81 | pressed, or a suspended USB hub resuming when a device is plugged in. |
| 82 | |
| 83 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 84 | When is a USB device idle? |
| 85 | -------------------------- |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 86 | |
| 87 | A device is idle whenever the kernel thinks it's not busy doing |
| 88 | anything important and thus is a candidate for being suspended. The |
| 89 | exact definition depends on the device's driver; drivers are allowed |
| 90 | to declare that a device isn't idle even when there's no actual |
| 91 | communication taking place. (For example, a hub isn't considered idle |
| 92 | unless all the devices plugged into that hub are already suspended.) |
| 93 | In addition, a device isn't considered idle so long as a program keeps |
| 94 | its usbfs file open, whether or not any I/O is going on. |
| 95 | |
| 96 | If a USB device has no driver, its usbfs file isn't open, and it isn't |
| 97 | being accessed through sysfs, then it definitely is idle. |
| 98 | |
| 99 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 100 | Forms of dynamic PM |
| 101 | ------------------- |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 102 | |
Alan Stern | baf6774 | 2009-12-08 15:49:48 -0500 | [diff] [blame] | 103 | Dynamic suspends occur when the kernel decides to suspend an idle |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 104 | device. This is called ``autosuspend`` for short. In general, a device |
Alan Stern | baf6774 | 2009-12-08 15:49:48 -0500 | [diff] [blame] | 105 | won't be autosuspended unless it has been idle for some minimum period |
| 106 | of time, the so-called idle-delay time. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 107 | |
| 108 | Of course, nothing the kernel does on its own initiative should |
| 109 | prevent the computer or its devices from working properly. If a |
| 110 | device has been autosuspended and a program tries to use it, the |
| 111 | kernel will automatically resume the device (autoresume). For the |
| 112 | same reason, an autosuspended device will usually have remote wakeup |
| 113 | enabled, if the device supports remote wakeup. |
| 114 | |
| 115 | It is worth mentioning that many USB drivers don't support |
| 116 | autosuspend. In fact, at the time of this writing (Linux 2.6.23) the |
| 117 | only drivers which do support it are the hub driver, kaweth, asix, |
| 118 | usblp, usblcd, and usb-skeleton (which doesn't count). If a |
| 119 | non-supporting driver is bound to a device, the device won't be |
| 120 | autosuspended. In effect, the kernel pretends the device is never |
| 121 | idle. |
| 122 | |
| 123 | We can categorize power management events in two broad classes: |
| 124 | external and internal. External events are those triggered by some |
| 125 | agent outside the USB stack: system suspend/resume (triggered by |
Alan Stern | baf6774 | 2009-12-08 15:49:48 -0500 | [diff] [blame] | 126 | userspace), manual dynamic resume (also triggered by userspace), and |
| 127 | remote wakeup (triggered by the device). Internal events are those |
| 128 | triggered within the USB stack: autosuspend and autoresume. Note that |
| 129 | all dynamic suspend events are internal; external agents are not |
| 130 | allowed to issue dynamic suspends. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 131 | |
| 132 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 133 | The user interface for dynamic PM |
| 134 | --------------------------------- |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 135 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 136 | The user interface for controlling dynamic PM is located in the ``power/`` |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 137 | subdirectory of each USB device's sysfs directory, that is, in |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 138 | ``/sys/bus/usb/devices/.../power/`` where "..." is the device's ID. The |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 139 | relevant attribute files are: wakeup, control, and |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 140 | ``autosuspend_delay_ms``. (There may also be a file named ``level``; this |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 141 | file was deprecated as of the 2.6.35 kernel and replaced by the |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 142 | ``control`` file. In 2.6.38 the ``autosuspend`` file will be deprecated |
| 143 | and replaced by the ``autosuspend_delay_ms`` file. The only difference |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 144 | is that the newer file expresses the delay in milliseconds whereas the |
| 145 | older file uses seconds. Confusingly, both files are present in 2.6.37 |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 146 | but only ``autosuspend`` works.) |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 147 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 148 | ``power/wakeup`` |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 149 | |
| 150 | This file is empty if the device does not support |
| 151 | remote wakeup. Otherwise the file contains either the |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 152 | word ``enabled`` or the word ``disabled``, and you can |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 153 | write those words to the file. The setting determines |
| 154 | whether or not remote wakeup will be enabled when the |
| 155 | device is next suspended. (If the setting is changed |
| 156 | while the device is suspended, the change won't take |
| 157 | effect until the following suspend.) |
| 158 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 159 | ``power/control`` |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 160 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 161 | This file contains one of two words: ``on`` or ``auto``. |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 162 | You can write those words to the file to change the |
| 163 | device's setting. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 164 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 165 | - ``on`` means that the device should be resumed and |
| 166 | autosuspend is not allowed. (Of course, system |
| 167 | suspends are still allowed.) |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 168 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 169 | - ``auto`` is the normal state in which the kernel is |
| 170 | allowed to autosuspend and autoresume the device. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 171 | |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 172 | (In kernels up to 2.6.32, you could also specify |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 173 | ``suspend``, meaning that the device should remain |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 174 | suspended and autoresume was not allowed. This |
| 175 | setting is no longer supported.) |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 176 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 177 | ``power/autosuspend_delay_ms`` |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 178 | |
| 179 | This file contains an integer value, which is the |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 180 | number of milliseconds the device should remain idle |
| 181 | before the kernel will autosuspend it (the idle-delay |
| 182 | time). The default is 2000. 0 means to autosuspend |
| 183 | as soon as the device becomes idle, and negative |
| 184 | values mean never to autosuspend. You can write a |
| 185 | number to the file to change the autosuspend |
| 186 | idle-delay time. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 187 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 188 | Writing ``-1`` to ``power/autosuspend_delay_ms`` and writing ``on`` to |
| 189 | ``power/control`` do essentially the same thing -- they both prevent the |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 190 | device from being autosuspended. Yes, this is a redundancy in the |
| 191 | API. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 192 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 193 | (In 2.6.21 writing ``0`` to ``power/autosuspend`` would prevent the device |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 194 | from being autosuspended; the behavior was changed in 2.6.22. The |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 195 | ``power/autosuspend`` attribute did not exist prior to 2.6.21, and the |
| 196 | ``power/level`` attribute did not exist prior to 2.6.22. ``power/control`` |
| 197 | was added in 2.6.34, and ``power/autosuspend_delay_ms`` was added in |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 198 | 2.6.37 but did not become functional until 2.6.38.) |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 199 | |
| 200 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 201 | Changing the default idle-delay time |
| 202 | ------------------------------------ |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 203 | |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 204 | The default autosuspend idle-delay time (in seconds) is controlled by |
| 205 | a module parameter in usbcore. You can specify the value when usbcore |
| 206 | is loaded. For example, to set it to 5 seconds instead of 2 you would |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 207 | do:: |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 208 | |
| 209 | modprobe usbcore autosuspend=5 |
| 210 | |
Lucas De Marchi | 970e248 | 2012-03-30 13:37:16 -0700 | [diff] [blame] | 211 | Equivalently, you could add to a configuration file in /etc/modprobe.d |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 212 | a line saying:: |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 213 | |
| 214 | options usbcore autosuspend=5 |
| 215 | |
| 216 | Some distributions load the usbcore module very early during the boot |
| 217 | process, by means of a program or script running from an initramfs |
| 218 | image. To alter the parameter value you would have to rebuild that |
| 219 | image. |
| 220 | |
| 221 | If usbcore is compiled into the kernel rather than built as a loadable |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 222 | module, you can add:: |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 223 | |
| 224 | usbcore.autosuspend=5 |
| 225 | |
| 226 | to the kernel's boot command line. |
| 227 | |
| 228 | Finally, the parameter value can be changed while the system is |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 229 | running. If you do:: |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 230 | |
| 231 | echo 5 >/sys/module/usbcore/parameters/autosuspend |
| 232 | |
| 233 | then each new USB device will have its autosuspend idle-delay |
| 234 | initialized to 5. (The idle-delay values for already existing devices |
| 235 | will not be affected.) |
| 236 | |
| 237 | Setting the initial default idle-delay to -1 will prevent any |
Alan Stern | 4e9c8e5 | 2013-03-28 15:31:13 -0400 | [diff] [blame] | 238 | autosuspend of any USB device. This has the benefit of allowing you |
| 239 | then to enable autosuspend for selected devices. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 240 | |
| 241 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 242 | Warnings |
| 243 | -------- |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 244 | |
| 245 | The USB specification states that all USB devices must support power |
| 246 | management. Nevertheless, the sad fact is that many devices do not |
| 247 | support it very well. You can suspend them all right, but when you |
| 248 | try to resume them they disconnect themselves from the USB bus or |
| 249 | they stop working entirely. This seems to be especially prevalent |
| 250 | among printers and scanners, but plenty of other types of device have |
| 251 | the same deficiency. |
| 252 | |
| 253 | For this reason, by default the kernel disables autosuspend (the |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 254 | ``power/control`` attribute is initialized to ``on``) for all devices other |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 255 | than hubs. Hubs, at least, appear to be reasonably well-behaved in |
| 256 | this regard. |
| 257 | |
| 258 | (In 2.6.21 and 2.6.22 this wasn't the case. Autosuspend was enabled |
| 259 | by default for almost all USB devices. A number of people experienced |
| 260 | problems as a result.) |
| 261 | |
| 262 | This means that non-hub devices won't be autosuspended unless the user |
| 263 | or a program explicitly enables it. As of this writing there aren't |
| 264 | any widespread programs which will do this; we hope that in the near |
| 265 | future device managers such as HAL will take on this added |
| 266 | responsibility. In the meantime you can always carry out the |
| 267 | necessary operations by hand or add them to a udev script. You can |
| 268 | also change the idle-delay time; 2 seconds is not the best choice for |
| 269 | every device. |
| 270 | |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 271 | If a driver knows that its device has proper suspend/resume support, |
| 272 | it can enable autosuspend all by itself. For example, the video |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 273 | driver for a laptop's webcam might do this (in recent kernels they |
| 274 | do), since these devices are rarely used and so should normally be |
| 275 | autosuspended. |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 276 | |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 277 | Sometimes it turns out that even when a device does work okay with |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 278 | autosuspend there are still problems. For example, the usbhid driver, |
| 279 | which manages keyboards and mice, has autosuspend support. Tests with |
| 280 | a number of keyboards show that typing on a suspended keyboard, while |
| 281 | causing the keyboard to do a remote wakeup all right, will nonetheless |
| 282 | frequently result in lost keystrokes. Tests with mice show that some |
| 283 | of them will issue a remote-wakeup request in response to button |
| 284 | presses but not to motion, and some in response to neither. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 285 | |
| 286 | The kernel will not prevent you from enabling autosuspend on devices |
| 287 | that can't handle it. It is even possible in theory to damage a |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 288 | device by suspending it at the wrong time. (Highly unlikely, but |
| 289 | possible.) Take care. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 290 | |
| 291 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 292 | The driver interface for Power Management |
| 293 | ----------------------------------------- |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 294 | |
| 295 | The requirements for a USB driver to support external power management |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 296 | are pretty modest; the driver need only define:: |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 297 | |
| 298 | .suspend |
| 299 | .resume |
| 300 | .reset_resume |
| 301 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 302 | methods in its :c:type:`usb_driver` structure, and the ``reset_resume`` method |
| 303 | is optional. The methods' jobs are quite simple: |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 304 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 305 | - The ``suspend`` method is called to warn the driver that the |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 306 | device is going to be suspended. If the driver returns a |
| 307 | negative error code, the suspend will be aborted. Normally |
| 308 | the driver will return 0, in which case it must cancel all |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 309 | outstanding URBs (:c:func:`usb_kill_urb`) and not submit any more. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 310 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 311 | - The ``resume`` method is called to tell the driver that the |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 312 | device has been resumed and the driver can return to normal |
| 313 | operation. URBs may once more be submitted. |
| 314 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 315 | - The ``reset_resume`` method is called to tell the driver that |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 316 | the device has been resumed and it also has been reset. |
| 317 | The driver should redo any necessary device initialization, |
| 318 | since the device has probably lost most or all of its state |
| 319 | (although the interfaces will be in the same altsettings as |
| 320 | before the suspend). |
| 321 | |
Alan Stern | 3c886c5 | 2007-11-16 11:58:15 -0500 | [diff] [blame] | 322 | If the device is disconnected or powered down while it is suspended, |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 323 | the ``disconnect`` method will be called instead of the ``resume`` or |
| 324 | ``reset_resume`` method. This is also quite likely to happen when |
Alan Stern | 3c886c5 | 2007-11-16 11:58:15 -0500 | [diff] [blame] | 325 | waking up from hibernation, as many systems do not maintain suspend |
| 326 | current to the USB host controllers during hibernation. (It's |
| 327 | possible to work around the hibernation-forces-disconnect problem by |
| 328 | using the USB Persist facility.) |
| 329 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 330 | The ``reset_resume`` method is used by the USB Persist facility (see |
Mauro Carvalho Chehab | e1c3e6e | 2017-04-05 10:23:15 -0300 | [diff] [blame] | 331 | :ref:`usb-persist`) and it can also be used under certain |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 332 | circumstances when ``CONFIG_USB_PERSIST`` is not enabled. Currently, if a |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 333 | device is reset during a resume and the driver does not have a |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 334 | ``reset_resume`` method, the driver won't receive any notification about |
| 335 | the resume. Later kernels will call the driver's ``disconnect`` method; |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 336 | 2.6.23 doesn't do this. |
| 337 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 338 | USB drivers are bound to interfaces, so their ``suspend`` and ``resume`` |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 339 | methods get called when the interfaces are suspended or resumed. In |
| 340 | principle one might want to suspend some interfaces on a device (i.e., |
| 341 | force the drivers for those interface to stop all activity) without |
| 342 | suspending the other interfaces. The USB core doesn't allow this; all |
| 343 | interfaces are suspended when the device itself is suspended and all |
| 344 | interfaces are resumed when the device is resumed. It isn't possible |
| 345 | to suspend or resume some but not all of a device's interfaces. The |
| 346 | closest you can come is to unbind the interfaces' drivers. |
| 347 | |
| 348 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 349 | The driver interface for autosuspend and autoresume |
| 350 | --------------------------------------------------- |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 351 | |
| 352 | To support autosuspend and autoresume, a driver should implement all |
| 353 | three of the methods listed above. In addition, a driver indicates |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 354 | that it supports autosuspend by setting the ``.supports_autosuspend`` flag |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 355 | in its usb_driver structure. It is then responsible for informing the |
| 356 | USB core whenever one of its interfaces becomes busy or idle. The |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 357 | driver does so by calling these six functions:: |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 358 | |
| 359 | int usb_autopm_get_interface(struct usb_interface *intf); |
| 360 | void usb_autopm_put_interface(struct usb_interface *intf); |
Alan Stern | 9ac39f2 | 2008-11-12 16:19:49 -0500 | [diff] [blame] | 361 | int usb_autopm_get_interface_async(struct usb_interface *intf); |
| 362 | void usb_autopm_put_interface_async(struct usb_interface *intf); |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 363 | void usb_autopm_get_interface_no_resume(struct usb_interface *intf); |
| 364 | void usb_autopm_put_interface_no_suspend(struct usb_interface *intf); |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 365 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 366 | The functions work by maintaining a usage counter in the |
| 367 | usb_interface's embedded device structure. When the counter is > 0 |
| 368 | then the interface is deemed to be busy, and the kernel will not |
| 369 | autosuspend the interface's device. When the usage counter is = 0 |
| 370 | then the interface is considered to be idle, and the kernel may |
| 371 | autosuspend the device. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 372 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 373 | Drivers need not be concerned about balancing changes to the usage |
| 374 | counter; the USB core will undo any remaining "get"s when a driver |
| 375 | is unbound from its interface. As a corollary, drivers must not call |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 376 | any of the ``usb_autopm_*`` functions after their ``disconnect`` |
| 377 | routine has returned. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 378 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 379 | Drivers using the async routines are responsible for their own |
| 380 | synchronization and mutual exclusion. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 381 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 382 | :c:func:`usb_autopm_get_interface` increments the usage counter and |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 383 | does an autoresume if the device is suspended. If the |
| 384 | autoresume fails, the counter is decremented back. |
| 385 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 386 | :c:func:`usb_autopm_put_interface` decrements the usage counter and |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 387 | attempts an autosuspend if the new value is = 0. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 388 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 389 | :c:func:`usb_autopm_get_interface_async` and |
| 390 | :c:func:`usb_autopm_put_interface_async` do almost the same things as |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 391 | their non-async counterparts. The big difference is that they |
| 392 | use a workqueue to do the resume or suspend part of their |
Alan Stern | 9ac39f2 | 2008-11-12 16:19:49 -0500 | [diff] [blame] | 393 | jobs. As a result they can be called in an atomic context, |
| 394 | such as an URB's completion handler, but when they return the |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 395 | device will generally not yet be in the desired state. |
Alan Stern | 9ac39f2 | 2008-11-12 16:19:49 -0500 | [diff] [blame] | 396 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 397 | :c:func:`usb_autopm_get_interface_no_resume` and |
| 398 | :c:func:`usb_autopm_put_interface_no_suspend` merely increment or |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 399 | decrement the usage counter; they do not attempt to carry out |
| 400 | an autoresume or an autosuspend. Hence they can be called in |
| 401 | an atomic context. |
Geoff Levand | 81ab5b8 | 2008-09-20 14:41:47 -0700 | [diff] [blame] | 402 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 403 | The simplest usage pattern is that a driver calls |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 404 | :c:func:`usb_autopm_get_interface` in its open routine and |
| 405 | :c:func:`usb_autopm_put_interface` in its close or release routine. But other |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 406 | patterns are possible. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 407 | |
| 408 | The autosuspend attempts mentioned above will often fail for one |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 409 | reason or another. For example, the ``power/control`` attribute might be |
| 410 | set to ``on``, or another interface in the same device might not be |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 411 | idle. This is perfectly normal. If the reason for failure was that |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 412 | the device hasn't been idle for long enough, a timer is scheduled to |
| 413 | carry out the operation automatically when the autosuspend idle-delay |
| 414 | has expired. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 415 | |
Alan Stern | baf6774 | 2009-12-08 15:49:48 -0500 | [diff] [blame] | 416 | Autoresume attempts also can fail, although failure would mean that |
| 417 | the device is no longer present or operating properly. Unlike |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 418 | autosuspend, there's no idle-delay for an autoresume. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 419 | |
| 420 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 421 | Other parts of the driver interface |
| 422 | ----------------------------------- |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 423 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 424 | Drivers can enable autosuspend for their devices by calling:: |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 425 | |
| 426 | usb_enable_autosuspend(struct usb_device *udev); |
| 427 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 428 | in their :c:func:`probe` routine, if they know that the device is capable of |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 429 | suspending and resuming correctly. This is exactly equivalent to |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 430 | writing ``auto`` to the device's ``power/control`` attribute. Likewise, |
| 431 | drivers can disable autosuspend by calling:: |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 432 | |
| 433 | usb_disable_autosuspend(struct usb_device *udev); |
| 434 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 435 | This is exactly the same as writing ``on`` to the ``power/control`` attribute. |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 436 | |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 437 | Sometimes a driver needs to make sure that remote wakeup is enabled |
| 438 | during autosuspend. For example, there's not much point |
| 439 | autosuspending a keyboard if the user can't cause the keyboard to do a |
| 440 | remote wakeup by typing on it. If the driver sets |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 441 | ``intf->needs_remote_wakeup`` to 1, the kernel won't autosuspend the |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 442 | device if remote wakeup isn't available. (If the device is already |
| 443 | autosuspended, though, setting this flag won't cause the kernel to |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 444 | autoresume it. Normally a driver would set this flag in its ``probe`` |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 445 | method, at which time the device is guaranteed not to be |
| 446 | autosuspended.) |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 447 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 448 | If a driver does its I/O asynchronously in interrupt context, it |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 449 | should call :c:func:`usb_autopm_get_interface_async` before starting output and |
| 450 | :c:func:`usb_autopm_put_interface_async` when the output queue drains. When |
| 451 | it receives an input event, it should call:: |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 452 | |
| 453 | usb_mark_last_busy(struct usb_device *udev); |
| 454 | |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 455 | in the event handler. This tells the PM core that the device was just |
| 456 | busy and therefore the next autosuspend idle-delay expiration should |
| 457 | be pushed back. Many of the usb_autopm_* routines also make this call, |
| 458 | so drivers need to worry only when interrupt-driven input arrives. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 459 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 460 | Asynchronous operation is always subject to races. For example, a |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 461 | driver may call the :c:func:`usb_autopm_get_interface_async` routine at a time |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 462 | when the core has just finished deciding the device has been idle for |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 463 | long enough but not yet gotten around to calling the driver's ``suspend`` |
| 464 | method. The ``suspend`` method must be responsible for synchronizing with |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 465 | the I/O request routine and the URB completion handler; it should |
| 466 | cause autosuspends to fail with -EBUSY if the driver needs to use the |
| 467 | device. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 468 | |
| 469 | External suspend calls should never be allowed to fail in this way, |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 470 | only autosuspend calls. The driver can tell them apart by applying |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 471 | the :c:func:`PMSG_IS_AUTO` macro to the message argument to the ``suspend`` |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 472 | method; it will return True for internal PM events (autosuspend) and |
| 473 | False for external PM events. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 474 | |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 475 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 476 | Mutual exclusion |
| 477 | ---------------- |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 478 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 479 | For external events -- but not necessarily for autosuspend or |
| 480 | autoresume -- the device semaphore (udev->dev.sem) will be held when a |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 481 | ``suspend`` or ``resume`` method is called. This implies that external |
| 482 | suspend/resume events are mutually exclusive with calls to ``probe``, |
| 483 | ``disconnect``, ``pre_reset``, and ``post_reset``; the USB core guarantees that |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 484 | this is true of autosuspend/autoresume events as well. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 485 | |
| 486 | If a driver wants to block all suspend/resume calls during some |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 487 | critical section, the best way is to lock the device and call |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 488 | :c:func:`usb_autopm_get_interface` (and do the reverse at the end of the |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 489 | critical section). Holding the device semaphore will block all |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 490 | external PM calls, and the :c:func:`usb_autopm_get_interface` will prevent any |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 491 | internal PM calls, even if it fails. (Exercise: Why?) |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 492 | |
| 493 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 494 | Interaction between dynamic PM and system PM |
| 495 | -------------------------------------------- |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 496 | |
| 497 | Dynamic power management and system power management can interact in |
| 498 | a couple of ways. |
| 499 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 500 | Firstly, a device may already be autosuspended when a system suspend |
| 501 | occurs. Since system suspends are supposed to be as transparent as |
| 502 | possible, the device should remain suspended following the system |
| 503 | resume. But this theory may not work out well in practice; over time |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 504 | the kernel's behavior in this regard has changed. As of 2.6.37 the |
| 505 | policy is to resume all devices during a system resume and let them |
| 506 | handle their own runtime suspends afterward. |
Alan Stern | cd38c1e | 2007-10-10 16:24:56 -0400 | [diff] [blame] | 507 | |
| 508 | Secondly, a dynamic power-management event may occur as a system |
| 509 | suspend is underway. The window for this is short, since system |
| 510 | suspends don't take long (a few seconds usually), but it can happen. |
| 511 | For example, a suspended device may send a remote-wakeup signal while |
| 512 | the system is suspending. The remote wakeup may succeed, which would |
| 513 | cause the system suspend to abort. If the remote wakeup doesn't |
| 514 | succeed, it may still remain active and thus cause the system to |
| 515 | resume as soon as the system suspend is complete. Or the remote |
| 516 | wakeup may fail and get lost. Which outcome occurs depends on timing |
| 517 | and on the hardware and firmware design. |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 518 | |
| 519 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 520 | xHCI hardware link PM |
| 521 | --------------------- |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 522 | |
| 523 | xHCI host controller provides hardware link power management to usb2.0 |
| 524 | (xHCI 1.0 feature) and usb3.0 devices which support link PM. By |
| 525 | enabling hardware LPM, the host can automatically put the device into |
| 526 | lower power state(L1 for usb2.0 devices, or U1/U2 for usb3.0 devices), |
| 527 | which state device can enter and resume very quickly. |
| 528 | |
Kevin Strasser | 655fe4e | 2015-06-16 10:35:30 -0700 | [diff] [blame] | 529 | The user interface for controlling hardware LPM is located in the |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 530 | ``power/`` subdirectory of each USB device's sysfs directory, that is, in |
| 531 | ``/sys/bus/usb/devices/.../power/`` where "..." is the device's ID. The |
| 532 | relevant attribute files are ``usb2_hardware_lpm`` and ``usb3_hardware_lpm``. |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 533 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 534 | ``power/usb2_hardware_lpm`` |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 535 | |
| 536 | When a USB2 device which support LPM is plugged to a |
| 537 | xHCI host root hub which support software LPM, the |
| 538 | host will run a software LPM test for it; if the device |
| 539 | enters L1 state and resume successfully and the host |
| 540 | supports USB2 hardware LPM, this file will show up and |
| 541 | driver will enable hardware LPM for the device. You |
| 542 | can write y/Y/1 or n/N/0 to the file to enable/disable |
| 543 | USB2 hardware LPM manually. This is for test purpose mainly. |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 544 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 545 | ``power/usb3_hardware_lpm_u1`` |
| 546 | ``power/usb3_hardware_lpm_u2`` |
Kevin Strasser | 655fe4e | 2015-06-16 10:35:30 -0700 | [diff] [blame] | 547 | |
| 548 | When a USB 3.0 lpm-capable device is plugged in to a |
| 549 | xHCI host which supports link PM, it will check if U1 |
| 550 | and U2 exit latencies have been set in the BOS |
Masanari Iida | 8da9704 | 2017-01-24 21:45:15 +0900 | [diff] [blame] | 551 | descriptor; if the check is passed and the host |
Kevin Strasser | 655fe4e | 2015-06-16 10:35:30 -0700 | [diff] [blame] | 552 | supports USB3 hardware LPM, USB3 hardware LPM will be |
Lu Baolu | bf5ce5b | 2015-11-14 16:26:32 +0800 | [diff] [blame] | 553 | enabled for the device and these files will be created. |
| 554 | The files hold a string value (enable or disable) |
| 555 | indicating whether or not USB3 hardware LPM U1 or U2 |
| 556 | is enabled for the device. |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 557 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 558 | USB Port Power Control |
| 559 | ---------------------- |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 560 | |
| 561 | In addition to suspending endpoint devices and enabling hardware |
| 562 | controlled link power management, the USB subsystem also has the |
| 563 | capability to disable power to ports under some conditions. Power is |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 564 | controlled through ``Set/ClearPortFeature(PORT_POWER)`` requests to a hub. |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 565 | In the case of a root or platform-internal hub the host controller |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 566 | driver translates ``PORT_POWER`` requests into platform firmware (ACPI) |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 567 | method calls to set the port power state. For more background see the |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 568 | Linux Plumbers Conference 2012 slides [#f1]_ and video [#f2]_: |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 569 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 570 | Upon receiving a ``ClearPortFeature(PORT_POWER)`` request a USB port is |
| 571 | logically off, and may trigger the actual loss of VBUS to the port [#f3]_. |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 572 | VBUS may be maintained in the case where a hub gangs multiple ports into |
| 573 | a shared power well causing power to remain until all ports in the gang |
| 574 | are turned off. VBUS may also be maintained by hub ports configured for |
| 575 | a charging application. In any event a logically off port will lose |
| 576 | connection with its device, not respond to hotplug events, and not |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 577 | respond to remote wakeup events. |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 578 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 579 | .. warning:: |
| 580 | |
| 581 | turning off a port may result in the inability to hot add a device. |
| 582 | Please see "User Interface for Port Power Control" for details. |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 583 | |
| 584 | As far as the effect on the device itself it is similar to what a device |
| 585 | goes through during system suspend, i.e. the power session is lost. Any |
| 586 | USB device or driver that misbehaves with system suspend will be |
| 587 | similarly affected by a port power cycle event. For this reason the |
| 588 | implementation shares the same device recovery path (and honors the same |
| 589 | quirks) as the system resume path for the hub. |
| 590 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 591 | .. [#f1] |
| 592 | |
| 593 | http://dl.dropbox.com/u/96820575/sarah-sharp-lpt-port-power-off2-mini.pdf |
| 594 | |
| 595 | .. [#f2] |
| 596 | |
| 597 | http://linuxplumbers.ubicast.tv/videos/usb-port-power-off-kerneluserspace-api/ |
| 598 | |
| 599 | .. [#f3] |
| 600 | |
| 601 | USB 3.1 Section 10.12 |
| 602 | |
| 603 | wakeup note: if a device is configured to send wakeup events the port |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 604 | power control implementation will block poweroff attempts on that |
| 605 | port. |
| 606 | |
| 607 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 608 | User Interface for Port Power Control |
| 609 | ------------------------------------- |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 610 | |
| 611 | The port power control mechanism uses the PM runtime system. Poweroff is |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 612 | requested by clearing the ``power/pm_qos_no_power_off`` flag of the port device |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 613 | (defaults to 1). If the port is disconnected it will immediately receive a |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 614 | ``ClearPortFeature(PORT_POWER)`` request. Otherwise, it will honor the pm |
| 615 | runtime rules and require the attached child device and all descendants to be |
| 616 | suspended. This mechanism is dependent on the hub advertising port power |
| 617 | switching in its hub descriptor (wHubCharacteristics logical power switching |
| 618 | mode field). |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 619 | |
| 620 | Note, some interface devices/drivers do not support autosuspend. Userspace may |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 621 | need to unbind the interface drivers before the :c:type:`usb_device` will |
| 622 | suspend. An unbound interface device is suspended by default. When unbinding, |
| 623 | be careful to unbind interface drivers, not the driver of the parent usb |
| 624 | device. Also, leave hub interface drivers bound. If the driver for the usb |
| 625 | device (not interface) is unbound the kernel is no longer able to resume the |
| 626 | device. If a hub interface driver is unbound, control of its child ports is |
| 627 | lost and all attached child-devices will disconnect. A good rule of thumb is |
| 628 | that if the 'driver/module' link for a device points to |
| 629 | ``/sys/module/usbcore`` then unbinding it will interfere with port power |
| 630 | control. |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 631 | |
| 632 | Example of the relevant files for port power control. Note, in this example |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 633 | these files are relative to a usb hub device (prefix):: |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 634 | |
| 635 | prefix=/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1 |
| 636 | |
| 637 | attached child device + |
| 638 | hub port device + | |
| 639 | hub interface device + | | |
| 640 | v v v |
| 641 | $prefix/3-1:1.0/3-1-port1/device |
| 642 | |
| 643 | $prefix/3-1:1.0/3-1-port1/power/pm_qos_no_power_off |
| 644 | $prefix/3-1:1.0/3-1-port1/device/power/control |
| 645 | $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intf0>/driver/unbind |
| 646 | $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intf1>/driver/unbind |
| 647 | ... |
| 648 | $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intfN>/driver/unbind |
| 649 | |
| 650 | In addition to these files some ports may have a 'peer' link to a port on |
| 651 | another hub. The expectation is that all superspeed ports have a |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 652 | hi-speed peer:: |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 653 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 654 | $prefix/3-1:1.0/3-1-port1/peer -> ../../../../usb2/2-1/2-1:1.0/2-1-port1 |
| 655 | ../../../../usb2/2-1/2-1:1.0/2-1-port1/peer -> ../../../../usb3/3-1/3-1:1.0/3-1-port1 |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 656 | |
| 657 | Distinct from 'companion ports', or 'ehci/xhci shared switchover ports' |
| 658 | peer ports are simply the hi-speed and superspeed interface pins that |
| 659 | are combined into a single usb3 connector. Peer ports share the same |
| 660 | ancestor XHCI device. |
| 661 | |
| 662 | While a superspeed port is powered off a device may downgrade its |
| 663 | connection and attempt to connect to the hi-speed pins. The |
| 664 | implementation takes steps to prevent this: |
| 665 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 666 | 1. Port suspend is sequenced to guarantee that hi-speed ports are powered-off |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 667 | before their superspeed peer is permitted to power-off. The implication is |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 668 | that the setting ``pm_qos_no_power_off`` to zero on a superspeed port may |
| 669 | not cause the port to power-off until its highspeed peer has gone to its |
| 670 | runtime suspend state. Userspace must take care to order the suspensions |
| 671 | if it wants to guarantee that a superspeed port will power-off. |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 672 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 673 | 2. Port resume is sequenced to force a superspeed port to power-on prior to its |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 674 | highspeed peer. |
| 675 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 676 | 3. Port resume always triggers an attached child device to resume. After a |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 677 | power session is lost the device may have been removed, or need reset. |
| 678 | Resuming the child device when the parent port regains power resolves those |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 679 | states and clamps the maximum port power cycle frequency at the rate the |
| 680 | child device can suspend (autosuspend-delay) and resume (reset-resume |
| 681 | latency). |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 682 | |
| 683 | Sysfs files relevant for port power control: |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 684 | |
| 685 | ``<hubdev-portX>/power/pm_qos_no_power_off``: |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 686 | This writable flag controls the state of an idle port. |
| 687 | Once all children and descendants have suspended the |
| 688 | port may suspend/poweroff provided that |
| 689 | pm_qos_no_power_off is '0'. If pm_qos_no_power_off is |
| 690 | '1' the port will remain active/powered regardless of |
| 691 | the stats of descendants. Defaults to 1. |
| 692 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 693 | ``<hubdev-portX>/power/runtime_status``: |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 694 | This file reflects whether the port is 'active' (power is on) |
| 695 | or 'suspended' (logically off). There is no indication to |
| 696 | userspace whether VBUS is still supplied. |
| 697 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 698 | ``<hubdev-portX>/connect_type``: |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 699 | An advisory read-only flag to userspace indicating the |
| 700 | location and connection type of the port. It returns |
| 701 | one of four values 'hotplug', 'hardwired', 'not used', |
| 702 | and 'unknown'. All values, besides unknown, are set by |
| 703 | platform firmware. |
| 704 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 705 | ``hotplug`` indicates an externally connectable/visible |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 706 | port on the platform. Typically userspace would choose |
| 707 | to keep such a port powered to handle new device |
| 708 | connection events. |
| 709 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 710 | ``hardwired`` refers to a port that is not visible but |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 711 | connectable. Examples are internal ports for USB |
| 712 | bluetooth that can be disconnected via an external |
| 713 | switch or a port with a hardwired USB camera. It is |
| 714 | expected to be safe to allow these ports to suspend |
| 715 | provided pm_qos_no_power_off is coordinated with any |
| 716 | switch that gates connections. Userspace must arrange |
| 717 | for the device to be connected prior to the port |
| 718 | powering off, or to activate the port prior to enabling |
| 719 | connection via a switch. |
| 720 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 721 | ``not used`` refers to an internal port that is expected |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 722 | to never have a device connected to it. These may be |
| 723 | empty internal ports, or ports that are not physically |
| 724 | exposed on a platform. Considered safe to be |
| 725 | powered-off at all times. |
| 726 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 727 | ``unknown`` means platform firmware does not provide |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 728 | information for this port. Most commonly refers to |
| 729 | external hub ports which should be considered 'hotplug' |
| 730 | for policy decisions. |
| 731 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 732 | .. note:: |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 733 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 734 | - since we are relying on the BIOS to get this ACPI |
| 735 | information correct, the USB port descriptions may |
| 736 | be missing or wrong. |
| 737 | |
| 738 | - Take care in clearing ``pm_qos_no_power_off``. Once |
| 739 | power is off this port will |
| 740 | not respond to new connect events. |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 741 | |
| 742 | Once a child device is attached additional constraints are |
| 743 | applied before the port is allowed to poweroff. |
| 744 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 745 | ``<child>/power/control``: |
| 746 | Must be ``auto``, and the port will not |
| 747 | power down until ``<child>/power/runtime_status`` |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 748 | reflects the 'suspended' state. Default |
| 749 | value is controlled by child device driver. |
| 750 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 751 | ``<child>/power/persist``: |
| 752 | This defaults to ``1`` for most devices and indicates if |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 753 | kernel can persist the device's configuration across a |
| 754 | power session loss (suspend / port-power event). When |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 755 | this value is ``0`` (quirky devices), port poweroff is |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 756 | disabled. |
| 757 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 758 | ``<child>/driver/unbind``: |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 759 | Wakeup capable devices will block port poweroff. At |
| 760 | this time the only mechanism to clear the usb-internal |
| 761 | wakeup-capability for an interface device is to unbind |
| 762 | its driver. |
| 763 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 764 | Summary of poweroff pre-requisite settings relative to a port device:: |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 765 | |
| 766 | echo 0 > power/pm_qos_no_power_off |
| 767 | echo 0 > peer/power/pm_qos_no_power_off # if it exists |
| 768 | echo auto > power/control # this is the default value |
| 769 | echo auto > <child>/power/control |
| 770 | echo 1 > <child>/power/persist # this is the default value |
| 771 | |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 772 | Suggested Userspace Port Power Policy |
| 773 | ------------------------------------- |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 774 | |
| 775 | As noted above userspace needs to be careful and deliberate about what |
| 776 | ports are enabled for poweroff. |
| 777 | |
| 778 | The default configuration is that all ports start with |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 779 | ``power/pm_qos_no_power_off`` set to ``1`` causing ports to always remain |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 780 | active. |
| 781 | |
| 782 | Given confidence in the platform firmware's description of the ports |
| 783 | (ACPI _PLD record for a port populates 'connect_type') userspace can |
| 784 | clear pm_qos_no_power_off for all 'not used' ports. The same can be |
| 785 | done for 'hardwired' ports provided poweroff is coordinated with any |
| 786 | connection switch for the port. |
| 787 | |
| 788 | A more aggressive userspace policy is to enable USB port power off for |
Mauro Carvalho Chehab | 3db5f40 | 2017-04-05 10:23:05 -0300 | [diff] [blame] | 789 | all ports (set ``<hubdev-portX>/power/pm_qos_no_power_off`` to ``0``) when |
Lan Tianyu | f64c519 | 2014-05-29 12:58:52 -0700 | [diff] [blame] | 790 | some external factor indicates the user has stopped interacting with the |
| 791 | system. For example, a distro may want to enable power off all USB |
| 792 | ports when the screen blanks, and re-power them when the screen becomes |
| 793 | active. Smart phones and tablets may want to power off USB ports when |
| 794 | the user pushes the power button. |