5

After plugging the USB to the port of my machine, if I do lsusb I see the device I am looking for:

Bus 001 Device 004: ID 04f2:b573 Some Manufacturer

If I list the devices under /dev/, I can see lots of device paths, such as /dev/tty0, /dev/ttyUSB0... etc. For what I understand, one of these paths will be my USB device, or be where my USB device is connected to.

How do I know which one is the one with the ID 04f2:b573?


This question is similar to mine, however (referring to the accepted answer) I don't know what exactly is $ID_SERIAL, or how to I set it to be equal to the Id I'm looking for, or even what is the correct path (since his example shows multiple device paths, such as /dev/ttyACM0, /dev/sdb, /dev/input/event5 ...)

Also, the second answer assumes that I know the device is connected to /dev/ttyUSB0, which I don't know beforehand (testing it on this specific path gives me no device, by the way).

Bersan
  • 152

3 Answers3

5

How do I know which one is the one with the ID 04f2:b573?

There may be better ways to do it, but a quick and dirty way to do it is with:

find_by_id(){
    v=${1%:*}; p=${1#*:}  # split vid:pid into 2 vars
    v=${v#${v%%[!0]*}}; p=${p#${p%%[!0]*}}  # strip leading zeros
    grep -il "^PRODUCT=$v/$p" /sys/bus/usb/devices/*:*/uevent |
    sed s,uevent,, |
    xargs -r grep -r '^DEVNAME=' --include uevent
}
find_by_id 04f2:b573

My old Huawei phone appears as two serial ttys:

$ find_by_id 12d1:
/sys/bus/usb/devices/3-1:1.0/ttyUSB0/tty/ttyUSB0/uevent:DEVNAME=ttyUSB0
/sys/bus/usb/devices/3-1:1.1/ttyUSB1/tty/ttyUSB1/uevent:DEVNAME=ttyUSB1

You can change the last grep to

xargs -r egrep -rB2 '^DEVNAME=|^IFINDEX=' --include uevent

in order to also find the usb network interfaces:

$ find_by_id 0e8d:
/sys/bus/usb/devices/1-6.2.3:1.0/net/usb0/uevent-INTERFACE=usb0
/sys/bus/usb/devices/1-6.2.3:1.0/net/usb0/uevent:IFINDEX=10

$ find_by_id 03f0:
/sys/bus/usb/devices/1-6.3:1.1/usbmisc/lp1/uevent-MAJOR=180
/sys/bus/usb/devices/1-6.3:1.1/usbmisc/lp1/uevent-MINOR=1
/sys/bus/usb/devices/1-6.3:1.1/usbmisc/lp1/uevent:DEVNAME=usb/lp1
2

The answer by @mosvy does not work on Debian systems because /sys/bus/usb/devices contains symlinks. It also will not work by following symlinks with grep -R because that filesystem contains symlink loops.

A simpler way to get what you need is:

lsusb | grep NameOfYourDevice | sed -nr 's|Bus (.*) Device ([^:]*):.*|/dev/bus/usb/\1/\2|p'

It converts the output of lsusb (from the usbutils package) to the /dev/bus/usb/... path

If you need a block/character device name, try something like this:

device() {
    v=${1%:*}; p=${1#*:}
    for dir in `find /sys/ -name idVendor | rev | cut -d/ -f 2- | rev`; do
        if grep -q $v $dir/idVendor; then
            if grep -q $p $dir/idProduct; then
                find $dir -name 'device' | rev | cut -d / -f 2 | rev
            fi
        fi
    done
}
sicvolo
  • 451
0

I am not sure whether it works on all *unix system. But in my computer, I can visit /dev/bus/{BUSID}/{DEVICEID}. The bus id and device id are what lsusb gives.

wznmickey
  • 101
  • Thanks for info. I also find the device path under "/dev/bus/usb" but I couldn't read using "cat" command. Was you able to read? (ref: https://gpswebshop.com/blogs/tech-support-by-os-linux/how-to-connect-an-usb-gps-receiver-with-a-linux-computer) – Cloud Cho May 25 '23 at 23:33
  • @CloudCho I did not try to read it by cat. I need it to change it to 777 to let other users use it becuase when the default only allow root. I think whether you can use cat is determined by how the USB device works. The reference link you give use /dev/ttyACM0,. It is used for UART I guess. – wznmickey May 29 '23 at 14:39
  • does not work in Ubuntu 23.04 – Steve Cohen Oct 23 '23 at 19:46