创建虚拟USB设备 [英] Creating a Virtual USB Device

查看:192
本文介绍了创建虚拟USB设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,正在学习如何为USB设备编写WDM设备驱动程序,并且发现可用的资料太难理解了(DDK在线文档是最难阅读的文档,而WDM设备驱动程序的作者是没什么比这更好的了。)

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).

所以,我有一个简单的问题。如果要创建虚拟USB设备(例如,看起来像连接到USB端口的真实USB鼠标的虚拟USB鼠标)来进行测试/学习,该从哪里开始。

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.

到目前为止,我了解的是HIDClass驱动程序(hidclass.sys)具有用于USB总线(hidusb.sys)的微型驱动程序,该驱动程序执行所连接的USB硬件的枚举。因此,如果我想劫持硬件枚举过程并创建自己的虚拟硬件,我是否应该在某处包含过滤器驱动程序以拦截一些与硬件枚举过程相关的IRP?

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?

很抱歉,如果上述内容根本没有意义,因为我仍处于学习阶段,这实际上是我可以帮助我更好地编写USB设备驱动程序的一项练习。

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.

推荐答案

Windows使用即插即用体系结构。
插入USB设备时,它将向设备发送低级USB请求,然后根据设备的响应来确定要加载的驱动程序。通过将供应商ID,产品ID等与inf文件部分进行比较来进行匹配。驱动程序以带有xxx.inf文件的已编译xxx.sys形式出现,并已加载到内核空间。 Windows根据设备驱动程序随附的* .inf文件来确定要加载的xxx.sys。

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:

[Manufacturer]
%Manufacturer% = DeviceInstall

[DeviceInstall]
"some usb dev"=OTHER_SECTION_DEV, USB\Vid_XXXX&Pid_yyyy

# This is where windows learns to match this information
# to your device, using the product id (Pid) and the 
# vendor id (Vid) that Windows gets back during the
# low level USB DeviceDescriptor request

[OTHER_SECTION_DEV]
CopyFiles = xxx.sys, 10,system32\drivers

(有关 inf 文件中内容的详细说明可以在 https:// docs上找到。 microsoft.com/zh-CN/windows-hardware/drivers/install/inf-manufacturer-section

(a more detailed description on what's in inf files can be found over on https://docs.microsoft.com/en-us/windows-hardware/drivers/install/inf-manufacturer-section)

详细查看USB枚举过程(使用USB Logger):

A detailed look at the USB enumeration process (Use USB Logger):


  • 已插入USB设备

  • USB总线驱动程序请求


    • GetDescriptor(设备)

    • GetDescriptor(配置)

    • GetDescriptor(String iSerialNumber),用作设备实例ID

    • GetDescriptor(String iProduct),用于已识别新硬件弹出窗口

    • USB Device Plugged
    • USB Bus Driver Request
      • GetDescriptor(Device)
      • GetDescriptor(Configuration)
      • GetDescriptor(String iSerialNumber), used as Device Instance ID
      • GetDescriptor(String iProduct), used in the "new Hardware been identified" popups

      • 设备ID字符串,代表USB供应商和产品ID,

      • 硬件ID字符串,

      • CompatibleIDs字符串,表示USB设备的接口类,子类和协议,

      • InstanceID字符串,表示具有相同实例的所有实例集合中此特定设备的uid兼容的ID连接到计算机上。

      • DeviceID string, representing the USB Vendor and Product ID,
      • HardwareIDs string,
      • CompatibleIDs string, representing USB device' Interface Class, Subclass and Protocol, and
      • InstanceID string, representing the uid for this particular device within the set of all instances with the same compatible id hooked up to the computer.

      对于任何已连接的USB设备,您都可以使用设备管理器查看以下字符串:

      For any connected USB device you can see these strings using the Device Manager:


      • 打开设备管理器(Windows菜单->设备管理器,或控制面板->系统->硬件->设备管理器)

      • 然后使用视图菜单切换到设备通过连接

      • 打开 ACPI [...]-> PCI总线 / PCI Express根联合体-> [[]] USB [...]主控制器

      • 展开主机控制器下的任何条目,对于列出的任何设备,右键单击以获取其属性,打开详细信息选项卡,然后使用属性下拉菜单找到硬件ID,兼容ID ,设备实例ID,匹配设备ID,服务等。

      • Open the Device Manager (windows menu -> "device manager", or control panel -> "System" -> "Hardware" -> "Device Manager")
      • then use the "view" menu to switch to "Device by Connection"
      • open "ACPI [...]" -> "PCI bus"/"PCI Express Root Complex" -> "[...] USB [...] Host Controller"
      • expand any of the entries under the host controller, and for any of the devices listed, right click to get their properties, open the "details" tab, and then use the property pulldown menu to find "Hardware Ids", "Compatible Ids", "Device Instance ID", "Matching Device Id", "Service", etc.

      例如,我有一个USB连接了 Device Id = usb\class_08& subclass_06& prot_50 的存储设备,此字符串可以与 .inf 文件,该文件在第一次枚举后添加到已知设备的列表中。该文件的字符串为 Service = USBSTOR ,因此我们知道 usbstor.sys 用于与此USB接口大容量存储设备。

      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 string Service = USBSTOR, and so we know that usbstor.sys is used to interface with this USB Mass Storage Device.

      让我们继续进行匹配过程。

      Let's continue with matching process.


      • PNP管理器尝试确定设备是否已经安装:


        • 它在注册表中搜索与 DeviceInstance ID匹配的项,以查看哪个服务可以与此接口交互设备。具体来说,它将在 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB

        • The PNP Manager tries to determine whether Device was already "installed":
          • It search the registry for a key matching the "DeviceInstance ID" to see which service handles interfacing with this device. Specifically, it searches for this in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB

          对于键上的磁盘,您可以看到类似以下内容的东西:

          For disk on key, you can see something like:

          [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\Vid_0781&Pid_5406\0775555ACA54ADE3]
          "Service"="USBSTOR"
          




          • 然后,PNP管理器根据PNP请求中的字符串与.inf数据库中的数据之间的匹配来加载关联的驱动程序: b
            $ b

            • inf数据库位于:C:\WINDOWS\inf\

            • drivers .sys文件位于:C :\WINDOWS\system32\drivers

            • 对于编写驱动程序,我的建议是:

              For writing drivers my advice is:


              1. 不要从实现HID(人机接口设备)设备入手,因为这可能会导致Windows使用鼠标或键盘而不是原始驱动程序使用自定义驱动程序,这将禁用鼠标或键盘,非常危险。

              2. 不要将驱动程序加载到开发计算机中:

              1. Don't start with implementing HID (human interface device) devices, because you can cause windows to use your custom driver for you mouse or keyboard instead of original driver, this will disable your mouse or keyboard, very dangerous.
              2. Don't load drivers into your dev machine:

              1. 使用虚拟机并在其中安装驱动程序。为您的虚拟机设置内核调试器: http://www.codeproject.com/KB /winsdk/KernelModeDebuggerSetup.asp

              2. 或在其他测试计算机上加载驱动程序。


            • USB驱动程序的良好学习平台是 OSR USB-FX2学习工具包

            • 这篇关于创建虚拟USB设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