change meaning of CFG_TUH_HID to total number of HID interfaces supported.

- previously CFG_TUH_HID is max number of interfaces per device which is
rather limited and consume more resources than needed.
- change hid host instance in API to index
This commit is contained in:
hathach 2023-03-21 21:04:06 +07:00
parent 8a0b17598c
commit d22fc550c7
5 changed files with 218 additions and 189 deletions

View File

@ -132,7 +132,7 @@
// max device support (excluding hub device)
#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports
#define CFG_TUH_HID 4
#define CFG_TUH_HID (4*CFG_TUH_DEVICE_MAX)
#define CFG_TUH_HID_EPIN_BUFSIZE 64
#define CFG_TUH_HID_EPOUT_BUFSIZE 64

View File

@ -36,7 +36,7 @@
#if CFG_TUSB_MCU == OPT_MCU_RP2040
// change to 1 if using pico-pio-usb as host controller for raspberry rp2040
#define CFG_TUH_RPI_PIO_USB 0
#define CFG_TUH_RPI_PIO_USB 1
#define BOARD_TUH_RHPORT CFG_TUH_RPI_PIO_USB
#endif
@ -97,7 +97,7 @@
#define CFG_TUH_HUB 1 // number of supported hubs
#define CFG_TUH_CDC 1
#define CFG_TUH_HID 4 // typical keyboard + mouse device can have 3-4 HID interfaces
#define CFG_TUH_HID (4*CFG_TUH_DEVICE_MAX) // typical keyboard + mouse device can have 3-4 HID interfaces
#define CFG_TUH_MSC 1
#define CFG_TUH_VENDOR 0

View File

@ -97,7 +97,7 @@
#define CFG_TUH_HUB 0
#define CFG_TUH_CDC 0
#define CFG_TUH_HID 4 // typical keyboard + mouse device can have 3-4 HID interfaces
#define CFG_TUH_HID (4*CFG_TUH_DEVICE_MAX) // typical keyboard + mouse device can have 3-4 HID interfaces
#define CFG_TUH_MSC 0
#define CFG_TUH_VENDOR 0

View File

