WinUSB驱动

本文详细解析了如何使用Windows Installer制作Windows USB驱动,包括注册表配置、制造商与模型设置、安装流程、源媒体段落、字符串定义等关键步骤,并提供了驱动签名和测试方法。同时,解释了如何通过WDDK选择性挂起USB设备,以及如何测试自签名的驱动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


WinUSB驱动  


|字号 订阅

; XXX USB Devices Driver

; Copyright (c) 2009, XXX company.

 

[Version]

Signature = "$Windows NT$"

Class = USB

ClassGuid={36fc9e60-c465-11cf-8056-444553540000}

Provider = %ProviderName%

DriverPackageDisplayName = %PackageDisplayName%

;CatalogFile=wudf.cat

CatalogFile.NTx86  = senx86.cat

CatalogFile.NTIA64 = senia64.cat

CatalogFile.NTAMD64 = senamd64.cat

DriverVer=09/01/2009,1.0.0.0

 

; ================== Class section ==================

 [ClassInstall32]

Addreg=MyDeviceClassReg

 [MyDeviceClassReg]

HKR,,,0,%ClassName%

HKR,,Icon,,-1

 ; ========== Manufacturer/Models sections ===========

 [Manufacturer]

%ProviderName% = MyDevice_WinUSB,NTx86,NTamd64,NTia64

 [MyDevice_WinUSB.NTx86]

%USB\MyDevice.DeviceDesc% =USB_Install, USB\VID_XXXX&PID_0101

 [MyDevice_WinUSB.NTamd64]

%USB\MyDevice.DeviceDesc% =USB_Install, USB\VID_XXXX&PID_0101

 [MyDevice_WinUSB.NTia64]

%USB\MyDevice.DeviceDesc% =USB_Install, USB\VID_XXXX&PID_0101

; =================== Installation ===================

 ;[1]

[USB_Install]

Include=winusb.inf

Needs=WINUSB.NT

 ;[2]

[USB_Install.Services]

Include=winusb.inf

AddService=WinUSB,0x00000002,WinUSB_ServiceInstall

 ;[3]

[WinUSB_ServiceInstall]

DisplayName     = %WinUSB_SvcDesc%

ServiceType     = 1

StartType       = 3

ErrorControl    = 1

ServiceBinary   = %12%\WinUSB.sys

 ;[4]

[USB_Install.Wdf]

KmdfService=WINUSB, WinUsb_Install

 [WinUSB_Install]

KmdfLibraryVersion=1.9

 ;[5]

[USB_Install.HW]

AddReg=Dev_AddReg

 [Dev_AddReg]

HKR,,DeviceInterfaceGUIDs,0x10000,"{8EA450C9-DA57-4973-8BF8-B21D59312345}"

;HKR,,"SystemWakeEnabled",0x00010001,1

HKR,,"DeviceIdleEnabled",0x00010001,1

HKR,,"DeviceIdleIgnoreWakeEnable",0x00010001,1

HKR,,"UserSetDeviceIdleEnabled",0x00010001,1

HKR,,"DefaultIdleState",0x00010001,0x1

HKR,,"DefaultIdleTimeout",0x00010001,500

 

;[6]

[USB_Install.CoInstallers]

AddReg=CoInstallers_AddReg

CopyFiles=CoInstallers_CopyFiles

 [CoInstallers_AddReg]

HKR,,CoInstallers32,0x00010000,"WinUSBCoInstaller2.dll","WdfCoInstaller01009.dll,WdfCoInstaller"

 [CoInstallers_CopyFiles]

WdfCoInstaller01009.dll

WinUSBCoInstaller2.dll

 [DestinationDirs]

CoInstallers_CopyFiles=11

 ; ================= Source Media Section =====================

;[7]

 [SourceDisksNames]

1 = %DISK_NAME%,,,\x86

2 = %DISK_NAME%,,,\amd64

3 = %DISK_NAME%,,,\ia64

 [SourceDisksFiles.x86]

WdfCoInstaller01009.dll=1

WinUSBCoInstaller2.dll=1

 [SourceDisksFiles.amd64]

WdfCoInstaller01009.dll=2

WinUSBCoInstaller2.dll=2

 [SourceDisksFiles.ia64]

WdfCoInstaller01009.dll=3

WinUSBCoInstaller2.dll=3

; =================== Strings ===================

 [Strings]

ProviderName="xxx provider"

PackageDisplayName="XXX USB Devices Driver"

USB\MyDevice.DeviceDesc="XXX SUB Devices"

WinUSB_SvcDesc="XXX USB Service"

