xref: /openbmc/linux/Documentation/driver-api/usb/power-management.rst (revision 0898782247ae533d1f4e47a06bc5d4870931b284)
13db5f406SMauro Carvalho Chehab.. _usb-power-management:
23db5f406SMauro Carvalho Chehab
33db5f406SMauro Carvalho ChehabPower Management for USB
43db5f406SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~
53db5f406SMauro Carvalho Chehab
63db5f406SMauro Carvalho Chehab:Author: Alan Stern <stern@rowland.harvard.edu>
73db5f406SMauro Carvalho Chehab:Date: Last-updated: February 2014
83db5f406SMauro Carvalho Chehab
93db5f406SMauro Carvalho Chehab..
103db5f406SMauro Carvalho Chehab	Contents:
113db5f406SMauro Carvalho Chehab	---------
123db5f406SMauro Carvalho Chehab	* What is Power Management?
133db5f406SMauro Carvalho Chehab	* What is Remote Wakeup?
143db5f406SMauro Carvalho Chehab	* When is a USB device idle?
153db5f406SMauro Carvalho Chehab	* Forms of dynamic PM
163db5f406SMauro Carvalho Chehab	* The user interface for dynamic PM
173db5f406SMauro Carvalho Chehab	* Changing the default idle-delay time
183db5f406SMauro Carvalho Chehab	* Warnings
193db5f406SMauro Carvalho Chehab	* The driver interface for Power Management
203db5f406SMauro Carvalho Chehab	* The driver interface for autosuspend and autoresume
213db5f406SMauro Carvalho Chehab	* Other parts of the driver interface
223db5f406SMauro Carvalho Chehab	* Mutual exclusion
233db5f406SMauro Carvalho Chehab	* Interaction between dynamic PM and system PM
243db5f406SMauro Carvalho Chehab	* xHCI hardware link PM
253db5f406SMauro Carvalho Chehab	* USB Port Power Control
263db5f406SMauro Carvalho Chehab	* User Interface for Port Power Control
273db5f406SMauro Carvalho Chehab	* Suggested Userspace Port Power Policy
283db5f406SMauro Carvalho Chehab
293db5f406SMauro Carvalho Chehab
303db5f406SMauro Carvalho ChehabWhat is Power Management?
313db5f406SMauro Carvalho Chehab-------------------------
323db5f406SMauro Carvalho Chehab
333db5f406SMauro Carvalho ChehabPower Management (PM) is the practice of saving energy by suspending
343db5f406SMauro Carvalho Chehabparts of a computer system when they aren't being used.  While a
353db5f406SMauro Carvalho Chehabcomponent is ``suspended`` it is in a nonfunctional low-power state; it
363db5f406SMauro Carvalho Chehabmight even be turned off completely.  A suspended component can be
373db5f406SMauro Carvalho Chehab``resumed`` (returned to a functional full-power state) when the kernel
383db5f406SMauro Carvalho Chehabneeds to use it.  (There also are forms of PM in which components are
393db5f406SMauro Carvalho Chehabplaced in a less functional but still usable state instead of being
403db5f406SMauro Carvalho Chehabsuspended; an example would be reducing the CPU's clock rate.  This
413db5f406SMauro Carvalho Chehabdocument will not discuss those other forms.)
423db5f406SMauro Carvalho Chehab
433db5f406SMauro Carvalho ChehabWhen the parts being suspended include the CPU and most of the rest of
443db5f406SMauro Carvalho Chehabthe system, we speak of it as a "system suspend".  When a particular
453db5f406SMauro Carvalho Chehabdevice is turned off while the system as a whole remains running, we
463db5f406SMauro Carvalho Chehabcall it a "dynamic suspend" (also known as a "runtime suspend" or
473db5f406SMauro Carvalho Chehab"selective suspend").  This document concentrates mostly on how
483db5f406SMauro Carvalho Chehabdynamic PM is implemented in the USB subsystem, although system PM is
49*151f4e2bSMauro Carvalho Chehabcovered to some extent (see ``Documentation/power/*.rst`` for more
503db5f406SMauro Carvalho Chehabinformation about system PM).
513db5f406SMauro Carvalho Chehab
523db5f406SMauro Carvalho ChehabSystem PM support is present only if the kernel was built with
533db5f406SMauro Carvalho Chehab``CONFIG_SUSPEND`` or ``CONFIG_HIBERNATION`` enabled.  Dynamic PM support
543db5f406SMauro Carvalho Chehab
553db5f406SMauro Carvalho Chehabfor USB is present whenever
563db5f406SMauro Carvalho Chehabthe kernel was built with ``CONFIG_PM`` enabled.
573db5f406SMauro Carvalho Chehab
583db5f406SMauro Carvalho Chehab[Historically, dynamic PM support for USB was present only if the
593db5f406SMauro Carvalho Chehabkernel had been built with ``CONFIG_USB_SUSPEND`` enabled (which depended on
603db5f406SMauro Carvalho Chehab``CONFIG_PM_RUNTIME``).  Starting with the 3.10 kernel release, dynamic PM
613db5f406SMauro Carvalho Chehabsupport for USB was present whenever the kernel was built with
623db5f406SMauro Carvalho Chehab``CONFIG_PM_RUNTIME`` enabled.  The ``CONFIG_USB_SUSPEND`` option had been
633db5f406SMauro Carvalho Chehabeliminated.]
643db5f406SMauro Carvalho Chehab
653db5f406SMauro Carvalho Chehab
663db5f406SMauro Carvalho ChehabWhat is Remote Wakeup?
673db5f406SMauro Carvalho Chehab----------------------
683db5f406SMauro Carvalho Chehab
693db5f406SMauro Carvalho ChehabWhen a device has been suspended, it generally doesn't resume until
703db5f406SMauro Carvalho Chehabthe computer tells it to.  Likewise, if the entire computer has been
713db5f406SMauro Carvalho Chehabsuspended, it generally doesn't resume until the user tells it to, say
723db5f406SMauro Carvalho Chehabby pressing a power button or opening the cover.
733db5f406SMauro Carvalho Chehab
743db5f406SMauro Carvalho ChehabHowever some devices have the capability of resuming by themselves, or
753db5f406SMauro Carvalho Chehabasking the kernel to resume them, or even telling the entire computer
763db5f406SMauro Carvalho Chehabto resume.  This capability goes by several names such as "Wake On
773db5f406SMauro Carvalho ChehabLAN"; we will refer to it generically as "remote wakeup".  When a
783db5f406SMauro Carvalho Chehabdevice is enabled for remote wakeup and it is suspended, it may resume
793db5f406SMauro Carvalho Chehabitself (or send a request to be resumed) in response to some external
803db5f406SMauro Carvalho Chehabevent.  Examples include a suspended keyboard resuming when a key is
813db5f406SMauro Carvalho Chehabpressed, or a suspended USB hub resuming when a device is plugged in.
823db5f406SMauro Carvalho Chehab
833db5f406SMauro Carvalho Chehab
843db5f406SMauro Carvalho ChehabWhen is a USB device idle?
853db5f406SMauro Carvalho Chehab--------------------------
863db5f406SMauro Carvalho Chehab
873db5f406SMauro Carvalho ChehabA device is idle whenever the kernel thinks it's not busy doing
883db5f406SMauro Carvalho Chehabanything important and thus is a candidate for being suspended.  The
893db5f406SMauro Carvalho Chehabexact definition depends on the device's driver; drivers are allowed
903db5f406SMauro Carvalho Chehabto declare that a device isn't idle even when there's no actual
913db5f406SMauro Carvalho Chehabcommunication taking place.  (For example, a hub isn't considered idle
923db5f406SMauro Carvalho Chehabunless all the devices plugged into that hub are already suspended.)
933db5f406SMauro Carvalho ChehabIn addition, a device isn't considered idle so long as a program keeps
943db5f406SMauro Carvalho Chehabits usbfs file open, whether or not any I/O is going on.
953db5f406SMauro Carvalho Chehab
963db5f406SMauro Carvalho ChehabIf a USB device has no driver, its usbfs file isn't open, and it isn't
973db5f406SMauro Carvalho Chehabbeing accessed through sysfs, then it definitely is idle.
983db5f406SMauro Carvalho Chehab
993db5f406SMauro Carvalho Chehab
1003db5f406SMauro Carvalho ChehabForms of dynamic PM
1013db5f406SMauro Carvalho Chehab-------------------
1023db5f406SMauro Carvalho Chehab
1033db5f406SMauro Carvalho ChehabDynamic suspends occur when the kernel decides to suspend an idle
1043db5f406SMauro Carvalho Chehabdevice.  This is called ``autosuspend`` for short.  In general, a device
1053db5f406SMauro Carvalho Chehabwon't be autosuspended unless it has been idle for some minimum period
1063db5f406SMauro Carvalho Chehabof time, the so-called idle-delay time.
1073db5f406SMauro Carvalho Chehab
1083db5f406SMauro Carvalho ChehabOf course, nothing the kernel does on its own initiative should
1093db5f406SMauro Carvalho Chehabprevent the computer or its devices from working properly.  If a
1103db5f406SMauro Carvalho Chehabdevice has been autosuspended and a program tries to use it, the
1113db5f406SMauro Carvalho Chehabkernel will automatically resume the device (autoresume).  For the
1123db5f406SMauro Carvalho Chehabsame reason, an autosuspended device will usually have remote wakeup
1133db5f406SMauro Carvalho Chehabenabled, if the device supports remote wakeup.
1143db5f406SMauro Carvalho Chehab
1153db5f406SMauro Carvalho ChehabIt is worth mentioning that many USB drivers don't support
1163db5f406SMauro Carvalho Chehabautosuspend.  In fact, at the time of this writing (Linux 2.6.23) the
1173db5f406SMauro Carvalho Chehabonly drivers which do support it are the hub driver, kaweth, asix,
1183db5f406SMauro Carvalho Chehabusblp, usblcd, and usb-skeleton (which doesn't count).  If a
1193db5f406SMauro Carvalho Chehabnon-supporting driver is bound to a device, the device won't be
1203db5f406SMauro Carvalho Chehabautosuspended.  In effect, the kernel pretends the device is never
1213db5f406SMauro Carvalho Chehabidle.
1223db5f406SMauro Carvalho Chehab
1233db5f406SMauro Carvalho ChehabWe can categorize power management events in two broad classes:
1243db5f406SMauro Carvalho Chehabexternal and internal.  External events are those triggered by some
1253db5f406SMauro Carvalho Chehabagent outside the USB stack: system suspend/resume (triggered by
1263db5f406SMauro Carvalho Chehabuserspace), manual dynamic resume (also triggered by userspace), and
1273db5f406SMauro Carvalho Chehabremote wakeup (triggered by the device).  Internal events are those
1283db5f406SMauro Carvalho Chehabtriggered within the USB stack: autosuspend and autoresume.  Note that
1293db5f406SMauro Carvalho Chehaball dynamic suspend events are internal; external agents are not
1303db5f406SMauro Carvalho Chehaballowed to issue dynamic suspends.
1313db5f406SMauro Carvalho Chehab
1323db5f406SMauro Carvalho Chehab
1333db5f406SMauro Carvalho ChehabThe user interface for dynamic PM
1343db5f406SMauro Carvalho Chehab---------------------------------
1353db5f406SMauro Carvalho Chehab
1363db5f406SMauro Carvalho ChehabThe user interface for controlling dynamic PM is located in the ``power/``
1373db5f406SMauro Carvalho Chehabsubdirectory of each USB device's sysfs directory, that is, in
1383db5f406SMauro Carvalho Chehab``/sys/bus/usb/devices/.../power/`` where "..." is the device's ID.  The
1393db5f406SMauro Carvalho Chehabrelevant attribute files are: wakeup, control, and
1403db5f406SMauro Carvalho Chehab``autosuspend_delay_ms``.  (There may also be a file named ``level``; this
1413db5f406SMauro Carvalho Chehabfile was deprecated as of the 2.6.35 kernel and replaced by the
1423db5f406SMauro Carvalho Chehab``control`` file.  In 2.6.38 the ``autosuspend`` file will be deprecated
1433db5f406SMauro Carvalho Chehaband replaced by the ``autosuspend_delay_ms`` file.  The only difference
1443db5f406SMauro Carvalho Chehabis that the newer file expresses the delay in milliseconds whereas the
1453db5f406SMauro Carvalho Chehabolder file uses seconds.  Confusingly, both files are present in 2.6.37
1463db5f406SMauro Carvalho Chehabbut only ``autosuspend`` works.)
1473db5f406SMauro Carvalho Chehab
1483db5f406SMauro Carvalho Chehab	``power/wakeup``
1493db5f406SMauro Carvalho Chehab
1503db5f406SMauro Carvalho Chehab		This file is empty if the device does not support
1513db5f406SMauro Carvalho Chehab		remote wakeup.  Otherwise the file contains either the
1523db5f406SMauro Carvalho Chehab		word ``enabled`` or the word ``disabled``, and you can
1533db5f406SMauro Carvalho Chehab		write those words to the file.  The setting determines
1543db5f406SMauro Carvalho Chehab		whether or not remote wakeup will be enabled when the
1553db5f406SMauro Carvalho Chehab		device is next suspended.  (If the setting is changed
1563db5f406SMauro Carvalho Chehab		while the device is suspended, the change won't take
1573db5f406SMauro Carvalho Chehab		effect until the following suspend.)
1583db5f406SMauro Carvalho Chehab
1593db5f406SMauro Carvalho Chehab	``power/control``
1603db5f406SMauro Carvalho Chehab
1613db5f406SMauro Carvalho Chehab		This file contains one of two words: ``on`` or ``auto``.
1623db5f406SMauro Carvalho Chehab		You can write those words to the file to change the
1633db5f406SMauro Carvalho Chehab		device's setting.
1643db5f406SMauro Carvalho Chehab
1653db5f406SMauro Carvalho Chehab		- ``on`` means that the device should be resumed and
1663db5f406SMauro Carvalho Chehab		  autosuspend is not allowed.  (Of course, system
1673db5f406SMauro Carvalho Chehab		  suspends are still allowed.)
1683db5f406SMauro Carvalho Chehab
1693db5f406SMauro Carvalho Chehab		- ``auto`` is the normal state in which the kernel is
1703db5f406SMauro Carvalho Chehab		  allowed to autosuspend and autoresume the device.
1713db5f406SMauro Carvalho Chehab
1723db5f406SMauro Carvalho Chehab		(In kernels up to 2.6.32, you could also specify
1733db5f406SMauro Carvalho Chehab		``suspend``, meaning that the device should remain
1743db5f406SMauro Carvalho Chehab		suspended and autoresume was not allowed.  This
1753db5f406SMauro Carvalho Chehab		setting is no longer supported.)
1763db5f406SMauro Carvalho Chehab
1773db5f406SMauro Carvalho Chehab	``power/autosuspend_delay_ms``
1783db5f406SMauro Carvalho Chehab
1793db5f406SMauro Carvalho Chehab		This file contains an integer value, which is the
1803db5f406SMauro Carvalho Chehab		number of milliseconds the device should remain idle
1813db5f406SMauro Carvalho Chehab		before the kernel will autosuspend it (the idle-delay
1823db5f406SMauro Carvalho Chehab		time).  The default is 2000.  0 means to autosuspend
1833db5f406SMauro Carvalho Chehab		as soon as the device becomes idle, and negative
1843db5f406SMauro Carvalho Chehab		values mean never to autosuspend.  You can write a
1853db5f406SMauro Carvalho Chehab		number to the file to change the autosuspend
1863db5f406SMauro Carvalho Chehab		idle-delay time.
1873db5f406SMauro Carvalho Chehab
1883db5f406SMauro Carvalho ChehabWriting ``-1`` to ``power/autosuspend_delay_ms`` and writing ``on`` to
1893db5f406SMauro Carvalho Chehab``power/control`` do essentially the same thing -- they both prevent the
1903db5f406SMauro Carvalho Chehabdevice from being autosuspended.  Yes, this is a redundancy in the
1913db5f406SMauro Carvalho ChehabAPI.
1923db5f406SMauro Carvalho Chehab
1933db5f406SMauro Carvalho Chehab(In 2.6.21 writing ``0`` to ``power/autosuspend`` would prevent the device
1943db5f406SMauro Carvalho Chehabfrom being autosuspended; the behavior was changed in 2.6.22.  The
1953db5f406SMauro Carvalho Chehab``power/autosuspend`` attribute did not exist prior to 2.6.21, and the
1963db5f406SMauro Carvalho Chehab``power/level`` attribute did not exist prior to 2.6.22.  ``power/control``
1973db5f406SMauro Carvalho Chehabwas added in 2.6.34, and ``power/autosuspend_delay_ms`` was added in
1983db5f406SMauro Carvalho Chehab2.6.37 but did not become functional until 2.6.38.)
1993db5f406SMauro Carvalho Chehab
2003db5f406SMauro Carvalho Chehab
2013db5f406SMauro Carvalho ChehabChanging the default idle-delay time
2023db5f406SMauro Carvalho Chehab------------------------------------
2033db5f406SMauro Carvalho Chehab
2043db5f406SMauro Carvalho ChehabThe default autosuspend idle-delay time (in seconds) is controlled by
2053db5f406SMauro Carvalho Chehaba module parameter in usbcore.  You can specify the value when usbcore
2063db5f406SMauro Carvalho Chehabis loaded.  For example, to set it to 5 seconds instead of 2 you would
2073db5f406SMauro Carvalho Chehabdo::
2083db5f406SMauro Carvalho Chehab
2093db5f406SMauro Carvalho Chehab	modprobe usbcore autosuspend=5
2103db5f406SMauro Carvalho Chehab
2113db5f406SMauro Carvalho ChehabEquivalently, you could add to a configuration file in /etc/modprobe.d
2123db5f406SMauro Carvalho Chehaba line saying::
2133db5f406SMauro Carvalho Chehab
2143db5f406SMauro Carvalho Chehab	options usbcore autosuspend=5
2153db5f406SMauro Carvalho Chehab
2163db5f406SMauro Carvalho ChehabSome distributions load the usbcore module very early during the boot
2173db5f406SMauro Carvalho Chehabprocess, by means of a program or script running from an initramfs
2183db5f406SMauro Carvalho Chehabimage.  To alter the parameter value you would have to rebuild that
2193db5f406SMauro Carvalho Chehabimage.
2203db5f406SMauro Carvalho Chehab
2213db5f406SMauro Carvalho ChehabIf usbcore is compiled into the kernel rather than built as a loadable
2223db5f406SMauro Carvalho Chehabmodule, you can add::
2233db5f406SMauro Carvalho Chehab
2243db5f406SMauro Carvalho Chehab	usbcore.autosuspend=5
2253db5f406SMauro Carvalho Chehab
2263db5f406SMauro Carvalho Chehabto the kernel's boot command line.
2273db5f406SMauro Carvalho Chehab
2283db5f406SMauro Carvalho ChehabFinally, the parameter value can be changed while the system is
2293db5f406SMauro Carvalho Chehabrunning.  If you do::
2303db5f406SMauro Carvalho Chehab
2313db5f406SMauro Carvalho Chehab	echo 5 >/sys/module/usbcore/parameters/autosuspend
2323db5f406SMauro Carvalho Chehab
2333db5f406SMauro Carvalho Chehabthen each new USB device will have its autosuspend idle-delay
2343db5f406SMauro Carvalho Chehabinitialized to 5.  (The idle-delay values for already existing devices
2353db5f406SMauro Carvalho Chehabwill not be affected.)
2363db5f406SMauro Carvalho Chehab
2373db5f406SMauro Carvalho ChehabSetting the initial default idle-delay to -1 will prevent any
2383db5f406SMauro Carvalho Chehabautosuspend of any USB device.  This has the benefit of allowing you
2393db5f406SMauro Carvalho Chehabthen to enable autosuspend for selected devices.
2403db5f406SMauro Carvalho Chehab
2413db5f406SMauro Carvalho Chehab
2423db5f406SMauro Carvalho ChehabWarnings
2433db5f406SMauro Carvalho Chehab--------
2443db5f406SMauro Carvalho Chehab
2453db5f406SMauro Carvalho ChehabThe USB specification states that all USB devices must support power
2463db5f406SMauro Carvalho Chehabmanagement.  Nevertheless, the sad fact is that many devices do not
2473db5f406SMauro Carvalho Chehabsupport it very well.  You can suspend them all right, but when you
2483db5f406SMauro Carvalho Chehabtry to resume them they disconnect themselves from the USB bus or
2493db5f406SMauro Carvalho Chehabthey stop working entirely.  This seems to be especially prevalent
2503db5f406SMauro Carvalho Chehabamong printers and scanners, but plenty of other types of device have
2513db5f406SMauro Carvalho Chehabthe same deficiency.
2523db5f406SMauro Carvalho Chehab
2533db5f406SMauro Carvalho ChehabFor this reason, by default the kernel disables autosuspend (the
2543db5f406SMauro Carvalho Chehab``power/control`` attribute is initialized to ``on``) for all devices other
2553db5f406SMauro Carvalho Chehabthan hubs.  Hubs, at least, appear to be reasonably well-behaved in
2563db5f406SMauro Carvalho Chehabthis regard.
2573db5f406SMauro Carvalho Chehab
2583db5f406SMauro Carvalho Chehab(In 2.6.21 and 2.6.22 this wasn't the case.  Autosuspend was enabled
2593db5f406SMauro Carvalho Chehabby default for almost all USB devices.  A number of people experienced
2603db5f406SMauro Carvalho Chehabproblems as a result.)
2613db5f406SMauro Carvalho Chehab
2623db5f406SMauro Carvalho ChehabThis means that non-hub devices won't be autosuspended unless the user
2633db5f406SMauro Carvalho Chehabor a program explicitly enables it.  As of this writing there aren't
2643db5f406SMauro Carvalho Chehabany widespread programs which will do this; we hope that in the near
2653db5f406SMauro Carvalho Chehabfuture device managers such as HAL will take on this added
2663db5f406SMauro Carvalho Chehabresponsibility.  In the meantime you can always carry out the
2673db5f406SMauro Carvalho Chehabnecessary operations by hand or add them to a udev script.  You can
2683db5f406SMauro Carvalho Chehabalso change the idle-delay time; 2 seconds is not the best choice for
2693db5f406SMauro Carvalho Chehabevery device.
2703db5f406SMauro Carvalho Chehab
2713db5f406SMauro Carvalho ChehabIf a driver knows that its device has proper suspend/resume support,
2723db5f406SMauro Carvalho Chehabit can enable autosuspend all by itself.  For example, the video
2733db5f406SMauro Carvalho Chehabdriver for a laptop's webcam might do this (in recent kernels they
2743db5f406SMauro Carvalho Chehabdo), since these devices are rarely used and so should normally be
2753db5f406SMauro Carvalho Chehabautosuspended.
2763db5f406SMauro Carvalho Chehab
2773db5f406SMauro Carvalho ChehabSometimes it turns out that even when a device does work okay with
2783db5f406SMauro Carvalho Chehabautosuspend there are still problems.  For example, the usbhid driver,
2793db5f406SMauro Carvalho Chehabwhich manages keyboards and mice, has autosuspend support.  Tests with
2803db5f406SMauro Carvalho Chehaba number of keyboards show that typing on a suspended keyboard, while
2813db5f406SMauro Carvalho Chehabcausing the keyboard to do a remote wakeup all right, will nonetheless
2823db5f406SMauro Carvalho Chehabfrequently result in lost keystrokes.  Tests with mice show that some
2833db5f406SMauro Carvalho Chehabof them will issue a remote-wakeup request in response to button
2843db5f406SMauro Carvalho Chehabpresses but not to motion, and some in response to neither.
2853db5f406SMauro Carvalho Chehab
2863db5f406SMauro Carvalho ChehabThe kernel will not prevent you from enabling autosuspend on devices
2873db5f406SMauro Carvalho Chehabthat can't handle it.  It is even possible in theory to damage a
2883db5f406SMauro Carvalho Chehabdevice by suspending it at the wrong time.  (Highly unlikely, but
2893db5f406SMauro Carvalho Chehabpossible.)  Take care.
2903db5f406SMauro Carvalho Chehab
2913db5f406SMauro Carvalho Chehab
2923db5f406SMauro Carvalho ChehabThe driver interface for Power Management
2933db5f406SMauro Carvalho Chehab-----------------------------------------
2943db5f406SMauro Carvalho Chehab
2953db5f406SMauro Carvalho ChehabThe requirements for a USB driver to support external power management
2963db5f406SMauro Carvalho Chehabare pretty modest; the driver need only define::
2973db5f406SMauro Carvalho Chehab
2983db5f406SMauro Carvalho Chehab	.suspend
2993db5f406SMauro Carvalho Chehab	.resume
3003db5f406SMauro Carvalho Chehab	.reset_resume
3013db5f406SMauro Carvalho Chehab
3023db5f406SMauro Carvalho Chehabmethods in its :c:type:`usb_driver` structure, and the ``reset_resume`` method
3033db5f406SMauro Carvalho Chehabis optional.  The methods' jobs are quite simple:
3043db5f406SMauro Carvalho Chehab
3053db5f406SMauro Carvalho Chehab      - The ``suspend`` method is called to warn the driver that the
3063db5f406SMauro Carvalho Chehab	device is going to be suspended.  If the driver returns a
3073db5f406SMauro Carvalho Chehab	negative error code, the suspend will be aborted.  Normally
3083db5f406SMauro Carvalho Chehab	the driver will return 0, in which case it must cancel all
3093db5f406SMauro Carvalho Chehab	outstanding URBs (:c:func:`usb_kill_urb`) and not submit any more.
3103db5f406SMauro Carvalho Chehab
3113db5f406SMauro Carvalho Chehab      - The ``resume`` method is called to tell the driver that the
3123db5f406SMauro Carvalho Chehab	device has been resumed and the driver can return to normal
3133db5f406SMauro Carvalho Chehab	operation.  URBs may once more be submitted.
3143db5f406SMauro Carvalho Chehab
3153db5f406SMauro Carvalho Chehab      - The ``reset_resume`` method is called to tell the driver that
3163db5f406SMauro Carvalho Chehab	the device has been resumed and it also has been reset.
3173db5f406SMauro Carvalho Chehab	The driver should redo any necessary device initialization,
3183db5f406SMauro Carvalho Chehab	since the device has probably lost most or all of its state
3193db5f406SMauro Carvalho Chehab	(although the interfaces will be in the same altsettings as
3203db5f406SMauro Carvalho Chehab	before the suspend).
3213db5f406SMauro Carvalho Chehab
3223db5f406SMauro Carvalho ChehabIf the device is disconnected or powered down while it is suspended,
3233db5f406SMauro Carvalho Chehabthe ``disconnect`` method will be called instead of the ``resume`` or
3243db5f406SMauro Carvalho Chehab``reset_resume`` method.  This is also quite likely to happen when
3253db5f406SMauro Carvalho Chehabwaking up from hibernation, as many systems do not maintain suspend
3263db5f406SMauro Carvalho Chehabcurrent to the USB host controllers during hibernation.  (It's
3273db5f406SMauro Carvalho Chehabpossible to work around the hibernation-forces-disconnect problem by
3283db5f406SMauro Carvalho Chehabusing the USB Persist facility.)
3293db5f406SMauro Carvalho Chehab
3303db5f406SMauro Carvalho ChehabThe ``reset_resume`` method is used by the USB Persist facility (see
331e1c3e6e1SMauro Carvalho Chehab:ref:`usb-persist`) and it can also be used under certain
3323db5f406SMauro Carvalho Chehabcircumstances when ``CONFIG_USB_PERSIST`` is not enabled.  Currently, if a
3333db5f406SMauro Carvalho Chehabdevice is reset during a resume and the driver does not have a
3343db5f406SMauro Carvalho Chehab``reset_resume`` method, the driver won't receive any notification about
3353db5f406SMauro Carvalho Chehabthe resume.  Later kernels will call the driver's ``disconnect`` method;
3363db5f406SMauro Carvalho Chehab2.6.23 doesn't do this.
3373db5f406SMauro Carvalho Chehab
3383db5f406SMauro Carvalho ChehabUSB drivers are bound to interfaces, so their ``suspend`` and ``resume``
3393db5f406SMauro Carvalho Chehabmethods get called when the interfaces are suspended or resumed.  In
3403db5f406SMauro Carvalho Chehabprinciple one might want to suspend some interfaces on a device (i.e.,
3413db5f406SMauro Carvalho Chehabforce the drivers for those interface to stop all activity) without
3423db5f406SMauro Carvalho Chehabsuspending the other interfaces.  The USB core doesn't allow this; all
3433db5f406SMauro Carvalho Chehabinterfaces are suspended when the device itself is suspended and all
3443db5f406SMauro Carvalho Chehabinterfaces are resumed when the device is resumed.  It isn't possible
3453db5f406SMauro Carvalho Chehabto suspend or resume some but not all of a device's interfaces.  The
3463db5f406SMauro Carvalho Chehabclosest you can come is to unbind the interfaces' drivers.
3473db5f406SMauro Carvalho Chehab
3483db5f406SMauro Carvalho Chehab
3493db5f406SMauro Carvalho ChehabThe driver interface for autosuspend and autoresume
3503db5f406SMauro Carvalho Chehab---------------------------------------------------
3513db5f406SMauro Carvalho Chehab
3523db5f406SMauro Carvalho ChehabTo support autosuspend and autoresume, a driver should implement all
3533db5f406SMauro Carvalho Chehabthree of the methods listed above.  In addition, a driver indicates
3543db5f406SMauro Carvalho Chehabthat it supports autosuspend by setting the ``.supports_autosuspend`` flag
3553db5f406SMauro Carvalho Chehabin its usb_driver structure.  It is then responsible for informing the
3563db5f406SMauro Carvalho ChehabUSB core whenever one of its interfaces becomes busy or idle.  The
3573db5f406SMauro Carvalho Chehabdriver does so by calling these six functions::
3583db5f406SMauro Carvalho Chehab
3593db5f406SMauro Carvalho Chehab	int  usb_autopm_get_interface(struct usb_interface *intf);
3603db5f406SMauro Carvalho Chehab	void usb_autopm_put_interface(struct usb_interface *intf);
3613db5f406SMauro Carvalho Chehab	int  usb_autopm_get_interface_async(struct usb_interface *intf);
3623db5f406SMauro Carvalho Chehab	void usb_autopm_put_interface_async(struct usb_interface *intf);
3633db5f406SMauro Carvalho Chehab	void usb_autopm_get_interface_no_resume(struct usb_interface *intf);
3643db5f406SMauro Carvalho Chehab	void usb_autopm_put_interface_no_suspend(struct usb_interface *intf);
3653db5f406SMauro Carvalho Chehab
3663db5f406SMauro Carvalho ChehabThe functions work by maintaining a usage counter in the
3673db5f406SMauro Carvalho Chehabusb_interface's embedded device structure.  When the counter is > 0
3683db5f406SMauro Carvalho Chehabthen the interface is deemed to be busy, and the kernel will not
3693db5f406SMauro Carvalho Chehabautosuspend the interface's device.  When the usage counter is = 0
3703db5f406SMauro Carvalho Chehabthen the interface is considered to be idle, and the kernel may
3713db5f406SMauro Carvalho Chehabautosuspend the device.
3723db5f406SMauro Carvalho Chehab
373c2b71462SAlan SternDrivers must be careful to balance their overall changes to the usage
374c2b71462SAlan Sterncounter.  Unbalanced "get"s will remain in effect when a driver is
375c2b71462SAlan Sternunbound from its interface, preventing the device from going into
376c2b71462SAlan Sternruntime suspend should the interface be bound to a driver again.  On
377c2b71462SAlan Sternthe other hand, drivers are allowed to achieve this balance by calling
378c2b71462SAlan Sternthe ``usb_autopm_*`` functions even after their ``disconnect`` routine
379c2b71462SAlan Sternhas returned -- say from within a work-queue routine -- provided they
380c2b71462SAlan Sternretain an active reference to the interface (via ``usb_get_intf`` and
381c2b71462SAlan Stern``usb_put_intf``).
3823db5f406SMauro Carvalho Chehab
3833db5f406SMauro Carvalho ChehabDrivers using the async routines are responsible for their own
3843db5f406SMauro Carvalho Chehabsynchronization and mutual exclusion.
3853db5f406SMauro Carvalho Chehab
3863db5f406SMauro Carvalho Chehab	:c:func:`usb_autopm_get_interface` increments the usage counter and
3873db5f406SMauro Carvalho Chehab	does an autoresume if the device is suspended.  If the
3883db5f406SMauro Carvalho Chehab	autoresume fails, the counter is decremented back.
3893db5f406SMauro Carvalho Chehab
3903db5f406SMauro Carvalho Chehab	:c:func:`usb_autopm_put_interface` decrements the usage counter and
3913db5f406SMauro Carvalho Chehab	attempts an autosuspend if the new value is = 0.
3923db5f406SMauro Carvalho Chehab
3933db5f406SMauro Carvalho Chehab	:c:func:`usb_autopm_get_interface_async` and
3943db5f406SMauro Carvalho Chehab	:c:func:`usb_autopm_put_interface_async` do almost the same things as
3953db5f406SMauro Carvalho Chehab	their non-async counterparts.  The big difference is that they
3963db5f406SMauro Carvalho Chehab	use a workqueue to do the resume or suspend part of their
3973db5f406SMauro Carvalho Chehab	jobs.  As a result they can be called in an atomic context,
3983db5f406SMauro Carvalho Chehab	such as an URB's completion handler, but when they return the
3993db5f406SMauro Carvalho Chehab	device will generally not yet be in the desired state.
4003db5f406SMauro Carvalho Chehab
4013db5f406SMauro Carvalho Chehab	:c:func:`usb_autopm_get_interface_no_resume` and
4023db5f406SMauro Carvalho Chehab	:c:func:`usb_autopm_put_interface_no_suspend` merely increment or
4033db5f406SMauro Carvalho Chehab	decrement the usage counter; they do not attempt to carry out
4043db5f406SMauro Carvalho Chehab	an autoresume or an autosuspend.  Hence they can be called in
4053db5f406SMauro Carvalho Chehab	an atomic context.
4063db5f406SMauro Carvalho Chehab
4073db5f406SMauro Carvalho ChehabThe simplest usage pattern is that a driver calls
4083db5f406SMauro Carvalho Chehab:c:func:`usb_autopm_get_interface` in its open routine and
4093db5f406SMauro Carvalho Chehab:c:func:`usb_autopm_put_interface` in its close or release routine.  But other
4103db5f406SMauro Carvalho Chehabpatterns are possible.
4113db5f406SMauro Carvalho Chehab
4123db5f406SMauro Carvalho ChehabThe autosuspend attempts mentioned above will often fail for one
4133db5f406SMauro Carvalho Chehabreason or another.  For example, the ``power/control`` attribute might be
4143db5f406SMauro Carvalho Chehabset to ``on``, or another interface in the same device might not be
4153db5f406SMauro Carvalho Chehabidle.  This is perfectly normal.  If the reason for failure was that
4163db5f406SMauro Carvalho Chehabthe device hasn't been idle for long enough, a timer is scheduled to
4173db5f406SMauro Carvalho Chehabcarry out the operation automatically when the autosuspend idle-delay
4183db5f406SMauro Carvalho Chehabhas expired.
4193db5f406SMauro Carvalho Chehab
4203db5f406SMauro Carvalho ChehabAutoresume attempts also can fail, although failure would mean that
4213db5f406SMauro Carvalho Chehabthe device is no longer present or operating properly.  Unlike
4223db5f406SMauro Carvalho Chehabautosuspend, there's no idle-delay for an autoresume.
4233db5f406SMauro Carvalho Chehab
4243db5f406SMauro Carvalho Chehab
4253db5f406SMauro Carvalho ChehabOther parts of the driver interface
4263db5f406SMauro Carvalho Chehab-----------------------------------
4273db5f406SMauro Carvalho Chehab
4283db5f406SMauro Carvalho ChehabDrivers can enable autosuspend for their devices by calling::
4293db5f406SMauro Carvalho Chehab
4303db5f406SMauro Carvalho Chehab	usb_enable_autosuspend(struct usb_device *udev);
4313db5f406SMauro Carvalho Chehab
4323db5f406SMauro Carvalho Chehabin their :c:func:`probe` routine, if they know that the device is capable of
4333db5f406SMauro Carvalho Chehabsuspending and resuming correctly.  This is exactly equivalent to
4343db5f406SMauro Carvalho Chehabwriting ``auto`` to the device's ``power/control`` attribute.  Likewise,
4353db5f406SMauro Carvalho Chehabdrivers can disable autosuspend by calling::
4363db5f406SMauro Carvalho Chehab
4373db5f406SMauro Carvalho Chehab	usb_disable_autosuspend(struct usb_device *udev);
4383db5f406SMauro Carvalho Chehab
4393db5f406SMauro Carvalho ChehabThis is exactly the same as writing ``on`` to the ``power/control`` attribute.
4403db5f406SMauro Carvalho Chehab
4413db5f406SMauro Carvalho ChehabSometimes a driver needs to make sure that remote wakeup is enabled
4423db5f406SMauro Carvalho Chehabduring autosuspend.  For example, there's not much point
4433db5f406SMauro Carvalho Chehabautosuspending a keyboard if the user can't cause the keyboard to do a
4443db5f406SMauro Carvalho Chehabremote wakeup by typing on it.  If the driver sets
4453db5f406SMauro Carvalho Chehab``intf->needs_remote_wakeup`` to 1, the kernel won't autosuspend the
4463db5f406SMauro Carvalho Chehabdevice if remote wakeup isn't available.  (If the device is already
4473db5f406SMauro Carvalho Chehabautosuspended, though, setting this flag won't cause the kernel to
4483db5f406SMauro Carvalho Chehabautoresume it.  Normally a driver would set this flag in its ``probe``
4493db5f406SMauro Carvalho Chehabmethod, at which time the device is guaranteed not to be
4503db5f406SMauro Carvalho Chehabautosuspended.)
4513db5f406SMauro Carvalho Chehab
4523db5f406SMauro Carvalho ChehabIf a driver does its I/O asynchronously in interrupt context, it
4533db5f406SMauro Carvalho Chehabshould call :c:func:`usb_autopm_get_interface_async` before starting output and
4543db5f406SMauro Carvalho Chehab:c:func:`usb_autopm_put_interface_async` when the output queue drains.  When
4553db5f406SMauro Carvalho Chehabit receives an input event, it should call::
4563db5f406SMauro Carvalho Chehab
4573db5f406SMauro Carvalho Chehab	usb_mark_last_busy(struct usb_device *udev);
4583db5f406SMauro Carvalho Chehab
4593db5f406SMauro Carvalho Chehabin the event handler.  This tells the PM core that the device was just
4603db5f406SMauro Carvalho Chehabbusy and therefore the next autosuspend idle-delay expiration should
4613db5f406SMauro Carvalho Chehabbe pushed back.  Many of the usb_autopm_* routines also make this call,
4623db5f406SMauro Carvalho Chehabso drivers need to worry only when interrupt-driven input arrives.
4633db5f406SMauro Carvalho Chehab
4643db5f406SMauro Carvalho ChehabAsynchronous operation is always subject to races.  For example, a
4653db5f406SMauro Carvalho Chehabdriver may call the :c:func:`usb_autopm_get_interface_async` routine at a time
4663db5f406SMauro Carvalho Chehabwhen the core has just finished deciding the device has been idle for
4673db5f406SMauro Carvalho Chehablong enough but not yet gotten around to calling the driver's ``suspend``
4683db5f406SMauro Carvalho Chehabmethod.  The ``suspend`` method must be responsible for synchronizing with
4693db5f406SMauro Carvalho Chehabthe I/O request routine and the URB completion handler; it should
4703db5f406SMauro Carvalho Chehabcause autosuspends to fail with -EBUSY if the driver needs to use the
4713db5f406SMauro Carvalho Chehabdevice.
4723db5f406SMauro Carvalho Chehab
4733db5f406SMauro Carvalho ChehabExternal suspend calls should never be allowed to fail in this way,
4743db5f406SMauro Carvalho Chehabonly autosuspend calls.  The driver can tell them apart by applying
4753db5f406SMauro Carvalho Chehabthe :c:func:`PMSG_IS_AUTO` macro to the message argument to the ``suspend``
4763db5f406SMauro Carvalho Chehabmethod; it will return True for internal PM events (autosuspend) and
4773db5f406SMauro Carvalho ChehabFalse for external PM events.
4783db5f406SMauro Carvalho Chehab
4793db5f406SMauro Carvalho Chehab
4803db5f406SMauro Carvalho ChehabMutual exclusion
4813db5f406SMauro Carvalho Chehab----------------
4823db5f406SMauro Carvalho Chehab
4833db5f406SMauro Carvalho ChehabFor external events -- but not necessarily for autosuspend or
4843db5f406SMauro Carvalho Chehabautoresume -- the device semaphore (udev->dev.sem) will be held when a
4853db5f406SMauro Carvalho Chehab``suspend`` or ``resume`` method is called.  This implies that external
4863db5f406SMauro Carvalho Chehabsuspend/resume events are mutually exclusive with calls to ``probe``,
4873db5f406SMauro Carvalho Chehab``disconnect``, ``pre_reset``, and ``post_reset``; the USB core guarantees that
4883db5f406SMauro Carvalho Chehabthis is true of autosuspend/autoresume events as well.
4893db5f406SMauro Carvalho Chehab
4903db5f406SMauro Carvalho ChehabIf a driver wants to block all suspend/resume calls during some
4913db5f406SMauro Carvalho Chehabcritical section, the best way is to lock the device and call
4923db5f406SMauro Carvalho Chehab:c:func:`usb_autopm_get_interface` (and do the reverse at the end of the
4933db5f406SMauro Carvalho Chehabcritical section).  Holding the device semaphore will block all
4943db5f406SMauro Carvalho Chehabexternal PM calls, and the :c:func:`usb_autopm_get_interface` will prevent any
4953db5f406SMauro Carvalho Chehabinternal PM calls, even if it fails.  (Exercise: Why?)
4963db5f406SMauro Carvalho Chehab
4973db5f406SMauro Carvalho Chehab
4983db5f406SMauro Carvalho ChehabInteraction between dynamic PM and system PM
4993db5f406SMauro Carvalho Chehab--------------------------------------------
5003db5f406SMauro Carvalho Chehab
5013db5f406SMauro Carvalho ChehabDynamic power management and system power management can interact in
5023db5f406SMauro Carvalho Chehaba couple of ways.
5033db5f406SMauro Carvalho Chehab
5043db5f406SMauro Carvalho ChehabFirstly, a device may already be autosuspended when a system suspend
5053db5f406SMauro Carvalho Chehaboccurs.  Since system suspends are supposed to be as transparent as
5063db5f406SMauro Carvalho Chehabpossible, the device should remain suspended following the system
5073db5f406SMauro Carvalho Chehabresume.  But this theory may not work out well in practice; over time
5083db5f406SMauro Carvalho Chehabthe kernel's behavior in this regard has changed.  As of 2.6.37 the
5093db5f406SMauro Carvalho Chehabpolicy is to resume all devices during a system resume and let them
5103db5f406SMauro Carvalho Chehabhandle their own runtime suspends afterward.
5113db5f406SMauro Carvalho Chehab
5123db5f406SMauro Carvalho ChehabSecondly, a dynamic power-management event may occur as a system
5133db5f406SMauro Carvalho Chehabsuspend is underway.  The window for this is short, since system
5143db5f406SMauro Carvalho Chehabsuspends don't take long (a few seconds usually), but it can happen.
5153db5f406SMauro Carvalho ChehabFor example, a suspended device may send a remote-wakeup signal while
5163db5f406SMauro Carvalho Chehabthe system is suspending.  The remote wakeup may succeed, which would
5173db5f406SMauro Carvalho Chehabcause the system suspend to abort.  If the remote wakeup doesn't
5183db5f406SMauro Carvalho Chehabsucceed, it may still remain active and thus cause the system to
5193db5f406SMauro Carvalho Chehabresume as soon as the system suspend is complete.  Or the remote
5203db5f406SMauro Carvalho Chehabwakeup may fail and get lost.  Which outcome occurs depends on timing
5213db5f406SMauro Carvalho Chehaband on the hardware and firmware design.
5223db5f406SMauro Carvalho Chehab
5233db5f406SMauro Carvalho Chehab
5243db5f406SMauro Carvalho ChehabxHCI hardware link PM
5253db5f406SMauro Carvalho Chehab---------------------
5263db5f406SMauro Carvalho Chehab
5273db5f406SMauro Carvalho ChehabxHCI host controller provides hardware link power management to usb2.0
5283db5f406SMauro Carvalho Chehab(xHCI 1.0 feature) and usb3.0 devices which support link PM. By
5293db5f406SMauro Carvalho Chehabenabling hardware LPM, the host can automatically put the device into
5303db5f406SMauro Carvalho Chehablower power state(L1 for usb2.0 devices, or U1/U2 for usb3.0 devices),
5313db5f406SMauro Carvalho Chehabwhich state device can enter and resume very quickly.
5323db5f406SMauro Carvalho Chehab
5333db5f406SMauro Carvalho ChehabThe user interface for controlling hardware LPM is located in the
5343db5f406SMauro Carvalho Chehab``power/`` subdirectory of each USB device's sysfs directory, that is, in
5353db5f406SMauro Carvalho Chehab``/sys/bus/usb/devices/.../power/`` where "..." is the device's ID. The
5363db5f406SMauro Carvalho Chehabrelevant attribute files are ``usb2_hardware_lpm`` and ``usb3_hardware_lpm``.
5373db5f406SMauro Carvalho Chehab
5383db5f406SMauro Carvalho Chehab	``power/usb2_hardware_lpm``
5393db5f406SMauro Carvalho Chehab
5403db5f406SMauro Carvalho Chehab		When a USB2 device which support LPM is plugged to a
5413db5f406SMauro Carvalho Chehab		xHCI host root hub which support software LPM, the
5423db5f406SMauro Carvalho Chehab		host will run a software LPM test for it; if the device
5433db5f406SMauro Carvalho Chehab		enters L1 state and resume successfully and the host
5443db5f406SMauro Carvalho Chehab		supports USB2 hardware LPM, this file will show up and
5453db5f406SMauro Carvalho Chehab		driver will enable hardware LPM	for the device. You
5463db5f406SMauro Carvalho Chehab		can write y/Y/1 or n/N/0 to the file to	enable/disable
5473db5f406SMauro Carvalho Chehab		USB2 hardware LPM manually. This is for	test purpose mainly.
5483db5f406SMauro Carvalho Chehab
5493db5f406SMauro Carvalho Chehab	``power/usb3_hardware_lpm_u1``
5503db5f406SMauro Carvalho Chehab	``power/usb3_hardware_lpm_u2``
5513db5f406SMauro Carvalho Chehab
5523db5f406SMauro Carvalho Chehab		When a USB 3.0 lpm-capable device is plugged in to a
5533db5f406SMauro Carvalho Chehab		xHCI host which supports link PM, it will check if U1
5543db5f406SMauro Carvalho Chehab		and U2 exit latencies have been set in the BOS
5553db5f406SMauro Carvalho Chehab		descriptor; if the check is passed and the host
5563db5f406SMauro Carvalho Chehab		supports USB3 hardware LPM, USB3 hardware LPM will be
5573db5f406SMauro Carvalho Chehab		enabled for the device and these files will be created.
5583db5f406SMauro Carvalho Chehab		The files hold a string value (enable or disable)
5593db5f406SMauro Carvalho Chehab		indicating whether or not USB3 hardware LPM U1 or U2
5603db5f406SMauro Carvalho Chehab		is enabled for the device.
5613db5f406SMauro Carvalho Chehab
5623db5f406SMauro Carvalho ChehabUSB Port Power Control
5633db5f406SMauro Carvalho Chehab----------------------
5643db5f406SMauro Carvalho Chehab
5653db5f406SMauro Carvalho ChehabIn addition to suspending endpoint devices and enabling hardware
5663db5f406SMauro Carvalho Chehabcontrolled link power management, the USB subsystem also has the
5673db5f406SMauro Carvalho Chehabcapability to disable power to ports under some conditions.  Power is
5683db5f406SMauro Carvalho Chehabcontrolled through ``Set/ClearPortFeature(PORT_POWER)`` requests to a hub.
5693db5f406SMauro Carvalho ChehabIn the case of a root or platform-internal hub the host controller
5703db5f406SMauro Carvalho Chehabdriver translates ``PORT_POWER`` requests into platform firmware (ACPI)
5713db5f406SMauro Carvalho Chehabmethod calls to set the port power state. For more background see the
5723db5f406SMauro Carvalho ChehabLinux Plumbers Conference 2012 slides [#f1]_ and video [#f2]_:
5733db5f406SMauro Carvalho Chehab
5743db5f406SMauro Carvalho ChehabUpon receiving a ``ClearPortFeature(PORT_POWER)`` request a USB port is
5753db5f406SMauro Carvalho Chehablogically off, and may trigger the actual loss of VBUS to the port [#f3]_.
5763db5f406SMauro Carvalho ChehabVBUS may be maintained in the case where a hub gangs multiple ports into
5773db5f406SMauro Carvalho Chehaba shared power well causing power to remain until all ports in the gang
5783db5f406SMauro Carvalho Chehabare turned off.  VBUS may also be maintained by hub ports configured for
5793db5f406SMauro Carvalho Chehaba charging application.  In any event a logically off port will lose
5803db5f406SMauro Carvalho Chehabconnection with its device, not respond to hotplug events, and not
5813db5f406SMauro Carvalho Chehabrespond to remote wakeup events.
5823db5f406SMauro Carvalho Chehab
5833db5f406SMauro Carvalho Chehab.. warning::
5843db5f406SMauro Carvalho Chehab
5853db5f406SMauro Carvalho Chehab   turning off a port may result in the inability to hot add a device.
5863db5f406SMauro Carvalho Chehab   Please see "User Interface for Port Power Control" for details.
5873db5f406SMauro Carvalho Chehab
5883db5f406SMauro Carvalho ChehabAs far as the effect on the device itself it is similar to what a device
5893db5f406SMauro Carvalho Chehabgoes through during system suspend, i.e. the power session is lost.  Any
5903db5f406SMauro Carvalho ChehabUSB device or driver that misbehaves with system suspend will be
5913db5f406SMauro Carvalho Chehabsimilarly affected by a port power cycle event.  For this reason the
5923db5f406SMauro Carvalho Chehabimplementation shares the same device recovery path (and honors the same
5933db5f406SMauro Carvalho Chehabquirks) as the system resume path for the hub.
5943db5f406SMauro Carvalho Chehab
5953db5f406SMauro Carvalho Chehab.. [#f1]
5963db5f406SMauro Carvalho Chehab
5973db5f406SMauro Carvalho Chehab  http://dl.dropbox.com/u/96820575/sarah-sharp-lpt-port-power-off2-mini.pdf
5983db5f406SMauro Carvalho Chehab
5993db5f406SMauro Carvalho Chehab.. [#f2]
6003db5f406SMauro Carvalho Chehab
6013db5f406SMauro Carvalho Chehab  http://linuxplumbers.ubicast.tv/videos/usb-port-power-off-kerneluserspace-api/
6023db5f406SMauro Carvalho Chehab
6033db5f406SMauro Carvalho Chehab.. [#f3]
6043db5f406SMauro Carvalho Chehab
6053db5f406SMauro Carvalho Chehab  USB 3.1 Section 10.12
6063db5f406SMauro Carvalho Chehab
6073db5f406SMauro Carvalho Chehab  wakeup note: if a device is configured to send wakeup events the port
6083db5f406SMauro Carvalho Chehab  power control implementation will block poweroff attempts on that
6093db5f406SMauro Carvalho Chehab  port.
6103db5f406SMauro Carvalho Chehab
6113db5f406SMauro Carvalho Chehab
6123db5f406SMauro Carvalho ChehabUser Interface for Port Power Control
6133db5f406SMauro Carvalho Chehab-------------------------------------
6143db5f406SMauro Carvalho Chehab
6153db5f406SMauro Carvalho ChehabThe port power control mechanism uses the PM runtime system.  Poweroff is
6163db5f406SMauro Carvalho Chehabrequested by clearing the ``power/pm_qos_no_power_off`` flag of the port device
6173db5f406SMauro Carvalho Chehab(defaults to 1).  If the port is disconnected it will immediately receive a
6183db5f406SMauro Carvalho Chehab``ClearPortFeature(PORT_POWER)`` request.  Otherwise, it will honor the pm
6193db5f406SMauro Carvalho Chehabruntime rules and require the attached child device and all descendants to be
6203db5f406SMauro Carvalho Chehabsuspended. This mechanism is dependent on the hub advertising port power
6213db5f406SMauro Carvalho Chehabswitching in its hub descriptor (wHubCharacteristics logical power switching
6223db5f406SMauro Carvalho Chehabmode field).
6233db5f406SMauro Carvalho Chehab
6243db5f406SMauro Carvalho ChehabNote, some interface devices/drivers do not support autosuspend.  Userspace may
6253db5f406SMauro Carvalho Chehabneed to unbind the interface drivers before the :c:type:`usb_device` will
6263db5f406SMauro Carvalho Chehabsuspend.  An unbound interface device is suspended by default.  When unbinding,
6273db5f406SMauro Carvalho Chehabbe careful to unbind interface drivers, not the driver of the parent usb
6283db5f406SMauro Carvalho Chehabdevice.  Also, leave hub interface drivers bound.  If the driver for the usb
6293db5f406SMauro Carvalho Chehabdevice (not interface) is unbound the kernel is no longer able to resume the
6303db5f406SMauro Carvalho Chehabdevice.  If a hub interface driver is unbound, control of its child ports is
6313db5f406SMauro Carvalho Chehablost and all attached child-devices will disconnect.  A good rule of thumb is
6323db5f406SMauro Carvalho Chehabthat if the 'driver/module' link for a device points to
6333db5f406SMauro Carvalho Chehab``/sys/module/usbcore`` then unbinding it will interfere with port power
6343db5f406SMauro Carvalho Chehabcontrol.
6353db5f406SMauro Carvalho Chehab
6363db5f406SMauro Carvalho ChehabExample of the relevant files for port power control.  Note, in this example
6373db5f406SMauro Carvalho Chehabthese files are relative to a usb hub device (prefix)::
6383db5f406SMauro Carvalho Chehab
6393db5f406SMauro Carvalho Chehab     prefix=/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1
6403db5f406SMauro Carvalho Chehab
6413db5f406SMauro Carvalho Chehab                      attached child device +
6423db5f406SMauro Carvalho Chehab                  hub port device +         |
6433db5f406SMauro Carvalho Chehab     hub interface device +       |         |
6443db5f406SMauro Carvalho Chehab                          v       v         v
6453db5f406SMauro Carvalho Chehab                  $prefix/3-1:1.0/3-1-port1/device
6463db5f406SMauro Carvalho Chehab
6473db5f406SMauro Carvalho Chehab     $prefix/3-1:1.0/3-1-port1/power/pm_qos_no_power_off
6483db5f406SMauro Carvalho Chehab     $prefix/3-1:1.0/3-1-port1/device/power/control
6493db5f406SMauro Carvalho Chehab     $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intf0>/driver/unbind
6503db5f406SMauro Carvalho Chehab     $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intf1>/driver/unbind
6513db5f406SMauro Carvalho Chehab     ...
6523db5f406SMauro Carvalho Chehab     $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intfN>/driver/unbind
6533db5f406SMauro Carvalho Chehab
6543db5f406SMauro Carvalho ChehabIn addition to these files some ports may have a 'peer' link to a port on
6553db5f406SMauro Carvalho Chehabanother hub.  The expectation is that all superspeed ports have a
6563db5f406SMauro Carvalho Chehabhi-speed peer::
6573db5f406SMauro Carvalho Chehab
6583db5f406SMauro Carvalho Chehab  $prefix/3-1:1.0/3-1-port1/peer -> ../../../../usb2/2-1/2-1:1.0/2-1-port1
6593db5f406SMauro Carvalho Chehab  ../../../../usb2/2-1/2-1:1.0/2-1-port1/peer -> ../../../../usb3/3-1/3-1:1.0/3-1-port1
6603db5f406SMauro Carvalho Chehab
6613db5f406SMauro Carvalho ChehabDistinct from 'companion ports', or 'ehci/xhci shared switchover ports'
6623db5f406SMauro Carvalho Chehabpeer ports are simply the hi-speed and superspeed interface pins that
6633db5f406SMauro Carvalho Chehabare combined into a single usb3 connector.  Peer ports share the same
6643db5f406SMauro Carvalho Chehabancestor XHCI device.
6653db5f406SMauro Carvalho Chehab
6663db5f406SMauro Carvalho ChehabWhile a superspeed port is powered off a device may downgrade its
6673db5f406SMauro Carvalho Chehabconnection and attempt to connect to the hi-speed pins.  The
6683db5f406SMauro Carvalho Chehabimplementation takes steps to prevent this:
6693db5f406SMauro Carvalho Chehab
6703db5f406SMauro Carvalho Chehab1. Port suspend is sequenced to guarantee that hi-speed ports are powered-off
6713db5f406SMauro Carvalho Chehab   before their superspeed peer is permitted to power-off.  The implication is
6723db5f406SMauro Carvalho Chehab   that the setting ``pm_qos_no_power_off`` to zero on a superspeed port may
6733db5f406SMauro Carvalho Chehab   not cause the port to power-off until its highspeed peer has gone to its
6743db5f406SMauro Carvalho Chehab   runtime suspend state.  Userspace must take care to order the suspensions
6753db5f406SMauro Carvalho Chehab   if it wants to guarantee that a superspeed port will power-off.
6763db5f406SMauro Carvalho Chehab
6773db5f406SMauro Carvalho Chehab2. Port resume is sequenced to force a superspeed port to power-on prior to its
6783db5f406SMauro Carvalho Chehab   highspeed peer.
6793db5f406SMauro Carvalho Chehab
6803db5f406SMauro Carvalho Chehab3. Port resume always triggers an attached child device to resume.  After a
6813db5f406SMauro Carvalho Chehab   power session is lost the device may have been removed, or need reset.
6823db5f406SMauro Carvalho Chehab   Resuming the child device when the parent port regains power resolves those
6833db5f406SMauro Carvalho Chehab   states and clamps the maximum port power cycle frequency at the rate the
6843db5f406SMauro Carvalho Chehab   child device can suspend (autosuspend-delay) and resume (reset-resume
6853db5f406SMauro Carvalho Chehab   latency).
6863db5f406SMauro Carvalho Chehab
6873db5f406SMauro Carvalho ChehabSysfs files relevant for port power control:
6883db5f406SMauro Carvalho Chehab
6893db5f406SMauro Carvalho Chehab	``<hubdev-portX>/power/pm_qos_no_power_off``:
6903db5f406SMauro Carvalho Chehab		This writable flag controls the state of an idle port.
6913db5f406SMauro Carvalho Chehab		Once all children and descendants have suspended the
6923db5f406SMauro Carvalho Chehab		port may suspend/poweroff provided that
6933db5f406SMauro Carvalho Chehab		pm_qos_no_power_off is '0'.  If pm_qos_no_power_off is
6943db5f406SMauro Carvalho Chehab		'1' the port will remain active/powered regardless of
6953db5f406SMauro Carvalho Chehab		the stats of descendants.  Defaults to 1.
6963db5f406SMauro Carvalho Chehab
6973db5f406SMauro Carvalho Chehab	``<hubdev-portX>/power/runtime_status``:
6983db5f406SMauro Carvalho Chehab		This file reflects whether the port is 'active' (power is on)
6993db5f406SMauro Carvalho Chehab		or 'suspended' (logically off).  There is no indication to
7003db5f406SMauro Carvalho Chehab		userspace whether VBUS is still supplied.
7013db5f406SMauro Carvalho Chehab
7023db5f406SMauro Carvalho Chehab	``<hubdev-portX>/connect_type``:
7033db5f406SMauro Carvalho Chehab		An advisory read-only flag to userspace indicating the
7043db5f406SMauro Carvalho Chehab		location and connection type of the port.  It returns
7053db5f406SMauro Carvalho Chehab		one of four values 'hotplug', 'hardwired', 'not used',
7063db5f406SMauro Carvalho Chehab		and 'unknown'.  All values, besides unknown, are set by
7073db5f406SMauro Carvalho Chehab		platform firmware.
7083db5f406SMauro Carvalho Chehab
7093db5f406SMauro Carvalho Chehab		``hotplug`` indicates an externally connectable/visible
7103db5f406SMauro Carvalho Chehab		port on the platform.  Typically userspace would choose
7113db5f406SMauro Carvalho Chehab		to keep such a port powered to handle new device
7123db5f406SMauro Carvalho Chehab		connection events.
7133db5f406SMauro Carvalho Chehab
7143db5f406SMauro Carvalho Chehab		``hardwired`` refers to a port that is not visible but
7153db5f406SMauro Carvalho Chehab		connectable. Examples are internal ports for USB
7163db5f406SMauro Carvalho Chehab		bluetooth that can be disconnected via an external
7173db5f406SMauro Carvalho Chehab		switch or a port with a hardwired USB camera.  It is
7183db5f406SMauro Carvalho Chehab		expected to be safe to allow these ports to suspend
7193db5f406SMauro Carvalho Chehab		provided pm_qos_no_power_off is coordinated with any
7203db5f406SMauro Carvalho Chehab		switch that gates connections.  Userspace must arrange
7213db5f406SMauro Carvalho Chehab		for the device to be connected prior to the port
7223db5f406SMauro Carvalho Chehab		powering off, or to activate the port prior to enabling
7233db5f406SMauro Carvalho Chehab		connection via a switch.
7243db5f406SMauro Carvalho Chehab
7253db5f406SMauro Carvalho Chehab		``not used`` refers to an internal port that is expected
7263db5f406SMauro Carvalho Chehab		to never have a device connected to it.  These may be
7273db5f406SMauro Carvalho Chehab		empty internal ports, or ports that are not physically
7283db5f406SMauro Carvalho Chehab		exposed on a platform.  Considered safe to be
7293db5f406SMauro Carvalho Chehab		powered-off at all times.
7303db5f406SMauro Carvalho Chehab
7313db5f406SMauro Carvalho Chehab		``unknown`` means platform firmware does not provide
7323db5f406SMauro Carvalho Chehab		information for this port.  Most commonly refers to
7333db5f406SMauro Carvalho Chehab		external hub ports which should be considered 'hotplug'
7343db5f406SMauro Carvalho Chehab		for policy decisions.
7353db5f406SMauro Carvalho Chehab
7363db5f406SMauro Carvalho Chehab		.. note::
7373db5f406SMauro Carvalho Chehab
7383db5f406SMauro Carvalho Chehab			- since we are relying on the BIOS to get this ACPI
7393db5f406SMauro Carvalho Chehab			  information correct, the USB port descriptions may
7403db5f406SMauro Carvalho Chehab			  be missing or wrong.
7413db5f406SMauro Carvalho Chehab
7423db5f406SMauro Carvalho Chehab			- Take care in clearing ``pm_qos_no_power_off``. Once
7433db5f406SMauro Carvalho Chehab			  power is off this port will
7443db5f406SMauro Carvalho Chehab			  not respond to new connect events.
7453db5f406SMauro Carvalho Chehab
7463db5f406SMauro Carvalho Chehab	Once a child device is attached additional constraints are
7473db5f406SMauro Carvalho Chehab	applied before the port is allowed to poweroff.
7483db5f406SMauro Carvalho Chehab
7493db5f406SMauro Carvalho Chehab	``<child>/power/control``:
7503db5f406SMauro Carvalho Chehab		Must be ``auto``, and the port will not
7513db5f406SMauro Carvalho Chehab		power down until ``<child>/power/runtime_status``
7523db5f406SMauro Carvalho Chehab		reflects the 'suspended' state.  Default
7533db5f406SMauro Carvalho Chehab		value is controlled by child device driver.
7543db5f406SMauro Carvalho Chehab
7553db5f406SMauro Carvalho Chehab	``<child>/power/persist``:
7563db5f406SMauro Carvalho Chehab		This defaults to ``1`` for most devices and indicates if
7573db5f406SMauro Carvalho Chehab		kernel can persist the device's configuration across a
7583db5f406SMauro Carvalho Chehab		power session loss (suspend / port-power event).  When
7593db5f406SMauro Carvalho Chehab		this value is ``0`` (quirky devices), port poweroff is
7603db5f406SMauro Carvalho Chehab		disabled.
7613db5f406SMauro Carvalho Chehab
7623db5f406SMauro Carvalho Chehab	``<child>/driver/unbind``:
7633db5f406SMauro Carvalho Chehab		Wakeup capable devices will block port poweroff.  At
7643db5f406SMauro Carvalho Chehab		this time the only mechanism to clear the usb-internal
7653db5f406SMauro Carvalho Chehab		wakeup-capability for an interface device is to unbind
7663db5f406SMauro Carvalho Chehab		its driver.
7673db5f406SMauro Carvalho Chehab
7683db5f406SMauro Carvalho ChehabSummary of poweroff pre-requisite settings relative to a port device::
7693db5f406SMauro Carvalho Chehab
7703db5f406SMauro Carvalho Chehab	echo 0 > power/pm_qos_no_power_off
7713db5f406SMauro Carvalho Chehab	echo 0 > peer/power/pm_qos_no_power_off # if it exists
7723db5f406SMauro Carvalho Chehab	echo auto > power/control # this is the default value
7733db5f406SMauro Carvalho Chehab	echo auto > <child>/power/control
7743db5f406SMauro Carvalho Chehab	echo 1 > <child>/power/persist # this is the default value
7753db5f406SMauro Carvalho Chehab
7763db5f406SMauro Carvalho ChehabSuggested Userspace Port Power Policy
7773db5f406SMauro Carvalho Chehab-------------------------------------
7783db5f406SMauro Carvalho Chehab
7793db5f406SMauro Carvalho ChehabAs noted above userspace needs to be careful and deliberate about what
7803db5f406SMauro Carvalho Chehabports are enabled for poweroff.
7813db5f406SMauro Carvalho Chehab
7823db5f406SMauro Carvalho ChehabThe default configuration is that all ports start with
7833db5f406SMauro Carvalho Chehab``power/pm_qos_no_power_off`` set to ``1`` causing ports to always remain
7843db5f406SMauro Carvalho Chehabactive.
7853db5f406SMauro Carvalho Chehab
7863db5f406SMauro Carvalho ChehabGiven confidence in the platform firmware's description of the ports
7873db5f406SMauro Carvalho Chehab(ACPI _PLD record for a port populates 'connect_type') userspace can
7883db5f406SMauro Carvalho Chehabclear pm_qos_no_power_off for all 'not used' ports.  The same can be
7893db5f406SMauro Carvalho Chehabdone for 'hardwired' ports provided poweroff is coordinated with any
7903db5f406SMauro Carvalho Chehabconnection switch for the port.
7913db5f406SMauro Carvalho Chehab
7923db5f406SMauro Carvalho ChehabA more aggressive userspace policy is to enable USB port power off for
7933db5f406SMauro Carvalho Chehaball ports (set ``<hubdev-portX>/power/pm_qos_no_power_off`` to ``0``) when
7943db5f406SMauro Carvalho Chehabsome external factor indicates the user has stopped interacting with the
7953db5f406SMauro Carvalho Chehabsystem.  For example, a distro may want to enable power off all USB
7963db5f406SMauro Carvalho Chehabports when the screen blanks, and re-power them when the screen becomes
7973db5f406SMauro Carvalho Chehabactive.  Smart phones and tablets may want to power off USB ports when
7983db5f406SMauro Carvalho Chehabthe user pushes the power button.
799