@ -39,6 +39,8 @@
typedef struct
{
uint8_t daddr;
uint8_t itf_num;
uint8_t ep_in;
uint8_t ep_out;
@ -56,74 +58,131 @@ typedef struct
uint8_t epout_buf[CFG_TUH_HID_EPOUT_BUFSIZE];
} hidh_interface_t;
typedef struct
{
uint8_t inst_count;
hidh_interface_t instances[CFG_TUH_HID];
} hidh_device_t;
CFG_TUSB_MEM_SECTION
static hidh_device_t _hidh_dev[CFG_TUH_DEVICE_MAX];
tu_static hidh_interface_t _hidh_itf[CFG_TUH_HID];
//------------- Internal prototypes -------------//
//--------------------------------------------------------------------+
// Helper
//--------------------------------------------------------------------+
// Get HID device & interface
TU_ATTR_ALWAYS_INLINE static inline hidh_device_t* get_dev(uint8_t dev_addr);
TU_ATTR_ALWAYS_INLINE static inline hidh_interface_t* get_instance(uint8_t dev_addr, uint8_t instance);
static uint8_t get_instance_id_by_itfnum(uint8_t dev_addr, uint8_t itf);
static uint8_t get_instance_id_by_epaddr(uint8_t dev_addr, uint8_t ep_addr);
TU_ATTR_ALWAYS_INLINE static inline
hidh_interface_t* get_hid_itf(uint8_t daddr, uint8_t idx)
{
TU_ASSERT(daddr && idx < CFG_TUH_HID, NULL);
hidh_interface_t* p_hid = &_hidh_itf[idx];
return (p_hid->daddr == daddr) ? p_hid : NULL;
}
// Get instance ID by endpoint address
static uint8_t get_idx_by_epaddr(uint8_t daddr, uint8_t ep_addr)
{
for ( uint8_t idx = 0; idx < CFG_TUH_HID; idx++ )
{
hidh_interface_t const * p_hid = &_hidh_itf[idx];
if ( p_hid->daddr == daddr &&
(p_hid->ep_in == ep_addr || p_hid->ep_out == ep_addr) )
{
return idx;
}
}
return TUSB_INDEX_INVALID_8;
}
static hidh_interface_t* find_new_itf(void)
{
for(uint8_t i=0; i<CFG_TUH_HID; i++)
{
if (_hidh_itf[i].daddr == 0) return &_hidh_itf[i];
}
return NULL;
}
//--------------------------------------------------------------------+
// Interface API
//--------------------------------------------------------------------+
uint8_t tuh_hid_instance_count(uint8_t dev_addr)
uint8_t tuh_hid_itf_get_count(uint8_t daddr)
{
return get_dev(dev_addr)->inst_count;
uint8_t count = 0;
for(uint8_t i=0; i<CFG_TUH_HID; i++)
{
if (_hidh_itf[i].daddr == daddr) count++;
}
return count;
}
bool tuh_hid_mounted(uint8_t dev_addr, uint8_t instance)
uint8_t tuh_hid_itf_get_total_count(void)
{
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
return (hid_itf->ep_in != 0) || (hid_itf->ep_out != 0);
uint8_t count = 0;
for(uint8_t i=0; i<CFG_TUH_HID; i++)
{
if (_hidh_itf[i].daddr != 0) count++;
}
return count;
}
uint8_t tuh_hid_interface_protocol(uint8_t dev_addr, uint8_t instance)
bool tuh_hid_mounted(uint8_t daddr, uint8_t idx)
{
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
return hid_itf->itf_protocol;
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
return p_hid != NULL;
}
uint8_t tuh_hid_itf_get_index(uint8_t daddr, uint8_t itf_num)
{
for ( uint8_t idx = 0; idx < CFG_TUH_HID; idx++ )
{
hidh_interface_t const * p_hid = &_hidh_itf[idx];
if ( p_hid->daddr == daddr && p_hid->itf_num == itf_num) return idx;
}
return TUSB_INDEX_INVALID_8;
}
uint8_t tuh_hid_interface_protocol(uint8_t daddr, uint8_t idx)
{
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
return p_hid ? p_hid->itf_protocol : 0;
}
//--------------------------------------------------------------------+
// Control Endpoint API
//--------------------------------------------------------------------+
uint8_t tuh_hid_get_protocol(uint8_t dev_addr, uint8_t instance)
uint8_t tuh_hid_get_protocol(uint8_t daddr, uint8_t idx)
{
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
return hid_itf->protocol_mode;
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
return p_hid ? p_hid->protocol_mode : 0;
}
static void set_protocol_complete(tuh_xfer_t* xfer)
{
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
uint8_t const daddr = xfer->daddr;
uint8_t const instance = get_instance_id_by_itfnum(daddr, itf_num);
hidh_interface_t* hid_itf = get_instance(daddr, instance);
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
uint8_t const daddr = xfer->daddr;
uint8_t const idx = tuh_hid_itf_get_index(daddr, itf_num);
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
TU_VERIFY(p_hid, );
if (XFER_RESULT_SUCCESS == xfer->result)
{
hid_itf->protocol_mode = (uint8_t) tu_le16toh(xfer->setup->wValue);
p_hid->protocol_mode = (uint8_t) tu_le16toh(xfer->setup->wValue);
}
if (tuh_hid_set_protocol_complete_cb)
{
tuh_hid_set_protocol_complete_cb(daddr, instance, hid_itf->protocol_mode);
tuh_hid_set_protocol_complete_cb(daddr, idx, p_hid->protocol_mode);
}
}
static bool _hidh_set_protocol(uint8_t dev_addr, uint8_t itf_num, uint8_t protocol, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
static bool _hidh_set_protocol(uint8_t daddr, uint8_t itf_num, uint8_t protocol, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
{
TU_LOG2("HID Set Protocol = %d\r\n", protocol);
@ -143,7 +202,7 @@ static bool _hidh_set_protocol(uint8_t dev_addr, uint8_t itf_num, uint8_t protoc
tuh_xfer_t xfer =
{
.daddr = dev_addr,
.daddr = daddr,
.ep_addr = 0,
.setup = &request,
.buffer = NULL,
@ -151,16 +210,15 @@ static bool _hidh_set_protocol(uint8_t dev_addr, uint8_t itf_num, uint8_t protoc
.user_data = user_data
};
TU_ASSERT( tuh_control_xfer(&xfer) );
return true;
return tuh_control_xfer(&xfer);
}
bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t instance, uint8_t protocol)
bool tuh_hid_set_protocol(uint8_t daddr, uint8_t idx, uint8_t protocol)
{
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
TU_VERIFY(hid_itf->itf_protocol != HID_ITF_PROTOCOL_NONE);
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
TU_VERIFY(p_hid && p_hid->itf_protocol != HID_ITF_PROTOCOL_NONE);
return _hidh_set_protocol(dev_addr, hid_itf->itf_num, protocol, set_protocol_complete, 0);
return _hidh_set_protocol(daddr, p_hid->itf_num, protocol, set_protocol_complete, 0);
}
static void set_report_complete(tuh_xfer_t* xfer)
@ -169,20 +227,22 @@ static void set_report_complete(tuh_xfer_t* xfer)
if (tuh_hid_set_report_complete_cb)
{
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
uint8_t const instance = get_instance_id_by_itfnum(xfer->daddr, itf_num);
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
uint8_t const idx = tuh_hid_itf_get_index(xfer->daddr, itf_num);
uint8_t const report_type = tu_u16_high(xfer->setup->wValue);
uint8_t const report_id = tu_u16_low(xfer->setup->wValue);
tuh_hid_set_report_complete_cb(xfer->daddr, instance, report_id, report_type,
tuh_hid_set_report_complete_cb(xfer->daddr, idx, report_id, report_type,
(xfer->result == XFER_RESULT_SUCCESS) ? xfer->setup->wLength : 0);
}
}
bool tuh_hid_set_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, void* report, uint16_t len)
bool tuh_hid_set_report(uint8_t daddr, uint8_t idx, uint8_t report_id, uint8_t report_type, void* report, uint16_t len)
{
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
TU_VERIFY(p_hid);
TU_LOG2("HID Set Report: id = %u, type = %u, len = %u\r\n", report_id, report_type, len);
tusb_control_request_t const request =
@ -194,14 +254,14 @@ bool tuh_hid_set_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, u
.direction = TUSB_DIR_OUT
},
.bRequest = HID_REQ_CONTROL_SET_REPORT,
.wValue = tu_u16(report_type, report_id),
.wIndex = hid_itf->itf_num,
.wValue = tu_htole16(tu_u16(report_type, report_id)),
.wIndex = tu_htole16((uint16_t)p_hid->itf_num),
.wLength = len
};
tuh_xfer_t xfer =
{
.daddr = dev_addr,
.daddr = daddr,
.ep_addr = 0,
.setup = &request,
.buffer = report,
@ -209,14 +269,14 @@ bool tuh_hid_set_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, u
.user_data = 0
};
TU_ASSERT( tuh_control_xfer(&xfer) );
return true;
return tuh_control_xfer(&xfer);
}
static bool _hidh_set_idle(uint8_t dev_addr, uint8_t itf_num, uint16_t idle_rate, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
static bool _hidh_set_idle(uint8_t daddr, uint8_t itf_num, uint16_t idle_rate, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
{
// SET IDLE request, device can stall if not support this request
TU_LOG2("HID Set Idle \r\n");
tusb_control_request_t const request =
{
.bmRequestType_bit =
@ -226,14 +286,14 @@ static bool _hidh_set_idle(uint8_t dev_addr, uint8_t itf_num, uint16_t idle_rate
.direction = TUSB_DIR_OUT
},
.bRequest = HID_REQ_CONTROL_SET_IDLE,
.wValue = idle_rate,
.wIndex = itf_num,
.wValue = tu_htole16(idle_rate),
.wIndex = tu_htole16((uint16_t)itf_num),
.wLength = 0
};
tuh_xfer_t xfer =
{
.daddr = dev_addr,
.daddr = daddr,
.ep_addr = 0,
.setup = &request,
.buffer = NULL,
@ -241,25 +301,24 @@ static bool _hidh_set_idle(uint8_t dev_addr, uint8_t itf_num, uint16_t idle_rate
.user_data = user_data
};
TU_ASSERT( tuh_control_xfer(&xfer) );
return true;
return tuh_control_xfer(&xfer);
}
//--------------------------------------------------------------------+
// Interrupt Endpoint API
//--------------------------------------------------------------------+
bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t instance)
bool tuh_hid_receive_report(uint8_t daddr, uint8_t idx)
{
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
TU_VERIFY(p_hid);
// claim endpoint
TU_VERIFY( usbh_edpt_claim(dev_addr, hid_itf->ep_in) );
TU_VERIFY( usbh_edpt_claim(daddr, p_hid->ep_in) );
if ( !usbh_edpt_xfer(dev_addr, hid_itf->ep_in, hid_itf->epin_buf, hid_itf->epin_size) )
if ( !usbh_edpt_xfer(daddr, p_hid->ep_in, p_hid->epin_buf, p_hid->epin_size) )
{
usbh_edpt_release(dev_addr, hid_itf->ep_in);
usbh_edpt_release(daddr, p_hid->ep_in);
return false;
}
@ -274,44 +333,45 @@ bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t instance)
// return !usbh_edpt_busy(dev_addr, hid_itf->ep_in);
//}
bool tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, const void* report, uint16_t len)
bool tuh_hid_send_report(uint8_t daddr, uint8_t idx, uint8_t report_id, const void* report, uint16_t len)
{
TU_LOG2("HID Send Report %d\r\n", report_id);
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
TU_VERIFY(p_hid);
if (hid_itf->ep_out == 0)
if (p_hid->ep_out == 0)
{
// This HID does not have an out endpoint (other than control)
return false;
}
else if (len > CFG_TUH_HID_EPOUT_BUFSIZE
|| (report_id != 0 && len > (CFG_TUH_HID_EPOUT_BUFSIZE - 1)))
else if (len > CFG_TUH_HID_EPOUT_BUFSIZE ||
(report_id != 0 && len > (CFG_TUH_HID_EPOUT_BUFSIZE - 1)))
{
// ep_out buffer is not large enough to hold contents
return false;
}
// claim endpoint
TU_VERIFY( usbh_edpt_claim(dev_addr, hid_itf->ep_out) );
TU_VERIFY( usbh_edpt_claim(daddr, p_hid->ep_out) );
if (report_id == 0)
{
// No report ID in transmission
memcpy(&hid_itf->epout_buf[0], report, len);
memcpy(&p_hid->epout_buf[0], report, len);
}
else
{
hid_itf->epout_buf[0] = report_id;
memcpy(&hid_itf->epout_buf[1], report, len);
p_hid->epout_buf[0] = report_id;
memcpy(&p_hid->epout_buf[1], report, len);
++len; // 1 more byte for report_id
}
TU_LOG3_MEM(hid_itf->epout_buf, len, 2);
TU_LOG3_MEM(p_hid->epout_buf, len, 2);
if ( !usbh_edpt_xfer(dev_addr, hid_itf->ep_out, hid_itf->epout_buf, len) )
if ( !usbh_edpt_xfer(daddr, p_hid->ep_out, p_hid->epout_buf, len) )
{
usbh_edpt_release(dev_addr, hid_itf->ep_out);
usbh_edpt_release(daddr, p_hid->ep_out);
return false;
}
@ -323,56 +383,57 @@ bool tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id,
//--------------------------------------------------------------------+
void hidh_init(void)
{
tu_memclr(_hidh_dev, sizeof(_hidh_dev));
tu_memclr(_hidh_itf, sizeof(_hidh_itf));
}
bool hidh_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
bool hidh_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
{
(void) result;
uint8_t const dir = tu_edpt_dir(ep_addr);
uint8_t const instance = get_instance_id_by_epaddr(dev_addr, ep_addr);
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
uint8_t const idx = get_idx_by_epaddr(daddr, ep_addr);
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
TU_VERIFY(p_hid);
if ( dir == TUSB_DIR_IN )
{
TU_LOG2(" Get Report callback (%u, %u)\r\n", dev_addr, instance);
TU_LOG3_MEM(hid_itf->epin_buf, xferred_bytes, 2);
tuh_hid_report_received_cb(dev_addr, instance, hid_itf->epin_buf, (uint16_t) xferred_bytes);
TU_LOG2(" Get Report callback (%u, %u)\r\n", daddr, idx);
TU_LOG3_MEM(p_hid->epin_buf, xferred_bytes, 2);
tuh_hid_report_received_cb(daddr, idx, p_hid->epin_buf, (uint16_t) xferred_bytes);
}else
{
if (tuh_hid_report_sent_cb) tuh_hid_report_sent_cb(dev_addr, instance, hid_itf->epout_buf, (uint16_t) xferred_bytes);
if (tuh_hid_report_sent_cb) tuh_hid_report_sent_cb(daddr, idx, p_hid->epout_buf, (uint16_t) xferred_bytes);
}
return true;
}
void hidh_close(uint8_t dev_addr)
void hidh_close(uint8_t daddr)
{
TU_VERIFY(dev_addr <= CFG_TUH_DEVICE_MAX, );
hidh_device_t* hid_dev = get_dev(dev_addr);
if (tuh_hid_umount_cb)
for(uint8_t i=0; i<CFG_TUH_HID; i++)
{
for (uint8_t inst = 0; inst < hid_dev->inst_count; inst++ ) tuh_hid_umount_cb(dev_addr, inst);
hidh_interface_t* p_hid = &_hidh_itf[i];
if (p_hid->daddr == daddr)
{
if(tuh_hid_umount_cb) tuh_hid_umount_cb(daddr, i);
p_hid->daddr = 0;
}
}
tu_memclr(hid_dev, sizeof(hidh_device_t));
}
//--------------------------------------------------------------------+
// Enumeration
//--------------------------------------------------------------------+
bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len)
bool hidh_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const *desc_itf, uint16_t max_len)
{
(void) rhport;
(void) max_len;
TU_VERIFY(TUSB_CLASS_HID == desc_itf->bInterfaceClass);
TU_LOG2("[%u] HID opening Interface %u\r\n", dev_addr, desc_itf->bInterfaceNumber);
TU_LOG2("[%u] HID opening Interface %u\r\n", daddr, desc_itf->bInterfaceNumber);
// len = interface + hid + n*endpoints
uint16_t const drv_len = (uint16_t) (sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) +
@ -386,12 +447,9 @@ bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *de
tusb_hid_descriptor_hid_t const *desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc;
TU_ASSERT(HID_DESC_TYPE_HID == desc_hid->bDescriptorType);
// not enough interface, try to increase CFG_TUH_HID
// TODO multiple devices
hidh_device_t* hid_dev = get_dev(dev_addr);
TU_ASSERT(hid_dev->inst_count < CFG_TUH_HID, 0);
hidh_interface_t* hid_itf = get_instance(dev_addr, hid_dev->inst_count);
hidh_interface_t* p_hid = find_new_itf();
TU_ASSERT(p_hid); // not enough interface, try to increase CFG_TUH_HID
p_hid->daddr = daddr;
//------------- Endpoint Descriptors -------------//
p_desc = tu_desc_next(p_desc);
@ -400,34 +458,35 @@ bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *de
for(int i = 0; i < desc_itf->bNumEndpoints; i++)
{
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType);
TU_ASSERT( tuh_edpt_open(dev_addr, desc_ep) );
TU_ASSERT( tuh_edpt_open(daddr, desc_ep) );
if(tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN)
{
hid_itf->ep_in = desc_ep->bEndpointAddress;
hid_itf->epin_size = tu_edpt_packet_size(desc_ep);
p_hid->ep_in = desc_ep->bEndpointAddress;
p_hid->epin_size = tu_edpt_packet_size(desc_ep);
}
else
{
hid_itf->ep_out = desc_ep->bEndpointAddress;
hid_itf->epout_size = tu_edpt_packet_size(desc_ep);
p_hid->ep_out = desc_ep->bEndpointAddress;
p_hid->epout_size = tu_edpt_packet_size(desc_ep);
}
p_desc = tu_desc_next(p_desc);
desc_ep = (tusb_desc_endpoint_t const *) p_desc;
}
hid_dev->inst_count++;
hid_itf->itf_num = desc_itf->bInterfaceNumber;
p_hid->itf_num = desc_itf->bInterfaceNumber;
// Assume bNumDescriptors = 1
hid_itf->report_desc_type = desc_hid->bReportType;
hid_itf->report_desc_len = tu_unaligned_read16(&desc_hid->wReportLength);
p_hid->report_desc_type = desc_hid->bReportType;
p_hid->report_desc_len = tu_unaligned_read16(&desc_hid->wReportLength);
// Per HID Specs: default is Report protocol, though we will force Boot protocol when set_config
hid_itf->protocol_mode = HID_PROTOCOL_BOOT;
if ( HID_SUBCLASS_BOOT == desc_itf->bInterfaceSubClass ) hid_itf->itf_protocol = desc_itf->bInterfaceProtocol;
p_hid->protocol_mode = HID_PROTOCOL_BOOT;
if ( HID_SUBCLASS_BOOT == desc_itf->bInterfaceSubClass )
{
p_hid->itf_protocol = desc_itf->bInterfaceProtocol;
}
return true;
}
@ -443,16 +502,16 @@ enum {
CONFIG_COMPLETE
};
static void config_driver_mount_complete(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len);
static void config_driver_mount_complete(uint8_t daddr, uint8_t idx, uint8_t const* desc_report, uint16_t desc_len);
static void process_set_config(tuh_xfer_t* xfer);
bool hidh_set_config(uint8_t dev_addr, uint8_t itf_num)
bool hidh_set_config(uint8_t daddr, uint8_t itf_num)
{
tusb_control_request_t request;
request.wIndex = tu_htole16((uint16_t) itf_num);
tuh_xfer_t xfer;
xfer.daddr = dev_addr;
xfer.daddr = daddr;
xfer.result = XFER_RESULT_SUCCESS;
xfer.setup = &request;
xfer.user_data = CONFG_SET_IDLE;
@ -477,8 +536,9 @@ static void process_set_config(tuh_xfer_t* xfer)
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
uint8_t const daddr = xfer->daddr;
uint8_t const instance = get_instance_id_by_itfnum(daddr, itf_num);
hidh_interface_t* hid_itf = get_instance(daddr, instance);
uint8_t const idx = tuh_hid_itf_get_index(daddr, itf_num);
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
TU_VERIFY(p_hid, );
switch(state)
{
@ -486,27 +546,27 @@ static void process_set_config(tuh_xfer_t* xfer)
{
// Idle rate = 0 mean only report when there is changes
const uint16_t idle_rate = 0;
const uintptr_t next_state = (hid_itf->itf_protocol != HID_ITF_PROTOCOL_NONE) ? CONFIG_SET_PROTOCOL : CONFIG_GET_REPORT_DESC;
const uintptr_t next_state = (p_hid->itf_protocol != HID_ITF_PROTOCOL_NONE) ? CONFIG_SET_PROTOCOL : CONFIG_GET_REPORT_DESC;
_hidh_set_idle(daddr, itf_num, idle_rate, process_set_config, next_state);
}
break;
case CONFIG_SET_PROTOCOL:
_hidh_set_protocol(daddr, hid_itf->itf_num, HID_PROTOCOL_BOOT, process_set_config, CONFIG_GET_REPORT_DESC);
_hidh_set_protocol(daddr, p_hid->itf_num, HID_PROTOCOL_BOOT, process_set_config, CONFIG_GET_REPORT_DESC);
break;
case CONFIG_GET_REPORT_DESC:
// Get Report Descriptor if possible
// using usbh enumeration buffer since report descriptor can be very long
if( hid_itf->report_desc_len > CFG_TUH_ENUMERATION_BUFSIZE )
if( p_hid->report_desc_len > CFG_TUH_ENUMERATION_BUFSIZE )
{
TU_LOG2("HID Skip Report Descriptor since it is too large %u bytes\r\n", hid_itf->report_desc_len);
TU_LOG2("HID Skip Report Descriptor since it is too large %u bytes\r\n", p_hid->report_desc_len);
// Driver is mounted without report descriptor
config_driver_mount_complete(daddr, instance, NULL, 0);
config_driver_mount_complete(daddr, idx, NULL, 0);
}else
{
tuh_descriptor_get_hid_report(daddr, itf_num, hid_itf->report_desc_type, 0, usbh_get_enum_buf(), hid_itf->report_desc_len, process_set_config, CONFIG_COMPLETE);
tuh_descriptor_get_hid_report(daddr, itf_num, p_hid->report_desc_type, 0, usbh_get_enum_buf(), p_hid->report_desc_len, process_set_config, CONFIG_COMPLETE);
}
break;
@ -515,7 +575,7 @@ static void process_set_config(tuh_xfer_t* xfer)
uint8_t const* desc_report = usbh_get_enum_buf();
uint16_t const desc_len = tu_le16toh(xfer->setup->wLength);
config_driver_mount_complete(daddr, instance, desc_report, desc_len);
config_driver_mount_complete(daddr, idx, desc_report, desc_len);
}
break;
@ -523,15 +583,16 @@ static void process_set_config(tuh_xfer_t* xfer)
}
}
static void config_driver_mount_complete(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len)
static void config_driver_mount_complete(uint8_t daddr, uint8_t idx, uint8_t const* desc_report, uint16_t desc_len)
{
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
TU_VERIFY(p_hid, );
// enumeration is complete
tuh_hid_mount_cb(dev_addr, instance, desc_report, desc_len);
if (tuh_hid_mount_cb) tuh_hid_mount_cb(daddr, idx, desc_report, desc_len);
// notify usbh that driver enumeration is complete
usbh_driver_set_config_complete(dev_addr, hid_itf->itf_num);
usbh_driver_set_config_complete(daddr, p_hid->itf_num);
}
//--------------------------------------------------------------------+
@ -676,46 +737,4 @@ uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* report_info_arr,
return report_num;
}
//--------------------------------------------------------------------+
// Helper
//--------------------------------------------------------------------+
// Get Device by address
TU_ATTR_ALWAYS_INLINE static inline hidh_device_t* get_dev(uint8_t dev_addr)
{
return &_hidh_dev[dev_addr-1];
}
// Get Interface by instance number
TU_ATTR_ALWAYS_INLINE static inline hidh_interface_t* get_instance(uint8_t dev_addr, uint8_t instance)
{
return &_hidh_dev[dev_addr-1].instances[instance];
}
// Get instance ID by interface number
static uint8_t get_instance_id_by_itfnum(uint8_t dev_addr, uint8_t itf)
{
for ( uint8_t inst = 0; inst < CFG_TUH_HID; inst++ )
{
hidh_interface_t *hid = get_instance(dev_addr, inst);
if ( (hid->itf_num == itf) && (hid->ep_in || hid->ep_out) ) return inst;
}
return 0xff;
}
// Get instance ID by endpoint address
static uint8_t get_instance_id_by_epaddr(uint8_t dev_addr, uint8_t ep_addr)
{
for ( uint8_t inst = 0; inst < CFG_TUH_HID; inst++ )
{
hidh_interface_t *hid = get_instance(dev_addr, inst);
if ( (ep_addr == hid->ep_in) || ( ep_addr == hid->ep_out) ) return inst;
}
return 0xff;
}
#endif

