我的设备装到哪了?(udev分析例子)
作者: lanzinc
我的设备装到哪了?
用udev工具,简单分析udev动态建立设备文件的过程。
试验环境:Arch Linux 0.7.2 (Gimmick) 2.6.18-ARCH udev 103-1
有时候,我们把一个设备通过热拔插接入或脱离计算机的时候,KDE界面上一点反应也没用,这时就会考虑,是设备有问题呢,还是没有合适的驱动,还是只是KDE这样的上层建筑没有合适的配置来反应这个改变呢。
这时候应该如何着手解决这个问题呢?
还有时候,我们插如一条数据线,或者usb网卡,我们却不知到设备会别认成什么。
在现在的Linux系统里,一个设备通过热拔插接入计算机的时候,内核如果能够识别该设备,就加载相应的模块,并发布消息;udev从内核得到一组(或单条)消息(uevent),然后根据uevent按照事先建立好的规则(rules),进行一系列操作,(比如对消息进行编辑,生成新的消息,建立设备文件,建立符号链接,运行指定的脚本或程序)。
代码:
Usually udev runs as udevd(8) and receives uevents directly from the kernel if a device is added or removed form the system.
If udev receives a device event, it matches its configured rules against the available device attributes provided in sysfs to identify the device. Rules that match, may provide additional device information or specify a device node name and multiple symlink names and instruct udev to run additional programs as part of the device event handling.
udev软件包,提供了一套工具,来分析和管理udev:
代码:
[root@206studio ~]# pacman -Ql udev | grep bin
udev /sbin/
udev /sbin/migrate-udev
udev /sbin/scsi_id
udev /sbin/udevcontrol
udev /sbin/udevd
udev /sbin/udevsettle
udev /sbin/udevtrigger
udev /usr/bin/
udev /usr/bin/udevinfo
udev /usr/bin/udevtest
udev /usr/sbin/
udev /usr/sbin/udevmonitor
[root@206studio ~]#
现在来解决一个实际问题:
我的Nokia有一条数据线,可以和手机通信,现在把他接到linux上不知道能不能用?
我手头有几个软件,能够从手机上拷贝通信录和短信,但都是和串口通信的。
如果熟悉udev和设备,很快可以到/dev/tty中找到,不过这不影响我们用他来作例子。
首先,把数据线接到USB口
lsusb 看看是什么东西:
代码:
Bus 003 Device 005: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
这个是新增加的(如果不熟悉,可以在插入数据先前先lsusb一下),就是数据线,lsusb能列出来,硬件应该没太多问题,至少是能“说话”的。
运行dmesg看看,内核能否认出来?
最后有几行:
代码:
usb 3-1: new full speed USB device using uhci_hcd and address 5
usb 3-1: configuration #1 chosen from 1 choice
pl2303 3-1:1.0: pl2303 converter detected
usb 3-1: pl2303 converter now attached to ttyUSB0
看来内核是认出来了,不过找不到她说的ttyUSB0的设备文件,怎么办呢?
把数据线拔了,
运行udevmonitor
(详细的udevmonitor信息请用man)
插入数据线
可以看到:
代码:
[root@206studio lanzinc]# udevmonitor
udevmonitor prints the received event from the kernel [UEVENT]
and the event which udev sends out after rule processing [UDEV]
UEVENT[1163352445.301250] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1
UEVENT[1163352445.301608] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/usbdev3.6_ep00
UEVENT[1163352445.303713] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0
UEVENT[1163352445.304027] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/ttyUSB0
UEVENT[1163352445.304246] add@/class/tty/ttyUSB0
UEVENT[1163352445.304417] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/usbdev3.6_ep81
UEVENT[1163352445.304602] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/usbdev3.6_ep02
UEVENT[1163352445.304786] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/usbdev3.6_ep83
UEVENT[1163352445.304974] add@/class/usb_device/usbdev3.6
UDEV [1163352445.337791] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1
UDEV [1163352445.346145] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/usbdev3.6_ep00
UDEV [1163352445.484712] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0
UDEV [1163352445.488357] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/ttyUSB0
UDEV [1163352445.498504] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/usbdev3.6_ep81
UDEV [1163352445.508543] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/usbdev3.6_ep02
UDEV [1163352445.517974] add@/devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/usbdev3.6_ep83
UDEV [1163352445.564777] add@/class/tty/ttyUSB0
UDEV [1163352445.611365] add@/class/usb_device/usbdev3.6
Ctrl+C退出来。
可以清楚的看到设备插入后,udev接收和发送的消息。
用你熟悉的编辑器,比如vi,把上面的内容变成下面这个样子,并存成一个脚本 a.sh
(我用的是kate的替换功能,把"UEVENT.* add@"正则表达式表示的内容替换成"udevtest ",udevtest的信息同样请教man)
代码:
udevtest /devices/pci0000:00/0000:00:10.2/usb3/3-1
udevtest /devices/pci0000:00/0000:00:10.2/usb3/3-1/usbdev3.6_ep00
udevtest /devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0
udevtest /devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/ttyUSB0
udevtest /class/tty/ttyUSB0
udevtest /devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/usbdev3.6_ep81
udevtest /devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/usbdev3.6_ep02
udevtest /devices/pci0000:00/0000:00:10.2/usb3/3-1/3-1:1.0/usbdev3.6_ep83
udevtest /class/usb_device/usbdev3.6
然后运行sh a.sh &> b.txt
这样就得到一个文件b.txt
看看b.txt就知道udev都干了些什么了。
我关心的是udev建立了那些设备文件
运行cat b.txt | grep "creating device node" > c.txt
得到文件c.txt
内容:
代码:
[root@206studio ~]# cat c.txt
udev_node_add: creating device node '/dev/usbdev3.6_ep00', major = '442', minor = '4101', mode = '0660', uid = '0', gid = '0'
udev_node_add: creating device node '/dev/tts/USB0', major = '188', minor = '0', mode = '0660', uid = '0', gid = '5'
udev_node_add: creating device node '/dev/usbdev3.6_ep81', major = '442', minor = '4101', mode = '0660', uid = '0', gid = '0'
udev_node_add: creating device node '/dev/usbdev3.6_ep02', major = '442', minor = '4101', mode = '0660', uid = '0', gid = '0'
udev_node_add: creating device node '/dev/usbdev3.6_ep83', major = '442', minor = '4101', mode = '0660', uid = '0', gid = '0'
udev_node_add: creating device node '/dev/bus/usb/003/006', major = '189', minor = '261', mode = '0664', uid = '0', gid = '0'
[root@206studio ~]#
很明显应该使用/dev/tts/USB0
下面使用xgnokii进行试验:
首先修改配置文件:
代码:
[global]
port = /dev/tts/USB0
model = series60
initlength = default
connection = dku5
use_locking = yes
serial_baudrate = 19200
smsc_timeout = 10
[gnokiid]
bindir = /usr/sbin/
[connect_script]
TELEPHONE = 12345678
[disconnect_script]
[logging]
debug = on
rlpdebug = off
xdebug = on
运行xgnokii
观察debug输出:
很快出现:
代码:
[Sending Ack of type 1b, seq: 4]
Message received: 0x1b / 0x0032
01 31 00 08 00 01 58 2c 00 26 56 20 30 36 2e 30 | 1 X, &V 06.0
31 20 20 20 20 20 0a 31 38 2d 30 33 2d 30 35 0a | 1 18-03-05
52 48 2d 31 39 0a 28 63 29 20 4e 6f 6b 0a 52 00 | RH-19 (c) Nok R
00 00 |
Received message type 1b
Received revision V 06.01
model length: 5
Received model RH-19
Found model "RH-19"
Found model "RH-19"
Model: 3100
Product: RH-19
IMEI: ***********
Revision: V 06.01
Phone connected. Starting monitoring...
通信成功!
下面就是本人的一堆通信录,别看了吧
阅读(2130) | 评论(0) | 转发(0) |