@@ -39,6 +39,7 @@ static xSemaphoreHandle tinyusb_hid_device_input_sem = NULL;
3939static xSemaphoreHandle tinyusb_hid_device_input_mutex = NULL ;
4040
4141static bool tinyusb_hid_is_initialized = false ;
42+ static hid_interface_protocol_enum_t tinyusb_interface_protocol = HID_ITF_PROTOCOL_NONE;
4243static uint8_t tinyusb_loaded_hid_devices_num = 0 ;
4344static uint16_t tinyusb_hid_device_descriptor_len = 0 ;
4445static uint8_t * tinyusb_hid_device_descriptor = NULL ;
@@ -173,7 +174,7 @@ static bool tinyusb_load_enabled_hid_devices(){
173174
174175 esp_hid_report_map_t *hid_report_map = esp_hid_parse_report_map (tinyusb_hid_device_descriptor, tinyusb_hid_device_descriptor_len);
175176 if (hid_report_map){
176- log_d (" Loaded HID Desriptor with the following reports:" );
177+ log_d (" Loaded HID Descriptor with the following reports:" );
177178 for (uint8_t i=0 ; i<hid_report_map->reports_len ; i++){
178179 if (hid_report_map->reports [i].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT){
179180 log_d (" ID: %3u, Type: %7s, Size: %2u, Usage: %8s" ,
@@ -201,14 +202,15 @@ extern "C" uint16_t tusb_hid_load_descriptor(uint8_t * dst, uint8_t * itf)
201202 tinyusb_hid_is_initialized = true ;
202203
203204 uint8_t str_index = tinyusb_add_string_descriptor (" TinyUSB HID" );
204- uint8_t ep_in = tinyusb_get_free_in_endpoint ();
205+ // For keyboard boot protocol, we've already called tinyusb_enable_interface2(reserve_endpoints=true)
206+ uint8_t ep_in = tinyusb_interface_protocol == HID_ITF_PROTOCOL_KEYBOARD ? 1 : tinyusb_get_free_in_endpoint ();
205207 TU_VERIFY (ep_in != 0 );
206- uint8_t ep_out = tinyusb_get_free_out_endpoint ();
208+ uint8_t ep_out = tinyusb_interface_protocol == HID_ITF_PROTOCOL_KEYBOARD ? 1 : tinyusb_get_free_out_endpoint ();
207209 TU_VERIFY (ep_out != 0 );
208210 uint8_t descriptor[TUD_HID_INOUT_DESC_LEN] = {
209211 // HID Input & Output descriptor
210212 // Interface number, string index, protocol, report descriptor len, EP OUT & IN address, size & polling interval
211- 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 )
213+ TUD_HID_INOUT_DESCRIPTOR (*itf, str_index, tinyusb_interface_protocol , tinyusb_hid_device_descriptor_len, ep_out, (uint8_t )(0x80 | ep_in), 64 , 1 )
212214 };
213215 *itf+=1 ;
214216 memcpy (dst, descriptor, TUD_HID_INOUT_DESC_LEN);
@@ -275,14 +277,15 @@ void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_
275277 }
276278}
277279
278- USBHID::USBHID (){
280+ USBHID::USBHID (hid_interface_protocol_enum_t itf_protocol ){
279281 if (!tinyusb_hid_devices_is_initialized){
280282 tinyusb_hid_devices_is_initialized = true ;
281283 for (uint8_t i=0 ; i<USB_HID_DEVICES_MAX; i++){
282284 memset (&tinyusb_hid_devices[i], 0 , sizeof (tinyusb_hid_device_t ));
283285 }
284286 tinyusb_hid_devices_num = 0 ;
285- tinyusb_enable_interface (USB_INTERFACE_HID, TUD_HID_INOUT_DESC_LEN, tusb_hid_load_descriptor);
287+ tinyusb_interface_protocol = itf_protocol;
288+ tinyusb_enable_interface2 (USB_INTERFACE_HID, TUD_HID_INOUT_DESC_LEN, tusb_hid_load_descriptor, itf_protocol == HID_ITF_PROTOCOL_KEYBOARD);
286289 }
287290}
288291
@@ -327,11 +330,16 @@ bool USBHID::SendReport(uint8_t id, const void* data, size_t len, uint32_t timeo
327330 return false ;
328331 }
329332
333+ // If we're configured to support boot protocol, and the host has requested boot protocol, prevent
334+ // sending of report ID, by passing report ID of 0 to tud_hid_n_report().
335+ uint8_t effective_id = ((tinyusb_interface_protocol != HID_ITF_PROTOCOL_NONE) &&
336+ (tud_hid_n_get_protocol (0 ) == HID_PROTOCOL_BOOT)) ? 0 : id;
337+
330338 bool res = ready ();
331339 if (!res){
332340 log_e (" not ready" );
333341 } else {
334- res = tud_hid_n_report (0 , id , data, len);
342+ res = tud_hid_n_report (0 , effective_id , data, len);
335343 if (!res){
336344 log_e (" report %u failed" , id);
337345 } else {
0 commit comments