This article was reproduced from: http://blog.csdn.net/zqixiao_09/article/details/50984074
In the end user's view, USB device provides a variety of additional functions, such as file transfer, sound playback, etc., but it is consistent with the interface of all USB devices for USB host. A USB device consists of 3 function modules:USB bus interface ,USB logic device and functional unit:
A--the USB bus interface here refers to the USB device in the serial Interface Engine (SIE);
B--USB logic device is regarded as a collection of endpoints by USB system software;
C--The functional unit is considered by the client software as a collection of interfaces . The SIE, the endpoint and the interface are all components of the USB device;
To better describe the characteristics of USB devices, USB presents the concept of device architecture . From this point of view, the USB device can be considered to consist of a number of configuration , interface and endpoint , that is, a USB device can contain one or more configurations, in each configuration can contain one or more interfaces, in each interface can contain a number of endpoints. The configuration and interface is the abstraction of the USB device function , and the actual data transfer is done by the endpoint . Before using a USB device, you must indicate the configuration and interface in which it is used. This step is typically done when the device is enumerated when the device is connected to the host.
The relationships between these units are as follows:
The device usually has one or more configurations;
Configuration usually has one or more interfaces;
An interface usually has one or more settings;
The interface has 0 or more endpoints.
The concept is too abstract to look like this: There is a device, such as a player that supports video and audio. So, for each of the 4 descriptors mentioned above, what descriptor are they for when setting them?
From what I now understand, such a device corresponds to a device descriptor , the function of supporting video corresponds to an interface descriptor, which supports an interface descriptor for the audio function. In order to support video, there are multiple ports at the lower level that work together to provide support for video data transmission, so there are multiple endpoint descriptors .
USB devices use a variety of descriptors to describe their device architecture, including device descriptors, configuration descriptors, interface descriptors, endpoint descriptors, and string descriptors, which are usually stored in the firmware program of the USB device .
1. Device Descriptor
The device represents a USB device, which consists of one or more configurations. Device descriptors are used to describe the overall information of the device and indicate the number of configurations it contains. A USB device can have only one device descriptor .
[CPP]View PlainCopy
- struct Usb_device_descriptor
- {
- _ _u8 Blength; //Descriptor length
- _ _u8 bDescriptorType; //Descriptor type number
- _ _le16 Bcdusb; //USB Version number
- _ _u8 Bdeviceclass; //USB assigned device class code
- _ _u8 Bdevicesubclass; //USB-assigned subclass code
- _ _u8 Bdeviceprotocol; //USB assigned Protocol code
- _ _u8 BMaxPacketSize0; //endpoint0 Maximum Packet size
- _ _le16 Idvendor; //Manufacturer number
- _ _le16 idproduct; //Product number
- _ _le16 Bcddevice; //Equipment Factory number
- _ _u8 Imanufacturer; //Describe the index of the vendor string
- _ _u8 iproduct; //Describe the index of the product string
- _ _u8 Iserialnumber; //Index describing the device serial number string
- _ _u8 bnumconfigurations; //number of possible configurations
- } _ _attribute_ _ ((packed));
2. Configuration Descriptor
A USB device can contain one or more configurations, such as a USB device's low-power mode and a high-power mode that can be configured separately. Before using a USB device, you must select an appropriate configuration for it. Configuration descriptors are used to describe the characteristics of each configuration in a USB device, such as the number of interfaces included in the configuration. Each configuration of a USB device must have a configuration descriptor.
[CPP]View PlainCopy
- struct Usb_config_descriptor
- {
- _ _u8 Blength; //Descriptor length
- _ _u8 bDescriptorType; //Descriptor type number
- _ _le16 Wtotallength; //Configure the size of all the data returned
- _ _u8 bnuminterfaces; //Configure the number of supported interfaces
- _ _u8 Bconfigurationvalue; parameter values required by the//set_configuration command
- _ _u8 iconfiguration; //The index value of the string describing the configuration
- _ _u8 bmattributes; //Power supply mode selection
- _ _u8 Bmaxpower; //device maximum current extracted from the bus
- } _ _attribute_ _ ((packed));
3. Interface Descriptor
A configuration can contain one or more interfaces, for example, for an optical drive that uses its mass storage interface when used for file transfers, and when used to play a CD, uses its audio interface. An interface is a collection of endpoints that can contain one or more replaceable settings that enable the user to change the number and characteristics of the current interface when USB is in the configured state. Interface descriptors are used to describe the characteristics of each interface in a device, such as the device class to which the interface belongs and its subclasses. Each interface of a USB device must have an interface descriptor
[CPP]View PlainCopy
- struct Usb_interface_descriptor
- {
- _ _u8 Blength; //Descriptor length
- _ _u8 bDescriptorType; //Descriptor Type
- _ _u8 Binterfacenumber; //number of the interface
- _ _u8 balternatesetting; //Alternate Interface descriptor number
- _ _u8 bnumendpoints; The number of end points used by the interface, excluding endpoint 0
- _ _u8 Binterfaceclass; //interface type
- _ _u8 Binterfacesubclass; //Interface sub-type
- _ _u8 Binterfaceprotocol; //The protocol followed by the interface
- _ _u8 IInterface; //The string index value that describes the interface
- } _ _attribute_ _ ((packed));
4. Endpoint Descriptor
The endpoint is the actual physical unit in the USB device, and theUSB data transfer is done between the host and the USB device endpoints . The endpoints are typically provided by USB interface chips, such as the Freescale Company's Mc68hc908jb8 and MC9S12UF32. Each endpoint in a USB device has a unique endpoint number, and the direction of data transmission supported by each endpoint is generally deterministic: either input (in), or output (out). There are also some chip-provided endpoints where the data direction is configurable, for example MC68HC908JB8 contains two endpoints for data sending and receiving: Endpoint 1 and Endpoint 2. Where endpoint 1 can only be used for data sending, that is, the input (in) operation is supported, and endpoint 2 can be used for both data sending and data reception, i.e. support input (in) and output (out) operations. The MC9S12UF32 has 6 endpoints.
You can specify an endpoint and communicate with it using the device address, endpoint number, and transport direction. The transport nature of the endpoint also determines the type of transport that it uses to communicate with the host, such as control endpoints that can only use control transmissions. Depending on the purpose of the endpoint, you can divide your endpoints into two categories: endpoint No. 0 and non-No. 0.
The No. 0 endpoint is special, it has the data input in and the data output out two physical units, and can only support control transmission . All USB devices must contain a NO. 0 endpoint, which is used as the default control pipe . The USB system software is used to configure communication with the USB logical device. The endpoint No. 0 will be available on the USB device and not the NO. 0 endpoint must be configured for use later.
Depending on the needs of the application, the USB device can also contain multiple endpoints other than the terminal No. 0 endpoint. For low-speed devices, the number of additional endpoints is up to 2, and for full speed/high speed devices, the maximum number of additional endpoints is 15.
[CPP]View PlainCopy
- struct Usb_endpoint_descriptor
- {
- _ _u8 Blength; //Descriptor length
- _ _u8 bDescriptorType; //Descriptor Type
- _ _u8 bendpointaddress; //Endpoint address: 0~3 bit is the endpoint number, 7th bit is direction (0-out,1-in)
- _ _u8 bmattributes; ///Endpoint properties: Bit[0:1] with a value of 00 for control, 01 for synchronization, 02 for batch, and 03 for interrupts
- _ _le16 wmaxpacketsize; ////the size of the maximum packets received or sent by this endpoint
- _ _u8 Binterval; //Polling the time interval for the data transfer endpoint
- //For the end of the bulk transfer and the endpoint that controls the transfer, this domain ignores
- //For synchronously routed endpoints, this field must be 1
- _ _u8 Brefresh;
- _ _u8 bsynchaddress;
- } _ _attribute_ _ ((packed));
5. String Descriptor
A string descriptor is usually included in a USB device to describe some proprietary information, such as the name of the manufacturer, the serial number of the device, and so on. Its contents are given in Unicode and can be read by the client software. String descriptors are optional for USB devices.
[CPP]View PlainCopy
- struct Usb_string_descriptor
- {
- _ _u8 Blength; //Descriptor length
- _ _u8 bDescriptorType; //Descriptor Type
- _ _le16 wdata[1];
- } _ _attribute_ _ ((packed));
6. Pipeline
In the structure of the USB system, it can be considered that the data transfer occurs directly between the USB host software and the various endpoints of the USB device, and the connection between them is called the pipeline. The pipeline is built during the configuration of the USB device. Pipeline is the abstraction of the communication flow between USB host and USB device, which indicates that there is logical data transfer between the USB host and the endpoint of the USB device, and the actual data transmission is done by the USB bus interface layer.
The pipeline corresponds to endpoint one by one in the USB device. The number of endpoints that a USB device contains, how many pipelines it can use to communicate with the USB host, and the type of the endpoint determines the type of transport in the data in the pipeline, such as the interrupt endpoint corresponding to the interrupt pipeline, and the pipeline can only interrupt transmission. No matter how many pipelines exist, the data transfer in each pipeline is independent.
7. USB Endpoint Classification
The most basic form of USB communication is through the endpoint. A USB endpoint can transmit data in one direction only (from the host to the device (called the output endpoint) or from the device to the host (called the input endpoint). An endpoint can be viewed as a one-way pipe.
There are 4 different types of USB endpoints, each with different data transfer modes:
1) controlling control
Control endpoints are used to control access to different parts of the USB device. Typically used as a configuration device, get device information, send a command to a device, or get a Device status report. These endpoints are usually smaller. Each USB device has a control endpoint called "Endpoint 0", which is used by the USB core to configure the device at plug-in. The USB protocol guarantees that there is always enough bandwidth left to the control endpoint to transmit data to the device.
2) Interrupt Interrupt
Each time the USB host requests data from the device, the interrupt endpoint transmits a small amount of data at a fixed rate. This is the primary data transfer method for USB keyboards and mice. It also transmits data to the USB device to control the device. It is not usually necessary to transmit large amounts of data. The USB protocol guarantees that there is always enough bandwidth left to the interrupt endpoint to transmit data to the device.
3) Batch Bulk
Bulk endpoints are used to transmit large amounts of data. These endpoints are usually much larger than the interrupt endpoint. They are commonly used in situations where there can be no data loss. The USB protocol does not guarantee that the transmission is completed within a specific time frame. If there is not enough space on the bus to send the entire bulk packet, it is divided into multiple packets for transmission. These endpoints are commonly used on printers, USB Mass storage, and USB network devices.
4) Equal time isochronous
The endpoint also transmits large amounts of data in bulk, but this data is not guaranteed to be delivered. These endpoints are used in devices that can handle data loss and rely more on persisting data flow. such as audio and video devices, and so on.
Control and batch endpoints are used for asynchronous data transfer, while interrupts and equal-time endpoints are periodic. This means that these endpoints are set up to transmit data continuously at a fixed time, and the USB core retains the corresponding bandwidth for them.
[CPP]View PlainCopy
- struct usb_host_endpoint{
- struct Usb_endpoint_descriptor desc; //Endpoint Descriptor
- struct List_head urb_list; //URB for this endpoint, maintained by the USB core
- void *hcpriv;
- struct Ep_device *ep_dev; / * for SYSFS info * /
- unsigned Char*extra; /* Extra descriptors * /
- int Extralen;
- int enabled;
- };
an int usb_hcd_link_urb_to_ep (struct USB_HCD *hcd, struct URB *urb) is called to add this urb to urb_ when invoking the USB device driver call Usb_submit_urb commit URB Request List on the tail. (Hcd:host Controller Driver, corresponding data structure struct USB_HCD)
Linux USB Driver Development (i)--USB equipment basic Concept "turn"