@@ -564,28 +564,43 @@ int HID_API_EXPORT hid_exit(void)
564
564
return 0 ;
565
565
}
566
566
567
- /**
568
- * Requires an opened device with *claimed interface*.
569
- */
570
- static void fill_device_info_usage (struct hid_device_info * cur_dev , libusb_device_handle * handle , int interface_num , uint16_t report_descriptor_size )
567
+ static int hid_get_report_descriptor_libusb (libusb_device_handle * handle , int interface_num , uint16_t expected_report_descriptor_size , unsigned char * buf , size_t buf_size )
571
568
{
572
- unsigned char data [4096 ];
573
- unsigned short page = 0 , usage = 0 ;
569
+ unsigned char tmp [HID_API_MAX_REPORT_DESCRIPTOR_SIZE ];
574
570
575
- if (report_descriptor_size > 4096 )
576
- report_descriptor_size = 4096 ;
571
+ if (expected_report_descriptor_size > HID_API_MAX_REPORT_DESCRIPTOR_SIZE )
572
+ expected_report_descriptor_size = HID_API_MAX_REPORT_DESCRIPTOR_SIZE ;
577
573
578
574
/* Get the HID Report Descriptor.
579
575
See USB HID Specificatin, sectin 7.1.1
580
576
*/
581
- int res = libusb_control_transfer (handle , LIBUSB_ENDPOINT_IN |LIBUSB_RECIPIENT_INTERFACE , LIBUSB_REQUEST_GET_DESCRIPTOR , (LIBUSB_DT_REPORT << 8 ), interface_num , data , report_descriptor_size , 5000 );
577
+ int res = libusb_control_transfer (handle , LIBUSB_ENDPOINT_IN |LIBUSB_RECIPIENT_INTERFACE , LIBUSB_REQUEST_GET_DESCRIPTOR , (LIBUSB_DT_REPORT << 8 ), interface_num , tmp , expected_report_descriptor_size , 5000 );
578
+ if (res < 0 ) {
579
+ LOG ("libusb_control_transfer() for getting the HID Report descriptor failed with %d: %s\n" , res , libusb_error_name (res ));
580
+ return -1 ;
581
+ }
582
+
583
+ if (res > (int )buf_size )
584
+ res = (int )buf_size ;
585
+
586
+ memcpy (buf , tmp , (size_t )res );
587
+ return res ;
588
+ }
589
+
590
+ /**
591
+ * Requires an opened device with *claimed interface*.
592
+ */
593
+ static void fill_device_info_usage (struct hid_device_info * cur_dev , libusb_device_handle * handle , int interface_num , uint16_t expected_report_descriptor_size )
594
+ {
595
+ unsigned char hid_report_descriptor [HID_API_MAX_REPORT_DESCRIPTOR_SIZE ];
596
+ unsigned short page = 0 , usage = 0 ;
597
+
598
+ int res = hid_get_report_descriptor_libusb (handle , interface_num , expected_report_descriptor_size , hid_report_descriptor , sizeof (hid_report_descriptor ));
582
599
if (res >= 0 ) {
583
600
/* Parse the usage and usage page
584
601
out of the report descriptor. */
585
- get_usage (data , res , & page , & usage );
602
+ get_usage (hid_report_descriptor , res , & page , & usage );
586
603
}
587
- else
588
- LOG ("libusb_control_transfer() for getting the HID report descriptor failed with %d: %s\n" , res , libusb_error_name (res ));
589
604
590
605
cur_dev -> usage_page = page ;
591
606
cur_dev -> usage = usage ;
@@ -619,7 +634,7 @@ static void invasive_fill_device_info_usage(struct hid_device_info *cur_dev, lib
619
634
LOG ("Can't release the interface.\n" );
620
635
}
621
636
else
622
- LOG ("Can't claim interface %d \n" , res );
637
+ LOG ("Can't claim interface: (%d) %s \n" , res , libusb_error_name ( res ) );
623
638
624
639
#ifdef DETACH_KERNEL_DRIVER
625
640
/* Re-attach kernel driver if necessary. */
@@ -675,7 +690,7 @@ static uint16_t get_report_descriptor_size_from_interface_descriptors(const stru
675
690
{
676
691
int i = 0 ;
677
692
int found_hid_report_descriptor = 0 ;
678
- uint16_t result = 4096 ;
693
+ uint16_t result = HID_API_MAX_REPORT_DESCRIPTOR_SIZE ;
679
694
const unsigned char * extra = intf_desc -> extra ;
680
695
int extra_length = intf_desc -> extra_length ;
681
696
@@ -943,7 +958,7 @@ static void read_callback(struct libusb_transfer *transfer)
943
958
/* Re-submit the transfer object. */
944
959
res = libusb_submit_transfer (transfer );
945
960
if (res != 0 ) {
946
- LOG ("Unable to submit URB. libusb error code: %d \n" , res );
961
+ LOG ("Unable to submit URB: (%d) %s \n" , res , libusb_error_name ( res ) );
947
962
dev -> shutdown_thread = 1 ;
948
963
dev -> transfer_loop_finished = 1 ;
949
964
}
@@ -981,7 +996,7 @@ static void *read_thread(void *param)
981
996
res = libusb_handle_events (usb_context );
982
997
if (res < 0 ) {
983
998
/* There was an error. */
984
- LOG ("read_thread(): libusb reports error # %d \n" , res );
999
+ LOG ("read_thread(): (%d) %s \n" , res , libusb_error_name ( res ) );
985
1000
986
1001
/* Break out of this loop only on fatal error.*/
987
1002
if (res != LIBUSB_ERROR_BUSY &&
@@ -1036,7 +1051,7 @@ static int hidapi_initialize_device(hid_device *dev, int config_number, const st
1036
1051
if (libusb_kernel_driver_active (dev -> device_handle , intf_desc -> bInterfaceNumber ) == 1 ) {
1037
1052
res = libusb_detach_kernel_driver (dev -> device_handle , intf_desc -> bInterfaceNumber );
1038
1053
if (res < 0 ) {
1039
- LOG ("Unable to detach Kernel Driver\n" );
1054
+ LOG ("Unable to detach Kernel Driver: (%d) %s \n" , res , libusb_error_name ( res ) );
1040
1055
return 0 ;
1041
1056
}
1042
1057
else {
@@ -1047,13 +1062,13 @@ static int hidapi_initialize_device(hid_device *dev, int config_number, const st
1047
1062
#endif
1048
1063
res = libusb_claim_interface (dev -> device_handle , intf_desc -> bInterfaceNumber );
1049
1064
if (res < 0 ) {
1050
- LOG ("can't claim interface %d: %d \n" , intf_desc -> bInterfaceNumber , res );
1065
+ LOG ("can't claim interface %d: (%d) %s \n" , intf_desc -> bInterfaceNumber , res , libusb_error_name ( res ) );
1051
1066
1052
1067
#ifdef DETACH_KERNEL_DRIVER
1053
1068
if (dev -> is_driver_detached ) {
1054
1069
res = libusb_attach_kernel_driver (dev -> device_handle , intf_desc -> bInterfaceNumber );
1055
1070
if (res < 0 )
1056
- LOG ("Failed to reattach the driver to kernel. \n" );
1071
+ LOG ("Failed to reattach the driver to kernel: (%d) %s \n" , res , libusb_error_name ( res ) );
1057
1072
}
1058
1073
#endif
1059
1074
return 0 ;
0 commit comments