@@ -40,6 +40,7 @@ static SemaphoreHandle_t tinyusb_hid_device_input_sem = NULL;
4040static SemaphoreHandle_t tinyusb_hid_device_input_mutex = NULL ;
4141
4242static bool tinyusb_hid_is_initialized = false ;
43+ static hid_interface_protocol_enum_t tinyusb_interface_protocol = HID_ITF_PROTOCOL_NONE;
4344static uint8_t tinyusb_loaded_hid_devices_num = 0 ;
4445static uint16_t tinyusb_hid_device_descriptor_len = 0 ;
4546static uint8_t * tinyusb_hid_device_descriptor = NULL ;
@@ -174,7 +175,7 @@ static bool tinyusb_load_enabled_hid_devices(){
174175
175176 esp_hid_report_map_t *hid_report_map = esp_hid_parse_report_map (tinyusb_hid_device_descriptor, tinyusb_hid_device_descriptor_len);
176177 if (hid_report_map){
177- log_d (" Loaded HID Desriptor with the following reports:" );
178+ log_d (" Loaded HID Descriptor with the following reports:" );
178179 for (uint8_t i=0 ; i<hid_report_map->reports_len ; i++){
179180 if (hid_report_map->reports [i].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT){
180181 log_d (" ID: %3u, Type: %7s, Size: %2u, Usage: %8s" ,
@@ -202,14 +203,15 @@ extern "C" uint16_t tusb_hid_load_descriptor(uint8_t * dst, uint8_t * itf)
202203 tinyusb_hid_is_initialized = true ;
203204
204205 uint8_t str_index = tinyusb_add_string_descriptor (" TinyUSB HID" );
205- uint8_t ep_in = tinyusb_get_free_in_endpoint ();
206+ // For keyboard boot protocol, we've already called tinyusb_enable_interface2(reserve_endpoints=true)
207+ uint8_t ep_in = tinyusb_interface_protocol == HID_ITF_PROTOCOL_KEYBOARD ? 1 : tinyusb_get_free_in_endpoint ();
206208 TU_VERIFY (ep_in != 0 );
207- uint8_t ep_out = tinyusb_get_free_out_endpoint ();
209+ uint8_t ep_out = tinyusb_interface_protocol == HID_ITF_PROTOCOL_KEYBOARD ? 1 : tinyusb_get_free_out_endpoint ();
208210 TU_VERIFY (ep_out != 0 );
209211 uint8_t descriptor[TUD_HID_INOUT_DESC_LEN] = {
210212 // HID Input & Output descriptor
211213 // Interface number, string index, protocol, report descriptor len, EP OUT & IN address, size & polling interval
212- TUD_HID_INOUT_DESCRIPTOR (*itf, str_index, HID_ITF_PROTOCOL_NONE , tinyusb_hid_device_descriptor_len, ep_out, (uint8_t )(0x80 | ep_in), 64 , 1 )
214+ TUD_HID_INOUT_DESCRIPTOR (*itf, str_index, tinyusb_interface_protocol , tinyusb_hid_device_descriptor_len, ep_out, (uint8_t )(0x80 | ep_in), 64 , 1 )
213215 };
214216 *itf+=1 ;
215217 memcpy (dst, descriptor, TUD_HID_INOUT_DESC_LEN);
@@ -276,14 +278,15 @@ void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_
276278 }
277279}
278280
279- USBHID::USBHID (){
281+ USBHID::USBHID (hid_interface_protocol_enum_t itf_protocol ){
280282 if (!tinyusb_hid_devices_is_initialized){
281283 tinyusb_hid_devices_is_initialized = true ;
282284 for (uint8_t i=0 ; i<USB_HID_DEVICES_MAX; i++){
283285 memset (&tinyusb_hid_devices[i], 0 , sizeof (tinyusb_hid_device_t ));
284286 }
285287 tinyusb_hid_devices_num = 0 ;
286- tinyusb_enable_interface (USB_INTERFACE_HID, TUD_HID_INOUT_DESC_LEN, tusb_hid_load_descriptor);
288+ tinyusb_interface_protocol = itf_protocol;
289+ tinyusb_enable_interface2 (USB_INTERFACE_HID, TUD_HID_INOUT_DESC_LEN, tusb_hid_load_descriptor, itf_protocol == HID_ITF_PROTOCOL_KEYBOARD);
287290 }
288291}
289292
@@ -342,6 +345,11 @@ bool USBHID::SendReport(uint8_t id, const void* data, size_t len, uint32_t timeo
342345 return false ;
343346 }
344347
348+ // If we're configured to support boot protocol, and the host has requested boot protocol, prevent
349+ // sending of report ID, by passing report ID of 0 to tud_hid_n_report().
350+ uint8_t effective_id = ((tinyusb_interface_protocol != HID_ITF_PROTOCOL_NONE) &&
351+ (tud_hid_n_get_protocol (0 ) == HID_PROTOCOL_BOOT)) ? 0 : id;
352+
345353 bool res = ready ();
346354 if (!res){
347355 log_e (" not ready" );
@@ -352,7 +360,7 @@ bool USBHID::SendReport(uint8_t id, const void* data, size_t len, uint32_t timeo
352360 // we can wait for it to be given after calling tud_hid_n_report().
353361 xSemaphoreTake (tinyusb_hid_device_input_sem, 0 );
354362
355- res = tud_hid_n_report (0 , id , data, len);
363+ res = tud_hid_n_report (0 , effective_id , data, len);
356364 if (!res){
357365 log_e (" report %u failed" , id);
358366 } else {
0 commit comments