Search code examples
linuxusbudev

udev rule generating symlink for USB-device using ATTRS{serial} in link name


I have a udev rule that generates symlinks for my USB devices in /dev according to their serial number (I have multiple otherwise identical devices but need reproducible device endpoints).

Currently I need to add a rule for every new serial number, like this:

SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", ATTRS{serial}=="S101", SYMLINK+="ttyS101"
SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", ATTRS{serial}=="S102", SYMLINK+="ttyS102"
...

I'm looking for a way to do this with a single rule, all that is dynamically accessible is %k, %n, and %c, but I found no way to access any other values. Using PROGRAM="" might be the way, but how would I access ATTRS from there?

I tried this:

SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", ATTRS{serial}=="S101", PROGRAM="/bin/sh -c 'udevadm info /dev/%k | grep ID_SERIAL_SHORT= | cut -d= -f 2'" SYMLINK+="tty%c"

but it looks like /dev/%k is not available when the program runs, so that doesn't work either.

Is there any way to use ATTRS{serial} for my SYMLINK+=""?


Solution

  • While looking for a way to setup a stable ttyACM_DEVMODEL_SERIAL symlink for a USB device with a CDC interface I found $attr{file}, %s{file} in udev's man page. The {file} in question is serial for USB devices serial strings. I tested it and it worked as expected for me.

    SUBSYSTEM=="tty", ATTRS{idVendor}=="1257", ATTRS{idProduct}=="3511", SYMLINK+="ttyACM_ASI%s{idProduct}_%s{serial}"