Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disconnect and reconnect ttyUSB0 programmatically in Linux

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.

like image 680
JosephITA Avatar asked Feb 05 '14 15:02

JosephITA


2 Answers

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.

like image 65
Robert Harvey Avatar answered Oct 24 '22 20:10

Robert Harvey


As @Robert Harvey Said,

You first need to find our driver that will help you 'unplug and plug' the usb. Type: ls /sys/bus/usb/drivers which should print something like this: btusb ftdi_sio hub usb usbfs usbhid usbserial_generic uvcvideo. These, are all the drivers for each usb device. Now, lets say mine is ftdi_sio, which is a device i use to program my arduino (atmega328p chip). I am not sure how Your/other usb devices name themselves there. Like, i dont know which of these is my mouse. Now, you can see the driver's commands using: ls /sys/bus/usb/drivers/ftdi_sio/, which will print something like: 1-4:1.0 bind module uevent unbind, Where 1-4:1.0 is the device's characteristic code, and the bind and unbind command, which are the 'plug' and 'unplug' command respectively.

Now, if i want to unplug programatically the ftdi usb port, i will type: echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/unbind and, to plug it again: echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/bind

Now, we can combine all the commands together, with a ';':

echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/unbind ; echo -n "1-4:1.0" > /sys/bus/usb/drivers/ftdi_sio/bind