2014-03-12 14:08:52 +07:00
|
|
|
/**************************************************************************/
|
|
|
|
/*!
|
|
|
|
@file hub.c
|
|
|
|
@author hathach (tinyusb.org)
|
|
|
|
|
|
|
|
@section LICENSE
|
|
|
|
|
|
|
|
Software License Agreement (BSD License)
|
|
|
|
|
|
|
|
Copyright (c) 2013, hathach (tinyusb.org)
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
3. Neither the name of the copyright holders nor the
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
|
|
|
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
|
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
This file is part of the tinyusb stack.
|
|
|
|
*/
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#include "tusb_option.h"
|
|
|
|
|
2018-12-06 21:46:34 +07:00
|
|
|
#if (MODE_HOST_SUPPORTED && CFG_TUH_HUB)
|
2014-03-12 14:08:52 +07:00
|
|
|
|
|
|
|
#define _TINY_USB_SOURCE_FILE_
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// INCLUDE
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
#include "hub.h"
|
|
|
|
#include "usbh_hub.h"
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// MACRO CONSTANT TYPEDEF
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
typedef struct {
|
|
|
|
pipe_handle_t pipe_status;
|
|
|
|
uint8_t interface_number;
|
|
|
|
uint8_t port_number;
|
|
|
|
uint8_t status_change; // data from status change interrupt endpoint
|
|
|
|
}usbh_hub_t;
|
|
|
|
|
2018-11-22 17:21:07 +07:00
|
|
|
CFG_TUSB_MEM_SECTION STATIC_VAR usbh_hub_t hub_data[CFG_TUSB_HOST_DEVICE_MAX];
|
|
|
|
ATTR_ALIGNED(4) CFG_TUSB_MEM_SECTION STATIC_VAR uint8_t hub_enum_buffer[sizeof(descriptor_hub_desc_t)];
|
2014-03-12 14:08:52 +07:00
|
|
|
|
|
|
|
//OSAL_SEM_DEF(hub_enum_semaphore);
|
|
|
|
//static osal_semaphore_handle_t hub_enum_sem_hdl;
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// HUB
|
|
|
|
//--------------------------------------------------------------------+
|
2018-12-06 22:47:49 +07:00
|
|
|
bool hub_port_clear_feature_subtask(uint8_t hub_addr, uint8_t hub_port, uint8_t feature)
|
2014-03-12 14:08:52 +07:00
|
|
|
{
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT(HUB_FEATURE_PORT_CONNECTION_CHANGE <= feature && feature <= HUB_FEATURE_PORT_RESET_CHANGE);
|
2014-03-12 14:08:52 +07:00
|
|
|
|
|
|
|
//------------- Clear Port Feature request -------------//
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT( usbh_control_xfer_subtask( hub_addr, bm_request_type(TUSB_DIR_OUT, TUSB_REQ_TYPE_CLASS, TUSB_REQ_RCPT_OTHER),
|
|
|
|
HUB_REQUEST_CLEAR_FEATURE, feature, hub_port,
|
|
|
|
0, NULL ) );
|
2014-03-12 14:08:52 +07:00
|
|
|
|
|
|
|
//------------- Get Port Status to check if feature is cleared -------------//
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT( usbh_control_xfer_subtask( hub_addr, bm_request_type(TUSB_DIR_IN, TUSB_REQ_TYPE_CLASS, TUSB_REQ_RCPT_OTHER),
|
|
|
|
HUB_REQUEST_GET_STATUS, 0, hub_port,
|
|
|
|
4, hub_enum_buffer ) );
|
2014-03-12 14:08:52 +07:00
|
|
|
|
|
|
|
//------------- Check if feature is cleared -------------//
|
2014-03-14 00:06:43 +07:00
|
|
|
hub_port_status_response_t * p_port_status;
|
|
|
|
p_port_status = (hub_port_status_response_t *) hub_enum_buffer;
|
|
|
|
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT( !BIT_TEST_(p_port_status->status_change.value, feature-16) );
|
2014-03-12 14:08:52 +07:00
|
|
|
|
2018-12-06 22:47:49 +07:00
|
|
|
return true;
|
2014-03-12 14:08:52 +07:00
|
|
|
}
|
|
|
|
|
2018-12-06 22:47:49 +07:00
|
|
|
bool hub_port_reset_subtask(uint8_t hub_addr, uint8_t hub_port)
|
2014-03-12 14:08:52 +07:00
|
|
|
{
|
2014-04-28 11:51:10 +07:00
|
|
|
enum { RESET_DELAY = 200 }; // USB specs say only 50ms but many devices require much longer
|
2014-03-12 14:08:52 +07:00
|
|
|
tusb_error_t error;
|
|
|
|
|
|
|
|
//------------- Set Port Reset -------------//
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT( usbh_control_xfer_subtask( hub_addr, bm_request_type(TUSB_DIR_OUT, TUSB_REQ_TYPE_CLASS, TUSB_REQ_RCPT_OTHER),
|
2014-03-12 14:08:52 +07:00
|
|
|
HUB_REQUEST_SET_FEATURE, HUB_FEATURE_PORT_RESET, hub_port,
|
2018-12-06 22:47:49 +07:00
|
|
|
0, NULL ) );
|
2014-03-12 14:08:52 +07:00
|
|
|
|
2014-04-28 11:51:10 +07:00
|
|
|
osal_task_delay(RESET_DELAY); // TODO Hub wait for Status Endpoint on Reset Change
|
2014-03-12 14:08:52 +07:00
|
|
|
|
|
|
|
//------------- Get Port Status to check if port is enabled, powered and reset_change -------------//
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT( usbh_control_xfer_subtask( hub_addr, bm_request_type(TUSB_DIR_IN, TUSB_REQ_TYPE_CLASS, TUSB_REQ_RCPT_OTHER),
|
|
|
|
HUB_REQUEST_GET_STATUS, 0, hub_port,
|
|
|
|
4, hub_enum_buffer ) );
|
2014-03-12 14:08:52 +07:00
|
|
|
|
2014-03-14 00:06:43 +07:00
|
|
|
hub_port_status_response_t * p_port_status;
|
|
|
|
p_port_status = (hub_port_status_response_t *) hub_enum_buffer;
|
|
|
|
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT ( p_port_status->status_change.reset && p_port_status->status_current.connect_status &&
|
|
|
|
p_port_status->status_current.port_power && p_port_status->status_current.port_enable);
|
2014-03-12 14:08:52 +07:00
|
|
|
|
2018-12-06 22:47:49 +07:00
|
|
|
return true;
|
2014-03-12 14:08:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// can only get the speed RIGHT AFTER hub_port_reset_subtask call
|
|
|
|
tusb_speed_t hub_port_get_speed(void)
|
|
|
|
{
|
|
|
|
hub_port_status_response_t * p_port_status = (hub_port_status_response_t *) hub_enum_buffer;
|
|
|
|
return (p_port_status->status_current.high_speed_device_attached) ? TUSB_SPEED_HIGH :
|
|
|
|
(p_port_status->status_current.low_speed_device_attached ) ? TUSB_SPEED_LOW : TUSB_SPEED_FULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// CLASS-USBH API (don't require to verify parameters)
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
void hub_init(void)
|
|
|
|
{
|
2018-10-23 12:19:32 +07:00
|
|
|
tu_memclr(hub_data, CFG_TUSB_HOST_DEVICE_MAX*sizeof(usbh_hub_t));
|
2014-03-12 14:08:52 +07:00
|
|
|
// hub_enum_sem_hdl = osal_semaphore_create( OSAL_SEM_REF(hub_enum_semaphore) );
|
|
|
|
}
|
|
|
|
|
2018-12-06 22:47:49 +07:00
|
|
|
bool hub_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length)
|
2014-03-12 14:08:52 +07:00
|
|
|
{
|
|
|
|
// not support multiple TT yet
|
2018-12-06 22:47:49 +07:00
|
|
|
if ( p_interface_desc->bInterfaceProtocol > 1 ) return false;
|
2014-03-12 14:08:52 +07:00
|
|
|
|
|
|
|
//------------- Open Interrupt Status Pipe -------------//
|
2018-03-23 12:32:40 +07:00
|
|
|
tusb_desc_endpoint_t const *p_endpoint;
|
|
|
|
p_endpoint = (tusb_desc_endpoint_t const *) descriptor_next( (uint8_t const*) p_interface_desc );
|
2014-03-14 00:06:43 +07:00
|
|
|
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint->bDescriptorType);
|
|
|
|
TU_ASSERT(TUSB_XFER_INTERRUPT == p_endpoint->bmAttributes.xfer);
|
2014-03-12 14:08:52 +07:00
|
|
|
|
|
|
|
hub_data[dev_addr-1].pipe_status = hcd_pipe_open(dev_addr, p_endpoint, TUSB_CLASS_HUB);
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT( pipehandle_is_valid(hub_data[dev_addr-1].pipe_status) );
|
2014-03-12 14:08:52 +07:00
|
|
|
hub_data[dev_addr-1].interface_number = p_interface_desc->bInterfaceNumber;
|
|
|
|
|
2018-03-23 12:32:40 +07:00
|
|
|
(*p_length) = sizeof(tusb_desc_interface_t) + sizeof(tusb_desc_endpoint_t);
|
2014-03-12 14:08:52 +07:00
|
|
|
|
|
|
|
//------------- Get Hub Descriptor -------------//
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT( usbh_control_xfer_subtask( dev_addr, bm_request_type(TUSB_DIR_IN, TUSB_REQ_TYPE_CLASS, TUSB_REQ_RCPT_DEVICE),
|
|
|
|
HUB_REQUEST_GET_DESCRIPTOR, 0, 0,
|
|
|
|
sizeof(descriptor_hub_desc_t), hub_enum_buffer ) );
|
2014-03-12 14:08:52 +07:00
|
|
|
|
|
|
|
// only care about this field in hub descriptor
|
|
|
|
hub_data[dev_addr-1].port_number = ((descriptor_hub_desc_t*) hub_enum_buffer)->bNbrPorts;
|
|
|
|
|
|
|
|
//------------- Set Port_Power on all ports -------------//
|
|
|
|
static uint8_t i;
|
|
|
|
for(i=1; i <= hub_data[dev_addr-1].port_number; i++)
|
|
|
|
{
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT( usbh_control_xfer_subtask( dev_addr, bm_request_type(TUSB_DIR_OUT, TUSB_REQ_TYPE_CLASS, TUSB_REQ_RCPT_OTHER),
|
|
|
|
HUB_REQUEST_SET_FEATURE, HUB_FEATURE_PORT_POWER, i,
|
|
|
|
0, NULL ) );
|
2014-03-12 14:08:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------- Queue the initial Status endpoint transfer -------------//
|
2018-12-06 22:47:49 +07:00
|
|
|
TU_ASSERT( TUSB_ERROR_NONE == hcd_pipe_xfer(hub_data[dev_addr-1].pipe_status, &hub_data[dev_addr-1].status_change, 1, true) );
|
2014-03-12 14:08:52 +07:00
|
|
|
|
2018-12-06 22:47:49 +07:00
|
|
|
return true;
|
2014-03-12 14:08:52 +07:00
|
|
|
}
|
|
|
|
|
2014-03-14 14:33:50 +07:00
|
|
|
// is the response of interrupt endpoint polling
|
2018-11-23 15:14:47 +07:00
|
|
|
void hub_isr(pipe_handle_t pipe_hdl, xfer_result_t event, uint32_t xferred_bytes)
|
2014-03-12 14:08:52 +07:00
|
|
|
{
|
2014-03-14 14:33:50 +07:00
|
|
|
(void) xferred_bytes; // TODO can be more than 1 for hub with lots of ports
|
|
|
|
|
2014-03-12 14:08:52 +07:00
|
|
|
usbh_hub_t * p_hub = &hub_data[pipe_hdl.dev_addr-1];
|
|
|
|
|
2018-11-23 15:17:43 +07:00
|
|
|
if ( event == XFER_RESULT_SUCCESS )
|
2014-03-14 14:33:50 +07:00
|
|
|
{
|
|
|
|
for (uint8_t port=1; port <= p_hub->port_number; port++)
|
|
|
|
{ // TODO HUB ignore bit0 hub_status_change
|
|
|
|
if ( BIT_TEST_(p_hub->status_change, port) )
|
|
|
|
{
|
|
|
|
usbh_hub_port_plugged_isr(pipe_hdl.dev_addr, port);
|
2014-04-28 11:51:10 +07:00
|
|
|
break; // handle one port at a time, next port if any will be handled in the next cycle
|
2014-03-14 14:33:50 +07:00
|
|
|
}
|
2014-03-12 14:08:52 +07:00
|
|
|
}
|
2014-03-14 14:33:50 +07:00
|
|
|
// 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
|
2018-03-29 13:07:27 +07:00
|
|
|
// TU_ASSERT ( TUSB_ERROR_NONE == hcd_pipe_xfer(pipe_hdl, &p_hub->status_change, 1, true) );
|
2014-03-12 14:08:52 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hub_close(uint8_t dev_addr)
|
|
|
|
{
|
|
|
|
(void) hcd_pipe_close(hub_data[dev_addr-1].pipe_status);
|
2018-10-23 12:19:32 +07:00
|
|
|
tu_memclr(&hub_data[dev_addr-1], sizeof(usbh_hub_t));
|
2014-03-12 14:08:52 +07:00
|
|
|
|
|
|
|
// osal_semaphore_reset(hub_enum_sem_hdl);
|
|
|
|
}
|
|
|
|
|
|
|
|
tusb_error_t hub_status_pipe_queue(uint8_t dev_addr)
|
|
|
|
{
|
|
|
|
return hcd_pipe_xfer(hub_data[dev_addr-1].pipe_status, &hub_data[dev_addr-1].status_change, 1, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|