您的位置:首页 > 其它

【原创】如何编写udev规则实现特定usb口设备创建指定的设备文件名?

2015-09-10 13:52 399 查看

【原创】如何编写udev规则实现特定usb口设备创建指定的设备文件名?

Author: chad

Mail: linczone@163.com

电脑上连接了4个uvc摄像头(编号A/B/C/D),每次重启系统后摄像头对应的设备文件名(/dev/video*)都会改变,很难确定摄像头编号与设备文件之间的对应关系!!!

后来查看文件
/proc/bus/input/devices
发现还是有规律可循的。

/proc/bus/input/devices
文件内容摘录如下:

I: Bus=0003 Vendor=04f2 Product=a14b Version=0428
N: Name="UVC Camera (04f2:a14b)"
P: Phys=usb-0000:00:14.0-1/button
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/input/input2
U: Uniq=
H: Handlers=kbd event2 
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=04f2 Product=a14b Version=0428
N: Name="UVC Camera (04f2:a14b)"
P: Phys=usb-0000:00:14.0-3/button
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.0/input/input3
U: Uniq=
H: Handlers=kbd event3 
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=04f2 Product=a14b Version=0428
N: Name="UVC Camera (04f2:a14b)"
P: Phys=usb-0000:00:1a.0-1.1/button
S: Sysfs=/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.1/1-1.1:1.0/input/input4
U: Uniq=
H: Handlers=kbd event4 
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=04f2 Product=a14b Version=0428
N: Name="UVC Camera (04f2:a14b)"
P: Phys=usb-0000:00:1d.0-1.5/button
S: Sysfs=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.5/2-1.5:1.0/input/input5
U: Uniq=
H: Handlers=kbd event5 
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0


通过比对发现,字段:

S: Sysfs=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.5/2-1.5:1.0/input/input5


已经标识出了usb接口 与 input序号之间的关系。由于设备安装完成后,摄像头安装的usb端口是固定的,及假设摄像头A 安装在
usb2/2-1/2-1.5/2-1.5
这个位置,那么这一部分是固定的,后面的
input5
标识出了对应的设备文件名信息。

但是如何编写udev规则,保证指定usb节点上的设备文件名固定呢?*

查看
/sys/class/video4linux/video0/
下的文件发现有用的信息,离解决问题更近一步了!

yr@yr-desktop:/proc$ ls /sys/class/video4linux/video0/ -l
total 0
-rw-r--r-- 1 root root 4096  9月 10 14:56 debug
-r--r--r-- 1 root root 4096  9月 10 14:56 dev
lrwxrwxrwx 1 root root    0  9月 10 14:56 device -> ../../../3-1:1.0
-r--r--r-- 1 root root 4096  9月 10 14:34 index
-r--r--r-- 1 root root 4096  9月 10 14:56 name
drwxr-xr-x 2 root root    0  9月 10 14:56 power


经过qq群里面一个哥们提醒,又看了下一直忽略的
lsusb  与 dmesg
打印信息,结果如下:





有了usb端口信息,有了VID,PID信息,但是udev规则中的其他参数信息从哪里查询呢???

————————————————-2015-09-10

经过大量搜阅资料,最后发现可以使用udevinfo命令,但是Ubuntu不支持该命令,ubuntu下对应的指令为:

udevadm info -a  /dev/video0


输出结果摘录如下:

yr@yr-desktop:~$ udevadm info -a  /dev/video0

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:14.0/usb3/3-1/3-1:1.0/video4linux/video0':
    KERNEL=="video0"
    SUBSYSTEM=="video4linux"
    DRIVER==""
    ATTR{name}=="UVC Camera (04f2:a14b)"
    ATTR{debug}=="0"
    ATTR{index}=="0"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0':
    KERNELS=="3-1:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="uvcvideo"
  。。。。。。。
    ATTRS{bNumEndpoints}=="01"
    ATTRS{iad_bFunctionSubClass}=="03"
    ATTRS{iad_bFunctionProtocol}=="00"
    ATTRS{supports_autosuspend}=="1"
  。。。。
  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-1':
    KERNELS=="3-1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{bDeviceSubClass}=="02"
    ATTRS{bDeviceProtocol}=="01"
    ATTRS{devpath}=="1"
    ATTRS{idVendor}=="04f2"
    ATTRS{speed}=="480"
 。。。。。。。
    ATTRS{configuration}==""
    ATTRS{bMaxPower}=="500mA"
    ATTRS{authorized}=="1"
    ATTRS{bmAttributes}=="80"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{maxchild}=="0"
    ATTRS{bcdDevice}=="0428"
 。。。。。。
    ATTRS{removable}=="unknown"
    ATTRS{idProduct}=="a14b"
    ATTRS{bDeviceClass}=="ef"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3':
    KERNELS=="usb3"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceProtocol}=="01"
