[PATCH] scsi/sd: fix suspend with USB-connected Android phone (one line)

From: Charles Hannum
Date: Wed May 11 2011 - 18:35:12 EST


Short version: My laptop doesn't suspend when my Android phone is
connected and has been “ejected”.

Long version:

Android phones connect as USB mass storage devices.  After the “Turn
on USB storage” button has been clicked, there are a few different
ways to detach the “disk”:

1) pull the cable
2) click “Turn off USB storage”
3) “eject” the device

In cases 2 & 3, the USB device is still attached to the system, but
will now return MEDIUM NOT PRESENT for many commands, including
SYNCHRONIZE CACHE—basically it acts like any device with removable
media.  However, the act of the “media” being removed does not
invalidate sdkp->WCE; therefore sd_shutdown() and sd_suspend() still
call sd_sync_cache(), which *fails* because it gets a MEDIUM NOT
PRESENT sense code.  In the sd_suspend() case, this causes the entire
suspend to fail, and the laptop rewakes immediately.

There are a few different ways to fix this; e.g. one could
specifically test media_not_present() if a sense code is returned in
sd_sync_cache().  However, the following patch seems simpler, and
avoids calling sd_sync_cache() at all in this case.  sdkp->WCE will be
reset when new medium is recognized and sd_read_cache_type() is
called.  Note this code always gets called—it's in the same path as
sd_read_capacity(), which has to be called for the device to be usable
again; thus the patch is inherently safe.

Kernel tested: 2.6.38 (Ubuntu Natty)
--- linux-2.6.38/drivers/scsi/sd.c.orig 2011-05-11 18:08:44.969497976 -0400
+++ linux-2.6.38/drivers/scsi/sd.c 2011-05-11 15:26:15.439610760 -0400
@@ -996,6 +996,7 @@
if (sdkp->device->removable) {
sdkp->media_present = 0;
sdkp->capacity = 0;
+ sdkp->WCE = 0;
}
}