power-management.txt 34 KB

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