Skip to content

Instantly share code, notes, and snippets.

@jpetazzo
Created October 21, 2022 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpetazzo/bc83657e98e2344877a42c3d51d8b359 to your computer and use it in GitHub Desktop.
Save jpetazzo/bc83657e98e2344877a42c3d51d8b359 to your computer and use it in GitHub Desktop.
#!/bin/sh
# TODO:
# lookup missing ids from /usr/share/hwdata/usb.ids
# lookup bus controller ids (readlink -f . /.. on the bus controller)
# ... and display their PCI ID when available
# colorize output to highlight bus speeds maybe?
# add emojis to indicate device speeds?
drawtree() {
local INDENT=$1
local DEV=$2
[ -f $DEV/idVendor ] || return
DESC='???'
if [ -f $DEV/product ]; then
DESC="$(cat $DEV/product)"
else
DESC=$(lsusb -d $(cat $DEV/idVendor):$(cat $DEV/idProduct) | head -n1 | cut -d" " -f7-)
fi
if [ -f $DEV/manufacturer ]; then
DESC="$(cat $DEV/manufacturer) $DESC"
fi
PORTS="(leaf)"
if [ -f $DEV/maxchild ]; then
MAXCHILD=$(cat $DEV/maxchild)
if [ "$MAXCHILD" != 0 ]; then
PORTS="($MAXCHILD ports)"
fi
fi
# Special case for USB roots: try to show the matching PCI device
case $DEV in
usb*)
SYSDEV=$(find /sys/devices -name $DEV)
PCIDEV=$(basename $(dirname $SYSDEV) | cut -d: -f2-)
DESC=$(lspci -m | grep ^$PCIDEV | cut --delimiter='"' --fields=4,6 --output-delimiter=" ")
;;
esac
printf "%${INDENT}s- %s|%s:%s|%s|%s|%s|%s\n" \
"" \
$DEV \
$(cat $DEV/idVendor) \
$(cat $DEV/idProduct) \
$(cat $DEV/version) \
$(cat $DEV/speed) \
"$PORTS" \
"$DESC" \
#
cd $DEV
for SUB in [1-9]*; do
drawtree $((INDENT+2)) $SUB
done
cd ..
}
cd /sys/bus/usb/devices
{
for BUS in usb*; do
drawtree 0 $BUS
done
} | column -s '|' -t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment