Part 14: USB Drivers
USB drivers connect the kernel to USB devices. This part covers USB device drivers (not host controller drivers).
USB Model
USB is hierarchical: Host → Hub(s) → Device → Interface(s) → Endpoint(s)
flowchart TB
subgraph Host["USB Host"]
HC["Host Controller"]
end
subgraph Device["USB Device"]
DEV["Device<br/>(VID:PID)"]
CFG["Configuration"]
IF1["Interface 0<br/>(class/subclass)"]
IF2["Interface 1"]
EP1["EP1 IN<br/>(Bulk)"]
EP2["EP2 OUT<br/>(Bulk)"]
EP3["EP0<br/>(Control)"]
end
HC --> DEV
DEV --> CFG
CFG --> IF1
CFG --> IF2
IF1 --> EP1
IF1 --> EP2
DEV --> EP3
style Host fill:#738f99,stroke:#0277bd
style Device fill:#8f8a73,stroke:#f9a825
Key insight: Your driver binds to an interface, not the whole device. A USB device can have multiple interfaces (e.g., keyboard + media keys).
Transfer Types
| Type | Use Case | Characteristics |
|---|---|---|
| Control | Configuration, commands | Guaranteed delivery, bidirectional |
| Bulk | Data transfer (storage) | Reliable, no bandwidth guarantee |
| Interrupt | Small periodic data (HID) | Guaranteed latency, small packets |
| Isochronous | Streaming (audio/video) | Guaranteed bandwidth, no retry |
Most drivers use bulk (data) or interrupt (events) transfers.
Linux USB Architecture
flowchart TB
subgraph Drivers["USB Drivers"]
YOUR["Your Driver"]
HID["HID Driver"]
STORAGE["Storage Driver"]
end
subgraph Core["USB Core"]
USBCORE["USB Core"]
USBBUS["USB Bus"]
end
subgraph HCD["Host Controller"]
EHCI["EHCI/XHCI"]
HW["USB Hardware"]
end
YOUR --> USBCORE
HID --> USBCORE
STORAGE --> USBCORE
USBCORE --> USBBUS
USBBUS --> EHCI
EHCI --> HW
style Drivers fill:#7a8f73,stroke:#2e7d32
style Core fill:#738f99,stroke:#0277bd
style HCD fill:#8f8a73,stroke:#f9a825
Chapters
| Chapter | What You’ll Learn |
|---|---|
| Concepts | USB model, endpoints, and URBs |
| Driver Skeleton | usb_driver structure and probe/disconnect |
| Transfers | Control, bulk, and interrupt URBs |
| Error Handling | Disconnect races, URB errors, recovery |
| Suspend and Resume | Autosuspend, PM callbacks |
| User Space Interface | Character device, file operations, ioctl |
| Porting Guide | Migrating from libusb, vendor SDKs |
| WinUSB and libusb | Cross-platform compatibility, usbfs |
| USB Class Drivers | ACM, mass storage, network (NCM/RNDIS), MCTP |
| USB Gadget Drivers | Device-side drivers, ConfigFS, FunctionFS |
| Dynamic Gadget Configuration | Runtime mode switching from kernel code |
Example
- USB Device Driver - Bulk transfer example with simple protocol
Prerequisites
- Kernel module basics (Part 1-2)
- Device model (Part 6) - probe/remove pattern
- [Managed resources (devm_)](/linux-driver-guide-tutorial/part6/05-devres.html) - USB drivers use usb_ and devm_* functions
- Understanding of asynchronous I/O
Further Reading
- USB API Documentation - Official reference
- USB Core API - Core functions
- Writing USB Drivers - Tutorial
- USB in a Nutshell - Protocol basics