From 307ba23046ff4c0795009c1426708facb37bdf82 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 18 Apr 2019 22:31:35 +0700 Subject: wrap up hid device refactor --- src/class/cdc/cdc_device.h | 48 +++++++++++------------ src/class/hid/hid_device.c | 1 + src/class/hid/hid_device.h | 94 +++++++++++++++++++++------------------------ src/class/msc/msc_device.h | 37 +++++++++--------- src/device/usbd.h | 7 ++++ src/device/usbd_auto_desc.c | 41 -------------------- src/tusb_option.h | 16 -------- 7 files changed, 94 insertions(+), 150 deletions(-) (limited to 'src') diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 23f4f609a..d7f89081c 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -94,17 +94,6 @@ ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char); ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts); ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding); -//--------------------------------------------------------------------+ -// INTERNAL USBD-CLASS DRIVER API -//--------------------------------------------------------------------+ -void cdcd_init (void); -bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); -bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * p_request); -bool cdcd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); -void cdcd_reset (uint8_t rhport); - - //--------------------------------------------------------------------+ // Interface Descriptor Template //--------------------------------------------------------------------+ @@ -116,31 +105,42 @@ void cdcd_reset (uint8_t rhport); // interface number, string index, EP notification address and size, EP data address (out,in) and size. #define TUD_CDC_DESCRIPTOR(_itfnum, _stridx, _ep_notif, _ep_notif_size, _epout, _epin, _epsize) \ /* Interface Associate */\ - 0x08, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 0x02, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, 0x00,\ + 8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, 0,\ /* CDC Control Interface */\ - 0x09, TUSB_DESC_INTERFACE, _itfnum, 0x00, 0x01, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, _stridx,\ + 9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, _stridx,\ /* CDC Header */\ - 0x05, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0120),\ + 5, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0120),\ /* CDC Call */\ - 0x05, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_CALL_MANAGEMENT, 0x00, (_itfnum) + 1,\ + 5, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_CALL_MANAGEMENT, 0, (_itfnum) + 1,\ /* CDC ACM: support line request */\ - 0x04, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, 0x02,\ + 4, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, 2,\ /* CDC Union */\ - 0x05, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_UNION, _itfnum, (_itfnum) + 1,\ + 5, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_UNION, _itfnum, (_itfnum) + 1,\ /* Endpoint Notification */\ - 0x07, TUSB_DESC_ENDPOINT, _ep_notif, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_notif_size), 0x10,\ + 7, TUSB_DESC_ENDPOINT, _ep_notif, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_notif_size), 16,\ /* CDC Data Interface */\ - 0x09, TUSB_DESC_INTERFACE, (_itfnum)+1, 0x00, 0x02, TUSB_CLASS_CDC_DATA, 0x00, 0x00, 0x00,\ + 9, TUSB_DESC_INTERFACE, (_itfnum)+1, 0, 2, TUSB_CLASS_CDC_DATA, 0, 0, 0,\ /* Endpoint Out */\ - 0x07, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00,\ + 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\ /* Endpoint In */\ - 0x07, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00 + 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0 + + +/** @} */ +/** @} */ + +//--------------------------------------------------------------------+ +// INTERNAL USBD-CLASS DRIVER API +//--------------------------------------------------------------------+ +void cdcd_init (void); +bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); +bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * p_request); +bool cdcd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); +bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); +void cdcd_reset (uint8_t rhport); #ifdef __cplusplus } #endif #endif /* _TUSB_CDC_DEVICE_H_ */ - -/** @} */ -/** @} */ diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 1a9e16105..ff572a0d2 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -147,6 +147,7 @@ bool tud_hid_keyboard_key_press(uint8_t report_id, char ch) //--------------------------------------------------------------------+ bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan) { + (void) pan; hid_mouse_report_t report = { .buttons = buttons, diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 07121ab17..c017a8227 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -42,55 +42,31 @@ #define CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP 0 #endif -#if !CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT -#error CFG_TUD_HID_KEYBOARD must be enabled -#endif - -#if !CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT -#error CFG_TUD_HID_MOUSE must be enabled -#endif - - //--------------------------------------------------------------------+ // Application API //--------------------------------------------------------------------+ -/** Check if the interface is ready to use - * \returns true if ready, otherwise interface may not be mounted or still busy transferring data - * \note Application must not perform any action if the interface is not ready - */ +// Check if the interface is ready to use bool tud_hid_ready(void); -bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len); // Check if current mode is Boot (true) or Report (false) bool tud_hid_boot_mode(void); +// Send report to host +bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len); + /*------------- Callbacks (Weak is optional) -------------*/ -/** Callback invoked when USB host request \ref HID_REQ_CONTROL_GET_REPORT. - * \param[in] report_type specify which report (INPUT, OUTPUT, FEATURE) that host requests - * \param[out] buffer data that application need to update, value must be accessible by USB controller (see \ref CFG_TUSB_MEM_SECTION) - * \param[in] reqlen number of bytes that host requested - * \retval non-zero Actual number of bytes in the response's buffer. - * \retval zero indicates the current request is not supported. Tinyusb device stack will reject the request by - * sending STALL in the data phase. - * \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. - * For Keyboard, USB host often uses this to turn on/off the LED for CAPLOCKS, NUMLOCK (\ref hid_keyboard_led_bm_t) - */ +// Invoked when receiving GET_REPORT control request +// Application must fill buffer report's content and return its length. +// Return zero will cause the stack to STALL request uint16_t tud_hid_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 - * \param[in] buffer containing the report's data - * \param[in] bufsize number of bytes in the \a buffer - * \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. - */ -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 void tud_hid_mode_changed_cb(uint8_t boot_mode); +// Invoked when receiving SET_REPORT control request +void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize); +// Invoked when host switch mode Boot <-> Report via SET_PROTOCOL request +void tud_hid_mode_changed_cb(uint8_t boot_mode) ATTR_WEAK; //--------------------------------------------------------------------+ // KEYBOARD API @@ -136,20 +112,38 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id) } //--------------------------------------------------------------------+ -// HID Report Descriptor Template +// Interface Descriptor Template //--------------------------------------------------------------------+ -/* These template should be used as follow - * - Only 1 report : no parameter - * uint8_t report_desc[] = { ID_REPORT_DESC_KEYBOARD() }; + +#define TUD_HID_DESC_LEN (9 + 9 + 7) + +#define TUD_HID_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epin, _epsize, _ep_interval) \ + /* Interface */\ + 9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_HID, (_boot_protocol) ? HID_SUBCLASS_BOOT : 0, _boot_protocol, _stridx,\ + /* HID descriptor */\ + 9, HID_DESC_TYPE_HID, U16_TO_U8S_LE(0x0111), 0, 1, HID_DESC_TYPE_REPORT, U16_TO_U8S_LE(_report_desc_len),\ + /* Endpoint descriptor */\ + 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval + +/* --------------------------------------------------------------------+ + * HID Report Descriptor Template + * + * Convenient for declaring popular HID device (keyboard, mouse, consumer, + * gamepad etc...). Templates take "HID_REPORT_ID(n)," as input, leave + * empty if multiple reports is not used + * + * - Only 1 report: no parameter + * uint8_t const report_desc[] = { HID_REPORT_DESC_KEYBOARD() }; * * - Multiple Reports: "HID_REPORT_ID(ID)," must be passed to template - * uint8_t report_desc[] = { - * ID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1) ,) , - * HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(2) ,) + * uint8_t const report_desc[] = + * { + * HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1), ) , + * HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(2), ) * }; - */ + *--------------------------------------------------------------------*/ -/*------------- Keyboard Descriptor Template -------------*/ +// Keyboard Report Descriptor Template #define HID_REPORT_DESC_KEYBOARD(...) \ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_KEYBOARD ) ,\ @@ -190,7 +184,7 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id) HID_OUTPUT ( HID_CONSTANT ) ,\ HID_COLLECTION_END \ -/*------------- Mouse Descriptor Template -------------*/ +// Mouse Report Descriptor Template #define HID_REPORT_DESC_MOUSE(...) \ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\ @@ -230,7 +224,7 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id) HID_COLLECTION_END ,\ HID_COLLECTION_END \ -//------------- Consumer Control Report Template -------------// +// Consumer Control Report Descriptor Template #define HID_REPORT_DESC_CONSUMER(...) \ HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ) ,\ HID_USAGE ( HID_USAGE_CONSUMER_CONTROL ) ,\ @@ -245,8 +239,8 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id) HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\ HID_COLLECTION_END \ -//------------- System Control Report Template -------------// -/* 0x00 - do nothing +/* System Control Report Descriptor Template + * 0x00 - do nothing * 0x01 - Power Off * 0x02 - Standby * 0x04 - Wake Host @@ -271,8 +265,8 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id) HID_INPUT ( HID_CONSTANT ) ,\ HID_COLLECTION_END \ -//------------- Gamepad Report Template -------------// -// Gamepad with 16 buttons and 2 joysticks +// Gamepad Report Descriptor Template +// with 16 buttons and 2 joysticks with following layout // | Button Map (2 bytes) | X | Y | Z | Rz #define HID_REPORT_DESC_GAMEPAD(...) \ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h index e7571ba60..b6cef78b6 100644 --- a/src/class/msc/msc_device.h +++ b/src/class/msc/msc_device.h @@ -62,24 +62,6 @@ TU_VERIFY_STATIC(CFG_TUD_MSC_BUFSIZE < UINT16_MAX, "Size is not correct"); #error CFG_TUD_MSC_PRODUCT_REV 4-byte string must be defined #endif - -//--------------------------------------------------------------------+ -// Interface Descriptor Template -//--------------------------------------------------------------------+ - -// Length of template descriptor: 23 bytes -#define TUD_MSC_DESC_LEN (9 + 7 + 7) - -// Interface Number, EP Out & EP In address -#define TUD_MSC_DESCRIPTOR(_itfnum, _stridx, _epout, _epin, _epsize) \ - /* Interface */\ - 0x09, TUSB_DESC_INTERFACE, _itfnum, 0x00, 0x02, TUSB_CLASS_MSC, MSC_SUBCLASS_SCSI, MSC_PROTOCOL_BOT, _stridx,\ - /* Endpoint Out */\ - 0x07, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00,\ - /* Endpoint In */\ - 0x07, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00 - - /** \addtogroup ClassDriver_MSC * @{ * \defgroup MSC_Device Device @@ -150,7 +132,7 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz */ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize); -/*------------- Optional callbacks : Could be used by application to free up resources -------------*/ +/*------------- Optional callbacks -------------*/ // Invoked when Read10 command is complete ATTR_WEAK void tud_msc_read10_complete_cb(uint8_t lun); @@ -164,6 +146,23 @@ ATTR_WEAK void tud_msc_scsi_complete_cb(uint8_t lun, uint8_t const scsi_cmd[16]) // Hook to make a mass storage device read-only. TODO remove ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun); +//--------------------------------------------------------------------+ +// Interface Descriptor Template +//--------------------------------------------------------------------+ + +// Length of template descriptor: 23 bytes +#define TUD_MSC_DESC_LEN (9 + 7 + 7) + +// Interface Number, EP Out & EP In address +#define TUD_MSC_DESCRIPTOR(_itfnum, _stridx, _epout, _epin, _epsize) \ + /* Interface */\ + 9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_MSC, MSC_SUBCLASS_SCSI, MSC_PROTOCOL_BOT, _stridx,\ + /* Endpoint Out */\ + 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\ + /* Endpoint In */\ + 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0 + + /** @} */ /** @} */ diff --git a/src/device/usbd.h b/src/device/usbd.h index f90c40aab..b3b66768c 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -91,6 +91,13 @@ ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en); // Invoked when usb bus is resumed ATTR_WEAK void tud_resume_cb(void); +//--------------------------------------------------------------------+ +// Interface Descriptor Template +//--------------------------------------------------------------------+ + +#define TUD_CONFIG_DESCRIPTOR(_itfcount, _stridx, _total_len, _attribute, _power_ma) \ + 9, TUSB_DESC_CONFIGURATION, U16_TO_U8S_LE(_total_len), _itfcount, 1, _stridx, TU_BIT(7) | _attribute, (_power_ma)/2 + #ifdef __cplusplus } #endif diff --git a/src/device/usbd_auto_desc.c b/src/device/usbd_auto_desc.c index ad90f7d70..b5a0ce4b2 100644 --- a/src/device/usbd_auto_desc.c +++ b/src/device/usbd_auto_desc.c @@ -473,47 +473,6 @@ desc_auto_cfg_t const _desc_auto_config_struct = }, #endif // boot keyboard - //------------- HID Mouse -------------// -#if CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT - .hid_mse_boot = - { - .itf = - { - .bLength = sizeof(tusb_desc_interface_t), - .bDescriptorType = TUSB_DESC_INTERFACE, - .bInterfaceNumber = ITF_NUM_HID_BOOT_MSE, - .bAlternateSetting = 0x00, - .bNumEndpoints = 1, - .bInterfaceClass = TUSB_CLASS_HID, - .bInterfaceSubClass = HID_SUBCLASS_BOOT, - .bInterfaceProtocol = HID_PROTOCOL_MOUSE, - .iInterface = 0 // 4 + CFG_TUD_CDC + CFG_TUD_MSC + CFG_TUD_HID_KEYBOARD - }, - - .hid_desc = - { - .bLength = sizeof(tusb_hid_descriptor_hid_t), - .bDescriptorType = HID_DESC_TYPE_HID, - .bcdHID = 0x0111, - .bCountryCode = HID_Local_NotSupported, - .bNumDescriptors = 1, - .bReportType = HID_DESC_TYPE_REPORT, - .wReportLength = sizeof(_desc_auto_hid_boot_mse_report) - }, - - .ep_in = - { - .bLength = sizeof(tusb_desc_endpoint_t), - .bDescriptorType = TUSB_DESC_ENDPOINT, - .bEndpointAddress = EP_HID_MSE_BOOT, - .bmAttributes = { .xfer = TUSB_XFER_INTERRUPT }, - .wMaxPacketSize = { .size = EP_HID_MSE_BOOT_SZ }, - .bInterval = 0x0A - }, - }, - -#endif // boot mouse - #if AUTO_DESC_HID_GENERIC //------------- HID Generic Multiple report -------------// .hid_generic = diff --git a/src/tusb_option.h b/src/tusb_option.h index 46d2186a0..f365e9c31 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -169,22 +169,6 @@ #define CFG_TUD_MSC 0 #endif - #ifndef CFG_TUD_HID_KEYBOARD - #define CFG_TUD_HID_KEYBOARD 0 - #endif - - #ifndef CFG_TUD_HID_MOUSE - #define CFG_TUD_HID_MOUSE 0 - #endif - - #ifndef CFG_TUD_HID_KEYBOARD_BOOT - #define CFG_TUD_HID_KEYBOARD_BOOT 0 - #endif - - #ifndef CFG_TUD_HID_MOUSE_BOOT - #define CFG_TUD_HID_MOUSE_BOOT 0 - #endif - #endif // TUSB_OPT_DEVICE_ENABLED //-------------------------------------------------------------------- -- cgit v1.3.1