Skip to content

Commit 490a075

Browse files
Make USBHIDKeyboard work at boot
Add a boot_protocol parameter to the USBHIDKeyboard constructor. When true: 1. The USB interface descriptor will have an interface subclass of boot and and interface protocol of keyboard. This will cause some PC BIOS to send a a SET PROTOCOL BOOT request to the device. 2. The USB report descriptor will not have a report ID because boot protocol does not use report IDs. 3. When the host sends reports to the device, USBHIDKeyboard will accept a report ID of 0 instead of checking for HID_REPORT_ID_KEYBOARD(1). 4. When the device sends reports to the host, it will not send a report ID because boot protocol does not use report IDs. 5. Use endpoint address of 1 for input and output because some PC BIOS require it.
1 parent c93bf11 commit 490a075

File tree

6 files changed

+39
-15
lines changed

6 files changed

+39
-15
lines changed

Diff for: cores/esp32/esp32-hal-tinyusb.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,11 @@ static void usb_device_task(void *param) {
651651
#endif
652652
static bool tinyusb_is_initialized = false;
653653

654-
esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface, uint16_t descriptor_len, tinyusb_descriptor_cb_t cb)
654+
esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface, uint16_t descriptor_len, tinyusb_descriptor_cb_t cb){
655+
return tinyusb_enable_interface2(interface, descriptor_len, cb, false);
656+
}
657+
658+
esp_err_t tinyusb_enable_interface2(tinyusb_interface_t interface, uint16_t descriptor_len, tinyusb_descriptor_cb_t cb, bool reserve_endpoints)
655659
{
656660
if(tinyusb_is_initialized){
657661
log_e("TinyUSB has already started! Interface %s not enabled", (interface >= USB_INTERFACE_MAX)?"":tinyusb_interface_names[interface]);
@@ -661,6 +665,13 @@ esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface, uint16_t descr
661665
log_e("Interface %s invalid or already enabled", (interface >= USB_INTERFACE_MAX)?"":tinyusb_interface_names[interface]);
662666
return ESP_FAIL;
663667
}
668+
if(interface == USB_INTERFACE_HID && reserve_endpoints){
669+
// Some simple PC BIOS requires specific endpoint addresses for keyboard at boot
670+
if(!tinyusb_reserve_out_endpoint(1) ||!tinyusb_reserve_in_endpoint(1)){
671+
log_e("HID Reserve Endpoints Failed");
672+
return ESP_FAIL;
673+
}
674+
}
664675
if(interface == USB_INTERFACE_CDC){
665676
if(!tinyusb_reserve_out_endpoint(3) ||!tinyusb_reserve_in_endpoint(4) || !tinyusb_reserve_in_endpoint(5)){
666677
log_e("CDC Reserve Endpoints Failed");

Diff for: cores/esp32/esp32-hal-tinyusb.h

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ typedef enum {
9494
typedef uint16_t (*tinyusb_descriptor_cb_t)(uint8_t * dst, uint8_t * itf);
9595

9696
esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface, uint16_t descriptor_len, tinyusb_descriptor_cb_t cb);
97+
esp_err_t tinyusb_enable_interface2(tinyusb_interface_t interface, uint16_t descriptor_len, tinyusb_descriptor_cb_t cb, bool reserve_endpoints);
9798
uint8_t tinyusb_add_string_descriptor(const char * str);
9899
uint8_t tinyusb_get_free_duplex_endpoint(void);
99100
uint8_t tinyusb_get_free_in_endpoint(void);

Diff for: libraries/USB/src/USBHID.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static xSemaphoreHandle tinyusb_hid_device_input_sem = NULL;
3939
static xSemaphoreHandle tinyusb_hid_device_input_mutex = NULL;
4040

4141
static bool tinyusb_hid_is_initialized = false;
42+
static hid_interface_protocol_enum_t tinyusb_interface_protocol = HID_ITF_PROTOCOL_NONE;
4243
static uint8_t tinyusb_loaded_hid_devices_num = 0;
4344
static uint16_t tinyusb_hid_device_descriptor_len = 0;
4445
static 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

Diff for: libraries/USB/src/USBHID.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class USBHIDDevice
6666
class USBHID
6767
{
6868
public:
69-
USBHID(void);
69+
USBHID(hid_interface_protocol_enum_t itf_protocol = HID_ITF_PROTOCOL_NONE);
7070
void begin(void);
7171
void end(void);
7272
bool ready(void);

Diff for: libraries/USB/src/USBHIDKeyboard.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,25 @@ static const uint8_t report_descriptor[] = {
3232
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(HID_REPORT_ID_KEYBOARD))
3333
};
3434

35-
USBHIDKeyboard::USBHIDKeyboard(): hid(){
35+
// Boot protocol does not use a report ID
36+
static const uint8_t boot_report_descriptor[] = {
37+
TUD_HID_REPORT_DESC_KEYBOARD()
38+
};
39+
40+
USBHIDKeyboard::USBHIDKeyboard(bool boot_protocol):
41+
hid(boot_protocol ? HID_ITF_PROTOCOL_KEYBOARD : HID_ITF_PROTOCOL_NONE),
42+
boot_protocol(boot_protocol){
3643
static bool initialized = false;
3744
if(!initialized){
3845
initialized = true;
39-
hid.addDevice(this, sizeof(report_descriptor));
46+
hid.addDevice(this, boot_protocol ? sizeof(boot_report_descriptor) : sizeof(report_descriptor));
4047
}
4148
}
4249

4350
uint16_t USBHIDKeyboard::_onGetDescriptor(uint8_t* dst){
44-
memcpy(dst, report_descriptor, sizeof(report_descriptor));
45-
return sizeof(report_descriptor);
51+
const size_t size = boot_protocol ? sizeof(boot_report_descriptor) : sizeof(report_descriptor);
52+
memcpy(dst, boot_protocol ? boot_report_descriptor : report_descriptor, size);
53+
return size;
4654
}
4755

4856
void USBHIDKeyboard::begin(){
@@ -60,7 +68,7 @@ void USBHIDKeyboard::onEvent(arduino_usb_hid_keyboard_event_t event, esp_event_h
6068
}
6169

6270
void USBHIDKeyboard::_onOutput(uint8_t report_id, const uint8_t* buffer, uint16_t len){
63-
if(report_id == HID_REPORT_ID_KEYBOARD){
71+
if((boot_protocol && report_id == 0) || report_id == HID_REPORT_ID_KEYBOARD){
6472
arduino_usb_hid_keyboard_event_data_t p;
6573
p.leds = buffer[0];
6674
arduino_usb_event_post(ARDUINO_USB_HID_KEYBOARD_EVENTS, ARDUINO_USB_HID_KEYBOARD_LED_EVENT, &p, sizeof(arduino_usb_hid_keyboard_event_data_t), portMAX_DELAY);
@@ -77,7 +85,7 @@ void USBHIDKeyboard::sendReport(KeyReport* keys)
7785
} else {
7886
memset(report.keycode, 0, 6);
7987
}
80-
hid.SendReport(HID_REPORT_ID_KEYBOARD, &report, sizeof(report));
88+
hid.SendReport(boot_protocol ? 0 : HID_REPORT_ID_KEYBOARD, &report, sizeof(report));
8189
}
8290

8391
#define SHIFT 0x80

Diff for: libraries/USB/src/USBHIDKeyboard.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ class USBHIDKeyboard: public USBHIDDevice, public Print
114114
private:
115115
USBHID hid;
116116
KeyReport _keyReport;
117+
bool boot_protocol;
117118
public:
118-
USBHIDKeyboard(void);
119+
USBHIDKeyboard(bool boot_protocol = false);
119120
void begin(void);
120121
void end(void);
121122
size_t write(uint8_t k);

0 commit comments

Comments
 (0)