Skip to content

Instantly share code, notes, and snippets.

@planetceres
Last active May 4, 2024 14:11
Show Gist options
  • Save planetceres/917840478e1e4d45f8373667630e51a0 to your computer and use it in GitHub Desktop.
Save planetceres/917840478e1e4d45f8373667630e51a0 to your computer and use it in GitHub Desktop.
Restart/reset USB kernel module in Ubuntu 18.04 without rebooting

Restart/reset USB kernel module in Ubuntu 18.04 without rebooting

Restart USB ports after a power overdraw (error -110). Unplug all devices prior to running script.

References:

All USB ports

for port in $(lspci | grep USB | cut -d' ' -f1); do
    echo -n "0000:${port}"| sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind;
    sleep 5;
    echo -n "0000:${port}" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind;
    sleep 5;
done

USB 3.1 Only

for port in $(lspci | grep xHCI | cut -d' ' -f1); do
    echo -n "0000:${port}"| sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind;
    sleep 5;
    echo -n "0000:${port}" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind;
    sleep 5;
done

Either my Dell Optiplex 990 computer or my Linux install has an issue: occasionally, when I re-plug a USB device into the system, it’ll cause a fault in the USB module in the kernel and USB goes dark. I’m unsure as to whether this is a hardware or software issue, but I’d simply like to restart my USB subsystem and continue working. When searching the web for ‘restart USB in Linux’ and ‘reload USB kernel module’, you get a plethora of results and none of which will work (seemingly due to how the Ubuntu standard kernel is compiled), at least for me within Ubuntu 12.04, Precise Pangolin. Until now, I’ve had no success and had to hard reset. No longer.

You’ll need root/sudo access to the machine to be able to run commands. In my case, without USB available, then I’ve either got to sprint for a PS/2 keyboard and mouse or login via SSH. You can do what I’ve done and prepared things into a suitable script I can run with just a Gnome launcher. Thanks to this fantastic post for the help. Either place the following into a script or run the commands directly:

echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind

The hardware identifiers being passed around here can be revealed using a command like lspci | grep USB. In my case, the identifiers in the original post were exactly what I have in my system.

I’m yet to see if my USB will correctly come back online after freezing up as it hasn’t happened yet, but I’ll try this when it does and report back. That said, the commands above definitely reload all USB devices attached to the system; that much I’ve tried.

#!/usr/bin/env bash
# USB 3.1 Only
for port in $(lspci | grep xHCI | cut -d' ' -f1); do
echo -n "0000:${port}"| sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind;
sleep 5;
echo -n "0000:${port}" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind;
sleep 5;
done
# All USB
for port in $(lspci | grep USB | cut -d' ' -f1); do
echo -n "0000:${port}"| sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind;
sleep 5;
echo -n "0000:${port}" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind;
sleep 5;
done
@jpolvora
Copy link

jpolvora commented Nov 1, 2022

it worked for me em kubuntu 22.04 LTS

@antroy-madetech
Copy link

Great fix thanks. This has been annoying me for a while now on Kubuntu 20.04 LTS

@yash10019coder
Copy link

great script works flawlessly without restarting the whole system

@deajan
Copy link

deajan commented Mar 31, 2023

I've built https://github.com/netinvent/usb_resetter than can restart devices / hubs or whole controllers
Install with pip install usb_resetter

Equivalent usage to the above statements:

usb_resetter --reset-all

@aceqbaceq
Copy link

it works , amazing!

@deajan
Copy link

deajan commented Sep 30, 2023

@aceqbaceq Who ever you're talking about...

@potistiri
Copy link

I have a dell laptop vostro 15 3000. Yet I don't understand WHY this is happened? Is this something that DELL have to release? Is this because of kernel support? How we can solve it without reset the usb connectors?

@deajan
Copy link

deajan commented Nov 7, 2023

@potistiri Can come from multiple problems, most of them are crappy usb devices and/or crappy/too long usb cables.
Nevertheless, you can always check if you have a BIOS update for your laptop, some of them may include fixes for USB 3 via UEFI drivers.

@corners2wall
Copy link

I get error with my mouse and when I run the script. Boom. Magic. All works fine.
Amazing worked on Ubuntu 22.04 lts

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