Skip to content

Commit

Permalink
Merge pull request adafruit#4734 from dhalbert/dynamic-usb-fixes
Browse files Browse the repository at this point in the history
fix HID; fix interface name table creation
  • Loading branch information
tannewt authored May 10, 2021
2 parents 51120a9 + becff6f commit 4eb4f14
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/pirkey_m0/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CIRCUITPY_FULL_BUILD = 0
CIRCUITPY_ANALOGIO = 0
CIRCUITPY_MATH = 0
CIRCUITPY_NEOPIXEL_WRITE = 0
CIRCUITPY_PULSEIO = 1
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 0
CIRCUITPY_USB_MIDI = 1
Expand Down
4 changes: 2 additions & 2 deletions shared-module/usb_hid/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static mp_obj_tuple_t default_hid_devices_tuple = {
};

bool usb_hid_enabled(void) {
return hid_devices_num == 0;
return hid_devices_num > 0;
}

void usb_hid_set_defaults(void) {
Expand All @@ -119,7 +119,7 @@ size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, uint8_t *current_interfac
descriptor_buf[HID_DESCRIPTOR_INTERFACE_INDEX] = *current_interface;
(*current_interface)++;

usb_add_interface_string(*current_interface, usb_hid_interface_name);
usb_add_interface_string(*current_interface_string, usb_hid_interface_name);
descriptor_buf[HID_DESCRIPTOR_INTERFACE_STRING_INDEX] = *current_interface_string;
(*current_interface_string)++;

Expand Down
8 changes: 4 additions & 4 deletions shared-module/usb_midi/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,19 @@ size_t usb_midi_add_descriptor(uint8_t *descriptor_buf, uint8_t *current_interfa
descriptor_buf[MIDI_STREAMING_INTERFACE_NUMBER_INDEX_2] = *current_interface;
(*current_interface)++;

usb_add_interface_string(*current_interface, midi_streaming_interface_name);
usb_add_interface_string(*current_interface_string, midi_streaming_interface_name);
descriptor_buf[MIDI_STREAMING_INTERFACE_STRING_INDEX] = *current_interface;
(*current_interface_string)++;

usb_add_interface_string(*current_interface, midi_audio_control_interface_name);
usb_add_interface_string(*current_interface_string, midi_audio_control_interface_name);
descriptor_buf[MIDI_AUDIO_CONTROL_INTERFACE_STRING_INDEX] = *current_interface;
(*current_interface_string)++;

usb_add_interface_string(*current_interface, midi_in_jack_name);
usb_add_interface_string(*current_interface_string, midi_in_jack_name);
descriptor_buf[MIDI_IN_JACK_STRING_INDEX] = *current_interface;
(*current_interface_string)++;

usb_add_interface_string(*current_interface, midi_out_jack_name);
usb_add_interface_string(*current_interface_string, midi_out_jack_name);
descriptor_buf[MIDI_OUT_JACK_STRING_INDEX] = *current_interface;
(*current_interface_string)++;

Expand Down
20 changes: 12 additions & 8 deletions supervisor/shared/usb/usb_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ void usb_add_interface_string(uint8_t interface_string_index, const char str[])
collected_interface_strings_length += strlen(str);
}

static const uint16_t language_id[] = {
0x0304,
0x0409,
};

static void usb_build_interface_string_table(void) {
// Allocate space for the le16 String descriptors.
// Space needed is 2 bytes for String Descriptor header, then 2 bytes for each character
Expand All @@ -259,29 +264,28 @@ static void usb_build_interface_string_table(void) {
uint16_t *string_descriptor = string_descriptors;

// Language ID is always the 0th string descriptor.
collected_interface_strings[0].descriptor = (uint16_t[]) {
0x0304,
0x0409,
};
collected_interface_strings[0].descriptor = language_id;

// Build the le16 versions of all the descriptor strings.
// Start at 1 to skip the Language ID.
for (uint8_t string_index = 1; string_index < current_interface_string; string_index++) {
const char *str = collected_interface_strings[string_index].char_str;
const size_t str_len = strlen(str);
uint8_t descriptor_size = 2 + (str_len * 2);
string_descriptor[0] = 0x0300 | descriptor_size;
// 1 word for descriptor type and length, 1 word for each character.
const uint8_t descriptor_size_words = 1 + str_len;
const uint8_t descriptor_size_bytes = descriptor_size_words * 2;
string_descriptor[0] = 0x0300 | descriptor_size_bytes;

// Convert to le16.
for (size_t i = 0; i <= str_len; i++) {
for (size_t i = 0; i < str_len; i++) {
string_descriptor[i + 1] = str[i];
}

// Save ptr to string descriptor with le16 str.
collected_interface_strings[string_index].descriptor = string_descriptor;

// Move to next descriptor slot.
string_descriptor += descriptor_size;
string_descriptor += descriptor_size_words;
}
}

Expand Down

0 comments on commit 4eb4f14

Please sign in to comment.