gpt4 book ai didi

usb - 来自 libusb 的错误消息 'Interface not claimed'

转载 作者:行者123 更新时间:2023-12-01 19:37:51 26 4
gpt4 key购买 nike

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

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

我不太明白为什么,因为据我所知,我是根据库中找到的描述来做的。这是我的代码:

#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;
}

正如你所看到的,我在转移之前确实声明了接口(interface)。 (我也对其他 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 时生成“接口(interface)未声明”已执行(但不是后续的,上面未显示),我通过单步执行 gdb 来确认这一点。 (顺便说一句,该错误消息来自 /linux/drivers/usb/core/devio.c )

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

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

显然,如果首先分离驱动程序,然后声明接口(interface) - 则不会生成任何错误。但这也是你在 OP 中所拥有的 - 所以我认为,OP 的技巧是拥有 detach ,然后set configuration ,然后claim interface ...

希望这有帮助,
干杯!

关于usb - 来自 libusb 的错误消息 'Interface not claimed',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11088691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com