18

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.

3 Answers 3

12

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;}'`
11

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)

In the output from the command you will 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
4
  • 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/ Sep 7, 2012 at 14:37
  • Thanks for the correction--I've edited the answer with that change, and added a find command to print the information. Sep 7, 2012 at 14:57
  • @JohnSGruber Ok, but how to mount it?!
    – Dr.jacky
    Jan 14, 2016 at 7:24
  • 1
    @Mr.Hyde: You can't mount a /dev/ttyUSB; only some device that operates like a disk drive can be mounted. Feb 1, 2016 at 22:18
2

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

udevadm info -a -p /sys/bus/usb-serial/devices/<specific tty>/

  for example

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

  last slash is optional

If not having a component, you will see "syspath not found"

In many cases, this command brought same information with udevadm info --name=/dev/<specific tty> --attribute-walk

1
  • Please remove the trailing slash. It gives "syspath not found".
    – nvd
    Oct 26, 2017 at 15:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .