Skip to content

Commit 4282ff4

Browse files
authored
Merge pull request #2133 from dhalbert/usb-selection-fix
Renumber endpoints for only chosen USB interfaces; fix HID report ids
2 parents 53b0b35 + 8f62671 commit 4282ff4

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

tools/gen_usb_descriptor.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,21 @@ def strings_in_order(cls):
173173
# args.hid_devices[1] has report_id 2
174174
# etc.
175175

176+
report_ids = {}
177+
176178
if len(args.hid_devices) == 1:
177179
name = args.hid_devices[0]
178180
combined_hid_report_descriptor = hid.ReportDescriptor(
179181
description=name,
180182
report_descriptor=bytes(hid_report_descriptors.REPORT_DESCRIPTOR_FUNCTIONS[name](0)))
183+
report_ids[name] = 0
181184
else:
182185
report_id = 1
183186
concatenated_descriptors = bytearray()
184187
for name in args.hid_devices:
185188
concatenated_descriptors.extend(
186189
bytes(hid_report_descriptors.REPORT_DESCRIPTOR_FUNCTIONS[name](report_id)))
190+
report_ids[name] = report_id
187191
report_id += 1
188192
combined_hid_report_descriptor = hid.ReportDescriptor(
189193
description="MULTIDEVICE",
@@ -288,10 +292,24 @@ def strings_in_order(cls):
288292
# Audio streaming interfaces must occur before MIDI ones.
289293
audio_interfaces = [audio_control_interface] + cs_ac_interface.audio_streaming_interfaces + cs_ac_interface.midi_streaming_interfaces
290294

291-
# This will renumber the endpoints to make them unique across descriptors,
295+
interfaces_to_join = []
296+
297+
if 'CDC' in args.devices:
298+
interfaces_to_join.append(cdc_interfaces)
299+
300+
if 'MSC' in args.devices:
301+
interfaces_to_join.append(msc_interfaces)
302+
303+
if 'HID' in args.devices:
304+
interfaces_to_join.append(hid_interfaces)
305+
306+
if 'AUDIO' in args.devices:
307+
interfaces_to_join.append(audio_interfaces)
308+
309+
# util.join_interfaces() will renumber the endpoints to make them unique across descriptors,
292310
# and renumber the interfaces in order. But we still need to fix up certain
293311
# interface cross-references.
294-
interfaces = util.join_interfaces(cdc_interfaces, msc_interfaces, hid_interfaces, audio_interfaces)
312+
interfaces = util.join_interfaces(*interfaces_to_join)
295313

296314
# Now adjust the CDC interface cross-references.
297315

@@ -323,13 +341,15 @@ def strings_in_order(cls):
323341
if 'MSC' in args.devices:
324342
descriptor_list.extend(msc_interfaces)
325343

344+
if 'HID' in args.devices:
345+
descriptor_list.extend(hid_interfaces)
346+
326347
if 'AUDIO' in args.devices:
327348
# Only add the control interface because other audio interfaces are managed by it to ensure the
328349
# correct ordering.
329350
descriptor_list.append(audio_control_interface)
330351

331-
if 'HID' in args.devices:
332-
descriptor_list.extend(hid_interfaces)
352+
# Finally, build the composite descriptor.
333353

334354
configuration = standard.ConfigurationDescriptor(
335355
description="Composite configuration",
@@ -502,7 +522,7 @@ def strings_in_order(cls):
502522
""")
503523

504524
# Write out USB HID report buffer definitions.
505-
for report_id, name in enumerate(args.hid_devices, start=1):
525+
for name in args.hid_devices:
506526
c_file.write("""\
507527
static uint8_t {name}_report_buffer[{report_length}];
508528
""".format(name=name.lower(), report_length=hid_report_descriptors.HID_DEVICE_DATA[name].report_length))
@@ -511,18 +531,18 @@ def strings_in_order(cls):
511531
c_file.write("""
512532
usb_hid_device_obj_t usb_hid_devices[] = {
513533
""");
514-
for report_id, name in enumerate(args.hid_devices, start=1):
534+
for name in args.hid_devices:
515535
device_data = hid_report_descriptors.HID_DEVICE_DATA[name]
516536
c_file.write("""\
517537
{{
518538
.base = {{ .type = &usb_hid_device_type }},
519539
.report_buffer = {name}_report_buffer,
520-
.report_id = {report_id:},
540+
.report_id = {report_id},
521541
.report_length = {report_length},
522542
.usage_page = {usage_page:#04x},
523543
.usage = {usage:#04x},
524544
}},
525-
""".format(name=name.lower(), report_id=report_id,
545+
""".format(name=name.lower(), report_id=report_ids[name],
526546
report_length=device_data.report_length,
527547
usage_page=device_data.usage_page,
528548
usage=device_data.usage))

0 commit comments

Comments
 (0)