Programming the Microsoft Windows Driver Model
Chapter 12
The Universal Serial Bus
End user convenience is the keynote of the universal serial bus (USB). The Plug and Play (PnP) concept has simplified the process of installing certain types of hardware on existing PCs. However, configuration issues continue to plague end users with respect to legacy devices such as serial and parallel ports, keyboards, and mice. Port availability is one of the factors that have historically limited proliferation of peripherals, including modems, answering machines, scanners, and personal digital assistants. USB helps solve these problems by providing a uniform method of connecting a potentially large number of self-identifying devices through a single PC port.
Although this book concerns software, a discussion of some of the electrical and mechanical aspects of USB is in order because they re important to software developers. From the end user s point of view, USB s main feature is the use by every device of an identical 4-conductor wire with a standardized plug that fits into a socket on the back of the PC or on a hub device plugged in to the PC. Furthermore, you can attach or remove USB devices at will without explicitly opening or closing the applications that use them and without worrying about electrical damage.
This chapter covers two broad topics. In the first part of the chapter, I ll describe the programming architecture of USB. This architecture encompasses several ideas, including a hierarchical method for attaching devices to a computer, a generic scheme for power management, and a standard for self-identification that relies on a hierarchy of descriptors on board the hardware. The USB architecture also employs a scheme for subdividing fixed-duration frames and microframes into packets that convey data to and from devices. Finally, USB allows for four different ways of transporting data between the host computer and endpoints on devices. One method, named isochronous, permits a fixed amount of data to be moved without error correction at regular intervals possibly as frequently as three times in every microframe. The other methods, named control, bulk, and interrupt, allow data to be transmitted with automatic error correction.
In the second part of this chapter, I ll describe the additional features of a Windows Driver Model driver for a USB device over and above the features you already know about. Rather than communicate directly with hardware by using hardware abstraction layer (HAL) function calls, a USB driver relies heavily on the bus driver and a kernel-mode library named USBD.SYS. To send a request to its device, the driver creates a USB request block (URB), which it submits to the bus driver. Configuring a USB device, for example, requires the driver to submit several URBs for reading descriptors and sending commands. The bus driver in turn schedules requests onto the bus according to demand and available bandwidth.
The ultimate source for information about USB is the official specification, which was at revision level 2.0 when this book went to press. The specification and various other documents produced by the USB committee and its working groups were available on line at http://www.usb.org/developers/.
Note on Sample Programs
Anchor Chips, Incorporated, kindly provided me one of their EZ-USB development kits that I used to develop the sample drivers for the first edition. Anchor Chips was subsequently acquired by Cypress Semiconductor (www.cypress.com). The Cypress Semiconductor USB chip set revolves around a modified 8051 microprocessor and additional core logic to perform some of the low-level protocol functions mandated by the USB specification. The development board also contains additional external memory, a UART and serial connector, a set of push buttons, and an LED readout to facilitate development and debugging of 8051 firmware using Cypress Semiconductor s software framework. One of the key features of the Cypress Semiconductor chip set is that you can download firmware over the USB connection easily. For a programmer like me with a phobia for hardware in general and EEPROM programming in particular, that feature is a godsend.The USB sample drivers in the companion content illustrate the simplest possible USB devices and stand alone as examples of how to perform various tasks. If you happen to have a Cypress Semiconductor development kit, however, you can also try out these samples with real firmware. Each sample contains a WDM driver in a SYS subdirectory, a MicrosoftWin32 test program in a TEST subdirectory, and a firmware program in an EZUSB directory. You can follow the directions in the HTM files included with each sample to build these components or simply to install the prebuilt versions that are in the companion content.