From: Samir Benmendil Date: Thu, 27 Dec 2018 13:47:21 +0000 (+0000) Subject: bin: add script that resets a usb device given vendor/product id X-Git-Url: https://git.rmz.io/dotfiles.git/commitdiff_plain/652a0e26c85885f9edb1532f039e326949f758ae bin: add script that resets a usb device given vendor/product id Mainly used to reset xbox controller after suspend. --- diff --git a/bin/reset-usb b/bin/reset-usb new file mode 100755 index 0000000..c392110 --- /dev/null +++ b/bin/reset-usb @@ -0,0 +1,66 @@ +#!/usr/bin/env bash + +version="0.1" + +function usage() +{ + echo "Usage : $0 [options] [:] + + Reset usb device identified by idVendor:idProduct. + + Options: + -h,--help Display this message + -v,--version Display script version" +} + +function check_ids() +{ + local syspath=$1 + [[ -n $idVendor && "$idVendor" != $(cat $syspath/idVendor 2>/dev/null) ]] && return 1 + [[ -n $idProduct && "$idProduct" != $(cat $syspath/idProduct 2>/dev/null) ]] && return 1 + return 0 +} + +function main() +{ + for syspath in /sys/bus/usb/devices/*; do + if ! check_ids $syspath; then + continue + fi + + echo "Resetting '$syspath'" + echo 0 | sudo tee $syspath/authorized >/dev/null + echo 1 | sudo tee $syspath/authorized >/dev/null + done +} + +# Parse arguments +declare -a args +while [[ $# -gt 0 ]]; do + opt="$1" + + case $opt in + -h|--help) usage; exit 0 ;; + -v|--version) echo "$0 -- Version $version"; exit 0 ;; + -* ) + echo -e "\n Option does not exist : $opt\n" + usage; exit 1 ;; + + *) args+=("$opt"); shift ;; + esac +done + +if [[ ${#args[@]} != 1 ]]; then + usage + exit 1 +fi + +if [[ ${args[0]} =~ ([^:]+):([^:]+) ]]; then + idVendor=${BASH_REMATCH[1]} + idProduct=${BASH_REMATCH[2]} +else + idProduct=${args[0]} +fi + +sudo -v +main