Skip to content

Instantly share code, notes, and snippets.

@filviu
Created December 5, 2018 09:18
Show Gist options
  • Save filviu/47f7febe0cb3b03e538a81e9b2db2a60 to your computer and use it in GitHub Desktop.
Save filviu/47f7febe0cb3b03e538a81e9b2db2a60 to your computer and use it in GitHub Desktop.
USB reset on linux

unfortunately I haven't noted exactly where I lifted this from

Avoiding reboot: Resetting USB on a Linux machine This post was written by eli on February 1, 2013 Posted Under: Linux Every now and then, some USB device misbehaves badly enough to knock out the entire interface, to the extent that the system doesn’t detect any new USB devices. Or work so well with the existing ones, for that matter. The solution for me until now was to reboot the computer. But hey, I don’t like rebooting Linux! So this script (I call it usbreset) performs a reset to the USB drivers as necessary to bring them back to life. As a side effect, an equivalent to unplugging and replugging all USB devices takes place. If possible, unmount any connected USB storage device (e.g. disk on key) before doing this. Also, if you’re in the middle of printing through a USB device, or some other operation, this will not be graceful. Frankly speaking, I don’t understand exactly how and why this works, and I can’t even tell if this is the smoothest way to solve the issue. It doesn’t look like there are any adverse side effects, but I can’t know. On my machine, this looked just like the USB devices going off and on the bus (in /var/log/messages). An interesting thing I noted, was that if there’s a really problematic device on the bus (for example, a device that fails to enumerate), this script doesn’t return. It probably gets stuck on one of the writes to “bind” (hasn’t checked that all the way through). Use at your own risk. You are root, you should know what you’re doing (or gamble like myself). Script follows. #!/bin/bash

if [[ $EUID != 0 ]] ; then echo This must be run as root! exit 1 fi

for xhci in /sys/bus/pci/drivers/?hci_hcd ; do

if ! cd $xhci ; then echo Weird error. Failed to change directory to $xhci exit 1 fi

echo Resetting devices from $xhci...

for i in ????:??:??.? ; do echo -n "$i" > unbind echo -n "$i" > bind done done

The script above is in fact a newer version of the script, which I updated to in June 2014, after upgrading my kernel to 3.12. The older one, to which some of the comments relate, is this one: #!/bin/bash

SYSEHCI=/sys/bus/pci/drivers/ehci_hcd SYSUHCI=/sys/bus/pci/drivers/uhci_hcd

if [[ $EUID != 0 ]] ; then echo This must be run as root! exit 1 fi

if ! cd $SYSUHCI ; then echo Weird error. Failed to change directory to $SYSUHCI exit 1 fi

for i in ????:??:??.? ; do echo -n "$i" > unbind echo -n "$i" > bind done

if ! cd $SYSEHCI ; then echo Weird error. Failed to change directory to $SYSEHCI exit 1 fi

for i in ????:??:??.? ; do echo -n "$i" > unbind echo -n "$i" > bind done The problem with the old script is that the EHCI subdirectory doesn’t appear on my system (maybe because there’s no EHCI device currently attached) and it seems like it appears as xhci on some systems. The new script catches all cases.

# by LC@ZW
#
# this script (I call it usbreset) performs a reset to the USB drivers as necessary to bring them back to life. As a side effect, an equivalent to unplugging and replugging all USB devices takes place.
cd /sys/bus/pci/drivers/xhci_hcd
echo -n "0000:0e:00.0" >unbind
echo -n "0000:0e:00.0" >bind
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment