Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5698400
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: LINUX

2008-09-11 23:38:47


2.6.13-rc3内核提供了一个新的特性,可以在用户空间手动的将驱动与设备进行帮定和分离。之前,我们只能够通过rmmod来将整个的驱动从内存中卸载才能将驱动与设备断开联系。

root@dell-desktop:~# lspci | grep Ethernet
00:19.0 Ethernet controller: Intel Corporation 82566DM-2 Gigabit Network Connection (rev 02)
root@dell-desktop:~# cd /sys/bus/pci/devices/0000:00:19.0
root@dell-desktop:/sys/bus/pci/devices/0000:00:19.0# ll
total 0
-rw-r--r-- 1 root root   4096 2008-09-10 10:07 broken_parity_status
lrwxrwxrwx 1 root root      0 2008-09-10 10:07 bus -> ../../../bus/pci
-r--r--r-- 1 root root   4096 2008-09-08 14:32 class
-rw-r--r-- 1 root root    256 2008-09-10 10:07 config
-r--r--r-- 1 root root   4096 2008-09-08 14:32 device
lrwxrwxrwx 1 root root      0 2008-09-11 23:15 driver -> ../../../bus/pci/drivers/e1000e
-rw------- 1 root root   4096 2008-09-10 10:07 enable
-r--r--r-- 1 root root   4096 2008-09-10 10:07 irq
-r--r--r-- 1 root root   4096 2008-09-10 10:07 local_cpulist
-r--r--r-- 1 root root   4096 2008-09-10 10:07 local_cpus
-r--r--r-- 1 root root   4096 2008-09-10 10:07 modalias
-rw-r--r-- 1 root root   4096 2008-09-10 10:07 msi_bus
lrwxrwxrwx 1 root root      0 2008-09-11 23:15 net:eth0 -> ../../../class/net/eth0
drwxr-xr-x 2 root root      0 2008-09-10 10:07 power
-r--r--r-- 1 root root   4096 2008-09-08 14:32 resource
-rw------- 1 root root 131072 2008-09-10 10:07 resource0
-rw------- 1 root root   4096 2008-09-10 10:07 resource1
-rw------- 1 root root     32 2008-09-10 10:07 resource2
lrwxrwxrwx 1 root root      0 2008-09-08 14:32 subsystem -> ../../../bus/pci
-r--r--r-- 1 root root   4096 2008-09-08 14:32 subsystem_device
-r--r--r-- 1 root root   4096 2008-09-08 14:32 subsystem_vendor
-rw-r--r-- 1 root root   4096 2008-09-08 14:32 uevent
-r--r--r-- 1 root root   4096 2008-09-08 14:32 vendor

可以看到网卡的驱动连接到e1000e上面。


unbind取消设备与驱动的联系:

root@dell-desktop:/sys/bus/pci/drivers/e1000e# echo -n "0000:00:19.0" > unbind
root@dell-desktop:/sys/bus/pci/drivers/e1000e# ll
total 0
--w------- 1 root root 4096 2008-09-11 23:23 bind
lrwxrwxrwx 1 root root    0 2008-09-11 23:23 module -> ../../../../module/e1000e
--w------- 1 root root 4096 2008-09-11 23:23 new_id
--w------- 1 root root 4096 2008-09-11 23:23 uevent
--w------- 1 root root 4096 2008-09-11 23:25 unbind


查看PCI相应的目录:

root@dell-desktop:/sys/bus/pci/devices/0000:00:19.0# ll
total 0
-rw-r--r-- 1 root root   4096 2008-09-10 10:07 broken_parity_status
lrwxrwxrwx 1 root root      0 2008-09-10 10:07 bus -> ../../../bus/pci
-r--r--r-- 1 root root   4096 2008-09-08 14:32 class
-rw-r--r-- 1 root root    256 2008-09-10 10:07 config
-r--r--r-- 1 root root   4096 2008-09-08 14:32 device
-rw------- 1 root root   4096 2008-09-10 10:07 enable
-r--r--r-- 1 root root   4096 2008-09-10 10:07 irq
-r--r--r-- 1 root root   4096 2008-09-10 10:07 local_cpulist
-r--r--r-- 1 root root   4096 2008-09-10 10:07 local_cpus
-r--r--r-- 1 root root   4096 2008-09-10 10:07 modalias
-rw-r--r-- 1 root root   4096 2008-09-10 10:07 msi_bus
drwxr-xr-x 2 root root      0 2008-09-10 10:07 power
-r--r--r-- 1 root root   4096 2008-09-08 14:32 resource
-rw------- 1 root root 131072 2008-09-10 10:07 resource0
-rw------- 1 root root   4096 2008-09-10 10:07 resource1
-rw------- 1 root root     32 2008-09-10 10:07 resource2
lrwxrwxrwx 1 root root      0 2008-09-08 14:32 subsystem -> ../../../bus/pci
-r--r--r-- 1 root root   4096 2008-09-08 14:32 subsystem_device
-r--r--r-- 1 root root   4096 2008-09-08 14:32 subsystem_vendor
-rw-r--r-- 1 root root   4096 2008-09-08 14:32 uevent
-r--r--r-- 1 root root   4096 2008-09-08 14:32 vendor


