2009-01-01 16:04:03

by Yan Li

[permalink] [raw]
Subject: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

Hello List,

By default CONFIG_USB_SUSPEND is not enabled and today I got bite by
this when I got a new WDC My Book hard drive. I have no way to unplug
it safely, according to it's manual, with a kernel that doesn't have
CONFIG_USB_SUSPEND=y.

On it's manual read "you should unplug and remove the device only when
the light is off." On Linux unmounting it won't turn it's light off
and I can feel it's motor is still running. The only way to unplug it
properly on a Linux system, as to my knowledge, is to echo `suspend'
to `/sys/bus/usb/devices/$DEV/power/level'. And one has to have
CONFIG_USB_SUSPEND=y to do this.

On Windows and Mac OS, `safely remove this device' function effectly
turns it into `suspend' mode, and it's light off.

I'm new in this field and I understood people's concern on enabling
autosuspend. As to my understanding, `enabling suspend' and `enabling
autosuspend' should be controlled by two different knobs. I did a
quick search in LKML and haven't found specific oppose to enable USB
suspend by default, and Debian has started shipping a kernel with
it turned on since 2.6.26.

Perhaps we should consider turning it on by default? Thanks!

--
Li, Yan


2009-01-01 16:07:45

by Yan Li

[permalink] [raw]
Subject: [PATCH] enable CONFIG_USB_SUSPEND by default

Some USB devices need to be put into suspend mode before
unplugging. Without CONFIG_USB_SUSPEND we have no way to unplug them
safely.

Signed-off-by: Yan Li <[email protected]>
---
drivers/usb/core/Kconfig | 8 +++++---
1 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/core/Kconfig b/drivers/usb/core/Kconfig
index e1759d1..457ee9d 100644
--- a/drivers/usb/core/Kconfig
+++ b/drivers/usb/core/Kconfig
@@ -89,18 +89,20 @@ config USB_DYNAMIC_MINORS
config USB_SUSPEND
bool "USB selective suspend/resume and wakeup"
depends on USB && PM
+ default y
help
If you say Y here, you can use driver calls or the sysfs
"power/level" file to suspend or resume individual USB
- peripherals and to enable or disable autosuspend (see
- Documentation/usb/power-management.txt for more details).
+ peripherals and to enable or disable autosuspend. Some USB
+ devices need to be put into suspend mode before unplugging
+ (see Documentation/usb/power-management.txt for more details).

Also, USB "remote wakeup" signaling is supported, whereby some
USB devices (like keyboards and network adapters) can wake up
their parent hub. That wakeup cascades up the USB tree, and
could wake the system from states like suspend-to-RAM.

- If you are unsure about this, say N here.
+ If you are unsure about this, say Y here.

config USB_OTG
bool
--
1.5.6.5


--
Li, Yan

2009-01-01 17:30:06

by Alan Stern

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Fri, 2 Jan 2009, Yan Li wrote:

> Hello List,
>
> By default CONFIG_USB_SUSPEND is not enabled and today I got bite by
> this when I got a new WDC My Book hard drive. I have no way to unplug
> it safely, according to it's manual, with a kernel that doesn't have
> CONFIG_USB_SUSPEND=y.
>
> On it's manual read "you should unplug and remove the device only when
> the light is off."

It's not clear whether you should believe this. The manual may have
overstated things. Still, better to be safe.

> On Linux unmounting it won't turn it's light off
> and I can feel it's motor is still running. The only way to unplug it
> properly on a Linux system, as to my knowledge, is to echo `suspend'
> to `/sys/bus/usb/devices/$DEV/power/level'. And one has to have
> CONFIG_USB_SUSPEND=y to do this.
>
> On Windows and Mac OS, `safely remove this device' function effectly
> turns it into `suspend' mode, and it's light off.

Yes -- but they don't suspend the device; instead they disable its
upstream port. As far as the device is concerned, there's no
difference. However the Linux API doesn't include any way to disable a
USB port. Maybe we should add one...

> I'm new in this field and I understood people's concern on enabling
> autosuspend. As to my understanding, `enabling suspend' and `enabling
> autosuspend' should be controlled by two different knobs.

They are.

> I did a
> quick search in LKML and haven't found specific oppose to enable USB
> suspend by default, and Debian has started shipping a kernel with
> it turned on since 2.6.26.
>
> Perhaps we should consider turning it on by default? Thanks!

That is indeed the next step to take.

Alan Stern

2009-01-01 17:33:54

