summaryrefslogtreecommitdiff
path: root/src/host/hub.c
diff options
context:
space:
mode:
authorhathach <[email protected]>2020-11-02 00:54:04 +0700
committerhathach <[email protected]>2020-11-02 00:54:04 +0700
commit6eafdfab935be976ba3198f63a576ed0add05cb6 (patch)
tree6ba57935978641c32fa4c8e6fe03547dee8f0485 /src/host/hub.c
parente029d6d7263f4a848c5150a567512f3990358b3f (diff)
update usbh with hub to use async control transfer
work ok with msc + hub, but definitely need more testing.
Diffstat (limited to 'src/host/hub.c')
-rw-r--r--src/host/hub.c305
1 files changed, 210 insertions, 95 deletions
diff --git a/src/host/hub.c b/src/host/hub.c
index ed96713e2..f778ef203 100644
--- a/src/host/hub.c
+++ b/src/host/hub.c
@@ -33,21 +33,21 @@
//--------------------------------------------------------------------+
#include "hub.h"
-extern void osal_task_delay(uint32_t msec); // TODO remove
-
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
typedef struct
{
uint8_t itf_num;
- uint8_t ep_status;
- uint8_t port_number;
+ uint8_t ep_in;
+ uint8_t port_count;
uint8_t status_change; // data from status change interrupt endpoint
+
+ hub_port_status_response_t port_status;
}usbh_hub_t;
CFG_TUSB_MEM_SECTION static usbh_hub_t hub_data[CFG_TUSB_HOST_DEVICE_MAX];
-TU_ATTR_ALIGNED(4) CFG_TUSB_MEM_SECTION static uint8_t hub_enum_buffer[sizeof(descriptor_hub_desc_t)];
+TU_ATTR_ALIGNED(4) CFG_TUSB_MEM_SECTION static uint8_t _hub_buffer[sizeof(descriptor_hub_desc_t)];
//OSAL_SEM_DEF(hub_enum_semaphore);
//static osal_semaphore_handle_t hub_enum_sem_hdl;
@@ -55,25 +55,30 @@ TU_ATTR_ALIGNED(4) CFG_TUSB_MEM_SECTION static uint8_t hub_enum_buffer[sizeof(de
//--------------------------------------------------------------------+
// HUB
//--------------------------------------------------------------------+
-bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature)
+bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature, tuh_control_complete_cb_t complete_cb)
{
- TU_ASSERT(HUB_FEATURE_PORT_CONNECTION_CHANGE <= feature && feature <= HUB_FEATURE_PORT_RESET_CHANGE);
-
- tusb_control_request_t request = {
- .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_OTHER, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_OUT },
- .bRequest = HUB_REQUEST_CLEAR_FEATURE,
- .wValue = feature,
- .wIndex = hub_port,
- .wLength = 0
+ tusb_control_request_t const request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_OTHER,
+ .type = TUSB_REQ_TYPE_CLASS,
+ .direction = TUSB_DIR_OUT
+ },
+ .bRequest = HUB_REQUEST_CLEAR_FEATURE,
+ .wValue = feature,
+ .wIndex = hub_port,
+ .wLength = 0
};
- TU_ASSERT( usbh_control_xfer( hub_addr, &request, NULL ) );
+ TU_LOG2("HUB Clear Port Feature: addr = 0x%02X, port = %u, feature = %u\r\n", hub_addr, hub_port, feature);
+ TU_ASSERT( tuh_control_xfer(hub_addr, &request, NULL, complete_cb) );
return true;
}
-bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, hub_port_status_response_t* resp)
+bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp, tuh_control_complete_cb_t complete_cb)
{
- tusb_control_request_t request =
+ tusb_control_request_t const request =
{
.bmRequestType_bit =
{
@@ -81,31 +86,35 @@ bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, hub_port_status_res
.type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_IN
},
-
.bRequest = HUB_REQUEST_GET_STATUS,
.wValue = 0,
.wIndex = hub_port,
.wLength = 4
};
- TU_ASSERT( usbh_control_xfer( hub_addr, &request, hub_enum_buffer ) );
-
- memcpy(resp, hub_enum_buffer, sizeof(hub_port_status_response_t));
+ TU_LOG2("HUB Get Port Status: addr = 0x%02X, port = %u\r\n", hub_addr, hub_port);
+ TU_ASSERT( tuh_control_xfer( hub_addr, &request, resp, complete_cb) );
return true;
}
-bool hub_port_reset(uint8_t hub_addr, uint8_t hub_port)
+bool hub_port_reset(uint8_t hub_addr, uint8_t hub_port, tuh_control_complete_cb_t complete_cb)
{
- //------------- Set Port Reset -------------//
- tusb_control_request_t request = {
- .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_OTHER, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_OUT },
- .bRequest = HUB_REQUEST_SET_FEATURE,
- .wValue = HUB_FEATURE_PORT_RESET,
- .wIndex = hub_port,
- .wLength = 0
+ tusb_control_request_t const request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_OTHER,
+ .type = TUSB_REQ_TYPE_CLASS,
+ .direction = TUSB_DIR_OUT
+ },
+ .bRequest = HUB_REQUEST_SET_FEATURE,
+ .wValue = HUB_FEATURE_PORT_RESET,
+ .wIndex = hub_port,
+ .wLength = 0
};
- TU_ASSERT( usbh_control_xfer( hub_addr, &request, NULL ) );
+ TU_LOG2("HUB Reset Port: addr = 0x%02X, port = %u\r\n", hub_addr, hub_port);
+ TU_ASSERT( tuh_control_xfer(hub_addr, &request, NULL, complete_cb) );
return true;
}
@@ -133,42 +142,179 @@ bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf
TU_ASSERT(usbh_edpt_open(rhport, dev_addr, ep_desc));
hub_data[dev_addr-1].itf_num = itf_desc->bInterfaceNumber;
- hub_data[dev_addr-1].ep_status = ep_desc->bEndpointAddress;
+ hub_data[dev_addr-1].ep_in = ep_desc->bEndpointAddress;
(*p_length) = sizeof(tusb_desc_interface_t) + sizeof(tusb_desc_endpoint_t);
+ return true;
+}
+
+static bool config_get_hub_desc_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
+static bool config_port_power_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
+
+static bool config_get_hub_desc_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+ (void) request;
+ TU_ASSERT(XFER_RESULT_SUCCESS == result);
+
+ usbh_hub_t* p_hub = &hub_data[dev_addr-1];
+
+ // 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;
+
+ // May need to GET_STATUS
+
+ // Ports must be powered on to be able to detect connection
+ tusb_control_request_t const new_request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_OTHER,
+ .type = TUSB_REQ_TYPE_CLASS,
+ .direction = TUSB_DIR_OUT
+ },
+ .bRequest = HUB_REQUEST_SET_FEATURE,
+ .wValue = HUB_FEATURE_PORT_POWER,
+ .wIndex = 1, // starting with port 1
+ .wLength = 0
+ };
+
+ TU_ASSERT( tuh_control_xfer(dev_addr, &new_request, NULL, config_port_power_complete) );
+
+ return true;
+}
+
+static bool config_port_power_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+ TU_ASSERT(XFER_RESULT_SUCCESS == result);
+ usbh_hub_t* p_hub = &hub_data[dev_addr-1];
+
+ if (request->wIndex == p_hub->port_count)
+ {
+ // All ports are power -> queue notification status endpoint and
+ // complete the SET CONFIGURATION
+ TU_ASSERT( usbh_edpt_xfer(dev_addr, p_hub->ep_in, &p_hub->status_change, 1) );
+
+ usbh_driver_set_config_complete(dev_addr, p_hub->itf_num);
+ }else
+ {
+ tusb_control_request_t new_request = *request;
+ new_request.wIndex++; // power next port
+
+ TU_ASSERT( tuh_control_xfer(dev_addr, &new_request, NULL, config_port_power_complete) );
+ }
+
+ return true;
+}
+
+bool hub_set_config(uint8_t dev_addr, uint8_t itf_num)
+{
+ usbh_hub_t* p_hub = &hub_data[dev_addr-1];
+ TU_ASSERT(itf_num == p_hub->itf_num);
+
//------------- Get Hub Descriptor -------------//
- tusb_control_request_t request = {
- .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_IN },
- .bRequest = HUB_REQUEST_GET_DESCRIPTOR,
- .wValue = 0,
- .wIndex = 0,
- .wLength = sizeof(descriptor_hub_desc_t)
+ tusb_control_request_t request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_DEVICE,
+ .type = TUSB_REQ_TYPE_CLASS,
+ .direction = TUSB_DIR_IN
+ },
+ .bRequest = HUB_REQUEST_GET_DESCRIPTOR,
+ .wValue = 0,
+ .wIndex = 0,
+ .wLength = sizeof(descriptor_hub_desc_t)
};
- TU_ASSERT( usbh_control_xfer( dev_addr, &request, hub_enum_buffer ) );
+ TU_ASSERT( tuh_control_xfer(dev_addr, &request, _hub_buffer, config_get_hub_desc_complete) );
+
+ return true;
+}
+
+static bool connection_clear_conn_change_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
+static bool connection_get_status_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
+static bool connection_port_reset_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
+
+static bool connection_port_reset_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+ TU_ASSERT(result == XFER_RESULT_SUCCESS);
- // only care about this field in hub descriptor
- hub_data[dev_addr-1].port_number = ((descriptor_hub_desc_t*) hub_enum_buffer)->bNbrPorts;
+ // usbh_hub_t * p_hub = &hub_data[dev_addr-1];
+ uint8_t const port_num = (uint8_t) request->wIndex;
- //------------- Set Port_Power on all ports -------------//
- // TODO may only power port with attached
- request = (tusb_control_request_t ) {
- .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_OTHER, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_OUT },
- .bRequest = HUB_REQUEST_SET_FEATURE,
- .wValue = HUB_FEATURE_PORT_POWER,
- .wIndex = 0,
- .wLength = 0
+ // submit attach event
+ hcd_event_t event =
+ {
+ .rhport = usbh_get_rhport(dev_addr),
+ .event_id = HCD_EVENT_DEVICE_ATTACH,
+ .connection =
+ {
+ .hub_addr = dev_addr,
+ .hub_port = port_num
+ }
};
- for(uint8_t i=1; i <= hub_data[dev_addr-1].port_number; i++)
+ hcd_event_handler(&event, false);
+
+ return true;
+}
+
+static bool connection_clear_conn_change_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+ TU_ASSERT(result == XFER_RESULT_SUCCESS);
+
+ usbh_hub_t * p_hub = &hub_data[dev_addr-1];
+ uint8_t const port_num = (uint8_t) request->wIndex;
+
+ if ( p_hub->port_status.status.connection )
{
- request.wIndex = i;
- TU_ASSERT( usbh_control_xfer( dev_addr, &request, NULL ) );
+ // Reset port if attach event
+ hub_port_reset(dev_addr, port_num, connection_port_reset_complete);
+ }else
+ {
+ // submit detach event
+ hcd_event_t event =
+ {
+ .rhport = usbh_get_rhport(dev_addr),
+ .event_id = HCD_EVENT_DEVICE_REMOVE,
+ .connection =
+ {
+ .hub_addr = dev_addr,
+ .hub_port = port_num
+ }
+ };
+
+ hcd_event_handler(&event, false);
}
- // Queue notification status endpoint
- TU_ASSERT( usbh_edpt_xfer(dev_addr, hub_data[dev_addr-1].ep_status, &hub_data[dev_addr-1].status_change, 1) );
+ return true;
+}
+
+static bool connection_get_status_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+ TU_ASSERT(result == XFER_RESULT_SUCCESS);
+ usbh_hub_t * p_hub = &hub_data[dev_addr-1];
+ uint8_t const port_num = (uint8_t) request->wIndex;
+
+ // 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, );
+
+ // Acknowledge Port Connection Change
+ hub_port_clear_feature(dev_addr, port_num, HUB_FEATURE_PORT_CONNECTION_CHANGE, connection_clear_conn_change_complete);
+ }else
+ {
+ // Other changes are: Enable, Suspend, Over Current, Reset, L1 state
+ // TODO clear change
+
+ // prepare for next hub status
+ // TODO continue with status_change, or maybe we can do it again with status
+ hub_status_pipe_queue(dev_addr);
+ }
return true;
}
@@ -179,55 +325,23 @@ bool hub_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32
{
(void) xferred_bytes; // TODO can be more than 1 for hub with lots of ports
(void) ep_addr;
+ TU_ASSERT( result == XFER_RESULT_SUCCESS);
usbh_hub_t * p_hub = &hub_data[dev_addr-1];
- if ( result == XFER_RESULT_SUCCESS )
+ TU_LOG2("Port Status Change = 0x%02X\r\n", p_hub->status_change);
+ for (uint8_t port=1; port <= p_hub->port_count; port++)
{
- TU_LOG2("Port Status Change = 0x%02X\r\n", p_hub->status_change);
- for (uint8_t port=1; port <= p_hub->port_number; port++)
+ // TODO HUB ignore bit0 hub_status_change
+ if ( tu_bit_test(p_hub->status_change, port) )
{
- // TODO HUB ignore bit0 hub_status_change
- if ( tu_bit_test(p_hub->status_change, port) )
- {
- hub_port_status_response_t port_status;
- hub_port_get_status(dev_addr, port, &port_status);
-
- // Connection change
- if (port_status.change.connection)
- {
- // 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(dev_addr, port, HUB_FEATURE_PORT_CONNECTION_CHANGE);
-
- // Reset port if attach event
- if ( port_status.status.connection ) hub_port_reset(dev_addr, port);
-
- hcd_event_t event =
- {
- .rhport = _usbh_devices[dev_addr].rhport,
- .event_id = port_status.status.connection ? HCD_EVENT_DEVICE_ATTACH : HCD_EVENT_DEVICE_REMOVE,
- .connection =
- {
- .hub_addr = dev_addr,
- .hub_port = port
- }
- };
-
- hcd_event_handler(&event, true);
- }
- }
+ hub_port_get_status(dev_addr, port, &p_hub->port_status, connection_get_status_complete);
+ break;
}
- // NOTE: next status transfer is queued by usbh.c after handling this request
- }
- else
- {
- // TODO [HUB] check if hub is still plugged before polling status endpoint since failed usually mean hub unplugged
-// TU_ASSERT ( hub_status_pipe_queue(dev_addr) );
}
+ // NOTE: next status transfer is queued by usbh.c after handling this request
+
return true;
}
@@ -239,7 +353,8 @@ void hub_close(uint8_t dev_addr)
bool hub_status_pipe_queue(uint8_t dev_addr)
{
- return hcd_pipe_xfer(dev_addr, hub_data[dev_addr-1].ep_status, &hub_data[dev_addr-1].status_change, 1, true);
+ usbh_hub_t * p_hub = &hub_data[dev_addr-1];
+ return hcd_pipe_xfer(dev_addr, p_hub->ep_in, &p_hub->status_change, 1, true);
}