blob: d56cb1a1155073987fc2ca108c766f03100d35cf [file] [log] [blame]
Alan Stern0458d5b2007-05-04 11:52:20 -04001 USB device persistence during system suspend
2
3 Alan Stern <stern@rowland.harvard.edu>
4
Alan Sternfeccc302008-03-03 15:15:59 -05005 September 2, 2006 (Updated February 25, 2008)
Alan Stern0458d5b2007-05-04 11:52:20 -04006
7
8 What is the problem?
9
10According to the USB specification, when a USB bus is suspended the
11bus must continue to supply suspend current (around 1-5 mA). This
12is so that devices can maintain their internal state and hubs can
13detect connect-change events (devices being plugged in or unplugged).
14The technical term is "power session".
15
16If a USB device's power session is interrupted then the system is
17required to behave as though the device has been unplugged. It's a
18conservative approach; in the absence of suspend current the computer
19has no way to know what has actually happened. Perhaps the same
20device is still attached or perhaps it was removed and a different
21device plugged into the port. The system must assume the worst.
22
23By default, Linux behaves according to the spec. If a USB host
24controller loses power during a system suspend, then when the system
25wakes up all the devices attached to that controller are treated as
26though they had disconnected. This is always safe and it is the
27"officially correct" thing to do.
28
29For many sorts of devices this behavior doesn't matter in the least.
30If the kernel wants to believe that your USB keyboard was unplugged
31while the system was asleep and a new keyboard was plugged in when the
32system woke up, who cares? It'll still work the same when you type on
33it.
34
35Unfortunately problems _can_ arise, particularly with mass-storage
36devices. The effect is exactly the same as if the device really had
37been unplugged while the system was suspended. If you had a mounted
38filesystem on the device, you're out of luck -- everything in that
39filesystem is now inaccessible. This is especially annoying if your
40root filesystem was located on the device, since your system will
41instantly crash.
42
43Loss of power isn't the only mechanism to worry about. Anything that
44interrupts a power session will have the same effect. For example,
45even though suspend current may have been maintained while the system
46was asleep, on many systems during the initial stages of wakeup the
47firmware (i.e., the BIOS) resets the motherboard's USB host
48controllers. Result: all the power sessions are destroyed and again
49it's as though you had unplugged all the USB devices. Yes, it's
50entirely the BIOS's fault, but that doesn't do _you_ any good unless
51you can convince the BIOS supplier to fix the problem (lots of luck!).
52
53On many systems the USB host controllers will get reset after a
54suspend-to-RAM. On almost all systems, no suspend current is
Alan Sternb41a60e2007-05-30 15:39:33 -040055available during hibernation (also known as swsusp or suspend-to-disk).
56You can check the kernel log after resuming to see if either of these
57has happened; look for lines saying "root hub lost power or was reset".
Alan Stern0458d5b2007-05-04 11:52:20 -040058
59In practice, people are forced to unmount any filesystems on a USB
60device before suspending. If the root filesystem is on a USB device,
61the system can't be suspended at all. (All right, it _can_ be
62suspended -- but it will crash as soon as it wakes up, which isn't
63much better.)
64
65
66 What is the solution?
67
Alan Sternfeccc302008-03-03 15:15:59 -050068The kernel includes a feature called USB-persist. It tries to work
69around these issues by allowing the core USB device data structures to
70persist across a power-session disruption.
71
Alan Stern0458d5b2007-05-04 11:52:20 -040072It works like this. If the kernel sees that a USB host controller is
73not in the expected state during resume (i.e., if the controller was
74reset or otherwise had lost power) then it applies a persistence check
Alan Sternb41a60e2007-05-30 15:39:33 -040075to each of the USB devices below that controller for which the
76"persist" attribute is set. It doesn't try to resume the device; that
77can't work once the power session is gone. Instead it issues a USB
78port reset and then re-enumerates the device. (This is exactly the
79same thing that happens whenever a USB device is reset.) If the
80re-enumeration shows that the device now attached to that port has the
81same descriptors as before, including the Vendor and Product IDs, then
82the kernel continues to use the same device structure. In effect, the
83kernel treats the device as though it had merely been reset instead of
Alan Sternfeccc302008-03-03 15:15:59 -050084unplugged. The same thing happens if the host controller is in the
85expected state but a USB device was unplugged and then replugged.
Alan Stern0458d5b2007-05-04 11:52:20 -040086
87If no device is now attached to the port, or if the descriptors are
88different from what the kernel remembers, then the treatment is what
89you would expect. The kernel destroys the old device structure and
90behaves as though the old device had been unplugged and a new device
Alan Sternfeccc302008-03-03 15:15:59 -050091plugged in.
Alan Stern0458d5b2007-05-04 11:52:20 -040092
93The end result is that the USB device remains available and usable.
94Filesystem mounts and memory mappings are unaffected, and the world is
95now a good and happy place.
96
Alan Sternfeccc302008-03-03 15:15:59 -050097Note that the "USB-persist" feature will be applied only to those
98devices for which it is enabled. You can enable the feature by doing
99(as root):
Alan Sternb41a60e2007-05-30 15:39:33 -0400100
101 echo 1 >/sys/bus/usb/devices/.../power/persist
102
103where the "..." should be filled in the with the device's ID. Disable
104the feature by writing 0 instead of 1. For hubs the feature is
Alan Sternfeccc302008-03-03 15:15:59 -0500105automatically and permanently enabled and the power/persist file
106doesn't even exist, so you only have to worry about setting it for
107devices where it really matters.
Alan Sternb41a60e2007-05-30 15:39:33 -0400108
Alan Stern0458d5b2007-05-04 11:52:20 -0400109
110 Is this the best solution?
111
112Perhaps not. Arguably, keeping track of mounted filesystems and
113memory mappings across device disconnects should be handled by a
114centralized Logical Volume Manager. Such a solution would allow you
115to plug in a USB flash device, create a persistent volume associated
116with it, unplug the flash device, plug it back in later, and still
117have the same persistent volume associated with the device. As such
Alan Sternfeccc302008-03-03 15:15:59 -0500118it would be more far-reaching than USB-persist.
Alan Stern0458d5b2007-05-04 11:52:20 -0400119
120On the other hand, writing a persistent volume manager would be a big
121job and using it would require significant input from the user. This
122solution is much quicker and easier -- and it exists now, a giant
123point in its favor!
124
Alan Sternfeccc302008-03-03 15:15:59 -0500125Furthermore, the USB-persist feature applies to _all_ USB devices, not
Alan Stern0458d5b2007-05-04 11:52:20 -0400126just mass-storage devices. It might turn out to be equally useful for
127other device types, such as network interfaces.
128
129
Alan Sternfeccc302008-03-03 15:15:59 -0500130 WARNING: USB-persist can be dangerous!!
Alan Stern0458d5b2007-05-04 11:52:20 -0400131
132When recovering an interrupted power session the kernel does its best
133to make sure the USB device hasn't been changed; that is, the same
134device is still plugged into the port as before. But the checks
135aren't guaranteed to be 100% accurate.
136
137If you replace one USB device with another of the same type (same
138manufacturer, same IDs, and so on) there's an excellent chance the
Alan Sterneb764c42008-03-03 15:16:04 -0500139kernel won't detect the change. The serial number string and other
140descriptors are compared with the kernel's stored values, but this
141might not help since manufacturers frequently omit serial numbers
142entirely in their devices.
Alan Stern0458d5b2007-05-04 11:52:20 -0400143
144Furthermore it's quite possible to leave a USB device exactly the same
145while changing its media. If you replace the flash memory card in a
146USB card reader while the system is asleep, the kernel will have no
147way to know you did it. The kernel will assume that nothing has
148happened and will continue to use the partition tables, inodes, and
149memory mappings for the old card.
150
151If the kernel gets fooled in this way, it's almost certain to cause
152data corruption and to crash your system. You'll have no one to blame
153but yourself.
154
155YOU HAVE BEEN WARNED! USE AT YOUR OWN RISK!
156
157That having been said, most of the time there shouldn't be any trouble
Alan Sternfeccc302008-03-03 15:15:59 -0500158at all. The USB-persist feature can be extremely useful. Make the
159most of it.