Rob*_*ert 9 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;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我确实在转移之前声明了界面.(我也尝试过与其他USB设备相同的代码,以防万一与它有关.)
我正在使用libusb-1.0.9,这是我能找到的最新版本.我在Ubuntu 12.04_64(精确穿山甲)上运行这个东西.
小智 5
尝试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
Run Code Online (Sandbox Code Playgroud)
这是来自libusb 文档,非常好。
我运行的代码中的错误消息看起来很好,但在内部它报告其他一些进程对其有独占声明,所以我无法使用它。
刚刚遇到了同样的问题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
...
Run Code Online (Sandbox Code Playgroud)
...为此,“未声明的接口”是在第一次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
...
Run Code Online (Sandbox Code Playgroud)
显然,如果首先分离驱动程序,然后声明接口 - 则不会产生错误。但这也是你在 OP 中所拥有的 - 所以我认为,OP 的诀窍是拥有detach,然后set configuration,然后claim interface......
希望这会
有所帮助,干杯!
您应该检查所有结果值,然后您可以轻松找出问题所在。只需检查所有结果值是否返回您期望的值。
核实: