Sending On/Off Signal to USB devices in Linux

Tags: February 15, 2014 1:14 PM

Some devices like USB modem get stuck when it failed to connect and just hang up forever when trying to dial a number. The solution is unplug and plug it again, weird but it works. But seriously, that is not "a geek way". Rather than plug/unplug we could do it via shell to send the device "on" or "off" signal.

Recognizing the USB Devices

These steps is used to get information about device that we want to send the signal to. A Tool used for this task is lsusb. Here's example of output in my system, make to sure run as root.
$ 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 001 Device 003: ID 04f2:b290 Chicony Electronics Co., Ltd 
Bus 001 Device 009: ID 0cf3:3005 Atheros Communications, Inc. AR3011 Bluetooth
Bus 002 Device 018: ID 201e:1022 <-- WE WANT TO SEND SIGNAL TO THIS DEVICE
Bus 002 Device 016: ID 03f0:ae07 Hewlett-Packard 
Bus 002 Device 012: ID 0458:00ee KYE Systems Corp. (Mouse Systems)
Alternatively if you want more verbose output you could pass "-v" argument to lsusb. Let say we want to send signal to device 201e:1022. What should we do? unfortunately we need more information about the BUS and Port which used by device. So pass the "-t" argument to lsusb.
$ lsusb -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/6p, 480M
        |__ Port 2: Dev 18, If 0, Class=vend., Driver=option, 12M
        |__ Port 2: Dev 18, If 1, Class=vend., Driver=option, 12M
        |__ Port 2: Dev 18, If 2, Class=vend., Driver=option, 12M
        |__ Port 2: Dev 18, If 3, Class=stor., Driver=usb-storage, 12M
        |__ Port 3: Dev 16, If 0, Class=stor., Driver=usb-storage, 480M
        |__ Port 5: Dev 12, If 0, Class=HID, Driver=usbhid, 1.5M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/6p, 480M
        |__ Port 2: Dev 3, If 0, Class='bInterfaceClass 0x0e not yet handled', Driver=uvcvideo, 480M
        |__ Port 2: Dev 3, If 1, Class='bInterfaceClass 0x0e not yet handled', Driver=uvcvideo, 480M
        |__ Port 5: Dev 9, If 0, Class='bInterfaceClass 0xe0 not yet handled', Driver=btusb, 12M
        |__ Port 5: Dev 9, If 1, Class='bInterfaceClass 0xe0 not yet handled', Driver=btusb, 12M
Remember that device 201e:1022 had an ID 018, so from output above the "Bus-Port.Port" hierarchy for our device is 2-1.2.

Sending the Off Signal

Sending signal to the device is easy, we just need echo command and redirect the output to the right location.
$ echo '2-1.2' > /sys/bus/usb/drivers/usb/unbind
To make sure the device receive the off signal, run the lsusb -t again. The device 18 (2-1.2) should be gone from the list.
lsusb -t
2-1.2: No such file or directory
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/6p, 480M
        |__ Port 3: Dev 16, If 0, Class=stor., Driver=usb-storage, 480M
        |__ Port 5: Dev 12, If 0, Class=HID, Driver=usbhid, 1.5M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/6p, 480M
        |__ Port 2: Dev 3, If 0, Class='bInterfaceClass 0x0e not yet handled', Driver=uvcvideo, 480M
        |__ Port 2: Dev 3, If 1, Class='bInterfaceClass 0x0e not yet handled', Driver=uvcvideo, 480M
        |__ Port 5: Dev 9, If 0, Class='bInterfaceClass 0xe0 not yet handled', Driver=btusb, 12M
        |__ Port 5: Dev 9, If 1, Class='bInterfaceClass 0xe0 not yet handled', Driver=btusb, 12M

Sending the On Signal

Almost the same as before but instead unbind we redirect the output to bind. The device should appear again, you can confirm it with lsusb -t
$ echo '2-1.2' > /sys/bus/usb/drivers/usb/bind

References

Share on Facebook Twitter

0 comments:

Post a Comment