来自libusb的错误消息'未声明接口' [英] Error message 'Interface not claimed' from libusb

查看:664
本文介绍了来自libusb的错误消息'未声明接口'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 libusb ,但出现以下错误消息:

I'm trying to use libusb, but I am get the following error message:

usbfs:进程24665(myprogram)在使用前未声明接口0

usbfs: process 24665 (myprogram) did not claim interface 0 before use

我真的不明白为什么,因为据我所知,我是根据库中的描述进行操作的.这是我的代码:

I don't really understand why, because as far as I can tell, I'm doing it according to the description found in the library. Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#include <libusb.h>

int main(void)
{
    int result;
    struct libusb_device_descriptor desc;
    libusb_device **list;
    libusb_device *my_device = NULL;

    result = libusb_init(NULL);
    libusb_set_debug(NULL, 3);

    ssize_t count = libusb_get_device_list(NULL, &list);
    for (int i = 0; i < count; i++) {
        libusb_device *device = list[i];
        result = libusb_get_device_descriptor(device, &desc);
        if((desc.idVendor == 0x03f0) && (desc.idProduct == 0x241d)) {
            my_device = device;
            break;
         }
    }

    if(my_device != NULL) {
        libusb_device_handle *handle;
        result = libusb_open(my_device, &handle);
        int kernelActive = libusb_kernel_driver_active(handle, 0);
        if(kernelActive == 1) {
            result = libusb_detach_kernel_driver(handle, 0);
        }
        result = libusb_claim_interface (handle, 0);
        result = libusb_control_transfer(handle,0x21,34,0x0003,0,NULL,0,0);
        result = libusb_release_interface (handle, 0);

        if(kernelActive == 1) {
          result = libusb_attach_kernel_driver(handle, 0);
        }
        libusb_close(handle);
    }
    libusb_free_device_list(list, 1);
    libusb_exit(NULL);
    return EXIT_SUCCESS;
}

如您所见,我确实在转移之前声明了接口. (我也尝试过与其他USB设备使用相同的代码,以防万一与它有关.)

As you can see, I do claim the interface before the transfer. (I have tried the same code with other USB devices as well, just in case that would have something to do with it.)

我正在使用libusb-1.0.9,这是我可以找到的最新版本.我正在 Ubuntu 12.04_64 (精确的穿山甲).

I'm using libusb-1.0.9, which is the latest release I could find. I'm running this thing on Ubuntu 12.04_64 (Precise Pangolin).

推荐答案

libusb-1.0也有相同的问题;我最初有这个顺序:

Just had the same problem with libusb-1.0; I originally had this sequence:

libusb_init
libusb_open_device_with_vid_pid
libusb_reset_device
libusb_get_device
libusb_reset_device
libusb_set_configuration
libusb_claim_interface
libusb_set_interface_alt_setting 
libusb_get_device_descriptor
libusb_get_bus_number 
libusb_get_device_address
libusb_get_string_descriptor_ascii
if(libusb_kernel_driver_active.. ) 
  if(libusb_detach_kernel_driver.. ) 
libusb_bulk_transfer
...

...,因此,执行第一个libusb_bulk_transfer时会生成未声明接口"(但后续的未显示,上面未显示),我通过进入gdb来确认. ( btw,该错误消息来自

... and for it, the "interface not claimed" was generated when the first libusb_bulk_transfer executed (but not subsequent ones, not shown above), which I confirmed by stepping in gdb. (btw, that error message comes from /linux/drivers/usb/core/devio.c)

此页面: USB隐藏问题·Yubico/yubikey-personalization Wiki· GitHub 是指libusb-0.1的修复程序,称为相应的"detach_driver"函数;所以我也开始在代码中移动"detach_driver"部分-最终,该序列似乎摆脱了"interface not Claim"消息:

This page: USB Hid Issue · Yubico/yubikey-personalization Wiki · GitHub refers to a fix for libusb-0.1 which called the corresponding "detach_driver" function; so I started moving the "detach_driver" part around in my code too - and finally this sequence seems to get rid of the "interface not claimed" message:

libusb_init
libusb_open_device_with_vid_pid
if(libusb_kernel_driver_active.. ) 
  if(libusb_detach_kernel_driver.. ) 
libusb_reset_device
libusb_get_device
libusb_set_configuration
libusb_claim_interface
libusb_set_interface_alt_setting 
libusb_get_device_descriptor
libusb_get_bus_number
libusb_get_device_address
libusb_get_string_descriptor_ascii
libusb_bulk_transfer
...

很明显,如果首先分离驱动程序,然后声明接口,则不会生成任何错误.但这也是您在OP中所拥有的-因此,我认为,OP的诀窍是先具有detach,然后是set configuration,然后是claim interface ...

Apparently, if the driver is first detached, and then interface is claimed - then no errors are generated. But that is also what you have in OP there - so I think, the trick for OP would be to have detach, then set configuration, and after that claim interface...

希望这会有所帮助,
干杯!

Hope this helps,
Cheers!

这篇关于来自libusb的错误消息'未声明接口'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