问题
I'm trying to use libusb, but I am get the following error message:
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;
}
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.)
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).
回答1:
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
...
... 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)
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
...
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!
回答2:
Try calling libusb_set_debug(context, where_to)
to get some more debug information from libusb. The where_to for the messages is an integer:
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
This is from the libusb documentation which is pretty good.
I ran code where the error messages looked just fine, but internally it reported that some other process had an exclusive claim on it, so I couldn't use it.
回答3:
You should check all the result values, then you can easily find out what is going wrong. Just check all result values if they return what you expect.
Verify:
- Does libusb_open return LIBUSB_SUCCESS?
- Does libusb_kernel_driver_active return 0 or 1, not an error code?
- Does libusb_detach_kernel_driver return LIBUSB_SUCCESS?
- Does libusb_claim_interface return LIBUSB_SUCCESS?
来源:https://stackoverflow.com/questions/11088691/error-message-interface-not-claimed-from-libusb