How to know which usb port usb device is connected to?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to know which usb port usb device is connected to?
Hi,
The usb device I am working with doesnot have a serial no, so to uniquely identify I thought I may use the information of which port it is connected to , to uniquely identify it.
I found out all the usb ports are listed in /dev/input/by-path. On my machine I have four entries in this directory . How can I find out which usb port My usb device is connected to. Is there some API call or something which I can use. I have to do this from inside a c code.
use the command "tail -f /var/log/messages"
plug the USB device to any usb port.
Wait & see the changes on the conmmand screen.
then,use the command "lsusb" to get the Bus & Device ID.
Repeat the operations by unplugging & plugging the device to another port.
I need to know the port no programatically I guess I can get it by parsing the /proc/bus/usb/devices file but is their no better way like a API or something
I have the same problem. I need to know the port to which my usb drive is conncted. But, it seems that the bus ID and the device ID are not constant but keep changing depending on the pen-drive. Thus, they are not unique to the port.
Here is the output of /var/log/messages, when i plug in two different drives, one 128 MB and one 512 MB, into the same port.
Mar 4 08:19:18 localhost kernel: usb 3-1: new full speed USB device using uhci_hcd and address 6
Mar 4 08:19:18 localhost kernel: usb 3-1: New USB device found, idVendor=09a6, idProduct=8001
Mar 4 08:19:18 localhost kernel: usb 3-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
Mar 4 08:19:18 localhost kernel: usb 3-1: configuration #1 chosen from 1 choice
Mar 4 08:19:18 localhost kernel: scsi18 : SCSI emulation for USB Mass Storage devices
Mar 4 08:19:23 localhost kernel: scsi 18:0:0:0: Direct-Access USB 2.0 Flash Disk 1.06 PQ: 0 ANSI: 0 CCS
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: Attached scsi generic sg3 type 0
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: [sdc] 260320 512-byte logical blocks: (133 MB/127 MiB)
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: [sdc] Write Protect is off
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: [sdc] Assuming drive cache: write through
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: [sdc] Assuming drive cache: write through
Mar 4 08:19:23 localhost kernel: sdc: sdc1
Mar 4 08:19:23 localhost kernel: sdc: p1 size 260320 exceeds device capacity, limited to end of disk
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: [sdc] Assuming drive cache: write through
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: [sdc] Attached SCSI removable disk
Mar 4 08:19:52 localhost kernel: usb 3-1: USB disconnect, address 6
Mar 4 08:20:10 localhost kernel: usb 1-3: new high speed USB device using ehci_hcd and address 19
Mar 4 08:20:10 localhost kernel: usb 1-3: New USB device found, idVendor=0ea0, idProduct=2168
Mar 4 08:20:10 localhost kernel: usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Mar 4 08:20:10 localhost kernel: usb 1-3: Product: Flash Disk
Mar 4 08:20:10 localhost kernel: usb 1-3: Manufacturer: USB
Mar 4 08:20:10 localhost kernel: usb 1-3: SerialNumber: 611041FD6E02003C
Mar 4 08:20:10 localhost kernel: usb 1-3: configuration #1 chosen from 1 choice
Mar 4 08:20:10 localhost kernel: scsi19 : SCSI emulation for USB Mass Storage devices
Mar 4 08:20:15 localhost kernel: scsi 19:0:0:0: Direct-Access JetFlash TS512MJF2B 2.00 PQ: 0 ANSI: 2
Mar 4 08:20:15 localhost kernel: sd 19:0:0:0: Attached scsi generic sg3 type 0
Mar 4 08:20:16 localhost kernel: ready
Mar 4 08:20:16 localhost kernel: sd 19:0:0:0: [sdc] 1022720 512-byte logical blocks: (523 MB/499 MiB)
Mar 4 08:20:16 localhost kernel: sd 19:0:0:0: [sdc] Write Protect is off
Mar 4 08:20:16 localhost kernel: sd 19:0:0:0: [sdc] Assuming drive cache: write through
Mar 4 08:20:16 localhost kernel: sd 19:0:0:0: [sdc] Assuming drive cache: write through
Mar 4 08:20:16 localhost kernel: sdc: sdc1
Mar 4 08:20:16 localhost kernel: sd 19:0:0:0: [sdc] Assuming drive cache: write through
Mar 4 08:20:16 localhost kernel: sd 19:0:0:0: [sdc] Attached SCSI removable disk
Mar 4 08:22:00 localhost kernel: usb 1-3: USB disconnect, address 19
Please tell me how to identify that these two devices have been connected to the same port.
For a particular device you can create a udev.rule that creates a symlink each time that device is plugged in. Then you can open it in your c program by using the symlink.
are you really serious this goes back to the 2.6.18 kernel where all drives will be looked at a scsi. it was well noted. that kernel log shows the kernel placing that as sdc and shows the partition to mount will be 1 .
Quote:
Mar 4 08:19:18 localhost kernel: usb 3-1: new full speed USB device using uhci_hcd and address 6
Mar 4 08:19:18 localhost kernel: usb 3-1: New USB device found, idVendor=09a6, idProduct=8001
Mar 4 08:19:18 localhost kernel: usb 3-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
Mar 4 08:19:18 localhost kernel: usb 3-1: configuration #1 chosen from 1 choice
Mar 4 08:19:18 localhost kernel: scsi18 : SCSI emulation for USB Mass Storage devices
Mar 4 08:19:23 localhost kernel: scsi 18:0:0:0: Direct-Access USB 2.0 Flash Disk 1.06 PQ: 0 ANSI: 0 CCS
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: Attached scsi generic sg3 type 0
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: [sdc] 260320 512-byte logical blocks: (133 MB/127 MiB)
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: [sdc] Write Protect is off
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: [sdc] Assuming drive cache: write through
Mar 4 08:19:23 localhost kernel: sd 18:0:0:0: [sdc] Assuming drive cache: write through
Mar 4 08:19:23 localhost kernel: sdc: sdc1
yes I know this thread is a bit of a zombie, but I think I have a comprehensive answer: the "usb-devices" script by Greg K-H et. al.. This is packaged in the usbutils package on Debian along with the also useful lsusb command.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.