博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转载】How to Reset USB Device in Linux
阅读量:5992 次
发布时间:2019-06-20

本文共 3043 字,大约阅读时间需要 10 分钟。

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 hubBus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hubBus 006 Device 002: ID 04b3:310c IBM Corp. Wheel MouseBus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hubBus 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 
#include 
#include 
#include 
#include 
 
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 

Reference: 

#!/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

转载于:https://www.cnblogs.com/Jeff-Tang/p/5822705.html

你可能感兴趣的文章
分析与设计数据库模型的简单过程
查看>>
C语言Makefile文件使用
查看>>
C#编程(十三)----------方法重载
查看>>
GentleNet使用之详细图解[语法使用增强版]
查看>>
php进程的SIGBUS故障
查看>>
telnet测试制定地址端口号
查看>>
保持Service不被Kill掉的方法--双Service守护 && Android实现双进程守护
查看>>
android 截取指定位置字符串
查看>>
李洪强iOS开发之initWithFrame,initWithCoder和aweakFormNib
查看>>
Android ActivityManager.killBackgroundProcesses方法去结束
查看>>
数据库设计原则(转载)
查看>>
MySQL 触发器简单实例
查看>>
Elasticsearch基本概念及核心配置文件详解
查看>>
一次使用Python连接数据库生成二维码并安装为windows服务的工作任务
查看>>
ios_基础篇1_关键字(strong和weak)
查看>>
PageControl
查看>>
我的友情链接
查看>>
远程桌面用户输入法的配置
查看>>
【Getty】Java NIO框架设计与实现
查看>>
常用监控命令工具-----vmstat
查看>>