power-management.txt 33 KB

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