Comprehensive Guide to USB Protocol and Linux USB Driver Architecture
This guide thoroughly explains USB technology and its Linux implementation, covering fundamentals, transmission modes, descriptor structures, enumeration flow, gadget driver architecture with MTP details, and host driver mechanisms such as URBs, mouse and storage drivers, plus references for further study.
This comprehensive guide provides an in-depth exploration of USB (Universal Serial Bus) technology and its implementation in Linux systems. The content is structured into five main sections covering fundamental concepts, Linux USB driver architecture, USB descriptors, enumeration processes, and specific driver implementations.
The guide begins by defining USB as a universal serial bus technology designed to simplify connections between personal computers and peripheral devices. It highlights key features such as hot-plugging capability, plug-and-play functionality, excellent scalability, and fast transmission speeds that have made USB technology widely adopted.
The second section delves into USB transmission fundamentals, starting with the USB pyramid topology structure. It explains how USB host controllers and root hubs form the top of the pyramid, with USB hubs expanding connectivity to support up to 127 devices through a 7-bit addressing system. The content covers NRZI encoding, packet formats, and the four distinct USB transmission modes: Control Transfers for enumeration and data integrity, Bulk Transfers for large data with low real-time requirements (like USB drives), Interrupt Transfers for small data with guaranteed polling frequency (like mice and keyboards), and Isochronous Transfers for large data with high real-time requirements (like audio/video).
The guide then explores USB device structure and descriptors, explaining how devices can have multiple configurations but only one active at a time, with configurations containing interfaces and interfaces containing endpoints. It details the various USB descriptors including device descriptors (containing vendor ID, product ID, and device class information), configuration descriptors, interface descriptors, and endpoint descriptors, providing the actual C struct definitions from Linux's ch9.h file.
The third section focuses on USB enumeration, describing it as the process where the host detects device insertion and sends standard requests to obtain USB descriptors. It includes a detailed enumeration flowchart and explains the structure of USB standard requests, along with data flow capture examples using Bus Hound for MTP+ADB configurations.
The fourth section analyzes USB gadget drivers, presenting the gadget framework architecture and code flow diagrams. It provides a detailed analysis of MTP interface startup processes, including system boot sequences, UsbDeviceManager handling, init.qcom.usb.rc configuration files, android.c event handling, and f_mtp.c driver implementation. The MTP transmission startup process is thoroughly examined, showing how UsbManager broadcasts to MtpReceiver, which starts MtpService and MtpServer, ultimately opening /dev/mtp_usb and processing messages.
The final section covers USB host drivers, introducing URB (USB Request Block) as the fundamental data structure for USB device communication. It analyzes specific drivers including mouse drivers (using either hiddev.c or usbmouse.c) and USB storage drivers. The USB storage analysis includes the framework showing how USB devices are emulated as SCSI devices, the mount process involving vold (volume daemon) receiving Uevents, NetlinkManager processing block subsystem messages, and the complete mount workflow through DirectVolume handling.
The guide concludes with comprehensive references including books on Linux device driver development, the official USB specification, and online resources for deeper understanding of USB protocols.
OPPO Kernel Craftsman
Sharing Linux kernel-related cutting-edge technology, technical articles, technical news, and curated tutorials
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.