用sysfs查看linux下usb设备的描述符

4 篇文章 0 订阅

/sys/bus/usb/devices/下的路径命名规则

 roothub-port[.port]:configuration:interface

一般,1-0是根hub自身信息,当hub上有插入设备时,就会出现1-1、1-2了。
1.查看目录:

ls /sys/bus/usb/devices/
1-0:1.0    1-1.1:1.0  2-1        4-0:1.0    usb1       usb4
1-1        1-1:1.0    2-1:1.0    5-0:1.0    usb2       usb5
1-1.1      2-0:1.0    3-0:1.0    6-0:1.0    usb3       usb6

这是什么鬼东西?

2.别急,再详细看看。

ls /sys/bus/usb/devices/usb1/
1-0:1.0              bcdDevice            manufacturer
1-1                  bmAttributes         maxchild
authorized           busnum               power
authorized_default   configuration        product
avoid_reset_quirk    descriptors          quirks
bConfigurationValue  dev                  removable
bDeviceClass         devnum               remove
bDeviceProtocol      devpath              serial
bDeviceSubClass      driver               speed
bMaxPacketSize0      ep_00                subsystem
bMaxPower            idProduct            uevent
bNumConfigurations   idVendor             urbnum
bNumInterfaces       ltm_capable          version

可以直接看到描述符?

3.子目录呢?

ls /sys/bus/usb/devices/usb1/1-0\:1.0/
bAlternateSetting     bNumEndpoints         power
bInterfaceClass       driver                subsystem
bInterfaceNumber      ep_81                 supports_autosuspend
bInterfaceProtocol    modalias              uevent
bInterfaceSubClass    port1

接口描述符?好家伙!

4.看看是啥设备:

cat /sys/bus/usb/devices/usb1/1-0\:1.0/bInterfaceClass
09

哇哦,是hub!

5.那1-1是啥东西?

cat /sys/bus/usb/devices/usb1/1-1/1-1.1/bDeviceClass
00

是一个设备,我刚刚插进去的安卓手机

于是可以得到:

接口描述符:

cat /sys/bus/usb/devices/usb1/1-1/1-1.1/1-1.1\:1.0/
bAlternateSetting     driver/               subsystem/
bInterfaceClass       ep_02/                supports_autosuspend
bInterfaceNumber      ep_81/                uevent
bInterfaceProtocol    interface             usbmisc/
bInterfaceSubClass    modalias              
bNumEndpoints         power/ 

往上是配置和设备描述符:

cat /sys/bus/usb/devices/usb1/1-1/1-1.1/           
1-1.1:1.0/           busnum               port/
authorized           configuration        power/
avoid_reset_quirk    descriptors          product
bConfigurationValue  dev                  quirks
bDeviceClass         devnum               removable
bDeviceProtocol      devpath              remove
bDeviceSubClass      driver/              serial
bMaxPacketSize0      ep_00/               speed
bMaxPower            idProduct            subsystem/
bNumConfigurations   idVendor             uevent
bNumInterfaces       ltm_capable          urbnum
bcdDevice            manufacturer         version
bmAttributes         maxchild           

再往上:

 cat /sys/bus/usb/devices/usb1/1-1/
1-1.1/               bmAttributes         port/
1-1:1.0/             busnum               power/
authorized           configuration        product
avoid_reset_quirk    descriptors          quirks
bConfigurationValue  dev                  removable
bDeviceClass         devnum               remove
bDeviceProtocol      devpath              speed
bDeviceSubClass      driver/              subsystem/
bMaxPacketSize0      ep_00/               uevent
bMaxPower            idProduct            urbnum
bNumConfigurations   idVendor             version
bNumInterfaces       ltm_capable          
bcdDevice            maxchild   

这个好像没有区别。

6.回到主目录:

ls /sys/bus/usb/devices/
1-0:1.0    1-1.1:1.0  2-1        4-0:1.0    usb1       usb4
1-1        1-1:1.0    2-1:1.0    5-0:1.0    usb2       usb5
1-1.1      2-0:1.0    3-0:1.0    6-0:1.0    usb3       usb6

一共3个接口,为啥有6个usb呢?追溯一下:

 ls -al /sys/bus/usb/devices/
total 0
drwxr-xr-x    2 root     root             0 Jan  1 06:59 .
drwxr-xr-x    4 root     root             0 Jan  1 06:59 ..
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 1-0:1.0 -> ../../../devices/soc/5200000.ehci1-controller/usb1/1-0:1.0
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 1-1 -> ../../../devices/soc/5200000.ehci1-controller/usb1/1-1
lrwxrwxrwx    1 root     root             0 Jan  1 08:46 1-1.1 -> ../../../devices/soc/5200000.ehci1-controller/usb1/1-1/1-1.1
lrwxrwxrwx    1 root     root             0 Jan  1 08:46 1-1.1:1.0 -> ../../../devices/soc/5200000.ehci1-controller/usb1/1-1/1-1.1/1-1.1:1.0
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 1-1:1.0 -> ../../../devices/soc/5200000.ehci1-controller/usb1/1-1/1-1:1.0
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 2-0:1.0 -> ../../../devices/soc/5310000.ehci2-controller/usb2/2-0:1.0
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 2-1 -> ../../../devices/soc/5310000.ehci2-controller/usb2/2-1
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 2-1:1.0 -> ../../../devices/soc/5310000.ehci2-controller/usb2/2-1/2-1:1.0
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 3-0:1.0 -> ../../../devices/soc/5311000.ehci3-controller/usb3/3-0:1.0
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 4-0:1.0 -> ../../../devices/soc/5200000.ohci1-controller/usb4/4-0:1.0
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 5-0:1.0 -> ../../../devices/soc/5310000.ohci2-controller/usb5/5-0:1.0
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 6-0:1.0 -> ../../../devices/soc/5311000.ohci3-controller/usb6/6-0:1.0
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 usb1 -> ../../../devices/soc/5200000.ehci1-controller/usb1
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 usb2 -> ../../../devices/soc/5310000.ehci2-controller/usb2
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 usb3 -> ../../../devices/soc/5311000.ehci3-controller/usb3
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 usb4 -> ../../../devices/soc/5200000.ohci1-controller/usb4
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 usb5 -> ../../../devices/soc/5310000.ohci2-controller/usb5
lrwxrwxrwx    1 root     root             0 Jan  1 06:59 usb6 -> ../../../devices/soc/5311000.ohci3-controller/usb6

看到了,ehci和ohci是分开的,所以一共有6个控制器。


void checkUSBDevice()
{
    bool ret = false;

    #define USB1_PATH "/sys/bus/usb/devices/usb1/1-1/1-1.1/product"
    #define USB2_PATH "/sys/bus/usb/devices/usb1/1-1/1-1.2/product"


    int fd = open(USB1_PATH,O_RDONLY);
    if(fd>0)
    {
        printf("usb 1 is exist!\n");
        char product[BUF_LEN] =  {'\0'};
        int seekLen = lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);
        int ret = read(fd,product,seekLen);

        close(fd);
    }
    else
    {
        printf("usb 1 is not exist!\n");
    }

    
    fd = open(USB2_PATH,O_RDONLY);
    if(fd>0)
    {
        printf("usb 2 is exist!\n");
        char product[BUF_LEN] =  {'\0'};
        int seekLen = lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);
        int ret = read(fd,product,seekLen);
        printf("usb1 name:%s,strlen=%d,seekLen=%d,reedlen=%d\n",product,strlen(product),seekLen,ret);
        close(fd);
    }
    else
    {
        printf("usb 2 is not exist!\n");
    }

}
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值