1 数据结构说明:
1.1 设备描述符结构
struct libusb_device_descriptor {
u_int8_t bLength;
u_int8_t bDescriptorType;
u_int16_t bcdUSB;
u_int8_t bDeviceClass;
u_int8_t bDeviceSubClass;
u_int8_t bDeviceProtocol;
u_int8_t bMaxPacketSize0;
u_int16_t idVendor;
u_int16_t idProduct;
u_int16_t bcdDevice;
u_int8_t iManufacturer;
u_int8_t iProduct;
u_int8_t iSerialNumber;
u_int8_t bNumConfigurations;
};
bLength : 描述符大小.固定为0x12.
bDescriptorType : 设备描述符类型.固定为0x01 LIBUSB_DT_DEVICE.
bcdUSB : USB 规范的版本号.表示了本设备能适用于那种协议,如2.0=0200,1.1=0110等.
bDeviceClass : 类型代码(由USB指定)。当它的值是0时,表示所有接口在配置描述符里,并且所有接口是独立的。当它的值是1到FEH时,表示不同的接口关联的。当它的值是0xFF时,它是厂商自己定义的.
bDeviceSubClass : 子类型代码(由USB分配).如果bDeviceClass值是0,一定要设置为0.其它情况就跟据USB-IF组织定义的编码.
bDeviceProtocol : 协议代码(由USB分配).如果使用USB-IF组织定义的协议,就需要设置这里的值,否则直接设置为0。如果厂商自己定义的可以设置为0xFF.
bMaxPacketSize0 : 端点0最大分组大小(只有8,16,32,64有效).
idVendor : 供应商ID(由USB分配).
idProduct : 产品ID(由厂商分配).由供应商ID和产品ID,就可以让操作系统加载不同的驱动程序.
bcdDevice : 设备出产编码.由厂家自行设置.
iManufacturer : 厂商描述符字符串索引.索引到对应的字符串描述符. 为0则表示没有.
iProduct : :产品描述符字符串索引.同上.
iSerialNumber : 设备序列号字符串索引.同上.
bNumConfigurations : 可能的配置数.指配置字符串的个数
USB class CODE 详细说明如下:
Class code 用途 详细说明
00h Device 表示CLASS的信息在接口的描述符中定义
01h Interface Audio 音频设备
02h Both Communications and CDC Control通信设备
03h Interface HID (Human Interface Device) 人机接口设备
05h Interface Physical 物理设备
06h Interface Image 图像设备
07h Interface Printer 打印机
08h Interface Mass Storage 大容量存储
09h Device Hub
0Ah Interface CDC-Data 通信设备
0Bh Interface Smart Card 智能卡
0Dh Interface Content Security 内容安全设备
0Eh Interface Video 视频设备
0Fh Interface Personal Healthcare 个人健康设备
10h Interface Audio/Video Devices 音频/视频设备
DCh Both Diagnostic Device 诊断设备
E0h Interface Wireless Controller 无线控制器(蓝牙设备等)
EFh Both Miscellaneous 杂项(ActiveSync,PalmSync,各种协会等)
FEh Interface Application Specific 应用专有规范(固件升级,红外,USB测试与测量等)
FFh Both Vendor Specific 供应商自定义规范
注:各类设备的class subclass protocol的详细定义可以参考http://www.usb.org/developers/defined_class
1.2 配置描述符,配置描述符定义了设备的配置信息,一个设备可以有多个配置描述符
struct libusb_config_descriptor {
u_int8_t bLength;
u_int8_t bDescriptorType;
u_int16_t wTotalLength;
u_int8_t bNumInterfaces;
u_int8_t bConfigurationValue;
u_int8_t iConfiguration;
u_int8_t bmAttributes;
u_int8_t MaxPower;
const struct libusb_interface *interface;
const unsigned char *extra;
int extralen;
};
bLength : 描述符大小.固定为0x09.
bDescriptorType : 配置描述符类型.固定为0x02 LIBUSB_DT_CONFIG.
wTotalLength : 返回整个数据的长度.指此配置返回的配置描述符,接口描述符以及端点描述符的全部大小.
bNumInterfaces : 配置所支持的接口数.指该配置配备的接口数量,也表示该配置下接口描述符数量.
bConfigurationValue : 作为Set Configuration的一个参数选择配置值.
iConfiguration : 用于描述该配置字符串描述符的索引.
bmAttributes : 供电模式选择.Bit4-0保留,D7:总线供电,D6:自供电,D5:远程唤醒.
MaxPower : 总线供电的USB设备的最大消耗电流.以2mA为单位.
1.3 libusb_interface
struct libusb_interface {
/** Array of interface descriptors. The length of this array is determined
* by the num_altsetting field. */
const struct libusb_interface_descriptor *altsetting;
/** The number of alternate settings that belong to this interface */
int num_altsetting;
};
altsetting :接口描述符的指针
num_altsetting:接口描述符的数量,这个值等于bNumInterfaces
1.4 接口描述符:接口描述符说明了接口所提供的配置,一个配置所拥有的接口数量通过配置描述符的bNumInterfaces决定
struct libusb_interface_descriptor {
u_int8_t bLength;
u_int8_t bDescriptorType;
u_int8_t bInterfaceNumber;
u_int8_t bAlternateSetting;
u_int8_t bNumEndpoints;
u_int8_t bInterfaceClass;
u_int8_t bInterfaceSubClass;
u_int8_t bInterfaceProtocol;
u_int8_t iInterface;
const struct libusb_endpoint_descriptor *endpoint;
const unsigned char *extra;
int extralen;
};
bLength : 描述符大小.固定为0x09.
bDescriptorType : 接口描述符类型.固定为0x04 LIBUSB_DT_INTERFACE.
bInterfaceNumber: 该接口的编号.
bAlternateSetting : 用于为上一个字段选择可供替换的位置.即备用的接口描述符标号.
bNumEndpoint : 使用的端点数目.端点0除外.
bInterfaceClass : 类型代码(由USB分配).
bInterfaceSunClass : 子类型代码(由USB分配).
bInterfaceProtocol : 协议代码(由USB分配).
iInterface : 字符串描述符的索引
1.5 端点描述符:USB设备中的每个端点都有自己的端点描述符,由接口描述符中的bNumEndpoint决定其数量
struct libusb_endpoint_descriptor {
u_int8_t bLength;
u_int8_t bDescriptorType;
u_int8_t bEndpointAddress;
u_int8_t bmAttributes;
u_int16_t wMaxPacketSize;
u_int8_t bInterval;
u_int8_t bRefresh;
u_int8_t bSynchAddress;
unsigned char *extra; /* Extra descriptors */
int extra_length;
};
bLength : 描述符大小.固定为0x07.
bDescriptorType : 接口描述符类型.固定为0x05 LIBUSB_DT_ENDPOINT.
bEndpointAddress : USB设备的端点地址.Bit7,方向,对于控制端点可以忽略,1/0:IN/OUT.Bit6-4,保留.BIt3-0:端点号.
bmAttributes : 端点属性.Bit7-2,保留.BIt1-0:00控制,01同步,02批量,03中断.
wMaxPacketSize : 本端点接收或发送的最大信息包大小.
bInterval : 轮询数据传送端点的时间间隔.对于批量传送和控制传送的端点忽略.对于同步传送的端点,必须为1,对于中断传送的端点,范围为1-255.
Endpoint 按属性分有四种类型:
1)控制端点(Control Endpoint):
通常用于 配置设备、获取设备信息、发送命令到设备、获取设备的状态报告等,即传输控制信息。
2)中断端点(Interrupt Endpoint):
当 USB 主机要求设备传输数据时,中断端点就以一个固定的速率来传送少量的数据。(USB键鼠即是此方式)
3)批量端点(Bulk Endpoint):
传输大批量数据,确保不丢失数据,但不保证特定时间内完成。(打印机、网络设备多用此方式)
4)同步端点(Isochronous Endpoint):
传输大批量数据,不保证数据是否到达,对传送延迟非常敏感,可保持一个恒定速率收发实时信息。(音视频设备多用此方式)