2020-06-03 18:40:05 -04:00
|
|
|
# SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2017-10-26 19:53:25 -04:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2017-11-02 16:00:50 -04:00
|
|
|
sys.path.append("../../tools/usb_descriptor")
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
from adafruit_usb_descriptor import audio, audio10, cdc, hid, midi, msc, standard, util
|
2018-04-24 13:28:26 -04:00
|
|
|
import hid_report_descriptors
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2019-11-12 16:29:35 -05:00
|
|
|
DEFAULT_INTERFACE_NAME = 'CircuitPython'
|
2021-01-20 21:24:42 -05:00
|
|
|
ALL_DEVICES='CDC,MSC,AUDIO,HID,VENDOR'
|
2019-09-03 14:44:46 -04:00
|
|
|
ALL_DEVICES_SET=frozenset(ALL_DEVICES.split(','))
|
|
|
|
DEFAULT_DEVICES='CDC,MSC,AUDIO,HID'
|
|
|
|
|
2019-09-03 21:16:14 -04:00
|
|
|
ALL_HID_DEVICES='KEYBOARD,MOUSE,CONSUMER,SYS_CONTROL,GAMEPAD,DIGITIZER,XAC_COMPATIBLE_GAMEPAD,RAW'
|
2019-09-03 14:44:46 -04:00
|
|
|
ALL_HID_DEVICES_SET=frozenset(ALL_HID_DEVICES.split(','))
|
|
|
|
# Digitizer works on Linux but conflicts with mouse, so omit it.
|
|
|
|
DEFAULT_HID_DEVICES='KEYBOARD,MOUSE,CONSUMER,GAMEPAD'
|
|
|
|
|
2021-02-04 17:35:42 -05:00
|
|
|
# In the following URL, don't include the https:// because that prefix gets added automatically
|
|
|
|
DEFAULT_WEBUSB_URL = 'circuitpython.org' # In the future, this may become a specific landing page
|
2021-01-22 09:16:10 -05:00
|
|
|
|
2017-10-26 19:53:25 -04:00
|
|
|
parser = argparse.ArgumentParser(description='Generate USB descriptors.')
|
2020-07-29 04:38:55 -04:00
|
|
|
parser.add_argument('--highspeed', default=False, action='store_true',
|
|
|
|
help='descriptor for highspeed device')
|
2017-10-26 19:53:25 -04:00
|
|
|
parser.add_argument('--manufacturer', type=str,
|
|
|
|
help='manufacturer of the device')
|
|
|
|
parser.add_argument('--product', type=str,
|
|
|
|
help='product name of the device')
|
|
|
|
parser.add_argument('--vid', type=lambda x: int(x, 16),
|
|
|
|
help='vendor id')
|
|
|
|
parser.add_argument('--pid', type=lambda x: int(x, 16),
|
|
|
|
help='product id')
|
|
|
|
parser.add_argument('--serial_number_length', type=int, default=32,
|
|
|
|
help='length needed for the serial number in digits')
|
2019-09-03 21:16:14 -04:00
|
|
|
parser.add_argument('--devices', type=lambda l: tuple(l.split(',')), default=DEFAULT_DEVICES,
|
2019-09-03 14:44:46 -04:00
|
|
|
help='devices to include in descriptor (AUDIO includes MIDI support)')
|
2019-09-03 21:16:14 -04:00
|
|
|
parser.add_argument('--hid_devices', type=lambda l: tuple(l.split(',')), default=DEFAULT_HID_DEVICES,
|
2019-09-03 14:44:46 -04:00
|
|
|
help='HID devices to include in HID report descriptor')
|
2019-11-12 16:29:35 -05:00
|
|
|
parser.add_argument('--interface_name', type=str,
|
|
|
|
help='The name/prefix to use in the interface descriptions',
|
|
|
|
default=DEFAULT_INTERFACE_NAME)
|
2019-10-08 03:26:02 -04:00
|
|
|
parser.add_argument('--no-renumber_endpoints', dest='renumber_endpoints', action='store_false',
|
|
|
|
help='use to not renumber endpoint')
|
2019-10-01 04:00:32 -04:00
|
|
|
parser.add_argument('--cdc_ep_num_notification', type=int, default=0,
|
|
|
|
help='endpoint number of CDC NOTIFICATION')
|
|
|
|
parser.add_argument('--cdc_ep_num_data_out', type=int, default=0,
|
|
|
|
help='endpoint number of CDC DATA OUT')
|
|
|
|
parser.add_argument('--cdc_ep_num_data_in', type=int, default=0,
|
|
|
|
help='endpoint number of CDC DATA IN')
|
|
|
|
parser.add_argument('--msc_ep_num_out', type=int, default=0,
|
|
|
|
help='endpoint number of MSC OUT')
|
|
|
|
parser.add_argument('--msc_ep_num_in', type=int, default=0,
|
|
|
|
help='endpoint number of MSC IN')
|
2019-10-07 07:40:44 -04:00
|
|
|
parser.add_argument('--hid_ep_num_out', type=int, default=0,
|
|
|
|
help='endpoint number of HID OUT')
|
2019-10-01 04:00:32 -04:00
|
|
|
parser.add_argument('--hid_ep_num_in', type=int, default=0,
|
|
|
|
help='endpoint number of HID IN')
|
|
|
|
parser.add_argument('--midi_ep_num_out', type=int, default=0,
|
|
|
|
help='endpoint number of MIDI OUT')
|
|
|
|
parser.add_argument('--midi_ep_num_in', type=int, default=0,
|
|
|
|
help='endpoint number of MIDI IN')
|
2021-01-22 09:16:10 -05:00
|
|
|
parser.add_argument('--webusb_url', type=str,
|
|
|
|
help='The URL to include in the WebUSB URL Descriptor',
|
|
|
|
default=DEFAULT_WEBUSB_URL)
|
2021-01-20 21:24:42 -05:00
|
|
|
parser.add_argument('--vendor_ep_num_out', type=int, default=0,
|
|
|
|
help='endpoint number of VENDOR OUT')
|
|
|
|
parser.add_argument('--vendor_ep_num_in', type=int, default=0,
|
|
|
|
help='endpoint number of VENDOR IN')
|
2020-08-19 17:47:20 -04:00
|
|
|
parser.add_argument('--max_ep', type=int, default=0,
|
|
|
|
help='total number of endpoints available')
|
2020-07-28 14:19:43 -04:00
|
|
|
parser.add_argument('--output_c_file', type=argparse.FileType('w', encoding='UTF-8'), required=True)
|
|
|
|
parser.add_argument('--output_h_file', type=argparse.FileType('w', encoding='UTF-8'), required=True)
|
2017-10-26 19:53:25 -04:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-09-03 21:16:14 -04:00
|
|
|
unknown_devices = list(frozenset(args.devices) - ALL_DEVICES_SET)
|
2019-09-03 14:44:46 -04:00
|
|
|
if unknown_devices:
|
|
|
|
raise ValueError("Unknown device(s)", unknown_devices)
|
|
|
|
|
2019-09-03 21:16:14 -04:00
|
|
|
unknown_hid_devices = list(frozenset(args.hid_devices) - ALL_HID_DEVICES_SET)
|
2019-09-03 14:44:46 -04:00
|
|
|
if unknown_hid_devices:
|
|
|
|
raise ValueError("Unknown HID devices(s)", unknown_hid_devices)
|
|
|
|
|
2019-10-04 05:00:00 -04:00
|
|
|
if not args.renumber_endpoints:
|
|
|
|
if 'CDC' in args.devices:
|
2019-10-08 03:35:04 -04:00
|
|
|
if args.cdc_ep_num_notification == 0:
|
|
|
|
raise ValueError("CDC notification endpoint number must not be 0")
|
|
|
|
elif args.cdc_ep_num_data_out == 0:
|
|
|
|
raise ValueError("CDC data OUT endpoint number must not be 0")
|
2019-10-08 03:52:00 -04:00
|
|
|
elif args.cdc_ep_num_data_in == 0:
|
2019-10-08 03:35:04 -04:00
|
|
|
raise ValueError("CDC data IN endpoint number must not be 0")
|
2019-10-04 05:00:00 -04:00
|
|
|
|
|
|
|
if 'MSC' in args.devices:
|
2019-10-08 03:35:04 -04:00
|
|
|
if args.msc_ep_num_out == 0:
|
|
|
|
raise ValueError("MSC endpoint OUT number must not be 0")
|
2021-01-20 21:24:42 -05:00
|
|
|
elif args.msc_ep_num_in == 0:
|
2019-10-08 03:35:04 -04:00
|
|
|
raise ValueError("MSC endpoint IN number must not be 0")
|
2019-10-04 05:00:00 -04:00
|
|
|
|
|
|
|
if 'HID' in args.devices:
|
2019-10-08 03:35:04 -04:00
|
|
|
if args.args.hid_ep_num_out == 0:
|
|
|
|
raise ValueError("HID endpoint OUT number must not be 0")
|
2021-01-20 21:24:42 -05:00
|
|
|
elif args.hid_ep_num_in == 0:
|
2019-10-08 03:35:04 -04:00
|
|
|
raise ValueError("HID endpoint IN number must not be 0")
|
2019-10-04 05:00:00 -04:00
|
|
|
|
|
|
|
if 'AUDIO' in args.devices:
|
2019-10-08 03:35:04 -04:00
|
|
|
if args.args.midi_ep_num_out == 0:
|
|
|
|
raise ValueError("MIDI endpoint OUT number must not be 0")
|
2021-01-20 21:24:42 -05:00
|
|
|
elif args.midi_ep_num_in == 0:
|
2019-10-08 03:35:04 -04:00
|
|
|
raise ValueError("MIDI endpoint IN number must not be 0")
|
2019-10-04 05:00:00 -04:00
|
|
|
|
2021-01-20 21:24:42 -05:00
|
|
|
if 'VENDOR' in args.devices:
|
|
|
|
if args.vendor_ep_num_out == 0:
|
|
|
|
raise ValueError("VENDOR endpoint OUT number must not be 0")
|
|
|
|
elif args.vendor_ep_num_in == 0:
|
|
|
|
raise ValueError("VENDOR endpoint IN number must not be 0")
|
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
class StringIndex:
|
|
|
|
"""Assign a monotonically increasing index to each unique string. Start with 0."""
|
|
|
|
string_to_index = {}
|
2018-10-19 21:46:22 -04:00
|
|
|
index_to_variable = {}
|
2018-04-08 09:33:02 -04:00
|
|
|
strings = []
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
@classmethod
|
2018-10-19 21:46:22 -04:00
|
|
|
def index(cls, string, *, variable_name = None):
|
2018-04-08 09:33:02 -04:00
|
|
|
if string in cls.string_to_index:
|
2018-10-19 21:46:22 -04:00
|
|
|
idx = cls.string_to_index[string]
|
|
|
|
if not cls.index_to_variable[idx]:
|
|
|
|
cls.index_to_variable[idx] = variable_name
|
|
|
|
return idx
|
2018-04-08 09:33:02 -04:00
|
|
|
else:
|
|
|
|
idx = len(cls.strings)
|
|
|
|
cls.string_to_index[string] = idx
|
|
|
|
cls.strings.append(string)
|
2018-10-19 21:46:22 -04:00
|
|
|
cls.index_to_variable[idx] = variable_name
|
2018-04-08 09:33:02 -04:00
|
|
|
return idx
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def strings_in_order(cls):
|
|
|
|
return cls.strings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# langid must be the 0th string descriptor
|
2018-10-19 21:46:22 -04:00
|
|
|
LANGID_INDEX = StringIndex.index("\u0409", variable_name="language_id")
|
2018-04-08 09:33:02 -04:00
|
|
|
assert LANGID_INDEX == 0
|
2018-10-19 21:46:22 -04:00
|
|
|
SERIAL_NUMBER_INDEX = StringIndex.index("S" * args.serial_number_length, variable_name="usb_serial_number")
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2017-11-02 16:00:50 -04:00
|
|
|
device = standard.DeviceDescriptor(
|
2018-03-30 23:20:24 -04:00
|
|
|
description="top",
|
2017-10-26 19:53:25 -04:00
|
|
|
idVendor=args.vid,
|
|
|
|
idProduct=args.pid,
|
2018-04-08 09:33:02 -04:00
|
|
|
iManufacturer=StringIndex.index(args.manufacturer),
|
|
|
|
iProduct=StringIndex.index(args.product),
|
|
|
|
iSerialNumber=SERIAL_NUMBER_INDEX)
|
|
|
|
|
|
|
|
# Interface numbers are interface-set local and endpoints are interface local
|
|
|
|
# until util.join_interfaces renumbers them.
|
|
|
|
|
|
|
|
cdc_union = cdc.Union(
|
|
|
|
description="CDC comm",
|
|
|
|
bMasterInterface=0x00, # Adjust this after interfaces are renumbered.
|
|
|
|
bSlaveInterface_list=[0x01]) # Adjust this after interfaces are renumbered.
|
|
|
|
|
|
|
|
cdc_call_management = cdc.CallManagement(
|
|
|
|
description="CDC comm",
|
|
|
|
bmCapabilities=0x01,
|
|
|
|
bDataInterface=0x01) # Adjust this after interfaces are renumbered.
|
|
|
|
|
|
|
|
cdc_comm_interface = standard.InterfaceDescriptor(
|
|
|
|
description="CDC comm",
|
|
|
|
bInterfaceClass=cdc.CDC_CLASS_COMM, # Communications Device Class
|
|
|
|
bInterfaceSubClass=cdc.CDC_SUBCLASS_ACM, # Abstract control model
|
2018-11-09 14:33:56 -05:00
|
|
|
bInterfaceProtocol=cdc.CDC_PROTOCOL_NONE,
|
2019-11-12 16:29:35 -05:00
|
|
|
iInterface=StringIndex.index("{} CDC control".format(args.interface_name)),
|
2018-04-08 09:33:02 -04:00
|
|
|
subdescriptors=[
|
|
|
|
cdc.Header(
|
|
|
|
description="CDC comm",
|
|
|
|
bcdCDC=0x0110),
|
|
|
|
cdc_call_management,
|
|
|
|
cdc.AbstractControlManagement(
|
|
|
|
description="CDC comm",
|
|
|
|
bmCapabilities=0x02),
|
|
|
|
cdc_union,
|
|
|
|
standard.EndpointDescriptor(
|
|
|
|
description="CDC comm in",
|
2019-10-01 04:00:32 -04:00
|
|
|
bEndpointAddress=args.cdc_ep_num_notification | standard.EndpointDescriptor.DIRECTION_IN,
|
2018-04-08 09:33:02 -04:00
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_INTERRUPT,
|
|
|
|
wMaxPacketSize=0x0040,
|
|
|
|
bInterval=0x10)
|
|
|
|
])
|
|
|
|
|
|
|
|
cdc_data_interface = standard.InterfaceDescriptor(
|
|
|
|
description="CDC data",
|
|
|
|
bInterfaceClass=cdc.CDC_CLASS_DATA,
|
2019-11-12 16:29:35 -05:00
|
|
|
iInterface=StringIndex.index("{} CDC data".format(args.interface_name)),
|
2018-04-08 09:33:02 -04:00
|
|
|
subdescriptors=[
|
|
|
|
standard.EndpointDescriptor(
|
|
|
|
description="CDC data out",
|
2019-10-01 04:00:32 -04:00
|
|
|
bEndpointAddress=args.cdc_ep_num_data_out | standard.EndpointDescriptor.DIRECTION_OUT,
|
2020-07-29 04:38:55 -04:00
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK,
|
|
|
|
bInterval=0,
|
|
|
|
wMaxPacketSize=512 if args.highspeed else 64),
|
2018-04-08 09:33:02 -04:00
|
|
|
standard.EndpointDescriptor(
|
|
|
|
description="CDC data in",
|
2019-10-01 04:00:32 -04:00
|
|
|
bEndpointAddress=args.cdc_ep_num_data_in | standard.EndpointDescriptor.DIRECTION_IN,
|
2020-07-29 04:38:55 -04:00
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK,
|
|
|
|
bInterval=0,
|
|
|
|
wMaxPacketSize=512 if args.highspeed else 64),
|
2018-04-08 09:33:02 -04:00
|
|
|
])
|
|
|
|
|
|
|
|
cdc_interfaces = [cdc_comm_interface, cdc_data_interface]
|
2017-10-26 19:53:25 -04:00
|
|
|
|
|
|
|
msc_interfaces = [
|
2017-11-02 16:00:50 -04:00
|
|
|
standard.InterfaceDescriptor(
|
2018-03-30 23:20:24 -04:00
|
|
|
description="MSC",
|
2018-03-24 18:29:12 -04:00
|
|
|
bInterfaceClass=msc.MSC_CLASS,
|
|
|
|
bInterfaceSubClass=msc.MSC_SUBCLASS_TRANSPARENT,
|
|
|
|
bInterfaceProtocol=msc.MSC_PROTOCOL_BULK,
|
2019-11-12 16:29:35 -05:00
|
|
|
iInterface=StringIndex.index("{} Mass Storage".format(args.interface_name)),
|
2017-10-26 19:53:25 -04:00
|
|
|
subdescriptors=[
|
2017-11-02 16:00:50 -04:00
|
|
|
standard.EndpointDescriptor(
|
2018-03-30 23:20:24 -04:00
|
|
|
description="MSC in",
|
2019-10-01 04:00:32 -04:00
|
|
|
bEndpointAddress=args.msc_ep_num_in | standard.EndpointDescriptor.DIRECTION_IN,
|
2018-04-16 20:18:51 -04:00
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK,
|
2019-10-04 07:49:33 -04:00
|
|
|
bInterval=0,
|
2020-07-29 04:38:55 -04:00
|
|
|
wMaxPacketSize=512 if args.highspeed else 64),
|
2017-11-02 16:00:50 -04:00
|
|
|
standard.EndpointDescriptor(
|
2018-03-30 23:20:24 -04:00
|
|
|
description="MSC out",
|
2019-10-01 04:00:32 -04:00
|
|
|
bEndpointAddress=(args.msc_ep_num_out | standard.EndpointDescriptor.DIRECTION_OUT),
|
2018-04-16 20:18:51 -04:00
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK,
|
2019-10-04 07:49:33 -04:00
|
|
|
bInterval=0,
|
2020-07-29 04:38:55 -04:00
|
|
|
wMaxPacketSize=512 if args.highspeed else 64),
|
2017-10-26 19:53:25 -04:00
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2019-09-03 21:16:14 -04:00
|
|
|
# When there's only one hid_device, it shouldn't have a report id.
|
|
|
|
# Otherwise, report ids are assigned sequentially:
|
|
|
|
# args.hid_devices[0] has report_id 1
|
|
|
|
# args.hid_devices[1] has report_id 2
|
|
|
|
# etc.
|
2019-09-03 14:44:46 -04:00
|
|
|
|
2019-09-09 15:12:06 -04:00
|
|
|
report_ids = {}
|
|
|
|
|
2019-09-03 21:16:14 -04:00
|
|
|
if len(args.hid_devices) == 1:
|
|
|
|
name = args.hid_devices[0]
|
2019-09-03 14:44:46 -04:00
|
|
|
combined_hid_report_descriptor = hid.ReportDescriptor(
|
|
|
|
description=name,
|
2019-09-03 21:16:14 -04:00
|
|
|
report_descriptor=bytes(hid_report_descriptors.REPORT_DESCRIPTOR_FUNCTIONS[name](0)))
|
2019-09-09 15:12:06 -04:00
|
|
|
report_ids[name] = 0
|
2019-09-03 14:44:46 -04:00
|
|
|
else:
|
2019-09-03 21:16:14 -04:00
|
|
|
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)))
|
2019-09-09 15:12:06 -04:00
|
|
|
report_ids[name] = report_id
|
2019-09-03 21:16:14 -04:00
|
|
|
report_id += 1
|
2019-09-03 14:44:46 -04:00
|
|
|
combined_hid_report_descriptor = hid.ReportDescriptor(
|
2019-09-03 21:16:14 -04:00
|
|
|
description="MULTIDEVICE",
|
|
|
|
report_descriptor=bytes(concatenated_descriptors))
|
2018-03-24 18:29:12 -04:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
# ASF4 expects keyboard and generic devices to have both in and out endpoints,
|
2018-04-02 19:08:18 -04:00
|
|
|
# and will fail (possibly silently) if both are not supplied.
|
2018-03-30 23:20:24 -04:00
|
|
|
hid_endpoint_in_descriptor = standard.EndpointDescriptor(
|
|
|
|
description="HID in",
|
2019-10-01 04:00:32 -04:00
|
|
|
bEndpointAddress=args.hid_ep_num_in | standard.EndpointDescriptor.DIRECTION_IN,
|
2018-03-30 23:20:24 -04:00
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_INTERRUPT,
|
2019-10-01 15:57:16 -04:00
|
|
|
bInterval=8)
|
|
|
|
|
|
|
|
hid_endpoint_out_descriptor = standard.EndpointDescriptor(
|
|
|
|
description="HID out",
|
2019-10-07 07:40:44 -04:00
|
|
|
bEndpointAddress=args.hid_ep_num_out | standard.EndpointDescriptor.DIRECTION_OUT,
|
2019-10-01 15:57:16 -04:00
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_INTERRUPT,
|
|
|
|
bInterval=8)
|
2018-03-24 18:29:12 -04:00
|
|
|
|
|
|
|
hid_interfaces = [
|
|
|
|
standard.InterfaceDescriptor(
|
2018-04-08 09:33:02 -04:00
|
|
|
description="HID Multiple Devices",
|
2018-03-30 23:20:24 -04:00
|
|
|
bInterfaceClass=hid.HID_CLASS,
|
|
|
|
bInterfaceSubClass=hid.HID_SUBCLASS_NOBOOT,
|
2018-04-08 09:33:02 -04:00
|
|
|
bInterfaceProtocol=hid.HID_PROTOCOL_NONE,
|
2019-11-12 16:29:35 -05:00
|
|
|
iInterface=StringIndex.index("{} HID".format(args.interface_name)),
|
2018-03-24 18:29:12 -04:00
|
|
|
subdescriptors=[
|
2018-04-08 09:33:02 -04:00
|
|
|
hid.HIDDescriptor(
|
|
|
|
description="HID",
|
2018-04-16 20:18:51 -04:00
|
|
|
wDescriptorLength=len(bytes(combined_hid_report_descriptor))),
|
2018-03-30 23:20:24 -04:00
|
|
|
hid_endpoint_in_descriptor,
|
2019-10-01 15:57:16 -04:00
|
|
|
hid_endpoint_out_descriptor,
|
2018-03-24 18:29:12 -04:00
|
|
|
]
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
# Audio!
|
2018-11-16 20:04:42 -05:00
|
|
|
# In and out here are relative to CircuitPython
|
|
|
|
|
|
|
|
# USB OUT -> midi_in_jack_emb -> midi_out_jack_ext -> CircuitPython
|
|
|
|
midi_in_jack_emb = midi.InJackDescriptor(
|
2019-11-12 16:29:35 -05:00
|
|
|
description="MIDI PC -> {}".format(args.interface_name),
|
2018-10-19 21:46:22 -04:00
|
|
|
bJackType=midi.JACK_TYPE_EMBEDDED,
|
2019-11-12 16:29:35 -05:00
|
|
|
iJack=StringIndex.index("{} usb_midi.ports[0]".format(args.interface_name)))
|
2018-11-16 20:04:42 -05:00
|
|
|
midi_out_jack_ext = midi.OutJackDescriptor(
|
|
|
|
description="MIDI data out to user code.",
|
|
|
|
bJackType=midi.JACK_TYPE_EXTERNAL,
|
|
|
|
input_pins=[(midi_in_jack_emb, 1)],
|
|
|
|
iJack=0)
|
|
|
|
|
|
|
|
# USB IN <- midi_out_jack_emb <- midi_in_jack_ext <- CircuitPython
|
|
|
|
midi_in_jack_ext = midi.InJackDescriptor(
|
|
|
|
description="MIDI data in from user code.",
|
|
|
|
bJackType=midi.JACK_TYPE_EXTERNAL,
|
|
|
|
iJack=0)
|
|
|
|
midi_out_jack_emb = midi.OutJackDescriptor(
|
2019-11-12 16:29:35 -05:00
|
|
|
description="MIDI PC <- {}".format(args.interface_name),
|
2018-10-19 21:46:22 -04:00
|
|
|
bJackType=midi.JACK_TYPE_EMBEDDED,
|
2018-11-16 20:04:42 -05:00
|
|
|
input_pins=[(midi_in_jack_ext, 1)],
|
2019-11-12 16:29:35 -05:00
|
|
|
iJack=StringIndex.index("{} usb_midi.ports[1]".format(args.interface_name)))
|
2018-11-16 20:04:42 -05:00
|
|
|
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
audio_midi_interface = standard.InterfaceDescriptor(
|
2018-11-16 20:04:42 -05:00
|
|
|
description="Midi goodness",
|
2018-10-19 21:46:22 -04:00
|
|
|
bInterfaceClass=audio.AUDIO_CLASS_DEVICE,
|
|
|
|
bInterfaceSubClass=audio.AUDIO_SUBCLASS_MIDI_STREAMING,
|
|
|
|
bInterfaceProtocol=audio.AUDIO_PROTOCOL_V1,
|
2019-11-12 16:29:35 -05:00
|
|
|
iInterface=StringIndex.index("{} MIDI".format(args.interface_name)),
|
2018-10-19 21:46:22 -04:00
|
|
|
subdescriptors=[
|
|
|
|
midi.Header(
|
|
|
|
jacks_and_elements=[
|
2018-11-16 20:04:42 -05:00
|
|
|
midi_in_jack_emb,
|
|
|
|
midi_in_jack_ext,
|
|
|
|
midi_out_jack_emb,
|
|
|
|
midi_out_jack_ext
|
|
|
|
],
|
2018-10-19 21:46:22 -04:00
|
|
|
),
|
|
|
|
standard.EndpointDescriptor(
|
2019-11-12 16:29:35 -05:00
|
|
|
description="MIDI data out to {}".format(args.interface_name),
|
2019-10-01 04:00:32 -04:00
|
|
|
bEndpointAddress=args.midi_ep_num_out | standard.EndpointDescriptor.DIRECTION_OUT,
|
2020-07-29 05:05:31 -04:00
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK,
|
|
|
|
bInterval=0,
|
|
|
|
wMaxPacketSize=512 if args.highspeed else 64),
|
2018-11-16 20:04:42 -05:00
|
|
|
midi.DataEndpointDescriptor(baAssocJack=[midi_in_jack_emb]),
|
2018-10-19 21:46:22 -04:00
|
|
|
standard.EndpointDescriptor(
|
2019-11-12 16:29:35 -05:00
|
|
|
description="MIDI data in from {}".format(args.interface_name),
|
2019-10-01 04:00:32 -04:00
|
|
|
bEndpointAddress=args.midi_ep_num_in | standard.EndpointDescriptor.DIRECTION_IN,
|
2018-11-16 20:04:42 -05:00
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK,
|
2020-07-29 05:05:31 -04:00
|
|
|
bInterval = 0x0,
|
|
|
|
wMaxPacketSize=512 if args.highspeed else 64),
|
2018-11-16 20:04:42 -05:00
|
|
|
midi.DataEndpointDescriptor(baAssocJack=[midi_out_jack_emb]),
|
2018-10-19 21:46:22 -04:00
|
|
|
])
|
|
|
|
|
|
|
|
cs_ac_interface = audio10.AudioControlInterface(
|
|
|
|
description="Empty audio control",
|
|
|
|
audio_streaming_interfaces = [],
|
|
|
|
midi_streaming_interfaces = [
|
|
|
|
audio_midi_interface
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
audio_control_interface = standard.InterfaceDescriptor(
|
|
|
|
description="All the audio",
|
|
|
|
bInterfaceClass=audio.AUDIO_CLASS_DEVICE,
|
|
|
|
bInterfaceSubClass=audio.AUDIO_SUBCLASS_CONTROL,
|
|
|
|
bInterfaceProtocol=audio.AUDIO_PROTOCOL_V1,
|
2019-11-12 16:29:35 -05:00
|
|
|
iInterface=StringIndex.index("{} Audio".format(args.interface_name)),
|
2018-10-19 21:46:22 -04:00
|
|
|
subdescriptors=[
|
|
|
|
cs_ac_interface,
|
|
|
|
])
|
|
|
|
|
|
|
|
# Audio streaming interfaces must occur before MIDI ones.
|
2018-11-16 20:04:42 -05:00
|
|
|
audio_interfaces = [audio_control_interface] + cs_ac_interface.audio_streaming_interfaces + cs_ac_interface.midi_streaming_interfaces
|
2018-10-19 21:46:22 -04:00
|
|
|
|
2021-02-03 19:51:48 -05:00
|
|
|
# Vendor-specific interface, for example WebUSB
|
2021-01-28 21:22:31 -05:00
|
|
|
vendor_endpoint_in_descriptor = standard.EndpointDescriptor(
|
|
|
|
description="VENDOR in",
|
|
|
|
bEndpointAddress=args.vendor_ep_num_in | standard.EndpointDescriptor.DIRECTION_IN,
|
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK,
|
|
|
|
bInterval=16)
|
|
|
|
|
|
|
|
vendor_endpoint_out_descriptor = standard.EndpointDescriptor(
|
|
|
|
description="VENDOR out",
|
|
|
|
bEndpointAddress=args.vendor_ep_num_out | standard.EndpointDescriptor.DIRECTION_OUT,
|
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK,
|
|
|
|
bInterval=16)
|
|
|
|
|
2021-02-03 12:16:19 -05:00
|
|
|
# We do the following conditionally to avoid adding unused entries to the StringIndex table
|
|
|
|
if 'VENDOR' in args.devices:
|
|
|
|
vendor_interface = standard.InterfaceDescriptor(
|
|
|
|
description="VENDOR",
|
2021-02-03 19:51:48 -05:00
|
|
|
bInterfaceClass=0xff, # Vendor-specific
|
|
|
|
bInterfaceSubClass=0x00,
|
|
|
|
bInterfaceProtocol=0x00,
|
2021-02-03 12:16:19 -05:00
|
|
|
iInterface=StringIndex.index("{} VENDOR".format(args.interface_name)),
|
|
|
|
subdescriptors=[
|
|
|
|
vendor_endpoint_in_descriptor,
|
|
|
|
vendor_endpoint_out_descriptor,
|
|
|
|
]
|
|
|
|
)
|
2021-01-20 21:24:42 -05:00
|
|
|
|
2021-02-03 12:16:19 -05:00
|
|
|
vendor_interfaces = [vendor_interface]
|
2021-01-20 21:24:42 -05:00
|
|
|
|
2019-09-09 15:12:06 -04:00
|
|
|
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)
|
|
|
|
|
2021-01-20 21:24:42 -05:00
|
|
|
if 'VENDOR' in args.devices:
|
|
|
|
interfaces_to_join.append(vendor_interfaces)
|
|
|
|
|
2019-10-04 05:00:00 -04:00
|
|
|
# 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(interfaces_to_join, renumber_endpoints=args.renumber_endpoints)
|
2018-04-08 09:33:02 -04:00
|
|
|
|
2020-08-19 17:47:20 -04:00
|
|
|
if args.max_ep != 0:
|
|
|
|
for interface in interfaces:
|
|
|
|
for subdescriptor in interface.subdescriptors:
|
|
|
|
endpoint_address = getattr(subdescriptor, 'bEndpointAddress', 0) & 0x7f
|
2020-08-27 16:10:52 -04:00
|
|
|
if endpoint_address >= args.max_ep:
|
|
|
|
raise ValueError("Endpoint address %d of %s must be less than %d" % (endpoint_address & 0x7f, interface.description, args.max_ep))
|
2020-08-19 17:47:20 -04:00
|
|
|
else:
|
|
|
|
print("Unable to check whether maximum number of endpoints is respected", file=sys.stderr)
|
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
# Now adjust the CDC interface cross-references.
|
|
|
|
|
|
|
|
cdc_union.bMasterInterface = cdc_comm_interface.bInterfaceNumber
|
|
|
|
cdc_union.bSlaveInterface_list = [cdc_data_interface.bInterfaceNumber]
|
|
|
|
|
|
|
|
cdc_call_management.bDataInterface = cdc_data_interface.bInterfaceNumber
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
cdc_iad = standard.InterfaceAssociationDescriptor(
|
|
|
|
description="CDC IAD",
|
|
|
|
bFirstInterface=cdc_comm_interface.bInterfaceNumber,
|
2017-10-26 19:53:25 -04:00
|
|
|
bInterfaceCount=len(cdc_interfaces),
|
2018-10-19 21:46:22 -04:00
|
|
|
bFunctionClass=cdc.CDC_CLASS_COMM, # Communications Device Class
|
|
|
|
bFunctionSubClass=cdc.CDC_SUBCLASS_ACM, # Abstract control model
|
2018-11-09 14:33:56 -05:00
|
|
|
bFunctionProtocol=cdc.CDC_PROTOCOL_NONE)
|
2018-10-19 21:46:22 -04:00
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
descriptor_list = []
|
2019-09-03 14:44:46 -04:00
|
|
|
|
|
|
|
if 'CDC' in args.devices:
|
|
|
|
# Put the CDC IAD just before the CDC interfaces.
|
|
|
|
# There appears to be a bug in the Windows composite USB driver that requests the
|
|
|
|
# HID report descriptor with the wrong interface number if the HID interface is not given
|
|
|
|
# first. However, it still fetches the descriptor anyway. We could reorder the interfaces but
|
|
|
|
# the Windows 7 Adafruit_usbser.inf file thinks CDC is at Interface 0, so we'll leave it
|
|
|
|
# there for backwards compatibility.
|
|
|
|
descriptor_list.append(cdc_iad)
|
|
|
|
descriptor_list.extend(cdc_interfaces)
|
|
|
|
|
|
|
|
if 'MSC' in args.devices:
|
|
|
|
descriptor_list.extend(msc_interfaces)
|
|
|
|
|
2019-09-09 15:12:06 -04:00
|
|
|
if 'HID' in args.devices:
|
|
|
|
descriptor_list.extend(hid_interfaces)
|
|
|
|
|
2019-09-03 14:44:46 -04:00
|
|
|
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)
|
|
|
|
|
2021-01-20 21:24:42 -05:00
|
|
|
if 'VENDOR' in args.devices:
|
|
|
|
descriptor_list.extend(vendor_interfaces)
|
|
|
|
|
2019-09-09 15:12:06 -04:00
|
|
|
# Finally, build the composite descriptor.
|
2018-04-08 09:33:02 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
configuration = standard.ConfigurationDescriptor(
|
|
|
|
description="Composite configuration",
|
|
|
|
wTotalLength=(standard.ConfigurationDescriptor.bLength +
|
|
|
|
sum([len(bytes(x)) for x in descriptor_list])),
|
|
|
|
bNumInterfaces=len(interfaces))
|
|
|
|
descriptor_list.insert(0, configuration)
|
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
string_descriptors = [standard.StringDescriptor(string) for string in StringIndex.strings_in_order()]
|
|
|
|
serial_number_descriptor = string_descriptors[SERIAL_NUMBER_INDEX]
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file = args.output_c_file
|
|
|
|
h_file = args.output_h_file
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
|
|
|
|
c_file.write("""\
|
2018-03-24 18:29:12 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2021-01-25 21:37:58 -05:00
|
|
|
#include "tusb.h"
|
2019-09-03 21:16:14 -04:00
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "shared-bindings/usb_hid/Device.h"
|
2018-03-30 23:20:24 -04:00
|
|
|
#include "{H_FILE_NAME}"
|
|
|
|
|
|
|
|
""".format(H_FILE_NAME=h_file.name))
|
2018-03-24 18:29:12 -04:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("""\
|
2018-10-19 21:46:22 -04:00
|
|
|
// {DESCRIPTION} : {CLASS}
|
|
|
|
""".format(DESCRIPTION=device.description,
|
|
|
|
CLASS=device.__class__))
|
|
|
|
|
|
|
|
c_file.write("""\
|
|
|
|
const uint8_t usb_desc_dev[] = {
|
|
|
|
""")
|
|
|
|
for b in bytes(device):
|
|
|
|
c_file.write("0x{:02x}, ".format(b))
|
|
|
|
|
|
|
|
c_file.write("""\
|
|
|
|
};
|
|
|
|
""")
|
|
|
|
|
|
|
|
c_file.write("""\
|
|
|
|
const uint8_t usb_desc_cfg[] = {
|
2018-03-24 18:29:12 -04:00
|
|
|
""")
|
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
# Write out all the regular descriptors as one long array (that's how ASF4 does it).
|
2017-10-26 19:53:25 -04:00
|
|
|
descriptor_length = 0
|
|
|
|
for descriptor in descriptor_list:
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("""\
|
|
|
|
// {DESCRIPTION} : {CLASS}
|
|
|
|
""".format(DESCRIPTION=descriptor.description,
|
|
|
|
CLASS=descriptor.__class__))
|
|
|
|
|
2017-10-26 19:53:25 -04:00
|
|
|
b = bytes(descriptor)
|
2018-10-19 21:46:22 -04:00
|
|
|
notes = descriptor.notes()
|
2017-10-26 19:53:25 -04:00
|
|
|
i = 0
|
2018-03-30 23:20:24 -04:00
|
|
|
|
2017-10-26 19:53:25 -04:00
|
|
|
# This prints each subdescriptor on a separate line.
|
2018-10-19 21:46:22 -04:00
|
|
|
n = 0
|
2017-10-26 19:53:25 -04:00
|
|
|
while i < len(b):
|
|
|
|
length = b[i]
|
|
|
|
for j in range(length):
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("0x{:02x}, ".format(b[i + j]))
|
2018-10-19 21:46:22 -04:00
|
|
|
c_file.write("// " + notes[n])
|
|
|
|
n += 1
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("\n")
|
2017-10-26 19:53:25 -04:00
|
|
|
i += length
|
2018-10-19 21:46:22 -04:00
|
|
|
descriptor_length += len(b)
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
c_file.write("""\
|
|
|
|
};
|
|
|
|
""")
|
|
|
|
|
|
|
|
pointers_to_strings = []
|
|
|
|
|
|
|
|
for idx, descriptor in enumerate(string_descriptors):
|
|
|
|
c_file.write("""\
|
|
|
|
// {DESCRIPTION} : {CLASS}
|
|
|
|
""".format(DESCRIPTION=descriptor.description,
|
|
|
|
CLASS=descriptor.__class__))
|
|
|
|
|
|
|
|
b = bytes(descriptor)
|
|
|
|
notes = descriptor.notes()
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
# This prints each subdescriptor on a separate line.
|
|
|
|
variable_name = StringIndex.index_to_variable[idx]
|
|
|
|
if not variable_name:
|
|
|
|
variable_name = "string_descriptor{}".format(idx)
|
|
|
|
|
|
|
|
const = "const "
|
|
|
|
if variable_name == "usb_serial_number":
|
|
|
|
const = ""
|
|
|
|
c_file.write("""\
|
|
|
|
{const}uint16_t {NAME}[] = {{
|
|
|
|
""".format(const=const, NAME=variable_name))
|
|
|
|
pointers_to_strings.append("{name}".format(name=variable_name))
|
|
|
|
n = 0
|
|
|
|
while i < len(b):
|
|
|
|
length = b[i]
|
|
|
|
for j in range(length // 2):
|
|
|
|
c_file.write("0x{:04x}, ".format(b[i + 2*j + 1] << 8 | b[i + 2*j]))
|
|
|
|
n += 1
|
|
|
|
c_file.write("\n")
|
|
|
|
i += length
|
|
|
|
c_file.write("""\
|
|
|
|
};
|
|
|
|
""")
|
2018-03-24 18:29:12 -04:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("""\
|
2018-10-19 21:46:22 -04:00
|
|
|
// array of pointer to string descriptors
|
|
|
|
uint16_t const * const string_desc_arr [] =
|
|
|
|
{
|
|
|
|
""")
|
|
|
|
c_file.write(""",\
|
|
|
|
|
|
|
|
""".join(pointers_to_strings))
|
|
|
|
|
|
|
|
c_file.write("""
|
2018-03-24 18:29:12 -04:00
|
|
|
};
|
2018-03-30 23:20:24 -04:00
|
|
|
""")
|
|
|
|
|
2020-08-19 08:18:17 -04:00
|
|
|
c_file.write("\n")
|
2018-10-19 21:46:22 -04:00
|
|
|
|
|
|
|
hid_descriptor_length = len(bytes(combined_hid_report_descriptor))
|
|
|
|
|
2021-01-20 21:24:42 -05:00
|
|
|
# Now the values we need for the .h file.
|
2018-03-30 23:20:24 -04:00
|
|
|
h_file.write("""\
|
|
|
|
#ifndef MICROPY_INCLUDED_AUTOGEN_USB_DESCRIPTOR_H
|
|
|
|
#define MICROPY_INCLUDED_AUTOGEN_USB_DESCRIPTOR_H
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2020-07-21 19:26:46 -04:00
|
|
|
extern const uint8_t usb_desc_dev[{device_length}];
|
|
|
|
extern const uint8_t usb_desc_cfg[{configuration_length}];
|
|
|
|
extern uint16_t usb_serial_number[{serial_number_length}];
|
|
|
|
extern uint16_t const * const string_desc_arr [{string_descriptor_length}];
|
2018-10-19 21:46:22 -04:00
|
|
|
|
2020-07-21 19:26:46 -04:00
|
|
|
extern const uint8_t hid_report_descriptor[{hid_report_descriptor_length}];
|
2019-09-03 21:16:14 -04:00
|
|
|
|
2020-07-29 05:05:31 -04:00
|
|
|
#define CFG_TUSB_RHPORT0_MODE ({rhport0_mode})
|
|
|
|
|
|
|
|
#define USB_HID_NUM_DEVICES {hid_num_devices}
|
2018-10-19 21:46:22 -04:00
|
|
|
|
|
|
|
// Vendor name included in Inquiry response, max 8 bytes
|
|
|
|
#define CFG_TUD_MSC_VENDOR "{msc_vendor}"
|
2018-03-30 23:20:24 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
// Product name included in Inquiry response, max 16 bytes
|
|
|
|
#define CFG_TUD_MSC_PRODUCT "{msc_product}"
|
2018-03-30 23:20:24 -04:00
|
|
|
|
|
|
|
"""
|
2018-10-19 21:46:22 -04:00
|
|
|
.format(serial_number_length=len(bytes(serial_number_descriptor)) // 2,
|
|
|
|
device_length=len(bytes(device)),
|
|
|
|
configuration_length=descriptor_length,
|
|
|
|
max_configuration_length=max(hid_descriptor_length, descriptor_length),
|
|
|
|
string_descriptor_length=len(pointers_to_strings),
|
2019-09-03 21:16:14 -04:00
|
|
|
hid_report_descriptor_length=len(bytes(combined_hid_report_descriptor)),
|
2020-07-29 05:05:31 -04:00
|
|
|
rhport0_mode='OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED' if args.highspeed else 'OPT_MODE_DEVICE',
|
2019-09-03 21:16:14 -04:00
|
|
|
hid_num_devices=len(args.hid_devices),
|
2018-10-19 21:46:22 -04:00
|
|
|
msc_vendor=args.manufacturer[:8],
|
|
|
|
msc_product=args.product[:16]))
|
2018-04-02 19:08:18 -04:00
|
|
|
|
2021-01-26 21:43:19 -05:00
|
|
|
if 'VENDOR' in args.devices:
|
|
|
|
h_file.write("""\
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
VENDOR_REQUEST_WEBUSB = 1,
|
|
|
|
VENDOR_REQUEST_MICROSOFT = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
extern uint8_t const desc_ms_os_20[];
|
|
|
|
|
|
|
|
// Currently getting compile-time errors in files like tusb_fifo.c
|
|
|
|
// if we try do define this here (TODO figure this out!)
|
|
|
|
//extern const tusb_desc_webusb_url_t desc_webusb_url;
|
|
|
|
|
|
|
|
""")
|
2021-01-22 09:16:10 -05:00
|
|
|
|
|
|
|
h_file.write("""\
|
|
|
|
#endif // MICROPY_INCLUDED_AUTOGEN_USB_DESCRIPTOR_H
|
|
|
|
""")
|
2018-03-30 23:20:24 -04:00
|
|
|
# Write out the report descriptor and info
|
2021-01-22 09:16:10 -05:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("""\
|
2018-10-19 21:46:22 -04:00
|
|
|
const uint8_t hid_report_descriptor[{HID_DESCRIPTOR_LENGTH}] = {{
|
|
|
|
""".format(HID_DESCRIPTOR_LENGTH=hid_descriptor_length))
|
2018-03-30 23:20:24 -04:00
|
|
|
|
2018-04-16 20:18:51 -04:00
|
|
|
for b in bytes(combined_hid_report_descriptor):
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("0x{:02x}, ".format(b))
|
2019-09-03 21:16:14 -04:00
|
|
|
c_file.write("""\
|
|
|
|
};
|
|
|
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
# Write out USB HID report buffer definitions.
|
2019-09-09 15:12:06 -04:00
|
|
|
for name in args.hid_devices:
|
2019-09-03 21:16:14 -04:00
|
|
|
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))
|
|
|
|
|
2020-08-19 08:18:17 -04:00
|
|
|
if hid_report_descriptors.HID_DEVICE_DATA[name].out_report_length > 0:
|
|
|
|
c_file.write("""\
|
|
|
|
static uint8_t {name}_out_report_buffer[{report_length}];
|
|
|
|
""".format(name=name.lower(), report_length=hid_report_descriptors.HID_DEVICE_DATA[name].out_report_length))
|
|
|
|
|
2019-09-03 21:16:14 -04:00
|
|
|
# Write out table of device objects.
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("""
|
2019-09-03 21:16:14 -04:00
|
|
|
usb_hid_device_obj_t usb_hid_devices[] = {
|
2020-08-19 08:18:17 -04:00
|
|
|
""")
|
2019-09-09 15:12:06 -04:00
|
|
|
for name in args.hid_devices:
|
2019-09-03 21:16:14 -04:00
|
|
|
device_data = hid_report_descriptors.HID_DEVICE_DATA[name]
|
2020-08-19 08:18:17 -04:00
|
|
|
out_report_buffer = '{}_out_report_buffer'.format(name.lower()) if device_data.out_report_length > 0 else 'NULL'
|
2019-09-03 21:16:14 -04:00
|
|
|
c_file.write("""\
|
|
|
|
{{
|
|
|
|
.base = {{ .type = &usb_hid_device_type }},
|
|
|
|
.report_buffer = {name}_report_buffer,
|
2019-09-09 15:12:06 -04:00
|
|
|
.report_id = {report_id},
|
2019-09-03 21:16:14 -04:00
|
|
|
.report_length = {report_length},
|
|
|
|
.usage_page = {usage_page:#04x},
|
|
|
|
.usage = {usage:#04x},
|
2020-08-19 08:18:17 -04:00
|
|
|
.out_report_buffer = {out_report_buffer},
|
|
|
|
.out_report_length = {out_report_length},
|
2019-09-03 21:16:14 -04:00
|
|
|
}},
|
2019-09-09 15:12:06 -04:00
|
|
|
""".format(name=name.lower(), report_id=report_ids[name],
|
2019-09-03 21:16:14 -04:00
|
|
|
report_length=device_data.report_length,
|
|
|
|
usage_page=device_data.usage_page,
|
2020-08-19 08:18:17 -04:00
|
|
|
usage=device_data.usage,
|
|
|
|
out_report_buffer=out_report_buffer,
|
|
|
|
out_report_length=device_data.out_report_length))
|
2019-09-03 21:16:14 -04:00
|
|
|
c_file.write("""\
|
|
|
|
};
|
|
|
|
""")
|
|
|
|
|
|
|
|
# Write out tuple of device objects.
|
|
|
|
c_file.write("""
|
|
|
|
mp_obj_tuple_t common_hal_usb_hid_devices = {{
|
|
|
|
.base = {{
|
|
|
|
.type = &mp_type_tuple,
|
|
|
|
}},
|
|
|
|
.len = {num_devices},
|
|
|
|
.items = {{
|
|
|
|
""".format(num_devices=len(args.hid_devices)))
|
|
|
|
for idx in range(len(args.hid_devices)):
|
|
|
|
c_file.write("""\
|
|
|
|
(mp_obj_t) &usb_hid_devices[{idx}],
|
|
|
|
""".format(idx=idx))
|
|
|
|
c_file.write("""\
|
|
|
|
},
|
2018-03-30 23:20:24 -04:00
|
|
|
};
|
2018-03-24 18:29:12 -04:00
|
|
|
""")
|
|
|
|
|
2021-01-22 09:16:10 -05:00
|
|
|
if 'VENDOR' in args.devices:
|
2021-01-26 21:43:19 -05:00
|
|
|
# Mimic what the tinyusb webusb demo does in it's main.c file
|
2021-01-22 09:16:10 -05:00
|
|
|
c_file.write("""
|
|
|
|
#define URL "{webusb_url}"
|
|
|
|
|
|
|
|
const tusb_desc_webusb_url_t desc_webusb_url =
|
|
|
|
{{
|
|
|
|
.bLength = 3 + sizeof(URL) - 1,
|
|
|
|
.bDescriptorType = 3, // WEBUSB URL type
|
|
|
|
.bScheme = 1, // 0: http, 1: https, 255: ""
|
|
|
|
.url = URL
|
|
|
|
}};
|
2021-01-25 21:37:58 -05:00
|
|
|
|
2021-02-04 17:18:36 -05:00
|
|
|
// These next two hardcoded descriptors were pulled from the usb_descriptor.c file
|
2021-01-26 21:43:19 -05:00
|
|
|
// of the tinyusb webusb_serial demo. TODO - this is probably something else to
|
2021-02-01 19:35:58 -05:00
|
|
|
// integrate into the adafruit_usb_descriptors project...
|
2021-01-26 21:43:19 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// BOS Descriptor
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
|
|
/* Microsoft OS 2.0 registry property descriptor
|
|
|
|
Per MS requirements https://msdn.microsoft.com/en-us/library/windows/hardware/hh450799(v=vs.85).aspx
|
|
|
|
device should create DeviceInterfaceGUIDs. It can be done by driver and
|
|
|
|
in case of real PnP solution device should expose MS "Microsoft OS 2.0
|
|
|
|
registry property descriptor". Such descriptor can insert any record
|
|
|
|
into Windows registry per device/configuration/interface. In our case it
|
|
|
|
will insert "DeviceInterfaceGUIDs" multistring property.
|
|
|
|
|
|
|
|
GUID is freshly generated and should be OK to use.
|
|
|
|
|
|
|
|
https://developers.google.com/web/fundamentals/native-hardware/build-for-webusb/
|
|
|
|
(Section Microsoft OS compatibility descriptors)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN + TUD_BOS_MICROSOFT_OS_DESC_LEN)
|
2021-01-25 21:37:58 -05:00
|
|
|
|
|
|
|
#define MS_OS_20_DESC_LEN 0xB2
|
|
|
|
|
2021-01-26 21:43:19 -05:00
|
|
|
// BOS Descriptor is required for webUSB
|
|
|
|
uint8_t const desc_bos[] =
|
|
|
|
{{
|
|
|
|
// total length, number of device caps
|
|
|
|
TUD_BOS_DESCRIPTOR(BOS_TOTAL_LEN, 2),
|
|
|
|
|
|
|
|
// Vendor Code, iLandingPage
|
|
|
|
TUD_BOS_WEBUSB_DESCRIPTOR(VENDOR_REQUEST_WEBUSB, 1),
|
|
|
|
|
|
|
|
// Microsoft OS 2.0 descriptor
|
|
|
|
TUD_BOS_MS_OS_20_DESCRIPTOR(MS_OS_20_DESC_LEN, VENDOR_REQUEST_MICROSOFT)
|
|
|
|
}};
|
|
|
|
|
|
|
|
uint8_t const * tud_descriptor_bos_cb(void)
|
|
|
|
{{
|
|
|
|
return desc_bos;
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
2021-02-01 19:51:11 -05:00
|
|
|
#define ITF_NUM_VENDOR {webusb_interface} // used in this next descriptor
|
2021-02-01 19:35:58 -05:00
|
|
|
|
2021-01-25 21:37:58 -05:00
|
|
|
uint8_t const desc_ms_os_20[] =
|
|
|
|
{{
|
|
|
|
// Set header: length, type, windows version, total length
|
|
|
|
U16_TO_U8S_LE(0x000A), U16_TO_U8S_LE(MS_OS_20_SET_HEADER_DESCRIPTOR), U32_TO_U8S_LE(0x06030000), U16_TO_U8S_LE(MS_OS_20_DESC_LEN),
|
|
|
|
|
|
|
|
// Configuration subset header: length, type, configuration index, reserved, configuration total length
|
|
|
|
U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_CONFIGURATION), 0, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A),
|
|
|
|
|
|
|
|
// Function Subset header: length, type, first interface, reserved, subset length
|
|
|
|
U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_FUNCTION), ITF_NUM_VENDOR, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08),
|
|
|
|
|
|
|
|
// MS OS 2.0 Compatible ID descriptor: length, type, compatible ID, sub compatible ID
|
|
|
|
U16_TO_U8S_LE(0x0014), U16_TO_U8S_LE(MS_OS_20_FEATURE_COMPATBLE_ID), 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // sub-compatible
|
|
|
|
|
|
|
|
// MS OS 2.0 Registry property descriptor: length, type
|
|
|
|
U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08-0x08-0x14), U16_TO_U8S_LE(MS_OS_20_FEATURE_REG_PROPERTY),
|
|
|
|
U16_TO_U8S_LE(0x0007), U16_TO_U8S_LE(0x002A), // wPropertyDataType, wPropertyNameLength and PropertyName "DeviceInterfaceGUIDs\0" in UTF-16
|
|
|
|
'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, 't', 0x00, 'e', 0x00,
|
|
|
|
'r', 0x00, 'f', 0x00, 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, 'U', 0x00, 'I', 0x00, 'D', 0x00, 's', 0x00, 0x00, 0x00,
|
|
|
|
U16_TO_U8S_LE(0x0050), // wPropertyDataLength
|
|
|
|
//bPropertyData: “{{975F44D9-0D08-43FD-8B3E-127CA8AFFF9D}}”.
|
|
|
|
'{{', 0x00, '9', 0x00, '7', 0x00, '5', 0x00, 'F', 0x00, '4', 0x00, '4', 0x00, 'D', 0x00, '9', 0x00, '-', 0x00,
|
|
|
|
'0', 0x00, 'D', 0x00, '0', 0x00, '8', 0x00, '-', 0x00, '4', 0x00, '3', 0x00, 'F', 0x00, 'D', 0x00, '-', 0x00,
|
|
|
|
'8', 0x00, 'B', 0x00, '3', 0x00, 'E', 0x00, '-', 0x00, '1', 0x00, '2', 0x00, '7', 0x00, 'C', 0x00, 'A', 0x00,
|
|
|
|
'8', 0x00, 'A', 0x00, 'F', 0x00, 'F', 0x00, 'F', 0x00, '9', 0x00, 'D', 0x00, '}}', 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
}};
|
|
|
|
|
|
|
|
TU_VERIFY_STATIC(sizeof(desc_ms_os_20) == MS_OS_20_DESC_LEN, "Incorrect size");
|
|
|
|
|
|
|
|
// End of section about desc_ms_os_20
|
|
|
|
|
2021-02-01 19:35:58 -05:00
|
|
|
""".format(webusb_url=args.webusb_url, webusb_interface=vendor_interface.bInterfaceNumber))
|