Map physical USB device path to the Bus/Device number returned by lsusb
Firstly, we need to prepend /sys
to the path returned by udev
, so that path becomes something like: /sys/devices/pci0000:00/0000:00:1d.0/usb5/5-2
. Then go to this directory, and there will be several files in it. Among others, there are busnum
and devnum
files, they contain these "logical" numbers. So, in bash script, we can retrieve them like that:
devpath='/devices/pci0000:00/0000:00:1d.0/usb5/5-2'
busnum=$(cat "/sys/$devpath/busnum")
devnum=$(cat "/sys/$devpath/devnum")
# we might want to make busnum and devnum have leading zeros
# (say, "003" instead of "3", and "012" instead of "12")
busnum=$(printf %03d $busnum)
devnum=$(printf %03d $devnum)
# now, we can retrieve device data by lsusb -D /dev/bus/usb/$busnum/$devnum
echo "busnum=$busnum, devnum=$devnum"
Also note that udev
can return these busnum
and devnum
directly: in RUN+="..."
we can use substitutions $attr{busnum}
and $attr{devnum}
respectively.