libusb_open_device_with_vid_pid always return null

8.6k views Asked by At

I'm Centos user. I tried to write to my USB-Flash disk by using libusb-1.0.19. But it seems "libusb_open_device_with_vid_pid" always return a NULL value. Why does this happen?, my program running in ROOT permission, and device is connected to my PC and program can be get Vendor ID and Product ID of it.

This is my code!!!

#include <cassert>
#include <cstdio>
#include <libusb-1.0/libusb.h>

#define VENDOR_ID    0x090c
#define PRODUCT_ID   0x1000


int main() 
{
   libusb_context       *context    = NULL   ;
   libusb_device_handle *dev_handle = NULL   ;
   libusb_device        **devs               ;
   int                  rc          = 0      ;
   ssize_t              count                ; //holding number of devices in list

   //----------------------------------------------------------------------------
   // Initialize the library
   //----------------------------------------------------------------------------
   rc = libusb_init(&context);
   assert(rc == 0);

   //----------------------------------------------------------------------------
   // Enable debug
   //----------------------------------------------------------------------------
   #ifndef NDEBUG
      libusb_set_debug(context, LIBUSB_LOG_LEVEL_WARNING); 
   #endif
   //----------------------------------------------------------------------------
   // Get device list
   //----------------------------------------------------------------------------
   count = libusb_get_device_list(context, &devs);
   assert(count > 0);


   for (size_t idx = 0; idx < count; ++idx) {
      libusb_device *device = devs[idx];
      libusb_device_descriptor desc = {0};

      rc = libusb_get_device_descriptor(device, &desc);
      assert(rc == 0);

      printf("Vendor:Device = %04x:%04x\n", desc.idVendor, desc.idProduct);
   }
   //----------------------------------------------------------------------------
   // open usb device by vendor ID and Product ID
   //----------------------------------------------------------------------------
   dev_handle = libusb_open_device_with_vid_pid(context,VENDOR_ID,PRODUCT_ID);
   assert(dev_handle == NULL);

   //----------------------------------------------------------------------------
   // Free device list 
   //----------------------------------------------------------------------------
   libusb_free_device_list(devs, 1); //free the list, unref the devices in it   

   //----------------------------------------------------------------------------
   // Write data to device
   //----------------------------------------------------------------------------
   unsigned char *data = new unsigned char[5]; //data to write
   int actual;
   data[0]='h';
   data[1]='e';
   data[2]='l';
   data[3]='l';
   data[4]='o';

   /*Check if kenel driver attached*/
   if(libusb_kernel_driver_active(dev_handle, 0))
   {
      rc = libusb_detach_kernel_driver(dev_handle, 0); // detach driver
      assert(rc == 0);
   }
   rc = libusb_claim_interface(dev_handle, 0);
   assert(rc < 0);

   rc = libusb_bulk_transfer(dev_handle, (64 | LIBUSB_ENDPOINT_OUT), data, 4, &actual, 0);
   assert (rc != 0 || actual != 5);

   rc = libusb_release_interface(dev_handle, 0);
   assert(rc != 0);

   printf("Wrote \"hello\" to usb device\n");
   //----------------------------------------------------------------------------
   // close dev_handle
   //----------------------------------------------------------------------------
   libusb_close(dev_handle);

   //----------------------------------------------------------------------------
   // exit 
   //----------------------------------------------------------------------------
   libusb_exit(context);

   return 0;
}

This is my result i got

Vendor:Device = 1d6b:0002
Vendor:Device = 1d6b:0002
Vendor:Device = 090c:1000
Vendor:Device = 1d6b:0002
Vendor:Device = 1d6b:0003
Vendor:Device = 1d6b:0002
Vendor:Device = 1d6b:0003
Vendor:Device = 8087:0024
Vendor:Device = 2109:0811
Vendor:Device = 0458:003a
Vendor:Device = 04d9:1503
test: test.c:49: int main(): Assertion `dev_handle == __null' failed.
Aborted (core dumped)

And this is the error message which libusb return.

libusb:error [submit_bulk_transfer] submiturb failed error -1 errno=22
1

There are 1 answers

2
Sauron On

It can happen for any reason. For example, you do not have sufficient rights to open the device or an input/output error occurred during opening it. libusb_open_device_with_vid_pid() does not give you an error code, which you can use for diagnostics. This is the reason not to use libusb_open_device_with_vid_pid() at all.