Bugs, Linux

How to reset a USB device without unplugging Linux from the command line

Compile the file usbreset.c File usbreset.c content: /* 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> […]

Compile the file usbreset.c

File usbreset.c content:

/* 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;
}

Execute those commands:

cc usbreset.c -o usbreset
chmod +x usbreset
lsusb
             Bus 004 Device 002: ID 04e8:60a6 Samsung Electronics Co., Ltd
sudo ./usbreset /dev/bus/usb/004/002

Script for reset by name:

	bus=`lsusb | grep 'Samsung Electronics' | awk '{print $2}'`
	dev=`lsusb | grep 'Samsung Electronics' | awk '{print $4}' | awk '{print substr($0,0,3)}'`
	/some_path/usbreset /dev/bus/usb/$bus/$dev
VN:F [1.9.22_1171]
Rating: 8.0/10 (2 votes cast)
How to reset a USB device without unplugging Linux from the command line, 8.0 out of 10 based on 2 ratings

About marcin

No information is provided by the author.

5 Comments

  1. Tony

    Your “#include” tags don’t have anything after them.

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    • marcin

      Thanks. fixed.

      VN:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
  2. jimmydick

    Thanks very much. I put this script in /etc/pm/sleep.d/ for a keyboard of mine that goes dead when resuming after a suspend.

    I like that this is simple and easy to read.

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


CAPTCHA Image

Reload Image
(required)