5

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+=""?

kratenko
  • 7,354
  • 4
  • 36
  • 61
  • Well written Q, but as this oesn't seem to rely on a programming language, and seems to be linux conf-file magic, seems more appropriate to ask this on [unix.se] or [sf]. (IMHO). Good luck! – shellter Feb 26 '20 at 14:34
  • udev rules are a programming language. They have conditionals and gotos. – David Grayson Feb 26 '20 at 19:06

2 Answers2

0

It looks like you are using USB serial ports. It should be easier for you to just access these devices via the symbolic links in /dev/serial/by-id. There is no need to make udev rules.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

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}"
dxb
  • 111
  • 3