【USB笔记】接口描述符Interface Descriptor

接口描述符InterfaceDescriptor是配置描述符的一部分,描述了接口的类、子类、协议及端点数。它用于主机获取配置信息,但不能直接通过GetDescriptor请求访问。每个接口可有多个替代设置,每次只有一个激活。接口描述符包含了设备类码、子类码、协议码等信息,用于定义设备功能和通信方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

USB笔记 接口描述符 (Interface Descriptor)

接口描述符(Interface Descriptor)描述了配置中一个特定的接口。配置提供了一个或多个接口,每个接口都含有类(Class),子类(SubClass)和协议(Protocol)的信息,以及接口所使用的端点(Endpoint)数目。

接口描述符(Interface Descriptor)总是作为配置描述符的一部分返回,不能直接用Get Descriptor或者Set Descriptor请求访问。

一个配置可含有多个同时活跃的接口。这些接口可能联合于某个功能,也可能并不相关。

每个接口都有它自己的接口描述符(Interface Descriptor)和附属描述符(subordinate descriptor)。

所有这些接口也都可拥有一个或多个替代的接口设置(alternate interface settings)。这些设置是相互排他的,每次只能有一个被激活。每个设置含有一个接口描述符(Interface Descriptor)及所的附属描述符(subordinate descriptor)。使用等时传输(isochronous transfers)的设备必须有替代接口设置(alternate interface settings),因为缺省的接口不能请求等时带宽(isochronous bandwidth)。

Interface Descriptor

OffsetFieldSizeValueDescription
0bLength1Number以字节为单位的描述符大小
1bDescriptorType1Constant接口描述符类型
2bInterfaceNumber1Number接口的编号
3bAlternateSetting1Number用来确认bInterfaceNumber的替代设置的编号
4bNumEndpoints1Number接口使用的端点数量
5bInterfaceClass1Class类码
6bInterfaceSubClass1SubClass子类码
7bInterfaceProtocol1Protocol协议码
8iInterface1Index接口字符串描述符的索引

bLength以字节为单位的描述符大小(0x09)。

bDescriptorType接口描述符类型,为INTERFACE(0x04)。

bInterfaceNumber接口的编号。表示该配置同时支持的接口序列索引。编号的值从0开始,缺省值为0x00。每个接口描述符中,该字段取值必须唯一。在组合设备(composite device )的配置中拥有多个同时活动的接口。

bAlternateSetting用来确认bInterfaceNumber的替代设置的编号。对于每个bInterfaceNumber ,设备将提供一个bAlternateSetting 字段为0x00的接口描述字,这是缺省的设置。替代设置的描述符bInterfaceNumber字段与缺省设备的值是相同的,但是bAlternateSetting的值不同,然后替代设置的描述符的最后5个字节的取值也会根据实际情况而不同。

对于每个bInterfaceNumber,每次只有一个bAlternateSetting 可被激活。替代设置可使主机能够请求带有不同带宽或其他要求和性能的接口。Get Interface请求会取得当前被激活的bAlternateSettingSet Interface请求可选择特定bInterfaceNumber所应当使用的bAlternateSetting

bNumEndpoints这个接口使用的端点数量(不包括端点0)。如果这个值为0,那么这个接口使用默认的控制管道(Control Pipe),既只使用端点0。

bInterfaceClass类码(Class code),这个类码(Class code)由USB-IF分配。除了用于由接口规定的设备外,其他与设备描述符(Device Descriptor)中bDeviceClass 字段相同。

值0x00为保留值,从0x01 – 0xFE的值保留给USB定义的类,0xFF作为厂商定义的类。