bind重新将设备与驱动进行帮定

root@dell-desktop:/sys/bus/pci/drivers/e1000e# echo -n "0000:00:19.0" > bind
root@dell-desktop:/sys/bus/pci/drivers/e1000e# ll
total 0
lrwxrwxrwx 1 root root    0 2008-09-11 23:25 0000:00:19.0 -> ../../../../devices/pci0000:00/0000:00:19.0
--w------- 1 root root 4096 2008-09-11 23:25 bind
lrwxrwxrwx 1 root root    0 2008-09-11 23:23 module -> ../../../../module/e1000e
--w------- 1 root root 4096 2008-09-11 23:23 new_id
--w------- 1 root root 4096 2008-09-11 23:23 uevent
--w------- 1 root root 4096 2008-09-11 23:25 unbind


查看PCI相应的目录:

root@dell-desktop:/sys/bus/pci/devices/0000:00:19.0# ll
total 0
-rw-r--r-- 1 root root   4096 2008-09-10 10:07 broken_parity_status
lrwxrwxrwx 1 root root      0 2008-09-10 10:07 bus -> ../../../bus/pci
-r--r--r-- 1 root root   4096 2008-09-08 14:32 class
-rw-r--r-- 1 root root    256 2008-09-10 10:07 config
-r--r--r-- 1 root root   4096 2008-09-08 14:32 device
lrwxrwxrwx 1 root root      0 2008-09-11 23:23 driver -> ../../../bus/pci/drivers/e1000e
-rw------- 1 root root   4096 2008-09-10 10:07 enable
-r--r--r-- 1 root root   4096 2008-09-10 10:07 irq
-r--r--r-- 1 root root   4096 2008-09-10 10:07 local_cpulist
-r--r--r-- 1 root root   4096 2008-09-10 10:07 local_cpus
-r--r--r-- 1 root root   4096 2008-09-10 10:07 modalias
-rw-r--r-- 1 root root   4096 2008-09-10 10:07 msi_bus
lrwxrwxrwx 1 root root      0 2008-09-11 23:23 net:eth0 -> ../../../class/net/eth0
drwxr-xr-x 2 root root      0 2008-09-10 10:07 power
-r--r--r-- 1 root root   4096 2008-09-08 14:32 resource
-rw------- 1 root root 131072 2008-09-10 10:07 resource0
-rw------- 1 root root   4096 2008-09-10 10:07 resource1
-rw------- 1 root root     32 2008-09-10 10:07 resource2
lrwxrwxrwx 1 root root      0 2008-09-08 14:32 subsystem -> ../../../bus/pci
-r--r--r-- 1 root root   4096 2008-09-08 14:32 subsystem_device
-r--r--r-- 1 root root   4096 2008-09-08 14:32 subsystem_vendor
-rw-r--r-- 1 root root   4096 2008-09-08 14:32 uevent
-r--r--r-- 1 root root   4096 2008-09-08 14:32 vendor

正如例子中所示,这个特性可以很方便的在处理相同类型的驱动间切换设备,一些商业版的linux发行版会在他们的内核包里面提供多个版本的驱动。这种手动绑定机制可以允许配置工具选择并设置设备与哪一个驱动进行绑定,允许用户只更新他们想要更新的设备驱动。

为了设备可以与一个驱动成功的进行绑定,需要驱动必须支持这个设备。这就是为什么不能将一个随便的设备与一个随便的驱动进行帮定的原因。

这个特性在虚拟机的实现中应用广泛,例如USB设备在虚拟机中的使用。
首先在host中unbind那个USB设备与原有的驱动(usb.ko),然后bind那个USB设备到虚拟机的虚拟USB驱动(vusb.ko)上 面。这样,虚拟机就可以使用USB设备了,但是主机就无法使用这个USB设备了,当主机要使用的时候,再进行ubind和bind操作。

注意:忽然想起之前的libe1000的bind问题,刚开始总是不开窍,一个设备只能够与一个驱动进行bind,在bind其他驱动的时候,就会提示:
bash: echo: write error: No such device
在这个地方糊涂了好长时间。
现在来看libe1000的udev规则文件:
KERNEL=="0000:81:00.0", WAIT_FOR_SYSFS="driver/unbind"
,
PROGRAM="modprobe libe1000",
       ATTR{driver/unbind}="$kernel",ATTR{subsystem/drivers/libe1000/bind}="$kernel"
当sysfs文件系统中"driver/unbind"被创建的时候,执行程序"modprobe libe1000",然后将设备与驱动unbind,然后向libe1000目录下面的bind写数据来绑定设备与驱动。

参考:

阅读(3064) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~