Skip to content

Instantly share code, notes, and snippets.

@planetceres
Last active March 16, 2024 01:24
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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
@xkr47
Copy link

xkr47 commented Oct 14, 2021

I would like to add that (at least some) Thunderbolt3 ports (which also support usb 3.1) do not have xHCI in their name, but the "All USB" version works for them too:

2c:00.0 USB controller: Intel Corporation JHL7540 Thunderbolt 3 USB Controller [Titan Ridge 4C 2018] (rev 06)

Thank you very much for not having to reboot my laptop anymore when this happens! For me the issue sometimes occurs when waking up the laptop from suspend. I do not know if there is a power overdraw involved, but at least I cannot find "-110" or "power" or "overdraw" anywhere in the journalctl -S "1 hour ago" logs..

@tilaktilak
Copy link

Hi there, tested on Ubuntu 20.04 and it worked, thanks !

@RomanHiden
Copy link

worked on Description: Ubuntu 20.04.4 LT

@yeswalrus
Copy link

Same issue on Ubuntu 20.04.4 LT with an HP laptop - after going to sleep, it wasn't recognizing the kb/mouse attached through the thunderbolt3 monitor hub, and would fail to detect the monitor being plugged/unplugged at all. After running this script, the KB & mouse are both recognized and hot-swapping the monitor works again

@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