Disconnect and reconnect ttyUSB0 programmatically in Linux

JosephITA picture JosephITA · Feb 5, 2014 · Viewed 13.7k times · Source

Trying to solve this problem (ttyUSB0 that works properly than stop working after about 1hr)I'm thinking on if disconnecting and reconnecting the usb device could be a good fix.

So, it is possibile to cut down power to the USB device and repower it programmatically (bash)?

# lsusb -t                                        
1-1:1.0: No such file or directory                                              
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=musb-hdrc/1p, 480M             
    |__ Port 1: Dev 2, If 0, Class=vend., Driver=, 12M                          
    |__ Port 1: Dev 2, If 1, Class=vend., Driver=cp210x, 12M

On am335x, kernel 3.2.0, ubuntu core armhf.

[    1.784332] usb 1-1: cp210x converter now attached to ttyUSB0

At the moment I need a complete power cycle to have ttyUSB0 back.

Answer

Robert Harvey picture Robert Harvey · Aug 4, 2014

This is the solution:

  • Find the identity of your usb device.

    # tree /sys/bus/usb/drivers/cp210x/
    /sys/bus/usb/drivers/cp210x/
    |-- 1-1:1.1 -> ../../../../devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1:1.1
    |-- bind
    |-- module -> ../../../../module/cp210x
    |-- remove_id
    |-- uevent
     -- unbind
    

So 1-1:1.1 is the identifier of my ttyUSB0(it can be discovered also via dmesg).

  • Then, disconnect the device (as root):

    # echo -n "1-1:1.1" > /sys/bus/usb/drivers/cp210x/unbind
    
  • reconnect it

    # echo -n "1-1:1.1" > /sys/bus/usb/drivers/cp210x/bind
    

At this point I had the same device but with a different name, it was now ttyUSB1 instead of ttyUSB0. - To avoid this I added a new rule in /etc/udev/rules.d/ by creating a new file named 99-usb-serial.rules with this line:

SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea70", ATTRS{serial}=="002DCFAF", SYMLINK+="sameName", MODE:="0666"

where idVendor, idProduct and serial must be the values of your device. This rule will create a new device called sameName linked to the ttyUSB* device normally generated from the OS.