"Reprint" How to Reset USB Device in Linux

Source: Internet
Author: User

USB devices is anywhere nowadays, even many embedded devices replace the traditional serial devices with USB devices. However, I experienced that USB devices-to-time. In most cases, a manual unplug and replug would 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 would 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 H Ubbus 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 a example, the device node for it is/dev/bus/usb/006/002, where 006 is the bus number, and 00 2 is the device number.

Second, apply IOCTL operation to reset the device. This is do 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 would 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 are effectively the same as you unplug and replug a USB device.

For another method of the reset USB using LIBUSB, please refer here

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

You can re-identify the USB device without having to pull the disk in Ubuntu

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

"Reprint" How to Reset USB Device in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.