类似设备的 udev 规则

类似设备的 udev 规则

我正在为一些类似的设备(串行 USB 端口)创建 udev 规则时遇到困难。以下是其中一个设备的详细信息输出:

$ udevadm info --name=/dev/ttyACM0 --attribute-walk 

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/devices/pci0000:00/0000:00:15.0/usb1/1-3/1-3:1.0/tty/ttyACM0':
    KERNEL=="ttyACM0"
    SUBSYSTEM=="tty"
    DRIVER==""

  looking at parent device '/devices/pci0000:00/0000:00:15.0/usb1/1-3/1-3:1.0':
    KERNELS=="1-3:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="cdc_acm"
    ATTRS{iad_bFunctionClass}=="02"
    ATTRS{iad_bInterfaceCount}=="02"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bInterfaceNumber}=="00"
    ATTRS{bInterfaceProtocol}=="01"
    ATTRS{iad_bFunctionSubClass}=="02"
    ATTRS{iad_bFirstInterface}=="00"
    ATTRS{bInterfaceSubClass}=="02"
    ATTRS{authorized}=="1"
    ATTRS{iad_bFunctionProtocol}=="00"
    ATTRS{bmCapabilities}=="6"
    ATTRS{supports_autosuspend}=="1"
    ATTRS{bInterfaceClass}=="02"
    ATTRS{bNumEndpoints}=="01"

  looking at parent device '/devices/pci0000:00/0000:00:15.0/usb1/1-3':
    KERNELS=="1-3"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{maxchild}=="0"
    ATTRS{busnum}=="1"
    ATTRS{speed}=="12"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{rx_lanes}=="1"
    ATTRS{bDeviceSubClass}=="02"
    ATTRS{idVendor}=="2341"
    ATTRS{devnum}=="6"
    ATTRS{bNumInterfaces}==" 2"
    ATTRS{bDeviceProtocol}=="01"
    ATTRS{quirks}=="0x0"
    ATTRS{devpath}=="3"
    ATTRS{bMaxPower}=="100mA"
    ATTRS{bDeviceClass}=="ef"
    ATTRS{tx_lanes}=="1"
    ATTRS{version}==" 2.00"
    ATTRS{manufacturer}=="Arduino"
    ATTRS{ltm_capable}=="no"
    ATTRS{removable}=="unknown"
    ATTRS{product}=="Nano 33 BLE"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{idProduct}=="805a"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{authorized}=="1"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{serial}=="F594686789F67DE5"
    ATTRS{bcdDevice}=="0101"
    ATTRS{configuration}==""
    ATTRS{urbnum}=="13"
    ATTRS{bmAttributes}=="c0"

  looking at parent device '/devices/pci0000:00/0000:00:15.0/usb1':
    KERNELS=="usb1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{bDeviceClass}=="09"
    ATTRS{maxchild}=="8"
    ATTRS{ltm_capable}=="no"
    ATTRS{urbnum}=="85"
    ATTRS{manufacturer}=="Linux 5.11.0-38-generic xhci-hcd"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{quirks}=="0x0"
    ATTRS{product}=="xHCI Host Controller"
    ATTRS{bDeviceProtocol}=="01"
    ATTRS{devpath}=="0"
    ATTRS{devnum}=="1"
    ATTRS{rx_lanes}=="1"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{authorized_default}=="1"
    ATTRS{speed}=="480"
    ATTRS{busnum}=="1"
    ATTRS{version}==" 2.00"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{idProduct}=="0002"
    ATTRS{authorized}=="1"
    ATTRS{bMaxPower}=="0mA"
    ATTRS{serial}=="0000:00:15.0"
    ATTRS{configuration}==""
    ATTRS{removable}=="unknown"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{idVendor}=="1d6b"
    ATTRS{bmAttributes}=="e0"
    ATTRS{bcdDevice}=="0511"
    ATTRS{tx_lanes}=="1"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{interface_authorized_default}=="1"

  looking at parent device '/devices/pci0000:00/0000:00:15.0':
    KERNELS=="0000:00:15.0"
    SUBSYSTEMS=="pci"
    DRIVERS=="xhci_hcd"
    ATTRS{revision}=="0x0d"
    ATTRS{irq}=="126"
    ATTRS{driver_override}=="(null)"
    ATTRS{dma_mask_bits}=="64"
    ATTRS{device}=="0x5aa8"
    ATTRS{subsystem_vendor}=="0x8086"
    ATTRS{enable}=="1"
    ATTRS{consistent_dma_mask_bits}=="64"
    ATTRS{vendor}=="0x8086"
    ATTRS{subsystem_device}=="0x5aa8"
    ATTRS{power_state}=="D0"
    ATTRS{local_cpulist}=="0-3"
    ATTRS{dbc}=="disabled"
    ATTRS{class}=="0x0c0330"
    ATTRS{ari_enabled}=="0"
    ATTRS{local_cpus}=="f"
    ATTRS{broken_parity_status}=="0"
    ATTRS{numa_node}=="-1"
    ATTRS{msi_bus}=="1"
    ATTRS{d3cold_allowed}=="1"

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

首先,我想为该设备创建一条规则,而不使用属性serial(见下文)。

/etc/udev/rules.d我创建的10-gesture.rules内容中:

SUBSYSTEM=="tty",SUBSYSTEMS=="usb",DRIVERS=="cdc_acm",ATTRS{idVendor}=="2341",ATTRS{idProduct}=="805a",SYMLINK+="gesture"

然后:

$ sudo chmod 0644 10-gesture.rules
$ udevadm control --reload-rules && udevadm trigger

但未创建符号链接。我还尝试过:

$ sudo udevadm control --log-priority=debug
$ journalctl -f

但我发现没有什么相关内容。

第一个问题:我确实在规则上做错了什么。你能帮我找出问题所在吗?

我不想使用属性,因为serial我需要连接多个这样的设备。我不知道它们的序列号(顺便说一下,实际的设备可能会改变),所以我会依赖设备的类型(SUBSYSTEMSUBSYSTEMS和/属性)。DRIVERSidVendoridProduct

第二个问题:如何为所有符合规则的设备创建带有渐进数字的符号链接(即“gesture1”、“gesture2”)?顺序并不重要。

Ubuntu 20.04

答案1

  • 10-gesture.rules对于此类设备来说,10 还为时过早,并且此规则只是为了创建 simlink。

    仅当您知道要覆盖(全部或部分)或稍后影响其操作的规则时,才尝试使用高数字 90~99)

  • udev 支持变量替换,因此您可以在符号链接名称中使用一些属性。否则,创建一个脚本来验证以前的符号链接(或将最后一个数字存储在文件中),递增并将其与RUN+=""操作一起使用。

相关内容