本文以 飞凌嵌入式 AM335x平台为例进行讲解。 在使用 4G模块的时候, 在 4G 信号不太理想的环境下需要检测网络,当设备断网后重启 4G模块重新拨号联网。但系统长期运行后,重启模块 会导致在 /dev 目录下的4G模块生成的设备节点ttyusb序号改变,导致应用程序拨号时出错。我们可以修改 udev规则文件解决这个问题。
udev 的规则文件里定义了很多变量,如KERNELS ,SUBSYSTEMS ,DRIVERS 等等,我们可以用这些变量中的几个来确定我们的设备,并修改我们其中的NAME 和SYMLINK 变量,这样我们就可以改变我们的设备在/dev 目录下的名字和链接了。
使用 udevadm info命令可以查询udev数据库中的设备信息。也可以从sysfs文件系统中查询到设备的属性以辅助创建udev规则。
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}=="Huawei Mobile Connect - Pcui" 的接口在 /dev 目录下的设备节点由 ttyUSB2 变为 ttyUSB10 ,如下图所示:
如果我们的应用还继续使用 ttyUSB2 ,肯定会出问题的,但是我们使用 USB 串口所做的软链接 ttyusb2 ,就不会发生问题了。