First, USB device description structure
1. Logical Organizational Structure
In the logical organization of USB devices, there are 4 levels of devices, configurations, interfaces, and endpoints. A device typically has one or more configurations, and the configuration usually has one or more interfaces with 0 or more endpoints.
Each USB device can contain one or more configurations, and different configurations allow the device to exhibit different combinations of functions, and the configuration consists of multiple interfaces. In the USB protocol, the interface represents a basic function, a complex USB device can have multiple interfaces, and an interface is a collection of endpoints.
A USB player with audio, video features, knobs and buttons.
Configuration 1: Audio (interface) + Knob (interface)
Configuration 2: Video (interface) + Knob (interface)
Configuration 3: Audio (interface) + video (interface) + button (interface)
A driver is required for audio interface, video interface, button interface, and knob interface.
The only addressable part of the USB device is the device endpoint, which acts like a register. Each endpoint has a unique endpoint number within the device, which is given at device design time. The communication between the host and the device is ultimately used for each endpoint on the device. The operations supported by each endpoint are one-way, either read-only or write-only.
2. Descriptor
Within each USB device, contains the fixed-format data, through which the USB host can obtain the type of USB device, manufacturer and other information. These fixed-format data, which we call the USB descriptor. Standard USB devices have 5 USB descriptors: Device descriptors, configuration descriptors, interface descriptors, endpoint descriptors, string descriptors.
2.1 Device Descriptor
Blength: Descriptor length, fixed to 0x12.
bDescriptorType: Device descriptor type, fixed to 0x01.
BCDUSB:USB specification release number. Indicates that the device can be used for that kind of protocol, such as 2.0=0200
Bdeviceclass: Type code.
Bdevicesubclass: Sub-type code.
Bdeviceprotocol: Protocol code.
BMAXPACKETSIZE0: Endpoint 0 maximum grouping size.
Idvendor: Vendor ID.
IDPRODUCT: Product ID (assigned by vendor).
Bcddevice: Device Production Code, set by the manufacturer itself.
Imanufacturer: The vendor Descriptor string Index. The index to the corresponding string descriptor.
IProduct:: Product descriptor string Index.
Iserialnumber: Device serial number string index.
Bnumconfigurations: The number of possible configurations.
2.2 Configuration Descriptor
Blength: Descriptor length, fixed to 0x09.
bDescriptorType: Configuration descriptor type, fixed to 0x02.
Wtotallength: Returns the length of the entire data, which is the configuration descriptor returned by this configuration, the interface descriptor, and the full size of the endpoint descriptor.
Bnuminterfaces: The number of interfaces supported by the configuration, the number of interfaces that the configuration is equipped with, and the number of interface descriptors under that configuration.
Bconfigurationvalue: Select the configuration value as a parameter of the set configuration.
IConfiguration: The index used to describe the configuration string descriptor.
Bmattributes: Power supply mode selection. bit4-0 reserved, D7: Bus powered, D6: Self-powered, D5: remote wakeup.
Maxpower: The maximum consumption current of a bus-powered USB device, in 2mA units.
2.3 Interface Descriptor
Blength: Descriptor length, fixed to 0x09.
bDescriptorType: Interface descriptor type, fixed to 0x04.
Binterfacenumber: The number of the interface.
Balternatesetting: Used to select a setting that can be replaced for the previous field.
Bnumendpoint: The number of endpoints used, except for endpoint 0.
Binterfaceclass: Type code (assigned by USB organization).
Binterfacesunclass: Sub-type code (assigned by USB organization).
Binterfaceprotocol: Protocol code (assigned by USB organization).
IInterface: The index of the string descriptor.
2.4 Endpoint Descriptor
Blength: Descriptor size, fixed to 0x07.
bDescriptorType: Interface descriptor type, fixed to 0x05.
The endpoint address of the BENDPOINTTYPE:USB device. Bit7, direction, for the control endpoint can be ignored, 1/0:in/out. Bit6-4, reserved. BIT3-0: Endpoint number.
Bmattributes: Endpoint Properties, Bit7-2, reserved. bit1-0:00 control, 01 sync, 02 Batch, 03 interrupt.
Wmaxpacketsize: The maximum packet size that this endpoint receives or sends.
Binterval: Rotation The time interval for the data transfer endpoint. For bulk transfer and control transfer of endpoint ignored. For synchronous-routed endpoints, it must be 1, and the range is 1-255 for the end point of the interrupt delivery .
Second, USB data communication
1. Communication model
2. Transmission
USB data communication First is based on the transmission (Transfer), the type of transmission is : interrupt transmission (such as USB, keyboard), bulk transmission (large-capacity transmission: such as a USB stick), synchronous transmission, control transmission .
3. Things
A single transport consists of one or more transactions (transaction), which can be divided into: intransaction, out transaction, Setup transaction
4. Package
A transaction consists of one or more packages (packet), which can be divided into: token package (Setup), packet (data), handshake Packet (ACK), and special package
5. Domain
A package consists of multiple domains, domain can be divided into: synchronization domain (sync), identity domain (PID), address domain (ADDR), endpoint domain (ENDP), Frame Number field (FRAM), data domain (database), check domain (CRC)
Third, USB data enumeration
USB device before it works, the first thing to do is enumerate. enumeration is to let the host recognize this USB device, and prepare resources for the device, to establish a good host and the data transfer channel between the device.
1. Get the device descriptor
2. Reset
3. Setting the Address
4. Get the device descriptor again
5. Get the configuration descriptor
6. Get interface, endpoint descriptor
7. Get the string descriptor
8. Select Device Configuration
USB Protocol Analysis