阅读:1810回复:5
请教高手设备插入时驱动定位和驱动栈建立的问题刚学驱动,看了翟洪涛翻译的驱动文章,包括Pnp以及inf文件两章,依然没有弄清楚当系统监测到新设备插入时,系统如何确定此设备所应装载的驱动程序以及驱动栈是怎样建立的。例如插入一块newman的usb flash disk,通过工具devicetree可以知道其对应的驱动程序为usbstor.sys,并看到驱动栈从上往下依次为usbstor.sys, disk.sys, partmgr.sys.但是这个驱动程序是如何定位的,以及这个驱动栈是如何建立的,请高手指点.另外,我从pnp driver test tool看到的flash盘的vid/pid也没有在usbstor.inf中出现,也觉得比较迷惑。 非常感谢! |
|
|
沙发#
发布于:2005-03-29 22:26
是这样的....
当你的u盘插入以后. usbhub能检测到有某个设备连接进来(usb某根数据线的电平超过最小输入电压2.5us).通知windows有一新设备连接进来了. 然后pnp manager发送一系列的pnp irp到这个新设备 比如query caps,query id.query resource,query resource req等等. 和我们讨论有关的就是query id了. pnp manager首先query的是device id.目的是用来定位所谓的device key 他位于current control set\\enum下面. usbhub为其返回的id格式如下 USB\\VID_xxxx&PID_xxxx 当然了,这两个数来自于device descriptor pnp manager接着query instance id.根据需要生成一个instance id string并附加到device id后面形成如下的string.也就是所谓的某个device的instance path USB\\VID_xxxx&PID_xxxx\\xxxx 然后用他作为sub key的名字去打开 LOCAL_MACHINE\\system\\CurrentControlSet\\Enum\\下的某个key 比如我的u盘的instance path是 USB\\Vid_0c45&Pid_1060\\5&1489a85&0&1 对应的device key就是 LOCAL_MACHINE\\System\\CurrentControlSet\\Enum\\USB\\Vid_0c45&Pid_1060\\5&1489a85&0&1 如果这个key已经存在.pnp manager则认为是一个以前就安装好驱动程序的设备.如果这个key不存在,pnp manager则认为是一个新设备,出现发现新硬件的提示.通知user mode要为这个设备选择一个驱动. 先看key存在的情况. pnp manager会query他的resource和resource requirement.并且查询适当的arbiter和translator交给他们作仲裁.并分配适当的资源.资源分配完成了以后.pnp manager开始start这个新的设备. 首先是为其加载function device和filter device. function driver 是由device key下面service字段指出来的 windows读取他.用他作为subkey的name去 current control set\\Services下面打开这个key 按照需要加载其driver的sys文件,创建driver object 而对于filter则有好些种 一类是专属于这个device的,另外一类是属于则device所在的class的 专属device的lower跟upper filters就在device key下面 而class的filter则是在class key的下面 在device key下面有一个classguid的value,pnp manager读取他 作为subkey 到 current control set\\control\\class下面去打开key 然后读取lower 跟upper filters.. 当然这些filters本身的driver也会被加载进来 然后把他们的driver objects放到一个数组里面,先后顺序是 LowerDeviceFilters LowerClassFilters DeviceService UpperDeviceFilters UpperClassFilters 从上到下逐一调用其AddDevice函数 这样就算初步搭建完了这个新设备的device stack 接下来就会有其他的pnp irp发送. 比如filter resource requirement,query caps,start device等等 然后pnp manager为这个新搭建的device stack发送query bus relations 的irp,如果这个新的设备是个bus driver的话,他也会按照通知windows有新的设备连接在其上面.windows再重复以上的步骤. 回过来.如果instance key不存在.windows则为其创建这个instance key并通知user mode,user mode的pnp manager则为其搜索所有可能的inf文件组成一个列表,然后从里面选择一个最佳匹配的inf为其安装驱动程序. 选择的依据就是query id返回的string.包括 device id hardware id compatible id inf文件在其install section里面写出其支持的id,可以是device id也可以是hardware id也可以是compatible id windows会把所有的匹配的inf文件全部收集起来. 比如某device返回device id是id-device hardware id是id-hardware compatible id是id-compatible 如果1.inf %install%= xxxx,id-device 2.inf里面 %install%= xxxx,id-hardware 3.inf里面 %install%= xxxx,id-compatible 那么这三个文件都会被收集起来 如果没有文件被收集到,那么就会出来添加新硬件向导,要求user的参与指定某inf的搜索路径.重新收集inf文件 然后windows从这些收集起来的文件里面选择一个最匹配的文件 从高到低的匹配优先顺序是. whql 的 device id匹配的inf whql 的 hardware id匹配的inf whql 的 compatible id 匹配的inf 未经过whql测试的device id 匹配的inf 未经过whql测试的hardware id匹配的inf 未经过whql测试的compatible it匹配的inf 找到这个最匹配的inf以后 按照inf指出的class要求class installer安装这个新硬件 这个过程就会设置device key下面的成员 比如class guid就会被设置成inf里面指出的guid 对于的class下面会创建一个新的key.也就是所谓的driver key 下面主要放的是与当前选择的inf相关的信息 inf里面指定的添加到driver key下面的reg value也会被添加并设置 serivce也会被设置.对应的service也会被创建在Services下面. 然后按照inf指定的添加到device key下面的reg value也被添加设置 其中就有upper lower filters.以及许多与device配置相关的reg value. 而如果是第一次安装某个class的设备,class key也会被创建,与class关联的upper filter,lower filter也会被创建并设置.这个是由class installer来完成的 至于你的例子 usbstor.sys是为usb 移动硬盘或者u 盘准备的 他的inf文件是usbstor.inf这个文件在windows的inf目录下面 你可以打开看看其指定的device id,hardware id,compatible id都是什么.然后看看usbstor的下层pdo返回的那些ids又是什么,然后看看是怎么匹配的,是匹配的device id还是hardware id还是compatible id. 而usbstor.sys会创建新的设备作为他的child device,他也返回这些个ids,你也看看他的ids都是什么. disk.sys对应的inf文件是disk.inf,同样作作ids的匹配对比 partmgr是disk class的upper filter.你找注册表看看他是在什么地方设置的. 以上 |
|
板凳#
发布于:2005-03-30 15:30
tiamo大侠的论述果然精辟,佩服佩服!
|
|
|
地板#
发布于:2005-03-30 19:03
非常感谢,对着您的解释,也查阅了一下ddk帮助,略微看明白了一些东西。但还有几个问题请教一下,希望不吝指教:
1)DDK中有下面这样的话,请问系统是如何判断有哪些bus filters需要装载,同样的问题是对于lower filters and upper filters. 另外,如果有多个filters需要装载,而且也没有顺序指定,那么是否可能存在问题?例如两个filter实现的功能存在冲突,或一个建立在一个之上? Optional bus filter drivers create filter DOs for each device they filter. When the PnP Manager detects a new device in a BusRelations list, it determines whether there are any bus filter drivers for the device. If so, for each such driver the PnP Manager ensures it is loaded (calls DriverEntry if necessary) and calls the driver\'s AddDevice routine. If the bus filter driver filters operations for this device, the filter driver creates a device object and attaches it to the device stack in its AddDevice routine. If more than one bus filter driver exists and is relevant to this device, each such filter driver creates and attaches its own device object. : 2) 下面是我用device tree工具看到的设备树,有些不清楚,望见谅。其中,有些东西不是很明白。主要问题有: 1. 第14行的MED代表什么意思? 2. FDO Attached的含义是否filter device object? 3. 我理解,第六行的PDO (USB\\Vid_09a6&Pid_8001)应该是对应我的U盘,为什么下面还会有PDO?是否U盘里面的flash也有pnp的概念,能被windows系统自动识别?从而导致disk.sys被加载? 4. 感觉最后两个PDO是完全一样的,只是一个上面有lower filter driver driver\\partmgr,一个没有罢了,为什么会出现这种情况? 非常感谢! |
|
|
地下室#
发布于:2005-03-30 19:10
不好意思,图不知道有什么办法可以插上.图是device tree的pnp图关于u盘的一个节略图.
|
|
|
5楼#
发布于:2005-03-30 19:11
不好意思,图不知道有什么办法可以插上,附件好象也看不到.图是device tree的pnp图关于u盘的一个节略图.
|
|
|