Linux: /dev/ttyUSB0 is removed when using pylibftdi

Issue #18 open
Greger Cronquist
created an issue

On my Raspberry Pi, when I boot I have a /dev/ttyUSB0 device. However, when I run my pylibftdi, which essentially does a

            with Device() as dev:
                dev.baudrate = 38400
                while not queue.empty():
                    command = queue.get()
                    dev.write(command.decode('hex'))

/dev/ttyUSB0 disappears with the dmesg message

40035.562819] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0
[40035.564264] ftdi_sio 1-1.2:1.0: device disconnected

I can replicate this by modprobe-ing to restart ftdi_sio. The pylibftdi program continues to work however, it's just the serial device that is removed.

Comments (7)

  1. Ben Bass repo owner

    Replicated. Running udevadm monitor

    Attach a FTDI device to USB bus for first time:

    KERNEL[95.952656] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4 (usb)
    UDEV  [95.974405] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4 (usb)
    KERNEL[96.012252] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0 (usb)
    KERNEL[96.051541] add      /module/usbserial (module)
    UDEV  [96.055586] add      /module/usbserial (module)
    KERNEL[96.058782] add      /bus/usb-serial (bus)
    UDEV  [96.063040] add      /bus/usb-serial (bus)
    KERNEL[96.066117] add      /bus/usb/drivers/usbserial (drivers)
    UDEV  [96.070583] add      /bus/usb/drivers/usbserial (drivers)
    KERNEL[96.073743] add      /bus/usb/drivers/usbserial_generic (drivers)
    UDEV  [96.078102] add      /bus/usb/drivers/usbserial_generic (drivers)
    KERNEL[96.082229] add      /bus/usb-serial/drivers/generic (drivers)
    KERNEL[96.097087] add      /module/ftdi_sio (module)
    UDEV  [96.101957] add      /bus/usb-serial/drivers/generic (drivers)
    UDEV  [96.106391] add      /module/ftdi_sio (module)
    KERNEL[96.109654] add      /bus/usb/drivers/ftdi_sio (drivers)
    UDEV  [96.114202] add      /bus/usb/drivers/ftdi_sio (drivers)
    KERNEL[96.117316] add      /bus/usb-serial/drivers/ftdi_sio (drivers)
    UDEV  [96.123062] add      /bus/usb-serial/drivers/ftdi_sio (drivers)
    KERNEL[96.126923] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0 (usb-serial)
    KERNEL[96.134435] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0/tty/ttyUSB0 (tty)
    UDEV  [96.139893] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0 (usb)
    UDEV  [96.146840] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0 (usb-serial)
    UDEV  [96.178753] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0/tty/ttyUSB0 (tty)
    

    Instantiating (which opens it by default) a pylibftdi.Device() causes the following:

    KERNEL[164.036685] remove   /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0/tty/ttyUSB0 (tty)
    UDEV  [164.042997] remove   /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0/tty/ttyUSB0 (tty)
    KERNEL[164.046177] remove   /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0 (usb-serial)
    UDEV  [164.053620] remove   /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0 (usb-serial)
    

    Running sudo modprobe -r ftdi_sio; sudo modprobe ftdi_sio while the Device is still open and no new /dev/ttyUSB0 device appears:

    KERNEL[211.398376] remove   /bus/usb-serial/drivers/ftdi_sio (drivers)
    UDEV  [211.404302] remove   /bus/usb-serial/drivers/ftdi_sio (drivers)
    KERNEL[211.407184] remove   /bus/usb/drivers/ftdi_sio (drivers)
    UDEV  [211.412120] remove   /bus/usb/drivers/ftdi_sio (drivers)
    KERNEL[211.412919] remove   /module/ftdi_sio (module)
    KERNEL[211.414637] remove   /bus/usb-serial/drivers/generic (drivers)
    KERNEL[211.418611] remove   /bus/usb/drivers/usbserial_generic (drivers)
    UDEV  [211.420255] remove   /module/ftdi_sio (module)
    KERNEL[211.421888] remove   /bus/usb/drivers/usbserial (drivers)
    KERNEL[211.422398] remove   /usb-serial (bus)
    KERNEL[211.423144] remove   /module/usbserial (module)
    UDEV  [211.426996] remove   /bus/usb-serial/drivers/generic (drivers)
    UDEV  [211.437271] remove   /bus/usb/drivers/usbserial_generic (drivers)
    UDEV  [211.446220] remove   /bus/usb/drivers/usbserial (drivers)
    UDEV  [211.461220] remove   /usb-serial (bus)
    UDEV  [211.465643] remove   /module/usbserial (module)
    KERNEL[211.531951] add      /module/usbserial (module)
    UDEV  [211.536035] add      /module/usbserial (module)
    KERNEL[211.539839] add      /bus/usb-serial (bus)
    KERNEL[211.542730] add      /bus/usb/drivers/usbserial (drivers)
    UDEV  [211.544709] add      /bus/usb-serial (bus)
    KERNEL[211.547976] add      /bus/usb/drivers/usbserial_generic (drivers)
    KERNEL[211.549916] add      /bus/usb-serial/drivers/generic (drivers)
    KERNEL[211.557137] add      /module/ftdi_sio (module)
    UDEV  [211.562935] add      /bus/usb/drivers/usbserial (drivers)
    UDEV  [211.565535] add      /bus/usb/drivers/usbserial_generic (drivers)
    KERNEL[211.570844] add      /bus/usb/drivers/ftdi_sio (drivers)
    UDEV  [211.574539] add      /bus/usb-serial/drivers/generic (drivers)
    KERNEL[211.577346] add      /bus/usb-serial/drivers/ftdi_sio (drivers)
    UDEV  [211.583584] add      /module/ftdi_sio (module)
    UDEV  [211.594463] add      /bus/usb/drivers/ftdi_sio (drivers)
    UDEV  [211.602801] add      /bus/usb-serial/drivers/ftdi_sio (drivers)
    

    run device.close() on the instance, re-run the modprobe cycle and the ttyUSB0 device reappears:

    ERNEL[287.586580] remove   /bus/usb-serial/drivers/ftdi_sio (drivers)
    UDEV  [287.591861] remove   /bus/usb-serial/drivers/ftdi_sio (drivers)
    KERNEL[287.595755] remove   /bus/usb/drivers/ftdi_sio (drivers)
    KERNEL[287.598627] remove   /module/ftdi_sio (module)
    UDEV  [287.601360] remove   /bus/usb/drivers/ftdi_sio (drivers)
    KERNEL[287.605841] remove   /bus/usb-serial/drivers/generic (drivers)
    KERNEL[287.606355] remove   /bus/usb/drivers/usbserial_generic (drivers)
    KERNEL[287.606813] remove   /bus/usb/drivers/usbserial (drivers)
    KERNEL[287.607281] remove   /usb-serial (bus)
    UDEV  [287.608733] remove   /module/ftdi_sio (module)
    KERNEL[287.609526] remove   /module/usbserial (module)
    UDEV  [287.619719] remove   /bus/usb-serial/drivers/generic (drivers)
    UDEV  [287.625933] remove   /bus/usb/drivers/usbserial_generic (drivers)
    UDEV  [287.632242] remove   /bus/usb/drivers/usbserial (drivers)
    UDEV  [287.641097] remove   /usb-serial (bus)
    UDEV  [287.646166] remove   /module/usbserial (module)
    KERNEL[287.716225] add      /module/usbserial (module)
    UDEV  [287.720629] add      /module/usbserial (module)
    KERNEL[287.724411] add      /bus/usb-serial (bus)
    KERNEL[287.727277] add      /bus/usb/drivers/usbserial (drivers)
    UDEV  [287.729144] add      /bus/usb-serial (bus)
    KERNEL[287.730669] add      /bus/usb/drivers/usbserial_generic (drivers)
    KERNEL[287.731351] add      /bus/usb-serial/drivers/generic (drivers)
    KERNEL[287.739470] add      /module/ftdi_sio (module)
    UDEV  [287.743465] add      /bus/usb/drivers/usbserial (drivers)
    UDEV  [287.749986] add      /bus/usb/drivers/usbserial_generic (drivers)
    UDEV  [287.755390] add      /bus/usb-serial/drivers/generic (drivers)
    KERNEL[287.758380] add      /bus/usb/drivers/ftdi_sio (drivers)
    KERNEL[287.761382] add      /bus/usb-serial/drivers/ftdi_sio (drivers)
    KERNEL[287.762606] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0 (usb-serial)
    UDEV  [287.768519] add      /module/ftdi_sio (module)
    KERNEL[287.772777] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0/tty/ttyUSB0 (tty)
    UDEV  [287.778300] add      /bus/usb/drivers/ftdi_sio (drivers)
    UDEV  [287.789226] add      /bus/usb-serial/drivers/ftdi_sio (drivers)
    UDEV  [287.799964] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0 (usb-serial)
    UDEV  [287.833355] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4:1.0/ttyUSB0/tty/ttyUSB0 (tty)
    

    So an open pylibftdi.Device cannot co-exist with ttyUSB0 (which seems reasonable to me). What would be nice would be if libftdi released a USB device then the appropriate udev signal caused ttyUSB0 to return, but I'm not really sure where that would be done - I've tried closing the device in a variety of ways without success, and I suspect this is something in the domain of libftdi, libusb or udev, so not hopeful that it will get fixed in pylibftdi itself beyond documentation...

  2. Greger Cronquist reporter

    Thanks for looking into this, sorry for not getting back earlier! Yes, I do use the latest raspbian and I also recompiled and tried with the latest libftdi1 (the one in the raspbian repo is quite old) without success. I can work around this, raising the issue with libftdi seems to be a lot of work ;-) I vote for closing this issue, and maybe update the docs.

  3. Kodie Goodwin

    Hi Ben, I am loving the library thanks!

    How would I reattach the kernel driver upon calling device.close()? After I run a program I want to reenumerate all four devices (FT4232H). Right now I have to unplug and plug the device back in.

  4. Ben Bass repo owner
    • changed status to open

    I've reproduced this and had a dig around playing with ctypes and the libusb call.

    I'm hopeful 41e70e8 will fix things; it did for me on a test with Linux, running watch ls /dev/ttyUSB0 whilst opening and closing a Device in an interactive Python session. Despite the generic nature of libusb, it doesn't seem to work its magic on OS X though, which would have been nice, but maybe a little hopeful.

  5. Log in to comment