5

I'm writing user-space program that is intended to control some device via usb so I decided to use libusb (libusb-1.0) to send control messages to and receive responses from that device.

But I constantly receive the following bunch of errors from my code (even when it's executed using 'sudo'):

USB error: could not set config 0: Device or resource busy
set configuration: failed
Check that you have permissions to write to 007/012 and, if you don't, that you set up hotplug (http://linux-hotplug.sourceforge.net/) correctly.
USB error: could not claim interface 0: Device or resource busy
claim interface: failed
USB error: error submitting URB: No such file or directory
bulk writing: failed
USB error: error submitting URB: No such file or directory
bulk reading: failed
response was: 

The code is:

usb_dev_handle* find_device ();

int 
main (int argc, char *argv[])
{
    usb_dev_handle* udev;
    int status;
    char request[] = "K1"; // 'ping' command used to check communication
    char response[256];

    udev = find_device ();
    // udev is successfully found here

    status = usb_set_configuration (udev, 0);
    printf ("set configuration: %s\n", status ? "failed" : "passed");

    status = usb_claim_interface (udev, 0);
    printf ("claim interface: %s\n", status ? "failed" : "passed");

    status = usb_bulk_write (udev, 3, request, sizeof (request), 500);
    printf ("bulk writing: %s\n", status ? "failed" : "passed");

    status = usb_bulk_read (udev, 2, response, sizeof (response), 500);
    printf ("bulk reading: %s\n", status ? "failed" : "passed");

    printf ("response was: %s\n", response);

    usb_close (udev);

    return 0;
}

What's wrong with the code? And how it could be fixed?

OS: Ubuntu 10.10

1
  • Were you able to get it to read and write from your device? Was there significant changes to your code you could post?
    – RunHolt
    Feb 29, 2012 at 20:04

3 Answers 3

10

Answering this question as I had faced this issue on the same OS and was able to solve in the following manner:

Download and Compile the latest libusb source code 1.0.8.

Following are some API calls that I used in order to claim USB interface 0:

libusb_init(NULL);
libusb_open_device_with_vid_pid(NULL, vendor_id, product_id);
libusb_detach_kernel_driver(devh, 0);
libusb_claim_interface(devh, 0);
libusb_close(devh);
libusb_exit(NULL);

Description of the variables in the above example:

static struct libusb_device_handle *devh = NULL;
uint16_t vendor_id;
uint16_t product_id;

To get the vendor ID and product ID, you may run the following commands (Ex. my device info)

$ lsusb
...
Bus 001 Device 013: ID 0930:6544 Toshiba Corp. Kingston DataTraveler 2.0 Stick (2GB)
...

The bold colon delimited string contains vendor and product id respectively.

How to compile the code:

I used the following command to compile my code:

/bin/bash libtool --silent --tag=CC --mode=link g++ -Wall -Wundef -Wunused -Wshadow -D_DEBUG -I../libusb -g -O2 -o read read.cpp ../libusb/libusb-1.0.la -lusb-1.0 -lrt

Copy libtool to the compilation area from the extracted libusb-1.0.8 directory.

Hope this helps.

1
  • should you set the configuration before claiming the interface? also, which numbers from the descriptors are the configuration, interface, and alternate setting parameters from?
    – fuzzyTew
    Mar 8, 2021 at 10:49
3

Don't you need to open() the device before you set its configuration and claim it?

0
2

here's a generic example of a libusb program that you can adapt as needed.

also see the API, which is great!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libusb.h>
#include <err.h>

#define MFGR_ID 0 // given manufacturer ID 
#define DEV_ID 0  // given device ID

/* If device IDs are not known, use libusb_get_device_list() to see a 
list of all USB devices connected to the machine. Follow this call with    
libusb_free_device_list() to free the allocated device list memory.
*/


int main() {
    int init = libusb_init(NULL); // NULL is the default libusb_context

    if (init < 0) {
        errx(1,”\n\nERROR: Cannot Initialize libusb\n\n”);  
    }

    struct libusb_device_handle *dh = NULL; // The device handle
    dh = libusb_open_device_with_vid_pid(NULL,MFGR_ID,DEV_ID);

    if (!dh) {
        errx(1,”\n\nERROR: Cannot connect to device %d\n\n”,DEV_ID);
    }

    // set fields for the setup packet as needed              
    uint8_t       bmReqType = 0;   // the request type (direction of transfer)
    uint8_t            bReq = 0;   // the request field for this packet
    uint16_t           wVal = 0;   // the value field for this packet
    uint16_t         wIndex = 0;   // the index field for this packet
    unsigned char*   data = ‘ ‘;   // the data buffer for the in/output data
    uint16_t           wLen = 0;   // length of this setup packet 
    unsigned int     to = 0;       // timeout duration (if transfer fails)

    // transfer the setup packet to the USB device
    int config =     
    libusb_control_transfer(dh,bmReqType,bReq,wVal,wIndex,data,wLen,to);

    if (config < 0) {
        errx(1,”\n\nERROR: No data transmitted to device %d\n\n”,DEV_ID);
    }

    // now you can use libusb_bulk_transfer to send raw data to the device

    libusb_exit(NULL);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.