udev的规则文件里定义了很多变量,如KERNELS,SUBSYSTEMS,DRIVERS等等,我们可以用这些变量中的几个来确定我们的设备,并修改我们其中的NAME和SYMLINK变量,这样我们就可以改变我们的设备在/dev目录下的名字和链接了。
使用udevadminfo命令可以查询udev数据库中的设备信息。也可以从sysfs文件系统中查询到设备的属性以辅助创建udev规则。
udevadm info -q path -n /dev/ttyUSB0可以查看ttyUSB0设备的系统设备路径
root@ok335x:~# udevadm info -q path -n /dev/ttyUSB0
/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2/1-1.2:2.2/ttyUSB0/tty/ttyUSB0
root@am335x:~# udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB4)
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/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2/1-1.2:2.2/ttyUSB0':
KERNEL=="ttyUSB0"
SUBSYSTEM=="usb-serial"
DRIVER=="option1"
ATTR{port_number}=="0"
looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2/1-1.2:2.2':
KERNELS=="1-1.2:2.2"
SUBSYSTEMS=="usb"
DRIVERS=="option"
ATTRS{bInterfaceNumber}=="02"
ATTRS{bAlternateSetting}==" 0"
ATTRS{bNumEndpoints}=="03"
ATTRS{bInterfaceClass}=="ff"
ATTRS{bInterfaceSubClass}=="06"
ATTRS{bInterfaceProtocol}=="10"
ATTRS{modalias}=="usb:v12D1p15C1d0102dc00dsc00dpFFicFFisc06ip10"
ATTRS{supports_autosuspend}=="1"
ATTRS{interface}=="Huawei Mobile Connect - Modem"
looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2':
KERNELS=="1-1.2"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{configuration}==""
ATTRS{bNumInterfaces}==" 7"
ATTRS{bConfigurationValue}=="2"
ATTRS{bmAttributes}=="a0"
ATTRS{bMaxPower}==" 2mA"
ATTRS{urbnum}=="26"
ATTRS{idVendor}=="12d1"
ATTRS{idProduct}=="15c1"
ATTRS{bcdDevice}=="0102"
ATTRS{bDeviceClass}=="00"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="ff"
ATTRS{bNumConfigurations}=="3"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{speed}=="480"
ATTRS{busnum}=="1"
ATTRS{devnum}=="6"
ATTRS{devpath}=="1.2"
ATTRS{version}==" 2.00"
ATTRS{maxchild}=="0"
ATTRS{quirks}=="0x0"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{authorized}=="1"
ATTRS{manufacturer}=="Huawei Technologies Co., Ltd."
ATTRS{product}=="HUAWEI Mobile V7R11"
ATTRS{serial}=="0123456789ABCDEF"
looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1':
KERNELS=="1-1"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{configuration}==""
ATTRS{bNumInterfaces}==" 1"
ATTRS{bConfigurationValue}=="1"
ATTRS{bmAttributes}=="e0"
ATTRS{bMaxPower}==" 2mA"
ATTRS{urbnum}=="100"
ATTRS{idVendor}=="0424"
ATTRS{idProduct}=="2514"
ATTRS{bcdDevice}=="0bb3"
ATTRS{bDeviceClass}=="09"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="02"
ATTRS{bNumConfigurations}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{speed}=="480"
ATTRS{busnum}=="1"
ATTRS{devnum}=="2"
ATTRS{devpath}=="1"
ATTRS{version}==" 2.00"
ATTRS{maxchild}=="4"
ATTRS{quirks}=="0x0"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{authorized}=="1"
looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1':
KERNELS=="usb1"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{configuration}==""
ATTRS{bNumInterfaces}==" 1"
ATTRS{bConfigurationValue}=="1"
ATTRS{bmAttributes}=="e0"
ATTRS{bMaxPower}==" 0mA"
ATTRS{urbnum}=="26"
ATTRS{idVendor}=="1d6b"
ATTRS{idProduct}=="0002"
ATTRS{bcdDevice}=="0302"
ATTRS{bDeviceClass}=="09"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="01"
ATTRS{bNumConfigurations}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{speed}=="480"
ATTRS{busnum}=="1"
ATTRS{devnum}=="1"
ATTRS{devpath}=="0"
ATTRS{version}==" 2.00"
ATTRS{maxchild}=="1"
ATTRS{quirks}=="0x0"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{authorized}=="1"
ATTRS{manufacturer}=="Linux 3.2.0-dirty musb-hcd"
ATTRS{product}=="MUSB HDRC host driver"
ATTRS{serial}=="musb-hdrc.1"
ATTRS{authorized_default}=="1"
looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1':
KERNELS=="musb-hdrc.1"
SUBSYSTEMS=="platform"
DRIVERS=="musb-hdrc"
ATTRS{modalias}=="platform:musb-hdrc"
ATTRS{mode}=="a_host"
ATTRS{vbus}=="Vbus off, timeout 1100"
looking at parent device '/devices/platform/omap/musb-ti81xx':
KERNELS=="musb-ti81xx"
SUBSYSTEMS=="platform"
DRIVERS=="musb-ti81xx"
ATTRS{modalias}=="platform:musb-ti81xx"
looking at parent device '/devices/platform/omap':
KERNELS=="omap"
SUBSYSTEMS==""
DRIVERS==""
looking at parent device '/devices/platform':
KERNELS=="platform"
SUBSYSTEMS==""
DRIVERS==""
我们查看每个USB串口设备节点的信息,相应设备信息如下:
root@am335x:~#udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB1)
root@am335x:~#udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB2)
root@am335x:~#udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB3)
root@am335x:~#udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB4)
我们通过观察各个设备节点的信息,返现ATTRS{interface}可以区别各个设备节点。
所以我们将SUBSYSTEMS=="usb" ATTRS{interface}=="Huawei Mobile Connect - Modem" 作为过滤条件。
在/etc/udev/rules.d/local.rules 中加入以下内容。将ttyUSB0-4做相应的软链接为ttyusb0-4
ATTRS{interface}=="Huawei Mobile Connect - Serial B",SUBSYSTEMS=="usb",SYMLINK+="ttyusb4"
ATTRS{interface}=="Huawei Mobile Connect - Ctrl",SUBSYSTEMS=="usb",SYMLINK+="ttyusb3"
ATTRS{interface}=="Huawei Mobile Connect - Pcui",SUBSYSTEMS=="usb",SYMLINK+="ttyusb2"
ATTRS{interface}=="Huawei Mobile Connect - Application",SUBSYSTEMS=="usb",SYMLINK+="ttyusb1"
ATTRS{interface}=="Huawei Mobile Connect - Modem",SUBSYSTEMS=="usb",SYMLINK+="ttyusb0"
我们重启系统输入查看设备节点
通过查看,完成了设备节点的软链接
如果我们在具体应用中通过/dev/ttyUSB2进行拨号上网。在实际长期运行过程中,我们的4g网络守护进程检测到4G离网时,会重启4G模块,有可能导致ttyUSB序号发生变化,如下:
通过查看/dev/ttyUSB10的设备信息与前期的设备信息对比发现,多次反复4G模块重启,属性为ATTRS{interface}=="HuaweiMobile Connect - Pcui"的接口在/dev目录下的设备节点由ttyUSB2变为ttyUSB10,如下图所示:
如果我们的应用还继续使用ttyUSB2,肯定会出问题的,但是我们使用USB串口所做的软链接ttyusb2,就不会发生问题了。
作者: ZXCZB123, 来源:面包板社区
链接: https://mbb.eet-china.com/blog/uid-me-3909953.html
版权声明:本文为博主原创,未经本人允许,禁止转载!
文章评论(0条评论)
登录后参与讨论