162306a36Sopenharmony_ci.. _usb-power-management: 262306a36Sopenharmony_ci 362306a36Sopenharmony_ciPower Management for USB 462306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci:Author: Alan Stern <stern@rowland.harvard.edu> 762306a36Sopenharmony_ci:Date: Last-updated: February 2014 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci.. 1062306a36Sopenharmony_ci Contents: 1162306a36Sopenharmony_ci --------- 1262306a36Sopenharmony_ci * What is Power Management? 1362306a36Sopenharmony_ci * What is Remote Wakeup? 1462306a36Sopenharmony_ci * When is a USB device idle? 1562306a36Sopenharmony_ci * Forms of dynamic PM 1662306a36Sopenharmony_ci * The user interface for dynamic PM 1762306a36Sopenharmony_ci * Changing the default idle-delay time 1862306a36Sopenharmony_ci * Warnings 1962306a36Sopenharmony_ci * The driver interface for Power Management 2062306a36Sopenharmony_ci * The driver interface for autosuspend and autoresume 2162306a36Sopenharmony_ci * Other parts of the driver interface 2262306a36Sopenharmony_ci * Mutual exclusion 2362306a36Sopenharmony_ci * Interaction between dynamic PM and system PM 2462306a36Sopenharmony_ci * xHCI hardware link PM 2562306a36Sopenharmony_ci * USB Port Power Control 2662306a36Sopenharmony_ci * User Interface for Port Power Control 2762306a36Sopenharmony_ci * Suggested Userspace Port Power Policy 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ciWhat is Power Management? 3162306a36Sopenharmony_ci------------------------- 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ciPower Management (PM) is the practice of saving energy by suspending 3462306a36Sopenharmony_ciparts of a computer system when they aren't being used. While a 3562306a36Sopenharmony_cicomponent is ``suspended`` it is in a nonfunctional low-power state; it 3662306a36Sopenharmony_cimight even be turned off completely. A suspended component can be 3762306a36Sopenharmony_ci``resumed`` (returned to a functional full-power state) when the kernel 3862306a36Sopenharmony_cineeds to use it. (There also are forms of PM in which components are 3962306a36Sopenharmony_ciplaced in a less functional but still usable state instead of being 4062306a36Sopenharmony_cisuspended; an example would be reducing the CPU's clock rate. This 4162306a36Sopenharmony_cidocument will not discuss those other forms.) 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ciWhen the parts being suspended include the CPU and most of the rest of 4462306a36Sopenharmony_cithe system, we speak of it as a "system suspend". When a particular 4562306a36Sopenharmony_cidevice is turned off while the system as a whole remains running, we 4662306a36Sopenharmony_cicall it a "dynamic suspend" (also known as a "runtime suspend" or 4762306a36Sopenharmony_ci"selective suspend"). This document concentrates mostly on how 4862306a36Sopenharmony_cidynamic PM is implemented in the USB subsystem, although system PM is 4962306a36Sopenharmony_cicovered to some extent (see ``Documentation/power/*.rst`` for more 5062306a36Sopenharmony_ciinformation about system PM). 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ciSystem PM support is present only if the kernel was built with 5362306a36Sopenharmony_ci``CONFIG_SUSPEND`` or ``CONFIG_HIBERNATION`` enabled. Dynamic PM support 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_cifor USB is present whenever 5662306a36Sopenharmony_cithe kernel was built with ``CONFIG_PM`` enabled. 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci[Historically, dynamic PM support for USB was present only if the 5962306a36Sopenharmony_cikernel had been built with ``CONFIG_USB_SUSPEND`` enabled (which depended on 6062306a36Sopenharmony_ci``CONFIG_PM_RUNTIME``). Starting with the 3.10 kernel release, dynamic PM 6162306a36Sopenharmony_cisupport for USB was present whenever the kernel was built with 6262306a36Sopenharmony_ci``CONFIG_PM_RUNTIME`` enabled. The ``CONFIG_USB_SUSPEND`` option had been 6362306a36Sopenharmony_cieliminated.] 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ciWhat is Remote Wakeup? 6762306a36Sopenharmony_ci---------------------- 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ciWhen a device has been suspended, it generally doesn't resume until 7062306a36Sopenharmony_cithe computer tells it to. Likewise, if the entire computer has been 7162306a36Sopenharmony_cisuspended, it generally doesn't resume until the user tells it to, say 7262306a36Sopenharmony_ciby pressing a power button or opening the cover. 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ciHowever some devices have the capability of resuming by themselves, or 7562306a36Sopenharmony_ciasking the kernel to resume them, or even telling the entire computer 7662306a36Sopenharmony_cito resume. This capability goes by several names such as "Wake On 7762306a36Sopenharmony_ciLAN"; we will refer to it generically as "remote wakeup". When a 7862306a36Sopenharmony_cidevice is enabled for remote wakeup and it is suspended, it may resume 7962306a36Sopenharmony_ciitself (or send a request to be resumed) in response to some external 8062306a36Sopenharmony_cievent. Examples include a suspended keyboard resuming when a key is 8162306a36Sopenharmony_cipressed, or a suspended USB hub resuming when a device is plugged in. 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ciWhen is a USB device idle? 8562306a36Sopenharmony_ci-------------------------- 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ciA device is idle whenever the kernel thinks it's not busy doing 8862306a36Sopenharmony_cianything important and thus is a candidate for being suspended. The 8962306a36Sopenharmony_ciexact definition depends on the device's driver; drivers are allowed 9062306a36Sopenharmony_cito declare that a device isn't idle even when there's no actual 9162306a36Sopenharmony_cicommunication taking place. (For example, a hub isn't considered idle 9262306a36Sopenharmony_ciunless all the devices plugged into that hub are already suspended.) 9362306a36Sopenharmony_ciIn addition, a device isn't considered idle so long as a program keeps 9462306a36Sopenharmony_ciits usbfs file open, whether or not any I/O is going on. 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ciIf a USB device has no driver, its usbfs file isn't open, and it isn't 9762306a36Sopenharmony_cibeing accessed through sysfs, then it definitely is idle. 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ciForms of dynamic PM 10162306a36Sopenharmony_ci------------------- 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ciDynamic suspends occur when the kernel decides to suspend an idle 10462306a36Sopenharmony_cidevice. This is called ``autosuspend`` for short. In general, a device 10562306a36Sopenharmony_ciwon't be autosuspended unless it has been idle for some minimum period 10662306a36Sopenharmony_ciof time, the so-called idle-delay time. 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ciOf course, nothing the kernel does on its own initiative should 10962306a36Sopenharmony_ciprevent the computer or its devices from working properly. If a 11062306a36Sopenharmony_cidevice has been autosuspended and a program tries to use it, the 11162306a36Sopenharmony_cikernel will automatically resume the device (autoresume). For the 11262306a36Sopenharmony_cisame reason, an autosuspended device will usually have remote wakeup 11362306a36Sopenharmony_cienabled, if the device supports remote wakeup. 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ciIt is worth mentioning that many USB drivers don't support 11662306a36Sopenharmony_ciautosuspend. In fact, at the time of this writing (Linux 2.6.23) the 11762306a36Sopenharmony_cionly drivers which do support it are the hub driver, kaweth, asix, 11862306a36Sopenharmony_ciusblp, usblcd, and usb-skeleton (which doesn't count). If a 11962306a36Sopenharmony_cinon-supporting driver is bound to a device, the device won't be 12062306a36Sopenharmony_ciautosuspended. In effect, the kernel pretends the device is never 12162306a36Sopenharmony_ciidle. 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ciWe can categorize power management events in two broad classes: 12462306a36Sopenharmony_ciexternal and internal. External events are those triggered by some 12562306a36Sopenharmony_ciagent outside the USB stack: system suspend/resume (triggered by 12662306a36Sopenharmony_ciuserspace), manual dynamic resume (also triggered by userspace), and 12762306a36Sopenharmony_ciremote wakeup (triggered by the device). Internal events are those 12862306a36Sopenharmony_citriggered within the USB stack: autosuspend and autoresume. Note that 12962306a36Sopenharmony_ciall dynamic suspend events are internal; external agents are not 13062306a36Sopenharmony_ciallowed to issue dynamic suspends. 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ciThe user interface for dynamic PM 13462306a36Sopenharmony_ci--------------------------------- 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ciThe user interface for controlling dynamic PM is located in the ``power/`` 13762306a36Sopenharmony_cisubdirectory of each USB device's sysfs directory, that is, in 13862306a36Sopenharmony_ci``/sys/bus/usb/devices/.../power/`` where "..." is the device's ID. The 13962306a36Sopenharmony_cirelevant attribute files are: wakeup, control, and 14062306a36Sopenharmony_ci``autosuspend_delay_ms``. (There may also be a file named ``level``; this 14162306a36Sopenharmony_cifile was deprecated as of the 2.6.35 kernel and replaced by the 14262306a36Sopenharmony_ci``control`` file. In 2.6.38 the ``autosuspend`` file will be deprecated 14362306a36Sopenharmony_ciand replaced by the ``autosuspend_delay_ms`` file. The only difference 14462306a36Sopenharmony_ciis that the newer file expresses the delay in milliseconds whereas the 14562306a36Sopenharmony_ciolder file uses seconds. Confusingly, both files are present in 2.6.37 14662306a36Sopenharmony_cibut only ``autosuspend`` works.) 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci ``power/wakeup`` 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci This file is empty if the device does not support 15162306a36Sopenharmony_ci remote wakeup. Otherwise the file contains either the 15262306a36Sopenharmony_ci word ``enabled`` or the word ``disabled``, and you can 15362306a36Sopenharmony_ci write those words to the file. The setting determines 15462306a36Sopenharmony_ci whether or not remote wakeup will be enabled when the 15562306a36Sopenharmony_ci device is next suspended. (If the setting is changed 15662306a36Sopenharmony_ci while the device is suspended, the change won't take 15762306a36Sopenharmony_ci effect until the following suspend.) 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci ``power/control`` 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci This file contains one of two words: ``on`` or ``auto``. 16262306a36Sopenharmony_ci You can write those words to the file to change the 16362306a36Sopenharmony_ci device's setting. 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci - ``on`` means that the device should be resumed and 16662306a36Sopenharmony_ci autosuspend is not allowed. (Of course, system 16762306a36Sopenharmony_ci suspends are still allowed.) 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci - ``auto`` is the normal state in which the kernel is 17062306a36Sopenharmony_ci allowed to autosuspend and autoresume the device. 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci (In kernels up to 2.6.32, you could also specify 17362306a36Sopenharmony_ci ``suspend``, meaning that the device should remain 17462306a36Sopenharmony_ci suspended and autoresume was not allowed. This 17562306a36Sopenharmony_ci setting is no longer supported.) 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci ``power/autosuspend_delay_ms`` 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci This file contains an integer value, which is the 18062306a36Sopenharmony_ci number of milliseconds the device should remain idle 18162306a36Sopenharmony_ci before the kernel will autosuspend it (the idle-delay 18262306a36Sopenharmony_ci time). The default is 2000. 0 means to autosuspend 18362306a36Sopenharmony_ci as soon as the device becomes idle, and negative 18462306a36Sopenharmony_ci values mean never to autosuspend. You can write a 18562306a36Sopenharmony_ci number to the file to change the autosuspend 18662306a36Sopenharmony_ci idle-delay time. 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ciWriting ``-1`` to ``power/autosuspend_delay_ms`` and writing ``on`` to 18962306a36Sopenharmony_ci``power/control`` do essentially the same thing -- they both prevent the 19062306a36Sopenharmony_cidevice from being autosuspended. Yes, this is a redundancy in the 19162306a36Sopenharmony_ciAPI. 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci(In 2.6.21 writing ``0`` to ``power/autosuspend`` would prevent the device 19462306a36Sopenharmony_cifrom being autosuspended; the behavior was changed in 2.6.22. The 19562306a36Sopenharmony_ci``power/autosuspend`` attribute did not exist prior to 2.6.21, and the 19662306a36Sopenharmony_ci``power/level`` attribute did not exist prior to 2.6.22. ``power/control`` 19762306a36Sopenharmony_ciwas added in 2.6.34, and ``power/autosuspend_delay_ms`` was added in 19862306a36Sopenharmony_ci2.6.37 but did not become functional until 2.6.38.) 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ciChanging the default idle-delay time 20262306a36Sopenharmony_ci------------------------------------ 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ciThe default autosuspend idle-delay time (in seconds) is controlled by 20562306a36Sopenharmony_cia module parameter in usbcore. You can specify the value when usbcore 20662306a36Sopenharmony_ciis loaded. For example, to set it to 5 seconds instead of 2 you would 20762306a36Sopenharmony_cido:: 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci modprobe usbcore autosuspend=5 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ciEquivalently, you could add to a configuration file in /etc/modprobe.d 21262306a36Sopenharmony_cia line saying:: 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci options usbcore autosuspend=5 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ciSome distributions load the usbcore module very early during the boot 21762306a36Sopenharmony_ciprocess, by means of a program or script running from an initramfs 21862306a36Sopenharmony_ciimage. To alter the parameter value you would have to rebuild that 21962306a36Sopenharmony_ciimage. 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ciIf usbcore is compiled into the kernel rather than built as a loadable 22262306a36Sopenharmony_cimodule, you can add:: 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci usbcore.autosuspend=5 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_cito the kernel's boot command line. 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ciFinally, the parameter value can be changed while the system is 22962306a36Sopenharmony_cirunning. If you do:: 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci echo 5 >/sys/module/usbcore/parameters/autosuspend 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_cithen each new USB device will have its autosuspend idle-delay 23462306a36Sopenharmony_ciinitialized to 5. (The idle-delay values for already existing devices 23562306a36Sopenharmony_ciwill not be affected.) 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ciSetting the initial default idle-delay to -1 will prevent any 23862306a36Sopenharmony_ciautosuspend of any USB device. This has the benefit of allowing you 23962306a36Sopenharmony_cithen to enable autosuspend for selected devices. 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ciWarnings 24362306a36Sopenharmony_ci-------- 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ciThe USB specification states that all USB devices must support power 24662306a36Sopenharmony_cimanagement. Nevertheless, the sad fact is that many devices do not 24762306a36Sopenharmony_cisupport it very well. You can suspend them all right, but when you 24862306a36Sopenharmony_citry to resume them they disconnect themselves from the USB bus or 24962306a36Sopenharmony_cithey stop working entirely. This seems to be especially prevalent 25062306a36Sopenharmony_ciamong printers and scanners, but plenty of other types of device have 25162306a36Sopenharmony_cithe same deficiency. 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ciFor this reason, by default the kernel disables autosuspend (the 25462306a36Sopenharmony_ci``power/control`` attribute is initialized to ``on``) for all devices other 25562306a36Sopenharmony_cithan hubs. Hubs, at least, appear to be reasonably well-behaved in 25662306a36Sopenharmony_cithis regard. 25762306a36Sopenharmony_ci 25862306a36Sopenharmony_ci(In 2.6.21 and 2.6.22 this wasn't the case. Autosuspend was enabled 25962306a36Sopenharmony_ciby default for almost all USB devices. A number of people experienced 26062306a36Sopenharmony_ciproblems as a result.) 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ciThis means that non-hub devices won't be autosuspended unless the user 26362306a36Sopenharmony_cior a program explicitly enables it. As of this writing there aren't 26462306a36Sopenharmony_ciany widespread programs which will do this; we hope that in the near 26562306a36Sopenharmony_cifuture device managers such as HAL will take on this added 26662306a36Sopenharmony_ciresponsibility. In the meantime you can always carry out the 26762306a36Sopenharmony_cinecessary operations by hand or add them to a udev script. You can 26862306a36Sopenharmony_cialso change the idle-delay time; 2 seconds is not the best choice for 26962306a36Sopenharmony_cievery device. 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ciIf a driver knows that its device has proper suspend/resume support, 27262306a36Sopenharmony_ciit can enable autosuspend all by itself. For example, the video 27362306a36Sopenharmony_cidriver for a laptop's webcam might do this (in recent kernels they 27462306a36Sopenharmony_cido), since these devices are rarely used and so should normally be 27562306a36Sopenharmony_ciautosuspended. 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_ciSometimes it turns out that even when a device does work okay with 27862306a36Sopenharmony_ciautosuspend there are still problems. For example, the usbhid driver, 27962306a36Sopenharmony_ciwhich manages keyboards and mice, has autosuspend support. Tests with 28062306a36Sopenharmony_cia number of keyboards show that typing on a suspended keyboard, while 28162306a36Sopenharmony_cicausing the keyboard to do a remote wakeup all right, will nonetheless 28262306a36Sopenharmony_cifrequently result in lost keystrokes. Tests with mice show that some 28362306a36Sopenharmony_ciof them will issue a remote-wakeup request in response to button 28462306a36Sopenharmony_cipresses but not to motion, and some in response to neither. 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ciThe kernel will not prevent you from enabling autosuspend on devices 28762306a36Sopenharmony_cithat can't handle it. It is even possible in theory to damage a 28862306a36Sopenharmony_cidevice by suspending it at the wrong time. (Highly unlikely, but 28962306a36Sopenharmony_cipossible.) Take care. 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ciThe driver interface for Power Management 29362306a36Sopenharmony_ci----------------------------------------- 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ciThe requirements for a USB driver to support external power management 29662306a36Sopenharmony_ciare pretty modest; the driver need only define:: 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci .suspend 29962306a36Sopenharmony_ci .resume 30062306a36Sopenharmony_ci .reset_resume 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_cimethods in its :c:type:`usb_driver` structure, and the ``reset_resume`` method 30362306a36Sopenharmony_ciis optional. The methods' jobs are quite simple: 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ci - The ``suspend`` method is called to warn the driver that the 30662306a36Sopenharmony_ci device is going to be suspended. If the driver returns a 30762306a36Sopenharmony_ci negative error code, the suspend will be aborted. Normally 30862306a36Sopenharmony_ci the driver will return 0, in which case it must cancel all 30962306a36Sopenharmony_ci outstanding URBs (:c:func:`usb_kill_urb`) and not submit any more. 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci - The ``resume`` method is called to tell the driver that the 31262306a36Sopenharmony_ci device has been resumed and the driver can return to normal 31362306a36Sopenharmony_ci operation. URBs may once more be submitted. 31462306a36Sopenharmony_ci 31562306a36Sopenharmony_ci - The ``reset_resume`` method is called to tell the driver that 31662306a36Sopenharmony_ci the device has been resumed and it also has been reset. 31762306a36Sopenharmony_ci The driver should redo any necessary device initialization, 31862306a36Sopenharmony_ci since the device has probably lost most or all of its state 31962306a36Sopenharmony_ci (although the interfaces will be in the same altsettings as 32062306a36Sopenharmony_ci before the suspend). 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_ciIf the device is disconnected or powered down while it is suspended, 32362306a36Sopenharmony_cithe ``disconnect`` method will be called instead of the ``resume`` or 32462306a36Sopenharmony_ci``reset_resume`` method. This is also quite likely to happen when 32562306a36Sopenharmony_ciwaking up from hibernation, as many systems do not maintain suspend 32662306a36Sopenharmony_cicurrent to the USB host controllers during hibernation. (It's 32762306a36Sopenharmony_cipossible to work around the hibernation-forces-disconnect problem by 32862306a36Sopenharmony_ciusing the USB Persist facility.) 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ciThe ``reset_resume`` method is used by the USB Persist facility (see 33162306a36Sopenharmony_ci:ref:`usb-persist`) and it can also be used under certain 33262306a36Sopenharmony_cicircumstances when ``CONFIG_USB_PERSIST`` is not enabled. Currently, if a 33362306a36Sopenharmony_cidevice is reset during a resume and the driver does not have a 33462306a36Sopenharmony_ci``reset_resume`` method, the driver won't receive any notification about 33562306a36Sopenharmony_cithe resume. Later kernels will call the driver's ``disconnect`` method; 33662306a36Sopenharmony_ci2.6.23 doesn't do this. 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ciUSB drivers are bound to interfaces, so their ``suspend`` and ``resume`` 33962306a36Sopenharmony_cimethods get called when the interfaces are suspended or resumed. In 34062306a36Sopenharmony_ciprinciple one might want to suspend some interfaces on a device (i.e., 34162306a36Sopenharmony_ciforce the drivers for those interface to stop all activity) without 34262306a36Sopenharmony_cisuspending the other interfaces. The USB core doesn't allow this; all 34362306a36Sopenharmony_ciinterfaces are suspended when the device itself is suspended and all 34462306a36Sopenharmony_ciinterfaces are resumed when the device is resumed. It isn't possible 34562306a36Sopenharmony_cito suspend or resume some but not all of a device's interfaces. The 34662306a36Sopenharmony_ciclosest you can come is to unbind the interfaces' drivers. 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ciThe driver interface for autosuspend and autoresume 35062306a36Sopenharmony_ci--------------------------------------------------- 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_ciTo support autosuspend and autoresume, a driver should implement all 35362306a36Sopenharmony_cithree of the methods listed above. In addition, a driver indicates 35462306a36Sopenharmony_cithat it supports autosuspend by setting the ``.supports_autosuspend`` flag 35562306a36Sopenharmony_ciin its usb_driver structure. It is then responsible for informing the 35662306a36Sopenharmony_ciUSB core whenever one of its interfaces becomes busy or idle. The 35762306a36Sopenharmony_cidriver does so by calling these six functions:: 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci int usb_autopm_get_interface(struct usb_interface *intf); 36062306a36Sopenharmony_ci void usb_autopm_put_interface(struct usb_interface *intf); 36162306a36Sopenharmony_ci int usb_autopm_get_interface_async(struct usb_interface *intf); 36262306a36Sopenharmony_ci void usb_autopm_put_interface_async(struct usb_interface *intf); 36362306a36Sopenharmony_ci void usb_autopm_get_interface_no_resume(struct usb_interface *intf); 36462306a36Sopenharmony_ci void usb_autopm_put_interface_no_suspend(struct usb_interface *intf); 36562306a36Sopenharmony_ci 36662306a36Sopenharmony_ciThe functions work by maintaining a usage counter in the 36762306a36Sopenharmony_ciusb_interface's embedded device structure. When the counter is > 0 36862306a36Sopenharmony_cithen the interface is deemed to be busy, and the kernel will not 36962306a36Sopenharmony_ciautosuspend the interface's device. When the usage counter is = 0 37062306a36Sopenharmony_cithen the interface is considered to be idle, and the kernel may 37162306a36Sopenharmony_ciautosuspend the device. 37262306a36Sopenharmony_ci 37362306a36Sopenharmony_ciDrivers must be careful to balance their overall changes to the usage 37462306a36Sopenharmony_cicounter. Unbalanced "get"s will remain in effect when a driver is 37562306a36Sopenharmony_ciunbound from its interface, preventing the device from going into 37662306a36Sopenharmony_ciruntime suspend should the interface be bound to a driver again. On 37762306a36Sopenharmony_cithe other hand, drivers are allowed to achieve this balance by calling 37862306a36Sopenharmony_cithe ``usb_autopm_*`` functions even after their ``disconnect`` routine 37962306a36Sopenharmony_cihas returned -- say from within a work-queue routine -- provided they 38062306a36Sopenharmony_ciretain an active reference to the interface (via ``usb_get_intf`` and 38162306a36Sopenharmony_ci``usb_put_intf``). 38262306a36Sopenharmony_ci 38362306a36Sopenharmony_ciDrivers using the async routines are responsible for their own 38462306a36Sopenharmony_cisynchronization and mutual exclusion. 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_ci :c:func:`usb_autopm_get_interface` increments the usage counter and 38762306a36Sopenharmony_ci does an autoresume if the device is suspended. If the 38862306a36Sopenharmony_ci autoresume fails, the counter is decremented back. 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci :c:func:`usb_autopm_put_interface` decrements the usage counter and 39162306a36Sopenharmony_ci attempts an autosuspend if the new value is = 0. 39262306a36Sopenharmony_ci 39362306a36Sopenharmony_ci :c:func:`usb_autopm_get_interface_async` and 39462306a36Sopenharmony_ci :c:func:`usb_autopm_put_interface_async` do almost the same things as 39562306a36Sopenharmony_ci their non-async counterparts. The big difference is that they 39662306a36Sopenharmony_ci use a workqueue to do the resume or suspend part of their 39762306a36Sopenharmony_ci jobs. As a result they can be called in an atomic context, 39862306a36Sopenharmony_ci such as an URB's completion handler, but when they return the 39962306a36Sopenharmony_ci device will generally not yet be in the desired state. 40062306a36Sopenharmony_ci 40162306a36Sopenharmony_ci :c:func:`usb_autopm_get_interface_no_resume` and 40262306a36Sopenharmony_ci :c:func:`usb_autopm_put_interface_no_suspend` merely increment or 40362306a36Sopenharmony_ci decrement the usage counter; they do not attempt to carry out 40462306a36Sopenharmony_ci an autoresume or an autosuspend. Hence they can be called in 40562306a36Sopenharmony_ci an atomic context. 40662306a36Sopenharmony_ci 40762306a36Sopenharmony_ciThe simplest usage pattern is that a driver calls 40862306a36Sopenharmony_ci:c:func:`usb_autopm_get_interface` in its open routine and 40962306a36Sopenharmony_ci:c:func:`usb_autopm_put_interface` in its close or release routine. But other 41062306a36Sopenharmony_cipatterns are possible. 41162306a36Sopenharmony_ci 41262306a36Sopenharmony_ciThe autosuspend attempts mentioned above will often fail for one 41362306a36Sopenharmony_cireason or another. For example, the ``power/control`` attribute might be 41462306a36Sopenharmony_ciset to ``on``, or another interface in the same device might not be 41562306a36Sopenharmony_ciidle. This is perfectly normal. If the reason for failure was that 41662306a36Sopenharmony_cithe device hasn't been idle for long enough, a timer is scheduled to 41762306a36Sopenharmony_cicarry out the operation automatically when the autosuspend idle-delay 41862306a36Sopenharmony_cihas expired. 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ciAutoresume attempts also can fail, although failure would mean that 42162306a36Sopenharmony_cithe device is no longer present or operating properly. Unlike 42262306a36Sopenharmony_ciautosuspend, there's no idle-delay for an autoresume. 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_ci 42562306a36Sopenharmony_ciOther parts of the driver interface 42662306a36Sopenharmony_ci----------------------------------- 42762306a36Sopenharmony_ci 42862306a36Sopenharmony_ciDrivers can enable autosuspend for their devices by calling:: 42962306a36Sopenharmony_ci 43062306a36Sopenharmony_ci usb_enable_autosuspend(struct usb_device *udev); 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ciin their :c:func:`probe` routine, if they know that the device is capable of 43362306a36Sopenharmony_cisuspending and resuming correctly. This is exactly equivalent to 43462306a36Sopenharmony_ciwriting ``auto`` to the device's ``power/control`` attribute. Likewise, 43562306a36Sopenharmony_cidrivers can disable autosuspend by calling:: 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_ci usb_disable_autosuspend(struct usb_device *udev); 43862306a36Sopenharmony_ci 43962306a36Sopenharmony_ciThis is exactly the same as writing ``on`` to the ``power/control`` attribute. 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ciSometimes a driver needs to make sure that remote wakeup is enabled 44262306a36Sopenharmony_ciduring autosuspend. For example, there's not much point 44362306a36Sopenharmony_ciautosuspending a keyboard if the user can't cause the keyboard to do a 44462306a36Sopenharmony_ciremote wakeup by typing on it. If the driver sets 44562306a36Sopenharmony_ci``intf->needs_remote_wakeup`` to 1, the kernel won't autosuspend the 44662306a36Sopenharmony_cidevice if remote wakeup isn't available. (If the device is already 44762306a36Sopenharmony_ciautosuspended, though, setting this flag won't cause the kernel to 44862306a36Sopenharmony_ciautoresume it. Normally a driver would set this flag in its ``probe`` 44962306a36Sopenharmony_cimethod, at which time the device is guaranteed not to be 45062306a36Sopenharmony_ciautosuspended.) 45162306a36Sopenharmony_ci 45262306a36Sopenharmony_ciIf a driver does its I/O asynchronously in interrupt context, it 45362306a36Sopenharmony_cishould call :c:func:`usb_autopm_get_interface_async` before starting output and 45462306a36Sopenharmony_ci:c:func:`usb_autopm_put_interface_async` when the output queue drains. When 45562306a36Sopenharmony_ciit receives an input event, it should call:: 45662306a36Sopenharmony_ci 45762306a36Sopenharmony_ci usb_mark_last_busy(struct usb_device *udev); 45862306a36Sopenharmony_ci 45962306a36Sopenharmony_ciin the event handler. This tells the PM core that the device was just 46062306a36Sopenharmony_cibusy and therefore the next autosuspend idle-delay expiration should 46162306a36Sopenharmony_cibe pushed back. Many of the usb_autopm_* routines also make this call, 46262306a36Sopenharmony_ciso drivers need to worry only when interrupt-driven input arrives. 46362306a36Sopenharmony_ci 46462306a36Sopenharmony_ciAsynchronous operation is always subject to races. For example, a 46562306a36Sopenharmony_cidriver may call the :c:func:`usb_autopm_get_interface_async` routine at a time 46662306a36Sopenharmony_ciwhen the core has just finished deciding the device has been idle for 46762306a36Sopenharmony_cilong enough but not yet gotten around to calling the driver's ``suspend`` 46862306a36Sopenharmony_cimethod. The ``suspend`` method must be responsible for synchronizing with 46962306a36Sopenharmony_cithe I/O request routine and the URB completion handler; it should 47062306a36Sopenharmony_cicause autosuspends to fail with -EBUSY if the driver needs to use the 47162306a36Sopenharmony_cidevice. 47262306a36Sopenharmony_ci 47362306a36Sopenharmony_ciExternal suspend calls should never be allowed to fail in this way, 47462306a36Sopenharmony_cionly autosuspend calls. The driver can tell them apart by applying 47562306a36Sopenharmony_cithe :c:func:`PMSG_IS_AUTO` macro to the message argument to the ``suspend`` 47662306a36Sopenharmony_cimethod; it will return True for internal PM events (autosuspend) and 47762306a36Sopenharmony_ciFalse for external PM events. 47862306a36Sopenharmony_ci 47962306a36Sopenharmony_ci 48062306a36Sopenharmony_ciMutual exclusion 48162306a36Sopenharmony_ci---------------- 48262306a36Sopenharmony_ci 48362306a36Sopenharmony_ciFor external events -- but not necessarily for autosuspend or 48462306a36Sopenharmony_ciautoresume -- the device semaphore (udev->dev.sem) will be held when a 48562306a36Sopenharmony_ci``suspend`` or ``resume`` method is called. This implies that external 48662306a36Sopenharmony_cisuspend/resume events are mutually exclusive with calls to ``probe``, 48762306a36Sopenharmony_ci``disconnect``, ``pre_reset``, and ``post_reset``; the USB core guarantees that 48862306a36Sopenharmony_cithis is true of autosuspend/autoresume events as well. 48962306a36Sopenharmony_ci 49062306a36Sopenharmony_ciIf a driver wants to block all suspend/resume calls during some 49162306a36Sopenharmony_cicritical section, the best way is to lock the device and call 49262306a36Sopenharmony_ci:c:func:`usb_autopm_get_interface` (and do the reverse at the end of the 49362306a36Sopenharmony_cicritical section). Holding the device semaphore will block all 49462306a36Sopenharmony_ciexternal PM calls, and the :c:func:`usb_autopm_get_interface` will prevent any 49562306a36Sopenharmony_ciinternal PM calls, even if it fails. (Exercise: Why?) 49662306a36Sopenharmony_ci 49762306a36Sopenharmony_ci 49862306a36Sopenharmony_ciInteraction between dynamic PM and system PM 49962306a36Sopenharmony_ci-------------------------------------------- 50062306a36Sopenharmony_ci 50162306a36Sopenharmony_ciDynamic power management and system power management can interact in 50262306a36Sopenharmony_cia couple of ways. 50362306a36Sopenharmony_ci 50462306a36Sopenharmony_ciFirstly, a device may already be autosuspended when a system suspend 50562306a36Sopenharmony_cioccurs. Since system suspends are supposed to be as transparent as 50662306a36Sopenharmony_cipossible, the device should remain suspended following the system 50762306a36Sopenharmony_ciresume. But this theory may not work out well in practice; over time 50862306a36Sopenharmony_cithe kernel's behavior in this regard has changed. As of 2.6.37 the 50962306a36Sopenharmony_cipolicy is to resume all devices during a system resume and let them 51062306a36Sopenharmony_cihandle their own runtime suspends afterward. 51162306a36Sopenharmony_ci 51262306a36Sopenharmony_ciSecondly, a dynamic power-management event may occur as a system 51362306a36Sopenharmony_cisuspend is underway. The window for this is short, since system 51462306a36Sopenharmony_cisuspends don't take long (a few seconds usually), but it can happen. 51562306a36Sopenharmony_ciFor example, a suspended device may send a remote-wakeup signal while 51662306a36Sopenharmony_cithe system is suspending. The remote wakeup may succeed, which would 51762306a36Sopenharmony_cicause the system suspend to abort. If the remote wakeup doesn't 51862306a36Sopenharmony_cisucceed, it may still remain active and thus cause the system to 51962306a36Sopenharmony_ciresume as soon as the system suspend is complete. Or the remote 52062306a36Sopenharmony_ciwakeup may fail and get lost. Which outcome occurs depends on timing 52162306a36Sopenharmony_ciand on the hardware and firmware design. 52262306a36Sopenharmony_ci 52362306a36Sopenharmony_ci 52462306a36Sopenharmony_cixHCI hardware link PM 52562306a36Sopenharmony_ci--------------------- 52662306a36Sopenharmony_ci 52762306a36Sopenharmony_cixHCI host controller provides hardware link power management to usb2.0 52862306a36Sopenharmony_ci(xHCI 1.0 feature) and usb3.0 devices which support link PM. By 52962306a36Sopenharmony_cienabling hardware LPM, the host can automatically put the device into 53062306a36Sopenharmony_cilower power state(L1 for usb2.0 devices, or U1/U2 for usb3.0 devices), 53162306a36Sopenharmony_ciwhich state device can enter and resume very quickly. 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ciThe user interface for controlling hardware LPM is located in the 53462306a36Sopenharmony_ci``power/`` subdirectory of each USB device's sysfs directory, that is, in 53562306a36Sopenharmony_ci``/sys/bus/usb/devices/.../power/`` where "..." is the device's ID. The 53662306a36Sopenharmony_cirelevant attribute files are ``usb2_hardware_lpm`` and ``usb3_hardware_lpm``. 53762306a36Sopenharmony_ci 53862306a36Sopenharmony_ci ``power/usb2_hardware_lpm`` 53962306a36Sopenharmony_ci 54062306a36Sopenharmony_ci When a USB2 device which support LPM is plugged to a 54162306a36Sopenharmony_ci xHCI host root hub which support software LPM, the 54262306a36Sopenharmony_ci host will run a software LPM test for it; if the device 54362306a36Sopenharmony_ci enters L1 state and resume successfully and the host 54462306a36Sopenharmony_ci supports USB2 hardware LPM, this file will show up and 54562306a36Sopenharmony_ci driver will enable hardware LPM for the device. You 54662306a36Sopenharmony_ci can write y/Y/1 or n/N/0 to the file to enable/disable 54762306a36Sopenharmony_ci USB2 hardware LPM manually. This is for test purpose mainly. 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci ``power/usb3_hardware_lpm_u1`` 55062306a36Sopenharmony_ci ``power/usb3_hardware_lpm_u2`` 55162306a36Sopenharmony_ci 55262306a36Sopenharmony_ci When a USB 3.0 lpm-capable device is plugged in to a 55362306a36Sopenharmony_ci xHCI host which supports link PM, it will check if U1 55462306a36Sopenharmony_ci and U2 exit latencies have been set in the BOS 55562306a36Sopenharmony_ci descriptor; if the check is passed and the host 55662306a36Sopenharmony_ci supports USB3 hardware LPM, USB3 hardware LPM will be 55762306a36Sopenharmony_ci enabled for the device and these files will be created. 55862306a36Sopenharmony_ci The files hold a string value (enable or disable) 55962306a36Sopenharmony_ci indicating whether or not USB3 hardware LPM U1 or U2 56062306a36Sopenharmony_ci is enabled for the device. 56162306a36Sopenharmony_ci 56262306a36Sopenharmony_ciUSB Port Power Control 56362306a36Sopenharmony_ci---------------------- 56462306a36Sopenharmony_ci 56562306a36Sopenharmony_ciIn addition to suspending endpoint devices and enabling hardware 56662306a36Sopenharmony_cicontrolled link power management, the USB subsystem also has the 56762306a36Sopenharmony_cicapability to disable power to ports under some conditions. Power is 56862306a36Sopenharmony_cicontrolled through ``Set/ClearPortFeature(PORT_POWER)`` requests to a hub. 56962306a36Sopenharmony_ciIn the case of a root or platform-internal hub the host controller 57062306a36Sopenharmony_cidriver translates ``PORT_POWER`` requests into platform firmware (ACPI) 57162306a36Sopenharmony_cimethod calls to set the port power state. For more background see the 57262306a36Sopenharmony_ciLinux Plumbers Conference 2012 slides [#f1]_ and video [#f2]_: 57362306a36Sopenharmony_ci 57462306a36Sopenharmony_ciUpon receiving a ``ClearPortFeature(PORT_POWER)`` request a USB port is 57562306a36Sopenharmony_cilogically off, and may trigger the actual loss of VBUS to the port [#f3]_. 57662306a36Sopenharmony_ciVBUS may be maintained in the case where a hub gangs multiple ports into 57762306a36Sopenharmony_cia shared power well causing power to remain until all ports in the gang 57862306a36Sopenharmony_ciare turned off. VBUS may also be maintained by hub ports configured for 57962306a36Sopenharmony_cia charging application. In any event a logically off port will lose 58062306a36Sopenharmony_ciconnection with its device, not respond to hotplug events, and not 58162306a36Sopenharmony_cirespond to remote wakeup events. 58262306a36Sopenharmony_ci 58362306a36Sopenharmony_ci.. warning:: 58462306a36Sopenharmony_ci 58562306a36Sopenharmony_ci turning off a port may result in the inability to hot add a device. 58662306a36Sopenharmony_ci Please see "User Interface for Port Power Control" for details. 58762306a36Sopenharmony_ci 58862306a36Sopenharmony_ciAs far as the effect on the device itself it is similar to what a device 58962306a36Sopenharmony_cigoes through during system suspend, i.e. the power session is lost. Any 59062306a36Sopenharmony_ciUSB device or driver that misbehaves with system suspend will be 59162306a36Sopenharmony_cisimilarly affected by a port power cycle event. For this reason the 59262306a36Sopenharmony_ciimplementation shares the same device recovery path (and honors the same 59362306a36Sopenharmony_ciquirks) as the system resume path for the hub. 59462306a36Sopenharmony_ci 59562306a36Sopenharmony_ci.. [#f1] 59662306a36Sopenharmony_ci 59762306a36Sopenharmony_ci http://dl.dropbox.com/u/96820575/sarah-sharp-lpt-port-power-off2-mini.pdf 59862306a36Sopenharmony_ci 59962306a36Sopenharmony_ci.. [#f2] 60062306a36Sopenharmony_ci 60162306a36Sopenharmony_ci http://linuxplumbers.ubicast.tv/videos/usb-port-power-off-kerneluserspace-api/ 60262306a36Sopenharmony_ci 60362306a36Sopenharmony_ci.. [#f3] 60462306a36Sopenharmony_ci 60562306a36Sopenharmony_ci USB 3.1 Section 10.12 60662306a36Sopenharmony_ci 60762306a36Sopenharmony_ci wakeup note: if a device is configured to send wakeup events the port 60862306a36Sopenharmony_ci power control implementation will block poweroff attempts on that 60962306a36Sopenharmony_ci port. 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci 61262306a36Sopenharmony_ciUser Interface for Port Power Control 61362306a36Sopenharmony_ci------------------------------------- 61462306a36Sopenharmony_ci 61562306a36Sopenharmony_ciThe port power control mechanism uses the PM runtime system. Poweroff is 61662306a36Sopenharmony_cirequested by clearing the ``power/pm_qos_no_power_off`` flag of the port device 61762306a36Sopenharmony_ci(defaults to 1). If the port is disconnected it will immediately receive a 61862306a36Sopenharmony_ci``ClearPortFeature(PORT_POWER)`` request. Otherwise, it will honor the pm 61962306a36Sopenharmony_ciruntime rules and require the attached child device and all descendants to be 62062306a36Sopenharmony_cisuspended. This mechanism is dependent on the hub advertising port power 62162306a36Sopenharmony_ciswitching in its hub descriptor (wHubCharacteristics logical power switching 62262306a36Sopenharmony_cimode field). 62362306a36Sopenharmony_ci 62462306a36Sopenharmony_ciNote, some interface devices/drivers do not support autosuspend. Userspace may 62562306a36Sopenharmony_cineed to unbind the interface drivers before the :c:type:`usb_device` will 62662306a36Sopenharmony_cisuspend. An unbound interface device is suspended by default. When unbinding, 62762306a36Sopenharmony_cibe careful to unbind interface drivers, not the driver of the parent usb 62862306a36Sopenharmony_cidevice. Also, leave hub interface drivers bound. If the driver for the usb 62962306a36Sopenharmony_cidevice (not interface) is unbound the kernel is no longer able to resume the 63062306a36Sopenharmony_cidevice. If a hub interface driver is unbound, control of its child ports is 63162306a36Sopenharmony_cilost and all attached child-devices will disconnect. A good rule of thumb is 63262306a36Sopenharmony_cithat if the 'driver/module' link for a device points to 63362306a36Sopenharmony_ci``/sys/module/usbcore`` then unbinding it will interfere with port power 63462306a36Sopenharmony_cicontrol. 63562306a36Sopenharmony_ci 63662306a36Sopenharmony_ciExample of the relevant files for port power control. Note, in this example 63762306a36Sopenharmony_cithese files are relative to a usb hub device (prefix):: 63862306a36Sopenharmony_ci 63962306a36Sopenharmony_ci prefix=/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1 64062306a36Sopenharmony_ci 64162306a36Sopenharmony_ci attached child device + 64262306a36Sopenharmony_ci hub port device + | 64362306a36Sopenharmony_ci hub interface device + | | 64462306a36Sopenharmony_ci v v v 64562306a36Sopenharmony_ci $prefix/3-1:1.0/3-1-port1/device 64662306a36Sopenharmony_ci 64762306a36Sopenharmony_ci $prefix/3-1:1.0/3-1-port1/power/pm_qos_no_power_off 64862306a36Sopenharmony_ci $prefix/3-1:1.0/3-1-port1/device/power/control 64962306a36Sopenharmony_ci $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intf0>/driver/unbind 65062306a36Sopenharmony_ci $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intf1>/driver/unbind 65162306a36Sopenharmony_ci ... 65262306a36Sopenharmony_ci $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intfN>/driver/unbind 65362306a36Sopenharmony_ci 65462306a36Sopenharmony_ciIn addition to these files some ports may have a 'peer' link to a port on 65562306a36Sopenharmony_cianother hub. The expectation is that all superspeed ports have a 65662306a36Sopenharmony_cihi-speed peer:: 65762306a36Sopenharmony_ci 65862306a36Sopenharmony_ci $prefix/3-1:1.0/3-1-port1/peer -> ../../../../usb2/2-1/2-1:1.0/2-1-port1 65962306a36Sopenharmony_ci ../../../../usb2/2-1/2-1:1.0/2-1-port1/peer -> ../../../../usb3/3-1/3-1:1.0/3-1-port1 66062306a36Sopenharmony_ci 66162306a36Sopenharmony_ciDistinct from 'companion ports', or 'ehci/xhci shared switchover ports' 66262306a36Sopenharmony_cipeer ports are simply the hi-speed and superspeed interface pins that 66362306a36Sopenharmony_ciare combined into a single usb3 connector. Peer ports share the same 66462306a36Sopenharmony_ciancestor XHCI device. 66562306a36Sopenharmony_ci 66662306a36Sopenharmony_ciWhile a superspeed port is powered off a device may downgrade its 66762306a36Sopenharmony_ciconnection and attempt to connect to the hi-speed pins. The 66862306a36Sopenharmony_ciimplementation takes steps to prevent this: 66962306a36Sopenharmony_ci 67062306a36Sopenharmony_ci1. Port suspend is sequenced to guarantee that hi-speed ports are powered-off 67162306a36Sopenharmony_ci before their superspeed peer is permitted to power-off. The implication is 67262306a36Sopenharmony_ci that the setting ``pm_qos_no_power_off`` to zero on a superspeed port may 67362306a36Sopenharmony_ci not cause the port to power-off until its highspeed peer has gone to its 67462306a36Sopenharmony_ci runtime suspend state. Userspace must take care to order the suspensions 67562306a36Sopenharmony_ci if it wants to guarantee that a superspeed port will power-off. 67662306a36Sopenharmony_ci 67762306a36Sopenharmony_ci2. Port resume is sequenced to force a superspeed port to power-on prior to its 67862306a36Sopenharmony_ci highspeed peer. 67962306a36Sopenharmony_ci 68062306a36Sopenharmony_ci3. Port resume always triggers an attached child device to resume. After a 68162306a36Sopenharmony_ci power session is lost the device may have been removed, or need reset. 68262306a36Sopenharmony_ci Resuming the child device when the parent port regains power resolves those 68362306a36Sopenharmony_ci states and clamps the maximum port power cycle frequency at the rate the 68462306a36Sopenharmony_ci child device can suspend (autosuspend-delay) and resume (reset-resume 68562306a36Sopenharmony_ci latency). 68662306a36Sopenharmony_ci 68762306a36Sopenharmony_ciSysfs files relevant for port power control: 68862306a36Sopenharmony_ci 68962306a36Sopenharmony_ci ``<hubdev-portX>/power/pm_qos_no_power_off``: 69062306a36Sopenharmony_ci This writable flag controls the state of an idle port. 69162306a36Sopenharmony_ci Once all children and descendants have suspended the 69262306a36Sopenharmony_ci port may suspend/poweroff provided that 69362306a36Sopenharmony_ci pm_qos_no_power_off is '0'. If pm_qos_no_power_off is 69462306a36Sopenharmony_ci '1' the port will remain active/powered regardless of 69562306a36Sopenharmony_ci the stats of descendants. Defaults to 1. 69662306a36Sopenharmony_ci 69762306a36Sopenharmony_ci ``<hubdev-portX>/power/runtime_status``: 69862306a36Sopenharmony_ci This file reflects whether the port is 'active' (power is on) 69962306a36Sopenharmony_ci or 'suspended' (logically off). There is no indication to 70062306a36Sopenharmony_ci userspace whether VBUS is still supplied. 70162306a36Sopenharmony_ci 70262306a36Sopenharmony_ci ``<hubdev-portX>/connect_type``: 70362306a36Sopenharmony_ci An advisory read-only flag to userspace indicating the 70462306a36Sopenharmony_ci location and connection type of the port. It returns 70562306a36Sopenharmony_ci one of four values 'hotplug', 'hardwired', 'not used', 70662306a36Sopenharmony_ci and 'unknown'. All values, besides unknown, are set by 70762306a36Sopenharmony_ci platform firmware. 70862306a36Sopenharmony_ci 70962306a36Sopenharmony_ci ``hotplug`` indicates an externally connectable/visible 71062306a36Sopenharmony_ci port on the platform. Typically userspace would choose 71162306a36Sopenharmony_ci to keep such a port powered to handle new device 71262306a36Sopenharmony_ci connection events. 71362306a36Sopenharmony_ci 71462306a36Sopenharmony_ci ``hardwired`` refers to a port that is not visible but 71562306a36Sopenharmony_ci connectable. Examples are internal ports for USB 71662306a36Sopenharmony_ci bluetooth that can be disconnected via an external 71762306a36Sopenharmony_ci switch or a port with a hardwired USB camera. It is 71862306a36Sopenharmony_ci expected to be safe to allow these ports to suspend 71962306a36Sopenharmony_ci provided pm_qos_no_power_off is coordinated with any 72062306a36Sopenharmony_ci switch that gates connections. Userspace must arrange 72162306a36Sopenharmony_ci for the device to be connected prior to the port 72262306a36Sopenharmony_ci powering off, or to activate the port prior to enabling 72362306a36Sopenharmony_ci connection via a switch. 72462306a36Sopenharmony_ci 72562306a36Sopenharmony_ci ``not used`` refers to an internal port that is expected 72662306a36Sopenharmony_ci to never have a device connected to it. These may be 72762306a36Sopenharmony_ci empty internal ports, or ports that are not physically 72862306a36Sopenharmony_ci exposed on a platform. Considered safe to be 72962306a36Sopenharmony_ci powered-off at all times. 73062306a36Sopenharmony_ci 73162306a36Sopenharmony_ci ``unknown`` means platform firmware does not provide 73262306a36Sopenharmony_ci information for this port. Most commonly refers to 73362306a36Sopenharmony_ci external hub ports which should be considered 'hotplug' 73462306a36Sopenharmony_ci for policy decisions. 73562306a36Sopenharmony_ci 73662306a36Sopenharmony_ci .. note:: 73762306a36Sopenharmony_ci 73862306a36Sopenharmony_ci - since we are relying on the BIOS to get this ACPI 73962306a36Sopenharmony_ci information correct, the USB port descriptions may 74062306a36Sopenharmony_ci be missing or wrong. 74162306a36Sopenharmony_ci 74262306a36Sopenharmony_ci - Take care in clearing ``pm_qos_no_power_off``. Once 74362306a36Sopenharmony_ci power is off this port will 74462306a36Sopenharmony_ci not respond to new connect events. 74562306a36Sopenharmony_ci 74662306a36Sopenharmony_ci Once a child device is attached additional constraints are 74762306a36Sopenharmony_ci applied before the port is allowed to poweroff. 74862306a36Sopenharmony_ci 74962306a36Sopenharmony_ci ``<child>/power/control``: 75062306a36Sopenharmony_ci Must be ``auto``, and the port will not 75162306a36Sopenharmony_ci power down until ``<child>/power/runtime_status`` 75262306a36Sopenharmony_ci reflects the 'suspended' state. Default 75362306a36Sopenharmony_ci value is controlled by child device driver. 75462306a36Sopenharmony_ci 75562306a36Sopenharmony_ci ``<child>/power/persist``: 75662306a36Sopenharmony_ci This defaults to ``1`` for most devices and indicates if 75762306a36Sopenharmony_ci kernel can persist the device's configuration across a 75862306a36Sopenharmony_ci power session loss (suspend / port-power event). When 75962306a36Sopenharmony_ci this value is ``0`` (quirky devices), port poweroff is 76062306a36Sopenharmony_ci disabled. 76162306a36Sopenharmony_ci 76262306a36Sopenharmony_ci ``<child>/driver/unbind``: 76362306a36Sopenharmony_ci Wakeup capable devices will block port poweroff. At 76462306a36Sopenharmony_ci this time the only mechanism to clear the usb-internal 76562306a36Sopenharmony_ci wakeup-capability for an interface device is to unbind 76662306a36Sopenharmony_ci its driver. 76762306a36Sopenharmony_ci 76862306a36Sopenharmony_ciSummary of poweroff pre-requisite settings relative to a port device:: 76962306a36Sopenharmony_ci 77062306a36Sopenharmony_ci echo 0 > power/pm_qos_no_power_off 77162306a36Sopenharmony_ci echo 0 > peer/power/pm_qos_no_power_off # if it exists 77262306a36Sopenharmony_ci echo auto > power/control # this is the default value 77362306a36Sopenharmony_ci echo auto > <child>/power/control 77462306a36Sopenharmony_ci echo 1 > <child>/power/persist # this is the default value 77562306a36Sopenharmony_ci 77662306a36Sopenharmony_ciSuggested Userspace Port Power Policy 77762306a36Sopenharmony_ci------------------------------------- 77862306a36Sopenharmony_ci 77962306a36Sopenharmony_ciAs noted above userspace needs to be careful and deliberate about what 78062306a36Sopenharmony_ciports are enabled for poweroff. 78162306a36Sopenharmony_ci 78262306a36Sopenharmony_ciThe default configuration is that all ports start with 78362306a36Sopenharmony_ci``power/pm_qos_no_power_off`` set to ``1`` causing ports to always remain 78462306a36Sopenharmony_ciactive. 78562306a36Sopenharmony_ci 78662306a36Sopenharmony_ciGiven confidence in the platform firmware's description of the ports 78762306a36Sopenharmony_ci(ACPI _PLD record for a port populates 'connect_type') userspace can 78862306a36Sopenharmony_ciclear pm_qos_no_power_off for all 'not used' ports. The same can be 78962306a36Sopenharmony_cidone for 'hardwired' ports provided poweroff is coordinated with any 79062306a36Sopenharmony_ciconnection switch for the port. 79162306a36Sopenharmony_ci 79262306a36Sopenharmony_ciA more aggressive userspace policy is to enable USB port power off for 79362306a36Sopenharmony_ciall ports (set ``<hubdev-portX>/power/pm_qos_no_power_off`` to ``0``) when 79462306a36Sopenharmony_cisome external factor indicates the user has stopped interacting with the 79562306a36Sopenharmony_cisystem. For example, a distro may want to enable power off all USB 79662306a36Sopenharmony_ciports when the screen blanks, and re-power them when the screen becomes 79762306a36Sopenharmony_ciactive. Smart phones and tablets may want to power off USB ports when 79862306a36Sopenharmony_cithe user pushes the power button. 799