。。。。。
    ATTRS{idVendor}=="1d6b"
。。。。。
    ATTRS{configuration}==""
    ATTRS{bMaxPower}=="0mA"
    ATTRS{authorized}=="1"
    ATTRS{bmAttributes}=="e0"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{maxchild}=="4"
    ATTRS{bcdDevice}=="0316"
    ATTRS{avoid_reset_quirk}=="0"
。。。。。
    ATTRS{ltm_capable}=="no"
    ATTRS{manufacturer}=="Linux 3.16.0-30-generic xhci_hcd"
。。。。

  looking at parent device '/devices/pci0000:00/0000:00:14.0':
    KERNELS=="0000:00:14.0"
    SUBSYSTEMS=="pci"
    DRIVERS=="xhci_hcd"
   。。。。。。。
    ATTRS{local_cpus}=="00000000,00000000,00000000,00000000,00000000,00000000,00000000,0000000f"
    ATTRS{device}=="0x1e31"
    ATTRS{enable}=="1"
    ATTRS{msi_bus}==""
。。。。。。

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


如上所示,信息包含整个usb链簇关系图谱,由于我的目的是固定usb端点对应固定序号的摄像头,所以我需要的信息主要如下:






根据以上信息,在目录
/etc/udev/rules.d
下新建udev规则文件
81-uvccam.rules
(81指主设备号,该文件只要以.rules为后缀即可)。内容如下:

root@yr-desktop:/etc/udev/rules.d# cat 81-uvccam.rules 
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# USB UVC Camera (04f2:a14b)
KERNEL=="video*", SUBSYSTEMS=="usb", ATTRS{idVendor}=="04f2", ATTRS{idProduct}=="a14b", KERNELS=="3-1",NAME="video0", SYMLINK+="my_video1"
KERNEL=="video*", SUBSYSTEMS=="usb", ATTRS{idVendor}=="04f2", ATTRS{idProduct}=="a14b", KERNELS=="3-2",NAME="video1", SYMLINK+="my_video2"
KERNEL=="video*", SUBSYSTEMS=="usb", ATTRS{idVendor}=="04f2", ATTRS{idProduct}=="a14b", KERNELS=="1-1.1",NAME="video2", SYMLINK+="my_video3"
KERNEL=="video*", SUBSYSTEMS=="usb", ATTRS{idVendor}=="04f2", ATTRS{idProduct}=="a14b", KERNELS=="2-1.5",NAME="video3", SYMLINK+="my_video4"
KERNEL=="video*", SUBSYSTEMS=="usb", ATTRS{idVendor}=="04f2", ATTRS{idProduct}=="a14b", KERNELS=="3-3",NAME="video4", SYMLINK+="my_video5"


保存后重启系统,经多次图像采集测试问题解决。

root@yr-desktop:/etc/udev/rules.d# ls -l /dev/my_video* 
lrwxrwxrwx 1 root root 6  9月 11 09:33 /dev/my_video1 -> video0
lrwxrwxrwx 1 root root 6  9月 11 09:33 /dev/my_video2 -> video1
lrwxrwxrwx 1 root root 6  9月 11 09:33 /dev/my_video3 -> video3
lrwxrwxrwx 1 root root 6  9月 11 09:33 /dev/my_video4 -> video4
lrwxrwxrwx 1 root root 6  9月 11 09:33 /dev/my_video5 -> video2


——————————————————2015-09-11
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: