Skip to content

Commit 40132e9

Browse files
author
build
committed
Do not increase max_report_size by one if not using report numbers
1 parent 8271508 commit 40132e9

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

libusb/hid.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ static size_t get_max_report_size(uint8_t * report_descriptor, int desc_size, en
297297
size_t cur_size = 0;
298298
size_t max_size = 0;
299299

300+
int report_id_used = 0;
301+
300302
while (i < desc_size) {
301303
int key = report_descriptor[i];
302304
int key_cmd = key & 0xfc;
@@ -336,6 +338,7 @@ static size_t get_max_report_size(uint8_t * report_descriptor, int desc_size, en
336338
cur_size += (report_count * report_size);
337339
}
338340
if (key_cmd == 0x84) { /* Report ID */
341+
report_id_used = 1;
339342
if (cur_size > max_size) {
340343
max_size = cur_size;
341344
}
@@ -355,8 +358,8 @@ static size_t get_max_report_size(uint8_t * report_descriptor, int desc_size, en
355358
return 0;
356359
} else {
357360
/* report_size is in bits. Determine the total size convert to bytes
358-
(rounded up), and add one byte for the report number. */
359-
return ((max_size + 7) / 8) + 1;
361+
(rounded up), and add one byte for the report number (if used). */
362+
return ((max_size + 7) / 8) + report_id_used;
360363
}
361364
}
362365

libusb/test/max_input_report_size_test.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ static int parse_max_input_report_size(const char * filename, struct max_report_
2121
return -1;
2222
}
2323

24+
int has_report_id = 0;
25+
2426
char line[256];
2527
{
2628
while (fgets(line, sizeof(line), file) != NULL) {
@@ -34,11 +36,31 @@ static int parse_max_input_report_size(const char * filename, struct max_report_
3436
if (sscanf(line, "pp_data->caps_info[2]->ReportByteLength = %hu\n", &temp_ushort) == 1) {
3537
sizes->feature = (size_t)temp_ushort;
3638
}
39+
if (sscanf(line, "pp_data->cap[%*hu]->ReportID = 0x%hu\n", &temp_ushort) == 1) {
40+
if (temp_ushort) {
41+
has_report_id = 1;
42+
}
43+
}
3744
}
3845
}
3946

4047
fclose(file);
4148

49+
// Windows includes ReportID byte in descriptor size even when it is not
50+
// used. Our libusb calculation does not include it, so remove one byte
51+
// from the sizes to make it match.
52+
if (!has_report_id) {
53+
if (sizes->input) {
54+
sizes->input--;
55+
}
56+
if (sizes->output) {
57+
sizes->output--;
58+
}
59+
if (sizes->feature) {
60+
sizes->feature--;
61+
}
62+
}
63+
4264
return 0;
4365
}
4466

0 commit comments

Comments
 (0)