Skip to content

Instantly share code, notes, and snippets.

@weshouman
Created October 20, 2022 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weshouman/c9d6e8e1e875b15928005a70fedb14fa to your computer and use it in GitHub Desktop.
Save weshouman/c9d6e8e1e875b15928005a70fedb14fa to your computer and use it in GitHub Desktop.
# follow https://unix.stackexchange.com/a/634849
# Usage
# getdevicename 1234:abcd
# getdevicenum 1234:abcd
# where
# 1234 is the IdVendor
# abcd is the IdProduct
# Those values could be captured using lsusb
getdevicename() {
idV=${1%:*}
idP=${1#*:}
for path in `find /sys/ -name idVendor 2>/dev/null | rev | cut -d/ -f 2- | rev`; do
if grep -q $idV $path/idVendor; then
if grep -q $idP $path/idProduct; then
find $path -name 'device' | grep block | rev | cut -d / -f 2 | rev
fi
fi
done
}
getdevicenum() {
idV=${1%:*}
idP=${1#*:}
for path in `find /sys/ -name idVendor 2>/dev/null | rev | cut -d/ -f 2- | rev`; do
if grep -q $idV $path/idVendor; then
if grep -q $idP $path/idProduct; then
find $path -name 'device' | rev | cut -d / -f 8 | rev | grep "-" | head -n1
fi
fi
done
}
simple:
source get_device.sh
getdevicename 1234:abcd
getdevicenum 1234:abcd
script:
#Device VENDOR ID
: ${DEVICE_VENDOR:="1234"}
#Device PRODUCT ID
: ${DEVICE_PRODUCT:="abcd"}
source ./get_device.sh
DEVICE_PATH=`getdevicename ${DEVICE_VENDOR}:${DEVICE_PRODUCT}`
echo "Device Path is $DEVICE_PATH"
cat /sys/bus/usb/devices/`getdevicenum ${DEVICE_VENDOR}:${DEVICE_PRODUCT}`/power/autosuspend
@weshouman
Copy link
Author

Example

I use the getdevicenum to automate disabling the auto-suspend, through the /etc/rc.local as instructed by this answer

#!/bin/bash

DEVICE_VENDOR=xxxx
DEVICE_PRODUCT=xxxx

getdevicenum() {
    idV=${1%:*}
    idP=${1#*:}
    for path in `find /sys/ -name idVendor 2>/dev/null | rev | cut -d/ -f 2- | rev`; do
        if grep -q $idV $path/idVendor; then
            if grep -q $idP $path/idProduct; then
                find $path -name 'device' | rev | cut -d / -f 8 | rev | grep "-" | head -n1
            fi
        fi
    done
}

echo setting /sys/bus/usb/devices/`getdevicenum ${DEVICE_VENDOR}:${DEVICE_PRODUCT}`/power/autosuspend to -1
echo -1 > /sys/bus/usb/devices/`getdevicenum ${DEVICE_VENDOR}:${DEVICE_PRODUCT}`/power/autosuspend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment