Restart the usb device under linux

        Some third-party usb devices we use sometimes have some abnormalities. In order to be able to recover automatically, we can generally plug and unplug the usb device. For the situation that cannot be plugged and unplugged, we can also reset our usb device through software.

Proceed as follows:

1 Save the following asusbreset.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;
}

Run the following command in a terminal:

2 Compile the program:

$ gcc usbreset.c -o usbreset

3 Get the bus and device ID of the USB device to be reset:

$ lsusb  
Bus 002 Device 003: ID 0fe9:9010 DVICO  

4 Make our compiled program executable:

$ chmod 777 usbreset

lsusb5 Execute the program with sudo privileges; find <Bus>and <Device> id the necessary substitutions by running the command:

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

Guess you like

Origin blog.csdn.net/w356877795/article/details/120800766