Creating Named Devices Using udev

Many devices use USB to serial bridge ICs like the FT232, CH340 or CP2102. Not only micro-controller developer boards like the Arduino, but 3D printers or RC remotes and some flight controllers as well. Some µCs have even integrated USB interfaces and don’t need these chips at all. All these devices use a serial interface to communicate. Regularly these devices will be shown on Linux as /dev/ttyUSBn or /dev/ttyACMn. By the way, if you want to know what’s the difference between those, I found this interesting article.

If you want to connect multiple of these devices at the same time, it gets pretty confusing, since it’s getting really hard to tell which software serial device points to which actual hardware device. There is also the fact, that lsusb doesn’t show these devices as “Arduino”, “Flight Controller” or “3D Printer”, but with the name/driver of the serial chip. If there are multiple devices of the same type, most of the time, you need to unplug the device, run lsusb, connect the device again, re-run lsusb and look which device “moved” or was added.

Understanding udev rules

udev basically manages all the connected devices of and on a Linux system. Using so called udev rules, it is very easy to add an additional link with a custom name, that points to the actual device (3D printer), like /dev/ttyUSB0. This makes it at lot easier to identify the correct device, especially if multiple ones are connected at the same time. There are pre-written rules that work all the time in the background of a running system. Some of these rules may be changed, some added and others overridden, since usually the last rule added matching a certain device gets used when the device is actually connected to the system.

Matching a device using rules

To match a certain device, the == or !=operators are used. A device can be matched by its own properties and properties that match one of its parent. Properties like KERNEL, SUBSYSTEM or ATTR{...} match the device itself.
Properties like KERNELS, SUBSYSTEMS or ATTRS{...} match the device or a parent. It is important to understand, that all these properties ending with an S need to match same parent or they won’t match any parent at all.

Setting properties of a selected device

To change a certain property of the device selected, the =, +=, -= or := operators are used.

= will set a value, += will add another value to a list of values, -= will remove a value from a list and := will set a value finally (no other changes allowed!).

Most of the values support shell glob pattern matching and similar patterns, like *, ? or [0-9].

Examples

The following rules will match, if a device that has a KERNEL (name) value of ttyUSB0 or any other up to ttyUSB9 and a SUBSYSTEM value of “tty” and a parent device with the kernel name beginning with “1-1”.
Further, the same parent device needs to have a product name attribute, beginning with “CP2102“, a vendor ID attribute of 10c4 and a product ID of ea60.
If a device matching these rules is found, a symlink named ttyAMS will be added (+=) to the device directory.

Note, that the “true” kernel device name is still added and not changed. So, if a device with the kernel name “/dev/ttyUSB1” is added, it will still exist, but there will also be a symbolic link with the given name pointing to it. In most of the cases, that’s what you want.

# CP210x (Anycubic Mega S 3D printer)
KERNEL=="ttyUSB[0-9]", SUBSYSTEM=="tty", KERNELS=="1-1*", ATTRS{product}=="CP2102*", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", SYMLINK+="ttyAMS"

The following examples first two matching conditions are the same as above. The third condition is a kernel name of the device itself or a parent device, starting with “1-2“. The product name/description attribute needs to start with “USB“, the vendor ID needs to be “1a86” and the product ID “7523“. If all this is true, a symbolic link with the name “ttySWX” will be added. Like above, the original file name still exists and won’t be changed.

# CH341 (Artillery Sidewinder X1)
KERNEL=="ttyUSB[0-9]", SUBSYSTEM=="tty", KERNELS=="1-2*", ATTRS{product}=="USB*", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="ttySWX"

1 Reply to “Creating Named Devices Using udev”

Leave a Reply

Your email address will not be published. Required fields are marked *