0001 .. _usb-power-management:
0002
0003 Power Management for USB
0004 ~~~~~~~~~~~~~~~~~~~~~~~~
0005
0006 :Author: Alan Stern <stern@rowland.harvard.edu>
0007 :Date: Last-updated: February 2014
0008
0009 ..
0010 Contents:
0011 ---------
0012 * What is Power Management?
0013 * What is Remote Wakeup?
0014 * When is a USB device idle?
0015 * Forms of dynamic PM
0016 * The user interface for dynamic PM
0017 * Changing the default idle-delay time
0018 * Warnings
0019 * The driver interface for Power Management
0020 * The driver interface for autosuspend and autoresume
0021 * Other parts of the driver interface
0022 * Mutual exclusion
0023 * Interaction between dynamic PM and system PM
0024 * xHCI hardware link PM
0025 * USB Port Power Control
0026 * User Interface for Port Power Control
0027 * Suggested Userspace Port Power Policy
0028
0029
0030 What is Power Management?
0031 -------------------------
0032
0033 Power Management (PM) is the practice of saving energy by suspending
0034 parts of a computer system when they aren't being used. While a
0035 component is ``suspended`` it is in a nonfunctional low-power state; it
0036 might even be turned off completely. A suspended component can be
0037 ``resumed`` (returned to a functional full-power state) when the kernel
0038 needs to use it. (There also are forms of PM in which components are
0039 placed in a less functional but still usable state instead of being
0040 suspended; an example would be reducing the CPU's clock rate. This
0041 document will not discuss those other forms.)
0042
0043 When the parts being suspended include the CPU and most of the rest of
0044 the system, we speak of it as a "system suspend". When a particular
0045 device is turned off while the system as a whole remains running, we
0046 call it a "dynamic suspend" (also known as a "runtime suspend" or
0047 "selective suspend"). This document concentrates mostly on how
0048 dynamic PM is implemented in the USB subsystem, although system PM is
0049 covered to some extent (see ``Documentation/power/*.rst`` for more
0050 information about system PM).
0051
0052 System PM support is present only if the kernel was built with
0053 ``CONFIG_SUSPEND`` or ``CONFIG_HIBERNATION`` enabled. Dynamic PM support
0054
0055 for USB is present whenever
0056 the kernel was built with ``CONFIG_PM`` enabled.
0057
0058 [Historically, dynamic PM support for USB was present only if the
0059 kernel had been built with ``CONFIG_USB_SUSPEND`` enabled (which depended on
0060 ``CONFIG_PM_RUNTIME``). Starting with the 3.10 kernel release, dynamic PM
0061 support for USB was present whenever the kernel was built with
0062 ``CONFIG_PM_RUNTIME`` enabled. The ``CONFIG_USB_SUSPEND`` option had been
0063 eliminated.]
0064
0065
0066 What is Remote Wakeup?
0067 ----------------------
0068
0069 When a device has been suspended, it generally doesn't resume until
0070 the computer tells it to. Likewise, if the entire computer has been
0071 suspended, it generally doesn't resume until the user tells it to, say
0072 by pressing a power button or opening the cover.
0073
0074 However some devices have the capability of resuming by themselves, or
0075 asking the kernel to resume them, or even telling the entire computer
0076 to resume. This capability goes by several names such as "Wake On
0077 LAN"; we will refer to it generically as "remote wakeup". When a
0078 device is enabled for remote wakeup and it is suspended, it may resume
0079 itself (or send a request to be resumed) in response to some external
0080 event. Examples include a suspended keyboard resuming when a key is
0081 pressed, or a suspended USB hub resuming when a device is plugged in.
0082
0083
0084 When is a USB device idle?
0085 --------------------------
0086
0087 A device is idle whenever the kernel thinks it's not busy doing
0088 anything important and thus is a candidate for being suspended. The
0089 exact definition depends on the device's driver; drivers are allowed
0090 to declare that a device isn't idle even when there's no actual
0091 communication taking place. (For example, a hub isn't considered idle
0092 unless all the devices plugged into that hub are already suspended.)
0093 In addition, a device isn't considered idle so long as a program keeps
0094 its usbfs file open, whether or not any I/O is going on.
0095
0096 If a USB device has no driver, its usbfs file isn't open, and it isn't
0097 being accessed through sysfs, then it definitely is idle.
0098
0099
0100 Forms of dynamic PM
0101 -------------------
0102
0103 Dynamic suspends occur when the kernel decides to suspend an idle
0104 device. This is called ``autosuspend`` for short. In general, a device
0105 won't be autosuspended unless it has been idle for some minimum period
0106 of time, the so-called idle-delay time.
0107
0108 Of course, nothing the kernel does on its own initiative should
0109 prevent the computer or its devices from working properly. If a
0110 device has been autosuspended and a program tries to use it, the
0111 kernel will automatically resume the device (autoresume). For the
0112 same reason, an autosuspended device will usually have remote wakeup
0113 enabled, if the device supports remote wakeup.
0114
0115 It is worth mentioning that many USB drivers don't support
0116 autosuspend. In fact, at the time of this writing (Linux 2.6.23) the
0117 only drivers which do support it are the hub driver, kaweth, asix,
0118 usblp, usblcd, and usb-skeleton (which doesn't count). If a
0119 non-supporting driver is bound to a device, the device won't be
0120 autosuspended. In effect, the kernel pretends the device is never
0121 idle.
0122
0123 We can categorize power management events in two broad classes:
0124 external and internal. External events are those triggered by some
0125 agent outside the USB stack: system suspend/resume (triggered by
0126 userspace), manual dynamic resume (also triggered by userspace), and
0127 remote wakeup (triggered by the device). Internal events are those
0128 triggered within the USB stack: autosuspend and autoresume. Note that
0129 all dynamic suspend events are internal; external agents are not
0130 allowed to issue dynamic suspends.
0131
0132
0133 The user interface for dynamic PM
0134 ---------------------------------
0135
0136 The user interface for controlling dynamic PM is located in the ``power/``
0137 subdirectory of each USB device's sysfs directory, that is, in
0138 ``/sys/bus/usb/devices/.../power/`` where "..." is the device's ID. The
0139 relevant attribute files are: wakeup, control, and
0140 ``autosuspend_delay_ms``. (There may also be a file named ``level``; this
0141 file was deprecated as of the 2.6.35 kernel and replaced by the
0142 ``control`` file. In 2.6.38 the ``autosuspend`` file will be deprecated
0143 and replaced by the ``autosuspend_delay_ms`` file. The only difference
0144 is that the newer file expresses the delay in milliseconds whereas the
0145 older file uses seconds. Confusingly, both files are present in 2.6.37
0146 but only ``autosuspend`` works.)
0147
0148 ``power/wakeup``
0149
0150 This file is empty if the device does not support
0151 remote wakeup. Otherwise the file contains either the
0152 word ``enabled`` or the word ``disabled``, and you can
0153 write those words to the file. The setting determines
0154 whether or not remote wakeup will be enabled when the
0155 device is next suspended. (If the setting is changed
0156 while the device is suspended, the change won't take
0157 effect until the following suspend.)
0158
0159 ``power/control``
0160
0161 This file contains one of two words: ``on`` or ``auto``.
0162 You can write those words to the file to change the
0163 device's setting.
0164
0165 - ``on`` means that the device should be resumed and
0166 autosuspend is not allowed. (Of course, system
0167 suspends are still allowed.)
0168
0169 - ``auto`` is the normal state in which the kernel is
0170 allowed to autosuspend and autoresume the device.
0171
0172 (In kernels up to 2.6.32, you could also specify
0173 ``suspend``, meaning that the device should remain
0174 suspended and autoresume was not allowed. This
0175 setting is no longer supported.)
0176
0177 ``power/autosuspend_delay_ms``
0178
0179 This file contains an integer value, which is the
0180 number of milliseconds the device should remain idle
0181 before the kernel will autosuspend it (the idle-delay
0182 time). The default is 2000. 0 means to autosuspend
0183 as soon as the device becomes idle, and negative
0184 values mean never to autosuspend. You can write a
0185 number to the file to change the autosuspend
0186 idle-delay time.
0187
0188 Writing ``-1`` to ``power/autosuspend_delay_ms`` and writing ``on`` to
0189 ``power/control`` do essentially the same thing -- they both prevent the
0190 device from being autosuspended. Yes, this is a redundancy in the
0191 API.
0192
0193 (In 2.6.21 writing ``0`` to ``power/autosuspend`` would prevent the device
0194 from being autosuspended; the behavior was changed in 2.6.22. The
0195 ``power/autosuspend`` attribute did not exist prior to 2.6.21, and the
0196 ``power/level`` attribute did not exist prior to 2.6.22. ``power/control``
0197 was added in 2.6.34, and ``power/autosuspend_delay_ms`` was added in
0198 2.6.37 but did not become functional until 2.6.38.)
0199
0200
0201 Changing the default idle-delay time
0202 ------------------------------------
0203
0204 The default autosuspend idle-delay time (in seconds) is controlled by
0205 a module parameter in usbcore. You can specify the value when usbcore
0206 is loaded. For example, to set it to 5 seconds instead of 2 you would
0207 do::
0208
0209 modprobe usbcore autosuspend=5
0210
0211 Equivalently, you could add to a configuration file in /etc/modprobe.d
0212 a line saying::
0213
0214 options usbcore autosuspend=5
0215
0216 Some distributions load the usbcore module very early during the boot
0217 process, by means of a program or script running from an initramfs
0218 image. To alter the parameter value you would have to rebuild that
0219 image.
0220
0221 If usbcore is compiled into the kernel rather than built as a loadable
0222 module, you can add::
0223
0224 usbcore.autosuspend=5
0225
0226 to the kernel's boot command line.
0227
0228 Finally, the parameter value can be changed while the system is
0229 running. If you do::
0230
0231 echo 5 >/sys/module/usbcore/parameters/autosuspend
0232
0233 then each new USB device will have its autosuspend idle-delay
0234 initialized to 5. (The idle-delay values for already existing devices
0235 will not be affected.)
0236
0237 Setting the initial default idle-delay to -1 will prevent any
0238 autosuspend of any USB device. This has the benefit of allowing you
0239 then to enable autosuspend for selected devices.
0240
0241
0242 Warnings
0243 --------
0244
0245 The USB specification states that all USB devices must support power
0246 management. Nevertheless, the sad fact is that many devices do not
0247 support it very well. You can suspend them all right, but when you
0248 try to resume them they disconnect themselves from the USB bus or
0249 they stop working entirely. This seems to be especially prevalent
0250 among printers and scanners, but plenty of other types of device have
0251 the same deficiency.
0252
0253 For this reason, by default the kernel disables autosuspend (the
0254 ``power/control`` attribute is initialized to ``on``) for all devices other
0255 than hubs. Hubs, at least, appear to be reasonably well-behaved in
0256 this regard.
0257
0258 (In 2.6.21 and 2.6.22 this wasn't the case. Autosuspend was enabled
0259 by default for almost all USB devices. A number of people experienced
0260 problems as a result.)
0261
0262 This means that non-hub devices won't be autosuspended unless the user
0263 or a program explicitly enables it. As of this writing there aren't
0264 any widespread programs which will do this; we hope that in the near
0265 future device managers such as HAL will take on this added
0266 responsibility. In the meantime you can always carry out the
0267 necessary operations by hand or add them to a udev script. You can
0268 also change the idle-delay time; 2 seconds is not the best choice for
0269 every device.
0270
0271 If a driver knows that its device has proper suspend/resume support,
0272 it can enable autosuspend all by itself. For example, the video
0273 driver for a laptop's webcam might do this (in recent kernels they
0274 do), since these devices are rarely used and so should normally be
0275 autosuspended.
0276
0277 Sometimes it turns out that even when a device does work okay with
0278 autosuspend there are still problems. For example, the usbhid driver,
0279 which manages keyboards and mice, has autosuspend support. Tests with
0280 a number of keyboards show that typing on a suspended keyboard, while
0281 causing the keyboard to do a remote wakeup all right, will nonetheless
0282 frequently result in lost keystrokes. Tests with mice show that some
0283 of them will issue a remote-wakeup request in response to button
0284 presses but not to motion, and some in response to neither.
0285
0286 The kernel will not prevent you from enabling autosuspend on devices
0287 that can't handle it. It is even possible in theory to damage a
0288 device by suspending it at the wrong time. (Highly unlikely, but
0289 possible.) Take care.
0290
0291
0292 The driver interface for Power Management
0293 -----------------------------------------
0294
0295 The requirements for a USB driver to support external power management
0296 are pretty modest; the driver need only define::
0297
0298 .suspend
0299 .resume
0300 .reset_resume
0301
0302 methods in its :c:type:`usb_driver` structure, and the ``reset_resume`` method
0303 is optional. The methods' jobs are quite simple:
0304
0305 - The ``suspend`` method is called to warn the driver that the
0306 device is going to be suspended. If the driver returns a
0307 negative error code, the suspend will be aborted. Normally
0308 the driver will return 0, in which case it must cancel all
0309 outstanding URBs (:c:func:`usb_kill_urb`) and not submit any more.
0310
0311 - The ``resume`` method is called to tell the driver that the
0312 device has been resumed and the driver can return to normal
0313 operation. URBs may once more be submitted.
0314
0315 - The ``reset_resume`` method is called to tell the driver that
0316 the device has been resumed and it also has been reset.
0317 The driver should redo any necessary device initialization,
0318 since the device has probably lost most or all of its state
0319 (although the interfaces will be in the same altsettings as
0320 before the suspend).
0321
0322 If the device is disconnected or powered down while it is suspended,
0323 the ``disconnect`` method will be called instead of the ``resume`` or
0324 ``reset_resume`` method. This is also quite likely to happen when
0325 waking up from hibernation, as many systems do not maintain suspend
0326 current to the USB host controllers during hibernation. (It's
0327 possible to work around the hibernation-forces-disconnect problem by
0328 using the USB Persist facility.)
0329
0330 The ``reset_resume`` method is used by the USB Persist facility (see
0331 :ref:`usb-persist`) and it can also be used under certain
0332 circumstances when ``CONFIG_USB_PERSIST`` is not enabled. Currently, if a
0333 device is reset during a resume and the driver does not have a
0334 ``reset_resume`` method, the driver won't receive any notification about
0335 the resume. Later kernels will call the driver's ``disconnect`` method;
0336 2.6.23 doesn't do this.
0337
0338 USB drivers are bound to interfaces, so their ``suspend`` and ``resume``
0339 methods get called when the interfaces are suspended or resumed. In
0340 principle one might want to suspend some interfaces on a device (i.e.,
0341 force the drivers for those interface to stop all activity) without
0342 suspending the other interfaces. The USB core doesn't allow this; all
0343 interfaces are suspended when the device itself is suspended and all
0344 interfaces are resumed when the device is resumed. It isn't possible
0345 to suspend or resume some but not all of a device's interfaces. The
0346 closest you can come is to unbind the interfaces' drivers.
0347
0348
0349 The driver interface for autosuspend and autoresume
0350 ---------------------------------------------------
0351
0352 To support autosuspend and autoresume, a driver should implement all
0353 three of the methods listed above. In addition, a driver indicates
0354 that it supports autosuspend by setting the ``.supports_autosuspend`` flag
0355 in its usb_driver structure. It is then responsible for informing the
0356 USB core whenever one of its interfaces becomes busy or idle. The
0357 driver does so by calling these six functions::
0358
0359 int usb_autopm_get_interface(struct usb_interface *intf);
0360 void usb_autopm_put_interface(struct usb_interface *intf);
0361 int usb_autopm_get_interface_async(struct usb_interface *intf);
0362 void usb_autopm_put_interface_async(struct usb_interface *intf);
0363 void usb_autopm_get_interface_no_resume(struct usb_interface *intf);
0364 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf);
0365
0366 The functions work by maintaining a usage counter in the
0367 usb_interface's embedded device structure. When the counter is > 0
0368 then the interface is deemed to be busy, and the kernel will not
0369 autosuspend the interface's device. When the usage counter is = 0
0370 then the interface is considered to be idle, and the kernel may
0371 autosuspend the device.
0372
0373 Drivers must be careful to balance their overall changes to the usage
0374 counter. Unbalanced "get"s will remain in effect when a driver is
0375 unbound from its interface, preventing the device from going into
0376 runtime suspend should the interface be bound to a driver again. On
0377 the other hand, drivers are allowed to achieve this balance by calling
0378 the ``usb_autopm_*`` functions even after their ``disconnect`` routine
0379 has returned -- say from within a work-queue routine -- provided they
0380 retain an active reference to the interface (via ``usb_get_intf`` and
0381 ``usb_put_intf``).
0382
0383 Drivers using the async routines are responsible for their own
0384 synchronization and mutual exclusion.
0385
0386 :c:func:`usb_autopm_get_interface` increments the usage counter and
0387 does an autoresume if the device is suspended. If the
0388 autoresume fails, the counter is decremented back.
0389
0390 :c:func:`usb_autopm_put_interface` decrements the usage counter and
0391 attempts an autosuspend if the new value is = 0.
0392
0393 :c:func:`usb_autopm_get_interface_async` and
0394 :c:func:`usb_autopm_put_interface_async` do almost the same things as
0395 their non-async counterparts. The big difference is that they
0396 use a workqueue to do the resume or suspend part of their
0397 jobs. As a result they can be called in an atomic context,
0398 such as an URB's completion handler, but when they return the
0399 device will generally not yet be in the desired state.
0400
0401 :c:func:`usb_autopm_get_interface_no_resume` and
0402 :c:func:`usb_autopm_put_interface_no_suspend` merely increment or
0403 decrement the usage counter; they do not attempt to carry out
0404 an autoresume or an autosuspend. Hence they can be called in
0405 an atomic context.
0406
0407 The simplest usage pattern is that a driver calls
0408 :c:func:`usb_autopm_get_interface` in its open routine and
0409 :c:func:`usb_autopm_put_interface` in its close or release routine. But other
0410 patterns are possible.
0411
0412 The autosuspend attempts mentioned above will often fail for one
0413 reason or another. For example, the ``power/control`` attribute might be
0414 set to ``on``, or another interface in the same device might not be
0415 idle. This is perfectly normal. If the reason for failure was that
0416 the device hasn't been idle for long enough, a timer is scheduled to
0417 carry out the operation automatically when the autosuspend idle-delay
0418 has expired.
0419
0420 Autoresume attempts also can fail, although failure would mean that
0421 the device is no longer present or operating properly. Unlike
0422 autosuspend, there's no idle-delay for an autoresume.
0423
0424
0425 Other parts of the driver interface
0426 -----------------------------------
0427
0428 Drivers can enable autosuspend for their devices by calling::
0429
0430 usb_enable_autosuspend(struct usb_device *udev);
0431
0432 in their :c:func:`probe` routine, if they know that the device is capable of
0433 suspending and resuming correctly. This is exactly equivalent to
0434 writing ``auto`` to the device's ``power/control`` attribute. Likewise,
0435 drivers can disable autosuspend by calling::
0436
0437 usb_disable_autosuspend(struct usb_device *udev);
0438
0439 This is exactly the same as writing ``on`` to the ``power/control`` attribute.
0440
0441 Sometimes a driver needs to make sure that remote wakeup is enabled
0442 during autosuspend. For example, there's not much point
0443 autosuspending a keyboard if the user can't cause the keyboard to do a
0444 remote wakeup by typing on it. If the driver sets
0445 ``intf->needs_remote_wakeup`` to 1, the kernel won't autosuspend the
0446 device if remote wakeup isn't available. (If the device is already
0447 autosuspended, though, setting this flag won't cause the kernel to
0448 autoresume it. Normally a driver would set this flag in its ``probe``
0449 method, at which time the device is guaranteed not to be
0450 autosuspended.)
0451
0452 If a driver does its I/O asynchronously in interrupt context, it
0453 should call :c:func:`usb_autopm_get_interface_async` before starting output and
0454 :c:func:`usb_autopm_put_interface_async` when the output queue drains. When
0455 it receives an input event, it should call::
0456
0457 usb_mark_last_busy(struct usb_device *udev);
0458
0459 in the event handler. This tells the PM core that the device was just
0460 busy and therefore the next autosuspend idle-delay expiration should
0461 be pushed back. Many of the usb_autopm_* routines also make this call,
0462 so drivers need to worry only when interrupt-driven input arrives.
0463
0464 Asynchronous operation is always subject to races. For example, a
0465 driver may call the :c:func:`usb_autopm_get_interface_async` routine at a time
0466 when the core has just finished deciding the device has been idle for
0467 long enough but not yet gotten around to calling the driver's ``suspend``
0468 method. The ``suspend`` method must be responsible for synchronizing with
0469 the I/O request routine and the URB completion handler; it should
0470 cause autosuspends to fail with -EBUSY if the driver needs to use the
0471 device.
0472
0473 External suspend calls should never be allowed to fail in this way,
0474 only autosuspend calls. The driver can tell them apart by applying
0475 the :c:func:`PMSG_IS_AUTO` macro to the message argument to the ``suspend``
0476 method; it will return True for internal PM events (autosuspend) and
0477 False for external PM events.
0478
0479
0480 Mutual exclusion
0481 ----------------
0482
0483 For external events -- but not necessarily for autosuspend or
0484 autoresume -- the device semaphore (udev->dev.sem) will be held when a
0485 ``suspend`` or ``resume`` method is called. This implies that external
0486 suspend/resume events are mutually exclusive with calls to ``probe``,
0487 ``disconnect``, ``pre_reset``, and ``post_reset``; the USB core guarantees that
0488 this is true of autosuspend/autoresume events as well.
0489
0490 If a driver wants to block all suspend/resume calls during some
0491 critical section, the best way is to lock the device and call
0492 :c:func:`usb_autopm_get_interface` (and do the reverse at the end of the
0493 critical section). Holding the device semaphore will block all
0494 external PM calls, and the :c:func:`usb_autopm_get_interface` will prevent any
0495 internal PM calls, even if it fails. (Exercise: Why?)
0496
0497
0498 Interaction between dynamic PM and system PM
0499 --------------------------------------------
0500
0501 Dynamic power management and system power management can interact in
0502 a couple of ways.
0503
0504 Firstly, a device may already be autosuspended when a system suspend
0505 occurs. Since system suspends are supposed to be as transparent as
0506 possible, the device should remain suspended following the system
0507 resume. But this theory may not work out well in practice; over time
0508 the kernel's behavior in this regard has changed. As of 2.6.37 the
0509 policy is to resume all devices during a system resume and let them
0510 handle their own runtime suspends afterward.
0511
0512 Secondly, a dynamic power-management event may occur as a system
0513 suspend is underway. The window for this is short, since system
0514 suspends don't take long (a few seconds usually), but it can happen.
0515 For example, a suspended device may send a remote-wakeup signal while
0516 the system is suspending. The remote wakeup may succeed, which would
0517 cause the system suspend to abort. If the remote wakeup doesn't
0518 succeed, it may still remain active and thus cause the system to
0519 resume as soon as the system suspend is complete. Or the remote
0520 wakeup may fail and get lost. Which outcome occurs depends on timing
0521 and on the hardware and firmware design.
0522
0523
0524 xHCI hardware link PM
0525 ---------------------
0526
0527 xHCI host controller provides hardware link power management to usb2.0
0528 (xHCI 1.0 feature) and usb3.0 devices which support link PM. By
0529 enabling hardware LPM, the host can automatically put the device into
0530 lower power state(L1 for usb2.0 devices, or U1/U2 for usb3.0 devices),
0531 which state device can enter and resume very quickly.
0532
0533 The user interface for controlling hardware LPM is located in the
0534 ``power/`` subdirectory of each USB device's sysfs directory, that is, in
0535 ``/sys/bus/usb/devices/.../power/`` where "..." is the device's ID. The
0536 relevant attribute files are ``usb2_hardware_lpm`` and ``usb3_hardware_lpm``.
0537
0538 ``power/usb2_hardware_lpm``
0539
0540 When a USB2 device which support LPM is plugged to a
0541 xHCI host root hub which support software LPM, the
0542 host will run a software LPM test for it; if the device
0543 enters L1 state and resume successfully and the host
0544 supports USB2 hardware LPM, this file will show up and
0545 driver will enable hardware LPM for the device. You
0546 can write y/Y/1 or n/N/0 to the file to enable/disable
0547 USB2 hardware LPM manually. This is for test purpose mainly.
0548
0549 ``power/usb3_hardware_lpm_u1``
0550 ``power/usb3_hardware_lpm_u2``
0551
0552 When a USB 3.0 lpm-capable device is plugged in to a
0553 xHCI host which supports link PM, it will check if U1
0554 and U2 exit latencies have been set in the BOS
0555 descriptor; if the check is passed and the host
0556 supports USB3 hardware LPM, USB3 hardware LPM will be
0557 enabled for the device and these files will be created.
0558 The files hold a string value (enable or disable)
0559 indicating whether or not USB3 hardware LPM U1 or U2
0560 is enabled for the device.
0561
0562 USB Port Power Control
0563 ----------------------
0564
0565 In addition to suspending endpoint devices and enabling hardware
0566 controlled link power management, the USB subsystem also has the
0567 capability to disable power to ports under some conditions. Power is
0568 controlled through ``Set/ClearPortFeature(PORT_POWER)`` requests to a hub.
0569 In the case of a root or platform-internal hub the host controller
0570 driver translates ``PORT_POWER`` requests into platform firmware (ACPI)
0571 method calls to set the port power state. For more background see the
0572 Linux Plumbers Conference 2012 slides [#f1]_ and video [#f2]_:
0573
0574 Upon receiving a ``ClearPortFeature(PORT_POWER)`` request a USB port is
0575 logically off, and may trigger the actual loss of VBUS to the port [#f3]_.
0576 VBUS may be maintained in the case where a hub gangs multiple ports into
0577 a shared power well causing power to remain until all ports in the gang
0578 are turned off. VBUS may also be maintained by hub ports configured for
0579 a charging application. In any event a logically off port will lose
0580 connection with its device, not respond to hotplug events, and not
0581 respond to remote wakeup events.
0582
0583 .. warning::
0584
0585 turning off a port may result in the inability to hot add a device.
0586 Please see "User Interface for Port Power Control" for details.
0587
0588 As far as the effect on the device itself it is similar to what a device
0589 goes through during system suspend, i.e. the power session is lost. Any
0590 USB device or driver that misbehaves with system suspend will be
0591 similarly affected by a port power cycle event. For this reason the
0592 implementation shares the same device recovery path (and honors the same
0593 quirks) as the system resume path for the hub.
0594
0595 .. [#f1]
0596
0597 http://dl.dropbox.com/u/96820575/sarah-sharp-lpt-port-power-off2-mini.pdf
0598
0599 .. [#f2]
0600
0601 http://linuxplumbers.ubicast.tv/videos/usb-port-power-off-kerneluserspace-api/
0602
0603 .. [#f3]
0604
0605 USB 3.1 Section 10.12
0606
0607 wakeup note: if a device is configured to send wakeup events the port
0608 power control implementation will block poweroff attempts on that
0609 port.
0610
0611
0612 User Interface for Port Power Control
0613 -------------------------------------
0614
0615 The port power control mechanism uses the PM runtime system. Poweroff is
0616 requested by clearing the ``power/pm_qos_no_power_off`` flag of the port device
0617 (defaults to 1). If the port is disconnected it will immediately receive a
0618 ``ClearPortFeature(PORT_POWER)`` request. Otherwise, it will honor the pm
0619 runtime rules and require the attached child device and all descendants to be
0620 suspended. This mechanism is dependent on the hub advertising port power
0621 switching in its hub descriptor (wHubCharacteristics logical power switching
0622 mode field).
0623
0624 Note, some interface devices/drivers do not support autosuspend. Userspace may
0625 need to unbind the interface drivers before the :c:type:`usb_device` will
0626 suspend. An unbound interface device is suspended by default. When unbinding,
0627 be careful to unbind interface drivers, not the driver of the parent usb
0628 device. Also, leave hub interface drivers bound. If the driver for the usb
0629 device (not interface) is unbound the kernel is no longer able to resume the
0630 device. If a hub interface driver is unbound, control of its child ports is
0631 lost and all attached child-devices will disconnect. A good rule of thumb is
0632 that if the 'driver/module' link for a device points to
0633 ``/sys/module/usbcore`` then unbinding it will interfere with port power
0634 control.
0635
0636 Example of the relevant files for port power control. Note, in this example
0637 these files are relative to a usb hub device (prefix)::
0638
0639 prefix=/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1
0640
0641 attached child device +
0642 hub port device + |
0643 hub interface device + | |
0644 v v v
0645 $prefix/3-1:1.0/3-1-port1/device
0646
0647 $prefix/3-1:1.0/3-1-port1/power/pm_qos_no_power_off
0648 $prefix/3-1:1.0/3-1-port1/device/power/control
0649 $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intf0>/driver/unbind
0650 $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intf1>/driver/unbind
0651 ...
0652 $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intfN>/driver/unbind
0653
0654 In addition to these files some ports may have a 'peer' link to a port on
0655 another hub. The expectation is that all superspeed ports have a
0656 hi-speed peer::
0657
0658 $prefix/3-1:1.0/3-1-port1/peer -> ../../../../usb2/2-1/2-1:1.0/2-1-port1
0659 ../../../../usb2/2-1/2-1:1.0/2-1-port1/peer -> ../../../../usb3/3-1/3-1:1.0/3-1-port1
0660
0661 Distinct from 'companion ports', or 'ehci/xhci shared switchover ports'
0662 peer ports are simply the hi-speed and superspeed interface pins that
0663 are combined into a single usb3 connector. Peer ports share the same
0664 ancestor XHCI device.
0665
0666 While a superspeed port is powered off a device may downgrade its
0667 connection and attempt to connect to the hi-speed pins. The
0668 implementation takes steps to prevent this:
0669
0670 1. Port suspend is sequenced to guarantee that hi-speed ports are powered-off
0671 before their superspeed peer is permitted to power-off. The implication is
0672 that the setting ``pm_qos_no_power_off`` to zero on a superspeed port may
0673 not cause the port to power-off until its highspeed peer has gone to its
0674 runtime suspend state. Userspace must take care to order the suspensions
0675 if it wants to guarantee that a superspeed port will power-off.
0676
0677 2. Port resume is sequenced to force a superspeed port to power-on prior to its
0678 highspeed peer.
0679
0680 3. Port resume always triggers an attached child device to resume. After a
0681 power session is lost the device may have been removed, or need reset.
0682 Resuming the child device when the parent port regains power resolves those
0683 states and clamps the maximum port power cycle frequency at the rate the
0684 child device can suspend (autosuspend-delay) and resume (reset-resume
0685 latency).
0686
0687 Sysfs files relevant for port power control:
0688
0689 ``<hubdev-portX>/power/pm_qos_no_power_off``:
0690 This writable flag controls the state of an idle port.
0691 Once all children and descendants have suspended the
0692 port may suspend/poweroff provided that
0693 pm_qos_no_power_off is '0'. If pm_qos_no_power_off is
0694 '1' the port will remain active/powered regardless of
0695 the stats of descendants. Defaults to 1.
0696
0697 ``<hubdev-portX>/power/runtime_status``:
0698 This file reflects whether the port is 'active' (power is on)
0699 or 'suspended' (logically off). There is no indication to
0700 userspace whether VBUS is still supplied.
0701
0702 ``<hubdev-portX>/connect_type``:
0703 An advisory read-only flag to userspace indicating the
0704 location and connection type of the port. It returns
0705 one of four values 'hotplug', 'hardwired', 'not used',
0706 and 'unknown'. All values, besides unknown, are set by
0707 platform firmware.
0708
0709 ``hotplug`` indicates an externally connectable/visible
0710 port on the platform. Typically userspace would choose
0711 to keep such a port powered to handle new device
0712 connection events.
0713
0714 ``hardwired`` refers to a port that is not visible but
0715 connectable. Examples are internal ports for USB
0716 bluetooth that can be disconnected via an external
0717 switch or a port with a hardwired USB camera. It is
0718 expected to be safe to allow these ports to suspend
0719 provided pm_qos_no_power_off is coordinated with any
0720 switch that gates connections. Userspace must arrange
0721 for the device to be connected prior to the port
0722 powering off, or to activate the port prior to enabling
0723 connection via a switch.
0724
0725 ``not used`` refers to an internal port that is expected
0726 to never have a device connected to it. These may be
0727 empty internal ports, or ports that are not physically
0728 exposed on a platform. Considered safe to be
0729 powered-off at all times.
0730
0731 ``unknown`` means platform firmware does not provide
0732 information for this port. Most commonly refers to
0733 external hub ports which should be considered 'hotplug'
0734 for policy decisions.
0735
0736 .. note::
0737
0738 - since we are relying on the BIOS to get this ACPI
0739 information correct, the USB port descriptions may
0740 be missing or wrong.
0741
0742 - Take care in clearing ``pm_qos_no_power_off``. Once
0743 power is off this port will
0744 not respond to new connect events.
0745
0746 Once a child device is attached additional constraints are
0747 applied before the port is allowed to poweroff.
0748
0749 ``<child>/power/control``:
0750 Must be ``auto``, and the port will not
0751 power down until ``<child>/power/runtime_status``
0752 reflects the 'suspended' state. Default
0753 value is controlled by child device driver.
0754
0755 ``<child>/power/persist``:
0756 This defaults to ``1`` for most devices and indicates if
0757 kernel can persist the device's configuration across a
0758 power session loss (suspend / port-power event). When
0759 this value is ``0`` (quirky devices), port poweroff is
0760 disabled.
0761
0762 ``<child>/driver/unbind``:
0763 Wakeup capable devices will block port poweroff. At
0764 this time the only mechanism to clear the usb-internal
0765 wakeup-capability for an interface device is to unbind
0766 its driver.
0767
0768 Summary of poweroff pre-requisite settings relative to a port device::
0769
0770 echo 0 > power/pm_qos_no_power_off
0771 echo 0 > peer/power/pm_qos_no_power_off # if it exists
0772 echo auto > power/control # this is the default value
0773 echo auto > <child>/power/control
0774 echo 1 > <child>/power/persist # this is the default value
0775
0776 Suggested Userspace Port Power Policy
0777 -------------------------------------
0778
0779 As noted above userspace needs to be careful and deliberate about what
0780 ports are enabled for poweroff.
0781
0782 The default configuration is that all ports start with
0783 ``power/pm_qos_no_power_off`` set to ``1`` causing ports to always remain
0784 active.
0785
0786 Given confidence in the platform firmware's description of the ports
0787 (ACPI _PLD record for a port populates 'connect_type') userspace can
0788 clear pm_qos_no_power_off for all 'not used' ports. The same can be
0789 done for 'hardwired' ports provided poweroff is coordinated with any
0790 connection switch for the port.
0791
0792 A more aggressive userspace policy is to enable USB port power off for
0793 all ports (set ``<hubdev-portX>/power/pm_qos_no_power_off`` to ``0``) when
0794 some external factor indicates the user has stopped interacting with the
0795 system. For example, a distro may want to enable power off all USB
0796 ports when the screen blanks, and re-power them when the screen becomes
0797 active. Smart phones and tablets may want to power off USB ports when
0798 the user pushes the power button.