From 1fc4aadb5d87643d6e64805b19a5a9e7671737ba Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Fri, 29 May 2020 22:22:55 +0200 Subject: udev: T2490: add persistent USB device files During testing it was discovered that on 5 out of 10 reboots the USB enumeration/mapping from physical port to /dev/ttyUSB is different. The root cause is that it's a FIFO so first found/loaded driver module will be assigned ttyUSB0. This mixed up the serial interfaces of my FTDI chips and my connected Sierra Wireless MC7710 card which was no longer functioning as it now was mapped to a different USB interface. The solution is a udev rule which persistently maps the USB-tree-device to a device file in /dev. Wait? isn't this what /dev/serial/by-{id,path} is for? Correct, it does the very same thing but the problem is as follows: * by-path uses device file names which also incorporate the parent bus system, this results in "pci-0000:00:10.0-usb-0:2.4:1.0-port0" * by-id will overwrite the assigned device symlink if a new USB device with the same name appears. This happens to some FTDI devices with no serial number programmed so the device added last wins and will be the only one in the by-id folder - cruel world! This commit adds a new directory /dev/serial/by-bus which holds the following device files (as example): $ ls -1 /dev/serial/by-bus/ usb0b1.3p1.0 usb0b1.3p1.2 usb0b1.3p1.3 usb0b2.4p1.0 usb0b2.4p1.1 usb0b2.4p1.2 usb0b2.4p1.3 --- src/etc/udev/rules.d/90-vyos-serial.rules | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/etc/udev/rules.d/90-vyos-serial.rules diff --git a/src/etc/udev/rules.d/90-vyos-serial.rules b/src/etc/udev/rules.d/90-vyos-serial.rules new file mode 100644 index 000000000..3f10f4924 --- /dev/null +++ b/src/etc/udev/rules.d/90-vyos-serial.rules @@ -0,0 +1,28 @@ +# do not edit this file, it will be overwritten on update + +ACTION=="remove", GOTO="serial_end" +SUBSYSTEM!="tty", GOTO="serial_end" + +SUBSYSTEMS=="pci", ENV{ID_BUS}="pci", ENV{ID_VENDOR_ID}="$attr{vendor}", ENV{ID_MODEL_ID}="$attr{device}" +SUBSYSTEMS=="pci", IMPORT{builtin}="hwdb --subsystem=pci" +SUBSYSTEMS=="usb", IMPORT{builtin}="usb_id", IMPORT{builtin}="hwdb --subsystem=usb" + +# /dev/serial/by-path/, /dev/serial/by-id/ for USB devices +KERNEL!="ttyUSB[0-9]*|ttyACM[0-9]*", GOTO="serial_end" + +SUBSYSTEMS=="usb-serial", ENV{.ID_PORT}="$attr{port_number}" + +IMPORT{builtin}="path_id", IMPORT{builtin}="usb_id" + +# Change the name of the usb id to a "more" human redable format. +# +# - $env{ID_PATH} usually is a name like: "pci-0000:00:10.0-usb-0:2.3.3.4:1.0-port0" so we strip the "pci-*" +# portion and only use the usb part +# - Transform the USB "speach" to the tree like structure so we start with "usb0" as root-complex 0. +# (tr -d -) does the replacement +# - Replace the first group after ":" to represent the bus relation (sed -e 0,/:/s//b/) indicated by "b" +# - Replace the next group after ":" to represent the port relation (sed -e 0,/:/s//p/) indicated by "p" +ENV{ID_PATH}=="?*", ENV{.ID_PORT}=="", PROGRAM="/bin/sh -c 'D=$env{ID_PATH}; echo ${D:17} | tr -d - | sed -e 0,/:/s//b/ | sed -e 0,/:/s//p/'", SYMLINK+="serial/by-bus/$result" +ENV{ID_PATH}=="?*", ENV{.ID_PORT}=="?*", PROGRAM="/bin/sh -c 'D=$env{ID_PATH}; echo ${D:17} | tr -d - | sed -e 0,/:/s//b/ | sed -e 0,/:/s//p/'", SYMLINK+="serial/by-bus/$result" + +LABEL="serial_end" -- cgit v1.2.3