clibusb

libusb returns two different values for device descriptor when use two different functions


I want to use libusb library to communicate with an HID device. I could implement many of its functions and it was ok. but when I want to get the device descriptor with libusb_get_device_descriptor() function and with libusb_get_descriptor() function, it shows me different values for some fields like Control Max Packet Size.

I've coded as following for two functions:

#include <stdio.h>
#include "libusb.h"

int main(void)
{
  libusb_device_handle* dev_handle;
  int init = libusb_init(NULL);
  dev_handle = libusb_open_device_with_vid_pid(NULL, 0x04d8, 0x01ff);
  libusb_claim_interface(dev_handle, 0);

  unsigned char buf[18];
  
  //first function
  libusb_get_descriptor(dev_handle, LIBUSB_DT_DEVICE, 0, buf, sizeof(buf));

  for (int i = 0; i < sizeof(buf); i++)
  {
    printf("%d ", buf[i]);
  }

// output
//18 1 0 2 0 0 0 64 216 4 255 1 0 1 1 2 0 1
  
  printf("\n");
  
  //second function
  struct libusb_device_descriptor desc;
  libusb_get_device_descriptor(libusb_get_device(dev_handle), &desc);
  printf("%d ", desc.bMaxPacketSize0);


  libusb_release_interface(dev_handle, 0);
  libusb_close(dev_handle);
  libusb_exit(NULL);
  return 0;
}

If you run the above code, the returned value for CTRL_Max_Packet_Size from the first function is equal to 64 but for the second function is 8 that obviously the second one is correct. but Why does the first function return wrong value? I have checked the first function for other descriptors like CONFIG_DESCRIPTOR and at this case also there are some incorrect values for some fields.

I'm using Windows 10 64 bit and I have compiled my code with both visual studio and mingw64. both return same incorrect value.


Solution

  • I found the solution. The problem was for the default installed driver in win10 that is hidUsb. I changed the driver to WinUsb using the Zadig software and now everything is ok.