USB2.0 protocol study notes --- various descriptors

 USB device descriptor

Field name Length (bytes)  Address offset Meaning
Blaenth   1  0  Descriptor length
bDescriptorType   1  1 Descriptor type (here, 1)
bcdUSB   2  2 USB specification version number (BCD code) 
bDeviceClass   1  4 Class Code 
bDeviceSubClass   1  5 Subclass code 
bDeviceProtocol  1  6 Protocol code 
bMaxPackSize0   1  7 Endpoint 0 maximum packet size supported 
idVendor   2  9 Vendor ID 
idProduct   2  11 Product ID 
bcdDevice   2  13 Device version number (BCD code) 
iManufacturer   1  14 Supplier string descriptor index 
iProduct   1  15 Product Character Description string index 
iSerialNumber   1  16 Product Serial good descriptor index 
bNumConfigurations   1  17 The number of supported configuration 

bLenth : The length of the descriptor in bytes general USB device descriptor is 18 bytes, i.e. fixed 0x12;

bDescriptorType: descriptor category code. USB device descriptor is 0x01;

bcdUSB: USB protocol version, using BCD encoding 0x0200 is 2.0, but a small USB-terminal, so that actual [0x00, 0x20}

bDeviceClass: Device class code, most of the interface functions 0 and bInterfaceClass interface descriptor specified, it is noted that when bDeviceClass is 0, bDeviceSubClass must also be 0;

bDeviceSubClass: with bDeviceClass code determines, USB standard definition.

bDeviceProtocol: device class protocol used to define the class and if not 0, the field is 0xFF indication custom protocols .

bMaxPackSize0: endpoint maximum length of 0, the minimum is 8, because the enumeration process, the Get Device Descriptor first read only once, it is necessary that the USB device descriptor contains in this package .

idVendor: vendor ID VID, need to pay protection money to apply.

idProduct: Product ID PID defined by the manufacturers themselves. Typically host drivers to load or source according to a local VID and PID.

bcdDevice: equipment version, also used BCD, empathy USB protocol version number.

iSerialNumber: serial number string index value.

bNumConfigurations: equipment number of configurations, most of the USB devices is only one configuration .

 

USB configuration descriptor

Note that in some cases, the configuration descriptor contains the interface descriptor, a special class descriptor, end descriptor information, etc., and returned to the host along with the device when the host enumeration requests the configuration descriptor, the host can not be returned to the individual .

Field name  Length (bytes)  Offset      meaning
Blaenth  1 0 Configuration descriptor length
bDescriptorType 1 1 Configuration descriptor type
wTotalLength 2 2 The total length of the configuration information
bNumInterfaces 1 4 Configure the number of interfaces
bConfigurationValue 1 5 Configuration values
iConfiguration 1 6 String descriptor index
bmAttributes 1 7 Configuration Features
bMaxPower 1 8 The maximum current (2mA units)

 

bLenth: The descriptor length

bDescriptorType: descriptor type, configuration descriptor to 0x02 ;

wTotalLength: total length of the entire configuration descriptors, including a configuration descriptor, interface descriptor, and endpoint descriptor special class descriptor.

bNumInterfaces: Configure the number of supported interfaces .

bConfigurationValue: Each configuration has an identification value.

iConfiguration: configuration descriptor index.

bmAttributes: characterization reserved D7, D6 identify power supply and 1 for self-powered or bus-powered, D5 identifies whether to support remote wake-up (1), D4-D0 reserved.

bMaxPower: maximum current bus-powered, such as the maximum current value 100 200mA.

 

USB interface descriptor

 Field name   Length (bytes)  Address offset     Meaning   
Blaenth 1 0 Length of the descriptor
bDescriptorType 1 1 Type descriptors
bInterfaceNumber 1 2 Interface No.
bAlterateSetting 1 3 Alternatively the set value
bNumEndpoint 1 4 Endpoint end points other than 0
bInterfaceClass 1 5 Class Code
bInterfaceSubClass 1 6 Subclass code
bInterfaceProtocol 1 7 Protocol code
iInterface 1 8 String descriptor index

bLenth:  length of the descriptor.

bDescriptorType:  Type of the descriptor (interface descriptor 4)

bInterfaceNumber:   indicates the number of the interface, in the configuration using a plurality of interfaces, each interface into a unique number, numbered from 0.