Class CodeDescription
0x00保留(Reserved)
0x01音频(Audio)
0x02通讯设备:通讯接口(Communications device: communication interface)
0x03人机接口设备(Human interface device)
0x05物理(Physical)
0x06图像(Image)
bInterfaceSubclass = 0x01
bInterfaceProtocol = 0x01: Imaging device
0x07打印机(Printer)
0x08大容量存储(Mass storage)
0x09集线器(Hub) (还必须在设备描述符(device descriptor)中声明 )
0x0A通讯设备:数据接口(Communications device: data interface)
0x0B智能卡(Smart Card)
0x0D内容安全(Content Security)
bInterfaceSubclass = 0x01
bInterfaceProtocol = 0x01: Content security device
0x0E视频(Video)
0x0F个人健康设备(Personal healthcare device) (can instead be declared at the device level)
0x10Audio/Video (AV):
bInterfaceSubclass = 0x01
bInterfaceProtocol = 0x00: AVControl Interface
bInterfaceSubclass = 0x02
bInterfaceProtocol = 0x00: AVData Video Streaming Interface
bInterfaceSubclass = 0x03
bInterfaceProtocol = 0x00: AVData Audio Streaming Interface
0xDC诊断设备Diagnostic device (can instead be declared at the device level)
bInterfaceSubclass= 0x01
bInterfaceProtocol = 0x01: USB2 compliance device
0xE0无线控制器Wireless controller
bInterfaceSubclass = 0x01
bInterfaceProtocol = 0x01: Bluetooth programming interface (should also be
declared at the device level)
bInterfaceProtocol = 0x02: UWB Radio control interface (Wireless USB)
bInterfaceProtocol = 0x03: RNDIS
bInterfaceProtocol = 0x04: Bluetooth AMP controller (should also be declared at the device level)
bInterfaceSubclass = 0x02: Host and device wire adapters (Wireless USB)
bInterfaceProtocol = 0x01: Host wire adapter control/data interface
bInterfaceProtocol = 0x02: Device wire adapter control/data interface
bInterfaceProtocol = 0x03: Device wire adapter isochronous interface
0xEF各种各样的Miscellaneous
bInterfaceSubclass = 0x01
bInterfaceProtocol = 0x01: active sync
bInterfaceProtocol = 0x02: Palm sync
bInterfaceSubclass = 0x02
bInterfaceProtocol = 0x01: Interface Association Descriptor
bInterfaceProtocol = 0x02: Wire Adapter Multifunction Peripheral programming
interface
bInterfaceSubclass = 0x03: Cable based association framework (Wireless USB)
bInterfaceProtocol = 0x01: Cable based association framework device
bInterfaceSubclass = 0x04
bInterfaceProtocol = 0x01: RNDIS over Ethernet
bInterfaceProtocol = 0x02: RNDIS over WiFi
bInterfaceProtocol = 0x03: RNDIS over Maxim
bInterfaceProtocol = 0x04: RNDIS over WWAN
bInterfaceProtocol = 0x05: RNDIS for Raw IPv4
bInterfaceProtocol = 0x06: RNDIS for Raw IPv6
bInterfaceProtocol = 0x07: RNDIS for GPRS
bInterfaceSubclass = 0x05: Machine vision device (USB3 Vision)
bInterfaceProtocol = 0x00: USB3 Vision Control Interface
bInterfaceProtocol = 0x01: USB0 Vision Event Interface
bInterfaceProtocol = 0x02: USB3 Vision Streaming Interface
0xFE应用程序专属Application specific
bInterfaceSubclass = 0x01: Device firmware upgrade
bInterfaceSubclass = 0x02: IrDA bridge
bInterfaceSubclass = 0x03: Test and measurement (USBTMC)
bInterfaceProtocol = 0x00: Complies with USBTMC spec
bInterfaceProtocol = 0x01: Complies with USBTMC USB488 subclass
0xFF厂商专属Vendor specific (can instead be declared at the device level)

bInterfaceSubClass子类码(SubClass code),这个子类码(SubClass code)由USB-IF分配。这个值由bInterfaceClass字段所限定。除了带有由接口定义的类的设备之外,此字段与设备描述符(Device Descriptor)中的bDeviceSubClass字段相同。如果bInterfaceClass等于0x00,则bInterfaceSubClass也必须等于0x00。如果bIntefaceClass是0x01–0xFE范围的值,则bInterfaceSubClass等于0x00或等于一个为接口的类定义的码。0xFF表明为厂商定义的子类(SubClass)。

bInterfaceProtocol协议码(Protocol code),这个协议码(Protocol code)由USB-IF分配。这个值由bInterfaceClass字段和bInterfaceSubClass字段所限定。如果接口支持专用的类型请求,这个协议码能识别出由协议类型规范定义、设备使用的协议。如果bInterfaceClass等于0x00,设备在这个接口不使用专门的类协议。如果为0xFF,设备在这个接口使用专用的厂商协议。除了带有由接口定义的类的设备之外,此字段与设备描述符(Device Descriptor)中的bDeviceProtocol字段相同。如果bIntefaceClass 是0x01–0xFE范围的值,则bInterfaceProtocol 等于0x00或等于一个为接口的类定义的码。

