noctis.de

The basic problem with hotplugging USB devices is, that you never know the current device file name for use in configuration files. Linux' udev has a ruleset that provides you the feature to create a symlink whenever a certain device is plugged in, provided you can uniquely identify it. based on this guide

Identify the udev device information

First of all it's necessary to find the device's path:

# udevinfo -q path -n /dev/ttyUSB0

/class/tty/ttyUSB0

This isn't specifically helpful, but it lays the foundation for additional information:

# udevinfo -a -p /class/tty/ttyUSB0

[...]

ID=="1-1"
SYSFS{manufacturer}=="Matrix Orbital"
SYSFS{product}=="LK/VK204-24-USB"
SYSFS{serial}=="00000131"

The device in question is a LC display connected to some FTDI USBserial interface which provides (among other values) a manufacturer string, a product name and a serial number.

Create the udev rule

Now there's enough information to create a rule for udev. I chose to place it in /etc/udev/rules.d/44-my-devices.rule with 44 being an arbitrary number which should be below the standard rules file. The name/directory of these files may differ on your system. From the standard rule file (/etc/udev/rules.d/50-udev.rules on my gentoo installation) I copied a template for my rule (namely the KERNEL, NAME, GROUP and MODE part) from the standard line for the USB to serial converters and added my identification information. The result is:

KERNEL=="ttyUSB[0-9]*", SYSFS{manufacturer}=="Matrix Orbital", NAME="%k", SYMLINK="USBdisplay%n", GROUP="tty", MODE="0660"

The added SYMLINK="USBdisplay%n" creates a symlink /dev/USBdisplay0 for the first display which points to whatever /dev/ttyUSB? device is assigned. The SYSFS{manufacturer}=="Matrix Orbital" does not identify this specific device, in case I ever get another one of these.

Possible Cavehat

It seems the number after USBdisplay is the same as that of the ttyUSB? node, instead of the excpected number of USBdisplays. So it might well be, that you end up with /dev/USBdisplay2 being the symlink for the first device. However I'm uncertain whether this is really true, but while testing (with another rule) I had this result (for the other rule). My workaround there: I locked the symlink to the very device and removed the number. Since that converter did not provide a serial number, I used the USB port that device is connected to as a unique identification:

KERNEL=="ttyUSB[0-9]*", ID=="1-2", SYSFS{manufacturer}=="Prolific Technology Inc.", NAME="%k", SYMLINK="USBrelaisBoard", GROUP="tty", MODE="0660"

In case I switch ports the relais board just won't get a symlink so the control software will complain about a non-existing device instead of risking that some random command turns everything off (or on). ;-)