diff --git a/shared-module/usb_hid/__init__.c b/shared-module/usb_hid/__init__.c index 42c1347357..c1df75b9d8 100644 --- a/shared-module/usb_hid/__init__.c +++ b/shared-module/usb_hid/__init__.c @@ -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) { @@ -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)++; diff --git a/shared-module/usb_midi/__init__.c b/shared-module/usb_midi/__init__.c index fb564af8d4..c258d35c23 100644 --- a/shared-module/usb_midi/__init__.c +++ b/shared-module/usb_midi/__init__.c @@ -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)++; diff --git a/supervisor/shared/usb/usb_desc.c b/supervisor/shared/usb/usb_desc.c index 99a769c704..f1d2ac7196 100644 --- a/supervisor/shared/usb/usb_desc.c +++ b/supervisor/shared/usb/usb_desc.c @@ -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 @@ -259,21 +264,20 @@ 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]; } @@ -281,7 +285,7 @@ static void usb_build_interface_string_table(void) { collected_interface_strings[string_index].descriptor = string_descriptor; // Move to next descriptor slot. - string_descriptor += descriptor_size; + string_descriptor += descriptor_size_words; } }