Skip to content

Instantly share code, notes, and snippets.

@george-hawkins
Created July 23, 2017 13:08
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 george-hawkins/5d93be322379afdc690b976e4b12dd71 to your computer and use it in GitHub Desktop.
Save george-hawkins/5d93be322379afdc690b976e4b12dd71 to your computer and use it in GitHub Desktop.
#!/bin/bash
DRIVERS_DIR=/sys/bus/pci/drivers
# George Hawkins Nov 18th, 2014
# I found various scripts that bind and unbind the USB devices to reset the whole USB system.
# All used hardcoded slots and drivers - slots aren't consistent across machines and driver names change with OS releases.
# This script attempts to discover all values.
# This awk script finds all PCI devices with class 'USB controller' that have a slot and a driver.
# All devices should have a slot, but it's possible for them not to have a driver.
# Important: the quotes around EOF prevent dollar expansion in the here document.
read -d '' get_devices << 'EOF'
/^Slot:/ {
slot = $2
}
/^Class:.*USB controller/ {
usb = 1
}
/^Driver:/ {
if (usb && slot) {
print slot, $2
}
}
/^$/ {
slot = ""
usb = 0
}
EOF
# Note: rather than 0 read will return 1 as it uses this to signal reaching EOF.
function apply {
action=$1
shift
while (( "$#" ))
do
slot=$1
driver=$2
shift
shift
echo -n $slot > $DRIVERS_DIR/$driver/$action
done
}
devices=$(lspci -vmmkD | awk "$get_devices")
apply unbind $devices
apply bind $devices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment