How do I reset an USB device without unplugging it in Linux?

6

8

Every now and then there might come a need to reset a USB device, is there a way to perform the reset in software without unplugging the device itself and then pluggin it back in?

More specifically I have a webcam which gets confused when playing too much with the settings in guvcapture and then needs a reset to get back on track.

Grumbel

Posted 2010-05-17T01:31:53.813

Reputation: 3 100

Answers

9

You could try Benjamin Close's resetusb program to reset all devices — there are no binaries available, but compiling it is rather easy. Save the source code as resetusb.c, then run:

gcc -lusb resetusb.c -o resetusb

You can now run the tool as resetusb. Alternatively, @unhammer points to Alan Stern's single-device version (plus some hints on how to use it).

Some people have also had luck just removing and modprobe-ing the relevant modules:

modprobe -vr ehci_hcd
modprobe -v ehci_hcd

(you could of course script this)

Some distributions may also have their own tools to restart the USB subsystem; Mandrake apparently has /etc/init.d/usb.

supervacuo

Posted 2010-05-17T01:31:53.813

Reputation: 531

$ sudo modprobe -vr ehci_hcd results in modprobe: FATAL: Module ehci_hcd is builtin. on kubuntu trusty. – naught101 – 2014-06-28T03:42:27.050

On my laptop with debian 8, ehci_hcd gives errors. However, rmmod ehci_pci && sleep 2 && modprobe ehci_pci works perfectly. – Bharat G – 2015-10-12T14:16:12.753

The modprobe trick did the job on CentOS 4 :-( , saved my day ! – Open SEO – 2016-06-09T13:09:10.650

http://askubuntu.com/questions/645/how-do-you-reset-a-usb-device-from-the-command-line has a similar program for resetting a single usb device – unhammer – 2013-12-31T14:34:59.660

4

Let's say I want to reset /dev/sdc.

# udevadm info -q all /dev/sdc | grep DEVPATH
E: DEVPATH=/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1.4/2-1.4:1.0/host2/target2:0:0/2:0:0:0/block/sdc

I take the 2-1.4 above (yours might just be 2-1 - my device is plugged into a hub) and do:

# echo 2-1.4 > /sys/bus/usb/drivers/usb/unbind
# echo 2-1.4 > /sys/bus/usb/drivers/usb/bind

Tom Hale

Posted 2010-05-17T01:31:53.813

Reputation: 1 348

1I've made a one-liner(-ish) for that:

DEV="sdc"; USB=$(udevadm info -q all /dev/$DEV | grep DEVPATH | grep -o '/usb[1-9]*/[1-9,-]*' | cut -d'/' -f3); echo $USB > /sys/bus/usb/drivers/usb/unbind; echo $USB > /sys/bus/usb/drivers/usb/bind

Make DEV the drive you want to reset, and run - it'll extract the USB port number and unbind/bind it (untested). – unfa – 2019-09-11T10:56:04.743

1

You can restart the hardware abstraction layer: sudo /etc/init.d/hal restart

Nerdfest

Posted 2010-05-17T01:31:53.813

Reputation: 808

2i think HAL is replaced by something else in the newest version of ubuntu.. so its depend of distrib.. – bAN – 2010-08-17T20:29:11.527

1I seriously doubt it would have worked anyway. HAL wasn't a low-level abstraction layer like the Windows component of the same name. – sourcejedi – 2012-12-10T12:18:50.070