Created
May 19, 2021 22:38
-
-
Save Jeffer-Lin/f6b2809bd3ff14be26af8ae91d06d4ef to your computer and use it in GitHub Desktop.
linux use command line to re-scan usb drive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* usbreset -- reset a USB device */ | |
/* | |
$ gcc usbreset.c -o usbreset | |
$ sudo mv usbreset /usr/local/bin/ | |
$ lsusb | |
Bus 001 Device 004: ID ..... | |
$ sudo usbreset /dev/bus/usb/001/004 | |
*/ | |
#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[]) | |
{ | |
if (argc != 2) { | |
fprintf(stderr, "Usage: usbreset device-filename\n"); | |
fprintf(stderr,"\ | |
$ lsusb\n\ | |
Bus 001 Device 004: ID .....\n\ | |
$ sudo usbreset /dev/bus/usb/001/004\n\ | |
"); | |
return 1; | |
} | |
const char *filename = argv[1]; | |
int fd = open(filename, O_WRONLY); | |
if (fd < 0) { | |
perror("Error opening output file"); | |
return 1; | |
} | |
printf("Resetting USB device %s\n", filename); | |
int 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