Udev rules for seemingly indistinguishable devices

I’m trying to use udev to automatically set up symlinks to an Intel RealSense D415. This is because I use several cameras on the machine and need to be able to reliably refer to them via a filename (that doesn’t change on reboot).

The RealSense D415 creates three video devices in /dev. I’m having no problem setting up symlinks for the first two, but as far as I can see the first and third devices are identical apart from the KERNEL and KERNELS attributes which are liable to change if another camera is plugged in first.

How can I use a udev rule to differentiate between these?

Output for sudo udevadm info -ap /devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.0/video4linux/video0:

  looking at device '/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.0/video4linux/video0':
    KERNEL=="video0"
    SUBSYSTEM=="video4linux"
    DRIVER==""
    ATTR{dev_debug}=="0"
    ATTR{index}=="0"
    ATTR{name}=="Intel(R) RealSense(TM) 415: Int"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.0':
    KERNELS=="2-1:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="uvcvideo"
    ATTRS{authorized}=="1"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bInterfaceClass}=="0e"
    ATTRS{bInterfaceNumber}=="00"
    ATTRS{bInterfaceProtocol}=="00"
    ATTRS{bInterfaceSubClass}=="01"
    ATTRS{bNumEndpoints}=="01"
    ATTRS{iad_bFirstInterface}=="00"
    ATTRS{iad_bFunctionClass}=="0e"
    ATTRS{iad_bFunctionProtocol}=="00"
    ATTRS{iad_bFunctionSubClass}=="03"
    ATTRS{iad_bInterfaceCount}=="03"
    ATTRS{interface}=="Intel(R) RealSense(TM) 415 Depth"
    ATTRS{supports_autosuspend}=="1"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb2/2-1':
    KERNELS=="2-1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{authorized}=="1"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bDeviceClass}=="ef"
    ATTRS{bDeviceProtocol}=="01"
    ATTRS{bDeviceSubClass}=="02"
    ATTRS{bMaxPacketSize0}=="9"
    ATTRS{bMaxPower}=="440mA"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bNumInterfaces}==" 5"
    ATTRS{bcdDevice}=="508f"
    ATTRS{bmAttributes}=="c0"
    ATTRS{busnum}=="2"
    ATTRS{configuration}==""
    ATTRS{devnum}=="2"
    ATTRS{devpath}=="1"
    ATTRS{idProduct}=="0ad3"
    ATTRS{idVendor}=="8086"
    ATTRS{ltm_capable}=="no"
    ATTRS{manufacturer}=="Intel(R) RealSense(TM) 415"
    ATTRS{maxchild}=="0"
    ATTRS{product}=="Intel(R) RealSense(TM) 415"
    ATTRS{quirks}=="0x0"
    ATTRS{removable}=="removable"
    ATTRS{serial}=="736613021813"
    ATTRS{speed}=="5000"
    ATTRS{urbnum}=="56"
    ATTRS{version}==" 3.10"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb2':
    KERNELS=="usb2"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{authorized}=="1"
    ATTRS{authorized_default}=="1"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bDeviceClass}=="09"
    ATTRS{bDeviceProtocol}=="03"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bMaxPacketSize0}=="9"
    ATTRS{bMaxPower}=="0mA"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bcdDevice}=="0415"
    ATTRS{bmAttributes}=="e0"
    ATTRS{busnum}=="2"
    ATTRS{configuration}==""
    ATTRS{devnum}=="1"
    ATTRS{devpath}=="0"
    ATTRS{idProduct}=="0003"
    ATTRS{idVendor}=="1d6b"
    ATTRS{interface_authorized_default}=="1"
    ATTRS{ltm_capable}=="yes"
    ATTRS{manufacturer}=="Linux 4.15.0-36-generic xhci-hcd"
    ATTRS{maxchild}=="10"
    ATTRS{product}=="xHCI Host Controller"
    ATTRS{quirks}=="0x0"
    ATTRS{removable}=="unknown"
    ATTRS{serial}=="0000:00:14.0"
    ATTRS{speed}=="5000"
    ATTRS{urbnum}=="101"
    ATTRS{version}==" 3.00"

  looking at parent device '/devices/pci0000:00/0000:00:14.0':
    KERNELS=="0000:00:14.0"
    SUBSYSTEMS=="pci"
    DRIVERS=="xhci_hcd"
    ATTRS{broken_parity_status}=="0"
    ATTRS{class}=="0x0c0330"
    ATTRS{consistent_dma_mask_bits}=="64"
    ATTRS{d3cold_allowed}=="1"
    ATTRS{dbc}=="disabled"
    ATTRS{device}=="0xa2af"
    ATTRS{dma_mask_bits}=="64"
    ATTRS{driver_override}=="(null)"
    ATTRS{enable}=="1"
    ATTRS{irq}=="123"
    ATTRS{local_cpulist}=="0-3"
    ATTRS{local_cpus}=="f"
    ATTRS{msi_bus}=="1"
    ATTRS{numa_node}=="-1"
    ATTRS{revision}=="0x00"
    ATTRS{subsystem_device}=="0x310c"
    ATTRS{subsystem_vendor}=="0x17aa"
    ATTRS{vendor}=="0x8086"

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""

