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