by Alexander E. Patrakov

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely



Yan Li wrote:
> Hello List,
>
> By default CONFIG_USB_SUSPEND is not enabled and today I got bite by
> this when I got a new WDC My Book hard drive. I have no way to unplug
> it safely, according to it's manual, with a kernel that doesn't have
> CONFIG_USB_SUSPEND=y.
>
> On it's manual read "you should unplug and remove the device only when
> the light is off." On Linux unmounting it won't turn it's light off
> and I can feel it's motor is still running. The only way to unplug it
> properly on a Linux system, as to my knowledge, is to echo `suspend'
> to `/sys/bus/usb/devices/$DEV/power/level'. And one has to have
> CONFIG_USB_SUSPEND=y to do this.
>
> On Windows and Mac OS, `safely remove this device' function effectly
> turns it into `suspend' mode, and it's light off.

You have obviously tested only Windows XP, because Vista doesn't cut the
power for at least some USB devices. And congratulations for obtaining a
Vista-incompatible hard drive (of course, joking).

BTW, does the "eject /dev/$DEV" command help?

--
Alexander E. Patrakov

2009-01-01 17:45:49

by Alan Stern

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Thu, 1 Jan 2009, Alexander E. Patrakov wrote:

> Yan Li wrote:
> > Hello List,
> >
> > By default CONFIG_USB_SUSPEND is not enabled and today I got bite by
> > this when I got a new WDC My Book hard drive. I have no way to unplug
> > it safely, according to it's manual, with a kernel that doesn't have
> > CONFIG_USB_SUSPEND=y.
> >
> > On it's manual read "you should unplug and remove the device only when
> > the light is off." On Linux unmounting it won't turn it's light off
> > and I can feel it's motor is still running. The only way to unplug it
> > properly on a Linux system, as to my knowledge, is to echo `suspend'
> > to `/sys/bus/usb/devices/$DEV/power/level'. And one has to have
> > CONFIG_USB_SUSPEND=y to do this.
> >
> > On Windows and Mac OS, `safely remove this device' function effectly
> > turns it into `suspend' mode, and it's light off.
>
> You have obviously tested only Windows XP, because Vista doesn't cut the
> power for at least some USB devices.

Yan never said that Windows cuts any power; he only said that the light
turns off. This is accomplished by disabling the USB port, not by
cutting power.

> And congratulations for obtaining a
> Vista-incompatible hard drive (of course, joking).
>
> BTW, does the "eject /dev/$DEV" command help?

I wouldn't expect it to help, since it doesn't disable the port or put
the device into suspend mode.

Alan Stern

2009-01-01 19:15:19

by Robert Hancock

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

Yan Li wrote:
> Hello List,
>
> By default CONFIG_USB_SUSPEND is not enabled and today I got bite by
> this when I got a new WDC My Book hard drive. I have no way to unplug
> it safely, according to it's manual, with a kernel that doesn't have
> CONFIG_USB_SUSPEND=y.
>
> On it's manual read "you should unplug and remove the device only when
> the light is off." On Linux unmounting it won't turn it's light off
> and I can feel it's motor is still running. The only way to unplug it
> properly on a Linux system, as to my knowledge, is to echo `suspend'
> to `/sys/bus/usb/devices/$DEV/power/level'. And one has to have
> CONFIG_USB_SUSPEND=y to do this.
>
> On Windows and Mac OS, `safely remove this device' function effectly
> turns it into `suspend' mode, and it's light off.
>
> I'm new in this field and I understood people's concern on enabling
> autosuspend. As to my understanding, `enabling suspend' and `enabling
> autosuspend' should be controlled by two different knobs. I did a
> quick search in LKML and haven't found specific oppose to enable USB
> suspend by default, and Debian has started shipping a kernel with
> it turned on since 2.6.26.
>
> Perhaps we should consider turning it on by default? Thanks!

It's probably not a bad idea. I'm not sure how big of a problem it
actually is, though. As long as the filesystem is unmounted before
unplug you'll probably be OK (unless you ripped out the cable/power so
quickly afterwards that there was still data in the write cache).

Ideally before disconnecting a removable hard drive one should flush the
write cache and park the heads (the latter being somewhat important for
2.5" drives in order to avoid emergency head unloads that would reduce
the life of the drive). I believe that there's a command one can echo
into the sd driver in sysfs somewhere that will trigger this.. Therefore
the right sequence would be:

-unmount filesystem
-remove SCSI disk (synchronize cache and stop unit)
-suspend SCSI device

Thing is that some USB storage devices don't support both of those
commands (I've got a USB-to-IDE enclosure with a Genesys chipset that
doesn't translate SCSI START STOP UNIT into an IDE Standby Immediate
command as it really should, think it just throws "invalid field in CDB"
errors). However if we've done the above we've really done the best we
can do..

2009-01-02 09:56:48

by Tino Keitel

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Fri, Jan 02, 2009 at 00:03:43 +0800, Yan Li wrote:
> Hello List,
>
> By default CONFIG_USB_SUSPEND is not enabled and today I got bite by
> this when I got a new WDC My Book hard drive. I have no way to unplug
> it safely, according to it's manual, with a kernel that doesn't have
> CONFIG_USB_SUSPEND=y.
>
> On it's manual read "you should unplug and remove the device only when
> the light is off." On Linux unmounting it won't turn it's light off
> and I can feel it's motor is still running. The only way to unplug it
> properly on a Linux system, as to my knowledge, is to echo `suspend'
> to `/sys/bus/usb/devices/$DEV/power/level'. And one has to have
> CONFIG_USB_SUSPEND=y to do this.

With a WD My Passport drive (2,5"), I need to use sg_start --stop --pc=3
to stop the disk before unplugging it. Maybe this works with your
drive, too. This is also required by some Firewire hard disks. Maybe
the eject command should be extended to use this method, as it is used
also by HAL to eject hotplug devices.

Regards,
Tino

2009-01-02 13:24:24

by Yan Li

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Thu, Jan 01, 2009 at 12:29:51PM -0500, Alan Stern wrote:
> On Fri, 2 Jan 2009, Yan Li wrote:
> > On it's manual read "you should unplug and remove the device only when
> > the light is off."
>
> It's not clear whether you should believe this. The manual may have
> overstated things. Still, better to be safe.

Yeah, that maybe true. Better to be safe. And we can't tell users just
to ignore the warnings on the manual.

> > On Windows and Mac OS, `safely remove this device' function effectly
> > turns it into `suspend' mode, and it's light off.
>
> Yes -- but they don't suspend the device; instead they disable its
> upstream port. As far as the device is concerned, there's no
> difference. However the Linux API doesn't include any way to disable a
> USB port. Maybe we should add one...

Definitely.

> > I did a
> > quick search in LKML and haven't found specific oppose to enable USB
> > suspend by default, and Debian has started shipping a kernel with
> > it turned on since 2.6.26.
> >
> > Perhaps we should consider turning it on by default? Thanks!
>
> That is indeed the next step to take.

Hope to see this happen soon.

--
Li, Yan

2009-01-02 13:38:14

by Yan Li

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Thu, Jan 01, 2009 at 10:32:40PM +0500, Alexander E. Patrakov wrote:
> Yan Li wrote:
>> On Windows and Mac OS, `safely remove this device' function effectly
>> turns it into `suspend' mode, and it's light off.
>
> You have obviously tested only Windows XP, because Vista doesn't cut the
> power for at least some USB devices. And congratulations for obtaining a
> Vista-incompatible hard drive (of course, joking).

To be honest I tested neither Windows, I just picked up other Linux
user's complaints (pretty much online) who are using both Windows and
Linux and thus can compare.

Curiously, is this change in behavior btw XP and Vista another blunder
MSFT made?

> BTW, does the "eject /dev/$DEV" command help?

Nope. Unlike iPOD this device doesn't honor eject.

--
Li, Yan

2009-01-02 13:47:21

by Alexander E. Patrakov

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

Yan Li wrote:
> To be honest I tested neither Windows, I just picked up other Linux
> user's complaints (pretty much online) who are using both Windows and
> Linux and thus can compare.
>
> Curiously, is this change in behavior btw XP and Vista another blunder
> MSFT made?

I don't know whether it is intentional. I don't have Vista installed on
my computer (but had a beta of it on my old computer). However, Google
search result for "vista usb flash light" suggests that other people see
the change, too.

--
Alexander E. Patrakov

2009-01-02 14:00:42

by Yan Li

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Fri, Jan 02, 2009 at 10:56:43AM +0100, Tino Keitel wrote:
> With a WD My Passport drive (2,5"), I need to use sg_start --stop --pc=3
> to stop the disk before unplugging it. Maybe this works with your
> drive, too. This is also required by some Firewire hard disks. Maybe
> the eject command should be extended to use this method, as it is used
> also by HAL to eject hotplug devices.

Hum... I grepped my hal and hal-info packages but haven't found any
scripts using `sg_*'. I'm using hal package 0.5.11-6 from Debian
testing.

Now I know 3 methods supposed can be used to shut the device:
1. (Alan Stern said Windows use this) cut the USB port's power
2. send STOP SCSI command to stop it
3. put it into suspend mode

For the 1st method I have no much experience with Windows so I can't
verify. But now I doubt it. Since there are many early USB devices
that still show light on after you "safely remove" it from Windows. I
guess the power is still supplied but the device is send a command to
be shutdown or suspended.

For the 2nd and 3rd methods, I don't know which one is better, hope
some experts can give advice.

Whatever, I think the suspend mode is a standard USB design and the
kernel should support it by default so we should enable
CONFIG_USB_SUSPEND.

--
Li, Yan

2009-01-02 14:09:04

by Tino Keitel

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Fri, Jan 02, 2009 at 22:00:23 +0800, Yan Li wrote:
> On Fri, Jan 02, 2009 at 10:56:43AM +0100, Tino Keitel wrote:
> > With a WD My Passport drive (2,5"), I need to use sg_start --stop --pc=3
> > to stop the disk before unplugging it. Maybe this works with your
> > drive, too. This is also required by some Firewire hard disks. Maybe
> > the eject command should be extended to use this method, as it is used
> > also by HAL to eject hotplug devices.
>
> Hum... I grepped my hal and hal-info packages but haven't found any
> scripts using `sg_*'. I'm using hal package 0.5.11-6 from Debian
> testing.

HAL uses /usr/bin/eject, which doesn't set the power condition (as
sg_start --pc=X does). So eject (and HAL) doesn't work with disks that
require to set the power condition.

Just try sg_start --stop --pc=3 /dev/sdX to see if it works for you.

Regards,
Tino

2009-01-02 14:28:49

by Yan Li

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Fri, Jan 02, 2009 at 10:00:23PM +0800, Yan Li wrote:
> Now I know 3 methods supposed can be used to shut the device:
> 1. (Alan Stern said Windows use this) cut the USB port's power
> 2. send STOP SCSI command to stop it
> 3. put it into suspend mode
>
> For the 1st method I have no much experience with Windows so I can't
> verify. But now I doubt it. Since there are many early USB devices
> that still show light on after you "safely remove" it from Windows. I
> guess the power is still supplied but the device is send a command to
> be shutdown or suspended.

Sorry, Alan have not said `cut the power'. He said `disable the port'.

--
Li, Yan

2009-01-02 15:49:18

by Mark Lord

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

Tino Keitel wrote:
> On Fri, Jan 02, 2009 at 00:03:43 +0800, Yan Li wrote:
>> Hello List,
>>
>> By default CONFIG_USB_SUSPEND is not enabled and today I got bite by
>> this when I got a new WDC My Book hard drive. I have no way to unplug
>> it safely, according to it's manual, with a kernel that doesn't have
>> CONFIG_USB_SUSPEND=y.
>>
>> On it's manual read "you should unplug and remove the device only when
>> the light is off." On Linux unmounting it won't turn it's light off
>> and I can feel it's motor is still running. The only way to unplug it
>> properly on a Linux system, as to my knowledge, is to echo `suspend'
>> to `/sys/bus/usb/devices/$DEV/power/level'. And one has to have
>> CONFIG_USB_SUSPEND=y to do this.
>
> With a WD My Passport drive (2,5"), I need to use sg_start --stop --pc=3
> to stop the disk before unplugging it. Maybe this works with your
> drive, too. This is also required by some Firewire hard disks. Maybe
> the eject command should be extended to use this method, as it is used
> also by HAL to eject hotplug devices.
..

Incidently, the WD Passport drives support SG_IO,
so things like hdparm and smartctl can work with them as well.

Cheers

2009-01-02 15:49:47

by Mark Lord

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

Mark Lord wrote:
> Tino Keitel wrote:
>> On Fri, Jan 02, 2009 at 00:03:43 +0800, Yan Li wrote:
>>> Hello List,
>>>
>>> By default CONFIG_USB_SUSPEND is not enabled and today I got bite by
>>> this when I got a new WDC My Book hard drive. I have no way to unplug
>>> it safely, according to it's manual, with a kernel that doesn't have
>>> CONFIG_USB_SUSPEND=y.
>>>
>>> On it's manual read "you should unplug and remove the device only when
>>> the light is off." On Linux unmounting it won't turn it's light off
>>> and I can feel it's motor is still running. The only way to unplug it
>>> properly on a Linux system, as to my knowledge, is to echo `suspend'
>>> to `/sys/bus/usb/devices/$DEV/power/level'. And one has to have
>>> CONFIG_USB_SUSPEND=y to do this.
>>
>> With a WD My Passport drive (2,5"), I need to use sg_start --stop --pc=3
>> to stop the disk before unplugging it. Maybe this works with your
>> drive, too. This is also required by some Firewire hard disks. Maybe
>> the eject command should be extended to use this method, as it is used
>> also by HAL to eject hotplug devices.
> ..
>
> Incidently, the WD Passport drives support SG_IO,
> so things like hdparm and smartctl can work with them as well.
..

s/SG_IO/SG_IO+SAT/

2009-01-02 16:04:55

by Alan Stern

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Fri, 2 Jan 2009, Yan Li wrote:

> Now I know 3 methods supposed can be used to shut the device:
> 1. (Alan Stern said Windows use this) cut the USB port's power

I said no such thing! In fact, I said exactly the opposite: Windows
does _not_ cut the port's power. Instead it disables the port.

> 2. send STOP SCSI command to stop it

Note that this is different. The START-STOP command is used to spin-up
or spin-down a disk. It does not affect the state of the data link.

> 3. put it into suspend mode

As far as the device is concerned, there is essentially no difference
between 1 and 3. When the device's upstream port is disabled, the
device must go into suspend mode.

The best approach is to send a SYNCHRONIZE CACHE command followed by
START-STOP (if the device supports it), and then to disable or suspend
the port. In Linux, those two commands will be sent automatically if
you unbind the device from usb-storage. The suspend has to be done
manually unless you have set up a udev rule (or something equivalent)
to enable autosuspend for the device. Of course, this requires
CONFIG_USB_SUSPEND.

Alan Stern

2009-01-02 18:16:05

by Tino Keitel

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Fri, Jan 02, 2009 at 10:51:05 -0500, Mark Lord wrote:
> Mark Lord wrote:

[...]

>> Incidently, the WD Passport drives support SG_IO,
>> so things like hdparm and smartctl can work with them as well.
> ..
>
> s/SG_IO/SG_IO+SAT/

The issue I noticed with the WD Passport drives is that the hard disk
won't spin down with /usr/bin/eject, which is used by HAL if the user
ejects the device (i.e. via his desktop environment). The WD Passport
drives need to be set into a sleep power condition. Something
like "sg_start --pc=3 /dev/sdX" needs to be used in this case.

I just checked hdparm -Y, and this works too.

Regards,
Tino

2009-01-03 02:24:35

by Yan Li

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Fri, Jan 02, 2009 at 07:15:38PM +0100, Tino Keitel wrote:
> The issue I noticed with the WD Passport drives is that the hard disk
> won't spin down with /usr/bin/eject, which is used by HAL if the user
> ejects the device (i.e. via his desktop environment). The WD Passport
> drives need to be set into a sleep power condition. Something
> like "sg_start --pc=3 /dev/sdX" needs to be used in this case.
>
> I just checked hdparm -Y, and this works too.

STOP SCSI command spins down the drive but the device is still `on'.

The only way I found on Linux to shut down it's power is to put it
into `suspend' mode.

--
Li, Yan

2009-01-03 02:33:25

by Yan Li

[permalink] [raw]
Subject: Re: Enable CONFIG_USB_SUSPEND by default or some mobile HD can't be unplugged safely

On Fri, Jan 02, 2009 at 11:04:40AM -0500, Alan Stern wrote:
> > Now I know 3 methods supposed can be used to shut the device:
> > 1. (Alan Stern said Windows use this) cut the USB port's power
>
> I said no such thing! In fact, I said exactly the opposite: Windows
> does _not_ cut the port's power. Instead it disables the port.

Sorry, my mistake.

> The best approach is to send a SYNCHRONIZE CACHE command followed by
> START-STOP (if the device supports it), and then to disable or suspend
> the port. In Linux, those two commands will be sent automatically if
> you unbind the device from usb-storage. The suspend has to be done
> manually unless you have set up a udev rule (or something equivalent)
> to enable autosuspend for the device. Of course, this requires
> CONFIG_USB_SUSPEND.

Quite clear, thanks.

--
Li, Yan