View File

@ -62,14 +62,24 @@ typedef struct
// Interface API
//--------------------------------------------------------------------+
// Get the number of HID instances
uint8_t tuh_hid_instance_count(uint8_t dev_addr);
// Get the number of mounted HID interfaces of a device
uint8_t tuh_hid_itf_get_count(uint8_t dev_addr);
// Check if HID instance is mounted
bool tuh_hid_mounted(uint8_t dev_addr, uint8_t instance);
// Get all mounted interfaces across devices
uint8_t tuh_hid_itf_get_total_count(void);
// backward compatible rename
#define tuh_hid_instance_count tuh_hid_itf_get_count
// Check if HID interface is mounted
bool tuh_hid_mounted(uint8_t dev_addr, uint8_t idx);
// Get Interface index from device address + interface number
// return TUSB_INDEX_INVALID_8 (0xFF) if not found
uint8_t tuh_hid_itf_get_index(uint8_t daddr, uint8_t itf_num);
// Get interface supported protocol (bInterfaceProtocol) check out hid_interface_protocol_enum_t for possible values
uint8_t tuh_hid_interface_protocol(uint8_t dev_addr, uint8_t instance);
uint8_t tuh_hid_interface_protocol(uint8_t dev_addr, uint8_t idx);
// Parse report descriptor into array of report_info struct and return number of reports.
// For complicated report, application should write its own parser.
@ -82,31 +92,31 @@ uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* reports_info_arr,
// Get current protocol: HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
// Note: Device will be initialized in Boot protocol for simplicity.
// Application can use set_protocol() to switch back to Report protocol.
uint8_t tuh_hid_get_protocol(uint8_t dev_addr, uint8_t instance);
uint8_t tuh_hid_get_protocol(uint8_t dev_addr, uint8_t idx);
// Set protocol to HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
// This function is only supported by Boot interface (tuh_n_hid_interface_protocol() != NONE)
bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t instance, uint8_t protocol);
bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t idx, uint8_t protocol);
// Set Report using control endpoint
// report_type is either Input, Output or Feature, (value from hid_report_type_t)
bool tuh_hid_set_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, void* report, uint16_t len);
bool tuh_hid_set_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, void* report, uint16_t len);
//--------------------------------------------------------------------+
// Interrupt Endpoint API
//--------------------------------------------------------------------+
// Check if the interface is ready to use
//bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance);
//bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t idx);
// Try to receive next report on Interrupt Endpoint. Immediately return
// - true If succeeded, tuh_hid_report_received_cb() callback will be invoked when report is available
// - false if failed to queue the transfer e.g endpoint is busy
bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t instance);
bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t idx);
// Send report using interrupt endpoint
// If report_id > 0 (composite), it will be sent as 1st byte, then report contents. Otherwise only report content is sent.
bool tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, const void* report, uint16_t len);
bool tuh_hid_send_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, const void* report, uint16_t len);
//--------------------------------------------------------------------+
// Callbacks (Weak is optional)
@ -117,24 +127,24 @@ bool tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id,
// can be used to parse common/simple enough descriptor.
// Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped
// therefore report_desc = NULL, desc_len = 0
void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report_desc, uint16_t desc_len);
TU_ATTR_WEAK void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report_desc, uint16_t desc_len);
// Invoked when device with hid interface is un-mounted
TU_ATTR_WEAK void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance);
TU_ATTR_WEAK void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t idx);
// Invoked when received report from device via interrupt endpoint
// Note: if there is report ID (composite), it is 1st byte of report
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len);
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report, uint16_t len);
// Invoked when sent report to device successfully via interrupt endpoint
TU_ATTR_WEAK void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len);
TU_ATTR_WEAK void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report, uint16_t len);
// Invoked when Sent Report to device via either control endpoint
// len = 0 indicate there is error in the transfer e.g stalled response
TU_ATTR_WEAK void tuh_hid_set_report_complete_cb(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len);
TU_ATTR_WEAK void tuh_hid_set_report_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, uint16_t len);
// Invoked when Set Protocol request is complete
TU_ATTR_WEAK void tuh_hid_set_protocol_complete_cb(uint8_t dev_addr, uint8_t instance, uint8_t protocol);
TU_ATTR_WEAK void tuh_hid_set_protocol_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t protocol);
//--------------------------------------------------------------------+
// Internal Class Driver API