Power OFF and ON USB device in linux (ubuntu)

http://loginroot.com/power-off-and-on-usb-device-in-linux-ubuntu/

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:

 
 
 
 

Shell

 
1
2
> uname -r
3.8.0-29-generic

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

 
 
 
 

Shell

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> 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:

 
 
 
 

Shell

 
1
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:

 
 
 
 

Shell

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

here is result from syslog:

 
 
 
 

Shell

 
1
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:

 
 
 
 

Shell

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

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

 
 
 
 

Shell

 
1
cd /sys/bus/usb/devices

Addressess will be listed similar:

 
 
 
 

Shell

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/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)

 
 
 
 

Shell

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

to turn off device:

 
 
 
 

Shell

 
1
echo suspend > level

to turn on:

 
 
 
 

Shell

 
1
echo on > level

Like this:

Comments (7)

  1. AnonymousSeptember 28, 2014 at 4:01 pm

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

    Reply ↓
  2. NiekoNovember 4, 2014 at 11:59 am

    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

    Reply ↓
  3. SantosNovember 8, 2014 at 2:25 am

    You could also use a switchable USB hub.

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

    but it’s more expensive :)

    Reply ↓
    1. nsc(Post author)November 8, 2014 at 9:47 am

      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 :)

      Reply ↓
      1. SantosNovember 8, 2014 at 1:58 pm

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

        Those are cool ideas for improving the product.

        Cheers

        Reply ↓
  4. Pingback: » Linux: Turning off power to usb port. Or turn off power to entire usb subsystem

  5. Pingback: Raspberry Pi + L-02C でバッチリ安定する多機能 LTE ルータをつくったメモ – 怠惰の形而上学

Leave a Reply

Power OFF and ON USB device in linux (ubuntu)的更多相关文章

  1. 【转载】How to Reset USB Device in Linux

    USB devices are anywhere nowadays, even many embedded devices replace the traditional serial devices ...

  2. usb device address error 110

    ubuntu失灵了,怎么都起不来,报一堆错误usb device descriptor read/64, error 110......重启,换kvm的接口,usb键盘鼠标...终于在试了下面这个方法 ...

  3. Install Slax on USB device (Slax U 盘安装)

    Slax is a modern, portable, small and fast Linux operating system with a modular approach and outsta ...

  4. Assigning Host USB device to a Guest VM

    Example Assigning Host USB device to a Guest VM This example is based on qemu-kvm (0.15.0) as instal ...

  5. USB device & USB controller & USB passthrough

    目录 USB device USB controller Linux 相关命令 Python 相关库 Libvirt USB passthrough 参考资料 近期往 openstack 里倒腾 US ...

  6. STM32F4 HAL Composite USB Device Example : CDC + MSC

    STM32F4 USB Composite CDC + MSC I'm in the process of building a USB composite CDC + MSC device on t ...

  7. Open images from USB camera on linux using V4L2 with OpenCV

    I have always been using OpenCV's VideoCapture API to capture images from webcam or USB cameras. Ope ...

  8. What is a Windows USB device path and how is it formatted?

    http://community.silabs.com/t5/Interface-Knowledge-Base/Windows-USB-Device-Path/ta-p/114059 Windows ...

  9. USB Device Finder

    http://www.velleman.eu/images/tmp/usbfind.c #ifdef __cplusplus extern "C" { #endif #includ ...

随机推荐

  1. mysql一:操作数据库

    创建数据库是指在数据库空间中划出一块空间用来存储相关的数据,表就是存储在对应的数据库里面.首先来看下查看数据库的命令:show databases. 这个是用来查询数据库空间下所有的数据库,其中inf ...

  2. (转)php 根据url自动生成缩略图并处理高并发问题

    分享是一种精神,与技术高低无关!   图片缩略图动态生成- [代码编程] 2011-08-23 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://www.blogbus.c ...

  3. selector + shape

    selector_shape.xml<?xml version="1.0" encoding="utf-8"?> <selector xmln ...

  4. Apache NiFi 开发 处理器使用说明

    NIFI的使用: 注意:FlowFile由[属性]和[内容]组成,在解析的过程中这个概念非常重要,因为有些组件操作的是属性,有些组件操作的是内容,在配置组件时Destination配置项的选择很重要, ...

  5. 《高性能Javascript》 Summary(二)

    第四章.算法和流程控制 Algorithms And Flow Control 原因:代码整体结构是执行速度的决定因素之一.代码量少不一定运行速度快,代码量多不一定运行速度慢.性能损失与组织代码和具体 ...

  6. Java多线程系列 基础篇10 wait/notify/sleep/yield/join

    1.Object类中的wait()/notify()/notifyAll() wait(): 让当前线程处于Waiting状态并释放掉持有的对象锁,直到其他线程调用此对象的线程notify()/not ...

  7. css浏览器兼容问题集锦

    表单按钮用input type=submit和a链接两者表现不一致的问题 表单的输入框.文本.验证码图片没有对齐 IE6/7中margin失效 IE6中margin双边距 1.问题: 表单按钮用inp ...

  8. 经典数学问题<手电过河问题>的动态解法--问题规模扩展至任意大小

    非常有趣的一件事是今天在TopCoder的1000分题里面发现了这道经典数学问题. Notes           -                   In an optimal solution ...

  9. ubuntu上swift开发学习2

    使用包管理器(package manager)创建一个可执行的项目 1. 创建一个Hello目录,然后进入目录 $ mkdir Hello $ cd Hello 2. 使用包初始化命令,参数表示创建一 ...

  10. Python中出现“TabError: inconsistent use of tabs and spaces in indentation”问题的解决