wip: working on descriptor templates

This commit is contained in:
Dan Halbert 2021-04-20 22:20:34 -04:00
parent 51ccf8dc30
commit 6b18a51d57
7 changed files with 366 additions and 149 deletions

View File

@ -41,54 +41,53 @@
#include "supervisor/usb.h"
#include "tusb.h"
static uint8_t[] storage_usb_msc_descriptor[] = {
static const uint8_t storage_usb_msc_descriptor_template[] = {
// MSC Interface Descriptor
0x09, // 0 bLength
0x04, // 1 bDescriptorType (Interface)
0xFF, // 2 bInterfaceNumber [SET AT RUNTIME]
#define MSC_INTERFACE_INDEX 2
0x00, // 3 bAlternateSetting
0x02, // 4 bNumEndpoints 2
0x08, // 5 bInterfaceClass: MSC
0x06, // 6 bInterfaceSubClass: TRANSPARENT
0x50, // 7 bInterfaceProtocol: BULK
0x00, // 8 iInterface (String Index) [SET AT RUNTIME]
0xFF, // 8 iInterface (String Index) [SET AT RUNTIME]
#define MSC_INTERFACE_STRING_INDEX 8
// MSC Endpoint IN Descriptor
0x07, // 9 bLength
0x05, // 10 bDescriptorType (Endpoint)
0xFF, // 11 bEndpointAddress (IN/D2H) [SET AT RUNTIME: 0x8 | number]
#define MSC_IN_ENDPOINT_INDEX 11
0x02, // 12 bmAttributes (Bulk)
0x40, 0x00, // 13,14 wMaxPacketSize 64
0x00, // 15 bInterval 0 (unit depends on device speed)
// MSC Endpoint OUT Descriptor
0x07, // 16 bLength
0x05, // 17 bDescriptorType (Endpoint)
0xFF, // 18 bEndpointAddress (OUT/H2D) [SET AT RUNTIME]
#define MSC_OUT_ENDPOINT_INDEX 18
0x02, // 19 bmAttributes (Bulk)
0x40, 0x00, // 20 wMaxPacketSize 64
0x00, // 21 bInterval 0 (unit depends on device speed)
0x40, 0x00, // 20,21 wMaxPacketSize 64
0x00, // 22 bInterval 0 (unit depends on device speed)
};
// Indices into usb_msc_descriptor for values that must be set at runtime.
#define MSC_INTERFACE_NUMBER_INDEX 2
#define MSC_INTERFACE_STRING_INDEX 8
#define MSC_IN_ENDPOINT_INDEX 11
#define MSC_OUT_ENDPOINT_INDEX 118
// Is the MSC device enabled?
bool storage_usb_enabled;
size_t storage_usb_desc_length(void) {
size_t storage_usb_descriptor_length(void) {
return sizeof(usb_msc_descriptor);
}
size_t storage_usb_add_desc(uint8_t *desc_buf, uint8_t interface_number, uint8_t in_endpoint_address,
uint8_t out_endpoint_address, uint8_t interface_string_index) {
memcpy(descriptor_buf, storage_usb_msc_descriptor, sizeof(storage_usb_msc_descriptor));
desc_buf[MSC_INTERFACE_NUMBER_INDEX] = interface_number;
desc_buf[MSC_INTERFACE_STRING_INDEX] = interface_string_index;
desc_buf[MSC_IN_ENDPOINT_INDEX] = in_endpoint_address;
desc_buf[MSC_OUT_ENDPOINT_INDEX] = 0x80 | out_endpoint_address;
return sizeof(usb_msc_descriptor);
size_t storage_usb_add_descriptor(uint8_t *descriptor_buf, uint8_t interface_number, uint8_t in_endpoint, uint8_t out_endpoint, uint8_t interface_string) {
memcpy(descriptor_buf, storage_usb_msc_descriptor_template, sizeof(storage_usb_msc_descriptor_template));
descriptor_buf[MSC_INTERFACE_INDEX] = interface_number;
descriptor_buf[MSC_INTERFACE_STRING_INDEX] = interface_string;
descriptor_buf[MSC_IN_ENDPOINT_INDEX] = in_endpoint_address;
descriptor_buf[MSC_OUT_ENDPOINT_INDEX] = 0x80 | out_endpoint_address;
return sizeof(storage_usb_msc_descriptor_template);
}

View File

@ -30,9 +30,7 @@
extern bool storage_usb_enabled;
void storage_init(void);
size_t storage_usb_desc_length(void);
size_t storage_usb_add_desc(uint8_t *desc_buf, uint8_t interface_number,
uint8_t in_endpoint_address, uint8_t out_endpoint_address,
uint8_t interface_string_index);
size_t storage_usb_descriptor_length(void);
size_t storage_usb_add_descriptor(uint8_t *descriptor_buf, uint8_t interface, uint8_t in_endpoint, uint8_t out_endpoint, uint8_tt interface_string);
#endif // SHARED_MODULE_STORAGE___INIT___H

View File

@ -41,6 +41,124 @@
bool usb_cdc_repl_enabled;
bool usb_cdc_data_enabled;
static const uint8_t usb_cdc_descriptor_template[] = {
// CDC IAD Descriptor
0x08, // 0 bLength
0x0B, // 1 bDescriptorType: IAD Descriptor
0xFF, // 2 bFirstInterface [SET AT RUNTIME]
#define CDC_FIRST_INTERFACE_INDEX 2
0x02, // 3 bInterfaceCount: 2
0x02, // 4 bFunctionClass: COMM
0x02, // 5 bFunctionSubclass: ACM
0x00, // 6 bFunctionProtocol: NONE
0x00, // 7 iFunction
// CDC Comm Interface Descriptor
0x09, // 8 bLength
0x04, // 9 bDescriptorType (Interface)
0xFF, // 10 bInterfaceNumber [SET AT RUNTIME]
#define CDC_COMM_INTERFACE_INDEX 10
0x00, // 11 bAlternateSetting
0x01, // 12 bNumEndpoints 1
0x02, // 13 bInterfaceClass: COMM
0x02, // 14 bInterfaceSubClass: ACM
0x00, // 15 bInterfaceProtocol: NONE
0xFF, // 16 iInterface (String Index)
#define CDC_COMM_INTERFACE_STRING_INDEX 16
// CDC Header Descriptor
0x05, // 17 bLength
0x24, // 18 bDescriptorType: CLASS SPECIFIC INTERFACE
0x00, // 19 bDescriptorSubtype: NONE
0x10, 0x01, // 20,21 bcdCDC: 1.10
// CDC Call Management Descriptor
0x05, // 22 bLength
0x24, // 23 bDescriptorType: CLASS SPECIFIC INTERFACE
0x01, // 24 bDescriptorSubtype: CALL MANAGEMENT
0x01, // 25 bmCapabilities
0xFF, // 26 bDataInterface [SET AT RUNTIME]
#define CDC_CALL_MANAGEMENT_DATA_INTERFACE_INDEX 26
// CDC Abstract Control Management Descriptor
0x04, // 27 bLength
0x24, // 28 bDescriptorType: CLASS SPECIFIC INTERFACE
0x02, // 29 bDescriptorSubtype: ABSTRACT CONTROL MANAGEMENT
0x02, // 30 bmCapabilities
// CDC Union Descriptor
0x05, // 31 bLength
0x24, // 32 bDescriptorType: CLASS SPECIFIC INTERFACE
0x06, // 33 bDescriptorSubtype: CDC
0xFF, // 34 bMasterInterface [SET AT RUNTIME]
#define CDC_UNION_MASTER_INTERFACE_INDEX 34
0xFF, // 35 bSlaveInterface_list (1 item)
#define CDC_UNION_SLAVE_INTERFACE_INDEX 35
// CDC Control IN Endpoint Descriptor
0x07, // 36 bLength
0x05, // 37 bDescriptorType (Endpoint)
0xFF, // 38 bEndpointAddress (IN/D2H) [SET AT RUNTIME: 0x8 | number]
#define CDC_CONTROL_IN_ENDPOINT_INDEX 38
0x03, // 39 bmAttributes (Interrupt)
0x40, 0x00, // 40, 41 wMaxPacketSize 64
0x10, // 42 bInterval 16 (unit depends on device speed)
// CDC Data Interface
0x09, // 43 bLength
0x04, // 44 bDescriptorType (Interface)
0xFF, // 45 bInterfaceNumber [SET AT RUNTIME]
#define CDC_DATA_INTERFACE_INDEX 45
0x00, // 46 bAlternateSetting
0x02, // 47 bNumEndpoints 2
0x0A, // 48 bInterfaceClass: DATA
0x00, // 49 bInterfaceSubClass: NONE
0x00, // 50 bInterfaceProtocol
0x05, // 51 iInterface (String Index)
#define CDC_DATA_INTERFACE_STRING_INDEX 51
// CDC Data OUT Endpoint Descriptor
0x07, // 52 bLength
0x05, // 53 bDescriptorType (Endpoint)
0xFF, // 54 bEndpointAddress (OUT/H2D) [SET AT RUNTIME]
#define CDC_DATA_OUT_ENDPOINT_INDEX 54
0x02, // 55 bmAttributes (Bulk)
0x40, 0x00, // 56,57 wMaxPacketSize 64
0x00, // 58 bInterval 0 (unit depends on device speed)
// CDC Data IN Endpoint Descriptor
0x07, // 59 bLength
0x05, // 60 bDescriptorType (Endpoint)
0xFF, // 61 bEndpointAddress (IN/D2H) [SET AT RUNTIME: 0x8 | number]
#define CDC_DATA_IN_ENDPOINT_INDEX 61
0x02, // 62 bmAttributes (Bulk)
0x40, 0x00, // 63,64 wMaxPacketSize 64
0x00, // 65 bInterval 0 (unit depends on device speed)
};
size_t usb_cdc_descriptor_length(void) {
return sizeof(usb_cdc_descriptor_template);
}
size_t usb_cdc_add_descriptor(uint8_t *descriptor_buf, uint8 comm_interface, uint8_t data_interface, uint8_t control_in_endpoint, uint8_t data_in_endpoint, uint8_t data_out_endpoint, uint8_t comm_interface_string, uint8_t data_interface_string) {
memcpy(descriptor_buf, usb_midi_descriptor_template, sizeof(usb_midi_descriptor_template));
descriptor_buf[CDC_FIRST_INTERFACE_INDEX] = comm_interface;
descriptor_buf[CDC_COMM_INTERFACE_INDEX] = comm_interface;
descriptor_buf[CDC_CALL_MANAGEMENT_DATA_INTERFACE_INDEX] = data_interface;
descriptor_buf[CDC_UNION_MASTER_INTERFACE_INDEX] = comm_interface;
descriptor_buf[CDC_UNION_SLAVE_INTERFACE_INDEX] = data_interface;
descriptor_buf[CDC_DATA_INTERFACE_INDEX] = data_interface;
descriptor_buf[CDC_CONTROL_IN_ENDPOINT_INDEX] = control_in_endpoint;
descriptor_buf[CDC_DATA_OUT_ENDPOINT_INDEX] = data_out_endpoint;
descriptor_buf[CDC_DATA_IN_ENDPOINT_INDEX] = data_in_endpoint;
descriptor_buf[CDC_COMM_INTERFACE_STRING_INDEX] = comm_interface_string;
descriptor_buf[CDC_DATA_INTERFACE_STRING_INDEX] = data_interface_string;
return sizeof(usb_midi_descriptor_template);
}
static usb_cdc_serial_obj_t usb_cdc_repl_obj = {
.base.type = &usb_cdc_serial_type,
.timeout = -1.0f,

View File

@ -33,5 +33,7 @@ extern bool usb_cdc_repl_enabled;
extern bool usb_cdc_data_enabled;
void usb_cdc_init(void);
size_t usb_cdc_descriptor_length(void);
size_t usb_cdc_add_descriptor(uint8_t *descriptor_buf, uint8 comm_interface, uint8_t data_interface, uint8_t control_in_endpoint, uint8_t data_in_endpoint, uint8_t data_out_endpoint, uint8_t comm_interface_string, uint8_t data_interface_string);
#endif /* SHARED_MODULE_USB_CDC___INIT___H */

View File

@ -38,111 +38,148 @@
supervisor_allocation *usb_midi_allocation;
static uint8_t[] usb_midi_descriptor[] = {
0x09, // 0 bLength
0x04, // 1 bDescriptorType (Interface)
0xFF, // 2 bInterfaceNumber [SET AT RUNTIME]
0x00, // 3 bAlternateSetting
0x00, // 4 bNumEndpoints 0
0x01, // 5 bInterfaceClass (Audio)
0x01, // 6 bInterfaceSubClass (Audio Control)
0x00, // 7 bInterfaceProtocol
0x00, // 8 iInterface (String Index) [SET AT RUNTIME]
static const uint8_t usb_midi_descriptor_template[] = {
// Audio Interface Descriptor
0x09, // 0 bLength
0x04, // 1 bDescriptorType (Interface)
0xFF, // 2 bInterfaceNumber [SET AT RUNTIME]
#define MIDI_AUDIO_CONTROL_INTERFACE_NUMBER_INDEX 2
0x00, // 3 bAlternateSetting
0x00, // 4 bNumEndpoints 0
0x01, // 5 bInterfaceClass (Audio)
0x01, // 6 bInterfaceSubClass (Audio Control)
0x00, // 7 bInterfaceProtocol
0xFF, // 8 iInterface (String Index) [SET AT RUNTIME]
#define MIDI_AUDIO_CONTROL_INTERFACE_STRING_INDEX 8
0x09, // 9 bLength
0x24, // 10 bDescriptorType (See Next Line)
0x01, // 11 bDescriptorSubtype (CS_INTERFACE -> HEADER)
0x00, 0x01, // 12,13 bcdADC 1.00
0x09, 0x00, // 14,15 wTotalLength 9
0x01, // 16 binCollection 0x01
0xFF, // 17 baInterfaceNr [SET AT RUNTIME: one-element list: same as 20]
// Audio10 Control Interface Descriptor
0x09, // 9 bLength
0x24, // 10 bDescriptorType (See Next Line)
0x01, // 11 bDescriptorSubtype (CS_INTERFACE -> HEADER)
0x00, 0x01, // 12,13 bcdADC 1.00
0x09, 0x00, // 14,15 wTotalLength 9
0x01, // 16 binCollection 0x01
0xFF, // 17 baInterfaceNr [SET AT RUNTIME: one-element list: same as 20]
#define MIDI_STREAMING_INTERFACE_NUMBER_INDEX_2 17
0x09, // 18 bLength
0x04, // 19 bDescriptorType (Interface)
0xFF, // 20 bInterfaceNumber [SET AT RUNTIME]
0x00, // 21 bAlternateSetting
0x02, // 22 bNumEndpoints 2
0x01, // 23 bInterfaceClass (Audio)
0x03, // 24 bInterfaceSubClass (MIDI Streaming)
0x00, // 25 bInterfaceProtocol
0x0A, // 26 iInterface (String Index) [SET AT RUNTIME]
// MIDI Streaming Interface Descriptor
0x09, // 18 bLength
0x04, // 19 bDescriptorType (Interface)
0xFF, // 20 bInterfaceNumber [SET AT RUNTIME]
#define MIDI_STREAMING_INTERFACE_NUMBER_INDEX 20
0x00, // 21 bAlternateSetting
0x02, // 22 bNumEndpoints 2
0x01, // 23 bInterfaceClass (Audio)
0x03, // 24 bInterfaceSubClass (MIDI Streaming)
0x00, // 25 bInterfaceProtocol
0xFF, // 26 iInterface (String Index) [SET AT RUNTIME]
#define MIDI_STREAMING_INTERFACE_STRING_INDEX 26
0x07, // 27 bLength
0x24, // 28 bDescriptorType (See Next Line)
0x01, 0x00, 0x01, 0x25, 0x00, // 29,30,31,32,33
0x06, // 34 bLength
0x24, // 35 bDescriptorType (See Next Line)
0x02, 0x01, 0x01, 0x08, // 36,37,38,39
// MIDI Header Descriptor
0x07, // 27 bLength
0x24, // 28 bDescriptorType: CLASS SPECIFIC INTERFACE
0x01, // 29 bDescriptorSubtype: MIDI STREAMING HEADER
0x00, 0x01, // 30,31 bsdMSC (MIDI STREAMING) version 1.0
0x25, 0x00 // 32,33 wLength
// MIDI Embedded In Jack Descriptor
0x06, // 34 bLength
0x24, // 35 bDescriptorType: CLASS SPECIFIC INTERFACE
0x02, // 36 bDescriptorSubtype: MIDI IN JACK
0x01, // 37 bJackType: EMBEDDED
0x01, // 38 id (always 1)
0xFF, // 39 iJack (String Index) [SET AT RUNTIME]
#define MIDI_IN_JACK_STRING_INDEX 39
// MIDI External In Jack Descriptor
0x06, // 40 bLength
0x24, // 41 bDescriptorType (See Next Line)
0x02, 0x02, 0x02, 0x00, // 42,43,44,45
0x24, // 41 bDescriptorType: CLASS SPECIFIC INTERFACE
0x02, // 42 bDescriptorSubtype: MIDI IN JACK
0x02, // 43 bJackType: EXTERNAL
0x02, // 44 bJackId (always 2)
0x00, // 45 iJack (String Index)
// MIDI Embedded Out Jack Descriptor
0x09, // 46 bLength
0x24, // 47 bDescriptorType (See Next Line)
0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x09, // 48,49,50,51,52,53,54
0x09, // 56 bLength
0x24, // 57 bDescriptorType (See Next Line)
0x03, 0x02, 0x04, 0x01, 0x01, 0x01, 0x00, // 58,59,60,61,62,63,64
0x07, // 65 bLength
0x05, // 66 bDescriptorType (See Next Line)
0xFF, // 67 bEndpointAddress (OUT/H2D) [SET AT RUNTIME]
0x02, // 68 bmAttributes (Bulk)
0x40, 0x00, // 69,70 wMaxPacketSize 64
0x00, // 71 bInterval 0 (unit depends on device speed)
0x24, // 47 bDescriptorType: CLASS SPECIFIC INTERFACE
0x03, // 48 bDescriptorSubtype: MIDI OUT JACK
0x01, // 49 bJackType: EMBEDDED
0x03, // 50 bJackID (always 3)
0x01, // 51 bNrInputPins (always 1)
0x02, // 52 BaSourceID(1) (always 2)
0x01, // 53 BaSourcePin(1) (always 1)
0xFF, // 54 iJack (String Index) [SET AT RUNTIME]
#define MIDI_OUT_JACK_STRING_INDEX 54
0x05, // 72 bLength
0x25, // 73 bDescriptorType (See Next Line)
0x01, 0x01, 0x01, // 74,75,76
0x07, // 77 bLength
0x05, // 78 bDescriptorType (See Next Line)
0xFF, // 79 bEndpointAddress (IN/D2H) [SET AT RUNTIME: 0x8 | number]
0x02, // 80 bmAttributes (Bulk)
0x40, 0x00, // 81,82 wMaxPacketSize 64
0x00, // 83 bInterval 0 (unit depends on device speed)
// MIDI External Out Jack Descriptor
0x09, // 55 bLength
0x24, // 56 bDescriptorType: CLASS SPECIFIC INTERFACE
0x03, // 57 bDescriptorSubtype: MIDI OUT JACK
0x02, // 58 bJackType: EXTERNAL
0x04, // 59 bJackID (always 4)
0x01, // 60 bNrInputPins (always 1)
0x01, // 61 BaSourceID(1) (always 1)
0x01, // 62 BaSourcePin(1) (always 1)
0x00, // 63 iJack (String Index)
0x05, // 84 bLength
0x25, // 85 bDescriptorType (See Next Line)
0x01, 0x01, 0x03, // 86,87,88
// MIDI Streaming Endpoint OUT Descriptor
0x07, // 64 bLength
0x05, // 65 bDescriptorType (EndPoint)
0xFF, // 66 bEndpointAddress (OUT/H2D) [SET AT RUNTIME]
#define MIDI_STREAMING_OUT_ENDPOINT_INDEX 66
0x02, // 67 bmAttributes (Bulk)
0x40, 0x00, // 68,69 wMaxPacketSize 64
0x00, // 70 bInterval 0 (unit depends on device speed)
// MIDI Data Endpoint Descriptor
0x05, // 71 bLength
0x25, // 72 bDescriptorType: CLASS SPECIFIC ENDPOINT
0x01, // 73 bDescriptorSubtype: MIDI STREAMING 1.0
0x01, // 74 bNumGrpTrmBlock (always 1)
0x01, // 75 baAssoGrpTrmBlkID(1) (always 1)
// MIDI IN Data Endpoint
0x07, // 76 bLength
0x05, // 77 bDescriptorType: Endpoint
0xFF, // 78 bEndpointAddress (IN/D2H) [SET AT RUNTIME: 0x8 | number]
#define MIDI_STREAMING_IN_ENDPOINT_INDEX 78
0x02, // 79 bmAttributes (Bulk)
0x40, 0x00, // 8081 wMaxPacketSize 64
0x00, // 82 bInterval 0 (unit depends on device speed)
// MIDI Data Endpoint Descriptor
0x05, // 83 bLength
0x25, // 84 bDescriptorType: CLASS SPECIFIC ENDPOINT
0x01, // 85 bDescriptorSubtype: MIDI STREAMING 1.0
0x01, // 86 bNumGrpTrmBlock (always 1)
0x03, // 87 baAssoGrpTrmBlkID(1) (always 3)
};
// Is the USB MIDI device enabled?
bool usb_midi_enabled;
// Indices into usb_midi_descriptor for values that must be set at runtime.
#define MIDI_AUDIO_CONTROL_INTERFACE_NUMBER_INDEX 2
#define MIDI_AUDIO_CONTROL_INTERFACE_STRING_INDEX 8
// These two get the same value.
#define MIDI_MIDI_STREAMING_INTERFACE_NUMBER_INDEX 20
#define MIDI_MIDI_STREAMING_INTERFACE_NUMBER_XREF_INDEX 17
#define MIDI_MIDI_STREAMING_INTERFACE_STRING_INDEX 26
#define MIDI_MIDI_STREAMING_OUT_ENDPOINT_INDEX 67
#define MIDI_MIDI_STREAMING_IN_ENDPOINT_INDEX 79
size_t usb_midi_desc_length(void) {
return sizeof(usb_midi_descriptor);
size_t usb_midi_descriptor_length(void) {
return sizeof(usb_midi_descriptor_template);
}
size_t usb_midi_add_desc(uint8_t *desc_buf,
uint8_t audio_control_interface_number,
uint8_t midi_streaming_interface_number,
uint8_t midi_streaming_in_endpoint_address,
uint8_t midi_streaming_out_endpoint_address,
uint8_t audio_control_interface_string,
uint8_t midi_streaming_interface_string) {
memcpy(descriptor_buf, usb_midi_descriptor, sizeof(usb_midi_descriptor));
desc_buf[MIDI_AUDIO_CONTROL_INTERFACE_NUMBER_INDEX] = audio_control_interface_number;
desc_buf[MIDI_AUDIO_CONTROL_INTERFACE_STRING_INDEX] = audio_control_interface_string;
size_t usb_midi_add_descriptor(uint8_t *descriptor_buf,
uint8_t audio_control_interface, uint8_t midi_streaming_interface, uint8_t midi_streaming_in_endpoint, uint8_t midi_streaming_out_endpoint, uint8_t audio_control_interface_string, uint8_t midi_streaming_interface_string, uint8_t in_jack_string, uint8_t out_jack_string) {
memcpy(descriptor_buf, usb_midi_descriptor_template, sizeof(usb_midi_descriptor_template));
descriptor_buf[MIDI_AUDIO_CONTROL_INTERFACE_NUMBER_INDEX] = audio_control_interface_number;
descriptor_buf[MIDI_AUDIO_CONTROL_INTERFACE_STRING_INDEX] = audio_control_interface_string;
desc_buf[MIDI_MIDI_STREAMING_INTERFACE_NUMBER_INDEX] = midi_streaming_interface_number;
desc_buf[MIDI_MIDI_STREAMING_INTERFACE_NUMBER_XREF_INDEX] = midi_streaming_interface_number;
desc_buf[MIDI_MIDI_STREAMING_INTERFACE_STRING_INDEX] = midi_streaming_interface_string;
descriptor_buf[MSC_IN_ENDPOINT_INDEX] = midi_streaming_in_endpoint;
descriptor_buf[MSC_OUT_ENDPOINT_INDEX] = 0x80 | midi_streaming_out_endpoint;
desc_buf[MSC_IN_ENDPOINT_INDEX] = midi_streaming_in_endpoint_address;
desc_buf[MSC_OUT_ENDPOINT_INDEX] = 0x80 | midi_streaming_out_endpoint_address;
return sizeof(usb_midi_descriptor);
descriptor_buf[MIDI_STREAMING_INTERFACE_NUMBER_INDEX] = midi_streaming_interface_number;
descriptor_buf[MIDI_STREAMING_INTERFACE_NUMBER_INDEX_2] = midi_streaming_interface_number;
descriptor_buf[MIDI_STREAMING_INTERFACE_STRING_INDEX] = midi_streaming_interface_string;
descriptor_buf[MIDI_IN_JACK_STRING_INDEX] = in_jack_string;
descriptor_buf[MIDI_OUT_JACK_STRING_INDEX] = out_jack_string;
return sizeof(usb_midi_descriptor_template);
}

View File

@ -31,14 +31,8 @@ extern bool usb_midi_enabled;
void usb_midi_init(void);
void usb_midi_usb_init(void);
size_t usb_midi_desc_length(void);
size_t usb_midi_add_desc(uint8_t *desc_buf,
uint8_t audio_control_interface_number,
uint8_t midi_streaming_interface_number,
uint8_t midi_streaming_in_endpoint_address,
uint8_t midi_streaming_out_endpoint_address,
uint8_t audio_control_interface_string,
uint8_t midi_streaming_interface_string);
size_t usb_midi_descriptor_length(void);
size_t usb_midi_add_descriptor(uint8_t *descriptor_buf, uint8_t audio_control_interface, uint8_t midi_streaming_interface, uint8_t midi_streaming_in_endpoint, uint8_t midi_streaming_out_endpoint, uint8_t audio_control_interface_string, uint8_t midi_streaming_interface_string, uint8_t in_jack_string, uint8_t out_jack_string);
#endif /* SHARED_MODULE_USB_MIDI___INIT___H */

View File

@ -46,62 +46,109 @@
#include "genhdr/autogen_usb_descriptor.h"
static uint8_t *config_desc;
static uint8_t *device_descriptor;
static uint8_t *config_descriptor;
// Invoked when received GET DEVICE DESCRIPTOR
// Application return pointer to descriptor
uint8_t const *tud_descriptor_device_cb(void) {
return usb_desc_dev;
static const uint8_t device_descriptor_template[] = {
0x12, // 0 bLength
0x01, // 1 bDescriptorType (Device)
0x00, 0x02, // 2,3 bcdUSB 2.00
0x00, // 4 bDeviceClass (Use class information in the Interface Descriptors)
0x00, // 5 bDeviceSubClass
0x00, // 6 bDeviceProtocol
0x40, // 7 bMaxPacketSize0 64
0x9A, 0x23, // 8,9 idVendor [SET AT RUNTIME: lo,hi]
#define DEVICE_VID_LO_INDEX 8
#define DEVICE_VID_HI_INDEX 9
0x, 0xFF, // 10,11 idProduct [SET AT RUNTIME: lo,hi]
#define DEVICE PID_LO_INDEX 10
#define DEVICE PID_HI_INDEX 11
0x00, 0x01, // 12,13 bcdDevice 2.00
0x02, // 14 iManufacturer (String Index) [SET AT RUNTIME]
#define DEVICE_MANUFACTURER_STRING_INDEX 14
0x03, // 15 iProduct (String Index) [SET AT RUNTIME]
#define DEVICE_PRODUCT_STRING_INDEX 15
0x01, // 16 iSerialNumber (String Index) [SET AT RUNTIME]
#define DEVICE_SERIAL_NUMBER_STRING_INDEX 16
0x01, // 17 bNumConfigurations 1
};
static const uint8_t configuration_descriptor_template[] = {
0x09, // 0 bLength
0x02, // 1 bDescriptorType (Configuration)
0xFF, 0xFF, // 2,3 wTotalLength [SET AT RUNTIME: lo, hi]
#define CONFIG_TOTAL_LENGTH_LO_INDEX 2
#define CONFIG_TOTAL_LENGTH_HI_INDEX 3
0xFF, // 4 bNumInterfaces [SET AT RUNTIME]
#define CONFIG_NUM_INTERFACES_INDEX 4
0x01, // 5 bConfigurationValue
0x00, // 6 iConfiguration (String Index)
0x80, // 7 bmAttributes
0x32, // 8 bMaxPower 100mA
};
void build_usb_device_descriptor(uint16_t vid, uint16_t pid, uint8_t manufacturer_string_index, uint8_t product_string_index, uint8_t serial_number_string_index) {
device_descriptor = m_malloc(sizeof(device_descriptor_template), false);
memcpy(device_descriptor, device_descriptor_template, sizeof(device_descriptor_template));
device_descriptor[DEVICE_VID_LO_INDEX] = vid & 0xFF;
device_descriptor[DEVICE_VID_HI_INDEX] = vid >> 8;
device_descriptor[DEVICE_PID_LO_INDEX] = pid & 0xFF;
device_descriptor[DEVICE_PID_HI_INDEX] = pid >> 8;
device_descriptor[DEVICE_MANUFACTURER_STRING_INDEX] = manufacturer_string_index;
device_descriptor[DEVICE_PRODUCT_STRING_INDEX] = product_string_index;
device_descriptor[DEVICE_SERIAL_NUMBER_STRING_INDEX] = serial_number_string_index;
}
// Invoked when received GET CONFIGURATION DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
(void)index; // for multiple configurations
size_t total_descriptor_length = 0;
void build_usb_configuration_descriptor(uint16_t total_length, uint8_t num_interfaces) {
size_t total_descriptor_length = sizeof(configuration_descriptor_template);
// CDC should be first, for compatibility with Adafruit Windows 7 drivers.
// In the past, the order has been CDC, MSC, MIDI, HID, so preserve
// that order.
#if CIRCUITPY_USB_CDC
if (usb_cdc_repl_enabled) {
total_descriptor_length += usb_cdc_desc_length();
total_descriptor_length += usb_cdc_descriptor_length();
}
if (usb_cdc_data_enabled) {
total_descriptor_length += usb_cdc_desc_length();
total_descriptor_length += usb_cdc_descriptor_length();
}
#endif
#if CIRCUITPY_USB_MSC
if (storage_usb_enabled) {
total_descriptor_length += storage_usb_desc_length();
total_descriptor_length += storage_usb_descriptor_length();
}
#endif
#if CIRCUITPY_USB_MIDI
if (usb_midi_enabled) {
total_descriptor_length += usb_midi_desc_length();
total_descriptor_length += usb_midi_descriptor_length();
}
#endif
#if CIRCUITPY_USB_HID
if (usb_hid_enabled) {
total_descriptor_length += usb_hid_desc_length();
total_descriptor_length += usb_hid_descriptor_length();
}
#endif
// Now we now how big the configuration descriptor will be.
config_desc = m_malloc(total_descriptor_length, false);
configuration_descriptor = m_malloc(total_descriptor_length, false);
// Copy the top-level template, and fix up its length.
memcpy(config_descriptor, configuration_descriptor_template, sizeof(configuration_descriptor_template));
configuration_descriptor[CONFIG_TOTAL_LENGTH_LO_INDEX] = total_descriptor_length & 0xFF;
configuration_descriptor[CONFIG_TOTAL_LENGTH_HI_INDEX] = (total_descriptor_length >> 8) & 0xFF;
// Number interfaces and endpoints.
// Endpoint 0 is already used for USB control
// Endpoint 0 is already used for USB control.
uint8_t current_interface = 0;
uint8_t current_endpoint = 1;
uint16_t current_interface_string = 1;
uint8_t *desc_buf_remaining = config_desc;
uint8_t *descriptor_buf_remaining = configuration_descriptor + sizeof(configuration_descriptor_template);
#if CIRCUITPY_USB_CDC
if (usb_cdc_repl_enabled) {
@ -115,8 +162,8 @@ uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
#if CIRCUITPY_USB_MSC
if (storage_usb_enabled) {
// Concatenate and fix up the MSC descriptor.
desc_buf_remaining += storage_usb_add_desc(
desc_buf_remaining, current_interface,
descriptor_buf_remaining += storage_usb_add_descriptor(
descriptor_buf_remaining, current_interface,
current_endpoint, // in
current_endpoint, // out
current_interface_string_index);
@ -129,18 +176,20 @@ uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
#if CIRCUITPY_USB_MIDI
if (usb_midi_enabled) {
// Concatenate and fix up the MIDI descriptor.
desc_buf_remaining += usb_midi_add_desc(
desc_buf_remaining,
descriptor_buf_remaining += usb_midi_add_descriptor(
descriptor_buf_remaining,
current_interface, // audio control
current_interface + 1, // MIDI streaming
current_endpoint, // in
current_endpoint, // out
current_interface_string, // audio control
current_interface_string + 1 // MIDI streaming
current_interface_string + 2 // in jack
current_interface_string + 3 // out jack
);
current_interface += 2;
current_endpoint++;
current_interface_string += 2;
current_interface += 2; // two interfaces: audio control and MIDI streaming
current_endpoint++; // MIDI streaming only (no audio data)
current_interface_string += 4; // two interface names: audio control and MIDI streaming
}
#endif
@ -151,7 +200,27 @@ uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
}
#endif
// Now we know how many interfaces have been used.
configuration_descriptor[CONFIG_NUM_INTERFACES_INDEX] = current_interface - 1;
// Did we run out of endpoints?
if (current_endpoint - 1 > USB_NUM_EP) {
mp_raise_SystemError("Not enough USB endpoints");
}
}
// Invoked when received GET DEVICE DESCRIPTOR
// Application return pointer to descriptor
uint8_t const *tud_descriptor_device_cb(void) {
return usb_descriptor_dev;
}
// Invoked when received GET CONFIGURATION DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
(void)index; // for multiple configurations
return config_desc;
}
@ -168,6 +237,6 @@ uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {
// Invoked when received GET STRING DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
uint8_t const max_index = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]);
return (index < max_index) ? string_desc_arr[index] : NULL;
uint8_t const max_index = sizeof(string_descriptor_arr) / sizeof(string_descriptor_arr[0]);
return (index < max_index) ? string_descriptor_arr[index] : NULL;
}