172 lines · plain
1.. _usb-persist:2 3USB device persistence during system suspend4~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~5 6:Author: Alan Stern <stern@rowland.harvard.edu>7:Date: September 2, 2006 (Updated February 25, 2008)8 9 10What is the problem?11====================12 13According to the USB specification, when a USB bus is suspended the14bus must continue to supply suspend current (around 1-5 mA). This15is so that devices can maintain their internal state and hubs can16detect connect-change events (devices being plugged in or unplugged).17The technical term is "power session".18 19If a USB device's power session is interrupted then the system is20required to behave as though the device has been unplugged. It's a21conservative approach; in the absence of suspend current the computer22has no way to know what has actually happened. Perhaps the same23device is still attached or perhaps it was removed and a different24device plugged into the port. The system must assume the worst.25 26By default, Linux behaves according to the spec. If a USB host27controller loses power during a system suspend, then when the system28wakes up all the devices attached to that controller are treated as29though they had disconnected. This is always safe and it is the30"officially correct" thing to do.31 32For many sorts of devices this behavior doesn't matter in the least.33If the kernel wants to believe that your USB keyboard was unplugged34while the system was asleep and a new keyboard was plugged in when the35system woke up, who cares? It'll still work the same when you type on36it.37 38Unfortunately problems _can_ arise, particularly with mass-storage39devices. The effect is exactly the same as if the device really had40been unplugged while the system was suspended. If you had a mounted41filesystem on the device, you're out of luck -- everything in that42filesystem is now inaccessible. This is especially annoying if your43root filesystem was located on the device, since your system will44instantly crash.45 46Loss of power isn't the only mechanism to worry about. Anything that47interrupts a power session will have the same effect. For example,48even though suspend current may have been maintained while the system49was asleep, on many systems during the initial stages of wakeup the50firmware (i.e., the BIOS) resets the motherboard's USB host51controllers. Result: all the power sessions are destroyed and again52it's as though you had unplugged all the USB devices. Yes, it's53entirely the BIOS's fault, but that doesn't do _you_ any good unless54you can convince the BIOS supplier to fix the problem (lots of luck!).55 56On many systems the USB host controllers will get reset after a57suspend-to-RAM. On almost all systems, no suspend current is58available during hibernation (also known as swsusp or suspend-to-disk).59You can check the kernel log after resuming to see if either of these60has happened; look for lines saying "root hub lost power or was reset".61 62In practice, people are forced to unmount any filesystems on a USB63device before suspending. If the root filesystem is on a USB device,64the system can't be suspended at all. (All right, it _can_ be65suspended -- but it will crash as soon as it wakes up, which isn't66much better.)67 68 69What is the solution?70=====================71 72The kernel includes a feature called USB-persist. It tries to work73around these issues by allowing the core USB device data structures to74persist across a power-session disruption.75 76It works like this. If the kernel sees that a USB host controller is77not in the expected state during resume (i.e., if the controller was78reset or otherwise had lost power) then it applies a persistence check79to each of the USB devices below that controller for which the80"persist" attribute is set. It doesn't try to resume the device; that81can't work once the power session is gone. Instead it issues a USB82port reset and then re-enumerates the device. (This is exactly the83same thing that happens whenever a USB device is reset.) If the84re-enumeration shows that the device now attached to that port has the85same descriptors as before, including the Vendor and Product IDs, then86the kernel continues to use the same device structure. In effect, the87kernel treats the device as though it had merely been reset instead of88unplugged.89 90The same thing happens if the host controller is in the expected state91but a USB device was unplugged and then replugged, or if a USB device92fails to carry out a normal resume.93 94If no device is now attached to the port, or if the descriptors are95different from what the kernel remembers, then the treatment is what96you would expect. The kernel destroys the old device structure and97behaves as though the old device had been unplugged and a new device98plugged in.99 100The end result is that the USB device remains available and usable.101Filesystem mounts and memory mappings are unaffected, and the world is102now a good and happy place.103 104Note that the "USB-persist" feature will be applied only to those105devices for which it is enabled. You can enable the feature by doing106(as root)::107 108 echo 1 >/sys/bus/usb/devices/.../power/persist109 110where the "..." should be filled in the with the device's ID. Disable111the feature by writing 0 instead of 1. For hubs the feature is112automatically and permanently enabled and the power/persist file113doesn't even exist, so you only have to worry about setting it for114devices where it really matters.115 116 117Is this the best solution?118==========================119 120Perhaps not. Arguably, keeping track of mounted filesystems and121memory mappings across device disconnects should be handled by a122centralized Logical Volume Manager. Such a solution would allow you123to plug in a USB flash device, create a persistent volume associated124with it, unplug the flash device, plug it back in later, and still125have the same persistent volume associated with the device. As such126it would be more far-reaching than USB-persist.127 128On the other hand, writing a persistent volume manager would be a big129job and using it would require significant input from the user. This130solution is much quicker and easier -- and it exists now, a giant131point in its favor!132 133Furthermore, the USB-persist feature applies to _all_ USB devices, not134just mass-storage devices. It might turn out to be equally useful for135other device types, such as network interfaces.136 137 138WARNING: USB-persist can be dangerous!!139=======================================140 141When recovering an interrupted power session the kernel does its best142to make sure the USB device hasn't been changed; that is, the same143device is still plugged into the port as before. But the checks144aren't guaranteed to be 100% accurate.145 146If you replace one USB device with another of the same type (same147manufacturer, same IDs, and so on) there's an excellent chance the148kernel won't detect the change. The serial number string and other149descriptors are compared with the kernel's stored values, but this150might not help since manufacturers frequently omit serial numbers151entirely in their devices.152 153Furthermore it's quite possible to leave a USB device exactly the same154while changing its media. If you replace the flash memory card in a155USB card reader while the system is asleep, the kernel will have no156way to know you did it. The kernel will assume that nothing has157happened and will continue to use the partition tables, inodes, and158memory mappings for the old card.159 160If the kernel gets fooled in this way, it's almost certain to cause161data corruption and to crash your system. You'll have no one to blame162but yourself.163 164For those devices with avoid_reset_quirk attribute being set, persist165maybe fail because they may morph after reset.166 167YOU HAVE BEEN WARNED! USE AT YOUR OWN RISK!168 169That having been said, most of the time there shouldn't be any trouble170at all. The USB-persist feature can be extremely useful. Make the171most of it.172