来自libusb的错误消息“未声明的接口”

时间:2012-06-18 18:18:49

标签: usb libusb libusb-1.0

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

  

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

我真的不明白为什么,因为据我所知,我是根据图书馆中的描述来做的。这是我的代码:

#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设备相同的代码,以防万一与它有关。)

我正在使用libusb-1.0.9,这是我能找到的最新版本。我在Ubuntu 12.04_64(精确穿山甲)上运行这个东西。

3 个答案:

答案 0 :(得分:4)

libusb-1.0遇到了同样的问题;我最初有这个序列:

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,该错误消息来自/linux/drivers/usb/core/devio.c

此页面:USB Hid Issue · Yubico/yubikey-personalization Wiki · GitHub指的是libusb-0.1的修复程序,它调用了相应的“detach_driver”函数;所以我开始在我的代码中移动“detach_driver”部分 - 最后这个序列似乎摆脱了“未声明的接口”消息:

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

希望这有帮助,
干杯!

答案 1 :(得分:3)

尝试calling libusb_set_debug(context, where_to)从libusb获取更多调试信息。消息的where_to是一个整数:

Level 0: no messages ever printed by the library (default)
Level 1: error messages are printed to stderr
Level 2: warning and error messages are printed to stderr
Level 3: informational messages are printed to stdout, warning and error messages are printed to stderr

这来自libusb documentation非常好。

我运行了错误消息看起来很好的代码,但在内部它报告了其他一些进程对它有独占权限,所以我无法使用它。

答案 2 :(得分:1)

您应该检查所有结果值,然后您可以轻松找出出错的地方。如果它们返回您期望的结果,只需检查所有结果值。

验证

  • libusb_open是否返回LIBUSB_SUCCESS?
  • libusb_kernel_driver_active是返回0还是1,而不是错误代码?
  • libusb_detach_kernel_driver是否返回LIBUSB_SUCCESS?
  • libusb_claim_interface是否返回LIBUSB_SUCCESS?