windows 插入新的 USB 设备时会发生什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3013142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:37:41  来源:igfitidea点击:

what happens when you plug in a new USB device?

windowsmacosusbdevice-driver

提问by Will

I have an embedded device with a USB connection. When the user plugs the device into their PC (Windows, OSX), how does the operating system discover what drivers to install? How do I get mydrivers to be selected? Can they reside on some central server (run by the OS vendor)?

我有一个带有 USB 连接的嵌入式设备。当用户将设备插入他们的 PC(Windows、OSX)时,操作系统如何发现要安装哪些驱动程序?如何让我的司机被选中?它们可以驻留在某个中央服务器上(由操作系统供应商运行)吗?

回答by JeremyP

For OS X:

对于 OS X:

Well I imagine the first part is similar to Windows. However, the driver (or rather the driver stack) is selected by a process known as driver matching. Each driver comes bundled with a special XML dictionary file which describes the device(s) that the driver is for.

好吧,我想第一部分类似于 Windows。但是,驱动程序(或者更确切地说是驱动程序堆栈)是由称为驱动程序匹配的过程选择的。每个驱动程序都捆绑了一个特殊的 XML 字典文件,该文件描述了驱动程序所适用的设备。

One of the keys in the dictionary is the IOProviderClass key which tells the operating system roughly what family of devices the driver is for. Drivers that have been loaded can provide "nubs" which are interfaces that other drivers can attach to. Which drivers try to attach to nubs is determined by the IOProviderClass key.

字典中的键之一是 IOProviderClass 键,它大致告诉操作系统驱动程序适用于哪些设备系列。已加载的驱动程序可以提供“小块”,这是其他驱动程序可以附加到的接口。哪些驱动程序尝试附加到 nubs 由 IOProviderClass 键决定。

So, for instance, when you plug a USB drive into your Mac, the already loaded USB controller driver detects this and provides a nub for the device. A low level USB driver is selected by driver matching - checking the key/values in the dictionary against values obtained from the device e.g. vendor id, device type etc and a score is derived for each driver of the right IO provider class. The one with the highest score is attached to the nub.

因此,例如,当您将 USB 驱动器插入 Mac 时,已加载的 USB 控制器驱动程序会检测到这一点并为设备提供一个小块。通过驱动程序匹配选择低级 USB 驱动程序 - 根据从设备获得的值(例如供应商 ID、设备类型等)检查字典中的键/值,并为正确 IO 提供程序类的每个驱动程序派生一个分数。得分最高的那个附在小块上。

This new driver might itself provide nubs for other drivers to attach to. For example, connecting a USB disk drive will cause the USB stack to create a nub for a SCSI block device driver to attach to. Normally, that will cause Apple's standard SCSI block device driver to be loaded which will in turn provide nubs for BSD drivers (one per partition) which in turn create the BSD device node in the dev file system.

这个新驱动程序本身可能会为其他驱动程序提供连接。例如,连接 USB 磁盘驱动器将导致 USB 堆栈创建一个用于连接 SCSI 块设备驱动程序的结点。通常,这会导致加载 Apple 的标准 SCSI 块设备驱动程序,这反过来会为 BSD 驱动程序提供 nubs(每个分区一个),然后在 dev 文件系统中创建 BSD 设备节点。

If you want your driver selected above others, all you need to do is add key value pairs for the device your driver is for which cause your driver to get a really high score. Usually it's enough to just put keys in for your vendor id/model. However, I think you can override the matching method (device drivers are written in a restricted set of C++) to give your driver a really high score.

如果您希望您的驱动程序在其他驱动程序之上被选中,您需要做的就是为您的驱动程序所针对的设备添加键值对,从而使您的驱动程序获得非常高的分数。通常只需为您的供应商 ID/型号输入密钥就足够了。但是,我认为您可以覆盖匹配方法(设备驱动程序是用一组受限制的 C++ 编写的)来给您的驱动程序一个非常高的分数。

That's a high level view. However, it is a number of years since I did any of this, so make sure you read the current Apple docs.

这是一个高层次的观点。然而,我已经很多年没有这样做了所以一定要阅读当前的 Apple 文档

回答by Christopher

This is for Windows :

这适用于 Windows:

When a USB device is plugged into the system, the USB bus driver is notified. The bus driver sends a standard USB request (USB_DEVICE_DESCRIPTOR) to the device. With this the device reports its name and type. (bDeviceClass/bDeviceSubClass/bDeviceProtocol).

当 USB 设备插入系统时,会通知 USB 总线驱动程序。总线驱动程序向设备发送标准 USB 请求 (USB_DEVICE_DESCRIPTOR)。通过此设备报告其名称和类型。(bDeviceClass/bDeviceSubClass/bDeviceProtocol)。

With this information, Windows creates an device entry in the system. This the Hardware-ID. The system now tries to find either a generic driver which can handle the device (HID/UVC) or a driver which specifically registered itself to support this Hardware-ID.

使用此信息,Windows 在系统中创建一个设备条目。这是硬件 ID。系统现在尝试找到可以处理设备 (HID/UVC) 的通用驱动程序或专门注册自身以支持此硬件 ID 的驱动程序。

To register a driver as the handler for a specific device, you have to install the driver into the system, or you must supply Microsoft with one, which they can provide on their servers.

要将驱动程序注册为特定设备的处理程序,您必须将该驱动程序安装到系统中,或者您必须向 Microsoft 提供一个驱动程序,他们可以在其服务器上提供该驱动程序。