LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to know which usb port usb device is connected to? (http://www.linuxquestions.org/questions/linux-general-1/how-to-know-which-usb-port-usb-device-is-connected-to-650978/)

jbsp72 06-23-2008 12:30 AM

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.

Thanks
Shivu

Swagata Paul 06-23-2008 04:46 AM

hi,

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.

jbsp72 06-23-2008 05:08 AM

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

Thanks
Shivu

greenberet 03-04-2010 01:02 AM

Hey,

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.

Thanks,

nwazsohail 05-22-2012 11:21 AM

use following command

cdrecrod --scanbus

JZL240I-U 02-27-2014 12:59 AM

Quote:

Originally Posted by nwazsohail (Post 4684869)
cdrecrod --scanbus

Just for the record ;), I presume you meant the command "cdrecord --scanbus".

Whether it solves the original problem I don't know...

Drakeo 02-27-2014 02:51 AM

Quote:

Mar 4 08:19:23 localhost kernel: sdc: sdc1
there you go.

JZL240I-U 02-27-2014 03:02 AM

That works only for hard disks.

briselec 02-28-2014 04:36 AM

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.

briselec 02-28-2014 04:43 AM

Another way you could do it is to use one of the functions provided by libusb

Drakeo 02-28-2014 08:12 AM

Quote:

Originally Posted by JZL240I-U (Post 5125631)
That works only for hard disks.

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

rtmistler 02-28-2014 02:00 PM

Um ... most recent question in this conversation was posting #4, in 2010.

The original question was posted in 2008.

Drakeo 02-28-2014 02:41 PM

wonders how it made quick links

JZL240I-U 03-03-2014 01:39 AM

Quote:

Originally Posted by Drakeo (Post 5126454)
are you really serious this goes back to the 2.6.18 kernel where all drives will be looked at a scsi. ...

The OP didn't restrict her/his question to hard disks. There it will work, of course, but what will you do, when it is e.g. a keyboard?

evo2 03-03-2014 01:48 AM

Hi,

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.

HTH,

Evo2.


All times are GMT -5. The time now is 11:46 AM.