summaryrefslogtreecommitdiff
path: root/components/drivers/usb/usbhost/core/hub.c
diff options
context:
space:
mode:
Diffstat (limited to 'components/drivers/usb/usbhost/core/hub.c')
-rw-r--r--components/drivers/usb/usbhost/core/hub.c186
1 files changed, 93 insertions, 93 deletions
diff --git a/components/drivers/usb/usbhost/core/hub.c b/components/drivers/usb/usbhost/core/hub.c
index eefa571472..b6381ec300 100644
--- a/components/drivers/usb/usbhost/core/hub.c
+++ b/components/drivers/usb/usbhost/core/hub.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -77,7 +77,7 @@ static rt_err_t root_hub_ctrl(struct uhcd *hcd, rt_uint16_t port, rt_uint8_t cmd
return RT_ERROR;
}
return RT_EOK;
-}
+}
void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS)
{
struct uhost_msg msg;
@@ -106,23 +106,23 @@ void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port)
}
/**
- * This function will do USB_REQ_GET_DESCRIPTOR bRequest for the device instance
+ * This function will do USB_REQ_GET_DESCRIPTOR bRequest for the device instance
* to get usb hub descriptor.
*
* @param intf the interface instance.
* @buffer the data buffer to save usb hub descriptor.
* @param nbytes the size of buffer
- *
+ *
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer, rt_size_t nbytes)
{
struct urequest setup;
int timeout = USB_TIMEOUT_BASIC;
-
+
/* parameter check */
RT_ASSERT(device != RT_NULL);
-
+
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE;
setup.bRequest = USB_REQ_GET_DESCRIPTOR;
setup.wIndex = 0;
@@ -135,24 +135,24 @@ rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer
{
return RT_EOK;
}
- }
- return -RT_FALSE;
+ }
+ return -RT_FALSE;
}
/**
- * This function will do USB_REQ_GET_STATUS bRequest for the device instance
+ * This function will do USB_REQ_GET_STATUS bRequest for the device instance
* to get usb hub status.
*
* @param intf the interface instance.
* @buffer the data buffer to save usb hub status.
- *
+ *
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint32_t* buffer)
{
struct urequest setup;
int timeout = USB_TIMEOUT_BASIC;
-
+
/* parameter check */
RT_ASSERT(device != RT_NULL);
@@ -168,31 +168,31 @@ rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint32_t* buffer)
return RT_EOK;
}
}
- return -RT_FALSE;
+ return -RT_FALSE;
}
/**
- * This function will do USB_REQ_GET_STATUS bRequest for the device instance
+ * This function will do USB_REQ_GET_STATUS bRequest for the device instance
* to get hub port status.
*
* @param intf the interface instance.
* @port the hub port to get status.
* @buffer the data buffer to save usb hub status.
- *
+ *
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port, rt_uint32_t* buffer)
{
struct urequest setup;
int timeout = USB_TIMEOUT_BASIC;
-
+
/* parameter check */
RT_ASSERT(hub != RT_NULL);
/* get roothub port status */
if(hub->is_roothub)
{
- root_hub_ctrl(hub->hcd, port, RH_GET_PORT_STATUS,
+ root_hub_ctrl(hub->hcd, port, RH_GET_PORT_STATUS,
(void*)buffer);
return RT_EOK;
}
@@ -210,36 +210,36 @@ rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port, rt_uint32_t*
return RT_EOK;
}
}
- return -RT_FALSE;
+ return -RT_FALSE;
}
/**
- * This function will do USB_REQ_CLEAR_FEATURE bRequest for the device instance
+ * This function will do USB_REQ_CLEAR_FEATURE bRequest for the device instance
* to clear feature of the hub port.
*
* @param intf the interface instance.
* @port the hub port.
* @feature feature to be cleared.
- *
+ *
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port, rt_uint16_t feature)
{
struct urequest setup;
int timeout = USB_TIMEOUT_BASIC;
-
+
/* parameter check */
RT_ASSERT(hub != RT_NULL);
/* clear roothub feature */
if(hub->is_roothub)
{
- root_hub_ctrl(hub->hcd, port, RH_CLEAR_PORT_FEATURE,
+ root_hub_ctrl(hub->hcd, port, RH_CLEAR_PORT_FEATURE,
(void*)(rt_uint32_t)feature);
return RT_EOK;
}
- setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
+ setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
USB_REQ_TYPE_OTHER;
setup.bRequest = USB_REQ_CLEAR_FEATURE;
setup.wIndex = port;
@@ -250,37 +250,37 @@ rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port, rt_uint16_
{
return RT_EOK;
}
- return -RT_FALSE;
+ return -RT_FALSE;
}
/**
- * This function will do USB_REQ_SET_FEATURE bRequest for the device instance
+ * This function will do USB_REQ_SET_FEATURE bRequest for the device instance
* to set feature of the hub port.
*
* @param intf the interface instance.
* @port the hub port.
* @feature feature to be set.
- *
+ *
* @return the error code, RT_EOK on successfully.
*/
-rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port,
+rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port,
rt_uint16_t feature)
{
struct urequest setup;
int timeout = USB_TIMEOUT_BASIC;
-
+
/* parameter check */
RT_ASSERT(hub != RT_NULL);
/* clear roothub feature */
if(hub->is_roothub)
{
- root_hub_ctrl(hub->hcd, port, RH_SET_PORT_FEATURE,
+ root_hub_ctrl(hub->hcd, port, RH_SET_PORT_FEATURE,
(void*)(rt_uint32_t)feature);
return RT_EOK;
}
- setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
+ setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
USB_REQ_TYPE_OTHER;
setup.bRequest = USB_REQ_SET_FEATURE;
setup.wIndex = port;
@@ -291,7 +291,7 @@ rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port,
{
return RT_EOK;
}
- else return -RT_FALSE;
+ else return -RT_FALSE;
}
/**
@@ -299,17 +299,17 @@ rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port,
*
* @param intf the interface instance.
* @param port the hub port.
- *
+ *
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_hub_reset_port(uhub_t hub, rt_uint16_t port)
{
rt_err_t ret;
rt_uint32_t pstatus;
-
+
/* parameter check */
RT_ASSERT(hub != RT_NULL);
-
+
rt_thread_delay(50);
/* reset hub port */
@@ -321,12 +321,12 @@ rt_err_t rt_usbh_hub_reset_port(uhub_t hub, rt_uint16_t port)
ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
if(!(pstatus & PORT_PRS)) break;
}
-
+
/* clear port reset feature */
- ret = rt_usbh_hub_clear_port_feature(hub, port, PORT_FEAT_C_RESET);
+ ret = rt_usbh_hub_clear_port_feature(hub, port, PORT_FEAT_C_RESET);
if(ret != RT_EOK) return ret;
- rt_thread_delay(50);
+ rt_thread_delay(50);
return RT_EOK;
}
@@ -336,7 +336,7 @@ rt_err_t rt_usbh_hub_reset_port(uhub_t hub, rt_uint16_t port)
*
* @param device the usb instance.
* @param port the hub port.
- *
+ *
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port)
@@ -356,15 +356,15 @@ rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port)
{
ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
if(ret != RT_EOK) return ret;
-
- if(!(pstatus & PORT_CCS))
+
+ if(!(pstatus & PORT_CCS))
{
connect = RT_FALSE;
break;
}
-
+
rt_thread_delay(delayticks);
- }
+ }
if(connect) return RT_EOK;
else return -RT_ERROR;
@@ -375,7 +375,7 @@ rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port)
* disconnect events.
*
* @param intf the interface instance.
- *
+ *
* @return the error code, RT_EOK on successfully.
*/
static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
@@ -386,7 +386,7 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
/* parameter check */
RT_ASSERT(hub != RT_NULL);
- /* get usb device instance */
+ /* get usb device instance */
for (i = 0; i < hub->num_ports; i++)
{
rt_err_t ret;
@@ -394,7 +394,7 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
rt_uint32_t pstatus = 0;
reconnect = RT_FALSE;
-
+
/* get hub port status */
ret = rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
if(ret != RT_EOK) continue;
@@ -402,8 +402,8 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
RT_DEBUG_LOG(RT_DEBUG_USB, ("port %d status 0x%x\n", i + 1, pstatus));
/* check port status change */
- if (pstatus & PORT_CCSC)
- {
+ if (pstatus & PORT_CCSC)
+ {
/* clear port status change feature */
rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_CONNECTION);
reconnect = RT_TRUE;
@@ -411,39 +411,39 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
if(pstatus & PORT_PESC)
{
- rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_ENABLE);
+ rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_ENABLE);
reconnect = RT_TRUE;
}
-
+
if(reconnect)
- {
+ {
if(hub->child[i] != RT_NULL && hub->child[i]->status != DEV_STATUS_IDLE)
- {
+ {
rt_usbh_detach_instance(hub->child[i]);
/* Child device have been detach. Set hub->child[i] to NULL. */
hub->child[i] = RT_NULL;
}
-
+
ret = rt_usbh_hub_port_debounce(hub, i + 1);
if(ret != RT_EOK) continue;
-
+
/* allocate an usb instance for new connected device */
device = rt_usbh_alloc_instance(hub->hcd);
if(device == RT_NULL) break;
-
+
/* set usb device speed */
device->speed = (pstatus & PORT_LSDA) ? 1 : 0;
- device->parent_hub = hub;
+ device->parent_hub = hub;
device->hcd = hub->hcd;
device->port = i + 1;
hub->child[i] = device;
/* reset usb roothub port */
rt_usbh_hub_reset_port(hub, i + 1);
-
+
/* attatch the usb instance to the hcd */
- rt_usbh_attatch_instance(device);
+ rt_usbh_attatch_instance(device);
}
}
@@ -454,7 +454,7 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
* This function is the callback function of hub's int endpoint, it is invoked when data comes.
*
* @param context the context of the callback function.
- *
+ *
* @return none.
*/
static void rt_usbh_hub_irq(void* context)
@@ -462,9 +462,9 @@ static void rt_usbh_hub_irq(void* context)
upipe_t pipe;
uhub_t hub;
int timeout = USB_TIMEOUT_BASIC;
-
+
RT_ASSERT(context != RT_NULL);
-
+
pipe = (upipe_t)context;
hub = (uhub_t)pipe->user_data;
@@ -473,14 +473,14 @@ static void rt_usbh_hub_irq(void* context)
RT_DEBUG_LOG(RT_DEBUG_USB,("hub irq error\n"));
return;
}
-
+
rt_usbh_hub_port_change(hub);
RT_DEBUG_LOG(RT_DEBUG_USB,("hub int xfer...\n"));
/* parameter check */
RT_ASSERT(pipe->inst->hcd != RT_NULL);
-
+
rt_usb_hcd_pipe_xfer(hub->self->hcd, pipe, hub->buffer, pipe->ep.wMaxPacketSize, timeout);
}
@@ -489,7 +489,7 @@ static void rt_usbh_hub_irq(void* context)
* as a hub class device, it will continue to do the enumulate process.
*
* @param arg the argument.
- *
+ *
* @return the error code, RT_EOK on successfully.
*/
@@ -505,7 +505,7 @@ static rt_err_t rt_usbh_hub_enable(void *arg)
int timeout = USB_TIMEOUT_LONG;
/* paremeter check */
RT_ASSERT(intf != RT_NULL);
-
+
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_run\n"));
/* get usb device instance */
@@ -515,7 +515,7 @@ static rt_err_t rt_usbh_hub_enable(void *arg)
hub = rt_malloc(sizeof(struct uhub));
RT_ASSERT(hub != RT_NULL);
rt_memset(hub, 0, sizeof(struct uhub));
-
+
/* make interface instance's user data point to hub instance */
intf->user_data = (void*)hub;
@@ -524,17 +524,17 @@ static rt_err_t rt_usbh_hub_enable(void *arg)
if(ret != RT_EOK)
{
rt_kprintf("get hub descriptor failed\n");
- return -RT_ERROR;
+ return -RT_ERROR;
}
/* get full hub descriptor */
- ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc,
+ ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc,
hub->hub_desc.length);
if(ret != RT_EOK)
{
rt_kprintf("get hub descriptor again failed\n");
- return -RT_ERROR;
- }
+ return -RT_ERROR;
+ }
/* get hub ports number */
/* If hub device supported ports over USB_HUB_PORT_NUM(Ex: 8 port hub). Set hub->num_ports to USB_HUB_PORT_NUM */
@@ -554,7 +554,7 @@ static rt_err_t rt_usbh_hub_enable(void *arg)
* 2 * RT_TICK_PER_SECOND / 1000 );
}
- if(intf->intf_desc->bNumEndpoints != 1)
+ if(intf->intf_desc->bNumEndpoints != 1)
return -RT_ERROR;
/* get endpoint descriptor from interface descriptor */
@@ -565,12 +565,12 @@ static rt_err_t rt_usbh_hub_enable(void *arg)
return -RT_ERROR;
}
- /* the endpoint type of hub class should be interrupt */
+ /* the endpoint type of hub class should be interrupt */
if( USB_EP_ATTR(ep_desc->bmAttributes) == USB_EP_ATTR_INT)
{
/* the endpoint direction of hub class should be in */
if(ep_desc->bEndpointAddress & USB_DIR_IN)
- {
+ {
/* allocate a pipe according to the endpoint type */
pipe_in = rt_usb_instance_find_pipe(device,ep_desc->bEndpointAddress);
if(pipe_in == RT_NULL)
@@ -585,17 +585,17 @@ static rt_err_t rt_usbh_hub_enable(void *arg)
/* parameter check */
RT_ASSERT(device->hcd != RT_NULL);
pipe_in->user_data = hub;
- rt_usb_hcd_pipe_xfer(hub->hcd, pipe_in, hub->buffer,
+ rt_usb_hcd_pipe_xfer(hub->hcd, pipe_in, hub->buffer,
pipe_in->ep.wMaxPacketSize, timeout);
return RT_EOK;
}
/**
- * This function will be invoked when usb hub plug out is detected and it would clean
+ * This function will be invoked when usb hub plug out is detected and it would clean
* and release all hub class related resources.
*
* @param arg the argument.
- *
+ *
* @return the error code, RT_EOK on successfully.
*/
static rt_err_t rt_usbh_hub_disable(void* arg)
@@ -615,7 +615,7 @@ static rt_err_t rt_usbh_hub_disable(void* arg)
if(hub->child[i] != RT_NULL)
rt_usbh_detach_instance(hub->child[i]);
}
-
+
if(hub != RT_NULL) rt_free(hub);
return RT_EOK;
@@ -624,13 +624,13 @@ static rt_err_t rt_usbh_hub_disable(void* arg)
/**
* This function will register hub class driver to the usb class driver manager.
* and it should be invoked in the usb system initialization.
- *
+ *
* @return the error code, RT_EOK on successfully.
*/
ucd_t rt_usbh_class_driver_hub(void)
-{
+{
hub_driver.class_code = USB_CLASS_HUB;
-
+
hub_driver.enable = rt_usbh_hub_enable;
hub_driver.disable = rt_usbh_hub_disable;
@@ -638,27 +638,27 @@ ucd_t rt_usbh_class_driver_hub(void)
}
/**
- * This function is the main entry of usb hub thread, it is in charge of
- * processing all messages received from the usb message buffer.
+ * This function is the main entry of usb hub thread, it is in charge of
+ * processing all messages received from the usb message buffer.
*
* @param parameter the parameter of the usb host thread.
- *
+ *
* @return none.
*/
static void rt_usbh_hub_thread_entry(void* parameter)
-{
+{
while(1)
- {
+ {
struct uhost_msg msg;
-
+
/* receive message */
- if(rt_mq_recv(usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER)
+ if(rt_mq_recv(usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER)
!= RT_EOK ) continue;
//RT_DEBUG_LOG(RT_DEBUG_USB, ("msg type %d\n", msg.type));
-
+
switch (msg.type)
- {
+ {
case USB_MSG_CONNECT_CHANGE:
rt_usbh_hub_port_change(msg.content.hub);
break;
@@ -668,7 +668,7 @@ static void rt_usbh_hub_thread_entry(void* parameter)
break;
default:
break;
- }
+ }
}
}
@@ -676,8 +676,8 @@ static void rt_usbh_hub_thread_entry(void* parameter)
* This function will post an message to the usb message queue,
*
* @param msg the message to be posted
- *
- * @return the error code, RT_EOK on successfully.
+ *
+ * @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_event_signal(struct uhost_msg* msg)
{
@@ -693,7 +693,7 @@ rt_err_t rt_usbh_event_signal(struct uhost_msg* msg)
* This function will initialize usb hub thread.
*
* @return none.
- *
+ *
*/
void rt_usbh_hub_init(uhcd_t hcd)
{
@@ -705,9 +705,9 @@ void rt_usbh_hub_init(uhcd_t hcd)
root_hub.num_ports = hcd->num_ports;
/* create usb message queue */
usb_mq = rt_mq_create("usbh", 32, 16, RT_IPC_FLAG_FIFO);
-
+
/* create usb hub thread */
- thread = rt_thread_create("usbh", rt_usbh_hub_thread_entry, RT_NULL,
+ thread = rt_thread_create("usbh", rt_usbh_hub_thread_entry, RT_NULL,
USB_THREAD_STACK_SIZE, 8, 20);
if(thread != RT_NULL)
{