# Powering down a specific USB port?

## The_Great_Sephiroth

I am doing more and more data recovery where the attached disk will work for ten minutes then disappear until it cools down. This means I get to babysit it and unplug the USB connection every time it disappears, wait ten minutes, reattach, and repeat. What I would like to do is write a small BASH script to start the recovery, then after five or ten minutes, stop the recovery and issue CTRL+C to ddrescue, and turn the USB port completely off. It will wait ten minutes, then repeat.

I can figure out the rest, but how do I turn off a USB port completely using a script?

----------

## eccerr0r

Depends on the hardware, some hardware it's not possible to completely remove power from the device.

----------

## The_Great_Sephiroth

Well where would I start? This is an older laptop with Intel USB2.0 ports.

----------

## szatox

AFAIK you can send a command to USB device that demands it to shut down, but some devices will not comply. It's independent of the hub, so you can't e.g. control an USB flashlight this way - it has connectors for power supply, but not for data lines and it will not receive your command.

You could try powering off the whole hub though, if you're not worried about other devices connected there. I'd expect this to actually cut any power that would otherwise be passed to the external connectors.

----------

## eccerr0r

 *The_Great_Sephiroth wrote:*   

> Well where would I start? This is an older laptop with Intel USB2.0 ports.

 

If it's an "expensive" laptop like a business class machine, there's a possibility there is a switching transistor per port to turn it on and off; if not and it's a "consumer" low end machine, then likely you can't - it's just fused at most.

Since the hardware is not common among all machines, driver support is spotty and a machine specific driver may be necessary.  You're probably going to have to make your own external power switcher dongle most likely.

----------

## The_Great_Sephiroth

It is a Dell Latitude E6400. I believe they were $2500 new, without the nVidia Quadro card, which mine lacks. I do not mind powering down the hub if needed. I need to simulate unplugging the USB cable so the externally attached drive loses power and can cool inside the fridge. I put these drives in a fridge with a 10ft active USB 2.0 cable coming out. It has helped save some data before. I just hate baby-sitting it!

----------

## chithanh

You need a USB hub that supports per port power switching. Intel USB root hubs don't support power switching in my experience. However, many USB hubs, even cheap ones, support this. Unfortunately this feature usually not mentioned in marketing material, so you may have to try a bunch until you find one that works.

lsusb -v will tell you whether the hub advertises power switching.

----------

## The_Great_Sephiroth

Could I shutdown the entire Intel USB hub somehow?

----------

## eccerr0r

 *chithanh wrote:*   

> However, many USB hubs, even cheap ones, support this.

 

Of the USB hubs that I've taken apart, this is not the case.  All of the cheap hubs I've opened up have power hooked straight up to the main power source - some aren't even fused.  Finding one that have switchable ports will be tricky without opening them.

----------

## NeddySeagoon

The_Great_Sephiroth,

hdparm may let you power down the drive.  See the -S option.

That might even be enough to do 10 min access (or whatever), wait 10 min, and have the drive spin down.

----------

## szatox

Since the root hub tends to be a PCI device:

 *Quote:*   

>  Try echo 0 > /sys/bus/pci/slot/$N/power for appropriate slot

 

----------

## The_Great_Sephiroth

Neddy, I did not think about that. I will try that first. Thanks!

----------

## chithanh

 *eccerr0r wrote:*   

>  *chithanh wrote:*   However, many USB hubs, even cheap ones, support this. 
> 
> Of the USB hubs that I've taken apart, this is not the case.  All of the cheap hubs I've opened up have power hooked straight up to the main power source - some aren't even fused.  Finding one that have switchable ports will be tricky without opening them.

 Indeed, you need to buy them randomly until you get your hands at one that works.

Here is a German forum where one user searched for USB hubs that support power switching. Cheapest with working power switching was a 5€ hub from Delock.

https://www.mikrocontroller.net/topic/311841

----------

## niceflower

If the usb is connected with a cable if it is on the front of your computer, you can disconnect it.

If the usb is on the motherboard you can try to desolder it that is a bit more tricky.

Or even if you have multiple usb ports lets say 1.0 - 2.0 - 3.0 you can use one of them, and denode the other usb in kernel.

----------

## The_Great_Sephiroth

I have a script almost done for this. Not sure how to do the loop though. I use "timeout" to time the length of recovery and then sleep with the HDD powered down, but I am not sure to how to figure out when ddrescue finishes to break the loop.

```

# Tell the disk to spin-down after five seconds of non-use

hdparm -S 1

while [ <ddrescue is not finished> ];  do

  timeout 300 ddrescue /dev/sdX /home/user/sdX.img /home/user/sdX.map

  <get return value in case it finished?>

  sleep 600

done

```

Not sure how to figure this one out. My guess is to set some variable to zero and somehow catch the ddrescue return value and if that value equals "I am done" then we have broken the loop.

----------

## krinn

none suggest it if i'm not wrong, but you could had set your usb as module and unload/reload it, it will power down the device. (if the hdd is powered only by usb)

----------

## The_Great_Sephiroth

I knew of that solution but I build all hardware drivers for hardware permanently attached to my laptop into my kernel. I would prefer to keep it that way, and if I can figure this script issue out I may have a solution.

*UPDATE*

I believe I have it.

```

#!/bin/bash

# Set variables

Reading = true

# Check parameters

if [ "$#" -ne 6 ]; then

        echo "Usage: $0 <device> <image> <map> <sector size> <read secs> <cooldown secs>"

        echo "Ex: $0 /dev/sdb /home/user/recover.img /home/user/recover.map 512 750 1500"

        echo "Ex: $0 /dev/sdb2 /home/user/sdb2.img /home/user/sdb2.map 4096 300 600"

        exit 1

fi

# Verify the device exists

if [ ! -b "$1" ]; then

        echo "Error: $1 does not appear to exist!"

        exit 2

fi

# Set aggressive power management for spin-down

hdparm -B 1 -S 1 $1

# Recover loop

while [ $Reading ]; do

        # Run the recovery for the specified amount of time

        timeout $5 ddrescue -b $4 -M -d -r 16 $1 $2 $3

        # See if the timeout was hit or if ddrescue exited

        if [ $? -ne 124 ]; then

                $Reading = false

                break

        fi

        # Give the disk time to cool down

        sleep $6

done

# Note that the disk recovery completed

echo "Disk recovery completed or ddrescue exited for some other reason."

exit 0

```

Timeout returns 124 if the timeout was hit, otherwise it returns whatever ddrescue returns. If ddrescue exits, we probably want to figure out why anyway!

----------

