USB in a Nutshell
Table of Contents
Communication Principle
- Transfer ::= Transaction{1, n}
- Transaction ::= Packet{2, 3}
- Packet ::= Field{3, 6}
- Field ::= Bit{1, n}
- Packet ::= Field{3, 6}
- Transaction ::= Packet{2, 3}
- Field = {Sync, PID, ADDR, ENDP, Frame, DATA, CRC, EOP}
- Sync: synchronize
- 8 bits
- used to synchronize the clock of the receiver with that of the transmitter
- PID: packet ID
- 8 bits
- used to identify the type of packet that is being sent
- ADDR: device address
- 7 bits
- specifies which device the packet is designated for
- allows for 127 devices to be supported
- ENDP: an endpoint of a device
- 4 bits
- a source or sink of data
- allows 16 endpoints for a device
- Frame: frame number
- 11 bits
- DATA
- USB 2.0: 0~1024 bytes
- USB 1.1: 0~1023 bytes
- CRC: Cyclic Redundancy Checks
- All token packets have a 5 bit CRC while data packets have a 16 bit CRC.
- EOP: end of packet
- signalled by a Single Ended Zero (SE0) for approximately 2 bit times followed by a J for 1 bit time.
- Sync: synchronize
- Packet = {Token, Data, Handshake, Special}
- Token = {SETUP, IN, OUT, SOF}
- {SETUP, IN, OUT} ::= Sync PID ADDR ENDP CRC5 EOP
- SOF ::= Sync PID Frame CRC5 EOP
- SOF: start of frame
- direction: only host -> device
- Data = {DATA0, DATA1, DATA2, MDATA}
- Data ::= Sync PID DATA CRC16 EOP
- direction: both host -> device and device -> host
- Handshake = {ACK, NAK, STALL, NYET}
- NYET: No Response Yet
- Handshake ::= Sync PID EOP
- host -> device: {ACK}
- device -> host: {ACK, NAK, STALL, NYET}
- Special = {PRE, ERR, Split, Ping}
- PRE: PREamble
- Token = {SETUP, IN, OUT, SOF}
- Transaction = {Setup, In, Out}
- Setup ::= SETUP DATA0{8 bytes} ACK
- for Requests
- In ::= IN Data Handshake{0,1}
- Isochronous transfer has no Handshake
- others have
- Out ::= OUT Data Handshake{0,1}
- Isochronous transfer has no Handshake
- others have
- Transaction ::= TokenPhase DataPhase HandshakePhase{0, 1} = Phase{2, 3}
- Phase = {TokenPhase, DataPhase, HandshakePhase}
- Phase ::= Packet
- Phase = {TokenPhase, DataPhase, HandshakePhase}
- Setup ::= SETUP DATA0{8 bytes} ACK
- Transfer = {Control, Interrupt, Bulk, Isochronous}
- Control ::= Stage{2, n} = SetupStage DataStage{0, n} StatusStage
- Stage = {SetupStage, DataStage, StatusStage}
- Stage ::= Token Data Handshake
- SetupStage ::= SETUP DATA0{8 bytes} ACK
- DataStage ::= {IN, OUT} Data+ {ACK, NAK, STALL}
- StatusStage ::= {IN, OUT} Data{0 bytes} {ACK, NAK, STALL}
- Null OUT or Null IN
- max packet size
- low speed: 8 bytes
- full speed: 8, 16, 32 or 64 bytes
- high speed: 64 bytes
- max data bytes / ms
- low speed: 24 (3*8 bytes)
- full speed: 832 (13*64 bytes / frame)
- high speed: 15872 (31*64 bytes / microframe)
- Stage = {SetupStage, DataStage, StatusStage}
- Bulk ::= (Token Data Handshake){1, n}
- here Token = {IN, OUT}
- max packet size
- full speed: 8, 16, 32 or 64 bytes
- high speed: 512 bytes
- max data bytes / ms
- full speed: 1216 (19*64 bytes / frame)
- high speed: 53248 (13*512 bytes / microframe)
- Interrupt ::= (Token Data Handshake){1, n}
- here Token = {IN, OUT}
- max packet size
- low speed: 1~8 bytes
- full speed: 1~64 bytes
- high speed: 1~1024 bytes
- max data bytes / ms
- low speed: 0.8 bytes (8 bytes / 10 ms)
- full speed: 64 (1*64 bytes / frame)
- high speed: 24576 (3*1024 bytes / microframe)
- Isochronous ::= (Token Data){1, n}
- here Token = {IN, OUT}
- max packet size
- full speed: 0~1023 bytes
- high speed: 0~1024 bytes
- max data bytes / ms
- full speed: 1023 (1*1023 bytes / frame)
- high speed: 24576 (3*1024 bytes / microframe)
- Control ::= Stage{2, n} = SetupStage DataStage{0, n} StatusStage
Endpoints
- An endpoint is an addressable FIFO
- USB Specification: a source or sink of data
- CONTROL: endpoint is bi-directional
- Others are uni-directional
- 4 address bits + 1 direction bit selects between 32 buffers (FIFOs)
- Different USB chips support
- Various numbers of endpoints
- Various buffer sizes
- USB Specification: a source or sink of data
Pipes
- Pipe
- Stream Pipe
- Interrupt Transfer
- Bulk Transfer
- Isochronous Transfer
- Message Pipe
- Control Transfer
- Stream Pipe
- Big pipe (USB bus) ::= Small pipes (devices)
- Small pipe (device) ::= Micro pipes (endpoints)
Error Checking
- Checking bits
- Cycle Redundancy Checks, CRC
- 4-bit complement in PID field
- Data Toggle
Suggested Readings
- USB Protocol Specification
- Ch3. USB Protocols
- Ch4. Endpoint Types
- USB 的八個問題和答案
- USB DIY 講座 (二) --- USB 特質
- USB DIY 講座 (三) --- USB 特質(續)
- Approved Class Specification Documents
- USB Complete
- USB Documents and Specifications
Control Transfer
Descriptors
- Device Descriptors
- Configuration Descriptors
- Interface Descriptors
- Endpoint Descriptors
- String Descriptors
- Interface Association Descriptors
- after USB 2.0
Requests
- Standard Requests
- Standard Device Requests
- Get Status
- Clear Feature and Set Feature
- Set Address
- Set Descriptor / Get Descriptor
- Get Configuration / Set Configuration
- Standard Interface Requests
- Get Status
- Clear Feature and Set Feature
- Get Interface / Set Interface
- Standard Endpoint Requests
- Get Status
- Clear Feature and Set Feature
- Synch Frame
- Standard Device Requests
- Class-specific Requests
- Hub-specific requests
- Clear Hub Feature
- Clear Port Feature
- Get Bus Status
- Get Hub Descriptor
- Get Hub Status
- Get Port Status
- Set Hub Descriptor
- Set Hub Feature
- Set Port Feature
- Hub-specific requests
- Vendor-specific Requests
Enumeration
- Powered state (after a device plugging-in)
- Hub detects D+, D- and notifies the host via an interrupt endpoint
- Host requests hub to notify port status
- via Get_Port_Status
- Host requests hub to reset the device
- via Set_Port_Feature
- During reset
- Full Speed to High Speed
- Polling if the reset completion
- via Get_Port_Status
- Default state (after the reset completion)
- Host asks for the Device Descriptor with default address (address 0)
- via Get_Descriptor
- Windows asks for a 64-byte descriptor although the Device Descriptor is total 18 bytes only.
- After receiving the first data packet (even if it less than 64 bytes), the host is into StatusStage immediately.
- Windows will issue another bus reset.
- via requesting Set_Port_Feature to the hub
- Address setting
- via Set_Address
- Host asks for the Device Descriptor with default address (address 0)
- Addressed state (after the address setting)
- Gets the Device Descriptor (18 bytes) with new address
- via Get_Descriptor
- Gets 9 bytes of the Configuration Descriptor
- via Get_Descriptor
- Gets the entire Configuration Descriptor (including all following descriptors)
- via Get_Descriptor
- Asks for any String Descriptors if they were specified
- Configuration setting
- via Set_Configuration
- Gets the Device Descriptor (18 bytes) with new address
- Configured state (after configuration setting)
Tips for Successful Enumeration
from USB Complete, Ch4
- Don't assume requests or events will occur in a specific order.
- Be ready to abandon a control transfer or end it early.
- On receiving a new Setup packet, a device must abandon any transfer in progress and begin the new one.
- On receiving an OUT token packet, the device must assume that the host is beginning the Status stage of the transfer even if the device hasn’t sent all of the requested data in the Data stage.
- Don't attempt to send more data than the host asks for.
- Send a zero-length data packet when required.
- If the device has less than the requested amount of data to return and if the amount of data is an exact multiple of the endpoint’s maximum packet size, the device should indicate that there is no more data by returning a zero-length data packet in response to the next IN token packet.
- Stall unsupported requests.
- Don't set the address too soon.
- In a Set_Address request, the device should set its new address only after the Status stage of the request is complete.
- Be ready to enter the Suspend state.
- A host can suspend the bus when the device is in any powered state, including before the device has been configured.
- Test under different host-controller types.
Suggested Readings
- Descriptors
- Requests
- Enumeration
Bulk Transfer
- USB DIY 講座 (八) --- Bulk Transfer 基礎
- USB DIY-- 自學計畫(三)
- USB DIY-- 自學計畫(四)
- USB DIY-- 自學計畫(五)
- Mass Storage Device Class
Isochronous Transfer
Drivers
usbstor.sys -> for mass storage
usbaudio.sys -> for Audio devices
usbvideo.sys -> for Video devices
C:\WINDOWS\system32\drivers\libusb0.sys
C:\WINDOWS\system32\libusb0.dll
C:\WINDOWS\inf\???.inf
- USB DIY 講座 (九) --- 基礎應用(一)
- USB DIY 講座 (十) --- 應用軟體簡介
- USB DIY 講座 (十一) --- 驅動程式
- Universal Serial Bus Device Driver Development
- Windows Driver Model
- LibUsb-Win32
- PyUSB
- libusb
Applications
CreateFile / CloseHandle
ReadFile/ReadFileEx
WriteFile/WriteFileEx
DeviceIoControl
WM_DEVICECHANGE
- USB DIY 講座(十二)-- 實作範例 USB ISP (一)
- MP3 系統應用簡介(三)-- USB 篇 part1
- MP3 系統應用簡介(五)-- USB 結語 + NAND 楔子
- 改寫原廠的USB應用程式(續二)
- from USBLAB 討論區
- 偵測 USB 插拔
- USB composite device
Windows Support
- Windows Hardware Developer Central, WHDC
- USB Technologies
- USB FAQ - Intermediate
- What are the major restrictions imposed by Usbccgp.sys?
- Usbccgp supports only the default configuration, configuration 1.
- Usbccgp does not support Selective Suspend in Windows XP and Windows Server 2003. This feature is supported only in Windows Vista and later versions of Windows.
- Usbccgp supports the interface association descriptor (IAD) only in Windows XP Service Pack 2, Windows Server 2003 Service Pack 1, and later Windows versions.
- Usbccgp supports nonconsecutive interface numbers only in Windows XP SP2, Windows Server 2003 SP1, and later Windows versions.
- What are the major restrictions imposed by Usbccgp.sys?
- MSDN
- USB Common Class Generic Parent Driver
- Descriptors on Composite USB Devices
- Prior to Windows Vista, Microsoft-supplied drivers only select configuration 1.
- In Windows Vista, you can set a registry value to specify which configuration the USB Common Class Generic Parent Driver will use.
- Support for Interface Collections
- USB Configuration Descriptors
Schematics
VBUS: 5V ~ 5.25V
DP: 3V ~ 3.6V





