Skip to content

Renumber endpoints for only chosen USB interfaces; fix HID report ids #2133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions tools/gen_usb_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,21 @@ def strings_in_order(cls):
# args.hid_devices[1] has report_id 2
# etc.

report_ids = {}

if len(args.hid_devices) == 1:
name = args.hid_devices[0]
combined_hid_report_descriptor = hid.ReportDescriptor(
description=name,
report_descriptor=bytes(hid_report_descriptors.REPORT_DESCRIPTOR_FUNCTIONS[name](0)))
report_ids[name] = 0
else:
report_id = 1
concatenated_descriptors = bytearray()
for name in args.hid_devices:
concatenated_descriptors.extend(
bytes(hid_report_descriptors.REPORT_DESCRIPTOR_FUNCTIONS[name](report_id)))
report_ids[name] = report_id
report_id += 1
combined_hid_report_descriptor = hid.ReportDescriptor(
description="MULTIDEVICE",
Expand Down Expand Up @@ -288,10 +292,24 @@ def strings_in_order(cls):
# Audio streaming interfaces must occur before MIDI ones.
audio_interfaces = [audio_control_interface] + cs_ac_interface.audio_streaming_interfaces + cs_ac_interface.midi_streaming_interfaces

# This will renumber the endpoints to make them unique across descriptors,
interfaces_to_join = []

if 'CDC' in args.devices:
interfaces_to_join.append(cdc_interfaces)

if 'MSC' in args.devices:
interfaces_to_join.append(msc_interfaces)

if 'HID' in args.devices:
interfaces_to_join.append(hid_interfaces)

if 'AUDIO' in args.devices:
interfaces_to_join.append(audio_interfaces)

# util.join_interfaces() will renumber the endpoints to make them unique across descriptors,
# and renumber the interfaces in order. But we still need to fix up certain
# interface cross-references.
interfaces = util.join_interfaces(cdc_interfaces, msc_interfaces, hid_interfaces, audio_interfaces)
interfaces = util.join_interfaces(*interfaces_to_join)

# Now adjust the CDC interface cross-references.

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

if 'HID' in args.devices:
descriptor_list.extend(hid_interfaces)

if 'AUDIO' in args.devices:
# Only add the control interface because other audio interfaces are managed by it to ensure the
# correct ordering.
descriptor_list.append(audio_control_interface)

if 'HID' in args.devices:
descriptor_list.extend(hid_interfaces)
# Finally, build the composite descriptor.

configuration = standard.ConfigurationDescriptor(
description="Composite configuration",
Expand Down Expand Up @@ -502,7 +522,7 @@ def strings_in_order(cls):
""")

# Write out USB HID report buffer definitions.
for report_id, name in enumerate(args.hid_devices, start=1):
for name in args.hid_devices:
c_file.write("""\
static uint8_t {name}_report_buffer[{report_length}];
""".format(name=name.lower(), report_length=hid_report_descriptors.HID_DEVICE_DATA[name].report_length))
Expand All @@ -511,18 +531,18 @@ def strings_in_order(cls):
c_file.write("""
usb_hid_device_obj_t usb_hid_devices[] = {
""");
for report_id, name in enumerate(args.hid_devices, start=1):
for name in args.hid_devices:
device_data = hid_report_descriptors.HID_DEVICE_DATA[name]
c_file.write("""\
{{
.base = {{ .type = &usb_hid_device_type }},
.report_buffer = {name}_report_buffer,
.report_id = {report_id:},
.report_id = {report_id},
.report_length = {report_length},
.usage_page = {usage_page:#04x},
.usage = {usage:#04x},
}},
""".format(name=name.lower(), report_id=report_id,
""".format(name=name.lower(), report_id=report_ids[name],
report_length=device_data.report_length,
usage_page=device_data.usage_page,
usage=device_data.usage))
Expand Down