LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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

Reply
 
Search this Thread
Old 06-23-2008, 01:30 AM   #1
jbsp72
LQ Newbie
 
Registered: Jun 2008
Posts: 3

Rep: Reputation: 0
Question 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
 
Old 06-23-2008, 05:46 AM   #2
Swagata Paul
Member
 
Registered: Jun 2008
Location: Kolkata, India
Posts: 57

Rep: Reputation: 15
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.
 
Old 06-23-2008, 06:08 AM   #3
jbsp72
LQ Newbie
 
Registered: Jun 2008
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 03-04-2010, 02:02 AM   #4
greenberet
Member
 
Registered: Jun 2006
Posts: 50

Rep: Reputation: 15
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,
 
Old 05-22-2012, 12:21 PM   #5
nwazsohail
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Rep: Reputation: Disabled
use following command

cdrecrod --scanbus
 
Old 02-27-2014, 01:59 AM   #6
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,680

Rep: Reputation: Disabled
Quote:
Originally Posted by nwazsohail View Post
cdrecrod --scanbus
Just for the record , I presume you meant the command "cdrecord --scanbus".

Whether it solves the original problem I don't know...
 
Old 02-27-2014, 03:51 AM   #7
Drakeo
Senior Member
 
Registered: Jan 2008
Location: Urbana IL
Distribution: Slackware, Slacko,
Posts: 2,658
Blog Entries: 3

Rep: Reputation: 222Reputation: 222Reputation: 222
Quote:
Mar 4 08:19:23 localhost kernel: sdc: sdc1
there you go.
 
Old 02-27-2014, 04:02 AM   #8
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,680

Rep: Reputation: Disabled
That works only for hard disks.
 
Old 02-28-2014, 05:36 AM   #9
briselec
Member
 
Registered: Jun 2013
Location: Ipswich, Australia
Distribution: Slackware
Posts: 32

Rep: Reputation: Disabled
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.
 
Old 02-28-2014, 05:43 AM   #10
briselec
Member
 
Registered: Jun 2013
Location: Ipswich, Australia
Distribution: Slackware
Posts: 32

Rep: Reputation: Disabled
Another way you could do it is to use one of the functions provided by libusb
 
Old 02-28-2014, 09:12 AM   #11
Drakeo
Senior Member
 
Registered: Jan 2008
Location: Urbana IL
Distribution: Slackware, Slacko,
Posts: 2,658
Blog Entries: 3

Rep: Reputation: 222Reputation: 222Reputation: 222
Quote:
Originally Posted by JZL240I-U View Post
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
 
Old 02-28-2014, 03:00 PM   #12
rtmistler
Senior Member
 
Registered: Mar 2011
Location: Milford, MA. USA
Distribution: Angstrom, Debian, Ubuntu, MINT
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 509Reputation: 509Reputation: 509Reputation: 509Reputation: 509Reputation: 509
Um ... most recent question in this conversation was posting #4, in 2010.

The original question was posted in 2008.
 
Old 02-28-2014, 03:41 PM   #13
Drakeo
Senior Member
 
Registered: Jan 2008
Location: Urbana IL
Distribution: Slackware, Slacko,
Posts: 2,658
Blog Entries: 3

Rep: Reputation: 222Reputation: 222Reputation: 222
wonders how it made quick links
 
Old 03-03-2014, 02:39 AM   #14
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,680

Rep: Reputation: Disabled
Quote:
Originally Posted by Drakeo View Post
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?
 
Old 03-03-2014, 02:48 AM   #15
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,527

Rep: Reputation: 1217Reputation: 1217Reputation: 1217Reputation: 1217Reputation: 1217Reputation: 1217Reputation: 1217Reputation: 1217Reputation: 1217
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
USB drive not working properly, read only device? (USB key storage device) Arodef Linux - Hardware 14 01-01-2010 08:32 AM
Printing Problem with Epson Stylus Color 440 connected to usb port entm Linux - Hardware 1 07-16-2007 07:16 AM
cannot find usb port of my connected printer kpachopoulos Debian 1 08-10-2006 08:29 AM
Installing Linux on External hard drive connected by USB port omidiora Linux - Newbie 1 04-29-2006 10:51 AM
HP Deskjet (USB) & CUPS & Slackware 9.1: Unable to open USB device "usb:/dev/usb/lp0&qu arnostienen Slackware 2 01-29-2004 04:22 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration