Skip to content

Instantly share code, notes, and snippets.

@turingbirds
Created October 31, 2014 20:30
Show Gist options
  • Save turingbirds/3cb9e64df0c09f5ffda6 to your computer and use it in GitHub Desktop.
Save turingbirds/3cb9e64df0c09f5ffda6 to your computer and use it in GitHub Desktop.
usbreset.c

usbreset.c

Due to Alan Stern [1].

[1]http://marc.info/?l=linux-usb&m=121459435621262&w=2

List your USB devices

$ lsusb
...
Bus 002 Device 002: ID 04f3:024c Elan Microelectronics Corp.
...

or use the dev node:

$ ls /dev/bus/usb/
001  002  003

(list the bus IDs)

$ ls /dev/bus/usb/002/
001  002  004  011  118

(list the device IDs)

Compile

Compile ANSI C code:

$ cc usbreset.c -o usbreset

If necessary, make the binary executable:

$ chmod +x usbreset

Execute the program

With the bus + device ID that you selected, execute the program as a superuser:

$ sudo ./usbreset /dev/bus/usb/<...>/<...>

The 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-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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment