Fundamentals 21 min read

Decoding the Mysterious Language of USB 2.0 Packets: A Deep Dive

This article breaks down the USB 2.0 communication protocol by explaining how raw serial bits are organized into fields and packets—covering SYNC, PID, address, endpoint, frame number, data, CRC, as well as token, SOF, data, split‑transaction and handshake packets, with concrete examples and timing details.

Linux Code Review Hub
Linux Code Review Hub
Linux Code Review Hub
Decoding the Mysterious Language of USB 2.0 Packets: A Deep Dive

Fields

All USB packets begin with a Start‑of‑Packet (SOP) delimiter followed by a SYNC field that aligns the incoming bit stream to the local clock. The SYNC field is immediately followed by a Packet Identifier (PID).

Packet Identifier (PID)

The PID consists of a 4‑bit packet‑type field and a 4‑bit checksum generated as the two’s‑complement of the type field.

The checksum guarantees reliable decoding; a mismatch signals a PID error.

All receivers must fully decode every PID. If the checksum fails or the value is undefined, the packet is considered corrupted and the entire packet is ignored.

PID codes are grouped into four encoding sets: Token, Data, Handshake, and Special.

The two most‑significant PID bits (PID<0:1>) indicate the encoding group.

Address Field (ADDR)

ADDR is a 7‑bit device address (ADDR<6:0>) used by token packets to specify the source or destination function.

During reset and power‑up, ADDR defaults to zero and is assigned by the host during enumeration.

ADDR 0 is reserved as the default address and cannot be used for other purposes.

Endpoint Field (ENDP)

The 4‑bit endpoint field allows multiple endpoints within a single function to be addressed.

Every function has endpoint 0 for the default control pipe.

Low‑speed devices support up to three endpoints (control 0 plus two additional endpoints), which can be configured as two control pipes, one control + one interrupt pipe, or two interrupt pipes.

Full‑speed and high‑speed functions can support up to 16 IN and 16 OUT endpoints (16 endpoint pairs).

Frame Number Field

The 11‑bit frame number ranges from 0 to 2047 and wraps to 0 after reaching 2047.

The frame number increments at the start of each new USB frame and provides a time base for time‑sensitive transfers such as audio and video.

Data Field

Payload size can be 0 – 1024 bytes (high‑speed), 0 – 1023 bytes (full‑speed), or up to 8 bytes (low‑speed).

The data field follows SYNC, PID, address, endpoint, and any optional control fields, and precedes the CRC.

It carries the actual payload—file data, control commands, interrupt data, etc.—depending on the transfer type.

CRC Field

Two CRC polynomials are defined:

Token CRC: G(x) = x⁵ + x² + 1 (5‑bit remainder). It protects the address and endpoint fields of token packets and the timestamp field of SOF packets.

Data CRC: G(x) = x¹⁶ + x¹⁵ + x² + 1 (16‑bit remainder). It protects only the data field of data packets; PID has its own checksum.

If all bits are received correctly, the Token CRC remainder is 01100₂ and the Data CRC remainder is 1000000000001101₂.

Packets

Token Packets

Compose PID (IN, OUT, SETUP, or PING), ADDR, and ENDP.

Token type determines direction: IN (device→host), OUT/SETUP (host→device), PING (handshake).

A 5‑bit CRC covers ADDR and ENDP but not PID.

Token and SOF packets must end with an EOP after three data bytes; otherwise the packet is invalid.

Typical uses:

Device initialization: host sends OUT token with address and initialization data.

Data read: host sends IN token; device returns a data packet.

Data write: host sends OUT token, then follows with a data packet.

Control: host sends SETUP token to configure the device.

Handshake: host sends PING token; device replies with ACK or NAK.

Start‑of‑Frame (SOF) Packets

Host periodically emits SOF packets: every 1.000 ms ± 0.0005 ms for full‑speed, every 125 µs ± 0.0625 µs for high‑speed.

SOF contains a PID and an 11‑bit frame number.

All devices (including hubs) receive SOF; it does not generate a response packet.

SOF provides frame synchronization and a time base for bandwidth allocation.

Data Packets

Structure: PID, optional data payload, CRC.

Four data‑PID values: DATA0, DATA1, DATA2, MDATA.

DATA0 and DATA1 support Data Toggle Synchronization.

MDATA, DATA0, DATA1 are used for split transactions.

All data packets must be transmitted in whole‑byte multiples.

Maximum payload sizes: 8 B (low‑speed), 1023 B (full‑speed), 1024 B (high‑speed).

Split Transaction Packets

Enable a high‑speed host to communicate with full‑/low‑speed devices via a hub.

Two special tokens: Start‑Split (SSPLIT) and Complete‑Split (CSPLIT).

SSPLIT fields: Hub address, Port number, SC = 0, S = 0 (speed = full/low), optional ET (endpoint type).

CSPLIT fields: SC = 1, same Hub address and Port, optional U field (must be 0).

Host sends SSPLIT, waits for NYET or ACK; device replies with NYET if not ready, otherwise proceeds.

CSPLIT signals the end of the split transaction and confirms successful data transfer.

Handshake Packets

Consist of a single PID; no data payload.

Four standard handshakes: ACK, NAK, STALL, and a special NYET (high‑speed only).

ACK indicates successful receipt with no CRC or bit‑stuffing errors.

NAK is returned by a function (never by the host) to signal temporary inability to send/receive data.

STALL indicates that the function cannot transfer or receive data or that a control request is unsupported.

NYET (Not Yet) is used in high‑speed PING or split‑transaction scenarios to indicate the device is not ready but may become ready later.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PIDHandshakeCRCpacket structuresplit transactionUSB 2.0
Linux Code Review Hub
Written by

Linux Code Review Hub

A professional Linux technology community and learning platform covering the kernel, memory management, process management, file system and I/O, performance tuning, device drivers, virtualization, and cloud computing.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.