From 8cffe4897eb114d65ff60685e111ff3e03d0fd74 Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 23 May 2021 13:56:32 +0700 Subject: [PATCH 1/3] change hid device internal boot_mode to protocol_mode --- src/class/hid/hid_device.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index ab9ef3ad..0d9cdb22 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -45,7 +45,7 @@ typedef struct uint8_t ep_out; // optional Out endpoint uint8_t itf_protocol; // Boot mouse or keyboard - bool boot_mode; // default = false (Report) + uint8_t protocol_mode; // Boot (0) or Report protocol (1) uint8_t idle_rate; // up to application to handle idle rate uint16_t report_desc_len; @@ -112,7 +112,7 @@ uint8_t tud_hid_n_interface_protocol(uint8_t instance) uint8_t tud_hid_n_get_protocol(uint8_t instance) { - return _hidd_itf[instance].boot_mode ? HID_PROTOCOL_BOOT : HID_PROTOCOL_REPORT; + return _hidd_itf[instance].protocol_mode; } bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modifier, uint8_t keycode[6]) @@ -213,7 +213,7 @@ uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint1 if ( desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT ) p_hid->itf_protocol = desc_itf->bInterfaceProtocol; - p_hid->boot_mode = false; // default mode is REPORT + p_hid->protocol_mode = HID_PROTOCOL_REPORT; // Per Specs: default is report mode p_hid->itf_num = desc_itf->bInterfaceNumber; // Use offsetof to avoid pointer to the odd/misaligned address @@ -326,8 +326,7 @@ bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t case HID_REQ_CONTROL_GET_PROTOCOL: if ( stage == CONTROL_STAGE_SETUP ) { - uint8_t protocol = (p_hid->boot_mode ? HID_PROTOCOL_BOOT : HID_PROTOCOL_REPORT); - tud_control_xfer(rhport, request, &protocol, 1); + tud_control_xfer(rhport, request, &p_hid->protocol_mode, 1); } break; @@ -338,10 +337,10 @@ bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t } else if ( stage == CONTROL_STAGE_ACK ) { - p_hid->boot_mode = (request->wValue == HID_PROTOCOL_BOOT); + p_hid->protocol_mode = (uint8_t) request->wValue; if (tud_hid_set_protocol_cb) { - tud_hid_set_protocol_cb(hid_itf, (uint8_t) request->wValue); + tud_hid_set_protocol_cb(hid_itf, p_hid->protocol_mode); } } break; From 3654d96e07f63e041f750aafd125c2c8bca27aa5 Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 23 May 2021 14:11:12 +0700 Subject: [PATCH 2/3] only invoke tuh_msc_umount_cb() if needed --- src/class/hid/hid_host.c | 2 +- src/class/msc/msc_host.c | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c index e3ae5a15..1bc86e4a 100644 --- a/src/class/hid/hid_host.c +++ b/src/class/hid/hid_host.c @@ -241,7 +241,7 @@ void hidh_close(uint8_t dev_addr) hidh_device_t* hid_dev = get_dev(dev_addr); if (tuh_hid_umount_cb) { - for ( uint8_t inst = 0; inst < hid_dev->inst_count; inst++) tuh_hid_umount_cb(dev_addr, inst); + for ( uint8_t inst = 0; inst < hid_dev->inst_count; inst++ ) tuh_hid_umount_cb(dev_addr, inst); } tu_memclr(hid_dev, sizeof(hidh_device_t)); diff --git a/src/class/msc/msc_host.c b/src/class/msc/msc_host.c index 08b4db9e..ca360ca2 100644 --- a/src/class/msc/msc_host.c +++ b/src/class/msc/msc_host.c @@ -297,10 +297,11 @@ void msch_init(void) void msch_close(uint8_t dev_addr) { msch_interface_t* p_msc = get_itf(dev_addr); - tu_memclr(p_msc, sizeof(msch_interface_t)); // invoke Application Callback - if (tuh_msc_umount_cb) tuh_msc_umount_cb(dev_addr); + if (p_msc->mounted && tuh_msc_umount_cb) tuh_msc_umount_cb(dev_addr); + + tu_memclr(p_msc, sizeof(msch_interface_t)); } bool msch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) From 4f033321189eb572c870d08281dd06e044c7597e Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 23 May 2021 20:14:01 +0700 Subject: [PATCH 3/3] clean up --- src/device/dcd.h | 6 ------ src/host/hcd.h | 44 ++++++++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/device/dcd.h b/src/device/dcd.h index 1e5b3ff1..71e88054 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -24,10 +24,6 @@ * This file is part of the TinyUSB stack. */ -/** \ingroup group_usbd - * \defgroup group_dcd Device Controller Driver (DCD) - * @{ */ - #ifndef _TUSB_DCD_H_ #define _TUSB_DCD_H_ @@ -168,5 +164,3 @@ extern void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t x #endif #endif /* _TUSB_DCD_H_ */ - -/// @} diff --git a/src/host/hcd.h b/src/host/hcd.h index a470a45e..ba6a9c5c 100644 --- a/src/host/hcd.h +++ b/src/host/hcd.h @@ -24,14 +24,10 @@ * This file is part of the TinyUSB stack. */ -/** \ingroup group_usbh - * \defgroup Group_HCD Host Controller Driver (HCD) - * @{ */ - #ifndef _TUSB_HCD_H_ #define _TUSB_HCD_H_ -#include +#include "common/tusb_common.h" #ifdef __cplusplus extern "C" { @@ -95,27 +91,46 @@ enum { #endif //--------------------------------------------------------------------+ -// Controller & Port API +// Controller API //--------------------------------------------------------------------+ + +// Initialize controller to host mode bool hcd_init(uint8_t rhport); + +// Interrupt Handler void hcd_int_handler(uint8_t rhport); + +// Enable USB interrupt void hcd_int_enable (uint8_t rhport); + +// Disable USB interrupt void hcd_int_disable(uint8_t rhport); // Get micro frame number (125 us) uint32_t hcd_uframe_number(uint8_t rhport); // Get frame number (1ms) -static inline uint32_t hcd_frame_number(uint8_t rhport) +TU_ATTR_ALWAYS_INLINE static inline +uint32_t hcd_frame_number(uint8_t rhport) { return hcd_uframe_number(rhport) >> 3; } -/// return the current connect status of roothub port -bool hcd_port_connect_status(uint8_t hostid); -void hcd_port_reset(uint8_t hostid); +//--------------------------------------------------------------------+ +// Port API +//--------------------------------------------------------------------+ + +// Get the current connect status of roothub port +bool hcd_port_connect_status(uint8_t rhport); + +// Reset USB bus on the port +void hcd_port_reset(uint8_t rhport); + +// TODO implement later void hcd_port_reset_end(uint8_t rhport); -tusb_speed_t hcd_port_speed_get(uint8_t hostid); + +// Get port link speed +tusb_speed_t hcd_port_speed_get(uint8_t rhport); // HCD closes all opened endpoints belong to this device void hcd_device_close(uint8_t rhport, uint8_t dev_addr); @@ -123,6 +138,7 @@ void hcd_device_close(uint8_t rhport, uint8_t dev_addr); //--------------------------------------------------------------------+ // Endpoints API //--------------------------------------------------------------------+ + bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]); bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc); @@ -134,14 +150,12 @@ bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr); bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen); //--------------------------------------------------------------------+ -// PIPE API +// PIPE API - TODO remove later //--------------------------------------------------------------------+ // TODO control xfer should be used via usbh layer bool hcd_pipe_queue_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t buffer[], uint16_t total_bytes); // only queue, not transferring yet bool hcd_pipe_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t buffer[], uint16_t total_bytes, bool int_on_complete); -// tusb_error_t hcd_pipe_cancel(); - //--------------------------------------------------------------------+ // Event API (implemented by stack) //--------------------------------------------------------------------+ @@ -163,5 +177,3 @@ extern void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t #endif #endif /* _TUSB_HCD_H_ */ - -/// @}