Linux對於某些USB設備的支持不是很好,插入的USB設備偶爾會卡死,如果希望避免重啟系統或者拔插設備,可以通過下面方案解決。
將以下代碼保存為usbreset.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;
}
在終端運行以下命令:
1. 編譯程序:
$ cc usbreset.c -o usbreset
2. 獲取需要重置的 USB 設備的<Bus>ID及<Device>ID:
$ lsusb
Bus 002 Device 003: ID 0fe9:9010 DVICO
3. 添加可執行權限給編譯的程序:
$ chmod +x usbreset
4. 以 sudo 權限執行程序;使用步驟2中lsusb命令獲取的<Bus>ID及<Device>ID對下方命令進行必要的修改或替換:
$ sudo ./usbreset /dev/bus/usb/002/003
