原创 AM335x平台如何通过udev固定4G模块串口名

2020-6-6 17:06 1339 14 14 分类: MCU/ 嵌入式 文集: 干货
本文以飞凌嵌入式AM335x平台为例进行讲解。在使用4G模块的时候,在4G信号不太理想的环境下需要检测网络,当设备断网后重启4G模块重新拨号联网。但系统长期运行后,重启模块会导致在/dev目录下的4G模块生成的设备节点ttyUSB序号改变,导致应用程序拨号时出错。我们可以修改udev规则文件解决这个问题。

udev的规则文件里定义了很多变量,如KERNELS,SUBSYSTEMS,DRIVERS等等,我们可以用这些变量中的几个来确定我们的设备,并修改我们其中的NAME和SYMLINK变量,这样我们就可以改变我们的设备在/dev目录下的名字和链接了。

使用udevadminfo命令可以查询udev数据库中的设备信息。也可以从sysfs文件系统中查询到设备的属性以辅助创建udev规则。

udevadm info -q path -n /dev/ttyUSB0可以查看ttyUSB0设备的系统设备路径

  1. root@ok335x:~# udevadm  info -q path -n /dev/ttyUSB0

  2. /devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2/1-1.2:2.2/ttyUSB0/tty/ttyUSB0

udevadminfo -a -p $(udevadm  info -q path -n/dev/ttyUSB0) 查询/dev/ttyUSB0设备,可以打印出设备总线的所有位置的父子关系,如下所示:
  1. root@am335x:~# udevadm info -a -p $(udevadm  info -q path -n /dev/ttyUSB4)

  2. Udevadm info starts with the device specified by the devpath and then

  3. walks up the chain of parent devices. It prints for every device

  4. found, all possible attributes in the udev rules key format.

  5. A rule to match, can be composed by the attributes of the device

  6. and the attributes from one single parent device.

  7.   looking at device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2/1-1.2:2.2/ttyUSB0':

  8.     KERNEL=="ttyUSB0"

  9.     SUBSYSTEM=="usb-serial"

  10.     DRIVER=="option1"

  11.     ATTR{port_number}=="0"

  12.   looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2/1-1.2:2.2':

  13.     KERNELS=="1-1.2:2.2"

  14.     SUBSYSTEMS=="usb"

  15.     DRIVERS=="option"

  16.     ATTRS{bInterfaceNumber}=="02"

  17.     ATTRS{bAlternateSetting}==" 0"

  18.     ATTRS{bNumEndpoints}=="03"

  19.     ATTRS{bInterfaceClass}=="ff"

  20.     ATTRS{bInterfaceSubClass}=="06"

  21.     ATTRS{bInterfaceProtocol}=="10"

  22.     ATTRS{modalias}=="usb:v12D1p15C1d0102dc00dsc00dpFFicFFisc06ip10"

  23.     ATTRS{supports_autosuspend}=="1"

  24.     ATTRS{interface}=="Huawei Mobile Connect - Modem"

  25.   looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2':

  26.     KERNELS=="1-1.2"

  27.     SUBSYSTEMS=="usb"

  28.     DRIVERS=="usb"

  29.     ATTRS{configuration}==""

  30.     ATTRS{bNumInterfaces}==" 7"

  31.     ATTRS{bConfigurationValue}=="2"

  32.     ATTRS{bmAttributes}=="a0"

  33.     ATTRS{bMaxPower}=="  2mA"

  34.     ATTRS{urbnum}=="26"

  35.     ATTRS{idVendor}=="12d1"

  36.     ATTRS{idProduct}=="15c1"

  37.     ATTRS{bcdDevice}=="0102"

  38.     ATTRS{bDeviceClass}=="00"

  39.     ATTRS{bDeviceSubClass}=="00"

  40.     ATTRS{bDeviceProtocol}=="ff"

  41.     ATTRS{bNumConfigurations}=="3"

  42.     ATTRS{bMaxPacketSize0}=="64"

  43.     ATTRS{speed}=="480"

  44.     ATTRS{busnum}=="1"

  45.     ATTRS{devnum}=="6"

  46.     ATTRS{devpath}=="1.2"

  47.     ATTRS{version}==" 2.00"

  48.     ATTRS{maxchild}=="0"

  49.     ATTRS{quirks}=="0x0"

  50.     ATTRS{avoid_reset_quirk}=="0"

  51.     ATTRS{authorized}=="1"

  52.     ATTRS{manufacturer}=="Huawei Technologies Co., Ltd."

  53.     ATTRS{product}=="HUAWEI Mobile V7R11"

  54.     ATTRS{serial}=="0123456789ABCDEF"

  55.   looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1':

  56.     KERNELS=="1-1"

  57.     SUBSYSTEMS=="usb"

  58.     DRIVERS=="usb"

  59.     ATTRS{configuration}==""

  60.     ATTRS{bNumInterfaces}==" 1"

  61.     ATTRS{bConfigurationValue}=="1"

  62.     ATTRS{bmAttributes}=="e0"

  63.     ATTRS{bMaxPower}=="  2mA"

  64.     ATTRS{urbnum}=="100"

  65.     ATTRS{idVendor}=="0424"

  66.     ATTRS{idProduct}=="2514"

  67.     ATTRS{bcdDevice}=="0bb3"

  68.     ATTRS{bDeviceClass}=="09"

  69.     ATTRS{bDeviceSubClass}=="00"

  70.     ATTRS{bDeviceProtocol}=="02"

  71.     ATTRS{bNumConfigurations}=="1"

  72.     ATTRS{bMaxPacketSize0}=="64"

  73.     ATTRS{speed}=="480"

  74.     ATTRS{busnum}=="1"

  75.     ATTRS{devnum}=="2"

  76.     ATTRS{devpath}=="1"

  77.     ATTRS{version}==" 2.00"

  78.     ATTRS{maxchild}=="4"

  79.     ATTRS{quirks}=="0x0"

  80.     ATTRS{avoid_reset_quirk}=="0"

  81.     ATTRS{authorized}=="1"

  82.   looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1':

  83.     KERNELS=="usb1"

  84.     SUBSYSTEMS=="usb"

  85.     DRIVERS=="usb"

  86.     ATTRS{configuration}==""

  87.     ATTRS{bNumInterfaces}==" 1"

  88.     ATTRS{bConfigurationValue}=="1"

  89.     ATTRS{bmAttributes}=="e0"

  90.     ATTRS{bMaxPower}=="  0mA"

  91.     ATTRS{urbnum}=="26"

  92.     ATTRS{idVendor}=="1d6b"

  93.     ATTRS{idProduct}=="0002"

  94.     ATTRS{bcdDevice}=="0302"

  95.     ATTRS{bDeviceClass}=="09"

  96.     ATTRS{bDeviceSubClass}=="00"

  97.     ATTRS{bDeviceProtocol}=="01"

  98.     ATTRS{bNumConfigurations}=="1"

  99.     ATTRS{bMaxPacketSize0}=="64"

  100.     ATTRS{speed}=="480"

  101.     ATTRS{busnum}=="1"

  102.     ATTRS{devnum}=="1"

  103.     ATTRS{devpath}=="0"

  104.     ATTRS{version}==" 2.00"

  105.     ATTRS{maxchild}=="1"

  106.     ATTRS{quirks}=="0x0"

  107.     ATTRS{avoid_reset_quirk}=="0"

  108.     ATTRS{authorized}=="1"

  109.     ATTRS{manufacturer}=="Linux 3.2.0-dirty musb-hcd"

  110.     ATTRS{product}=="MUSB HDRC host driver"

  111.     ATTRS{serial}=="musb-hdrc.1"

  112.     ATTRS{authorized_default}=="1"

  113.   looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1':

  114.     KERNELS=="musb-hdrc.1"

  115.     SUBSYSTEMS=="platform"

  116.     DRIVERS=="musb-hdrc"

  117.     ATTRS{modalias}=="platform:musb-hdrc"

  118.     ATTRS{mode}=="a_host"

  119.     ATTRS{vbus}=="Vbus off, timeout 1100"

  120.   looking at parent device '/devices/platform/omap/musb-ti81xx':

  121.     KERNELS=="musb-ti81xx"

  122.     SUBSYSTEMS=="platform"

  123.     DRIVERS=="musb-ti81xx"

  124.     ATTRS{modalias}=="platform:musb-ti81xx"

  125.   looking at parent device '/devices/platform/omap':

  126.     KERNELS=="omap"

  127.     SUBSYSTEMS==""

  128.     DRIVERS==""

  129.   looking at parent device '/devices/platform':

  130.     KERNELS=="platform"

  131.     SUBSYSTEMS==""

  132.     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

  1. ATTRS{interface}=="Huawei Mobile Connect - Serial B",SUBSYSTEMS=="usb",SYMLINK+="ttyusb4"

  2. ATTRS{interface}=="Huawei Mobile Connect - Ctrl",SUBSYSTEMS=="usb",SYMLINK+="ttyusb3"

  3. ATTRS{interface}=="Huawei Mobile Connect - Pcui",SUBSYSTEMS=="usb",SYMLINK+="ttyusb2"

  4. ATTRS{interface}=="Huawei Mobile Connect - Application",SUBSYSTEMS=="usb",SYMLINK+="ttyusb1"

  5. 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条评论)

登录后参与讨论
我要评论
0
14
关闭 站长推荐上一条 /2 下一条