iInterface描述这个接口的字符串描述符的索引。如果没有字符串描述符的话则此值为0。

获取接口描述符

主机通过发送Get Descriptor请求,取得配置描述符,接口描述符,端点描述符及其附属描述符。

接口描述符(Interface Descriptor)总是作为配置描述符Configuration Descriptor的一部分返回,不能单独获取。

Get DescriptorValue
bmRequestType0x80
  bmRequestType.Recipient0b00000(Device)
  bmRequestType.Type0b00(Standard)
  bmRequestType.Direction0b1(Device-to-Host)
bRequest0x06(Get Descriptor)
wValue0x0200(Configuration #0)
wIndex0x0000
wLength0x???(wTotalLength)

下面举例说明一个获取接口描述符的过程。
获取一个键盘的接口描述符,该键盘的接口描述符如下所示:
0

使用USB分析仪抓包可以看到,获取配置描述符Configuration Descriptor,而接口描述符Interface Descriptor就包含在其中:
1

接口描述符Interface Descriptor的获取过程如下:
2

再展开看到详细的过程:
3

接下来详细看整个获取过程的各个字段含义:
4

一开始的setup包过程:
5

setup data:
6

IN包过程
7

OUT包过程:
8

完成整个接口描述符Interface Descriptor的获取。


描述符示例

这里举例一个带有该描述符的示例设备,这是一个键盘Keyboard的完整描述符信息。

Device Descriptor

OffsetFieldSizeValueDescription
0bLength10x12
1bDescriptorType10x01DEVICE
2bcdUSB20x01101.10
4bDeviceClass10x00
5bDeviceSubClass10x00
6bDeviceProtocol10x00
7bMaxPacketSize010x08
8idVendor20x046d
10idProduct20xc31c
12bcdDevice20x640064.00
14iManufacturer10x01
15iProduct10x02
16iSerialNumber10x00
17bNumConfigurations10x01

Configuration Descriptor

OffsetFieldSizeValueDescription
0bLength10x09
1bDescriptorType10x02CONFIGURATION
2wTotalLength20x003b
4bNumInterfaces10x02
5bConfigurationValue10x01
6iConfiguration10x03
7bmAttributes10xa0
8bMaxPower10x2d

Interface Descriptor

OffsetFieldSizeValueDescription
0bLength10x09
1bDescriptorType10x04INTERFACE
2bInterfaceNumber10x00
3bAlternateSetting10x00
4bNumEndpoints10x01
5bInterfaceClass10x03Human Interface Device
6bInterfaceSubClass10x01Boot Interface
7bInterfaceProtocol10x01Keyboard
8iInterface10x02

HID Descriptor

OffsetFieldSizeValueDescription
0bLength10x09
1bDescriptorType10x21HID
2bcdHID20x01101.10
4bCountryCode10x00
5bNumDescriptors10x01
6bDescriptorType10x22REPORT
7wDescriptorLength20x0041

Endpoint Descriptor

OffsetFieldSizeValueDescription
0bLength10x07
1bDescriptorType10x05ENDPOINT
2bEndpointAddress10x811 IN
3bmAttributes10x03Interrupt
4wMaxPacketSize20x0008
6bInterval10x0a

Interface Descriptor

OffsetFieldSizeValueDescription
0bLength10x09
1bDescriptorType10x04INTERFACE
2bInterfaceNumber10x01
3bAlternateSetting10x00
4bNumEndpoints10x01
5bInterfaceClass10x03Human Interface Device
6bInterfaceSubClass10x00
7bInterfaceProtocol10x00
8iInterface10x02

HID Descriptor

OffsetFieldSizeValueDescription
0bLength10x09
1bDescriptorType10x21HID
2bcdHID20x01101.10
4bCountryCode10x00
5bNumDescriptors10x01
6bDescriptorType10x22REPORT
7wDescriptorLength20x009f

Endpoint Descriptor

OffsetFieldSizeValueDescription
0bLength10x07
1bDescriptorType10x05ENDPOINT
2bEndpointAddress10x822 IN
3bmAttributes10x03Interrupt
4wMaxPacketSize20x0004
6bInterval10xff

附录

USB1.x/USB 2.0/USB 3.x Interface Descriptor

OffsetFieldSizeValueDescription
0bLength1NumberSize of this descriptor in bytes
1bDescriptorType1ConstantINTERFACE Descriptor Type
2bInterfaceNumber1NumberNumber of this interface. Zero-based value identifying the index in the array of concurrent interfaces supported by this configuration.
3bAlternateSetting1NumberValue used to select this alternate setting for the interface identified in the prior field.
4bNumEndpoints1NumberNumber of endpoints used by this interface (excluding endpoint zero). If this value is zero, this interface only uses the Default Control Pipe.
5bInterfaceClass1ClassClass code (assigned by the USB-IF).
A value of zero is reserved for future standardization.
If this field is set to FFH, the interface class is vendor-specific.
All other values are reserved for assignment by the USB-IF.
6bInterfaceSubClass1SubClassSubclass code (assigned by the USB-IF).
These codes are qualified by the value of the bInterfaceClass field.
If the bInterfaceClass field is reset to zero, this field must also be reset to zero.
If the bInterfaceClass field is not set to FFH, all values are reserved for assignment by the USB-IF.
7bInterfaceProtocol1ProtocolProtocol code (assigned by the USB).
These codes are qualified by the value of the bInterfaceClass and the bInterfaceSubClass fields. If an interface supports class-specific requests, this code identifies the protocols that the device uses as defined by the specification of the device class.
If this field is reset to zero, the device does not use a class-specific protocol on this interface.
If this field is set to FFH, the device uses a vendor-specific protocol for this interface.
8iInterface1IndexIndex of string descriptor describing this interface.

[参考资料]

Universal Serial Bus Specification Revision 1.0

Universal Serial Bus Specification Revision 1.1

Universal Serial Bus Specification Revision 2.0

Universal Serial Bus 3.0 Specification

Universal Serial Bus 3.1 Specification

Universal Serial Bus 3.2 Specification

USB Complete, 5th Edition

USB开发大全

USB2.0与OTG规范及开发指南


本文链接:https://blog.csdn.net/u012028275/article/details/109567196

<think>嗯,用户给了一段关于USB HID描述符配置的代码,需要我帮忙解释或者介绍这部分内容。首先,我需要仔细看看这段代码的结构,了解每个字段的含义。看起来这是一个结构体的初始化,可能是在嵌入式开发或者USB设备驱动中使用的。 用户可能是在学习USB HID设备的开发,遇到了配置描述符的问题,需要理解每个字段的作用。他们可能不太清楚各个描述符之间的关系,比如配置描述符接口描述符、端点描述符等。也有可能是在调试设备时遇到了问题,需要检查描述符是否正确设置。 首先,我要确定这个结构体是用于配置USB HID设备的描述符USB描述符有多个层次,比如配置描述符接口描述符、HID描述符、端点描述符等。每个描述符都有特定的字段需要设置,比如bLength表示描述符的长度,bDescriptorType表示类型,然后是各种属性。 接下来,我需要分部分解析代码。首先是config部分,属于配置描述符,里面包括头部信息、总长度、接口数量、配置值等。然后hid_itf是接口描述符,说明接口编号、端点数量、类、子类、协议等信息。hid_vendor可能是HID描述符,包含HID版本、国家代码、报告描述符长度等。hid_epin是端点描述符,设置端点地址、传输类型、最大包大小和间隔。 需要注意每个字段的取值是否正确。例如,bInterfaceClass设为USB_HID_CLASS,表明这是HID设备。bInterfaceProtocol设置为键盘协议,说明这个设备是键盘类型。端点地址可能是一个输入端点,使用中断传输方式。 用户可能想知道每个字段的具体作用,以及如何根据需求修改这些值。例如,如果需要修改报告描述符的长度,或者调整端点的轮询间隔,应该改哪里。还有可能想知道各个描述符之间的关系,比如配置描述符包含接口描述符接口描述符又关联到端点和HID描述符。 另外,用户提到后面的语言ID描述符注释,可能在问是否需要配置这部分,或者是否有遗漏。需要指出语言ID描述符通常是在设备描述符中指定,但这段代码可能没有包含,可能需要另外定义。 最后,要确保解释清晰,帮助用户逐步理解每个部分,并确保他们能够正确配置自己的USB HID设备。可能还需要提醒常见的错误点,比如描述符长度计算错误,端点地址方向设置不对等。</think>这是一个USB HID设备配置描述符的结构体初始化代码,主要用于定义USB人机交互设备(如键盘)的通信特征。以下是对各部分的逐层解析: --- ### 一、配置描述符(Configuration Descriptor) ```c .config = { .header = { .bLength = sizeof(usb_desc_config), .bDescriptorType = USB_DESCTYPE_CONFIG }, .wTotalLength = USB_HID_CONFIG_DESC_LEN, // 配置描述符总长度 .bNumInterfaces = 0x01U, // 包含1个接口 .bConfigurationValue = 0x01U, // 配置编号为1 .bmAttributes = 0xA0U, // 总线供电,支持远程唤醒 .bMaxPower = 0x32U // 最大功耗 100mA (0x32*2mA=100mA) } ``` - **关键字段**: - `bmAttributes=0xA0`: 二进制 `10100000`,最低位为1表示总线供电,bit6为1表示支持远程唤醒 - `bMaxPower=0x32`: 十进制50,计算功耗为 $50 \times 2\text{mA} = 100\text{mA}$ --- ### 二、接口描述符Interface Descriptor) ```c .hid_itf = { .header = { .bLength = sizeof(usb_desc_itf), .bDescriptorType = USB_DESCTYPE_ITF }, .bInterfaceNumber = 0x00U, // 接口0 .bNumEndpoints = 0x01U, // 1个端点 .bInterfaceClass = USB_HID_CLASS, // HID类设备 .bInterfaceSubClass = USB_HID_SUBCLASS_BOOT_ITF, // 支持BIOS级启动协议 .bInterfaceProtocol = USB_HID_PROTOCOL_KEYBOARD // 键盘协议 } ``` - **协议特性**: - 支持`Boot Protocol`,可在BIOS/UEFI层直接识别为键盘 - 使用`Keyboard`协议,符合USB HID规范中的键盘设备定义 --- ### 三、HID描述符(HID-Specific Descriptor) ```c .hid_vendor = { .header = { .bLength = sizeof(usb_desc_hid), .bDescriptorType = USB_DESCTYPE_HID }, .bcdHID = 0x0111U, // HID规范版本1.11 .bNumDescriptors = 0x01U, // 包含1个报告描述符 .bDescriptorType = USB_DESCTYPE_REPORT, .wDescriptorLength = USB_HID_REPORT_DESC_LEN // 报告描述符长度 } ``` - **版本信息**: - `bcdHID=0x0111` 表示遵循HID 1.11规范 - 报告描述符长度需要与实际的键盘报告描述符长度一致 --- ### 四、端点描述符(Endpoint Descriptor) ```c .hid_epin = { .header = { .bLength = sizeof(usb_desc_ep), .bDescriptorType = USB_DESCTYPE_EP }, .bEndpointAddress = HID_IN_EP, // 输入端点地址(通常为0x81) .bmAttributes = USB_EP_ATTR_INT, // 中断传输模式 .wMaxPacketSize = HID_IN_PACKET, // 最大包大小(例如8字节) .bInterval = 0x40U // 轮询间隔64ms } ``` - **传输特性**: - 中断传输(Interrupt Transfer)适合HID设备的实时性要求 - 典型键盘输入包大小为8字节(包含按键码和修饰键状态) --- ### 五、常见问题 1. **为什么需要多个描述符?** USB设备通过分层描述符定义功能: - 配置描述符接口描述符 → HID描述符 → 端点描述符 2. **如何扩展功能?** 修改`bNumInterfaces`并添加新的接口描述符,例如同时实现键盘+鼠标功能 3. **描述符长度计算:** `wTotalLength`需包含所有子描述符的长度总和,错误计算会导致枚举失败 --- ### 六、语言ID描述符补充 代码末尾的注释`/* USB language ID Descriptor */`提示需要定义语言描述符,通常如下: ```c { 0x04, USB_DESCTYPE_STRING, 0x09, 0x04 } // 0x0409表示英语(美国) ``` 这个结构体完整定义了USB HID键盘设备的标准通信参数,实际开发中需配合报告描述符共同工作。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值