usbreset

Published: 2012-01-19
Updated: 2013-09-22

This utility for linux (tested with kernels 2.6.32 and 2.6.38) creates a special usb subsystem controlling device node, and issues an ioctl to have the linux kernel reset all usb devices, causing them all to re-enumerate and initialize.

I think I found this technique in this mailing list post by Alan Stern. I added the automatic device-node creation bit to make it more automated.

:::c
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>

int main()
{
    const char *dev = "/dev/usb-001-001";
    struct stat statbuf;
    int sr = stat(dev, &statbuf);
    if (sr != 0 && errno != ENOENT          ) return 1;
    if (sr == 0 && !S_ISCHR(statbuf.st_mode)) return 1;
    if (sr != 0) {
        int nr = mknod(dev, 0644 | S_IFCHR, makedev(189,0));
        if (nr != 0) return 1;
    }
    int fd = open(dev, O_WRONLY);
    if (fd < 0) return 1;
    ioctl(fd, USBDEVFS_RESET, 0);
    close(fd);
    sleep(5); // wait for usb enumeration
    return 0;
}

A different technique using ''libusb'' can be found in this blog post.