USB devices are anywhere nowadays, even many embedded devices replace the traditional serial devices with usb devices. However, I experienced that USB devices hang from time to time. In most cases, a manual unplug and replug will solve the issue. Actually, usb reset can simulate the unplug and replug operation.

First, get the device path for your usb device. Enter the command lsusb will give you something similar as below,

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

Use the IBM Wheel Mouse as an example, the device node for it is /dev/bus/usb/006/002, where 006 is the bus number, and 002 is the device number.

Second, apply ioctl operation to reset the device. This is done in C code,

#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];
	fd = open(filename, O_WRONLY);
	ioctl(fd, USBDEVFS_RESET, 0);
	close(fd);
	return;
}

Save the code above as reset.c, then compile the code using

gcc -o reset reset.c
 

This will produce the a binary named reset. Again, using the wheel mouse as an example, execute the following commands,

sudo ./reset /dev/bus/usb/006/002
 

You can take a look at the message by,

tail -f /var/log/messages
 

On my Ubuntu desktop, the last line reads,

May 4 16:09:17 roman10 kernel: [ 1663.013118] usb 6-2:
reset low speed USB device using uhci_hcd and address 2
 

This reset operation is effectively the same as you unplug and replug a usb device.

For another method of reset usb using libusb, please refer here

Reference: http://forums.x-plane.org/index.php?app=downloads&showfile=9485.

ubuntu下不用拔盘就可以重新识别usb设备

#!/bin/sh
# Usage: ./resetusb ARGUMENT(The keyword for your usb device)

var1=$1
keyword=${var1:=Storage}

debug=$(lsusb)
bus=$(lsusb|grep $keyword|perl -nE "/\D+(\d+)\D+(\d+).+/; print qq(\$1)")
device=$(lsusb|grep $keyword|perl -nE "/\D+(\d+)\D+(\d+).+/; print qq(\$2)")

echo "/* 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) {
return 1;
}
filename = argv[1];

fd = open(filename, O_WRONLY);
if (fd < 0) {
return 1;
}

rc = ioctl(fd, USBDEVFS_RESET, 0);
if (rc < 0) {
return 1;
}

close(fd);
return 0;
}" > /tmp/usbreset.c

$(cc /tmp/usbreset.c -o /tmp/usbreset)
$(chmod +x /tmp/usbreset)
$(cd /tmp/;./usbreset /dev/bus/usb/$bus/$device)
result=$?
if [ $result != 0 ];then
echo "Reset Usb Failed!"
echo "Please make sure you have inputted right device keyword: $keyword"
echo "You have chose bus:${bus:=Not Found},device:${device:=Not Found}"
echo "More info:\n$debug"
else
echo "Reset Usb $keyword Successfully!"
fi

最新文章

  1. c++ basic 整理2
  2. DBConfigReader.java
  3. 手机浏览器JS识别
  4. [BS-10] 统一设置app所有页面的“返回”按钮样式
  5. udp 视频包网络传输花屏
  6. artTemplate的使用总结
  7. cryptopp开源库的使用(二):base64加密
  8. pyqt显示指定范围的数字
  9. UIAlertController高级之嵌入其他控件
  10. 18、面向对象基本原则及UML类图简介
  11. 微信小程序教学第三章(含视频):小程序中级实战教程:列表-页面逻辑处理
  12. C语言程序设计(基础)- 第6周作业
  13. lxml库
  14. 使用secureCRT和Telnet将文件压缩导出到Ubuntu中,到Ubuntu中加压缩发现:tar解压包的时候出现错误gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now
  15. webpack - minipack 打包原理
  16. 如何注册Tomcat到Window Service服务
  17. [HAOI2012]道路(最短路DAG上计数)
  18. PHP7 网络编程(六)Socket和IO多路复用【待】
  19. RuntimeError: Object: Could not open SDE workspace
  20. UESTC 486 Good Morning (水题+坑!)

热门文章

  1. Java 微信公众号上传永久素材的方法
  2. lpad&amp;amp;rpad
  3. hdu1181(变形课)
  4. 一个操作oracle的c#类 含分页
  5. oracle 11gR2 如何修改 private ip
  6. DB-MySQL:MySQL 处理重复数据
  7. pyCrypto python 3.5--转
  8. React组件化开发
  9. mysql面试几个问题
  10. oracle调优使用到相关sql