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

【转载】How to Reset USB Device in Linux的更多相关文章

  1. Power OFF and ON USB device in linux (ubuntu)

    Power OFF and ON USB device in linux (ubuntu) http://loginroot.com/power-off-and-on-usb-device-in-li ...

  2. usb device address error 110

    ubuntu失灵了,怎么都起不来,报一堆错误usb device descriptor read/64, error 110......重启,换kvm的接口,usb键盘鼠标...终于在试了下面这个方法 ...

  3. Install Slax on USB device (Slax U 盘安装)

    Slax is a modern, portable, small and fast Linux operating system with a modular approach and outsta ...

  4. Assigning Host USB device to a Guest VM

    Example Assigning Host USB device to a Guest VM This example is based on qemu-kvm (0.15.0) as instal ...

  5. USB device & USB controller & USB passthrough

    目录 USB device USB controller Linux 相关命令 Python 相关库 Libvirt USB passthrough 参考资料 近期往 openstack 里倒腾 US ...

  6. Open images from USB camera on linux using V4L2 with OpenCV

    I have always been using OpenCV's VideoCapture API to capture images from webcam or USB cameras. Ope ...

  7. What is a Windows USB device path and how is it formatted?

    http://community.silabs.com/t5/Interface-Knowledge-Base/Windows-USB-Device-Path/ta-p/114059 Windows ...

  8. USB Device Finder

    http://www.velleman.eu/images/tmp/usbfind.c #ifdef __cplusplus extern "C" { #endif #includ ...

  9. usb host和usb device

    S3C2440的数据手册将USB功能分为两章--usb host和usb device.具体什么意思呢? usb host: 微处理器作为usb主设备,可以挂接U盘之类的从属设备. usb devic ...

随机推荐

  1. HDU 4351

    求某区间各个子集的和的根的最大的五个数. 子集根其实就是这个数模9后的余数,注意的是要区分0和9,两者的余数都是0.. 记录前i个数的和的根,设为sum数组,然后处理两个数组 lft[i][j]表示从 ...

  2. [React] Reference a node using createRef() in React 16.3

    In this lesson, we look at where we came from with refs in React. Starting with the deprecated strin ...

  3. Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...

  4. 《AndroidStudio每日一贴》3.高速切换代码风格、配色方案和键盘

    <AndroidStudio每日一贴>3.高速切换代码风格.配色方案和键盘 高速切换代码风格.配色方案和键盘,使用快捷键: control + ~ 很多其它有用技巧请查看<Andro ...

  5. WPF Prefix 'attach' does not map to a namespace.

    这个是用附加属性时,一定要在属性前面加Path= Visibility="{Binding Path=PlacementTarget.(attach:CommonAttachedProper ...

  6. ComponentName

    ComponentName,顾名思义,就是组件名称,通过调用Intent中的setComponent方法,我们可以打开另外一个应用中的Activity或者服务. 实例化一个ComponentName需 ...

  7. 点击了一个link button,查看后台调用

    使用F12进行监视 本身是一个linkbutton,可以看到绑定了一个JavaScript <a id="gvStaticConnection_ctl02_fresh" hr ...

  8. redis配置外部访问

    iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT 开放6379端口

  9. flask-alembic数据迁移工具

    alembic是用来做ORM模型与数据库的迁移与映射.alembic使用方式跟git有点类似,表现在两个方面, 第一,alemibi的所有命令都是以alembic开头: 第二,alembic的迁移文件 ...

  10. KCP kcptun

    简介 KCP是一个快速可靠协议,能以比 TCP浪费10%-20%的带宽的代价,换取平均延迟降低 30%-40%,且最大延迟降低三倍的传输效果.纯算法实现,并不负责底层协议(如UDP)的收发,需要使用者 ...