USB in a Nutshell

Communication Principle

  • Transfer ::= Transaction{1, n}
    • Transaction ::= Packet{2, 3}
      • Packet ::= Field{3, 6}
        • Field ::= Bit{1, n}
  • 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.
  • 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
  • Transaction = {Setup, In, Out}
    • Setup ::= SETUP DATA0{8 bytes} ACK
    • 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
  • 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)
    • 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)

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)
      • bit 0..3: endpoint number
      • bit 4..6: Reserved. Set to Zero
      • bit 7: Direction 0 = Out, 1 = In (Ignored for Control Endpoints)
      • Endpoint zero is always assumed to be a control endpoint and is configured before any descriptors are even requested.
    • Different USB chips support
      • Various numbers of endpoints
      • Various buffer sizes

Pipes

  • Pipe
    • Stream Pipe
      • Interrupt Transfer
      • Bulk Transfer
      • Isochronous Transfer
    • Message Pipe
      • Control Transfer
  • 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

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
  • 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
  • Vendor-specific Requests

Enumeration

  1. 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
  2. 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
  3. 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
  4. 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

Bulk Transfer

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

Applications

CreateFile / CloseHandle
ReadFile/ReadFileEx
WriteFile/WriteFileEx
DeviceIoControl
WM_DEVICECHANGE

Windows Support

Schematics

VBUS: 5V ~ 5.25V
DP: 3V ~ 3.6V

Suggested Readings

Forum

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License