July 2, 2011

holeygaurav holeygaurav
Lab Rat
9 posts

Custom HID Device Driver: Not able to go beyond HidD_GetHidGuid Call?

 

Dear All

We are trying to write driver for our microcontroller based device. We are using Qt mingw ddk and all the supported header files and library files for this project.But not able to go beyond the call to HidD_GetHidGuid, debugging gives us segmentation fault?

We are using a usb mouse with 0×1c4F as vendor id,0×0003 as product id, 0×0110 as its version number all retrieved using USBView utility. We are working on winxp service pack2.

Please guide us…every help would be life saving….We are able to scribble following for the Hid…please go through it…

  1. #include <ddk/usb.h>
  2. #include <setupapi.h>
  3. #include <ddk/hidsdi.h>
  4.  
  5. HANDLE connectToIthUSBHIDDevice (DWORD deviceIndex)
  6. {
  7.     GUID hidGUID;
  8.     HDEVINFO hardwareDeviceInfoSet;
  9.     SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
  10.     PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;
  11.     ULONG requiredSize;
  12.     HANDLE deviceHandle = INVALID_HANDLE_VALUE;
  13.     DWORD result;
  14.  
  15.     //Get the HID GUID value - used as mask to get list of devices
  16.     HidD_GetHidGuid (&hidGUID);
  17.  
  18.     //Get a list of devices matching the criteria (hid interface, present)
  19.     hardwareDeviceInfoSet = SetupDiGetClassDevs (&hidGUID,
  20.                                                  NULL, // Define no enumerator (global)
  21.                                                  NULL, // Define no
  22.                                                  (DIGCF_PRESENT | // Only Devices present
  23.                                                  DIGCF_DEVICEINTERFACE)); // Function class devices.
  24.  
  25.     deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  26.  
  27.     //Go through the list and get the interface data
  28.     result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet,
  29.                                           NULL, //infoData,
  30.                                           &hidGUID, //interfaceClassGuid,
  31.                                           deviceIndex,
  32.                                           &deviceInterfaceData);
  33.  
  34.     /* Failed to get a device - possibly the index is larger than the number of devices */
  35.     if (result == FALSE)
  36.     {
  37.         SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
  38.         return INVALID_HANDLE_VALUE;
  39.     }
  40.  
  41.     //Get the details with null values to get the required size of the buffer
  42.     SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
  43.                                      &deviceInterfaceData,
  44.                                      NULL, //interfaceDetail,
  45.                                      0, //interfaceDetailSize,
  46.                                      &requiredSize,
  47.                                      0); //infoData))
  48.  
  49.     //Allocate the buffer
  50.     deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredSize);
  51.     deviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
  52.  
  53.     //Fill the buffer with the device details
  54.     if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
  55.                                           &deviceInterfaceData,
  56.                                           deviceDetail,
  57.                                           requiredSize,
  58.                                           &requiredSize,
  59.                                           NULL))
  60.     {
  61.         SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
  62.         free (deviceDetail);
  63.         return INVALID_HANDLE_VALUE;
  64.     }
  65.  
  66.     //Open file on the device
  67.     deviceHandle = CreateFile (deviceDetail->DevicePath,
  68.                                GENERIC_READ,
  69.                                FILE_SHARE_READ | FILE_SHARE_WRITE,
  70.                                NULL,        // no SECURITY_ATTRIBUTES structure
  71.                                OPEN_EXISTING, // No special create flags
  72.                                0,
  73.                                NULL);       // No template file
  74.  
  75.     SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
  76.     free (deviceDetail);
  77.     return deviceHandle;
  78. }
  79.  
  80. HANDLE connectToUSBHIDDevice (DWORD *vendorID, DWORD *productID, DWORD *versionNumber)
  81. {
  82.     HANDLE deviceHandle = INVALID_HANDLE_VALUE;
  83.     DWORD index = 0;
  84.     HIDD_ATTRIBUTES deviceAttributes;
  85.     BOOL matched = FALSE;
  86.  
  87.     while (!matched && (deviceHandle = connectToIthUSBHIDDevice (index)) != INVALID_HANDLE_VALUE)
  88.     {
  89.         if (!HidD_GetAttributes (deviceHandle, &deviceAttributes))
  90.             return INVALID_HANDLE_VALUE;
  91.  
  92.         if ((vendorID == 0 || deviceAttributes.VendorID == *vendorID) &&
  93.             (productID == 0 || deviceAttributes.ProductID == *productID) &&
  94.             (versionNumber == 0 || deviceAttributes.VersionNumber == *versionNumber))
  95.             return deviceHandle; /* matched */
  96.        
  97.         CloseHandle (deviceHandle); /* not a match - close and try again */
  98.  
  99.         index++;
  100.     }
  101.  
  102.     return INVALID_HANDLE_VALUE;
  103. }

Thanks and Regards,
Gaurav Holey

0 replies

 
  ‹‹ [SOLVED] Plugin can load or not depending on whether my app is launched from Qt Creator or not      TrayIconMenu Color change. ››

You must log in to post a reply. Not a member yet? Register here!