Skip to content
systemcoding.dev
Kernels

How USB Works: Devices, Configurations, Interfaces and Endpoints

How a USB device is organized into configurations, interfaces and endpoints, and how Linux 6.12 shows that tree in sysfs and hands each interface to a driver.

7 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, FreeBSD and Rust logos.

A USB device is not one thing that you talk to. It is a small tree of descriptors, and every Linux USB driver is written against one branch of that tree. The most common beginner mistake is to treat a USB device as a single port with one data pipe, then wonder why a webcam shows up as four separate devices or why a driver for a serial adapter claims only half of it. This post walks the tree from the top (device) to the bottom (endpoint), shows where each level appears in sysfs on Linux 6.12, and explains why the kernel binds drivers to interfaces and not to devices.

The authoritative description is the USB 2.0 specification, chapter 9, and the kernel's own view is documented in the USB API guide.

The host is in charge

USB is a host-controlled bus. A device never speaks first and never sends data on its own schedule. The host controller (xHCI on any recent PC or board) polls each device and each endpoint, and a device answers only when it is addressed. That one fact explains most of the design: a keyboard that has no key to report still gets asked, and answers "nothing yet", because the host sets the polling interval.

Devices hang off hubs in a tiered star, never a ring or a daisy chain. The root hub is part of the host controller, and up to five hub tiers are allowed below it. Every device gets a unique address on its bus during enumeration, and the kernel numbers each root hub as a bus (usb1, usb2 and so on). How a device gets that address is covered in a separate post on enumeration. Here the focus is on what the device describes about itself once it has one.

The four levels of the tree

Every device answers a request for its descriptors. The descriptors nest like this:

LevelWhat it describesKernel structure
DeviceVendor ID, product ID, USB version, how many configurations existstruct usb_device
ConfigurationOne complete power and function setup, selected as a wholestruct usb_host_config
InterfaceOne function, such as audio streaming or a mass storage unitstruct usb_interface
EndpointOne unidirectional data pipe with a type and a packet sizestruct usb_host_endpoint

A device descriptor is always 18 bytes and carries idVendor, idProduct, bDeviceClass and bNumConfigurations. The vendor and product IDs are what most drivers match on.

A configuration is a complete way of operating the device. Almost every device has exactly one. The few that have several use them for real alternatives, such as a bus-powered mode and a self-powered mode. Only one configuration is active at a time, and the host selects it with a SET_CONFIGURATION request. Linux picks one automatically unless a driver or a udev rule chooses another.

An interface is a function. A USB webcam with a microphone has at least a video control interface, a video streaming interface, an audio control interface and an audio streaming interface, all inside one configuration. They are independent: different drivers can own different interfaces of one physical device, and they do.

An endpoint is where data actually moves. Each endpoint has an address, a direction, a transfer type and a maximum packet size.

Endpoints and the four transfer types

An endpoint address is one byte. Bits 0 to 3 are the endpoint number and bit 7 is the direction, seen from the host: IN (bit 7 set) means device to host and OUT means host to device. So 0x81 is endpoint 1, IN, and 0x02 is endpoint 2, OUT. Endpoint 0 is special: every device has it, it is bidirectional, and it carries all control transfers, including the enumeration traffic.

There are four transfer types, and picking the right one is the first design decision in any driver:

TypeUsed forGuarantee
ControlConfiguration and commands, always on endpoint 0Delivery is checked and retried; small and bursty
BulkLarge data where timing does not matter, such as disks and printersNo latency promise; uses whatever bandwidth is left
InterruptSmall data that must arrive within a bounded delay, such as keys and micePolled at a fixed interval the device declares
IsochronousStreams such as audio and videoReserved bandwidth every frame; no retries

Interrupt is a name that misleads. A USB device cannot interrupt the host. "Interrupt" endpoints are polled by the host at the interval the device asks for, which is why a USB keyboard has a latency measured in milliseconds rather than microseconds.

See the tree in sysfs

Linux exposes every level as a directory under /sys/bus/usb/devices. The naming is systematic. A device is <bus>-<port path>, and an interface is <device>:<configuration>.<interface>. Take a machine with a single USB flash drive on the first port of bus 1:

