power-management.rst 35 KB

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