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



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

USBFS:进程 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(精确穿山甲)上运行这个东西。

只是对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分步确认了这一点。(顺便说一句,该错误消息来自/linux/drivers/usb/core/devio.c

本页: USB 隐藏问题 ·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......

希望这有帮助,
干杯!

尝试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文档,非常好。

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

您应该检查所有结果值,然后可以轻松找出问题所在。只需检查所有结果值是否返回您的预期值。

验证:

  • libusb_open会LIBUSB_SUCCESS返回吗?
  • libusb_kernel_driver_active返回 0 或 1,而不是错误代码吗?
  • libusb_detach_kernel_driver会LIBUSB_SUCCESS返回吗?
  • libusb_claim_interface会LIBUSB_SUCCESS返回吗?

最新更新