Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need to write an script for restart USB dongles. I have all tools but I can't link my /dev/ttyUSBx to physical BUS and DEVICE. An issue is that I have three dongles with the same id vendor and id product.

If I do lsusb the output is:

Bus 001 Device 004: ID 12d1:1003 Huawei Technologies Co., Ltd. E220 HSDPA Modem / E270 HSDPA/HSUPA Modem
Bus 001 Device 006: ID 12d1:1003 Huawei Technologies Co., Ltd. E220 HSDPA Modem / E270 HSDPA/HSUPA Modem
Bus 001 Device 007: ID 12d1:1003 Huawei Technologies Co., Ltd. E220 HSDPA Modem / E270 HSDPA/HSUPA Modem
Bus 001 Device 002: ID 80ee:0021
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

And I have attached it to:

/dev/ttyUSB0
/dev/ttyUSB3
/dev/ttyUSB5

But I want to know which device is related with which Bus Device. By example I need to get the following:

/dev/ttyUSB0 -> Bus 001 Device 006
/dev/ttyUSB3 -> Bus 001 Device 004
/dev/ttyUSB5 -> Bus 001 Device 007

I'm using Ubuntu Server 10.04 and I tested the tools:

lsusb
hal
lsmod

But I can't get the relationship.

share|improve this question
add comment

3 Answers 3

up vote 4 down vote accepted

Since I don't have your hardware I can't give you an exact answer.

I'd suggest that you go to the device in question, for example:

ls -l /dev/ttyUSB0

and obtain the major and minor node numbers. Let's say they are 116, 7

Then go to:

ls -l /sys/dev/char/116:7

(char, because a tty is a character device, if you were tracing down a disk device you would specify block instead of char

and see something a little like:

lrwxrwxrwx 1 root root 0 Sep 6 00:17 116:7 -> ../../devices//sys/devices/pci0000:00/0000:00:13.5/pci0000:00/0000:00:13.5/usb1/1-3/1-3:1.1/sound/card1/pcmC1D0c

The directory /sys/devices/pci0000:00/0000:00:13.5/usb1/1-3/ and its subdirectories have much information on the device. As an example, /sys/devices/pci0000:00/0000:00:13.5/usb1/1-3/busnum and /sys/devices/pci0000:00/0000:00:13.5/usb1/1-3/devnum. On my system these match the bus and device number mentioned in the device hotplug entries in /var/log/syslog.

Something else that works on my system for /dev/video0:

find /sys/dev -lname '*video0' -exec cat {}/device/busnum {}/device/devnum \; 2>/dev/null
share|improve this answer
    
Thank you very much!, I have fighting with this for many time. Just for further visitors: In my ubuntu Ubuntu 10.04.2 LTS the device directory is /sys/dev/char/ instead of /sys/char/ –  Ernesto Campohermoso Sep 7 '12 at 14:37
    
Thanks for the correction--I've edited the answer with that change, and added a find command to print the information. –  John S Gruber Sep 7 '12 at 14:57
add comment

There really isn't a good way to map character devices like /dev/ttyUSB0 to the corresponding USB device and bus numbers. BUT, it is possible!

Something like this might work:

udevadm info --name=/dev/ttyUSB0 --attribute-walk

From there, you'll get a bunch of information about the device and its parent devices. Now, it's just all about parsing those data to get what you want. I've used this in the past:

echo /dev/bus/usb/`udevadm info --name=/dev/ttyUSB0 --attribute-walk | sed -n 's/\s*ATTRS{\(\(devnum\)\|\(busnum\)\)}==\"\([^\"]\+\)\"/\4/p' | head -n 2 | awk '{$1 = sprintf("%03d", $1); print}'` | tr " " "/"

Now, if that's not a mouthful, I dunno what is! Let's break it down:

udevadm info --name=/dev/ttyUSB0 --attribute-walk - returns info about the device and its parent devices

sed -n 's/\s*ATTRS{\(\(devnum\)\|\(busnum\)\)}==\"\([^\"]\+\)\"/\4/p' - parses these data and returns any lines containing the "devnum" or "busnum" attribute. We'll assume that "busnum" is listed first... and in that case... we can just grab the first two lines of output.

head -n 2 - Grab the first 2 lines of output. We could get more fancy here, but we are just making an assumption that the closest parent's "busnum" and "devnum" are going to be listed first.

awk '{$1 = sprintf("%03d", $1); print}' - Takes those digits and pads them with zeros.

echo /dev/bus/usb... - all this jazz is wrapped up into an echo statement to replace the newlines with spaces. You can add the -n to echo if you want to cut off the trailing newline.

tr " " "/" - replace the spaces in the output with forward slashes

This will output something like:

/dev/bus/usb/001/011

... which, of course, is the device. If you'd like, you can easily output something like busnum:001 devnum:011 with a few tweaks:

echo `udevadm info --name=/dev/ttyUSB0 --attribute-walk | sed -n 's/\s*ATTRS{\(\(devnum\)\|\(busnum\)\)}==\"\([^\"]\+\)\"/\1\ \4/p' | head -n 2 | awk '{$1 = sprintf("%s:%03d", $1, $2); print $1;}'`
share|improve this answer
add comment

You are able to find all information about node using below command:

udevadm info -a -p /sys/bus/usb-serial/devices/ttyUSB0/

May it helps you.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.