The idea: the host asks, the device describes itself
When you plug in a USB keyboard, the computer knows nothing about it. It does not know its speed, its address, what kind of device it is or which driver it needs. Enumeration is the short conversation in which the host finds all that out. The host always asks and the device only answers. Each question is a control transfer on endpoint 0, the one endpoint every device has. The answers are descriptors: small tables of bytes in a format fixed by chapter 9 of the USB 2.0 specification. At the end the host picks a configuration and a driver takes over.
Attach: the pull-up tells the speed
A USB cable has four wires: VBUS (5 V), ground, and the data pair D+ and D−. The hub keeps both data lines low through weak resistors. A full-speed device (12 Mbit/s) connects a 1.5 kΩ pull-up on D+; a low-speed device (1.5 Mbit/s) puts it on D−. So the hub sees which line goes high and knows the speed before any packet is sent. A high-speed device starts as full speed and switches later, during the reset.
The hub reports the change (C_PORT_CONNECTION) on its status-change endpoint. The host waits 100 ms for the plug to settle, then asks the hub to reset the port (SetPortFeature(PORT_RESET)). The hub drives both lines low, called SE0, for at least 10 ms. After a reset every device is in the Default state and answers at address 0. The hub enables one new port at a time, so only one device ever answers at address 0 (Demo: plug in, reset, read the first 8 bytes).
| State (Fig. 9-1) | How the keyboard gets there | What it answers to |
|---|---|---|
| Attached, Powered | plugged in; VBUS gives 5 V | nothing yet |
| Default | bus reset (SE0 for 10 ms) | address 0, endpoint 0 |
| Address | SET_ADDRESS, after its status stage | its own address, endpoint 0 |
| Configured | SET_CONFIGURATION with a non-zero value | endpoint 0 and the configuration's endpoints |
A control transfer: SETUP, DATA, STATUS
Every request in enumeration has the same shape. The SETUP stage carries 8 request bytes: bmRequestType (direction, type, recipient), bRequest (6 = GET_DESCRIPTOR, 5 = SET_ADDRESS, 9 = SET_CONFIGURATION), wValue, wIndex and wLength. An optional DATA stage moves up to wLength bytes. The STATUS stage always goes the other way and has no data: it confirms the whole transfer.
Each stage is made of packets. The host sends a token (SETUP, IN or OUT), then a data packet goes one way, then the receiver answers with a handshake. ACK means "got it, CRC good". NAK means "not ready, ask again"; it is not an error, and the host controller just repeats the token (Demo: plug in, reset, read the first 8 bytes). No answer, or a damaged one, counts as an error. Data packets alternate between DATA0 and DATA1, so a receiver can spot a packet sent twice. A SETUP is always DATA0; the first packet of the DATA stage and the STATUS packet are DATA1.
Why the first read is only 8 bytes
At address 0 the host does not know how big a packet endpoint 0 accepts. It can be 8, 16, 32 or 64 bytes at full speed, and that number, bMaxPacketSize0, is byte 7 of the device descriptor. So the host first asks for only 8 bytes, which fit any device. This keyboard answers 12 01 00 02 00 00 00 40: an 18-byte descriptor, type Device, USB 2.0, and bMaxPacketSize0 = 0x40 = 64.
Next, SET_ADDRESS(5) gives the keyboard its own address. It has no DATA stage, and the keyboard changes its address only after the STATUS stage, so the whole request still happens at address 0. The spec gives it 2 ms to switch. The host then reads all 18 bytes of the device descriptor at address 5 (Demo: SET_ADDRESS(5), then the full device descriptor):
| Descriptor | Field | Value here |
|---|---|---|
| Device (18 bytes) | idVendor, idProduct | 0x1234, 0x5678 (example values) |
| Device | bDeviceClass, bNumConfigurations | 0 (class is set per interface), 1 |
| Configuration (9) | wTotalLength, bConfigurationValue, bMaxPower | 34 bytes, 1, 100 mA |
| Interface (9) | bInterfaceClass, SubClass, Protocol | 3 HID, 1 boot, 1 keyboard |
| HID (9) | bcdHID, report descriptor length | 1.11, 63 bytes |
| Endpoint (7) | bEndpointAddress, bmAttributes, wMaxPacketSize, bInterval | 0x81 (endpoint 1 IN), interrupt, 8, 10 ms |
One GET_DESCRIPTOR for the configuration returns the configuration descriptor together with everything inside it: interfaces, class descriptors and endpoints. Here that is 34 bytes. The host asked for up to 255, so the short packet tells it the data is complete (Demo: configuration descriptor of a boot keyboard).
Configuration and the driver
SET_CONFIGURATION(1) switches on the interface and its endpoints; the keyboard is now Configured. The operating system matches interface class 3 to its HID driver. The driver then reads the keyboard through endpoint 0x81. An interrupt endpoint does not interrupt anything: the host polls it at least every bInterval = 10 ms. When no key has changed the keyboard answers NAK. When one has, it sends an 8-byte boot report: modifier bits, a reserved byte and up to six key codes (0x04 = A) (Demo: SET_CONFIGURATION(1), the HID driver polls). Compare the device-side interrupt in The Interrupt-Driven I/O Cycle: here the host controller polls on the bus and raises one interrupt in the PC when a report arrives.
When enumeration fails
A bad cable, a weak power supply or a broken device shows up as transaction errors: a packet with a bad CRC, or no answer before the bus timeout. The host controller retries a transaction up to three times. After three errors the transfer fails, and Linux reports error -71 (EPROTO). The host then resets the port and tries again, often with a new device number. When even SET_ADDRESS gets no handshake, it gives up and disables the port. The log lines on the canvas follow Linux's wording (Demo: bad cable, descriptor read error, SET_ADDRESS fails):
usb 2-1: device descriptor read/8, error -71 usb 2-1: new full-speed USB device number 6 using ohci-pci usb 2-1: device not accepting address 6, error -71 usb usb2-port1: unable to enumerate USB device
See also The I2C Bus and The CAN Bus, two other buses where every byte is acknowledged, and CRC for the check that tells a receiver a packet was damaged.
See also DMA: a USB host controller moves every packet's bytes to and from memory by DMA.
What the page leaves out
Low-speed and high-speed devices (the chirp during reset), the split transactions a high-speed hub uses for slower devices, packet IDs, SYNC, bit stuffing and NRZI coding, string descriptors and GET_STATUS, the HID requests a driver sends next (SET_IDLE, SET_PROTOCOL, GET_REPORT descriptor), STALL handshakes, suspend and resume, and USB 3 and xHCI, whose controller chooses the address itself (Address Device command) instead of software sending SET_ADDRESS. Linux by default first asks for 64 bytes of the device descriptor instead of 8 and reads 9 bytes of the configuration descriptor before the whole thing; the page shows the spec's simpler order. The model clock (1 ms per control transfer) is rounded.