power-management.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. Power Management for USB
  2. Alan Stern <stern@rowland.harvard.edu>
  3. October 28, 2010
  4. What is Power Management?
  5. -------------------------
  6. Power Management (PM) is the practice of saving energy by suspending
  7. parts of a computer system when they aren't being used. While a
  8. component is "suspended" it is in a nonfunctional low-power state; it
  9. might even be turned off completely. A suspended component can be
  10. "resumed" (returned to a functional full-power state) when the kernel
  11. needs to use it. (There also are forms of PM in which components are
  12. placed in a less functional but still usable state instead of being
  13. suspended; an example would be reducing the CPU's clock rate. This
  14. document will not discuss those other forms.)
  15. When the parts being suspended include the CPU and most of the rest of
  16. the system, we speak of it as a "system suspend". When a particular
  17. device is turned off while the system as a whole remains running, we
  18. call it a "dynamic suspend" (also known as a "runtime suspend" or
  19. "selective suspend"). This document concentrates mostly on how
  20. dynamic PM is implemented in the USB subsystem, although system PM is
  21. covered to some extent (see Documentation/power/*.txt for more
  22. information about system PM).
  23. Note: Dynamic PM support for USB is present only if the kernel was
  24. built with CONFIG_USB_SUSPEND enabled (which depends on
  25. CONFIG_PM_RUNTIME). System PM support is present only if the kernel
  26. was built with CONFIG_SUSPEND or CONFIG_HIBERNATION enabled.
  27. (Starting with the 3.10 kernel release, dynamic PM support for USB is
  28. present whenever the kernel was built with CONFIG_PM_RUNTIME enabled.
  29. The CONFIG_USB_SUSPEND option has been eliminated.)
  30. What is Remote Wakeup?
  31. ----------------------
  32. When a device has been suspended, it generally doesn't resume until
  33. the computer tells it to. Likewise, if the entire computer has been
  34. suspended, it generally doesn't resume until the user tells it to, say
  35. by pressing a power button or opening the cover.
  36. However some devices have the capability of resuming by themselves, or
  37. asking the kernel to resume them, or even telling the entire computer
  38. to resume. This capability goes by several names such as "Wake On
  39. LAN"; we will refer to it generically as "remote wakeup". When a
  40. device is enabled for remote wakeup and it is suspended, it may resume
  41. itself (or send a request to be resumed) in response to some external
  42. event. Examples include a suspended keyboard resuming when a key is
  43. pressed, or a suspended USB hub resuming when a device is plugged in.
  44. When is a USB device idle?
  45. --------------------------
  46. A device is idle whenever the kernel thinks it's not busy doing
  47. anything important and thus is a candidate for being suspended. The
  48. exact definition depends on the device's driver; drivers are allowed
  49. to declare that a device isn't idle even when there's no actual
  50. communication taking place. (For example, a hub isn't considered idle
  51. unless all the devices plugged into that hub are already suspended.)
  52. In addition, a device isn't considered idle so long as a program keeps
  53. its usbfs file open, whether or not any I/O is going on.
  54. If a USB device has no driver, its usbfs file isn't open, and it isn't
  55. being accessed through sysfs, then it definitely is idle.
  56. Forms of dynamic PM
  57. -------------------
  58. Dynamic suspends occur when the kernel decides to suspend an idle
  59. device. This is called "autosuspend" for short. In general, a device
  60. won't be autosuspended unless it has been idle for some minimum period
  61. of time, the so-called idle-delay time.
  62. Of course, nothing the kernel does on its own initiative should
  63. prevent the computer or its devices from working properly. If a
  64. device has been autosuspended and a program tries to use it, the
  65. kernel will automatically resume the device (autoresume). For the
  66. same reason, an autosuspended device will usually have remote wakeup
  67. enabled, if the device supports remote wakeup.
  68. It is worth mentioning that many USB drivers don't support
  69. autosuspend. In fact, at the time of this writing (Linux 2.6.23) the
  70. only drivers which do support it are the hub driver, kaweth, asix,
  71. usblp, usblcd, and usb-skeleton (which doesn't count). If a
  72. non-supporting driver is bound to a device, the device won't be
  73. autosuspended. In effect, the kernel pretends the device is never
  74. idle.
  75. We can categorize power management events in two broad classes:
  76. external and internal. External events are those triggered by some
  77. agent outside the USB stack: system suspend/resume (triggered by
  78. userspace), manual dynamic resume (also triggered by userspace), and
  79. remote wakeup (triggered by the device). Internal events are those
  80. triggered within the USB stack: autosuspend and autoresume. Note that
  81. all dynamic suspend events are internal; external agents are not
  82. allowed to issue dynamic suspends.
  83. The user interface for dynamic PM
  84. ---------------------------------
  85. The user interface for controlling dynamic PM is located in the power/
  86. subdirectory of each USB device's sysfs directory, that is, in
  87. /sys/bus/usb/devices/.../power/ where "..." is the device's ID. The
  88. relevant attribute files are: wakeup, control, and
  89. autosuspend_delay_ms. (There may also be a file named "level"; this
  90. file was deprecated as of the 2.6.35 kernel and replaced by the
  91. "control" file. In 2.6.38 the "autosuspend" file will be deprecated
  92. and replaced by the "autosuspend_delay_ms" file. The only difference
  93. is that the newer file expresses the delay in milliseconds whereas the
  94. older file uses seconds. Confusingly, both files are present in 2.6.37
  95. but only "autosuspend" works.)
  96. power/wakeup
  97. This file is empty if the device does not support
  98. remote wakeup. Otherwise the file contains either the
  99. word "enabled" or the word "disabled", and you can
  100. write those words to the file. The setting determines
  101. whether or not remote wakeup will be enabled when the
  102. device is next suspended. (If the setting is changed
  103. while the device is suspended, the change won't take
  104. effect until the following suspend.)
  105. power/control
  106. This file contains one of two words: "on" or "auto".
  107. You can write those words to the file to change the
  108. device's setting.
  109. "on" means that the device should be resumed and
  110. autosuspend is not allowed. (Of course, system
  111. suspends are still allowed.)
  112. "auto" is the normal state in which the kernel is
  113. allowed to autosuspend and autoresume the device.
  114. (In kernels up to 2.6.32, you could also specify
  115. "suspend", meaning that the device should remain
  116. suspended and autoresume was not allowed. This
  117. setting is no longer supported.)
  118. power/autosuspend_delay_ms
  119. This file contains an integer value, which is the
  120. number of milliseconds the device should remain idle
  121. before the kernel will autosuspend it (the idle-delay
  122. time). The default is 2000. 0 means to autosuspend
  123. as soon as the device becomes idle, and negative
  124. values mean never to autosuspend. You can write a
  125. number to the file to change the autosuspend
  126. idle-delay time.
  127. Writing "-1" to power/autosuspend_delay_ms and writing "on" to
  128. power/control do essentially the same thing -- they both prevent the
  129. device from being autosuspended. Yes, this is a redundancy in the
  130. API.
  131. (In 2.6.21 writing "0" to power/autosuspend would prevent the device
  132. from being autosuspended; the behavior was changed in 2.6.22. The
  133. power/autosuspend attribute did not exist prior to 2.6.21, and the
  134. power/level attribute did not exist prior to 2.6.22. power/control
  135. was added in 2.6.34, and power/autosuspend_delay_ms was added in
  136. 2.6.37 but did not become functional until 2.6.38.)
  137. Changing the default idle-delay time
  138. ------------------------------------
  139. The default autosuspend idle-delay time (in seconds) is controlled by
  140. a module parameter in usbcore. You can specify the value when usbcore
  141. is loaded. For example, to set it to 5 seconds instead of 2 you would
  142. do:
  143. modprobe usbcore autosuspend=5
  144. Equivalently, you could add to a configuration file in /etc/modprobe.d
  145. a line saying:
  146. options usbcore autosuspend=5
  147. Some distributions load the usbcore module very early during the boot
  148. process, by means of a program or script running from an initramfs
  149. image. To alter the parameter value you would have to rebuild that
  150. image.
  151. If usbcore is compiled into the kernel rather than built as a loadable
  152. module, you can add
  153. usbcore.autosuspend=5
  154. to the kernel's boot command line.
  155. Finally, the parameter value can be changed while the system is
  156. running. If you do:
  157. echo 5 >/sys/module/usbcore/parameters/autosuspend
  158. then each new USB device will have its autosuspend idle-delay
  159. initialized to 5. (The idle-delay values for already existing devices
  160. will not be affected.)
  161. Setting the initial default idle-delay to -1 will prevent any
  162. autosuspend of any USB device. This has the benefit of allowing you
  163. then to enable autosuspend for selected devices.
  164. Warnings
  165. --------
  166. The USB specification states that all USB devices must support power
  167. management. Nevertheless, the sad fact is that many devices do not
  168. support it very well. You can suspend them all right, but when you
  169. try to resume them they disconnect themselves from the USB bus or
  170. they stop working entirely. This seems to be especially prevalent
  171. among printers and scanners, but plenty of other types of device have
  172. the same deficiency.
  173. For this reason, by default the kernel disables autosuspend (the
  174. power/control attribute is initialized to "on") for all devices other
  175. than hubs. Hubs, at least, appear to be reasonably well-behaved in
  176. this regard.
  177. (In 2.6.21 and 2.6.22 this wasn't the case. Autosuspend was enabled
  178. by default for almost all USB devices. A number of people experienced
  179. problems as a result.)
  180. This means that non-hub devices won't be autosuspended unless the user
  181. or a program explicitly enables it. As of this writing there aren't
  182. any widespread programs which will do this; we hope that in the near
  183. future device managers such as HAL will take on this added
  184. responsibility. In the meantime you can always carry out the
  185. necessary operations by hand or add them to a udev script. You can
  186. also change the idle-delay time; 2 seconds is not the best choice for
  187. every device.
  188. If a driver knows that its device has proper suspend/resume support,
  189. it can enable autosuspend all by itself. For example, the video
  190. driver for a laptop's webcam might do this (in recent kernels they
  191. do), since these devices are rarely used and so should normally be
  192. autosuspended.
  193. Sometimes it turns out that even when a device does work okay with
  194. autosuspend there are still problems. For example, the usbhid driver,
  195. which manages keyboards and mice, has autosuspend support. Tests with
  196. a number of keyboards show that typing on a suspended keyboard, while
  197. causing the keyboard to do a remote wakeup all right, will nonetheless
  198. frequently result in lost keystrokes. Tests with mice show that some
  199. of them will issue a remote-wakeup request in response to button
  200. presses but not to motion, and some in response to neither.
  201. The kernel will not prevent you from enabling autosuspend on devices
  202. that can't handle it. It is even possible in theory to damage a
  203. device by suspending it at the wrong time. (Highly unlikely, but
  204. possible.) Take care.
  205. The driver interface for Power Management
  206. -----------------------------------------
  207. The requirements for a USB driver to support external power management
  208. are pretty modest; the driver need only define
  209. .suspend
  210. .resume
  211. .reset_resume
  212. methods in its usb_driver structure, and the reset_resume method is
  213. optional. The methods' jobs are quite simple:
  214. The suspend method is called to warn the driver that the
  215. device is going to be suspended. If the driver returns a
  216. negative error code, the suspend will be aborted. Normally
  217. the driver will return 0, in which case it must cancel all
  218. outstanding URBs (usb_kill_urb()) and not submit any more.
  219. The resume method is called to tell the driver that the
  220. device has been resumed and the driver can return to normal
  221. operation. URBs may once more be submitted.
  222. The reset_resume method is called to tell the driver that
  223. the device has been resumed and it also has been reset.
  224. The driver should redo any necessary device initialization,
  225. since the device has probably lost most or all of its state
  226. (although the interfaces will be in the same altsettings as
  227. before the suspend).
  228. If the device is disconnected or powered down while it is suspended,
  229. the disconnect method will be called instead of the resume or
  230. reset_resume method. This is also quite likely to happen when
  231. waking up from hibernation, as many systems do not maintain suspend
  232. current to the USB host controllers during hibernation. (It's
  233. possible to work around the hibernation-forces-disconnect problem by
  234. using the USB Persist facility.)
  235. The reset_resume method is used by the USB Persist facility (see
  236. Documentation/usb/persist.txt) and it can also be used under certain
  237. circumstances when CONFIG_USB_PERSIST is not enabled. Currently, if a
  238. device is reset during a resume and the driver does not have a
  239. reset_resume method, the driver won't receive any notification about
  240. the resume. Later kernels will call the driver's disconnect method;
  241. 2.6.23 doesn't do this.
  242. USB drivers are bound to interfaces, so their suspend and resume
  243. methods get called when the interfaces are suspended or resumed. In
  244. principle one might want to suspend some interfaces on a device (i.e.,
  245. force the drivers for those interface to stop all activity) without
  246. suspending the other interfaces. The USB core doesn't allow this; all
  247. interfaces are suspended when the device itself is suspended and all
  248. interfaces are resumed when the device is resumed. It isn't possible
  249. to suspend or resume some but not all of a device's interfaces. The
  250. closest you can come is to unbind the interfaces' drivers.
  251. The driver interface for autosuspend and autoresume
  252. ---------------------------------------------------
  253. To support autosuspend and autoresume, a driver should implement all
  254. three of the methods listed above. In addition, a driver indicates
  255. that it supports autosuspend by setting the .supports_autosuspend flag
  256. in its usb_driver structure. It is then responsible for informing the
  257. USB core whenever one of its interfaces becomes busy or idle. The
  258. driver does so by calling these six functions:
  259. int usb_autopm_get_interface(struct usb_interface *intf);
  260. void usb_autopm_put_interface(struct usb_interface *intf);
  261. int usb_autopm_get_interface_async(struct usb_interface *intf);
  262. void usb_autopm_put_interface_async(struct usb_interface *intf);
  263. void usb_autopm_get_interface_no_resume(struct usb_interface *intf);
  264. void usb_autopm_put_interface_no_suspend(struct usb_interface *intf);
  265. The functions work by maintaining a usage counter in the
  266. usb_interface's embedded device structure. When the counter is > 0
  267. then the interface is deemed to be busy, and the kernel will not
  268. autosuspend the interface's device. When the usage counter is = 0
  269. then the interface is considered to be idle, and the kernel may
  270. autosuspend the device.
  271. Drivers need not be concerned about balancing changes to the usage
  272. counter; the USB core will undo any remaining "get"s when a driver
  273. is unbound from its interface. As a corollary, drivers must not call
  274. any of the usb_autopm_* functions after their disconnect() routine has
  275. returned.
  276. Drivers using the async routines are responsible for their own
  277. synchronization and mutual exclusion.
  278. usb_autopm_get_interface() increments the usage counter and
  279. does an autoresume if the device is suspended. If the
  280. autoresume fails, the counter is decremented back.
  281. usb_autopm_put_interface() decrements the usage counter and
  282. attempts an autosuspend if the new value is = 0.
  283. usb_autopm_get_interface_async() and
  284. usb_autopm_put_interface_async() do almost the same things as
  285. their non-async counterparts. The big difference is that they
  286. use a workqueue to do the resume or suspend part of their
  287. jobs. As a result they can be called in an atomic context,
  288. such as an URB's completion handler, but when they return the
  289. device will generally not yet be in the desired state.
  290. usb_autopm_get_interface_no_resume() and
  291. usb_autopm_put_interface_no_suspend() merely increment or
  292. decrement the usage counter; they do not attempt to carry out
  293. an autoresume or an autosuspend. Hence they can be called in
  294. an atomic context.
  295. The simplest usage pattern is that a driver calls
  296. usb_autopm_get_interface() in its open routine and
  297. usb_autopm_put_interface() in its close or release routine. But other
  298. patterns are possible.
  299. The autosuspend attempts mentioned above will often fail for one
  300. reason or another. For example, the power/control attribute might be
  301. set to "on", or another interface in the same device might not be
  302. idle. This is perfectly normal. If the reason for failure was that
  303. the device hasn't been idle for long enough, a timer is scheduled to
  304. carry out the operation automatically when the autosuspend idle-delay
  305. has expired.
  306. Autoresume attempts also can fail, although failure would mean that
  307. the device is no longer present or operating properly. Unlike
  308. autosuspend, there's no idle-delay for an autoresume.
  309. Other parts of the driver interface
  310. -----------------------------------
  311. Drivers can enable autosuspend for their devices by calling
  312. usb_enable_autosuspend(struct usb_device *udev);
  313. in their probe() routine, if they know that the device is capable of
  314. suspending and resuming correctly. This is exactly equivalent to
  315. writing "auto" to the device's power/control attribute. Likewise,
  316. drivers can disable autosuspend by calling
  317. usb_disable_autosuspend(struct usb_device *udev);
  318. This is exactly the same as writing "on" to the power/control attribute.
  319. Sometimes a driver needs to make sure that remote wakeup is enabled
  320. during autosuspend. For example, there's not much point
  321. autosuspending a keyboard if the user can't cause the keyboard to do a
  322. remote wakeup by typing on it. If the driver sets
  323. intf->needs_remote_wakeup to 1, the kernel won't autosuspend the
  324. device if remote wakeup isn't available. (If the device is already
  325. autosuspended, though, setting this flag won't cause the kernel to
  326. autoresume it. Normally a driver would set this flag in its probe
  327. method, at which time the device is guaranteed not to be
  328. autosuspended.)
  329. If a driver does its I/O asynchronously in interrupt context, it
  330. should call usb_autopm_get_interface_async() before starting output and
  331. usb_autopm_put_interface_async() when the output queue drains. When
  332. it receives an input event, it should call
  333. usb_mark_last_busy(struct usb_device *udev);
  334. in the event handler. This tells the PM core that the device was just
  335. busy and therefore the next autosuspend idle-delay expiration should
  336. be pushed back. Many of the usb_autopm_* routines also make this call,
  337. so drivers need to worry only when interrupt-driven input arrives.
  338. Asynchronous operation is always subject to races. For example, a
  339. driver may call the usb_autopm_get_interface_async() routine at a time
  340. when the core has just finished deciding the device has been idle for
  341. long enough but not yet gotten around to calling the driver's suspend
  342. method. The suspend method must be responsible for synchronizing with
  343. the I/O request routine and the URB completion handler; it should
  344. cause autosuspends to fail with -EBUSY if the driver needs to use the
  345. device.
  346. External suspend calls should never be allowed to fail in this way,
  347. only autosuspend calls. The driver can tell them apart by applying
  348. the PMSG_IS_AUTO() macro to the message argument to the suspend
  349. method; it will return True for internal PM events (autosuspend) and
  350. False for external PM events.
  351. Mutual exclusion
  352. ----------------
  353. For external events -- but not necessarily for autosuspend or
  354. autoresume -- the device semaphore (udev->dev.sem) will be held when a
  355. suspend or resume method is called. This implies that external
  356. suspend/resume events are mutually exclusive with calls to probe,
  357. disconnect, pre_reset, and post_reset; the USB core guarantees that
  358. this is true of autosuspend/autoresume events as well.
  359. If a driver wants to block all suspend/resume calls during some
  360. critical section, the best way is to lock the device and call
  361. usb_autopm_get_interface() (and do the reverse at the end of the
  362. critical section). Holding the device semaphore will block all
  363. external PM calls, and the usb_autopm_get_interface() will prevent any
  364. internal PM calls, even if it fails. (Exercise: Why?)
  365. Interaction between dynamic PM and system PM
  366. --------------------------------------------
  367. Dynamic power management and system power management can interact in
  368. a couple of ways.
  369. Firstly, a device may already be autosuspended when a system suspend
  370. occurs. Since system suspends are supposed to be as transparent as
  371. possible, the device should remain suspended following the system
  372. resume. But this theory may not work out well in practice; over time
  373. the kernel's behavior in this regard has changed. As of 2.6.37 the
  374. policy is to resume all devices during a system resume and let them
  375. handle their own runtime suspends afterward.
  376. Secondly, a dynamic power-management event may occur as a system
  377. suspend is underway. The window for this is short, since system
  378. suspends don't take long (a few seconds usually), but it can happen.
  379. For example, a suspended device may send a remote-wakeup signal while
  380. the system is suspending. The remote wakeup may succeed, which would
  381. cause the system suspend to abort. If the remote wakeup doesn't
  382. succeed, it may still remain active and thus cause the system to
  383. resume as soon as the system suspend is complete. Or the remote
  384. wakeup may fail and get lost. Which outcome occurs depends on timing
  385. and on the hardware and firmware design.
  386. xHCI hardware link PM
  387. ---------------------
  388. xHCI host controller provides hardware link power management to usb2.0
  389. (xHCI 1.0 feature) and usb3.0 devices which support link PM. By
  390. enabling hardware LPM, the host can automatically put the device into
  391. lower power state(L1 for usb2.0 devices, or U1/U2 for usb3.0 devices),
  392. which state device can enter and resume very quickly.
  393. The user interface for controlling USB2 hardware LPM is located in the
  394. power/ subdirectory of each USB device's sysfs directory, that is, in
  395. /sys/bus/usb/devices/.../power/ where "..." is the device's ID. The
  396. relevant attribute files is usb2_hardware_lpm.
  397. power/usb2_hardware_lpm
  398. When a USB2 device which support LPM is plugged to a
  399. xHCI host root hub which support software LPM, the
  400. host will run a software LPM test for it; if the device
  401. enters L1 state and resume successfully and the host
  402. supports USB2 hardware LPM, this file will show up and
  403. driver will enable hardware LPM for the device. You
  404. can write y/Y/1 or n/N/0 to the file to enable/disable
  405. USB2 hardware LPM manually. This is for test purpose mainly.