Linux USB Application using libusb
I am trying to write an application to read and write to a connected USB device. I am using libusb. It seems after I find the device, configuration fails. I am following the developers guide for libusb at http://libusb.sourceforge.net/doc/index.html. Which states that:
usb_set_configuration sets the active configuration of a device. The configuration parameter is the value as specified in the descriptor field bConfigurationValue. Returns 0 on success or < 0 on error.
I demonstrate in my source (below) that I use the bConfigurationValue as the configuration parameter, but my return value from usb_set_configuration is always -1. I don't understand why?
The output:
[user@local workspace]$ ./application
Device Found @ Address 005
Vendor ID 0x018d1
Product ID 0x04e12
INFO: bConfigurationValue = 1
config status =-1
claim status =-1
alt status =-22
TX status =-1
RX status =-1 -> RX char = 0
[user@local workspace]$
The source:
int main(int argc, char *argv[])
{
struct usb_bus *bus;
struct usb_device *dev;
struct usb_dev_handle *android_handle;
int status;
unsigned char send_data = 0x51;
unsigned char receive_data = 0;
usb_init();
if ((handle = locate_device()) == 0)
{
printf("Error: Could not open the device\n");
return (-1);
}
printf("INFO: bConfigurationValue = %d\n", config_val);
status = usb_set_configuration(handle, config_val);
printf("config status = %d\n", status);
status = usb_claim_interface(handle, 0);
printf("claim status = %d\n", status);
open_status = usb_set_altinterface(handle, 0);
printf("alt status = %d\n", status);
status = usb_bulk_write(handle, 4, &send_data, 1, 500);
printf("TX status = %d\n", status);
usb_bulk_read(handle, 3, &receive_data, 1, 500);
printf("RX status = %d -> RX char = %d\n", status, receive_data);
usb_close(handle);
return 0;
}
usb_dev_handle *locate_device(void)
{
...
config_val = dev->config[myDeviceIndex].bConfigurationValue;
...
}
EDIT:
The following error codes are returned when the application is ran as root:
Device Found @ Address 005
Vendor ID 0x01开发者_运维知识库8d1
Product ID 0x04e12
INFO: bConfigurationValue = 1
config status = -16
claim status = -16
alt status = -22
TX status = -16
RX status = -16 -> RX char = 0
精彩评论