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.

Time-down diagram between host and keyboard: plug in and port reset put the keyboard in the Default state at address 0; GET_DESCRIPTOR(Device, 8) returns bMaxPacketSize0 = 64; SET_ADDRESS(5) moves it to the Address state; GET_DESCRIPTOR(Device, 18) returns idVendor and idProduct; GET_DESCRIPTOR(Configuration) returns 34 bytes with the HID interface and endpoint 0x81; SET_CONFIGURATION(1) makes it Configured; then the host sends IN to endpoint 0x81 every 10 ms.
Five control transfers take the keyboard from Default at address 0 to Configured; then the HID driver polls it.

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 thereWhat it answers to
Attached, Poweredplugged in; VBUS gives 5 Vnothing yet
Defaultbus reset (SE0 for 10 ms)address 0, endpoint 0
AddressSET_ADDRESS, after its status stageits own address, endpoint 0
ConfiguredSET_CONFIGURATION with a non-zero valueendpoint 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.

Two rows of packets. GET_DESCRIPTOR: SETUP stage with SETUP, DATA0 8 bytes and ACK; DATA stage with IN, DATA1 from the keyboard and ACK; STATUS stage with OUT, DATA1 0 bytes and ACK. SET_ADDRESS and SET_CONFIGURATION: SETUP stage the same, no DATA stage, and a STATUS stage with IN, DATA1 0 bytes from the keyboard and ACK.
A control read ends with an OUT status stage; a request with no DATA stage ends with an IN status stage.

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):

DescriptorFieldValue here
Device (18 bytes)idVendor, idProduct0x1234, 0x5678 (example values)
DevicebDeviceClass, bNumConfigurations0 (class is set per interface), 1
Configuration (9)wTotalLength, bConfigurationValue, bMaxPower34 bytes, 1, 100 mA
Interface (9)bInterfaceClass, SubClass, Protocol3 HID, 1 boot, 1 keyboard
HID (9)bcdHID, report descriptor length1.11, 63 bytes
Endpoint (7)bEndpointAddress, bmAttributes, wMaxPacketSize, bInterval0x81 (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.

On the mind map

Open the Computer Architecture mind mapComputerArchitectureAbstraction and numbers: layers and bit patternsAbstraction and numbersCombinational logic: output depends only on inputsCombinational logicSequential logic: state held by a clockSequential logicMIPS software: how C becomes machine wordsMIPS softwareThe processor: datapath plus controlThe processorPipelines and parallelism: more instructions at oncePipelines and parallelismMemory and I/O: small fast near, big slow farMemory and I/OMemory arraysMemory arraysdecoder picks wordline, bitlines readCPU cacheCPU cachetag, index, offset; hit or missCache address breakdownCache address breakdownsets = lines/ways; tag = rest of addressCache Write PoliciesCache Write PoliciesWrite-back sends dirty lines at evictionLoop tiling and interchangeLoop tiling and interchangeWalk rows; tile so reused data fitsMemory hierarchyMemory hierarchyAMAT = hit time + miss rate × penaltyDRAM timingDRAM timingopen row is fast, row change is slowMESI and false sharingMESI and false sharinga write invalidates other copiesVirtual memoryVirtual memoryTLB caches the page tableEmbedded I/OEmbedded I/Odevices are addresses; poll or interruptMCU startup to mainMCU startup to mainCopy .data, zero .bss, then mainButton debounce FSMButton debounce FSMTwo equal 10 ms samples = one pressI2C BusI2C BusWired-AND bus: ACK, stretch, arbitrateUSB enumerationAddress 0, descriptors, SET_CONFIGURATIONCAN bus arbitrationCAN bus arbitrationDominant 0 wins: lowest ID sends unharmedModbus RTU and TCPModbus RTU and TCPone master asks; bad CRC means silenceTDMA time-triggered busTDMA time-triggered busFixed slots; FTA drops extremes, averagesAMBA AXI, AHB, APBAMBA AXI, AHB, APBVALID and READY both 1 on the edgePCIe TLPsPCIe TLPsRequests by address, completions by IDBus vs Crossbar vs NoCBus vs Crossbar vs NoCBus: one at a time; NoC: links in parallelADC and PWMADC and PWMSAR = binary search; duty = OC1RS/(PR2+1)PID ControlPID ControlI removes error; clamp stops windupOpen the Computer Architecture mind mapComputer Architecturesmall fast near, big slow farMemory and I/OMemory arraysMemory arraysdecoder picks wordline, bitlines readCPU cacheCPU cachetag, index, offset; hit or missCache address breakdownCache address breakdownsets = lines/ways; tag = rest of addressCache Write PoliciesCache Write PoliciesWrite-back sends dirty lines at evictionLoop tiling and interchangeLoop tiling and interchangeWalk rows; tile so reused data fitsMemory hierarchyMemory hierarchyAMAT = hit time + miss rate × penaltyDRAM timingDRAM timingopen row is fast, row change is slowMESI and false sharingMESI and false sharinga write invalidates other copiesVirtual memoryVirtual memoryTLB caches the page tableEmbedded I/OEmbedded I/Odevices are addresses; poll or interruptMCU startup to mainMCU startup to mainCopy .data, zero .bss, then mainButton debounce FSMButton debounce FSMTwo equal 10 ms samples = one pressI2C BusI2C BusWired-AND bus: ACK, stretch, arbitrateUSB enumerationAddress 0, descriptors, SET_CONFIGURATIONCAN bus arbitrationCAN bus arbitrationDominant 0 wins: lowest ID sends unharmedModbus RTU and TCPModbus RTU and TCPone master asks; bad CRC means silenceTDMA time-triggered busTDMA time-triggered busFixed slots; FTA drops extremes, averagesAMBA AXI, AHB, APBAMBA AXI, AHB, APBVALID and READY both 1 on the edgePCIe TLPsPCIe TLPsRequests by address, completions by IDBus vs Crossbar vs NoCBus vs Crossbar vs NoCBus: one at a time; NoC: links in parallelADC and PWMADC and PWMSAR = binary search; duty = OC1RS/(PR2+1)PID ControlPID ControlI removes error; clamp stops windup
This page on the Computer Architecture mind map · 79 pages in 7 branches · open it to see the whole map and test yourself in recall mode.