我们使用的一些第三方usb设备,有时会出现一些异常,为了能够自动恢复,我们一般可以插拔一下usb设备,针对不能插拔的情况,我们也可以通过软件来复位我们的usb设备。
步骤如下:
1 将以下内容保存为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;
}
在终端中运行以下命令:
2 编译程序:
$ gcc usbreset.c -o usbreset
3 获取要重置的USB设备的总线和设备ID:
$ lsusb
Bus 002 Device 003: ID 0fe9:9010 DVICO
4 让我们的编译程序可执行:
$ chmod 777 usbreset
5 用sudo权限执行程序;通过运行lsusb
命令找到<Bus>
和<Device>
id的必要替代:
$ sudo ./usbreset /dev/bus/usb/002/003