创建虚拟USB设备
我是一个新手,学习如何为 USB 设备编写 WDM 设备驱动程序,发现现有的材料都太难理解(DDK 在线文档是最难阅读的文档之一,Oney 的 WDM 设备驱动程序书则不是)没有什么更好的了)。
所以,我有一个简单的问题。如果我想创建一个虚拟 USB 设备(例如,看起来像连接到 USB 端口的真实 USB 鼠标的虚拟 USB 鼠标)用于测试/学习,我应该从哪里开始。
到目前为止,我所了解的是 HIDClass 驱动程序 (hidclass.sys) 有一个用于 USB 总线 (hidusb.sys) 的微型驱动程序,用于执行所连接的 USB 硬件的枚举。那么,如果我想劫持硬件枚举过程并创建自己的虚拟硬件,我是否应该在某处包含一个过滤驱动程序来拦截一些与硬件枚举过程相关的IRP?
抱歉,如果上述内容完全没有意义,因为我仍处于学习阶段,这实际上是我认为可以帮助我更好地学习编写 USB 设备驱动程序的练习之一。
I am a newbie learning how to write WDM device drivers for USB devices and found that the materials available are all too hard to comprehend (the DDK online doc is one of the most difficult to read, and the WDM Device driver book by Oney isn't anything better).
So, I've got a simple question. Where do I start if I want to create a virtual USB device (for example, a virtual USB mouse which looks like a real USB mouse attached to a USB port) for testing/learning.
So far what I understand is the HIDClass driver (hidclass.sys) has a minidriver for the usb bus (hidusb.sys) that carries out the enumeration of attached USB hardware. So, if I want to hijack the hardware enumeration process and creates my own virtual hardware, should I include a filter driver somewhere to intercept some IRPs related to the hardware enumeration process?
Sorry if the above does not make sense at all since I am still in the learning stage and this is actually one of the exercise I think could help me learn about writing USB device drivers better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Windows 使用即插即用架构。
当您插入 USB 设备时,它会向设备发送低级 USB 请求,然后根据设备的响应决定要加载的驱动程序。匹配是通过将供应商 ID、产品 ID 等与 inf 文件部分进行比较来完成的。驱动程序以编译后的 xxx.sys 和 xxx.inf 文件的形式出现,并加载到内核空间。 Windows 根据设备驱动程序附带的 *.inf 文件来决定加载哪个 xxx.sys。
这些文件具有如下部分:(
有关
inf
文件内容的更详细说明可以在 https://learn.microsoft.com/en-us/windows-hardware/drivers/install/inf-manufacturer-section)详细查看USB枚举过程(使用USB记录器):
对于任何连接的 USB 设备,您可以使用设备管理器看到这些字符串:
例如,我有一个 USB 存储设备,其
Device Id = usb\class_08&subclass_06&prot_50
连接起来,并且该字符串可以与第一次枚举后添加到已知设备列表中的.inf
文件相匹配。该文件有一个字符串Service = USBSTOR
,因此我们知道usbstor.sys
用于与此 USB 大容量存储设备交互。让我们继续匹配过程。
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB
中搜索此内容
内容
对于编写驱动程序,我的建议是:
Windows uses a Plug and Play Architecture.
When you insert a USB device, It sends low level USB request to the device and then based on the response from a device decides what driver to load. Matching is done by comparing vendor id, product id and etc to inf files sections. Drivers come in the form of a compiled xxx.sys with xxx.inf file and is loaded to kernel space. Windows decides which xxx.sys to load based on the *.inf file that comes with the device's driver.
These files have sections like this:
(a more detailed description on what's in
inf
files can be found over on https://learn.microsoft.com/en-us/windows-hardware/drivers/install/inf-manufacturer-section)A detailed look at the USB enumeration process (Use USB Logger):
For any connected USB device you can see these strings using the Device Manager:
For example, I have a USB storage device with
Device Id = usb\class_08&subclass_06&prot_50
hooked up, and this string can be matched to an.inf
file that was added to the list of known devices after first enumeration. This file has a stringService = USBSTOR
, and so we know thatusbstor.sys
is used to interface with this USB Mass Storage Device.Let's continue with matching process.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB
For disk on key, you can see something like:
For writing drivers my advice is:
使用设备模拟框架 (DSF)。
http://msdn.microsoft.com/en-us/library /windows/hardware/gg454516.aspx
Use Device Simulation Framework (DSF).
http://msdn.microsoft.com/en-us/library/windows/hardware/gg454516.aspx
您可以使用 USB/IP 项目来模拟任何您想要的设备。在我的博客中,我演示了如何使用 USB/IP 项目在 python 中模拟 USB 鼠标设备:
http://break -the-system.blogspot.com/2014/08/emulated-usb-devices-in-python-with-no.html
它不会帮助您了解如何创建虚拟USB设备(该过程已完成在 USB/IP 驱动程序中,您可以读取代码),但它将创建虚拟 USB HID 设备,您可以使用发送到 USB 驱动程序的 HID 参数。
You can use the USB/IP project to emulate any device that you want. In my blog I demonstrated how to emulate USB Mouse device in python using the USB/IP project:
http://breaking-the-system.blogspot.com/2014/08/emulating-usb-devices-in-python-with-no.html
It wont help you to understand how to create the virtual USB device (the process is done in the USB/IP driver, you could read the code), but it will create the virtual USB HID device and you could play with the HID arguments sent to the USB driver.
提供您自己的总线类型和枚举器不是更有意义吗?
Wouldn't it make more sense to provide your own bus type and enumerator?