From 6034553be7fed82e56da572fad798220117e8f1f Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 28 Jul 2018 13:50:46 +0700 Subject: [PATCH] adding hid_generic support --- src/class/hid/hid_device.c | 48 +++++++++++++++----------------------- src/class/hid/hid_device.h | 12 +++++----- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index bd9d800d..0b411600 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -59,7 +59,8 @@ typedef struct { uint8_t itf_num; uint8_t ep_in; - uint8_t idle_rate; // in unit of 4 ms + uint8_t idle_rate; // in unit of 4 ms + uint8_t usage; // HID_USAGE_* bool boot_protocol; uint8_t report_id; @@ -67,8 +68,8 @@ typedef struct { uint8_t const * report_desc; // class specific control request - uint16_t (*get_report_cb) (hid_report_type_t type, uint8_t* buffer, uint16_t reqlen); - void (*set_report_cb) (hid_report_type_t type, uint8_t const* buffer, uint16_t bufsize); + uint16_t (*get_report_cb) (uint8_t report_id, hid_report_type_t type, uint8_t* buffer, uint16_t reqlen); + void (*set_report_cb) (uint8_t report_id, hid_report_type_t type, uint8_t const* buffer, uint16_t bufsize); CFG_TUSB_MEM_ALIGN uint8_t report_buf[REPORT_BUFSIZE]; }hidd_interface_t; @@ -84,6 +85,12 @@ CFG_TUSB_ATTR_USBRAM static hidd_interface_t _mse_itf; CFG_TUSB_ATTR_USBRAM static hidd_interface_t _hidd_itf; +static inline hidd_interface_t* get_interface_by_itfnum(uint8_t itf_num) +{ + return ( itf_num == _kbd_itf.itf_num ) ? &_kbd_itf : + ( itf_num == _mse_itf.itf_num ) ? &_mse_itf : NULL; +} + //--------------------------------------------------------------------+ // HID GENERIC API @@ -245,18 +252,6 @@ bool tud_hid_mouse_scroll(int8_t vertical, int8_t horizontal) #endif -static inline hidd_interface_t* get_interface_by_edpt(uint8_t ep_addr) -{ - return ( ep_addr == _kbd_itf.ep_in ) ? &_kbd_itf : - ( ep_addr == _mse_itf.ep_in ) ? &_mse_itf : NULL; -} - -static inline hidd_interface_t* get_interface_by_number(uint8_t itf_num) -{ - return ( itf_num == _kbd_itf.itf_num ) ? &_kbd_itf : - ( itf_num == _mse_itf.itf_num ) ? &_mse_itf : NULL; -} - //--------------------------------------------------------------------+ // USBD-CLASS API //--------------------------------------------------------------------+ @@ -343,8 +338,8 @@ tusb_error_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, u p_hid->report_id = 0; p_hid->report_len = 0; p_hid->report_desc = NULL; - //p_hid->get_report_cb = tud_hid_get_report_cb; - //p_hid->set_report_cb = tud_hid_set_report_cb; + p_hid->get_report_cb = tud_hid_generic_get_report_cb; + p_hid->set_report_cb = tud_hid_generic_set_report_cb; return ERR_TUD_INVALID_DESCRIPTOR; } @@ -354,7 +349,7 @@ tusb_error_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, u tusb_error_t hidd_control_request_st(uint8_t rhport, tusb_control_request_t const * p_request) { - hidd_interface_t* p_hid = get_interface_by_number( (uint8_t) p_request->wIndex ); + hidd_interface_t* p_hid = get_interface_by_itfnum( (uint8_t) p_request->wIndex ); TU_ASSERT(p_hid, TUSB_ERROR_FAILED); OSAL_SUBTASK_BEGIN @@ -364,14 +359,12 @@ tusb_error_t hidd_control_request_st(uint8_t rhport, tusb_control_request_t cons { uint8_t const desc_type = u16_high_u8(p_request->wValue); uint8_t const desc_index = u16_low_u8 (p_request->wValue); - (void) desc_index; if (p_request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT) { - STASK_ASSERT ( p_hid->report_len <= CFG_TUD_CTRL_BUFSIZE ); - // use device control buffer + STASK_ASSERT ( p_hid->report_len <= CFG_TUD_CTRL_BUFSIZE ); memcpy(_usbd_ctrl_buf, p_hid->report_desc, p_hid->report_len); usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, _usbd_ctrl_buf, p_hid->report_len); @@ -389,16 +382,10 @@ tusb_error_t hidd_control_request_st(uint8_t rhport, tusb_control_request_t cons uint8_t const report_type = u16_high_u8(p_request->wValue); uint8_t const report_id = u16_low_u8(p_request->wValue); - // Composite interface need to determine it is Keyboard, Mouse or Gamepad - if ( report_id > 0 ) - { - - } - uint16_t xferlen; if ( p_hid->get_report_cb ) { - xferlen = p_hid->get_report_cb((hid_report_type_t) report_type, p_hid->report_buf, p_request->wLength); + xferlen = p_hid->get_report_cb(p_hid->report_id, (hid_report_type_t) report_type, p_hid->report_buf, p_request->wLength); }else { xferlen = p_request->wLength; @@ -411,11 +398,14 @@ tusb_error_t hidd_control_request_st(uint8_t rhport, tusb_control_request_t cons else if ( HID_REQ_CONTROL_SET_REPORT == p_request->bRequest ) { // wValue = Report Type | Report ID + uint8_t const report_type = u16_high_u8(p_request->wValue); + uint8_t const report_id = u16_low_u8(p_request->wValue); + usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, _usbd_ctrl_buf, p_request->wLength); if ( p_hid->set_report_cb ) { - p_hid->set_report_cb(u16_high_u8(p_request->wValue), _usbd_ctrl_buf, p_request->wLength); + p_hid->set_report_cb(report_id, (hid_report_type_t) report_type, _usbd_ctrl_buf, p_request->wLength); } } else if (HID_REQ_CONTROL_SET_IDLE == p_request->bRequest) diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 06c07d44..c78f56bb 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -55,8 +55,8 @@ bool tud_hid_generic_ready(void); bool tud_hid_generic_report(void); /*------------- Callbacks -------------*/ -ATTR_WEAK uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen); -ATTR_WEAK void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize); +ATTR_WEAK uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen); +ATTR_WEAK void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize); //--------------------------------------------------------------------+ // KEYBOARD API @@ -104,7 +104,7 @@ extern const hid_ascii_to_keycode_entry_t HID_ASCII_TO_KEYCODE[128]; * the completion of this control request will not be reported to application. * For Keyboard, USB host often uses this to turn on/off the LED for CAPLOCKS, NUMLOCK (\ref hid_keyboard_led_bm_t) */ -ATTR_WEAK uint16_t tud_hid_keyboard_get_report_cb(hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen); +ATTR_WEAK uint16_t tud_hid_keyboard_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen); /** Callback invoked when USB host request \ref HID_REQ_CONTROL_SET_REPORT. * \param[in] report_type specify which report (INPUT, OUTPUT, FEATURE) that host requests @@ -113,7 +113,7 @@ ATTR_WEAK uint16_t tud_hid_keyboard_get_report_cb(hid_report_type_t report_type, * \note By the time this callback is invoked, the USB control transfer is already completed in the hardware side. * Application are free to handle data at its own will. */ -ATTR_WEAK void tud_hid_keyboard_set_report_cb(hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize); +ATTR_WEAK void tud_hid_keyboard_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize); //ATTR_WEAK void tud_hid_keyboard_set_protocol_cb(bool boot_protocol); @@ -166,7 +166,7 @@ static inline bool tud_hid_mouse_button_release(void) * \note After this callback, the request is silently executed by the tinyusb stack, thus * the completion of this control request will not be reported to application */ -ATTR_WEAK uint16_t tud_hid_mouse_get_report_cb(hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen); +ATTR_WEAK uint16_t tud_hid_mouse_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen); /** * Callback function that is invoked when USB host request \ref HID_REQ_CONTROL_SET_REPORT. @@ -176,7 +176,7 @@ ATTR_WEAK uint16_t tud_hid_mouse_get_report_cb(hid_report_type_t report_type, ui * \note By the time this callback is invoked, the USB control transfer is already completed in the hardware side. * Application are free to handle data at its own will. */ -ATTR_WEAK void tud_hid_mouse_set_report_cb(hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize); +ATTR_WEAK void tud_hid_mouse_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize); //ATTR_WEAK void tud_hid_mouse_set_protocol_cb(bool boot_protocol);