Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is it possible to reset the connection of a USB device, without physically disconnecting/connecting from the PC?

Specifically, my device is a digital camera. I'm using gphoto2, but lately I get "device read errors", so I'd like to try to do a software-reset of the connection.

From what I can tell, there are no kernel modules being loaded for the camera. The only one that looks related is usbhid.

share|improve this question
    
Which version of Ubuntu are you using? –  User Aug 1 '10 at 20:15
    
i tried both solutions by Li Lo and ssokolow, all i get is permission denied, nomatter if i use the usbreset code or the command line "echo 0 > ..." i use sudo, also my usb devices are owned by root but i can use them without admin rights(cameras..) –  phil Jun 8 at 16:40
    
If you are getting read errors, you might have some data corruption. If your camera uses an external memory card (such as MicroSD), it might be wise to connect it to the computer and run fsck. –  TSJNachos117 Jun 8 at 18:47

7 Answers 7

up vote 32 down vote accepted

Save the following as usbreset.c

/* usbreset -- send a USB port reset to a USB device */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>

#include <linux/usbdevice_fs.h>


int main(int argc, char **argv)
{
    const char *filename;
    int fd;
    int rc;

    if (argc != 2) {
        fprintf(stderr, "Usage: usbreset device-filename\n");
        return 1;
    }
    filename = argv[1];

    fd = open(filename, O_WRONLY);
    if (fd < 0) {
        perror("Error opening output file");
        return 1;
    }

    printf("Resetting USB device %s\n", filename);
    rc = ioctl(fd, USBDEVFS_RESET, 0);
    if (rc < 0) {
        perror("Error in ioctl");
        return 1;
    }
    printf("Reset successful\n");

    close(fd);
    return 0;
}

The run the following commands in terminal:

  1. Compile the program:

    $ cc usbreset.c -o usbreset
    
  2. Get the Bus and Device ID of the USB device you want to reset:

    $ lsusb  
    Bus 002 Device 003: ID 0fe9:9010 DVICO  
    
  3. Make our compiled program executable:

    $ chmod +x usbreset
    
  4. Execute the program with sudo privilege; make necessary substitution for <Bus> and <Device> ids as found by running the lsusb command:

    $ sudo ./usbreset /dev/bus/usb/002/003  
    

Source of above program: http://marc.info/?l=linux-usb&m=121459435621262&w=2

share|improve this answer
    
I got errors like this: ./usbreset: command not found Dan 11.04 Natty –  user24895 Sep 13 '11 at 4:33
    
Thank you so much! This will help me get a lot more life out of my dying Intellimouse. –  Randall Ma Dec 15 '13 at 0:55
1  
This works with ubuntu 13.10. The device ID can vary. TO get it for the mouse I have wrapped above code in few shell commands echo $(lsusb | grep Mouse) mouse=$( lsusb | grep Mouse | perl -nE "/\D+(\d+)\D+(\d+).+/; print qq(\$1/\$2)") sudo /path/to/c-program/usbreset /dev/bus/usb/$mouse –  knb Dec 22 '13 at 11:04
    
my external drive seems to become undetectable (I have to hard reconnect the usb cable); it is a usb2.0 connected on a usb3.0 desktop PC port; when I run usbreset /dev/bus/usb/011/001 that is one of the 2 usb 3.0 root hubs at lsusb, it errors: "Error in ioctl: Is a directory", any ideia? I tried on both usb 3.0 hubs –  Aquarius Power Oct 30 at 3:34

Quickest way to reset will be to reset the USB controller itself. Doing so will enforce udev to unregister the device on disconnection, and register is back once you enable it.

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

This should work for most PC environment. However, if you are using some custom hardware you can simply iterate through the device names. With this method you don't need to find out the device name by lsusb. You can incorporate in a automated script as well.

share|improve this answer

Maybe this guide can help you:

If you are pestered by the bug that doesn;t let you mount USB devices in Ubuntu Lucid Lynx, the issue is caused by the floppy module. Disable it with:

sudo modprobe -r floppy

After a reboot the module will likely reload.

share|improve this answer

This will reset all of USB2/3 attached ports[1]:

# reseting USB2 ports
for i in $(ls /sys/bus/pci/drivers/ehci_hcd/|grep :)
 do echo $i >/sys/bus/pci/drivers/ehci_hcd/unbind
 echo $i >/sys/bus/pci/drivers/ehci_hcd/bind
done
# reseting USB3 ports (if there none you'll get errors)
for i in $(ls /sys/bus/pci/drivers/xhci_hcd/|grep :)
 do echo $i >/sys/bus/pci/drivers/xhci_hcd/unbind
 echo $i >/sys/bus/pci/drivers/xhci_hcd/bind
done

I believe this will solve your problem. If you do not want to reset all of the USB endpoints, you can use appropriate device ID from /sys/bus/pci/drivers/ehci_hcd


Notes: [1]: the ehci_hcd and xhci_hcd kernel drivers typically control the USB ports. ehci_hcd is for USB2 ports and xhci_hcd is for USB3 ports.

P.S. It solved my (ndemou) problems on a Dell XPS13 notebook were the USB controller (especially the USB3 one) was locking up every few days or weeks. In case it helps someone else lspci on my ubuntu 12.04 reports these USB controllers:

  • Fresco Logic FL1009 USB 3.0 Host Controller (rev 02)
  • Intel Corporation 6 Series/C200 Series Chipset Family USB Enhanced Host Controller #1 (rev 05)

