USB驱动程序的加载(一)
USB设备插入系统后,系统会从HKEY_LOCAL_MACHINE\Drivers\USB\LoadClients\Group1_ID\Group2_ID\Group3_ID\DriverName加载USB驱动
其中Group1_ID可以为
DeviceVendorID,
DeviceVendorID_DeviceProductID,
DeviceVendorID_DeviceProductID_DeviceReleaseNumber
或Default
其中Group2_ID可以为
DeviceClassCode,
DeviceClassCode_DeviceSubclassCode,
DeviceClassCode_DeviceSubclassCode_DeviceProtocolCode
或Default
其中Group3_ID可以为
InterfaceClassCode,
InterfaceClassCode_InterfaceSubclassCode,
InterfaceClassCode_InterfaceSubclassCode_InterfaceProtocolCode
或Default
最终的注册表值可以为上面三个Group的不同组合,最多可达64种.
例如,有一USB摄像头设备如下:
Device Descriptor:
bcdUSB: 0x0110
bDeviceClass: 0xEF DeviceClassCode
bDeviceSubClass: 0x02 DeviceSubclassCode
bDeviceProtocol: 0x01 DeviceProtocolCode
bMaxPacketSize0: 0x40 (64)
idVendor: 0x1C4F DeviceVendorID
idProduct: 0x3000 DeviceProductID
bcdDevice: 0x0100 DeviceReleaseNumber
iManufacturer: 0x01
0x0409: "SiGma Micro"
iProduct: 0x02
0x0409: "USB Web Camera"
0x0409: "USB Web Camera"
iSerialNumber: 0x00
bNumConfigurations: 0x01
ConnectionStatus: DeviceConnected
Current Config Value: 0x01
Device Bus Speed: Full
Device Address: 0x01
Open Pipes: 1
Endpoint Descriptor:
bEndpointAddress: 0x81
Transfer Type: Interrupt
wMaxPacketSize: 0x0008 (8)
bInterval: 0x20
Configuration Descriptor:
wTotalLength: 0x0156
bNumInterfaces: 0x02
bConfigurationValue: 0x01
iConfiguration: 0x00
bmAttributes: 0x80 (Bus Powered )
MaxPower: 0x7D (250 Ma)
Unknown Descriptor:
bDescriptorType: 0x0B
bLength: 0x08
08 0B 00 02 0E 03 00 02
Interface Descriptor:
bInterfaceNumber: 0x00
bAlternateSetting: 0x00
bNumEndpoints: 0x01
bInterfaceClass: 0x0E InterfaceClassCode
bInterfaceSubClass: 0x01 InterfaceSubclassCode
bInterfaceProtocol: 0x00 InterfaceProtocolCode
iInterface: 0x02
0x0409: "USB Web Camera"
0x0409: "USB Web Camera"
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x0D
0D 24 01 00 01 43 00 00 6C DC 02 01 01
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x12
12 24 02 01 01 02 00 00 00 00 00 00 00 00 03 0A
20 04
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x08
08 24 02 02 01 04 00 00
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x09
09 24 03 03 01 01 00 05 00
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x08
08 24 04 04 02 01 02 00
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x0B
0B 24 05 05 04 00 00 02 3F 16 00
Endpoint Descriptor:
bEndpointAddress: 0x81
Transfer Type: Interrupt
wMaxPacketSize: 0x0008 (8)
bInterval: 0x20
Unknown Descriptor:
bDescriptorType: 0x25
bLength: 0x05
05 25 03 08 00
Interface Descriptor:
bInterfaceNumber: 0x01
bAlternateSetting: 0x00
bNumEndpoints: 0x00
bInterfaceClass: 0x0E
bInterfaceSubClass: 0x02
bInterfaceProtocol: 0x00
iInterface: 0x00
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x0E
0E 24 01 01 91 00 88 00 03 02 01 00 01 00
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x0B
0B 24 06 01 03 10 01 00 00 00 00
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x26
26 24 07 01 03 80 02 E0 01 00 00 4B 00 00 00 4B
00 00 60 09 00 15 16 05 00 00 15 16 05 00 2A 2C
0A 00 15 16 05 00
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x26
26 24 07 02 03 40 01 F0 00 00 00 4B 00 00 00 4B
00 00 60 09 00 15 16 05 00 00 15 16 05 00 2A 2C
0A 00 15 16 05 00
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x26
26 24 07 03 03 A0 00 78 00 00 00 4B 00 00 00 4B
00 00 60 09 00 15 16 05 00 00 15 16 05 00 2A 2C
0A 00 15 16 05 00
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x13
13 24 03 00 03 80 02 E0 01 40 01 F0 00 A0 00 78
00 01 80
Unknown Descriptor:
bDescriptorType: 0x24
bLength: 0x06
06 24 0D 01 01 01
Interface Descriptor:
bInterfaceNumber: 0x01
bAlternateSetting: 0x01
bNumEndpoints: 0x01
bInterfaceClass: 0x0E
bInterfaceSubClass: 0x02
bInterfaceProtocol: 0x00
iInterface: 0x00
Endpoint Descriptor:
bEndpointAddress: 0x88
Transfer Type: Isochronous
wMaxPacketSize: 0x025C (604)
bInterval: 0x01
Interface Descriptor:
bInterfaceNumber: 0x01
bAlternateSetting: 0x02
bNumEndpoints: 0x01
bInterfaceClass: 0x0E
bInterfaceSubClass: 0x02
bInterfaceProtocol: 0x00
iInterface: 0x00
Endpoint Descriptor:
bEndpointAddress: 0x88
Transfer Type: Isochronous
wMaxPacketSize: 0x03BC (956)
bInterval: 0x01
Interface Descriptor:
bInterfaceNumber: 0x01
bAlternateSetting: 0x03
bNumEndpoints: 0x01
bInterfaceClass: 0x0E
bInterfaceSubClass: 0x02
bInterfaceProtocol: 0x00
iInterface: 0x00
Endpoint Descriptor:
bEndpointAddress: 0x88
Transfer Type: Isochronous
wMaxPacketSize: 0x024C (588)
bInterval: 0x01
Interface Descriptor:
bInterfaceNumber: 0x01
bAlternateSetting: 0x04
bNumEndpoints: 0x01
bInterfaceClass: 0x0E
bInterfaceSubClass: 0x02
bInterfaceProtocol: 0x00
iInterface: 0x00
Endpoint Descriptor:
bEndpointAddress: 0x88
Transfer Type: Isochronous
wMaxPacketSize: 0x02B0 (688)
bInterval: 0x01
系统会搜索如下路径:
Drivers\USB\LoadClients\7247_12288_256\239_2_1\Default
Drivers\USB\LoadClients\7247_12288_256\239_2\Default
Drivers\USB\LoadClients\7247_12288_256\239\Default
Drivers\USB\LoadClients\7247_12288\239_2_1\Default
Drivers\USB\LoadClients\7247_12288\239_2\Default
Drivers\USB\LoadClients\7247_12288\239\Default
Drivers\USB\LoadClients\7247\239_2_1\Default
Drivers\USB\LoadClients\7247\239_2\Default
Drivers\USB\LoadClients\7247\239\Default
Drivers\USB\LoadClients\7247_12288_256\Default\Default
Drivers\USB\LoadClients\7247_12288\Default\Default
Drivers\USB\LoadClients\7247\Default\Default
Drivers\USB\LoadClients\Default\239_2_1\Default
Drivers\USB\LoadClients\Default\239_2\Default
Drivers\USB\LoadClients\Default\239\Default
...
如果以上路径都没有,则提示用户输入驱动程序名.
用户输入驱动程序名后,系统调用该驱动程序中的USBInstallDriver().USBInstallDriver()会调用RegisterClientDriverID()与RegisterClientSettings()注册USB Client 驱动.
该函数成功调用后,会生成如下的注册表项:
HKEY_LOCAL_MACHINE\Drivers\USB\ClientDrivers\Video_Class
"DLL"="camera.dll"
"Prefix"="CAM"
[HKEY_LOCAL_MACHINE\Drivers\USB\LoadClients\GroupID1\GroupID2\GroupID3\Video_Class]
"DLL"="camera.dll"
并且会重新调用LoadUSBClient()来加载USB Client Driver.
其中GroupID1,GroupID2,GroupID3为上述的某种组合,Video_Class会因为设备类别不同而不同。
RegisterClientDriverID:
生成HKEY_LOCAL_MACHINE\Drivers\USB\ClientDrivers\Video_Class
RegisterClientSettings:
生成[HKEY_LOCAL_MACHINE\Drivers\USB\LoadClients\GroupID1\GroupID2\GroupID3\Video_Class]
"DLL"="camera.dll"
最后在USBDeviceAttach()调用ActivateDevice()来加载该驱动程序。