How to Reset USB Device in Linux–using libusb
This is a follow up of the previous entry How to Reset USB Device in Linux. The previous blog covers a method to reset usb device using ioctl system call. This blog gives an alternative approach to reset usb device, using libusb.
libusb is a cross-platform library giving user level application uniform access to usb devices. You’ll need to install the development package first. Simply typing the following commands to your terminal (using Ubuntu as an example),
sudo apt-get install libusb-1.0-0-dev
After installation, you can use the methods in libusb. Below is the C source code to reset usb device using libusb API,
#include <stdio.h>#include <stdlib.h>#include <libusb-1.0/libusb.h>//compile: gcc usbreset.c -o usbreset -lusb-1.0//usage: ./usbreset 2 6//use lsusb to check out the bus number and device numberstruct libusb_device_handle *devh;struct libusb_device *dev;struct libusb_device **devs;void resetUSB() {int success;int bpoint = 0;do {success = libusb_reset_device(devh);if ((bpoint % 10) == 0) {printf(".");}++bpoint;if (bpoint > 100) {success = 1;}} while (success < 0);if (success) {printf("\nreset usb device failed:%d\n", success);} else {printf("\nreset usb device ok\n");}}struct libusb_device* search_device(int _busNum, int _devNum) {libusb_device *l_dev;int i = 0;int l_busNum, l_devNum;while ((l_dev = devs[i++]) != NULL) {printf("check against %d device\n", i);l_busNum =(int) libusb_get_bus_number(l_dev);l_devNum =(int) libusb_get_device_address(l_dev);printf("bus number: %d; device number: %d\n", l_busNum, l_devNum);if ((l_busNum == _busNum) && (l_devNum == _devNum)) {printf("found device\n");return l_dev;}}return NULL;}int main(int argc, char **argv) {//parse the input parameters to get the bus number and device numberint l_busNum, l_devNum;int l_ret;printf("program started!\n");if (argc < 3) {printf("not enough arguments!\n");printf("usage: ./usbreset <bus number> <dev number>\n");return 0;}printf("bus number: %s\n", argv[1]);printf("dev number: %s\n", argv[2]);l_busNum = atoi(argv[1]);l_devNum = atoi(argv[2]);printf("bus number: %d; dev number: %d\n", l_busNum, l_devNum);l_ret = libusb_init(NULL);if (l_ret < 0) {return l_ret;}l_ret = libusb_get_device_list(NULL, &devs);if (l_ret < 0) {return (int) l_ret;}dev = search_device(l_busNum, l_devNum);if (dev == NULL) {printf("device not found\n");return 0;}l_ret = libusb_open(dev, &devh);if (l_ret == 0) {printf("got the usb handle successfully.\n");} else {printf("error getting usb handle.\n");}//reset the usb deviceresetUSB();//free the device listlibusb_free_device_list(devs, 1);libusb_exit(NULL);return 0;}The code can be compiled using
gcc usbreset.c -o usbreset -lusb-1.0To use the complied program usbreset, you can simply use,
sudo ./usbreset <bus number> <device number>To check the bus number and device number of a usb device, you can use lsusb command.
To check if a device is resetted or not, you can use tail -f /var/log/messages.
An detailed example is given in How to Reset USB Device.
6 comments on “How to Reset USB Device in Linux–using libusb”
Leave a Reply Cancel reply
40% Discount on My Book — Android NDK Cookbook
Android NDK Cookbook ebook 40% discount with promotion code MREANC40 at Packt Publishing The promotion code is valid until 15th June.Categories
- Android Apps (18)
- Android Audio Editor (1)
- TS 2 (3)
- Video Converter Android (8)
- Video2Gif (1)
- Android Tutorial (26)
- Android Dev Tools (1)
- API illustrated (8)
- Multimedia API (3)
- ffmpeg on Android (4)
- NDK (6)
- UI (5)
- Animation (1)
- Code Snippet (2)
- Coding Beyond Technique (18)
- a word, a world (4)
- Bug Rectified (4)
- Programming Habit (1)
- Software as a Career (1)
- Software as User Experience (1)
- Compilers and Related (2)
- ELF (2)
- Computer Languages (31)
- C/C++ (13)
- Java (9)
- JavaScript (2)
- PHP (1)
- Python (8)
- Data Structure & Algorithms (29)
- Bits (1)
- Data Structure (5)
- Integers (10)
- BigInteger (1)
- Prime (4)
- Search (3)
- Sorting (5)
- Strings (5)
- Database (1)
- SQLite (1)
- Digital Signal Processing (33)
- Distributed Systems (17)
- Apache Cassandra (6)
- Apache Hadoop (8)
- Apache Avro (3)
- Apache Nutch (3)
- Apache Solr (1)
- Linux Study Notes (40)
- crontab (1)
- Linux Kernel Programming (8)
- Linux Programming (12)
- IPC (2)
- Linux Network Programming (5)
- Linux Signals (2)
- Linux Shell Scripting (1)
- ssh (3)
- Machinery (30)
- misc (1)
- My Ideas (1)
- My Project (3)
- Mobile Caching (1)
- Selective Decoding (2)
- My Publication (1)
- My Readings (1)
- Networking (15)
- Program for Performance (8)
- Uncategorized (1)
- Virtual Machine (2)
- Web Dev (8)
- web components (3)
- Android Apps (18)
Recent Comments
Archives
- May 2013 (1)
- April 2013 (1)
- March 2013 (4)
- December 2012 (2)
- November 2012 (6)
- October 2012 (6)
- September 2012 (3)
- August 2012 (13)
- July 2012 (15)
- June 2012 (3)
- May 2012 (8)
- April 2012 (4)
- March 2012 (13)
- February 2012 (19)
- January 2012 (9)
- December 2011 (11)
- November 2011 (12)
- October 2011 (4)
- September 2011 (12)
- August 2011 (16)
- July 2011 (15)
- June 2011 (6)
- May 2011 (10)
- April 2011 (13)
- March 2011 (20)
- February 2011 (4)
- November 2010 (2)
- May 2010 (1)
- April 2010 (1)
- February 2010 (1)





How do I reset my
password
What do you mean by reset your password? In Linux, if you’re superuser, passwd will do.
Thanks!
You’re welcome!
i am tryin the same in debian….
so i installed libusb 0.1.12-13 in my PC
and used your reset code..but when i try to compile get the followin error..
i am a newbie ..please guide..
vir-deb5:/usr# gcc lib_reset.c -o gg -lusb
lib_reset.c: In function âsearch_deviceâ:
lib_reset.c:93: error: âlibusb_deviceâ undeclared (first use in this function)
lib_reset.c:93: error: (Each undeclared identifier is reported only once
lib_reset.c:93: error: for each function it appears in.)
lib_reset.c:93: error: âl_devâ undeclared (first use in this function)
It seems that you’re using the wrong libusb version. Please remove the wrong library and follow the instructions in this blog to install the correct one.