Udev-How to Get Value of a Child Device Attributes

UDEV-How to get value of a child device attributes

This is rule I made to create an alias for dual port FTDI chip:

# Internal serial ports
SUBSYSTEMS=="usb", ATTRS{interface}=="Dual RS232", SYMLINK+="sertest%s{bInterfaceNumber}"

According to this post the attributes must be matching on one level. That's why idVendor and idProduct won't work with bInterfaceNumber. Below you can see, that interface and bInterfaceNumber belong to the same level:

looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2/1-1.2:1.0':
KERNELS=="1-1.2:1.0"
SUBSYSTEMS=="usb"
DRIVERS=="ftdi_sio"
ATTRS{bInterfaceNumber}=="00"
ATTRS{bAlternateSetting}==" 0"
ATTRS{bNumEndpoints}=="02"
ATTRS{bInterfaceClass}=="ff"
ATTRS{bInterfaceSubClass}=="ff"
ATTRS{bInterfaceProtocol}=="ff"
ATTRS{supports_autosuspend}=="1"
ATTRS{interface}=="Dual RS232"

udev rule with few parent device attributes

After many of unsuccessful experiences, i found the solution!

The key feature of it is setting the environment variable:

  1. On plugging event, we looking the vendor:id pair and remember it in environment variable.
  2. On the same event, we compare the saved variable and usb-device tree nodes IDs to assign exact names of ports of certain usb-hub.

This document helped me http://www.reactivated.net/writing_udev_rules.html

KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="05e3", ATTRS{idProduct}=="0610", ENV{USB_HUB_TYPE}="05e3:0610"
KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="0835", ATTRS{idProduct}=="8500", ENV{USB_HUB_TYPE}="0835:8500"
#
ENV{USB_HUB_TYPE}=="0835:8500", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].4:1.0", SYMLINK+="port1"
ENV{USB_HUB_TYPE}=="0835:8500", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].3:1.0", SYMLINK+="port2"
ENV{USB_HUB_TYPE}=="0835:8500", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].2:1.0", SYMLINK+="port3"
ENV{USB_HUB_TYPE}=="0835:8500", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].5.5:1.0", SYMLINK+="port4"
ENV{USB_HUB_TYPE}=="0835:8500", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].5.2:1.0", SYMLINK+="port5"
ENV{USB_HUB_TYPE}=="0835:8500", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].5.3:1.0", SYMLINK+="port6"
ENV{USB_HUB_TYPE}=="0835:8500", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].5.4:1.0", SYMLINK+="port7"
#
ENV{USB_HUB_TYPE}=="05e3:0610", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].1.1:1.0" SYMLINK+="port1"
ENV{USB_HUB_TYPE}=="05e3:0610", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].2:1.0", SYMLINK+="port2"
ENV{USB_HUB_TYPE}=="05e3:0610", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].1.2:1.0", SYMLINK+="port3"
ENV{USB_HUB_TYPE}=="05e3:0610", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].3:1.0", SYMLINK+="port4"
ENV{USB_HUB_TYPE}=="05e3:0610", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].1.3:1.0", SYMLINK+="port5"
ENV{USB_HUB_TYPE}=="05e3:0610", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].4:1.0", SYMLINK+="port6"
ENV{USB_HUB_TYPE}=="05e3:0610", KERNEL=="ttyUSB[0-9]*", SUBSYSTEM=="tty", KERNELS=="1-1.[2-3].1.4:1.0", SYMLINK+="port7"

Perhaps, it will be useful for someone.

Create device symlink with udev based on device response, not udev device info

Blindly chatting on a serial port automatically the moment it is connected to the computer is quite a scary thing to do. Are you sure you want to do this? First of all it will take some time, which will delay the appearance of the device node if udev has to wait for a result before creating it. If the device happens to be powered off or its serial port is disconnected then you won't be able to name it properly (and you will need to have a timeout to detect this). Finally, if another device besides one of the ones you expect is connected, you may be sending it garbage that has unintended consequences.

You might consider relying on the adaptors' serial numbers to tell them apart, and associate each adaptor to an external device in a consistent fashion. If your adaptors even have real serial numbers instead of placeholder strings like 00000000 then you are already lucky!

Nevertheless, this is how you would do it.

As you suspected, you can't use RUN, because that's too late, the device node has already been created. You have to use PROGRAM. In your program you are going to have to create the device node yourself using mknod because udev hasn't done it yet. You should create a temporary node in a temporary location and destroy it before your program exits.

### Create the temporary device node in /tmp
device="/tmp/udev_device_guesser.$$"
# Note: mknod does not appear to be vulnerable to a symlink attack
mknod "$device" c "$MAJOR" "$MINOR"

### Use this device node to query what's attached to the serial port
insert your code here

### Get rid of the temporary node
rm -f "$device"
exit 0

Persistant name in usb device which open several connections || connect USB to specific port

Change default names for USB virtual serial ports in Linux

This post was the solution to my troubles, instead of using the /devttyACMx
y changed to used the names in /dev/serial/by-id/
up until now it has worked fine, if it stops doing it I will post it

How to find reasons why an udev rule is not applied?

For the udev rule
/ect/udev/rules.d/99-bizrfid.rules I would add the SUBSYSTEM:

ACTION=="add", SUBSYSTEM=="tty", ATTRS{idVendor}=="09d8", ATTRS{idProduct}=="0420", SYMLINK+="ttyMyDevice"

You could add also ATTRS{manufacturer}==, but idVendor and idProduct are sufficient.

Otherwise it looks correct and the test run creates the symlink:

creating link '/dev/ttyMyDevice' to '/dev/ttyACM0'

Conditionless GOTO in udev rules (and Medion RC-0617)

Indeed, the second line (GOTO="end") was just ignored. It took me some time to figure that out, but the solution is actually pretty simple:

If there are no conditions to match against, udev treats the rule as "always not matching" and therefore does not execute the GOTO at all.

My working udev rules file looked like this:

# Test if the wanted dongle is a parent device
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{idVendor}=="04f2", ATTRS{idProduct}=="0618", GOTO="match"
# If not, skip the next 3 rules. The test against SUBSYSTEM=="hidraw" is there to produce a rule match
SUBSYSTEM=="hidraw", GOTO="end"
LABEL="match"
# Those 3 rules actually assign the right symlink depending on the bInterfaceProtocol property.
# Note that ALL of those rules contain the SUBSYSTEM=="hidraw" check, because the GOTO in the second line
# does not get executed for non-hidraw devices and the rules get evaluated for any non-hidraw device.
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{bInterfaceProtocol}=="01", SYMLINK="mdremote0", MODE="0666"
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{bInterfaceProtocol}=="00", SYMLINK="mdremote1", MODE="0666"
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{bInterfaceProtocol}=="02", SYMLINK="mdremote2", MODE="0666"
LABEL="end"

This turned out to work fine. It can still be improved by providing a better match rule for the GOTO="end" statement, but I left it that way.



Related Topics



Leave a reply



Submit