Usbreset.c

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search

This is an amazing little bit of code that is profoundly useful. Sometimes a USB device, such as an input device like a wireless keyboard, or USB webcam, will go dead on the USB port. The USB port will simply stop working unless the device is physically disconnected and reconnected.

Wouldn't it be nice if you could reset the USB port without having to reach back behind the computer to unplug and plug the device back in again?

Open a text editor

vi usbreset.c

and paste the following code:

/* 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-filenamen");
               return 1;
       }
       filename = argv[1];

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

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

       close(fd);
       return 0;

Compile the code with the GNU C compiler.

cc usbreset.c -o usbreset

Drop the new command off in sbin

mv ./usbreset /usr/local/sbin

You will need to run this as root

Find the USB device that needs to be rest

lsusb
Bus 002 Device 004: ID 093b:a102 Plextor Corp. ConvertX M402U A/V Capture
Bus 004 Device 002: ID 09da:0006 A4 Tech Co., Ltd Optical Mouse WOP-35 / Trust 450L Optical Mouse

We can reset the video capture device:

usbreset /dev/bus/usb/002/004

We can reset the mouse:

usbreset /dev/bus/usb/004/002

sources and credit