views:

109

answers:

2

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 my drivers to be selected? Can they reside on some central server (run by the OS vendor)?

+4  A: 

This is for 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).

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.

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.

Christopher
What general way you give a copy to Microsoft (as in what's their term for it called)?
Will
WHQL. And no, this is not the first step.
Christopher
Understood :) .
Will
+1  A: 

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

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.

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.

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.

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.

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.

JeremyP
Sorry I can't accept *two* answers
Will
@Will: no problem. I enjoyed reminiscing about my device driver days, anyway.
JeremyP