Output for sudo udevadm info -ap /devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.3/video4linux/video2:

  looking at device '/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.3/video4linux/video2':
    KERNEL=="video2"
    SUBSYSTEM=="video4linux"
    DRIVER==""
    ATTR{dev_debug}=="0"
    ATTR{index}=="0"
    ATTR{name}=="Intel(R) RealSense(TM) 415: Int"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.3':
    KERNELS=="2-1:1.3"
    SUBSYSTEMS=="usb"
    DRIVERS=="uvcvideo"
    ATTRS{authorized}=="1"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bInterfaceClass}=="0e"
    ATTRS{bInterfaceNumber}=="03"
    ATTRS{bInterfaceProtocol}=="00"
    ATTRS{bInterfaceSubClass}=="01"
    ATTRS{bNumEndpoints}=="00"
    ATTRS{iad_bFirstInterface}=="03"
    ATTRS{iad_bFunctionClass}=="0e"
    ATTRS{iad_bFunctionProtocol}=="00"
    ATTRS{iad_bFunctionSubClass}=="03"
    ATTRS{iad_bInterfaceCount}=="02"
    ATTRS{interface}=="Intel(R) RealSense(TM) 415 RGB"
    ATTRS{supports_autosuspend}=="1"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb2/2-1':
    KERNELS=="2-1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{authorized}=="1"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bDeviceClass}=="ef"
    ATTRS{bDeviceProtocol}=="01"
    ATTRS{bDeviceSubClass}=="02"
    ATTRS{bMaxPacketSize0}=="9"
    ATTRS{bMaxPower}=="440mA"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bNumInterfaces}==" 5"
    ATTRS{bcdDevice}=="508f"
    ATTRS{bmAttributes}=="c0"
    ATTRS{busnum}=="2"
    ATTRS{configuration}==""
    ATTRS{devnum}=="2"
    ATTRS{devpath}=="1"
    ATTRS{idProduct}=="0ad3"
    ATTRS{idVendor}=="8086"
    ATTRS{ltm_capable}=="no"
    ATTRS{manufacturer}=="Intel(R) RealSense(TM) 415"
    ATTRS{maxchild}=="0"
    ATTRS{product}=="Intel(R) RealSense(TM) 415"
    ATTRS{quirks}=="0x0"
    ATTRS{removable}=="removable"
    ATTRS{serial}=="736613021813"
    ATTRS{speed}=="5000"
    ATTRS{urbnum}=="56"
    ATTRS{version}==" 3.10"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb2':
    KERNELS=="usb2"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{authorized}=="1"
    ATTRS{authorized_default}=="1"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bDeviceClass}=="09"
    ATTRS{bDeviceProtocol}=="03"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bMaxPacketSize0}=="9"
    ATTRS{bMaxPower}=="0mA"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bcdDevice}=="0415"
    ATTRS{bmAttributes}=="e0"
    ATTRS{busnum}=="2"
    ATTRS{configuration}==""
    ATTRS{devnum}=="1"
    ATTRS{devpath}=="0"
    ATTRS{idProduct}=="0003"
    ATTRS{idVendor}=="1d6b"
    ATTRS{interface_authorized_default}=="1"
    ATTRS{ltm_capable}=="yes"
    ATTRS{manufacturer}=="Linux 4.15.0-36-generic xhci-hcd"
    ATTRS{maxchild}=="10"
    ATTRS{product}=="xHCI Host Controller"
    ATTRS{quirks}=="0x0"
    ATTRS{removable}=="unknown"
    ATTRS{serial}=="0000:00:14.0"
    ATTRS{speed}=="5000"
    ATTRS{urbnum}=="101"
    ATTRS{version}==" 3.00"

  looking at parent device '/devices/pci0000:00/0000:00:14.0':
    KERNELS=="0000:00:14.0"
    SUBSYSTEMS=="pci"
    DRIVERS=="xhci_hcd"
    ATTRS{broken_parity_status}=="0"
    ATTRS{class}=="0x0c0330"
    ATTRS{consistent_dma_mask_bits}=="64"
    ATTRS{d3cold_allowed}=="1"
    ATTRS{dbc}=="disabled"
    ATTRS{device}=="0xa2af"
    ATTRS{dma_mask_bits}=="64"
    ATTRS{driver_override}=="(null)"
    ATTRS{enable}=="1"
    ATTRS{irq}=="123"
    ATTRS{local_cpulist}=="0-3"
    ATTRS{local_cpus}=="f"
    ATTRS{msi_bus}=="1"
    ATTRS{numa_node}=="-1"
    ATTRS{revision}=="0x00"
    ATTRS{subsystem_device}=="0x310c"
    ATTRS{subsystem_vendor}=="0x17aa"
    ATTRS{vendor}=="0x8086"

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There seems to be one distinguishing factor here:

