summaryrefslogtreecommitdiff
path: root/src/host
diff options
context:
space:
mode:
authorHa Thach <[email protected]>2025-03-27 10:47:10 +0700
committerGitHub <[email protected]>2025-03-27 10:47:10 +0700
commit8c1802e41d37c915334a19b859b24cb2a1b48ee5 (patch)
treed159c6f13f7c4815622b83fb736377d1c090e4d9 /src/host
parentcf76af1056e9a3019d9b08503b783a86602374f0 (diff)
parent5531de4f2c7e725b95f23083aad7d68d0d95b586 (diff)
Merge pull request #3043 from hathach/hcd-close-ep
feat(host) add endpoint close API and feat(HIL) improvement
Diffstat (limited to 'src/host')
-rw-r--r--src/host/hcd.h3
-rw-r--r--src/host/usbh.c83
-rw-r--r--src/host/usbh.h23
3 files changed, 68 insertions, 41 deletions
diff --git a/src/host/hcd.h b/src/host/hcd.h
index 56b6fdb5d..aa9e9cf3b 100644
--- a/src/host/hcd.h
+++ b/src/host/hcd.h
@@ -165,6 +165,9 @@ void hcd_device_close(uint8_t rhport, uint8_t dev_addr);
// Open an endpoint
bool hcd_edpt_open(uint8_t rhport, uint8_t daddr, tusb_desc_endpoint_t const * ep_desc);
+// Close an endpoint
+bool hcd_edpt_close(uint8_t rhport, uint8_t daddr, uint8_t ep_addr);
+
// Submit a transfer, when complete hcd_event_xfer_complete() must be invoked
bool hcd_edpt_xfer(uint8_t rhport, uint8_t daddr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen);
diff --git a/src/host/usbh.c b/src/host/usbh.c
index c87f058cd..e60db53da 100644
--- a/src/host/usbh.c
+++ b/src/host/usbh.c
@@ -52,21 +52,25 @@ enum {
// Weak stubs: invoked if no strong implementation is available
//--------------------------------------------------------------------+
TU_ATTR_WEAK bool hcd_deinit(uint8_t rhport) {
- (void) rhport;
- return false;
+ (void) rhport; return false;
}
TU_ATTR_WEAK bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param) {
- (void) rhport;
- (void) cfg_id;
- (void) cfg_param;
+ (void) rhport; (void) cfg_id; (void) cfg_param;
return false;
}
+TU_ATTR_WEAK void tuh_enum_descriptor_device_cb(uint8_t daddr, const tusb_desc_device_t *desc_device) {
+ (void) daddr; (void) desc_device;
+}
+
+TU_ATTR_WEAK bool tuh_enum_descriptor_configuration_cb(uint8_t daddr, uint8_t cfg_index, const tusb_desc_configuration_t *desc_config) {
+ (void) daddr; (void) cfg_index; (void) desc_config;
+ return true;
+}
+
TU_ATTR_WEAK void tuh_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr) {
- (void) rhport;
- (void) eventid;
- (void) in_isr;
+ (void) rhport; (void) eventid; (void) in_isr;
}
TU_ATTR_WEAK bool hcd_dcache_clean(const void* addr, uint32_t data_size) {
@@ -126,6 +130,7 @@ typedef struct {
uint8_t i_manufacturer;
uint8_t i_product;
uint8_t i_serial;
+ uint8_t bNumConfigurations;
// Configuration Descriptor
// uint8_t interface_count; // bNumInterfaces alias
@@ -230,7 +235,6 @@ static usbh_class_driver_t const usbh_class_drivers[] = {
};
enum { BUILTIN_DRIVER_COUNT = TU_ARRAY_SIZE(usbh_class_drivers) };
-enum { CONFIG_NUM = 1 }; // default to use configuration 1
// Additional class drivers implemented by application
tu_static usbh_class_driver_t const * _app_driver = NULL;
@@ -516,7 +520,7 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
// Loop until there is no more events in the queue
while (1) {
hcd_event_t event;
- if (!osal_queue_receive(_usbh_q, &event, timeout_ms)) return;
+ if (!osal_queue_receive(_usbh_q, &event, timeout_ms)) { return; }
switch (event.event_id) {
case HCD_EVENT_DEVICE_ATTACH:
@@ -990,6 +994,12 @@ bool tuh_edpt_open(uint8_t dev_addr, tusb_desc_endpoint_t const* desc_ep) {
return hcd_edpt_open(usbh_get_rhport(dev_addr), dev_addr, desc_ep);
}
+bool tuh_edpt_close(uint8_t daddr, uint8_t ep_addr) {
+ TU_VERIFY(0 != tu_edpt_number(ep_addr)); // cannot close EP0
+ tuh_edpt_abort_xfer(daddr, ep_addr); // abort any pending transfer
+ return hcd_edpt_close(usbh_get_rhport(daddr), daddr, ep_addr);
+}
+
bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) {
usbh_device_t* dev = get_device(dev_addr);
TU_VERIFY(dev);
@@ -1372,8 +1382,8 @@ enum {
ENUM_CONFIG_DRIVER
};
-static bool enum_request_set_addr(void);
-static bool _parse_configuration_descriptor (uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg);
+static bool enum_request_set_addr(tusb_desc_device_t const* desc_device);
+static bool enum_parse_configuration_desc (uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg);
static void enum_full_complete(void);
// process device enumeration
@@ -1489,7 +1499,7 @@ static void process_enumeration(tuh_xfer_t* xfer) {
#endif
case ENUM_SET_ADDR:
- enum_request_set_addr();
+ enum_request_set_addr((tusb_desc_device_t*) _usbh_epbuf.ctrl);
break;
case ENUM_GET_DEVICE_DESC: {
@@ -1523,6 +1533,9 @@ static void process_enumeration(tuh_xfer_t* xfer) {
dev->i_manufacturer = desc_device->iManufacturer;
dev->i_product = desc_device->iProduct;
dev->i_serial = desc_device->iSerialNumber;
+ dev->bNumConfigurations = desc_device->bNumConfigurations;
+
+ tuh_enum_descriptor_device_cb(daddr, desc_device); // callback
tuh_descriptor_get_string_langid(daddr, _usbh_epbuf.ctrl, CFG_TUH_ENUMERATION_BUFSIZE,
process_enumeration, ENUM_GET_STRING_MANUFACTURER);
@@ -1574,8 +1587,8 @@ static void process_enumeration(tuh_xfer_t* xfer) {
case ENUM_GET_9BYTE_CONFIG_DESC: {
// Get 9-byte for total length
- uint8_t const config_idx = CONFIG_NUM - 1;
- TU_LOG_USBH("Get Configuration[0] Descriptor (9 bytes)\r\n");
+ uint8_t const config_idx = 0;
+ TU_LOG_USBH("Get Configuration[%u] Descriptor (9 bytes)\r\n", config_idx);
TU_ASSERT(tuh_descriptor_get_configuration(daddr, config_idx, _usbh_epbuf.ctrl, 9,
process_enumeration, ENUM_GET_FULL_CONFIG_DESC),);
break;
@@ -1585,25 +1598,32 @@ static void process_enumeration(tuh_xfer_t* xfer) {
uint8_t const* desc_config = _usbh_epbuf.ctrl;
// Use offsetof to avoid pointer to the odd/misaligned address
- uint16_t const total_len = tu_le16toh(
- tu_unaligned_read16(desc_config + offsetof(tusb_desc_configuration_t, wTotalLength)));
+ uint16_t const total_len = tu_le16toh(tu_unaligned_read16(desc_config + offsetof(tusb_desc_configuration_t, wTotalLength)));
// TODO not enough buffer to hold configuration descriptor
TU_ASSERT(total_len <= CFG_TUH_ENUMERATION_BUFSIZE,);
// Get full configuration descriptor
- uint8_t const config_idx = CONFIG_NUM - 1;
- TU_LOG_USBH("Get Configuration[0] Descriptor\r\n");
+ uint8_t const config_idx = (uint8_t) tu_le16toh(xfer->setup->wIndex);
+ TU_LOG_USBH("Get Configuration[%u] Descriptor\r\n", config_idx);
TU_ASSERT(tuh_descriptor_get_configuration(daddr, config_idx, _usbh_epbuf.ctrl, total_len,
process_enumeration, ENUM_SET_CONFIG),);
break;
}
- case ENUM_SET_CONFIG:
- // tuh_desc_configuration_cb(daddr, CONFIG_NUM-1, (const tusb_desc_configuration_t*) _usbh_epbuf.ctrl);
-
- TU_ASSERT(tuh_configuration_set(daddr, CONFIG_NUM, process_enumeration, ENUM_CONFIG_DRIVER),);
+ case ENUM_SET_CONFIG: {
+ uint8_t config_idx = (uint8_t) tu_le16toh(xfer->setup->wIndex);
+ if (tuh_enum_descriptor_configuration_cb(daddr, config_idx, (const tusb_desc_configuration_t*) _usbh_epbuf.ctrl)) {
+ TU_ASSERT(tuh_configuration_set(daddr, config_idx+1, process_enumeration, ENUM_CONFIG_DRIVER),);
+ } else {
+ config_idx++;
+ TU_ASSERT(config_idx < dev->bNumConfigurations,);
+ TU_LOG_USBH("Get Configuration[%u] Descriptor (9 bytes)\r\n", config_idx);
+ TU_ASSERT(tuh_descriptor_get_configuration(daddr, config_idx, _usbh_epbuf.ctrl, 9,
+ process_enumeration, ENUM_GET_FULL_CONFIG_DESC),);
+ }
break;
+ }
case ENUM_CONFIG_DRIVER: {
TU_LOG_USBH("Device configured\r\n");
@@ -1613,7 +1633,7 @@ static void process_enumeration(tuh_xfer_t* xfer) {
// Parse configuration & set up drivers
// driver_open() must not make any usb transfer
- TU_ASSERT(_parse_configuration_descriptor(daddr, (tusb_desc_configuration_t*) _usbh_epbuf.ctrl),);
+ TU_ASSERT(enum_parse_configuration_desc(daddr, (tusb_desc_configuration_t*) _usbh_epbuf.ctrl),);
// Start the Set Configuration process for interfaces (itf = TUSB_INDEX_INVALID_8)
// Since driver can perform control transfer within its set_config, this is done asynchronously.
@@ -1624,14 +1644,11 @@ static void process_enumeration(tuh_xfer_t* xfer) {
}
default:
- // stop enumeration if unknown state
- enum_full_complete();
+ enum_full_complete(); // stop enumeration if unknown state
break;
}
}
-
-
static bool enum_new_device(hcd_event_t* event) {
_dev0.rhport = event->rhport;
_dev0.hub_addr = event->connection.hub_addr;
@@ -1701,9 +1718,7 @@ static uint8_t get_new_address(bool is_hub) {
return 0; // invalid address
}
-static bool enum_request_set_addr(void) {
- tusb_desc_device_t const* desc_device = (tusb_desc_device_t const*) _usbh_epbuf.ctrl;
-
+static bool enum_request_set_addr(tusb_desc_device_t const* desc_device) {
// Get new address
uint8_t const new_addr = get_new_address(desc_device->bDeviceClass == TUSB_CLASS_HUB);
TU_ASSERT(new_addr != 0);
@@ -1741,7 +1756,7 @@ static bool enum_request_set_addr(void) {
return true;
}
-static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg) {
+static bool enum_parse_configuration_desc(uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg) {
usbh_device_t* dev = get_device(dev_addr);
uint16_t const total_len = tu_le16toh(desc_cfg->wTotalLength);
uint8_t const* desc_end = ((uint8_t const*) desc_cfg) + total_len;
@@ -1858,7 +1873,9 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num) {
TU_LOG_USBH("HUB address = %u is mounted\r\n", dev_addr);
}else {
// Invoke callback if available
- if (tuh_mount_cb) tuh_mount_cb(dev_addr);
+ if (tuh_mount_cb) {
+ tuh_mount_cb(dev_addr);
+ }
}
}
}
diff --git a/src/host/usbh.h b/src/host/usbh.h
index 52b4f478c..4829a8183 100644
--- a/src/host/usbh.h
+++ b/src/host/usbh.h
@@ -33,6 +33,10 @@
#include "common/tusb_common.h"
+#if CFG_TUH_MAX3421
+#include "portable/analog/max3421/hcd_max3421.h"
+#endif
+
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
@@ -100,10 +104,13 @@ typedef union {
//--------------------------------------------------------------------+
// Invoked when enumeration get device descriptor
-// TU_ATTR_WEAK void tuh_descriptor_device_cb(uint8_t daddr, const tusb_desc_device_t *desc_device);
+// Device is not ready to communicate yet, application can copy the descriptor if needed
+void tuh_enum_descriptor_device_cb(uint8_t daddr, const tusb_desc_device_t *desc_device);
// Invoked when enumeration get configuration descriptor
-// TU_ATTR_WEAK void tuh_desc_configuration_cb(uint8_t daddr, uint8_t cfg_index, const tusb_desc_configuration_t *desc_config);
+// For multi-configuration device return false to skip, true to proceed with this configuration (may not be implemented yet)
+// Device is not ready to communicate yet, application can copy the descriptor if needed
+bool tuh_enum_descriptor_configuration_cb(uint8_t daddr, uint8_t cfg_index, const tusb_desc_configuration_t *desc_config);
// Invoked when a device is mounted (configured)
TU_ATTR_WEAK void tuh_mount_cb (uint8_t daddr);
@@ -155,8 +162,7 @@ bool tuh_inited(void);
void tuh_task_ext(uint32_t timeout_ms, bool in_isr);
// Task function should be called in main/rtos loop
-TU_ATTR_ALWAYS_INLINE static inline
-void tuh_task(void) {
+TU_ATTR_ALWAYS_INLINE static inline void tuh_task(void) {
tuh_task_ext(UINT32_MAX, false);
}
@@ -197,16 +203,14 @@ bool tuh_mounted(uint8_t daddr);
bool tuh_connected(uint8_t daddr);
// Check if device is suspended
-TU_ATTR_ALWAYS_INLINE static inline
-bool tuh_suspended(uint8_t daddr) {
+TU_ATTR_ALWAYS_INLINE static inline bool tuh_suspended(uint8_t daddr) {
// TODO implement suspend & resume on host
(void) daddr;
return false;
}
// Check if device is ready to communicate with
-TU_ATTR_ALWAYS_INLINE static inline
-bool tuh_ready(uint8_t daddr) {
+TU_ATTR_ALWAYS_INLINE static inline bool tuh_ready(uint8_t daddr) {
return tuh_mounted(daddr) && !tuh_suspended(daddr);
}
@@ -227,6 +231,9 @@ bool tuh_edpt_xfer(tuh_xfer_t* xfer);
// Open a non-control endpoint
bool tuh_edpt_open(uint8_t daddr, tusb_desc_endpoint_t const * desc_ep);
+// Close a non-control endpoint, it will abort any pending transfer
+bool tuh_edpt_close(uint8_t daddr, uint8_t ep_addr);
+
// Abort a queued transfer. Note: it can only abort transfer that has not been started
// Return true if a queued transfer is aborted, false if there is no transfer to abort
bool tuh_edpt_abort_xfer(uint8_t daddr, uint8_t ep_addr);