Skip to content

Commit 8f7f286

Browse files
authored
Merge pull request #3558 from hathach/hid-host-set-protocol-skipable
Add `CFG_TUH_HID_SET_PROTOCOL_ON_ENUM` configuration option
2 parents 151a5ad + a46c863 commit 8f7f286

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/class/hid/hid_host.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void tuh_hid_set_default_protocol(uint8_t protocol) {
240240
_hidh_default_protocol = protocol;
241241
}
242242

243-
static bool _hidh_set_protocol(uint8_t daddr, uint8_t itf_num, uint8_t protocol,
243+
static bool hidh_set_protocol(uint8_t daddr, uint8_t itf_num, uint8_t protocol,
244244
tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
245245
TU_LOG_DRV("HID Set Protocol = %d\r\n", protocol);
246246

@@ -272,7 +272,7 @@ bool tuh_hid_set_protocol(uint8_t daddr, uint8_t idx, uint8_t protocol) {
272272
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
273273
TU_VERIFY(p_hid && p_hid->itf_protocol != HID_ITF_PROTOCOL_NONE);
274274

275-
return _hidh_set_protocol(daddr, p_hid->itf_num, protocol, set_protocol_complete, 0);
275+
return hidh_set_protocol(daddr, p_hid->itf_num, protocol, set_protocol_complete, 0);
276276
}
277277

278278
static void get_report_complete(tuh_xfer_t* xfer) {
@@ -359,7 +359,7 @@ bool tuh_hid_set_report(uint8_t daddr, uint8_t idx, uint8_t report_id, uint8_t r
359359
return tuh_control_xfer(&xfer);
360360
}
361361

362-
static bool _hidh_set_idle(uint8_t daddr, uint8_t itf_num, uint16_t idle_rate,
362+
static bool hidh_set_idle(uint8_t daddr, uint8_t itf_num, uint16_t idle_rate,
363363
tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
364364
// SET IDLE request, device can stall if not support this request
365365
TU_LOG_DRV("HID Set Idle \r\n");
@@ -566,8 +566,8 @@ uint16_t hidh_open(uint8_t rhport, uint8_t daddr, const tusb_desc_interface_t *d
566566
// Use offsetof to avoid pointer to the odd/misaligned address
567567
p_hid->report_desc_len = tu_unaligned_read16((uint8_t const*)desc_hid + offsetof(tusb_hid_descriptor_hid_t, wReportLength));
568568

569-
// Per HID Specs: default is Report protocol, though we will force Boot protocol when set_config
570-
p_hid->protocol_mode = _hidh_default_protocol;
569+
// Per HID Specs: default is Report protocol
570+
p_hid->protocol_mode = HID_PROTOCOL_REPORT;
571571
if (HID_SUBCLASS_BOOT == desc_itf->bInterfaceSubClass) {
572572
p_hid->itf_protocol = desc_itf->bInterfaceProtocol;
573573
}
@@ -623,25 +623,30 @@ static void process_set_config(tuh_xfer_t* xfer) {
623623

624624
switch (state) {
625625
case CONFG_SET_IDLE: {
626-
// Idle rate = 0 mean only report when there is changes
626+
// Idle rate = 0 mean only report when there are changes
627627
const uint16_t idle_rate = 0;
628628
const uintptr_t next_state = (p_hid->itf_protocol != HID_ITF_PROTOCOL_NONE)
629629
? CONFIG_SET_PROTOCOL : CONFIG_GET_REPORT_DESC;
630-
_hidh_set_idle(daddr, itf_num, idle_rate, process_set_config, next_state);
630+
hidh_set_idle(daddr, itf_num, idle_rate, process_set_config, next_state);
631631
break;
632632
}
633633

634634
case CONFIG_SET_PROTOCOL:
635-
_hidh_set_protocol(daddr, p_hid->itf_num, _hidh_default_protocol, process_set_config, CONFIG_GET_REPORT_DESC);
635+
#if CFG_TUH_HID_SET_PROTOCOL_ON_ENUM
636+
hidh_set_protocol(daddr, p_hid->itf_num, _hidh_default_protocol, process_set_config, CONFIG_GET_REPORT_DESC);
636637
break;
638+
#else
639+
TU_ATTR_FALLTHROUGH;
640+
#endif
637641

638642
case CONFIG_GET_REPORT_DESC:
643+
if (xfer->setup->bRequest == HID_REQ_CONTROL_SET_PROTOCOL && xfer->result == XFER_RESULT_SUCCESS) {
644+
p_hid->protocol_mode = (uint8_t) tu_le16toh(xfer->setup->wValue);
645+
}
639646
// Get Report Descriptor if possible
640-
// using usbh enumeration buffer since report descriptor can be very long
647+
// using usbh enumeration buffer since the report descriptor can be very long
641648
if (p_hid->report_desc_len > CFG_TUH_ENUMERATION_BUFSIZE) {
642649
TU_LOG_DRV("HID Skip Report Descriptor since it is too large %u bytes\r\n", p_hid->report_desc_len);
643-
644-
// Driver is mounted without report descriptor
645650
config_driver_mount_complete(daddr, idx, NULL, 0);
646651
} else {
647652
tuh_descriptor_get_hid_report(daddr, itf_num, p_hid->report_desc_type, 0,
@@ -651,8 +656,8 @@ static void process_set_config(tuh_xfer_t* xfer) {
651656
break;
652657

653658
case CONFIG_COMPLETE: {
654-
uint8_t const* desc_report = usbh_get_enum_buf();
655-
uint16_t const desc_len = tu_le16toh(xfer->setup->wLength);
659+
const uint8_t *desc_report = usbh_get_enum_buf();
660+
const uint16_t desc_len = tu_le16toh(xfer->setup->wLength);
656661

657662
config_driver_mount_complete(daddr, idx, desc_report, desc_len);
658663
break;

src/class/hid/hid_host.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ extern "C" {
3636
//--------------------------------------------------------------------+
3737
// Class Driver Configuration
3838
//--------------------------------------------------------------------+
39-
4039
// TODO Highspeed interrupt can be up to 512 bytes
4140
#ifndef CFG_TUH_HID_EPIN_BUFSIZE
4241
#define CFG_TUH_HID_EPIN_BUFSIZE 64
@@ -46,7 +45,13 @@ extern "C" {
4645
#define CFG_TUH_HID_EPOUT_BUFSIZE 64
4746
#endif
4847

48+
#ifndef CFG_TUH_HID_SET_PROTOCOL_ON_ENUM
49+
#define CFG_TUH_HID_SET_PROTOCOL_ON_ENUM 1
50+
#endif
4951

52+
//--------------------------------------------------------------------+
53+
// Interface API
54+
//--------------------------------------------------------------------+
5055
typedef struct {
5156
uint8_t report_id;
5257
uint8_t usage;
@@ -57,10 +62,6 @@ typedef struct {
5762
// uint8_t out_len; // length of OUT report
5863
} tuh_hid_report_info_t;
5964

60-
//--------------------------------------------------------------------+
61-
// Interface API
62-
//--------------------------------------------------------------------+
63-
6465
// Get the total number of mounted HID interfaces of a device
6566
uint8_t tuh_hid_itf_get_count(uint8_t dev_addr);
6667

0 commit comments

Comments
 (0)