usb设备拓扑关系

本文介绍了USB设备在Linux系统中的拓扑关系,包括通过debugfs挂载查看总线上的设备信息,详细解析设备目录内容,以及使用usbutils工具如lsusb命令以树形结构展示设备层次。内容涵盖了USB设备的挂载方式、信息解析及不同版本Linux下的变化。

1.简介

我们在调试分析USB时通常要知道设备是被挂载到哪一个usb端口上,或者usb端口中接入一个hub,hub上又接了设备,那么这个设备是被接到hub中的哪一个端口呢。
要想知道这些usb驱动框架提供了两种查看方式:

  • 早期的系统linux-2.6采用的是/proc/bus/usb的方式进行查看设备挂载的拓扑关系。
    在linux-3.3之后移除了usbfs,改成通过debugfs的方式查看拓扑关系,并将/proc/bus/usb移到/dev/bus/usb下.
  • 通过libusb utils工具进行查看拓扑关系

2.usbfs和debugfs拓扑

USB设备的usbfs文件系统传统上安装在/proc/bus/usb。它提供/proc/bus/usb/devices文件,以及/proc/bus/usb/BBB/DDD文件。
在许多现代系统中linux-3.3以后的版本,根本不使用usbfs文件系统。相反USB设备节点是在/dev/USB/或类似的地方创建的。这个“devices”文件在debugfs中可用,通常为/sys/kernel/debug/usb/设备。

2.1挂载方式

  • usbfs
    系统配置挂载:
    Device Drivers —>
    [] USB Support —>
    [
    ] USB device filesystem(DEPRECATED)
    或者手动挂载
    mount -t usbfs usbfs /proc/bus/usb/
    这样可以通过查看/proc/bus/usb/devices文件,对文件内容进行分析

  • debugfs
    手动挂载
    mount -t debugfs none /sys/kernel/debug/
    详细的挂载可以参考自动挂载方式

2.2拓扑关系信息说明

当我们挂载了usbfs文件之后,就可以对文件进行查看所有的usb 设备挂载的信息。
我们以新的debugfs的方式进行举例,这种方式对于usbfs来说就是对相应的挂载文件做了迁移。

2.2.1usb总线上的设备信息

进入到/sys/bus/usb/devices文件进行查看,该文件列出了所有设备在usb总线上的挂载情况。

aiv8167sm3_bsp:/proc/bus $ ls /sys/bus/usb/devices/ -l
total 0
lrwxrwxrwx 1 root root 0 2023-03-06 17:49 1-0:1.0 -> ../../../devices/platform/soc/mt_usb/musb-hdrc/usb1/1-0:1.0
lrwxrwxrwx 1 root root 0 2023-03-06 22:34 1-1 -> ../../../devices/platform/soc/mt_usb/musb-hdrc/usb1/1-1
lrwxrwxrwx 1 root root 0 2023-03-06 22:35 1-1.2 -> ../../../devices/platform/soc/mt_usb/musb-hdrc/usb1/1-1/1-1.2
lrwxrwxrwx 1 root root 0 2023-03-06 22:35 1-1.2:1.0 -> ../../../devices/platform/soc/mt_usb/musb-hdrc/usb1/1-1/1-1.2/1-1.2:1.0
lrwxrwxrwx 1 root root 0 2023-03-06 22:35 1-1.2:1.1 -> ../../../devices/platform/soc/mt_usb/musb-hdrc/usb1/1-1/1-1.2/1-1.2:1.1
lrwxrwxrwx 1 root root 0 2023-03-06 22:34 1-1:1.0 -> ../../../devices/platform/soc/mt_usb/musb-hdrc/usb1/1-1/1-1:1.0
lrwxrwxrwx 1 root root 0 2023-03-06 17:49 2-0:1.0 -> ../../../devices/platform/soc/11190000.usb/musbfsh-hdrc.0/usb2/2-0:1.0
lrwxrwxrwx 1 root root 0 2023-03-06 17:12 usb1 -> ../../../devices/platform/soc/mt_usb/musb-hdrc/usb1
lrwxrwxrwx 1 root root 0 2023-03-06 17:12 usb2 -> ../../../devices/platform/soc/11190000.usb/musbfsh-hdrc.0/usb2

