Skip to content

Reset USB device in linux

  • by

I had old UPS with serial port which was connected using Profilic USB to serial adapter. Sometimes this USB dongle had frozen and need to be restated – re-attached. This can be done in linux using IOCTL.

Simple C code from stackowerflow:

#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;
}

Device id (argv1) need to be passed like this:

# Bus 00X Device 00Y: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
DEV=`lsusb| grep '067b:2303' |awk {'print "/dev/bus/usb/"$2"/"$4'}|cut -f1 -d:`
echo "Reseting usb device $DEV"
usbreset $DEV

https://nextcloud.iwik.org/s/usbreset

Note: This is working for physical servers, not for USB devices pass-through to VM.

Leave a Reply

Your email address will not be published. Required fields are marked *