udev 区分USB pid vid相同的设备

问题现象

模块lsusb结果dev
IMU USB串口模块10c4:ea60 Silicon Labs CP210x UART Bridge/dev/ttyUSB0
底盘 USB串口模块10c4:ea60 Silicon Labs CP210x UART Bridge/dev/ttyUSB1

1.重启设备时,低概率两个模块的ttyUSB{0,1}序号交换,导致重启系统之后,程序无法运行.
2.想到通过USB pid:vid区分模块, 但两个模块uid:pid相同

想到的方法

通过更细致的udev ATTRS属性区分模块

调试与验证

查询imu usb模块属性

1.插入IMU usb串口模块, 新增/sys/bus/usb/drivers/usb/7-1.1
2.查询usb ATTR属性
udevadm info -a -p /sys/bus/usb/drivers/usb/7-1.1/

  looking at device '/devices/platform/usbdrd3_0/fc000000.usb/xhci-hcd.9.auto/usb7/7-1/7-1.1':
    KERNEL=="7-1.1"
    SUBSYSTEM=="usb"
    DRIVER=="usb"
    ATTR{devspec}=="(null)"
    ATTR{authorized}=="1"
    ATTR{speed}=="12"
    ATTR{quirks}=="0x0"
    ATTR{configuration}==""
    ATTR{devpath}=="1.1"
    ATTR{bNumConfigurations}=="1"
    ATTR{ltm_capable}=="no"
    ATTR{bMaxPower}=="100mA"
    ATTR{bDeviceSubClass}=="00"
    ATTR{rx_lanes}=="1"
    ATTR{idVendor}=="10c4"
    ATTR{bmAttributes}=="80"
    ATTR{maxchild}=="0"
    ATTR{bNumInterfaces}==" 1"
    ATTR{manufacturer}=="Silicon Labs"
    ATTR{busnum}=="7"
    ATTR{idProduct}=="ea60"
    ATTR{bMaxPacketSize0}=="64"
    ATTR{removable}=="unknown"
    ATTR{product}=="CP2102 USB to UART Bridge Controller"
    ATTR{urbnum}=="307803"
    ATTR{bDeviceProtocol}=="00"
    ATTR{bConfigurationValue}=="1"
    ATTR{bDeviceClass}=="00"
    ATTR{avoid_reset_quirk}=="0"
    ATTR{tx_lanes}=="1"
    ATTR{bcdDevice}=="0100"
    ATTR{devnum}=="40"
    ATTR{version}==" 1.10"
    ATTR{serial}=="0001"

查询底盘 usb模块属性

1.插入IMU usb串口模块, 新增/sys/bus/usb/drivers/usb/7-1.2
2.查询usb ATTR属性
udevadm info -a -p /sys/bus/usb/drivers/usb/7-1.2/

  looking at device '/devices/platform/usbdrd3_0/fc000000.usb/xhci-hcd.9.auto/usb7/7-1/7-1.2':
    KERNEL=="7-1.2"
    SUBSYSTEM=="usb"
    DRIVER=="usb"
    ATTR{urbnum}=="127201"
    ATTR{maxchild}=="0"
    ATTR{authorized}=="1"
    ATTR{bDeviceSubClass}=="00"
    ATTR{manufacturer}=="Silicon Labs"
    ATTR{bDeviceClass}=="00"
    ATTR{busnum}=="7"
    ATTR{bDeviceProtocol}=="00"
    ATTR{bConfigurationValue}=="1"
    ATTR{bmAttributes}=="80"
    ATTR{quirks}=="0x0"
    ATTR{ltm_capable}=="no"
    ATTR{version}==" 2.00"
    ATTR{bMaxPower}=="100mA"
    ATTR{removable}=="unknown"
    ATTR{rx_lanes}=="1"
    ATTR{tx_lanes}=="1"
    ATTR{devnum}=="38"
    ATTR{idVendor}=="10c4"
    ATTR{serial}=="6275b2d5040dec11ab81dccebd930c07"
    ATTR{product}=="CP2102N USB to UART Bridge Controller"
    ATTR{avoid_reset_quirk}=="0"
    ATTR{bNumInterfaces}==" 1"
    ATTR{bMaxPacketSize0}=="64"
    ATTR{configuration}==""
    ATTR{bcdDevice}=="0100"
    ATTR{speed}=="12"
    ATTR{devspec}=="(null)"
    ATTR{idProduct}=="ea60"
    ATTR{devpath}=="1.2"
    ATTR{bNumConfigurations}=="1"

