summaryrefslogtreecommitdiff
path: root/src/host
diff options
context:
space:
mode:
authorHiFiPhile <[email protected]>2025-04-18 11:13:15 +0200
committerHiFiPhile <[email protected]>2025-04-18 11:13:15 +0200
commitb6abc9022a4beae98cc206d8fbeefb4700deef2f (patch)
treebdd12bb710caa9d161edc1488a37911de0931a3d /src/host
parent713410997326269b4072dce906933f8f44fd0efe (diff)
parent865e3488f99209c57b73953428dccf2bc666f0be (diff)
Merge remote-tracking branch 'upstream/master' into hcd-template-comments
Signed-off-by: HiFiPhile <[email protected]>
Diffstat (limited to 'src/host')
-rw-r--r--src/host/hcd.h29
-rw-r--r--src/host/hub.c386
-rw-r--r--src/host/hub.h184
-rw-r--r--src/host/usbh.c627
-rw-r--r--src/host/usbh.h71
-rw-r--r--src/host/usbh_pvt.h8
6 files changed, 727 insertions, 578 deletions
diff --git a/src/host/hcd.h b/src/host/hcd.h
index 5547c7cc5..b20d96d54 100644
--- a/src/host/hcd.h
+++ b/src/host/hcd.h
@@ -53,26 +53,21 @@
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
-typedef enum
-{
+typedef enum {
HCD_EVENT_DEVICE_ATTACH,
HCD_EVENT_DEVICE_REMOVE,
HCD_EVENT_XFER_COMPLETE,
- // Not an HCD event, just a convenient way to defer ISR function
- USBH_EVENT_FUNC_CALL,
-
+ USBH_EVENT_FUNC_CALL, // Not an HCD event
HCD_EVENT_COUNT
} hcd_eventid_t;
-typedef struct
-{
+typedef struct {
uint8_t rhport;
uint8_t event_id;
uint8_t dev_addr;
- union
- {
+ union {
// Attach, Remove
struct {
uint8_t hub_addr;
@@ -93,11 +88,9 @@ typedef struct
void* param;
}func_call;
};
-
} hcd_event_t;
-typedef struct
-{
+typedef struct {
uint8_t rhport;
uint8_t hub_addr;
uint8_t hub_port;
@@ -110,15 +103,15 @@ typedef struct
// clean/flush data cache: write cache -> memory.
// Required before an DMA TX transfer to make sure data is in memory
-bool hcd_dcache_clean(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
+bool hcd_dcache_clean(void const* addr, uint32_t data_size);
// invalidate data cache: mark cache as invalid, next read will read from memory
// Required BOTH before and after an DMA RX transfer
-bool hcd_dcache_invalidate(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
+bool hcd_dcache_invalidate(void const* addr, uint32_t data_size);
// clean and invalidate data cache
// Required before an DMA transfer where memory is both read/write by DMA
-bool hcd_dcache_clean_invalidate(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
+bool hcd_dcache_clean_invalidate(void const* addr, uint32_t data_size);
//--------------------------------------------------------------------+
// Controller API
@@ -128,7 +121,7 @@ bool hcd_dcache_clean_invalidate(void const* addr, uint32_t data_size) TU_ATTR_W
bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param);
// Initialize controller to host mode
-bool hcd_init(uint8_t rhport);
+bool hcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init);
// De-initialize controller
bool hcd_deinit(uint8_t rhport);
@@ -170,8 +163,12 @@ void hcd_device_close(uint8_t rhport, uint8_t dev_addr);
//--------------------------------------------------------------------+
// Open an endpoint
+// return true if successfully opened or endpoint is currently opened
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/hub.c b/src/host/hub.c
index e97014443..61efa8ba5 100644
--- a/src/host/hub.c
+++ b/src/host/hub.c
@@ -40,29 +40,36 @@
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
-typedef struct
-{
+typedef struct {
uint8_t itf_num;
uint8_t ep_in;
- uint8_t port_count;
- CFG_TUH_MEM_ALIGN uint8_t status_change;
- CFG_TUH_MEM_ALIGN hub_port_status_response_t port_status;
- CFG_TUH_MEM_ALIGN hub_status_response_t hub_status;
+ // from hub descriptor
+ uint8_t bNbrPorts;
+ uint8_t bPwrOn2PwrGood_2ms; // port power on to good, in 2ms unit
+ // uint16_t wHubCharacteristics;
+
+ hub_port_status_response_t port_status;
} hub_interface_t;
-CFG_TUH_MEM_SECTION static hub_interface_t hub_data[CFG_TUH_HUB];
-CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN static uint8_t _hub_buffer[sizeof(descriptor_hub_desc_t)];
+typedef struct {
+ TUH_EPBUF_DEF(status_change, 4); // interrupt endpoint
+ TUH_EPBUF_DEF(ctrl_buf, CFG_TUH_HUB_BUFSIZE);
+} hub_epbuf_t;
+
+static hub_interface_t hub_itfs[CFG_TUH_HUB];
+CFG_TUH_MEM_SECTION static hub_epbuf_t hub_epbufs[CFG_TUH_HUB];
+
+TU_ATTR_ALWAYS_INLINE static inline hub_interface_t* get_hub_itf(uint8_t daddr) {
+ return &hub_itfs[daddr-1-CFG_TUH_DEVICE_MAX];
+}
-TU_ATTR_ALWAYS_INLINE
-static inline hub_interface_t* get_itf(uint8_t dev_addr)
-{
- return &hub_data[dev_addr-1-CFG_TUH_DEVICE_MAX];
+TU_ATTR_ALWAYS_INLINE static inline hub_epbuf_t* get_hub_epbuf(uint8_t daddr) {
+ return &hub_epbufs[daddr-1-CFG_TUH_DEVICE_MAX];
}
-#if CFG_TUSB_DEBUG >= 2
-static char const* const _hub_feature_str[] =
-{
+#if CFG_TUSB_DEBUG >= HUB_DEBUG
+static char const* const _hub_feature_str[] = {
[HUB_FEATURE_PORT_CONNECTION ] = "PORT_CONNECTION",
[HUB_FEATURE_PORT_ENABLE ] = "PORT_ENABLE",
[HUB_FEATURE_PORT_SUSPEND ] = "PORT_SUSPEND",
@@ -84,12 +91,9 @@ static char const* const _hub_feature_str[] =
// HUB
//--------------------------------------------------------------------+
bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
- tuh_xfer_cb_t complete_cb, uintptr_t user_data)
-{
- tusb_control_request_t const request =
- {
- .bmRequestType_bit =
- {
+ tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
+ tusb_control_request_t const request = {
+ .bmRequestType_bit = {
.recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER,
.type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_OUT
@@ -100,8 +104,7 @@ bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
.wLength = 0
};
- tuh_xfer_t xfer =
- {
+ tuh_xfer_t xfer = {
.daddr = hub_addr,
.ep_addr = 0,
.setup = &request,
@@ -110,18 +113,15 @@ bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
.user_data = user_data
};
- TU_LOG2("HUB Clear Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port);
- TU_ASSERT( tuh_control_xfer(&xfer) );
+ TU_LOG_DRV("HUB Clear Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port);
+ TU_ASSERT(tuh_control_xfer(&xfer));
return true;
}
bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
- tuh_xfer_cb_t complete_cb, uintptr_t user_data)
-{
- tusb_control_request_t const request =
- {
- .bmRequestType_bit =
- {
+ tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
+ tusb_control_request_t const request = {
+ .bmRequestType_bit = {
.recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER,
.type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_OUT
@@ -132,8 +132,7 @@ bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
.wLength = 0
};
- tuh_xfer_t xfer =
- {
+ tuh_xfer_t xfer = {
.daddr = hub_addr,
.ep_addr = 0,
.setup = &request,
@@ -142,18 +141,15 @@ bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
.user_data = user_data
};
- TU_LOG2("HUB Set Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port);
+ TU_LOG_DRV("HUB Set Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port);
TU_ASSERT( tuh_control_xfer(&xfer) );
return true;
}
bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp,
- tuh_xfer_cb_t complete_cb, uintptr_t user_data)
-{
- tusb_control_request_t const request =
- {
- .bmRequestType_bit =
- {
+ tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
+ tusb_control_request_t const request = {
+ .bmRequestType_bit = {
.recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER,
.type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_IN
@@ -161,11 +157,10 @@ bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp,
.bRequest = HUB_REQUEST_GET_STATUS,
.wValue = 0,
.wIndex = hub_port,
- .wLength = 4
+ .wLength = tu_htole16(4)
};
- tuh_xfer_t xfer =
- {
+ tuh_xfer_t xfer = {
.daddr = hub_addr,
.ep_addr = 0,
.setup = &request,
@@ -174,7 +169,7 @@ bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp,
.user_data = user_data
};
- TU_LOG2("HUB Get Port Status: addr = %u port = %u\r\n", hub_addr, hub_port);
+ TU_LOG_DRV("HUB Get Port Status: addr = %u port = %u\r\n", hub_addr, hub_port);
TU_VERIFY( tuh_control_xfer(&xfer) );
return true;
}
@@ -183,7 +178,7 @@ bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp,
// CLASS-USBH API (don't require to verify parameters)
//--------------------------------------------------------------------+
bool hub_init(void) {
- tu_memclr(hub_data, sizeof(hub_data));
+ tu_memclr(hub_itfs, sizeof(hub_itfs));
return true;
}
@@ -191,40 +186,32 @@ bool hub_deinit(void) {
return true;
}
-bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
-{
+bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len) {
(void) rhport;
TU_VERIFY(TUSB_CLASS_HUB == itf_desc->bInterfaceClass &&
0 == itf_desc->bInterfaceSubClass);
+ TU_VERIFY(itf_desc->bInterfaceProtocol <= 1); // not support multiple TT yet
- // hub driver does not support multiple TT yet
- TU_VERIFY(itf_desc->bInterfaceProtocol <= 1);
-
- // msc driver length is fixed
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_desc_endpoint_t);
TU_ASSERT(drv_len <= max_len);
- //------------- Interrupt Status endpoint -------------//
+ // Interrupt Status endpoint
tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc);
-
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType &&
TUSB_XFER_INTERRUPT == desc_ep->bmAttributes.xfer, 0);
-
TU_ASSERT(tuh_edpt_open(dev_addr, desc_ep));
- hub_interface_t* p_hub = get_itf(dev_addr);
-
+ hub_interface_t* p_hub = get_hub_itf(dev_addr);
p_hub->itf_num = itf_desc->bInterfaceNumber;
p_hub->ep_in = desc_ep->bEndpointAddress;
return true;
}
-void hub_close(uint8_t dev_addr)
-{
+void hub_close(uint8_t dev_addr) {
TU_VERIFY(dev_addr > CFG_TUH_DEVICE_MAX, );
- hub_interface_t* p_hub = get_itf(dev_addr);
+ hub_interface_t* p_hub = get_hub_itf(dev_addr);
if (p_hub->ep_in) {
TU_LOG_DRV(" HUB close addr = %d\r\n", dev_addr);
@@ -232,30 +219,33 @@ void hub_close(uint8_t dev_addr)
}
}
-bool hub_edpt_status_xfer(uint8_t dev_addr)
-{
- hub_interface_t* hub_itf = get_itf(dev_addr);
- return usbh_edpt_xfer(dev_addr, hub_itf->ep_in, &hub_itf->status_change, 1);
-}
+bool hub_edpt_status_xfer(uint8_t daddr) {
+ hub_interface_t* p_hub = get_hub_itf(daddr);
+ hub_epbuf_t* p_epbuf = get_hub_epbuf(daddr);
+
+ TU_VERIFY(usbh_edpt_claim(daddr, p_hub->ep_in));
+ if (!usbh_edpt_xfer(daddr, p_hub->ep_in, p_epbuf->status_change, 1)) {
+ usbh_edpt_release(daddr, p_hub->ep_in);
+ return false;
+ }
+ return true;
+}
//--------------------------------------------------------------------+
// Set Configure
//--------------------------------------------------------------------+
-
static void config_set_port_power (tuh_xfer_t* xfer);
static void config_port_power_complete (tuh_xfer_t* xfer);
-bool hub_set_config(uint8_t dev_addr, uint8_t itf_num)
-{
- hub_interface_t* p_hub = get_itf(dev_addr);
+bool hub_set_config(uint8_t dev_addr, uint8_t itf_num) {
+ hub_interface_t* p_hub = get_hub_itf(dev_addr);
TU_ASSERT(itf_num == p_hub->itf_num);
+ hub_epbuf_t* p_epbuf = get_hub_epbuf(dev_addr);
// Get Hub Descriptor
- tusb_control_request_t const request =
- {
- .bmRequestType_bit =
- {
+ tusb_control_request_t const request = {
+ .bmRequestType_bit = {
.recipient = TUSB_REQ_RCPT_DEVICE,
.type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_IN
@@ -263,34 +253,33 @@ bool hub_set_config(uint8_t dev_addr, uint8_t itf_num)
.bRequest = HUB_REQUEST_GET_DESCRIPTOR,
.wValue = 0,
.wIndex = 0,
- .wLength = sizeof(descriptor_hub_desc_t)
+ .wLength = sizeof(hub_desc_cs_t)
};
- tuh_xfer_t xfer =
- {
+ tuh_xfer_t xfer = {
.daddr = dev_addr,
.ep_addr = 0,
.setup = &request,
- .buffer = _hub_buffer,
+ .buffer = p_epbuf->ctrl_buf,
.complete_cb = config_set_port_power,
.user_data = 0
};
- TU_ASSERT( tuh_control_xfer(&xfer) );
-
+ TU_ASSERT(tuh_control_xfer(&xfer));
return true;
}
-static void config_set_port_power (tuh_xfer_t* xfer)
-{
+static void config_set_port_power (tuh_xfer_t* xfer) {
TU_ASSERT(XFER_RESULT_SUCCESS == xfer->result, );
uint8_t const daddr = xfer->daddr;
- hub_interface_t* p_hub = get_itf(daddr);
+ hub_interface_t* p_hub = get_hub_itf(daddr);
+ hub_epbuf_t* p_epbuf = get_hub_epbuf(daddr);
// only use number of ports in hub descriptor
- descriptor_hub_desc_t const* desc_hub = (descriptor_hub_desc_t const*) _hub_buffer;
- p_hub->port_count = desc_hub->bNbrPorts;
+ hub_desc_cs_t const* desc_hub = (hub_desc_cs_t const*) p_epbuf->ctrl_buf;
+ p_hub->bNbrPorts = desc_hub->bNbrPorts;
+ p_hub->bPwrOn2PwrGood_2ms = desc_hub->bPwrOn2PwrGood;
// May need to GET_STATUS
@@ -299,22 +288,21 @@ static void config_set_port_power (tuh_xfer_t* xfer)
hub_port_set_feature(daddr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete, 0);
}
-static void config_port_power_complete (tuh_xfer_t* xfer)
-{
+static void config_port_power_complete (tuh_xfer_t* xfer) {
TU_ASSERT(XFER_RESULT_SUCCESS == xfer->result, );
uint8_t const daddr = xfer->daddr;
- hub_interface_t* p_hub = get_itf(daddr);
+ hub_interface_t* p_hub = get_hub_itf(daddr);
- if (xfer->setup->wIndex == p_hub->port_count)
- {
+ if (xfer->setup->wIndex == p_hub->bNbrPorts) {
// All ports are power -> queue notification status endpoint and
// complete the SET CONFIGURATION
- TU_ASSERT( usbh_edpt_xfer(daddr, p_hub->ep_in, &p_hub->status_change, 1), );
-
+ if (!hub_edpt_status_xfer(daddr)) {
+ TU_MESS_FAILED();
+ TU_BREAKPOINT();
+ }
usbh_driver_set_config_complete(daddr, p_hub->itf_num);
- }else
- {
+ } else {
// power next port
uint8_t const hub_port = (uint8_t) (xfer->setup->wIndex + 1);
hub_port_set_feature(daddr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete, 0);
@@ -324,180 +312,162 @@ static void config_port_power_complete (tuh_xfer_t* xfer)
//--------------------------------------------------------------------+
// Connection Changes
//--------------------------------------------------------------------+
-
-static void hub_port_get_status_complete (tuh_xfer_t* xfer);
-static void hub_get_status_complete (tuh_xfer_t* xfer);
+static void get_status_complete (tuh_xfer_t* xfer);
+static void port_get_status_complete (tuh_xfer_t* xfer);
+static void port_clear_feature_complete_stub(tuh_xfer_t* xfer);
static void connection_clear_conn_change_complete (tuh_xfer_t* xfer);
static void connection_port_reset_complete (tuh_xfer_t* xfer);
// callback as response of interrupt endpoint polling
-bool hub_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
- (void) xferred_bytes; // TODO can be more than 1 for hub with lots of ports
+bool hub_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
+ (void) xferred_bytes;
(void) ep_addr;
- TU_VERIFY(result == XFER_RESULT_SUCCESS);
- hub_interface_t* p_hub = get_itf(dev_addr);
+ bool processed = false; // true if new status is processed
- uint8_t const status_change = p_hub->status_change;
- TU_LOG2(" Hub Status Change = 0x%02X\r\n", status_change);
-
- if ( status_change == 0 ) {
- // The status change event was neither for the hub, nor for any of its ports.
- // This shouldn't happen, but it does with some devices.
- // Initiate the next interrupt poll here.
- return hub_edpt_status_xfer(dev_addr);
- }
+ if (result == XFER_RESULT_SUCCESS) {
+ hub_interface_t* p_hub = get_hub_itf(daddr);
+ hub_epbuf_t *p_epbuf = get_hub_epbuf(daddr);
+ const uint8_t status_change = p_epbuf->status_change[0];
+ TU_LOG_DRV(" Hub Status Change = 0x%02X\r\n", status_change);
- if (tu_bit_test(status_change, 0)) {
- // Hub bit 0 is for the hub device events
- if (hub_port_get_status(dev_addr, 0, &p_hub->hub_status, hub_get_status_complete, 0) == false) {
- //Hub status control transfer failed, retry
- hub_edpt_status_xfer(dev_addr);
- }
- }
- else {
- // Hub bits 1 to n are hub port events
- for (uint8_t port=1; port <= p_hub->port_count; port++) {
- if ( tu_bit_test(status_change, port) ) {
- if (hub_port_get_status(dev_addr, port, &p_hub->port_status, hub_port_get_status_complete, 0) == false) {
- //Hub status control transfer failed, retry
- hub_edpt_status_xfer(dev_addr);
+ if (status_change == 0) {
+ // The status change event was neither for the hub, nor for any of its ports.
+ // This shouldn't happen, but it does with some devices. Re-Initiate the interrupt poll.
+ processed = false;
+ } else if (tu_bit_test(status_change, 0)) {
+ // Hub bit 0 is for the hub device events
+ processed = hub_get_status(daddr, p_epbuf->ctrl_buf, get_status_complete, 0);
+ } else {
+ // Hub bits 1 to n are hub port events
+ for (uint8_t port=1; port <= p_hub->bNbrPorts; port++) {
+ if (tu_bit_test(status_change, port)) {
+ processed = hub_port_get_status(daddr, port, p_epbuf->ctrl_buf, port_get_status_complete, 0);
+ break; // after completely processed one port, we will re-queue the status poll and handle next one
}
- break;
}
}
}
- // NOTE: next status transfer is queued by usbh.c after handling this request
+ // If new status event is processed: next status pool is queued by usbh.c after handled this request
+ // Otherwise re-queue the status poll here
+ if (!processed) {
+ TU_ASSERT(hub_edpt_status_xfer(daddr));
+ }
+
return true;
}
-static void hub_clear_feature_complete_stub(tuh_xfer_t* xfer)
-{
- TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
+static void port_clear_feature_complete_stub(tuh_xfer_t* xfer) {
hub_edpt_status_xfer(xfer->daddr);
}
-static void hub_get_status_complete (tuh_xfer_t* xfer)
-{
- TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
+static void get_status_complete(tuh_xfer_t *xfer) {
+ const uint8_t daddr = xfer->daddr;
- uint8_t const daddr = xfer->daddr;
- hub_interface_t* p_hub = get_itf(daddr);
- uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
- TU_ASSERT(port_num == 0 , );
+ bool processed = false; // true if new status is processed
+ if (xfer->result == XFER_RESULT_SUCCESS) {
+ hub_status_response_t hub_status = *((const hub_status_response_t *) (uintptr_t) xfer->buffer);
- TU_LOG2("HUB Got hub status, addr = %u, status = %04x\r\n", daddr, p_hub->hub_status.change.value);
+ TU_LOG_DRV("HUB Got hub status, addr = %u, status = %04x\r\n", daddr, hub_status.change.value);
- if (p_hub->hub_status.change.local_power_source)
- {
- TU_LOG2("HUB Local Power Change, addr = %u\r\n", daddr);
- hub_port_clear_feature(daddr, port_num, HUB_FEATURE_HUB_LOCAL_POWER_CHANGE, hub_clear_feature_complete_stub, 0);
+ if (hub_status.change.local_power_source) {
+ TU_LOG_DRV(" Local Power Change\r\n");
+ processed = hub_clear_feature(daddr, HUB_FEATURE_HUB_LOCAL_POWER_CHANGE, port_clear_feature_complete_stub, 0);
+ } else if (hub_status.change.over_current) {
+ TU_LOG_DRV(" Over Current\r\n");
+ processed = hub_clear_feature(daddr, HUB_FEATURE_HUB_OVER_CURRENT_CHANGE, port_clear_feature_complete_stub, 0);
+ }
}
- else if (p_hub->hub_status.change.over_current)
- {
- TU_LOG1("HUB Over Current, addr = %u\r\n", daddr);
- hub_port_clear_feature(daddr, port_num, HUB_FEATURE_HUB_OVER_CURRENT_CHANGE, hub_clear_feature_complete_stub, 0);
+
+ if (!processed) {
+ TU_ASSERT(hub_edpt_status_xfer(daddr), );
}
}
-static void hub_port_get_status_complete (tuh_xfer_t* xfer)
-{
- TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
+static void port_get_status_complete(tuh_xfer_t *xfer) {
+ const uint8_t daddr = xfer->daddr;
+ bool processed = false; // true if new status is processed
- uint8_t const daddr = xfer->daddr;
- hub_interface_t* p_hub = get_itf(daddr);
- uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
+ if (xfer->result == XFER_RESULT_SUCCESS) {
+ const uint8_t port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
+ hub_interface_t *p_hub = get_hub_itf(daddr);
+ p_hub->port_status = *((const hub_port_status_response_t *) (uintptr_t) xfer->buffer);
- // Connection change
- if (p_hub->port_status.change.connection)
- {
- // Port is powered and enabled
- //TU_VERIFY(port_status.status_current.port_power && port_status.status_current.port_enable, );
+ // Clear port status change interrupts
+ if (p_hub->port_status.change.connection) {
+ // Connection change
+ // Port is powered and enabled
+ //TU_VERIFY(port_status.status_current.port_power && port_status.status_current.port_enable, );
- // Acknowledge Port Connection Change
- hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_CONNECTION_CHANGE, connection_clear_conn_change_complete, 0);
- }else
- {
- // Clear other port status change interrupts. TODO Not currently handled - just cleared.
- if (p_hub->port_status.change.port_enable)
- {
- hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_ENABLE_CHANGE, hub_clear_feature_complete_stub, 0);
- }
- else if (p_hub->port_status.change.suspend)
- {
- hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_SUSPEND_CHANGE, hub_clear_feature_complete_stub, 0);
- }
- else if (p_hub->port_status.change.over_current)
- {
- hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_OVER_CURRENT_CHANGE, hub_clear_feature_complete_stub, 0);
- }
- else if (p_hub->port_status.change.reset)
- {
- hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_RESET_CHANGE, hub_clear_feature_complete_stub, 0);
+ // Acknowledge Port Connection Change
+ processed = hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_CONNECTION_CHANGE, connection_clear_conn_change_complete, 0);
+ } else if (p_hub->port_status.change.port_enable) {
+ processed = hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_ENABLE_CHANGE, port_clear_feature_complete_stub, 0);
+ } else if (p_hub->port_status.change.suspend) {
+ processed = hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_SUSPEND_CHANGE, port_clear_feature_complete_stub, 0);
+ } else if (p_hub->port_status.change.over_current) {
+ processed = hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_OVER_CURRENT_CHANGE, port_clear_feature_complete_stub, 0);
+ } else if (p_hub->port_status.change.reset) {
+ processed = hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_RESET_CHANGE, port_clear_feature_complete_stub, 0);
}
- // Other changes are: L1 state
- // TODO clear change
+ }
- else
- {
- // prepare for next hub status
- // TODO continue with status_change, or maybe we can do it again with status
- hub_edpt_status_xfer(daddr);
- }
+ if (!processed) {
+ TU_ASSERT(hub_edpt_status_xfer(daddr), );
}
}
-static void connection_clear_conn_change_complete (tuh_xfer_t* xfer)
-{
- TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
+static void connection_clear_conn_change_complete (tuh_xfer_t* xfer) {
+ const uint8_t daddr = xfer->daddr;
- uint8_t const daddr = xfer->daddr;
- hub_interface_t* p_hub = get_itf(daddr);
- uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
+ if (xfer->result != XFER_RESULT_SUCCESS) {
+ TU_ASSERT(hub_edpt_status_xfer(daddr), );
+ return;
+ }
+
+ hub_interface_t *p_hub = get_hub_itf(daddr);
+ const uint8_t port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
- if ( p_hub->port_status.status.connection )
- {
+ if (p_hub->port_status.status.connection) {
// Reset port if attach event
hub_port_reset(daddr, port_num, connection_port_reset_complete, 0);
- }else
- {
+ } else {
// submit detach event
- hcd_event_t event =
- {
+ const hcd_event_t event = {
.rhport = usbh_get_rhport(daddr),
.event_id = HCD_EVENT_DEVICE_REMOVE,
- .connection =
- {
+ .connection = {
.hub_addr = daddr,
.hub_port = port_num
}
};
-
hcd_event_handler(&event, false);
}
}
-static void connection_port_reset_complete (tuh_xfer_t* xfer)
-{
- TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
+static void connection_port_reset_complete (tuh_xfer_t* xfer) {
+ const uint8_t daddr = xfer->daddr;
- uint8_t const daddr = xfer->daddr;
- // hub_interface_t* p_hub = get_itf(daddr);
- uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
+ if (xfer->result != XFER_RESULT_SUCCESS) {
+ // retry port reset if failed
+ if (!tuh_control_xfer(xfer)) {
+ TU_ASSERT(hub_edpt_status_xfer(daddr), ); // back to status poll if failed to queue request
+ }
+ return;
+ }
+
+ const uint8_t port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
// submit attach event
- hcd_event_t event =
- {
+ hcd_event_t event = {
.rhport = usbh_get_rhport(daddr),
.event_id = HCD_EVENT_DEVICE_ATTACH,
- .connection =
- {
+ .connection = {
.hub_addr = daddr,
.hub_port = port_num
}
};
-
hcd_event_handler(&event, false);
}
diff --git a/src/host/hub.h b/src/host/hub.h
index 385efe6b2..e4e576661 100644
--- a/src/host/hub.h
+++ b/src/host/hub.h
@@ -24,17 +24,8 @@
* This file is part of the TinyUSB stack.
*/
-/** \ingroup group_class
- * \defgroup ClassDriver_Hub Hub (Host only)
- * \details Like most PC's OS, Hub support is completely hidden from Application. In fact, application cannot determine whether
- * a device is mounted directly via roothub or via a hub's port. All Hub-related procedures are performed and managed
- * by tinyusb stack. Unless you are trying to develop the stack itself, there are nothing else can be used by Application.
- * \note Due to my laziness, only 1-level of Hub is supported. In other way, the stack cannot mount a hub via another hub.
- * @{
- */
-
-#ifndef _TUSB_HUB_H_
-#define _TUSB_HUB_H_
+#ifndef TUSB_HUB_H_
+#define TUSB_HUB_H_
#include "common/tusb_common.h"
@@ -42,63 +33,23 @@
extern "C" {
#endif
-//D1...D0: Logical Power Switching Mode
-//00: Ganged power switching (all ports’power at
-//once)
-//01: Individual port power switching
-//1X: Reserved. Used only on 1.0 compliant hubs
-//that implement no power switching
-//D2: Identifies a Compound Device
-//0: Hub is not part of a compound device.
-//1: Hub is part of a compound device.
-//D4...D3: Over-current Protection Mode
-//00: Global Over-current Protection. The hub
-//reports over-current as a summation of all
-//ports’current draw, without a breakdown of
-//individual port over-current status.
-//01: Individual Port Over-current Protection. The
-//hub reports over-current on a per-port basis.
-//Each port has an over-current status.
-//1X: No Over-current Protection. This option is
-//allowed only for bus-powered hubs that do not
-//implement over-current protection.
-//
-//D6...D5: TT Think TIme
-//00: TT requires at most 8 FS bit times of inter
-//transaction gap on a full-/low-speed
-//downstream bus.
-//01: TT requires at most 16 FS bit times.
-//10: TT requires at most 24 FS bit times.
-//11: TT requires at most 32 FS bit times.
-//D7: Port Indicators Supported
-//0: Port Indicators are not supported on its
-//downstream facing ports and the
-//PORT_INDICATOR request has no effect.
-//1: Port Indicators are supported on its
-//downstream facing ports and the
-//PORT_INDICATOR request controls the
-//indicators. See Section 11.5.3.
-//D15...D8: Reserved
-
-typedef struct TU_ATTR_PACKED{
- uint8_t bLength ; ///< Size of descriptor
- uint8_t bDescriptorType ; ///< Other_speed_Configuration Type
- uint8_t bNbrPorts;
- uint16_t wHubCharacteristics;
- uint8_t bPwrOn2PwrGood;
- uint8_t bHubContrCurrent;
- uint8_t DeviceRemovable; // bitmap each bit for a port (from bit1)
- uint8_t PortPwrCtrlMask; // just for compatibility, should be 0xff
-} descriptor_hub_desc_t;
+//--------------------------------------------------------------------+
+// Configuration
+//--------------------------------------------------------------------+
-TU_VERIFY_STATIC( sizeof(descriptor_hub_desc_t) == 9, "size is not correct");
+#ifndef CFG_TUH_HUB_BUFSIZE
+ #define CFG_TUH_HUB_BUFSIZE 12
+#endif
+//--------------------------------------------------------------------+
+//
+//--------------------------------------------------------------------+
enum {
HUB_REQUEST_GET_STATUS = 0 ,
HUB_REQUEST_CLEAR_FEATURE = 1 ,
-
+ // 2 is reserved
HUB_REQUEST_SET_FEATURE = 3 ,
-
+ // 4-5 are reserved
HUB_REQUEST_GET_DESCRIPTOR = 6 ,
HUB_REQUEST_SET_DESCRIPTOR = 7 ,
HUB_REQUEST_CLEAR_TT_BUFFER = 8 ,
@@ -118,10 +69,10 @@ enum{
HUB_FEATURE_PORT_SUSPEND = 2,
HUB_FEATURE_PORT_OVER_CURRENT = 3,
HUB_FEATURE_PORT_RESET = 4,
-
+ // 5-7 are reserved
HUB_FEATURE_PORT_POWER = 8,
HUB_FEATURE_PORT_LOW_SPEED = 9,
-
+ // 10-15 are reserved
HUB_FEATURE_PORT_CONNECTION_CHANGE = 16,
HUB_FEATURE_PORT_ENABLE_CHANGE = 17,
HUB_FEATURE_PORT_SUSPEND_CHANGE = 18,
@@ -131,6 +82,41 @@ enum{
HUB_FEATURE_PORT_INDICATOR = 22
};
+enum {
+ HUB_CHARS_POWER_GANGED_SWITCHING = 0,
+ HUB_CHARS_POWER_INDIVIDUAL_SWITCHING = 1,
+};
+
+enum {
+ HUB_CHARS_OVER_CURRENT_GLOBAL = 0,
+ HUB_CHARS_OVER_CURRENT_INDIVIDUAL = 1,
+};
+
+typedef struct TU_ATTR_PACKED{
+ uint8_t bLength ; ///< Size of descriptor
+ uint8_t bDescriptorType ; ///< Other_speed_Configuration Type
+ uint8_t bNbrPorts;
+ uint16_t wHubCharacteristics;
+ uint8_t bPwrOn2PwrGood;
+ uint8_t bHubContrCurrent;
+ uint8_t DeviceRemovable; // bitmap each bit for a port (from bit1)
+ uint8_t PortPwrCtrlMask; // just for compatibility, should be 0xff
+} hub_desc_cs_t;
+TU_VERIFY_STATIC(sizeof(hub_desc_cs_t) == 9, "size is not correct");
+TU_VERIFY_STATIC(CFG_TUH_HUB_BUFSIZE >= sizeof(hub_desc_cs_t), "buffer is not big enough");
+
+typedef struct TU_ATTR_PACKED {
+ struct TU_ATTR_PACKED {
+ uint8_t logical_power_switching_mode : 2; // [0..1] gannged or individual power switching
+ uint8_t compound_device : 1; // [2] hub is part of compound device
+ uint8_t over_current_protect_mode : 2; // [3..4] global or individual port over-current protection
+ uint8_t tt_think_time : 2; // [5..6] TT think time
+ uint8_t port_indicator_supported : 1; // [7] port indicator supported
+ };
+ uint8_t rsv1;
+} hub_characteristics_t;
+TU_VERIFY_STATIC(sizeof(hub_characteristics_t) == 2, "size is not correct");
+
// data in response of HUB_REQUEST_GET_STATUS, wIndex = 0 (hub)
typedef struct {
union{
@@ -143,48 +129,52 @@ typedef struct {
uint16_t value;
} status, change;
} hub_status_response_t;
-
TU_VERIFY_STATIC( sizeof(hub_status_response_t) == 4, "size is not correct");
// data in response of HUB_REQUEST_GET_STATUS, wIndex = Port num
typedef struct {
- union {
+ union TU_ATTR_PACKED {
struct TU_ATTR_PACKED {
- uint16_t connection : 1;
- uint16_t port_enable : 1;
- uint16_t suspend : 1;
- uint16_t over_current : 1;
- uint16_t reset : 1;
+ // Bit 0-4 are for change & status
+ uint16_t connection : 1; // [0] 0 = no device, 1 = device connected
+ uint16_t port_enable : 1; // [1] port is enabled
+ uint16_t suspend : 1; // [2]
+ uint16_t over_current : 1; // [3] over-current exists
+ uint16_t reset : 1; // [4] 0 = no reset, 1 = resetting
- uint16_t : 3;
- uint16_t port_power : 1;
- uint16_t low_speed : 1;
- uint16_t high_speed : 1;
- uint16_t port_test_mode : 1;
- uint16_t port_indicator_control : 1;
- uint16_t TU_RESERVED : 3;
+ // From Bit 5 are for status only
+ uint16_t rsv5_7 : 3; // [5..7] reserved
+ uint16_t port_power : 1; // [8] 0 = port is off, 1 = port is on
+ uint16_t low_speed : 1; // [9] low speed device attached
+ uint16_t high_speed : 1; // [10] high speed device attached
+ uint16_t port_test_mode : 1; // [11] port in test mode
+ uint16_t port_indicator_control : 1; // [12] 0: default color, 1: indicator is software controlled
+ uint16_t TU_RESERVED : 3; // [13..15] reserved
};
uint16_t value;
} status, change;
} hub_port_status_response_t;
-
TU_VERIFY_STATIC( sizeof(hub_port_status_response_t) == 4, "size is not correct");
-// Clear feature
-bool hub_port_clear_feature (uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
- tuh_xfer_cb_t complete_cb, uintptr_t user_data);
+//--------------------------------------------------------------------+
+// HUB API
+//--------------------------------------------------------------------+
+
+// Clear port feature
+bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
+ tuh_xfer_cb_t complete_cb, uintptr_t user_data);
-// Set feature
-bool hub_port_set_feature (uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
- tuh_xfer_cb_t complete_cb, uintptr_t user_data);
+// Set port feature
+bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
+ tuh_xfer_cb_t complete_cb, uintptr_t user_data);
// Get port status
-bool hub_port_get_status (uint8_t hub_addr, uint8_t hub_port, void* resp,
- tuh_xfer_cb_t complete_cb, uintptr_t user_data);
+bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void *resp,
+ tuh_xfer_cb_t complete_cb, uintptr_t user_data);
// Get status from Interrupt endpoint
-bool hub_edpt_status_xfer(uint8_t dev_addr);
+bool hub_edpt_status_xfer(uint8_t daddr);
// Reset a port
TU_ATTR_ALWAYS_INLINE static inline
@@ -192,13 +182,23 @@ bool hub_port_reset(uint8_t hub_addr, uint8_t hub_port, tuh_xfer_cb_t complete_c
return hub_port_set_feature(hub_addr, hub_port, HUB_FEATURE_PORT_RESET, complete_cb, user_data);
}
-// Clear Reset Change
+// Clear Port Reset Change
TU_ATTR_ALWAYS_INLINE static inline
bool hub_port_clear_reset_change(uint8_t hub_addr, uint8_t hub_port, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
return hub_port_clear_feature(hub_addr, hub_port, HUB_FEATURE_PORT_RESET_CHANGE, complete_cb, user_data);
}
+// Get Hub status
+TU_ATTR_ALWAYS_INLINE static inline
+bool hub_get_status(uint8_t hub_addr, void* resp, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
+ return hub_port_get_status(hub_addr, 0, resp, complete_cb, user_data);
+}
+// Clear Hub feature
+TU_ATTR_ALWAYS_INLINE static inline
+bool hub_clear_feature(uint8_t hub_addr, uint8_t feature, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
+ return hub_port_clear_feature(hub_addr, 0, feature, complete_cb, user_data);
+}
//--------------------------------------------------------------------+
// Internal Class Driver API
//--------------------------------------------------------------------+
@@ -206,13 +206,11 @@ bool hub_init (void);
bool hub_deinit (void);
bool hub_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len);
bool hub_set_config (uint8_t dev_addr, uint8_t itf_num);
-bool hub_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
+bool hub_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
void hub_close (uint8_t dev_addr);
#ifdef __cplusplus
}
#endif
-#endif /* _TUSB_HUB_H_ */
-
-/** @} */
+#endif
diff --git a/src/host/usbh.c b/src/host/usbh.c
index 8c488aaa2..e60db53da 100644
--- a/src/host/usbh.c
+++ b/src/host/usbh.c
@@ -44,25 +44,48 @@
#define CFG_TUH_INTERFACE_MAX 8
#endif
+enum {
+ USBH_CONTROL_RETRY_MAX = 3,
+};
+
//--------------------------------------------------------------------+
// 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) {
+ (void) addr; (void) data_size;
+ return false;
+}
+
+TU_ATTR_WEAK bool hcd_dcache_invalidate(const void* addr, uint32_t data_size) {
+ (void) addr; (void) data_size;
+ return false;
+}
+
+TU_ATTR_WEAK bool hcd_dcache_clean_invalidate(const void* addr, uint32_t data_size) {
+ (void) addr; (void) data_size;
+ return false;
}
//--------------------------------------------------------------------+
@@ -107,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
@@ -137,69 +161,80 @@ typedef struct {
#endif
static usbh_class_driver_t const usbh_class_drivers[] = {
- #if CFG_TUH_CDC
- {
- .name = DRIVER_NAME("CDC"),
- .init = cdch_init,
- .deinit = cdch_deinit,
- .open = cdch_open,
- .set_config = cdch_set_config,
- .xfer_cb = cdch_xfer_cb,
- .close = cdch_close
- },
- #endif
+ #if CFG_TUH_CDC
+ {
+ .name = DRIVER_NAME("CDC"),
+ .init = cdch_init,
+ .deinit = cdch_deinit,
+ .open = cdch_open,
+ .set_config = cdch_set_config,
+ .xfer_cb = cdch_xfer_cb,
+ .close = cdch_close
+ },
+ #endif
- #if CFG_TUH_MSC
- {
- .name = DRIVER_NAME("MSC"),
- .init = msch_init,
- .deinit = msch_deinit,
- .open = msch_open,
- .set_config = msch_set_config,
- .xfer_cb = msch_xfer_cb,
- .close = msch_close
- },
- #endif
+ #if CFG_TUH_MSC
+ {
+ .name = DRIVER_NAME("MSC"),
+ .init = msch_init,
+ .deinit = msch_deinit,
+ .open = msch_open,
+ .set_config = msch_set_config,
+ .xfer_cb = msch_xfer_cb,
+ .close = msch_close
+ },
+ #endif
- #if CFG_TUH_HID
- {
- .name = DRIVER_NAME("HID"),
- .init = hidh_init,
- .deinit = hidh_deinit,
- .open = hidh_open,
- .set_config = hidh_set_config,
- .xfer_cb = hidh_xfer_cb,
- .close = hidh_close
- },
- #endif
+ #if CFG_TUH_HID
+ {
+ .name = DRIVER_NAME("HID"),
+ .init = hidh_init,
+ .deinit = hidh_deinit,
+ .open = hidh_open,
+ .set_config = hidh_set_config,
+ .xfer_cb = hidh_xfer_cb,
+ .close = hidh_close
+ },
+ #endif
- #if CFG_TUH_HUB
- {
- .name = DRIVER_NAME("HUB"),
- .init = hub_init,
- .deinit = hub_deinit,
- .open = hub_open,
- .set_config = hub_set_config,
- .xfer_cb = hub_xfer_cb,
- .close = hub_close
- },
- #endif
+ #if CFG_TUH_MIDI
+ {
+ .name = DRIVER_NAME("MIDI"),
+ .init = midih_init,
+ .deinit = midih_deinit,
+ .open = midih_open,
+ .set_config = midih_set_config,
+ .xfer_cb = midih_xfer_cb,
+ .close = midih_close
+ },
+ #endif
- #if CFG_TUH_VENDOR
- {
- .name = DRIVER_NAME("VENDOR"),
- .init = cush_init,
- .deinit = cush_deinit,
- .open = cush_open,
- .set_config = cush_set_config,
- .xfer_cb = cush_isr,
- .close = cush_close
- }
- #endif
+ #if CFG_TUH_HUB
+ {
+ .name = DRIVER_NAME("HUB"),
+ .init = hub_init,
+ .deinit = hub_deinit,
+ .open = hub_open,
+ .set_config = hub_set_config,
+ .xfer_cb = hub_xfer_cb,
+ .close = hub_close
+ },
+ #endif
+
+ #if CFG_TUH_VENDOR
+ {
+ .name = DRIVER_NAME("VENDOR"),
+ .init = cush_init,
+ .deinit = cush_deinit,
+ .open = cush_open,
+ .set_config = cush_set_config,
+ .xfer_cb = cush_isr,
+ .close = cush_close
+ }
+ #endif
};
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;
@@ -249,25 +284,28 @@ static usbh_device_t _usbh_devices[TOTAL_DEVICES];
OSAL_QUEUE_DEF(usbh_int_set, _usbh_qdef, CFG_TUH_TASK_QUEUE_SZ, hcd_event_t);
static osal_queue_t _usbh_q;
-CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN
-static uint8_t _usbh_ctrl_buf[CFG_TUH_ENUMERATION_BUFSIZE];
-
// Control transfers: since most controllers do not support multiple control transfers
// on multiple devices concurrently and control transfers are not used much except for
// enumeration, we will only execute control transfers one at a time.
-CFG_TUH_MEM_SECTION struct {
- CFG_TUH_MEM_ALIGN tusb_control_request_t request;
+static struct {
uint8_t* buffer;
tuh_xfer_cb_t complete_cb;
uintptr_t user_data;
- uint8_t daddr;
volatile uint8_t stage;
+ uint8_t daddr;
volatile uint16_t actual_len;
-}_ctrl_xfer;
+ uint8_t failed_count;
+} _ctrl_xfer;
-//------------- Helper Function -------------//
+typedef struct {
+ TUH_EPBUF_TYPE_DEF(tusb_control_request_t, request);
+ TUH_EPBUF_DEF(ctrl, CFG_TUH_ENUMERATION_BUFSIZE);
+} usbh_epbuf_t;
+
+CFG_TUH_MEM_SECTION static usbh_epbuf_t _usbh_epbuf;
+//------------- Helper Function -------------//
TU_ATTR_ALWAYS_INLINE static inline usbh_device_t* get_device(uint8_t dev_addr) {
TU_VERIFY(dev_addr > 0 && dev_addr <= TOTAL_DEVICES, NULL);
return &_usbh_devices[dev_addr-1];
@@ -278,15 +316,6 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
static bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size);
static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
-#if CFG_TUSB_OS == OPT_OS_NONE
-// TODO rework time-related function later
-// weak and overridable
-TU_ATTR_WEAK void osal_task_delay(uint32_t msec) {
- const uint32_t start = hcd_frame_number(_usbh_controller);
- while ( ( hcd_frame_number(_usbh_controller) - start ) < msec ) {}
-}
-#endif
-
TU_ATTR_ALWAYS_INLINE static inline bool queue_event(hcd_event_t const * event, bool in_isr) {
TU_ASSERT(osal_queue_send(_usbh_q, event, in_isr));
tuh_event_hook_cb(event->rhport, event->event_id, in_isr);
@@ -303,6 +332,15 @@ bool tuh_mounted(uint8_t dev_addr) {
return dev->configured;
}
+bool tuh_connected(uint8_t daddr) {
+ if (daddr == 0) {
+ return _dev0.enumerating; // dev0 is connected if still enumerating
+ } else {
+ const usbh_device_t* dev = get_device(daddr);
+ return dev && dev->connected;
+ }
+}
+
bool tuh_vid_pid_get(uint8_t dev_addr, uint16_t *vid, uint16_t *pid) {
*vid = *pid = 0;
@@ -352,11 +390,13 @@ bool tuh_inited(void) {
return _usbh_controller != TUSB_INDEX_INVALID_8;
}
-bool tuh_init(uint8_t rhport) {
- // skip if already initialized
- if (tuh_rhport_is_active(rhport)) return true;
+bool tuh_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
+ if (tuh_rhport_is_active(rhport)) {
+ return true; // skip if already initialized
+ }
- TU_LOG_USBH("USBH init on controller %u\r\n", rhport);
+ TU_LOG_USBH("USBH init on controller %u, speed = %s\r\n", rhport,
+ rh_init->speed == TUSB_SPEED_HIGH ? "High" : "Full");
// Init host stack if not already
if (!tuh_inited()) {
@@ -402,15 +442,17 @@ bool tuh_init(uint8_t rhport) {
}
// Init host controller
- _usbh_controller = rhport;;
- TU_ASSERT(hcd_init(rhport));
+ _usbh_controller = rhport;
+ TU_ASSERT(hcd_init(rhport, rh_init));
hcd_int_enable(rhport);
return true;
}
bool tuh_deinit(uint8_t rhport) {
- if (!tuh_rhport_is_active(rhport)) return true;
+ if (!tuh_rhport_is_active(rhport)) {
+ return true;
+ }
// deinit host controller
hcd_int_disable(rhport);
@@ -445,9 +487,9 @@ bool tuh_deinit(uint8_t rhport) {
}
bool tuh_task_event_ready(void) {
- // Skip if stack is not initialized
- if ( !tuh_inited() ) return false;
-
+ if (!tuh_inited()) {
+ return false; // Skip if stack is not initialized
+ }
return !osal_queue_empty(_usbh_q);
}
@@ -456,13 +498,11 @@ bool tuh_task_event_ready(void) {
* This should be called periodically within the mainloop or rtos thread.
*
@code
- int main(void)
- {
+ int main(void) {
application_init();
- tusb_init();
+ tusb_init(0, TUSB_ROLE_HOST);
- while(1) // the mainloop
- {
+ while(1) { // the mainloop
application_code();
tuh_task(); // tinyusb host task
}
@@ -473,29 +513,41 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
(void) in_isr; // not implemented yet
// Skip if stack is not initialized
- if (!tuh_inited()) return;
+ if (!tuh_inited()) {
+ return;
+ }
// 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:
- // due to the shared _usbh_ctrl_buf, we must complete enumerating one device before enumerating another one.
+ // due to the shared control buffer, we must complete enumerating one device before enumerating another one.
// TODO better to have an separated queue for newly attached devices
if (_dev0.enumerating) {
- TU_LOG_USBH("[%u:] USBH Defer Attach until current enumeration complete\r\n", event.rhport);
+ // Some device can cause multiple duplicated attach events
+ // drop current enumerating and start over for a proper port reset
+ if (event.rhport == _dev0.rhport && event.connection.hub_addr == _dev0.hub_addr &&
+ event.connection.hub_port == _dev0.hub_port) {
+ // abort/cancel current enumeration and start new one
+ TU_LOG1("[%u:] USBH Device Attach (duplicated)\r\n", event.rhport);
+ tuh_edpt_abort_xfer(0, 0);
+ enum_new_device(&event);
+ } else {
+ TU_LOG_USBH("[%u:] USBH Defer Attach until current enumeration complete\r\n", event.rhport);
- bool is_empty = osal_queue_empty(_usbh_q);
- queue_event(&event, in_isr);
+ bool is_empty = osal_queue_empty(_usbh_q);
+ queue_event(&event, in_isr);
- if (is_empty) {
- // Exit if this is the only event in the queue, otherwise we may loop forever
- return;
+ if (is_empty) {
+ // Exit if this is the only event in the queue, otherwise we may loop forever
+ return;
+ }
}
} else {
- TU_LOG_USBH("[%u:] USBH DEVICE ATTACH\r\n", event.rhport);
+ TU_LOG1("[%u:] USBH Device Attach\r\n", event.rhport);
_dev0.enumerating = 1;
enum_new_device(&event);
}
@@ -597,17 +649,9 @@ static void _control_blocking_complete_cb(tuh_xfer_t* xfer) {
// TODO timeout_ms is not supported yet
bool tuh_control_xfer (tuh_xfer_t* xfer) {
- // EP0 with setup packet
- TU_VERIFY(xfer->ep_addr == 0 && xfer->setup);
-
- // Check if device is still connected (enumerating for dev0)
- uint8_t const daddr = xfer->daddr;
- if ( daddr == 0 ) {
- if (!_dev0.enumerating) return false;
- } else {
- usbh_device_t const* dev = get_device(daddr);
- if (dev && dev->connected == 0) return false;
- }
+ TU_VERIFY(xfer->ep_addr == 0 && xfer->setup); // EP0 with setup packet
+ const uint8_t daddr = xfer->daddr;
+ TU_VERIFY(tuh_connected(daddr)); // Check if device is still connected (enumerating for dev0)
// pre-check to help reducing mutex lock
TU_VERIFY(_ctrl_xfer.stage == CONTROL_STAGE_IDLE);
@@ -615,14 +659,15 @@ bool tuh_control_xfer (tuh_xfer_t* xfer) {
bool const is_idle = (_ctrl_xfer.stage == CONTROL_STAGE_IDLE);
if (is_idle) {
- _ctrl_xfer.stage = CONTROL_STAGE_SETUP;
- _ctrl_xfer.daddr = daddr;
- _ctrl_xfer.actual_len = 0;
+ _ctrl_xfer.stage = CONTROL_STAGE_SETUP;
+ _ctrl_xfer.daddr = daddr;
+ _ctrl_xfer.actual_len = 0;
+ _ctrl_xfer.failed_count = 0;
- _ctrl_xfer.request = (*xfer->setup);
- _ctrl_xfer.buffer = xfer->buffer;
- _ctrl_xfer.complete_cb = xfer->complete_cb;
- _ctrl_xfer.user_data = xfer->user_data;
+ _ctrl_xfer.buffer = xfer->buffer;
+ _ctrl_xfer.complete_cb = xfer->complete_cb;
+ _ctrl_xfer.user_data = xfer->user_data;
+ _usbh_epbuf.request = (*xfer->setup);
}
(void) osal_mutex_unlock(_usbh_mutex);
@@ -636,7 +681,7 @@ bool tuh_control_xfer (tuh_xfer_t* xfer) {
TU_LOG_BUF_USBH(xfer->setup, 8);
if (xfer->complete_cb) {
- TU_ASSERT( hcd_setup_send(rhport, daddr, (uint8_t const*) &_ctrl_xfer.request) );
+ TU_ASSERT(hcd_setup_send(rhport, daddr, (uint8_t const *) &_usbh_epbuf.request));
}else {
// blocking if complete callback is not provided
// change callback to internal blocking, and result as user argument
@@ -646,7 +691,7 @@ bool tuh_control_xfer (tuh_xfer_t* xfer) {
_ctrl_xfer.user_data = (uintptr_t) &result;
_ctrl_xfer.complete_cb = _control_blocking_complete_cb;
- TU_ASSERT( hcd_setup_send(rhport, daddr, (uint8_t*) &_ctrl_xfer.request) );
+ TU_ASSERT(hcd_setup_send(rhport, daddr, (uint8_t *) &_usbh_epbuf.request));
while (result == XFER_RESULT_INVALID) {
// Note: this can be called within an callback ie. part of tuh_task()
@@ -678,7 +723,7 @@ static void _control_xfer_complete(uint8_t daddr, xfer_result_t result) {
TU_LOG_USBH("\r\n");
// duplicate xfer since user can execute control transfer within callback
- tusb_control_request_t const request = _ctrl_xfer.request;
+ tusb_control_request_t const request = _usbh_epbuf.request;
tuh_xfer_t xfer_temp = {
.daddr = daddr,
.ep_addr = 0,
@@ -701,30 +746,48 @@ static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t
(void) ep_addr;
const uint8_t rhport = usbh_get_rhport(daddr);
- tusb_control_request_t const * request = &_ctrl_xfer.request;
+ tusb_control_request_t const * request = &_usbh_epbuf.request;
- if (XFER_RESULT_SUCCESS != result) {
- TU_LOG_USBH("[%u:%u] Control %s, xferred_bytes = %" PRIu32 "\r\n", rhport, daddr, result == XFER_RESULT_STALLED ? "STALLED" : "FAILED", xferred_bytes);
- TU_LOG_BUF_USBH(request, 8);
+ switch (result) {
+ case XFER_RESULT_STALLED:
+ TU_LOG_USBH("[%u:%u] Control STALLED, xferred_bytes = %" PRIu32 "\r\n", rhport, daddr, xferred_bytes);
+ TU_LOG_BUF_USBH(request, 8);
+ _control_xfer_complete(daddr, result);
+ break;
- // terminate transfer if any stage failed
- _control_xfer_complete(daddr, result);
- }else {
- switch(_ctrl_xfer.stage) {
- case CONTROL_STAGE_SETUP:
- if (request->wLength) {
- // DATA stage: initial data toggle is always 1
- _set_control_xfer_stage(CONTROL_STAGE_DATA);
- TU_ASSERT( hcd_edpt_xfer(rhport, daddr, tu_edpt_addr(0, request->bmRequestType_bit.direction), _ctrl_xfer.buffer, request->wLength) );
- return true;
- }
+ case XFER_RESULT_FAILED:
+ if (tuh_connected(daddr) && _ctrl_xfer.failed_count < USBH_CONTROL_RETRY_MAX) {
+ TU_LOG_USBH("[%u:%u] Control FAILED %u/%u, retrying\r\n", rhport, daddr, _ctrl_xfer.failed_count+1, USBH_CONTROL_RETRY_MAX);
+ (void) osal_mutex_lock(_usbh_mutex, OSAL_TIMEOUT_WAIT_FOREVER);
+ _ctrl_xfer.stage = CONTROL_STAGE_SETUP;
+ _ctrl_xfer.failed_count++;
+ _ctrl_xfer.actual_len = 0; // reset actual_len
+ (void) osal_mutex_unlock(_usbh_mutex);
+
+ TU_ASSERT(hcd_setup_send(rhport, daddr, (uint8_t const *) request));
+ } else {
+ TU_LOG_USBH("[%u:%u] Control FAILED, xferred_bytes = %" PRIu32 "\r\n", rhport, daddr, xferred_bytes);
+ TU_LOG_BUF_USBH(request, 8);
+ _control_xfer_complete(daddr, result);
+ }
+ break;
+
+ case XFER_RESULT_SUCCESS:
+ switch(_ctrl_xfer.stage) {
+ case CONTROL_STAGE_SETUP:
+ if (request->wLength) {
+ // DATA stage: initial data toggle is always 1
+ _set_control_xfer_stage(CONTROL_STAGE_DATA);
+ TU_ASSERT( hcd_edpt_xfer(rhport, daddr, tu_edpt_addr(0, request->bmRequestType_bit.direction), _ctrl_xfer.buffer, request->wLength) );
+ return true;
+ }
TU_ATTR_FALLTHROUGH;
- case CONTROL_STAGE_DATA:
- if (request->wLength) {
- TU_LOG_USBH("[%u:%u] Control data:\r\n", rhport, daddr);
- TU_LOG_MEM_USBH(_ctrl_xfer.buffer, xferred_bytes, 2);
- }
+ case CONTROL_STAGE_DATA:
+ if (request->wLength) {
+ TU_LOG_USBH("[%u:%u] Control data:\r\n", rhport, daddr);
+ TU_LOG_MEM_USBH(_ctrl_xfer.buffer, xferred_bytes, 2);
+ }
_ctrl_xfer.actual_len = (uint16_t) xferred_bytes;
@@ -733,23 +796,26 @@ static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t
TU_ASSERT( hcd_edpt_xfer(rhport, daddr, tu_edpt_addr(0, 1 - request->bmRequestType_bit.direction), NULL, 0) );
break;
- case CONTROL_STAGE_ACK: {
- // Abort all pending transfers if SET_CONFIGURATION request
- // NOTE: should we force closing all non-control endpoints in the future?
- if (request->bRequest == TUSB_REQ_SET_CONFIGURATION && request->bmRequestType == 0x00) {
- for(uint8_t epnum=1; epnum<CFG_TUH_ENDPOINT_MAX; epnum++) {
- for(uint8_t dir=0; dir<2; dir++) {
- tuh_edpt_abort_xfer(daddr, tu_edpt_addr(epnum, dir));
+ case CONTROL_STAGE_ACK: {
+ // Abort all pending transfers if SET_CONFIGURATION request
+ // NOTE: should we force closing all non-control endpoints in the future?
+ if (request->bRequest == TUSB_REQ_SET_CONFIGURATION && request->bmRequestType == 0x00) {
+ for(uint8_t epnum=1; epnum<CFG_TUH_ENDPOINT_MAX; epnum++) {
+ for(uint8_t dir=0; dir<2; dir++) {
+ tuh_edpt_abort_xfer(daddr, tu_edpt_addr(epnum, dir));
+ }
}
}
+
+ _control_xfer_complete(daddr, result);
+ break;
}
- _control_xfer_complete(daddr, result);
- break;
+ default: return false; // unsupported stage
}
+ break;
- default: return false;
- }
+ default: return false; // unsupported result
}
return true;
@@ -776,24 +842,26 @@ bool tuh_edpt_xfer(tuh_xfer_t* xfer) {
}
bool tuh_edpt_abort_xfer(uint8_t daddr, uint8_t ep_addr) {
- usbh_device_t* dev = get_device(daddr);
- TU_VERIFY(dev);
-
TU_LOG_USBH("[%u] Aborted transfer on EP %02X\r\n", daddr, ep_addr);
- uint8_t const epnum = tu_edpt_number(ep_addr);
- uint8_t const dir = tu_edpt_dir(ep_addr);
+ const uint8_t epnum = tu_edpt_number(ep_addr);
+ const uint8_t dir = tu_edpt_dir(ep_addr);
+
+ if (epnum == 0) {
+ // Also include dev0 for aborting enumerating
+ const uint8_t rhport = usbh_get_rhport(daddr);
- if ( epnum == 0 ) {
// control transfer: only 1 control at a time, check if we are aborting the current one
TU_VERIFY(daddr == _ctrl_xfer.daddr && _ctrl_xfer.stage != CONTROL_STAGE_IDLE);
- TU_VERIFY(hcd_edpt_abort_xfer(dev->rhport, daddr, ep_addr));
- // reset control transfer state to idle
- _set_control_xfer_stage(CONTROL_STAGE_IDLE);
+ hcd_edpt_abort_xfer(rhport, daddr, ep_addr);
+ _set_control_xfer_stage(CONTROL_STAGE_IDLE); // reset control transfer state to idle
} else {
- // non-control skip if not busy
- TU_VERIFY(dev->ep_status[epnum][dir].busy);
- TU_VERIFY(hcd_edpt_abort_xfer(dev->rhport, daddr, ep_addr));
+ usbh_device_t* dev = get_device(daddr);
+ TU_VERIFY(dev);
+
+ TU_VERIFY(dev->ep_status[epnum][dir].busy); // non-control skip if not busy
+ hcd_edpt_abort_xfer(dev->rhport, daddr, ep_addr);
+
// mark as ready and release endpoint if transfer is aborted
dev->ep_status[epnum][dir].busy = false;
tu_edpt_release(&dev->ep_status[epnum][dir], _usbh_mutex);
@@ -812,7 +880,7 @@ uint8_t usbh_get_rhport(uint8_t dev_addr) {
}
uint8_t *usbh_get_enum_buf(void) {
- return _usbh_ctrl_buf;
+ return _usbh_epbuf.ctrl;
}
void usbh_int_set(bool enabled) {
@@ -868,7 +936,6 @@ bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr) {
}
// Submit an transfer
-// TODO call usbh_edpt_release if failed
bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes,
tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
(void) complete_cb;
@@ -923,10 +990,16 @@ static bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size) {
}
bool tuh_edpt_open(uint8_t dev_addr, tusb_desc_endpoint_t const* desc_ep) {
- TU_ASSERT(tu_edpt_validate(desc_ep, tuh_speed_get(dev_addr)));
+ TU_ASSERT(tu_edpt_validate(desc_ep, tuh_speed_get(dev_addr), true));
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);
@@ -1231,13 +1304,17 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
removing_hubs |= TU_BIT(dev_id - CFG_TUH_DEVICE_MAX);
} else {
// Invoke callback before closing driver (maybe call it later ?)
- if (tuh_umount_cb) tuh_umount_cb(daddr);
+ if (tuh_umount_cb) {
+ tuh_umount_cb(daddr);
+ }
}
// Close class driver
for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) {
usbh_class_driver_t const* driver = get_driver(drv_id);
- if (driver) driver->close(daddr);
+ if (driver) {
+ driver->close(daddr);
+ }
}
hcd_device_close(rhport, daddr);
@@ -1274,14 +1351,14 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
// Enumeration Process
// is a lengthy process with a series of control transfer to configure
// newly attached device.
-// NOTE: due to the shared _usbh_ctrl_buf, we must complete enumerating
+// NOTE: due to the shared control buffer, we must complete enumerating
// one device before enumerating another one.
//--------------------------------------------------------------------+
enum {
- ENUM_RESET_DELAY = 50, // USB specs: 10 to 50ms
- ENUM_CONTACT_DEBOUNCING_DELAY = 450, // when plug/unplug a device, physical connection can be bouncing and may
- // generate a series of attach/detach event. This delay wait for stable connection
+ ENUM_RESET_DELAY_MS = 50, // USB specs: 10 to 50ms
+ ENUM_DEBOUNCING_DELAY_MS = 450, // when plug/unplug a device, physical connection can be bouncing and may
+ // generate a series of attach/detach event. This delay wait for stable connection
};
enum {
@@ -1294,39 +1371,42 @@ enum {
ENUM_HUB_GET_STATUS_2,
ENUM_HUB_CLEAR_RESET_2,
ENUM_SET_ADDR,
-
ENUM_GET_DEVICE_DESC,
+ ENUM_GET_STRING_LANGUAGE_ID,
+ ENUM_GET_STRING_MANUFACTURER,
+ ENUM_GET_STRING_PRODUCT,
+ ENUM_GET_STRING_SERIAL,
ENUM_GET_9BYTE_CONFIG_DESC,
ENUM_GET_FULL_CONFIG_DESC,
ENUM_SET_CONFIG,
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
static void process_enumeration(tuh_xfer_t* xfer) {
- // Retry a few times with transfers in enumeration since device can be unstable when starting up
- enum {
- ATTEMPT_COUNT_MAX = 3,
- ATTEMPT_DELAY_MS = 100
- };
+ // Retry a few times while enumerating since device can be unstable when starting up
static uint8_t failed_count = 0;
+ if (XFER_RESULT_FAILED == xfer->result) {
+ enum {
+ ATTEMPT_COUNT_MAX = 3,
+ ATTEMPT_DELAY_MS = 100
+ };
- if (XFER_RESULT_SUCCESS != xfer->result) {
// retry if not reaching max attempt
+ failed_count++;
bool retry = _dev0.enumerating && (failed_count < ATTEMPT_COUNT_MAX);
- if ( retry ) {
- failed_count++;
- osal_task_delay(ATTEMPT_DELAY_MS); // delay a bit
- TU_LOG1("Enumeration attempt %u\r\n", failed_count);
+ if (retry) {
+ tusb_time_delay_ms_api(ATTEMPT_DELAY_MS); // delay a bit
+ TU_LOG1("Enumeration attempt %u/%u\r\n", failed_count+1, ATTEMPT_COUNT_MAX);
retry = tuh_control_xfer(xfer);
}
if (!retry) {
- enum_full_complete();
+ enum_full_complete(); // complete as failed
}
return;
@@ -1335,6 +1415,8 @@ static void process_enumeration(tuh_xfer_t* xfer) {
uint8_t const daddr = xfer->daddr;
uintptr_t const state = xfer->user_data;
+ usbh_device_t* dev = get_device(daddr);
+ uint16_t langid = 0x0409; // default is English
switch (state) {
#if CFG_TUH_HUB
@@ -1342,7 +1424,7 @@ static void process_enumeration(tuh_xfer_t* xfer) {
case ENUM_HUB_CLEAR_RESET_1: {
hub_port_status_response_t port_status;
- memcpy(&port_status, _usbh_ctrl_buf, sizeof(hub_port_status_response_t));
+ memcpy(&port_status, _usbh_epbuf.ctrl, sizeof(hub_port_status_response_t));
if (!port_status.status.connection) {
// device unplugged while delaying, nothing else to do
@@ -1362,14 +1444,14 @@ static void process_enumeration(tuh_xfer_t* xfer) {
}
case ENUM_HUB_GET_STATUS_2:
- osal_task_delay(ENUM_RESET_DELAY);
- TU_ASSERT(hub_port_get_status(_dev0.hub_addr, _dev0.hub_port, _usbh_ctrl_buf,
+ tusb_time_delay_ms_api(ENUM_RESET_DELAY_MS);
+ TU_ASSERT(hub_port_get_status(_dev0.hub_addr, _dev0.hub_port, _usbh_epbuf.ctrl,
process_enumeration, ENUM_HUB_CLEAR_RESET_2),);
break;
case ENUM_HUB_CLEAR_RESET_2: {
hub_port_status_response_t port_status;
- memcpy(&port_status, _usbh_ctrl_buf, sizeof(hub_port_status_response_t));
+ memcpy(&port_status, _usbh_epbuf.ctrl, sizeof(hub_port_status_response_t));
// Acknowledge Port Reset Change if Reset Successful
if (port_status.change.reset) {
@@ -1387,7 +1469,7 @@ static void process_enumeration(tuh_xfer_t* xfer) {
// Get first 8 bytes of device descriptor for Control Endpoint size
TU_LOG_USBH("Get 8 byte of Device Descriptor\r\n");
- TU_ASSERT(tuh_descriptor_get_device(addr0, _usbh_ctrl_buf, 8,
+ TU_ASSERT(tuh_descriptor_get_device(addr0, _usbh_epbuf.ctrl, 8,
process_enumeration, ENUM_SET_ADDR),);
break;
}
@@ -1400,7 +1482,7 @@ static void process_enumeration(tuh_xfer_t* xfer) {
if (_dev0.hub_addr == 0) {
// connected directly to roothub
hcd_port_reset( _dev0.rhport );
- osal_task_delay(RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since
+ tusb_time_delay_ms_api(RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since
// sof of controller may not running while resetting
hcd_port_reset_end(_dev0.rhport);
// TODO: fall through to SET ADDRESS, refactor later
@@ -1417,15 +1499,14 @@ 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: {
// Allow 2ms for address recovery time, Ref USB Spec 9.2.6.3
- osal_task_delay(2);
-
- uint8_t const new_addr = (uint8_t) tu_le16toh(xfer->setup->wValue);
+ tusb_time_delay_ms_api(2);
+ const uint8_t new_addr = (uint8_t) tu_le16toh(xfer->setup->wValue);
usbh_device_t* new_dev = get_device(new_addr);
TU_ASSERT(new_dev,);
new_dev->addressed = 1;
@@ -1438,64 +1519,121 @@ static void process_enumeration(tuh_xfer_t* xfer) {
// Get full device descriptor
TU_LOG_USBH("Get Device Descriptor\r\n");
- TU_ASSERT(tuh_descriptor_get_device(new_addr, _usbh_ctrl_buf, sizeof(tusb_desc_device_t),
- process_enumeration, ENUM_GET_9BYTE_CONFIG_DESC),);
+ TU_ASSERT(tuh_descriptor_get_device(new_addr, _usbh_epbuf.ctrl, sizeof(tusb_desc_device_t),
+ process_enumeration, ENUM_GET_STRING_LANGUAGE_ID),);
break;
}
- case ENUM_GET_9BYTE_CONFIG_DESC: {
- tusb_desc_device_t const* desc_device = (tusb_desc_device_t const*) _usbh_ctrl_buf;
- usbh_device_t* dev = get_device(daddr);
+ case ENUM_GET_STRING_LANGUAGE_ID: {
+ // save the received device descriptor
TU_ASSERT(dev,);
-
+ tusb_desc_device_t const* desc_device = (tusb_desc_device_t const*) _usbh_epbuf.ctrl;
dev->vid = desc_device->idVendor;
dev->pid = desc_device->idProduct;
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);
+ break;
+ }
- // if (tuh_attach_cb) tuh_attach_cb((tusb_desc_device_t*) _usbh_ctrl_buf);
+ case ENUM_GET_STRING_MANUFACTURER: {
+ TU_ASSERT(dev,);
+ const tusb_desc_string_t* desc_langid = (tusb_desc_string_t const*) _usbh_epbuf.ctrl;
+ if (desc_langid->bLength >= 4) {
+ langid = tu_le16toh(desc_langid->utf16le[0]);
+ }
+ if (dev->i_manufacturer != 0) {
+ tuh_descriptor_get_string(daddr, dev->i_manufacturer, langid, _usbh_epbuf.ctrl, CFG_TUH_ENUMERATION_BUFSIZE,
+ process_enumeration, ENUM_GET_STRING_PRODUCT);
+ break;
+ } else {
+ TU_ATTR_FALLTHROUGH;
+ }
+ }
+ case ENUM_GET_STRING_PRODUCT: {
+ TU_ASSERT(dev,);
+ if (state == ENUM_GET_STRING_PRODUCT) {
+ langid = tu_le16toh(xfer->setup->wIndex); // if not fall through, get langid from previous setup packet
+ }
+ if (dev->i_product != 0) {
+ tuh_descriptor_get_string(daddr, dev->i_product, 0x0409, _usbh_epbuf.ctrl, CFG_TUH_ENUMERATION_BUFSIZE,
+ process_enumeration, ENUM_GET_STRING_SERIAL);
+ break;
+ } else {
+ TU_ATTR_FALLTHROUGH;
+ }
+ }
+
+ case ENUM_GET_STRING_SERIAL: {
+ TU_ASSERT(dev,);
+ if (state == ENUM_GET_STRING_SERIAL) {
+ langid = tu_le16toh(xfer->setup->wIndex); // if not fall through, get langid from previous setup packet
+ }
+ if (dev->i_serial != 0) {
+ tuh_descriptor_get_string(daddr, dev->i_serial, langid, _usbh_epbuf.ctrl, CFG_TUH_ENUMERATION_BUFSIZE,
+ process_enumeration, ENUM_GET_9BYTE_CONFIG_DESC);
+ break;
+ } else {
+ TU_ATTR_FALLTHROUGH;
+ }
+ }
+
+ 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");
- TU_ASSERT(tuh_descriptor_get_configuration(daddr, config_idx, _usbh_ctrl_buf, 9,
+ 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;
}
case ENUM_GET_FULL_CONFIG_DESC: {
- uint8_t const* desc_config = _usbh_ctrl_buf;
+ 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");
- TU_ASSERT(tuh_descriptor_get_configuration(daddr, config_idx, _usbh_ctrl_buf, total_len,
+ 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:
- 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");
- usbh_device_t* dev = get_device(daddr);
TU_ASSERT(dev,);
dev->configured = 1;
// Parse configuration & set up drivers
// driver_open() must not make any usb transfer
- TU_ASSERT(_parse_configuration_descriptor(daddr, (tusb_desc_configuration_t*) _usbh_ctrl_buf),);
+ 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.
@@ -1506,8 +1644,7 @@ 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;
}
}
@@ -1518,14 +1655,17 @@ static bool enum_new_device(hcd_event_t* event) {
_dev0.hub_port = event->connection.hub_port;
if (_dev0.hub_addr == 0) {
- // connected/disconnected directly with roothub
+ // connected directly to roothub
hcd_port_reset(_dev0.rhport);
- osal_task_delay(ENUM_RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since
- // sof of controller may not running while resetting
+
+ // Since we are in middle of rhport reset, frame number is not available yet.
+ // need to depend on tusb_time_millis_api()
+ tusb_time_delay_ms_api(ENUM_RESET_DELAY_MS);
+
hcd_port_reset_end(_dev0.rhport);
// wait until device connection is stable TODO non blocking
- osal_task_delay(ENUM_CONTACT_DEBOUNCING_DELAY);
+ tusb_time_delay_ms_api(ENUM_DEBOUNCING_DELAY_MS);
// device unplugged while delaying
if (!hcd_port_connect_status(_dev0.rhport)) {
@@ -1546,13 +1686,12 @@ static bool enum_new_device(hcd_event_t* event) {
}
#if CFG_TUH_HUB
else {
- // connected/disconnected via external hub
+ // connected via external hub
// wait until device connection is stable TODO non blocking
- osal_task_delay(ENUM_CONTACT_DEBOUNCING_DELAY);
+ tusb_time_delay_ms_api(ENUM_DEBOUNCING_DELAY_MS);
// ENUM_HUB_GET_STATUS
- //TU_ASSERT( hub_port_get_status(_dev0.hub_addr, _dev0.hub_port, _usbh_ctrl_buf, enum_hub_get_status0_complete, 0) );
- TU_ASSERT(hub_port_get_status(_dev0.hub_addr, _dev0.hub_port, _usbh_ctrl_buf,
+ TU_ASSERT(hub_port_get_status(_dev0.hub_addr, _dev0.hub_port, _usbh_epbuf.ctrl,
process_enumeration, ENUM_HUB_CLEAR_RESET_1));
}
#endif // hub
@@ -1579,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_ctrl_buf;
-
+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);
@@ -1619,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;
@@ -1629,6 +1766,13 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
// parse each interfaces
while( p_desc < desc_end ) {
+ if ( 0 == tu_desc_len(p_desc) ) {
+ // A zero length descriptor indicates that the device is off spec (e.g. wrong wTotalLength).
+ // Parsed interfaces should still be usable
+ TU_LOG_USBH("Encountered a zero-length descriptor after %" PRIu32 " bytes\r\n", (uint32_t)p_desc - (uint32_t)desc_cfg);
+ break;
+ }
+
uint8_t assoc_itf_count = 1;
// Class will always starts with Interface Association (if any) and then Interface descriptor
@@ -1729,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);
+ }
}
}
}
@@ -1739,8 +1885,9 @@ static void enum_full_complete(void) {
_dev0.enumerating = 0;
#if CFG_TUH_HUB
- // get next hub status
- if (_dev0.hub_addr) hub_edpt_status_xfer(_dev0.hub_addr);
+ if (_dev0.hub_addr) {
+ hub_edpt_status_xfer(_dev0.hub_addr); // get next hub status
+ }
#endif
}
diff --git a/src/host/usbh.h b/src/host/usbh.h
index 359684169..4829a8183 100644
--- a/src/host/usbh.h
+++ b/src/host/usbh.h
@@ -33,10 +33,17 @@
#include "common/tusb_common.h"
+#if CFG_TUH_MAX3421
+#include "portable/analog/max3421/hcd_max3421.h"
+#endif
+
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
+// Endpoint Bulk size depending on host mx speed
+#define TUH_EPSIZE_BULK_MPS (TUD_OPT_HIGH_SPEED ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS)
+
// forward declaration
struct tuh_xfer_s;
typedef struct tuh_xfer_s tuh_xfer_t;
@@ -81,7 +88,7 @@ enum {
};
typedef struct {
- uint8_t max_nak; // max NAK per endpoint per frame
+ uint8_t max_nak; // max NAK per endpoint per frame to save CPU/SPI bus usage
uint8_t cpuctl; // R16: CPU Control Register
uint8_t pinctl; // R17: Pin Control Register. FDUPSPI bit is ignored
} tuh_configure_max3421_t;
@@ -96,7 +103,14 @@ typedef union {
// APPLICATION CALLBACK
//--------------------------------------------------------------------+
-//TU_ATTR_WEAK uint8_t tuh_attach_cb (tusb_desc_device_t const *desc_device);
+// Invoked when enumeration get device descriptor
+// 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
+// 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);
@@ -120,8 +134,20 @@ void tuh_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr);
// - cfg_param: configure data, structure depends on the ID
bool tuh_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param);
+// New API to replace tuh_init() to init host stack on specific roothub port
+bool tuh_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init);
+
// Init host stack
-bool tuh_init(uint8_t rhport);
+#if TUSB_VERSION_NUMBER > 2000 // 0.20.0
+TU_ATTR_DEPRECATED("Please use tusb_init(rhport, rh_init) instead")
+#endif
+TU_ATTR_ALWAYS_INLINE static inline bool tuh_init(uint8_t rhport) {
+ const tusb_rhport_init_t rh_init = {
+ .role = TUSB_ROLE_HOST,
+ .speed = TUH_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL,
+ };
+ return tuh_rhport_init(rhport, &rh_init);
+}
// Deinit host stack on rhport
bool tuh_deinit(uint8_t rhport);
@@ -136,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);
}
@@ -149,12 +174,10 @@ extern void hcd_int_handler(uint8_t rhport, bool in_isr);
#endif
// Interrupt handler alias to HCD with in_isr as optional parameter
-// - tuh_int_handler(rhport) --> hcd_int_handler(rhport, true)
-// - tuh_int_handler(rhport, in_isr) --> hcd_int_handler(rhport, in_isr)
-// Note: this is similar to TU_VERIFY(), _GET_3RD_ARG() is defined in tusb_verify.h
-#define _tuh_int_handler_1arg(_rhport) hcd_int_handler(_rhport, true)
-#define _tuh_int_hanlder_2arg(_rhport, _in_isr) hcd_int_handler(_rhport, _in_isr)
-#define tuh_int_handler(...) _GET_3RD_ARG(__VA_ARGS__, _tuh_int_hanlder_2arg, _tuh_int_handler_1arg, _dummy)(__VA_ARGS__)
+#define _tuh_int_handler_arg0() TU_VERIFY_STATIC(false, "tuh_int_handler() must have 1 or 2 arguments")
+#define _tuh_int_handler_arg1(_rhport) hcd_int_handler(_rhport, true)
+#define _tuh_int_handler_arg2(_rhport, _in_isr) hcd_int_handler(_rhport, _in_isr)
+#define tuh_int_handler(...) TU_FUNC_OPTIONAL_ARG(_tuh_int_handler, __VA_ARGS__)
// Check if roothub port is initialized and active as a host
bool tuh_rhport_is_active(uint8_t rhport);
@@ -175,17 +198,19 @@ tusb_speed_t tuh_speed_get(uint8_t daddr);
// Check if device is connected and configured
bool tuh_mounted(uint8_t daddr);
+// Check if device is connected which mean device has at least 1 successful transfer
+// Note: It may not be addressed/configured/mounted yet
+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);
}
@@ -206,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);
@@ -253,6 +281,13 @@ bool tuh_descriptor_get_hid_report(uint8_t daddr, uint8_t itf_num, uint8_t desc_
bool tuh_descriptor_get_string(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len,
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
+// Get language id string descriptor (control transfer)
+TU_ATTR_ALWAYS_INLINE static inline
+bool tuh_descriptor_get_string_langid(uint8_t daddr, void* buffer, uint16_t len,
+ tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
+ return tuh_descriptor_get_string(daddr, 0, 0, buffer, len, complete_cb, user_data);
+}
+
// Get manufacturer string descriptor (control transfer)
// true on success, false if there is on-going control transfer or incorrect parameters
bool tuh_descriptor_get_manufacturer_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len,
@@ -292,6 +327,12 @@ uint8_t tuh_descriptor_get_hid_report_sync(uint8_t daddr, uint8_t itf_num, uint8
// return transfer result
uint8_t tuh_descriptor_get_string_sync(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len);
+// Sync (blocking) version of tuh_descriptor_get_string_langid()
+TU_ATTR_ALWAYS_INLINE static inline
+uint8_t tuh_descriptor_get_string_langid_sync(uint8_t daddr, void* buffer, uint16_t len) {
+ return tuh_descriptor_get_string_sync(daddr, 0, 0, buffer, len);
+}
+
// Sync (blocking) version of tuh_descriptor_get_manufacturer_string()
// return transfer result
uint8_t tuh_descriptor_get_manufacturer_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len);
diff --git a/src/host/usbh_pvt.h b/src/host/usbh_pvt.h
index 95de915e9..bfa1fb2ba 100644
--- a/src/host/usbh_pvt.h
+++ b/src/host/usbh_pvt.h
@@ -41,10 +41,6 @@
#define TU_LOG_INT_USBH(...) TU_LOG_INT(CFG_TUH_LOG_LEVEL, __VA_ARGS__)
#define TU_LOG_HEX_USBH(...) TU_LOG_HEX(CFG_TUH_LOG_LEVEL, __VA_ARGS__)
-enum {
- USBH_EPSIZE_BULK_MAX = (TUH_OPT_HIGH_SPEED ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS)
-};
-
//--------------------------------------------------------------------+
// Class Driver API
//--------------------------------------------------------------------+
@@ -83,8 +79,8 @@ void usbh_defer_func(osal_task_func_t func, void *param, bool in_isr);
bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes,
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
-TU_ATTR_ALWAYS_INLINE
-static inline bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) {
+TU_ATTR_ALWAYS_INLINE static inline
+bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) {
return usbh_edpt_xfer_with_callback(dev_addr, ep_addr, buffer, total_bytes, NULL, 0);
}