mirror of
https://github.com/hathach/tinyusb.git
synced 2025-01-17 05:32:55 +08:00
update hub
This commit is contained in:
parent
65e5872d81
commit
4b2f32b778
224
src/host/hub.c
224
src/host/hub.c
@ -144,7 +144,6 @@ bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp, tuh_con
|
||||
void hub_init(void)
|
||||
{
|
||||
tu_memclr(hub_data, CFG_TUSB_HOST_DEVICE_MAX*sizeof( hub_interface_t));
|
||||
// hub_enum_sem_hdl = osal_semaphore_create( OSAL_SEM_REF(hub_enum_semaphore) );
|
||||
}
|
||||
|
||||
bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t *p_length)
|
||||
@ -169,19 +168,56 @@ bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf
|
||||
return true;
|
||||
}
|
||||
|
||||
void hub_close(uint8_t dev_addr)
|
||||
{
|
||||
tu_memclr(&hub_data[dev_addr-1], sizeof( hub_interface_t));
|
||||
}
|
||||
|
||||
bool hub_status_pipe_queue(uint8_t dev_addr)
|
||||
{
|
||||
hub_interface_t * p_hub = &hub_data[dev_addr-1];
|
||||
return usbh_edpt_xfer(dev_addr, p_hub->ep_in, &p_hub->status_change, 1);
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Set Configure
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
static bool config_get_hub_desc_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
|
||||
static bool config_set_port_power (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)
|
||||
bool hub_set_config(uint8_t dev_addr, uint8_t itf_num)
|
||||
{
|
||||
hub_interface_t* p_hub = &hub_data[dev_addr-1];
|
||||
TU_ASSERT(itf_num == p_hub->itf_num);
|
||||
|
||||
// Get Hub Descriptor
|
||||
tusb_control_request_t const 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( tuh_control_xfer(dev_addr, &request, _hub_buffer, config_set_port_power) );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool config_set_port_power (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
|
||||
{
|
||||
(void) request;
|
||||
TU_ASSERT(XFER_RESULT_SUCCESS == result);
|
||||
|
||||
hub_interface_t* p_hub = &hub_data[dev_addr-1];
|
||||
hub_interface_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;
|
||||
@ -189,26 +225,9 @@ static bool config_get_hub_desc_complete (uint8_t dev_addr, tusb_control_request
|
||||
|
||||
// May need to GET_STATUS
|
||||
|
||||
// Set Port Power 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_LOG(2, "HUB Set Port Power: port = %u\r\n", new_request.wIndex);
|
||||
|
||||
TU_ASSERT( tuh_control_xfer(dev_addr, &new_request, NULL, config_port_power_complete) );
|
||||
|
||||
return true;
|
||||
// Set Port Power to be able to detect connection, starting with port 1
|
||||
uint8_t const hub_port = 1;
|
||||
return hub_port_set_feature(dev_addr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete);
|
||||
}
|
||||
|
||||
static bool config_port_power_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
|
||||
@ -225,70 +244,71 @@ static bool config_port_power_complete (uint8_t dev_addr, tusb_control_request_t
|
||||
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_LOG(2, "HUB Set Port Power: port = %u\r\n", new_request.wIndex);
|
||||
|
||||
TU_ASSERT( tuh_control_xfer(dev_addr, &new_request, NULL, config_port_power_complete) );
|
||||
// power next port
|
||||
uint8_t const hub_port = (uint8_t) (request->wIndex + 1);
|
||||
return hub_port_set_feature(dev_addr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool hub_set_config(uint8_t dev_addr, uint8_t itf_num)
|
||||
{
|
||||
hub_interface_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)
|
||||
};
|
||||
|
||||
TU_ASSERT( tuh_control_xfer(dev_addr, &request, _hub_buffer, config_get_hub_desc_complete) );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Connection Changes
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
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_clear_conn_change_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)
|
||||
// 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
|
||||
(void) ep_addr;
|
||||
TU_ASSERT(result == XFER_RESULT_SUCCESS);
|
||||
|
||||
// usbh_hub_t * p_hub = &hub_data[dev_addr-1];
|
||||
hub_interface_t * p_hub = &hub_data[dev_addr-1];
|
||||
|
||||
TU_LOG2(" Port Status Change = 0x%02X\r\n", p_hub->status_change);
|
||||
|
||||
// Hub ignore bit0 in status change
|
||||
for (uint8_t port=1; port <= p_hub->port_count; port++)
|
||||
{
|
||||
if ( tu_bit_test(p_hub->status_change, port) )
|
||||
{
|
||||
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
|
||||
|
||||
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);
|
||||
hub_interface_t * p_hub = &hub_data[dev_addr-1];
|
||||
uint8_t const port_num = (uint8_t) request->wIndex;
|
||||
|
||||
// submit attach event
|
||||
hcd_event_t event =
|
||||
// Connection change
|
||||
if (p_hub->port_status.change.connection)
|
||||
{
|
||||
.rhport = usbh_get_rhport(dev_addr),
|
||||
.event_id = HCD_EVENT_DEVICE_ATTACH,
|
||||
.connection =
|
||||
{
|
||||
.hub_addr = dev_addr,
|
||||
.hub_port = port_num
|
||||
}
|
||||
};
|
||||
// Port is powered and enabled
|
||||
//TU_VERIFY(port_status.status_current.port_power && port_status.status_current.port_enable, );
|
||||
|
||||
hcd_event_handler(&event, false);
|
||||
// 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;
|
||||
}
|
||||
@ -324,70 +344,28 @@ static bool connection_clear_conn_change_complete (uint8_t dev_addr, tusb_contro
|
||||
return true;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
TU_ASSERT(result == XFER_RESULT_SUCCESS);
|
||||
hub_interface_t * p_hub = &hub_data[dev_addr-1];
|
||||
|
||||
// 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)
|
||||
// submit attach event
|
||||
hcd_event_t event =
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
|
||||
// is the response of interrupt endpoint polling
|
||||
#include "usbh_hcd.h" // FIXME remove
|
||||
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
|
||||
(void) ep_addr;
|
||||
TU_ASSERT( result == XFER_RESULT_SUCCESS);
|
||||
|
||||
hub_interface_t * p_hub = &hub_data[dev_addr-1];
|
||||
|
||||
TU_LOG2(" Port Status Change = 0x%02X\r\n", p_hub->status_change);
|
||||
|
||||
// Hub ignore bit0 in status change
|
||||
for (uint8_t port=1; port <= p_hub->port_count; port++)
|
||||
{
|
||||
if ( tu_bit_test(p_hub->status_change, port) )
|
||||
.rhport = usbh_get_rhport(dev_addr),
|
||||
.event_id = HCD_EVENT_DEVICE_ATTACH,
|
||||
.connection =
|
||||
{
|
||||
hub_port_get_status(dev_addr, port, &p_hub->port_status, connection_get_status_complete);
|
||||
break;
|
||||
.hub_addr = dev_addr,
|
||||
.hub_port = port_num
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// NOTE: next status transfer is queued by usbh.c after handling this request
|
||||
hcd_event_handler(&event, false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void hub_close(uint8_t dev_addr)
|
||||
{
|
||||
tu_memclr(&hub_data[dev_addr-1], sizeof( hub_interface_t));
|
||||
}
|
||||
|
||||
bool hub_status_pipe_queue(uint8_t dev_addr)
|
||||
{
|
||||
hub_interface_t * p_hub = &hub_data[dev_addr-1];
|
||||
return usbh_edpt_xfer(dev_addr, p_hub->ep_in, &p_hub->status_change, 1);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -455,7 +455,7 @@ void hcd_event_device_attach(uint8_t rhport, bool in_isr)
|
||||
{
|
||||
hcd_event_t event =
|
||||
{
|
||||
.rhport = rhport,
|
||||
.rhport = rhport,
|
||||
.event_id = HCD_EVENT_DEVICE_ATTACH
|
||||
};
|
||||
|
||||
@ -469,7 +469,7 @@ void hcd_event_device_remove(uint8_t hostid, bool in_isr)
|
||||
{
|
||||
hcd_event_t event =
|
||||
{
|
||||
.rhport = hostid,
|
||||
.rhport = hostid,
|
||||
.event_id = HCD_EVENT_DEVICE_REMOVE
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user