近日,看了博客:多个设备对象(同一个驱动),它创建了多个设备对象,对应不同的设备,但是采用该博文介绍的方法创建的设备对象是垂直关系的。有没有多个设备对象呈水平关系的方法呢?下面将以微软发布的“PLX9x5x设备驱动示例”进行讲解。
一、Device ID
“PLX9x5x设备驱动示例”将同一个驱动,用于于不同的设备——“9056”和“9656”,主要是在它的inf文件中包含了两种不同ID的设备。参看INF文件该节:
; For XP and later
[MSFT.NTamd64]
; DisplayName Section DeviceId
; ----------- ------- --------
%Pci9056.DRVDESC%= Pci9x5x_Inst, PCI\VEN_10b5&DEV_5601
%Pci9656.DRVDESC%= Pci9x5x_Inst, PCI\VEN_10b5&DEV_9601
该节为 “9056”和“9656”关联了不同的ID,当功能相同,都是应用”Pci9x5x_Inst“。如果要创建不一样的功能,可以生成两个sys文件,同一个GUID即可。然后,需要让“section -- pxi9x5x_inst”复制两份。
再看字符节:
[Strings]
ProviderString = "TODO-Set-Provider"
ManufacturerString = "TODO-Set-Manufacturer"
ClassName = "Sample Device"
Pci9x5x.SVCDESC = "Sample Driver Service for the PCI9x5xRDK-Lite adapter"
Pci9056.DRVDESC = "Sample Driver for the PCI9056RDK-Lite adapter"
Pci9656.DRVDESC = "Sample Driver for the PCI9656RDK-Lite adapter"
DISK_NAME = "Pci9x5x Sample Install Disk"
“9056”和“9656”这两个不同ID的设备的符号名是不同的。 在应用程序中,需要调“SetupDiEnumDeviceInterfaces”函数扫描一个设备列表,再通过根据设备的符号名去区分。
二、设备实例路径(Device Instance ID)
对于不同类型的设备,可以按照上述的方法根据Device ID去标识不同的设备。如果是同一种设备,Device ID相同的情况下,则需要根据“设备实例路径”来标识。
设备实例路径是总线根据设备在总线上的插槽位置生成的ID,它是“instance ID和device ID的组合”。同一设备在不同的插槽,该ID不同;不同设备在同一插槽,该ID也不同。只有同一设备插在同一插槽位置,该ID才不变。
MSDN文档:
A device instance ID is a system-supplied device identification string that uniquely identifies a device in the system. The Plug and Play (PnP) manager assigns a device instance ID to each device node (devnode) in a system's device tree.
The format of this string consists of an instance ID concatenated to a device ID, as follows:
<device-ID>\<instance-specific-ID>
The number of characters of a device instance ID, excluding a NULL-terminator, must be less than MAX_DEVICE_ID_LEN. This constraint applies to the sum of the lengths of all the fields and "\" field separator between the device ID and instance-specific-ID fields.
A device instance ID is persistent across system restarts.
The following is an example of an instance ID ("1&08") concatenated to a device ID for a PCI device:
PCI\VEN_1000&DEV_0001&SUBSYS_00000000&REV_02\1&08
可以通过函数:SetupDiGetDeviceInstanceId 获取它。
另外可以参考博文:USB设备加载流程
三、同一个GUID和INF,多个设备,多个.sys文件
有时候,我们需要为不同类型的设备开发不同的驱动程序,然而在使用的时候,又需要将这多个设备绑定为一个设备组。比如,现在有D1、D2、D3、D4、D5、D6六个设备,其中D1、D2共用一个驱动程序A.sys,其他的设备使用驱动程序B.sys。他们组成一个设备组,共用一个GUID和INF文件,但需要对INF做如下处理。在此,以PLX9x5x工程的INF文件为基础,为其添加一个名为XiLinx.sys的驱动。
1,添加一个驱动二进制文件
[SourceDisksFiles]
Pci9x5x.sys=1
XiLin.sys=1
2,添加设备ID和安装节
[MSFT.NTamd64]
; DisplayName Section DeviceId
; ----------- ------- --------
%Pci9056.DRVDESC%= Pci9x5x_Inst, PCI\VEN_104c&DEV_5602
%Pci9656.DRVDESC%= Pci9x5x_Inst, PCI\VEN_104c&DEV_9601
%XiLinx.RFTXDRVDESC%= XiLinx_Inst, PCI\VEN_10EE&DEV_7019&SUBSYS_000710EE&REV_00
%XiLinx.RFRXDRVDESC%= XiLinx_Inst, PCI\VEN_10EE&DEV_7020&SUBSYS_000710EE&REV_00
.........
3,将Inst节全部拷贝一份并添加string节
全部修改的INF如下:
;/*++
;
;Copyright (c) Microsoft Corporation. All rights reserved.
;
; THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
; KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
; IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
; PURPOSE.
;
;Module Name:
;
; Pci9x5x.INF
;
;Abstract:
; INF file for the PLx PCI9x5xRDK-Lite driver.
;
;--*/
[Version]
Signature="$WINDOWS NT$"
Class=Sample
ClassGuid={78A1C341-4539-11d3-B88D-00C04FAD5171}
Provider=%ProviderString%
DriverVer=04/22/2016,14.39.56.418
CatalogFile=KmdfSamples.cat
[DestinationDirs]
DefaultDestDir = 12
; ================= Class section =====================
[ClassInstall32]
Addreg=SampleClassReg
[SampleClassReg]
HKR,,,0,%ClassName%
HKR,,Icon,,-5
HKR,,DeviceCharacteristics,0x10001,0x100 ;Use same security checks on relative opens
HKR,,Security,,"D:P(A;;GA;;;SY)(A;;GA;;;BA)" ;Allow generic all access to system and built-in Admin.
; ================= Device Install section =====================
[ControlFlags]
ExcludeFromSelect=*
[Manufacturer]
%ManufacturerString%=MSFT,NTamd64
[SourceDisksFiles]
Pci9x5x.sys=1
XiLin.sys=1
[SourceDisksNames]
1=%DISK_NAME%,
; For XP and later
[MSFT.NTamd64]
; DisplayName Section DeviceId
; ----------- ------- --------
%Pci9056.DRVDESC%= Pci9x5x_Inst, PCI\VEN_104c&DEV_5602
%Pci9656.DRVDESC%= Pci9x5x_Inst, PCI\VEN_104c&DEV_9601
%XiLinx.RFTXDRVDESC%= XiLinx_Inst, PCI\VEN_10EE&DEV_7019&SUBSYS_000710EE&REV_00
%XiLinx.RFRXDRVDESC%= XiLinx_Inst, PCI\VEN_10EE&DEV_7020&SUBSYS_000710EE&REV_00
;%XiLinx.DIGIFDRVDESC%= XiLinx_BB_Inst, PCI\VEN_10EE&DEV_7018&SUBSYS_000710EE&REV_00
%XiLinx.SALODRVDESC%= XiLinx_Inst, PCI\VEN_10EE&DEV_7021&SUBSYS_000710EE&REV_00
%XiLinx.RFIODRVDESC%= XiLinx_Inst, PCI\VEN_10EE&DEV_7022&SUBSYS_000710EE&REV_00
%XiLinx.REFDRVDESC%= XiLinx_Inst, PCI\VEN_10EE&DEV_7023&SUBSYS_000710EE&REV_00
%XiLinx.REFLEXDRVDESC%= XiLinx_Inst, PCI\VEN_10EE&DEV_7024&SUBSYS_000710EE&REV_00
[Pci9x5x_Inst.NT]
CopyFiles=Pci9x5x.CopyFiles
[XiLinx_Inst.NT]
CopyFiles=XiLinx.CopyFiles
[Pci9x5x.CopyFiles]
Pci9x5x.sys
[XiLinx.CopyFiles]
XiLinx_TrB.sys
[Pci9x5x_Inst.NT.Services]
AddService=Pci9x5x,0x00000002,Pci9x5x_Service
[XiLinx_Inst.NT.Services]
AddService=XiLinxService,0x00000002,XiLinx_Service
[Pci9x5x_Service]
DisplayName = %Pci9x5x.SVCDESC%
ServiceType = 1 ; SERVICE_KERNEL_DRIVER
StartType = 3 ; SERVICE_DEMAND_START
ErrorControl = 1 ; SERVICE_ERROR_NORMAL
ServiceBinary = %12%\Pci9x5x.sys
[XiLinx_Service]
DisplayName = %XiLinx.SVCDESC%
ServiceType = 1 ; SERVICE_KERNEL_DRIVER
StartType = 3 ; SERVICE_DEMAND_START
ErrorControl = 1 ; SERVICE_ERROR_NORMAL
ServiceBinary = %12%\XiLinx_TrB.sys
;AddReg = XiLinx_Parameters_AddReg
[Pci9x5x_Inst.NT.Wdf]
KmdfService = Pci9x5x, Pci9x5x_wdfsect
[Pci9x5x_wdfsect]
KmdfLibraryVersion = 1.9
[XiLinx_Inst.NT.Wdf]
KmdfService = XiLinxService, XiLinxService_wdfsect
[XiLinxService_wdfsect]
KmdfLibraryVersion = 1.9
[Strings]
ProviderString = "TODO-Set-Provider"
ManufacturerString = "TODO-Set-Manufacturer"
ClassName = "Sample Device"
Pci9x5x.SVCDESC = "Sample Driver Service for the PCI9x5xRDK-Lite adapter"
Pci9056.DRVDESC = "Digital BaseBand"
;Pci9056.DRVDESC = "Sample Driver for the PCI9056RDK-Lite adapter"
Pci9656.DRVDESC = "Sample Driver for the PCI9656RDK-Lite adapter"
XiLinx.SVCDESC = "XiLinx Service for the XiLinx board"
;XiLinx.DIGIFDRVDESC = "Digital BaseBand Driver for the PXIE Board"
XiLinx.RFTXDRVDESC = "Singal Generator Driver for the PXIE Board"
XiLinx.RFRXDRVDESC = "Singal Analysis Driver for the PXIE Board"
XiLinx.SALODRVDESC = "Singal Analysis LO Driver for the PXIE Board"
XiLinx.RFIODRVDESC = "PXI Multi-way Active RF Combiner"
XiLinx.REFDRVDESC = "XiLinx Reference Driver for the PXIE board"
XiLinx.REFLEXDRVDESC = "Singal Reflex Driver for the PXIE board"
DISK_NAME = "Pci9x5x Sample Install Disk"