嵌入式Linux USB设备检测端口和地址
我正在为我的主板开发 USB 应用程序。它有两个 USB 端口。当我将 USB 驱动器插入每个设备时,我在控制台上收到以下消息:
在端口 1 上:
usb 1-1: new high speed USB device using atmel-ehci and address 4
usb 1-1: New USB device found, idVendor=0781, idProduct=5567
usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-1: Product: Cruzer Blade
usb 1-1: Manufacturer: SanDisk
usb 1-1: SerialNumber: 3515430A2EE2729D
scsi2 : usb-storage 1-1:1.0
scsi 2:0:0:0: Direct-Access SanDisk Cruzer Blade 8.02 PQ: 0 ANSI: 0 CCS
sd 2:0:0:0: [sdb] 7856127 512-byte logical blocks: (4.02 GB/3.74 GiB)
sd 2:0:0:0: [sdb] Write Protect is off
sd 2:0:0:0: [sdb] Assuming drive cache: write through
sd 2:0:0:0: [sdb] Assuming drive cache: write through
sdb:
sdb1
sd 2:0:0:0: [sdb] Assuming drive cache: write through
sd 2:0:0:0: [sdb] Attached SCSI removable disk
在端口 2 上:
usb 1-2: USB disconnect, address 3
usb 1-2: new high speed USB device using atmel-ehci and address 5
usb 1-2: New USB device found, idVendor=0781, idProduct=5567
usb 1-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-2: Product: Cruzer Blade
usb 1-2: Manufacturer: SanDisk
usb 1-2: SerialNumber: 3515430A2EE2729D
scsi3 : usb-storage 1-2:1.0
scsi 3:0:0:0: Direct-Access SanDisk Cruzer Blade 8.02 PQ: 0 ANSI: 0 CCS
sd 3:0:0:0: [sda] 7856127 512-byte logical blocks: (4.02 GB/3.74 GiB)
sd 3:0:0:0: [sda] Write Protect is off
sd 3:0:0:0: [sda] Assuming drive cache: write through
sd 3:0:0:0: [sda] Assuming drive cache: write through
sda:
sda1
sd 3:0:0:0: [sda] Assuming drive cache: write through
sd 3:0:0:0: [sda] Attached SCSI removable disk
但这些消息都在控制台上。
我想检测哪个设备已连接到具有该地址的哪个端口(如 sda、sdb 等)。
我对此进行了谷歌搜索,发现两个独立提供此信息的系统路径: 1) /sys/bus/usb/devices/1-x
2) /sys/class/scsi_disk/x:0:0:0/device
第一个给出这个信息:
1-2:1.0 bus maxchild
authorized busnum product
bConfigurationValue configuration quirks
bDeviceClass descriptors remove
bDeviceProtocol dev serial
bDeviceSubClass devnum speed
bMaxPacketSize0 devpath subsystem
bMaxPower driver uevent
bNumConfigurations ep_00 urbnum
bNumInterfaces idProduct usb_device:usbdev1.4
bcdDevice idVendor version
bmAttributes manufacturer
第二个提供了这个信息:
block:sdb iorequest_cnt scsi_disk:2:0:0:0
bus max_sectors scsi_level
delete modalias state
device_blocked model subsystem
driver queue_depth timeout
evt_media_change queue_type type
iocounterbits rescan uevent
iodone_cnt rev vendor
ioerr_cnt scsi_device:2:0:0:0
所以这两个数据结构之间没有共同的信息。我可以得到端口号。和序列号。第一个路径中的 USB 设备。我可以从第二条路径获取地址(sda、sdb等)。但他们之间没有共同信息。我如何知道端口 1 上的特定 USB 设备已安装在 sda 上?或者端口 2 上的设备(如果安装在 sdc 上)?
I'm developing a USB application for my board. It has two USB ports. When I plug in a USB drive in each of them I get the following messages on console :
On port 1 :
usb 1-1: new high speed USB device using atmel-ehci and address 4
usb 1-1: New USB device found, idVendor=0781, idProduct=5567
usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-1: Product: Cruzer Blade
usb 1-1: Manufacturer: SanDisk
usb 1-1: SerialNumber: 3515430A2EE2729D
scsi2 : usb-storage 1-1:1.0
scsi 2:0:0:0: Direct-Access SanDisk Cruzer Blade 8.02 PQ: 0 ANSI: 0 CCS
sd 2:0:0:0: [sdb] 7856127 512-byte logical blocks: (4.02 GB/3.74 GiB)
sd 2:0:0:0: [sdb] Write Protect is off
sd 2:0:0:0: [sdb] Assuming drive cache: write through
sd 2:0:0:0: [sdb] Assuming drive cache: write through
sdb:
sdb1
sd 2:0:0:0: [sdb] Assuming drive cache: write through
sd 2:0:0:0: [sdb] Attached SCSI removable disk
On port 2 :
usb 1-2: USB disconnect, address 3
usb 1-2: new high speed USB device using atmel-ehci and address 5
usb 1-2: New USB device found, idVendor=0781, idProduct=5567
usb 1-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-2: Product: Cruzer Blade
usb 1-2: Manufacturer: SanDisk
usb 1-2: SerialNumber: 3515430A2EE2729D
scsi3 : usb-storage 1-2:1.0
scsi 3:0:0:0: Direct-Access SanDisk Cruzer Blade 8.02 PQ: 0 ANSI: 0 CCS
sd 3:0:0:0: [sda] 7856127 512-byte logical blocks: (4.02 GB/3.74 GiB)
sd 3:0:0:0: [sda] Write Protect is off
sd 3:0:0:0: [sda] Assuming drive cache: write through
sd 3:0:0:0: [sda] Assuming drive cache: write through
sda:
sda1
sd 3:0:0:0: [sda] Assuming drive cache: write through
sd 3:0:0:0: [sda] Attached SCSI removable disk
But these messages are on the console.
I want to detect which device has been connected to which port with the address (like sda, sdb, etc.)
I have google a bit on this and found two system paths that give this info independently :
1) /sys/bus/usb/devices/1-x
2) /sys/class/scsi_disk/x:0:0:0/device
The first one gives this info :
1-2:1.0 bus maxchild
authorized busnum product
bConfigurationValue configuration quirks
bDeviceClass descriptors remove
bDeviceProtocol dev serial
bDeviceSubClass devnum speed
bMaxPacketSize0 devpath subsystem
bMaxPower driver uevent
bNumConfigurations ep_00 urbnum
bNumInterfaces idProduct usb_device:usbdev1.4
bcdDevice idVendor version
bmAttributes manufacturer
The second one gives this info :
block:sdb iorequest_cnt scsi_disk:2:0:0:0
bus max_sectors scsi_level
delete modalias state
device_blocked model subsystem
driver queue_depth timeout
evt_media_change queue_type type
iocounterbits rescan uevent
iodone_cnt rev vendor
ioerr_cnt scsi_device:2:0:0:0
So there is no common info between these two datastructures. I can get the port no. and serial no. of the USB device from the first path. And I can get the address (sda, sdb, etc.) from the second path. But there is no common info between them. How can I know that a particular USB device on port 1 is mounted on sda ? Or a device on port 2 if mounted on sdc ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所建议的,使用 udev 可能是一种干净的方法。但要直接回答你的问题,如果你枚举 /sys/block 并读取所有块设备的符号链接(即
readlink
),你会看到如下内容:符号链接完成了你所寻找的信息,这就是USB拓扑中块设备和节点的关系。
另外,请注意问题最后引用的文本中的“块:”。
As other folks suggest, using udev might be a clean approach. But to answer your question directly, if you enumerate /sys/block and read the symlinks (i.e.
readlink
) of all block devices, you'd see something such as:The symlink completes the information you seek, which is the relationship between the block device and the node in the USB topology.
Also, notice the "block:" in the last quoted text of your question.
我相信你应该了解更多关于 udev (或者hal 在旧系统上)。
I believe you should learn more about udev (or perhaps hal on an old system).
您可以使用 udev 子系统 在系统启动期间映射设备
使用命令
[lsusb] 和 [lsusb -v] 识别您想要控制的每个设备的 VendorID、ProductID 和 SerialID
在[/etc/udev/rules.d/]中创建新文件,假设文件名为[77-my.rules]
内容基于以下模板:
子系统==“usb”,内核==“ttyUSB*”,ATTRS {idVendor}==“0781”,
ATTRS{idProduct}==“5567”,ATTRS{序列}==“3515430A2EE2729D”,
SYMLINK+="mydisk00"
子系统==“usb”,内核==“ttyUSB*”,ATTRS {idVendor}==“0781”,
ATTRS{idProduct}=="5567", ATTRS{serial}=="3515430A2EE2ABA", SYMLINK+="mydisk11"
重新启动 udev 子系统 [/etc/init.d/udev restart ] 或重新启动 linux 盒子。
You can use udev subsystem to map devices during system startup
Use commands
[lsusb] and [lsusb -v] to identify VendorID, ProductID and SerialID for every device you want to control
Make new file in [/etc/udev/rules.d/], let say file named [77-my.rules]
with content based on following template:
SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{idVendor}=="0781",
ATTRS{idProduct}=="5567", ATTRS{serial}=="3515430A2EE2729D",
SYMLINK+="mydisk00"
SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{idVendor}=="0781",
ATTRS{idProduct}=="5567", ATTRS{serial}=="3515430A2EE2ABA", SYMLINK+="mydisk11"
Restart udev subsystem [/etc/init.d/udev restart ] or reboot linux box.