USB子系统学习(三)USB描述符和设备枚举过程

server/2025/2/8 2:37:30/

文章目录

  • 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,想一次性把当前配置描述符、它下面的接口描述符、端点描述符全部读出来:

  • 读取字符描述符:


http://www.ppmy.cn/server/165827.html

相关文章

Pixflow - CL-DJI Drone LUTs 120个大疆Drone无人机相机航拍电影级镜头LUT调色预设

120组电影质感DJI大疆无人机航拍视频LOG&Rec 709还原颜色分级调色LUTs预设包Pixflow – CL-DJI Drone LUTs 使用基于城市外观和 DJI 无人机镜头的最佳 Drone Luts 颜色预设来提升您的视频。 120 个出色的颜色分级 LUTS&#xff0c;您可以将其与任何无人机视频素材一起使用…

深度学习篇---深度学习中的超参数张量转换模型训练

文章目录 前言第一部分&#xff1a;深度学习中的超参数1. 学习率&#xff08;Learning Rate&#xff09;定义重要性常见设置 2. 批处理大小&#xff08;Batch Size&#xff09;定义重要性常见设置 3. 迭代次数&#xff08;Number of Epochs&#xff09;定义重要性常见设置 4. 优…

【机器学习】K近邻算法的实现

K近邻算法 一、k近邻&#xff08;k-NN&#xff09;算法介绍二、k近邻算法原理三、k近邻算法实现1. 实现思路2. 在jupyter中实现过程 四、k近邻算法的封装与优点1. KNN代码封装成函数2. 在jupyter中调用并执行3. 封装的好处 一、k近邻&#xff08;k-NN&#xff09;算法介绍 k近…

[转]Java面试近一个月的面试总结

本文是在学习中的总结&#xff0c;欢迎转载但请注明出处&#xff1a;http://blog.csdn.net/pistolove/article/details/46753275 前言 打算换个工作&#xff0c;近一个月面试了不少的公司&#xff0c;下面将一些面试经验和思考分享给大家。另外校招也快要开始了&#xff0c;为…

5 计算机网络

5 计算机网络 5.1 OSI/RM七层模型 5.2 TCP/IP协议簇 5.2.1:常见协议基础 一、 TCP是可靠的&#xff0c;效率低的&#xff1b; 1.HTTP协议端口默认80&#xff0c;HTTPSSL之后成为HTTPS协议默认端口443。 2.对于0~1023一般是默认的公共端口不需要注册&#xff0c;1024以后的则需…

【数据结构篇】时间复杂度

一.数据结构前言 1.1 数据结构的概念 数据结构(Data Structure)是计算机存储、组织数据的⽅式&#xff0c;指相互之间存在⼀种或多种特定关系的数 据元素的集合。没有⼀种单⼀的数据结构对所有⽤途都有⽤&#xff0c;所以我们要学各式各样的数据结构&#xff0c; 如&#xff1a…

[论文笔记] Deepseek-R1R1-zero技术报告阅读

启发: 1、SFT&RL的训练数据使用CoT输出的格式,先思考再回答,大大提升模型的数学与推理能力。 2、RL训练使用群体相对策略优化(GRPO),奖励模型是规则驱动,准确性奖励和格式化奖励。 1. 总体概述 背景与目标 报告聚焦于利用强化学习(RL)提升大型语言模型(LLMs)…

WebShell分析

一.WebShell基础 1.简介 介绍&#xff1a;WebShell是一种黑客常用的恶意脚本&#xff0c;主要目的是通过在目标服务器上植入恶意代码&#xff0c;获得执行操作的权限。常见的WebShell编写语言包括&#xff1a; ASPJSPPHP 2.特点 持久化控制 上传WebShell后&#xff0c;黑客能…