Linux从命令行重置USB设备

·
2026-08-06 12:13:43

Linux对于某些USB设备的支持不是很好,插入的USB设备偶尔会卡死,如果希望避免重启系统或者拔插设备,可以通过下面方案解决。

将以下代码保存为usbreset.c

/* usbreset -- send a USB port reset to a USB device */

#include

#include

#include

#include

#include

#include

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 设备的ID及ID:

$ lsusb

Bus 002 Device 003: ID 0fe9:9010 DVICO

3. 添加可执行权限给编译的程序:

$ chmod +x usbreset

4. 以 sudo 权限执行程序;使用步骤2中lsusb命令获取的ID及ID对下方命令进行必要的修改或替换:

$ sudo ./usbreset /dev/bus/usb/002/003