文章目录
- 1、声明
- 2、标准设备请求
- 2.1、SETUP事务的数据格式
- 2.2、标准设备请求
- 2.3、设备/配置/接口/端点
- 3、描述符
- 3.1、设备描述符
- 3.2、配置描述符
- 3.3、接口描述符
- 3.4、端点描述符
- 3.5、示例
- 4、设备枚举过程
1、声明
本文是在学习韦东山《驱动大全》USB子系统时,为梳理知识点和自己回看而记录,全部内容高度复制粘贴。
韦老师的《驱动大全》:商品详情
其对应的讲义资料:https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
2、标准设备请求
我们先从setup事务的数据格式来引入设备/配置/接口/端点的概念。
2.1、SETUP事务的数据格式
Host使用控制传输来识别设备、设置设备地址、启动设备的某些特性,对于控制传输,它首先发出"setup事务",如下:
在"setup事务"中,
- SETUP令牌包:用来通知设备,“要开始传输了”
- DATA0数据包:它含有固定的格式,用来告诉设备"是读还是写"、“读什么”、“写什么”
Host通过DATA0数据包发送8字节数据给设备,它的格式如下图所示:
2.2、标准设备请求
控制传输的建立事务中,可以使用下列格式的数据:
上表中各个"宏"取值如下:
2.3、设备/配置/接口/端点
在SETUP事务的数据里,表示了要访问的是什么:Device?Interface?Endpoint?
对于一个USB设备,它可以多种配置(Configuration)。比如4G上网卡就有2种配置:U盘、上网卡。第1次把4G上网卡插入电脑时,它是一个U盘,可以按照里面的程序。装好程序后,把它再次插入电脑,它就是一个上网卡。驱动程序可以选择让它工作于哪种配置,同一时间只能有一种配置。大多数的USB设备只有一种配置。
一个配置下,可以有多个接口(Interface),接口等同于功能(Function)。比如USB耳机有两个接口(功能):声音收发、按键控制。
一个接口,可能有多个设置(Setting),比如默认设置下它使用较低的带宽,可以选择其他设置以使用更高带宽。
一个接口,由一个或多个端点(Endpoint)组成。端点0属于整个设备的,端点0是双向的。接口还可以有其他端点,这些端点是单向的,要么是批量(Bulk)端点、要么是中断(Interrupt)端点、要么是同步(Isochronous)端点。
3、描述符
怎么描述设备、配置、接口、端点?使用描述符(Descriptors),有设备描述符、配置描述符、接口描述符、端点描述符。所谓描述符,就是一些格式化的数据,用来描述信息。
一个USB设备:
- 只有一个设备描述符:用来表示设备的ID、它有多少个配置、它的端点0一次最大能传输多少字节数据。
- 可能有多个配置描述符:用来表示它有多少个接口、供电方式、最大电流。
- 一个配置描述符下面,可能有多个接口描述符:用来表示它是哪类接口、有几个设置(Setting)、有几个端点。
- 一个接口描述符符下面,可能有多个端点描述符:用来表示端点号、方向(IN/OUT)、类型(批量/中断/同步)。
还有一些字符串描述符(String descriptors),它用可读的文字来描述设备,是可选的。
3.1、设备描述符
3.2、配置描述符
3.3、接口描述符
3.4、端点描述符
3.5、示例
在Ubuntu中可以执行<font style="color:rgb(51, 51, 51);background-color:rgb(243, 244, 244);">lsusb -v</font>
查看USB设备的描述符信息:
book@100ask:~$ sudo lsusb -v
[sudo] password for book:Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Device Descriptor:bLength 18bDescriptorType 1bcdUSB 2.00bDeviceClass 9 HubbDeviceSubClass 0 UnusedbDeviceProtocol 0 Full speed (or root) hubbMaxPacketSize0 64idVendor 0x1d6b Linux FoundationidProduct 0x0002 2.0 root hubbcdDevice 5.04iManufacturer 3 Linux 5.4.0-124-generic ehci_hcdiProduct 2 EHCI Host ControlleriSerial 1 0000:02:03.0bNumConfigurations 1Configuration Descriptor:bLength 9bDescriptorType 2wTotalLength 25bNumInterfaces 1bConfigurationValue 1iConfiguration 0bmAttributes 0xe0Self PoweredRemote WakeupMaxPower 0mAInterface Descriptor:bLength 9bDescriptorType 4bInterfaceNumber 0bAlternateSetting 0bNumEndpoints 1bInterfaceClass 9 HubbInterfaceSubClass 0 UnusedbInterfaceProtocol 0 Full speed (or root) hubiInterface 0Endpoint Descriptor:bLength 7bDescriptorType 5bEndpointAddress 0x81 EP 1 INbmAttributes 3Transfer Type InterruptSynch Type NoneUsage Type DatawMaxPacketSize 0x0004 1x 4 bytesbInterval 12
Hub Descriptor:bLength 9bDescriptorType 41nNbrPorts 6wHubCharacteristic 0x000aNo power switching (usb 1.0)Per-port overcurrent protectionbPwrOn2PwrGood 10 * 2 milli secondsbHubContrCurrent 0 milli AmpereDeviceRemovable 0x00PortPwrCtrlMask 0xffHub Port Status:Port 1: 0000.0100 powerPort 2: 0000.0100 powerPort 3: 0000.0100 powerPort 4: 0000.0100 powerPort 5: 0000.0100 powerPort 6: 0000.0100 power
Device Status: 0x0001Self PoweredBus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
Device Descriptor:bLength 18bDescriptorType 1bcdUSB 1.10bDeviceClass 9 HubbDeviceSubClass 0 UnusedbDeviceProtocol 0 Full speed (or root) hubbMaxPacketSize0 8idVendor 0x0e0f VMware, Inc.idProduct 0x0002 Virtual USB HubbcdDevice 1.00iManufacturer 1 VMware, Inc.iProduct 2 VMware Virtual USB HubiSerial 0bNumConfigurations 1Configuration Descriptor:bLength 9bDescriptorType 2wTotalLength 25bNumInterfaces 1bConfigurationValue 1iConfiguration 1 VMware, Inc.bmAttributes 0xe0Self PoweredRemote WakeupMaxPower 0mAInterface Descriptor:bLength 9bDescriptorType 4bInterfaceNumber 0bAlternateSetting 0bNumEndpoints 1bInterfaceClass 9 HubbInterfaceSubClass 0 UnusedbInterfaceProtocol 0 Full speed (or root) hubiInterface 1 VMware, Inc.Endpoint Descriptor:bLength 7bDescriptorType 5bEndpointAddress 0x81 EP 1 INbmAttributes 3Transfer Type InterruptSynch Type NoneUsage Type DatawMaxPacketSize 0x0001 1x 1 bytesbInterval 255
Hub Descriptor:bLength 9bDescriptorType 41nNbrPorts 7wHubCharacteristic 0x0009Per-port power switchingPer-port overcurrent protectionbPwrOn2PwrGood 50 * 2 milli secondsbHubContrCurrent 100 milli AmpereDeviceRemovable 0x00PortPwrCtrlMask 0xfeHub Port Status:Port 1: 0000.0100 powerPort 2: 0000.0100 powerPort 3: 0000.0100 powerPort 4: 0000.0100 powerPort 5: 0000.0100 powerPort 6: 0000.0100 powerPort 7: 0000.0100 power
Device Status: 0x2909Self PoweredBus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
Device Descriptor:bLength 18bDescriptorType 1bcdUSB 1.10bDeviceClass 0 (Defined at Interface level)bDeviceSubClass 0bDeviceProtocol 0bMaxPacketSize0 8idVendor 0x0e0f VMware, Inc.idProduct 0x0003 Virtual MousebcdDevice 1.03iManufacturer 1 VMwareiProduct 2 VMware Virtual USB MouseiSerial 0bNumConfigurations 1Configuration Descriptor:bLength 9bDescriptorType 2wTotalLength 34bNumInterfaces 1bConfigurationValue 1iConfiguration 1 VMwarebmAttributes 0xc0Self PoweredMaxPower 0mAInterface Descriptor:bLength 9bDescriptorType 4bInterfaceNumber 0bAlternateSetting 0bNumEndpoints 1bInterfaceClass 3 Human Interface DevicebInterfaceSubClass 1 Boot Interface SubclassbInterfaceProtocol 2 MouseiInterface 1 VMwareHID Device Descriptor:bLength 9bDescriptorType 33bcdHID 1.10bCountryCode 0 Not supportedbNumDescriptors 1bDescriptorType 34 ReportwDescriptorLength 46Report Descriptors:** UNAVAILABLE **Endpoint Descriptor:bLength 7bDescriptorType 5bEndpointAddress 0x81 EP 1 INbmAttributes 3Transfer Type InterruptSynch Type NoneUsage Type DatawMaxPacketSize 0x0008 1x 8 bytesbInterval 1
Device Status: 0x0001Self PoweredBus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Device Descriptor:bLength 18bDescriptorType 1bcdUSB 1.10bDeviceClass 9 HubbDeviceSubClass 0 UnusedbDeviceProtocol 0 Full speed (or root) hubbMaxPacketSize0 64idVendor 0x1d6b Linux FoundationidProduct 0x0001 1.1 root hubbcdDevice 5.04iManufacturer 3 Linux 5.4.0-124-generic uhci_hcdiProduct 2 UHCI Host ControlleriSerial 1 0000:02:00.0bNumConfigurations 1Configuration Descriptor:bLength 9bDescriptorType 2wTotalLength 25bNumInterfaces 1bConfigurationValue 1iConfiguration 0bmAttributes 0xe0Self PoweredRemote WakeupMaxPower 0mAInterface Descriptor:bLength 9bDescriptorType 4bInterfaceNumber 0bAlternateSetting 0bNumEndpoints 1bInterfaceClass 9 HubbInterfaceSubClass 0 UnusedbInterfaceProtocol 0 Full speed (or root) hubiInterface 0Endpoint Descriptor:bLength 7bDescriptorType 5bEndpointAddress 0x81 EP 1 INbmAttributes 3Transfer Type InterruptSynch Type NoneUsage Type DatawMaxPacketSize 0x0002 1x 2 bytesbInterval 255
Hub Descriptor:bLength 9bDescriptorType 41nNbrPorts 2wHubCharacteristic 0x000aNo power switching (usb 1.0)Per-port overcurrent protectionbPwrOn2PwrGood 1 * 2 milli secondsbHubContrCurrent 0 milli AmpereDeviceRemovable 0x00PortPwrCtrlMask 0xffHub Port Status:Port 1: 0000.0103 power enable connectPort 2: 0000.0107 power suspend enable connect
Device Status: 0x0001Self Powered
4、设备枚举过程
使用"usbprotocolsuite"打开,可以看到设备的枚举过程:
- 使用控制传输,读取设备信息(设备描述符):第一次读取时,它只需要得到8字节数据,因为第8个数据表示端点0能传输的最大数据长度。
- Host分配地址给设备,然后把新地址发给设备:
- 使用新地址,重新读取设备描述符,设备描述符长度是18:
- 读取配置描述符:它传入的长度是255,想一次性把当前配置描述符、它下面的接口描述符、端点描述符全部读出来:
- 读取字符描述符: