Sunday 8 October 2017

windows - How does USB device recognition work?


I'm curious how USB device recognition works in Windows. I imagine it's something like this:



  • When you plug in a device, it tells Windows "here's my device ID to tell you what I am"

  • Windows looks to see if any drivers have been installed that match that device ID. The driver probably tells Windows what the device should be called - like "BlackBerry Curve" or "Canon Printer"

  • If so, it somehow associates that device with that driver

  • Otherwise, it looks for a matching driver online (if you let it)


Am I right? If so, that still leaves some questions.



  • When you install drivers, where do they go? Are they files in a folder, or do they get added to the registry?

  • What is Windows doing when it first recognizes the device, thinks, and finally says "your new device is installed and ready to use?"

  • Where does Windows look for missing drivers? Is it in their own database? Do device manufacturers submit drivers to Microsoft for inclusion there?


Can anybody explain how this process really works? Also, do other OSes do this differently?



Answer



The USB bus is physically designed so that the act of inserting (and removing) a plug can be recognized by the host controller. When this "plug" event happens, the host controller informs its device driver which scans the bus and asks each device to identify itself.


All USB devices contain a collection of information about the device, called the descriptors. Device descriptors are retrieved from all devices with the same command. This allows a device driver for the USB bus itself to effectively ask a newly connected device what it is, and expect to get a reasonable response.


Of all the descriptors, only a few are directly used to match drivers to most devices. This is because USB defines classes of devices, and usually the system driver for each device class is sufficient to handle any devices that claim to be in that class.


All keyboards will claim to be in the HID (Human Interface) class, as will mice, tablets, and game controllers, for example. The HID class has several subclasses (keyboards, mice, etc.), so each gets handled in the expected way.


Most disk-like devices will claim to be in the Mass Storage class, and the system driver just works for those as well.


In addition to the class and subclass, the descriptors also include a vendor ID (VID), product ID (PID), and revision. The vendor IDs are assigned by the standards committee (mostly in order of issue but some companies got special requests: intel is 0x8086, for example). Product IDs are assigned by each vendor, and the combination of VID and PID must be unique to each released product.


When a device is first installed, the VID, PID, revision, class and subclass are used in a predictable way to choose the device driver that is loaded. Having vendor and product specific names makes it possible for a vendor to customize a device that might otherwise be handled (nearly) correctly by the stock system driver.


Another important descriptor is the device serial number. If a device has a serial number, then it can be recognized and treated the same when plugged in again even if a different physical USB port is used. This is important for storage devices so that they get the same drive letter assigned, and for devices like serial port adapters and modems so that they are given the same COM port designation.


This whole process is documented at MSDN, but the details are spread out among a variety of places.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...