bAlterateSetting:接口的备用编号,规则同上。一般不用

bNumEndpoint:该接口使用的端点数,不包括端点0。即如果为0 就只有控制端点

bInterfaceClass:接口使用的类

bInterfaceSubClass:接口使用的子类

bInterfaceProtocol:接口使用的协议。三个一起定义了设备的功能,鼠标键盘就只需要改协议部分就可以。其他两个都是HID类

iInterface:接口字符串描述符的索引值。

 

USB端点描述符

域  大小(字节) 偏移
bLenth 1 0
bDescriptorType 1 1
bEndpointAddress 1 2
bmAttributes 1 3
wMaxPackSize 2 4
bInterval 1 6

bLenth: 该描述符的长度(字节)

bDescriptorType: 该描述符的类型(0x05)

bEndpointAddress:端点的地址,D7表示端点的传输方向,为1则为输入,为0则为输出,D3-D0为端点号,其他位没有用。

bmAttributes:一个字节的属性描述字节,D1~D0表示端点传输类型,0 为控制传输,1为等时传输,2为批量传输,3为中断传输。如果为等时传输,D3-D2表示等时传输的类型,0表示无同步,1为异步,2为适配,3为同步;D5-D4辨识用途,0为数据端点,1为反馈端点,2为暗反馈端点,3保留;D7-D6保留。但是如果不是同步传输则只用到D1-D0其他位全部保留。

wMaxPackSize:该端点支持的最大数据长度,对于低速和全速设备而言,D10-D0表示数据包最大长,其他位未用,对于高速设备D12-D11表示每个帧的附件传送次数,具体参考USB标准协议。

bInterval:表示该端点的查询时间,对于中断传输表示查询的帧间隔数;对于其他传输方式参考USB标准协议。

 

 类特殊描述符

有些设备还需要有类特殊描述符,这里拿HID设备举例,HID设备的特殊描述符如下

大小 偏移 说明
bLenth 1 0 描述符长
bDescriptorType 1 1 描述符类型
bcdHID 2 2 HID 协议版本
bCountryCode 1 4 国家代码
bNumDescriptors 1 5 下级描述符数量
bDescriptorType 1 6 下级描述符类型
bDescriptorLength 2 7 下级描述符长度
bDescriptorType 1 9 下级描述符的类型(可选)
wDescriptorLength 2 10 下级描述符的长度(可选)
... ... ... ...

 

类比前面描述符的作用这里只需要记一下一下几个字段的意义:

bcdHID:为 HID设备的版本,用BCD码表示,两个字节,如下表示版本为1.10 

bCountryCode:  国家代码(这个用在特殊情况下 比如键盘,美式键盘等)。

bNumDescriptors:下级描述符的数量,在HID设备中至少有一个是报告描述符。

bDescriptorType:HID报告描述符为0x22.

bDescriptorLength:对应描述符的大小

因为下级描述符至少有一个因此,HID类描述至少是10个字节,底下就是一个HID描述符的例子。

 

 

 

    0x09,         /* bLength: HID Descriptor size */
    HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */
    0x10,         /* bcdHID: HID Class Spec release number */
    0x01,
    0x00,         /* bCountryCode: Hardware target country */
    0x01,         /* bNumDescriptors: Number of HID class descriptors to follow */
    0x22,         /* bDescriptorType */
    CUSTOMHID_SIZ_REPORT_DESC,/* wItemLength: Total length of Report descriptor */
    0x00,

 

看了这么多,现在我对USB协议的理解是这样的:USB设备可以理解为一个类,描述符就是这个类的属性,而枚举过程就是host主动请求调用,获取属性的方法,从而知道这个类的属性,其余在主机和设备数据交换过程的处理细节就是设备类的方法,我们实现一个USB设备就是在定义设备属性和定义方法接口的过程,只是定义属性需要了解到以上USB标准中的这些规则(profile),同时枚举过程的一些操作就是必须要实现的接口,其余的部分就要看我们的设备,设计出来支持的操作有哪些,选择的实现了,好了暂时就这些,后面还有一部分概念需要学习,待续。。。

2019-06-02 11:39:57

Guess you like

Origin www.cnblogs.com/w-smile/p/10946665.html