ATTRS{interface}=="Intel(R) RealSense(TM) 415 Depth"

vs.

ATTRS{interface}=="Intel(R) RealSense(TM) 415 RGB"

And to tell multiple cameras apart, you’ll probably need this one:

ATTRS{serial}=="736613021813"

The udev(7) man page says:

If multiple keys that match a parent device are specified in a single rule, all these keys must match at one and the same parent device.

The attribute that makes the interfaces distinguishable is in the parent block, and the attribute that makes the cameras distinguishable is in the grandparent block.
So, you’ll have to use multiple rule lines.

For each camera, there should be a group of rules like this:

SUBSYSTEM=="video4linux", ATTRS{serial}!="736613021813", GOTO="not_first_camera"
SUBSYSTEM=="video4linux", KERNEL=="video*", ATTRS{interface}=="Intel(R) RealSense(TM) 415 Depth", SYMLINK+="cam1_depth"
SUBSYSTEM=="video4linux", KERNEL=="video*", ATTRS{interface}=="Intel(R) RealSense(TM) 415 RGB", SYMLINK+="cam1_rgb"
LABEL="not_first_camera"

The first rule bypasses the two following rules if the camera serial number does not match. In this way, between the first rule and the LABEL associated with it, we can be sure of the identity of the camera we’re talking about and can concentrate on telling the interfaces apart.

Each block should have an unique LABEL= line and a GOTO= statement that matches the unique label. Of course you can name the SYMLINKs as you wish.

Method 2

In my case I had 4 identical cameras on a USB hub. No serial numbers. To make things worse, some models of camera came up as video0..video3 on boot and others came up as video0..video6 (two per camera). I was taking pics using fswebcam. In the case of the cameras that took up two video numbers only one would work. So my collection of (identical) cameras was either video 0-1-2-3 or video 0-2-4-6

To identify the working video numbers run a loop
fswebcam -q – d /dev/videoX tpic.jpg – where [X] is 0-8
and recorded which X values work.

Then for each one that worked run
udevadm info –attribute walk –name=/dev/videoX > a file

From that copious output in the file (as listed above) extract the (KERNEL==) and (KERNELS==)[USB hub port address] fields. The correspondence between KERNEL and KERNELS does not always survive a reboot – it is possible for the camera that was video0 to become video2 etc etc

So from the extracted information make a list like

KERNEL==”video0″,SUBSYSTEM==”video4linux”,KERNELS==”1-1.1.3:0″
KERNEL==”video1″,SUBSYSTEM==”video4linux”,KERNELS==”1-1.1.1:0″

etc

Finally sort the list on the KERNELS field, so that the KERNELS number (USB hub address) is always in the same place in the list

Then add a SYMLINK at the end of each line in the sorted list

KERNEL==”video1″,SUBSYSTEM==”video4linux”,KERNELS==”1-1.1.1:0″,SYMLINK+=”cam1″
KERNEL==”video0″,SUBSYSTEM==”video4linux”,KERNELS==”1-1.1.3:0″,SYMLINK+=”cam2″

etc

This ties a camera number (cam1-cam4 or whatever you want to call them) to a USB hub port and carries along the video number.

I wrote some python to do it all – lots of os.system bash calls.

Once I had something that worked I lost interest … If there is a simpler way it would be good to know. You could probably do it with bash using grep and stuff


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x