USB描述符作用以及层次结构
USB是通过USB描述符来对USB设备进行属性的说明,包括使用的协议、接口数目、端点和传输方式等等。当USB设备插入主机后,主机要对其进行总线枚举,配置该设备所需的驱动等信息。主机通过标准请求Get Descriptor来读取USB的描述符,从而得到设备的相关信息,根据这些信息,然后建立通信。因此说,只有正确设置USB的描述符,才能使USB设备正常的工作起来。
标准的USB设备有5种USB描述符,分别为设备描述符、配置描述符、接口描述符、端点描述符和字符串描述符。每一个USB设备都有一个设备描述符。而每一个设备描述符有一个默认的配置描述符,配置描述符主要定义了USB设备的功能,如果USB支持多个功能,那样就需要为每个功能定义一个配置。但是同一时刻只有一个配置可用。一个配置支持至少一个接口。接口只要定义了实现功能的硬件的集合。每一个能够与USB实现数据交换的硬件叫做端点。也就是说接口是端口的集合。字符串描述符是一个连续的数字。
下图显示了USB描述符的层次结构:
下面我们对每一种描述符的结构进行详细的介绍。
l 设备描述符
typedef __packed struct _USB_DEVICE_DESCRIPTOR
{
byte bLength; //the length of descriptor = 12H
byte bDescriptorType; // the type of descriptor = 01H
word bcdUSB; // the number of USB programming issue
byte bDeviceClass; // types code
byte bDeviceSubClass; // subtypes code
byte bDeviceProtocol; // the code of protocol
byte bMaxPacketSize0; // the max group of endpoint 0
word idVendor; // the ID of vendor
word idProduct; // the ID of product
word bcdDevice; // the number of device as leaving factory
byte iManufacturer; // the index of the manufacturer descriptor
byte iProduct; // the index of the product descriptor
byte iSerialNumber; //the index of number of sequence
byte bNumConfigurations; // the number of maybe confabulated
}
USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
l 配置描述符
typedef __packed struct _USB_CONFIGURATION_DESCRIPTOR
{
byte bLength; // the length of descriptor = 09H
byte bDescriptorType; // the type of descriptor
word wTotalLength; // the length of the data, including the
// entire configuration.
byte bNumInterfaces; // 配置所支持的接口数
byte bConfigurationValue; // as a choice of set configuration parameter
byte iConfiguration; // the index of string describing the configuration
byte bmAttributes; // 配置特性
// D7:总线供电
// D6:自供电
// D5:远程唤醒
// D4…0:保留
byte MaxPower; // 当设备完全可以操作时,总线供电的USB设备的最
// 大消耗电流,以2MA为单位
}
USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;
l 接口描述符
typedef __packed struct _USB_INTERFACE_DESCRIPTOR
{
byte bLength; // 描述符大小 = 09H
byte bDescriptorType; // 接口描述符类型 = 04H
byte bInterfaceNumber; // 接口的编号
byte bAlternateSetting; // 用于为上一个字段悬着可供替换的设置
byte bNumEndpoints; // 使用的端点数目(0端点除外)
byte bInterfaceClass; // 类型代码(由USB分配)
byte bInterfaceSubClass; // 子类型代码(由USB分配)
byte bInterfaceProtocol; // 协议代码(由USB分配)
byte iInterface; // 字符串描述符的索引
}
USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;
l 端点描述符
typedef __packed struct _USB_ENDPOINT_DESCRIPTOR
{
byte bLength; //描述符大小 = 07H
byte bDescriptorType; //端点描述符类型 = 05H
byte bEndpointAddress; //USB端点地址:bit 0…3 端点号
// bit 4…6 保留
// bit 7 方向控制 0:OUT 1:IN
byte bmAttributes; // 端点属性
// bit 0…1
// 00:控制 01:同步
// 02:批量 03:中断
word wMaxPacketSize; // 本端点接受或发送的最大信息包的大小
byte bInterval; // 轮询数据传送端点的时间间隔
//对于批量传送的端点以及控制传送的端点忽略
//对于同步传送的端点此域必须为1
//对于中断传送的端点,此域值的范围为1~255
}
USB_ENDPOINT_DESCRIPTOR, *PUSB_ENDPOINT_DESCRIPTOR;
l 字符串描述符
typedef __packed struct _USB_STRING_DESCRIPTOR
{
byte bLength; // 描述符大小
byte bDescriptorType; // 描述符类型
byte bString[1]; // 字符串长度
}
USB_STRING_DESCRIPTOR, *PUSB_STRING_DESCRIPTOR;