DISK_NAME=" XXX USB Device Install Disk"

ClassName="xxx USB Devices"

 

1.注册表查看USB设备的信息:

Computer -> HKEY_LOCAL_MACHINE -> SYSTEM -> Enum -> USB -> 对应的VID_XXXX&PID_YYYY

2.想了解USB Driver, WinUSB 等相关知识,可参考http://www.lvr.com/usbcode.htm 该网站有源代码程序。

3. Q: How a host application read or write to USB device using WinUSB driver?
A: 如果使用WinUSB + UMDF, 使用 DeviceIoControl api (import kernel32.dll参考 http://www.pinvoke.net/default.aspx/kernel32/DeviceIoControl.html ), 如果仅使用 WinUSB, 则使用winusb.dll,参考http://www.lvr.com/usbcode.htm, 下为使用winusb.dll的例子:

[DllImport("winusb.dll", SetLastError = true)]

internal static extern Boolean WinUsb_Initialize(SafeFileHandle

DeviceHandle, ref IntPtr InterfaceHandle);

另注意HKR,,DeviceInterfaceGUIDs,0x10000,"{8EA450C9-DA57-4973-8BF8-B21D593A9116}"中的GUID,可以在host application中用来查找设备。这个GUID是由驱动开发者自定义的,如下所示,在可以在每个驱动版本中使用不同的GUID,以便区分。

// Guid for driver 5.0

DEFINE_GUID(GUID_INTERFACE_CP210x_50,

0xa2a39220, 0x39f4, 0x4b88, 0xae, 0xcb, 0x3d, 0x86, 0xa3, 0x5d, 0xc7, 0x48);

// Guid for driver 4.20 through 4.40

DEFINE_GUID(GUID_INTERFACE_CP210x_420_440,

0x993f7832, 0x6e2d, 0x4a0f, 0xb2, 0x72, 0xe2, 0xc7, 0x8e, 0x74, 0xf9, 0x3e);

// Guid for pre-4.20 driver, uncommon

DEFINE_GUID(GUID_INTERFACE_CP210x_410,

0xE4768D57, 0x352E, 0x4495, 0x88, 0x0C, 0x05, 0x6A, 0x7C, 0xB3, 0x46, 0x11);

// Guid for CP210x device programmed as USBXpress devices (uses USBXpress driver)

DEFINE_GUID(GUID_INTERFACE_USBXPRESS,

0x3c5e1462, 0x5695, 0x4e18, 0x87, 0x6b, 0xf3, 0xf3, 0xd0, 0x8a, 0xaf, 0x18);

4.Q: Selective suspend USB, 选择性挂起USB设备不工作?

首先确保使用的是WDDK 7600.16385.0,旧版本的WDDKselective suspend USB功能不工作。然后参考如下说明,有些USB hub并不支持selective suspend USB.

I have two PC.
One PC running Windows XP bought in 2006
The other one running Vista bought in 2008.
I install WinUSB (latest version from WinDDK 7600.16385.0) as my USB device 
driver in both PC. The idle timeout is 100 ms.
 
I plug in my USB device to Vista PC, it can selective suspend successfully, 
and read or write data is fine.
I plug in my USB device to XP PC, it cann't selective suspend, but read or 
write data is fine. If I disable the usb hub in Device Manager, it can 
suspend successfully (I think this is not selective suspend, this is global 
suspend). 
I plug in a high speed usb hub to XP PC, and then plug in my usb device to 
the high speed usb hub, it can selective suspend successfully, and read or 
write data is fine.
 
Conclusion: Some usb hub does not support selective suspend. I couldn't find 
out what kind of USB hub does not support selective suspend yet. But I would 
like to share what I already know.

 

5.Q: 如何签名 WinUSB驱动?

There is a Microsoft
document that shows what to do.

http://www.microsoft.com/whdc/connec...usb_howto.mspx

Basically, you have to write an .inf file for your device (N.B. there is an
error in the example .inf, [SourceDisksFiles.NTamd64] should read
[SourceDisksFiles.amd64])
Next, make a .cat file with the inf2cat utility from the DDK. (the document
says it is not in the DDK, but it is!)
Then sign this .cat file with the right certificate and include it in your
package.
There is a list with CA's from Microsoft for this purpose, with certificates
that ultimately link to the Microsoft CA. 
http://msdn.microsoft.com/en-us/library/ms995347.aspx
Comodo is not on the list, I chose the cheapest one, which was GlobalSign.

Afterwards, I found out that a fresh Vista x64 install from MSDN does not
contain the root GlobalSign certificate (only the one from Verisign, which
is the most expensive CA).
First I tried to install the GlobalSign certificate myself with a program,
but this would generate another warning, which defeats the idea of signing
to stop nasty warnings. I fixed this by running 'rootsupd.exe' from MS
Update. This works without warnings, but will sometimes trigger a UAC
warning in Vista. All in all, the system is not really waterproof in my
opinion. MS should of course include the root certificates that they advise
to use in every new installation, not just after running the right update!

 

 

检查*.inf文件语法: 新建CheckInf.bat文件,内容如下:

cd /d C:\WinDDK\7600.16385.0\tools\Chkinf
chkinf D:\Projects\Release\Driver\Driver_v1.0.1\Driver\OSWinUSB.inf /L D:\Projects\Release\Driver\log.txt /b

创建cat文件:新建Inf2Cat.bat,内容如下:

 cd /d C:\WinDDK\7600.16385.0\bin\selfsign
Inf2Cat /driver:D:\Projects\Release\Driver\Driver_v1.0.1\Driver /os:XP_X86,Vista_X86,7_X86
Inf2Cat /driver:D:\Projects\Release\Driver\Driver_v1.0.1\Driver /os:XP_X64,Vista_X64,7_X64

关于如何测试自签名的驱动请参考:

http://www.microsoft.com/whdc/driver/install/drvsign/kmcs-walkthrough.mspx

或者安装WinDDK后,搜索selfsign_example.cmd

### WinUSB 驱动开发指南及资源 WinUSB 是 Windows 提供的一种通用驱动程序,用于支持自定义 USB 设备。它允许开发者直接与 USB 设备进行通信,而无需编写复杂的内核模式驱动程序[^5]。以下是一些关于 WinUSB 驱动开发的关键信息和教程: #### 1. WinUSB 驱动的基本概念 WinUSB 是一种用户模式驱动程序,适用于需要自定义功能的 USB 设备。它通过简化设备驱动程序的开发流程,使开发者能够专注于应用程序逻辑而非底层硬件交互[^6]。WinUSB 支持以下特性: - 支持所有版本的 Windows 操作系统(从 Windows Vista 开始)。 - 提供对控制传输、批量传输和中断传输的支持。 - 允许开发者通过 API 调用直接与设备通信。 #### 2. WinUSB 驱动的安装步骤 在开发 WinUSB 驱动时,通常需要完成以下任务: - **设备选择**:确保设备支持 WinUSB 驱动,并且其描述符中包含正确的类代码(如 Vendor-Specific Class)[^7]。 - **驱动安装**:使用 INF 文件将设备绑定到 WinUSB 驱动程序。INF 文件定义了设备与驱动程序之间的映射关系。 - **测试验证**:通过工具(如 Device Manager 和 USBView)验证设备是否正确加载 WinUSB 驱动程序。 #### 3. WinUSB 驱动开发教程 以下是开发 WinUSB 驱动的一个简要教程: ```c #include <windows.h> #include <winusb.h> // 初始化 WinUSB WINUSB_INTERFACE_HANDLE winusb_handle; BOOL result = WinUsb_Initialize(device_handle, &winusb_handle); // 发送控制传输 WINUSB_SETUP_PACKET setup_packet = {0}; setup_packet.RequestType = 0x40; // 标准请求类型 setup_packet.Request = 0x01; // 请求码 setup_packet.Value = 0x0001; // 值 setup_packet.Index = 0x0000; // 索引 setup_packet.Length = 8; // 数据长度 BYTE buffer[8] = {0}; ULONG bytes_transferred = 0; result = WinUsb_ControlTransfer(winusb_handle, setup_packet, buffer, sizeof(buffer), &bytes_transferred, NULL); // 关闭 WinUSB 句柄 WinUsb_Free(winusb_handle); ``` #### 4. WinUSB 驱动的下载资源 Microsoft 提供了官方的 WinUSB 驱动程序和开发工具,开发者可以从以下链接下载相关资源: - 官方文档:[WinUSB Functions](https://learn.microsoft.com/en-us/windows/win32/api/_usb/) - 示例代码:[WinUSB Sample Code](https://github.com/microsoft/Windows-driver-samples/tree/main/usb) - 工具下载:[Windows Driver Kit (WDK)](https://developer.microsoft.com/en-us/windows/hardware/download-wdk) #### 5. 注意事项 - 在开发 WinUSB 驱动时,请确保设备的描述符符合 USB 规范,并正确配置设备类代码[^8]。 - 如果设备需要特定的功能(如视频流或音频处理),可能需要额外的驱动程序支持。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值