1*4882a593Smuzhiyun.. _usb-power-management: 2*4882a593Smuzhiyun 3*4882a593SmuzhiyunPower Management for USB 4*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~ 5*4882a593Smuzhiyun 6*4882a593Smuzhiyun:Author: Alan Stern <stern@rowland.harvard.edu> 7*4882a593Smuzhiyun:Date: Last-updated: February 2014 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun.. 10*4882a593Smuzhiyun Contents: 11*4882a593Smuzhiyun --------- 12*4882a593Smuzhiyun * What is Power Management? 13*4882a593Smuzhiyun * What is Remote Wakeup? 14*4882a593Smuzhiyun * When is a USB device idle? 15*4882a593Smuzhiyun * Forms of dynamic PM 16*4882a593Smuzhiyun * The user interface for dynamic PM 17*4882a593Smuzhiyun * Changing the default idle-delay time 18*4882a593Smuzhiyun * Warnings 19*4882a593Smuzhiyun * The driver interface for Power Management 20*4882a593Smuzhiyun * The driver interface for autosuspend and autoresume 21*4882a593Smuzhiyun * Other parts of the driver interface 22*4882a593Smuzhiyun * Mutual exclusion 23*4882a593Smuzhiyun * Interaction between dynamic PM and system PM 24*4882a593Smuzhiyun * xHCI hardware link PM 25*4882a593Smuzhiyun * USB Port Power Control 26*4882a593Smuzhiyun * User Interface for Port Power Control 27*4882a593Smuzhiyun * Suggested Userspace Port Power Policy 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun 30*4882a593SmuzhiyunWhat is Power Management? 31*4882a593Smuzhiyun------------------------- 32*4882a593Smuzhiyun 33*4882a593SmuzhiyunPower Management (PM) is the practice of saving energy by suspending 34*4882a593Smuzhiyunparts of a computer system when they aren't being used. While a 35*4882a593Smuzhiyuncomponent is ``suspended`` it is in a nonfunctional low-power state; it 36*4882a593Smuzhiyunmight even be turned off completely. A suspended component can be 37*4882a593Smuzhiyun``resumed`` (returned to a functional full-power state) when the kernel 38*4882a593Smuzhiyunneeds to use it. (There also are forms of PM in which components are 39*4882a593Smuzhiyunplaced in a less functional but still usable state instead of being 40*4882a593Smuzhiyunsuspended; an example would be reducing the CPU's clock rate. This 41*4882a593Smuzhiyundocument will not discuss those other forms.) 42*4882a593Smuzhiyun 43*4882a593SmuzhiyunWhen the parts being suspended include the CPU and most of the rest of 44*4882a593Smuzhiyunthe system, we speak of it as a "system suspend". When a particular 45*4882a593Smuzhiyundevice is turned off while the system as a whole remains running, we 46*4882a593Smuzhiyuncall it a "dynamic suspend" (also known as a "runtime suspend" or 47*4882a593Smuzhiyun"selective suspend"). This document concentrates mostly on how 48*4882a593Smuzhiyundynamic PM is implemented in the USB subsystem, although system PM is 49*4882a593Smuzhiyuncovered to some extent (see ``Documentation/power/*.rst`` for more 50*4882a593Smuzhiyuninformation about system PM). 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunSystem PM support is present only if the kernel was built with 53*4882a593Smuzhiyun``CONFIG_SUSPEND`` or ``CONFIG_HIBERNATION`` enabled. Dynamic PM support 54*4882a593Smuzhiyun 55*4882a593Smuzhiyunfor USB is present whenever 56*4882a593Smuzhiyunthe kernel was built with ``CONFIG_PM`` enabled. 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun[Historically, dynamic PM support for USB was present only if the 59*4882a593Smuzhiyunkernel had been built with ``CONFIG_USB_SUSPEND`` enabled (which depended on 60*4882a593Smuzhiyun``CONFIG_PM_RUNTIME``). Starting with the 3.10 kernel release, dynamic PM 61*4882a593Smuzhiyunsupport for USB was present whenever the kernel was built with 62*4882a593Smuzhiyun``CONFIG_PM_RUNTIME`` enabled. The ``CONFIG_USB_SUSPEND`` option had been 63*4882a593Smuzhiyuneliminated.] 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun 66*4882a593SmuzhiyunWhat is Remote Wakeup? 67*4882a593Smuzhiyun---------------------- 68*4882a593Smuzhiyun 69*4882a593SmuzhiyunWhen a device has been suspended, it generally doesn't resume until 70*4882a593Smuzhiyunthe computer tells it to. Likewise, if the entire computer has been 71*4882a593Smuzhiyunsuspended, it generally doesn't resume until the user tells it to, say 72*4882a593Smuzhiyunby pressing a power button or opening the cover. 73*4882a593Smuzhiyun 74*4882a593SmuzhiyunHowever some devices have the capability of resuming by themselves, or 75*4882a593Smuzhiyunasking the kernel to resume them, or even telling the entire computer 76*4882a593Smuzhiyunto resume. This capability goes by several names such as "Wake On 77*4882a593SmuzhiyunLAN"; we will refer to it generically as "remote wakeup". When a 78*4882a593Smuzhiyundevice is enabled for remote wakeup and it is suspended, it may resume 79*4882a593Smuzhiyunitself (or send a request to be resumed) in response to some external 80*4882a593Smuzhiyunevent. Examples include a suspended keyboard resuming when a key is 81*4882a593Smuzhiyunpressed, or a suspended USB hub resuming when a device is plugged in. 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunWhen is a USB device idle? 85*4882a593Smuzhiyun-------------------------- 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunA device is idle whenever the kernel thinks it's not busy doing 88*4882a593Smuzhiyunanything important and thus is a candidate for being suspended. The 89*4882a593Smuzhiyunexact definition depends on the device's driver; drivers are allowed 90*4882a593Smuzhiyunto declare that a device isn't idle even when there's no actual 91*4882a593Smuzhiyuncommunication taking place. (For example, a hub isn't considered idle 92*4882a593Smuzhiyununless all the devices plugged into that hub are already suspended.) 93*4882a593SmuzhiyunIn addition, a device isn't considered idle so long as a program keeps 94*4882a593Smuzhiyunits usbfs file open, whether or not any I/O is going on. 95*4882a593Smuzhiyun 96*4882a593SmuzhiyunIf a USB device has no driver, its usbfs file isn't open, and it isn't 97*4882a593Smuzhiyunbeing accessed through sysfs, then it definitely is idle. 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun 100*4882a593SmuzhiyunForms of dynamic PM 101*4882a593Smuzhiyun------------------- 102*4882a593Smuzhiyun 103*4882a593SmuzhiyunDynamic suspends occur when the kernel decides to suspend an idle 104*4882a593Smuzhiyundevice. This is called ``autosuspend`` for short. In general, a device 105*4882a593Smuzhiyunwon't be autosuspended unless it has been idle for some minimum period 106*4882a593Smuzhiyunof time, the so-called idle-delay time. 107*4882a593Smuzhiyun 108*4882a593SmuzhiyunOf course, nothing the kernel does on its own initiative should 109*4882a593Smuzhiyunprevent the computer or its devices from working properly. If a 110*4882a593Smuzhiyundevice has been autosuspended and a program tries to use it, the 111*4882a593Smuzhiyunkernel will automatically resume the device (autoresume). For the 112*4882a593Smuzhiyunsame reason, an autosuspended device will usually have remote wakeup 113*4882a593Smuzhiyunenabled, if the device supports remote wakeup. 114*4882a593Smuzhiyun 115*4882a593SmuzhiyunIt is worth mentioning that many USB drivers don't support 116*4882a593Smuzhiyunautosuspend. In fact, at the time of this writing (Linux 2.6.23) the 117*4882a593Smuzhiyunonly drivers which do support it are the hub driver, kaweth, asix, 118*4882a593Smuzhiyunusblp, usblcd, and usb-skeleton (which doesn't count). If a 119*4882a593Smuzhiyunnon-supporting driver is bound to a device, the device won't be 120*4882a593Smuzhiyunautosuspended. In effect, the kernel pretends the device is never 121*4882a593Smuzhiyunidle. 122*4882a593Smuzhiyun 123*4882a593SmuzhiyunWe can categorize power management events in two broad classes: 124*4882a593Smuzhiyunexternal and internal. External events are those triggered by some 125*4882a593Smuzhiyunagent outside the USB stack: system suspend/resume (triggered by 126*4882a593Smuzhiyunuserspace), manual dynamic resume (also triggered by userspace), and 127*4882a593Smuzhiyunremote wakeup (triggered by the device). Internal events are those 128*4882a593Smuzhiyuntriggered within the USB stack: autosuspend and autoresume. Note that 129*4882a593Smuzhiyunall dynamic suspend events are internal; external agents are not 130*4882a593Smuzhiyunallowed to issue dynamic suspends. 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunThe user interface for dynamic PM 134*4882a593Smuzhiyun--------------------------------- 135*4882a593Smuzhiyun 136*4882a593SmuzhiyunThe user interface for controlling dynamic PM is located in the ``power/`` 137*4882a593Smuzhiyunsubdirectory of each USB device's sysfs directory, that is, in 138*4882a593Smuzhiyun``/sys/bus/usb/devices/.../power/`` where "..." is the device's ID. The 139*4882a593Smuzhiyunrelevant attribute files are: wakeup, control, and 140*4882a593Smuzhiyun``autosuspend_delay_ms``. (There may also be a file named ``level``; this 141*4882a593Smuzhiyunfile was deprecated as of the 2.6.35 kernel and replaced by the 142*4882a593Smuzhiyun``control`` file. In 2.6.38 the ``autosuspend`` file will be deprecated 143*4882a593Smuzhiyunand replaced by the ``autosuspend_delay_ms`` file. The only difference 144*4882a593Smuzhiyunis that the newer file expresses the delay in milliseconds whereas the 145*4882a593Smuzhiyunolder file uses seconds. Confusingly, both files are present in 2.6.37 146*4882a593Smuzhiyunbut only ``autosuspend`` works.) 147*4882a593Smuzhiyun 148*4882a593Smuzhiyun ``power/wakeup`` 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun This file is empty if the device does not support 151*4882a593Smuzhiyun remote wakeup. Otherwise the file contains either the 152*4882a593Smuzhiyun word ``enabled`` or the word ``disabled``, and you can 153*4882a593Smuzhiyun write those words to the file. The setting determines 154*4882a593Smuzhiyun whether or not remote wakeup will be enabled when the 155*4882a593Smuzhiyun device is next suspended. (If the setting is changed 156*4882a593Smuzhiyun while the device is suspended, the change won't take 157*4882a593Smuzhiyun effect until the following suspend.) 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun ``power/control`` 160*4882a593Smuzhiyun 161*4882a593Smuzhiyun This file contains one of two words: ``on`` or ``auto``. 162*4882a593Smuzhiyun You can write those words to the file to change the 163*4882a593Smuzhiyun device's setting. 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun - ``on`` means that the device should be resumed and 166*4882a593Smuzhiyun autosuspend is not allowed. (Of course, system 167*4882a593Smuzhiyun suspends are still allowed.) 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun - ``auto`` is the normal state in which the kernel is 170*4882a593Smuzhiyun allowed to autosuspend and autoresume the device. 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun (In kernels up to 2.6.32, you could also specify 173*4882a593Smuzhiyun ``suspend``, meaning that the device should remain 174*4882a593Smuzhiyun suspended and autoresume was not allowed. This 175*4882a593Smuzhiyun setting is no longer supported.) 176*4882a593Smuzhiyun 177*4882a593Smuzhiyun ``power/autosuspend_delay_ms`` 178*4882a593Smuzhiyun 179*4882a593Smuzhiyun This file contains an integer value, which is the 180*4882a593Smuzhiyun number of milliseconds the device should remain idle 181*4882a593Smuzhiyun before the kernel will autosuspend it (the idle-delay 182*4882a593Smuzhiyun time). The default is 2000. 0 means to autosuspend 183*4882a593Smuzhiyun as soon as the device becomes idle, and negative 184*4882a593Smuzhiyun values mean never to autosuspend. You can write a 185*4882a593Smuzhiyun number to the file to change the autosuspend 186*4882a593Smuzhiyun idle-delay time. 187*4882a593Smuzhiyun 188*4882a593SmuzhiyunWriting ``-1`` to ``power/autosuspend_delay_ms`` and writing ``on`` to 189*4882a593Smuzhiyun``power/control`` do essentially the same thing -- they both prevent the 190*4882a593Smuzhiyundevice from being autosuspended. Yes, this is a redundancy in the 191*4882a593SmuzhiyunAPI. 192*4882a593Smuzhiyun 193*4882a593Smuzhiyun(In 2.6.21 writing ``0`` to ``power/autosuspend`` would prevent the device 194*4882a593Smuzhiyunfrom being autosuspended; the behavior was changed in 2.6.22. The 195*4882a593Smuzhiyun``power/autosuspend`` attribute did not exist prior to 2.6.21, and the 196*4882a593Smuzhiyun``power/level`` attribute did not exist prior to 2.6.22. ``power/control`` 197*4882a593Smuzhiyunwas added in 2.6.34, and ``power/autosuspend_delay_ms`` was added in 198*4882a593Smuzhiyun2.6.37 but did not become functional until 2.6.38.) 199*4882a593Smuzhiyun 200*4882a593Smuzhiyun 201*4882a593SmuzhiyunChanging the default idle-delay time 202*4882a593Smuzhiyun------------------------------------ 203*4882a593Smuzhiyun 204*4882a593SmuzhiyunThe default autosuspend idle-delay time (in seconds) is controlled by 205*4882a593Smuzhiyuna module parameter in usbcore. You can specify the value when usbcore 206*4882a593Smuzhiyunis loaded. For example, to set it to 5 seconds instead of 2 you would 207*4882a593Smuzhiyundo:: 208*4882a593Smuzhiyun 209*4882a593Smuzhiyun modprobe usbcore autosuspend=5 210*4882a593Smuzhiyun 211*4882a593SmuzhiyunEquivalently, you could add to a configuration file in /etc/modprobe.d 212*4882a593Smuzhiyuna line saying:: 213*4882a593Smuzhiyun 214*4882a593Smuzhiyun options usbcore autosuspend=5 215*4882a593Smuzhiyun 216*4882a593SmuzhiyunSome distributions load the usbcore module very early during the boot 217*4882a593Smuzhiyunprocess, by means of a program or script running from an initramfs 218*4882a593Smuzhiyunimage. To alter the parameter value you would have to rebuild that 219*4882a593Smuzhiyunimage. 220*4882a593Smuzhiyun 221*4882a593SmuzhiyunIf usbcore is compiled into the kernel rather than built as a loadable 222*4882a593Smuzhiyunmodule, you can add:: 223*4882a593Smuzhiyun 224*4882a593Smuzhiyun usbcore.autosuspend=5 225*4882a593Smuzhiyun 226*4882a593Smuzhiyunto the kernel's boot command line. 227*4882a593Smuzhiyun 228*4882a593SmuzhiyunFinally, the parameter value can be changed while the system is 229*4882a593Smuzhiyunrunning. If you do:: 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun echo 5 >/sys/module/usbcore/parameters/autosuspend 232*4882a593Smuzhiyun 233*4882a593Smuzhiyunthen each new USB device will have its autosuspend idle-delay 234*4882a593Smuzhiyuninitialized to 5. (The idle-delay values for already existing devices 235*4882a593Smuzhiyunwill not be affected.) 236*4882a593Smuzhiyun 237*4882a593SmuzhiyunSetting the initial default idle-delay to -1 will prevent any 238*4882a593Smuzhiyunautosuspend of any USB device. This has the benefit of allowing you 239*4882a593Smuzhiyunthen to enable autosuspend for selected devices. 240*4882a593Smuzhiyun 241*4882a593Smuzhiyun 242*4882a593SmuzhiyunWarnings 243*4882a593Smuzhiyun-------- 244*4882a593Smuzhiyun 245*4882a593SmuzhiyunThe USB specification states that all USB devices must support power 246*4882a593Smuzhiyunmanagement. Nevertheless, the sad fact is that many devices do not 247*4882a593Smuzhiyunsupport it very well. You can suspend them all right, but when you 248*4882a593Smuzhiyuntry to resume them they disconnect themselves from the USB bus or 249*4882a593Smuzhiyunthey stop working entirely. This seems to be especially prevalent 250*4882a593Smuzhiyunamong printers and scanners, but plenty of other types of device have 251*4882a593Smuzhiyunthe same deficiency. 252*4882a593Smuzhiyun 253*4882a593SmuzhiyunFor this reason, by default the kernel disables autosuspend (the 254*4882a593Smuzhiyun``power/control`` attribute is initialized to ``on``) for all devices other 255*4882a593Smuzhiyunthan hubs. Hubs, at least, appear to be reasonably well-behaved in 256*4882a593Smuzhiyunthis regard. 257*4882a593Smuzhiyun 258*4882a593Smuzhiyun(In 2.6.21 and 2.6.22 this wasn't the case. Autosuspend was enabled 259*4882a593Smuzhiyunby default for almost all USB devices. A number of people experienced 260*4882a593Smuzhiyunproblems as a result.) 261*4882a593Smuzhiyun 262*4882a593SmuzhiyunThis means that non-hub devices won't be autosuspended unless the user 263*4882a593Smuzhiyunor a program explicitly enables it. As of this writing there aren't 264*4882a593Smuzhiyunany widespread programs which will do this; we hope that in the near 265*4882a593Smuzhiyunfuture device managers such as HAL will take on this added 266*4882a593Smuzhiyunresponsibility. In the meantime you can always carry out the 267*4882a593Smuzhiyunnecessary operations by hand or add them to a udev script. You can 268*4882a593Smuzhiyunalso change the idle-delay time; 2 seconds is not the best choice for 269*4882a593Smuzhiyunevery device. 270*4882a593Smuzhiyun 271*4882a593SmuzhiyunIf a driver knows that its device has proper suspend/resume support, 272*4882a593Smuzhiyunit can enable autosuspend all by itself. For example, the video 273*4882a593Smuzhiyundriver for a laptop's webcam might do this (in recent kernels they 274*4882a593Smuzhiyundo), since these devices are rarely used and so should normally be 275*4882a593Smuzhiyunautosuspended. 276*4882a593Smuzhiyun 277*4882a593SmuzhiyunSometimes it turns out that even when a device does work okay with 278*4882a593Smuzhiyunautosuspend there are still problems. For example, the usbhid driver, 279*4882a593Smuzhiyunwhich manages keyboards and mice, has autosuspend support. Tests with 280*4882a593Smuzhiyuna number of keyboards show that typing on a suspended keyboard, while 281*4882a593Smuzhiyuncausing the keyboard to do a remote wakeup all right, will nonetheless 282*4882a593Smuzhiyunfrequently result in lost keystrokes. Tests with mice show that some 283*4882a593Smuzhiyunof them will issue a remote-wakeup request in response to button 284*4882a593Smuzhiyunpresses but not to motion, and some in response to neither. 285*4882a593Smuzhiyun 286*4882a593SmuzhiyunThe kernel will not prevent you from enabling autosuspend on devices 287*4882a593Smuzhiyunthat can't handle it. It is even possible in theory to damage a 288*4882a593Smuzhiyundevice by suspending it at the wrong time. (Highly unlikely, but 289*4882a593Smuzhiyunpossible.) Take care. 290*4882a593Smuzhiyun 291*4882a593Smuzhiyun 292*4882a593SmuzhiyunThe driver interface for Power Management 293*4882a593Smuzhiyun----------------------------------------- 294*4882a593Smuzhiyun 295*4882a593SmuzhiyunThe requirements for a USB driver to support external power management 296*4882a593Smuzhiyunare pretty modest; the driver need only define:: 297*4882a593Smuzhiyun 298*4882a593Smuzhiyun .suspend 299*4882a593Smuzhiyun .resume 300*4882a593Smuzhiyun .reset_resume 301*4882a593Smuzhiyun 302*4882a593Smuzhiyunmethods in its :c:type:`usb_driver` structure, and the ``reset_resume`` method 303*4882a593Smuzhiyunis optional. The methods' jobs are quite simple: 304*4882a593Smuzhiyun 305*4882a593Smuzhiyun - The ``suspend`` method is called to warn the driver that the 306*4882a593Smuzhiyun device is going to be suspended. If the driver returns a 307*4882a593Smuzhiyun negative error code, the suspend will be aborted. Normally 308*4882a593Smuzhiyun the driver will return 0, in which case it must cancel all 309*4882a593Smuzhiyun outstanding URBs (:c:func:`usb_kill_urb`) and not submit any more. 310*4882a593Smuzhiyun 311*4882a593Smuzhiyun - The ``resume`` method is called to tell the driver that the 312*4882a593Smuzhiyun device has been resumed and the driver can return to normal 313*4882a593Smuzhiyun operation. URBs may once more be submitted. 314*4882a593Smuzhiyun 315*4882a593Smuzhiyun - The ``reset_resume`` method is called to tell the driver that 316*4882a593Smuzhiyun the device has been resumed and it also has been reset. 317*4882a593Smuzhiyun The driver should redo any necessary device initialization, 318*4882a593Smuzhiyun since the device has probably lost most or all of its state 319*4882a593Smuzhiyun (although the interfaces will be in the same altsettings as 320*4882a593Smuzhiyun before the suspend). 321*4882a593Smuzhiyun 322*4882a593SmuzhiyunIf the device is disconnected or powered down while it is suspended, 323*4882a593Smuzhiyunthe ``disconnect`` method will be called instead of the ``resume`` or 324*4882a593Smuzhiyun``reset_resume`` method. This is also quite likely to happen when 325*4882a593Smuzhiyunwaking up from hibernation, as many systems do not maintain suspend 326*4882a593Smuzhiyuncurrent to the USB host controllers during hibernation. (It's 327*4882a593Smuzhiyunpossible to work around the hibernation-forces-disconnect problem by 328*4882a593Smuzhiyunusing the USB Persist facility.) 329*4882a593Smuzhiyun 330*4882a593SmuzhiyunThe ``reset_resume`` method is used by the USB Persist facility (see 331*4882a593Smuzhiyun:ref:`usb-persist`) and it can also be used under certain 332*4882a593Smuzhiyuncircumstances when ``CONFIG_USB_PERSIST`` is not enabled. Currently, if a 333*4882a593Smuzhiyundevice is reset during a resume and the driver does not have a 334*4882a593Smuzhiyun``reset_resume`` method, the driver won't receive any notification about 335*4882a593Smuzhiyunthe resume. Later kernels will call the driver's ``disconnect`` method; 336*4882a593Smuzhiyun2.6.23 doesn't do this. 337*4882a593Smuzhiyun 338*4882a593SmuzhiyunUSB drivers are bound to interfaces, so their ``suspend`` and ``resume`` 339*4882a593Smuzhiyunmethods get called when the interfaces are suspended or resumed. In 340*4882a593Smuzhiyunprinciple one might want to suspend some interfaces on a device (i.e., 341*4882a593Smuzhiyunforce the drivers for those interface to stop all activity) without 342*4882a593Smuzhiyunsuspending the other interfaces. The USB core doesn't allow this; all 343*4882a593Smuzhiyuninterfaces are suspended when the device itself is suspended and all 344*4882a593Smuzhiyuninterfaces are resumed when the device is resumed. It isn't possible 345*4882a593Smuzhiyunto suspend or resume some but not all of a device's interfaces. The 346*4882a593Smuzhiyunclosest you can come is to unbind the interfaces' drivers. 347*4882a593Smuzhiyun 348*4882a593Smuzhiyun 349*4882a593SmuzhiyunThe driver interface for autosuspend and autoresume 350*4882a593Smuzhiyun--------------------------------------------------- 351*4882a593Smuzhiyun 352*4882a593SmuzhiyunTo support autosuspend and autoresume, a driver should implement all 353*4882a593Smuzhiyunthree of the methods listed above. In addition, a driver indicates 354*4882a593Smuzhiyunthat it supports autosuspend by setting the ``.supports_autosuspend`` flag 355*4882a593Smuzhiyunin its usb_driver structure. It is then responsible for informing the 356*4882a593SmuzhiyunUSB core whenever one of its interfaces becomes busy or idle. The 357*4882a593Smuzhiyundriver does so by calling these six functions:: 358*4882a593Smuzhiyun 359*4882a593Smuzhiyun int usb_autopm_get_interface(struct usb_interface *intf); 360*4882a593Smuzhiyun void usb_autopm_put_interface(struct usb_interface *intf); 361*4882a593Smuzhiyun int usb_autopm_get_interface_async(struct usb_interface *intf); 362*4882a593Smuzhiyun void usb_autopm_put_interface_async(struct usb_interface *intf); 363*4882a593Smuzhiyun void usb_autopm_get_interface_no_resume(struct usb_interface *intf); 364*4882a593Smuzhiyun void usb_autopm_put_interface_no_suspend(struct usb_interface *intf); 365*4882a593Smuzhiyun 366*4882a593SmuzhiyunThe functions work by maintaining a usage counter in the 367*4882a593Smuzhiyunusb_interface's embedded device structure. When the counter is > 0 368*4882a593Smuzhiyunthen the interface is deemed to be busy, and the kernel will not 369*4882a593Smuzhiyunautosuspend the interface's device. When the usage counter is = 0 370*4882a593Smuzhiyunthen the interface is considered to be idle, and the kernel may 371*4882a593Smuzhiyunautosuspend the device. 372*4882a593Smuzhiyun 373*4882a593SmuzhiyunDrivers must be careful to balance their overall changes to the usage 374*4882a593Smuzhiyuncounter. Unbalanced "get"s will remain in effect when a driver is 375*4882a593Smuzhiyununbound from its interface, preventing the device from going into 376*4882a593Smuzhiyunruntime suspend should the interface be bound to a driver again. On 377*4882a593Smuzhiyunthe other hand, drivers are allowed to achieve this balance by calling 378*4882a593Smuzhiyunthe ``usb_autopm_*`` functions even after their ``disconnect`` routine 379*4882a593Smuzhiyunhas returned -- say from within a work-queue routine -- provided they 380*4882a593Smuzhiyunretain an active reference to the interface (via ``usb_get_intf`` and 381*4882a593Smuzhiyun``usb_put_intf``). 382*4882a593Smuzhiyun 383*4882a593SmuzhiyunDrivers using the async routines are responsible for their own 384*4882a593Smuzhiyunsynchronization and mutual exclusion. 385*4882a593Smuzhiyun 386*4882a593Smuzhiyun :c:func:`usb_autopm_get_interface` increments the usage counter and 387*4882a593Smuzhiyun does an autoresume if the device is suspended. If the 388*4882a593Smuzhiyun autoresume fails, the counter is decremented back. 389*4882a593Smuzhiyun 390*4882a593Smuzhiyun :c:func:`usb_autopm_put_interface` decrements the usage counter and 391*4882a593Smuzhiyun attempts an autosuspend if the new value is = 0. 392*4882a593Smuzhiyun 393*4882a593Smuzhiyun :c:func:`usb_autopm_get_interface_async` and 394*4882a593Smuzhiyun :c:func:`usb_autopm_put_interface_async` do almost the same things as 395*4882a593Smuzhiyun their non-async counterparts. The big difference is that they 396*4882a593Smuzhiyun use a workqueue to do the resume or suspend part of their 397*4882a593Smuzhiyun jobs. As a result they can be called in an atomic context, 398*4882a593Smuzhiyun such as an URB's completion handler, but when they return the 399*4882a593Smuzhiyun device will generally not yet be in the desired state. 400*4882a593Smuzhiyun 401*4882a593Smuzhiyun :c:func:`usb_autopm_get_interface_no_resume` and 402*4882a593Smuzhiyun :c:func:`usb_autopm_put_interface_no_suspend` merely increment or 403*4882a593Smuzhiyun decrement the usage counter; they do not attempt to carry out 404*4882a593Smuzhiyun an autoresume or an autosuspend. Hence they can be called in 405*4882a593Smuzhiyun an atomic context. 406*4882a593Smuzhiyun 407*4882a593SmuzhiyunThe simplest usage pattern is that a driver calls 408*4882a593Smuzhiyun:c:func:`usb_autopm_get_interface` in its open routine and 409*4882a593Smuzhiyun:c:func:`usb_autopm_put_interface` in its close or release routine. But other 410*4882a593Smuzhiyunpatterns are possible. 411*4882a593Smuzhiyun 412*4882a593SmuzhiyunThe autosuspend attempts mentioned above will often fail for one 413*4882a593Smuzhiyunreason or another. For example, the ``power/control`` attribute might be 414*4882a593Smuzhiyunset to ``on``, or another interface in the same device might not be 415*4882a593Smuzhiyunidle. This is perfectly normal. If the reason for failure was that 416*4882a593Smuzhiyunthe device hasn't been idle for long enough, a timer is scheduled to 417*4882a593Smuzhiyuncarry out the operation automatically when the autosuspend idle-delay 418*4882a593Smuzhiyunhas expired. 419*4882a593Smuzhiyun 420*4882a593SmuzhiyunAutoresume attempts also can fail, although failure would mean that 421*4882a593Smuzhiyunthe device is no longer present or operating properly. Unlike 422*4882a593Smuzhiyunautosuspend, there's no idle-delay for an autoresume. 423*4882a593Smuzhiyun 424*4882a593Smuzhiyun 425*4882a593SmuzhiyunOther parts of the driver interface 426*4882a593Smuzhiyun----------------------------------- 427*4882a593Smuzhiyun 428*4882a593SmuzhiyunDrivers can enable autosuspend for their devices by calling:: 429*4882a593Smuzhiyun 430*4882a593Smuzhiyun usb_enable_autosuspend(struct usb_device *udev); 431*4882a593Smuzhiyun 432*4882a593Smuzhiyunin their :c:func:`probe` routine, if they know that the device is capable of 433*4882a593Smuzhiyunsuspending and resuming correctly. This is exactly equivalent to 434*4882a593Smuzhiyunwriting ``auto`` to the device's ``power/control`` attribute. Likewise, 435*4882a593Smuzhiyundrivers can disable autosuspend by calling:: 436*4882a593Smuzhiyun 437*4882a593Smuzhiyun usb_disable_autosuspend(struct usb_device *udev); 438*4882a593Smuzhiyun 439*4882a593SmuzhiyunThis is exactly the same as writing ``on`` to the ``power/control`` attribute. 440*4882a593Smuzhiyun 441*4882a593SmuzhiyunSometimes a driver needs to make sure that remote wakeup is enabled 442*4882a593Smuzhiyunduring autosuspend. For example, there's not much point 443*4882a593Smuzhiyunautosuspending a keyboard if the user can't cause the keyboard to do a 444*4882a593Smuzhiyunremote wakeup by typing on it. If the driver sets 445*4882a593Smuzhiyun``intf->needs_remote_wakeup`` to 1, the kernel won't autosuspend the 446*4882a593Smuzhiyundevice if remote wakeup isn't available. (If the device is already 447*4882a593Smuzhiyunautosuspended, though, setting this flag won't cause the kernel to 448*4882a593Smuzhiyunautoresume it. Normally a driver would set this flag in its ``probe`` 449*4882a593Smuzhiyunmethod, at which time the device is guaranteed not to be 450*4882a593Smuzhiyunautosuspended.) 451*4882a593Smuzhiyun 452*4882a593SmuzhiyunIf a driver does its I/O asynchronously in interrupt context, it 453*4882a593Smuzhiyunshould call :c:func:`usb_autopm_get_interface_async` before starting output and 454*4882a593Smuzhiyun:c:func:`usb_autopm_put_interface_async` when the output queue drains. When 455*4882a593Smuzhiyunit receives an input event, it should call:: 456*4882a593Smuzhiyun 457*4882a593Smuzhiyun usb_mark_last_busy(struct usb_device *udev); 458*4882a593Smuzhiyun 459*4882a593Smuzhiyunin the event handler. This tells the PM core that the device was just 460*4882a593Smuzhiyunbusy and therefore the next autosuspend idle-delay expiration should 461*4882a593Smuzhiyunbe pushed back. Many of the usb_autopm_* routines also make this call, 462*4882a593Smuzhiyunso drivers need to worry only when interrupt-driven input arrives. 463*4882a593Smuzhiyun 464*4882a593SmuzhiyunAsynchronous operation is always subject to races. For example, a 465*4882a593Smuzhiyundriver may call the :c:func:`usb_autopm_get_interface_async` routine at a time 466*4882a593Smuzhiyunwhen the core has just finished deciding the device has been idle for 467*4882a593Smuzhiyunlong enough but not yet gotten around to calling the driver's ``suspend`` 468*4882a593Smuzhiyunmethod. The ``suspend`` method must be responsible for synchronizing with 469*4882a593Smuzhiyunthe I/O request routine and the URB completion handler; it should 470*4882a593Smuzhiyuncause autosuspends to fail with -EBUSY if the driver needs to use the 471*4882a593Smuzhiyundevice. 472*4882a593Smuzhiyun 473*4882a593SmuzhiyunExternal suspend calls should never be allowed to fail in this way, 474*4882a593Smuzhiyunonly autosuspend calls. The driver can tell them apart by applying 475*4882a593Smuzhiyunthe :c:func:`PMSG_IS_AUTO` macro to the message argument to the ``suspend`` 476*4882a593Smuzhiyunmethod; it will return True for internal PM events (autosuspend) and 477*4882a593SmuzhiyunFalse for external PM events. 478*4882a593Smuzhiyun 479*4882a593Smuzhiyun 480*4882a593SmuzhiyunMutual exclusion 481*4882a593Smuzhiyun---------------- 482*4882a593Smuzhiyun 483*4882a593SmuzhiyunFor external events -- but not necessarily for autosuspend or 484*4882a593Smuzhiyunautoresume -- the device semaphore (udev->dev.sem) will be held when a 485*4882a593Smuzhiyun``suspend`` or ``resume`` method is called. This implies that external 486*4882a593Smuzhiyunsuspend/resume events are mutually exclusive with calls to ``probe``, 487*4882a593Smuzhiyun``disconnect``, ``pre_reset``, and ``post_reset``; the USB core guarantees that 488*4882a593Smuzhiyunthis is true of autosuspend/autoresume events as well. 489*4882a593Smuzhiyun 490*4882a593SmuzhiyunIf a driver wants to block all suspend/resume calls during some 491*4882a593Smuzhiyuncritical section, the best way is to lock the device and call 492*4882a593Smuzhiyun:c:func:`usb_autopm_get_interface` (and do the reverse at the end of the 493*4882a593Smuzhiyuncritical section). Holding the device semaphore will block all 494*4882a593Smuzhiyunexternal PM calls, and the :c:func:`usb_autopm_get_interface` will prevent any 495*4882a593Smuzhiyuninternal PM calls, even if it fails. (Exercise: Why?) 496*4882a593Smuzhiyun 497*4882a593Smuzhiyun 498*4882a593SmuzhiyunInteraction between dynamic PM and system PM 499*4882a593Smuzhiyun-------------------------------------------- 500*4882a593Smuzhiyun 501*4882a593SmuzhiyunDynamic power management and system power management can interact in 502*4882a593Smuzhiyuna couple of ways. 503*4882a593Smuzhiyun 504*4882a593SmuzhiyunFirstly, a device may already be autosuspended when a system suspend 505*4882a593Smuzhiyunoccurs. Since system suspends are supposed to be as transparent as 506*4882a593Smuzhiyunpossible, the device should remain suspended following the system 507*4882a593Smuzhiyunresume. But this theory may not work out well in practice; over time 508*4882a593Smuzhiyunthe kernel's behavior in this regard has changed. As of 2.6.37 the 509*4882a593Smuzhiyunpolicy is to resume all devices during a system resume and let them 510*4882a593Smuzhiyunhandle their own runtime suspends afterward. 511*4882a593Smuzhiyun 512*4882a593SmuzhiyunSecondly, a dynamic power-management event may occur as a system 513*4882a593Smuzhiyunsuspend is underway. The window for this is short, since system 514*4882a593Smuzhiyunsuspends don't take long (a few seconds usually), but it can happen. 515*4882a593SmuzhiyunFor example, a suspended device may send a remote-wakeup signal while 516*4882a593Smuzhiyunthe system is suspending. The remote wakeup may succeed, which would 517*4882a593Smuzhiyuncause the system suspend to abort. If the remote wakeup doesn't 518*4882a593Smuzhiyunsucceed, it may still remain active and thus cause the system to 519*4882a593Smuzhiyunresume as soon as the system suspend is complete. Or the remote 520*4882a593Smuzhiyunwakeup may fail and get lost. Which outcome occurs depends on timing 521*4882a593Smuzhiyunand on the hardware and firmware design. 522*4882a593Smuzhiyun 523*4882a593Smuzhiyun 524*4882a593SmuzhiyunxHCI hardware link PM 525*4882a593Smuzhiyun--------------------- 526*4882a593Smuzhiyun 527*4882a593SmuzhiyunxHCI host controller provides hardware link power management to usb2.0 528*4882a593Smuzhiyun(xHCI 1.0 feature) and usb3.0 devices which support link PM. By 529*4882a593Smuzhiyunenabling hardware LPM, the host can automatically put the device into 530*4882a593Smuzhiyunlower power state(L1 for usb2.0 devices, or U1/U2 for usb3.0 devices), 531*4882a593Smuzhiyunwhich state device can enter and resume very quickly. 532*4882a593Smuzhiyun 533*4882a593SmuzhiyunThe user interface for controlling hardware LPM is located in the 534*4882a593Smuzhiyun``power/`` subdirectory of each USB device's sysfs directory, that is, in 535*4882a593Smuzhiyun``/sys/bus/usb/devices/.../power/`` where "..." is the device's ID. The 536*4882a593Smuzhiyunrelevant attribute files are ``usb2_hardware_lpm`` and ``usb3_hardware_lpm``. 537*4882a593Smuzhiyun 538*4882a593Smuzhiyun ``power/usb2_hardware_lpm`` 539*4882a593Smuzhiyun 540*4882a593Smuzhiyun When a USB2 device which support LPM is plugged to a 541*4882a593Smuzhiyun xHCI host root hub which support software LPM, the 542*4882a593Smuzhiyun host will run a software LPM test for it; if the device 543*4882a593Smuzhiyun enters L1 state and resume successfully and the host 544*4882a593Smuzhiyun supports USB2 hardware LPM, this file will show up and 545*4882a593Smuzhiyun driver will enable hardware LPM for the device. You 546*4882a593Smuzhiyun can write y/Y/1 or n/N/0 to the file to enable/disable 547*4882a593Smuzhiyun USB2 hardware LPM manually. This is for test purpose mainly. 548*4882a593Smuzhiyun 549*4882a593Smuzhiyun ``power/usb3_hardware_lpm_u1`` 550*4882a593Smuzhiyun ``power/usb3_hardware_lpm_u2`` 551*4882a593Smuzhiyun 552*4882a593Smuzhiyun When a USB 3.0 lpm-capable device is plugged in to a 553*4882a593Smuzhiyun xHCI host which supports link PM, it will check if U1 554*4882a593Smuzhiyun and U2 exit latencies have been set in the BOS 555*4882a593Smuzhiyun descriptor; if the check is passed and the host 556*4882a593Smuzhiyun supports USB3 hardware LPM, USB3 hardware LPM will be 557*4882a593Smuzhiyun enabled for the device and these files will be created. 558*4882a593Smuzhiyun The files hold a string value (enable or disable) 559*4882a593Smuzhiyun indicating whether or not USB3 hardware LPM U1 or U2 560*4882a593Smuzhiyun is enabled for the device. 561*4882a593Smuzhiyun 562*4882a593SmuzhiyunUSB Port Power Control 563*4882a593Smuzhiyun---------------------- 564*4882a593Smuzhiyun 565*4882a593SmuzhiyunIn addition to suspending endpoint devices and enabling hardware 566*4882a593Smuzhiyuncontrolled link power management, the USB subsystem also has the 567*4882a593Smuzhiyuncapability to disable power to ports under some conditions. Power is 568*4882a593Smuzhiyuncontrolled through ``Set/ClearPortFeature(PORT_POWER)`` requests to a hub. 569*4882a593SmuzhiyunIn the case of a root or platform-internal hub the host controller 570*4882a593Smuzhiyundriver translates ``PORT_POWER`` requests into platform firmware (ACPI) 571*4882a593Smuzhiyunmethod calls to set the port power state. For more background see the 572*4882a593SmuzhiyunLinux Plumbers Conference 2012 slides [#f1]_ and video [#f2]_: 573*4882a593Smuzhiyun 574*4882a593SmuzhiyunUpon receiving a ``ClearPortFeature(PORT_POWER)`` request a USB port is 575*4882a593Smuzhiyunlogically off, and may trigger the actual loss of VBUS to the port [#f3]_. 576*4882a593SmuzhiyunVBUS may be maintained in the case where a hub gangs multiple ports into 577*4882a593Smuzhiyuna shared power well causing power to remain until all ports in the gang 578*4882a593Smuzhiyunare turned off. VBUS may also be maintained by hub ports configured for 579*4882a593Smuzhiyuna charging application. In any event a logically off port will lose 580*4882a593Smuzhiyunconnection with its device, not respond to hotplug events, and not 581*4882a593Smuzhiyunrespond to remote wakeup events. 582*4882a593Smuzhiyun 583*4882a593Smuzhiyun.. warning:: 584*4882a593Smuzhiyun 585*4882a593Smuzhiyun turning off a port may result in the inability to hot add a device. 586*4882a593Smuzhiyun Please see "User Interface for Port Power Control" for details. 587*4882a593Smuzhiyun 588*4882a593SmuzhiyunAs far as the effect on the device itself it is similar to what a device 589*4882a593Smuzhiyungoes through during system suspend, i.e. the power session is lost. Any 590*4882a593SmuzhiyunUSB device or driver that misbehaves with system suspend will be 591*4882a593Smuzhiyunsimilarly affected by a port power cycle event. For this reason the 592*4882a593Smuzhiyunimplementation shares the same device recovery path (and honors the same 593*4882a593Smuzhiyunquirks) as the system resume path for the hub. 594*4882a593Smuzhiyun 595*4882a593Smuzhiyun.. [#f1] 596*4882a593Smuzhiyun 597*4882a593Smuzhiyun http://dl.dropbox.com/u/96820575/sarah-sharp-lpt-port-power-off2-mini.pdf 598*4882a593Smuzhiyun 599*4882a593Smuzhiyun.. [#f2] 600*4882a593Smuzhiyun 601*4882a593Smuzhiyun http://linuxplumbers.ubicast.tv/videos/usb-port-power-off-kerneluserspace-api/ 602*4882a593Smuzhiyun 603*4882a593Smuzhiyun.. [#f3] 604*4882a593Smuzhiyun 605*4882a593Smuzhiyun USB 3.1 Section 10.12 606*4882a593Smuzhiyun 607*4882a593Smuzhiyun wakeup note: if a device is configured to send wakeup events the port 608*4882a593Smuzhiyun power control implementation will block poweroff attempts on that 609*4882a593Smuzhiyun port. 610*4882a593Smuzhiyun 611*4882a593Smuzhiyun 612*4882a593SmuzhiyunUser Interface for Port Power Control 613*4882a593Smuzhiyun------------------------------------- 614*4882a593Smuzhiyun 615*4882a593SmuzhiyunThe port power control mechanism uses the PM runtime system. Poweroff is 616*4882a593Smuzhiyunrequested by clearing the ``power/pm_qos_no_power_off`` flag of the port device 617*4882a593Smuzhiyun(defaults to 1). If the port is disconnected it will immediately receive a 618*4882a593Smuzhiyun``ClearPortFeature(PORT_POWER)`` request. Otherwise, it will honor the pm 619*4882a593Smuzhiyunruntime rules and require the attached child device and all descendants to be 620*4882a593Smuzhiyunsuspended. This mechanism is dependent on the hub advertising port power 621*4882a593Smuzhiyunswitching in its hub descriptor (wHubCharacteristics logical power switching 622*4882a593Smuzhiyunmode field). 623*4882a593Smuzhiyun 624*4882a593SmuzhiyunNote, some interface devices/drivers do not support autosuspend. Userspace may 625*4882a593Smuzhiyunneed to unbind the interface drivers before the :c:type:`usb_device` will 626*4882a593Smuzhiyunsuspend. An unbound interface device is suspended by default. When unbinding, 627*4882a593Smuzhiyunbe careful to unbind interface drivers, not the driver of the parent usb 628*4882a593Smuzhiyundevice. Also, leave hub interface drivers bound. If the driver for the usb 629*4882a593Smuzhiyundevice (not interface) is unbound the kernel is no longer able to resume the 630*4882a593Smuzhiyundevice. If a hub interface driver is unbound, control of its child ports is 631*4882a593Smuzhiyunlost and all attached child-devices will disconnect. A good rule of thumb is 632*4882a593Smuzhiyunthat if the 'driver/module' link for a device points to 633*4882a593Smuzhiyun``/sys/module/usbcore`` then unbinding it will interfere with port power 634*4882a593Smuzhiyuncontrol. 635*4882a593Smuzhiyun 636*4882a593SmuzhiyunExample of the relevant files for port power control. Note, in this example 637*4882a593Smuzhiyunthese files are relative to a usb hub device (prefix):: 638*4882a593Smuzhiyun 639*4882a593Smuzhiyun prefix=/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1 640*4882a593Smuzhiyun 641*4882a593Smuzhiyun attached child device + 642*4882a593Smuzhiyun hub port device + | 643*4882a593Smuzhiyun hub interface device + | | 644*4882a593Smuzhiyun v v v 645*4882a593Smuzhiyun $prefix/3-1:1.0/3-1-port1/device 646*4882a593Smuzhiyun 647*4882a593Smuzhiyun $prefix/3-1:1.0/3-1-port1/power/pm_qos_no_power_off 648*4882a593Smuzhiyun $prefix/3-1:1.0/3-1-port1/device/power/control 649*4882a593Smuzhiyun $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intf0>/driver/unbind 650*4882a593Smuzhiyun $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intf1>/driver/unbind 651*4882a593Smuzhiyun ... 652*4882a593Smuzhiyun $prefix/3-1:1.0/3-1-port1/device/3-1.1:<intfN>/driver/unbind 653*4882a593Smuzhiyun 654*4882a593SmuzhiyunIn addition to these files some ports may have a 'peer' link to a port on 655*4882a593Smuzhiyunanother hub. The expectation is that all superspeed ports have a 656*4882a593Smuzhiyunhi-speed peer:: 657*4882a593Smuzhiyun 658*4882a593Smuzhiyun $prefix/3-1:1.0/3-1-port1/peer -> ../../../../usb2/2-1/2-1:1.0/2-1-port1 659*4882a593Smuzhiyun ../../../../usb2/2-1/2-1:1.0/2-1-port1/peer -> ../../../../usb3/3-1/3-1:1.0/3-1-port1 660*4882a593Smuzhiyun 661*4882a593SmuzhiyunDistinct from 'companion ports', or 'ehci/xhci shared switchover ports' 662*4882a593Smuzhiyunpeer ports are simply the hi-speed and superspeed interface pins that 663*4882a593Smuzhiyunare combined into a single usb3 connector. Peer ports share the same 664*4882a593Smuzhiyunancestor XHCI device. 665*4882a593Smuzhiyun 666*4882a593SmuzhiyunWhile a superspeed port is powered off a device may downgrade its 667*4882a593Smuzhiyunconnection and attempt to connect to the hi-speed pins. The 668*4882a593Smuzhiyunimplementation takes steps to prevent this: 669*4882a593Smuzhiyun 670*4882a593Smuzhiyun1. Port suspend is sequenced to guarantee that hi-speed ports are powered-off 671*4882a593Smuzhiyun before their superspeed peer is permitted to power-off. The implication is 672*4882a593Smuzhiyun that the setting ``pm_qos_no_power_off`` to zero on a superspeed port may 673*4882a593Smuzhiyun not cause the port to power-off until its highspeed peer has gone to its 674*4882a593Smuzhiyun runtime suspend state. Userspace must take care to order the suspensions 675*4882a593Smuzhiyun if it wants to guarantee that a superspeed port will power-off. 676*4882a593Smuzhiyun 677*4882a593Smuzhiyun2. Port resume is sequenced to force a superspeed port to power-on prior to its 678*4882a593Smuzhiyun highspeed peer. 679*4882a593Smuzhiyun 680*4882a593Smuzhiyun3. Port resume always triggers an attached child device to resume. After a 681*4882a593Smuzhiyun power session is lost the device may have been removed, or need reset. 682*4882a593Smuzhiyun Resuming the child device when the parent port regains power resolves those 683*4882a593Smuzhiyun states and clamps the maximum port power cycle frequency at the rate the 684*4882a593Smuzhiyun child device can suspend (autosuspend-delay) and resume (reset-resume 685*4882a593Smuzhiyun latency). 686*4882a593Smuzhiyun 687*4882a593SmuzhiyunSysfs files relevant for port power control: 688*4882a593Smuzhiyun 689*4882a593Smuzhiyun ``<hubdev-portX>/power/pm_qos_no_power_off``: 690*4882a593Smuzhiyun This writable flag controls the state of an idle port. 691*4882a593Smuzhiyun Once all children and descendants have suspended the 692*4882a593Smuzhiyun port may suspend/poweroff provided that 693*4882a593Smuzhiyun pm_qos_no_power_off is '0'. If pm_qos_no_power_off is 694*4882a593Smuzhiyun '1' the port will remain active/powered regardless of 695*4882a593Smuzhiyun the stats of descendants. Defaults to 1. 696*4882a593Smuzhiyun 697*4882a593Smuzhiyun ``<hubdev-portX>/power/runtime_status``: 698*4882a593Smuzhiyun This file reflects whether the port is 'active' (power is on) 699*4882a593Smuzhiyun or 'suspended' (logically off). There is no indication to 700*4882a593Smuzhiyun userspace whether VBUS is still supplied. 701*4882a593Smuzhiyun 702*4882a593Smuzhiyun ``<hubdev-portX>/connect_type``: 703*4882a593Smuzhiyun An advisory read-only flag to userspace indicating the 704*4882a593Smuzhiyun location and connection type of the port. It returns 705*4882a593Smuzhiyun one of four values 'hotplug', 'hardwired', 'not used', 706*4882a593Smuzhiyun and 'unknown'. All values, besides unknown, are set by 707*4882a593Smuzhiyun platform firmware. 708*4882a593Smuzhiyun 709*4882a593Smuzhiyun ``hotplug`` indicates an externally connectable/visible 710*4882a593Smuzhiyun port on the platform. Typically userspace would choose 711*4882a593Smuzhiyun to keep such a port powered to handle new device 712*4882a593Smuzhiyun connection events. 713*4882a593Smuzhiyun 714*4882a593Smuzhiyun ``hardwired`` refers to a port that is not visible but 715*4882a593Smuzhiyun connectable. Examples are internal ports for USB 716*4882a593Smuzhiyun bluetooth that can be disconnected via an external 717*4882a593Smuzhiyun switch or a port with a hardwired USB camera. It is 718*4882a593Smuzhiyun expected to be safe to allow these ports to suspend 719*4882a593Smuzhiyun provided pm_qos_no_power_off is coordinated with any 720*4882a593Smuzhiyun switch that gates connections. Userspace must arrange 721*4882a593Smuzhiyun for the device to be connected prior to the port 722*4882a593Smuzhiyun powering off, or to activate the port prior to enabling 723*4882a593Smuzhiyun connection via a switch. 724*4882a593Smuzhiyun 725*4882a593Smuzhiyun ``not used`` refers to an internal port that is expected 726*4882a593Smuzhiyun to never have a device connected to it. These may be 727*4882a593Smuzhiyun empty internal ports, or ports that are not physically 728*4882a593Smuzhiyun exposed on a platform. Considered safe to be 729*4882a593Smuzhiyun powered-off at all times. 730*4882a593Smuzhiyun 731*4882a593Smuzhiyun ``unknown`` means platform firmware does not provide 732*4882a593Smuzhiyun information for this port. Most commonly refers to 733*4882a593Smuzhiyun external hub ports which should be considered 'hotplug' 734*4882a593Smuzhiyun for policy decisions. 735*4882a593Smuzhiyun 736*4882a593Smuzhiyun .. note:: 737*4882a593Smuzhiyun 738*4882a593Smuzhiyun - since we are relying on the BIOS to get this ACPI 739*4882a593Smuzhiyun information correct, the USB port descriptions may 740*4882a593Smuzhiyun be missing or wrong. 741*4882a593Smuzhiyun 742*4882a593Smuzhiyun - Take care in clearing ``pm_qos_no_power_off``. Once 743*4882a593Smuzhiyun power is off this port will 744*4882a593Smuzhiyun not respond to new connect events. 745*4882a593Smuzhiyun 746*4882a593Smuzhiyun Once a child device is attached additional constraints are 747*4882a593Smuzhiyun applied before the port is allowed to poweroff. 748*4882a593Smuzhiyun 749*4882a593Smuzhiyun ``<child>/power/control``: 750*4882a593Smuzhiyun Must be ``auto``, and the port will not 751*4882a593Smuzhiyun power down until ``<child>/power/runtime_status`` 752*4882a593Smuzhiyun reflects the 'suspended' state. Default 753*4882a593Smuzhiyun value is controlled by child device driver. 754*4882a593Smuzhiyun 755*4882a593Smuzhiyun ``<child>/power/persist``: 756*4882a593Smuzhiyun This defaults to ``1`` for most devices and indicates if 757*4882a593Smuzhiyun kernel can persist the device's configuration across a 758*4882a593Smuzhiyun power session loss (suspend / port-power event). When 759*4882a593Smuzhiyun this value is ``0`` (quirky devices), port poweroff is 760*4882a593Smuzhiyun disabled. 761*4882a593Smuzhiyun 762*4882a593Smuzhiyun ``<child>/driver/unbind``: 763*4882a593Smuzhiyun Wakeup capable devices will block port poweroff. At 764*4882a593Smuzhiyun this time the only mechanism to clear the usb-internal 765*4882a593Smuzhiyun wakeup-capability for an interface device is to unbind 766*4882a593Smuzhiyun its driver. 767*4882a593Smuzhiyun 768*4882a593SmuzhiyunSummary of poweroff pre-requisite settings relative to a port device:: 769*4882a593Smuzhiyun 770*4882a593Smuzhiyun echo 0 > power/pm_qos_no_power_off 771*4882a593Smuzhiyun echo 0 > peer/power/pm_qos_no_power_off # if it exists 772*4882a593Smuzhiyun echo auto > power/control # this is the default value 773*4882a593Smuzhiyun echo auto > <child>/power/control 774*4882a593Smuzhiyun echo 1 > <child>/power/persist # this is the default value 775*4882a593Smuzhiyun 776*4882a593SmuzhiyunSuggested Userspace Port Power Policy 777*4882a593Smuzhiyun------------------------------------- 778*4882a593Smuzhiyun 779*4882a593SmuzhiyunAs noted above userspace needs to be careful and deliberate about what 780*4882a593Smuzhiyunports are enabled for poweroff. 781*4882a593Smuzhiyun 782*4882a593SmuzhiyunThe default configuration is that all ports start with 783*4882a593Smuzhiyun``power/pm_qos_no_power_off`` set to ``1`` causing ports to always remain 784*4882a593Smuzhiyunactive. 785*4882a593Smuzhiyun 786*4882a593SmuzhiyunGiven confidence in the platform firmware's description of the ports 787*4882a593Smuzhiyun(ACPI _PLD record for a port populates 'connect_type') userspace can 788*4882a593Smuzhiyunclear pm_qos_no_power_off for all 'not used' ports. The same can be 789*4882a593Smuzhiyundone for 'hardwired' ports provided poweroff is coordinated with any 790*4882a593Smuzhiyunconnection switch for the port. 791*4882a593Smuzhiyun 792*4882a593SmuzhiyunA more aggressive userspace policy is to enable USB port power off for 793*4882a593Smuzhiyunall ports (set ``<hubdev-portX>/power/pm_qos_no_power_off`` to ``0``) when 794*4882a593Smuzhiyunsome external factor indicates the user has stopped interacting with the 795*4882a593Smuzhiyunsystem. For example, a distro may want to enable power off all USB 796*4882a593Smuzhiyunports when the screen blanks, and re-power them when the screen becomes 797*4882a593Smuzhiyunactive. Smart phones and tablets may want to power off USB ports when 798*4882a593Smuzhiyunthe user pushes the power button. 799