Shell
ls /sys/bus/usb/devices
Text
1-0:1.0  1-2  1-2:1.0  usb1

usb1 is the root hub device for bus 1, 1-0:1.0 is that hub's only interface, 1-2 is the flash drive on port 2, and 1-2:1.0 is the drive's first interface in its first configuration. Reading the files in the device directory shows the descriptor fields:

Shell
cd /sys/bus/usb/devices/1-2
cat idVendor idProduct bNumConfigurations bConfigurationValue
Text
0951
1666
1
1

The values here are an example, and yours will differ. bNumConfigurations is 1, and bConfigurationValue is 1, which means configuration 1 is active. Now look one level down, at the interface and its endpoints:

Shell
cd /sys/bus/usb/devices/1-2:1.0
cat bInterfaceClass bNumEndpoints
ls -d ep_*
Text
08
02
ep_02  ep_81

Class 08 is mass storage. The two endpoint directories are ep_81 (bulk IN) and ep_02 (bulk OUT), which is the pair every mass storage device uses for data. Each endpoint directory has type, direction, wMaxPacketSize and bInterval files you can read the same way.

Why drivers bind to interfaces

The kernel matches a driver to an interface, not to the device. The matching rule is the id_table in the driver, and the entries can test a vendor and product pair, a device class, or an interface class, subclass and protocol triple. When the match succeeds, the kernel calls the driver's probe with a struct usb_interface.

This is the reason a composite device works at all. The USB core reads the configuration once and then offers each interface to the registered drivers separately. The audio interfaces go to the sound driver, the HID interface goes to the input driver, and a vendor-specific interface may go to a driver you wrote. A bInterfaceClass of 0xff means vendor specific, and a driver for such an interface almost always matches on the vendor and product IDs instead.

Inside probe, the driver finds its endpoints by walking the current alternate setting. Kernel 6.12 offers a helper that does the common case in one call, so you do not loop over the descriptors by hand:

c
#include <linux/usb.h>

static int sample_probe(struct usb_interface *intf,
			const struct usb_device_id *id)
{
	struct usb_endpoint_descriptor *bulk_in, *bulk_out;
	int ret;

	ret = usb_find_common_endpoints(intf->cur_altsetting,
					&bulk_in, &bulk_out, NULL, NULL);
	if (ret)
		return ret;

	dev_info(&intf->dev, "bulk in 0x%02x, bulk out 0x%02x, max packet %u\n",
		 bulk_in->bEndpointAddress, bulk_out->bEndpointAddress,
		 usb_endpoint_maxp(bulk_in));
	return 0;
}

For the flash drive above, a driver that bound to its interface would log the following in the kernel log:

Text
usb 1-2:1.0: bulk in 0x81, bulk out 0x02, max packet 512

The 512 is the maximum bulk packet size at high speed, which is fixed by the specification. At full speed it can be 8, 16, 32 or 64, and at SuperSpeed it is 1024. The function returns -ENXIO when the interface lacks one of the requested endpoint types, which is exactly the signal probe needs to decline an interface it does not understand.

Alternate settings

An interface can have alternate settings, numbered from 0. Setting 0 is the default and the one active after configuration. Alternates exist mainly for isochronous interfaces: a webcam offers setting 0 with no streaming endpoint (zero bandwidth reserved) and further settings with larger packet sizes. The driver picks one with usb_set_interface() when streaming starts, so an idle camera does not hold bus bandwidth that other devices could use. That is why intf->cur_altsetting is the right field to read in probe, and why a driver for a streaming device must not assume the endpoints it needs exist in alternate 0.

Where this model does not apply

The tree above describes a device that follows the standard descriptor layout. Three cases bend it. A device in a vendor-specific mode may present a different tree after a firmware download, and then the vendor and product IDs change too (a very common pattern for cameras and radio dongles). A USB Type-C device in an alternate mode, such as DisplayPort, negotiates that over the Power Delivery protocol, which is outside this descriptor model. And a gadget (the Linux machine acting as the device) builds the same descriptors in software, which is covered in the posts on USB gadgets.

Also inHardware
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.