Power OFF and ON USB device in linux (ubuntu)

by

in

There are two methods of doing this. So first method is for kernels after 2.6.32, and second for older ones.

To view kernel version, simply enter:

> uname -r 
3.8.0-29-generic

 

find Your USB device, no mather what it is USB flash drive, Yubikey, USB disk drive:

> lsusb 
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 005: ID 12d1:14db Huawei Technologies Co., Ltd. 
Bus 002 Device 006: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub

> lsusb -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/6p, 480M
        |__ Port 1: Dev 5, If 0, Class=comm., Driver=cdc_ether, 480M
        |__ Port 1: Dev 5, If 1, Class=data, Driver=cdc_ether, 480M
        |__ Port 5: Dev 6, If 0, Class=hub, Driver=hub/4p, 480M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/6p, 480M


Our target is:

Bus 002 Device 005: ID 12d1:14db Huawei Technologies Co., Ltd. 

Which is located at 2-1.1 (tree part where device 5 is located)
First method (newer kernel)

To turn it off simply echo address:

echo '2-1.1' > /sys/bus/usb/drivers/usb/unbind

 

here is result from syslog:

Feb 20 12:03:27 vgsms6 kernel: [ 1734.993667] cdc_ether 2-1.1:1.0 eth2: unregister 'cdc_ether' usb-0000:00:1d.0-1.1, CDC Ethernet Device

Unfortunately this device was still visible in lsusb, so I turned off whole usb hub there (2-1), and voila, usb device gone.

To enable it, echo same address to “bind” part, for example:

echo '2-1.1' > /sys/bus/usb/drivers/usb/bind

 

Second method (older kernel)
You need to go to devices:

cd /sys/bus/usb/devices

 

Addressess will be listed similar:

/sys/bus/usb/devices# ls -alh
total 0
drwxr-xr-x 2 root root 0 Feb 20 11:34 .
drwxr-xr-x 4 root root 0 Feb 20 11:34 ..
lrwxrwxrwx 1 root root 0 Feb 20 11:34 1-0:1.0 -> ../../../devices/pci0000:00/0000:00:1a.0/usb1/1-0:1.0
lrwxrwxrwx 1 root root 0 Feb 20 11:34 1-1 -> ../../../devices/pci0000:00/0000:00:1a.0/usb1/1-1
lrwxrwxrwx 1 root root 0 Feb 20 11:34 1-1:1.0 -> ../../../devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1:1.0
lrwxrwxrwx 1 root root 0 Feb 20 11:34 2-0:1.0 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2/2-0:1.0
lrwxrwxrwx 1 root root 0 Feb 20 11:34 2-1 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2/2-1
lrwxrwxrwx 1 root root 0 Feb 20 12:34 2-1.1 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1
lrwxrwxrwx 1 root root 0 Feb 20 12:34 2-1:1.0 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1:1.0
lrwxrwxrwx 1 root root 0 Feb 20 12:34 2-1.1:1.0 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1/2-1.1:1.0
lrwxrwxrwx 1 root root 0 Feb 20 12:34 2-1.1:1.1 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1/2-1.1:1.1
lrwxrwxrwx 1 root root 0 Feb 20 12:34 2-1.5 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.5
lrwxrwxrwx 1 root root 0 Feb 20 12:34 2-1.5:1.0 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.5/2-1.5:1.0
lrwxrwxrwx 1 root root 0 Feb 20 11:34 usb1 -> ../../../devices/pci0000:00/0000:00:1a.0/usb1
lrwxrwxrwx 1 root root 0 Feb 20 11:34 usb2 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2

 

So go to in our example 2-1.1/power (full path /sys/bus/usb/devices2-1.1/power)

cd /sys/bus/usb/devices2-1.1/power

 

to turn off device:

echo suspend > level

 

to turn on:

echo on > level

 

 


Comments

10 responses to “Power OFF and ON USB device in linux (ubuntu)”

  1. Anonymous Avatar

    ~ $ sudo echo ‘1-1.2’ > /sys/bus/usb/drivers/usb/unbind
    -bash: /sys/bus/usb/drivers/usb/unbind: Permission denied

  2. Thanks, finally got it working! :)

    Anonymous: your command executes “echo ‘1-1.2′” under sudo, and then tries to write that to /sys/bus/usb/drivers/usb/unbind as the normal user again. Try this: echo ‘1-1.2″ | sudo tee /sys/bus/usb/drivers/usb/unbind

  3. You could also use a switchable USB hub.

    Something like this https://www.yepkit.com/products/ykush

    but it’s more expensive :)

    1. Smart way of leaving a backlink.
      I encourage doing opensource projects :)

      Usually the budget depends on what are the requirements. For a simple “faulty” usb emergency device reset I would use my way (I wouldn’t expect something to break, would I?).

      But for day to day planned switch off/on I’d consider such device.
      Thanks for sharing Your product.
      That’s pretty useful thing, it should have more reliable switching capability and it’s more powerful than a server usb port.

      You should consider making a bigger hub, as it seems to be possible to use it as a powersource for usb devices (2Amps of current).
      For example external usb 2.5 hdd would already take 2 ports, leaving only one free.
      Or make an usb controlled power source switcher with relays, it would perfectly go as a combo with this usb hub device :)

      1. Fully agree with you, the best approach depends of the requirements.

        Those are cool ideas for improving the product.

        Cheers

  4. Michael Avatar

    I know the post is quite old, I just found it and trying to figure out how to make it work, this is the closest I got, but still not working (maybe due to my lack of deep Linux knowledge), I got an error “no such device”, is the error due to my device number in the echo command is wrong or I choose the wrong unbind?

  5. […] Thanks to login: root (en) for Power OFF and ON USB device in linux (ubuntu) (en). […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.