(Sorry for the PS but I don't have enough reputation to add a comment -- still I think it might help someone else googling for the same notebook/controllers)

share|improve this answer
    
do you believe it may work to wakeup an usb storage? –  Aquarius Power Jun 30 at 5:53
    
Although I've received the following message: ls: cannot access /sys/bus/pci/drivers/ehci_hcd/: No such file or directory this has resolved the issue, the mouse has started working immediately. +1 –  Attila Fulop Oct 10 at 6:16

Try this, it's a software unplug (Eject).

Sometimes doesn't work simply unbind device for some devices.

Example:

I want to remove or eject my "Genius NetScroll 120".

Then i first Check my attached usb device

$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 003: ID 03f0:231d Hewlett-Packard 
Bus 001 Device 004: ID 138a:0007 Validity Sensors, Inc. VFS451 Fingerprint Reader
Bus 001 Device 005: ID 04f2:b163 Chicony Electronics Co., Ltd 
Bus 002 Device 009: ID 0458:003a KYE Systems Corp. (Mouse Systems) NetScroll+ Mini Traveler / Genius NetScroll 120  **<----This my Mouse! XDDD**

Ok, i found my mouse, it's has a Bus 002, Device 009, idVendor 0458 and idProduct 003a, so this is a reference device info about the mouse.

This is important, the Bus number is the begin name path to device and i will check the product Id and Vendor to ensure the correct device to remove.

$ ls /sys/bus/usb/drivers/usb/
1-1/    1-1.1/  1-1.3/  1-1.5/  2-1/    2-1.3/  bind    uevent  unbind  usb1/   usb2/

Pay atention on the folders, check the begining with folder number 2, i will check this one because my Bus is 002, and one by one i have check each folder containing the correct idVendor and idProduct about my mouse info.

In this case, i will retrieve the info with this command:

cat /sys/bus/usb/drivers/usb/2-1.3/idVendor
0458
cat /sys/bus/usb/drivers/usb/2-1.3/idProduct
003a

Ok, the path /sys/bus/usb/drivers/usb/2-1.3/ match with my info mouse! XDDD.

It's time to remove the device!

su -c "echo 1 > /sys/bus/usb/drivers/usb/2-1.3/remove"

Plug again the usb device and it's work again!

share|improve this answer
    
What if you can't plug it in again? (for example it's an internal sdcard reader) –  aleb Jun 29 at 20:57

Perhaps this works for a camera, too:

Following revived a starved USB 3.0 HDD on a 3.4.42 (kernel.org) Linux on my side. dmesg told, that it was timing out commands after 360s (sorry, I cannot copy the syslog here, not connected networks) and the drive hung completely. Processes accessing the device were blocked in the kernel, unkillable. NFS hung, ZFS hung, dd hung.

After doing this, everything worked again. dmesg told just a single line about the USB device found.

I really have no idea what following does in detail. But it worked.

The following example output is from Debian Squeeze with 2.6.32-5-686 kernel, so I think it works for 2.6 and above:

$ ls -al /dev/sdb
brw-rw---T 1 root floppy 8, 16 Jun  3 20:24 /dev/sdb

$ ls -al /sys/dev/block/8:16/device/rescan
--w------- 1 root root 4096 Jun  6 01:46 /sys/dev/block/8:16/device/rescan

$ echo 1 > /sys/dev/block/8:16/device/rescan

If this does not work, perhaps somebody else can figure out how to send a real reset to a device.

share|improve this answer

I haven't found myself in your specific circumstances before, so I'm not sure if it'll do enough, but the simplest way I've found to reset a USB device is this command: (No external apps necessary)

sudo sh -c "echo 0 > /sys/bus/usb/devices/1-4.6/authorized"

That's the actual one I use to reset my Kinect since libfreenect seems to have no API for putting it back to sleep. It's on my Gentoo box, but the kernel should be new enough to use the same path structure for sysfs.

Yours obviously wouldn't be 1-4.6 but you can either pull that device path from your kernel log (dmesg) or you can use something like lsusb to get the vendor and product IDs and then use a quick command like this to list how the paths relate to different vendor/product ID pairs:

for X in /sys/bus/usb/devices/*; do 
    echo "$X"
    cat "$X/idVendor" 2>/dev/null 
    cat "$X/idProduct" 2>/dev/null
    echo
done
share|improve this answer
    
sh: 1: cannot create /sys/bus/usb/devices/1-3.1:1.0/authorized: Directory nonexistent –  Nicolas Marchildon May 31 '12 at 3:22
    
It looks like they've changed the layout of the usbfs filesystem. I'll try to figure out what the new way of doing things is on Ubuntu once I'm not so sleepy. –  ssokolow Jun 2 '12 at 15:46
    
Thank you worked great! Maybe you should also mention to perform a echo 1 > /sys/bus/usb/devices/whatever/authorized inside a script to re-enable the device as soon as it has been disabled. I did it on both my mouse and usb keyboard and I ended up with a completely deaf system :) –  Avio Apr 28 '13 at 8:43
    
Last time I used it, something in the system would reset it to 1 a second or two later without me having to do it manually. –  ssokolow Apr 29 '13 at 20:33
    
It's extremely strange if it automatically re-set the value to 1 as setting it to 0 is telling the system you don't want the device to be "authorized" and therefore inaccessible. –  Tim Tisdall Oct 18 '13 at 19:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.