How to Reset USB Device in Linux–using libusb

本文介绍了一种使用LibUSB库在Linux环境下重置USB设备的方法,并提供了具体的C语言实现代码。通过该方法,用户级别的应用程序可以统一地访问USB设备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文转载至:http://www.roman10.net/how-to-reset-usb-device-in-linuxusing-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 number
struct 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 number
    int 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 device
    resetUSB();
    //free the device list
    libusb_free_device_list(devs, 1);
    libusb_exit(NULL);
    return 0;
}

The code can be compiled using

gcc usbreset.c -o usbreset -lusb-1.0

To 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值