Skip to content

Instantly share code, notes, and snippets.

@skx

skx/usb-reset

Created Feb 14, 2014
Embed
What would you like to do?
#!/bin/sh
if [ "$(id -u)" != 0 ] ; then
echo This must be run as root!
exit 1
fi
#
# Look for the most comment cases
#
for driver in xhci_hcd ehci_hcd uhci_hcd ; do
#
# The directory to operate within
#
dir=/sys/bus/pci/drivers/${driver}
#
# If it exists we have the appropriate USB-driver
#
if [ -d $dir ]; then
echo "Reseting: ${driver}"
cd ${dir}
for dev_id in ????:??:??.? ; do
printf "${dev_id}" > unbind
printf "${dev_id}" > bind
done
else
echo "Not present: ${driver}"
fi
done
@skx

This comment has been minimized.

Copy link
Owner Author

@skx skx commented Feb 14, 2014

@fduraibi

This comment has been minimized.

Copy link

@fduraibi fduraibi commented Feb 27, 2014

The script should check also if an existing folder has something in it or not.
Since in my case i have both xhci_hcd and uhci_hcd. The USB dev id were only inside the xhci_hcd. However, for uhci_hcd the returned dev_id from the for loop has in it "????:??:??.?" since it didn't match anything so it assumed it was a string!
That was not a big deal but i was giving error messages since it could not find a folder named "????:??:??.?"

/usb-reset: line 28: printf: write error: No such device
/usb-reset: line 29: printf: write error: No such device

so i added an "if" statment inside the second for loop:

        for dev_id in ????:??:??.? ; do
            if [ "${dev_id}" == "????:??:??.?" ] ; then
                 break
            fi
            printf "${dev_id}" > unbind
            printf "${dev_id}" > bind
        done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.