其中 usbx/第x个总线,x-y:a.b/的目录格式,x表示总线号,y表示端口,a表示配置,b表示接口。
官方给的 linux系统下 sysfs关于USB文件结构关系如下:

Q: What are the sysfs structures for Linux USB?
A: For example the directory will have something like:

# ls  /sys/bus/usb/devices/
1-0:1.0      1-1.3        1-1.3.1:1.0  1-1:1.0
1-1          1-1.3.1      1-1.3:1.0    usb1

The names that begin with "usb" refer to USB controllers. More accurately, they refer to the "root hub" associated with each controller. The number is the USB bus number. In the example there is only one controller, so its bus is number 1. Hence the name "usb1".

"1-0:1.0" is a special case. It refers to the root hub's interface. This acts just like the interface in an actual hub an almost every respect; see below.

All the other entries refer to genuine USB devices and their interfaces. The devices are named by a scheme like this:

	bus-port.port.port ...

In other words, the name starts with the bus number followed by a '-'. Then comes the sequence of port numbers for each of the intermediate hubs along the path to the device.

For example, "1-1" is a device plugged into bus 1, port 1. It happens to be a hub, and "1-1.3" is the device plugged into port 3 of that hub. That device is another hub, and "1-1.3.1" is the device plugged into its port 1.

The interfaces are indicated by suffixes having this form:

	:config.interface

That is, a ':' followed by the configuration number followed by '.' followed by the interface number. In the above example, each of the devices is using configuration 1 and this configuration has only a single interface, number 0. So the interfaces show up as;

	1-1:1.0		1-1.3:1.0		1-1.3.1:1.0

A hub will never have more than a single interface; that's part of the USB spec. But other devices can and do have multiple interfaces (and sometimes multiple configurations). Each interface gets its own entry in sysfs and can have its own driver.

主要意思就是:

  1. 以usb开头的代表usb controller(可以理解usb驱动)也就是一个roothub即代表一个usb总线,后面跟着的数字即代表总线号,从1开始递增。如:usb1 usb2 usbx等
  2. “1-0:1.0” “2-0:1.0” 是一个特殊的实例,代表是一个root hub接口。
    “1-0:1.0” 就代表总线1的root hub, “2-0:1.0” 就代表总线2的root hub;

注意:一个hub只有一个interface

  1. 其它的文件命名,都代表一个具体的usb设备,命名规则如下:
    bus-port.port.port …
    每一个名称都是以总线号开始,然后用‘-’进行连接,后面跟着是设备路径devpath端口序号,可以理解为端口号。
    举例:
    “1-1” 代表插入总线1端口为1的设备, 它代表的是一个hub设备,可以通过bDeviceClass查看09
    “1-1.2” 代表插入总线1端口为1的设备上的它的端口为2的设备,它代表的是一个cdc设备, 可以通过bDeviceClass查看02
  2. 接口由以下的形式进行表示:
    :config.interface
    就是’:'后面跟着配置号,后面跟着“.”后跟接口号。在上面的示例中,每个设备都使用配置1,该配置只有一个接口,编号为0。因此,界面显示为:
    1-1:1.0 1-1.3:1.0 1-1.3.1:1.0 // 上述官方实例
    1-1.2:1.0 1-1.2:1.1 1-1:1.0 // 实际实例

一个集线器hub永远只有一个接口;这是USB规范的一部分,但其他设备可以也确实有多个接口(有时还有多种配置)。每个接口在sysfs中都有自己的条目,并且可以有自己的驱动程序。

2.2.2总线上特定设备的详细信息

进入到某个目录中去,可以看到该设备的详细信息,可用cat命令获取具体文件信息。

aiv8167sm3_bsp:/sys/bus/usb/devices $ cd 1-1
aiv8167sm3_bsp:/sys/bus/usb/devices/1-1 $ ls
1-1.2      avoid_reset_quirk   bDeviceProtocol bMaxPower          bcdDevice    configuration devnum  ep_00     ltm_capable power   removable   speed     urbnum
1-1:1.0    bConfigurationValue bDeviceSubClass bNumConfigurations bmAttributes descriptors   devpath idProduct maxchild    product remove      subsystem version
authorized bDeviceClass        bMaxPacketSize0 bNumInterfaces     busnum       dev           driver  idVendor  port        quirks  set_suspend uevent
aiv8167sm3_bsp:/sys/bus/usb/devices/1-1 $ 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值