文章目录
参考
usb 官方文档
USB 2.0 Specification
Linux USB Guide
目的
学习到 uvc 驱动的编写,被 usb 的各种术语搞得晕头转向,结构体一层一层又一层,向无头苍蝇一般迷失在 C 语言混乱的结构体定义当中。
“ 等等,刚才是哪个结构包含哪个结构来着? ”
“ 这玩意是指针还是数组?”
“ 这个变量的意思是啥?”
……
所以,还是先好好搞懂 usb 的协议吧。
综述
USB 主要由四个部分组成:
- USB Physical Device: A piece of hardware on the end of a USB cable that performs some useful end user function.
- Client Software: Software that executes on the host, corresponding to a USB device. This client software is typically supplied with the operating system or provided along with the USB device.
- USB System Software: Software that supports the USB in a particular operating system. The USB System Software is typically supplied with the operating system, independently of particular USB devices or client software.
- USB Host Controller (Host Side Bus Interface): The hardware and software that allows USB devices to be attached to a host.
下面来看看 USB 协议的层次图,和 TCP/IP 协议看起来比较相似。
内核已经实现了 USB System SW 模块,该模块与 USB Logical Device 模块进行逻辑上的通信,所以配置、接口、端点等都是纯软件协议的内容。
USB 物理设备为主机提供额外的功能,这些 USB 物理设备提供的功能都是不一样的,比如摄像头、移动硬盘等。但是所有 USB 逻辑设备都提供了相同的基本接口,所以可以使用相同的管理方式来管理这些 USB 设备。
详细介绍
A USB logical device appears to the USB system as a collection of endpoints. Endpoints are grouped into endpoint sets that implement an interface. Interfaces are views to the function.
一个 USB 的逻辑设备是一系列端点的集合。通过对端点进行分组来实现一个接口。物理设备功能的实现依靠接口来实现。
USB Device Framework
一个 USB 设备可以被分为三层:
- 最底层为物理总线接口层,来传输和接收数据。
- 中间层对物理总线接口与各种端点之间的路由数据进行处理。一个端点是数据的最终消费者或提供者。
- 最上层是功能提供者,比如鼠标。
标准 USB 描述符定义
1. 设备描述符 (struct usb_device_descriptor)
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | Number | 描述符字节数 |
1 | bDescriptorType | 1 | Constant | 设备描述符的类型 |
2 | bcdUSB | 2 | BCD | USB协议版本号 |
4 | bDeviceClass | 1 | Class | 设备类型编号 (详见 USB-IF) |
5 | bDeviceSubClass | 1 | SubClass | 设备子类型编号 (详见 USB-IF) |
6 | bDeviceProtocol | 1 | Protocol | 协议编号 (详见 USB-IF) |
7 | bMaxPacketSize0 | 1 | Number | 端点0数据包最大字节数 (只有 8,16,32,64 有效) |
8 | idVendor | 2 | ID | 供应商 ID(详见 USB-IF) |
10 | idProduct | 2 | ID | 产品 ID(由供应商定义) |
12 | bcdDevice | 2 | BCD | 设备版本号 |
14 | iManufacturer | 1 | Index | 制造商描述字符串索引值 |
15 | iProduct | 1 | Index | 产品描述字符串索引值 |
16 | iSerialNumber | 1 | Index | 设备序列号描述字符串索引值 |
17 | bNumConfigurations | 1 | Number | 设备配置的数量 (有多少个配置) |
2. 配置描述符(struct usb_config_descriptor)
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | Number | 配置描述符字节数 |
1 | bDescriptorType | 1 | Constant | 配置描述符的类型 |
2 | wTotalLength | 2 | Number | 此配置的数据总长度 (配置、接口、端点和特定类或供应商的组合长度) |
4 | bNumInterfaces | 1 | Number | 此配置包含的接口数量 |
5 | bConfigurationValue | 1 | Number | 将该值作为参数传入 SetConfiguration() 来设置此配置 |
6 | iConfiguration | 1 | Index | 此配置描述符的描述字符串索引值 |
7 | bmAttributes | 1 | Bitmap | 配置特性: D7: Reserved (set to one) D6: Self-powered D5: Remote Wakeup D4…0: Reserved (reset to zero) |
8 | bMaxPower | 1 | mA | 此配置中 USB 设备使用的最大功耗 (单位为2mA,如50 = 100mA) |
3. 联合接口描述符(struct usb_interface_assoc_descriptor)
联合接口描述符,将多个接口联合到相同的功能上。一个联合接口包括多个接口,和这些接口的所有备用设置接口。
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | Number | 描述符字节数 |
1 | bDescriptorType | 1 | Constant | 描述符的类型 |
2 | bFirstInterface | 1 | Number | 关联到此功能上的第一个接口号 |
3 | bInterfaceCount | 1 | Number | 关联到此功能上的接口数量 |
4 | bFunctionClass | 1 | Class | 类型编号 (详见 USB-IF) |
5 | bFunctionSubClass | 1 | SubClass | 子类型编号 (详见 USB-IF) |
6 | bFunctionProtocol | 1 | Protocol | 功能协议编号 (详见 USB-IF) |
7 | iFunction | 1 | Index | 该功能描述字符串的索引值 |
4. 接口描述符(struct usb_interface_descriptor)
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | Number | 接口描述符字节数 |
1 | bDescriptorType | 1 | Constant | 接口描述符的类型 |
2 | bInterfaceNumber | 1 | Number | 该接口的编号1 |
3 | bAlternateSetting | 1 | Number | 该接口的备用设置的编号1 |
4 | bNumEndpoints | 1 | Number | 端点的数量 (除掉端点0) 如果该值为0,表示该接口只使用默认控制管道 |
5 | bInterfaceClass | 1 | Class | 接口类型编号 (详见 USB-IF) |
6 | bInterfaceSubClass | 1 | SubClass | 接口子类型编号 (详见 USB-IF) |
7 | bInterfaceProtocol | 1 | Protocol | 协议编号 (详见 USB-IF) |
8 | iInterface | 1 | Index | 该接口描述字符串的索引值 |
5. 端点描述符(struct usb_endpoint_descriptor)
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | Number | 端点描述符字节数 |
1 | bDescriptorType | 1 | Constant | 端点描述符类型 |
2 | bEndpointAddress | 1 | Endpoint | USB 设备上端点的地址 Bit 3…0: The endpoint number Bit 6…4: Reserved, reset to zero Bit 7: Direction, 0 = OUT endpoint 1 = IN endpoint |
3 | bmAttributes | 1 | Bitmap | 该字段描述端点的特性,当它被bConfigurationValue设置时 Bits 1…0: Transfer Type 00 = Control 01 = Isochronous 10 = Bulk 11 = Interrupt If not an isochronous endpoint, bits 5…2 are reserved Bits 3…2: Synchronization Type 00 = No Synchronization 01 = Asynchronous 10 = Adaptive 11 = Synchronous Bits 5…4: Usage Type 00 = Data endpoint 01 = Feedback endpoint 10 = Implicit feedback Data endpoint 11 = Reserved |
4 | wMaxPacketSize | 2 | Number | 数据包的最大长度 (单位字节) 2 |
6 | bInterval | 1 | Number | 轮询端点的时间间隔 |
结构关系
这是我根据文档描述自己理解画出的结构关系,如果有错误或不恰当的地方请告知我。
- 一个 USB 设备可以有多个配置,但同时只能有一个配置生效。
- 一个配置一般有一个或多个接口。
- 一个接口一般有一个设置或多个备用设置。
- 一个设置一般有零个端点或多个端点,结合(3)所以一个接口一般有零个端点或多个端点。
- 联合接口的定义是 USB 补充的协议,所以应该不是必须的。
- 如果一个设置没有端点,那么它就使用默认的端点 0 控制管道。
实例分析
这里分析一个 USB 摄像头,首先使用下面命令获得完整 USB 描述:
lsusb -v -d 0x1d6b: # 0x1d6b 为供应商编号
仔细分析后获得下图:(没有列出 UVC 协议相关描述)
相似的备用设置只列出了一个。
- 该 USB 设备只有一个配置描述符。
- 该配置有 4 个接口,分为两个联合接口,其中一个为 Video 功能联合接口,起始接口号为 0 ,有 2 个接口,另一个为 Audio 功能联合接口,起始接口号为 2 ,有 2 个接口。
- 接口 0 是 Video Control 接口,只有一个设置,一个端点。
- 接口 1 是 Video Streaming 接口,备用设置 0 只有端点 0 ,备用设置 1 - 6 各有一个端点,端点地址相同,数据包长度不同。
- 接口 2 是 Audio Control 接口,只有一个设置,端点 0 。
- 接口 3 是 Audio Streamin 接口,备用设置 0 只有端点 0 ,备用设置 1 - 2 各有一个端点,端点地址相同,数据传输方式不一样。
USB 设备完整描述符
Bus 002 Device 005: ID 1b3b:2977
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 239 Miscellaneous Device
bDeviceSubClass 2 ?
bDeviceProtocol 1 Interface Association
bMaxPacketSize0 64
idVendor 0x1b3b
idProduct 0x2977
bcdDevice 1.0a
iManufacturer 0
iProduct 0
iSerial 0
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 492
bNumInterfaces 4
bConfigurationValue 1
iConfiguration 0
bmAttributes 0x80
(Bus Powered)
MaxPower 500mA
Interface Association:
bLength 8
bDescriptorType 11
bFirstInterface 0
bInterfaceCount 2
bFunctionClass 14 Video
bFunctionSubClass 3 Video Interface Collection
bFunctionProtocol 0
iFunction 0
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 1
bInterfaceClass 14 Video
bInterfaceSubClass 1 Video Control
bInterfaceProtocol 0
iInterface 0
VideoControl Interface Descriptor:
bLength 13
bDescriptorType 36
bDescriptorSubtype 1 (HEADER)
bcdUVC 1.00
wTotalLength 77
dwClockFrequency 6.000000MHz
bInCollection 1
baInterfaceNr( 0) 1
VideoControl Interface Descriptor:
bLength 18
bDescriptorType 36
bDescriptorSubtype 2 (INPUT_TERMINAL)
bTerminalID 1
wTerminalType 0x0201 Camera Sensor
bAssocTerminal 0
iTerminal 0
wObjectiveFocalLengthMin 1
wObjectiveFocalLengthMax 3
wOcularFocalLength 1
bControlSize 3
bmControls 0x00002a80
Iris (Absolute)
Zoom (Absolute)
PanTilt (Absolute)
Roll (Absolute)
VideoControl Interface Descriptor:
bLength 11
bDescriptorType 36
bDescriptorSubtype 5 (PROCESSING_UNIT)
Warning: Descriptor too short
bUnitID 3
bSourceID 1
wMaxMultiplier 0
bControlSize 2
bmControls 0x0000053f
Brightness
Contrast
Hue
Saturation
Sharpness
Gamma
Backlight Compensation
Power Line Frequency
iProcessing 0
bmVideoStandards 0x1a
NTSC - 525/60
SECAM - 625/50
NTSC - 625/50
VideoControl Interface Descriptor:
bLength 26
bDescriptorType 36
bDescriptorSubtype 6 (EXTENSION_UNIT)
bUnitID 4
guidExtensionCode {3aab9199-efb2-c948-8fe9-8fe3634771d0}
bNumControl 8
bNrPins 1
baSourceID( 0) 3
bControlSize 1
bmControls( 0) 0x0f
iExtension 0
VideoControl Interface Descriptor:
bLength 9
bDescriptorType 36
bDescriptorSubtype 3 (OUTPUT_TERMINAL)
bTerminalID 2
wTerminalType 0x0101 USB Streaming
bAssocTerminal 0
bSourceID 4
iTerminal 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 6
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 0
bNumEndpoints 0
bInterfaceClass 14 Video
bInterfaceSubClass 2 Video Streaming
bInterfaceProtocol 0
iInterface 0
VideoStreaming Interface Descriptor:
bLength 14
bDescriptorType 36
bDescriptorSubtype 1 (INPUT_HEADER)
bNumFormats 1
wTotalLength 121
bEndPointAddress 130
bmInfo 0
bTerminalLink 2
bStillCaptureMethod 0
bTriggerSupport 0
bTriggerUsage 1
bControlSize 1
bmaControls( 0) 11
VideoStreaming Interface Descriptor:
bLength 11
bDescriptorType 36
bDescriptorSubtype 6 (FORMAT_MJPEG)
bFormatIndex 1
bNumFrameDescriptors 3
bFlags 0
Fixed-size samples: No
bDefaultFrameIndex 1
bAspectRatioX 0
bAspectRatioY 0
bmInterlaceFlags 0x00
Interlaced stream or variable: No
Fields per frame: 1 fields
Field 1 first: No
Field pattern: Field 1 only
bCopyProtect 0
VideoStreaming Interface Descriptor:
bLength 30
bDescriptorType 36
bDescriptorSubtype 7 (FRAME_MJPEG)
bFrameIndex 1
bmCapabilities 0x01
Still image supported
wWidth 640
wHeight 480
dwMinBitRate 2304000
dwMaxBitRate 2304000
dwMaxVideoFrameBufferSize 76800
dwDefaultFrameInterval 333333
bFrameIntervalType 1
dwFrameInterval( 0) 333333
VideoStreaming Interface Descriptor:
bLength 30
bDescriptorType 36
bDescriptorSubtype 7 (FRAME_MJPEG)
bFrameIndex 2
bmCapabilities 0x01
Still image supported
wWidth 320
wHeight 240
dwMinBitRate 576000
dwMaxBitRate 576000
dwMaxVideoFrameBufferSize 19200
dwDefaultFrameInterval 333333
bFrameIntervalType 1
dwFrameInterval( 0) 333333
VideoStreaming Interface Descriptor:
bLength 30
bDescriptorType 36
bDescriptorSubtype 7 (FRAME_MJPEG)
bFrameIndex 3
bmCapabilities 0x01
Still image supported
wWidth 160
wHeight 120
dwMinBitRate 144000
dwMaxBitRate 144000
dwMaxVideoFrameBufferSize 4800
dwDefaultFrameInterval 333333
bFrameIntervalType 1
dwFrameInterval( 0) 333333
VideoStreaming Interface Descriptor:
bLength 6
bDescriptorType 36
bDescriptorSubtype 13 (COLORFORMAT)
bColorPrimaries 1 (BT.709,sRGB)
bTransferCharacteristics 1 (BT.709)
bMatrixCoefficients 4 (SMPTE 170M (BT.601))
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 1
bNumEndpoints 1
bInterfaceClass 14 Video
bInterfaceSubClass 2 Video Streaming
bInterfaceProtocol 0
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 5
Transfer Type Isochronous
Synch Type Asynchronous
Usage Type Data
wMaxPacketSize 0x0080 1x 128 bytes
bInterval 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 2
bNumEndpoints 1
bInterfaceClass 14 Video
bInterfaceSubClass 2 Video Streaming
bInterfaceProtocol 0
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 5
Transfer Type Isochronous
Synch Type Asynchronous
Usage Type Data
wMaxPacketSize 0x0100 1x 256 bytes
bInterval 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 3
bNumEndpoints 1
bInterfaceClass 14 Video
bInterfaceSubClass 2 Video Streaming
bInterfaceProtocol 0
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 5
Transfer Type Isochronous
Synch Type Asynchronous
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 4
bNumEndpoints 1
bInterfaceClass 14 Video
bInterfaceSubClass 2 Video Streaming
bInterfaceProtocol 0
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 5
Transfer Type Isochronous
Synch Type Asynchronous
Usage Type Data
wMaxPacketSize 0x0258 1x 600 bytes
bInterval 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 5
bNumEndpoints 1
bInterfaceClass 14 Video
bInterfaceSubClass 2 Video Streaming
bInterfaceProtocol 0
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 5
Transfer Type Isochronous
Synch Type Asynchronous
Usage Type Data
wMaxPacketSize 0x0320 1x 800 bytes
bInterval 1
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 6
bNumEndpoints 1
bInterfaceClass 14 Video
bInterfaceSubClass 2 Video Streaming
bInterfaceProtocol 0
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 5
Transfer Type Isochronous
Synch Type Asynchronous
Usage Type Data
wMaxPacketSize 0x03bc 1x 956 bytes
bInterval 1
Interface Association:
bLength 8
bDescriptorType 11
bFirstInterface 2
bInterfaceCount 2
bFunctionClass 1 Audio
bFunctionSubClass 0
bFunctionProtocol 0
iFunction 0
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 2
bAlternateSetting 0
bNumEndpoints 0
bInterfaceClass 1 Audio
bInterfaceSubClass 1 Control Device
bInterfaceProtocol 0
iInterface 0
AudioControl Interface Descriptor:
bLength 9
bDescriptorType 36
bDescriptorSubtype 1 (HEADER)
bcdADC 1.00
wTotalLength 39
bInCollection 1
baInterfaceNr( 0) 3
AudioControl Interface Descriptor:
bLength 12
bDescriptorType 36
bDescriptorSubtype 2 (INPUT_TERMINAL)
bTerminalID 1
wTerminalType 0x0201 Microphone
bAssocTerminal 0
bNrChannels 1
wChannelConfig 0x0000
iChannelNames 0
iTerminal 0
AudioControl Interface Descriptor:
bLength 9
bDescriptorType 36
bDescriptorSubtype 6 (FEATURE_UNIT)
bUnitID 3
bSourceID 1
bControlSize 2
bmaControls( 0) 0x03
bmaControls( 0) 0x00
Mute
Volume
iFeature 0
AudioControl Interface Descriptor:
bLength 9
bDescriptorType 36
bDescriptorSubtype 3 (OUTPUT_TERMINAL)
bTerminalID 2
wTerminalType 0x0101 USB Streaming
bAssocTerminal 0
bSourceID 3
iTerminal 0
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 3
bAlternateSetting 0
bNumEndpoints 0
bInterfaceClass 1 Audio
bInterfaceSubClass 2 Streaming
bInterfaceProtocol 0
iInterface 0
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 3
bAlternateSetting 1
bNumEndpoints 1
bInterfaceClass 1 Audio
bInterfaceSubClass 2 Streaming
bInterfaceProtocol 0
iInterface 0
AudioStreaming Interface Descriptor:
bLength 7
bDescriptorType 36
bDescriptorSubtype 1 (AS_GENERAL)
bTerminalLink 2
bDelay 1 frames
wFormatTag 1 PCM
AudioStreaming Interface Descriptor:
bLength 11
bDescriptorType 36
bDescriptorSubtype 2 (FORMAT_TYPE)
bFormatType 1 (FORMAT_TYPE_I)
bNrChannels 1
bSubframeSize 2
bBitResolution 16
bSamFreqType 1 Discrete
tSamFreq[ 0] 16000
Endpoint Descriptor:
bLength 9
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 1
Transfer Type Isochronous
Synch Type None
Usage Type Data
wMaxPacketSize 0x0020 1x 32 bytes
bInterval 1
bRefresh 0
bSynchAddress 0
AudioControl Endpoint Descriptor:
bLength 7
bDescriptorType 37
bDescriptorSubtype 1 (EP_GENERAL)
bmAttributes 0x00
bLockDelayUnits 0 Undefined
wLockDelay 0 Undefined
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 3
bAlternateSetting 2
bNumEndpoints 1
bInterfaceClass 1 Audio
bInterfaceSubClass 2 Streaming
bInterfaceProtocol 0
iInterface 0
AudioStreaming Interface Descriptor:
bLength 7
bDescriptorType 36
bDescriptorSubtype 1 (AS_GENERAL)
bTerminalLink 2
bDelay 1 frames
wFormatTag 1 PCM
AudioStreaming Interface Descriptor:
bLength 11
bDescriptorType 36
bDescriptorSubtype 2 (FORMAT_TYPE)
bFormatType 1 (FORMAT_TYPE_I)
bNrChannels 1
bSubframeSize 2
bBitResolution 16
bSamFreqType 1 Discrete
tSamFreq[ 0] 8000
Endpoint Descriptor:
bLength 9
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 5
Transfer Type Isochronous
Synch Type Asynchronous
Usage Type Data
wMaxPacketSize 0x0010 1x 16 bytes
bInterval 1
bRefresh 0
bSynchAddress 0
AudioControl Endpoint Descriptor:
bLength 7
bDescriptorType 37
bDescriptorSubtype 1 (EP_GENERAL)
bmAttributes 0x00
bLockDelayUnits 0 Undefined
wLockDelay 0 Undefined
bInterfaceNumber : Zero-based value identifying the index in the array of concurrent interfaces supported by this configuration.
(1) 在USB协议中同一个配置中的不同接口不能共用端点。
(2) 同一个接口可以有多个接口描述符,用bAlternateSetting来识别。
(3) 同一个接口的不同接口描述符,可以拥有相同的端点。
(4) 同一个接口中的不同描述符可以像其他接口描述符一样写在配置描述符里 ↩︎ ↩︎Maximum packet size this endpoint is capable of sending or receiving when this configuration is selected.
For isochronous endpoints, this value is used to reserve the bus time in the schedule, required for the
per-(micro)frame data payloads. The pipe may, on an ongoing basis, actually use less bandwidth than that reserved. The device reports, if necessary, the actual bandwidth used via its normal, non-USB defined mechanisms.
For all endpoints, bits 10…0 specify the maximum packet size (in bytes).
For high-speed isochronous and interrupt endpoints:
Bits 12…11 specify the number of additional transaction opportunities per microframe:
00 = None (1 transaction per microframe)
01 = 1 additional (2 per microframe)
10 = 2 additional (3 per microframe)
11 = Reserved
Bits 15…13 are reserved and must be set to zero.
Refer to Chapter 5 for more information. ↩︎