androidFan
androidFan

Reputation: 621

Embedded Linux USB device detect port and address

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 ??

Upvotes: 3

Views: 4776

Answers (3)

Amit Vujic
Amit Vujic

Reputation: 1788

You can use udev subsystem to map devices during system startup

  1. Use commands

    [lsusb] and [lsusb -v] to identify VendorID, ProductID and SerialID for every device you want to control

  2. 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"

  3. Restart udev subsystem [/etc/init.d/udev restart ] or reboot linux box.

Upvotes: 0

Dan Aloni
Dan Aloni

Reputation: 4098

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:

sde -> ../devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4.4/1-1.2.4.4:1.0/host12/target12:0:0/12:0:0:1/block/sde

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.

Upvotes: 4

I believe you should learn more about udev (or perhaps hal on an old system).

Upvotes: 3

Related Questions