对比属性差异

#底盘
-- ATTR{product}=="CP2102 USB to UART Bridge Controller"
-- ATTR{devpath}==" 1.2"

#imu
++ ATTR{version}==" 2.00"
++ ATTR{product}=="CP2102N USB to UART Bridge Controller"

编写规则

/etc/udev/rules.d/900-myself-dev.rules

KERNEL=="ttyUSB*", ACTION=="add",ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", ATTRS{version}==" 2.00",ATTRS{product}=="CP2102N USB to UART Bridge Controller", MODE:="0666", SYMLINK+="ttyImu"
KERNEL=="ttyUSB*", ACTION=="add",ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", ATTRS{version}==" 1.10",ATTRS{product}=="CP2102 USB to UART Bridge Controller", MODE:="0666", SYMLINK+="ttyChaisses"

验证验证:
拔插USB不生效

调试udev方法

方法1: 拔插USB,开启udev日志
udevadm control --log-priority=debug   #设置日志等级
journalctl -f                          #查询udev service日志

May 15 15:56:15 orangepi5 systemd-udevd[21823]: ttyUSB0: Processing device (SEQNUM=5744, ACTION=bind)
May 15 15:56:15 orangepi5 systemd-udevd[21823]: ttyUSB0: /etc/udev/rules.d/900-myself-dev.rules:3 MODE 0666
May 15 15:56:15 orangepi5 systemd-udevd[21823]: ttyUSB0: /etc/udev/rules.d/900-myself-dev.rules:3 RUN ‘/etc/ruichi/udev-test.sh’
May 15 15:56:15 orangepi5 systemd-udevd[21823]: ttyUSB0: Running command “/etc/myself/udev-test.sh”
May 15 15:56:15 orangepi5 systemd-udevd[21823]: ttyUSB0: Starting ‘/etc/myself/udev-test.sh’

通过udev日志可以看到, 有触发900-myself-dev.rules规则, 有枚举对应的/dev/ttyImu设备

方法2: udevadm test模拟
udevadm test /sys/bus/usb/drivers/usb/7-1.2/

Reading rules file: /etc/udev/rules.d/900-ruichi-dev.rules
Invalid inotify descriptor.
DEVPATH=/devices/platform/usbdrd3_0/fc000000.usb/xhci-hcd.9.auto/usb7/7-1/7-1.2
DEVNAME=/dev/bus/usb/007/038
DEVTYPE=usb_device
DRIVER=usb
PRODUCT=10c4/ea60/100
TYPE=0/0/0
BUSNUM=007
DEVNUM=038
MAJOR=189
MINOR=805
ACTION=add
SUBSYSTEM=usb
ID_VENDOR=Silicon_Labs
ID_VENDOR_ENC=Silicon\x20Labs
ID_VENDOR_ID=10c4
ID_MODEL=CP2102N_USB_to_UART_Bridge_Controller
ID_MODEL_ENC=CP2102N\x20USB\x20to\x20UART\x20Bridge\x20Controller
ID_MODEL_ID=ea60
ID_REVISION=0100
ID_SERIAL=Silicon_Labs_CP2102N_USB_to_UART_Bridge_Controller_6275b2d5040dec11ab81dccebd930c07
ID_SERIAL_SHORT=6275b2d5040dec11ab81dccebd930c07
ID_BUS=usb
ID_USB_INTERFACES=:ff0000:
ID_VENDOR_FROM_DATABASE=Silicon Labs
ID_MODEL_FROM_DATABASE=CP210x UART Bridge
REALTEK_NIC_MODE=1
USEC_INITIALIZED=9888901551
Unload module index
Unloaded link configuration context.

结果:
不生效, 不知道是否路径,命令使用问题.

规则不生效解决方法: 重启udev

udevadm control --reload-rules    #重新加载规则
systemctl restart udev.service    #重启udev service

结果:
枚举成功

/dev/ttyUSB0 /dev/ttyChaisses
/dev/ttyUSB1 /dev/ttyImu

更新记录

1.更新rules规则
1.1 ACTION==“bind” – ACTION==“add”
1.2 去掉ACTION==“bind”, 实测也生效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值