USB devices are anywhere nowadays, even many embedded devices replace the traditional serial devices with usb devices. However, I experienced that USB devices hang from time to time. In most cases, a manual unplug and replug will solve the issue. Actually, usb reset can simulate the unplug and replug operation.
First, get the device path for your usb device. Enter the command lsusb will give you something similar as below,
Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 006 Device 002: ID 04b3:310c IBM Corp. Wheel Mouse Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 004 Device 002: ID 0a5c:2145 Broadcom Corp. Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Use the IBM Wheel Mouse as an example, the device node for it is /dev/bus/usb/006/002, where 006 is the bus number, and 002 is the device number.
Second, apply ioctl operation to reset the device. This is done in C code,
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
void main(int argc, char **argv)
{
const char *filename;
int fd;
filename = argv[1];
fd = open(filename, O_WRONLY);
ioctl(fd, USBDEVFS_RESET, 0);
close(fd);
return;
}
Save the code above as reset.c, then compile the code using
gcc -o reset reset.c
This will produce the a binary named reset. Again, using the wheel mouse as an example, execute the following commands,
sudo ./reset /dev/bus/usb/006/002
You can take a look at the message by,
tail -f /var/log/messages
On my Ubuntu desktop, the last line reads,
May 4 16:09:17 roman10 kernel: [ 1663.013118] usb 6-2: reset low speed USB device using uhci_hcd and address 2
This reset operation is effectively the same as you unplug and replug a usb device.
For another method of reset usb using libusb, please refer here
Reference: http://forums.x-plane.org/index.php?app=downloads&showfile=9485.
Coool, thanx a lot !
My problems is that after a while all USBs dissappear. How do you restart that whtiout having to restart the computer itself.
Excellent! Worked like a charm! Thanks a lot.
I have some improvements.
1): add the line for debug info (so the program shows it did something)
printf(“Resetting %sn”, argv[1]);
2): bash script to automate
#!/bin/sh
/path.to/usbreset /dev/bus/usb/`lsusb | grep 12d1:1003 | awk ‘{ print $2″/”$4}’ | sed -e ‘s/://’`
Where 12d1:1003 is the ID of USB hardware because is not always connected to same bus, but always have same ID. So replace 12d1:1003 with your own usb hardware id.
Regards!
life-saver. Works perfectly to reset buggy usb-cams.
Great! Finally, I can kick sleepy usb wifi ID 0bda:8171 Realtek Semiconductor Corp. RTL8188SU 802.11n WLAN Adapter in Ubuntu Natty
Thanks, this trick works fine!!
Works perfect!
Nice hack. Thanks. Works great for reinitializing my LCD2USB controller.
Works like a charm ! Thanks a lot. Inh my case the problem was that the UPS (nut) lost the connection from time to time without reason. It seems there is no cure for that, google is full with questions about this problem. So, one solution is to reset the USB – lets say all 30 minutes – with a CRON-job.
非常好的方法,赞一个。
very good. i fix a diffcult problem use this method.
Say I want to use this in a Cron job. Is there a way to grab the name of the device from lsusb, pass it to the reset usb script? We have some USB hdds for backups, sometimes, for some reason, the USB isn’t refreshed – still cached index of previous hdd? Don’t know. Initiate the ./usbreset and voila is good. But I wanna not have to log in each day to do that.