USB Complete
Everything You Need
to Develop Custom USB Peripherals
Third Edition
Jan Axelson
Lakeview Research LLC
Madison, WI 53704
USB Complete: Everything You Need to Develop USB Peripherals, Third Edition
by Jan Axelson
Copyright 1999-2005 by Janet L. Axelson
All rights reserved. No part of the contents of this book, except the program code, may
be reproduced or transmitted in any form or by any means without the written permis-
sion of the publisher. The program code may be stored and executed in a computer sys-
tem and may be incorporated into computer programs developed by the reader.
The information, computer programs, schematic diagrams, documentation, and other
material in this book are provided “as is,” without warranty of any kind, expressed or
implied, including without limitation any warranty concerning the accuracy, adequacy,
or completeness of the material or the results obtained from using the material. Neither
the publisher nor the author shall be responsible for any claims attributable to errors,
omissions, or other inaccuracies in the material in this book. In no event shall the pub-
lisher or author be liable for direct, indirect, special, incidental, or consequential dam-
ages in connection with, or arising out of, the construction, performance, or other use of
the materials contained herein.
Many of the products and company names mentioned herein are the trademarks of their
respective holders. PICMicro is a registered trademark of Microchip Technology Inc. in
the U.S.A. and other countries.
Published by Lakeview Research LLC, 5310 Chinook Ln., Madison WI 53704
On the web at www.Lvr.com
Distributed by Independent Publishers Group (www.ipgbook.com).
Cover by Rattray Design. Cover Photo by Bill Bilsley Photography.
Developing a Device 29
Elements in the Link 29
Tools for Developing 30
Steps in Developing a Project 30
2. Inside USB Transfers 33
Transfer Basics 34
Enumeration Communications 34
Application Communications 34
Managing Data on the Bus 35
Host Speed and Bus Speed 36
Elements of a Transfer 37
Device Endpoints: the Source and Sink of Data 38
Pipes: Connecting Endpoints to the Host 40
Types of Transfers 40
Stream and Message Pipes 42
Initiating a Transfer 43
Transactions: the Building Blocks of a Transfer 44
Transaction Phases 45
Ensuring that Transfers Are Successful 51
Handshaking 51
Reporting the Status of Control Transfers 55
Error Checking 56
3. A Transfer Type for Every Purpose 61
Control Transfers 61
Availability 62
Structure 62
Data Size 66
Speed 66
Detecting and Handling Errors 68
Bulk Transfers 68
Descriptors 93
Types of Descriptors 94
Device Descriptor 96
Device_qualifier Descriptor 99
Configuration Descriptor 101
Other_speed_configuration Descriptor 103
Interface Association Descriptor 103
Interface Descriptor 106
Endpoint Descriptor 108
String Descriptor 112
Other Standard Descriptors 113
The Microsoft OS Descriptor 114
Descriptors in 2.0-compliant Devices 114
Making 1.x Descriptors 2.0-compliant 114
Detecting the Speed of a Dual-Speed Device 116
Contents
vi
5. Control Transfers:
Structured Requests for Critical Data 117
Elements of a Control Transfer 117
Setup Stage 118
Data Stage 120
Status Stage 122
Handling Errors 124
Device Firmware 125
The Requests 127
Get_Status 129
Clear_Feature 130
Set_Feature 131
Set_Address 132
Philips Semiconductors ISP1581 168
PLX Technology NET2272 169
FTDI Chip FT232BM and FT245BM 170
7. Device Classes 177
About Classes 177
Device Working Groups 178
Elements of a Class Specification 178
Defined Classes 181
Audio 181
Chip/Smart Card Interface 189
Communication Devices: Modems and Networks 191
Content Security 198
Device Firmware Upgrade 200
Human Interface 203
IrDA Bridge 206
Mass Storage 208
Printers 213
Still Image Capture: Cameras and Scanners 217
Test and Measurement 220
Video 221
Implementing Non-standard Functions 226
Standard or Custom Driver? 226
Converting from RS-232 227
Converting from the Parallel Port 229
PC-to-PC Communications 229
Using a Generic Driver 231
8. How the Host Communicates 233
Device Driver Basics 233
Insulating Applications from the Details 234
Options for USB Devices 235
What the User Sees 279
10. Detecting Devices 281
A Brief Guide to Calling API Functions 281
Managed and Unmanaged Code 282
Documentation 284
Using Visual C++ .NET 284
Using Visual Basic .NET 286
Finding Your Device 291
Obtaining the Device Interface GUID 292
Requesting a Pointer to a Device Information Set 293
Identifying a Device Interface 295
Requesting a Structure Containing the Device Path Name 298
Extracting the Device Path Name 301
Closing Communications 302
Contents
ix
Obtaining a Handle 303
Requesting a Communications Handle 303
Closing the Handle 306
Detecting Attachment and Removal 306
About Device Notifications 307
Registering for Device Notifications 307
Capturing Device Change Messages 311
Reading Device Change Messages 312
Retrieving the Device Path Name in the Message 314
Stopping Device Notifications 317
11. Human Interface Devices:
Using Control and Interrupt Transfers 319
What is a HID? 320
Hardware Requirements 321
Describing the Data’s Use 363
Converting Units 365
Converting Raw Data 366
Describing the Data’s Size and Format 369
Saving and Restoring Global Items 369
The Local Item Type 370
Physical Descriptors 373
Padding 373
13. Human Interface Devices: Host Application 375
HID API Functions 375
Requesting Information about the HID 376
Sending and Receiving Reports 376
Providing and Using Report Data 378
Managing HID Communications 379
Identifying a Device 379
Reading the Vendor and Product IDs 380
Getting a Pointer to a Buffer with Device Capabilities 384
Getting the Device’s Capabilities 385
Getting the Capabilities of the Buttons and Values 388
Sending and Receiving Reports 388
Sending an Output Report to the Device 389
Reading an Input Report from the Device 392
Writing a Feature Report to the Device 402
Reading a Feature Report from a Device 404
Closing Communications 406
14. Bulk Transfers for Any CPU 407
Two Projects 407
Asynchronous Serial Interface 408
Parallel Interface 414
Host Programming 421
Power Sources 461
Over-current Protection 462
Power Switching 463
Saving Power 464
Global and Selective Suspends 464
Current Limits for Suspended Devices 464
Resuming Communications 466
Power Management under Windows 467
17. Testing and Debugging 471
Tools 471
Hardware Protocol Analyzers 472
Software Protocol Analyzers 475
Traffic Generators 477
Contents
xii
Testing 477
Compliance Testing 478
WHQL Testing 484
18. Signals and Encoding 489
Bus States 489
Low-speed and Full-speed Bus States 490
High-speed Bus States 492
Data Encoding 494
Staying Synchronized 496
Timing Accuracy 498
Packet Format 499
Fields 499
Inter-packet Delay 501
Test Modes 502
Entering and Exiting Test Modes 502
Requirements for an OTG Device 538
The OTG Descriptor 545
Feature Codes for HNP 545
OTG Controller Chips 545
Philips ISP1362 546
TransDimension TD242LP 547
Cypress CY7C67200 EZ-OTG 548
Philips ISP1261 Bridge Controller 549
Index 551
Contents
xiv
Introduction
USB Complete xv
Introduction
This book is for developers who design and program devices that use the
Universal Serial Bus (USB) interface. My goal is to introduce you to USB
and to help you get your devices up and communicating as quickly and eas-
ily as possible.
The USB interface is versatile enough for a wide range of peripheral devices.
Standard peripherals that use USB include mice, keyboards, drives, printers,
and audio/video devices. USB is also suitable for data-acquisition units, con-
trol systems, and other devices with specialized functions, including
one-of-a-kind designs.
To develop a device with a USB interface, you need to know something
about how the interface works, what tasks your device firmware must per-
form to communicate on the bus, and what class drivers and other support
are available on the host computers that your device will attach to. The right
choices of device hardware, device class, and development tools and tech-
Introduction
xvi USB Complete
custom driver, you’ll learn what’s involved in writing a driver, what tools
Introduction
USB Complete xvii
can help speed up the process, and options for obtaining drivers from
other sources. Example code shows how to detect and communicate with
devices in Visual Basic .NET and Visual C++ .NET applications.
• What firmware does my device need to support USB communications? Learn
how to write device firmware that enables your device to respond to
received requests and exchange other data on the bus.
• How do I decide whether my device can use bus power or needs its own sup-
ply? Many USB devices can be powered entirely from the bus. Find out
whether your device can use bus power. Learn how to ensure that your
device meets USB’s requirement to limit the use of bus current when the
host computer suspends the bus.
• Can I connect other USB peripherals to my device? Find out how to use
USB On-The-Go to enable your device to act as a limited-capability host
that can access other USB peripherals.
• How can I ensure that my device will communicate without problems? At the
device, writing bugfree firmware requires understanding what your
device must do to meet the requirements of the USB specifications. At
the host computer, Windows must have the information needed to iden-
tify the device and locate a driver to communicate with the device. This
book has tips, example code, and information about debugging software
and hardware to help with these tasks.
To understand the material in the book, it’s helpful to have basic knowledge
in a few areas. I assume you have some experience with digital logic, applica-
tion programming for PCs and writing embedded code for peripherals. You
don’t have to know anything at all about USB.
What’s New in the Third Edition?
Since the publication of USB Complete Second Edition, much has happened
of the USB-IF’s Audio Device Working Group, Walter Oney of Walter
Oney Software, and Marc Reinig of System Solutions.
Introduction
USB Complete xix
Others I want to thank for their support are Glenn M. Roberts of Cypress
Semiconductor, Fred Dart and Keith Dingwall of FTDI Chip, Wendy Dee
of Keil Software, Michael DeVault of DeVaSys Embedded Systems, Alan
Lowne of Saelig Company Inc., Laurent Guinnard of Ellisys, Rich Moran of
RPM Systems Corporation, and Bob Nathan of NCR Corporation.
For help and support with the previous editions that this edition builds on,
thanks to Joshua Buergel, Gary Crowell, Dave Dowler, Mike Fahrion, John
M. Goodman, Lane Hauck, David James, Christer Johansson, Kosta
Koeman, Jon Lueker, Brad Markisohn, Amar Rajan, Robert Severson, Craig
R. Smith, and Dave Wright.
I hope you find the book useful. Comments invited!
Jan Axelson
Introduction
xx USB Complete
USB Basics
USB Complete 1
1
USB Basics
What if you had the chance to design a peripheral interface from scratch?
Your wish list would likely include these qualities:
• Easy to use, so there’s no need to fiddle with configuration and setup
details.
• Fast, so the interface doesn’t become a communications bottleneck.
• Reliable, so that errors are rare, with automatic retries when errors occur.
• Versatile, so many kinds of peripherals can use the interface.
Ease of Use
Ease of use was a major design goal for USB, and the result is an interface
that’s a pleasure to use for many reasons:
One interface for many devices. USB is versatile enough to be usable with
a variety of peripheral types. Instead of having a different connector type
and supporting hardware for each peripheral, one interface serves many.
USB Basics
USB Complete 3
Table 1-1: Comparison of popular computer interfaces. Where a standard
doesn’t specify a maximum, the table shows a typical maximum.
Interface Format Number of
Devices
(maximum)
Distance
(maximum,
feet)
Speed
(maximum,
bits/sec.)
Typical Use
USB asynchronous
serial
127 16 (up to 96
ft. with 5
hubs)
1.5M, 12M,
480M
Mouse,
keyboard, drive,
audio, printer,
flow-through
mode)
50 31.5k Music, show
control
Parallel Printer
Port
parallel 2 (8 with
daisy-chain
support)
10–30 8M Printers,
scanners, disk
drives
RS-232
(EIA/TIA-232)
asynchronous
serial
2 50-100 20k (115k
with some
hardware)
Modem, mouse,
instrumentation
RS-485
(TIA/EIA-485)
asynchronous
serial
32 unit loads
(up to 256
devices with
some
hardware)
set or configuration utilities to run.
Frees hardware resources for other devices. Using USB for as many
peripherals as possible frees up IRQ lines for the peripherals that require
them. The PC dedicates a series of port addresses and one IRQ line to the
USB host controller, but individual peripherals don’t require additional
resources or any PC programming that involves specifying port addresses or
detecting hardware interrupts. In contrast, peripherals with other interfaces
may require dedicated port addresses, an IRQ line, and an expansion slot.
No power supply required (sometimes). The USB interface includes
power-supply and ground lines that provide a nominal +5V from the com-
puter’s or hub’s power supply. A peripheral that requires up to 500 milliam-
peres can draw all of its power from the bus instead of having to provide a