android /proc/bus/usb/devices,c - How to map /proc/bus/usb/devices entry to a /dev/sdX device? - Sta...

This isn't all that easy, nor very well documented (at least from a high-level perspective). The following should work in Kernel's from version 3.1 upward (at least).

I have found the easiest (probably not the only way) is to navigate from the block device entry and test each block device until you find the one that matches your USB entry.

For example, given a block device in /sys/block, such as sdb, you can find the hardware device descriptor entry like this:

# cd /sys/block

# cd $(readlink sdb); cd ../../../../../..

# ls -l

total 0

drwxr-xr-x 6 root root 0 Aug 14 10:47 1-1:1.0

-rw-r--r-- 1 root root 4096 Aug 14 10:52 authorized

-rw-r--r-- 1 root root 4096 Aug 14 10:52 avoid_reset_quirk

-r--r--r-- 1 root root 4096 Aug 14 10:47 bcdDevice

-rw-r--r-- 1 root root 4096 Aug 14 10:49 bConfigurationValue

-r--r--r-- 1 root root 4096 Aug 14 10:47 bDeviceClass

-r--r--r-- 1 root root 4096 Aug 14 10:49 bDeviceProtocol

-r--r--r-- 1 root root 4096 Aug 14 10:49 bDeviceSubClass

-r--r--r-- 1 root root 4096 Aug 14 10:49 bmAttributes

-r--r--r-- 1 root root 4096 Aug 14 10:49 bMaxPacketSize0

-r--r--r-- 1 root root 4096 Aug 14 10:49 bMaxPower

-r--r--r-- 1 root root 4096 Aug 14 10:49 bNumConfigurations

-r--r--r-- 1 root root 4096 Aug 14 10:49 bNumInterfaces

-r--r--r-- 1 root root 4096 Aug 14 10:49 busnum

-r--r--r-- 1 root root 4096 Aug 14 10:52 configuration

-r--r--r-- 1 root root 65553 Aug 14 10:47 descriptors

-r--r--r-- 1 root root 4096 Aug 14 10:52 dev

-r--r--r-- 1 root root 4096 Aug 14 10:49 devnum

-r--r--r-- 1 root root 4096 Aug 14 10:52 devpath

lrwxrwxrwx 1 root root 0 Aug 14 10:47 driver -> ../../../../../../bus/usb/drivers/usb

drwxr-xr-x 3 root root 0 Aug 14 10:52 ep_00

-r--r--r-- 1 root root 4096 Aug 14 10:47 idProduct

-r--r--r-- 1 root root 4096 Aug 14 10:47 idVendor

-r--r--r-- 1 root root 4096 Aug 14 10:52 ltm_capable

-r--r--r-- 1 root root 4096 Aug 14 10:47 manufacturer

-r--r--r-- 1 root root 4096 Aug 14 10:49 maxchild

lrwxrwxrwx 1 root root 0 Aug 14 10:52 port -> ../1-0:1.0/port1

drwxr-xr-x 2 root root 0 Aug 14 10:52 power

-r--r--r-- 1 root root 4096 Aug 14 10:47 product

-r--r--r-- 1 root root 4096 Aug 14 10:52 quirks

-r--r--r-- 1 root root 4096 Aug 14 10:47 removable

--w------- 1 root root 4096 Aug 14 10:52 remove

-r--r--r-- 1 root root 4096 Aug 14 10:47 serial

-r--r--r-- 1 root root 4096 Aug 14 10:49 speed

lrwxrwxrwx 1 root root 0 Aug 14 10:47 subsystem -> ../../../../../../bus/usb

-rw-r--r-- 1 root root 4096 Aug 14 10:47 uevent

-r--r--r-- 1 root root 4096 Aug 14 10:52 urbnum

-r--r--r-- 1 root root 4096 Aug 14 10:49 version

(You can find excellent documentation for the contents of the USB Descriptor here on the BeyondLogic site.)

Given the above, you should be able to map one or more of the USB device fields to the contents of /proc/bus/usb/devices. I find that the serial number is the easiest to match on, so that if you were to cat serial above, you would get the same serial number as listed:

T: Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=480 MxCh= 0

D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1

P: Vendor=0781 ProdID=5575 Rev=01.26

S: Manufacturer=SanDisk

S: Product=Cruzer Glide

S: SerialNumber=4C530100801115115112

C: #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=200mA

I: If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage

If you go to /sys/block, you can list the full path to the host device entry in the storage driver sysfs entry for each device. Typically, I do this using some programmatic means instead of at the shell prompt, but here you can see the links themselves:

# ls -l sd*

lrwxrwxrwx 1 root root 0 Aug 14 10:45 sda -> ../devices/pci0000:00/0000:00:10.0/host32/target32:0:0/32:0:0:0/block/sda

lrwxrwxrwx 1 root root 0 Aug 14 10:47 sdb -> ../devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/block/sdb

Note that you mustn't make any assumptions about the numbers you see in the links. Depending upon the bus subsystem, the mappings could be quite different. For example, on a Raspberry Pi, it looks like this:

# ls -l sd*

lrwxrwxrwx 1 root root 0 Aug 13 23:54 sda -> ../devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host3/target3:0:0/3:0:0:0/block/sda

lrwxrwxrwx 1 root root 0 Aug 13 23:54 sdb -> ../devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host4/target4:0:0/4:0:0:0/block/sdb

So, the best approach is to take the approach listed at the top and navigate relative to the storage driver to find the USB device descriptor.

I'd be curious about more authoritative answers to this. The method above was arrived at by trial-and-error but has been working on several different devices and Kernels with no problem.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值