162306a36Sopenharmony_ci.. _usb-persist: 262306a36Sopenharmony_ci 362306a36Sopenharmony_ciUSB device persistence during system suspend 462306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci:Author: Alan Stern <stern@rowland.harvard.edu> 762306a36Sopenharmony_ci:Date: September 2, 2006 (Updated February 25, 2008) 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ciWhat is the problem? 1162306a36Sopenharmony_ci==================== 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ciAccording to the USB specification, when a USB bus is suspended the 1462306a36Sopenharmony_cibus must continue to supply suspend current (around 1-5 mA). This 1562306a36Sopenharmony_ciis so that devices can maintain their internal state and hubs can 1662306a36Sopenharmony_cidetect connect-change events (devices being plugged in or unplugged). 1762306a36Sopenharmony_ciThe technical term is "power session". 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ciIf a USB device's power session is interrupted then the system is 2062306a36Sopenharmony_cirequired to behave as though the device has been unplugged. It's a 2162306a36Sopenharmony_ciconservative approach; in the absence of suspend current the computer 2262306a36Sopenharmony_cihas no way to know what has actually happened. Perhaps the same 2362306a36Sopenharmony_cidevice is still attached or perhaps it was removed and a different 2462306a36Sopenharmony_cidevice plugged into the port. The system must assume the worst. 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ciBy default, Linux behaves according to the spec. If a USB host 2762306a36Sopenharmony_cicontroller loses power during a system suspend, then when the system 2862306a36Sopenharmony_ciwakes up all the devices attached to that controller are treated as 2962306a36Sopenharmony_cithough they had disconnected. This is always safe and it is the 3062306a36Sopenharmony_ci"officially correct" thing to do. 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ciFor many sorts of devices this behavior doesn't matter in the least. 3362306a36Sopenharmony_ciIf the kernel wants to believe that your USB keyboard was unplugged 3462306a36Sopenharmony_ciwhile the system was asleep and a new keyboard was plugged in when the 3562306a36Sopenharmony_cisystem woke up, who cares? It'll still work the same when you type on 3662306a36Sopenharmony_ciit. 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ciUnfortunately problems _can_ arise, particularly with mass-storage 3962306a36Sopenharmony_cidevices. The effect is exactly the same as if the device really had 4062306a36Sopenharmony_cibeen unplugged while the system was suspended. If you had a mounted 4162306a36Sopenharmony_cifilesystem on the device, you're out of luck -- everything in that 4262306a36Sopenharmony_cifilesystem is now inaccessible. This is especially annoying if your 4362306a36Sopenharmony_ciroot filesystem was located on the device, since your system will 4462306a36Sopenharmony_ciinstantly crash. 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ciLoss of power isn't the only mechanism to worry about. Anything that 4762306a36Sopenharmony_ciinterrupts a power session will have the same effect. For example, 4862306a36Sopenharmony_cieven though suspend current may have been maintained while the system 4962306a36Sopenharmony_ciwas asleep, on many systems during the initial stages of wakeup the 5062306a36Sopenharmony_cifirmware (i.e., the BIOS) resets the motherboard's USB host 5162306a36Sopenharmony_cicontrollers. Result: all the power sessions are destroyed and again 5262306a36Sopenharmony_ciit's as though you had unplugged all the USB devices. Yes, it's 5362306a36Sopenharmony_cientirely the BIOS's fault, but that doesn't do _you_ any good unless 5462306a36Sopenharmony_ciyou can convince the BIOS supplier to fix the problem (lots of luck!). 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ciOn many systems the USB host controllers will get reset after a 5762306a36Sopenharmony_cisuspend-to-RAM. On almost all systems, no suspend current is 5862306a36Sopenharmony_ciavailable during hibernation (also known as swsusp or suspend-to-disk). 5962306a36Sopenharmony_ciYou can check the kernel log after resuming to see if either of these 6062306a36Sopenharmony_cihas happened; look for lines saying "root hub lost power or was reset". 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ciIn practice, people are forced to unmount any filesystems on a USB 6362306a36Sopenharmony_cidevice before suspending. If the root filesystem is on a USB device, 6462306a36Sopenharmony_cithe system can't be suspended at all. (All right, it _can_ be 6562306a36Sopenharmony_cisuspended -- but it will crash as soon as it wakes up, which isn't 6662306a36Sopenharmony_cimuch better.) 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ciWhat is the solution? 7062306a36Sopenharmony_ci===================== 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ciThe kernel includes a feature called USB-persist. It tries to work 7362306a36Sopenharmony_ciaround these issues by allowing the core USB device data structures to 7462306a36Sopenharmony_cipersist across a power-session disruption. 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ciIt works like this. If the kernel sees that a USB host controller is 7762306a36Sopenharmony_cinot in the expected state during resume (i.e., if the controller was 7862306a36Sopenharmony_cireset or otherwise had lost power) then it applies a persistence check 7962306a36Sopenharmony_cito each of the USB devices below that controller for which the 8062306a36Sopenharmony_ci"persist" attribute is set. It doesn't try to resume the device; that 8162306a36Sopenharmony_cican't work once the power session is gone. Instead it issues a USB 8262306a36Sopenharmony_ciport reset and then re-enumerates the device. (This is exactly the 8362306a36Sopenharmony_cisame thing that happens whenever a USB device is reset.) If the 8462306a36Sopenharmony_cire-enumeration shows that the device now attached to that port has the 8562306a36Sopenharmony_cisame descriptors as before, including the Vendor and Product IDs, then 8662306a36Sopenharmony_cithe kernel continues to use the same device structure. In effect, the 8762306a36Sopenharmony_cikernel treats the device as though it had merely been reset instead of 8862306a36Sopenharmony_ciunplugged. 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ciThe same thing happens if the host controller is in the expected state 9162306a36Sopenharmony_cibut a USB device was unplugged and then replugged, or if a USB device 9262306a36Sopenharmony_cifails to carry out a normal resume. 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ciIf no device is now attached to the port, or if the descriptors are 9562306a36Sopenharmony_cidifferent from what the kernel remembers, then the treatment is what 9662306a36Sopenharmony_ciyou would expect. The kernel destroys the old device structure and 9762306a36Sopenharmony_cibehaves as though the old device had been unplugged and a new device 9862306a36Sopenharmony_ciplugged in. 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ciThe end result is that the USB device remains available and usable. 10162306a36Sopenharmony_ciFilesystem mounts and memory mappings are unaffected, and the world is 10262306a36Sopenharmony_cinow a good and happy place. 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ciNote that the "USB-persist" feature will be applied only to those 10562306a36Sopenharmony_cidevices for which it is enabled. You can enable the feature by doing 10662306a36Sopenharmony_ci(as root):: 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci echo 1 >/sys/bus/usb/devices/.../power/persist 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ciwhere the "..." should be filled in the with the device's ID. Disable 11162306a36Sopenharmony_cithe feature by writing 0 instead of 1. For hubs the feature is 11262306a36Sopenharmony_ciautomatically and permanently enabled and the power/persist file 11362306a36Sopenharmony_cidoesn't even exist, so you only have to worry about setting it for 11462306a36Sopenharmony_cidevices where it really matters. 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ciIs this the best solution? 11862306a36Sopenharmony_ci========================== 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ciPerhaps not. Arguably, keeping track of mounted filesystems and 12162306a36Sopenharmony_cimemory mappings across device disconnects should be handled by a 12262306a36Sopenharmony_cicentralized Logical Volume Manager. Such a solution would allow you 12362306a36Sopenharmony_cito plug in a USB flash device, create a persistent volume associated 12462306a36Sopenharmony_ciwith it, unplug the flash device, plug it back in later, and still 12562306a36Sopenharmony_cihave the same persistent volume associated with the device. As such 12662306a36Sopenharmony_ciit would be more far-reaching than USB-persist. 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ciOn the other hand, writing a persistent volume manager would be a big 12962306a36Sopenharmony_cijob and using it would require significant input from the user. This 13062306a36Sopenharmony_cisolution is much quicker and easier -- and it exists now, a giant 13162306a36Sopenharmony_cipoint in its favor! 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ciFurthermore, the USB-persist feature applies to _all_ USB devices, not 13462306a36Sopenharmony_cijust mass-storage devices. It might turn out to be equally useful for 13562306a36Sopenharmony_ciother device types, such as network interfaces. 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ciWARNING: USB-persist can be dangerous!! 13962306a36Sopenharmony_ci======================================= 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ciWhen recovering an interrupted power session the kernel does its best 14262306a36Sopenharmony_cito make sure the USB device hasn't been changed; that is, the same 14362306a36Sopenharmony_cidevice is still plugged into the port as before. But the checks 14462306a36Sopenharmony_ciaren't guaranteed to be 100% accurate. 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ciIf you replace one USB device with another of the same type (same 14762306a36Sopenharmony_cimanufacturer, same IDs, and so on) there's an excellent chance the 14862306a36Sopenharmony_cikernel won't detect the change. The serial number string and other 14962306a36Sopenharmony_cidescriptors are compared with the kernel's stored values, but this 15062306a36Sopenharmony_cimight not help since manufacturers frequently omit serial numbers 15162306a36Sopenharmony_cientirely in their devices. 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ciFurthermore it's quite possible to leave a USB device exactly the same 15462306a36Sopenharmony_ciwhile changing its media. If you replace the flash memory card in a 15562306a36Sopenharmony_ciUSB card reader while the system is asleep, the kernel will have no 15662306a36Sopenharmony_ciway to know you did it. The kernel will assume that nothing has 15762306a36Sopenharmony_cihappened and will continue to use the partition tables, inodes, and 15862306a36Sopenharmony_cimemory mappings for the old card. 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ciIf the kernel gets fooled in this way, it's almost certain to cause 16162306a36Sopenharmony_cidata corruption and to crash your system. You'll have no one to blame 16262306a36Sopenharmony_cibut yourself. 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ciFor those devices with avoid_reset_quirk attribute being set, persist 16562306a36Sopenharmony_cimaybe fail because they may morph after reset. 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ciYOU HAVE BEEN WARNED! USE AT YOUR OWN RISK! 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ciThat having been said, most of the time there shouldn't be any trouble 17062306a36Sopenharmony_ciat all. The USB-persist feature can be extremely useful. Make the 17162306a36Sopenharmony_cimost of it. 172