然而,实际上USB设备有时会挂起。在大多数情况下,手动拔下replug就可以解决这个问题。实际上,usb复位可以模拟拔插和replug操作
首先确定那个usb设备需要被控制.
点击(此处)折叠或打开
- #lsusb
- Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
- Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
- Bus 006 Device 002: ID 04b3:310c IBM Corp. Wheel Mouse
- Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
- Bus 004 Device 002: ID 0a5c:2145 Broadcom Corp.
- Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
以 Wheel Mouse 举例, 设备为 /dev/bus/usb/006/002 006为总线号, 002为设备号.
点击(此处)折叠或打开
- /* reset.c */
- #include <stdio.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <sys/ioctl.h>
- #include <linux/usbdevice_fs.h>
- void main(int argc, char **argv)
- {
- const char *filename;
- int fd;
- filename = argv[1]; //argv[1] = /dev/bus/usb/006/002
- fd = open(filename, O_WRONLY);
- ioctl(fd, USBDEVFS_RESET, 0);
- close(fd);
- return;
- }
点击(此处)折叠或打开
- 编译
- #gcc -o reset reset.c
- 执行
- #sudo ./reset /dev/bus/usb/006/002
- 查看系统日志
- #tail -f /var/log/messages
- May 4 16:09:17 roman10 kernel: [ 1663.013118] usb 6-2:
- reset low speed USB device using uhci_hcd and address 2
- 基本上手动拔插得到的日志内容相同.