2013-01-08 22:40:58 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2011-12-12 Yi Qiu first version
|
2021-02-23 17:05:01 +08:00
|
|
|
* 2021-02-23 Leslie Lee provide possibility for multi usb host
|
2013-01-08 22:40:58 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <drivers/usb_host.h>
|
|
|
|
|
2017-12-14 22:45:12 +08:00
|
|
|
#define USB_THREAD_STACK_SIZE 4096
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2021-02-23 17:05:01 +08:00
|
|
|
// static struct rt_messagequeue *usb_mq;
|
2013-01-08 22:40:58 +08:00
|
|
|
static struct uclass_driver hub_driver;
|
2021-02-23 17:05:01 +08:00
|
|
|
// static struct uhub root_hub;
|
2017-12-14 03:14:44 +08:00
|
|
|
|
|
|
|
static rt_err_t root_hub_ctrl(struct uhcd *hcd, rt_uint16_t port, rt_uint8_t cmd, void *args)
|
|
|
|
{
|
|
|
|
switch(cmd)
|
|
|
|
{
|
|
|
|
case RH_GET_PORT_STATUS:
|
|
|
|
(*(rt_uint32_t *)args) = hcd->roothub->port_status[port-1];
|
|
|
|
break;
|
|
|
|
case RH_SET_PORT_STATUS:
|
|
|
|
hcd->roothub->port_status[port-1] = (*(rt_uint32_t *)args);
|
|
|
|
break;
|
|
|
|
case RH_CLEAR_PORT_FEATURE:
|
|
|
|
switch(((rt_uint32_t)args))
|
|
|
|
{
|
|
|
|
case PORT_FEAT_C_CONNECTION:
|
|
|
|
hcd->roothub->port_status[port-1] &= ~PORT_CCSC;
|
|
|
|
break;
|
|
|
|
case PORT_FEAT_C_ENABLE:
|
|
|
|
hcd->roothub->port_status[port-1] &= ~PORT_PESC;
|
|
|
|
break;
|
|
|
|
case PORT_FEAT_C_SUSPEND:
|
|
|
|
hcd->roothub->port_status[port-1] &= ~PORT_PSSC;
|
|
|
|
break;
|
|
|
|
case PORT_FEAT_C_OVER_CURRENT:
|
|
|
|
hcd->roothub->port_status[port-1] &= ~PORT_POCIC;
|
|
|
|
break;
|
|
|
|
case PORT_FEAT_C_RESET:
|
|
|
|
hcd->roothub->port_status[port-1] &= ~PORT_PRSC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RH_SET_PORT_FEATURE:
|
|
|
|
switch((rt_uint32_t)args)
|
|
|
|
{
|
|
|
|
case PORT_FEAT_CONNECTION:
|
|
|
|
hcd->roothub->port_status[port-1] |= PORT_CCSC;
|
|
|
|
break;
|
|
|
|
case PORT_FEAT_ENABLE:
|
|
|
|
hcd->roothub->port_status[port-1] |= PORT_PESC;
|
|
|
|
break;
|
|
|
|
case PORT_FEAT_SUSPEND:
|
|
|
|
hcd->roothub->port_status[port-1] |= PORT_PSSC;
|
|
|
|
break;
|
|
|
|
case PORT_FEAT_OVER_CURRENT:
|
|
|
|
hcd->roothub->port_status[port-1] |= PORT_POCIC;
|
|
|
|
break;
|
|
|
|
case PORT_FEAT_RESET:
|
|
|
|
hcd->ops->reset_port(port);
|
|
|
|
break;
|
|
|
|
case PORT_FEAT_POWER:
|
|
|
|
break;
|
|
|
|
case PORT_FEAT_LOWSPEED:
|
|
|
|
break;
|
|
|
|
case PORT_FEAT_HIGHSPEED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return RT_ERROR;
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
2021-03-08 18:19:04 +08:00
|
|
|
}
|
2017-12-14 03:14:44 +08:00
|
|
|
void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS)
|
|
|
|
{
|
|
|
|
struct uhost_msg msg;
|
|
|
|
msg.type = USB_MSG_CONNECT_CHANGE;
|
|
|
|
msg.content.hub = hcd->roothub;
|
|
|
|
hcd->roothub->port_status[port - 1] |= PORT_CCS | PORT_CCSC;
|
|
|
|
if(isHS)
|
|
|
|
{
|
|
|
|
hcd->roothub->port_status[port - 1] &= ~PORT_LSDA;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hcd->roothub->port_status[port - 1] |= PORT_LSDA;
|
|
|
|
}
|
2021-02-23 17:05:01 +08:00
|
|
|
rt_usbh_event_signal(hcd, &msg);
|
2017-12-14 03:14:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port)
|
|
|
|
{
|
|
|
|
struct uhost_msg msg;
|
|
|
|
msg.type = USB_MSG_CONNECT_CHANGE;
|
|
|
|
msg.content.hub = hcd->roothub;
|
|
|
|
hcd->roothub->port_status[port - 1] |= PORT_CCSC;
|
|
|
|
hcd->roothub->port_status[port - 1] &= ~PORT_CCS;
|
2021-02-23 17:05:01 +08:00
|
|
|
rt_usbh_event_signal(hcd, &msg);
|
2017-12-14 03:14:44 +08:00
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/**
|
2021-03-08 18:19:04 +08:00
|
|
|
* This function will do USB_REQ_GET_DESCRIPTOR bRequest for the device instance
|
2013-01-08 22:40:58 +08:00
|
|
|
* to get usb hub descriptor.
|
|
|
|
*
|
2014-03-04 08:59:53 +08:00
|
|
|
* @param intf the interface instance.
|
2013-01-08 22:40:58 +08:00
|
|
|
* @buffer the data buffer to save usb hub descriptor.
|
|
|
|
* @param nbytes the size of buffer
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2017-12-13 15:58:11 +08:00
|
|
|
rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer, rt_size_t nbytes)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2015-03-19 16:52:28 +08:00
|
|
|
struct urequest setup;
|
2018-10-23 20:03:18 +08:00
|
|
|
int timeout = USB_TIMEOUT_BASIC;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2017-12-13 15:58:11 +08:00
|
|
|
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE;
|
2017-11-11 10:51:47 +08:00
|
|
|
setup.bRequest = USB_REQ_GET_DESCRIPTOR;
|
|
|
|
setup.wIndex = 0;
|
|
|
|
setup.wLength = nbytes;
|
|
|
|
setup.wValue = USB_DESC_TYPE_HUB << 8;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2017-12-13 15:58:11 +08:00
|
|
|
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
|
|
|
{
|
|
|
|
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, nbytes, timeout) == nbytes)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
}
|
|
|
|
return -RT_FALSE;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-08 18:19:04 +08:00
|
|
|
* This function will do USB_REQ_GET_STATUS bRequest for the device instance
|
2013-01-08 22:40:58 +08:00
|
|
|
* to get usb hub status.
|
|
|
|
*
|
2014-03-04 08:59:53 +08:00
|
|
|
* @param intf the interface instance.
|
2013-01-08 22:40:58 +08:00
|
|
|
* @buffer the data buffer to save usb hub status.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2017-12-13 15:58:11 +08:00
|
|
|
rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint32_t* buffer)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2015-03-19 16:52:28 +08:00
|
|
|
struct urequest setup;
|
2018-10-23 20:03:18 +08:00
|
|
|
int timeout = USB_TIMEOUT_BASIC;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2017-12-13 15:58:11 +08:00
|
|
|
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE;
|
2017-11-11 10:51:47 +08:00
|
|
|
setup.bRequest = USB_REQ_GET_STATUS;
|
|
|
|
setup.wIndex = 0;
|
2017-12-13 15:58:11 +08:00
|
|
|
setup.wLength = 4;
|
2017-11-11 10:51:47 +08:00
|
|
|
setup.wValue = 0;
|
2017-12-13 15:58:11 +08:00
|
|
|
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
|
|
|
{
|
|
|
|
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, 4, timeout) == 4)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
return -RT_FALSE;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-08 18:19:04 +08:00
|
|
|
* This function will do USB_REQ_GET_STATUS bRequest for the device instance
|
2013-01-08 22:40:58 +08:00
|
|
|
* to get hub port status.
|
|
|
|
*
|
2014-03-04 08:59:53 +08:00
|
|
|
* @param intf the interface instance.
|
2013-01-08 22:40:58 +08:00
|
|
|
* @port the hub port to get status.
|
|
|
|
* @buffer the data buffer to save usb hub status.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2017-12-13 15:58:11 +08:00
|
|
|
rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port, rt_uint32_t* buffer)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2015-03-19 16:52:28 +08:00
|
|
|
struct urequest setup;
|
2018-10-23 20:03:18 +08:00
|
|
|
int timeout = USB_TIMEOUT_BASIC;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(hub != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* get roothub port status */
|
2014-03-04 08:59:53 +08:00
|
|
|
if(hub->is_roothub)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2021-03-08 18:19:04 +08:00
|
|
|
root_hub_ctrl(hub->hcd, port, RH_GET_PORT_STATUS,
|
2013-01-08 22:40:58 +08:00
|
|
|
(void*)buffer);
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2017-12-13 15:58:11 +08:00
|
|
|
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_OTHER;
|
2017-11-11 10:51:47 +08:00
|
|
|
setup.bRequest = USB_REQ_GET_STATUS;
|
|
|
|
setup.wIndex = port;
|
|
|
|
setup.wLength = 4;
|
|
|
|
setup.wValue = 0;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2017-12-13 15:58:11 +08:00
|
|
|
if(rt_usb_hcd_setup_xfer(hub->hcd, hub->self->pipe_ep0_out, &setup, timeout) == 8)
|
|
|
|
{
|
|
|
|
if(rt_usb_hcd_pipe_xfer(hub->hcd, hub->self->pipe_ep0_in, buffer, 4, timeout) == 4)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
return -RT_FALSE;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-08 18:19:04 +08:00
|
|
|
* This function will do USB_REQ_CLEAR_FEATURE bRequest for the device instance
|
2013-01-08 22:40:58 +08:00
|
|
|
* to clear feature of the hub port.
|
|
|
|
*
|
2014-03-04 08:59:53 +08:00
|
|
|
* @param intf the interface instance.
|
2013-01-08 22:40:58 +08:00
|
|
|
* @port the hub port.
|
|
|
|
* @feature feature to be cleared.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2017-12-13 15:58:11 +08:00
|
|
|
rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port, rt_uint16_t feature)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2015-03-19 16:52:28 +08:00
|
|
|
struct urequest setup;
|
2018-10-23 20:03:18 +08:00
|
|
|
int timeout = USB_TIMEOUT_BASIC;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(hub != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* clear roothub feature */
|
2014-03-04 08:59:53 +08:00
|
|
|
if(hub->is_roothub)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2021-03-08 18:19:04 +08:00
|
|
|
root_hub_ctrl(hub->hcd, port, RH_CLEAR_PORT_FEATURE,
|
2018-08-28 03:18:39 +08:00
|
|
|
(void*)(rt_uint32_t)feature);
|
2013-01-08 22:40:58 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
|
2013-01-08 22:40:58 +08:00
|
|
|
USB_REQ_TYPE_OTHER;
|
2017-11-11 10:51:47 +08:00
|
|
|
setup.bRequest = USB_REQ_CLEAR_FEATURE;
|
|
|
|
setup.wIndex = port;
|
|
|
|
setup.wLength = 0;
|
|
|
|
setup.wValue = feature;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2017-12-13 15:58:11 +08:00
|
|
|
if(rt_usb_hcd_setup_xfer(hub->hcd, hub->self->pipe_ep0_out, &setup, timeout) == 8)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
return -RT_FALSE;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-08 18:19:04 +08:00
|
|
|
* This function will do USB_REQ_SET_FEATURE bRequest for the device instance
|
2013-01-08 22:40:58 +08:00
|
|
|
* to set feature of the hub port.
|
|
|
|
*
|
2014-03-04 08:59:53 +08:00
|
|
|
* @param intf the interface instance.
|
2013-01-08 22:40:58 +08:00
|
|
|
* @port the hub port.
|
|
|
|
* @feature feature to be set.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2021-03-08 18:19:04 +08:00
|
|
|
rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port,
|
2013-01-08 22:40:58 +08:00
|
|
|
rt_uint16_t feature)
|
|
|
|
{
|
2015-03-19 16:52:28 +08:00
|
|
|
struct urequest setup;
|
2018-10-23 20:03:18 +08:00
|
|
|
int timeout = USB_TIMEOUT_BASIC;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(hub != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* clear roothub feature */
|
2014-03-04 08:59:53 +08:00
|
|
|
if(hub->is_roothub)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2021-03-08 18:19:04 +08:00
|
|
|
root_hub_ctrl(hub->hcd, port, RH_SET_PORT_FEATURE,
|
2018-08-28 03:18:39 +08:00
|
|
|
(void*)(rt_uint32_t)feature);
|
2013-01-08 22:40:58 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
|
2013-01-08 22:40:58 +08:00
|
|
|
USB_REQ_TYPE_OTHER;
|
2017-11-11 10:51:47 +08:00
|
|
|
setup.bRequest = USB_REQ_SET_FEATURE;
|
|
|
|
setup.wIndex = port;
|
|
|
|
setup.wLength = 0;
|
|
|
|
setup.wValue = feature;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2017-12-13 15:58:11 +08:00
|
|
|
if(rt_usb_hcd_setup_xfer(hub->hcd, hub->self->pipe_ep0_out, &setup, timeout) == 8)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
else return -RT_FALSE;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will rest hub port, it is invoked when sub device attached to the hub port.
|
|
|
|
*
|
2014-03-04 08:59:53 +08:00
|
|
|
* @param intf the interface instance.
|
2013-01-08 22:40:58 +08:00
|
|
|
* @param port the hub port.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_err_t rt_usbh_hub_reset_port(uhub_t hub, rt_uint16_t port)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
rt_err_t ret;
|
|
|
|
rt_uint32_t pstatus;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(hub != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
rt_thread_delay(50);
|
|
|
|
|
|
|
|
/* reset hub port */
|
2014-03-04 08:59:53 +08:00
|
|
|
ret = rt_usbh_hub_set_port_feature(hub, port, PORT_FEAT_RESET);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(ret != RT_EOK) return ret;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
2017-12-14 03:14:44 +08:00
|
|
|
ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(!(pstatus & PORT_PRS)) break;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* clear port reset feature */
|
2021-03-08 18:19:04 +08:00
|
|
|
ret = rt_usbh_hub_clear_port_feature(hub, port, PORT_FEAT_C_RESET);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(ret != RT_EOK) return ret;
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
rt_thread_delay(50);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will do debouce, it is invoked when sub device attached to the hub port.
|
|
|
|
*
|
2014-03-04 08:59:53 +08:00
|
|
|
* @param device the usb instance.
|
2013-01-08 22:40:58 +08:00
|
|
|
* @param port the hub port.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
rt_err_t ret;
|
|
|
|
int i = 0, times = 20;
|
|
|
|
rt_uint32_t pstatus;
|
|
|
|
rt_bool_t connect = RT_TRUE;
|
2018-10-23 20:03:18 +08:00
|
|
|
int delayticks = USB_DEBOUNCE_TIME / times;
|
|
|
|
if (delayticks < 1)
|
|
|
|
delayticks = 1;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(hub != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
for(i=0; i<times; i++)
|
|
|
|
{
|
2017-12-14 03:14:44 +08:00
|
|
|
ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(ret != RT_EOK) return ret;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
|
|
|
if(!(pstatus & PORT_CCS))
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
connect = RT_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2018-10-23 20:03:18 +08:00
|
|
|
rt_thread_delay(delayticks);
|
2021-03-08 18:19:04 +08:00
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
if(connect) return RT_EOK;
|
|
|
|
else return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will poll all the hub ports to detect port status, especially connect and
|
|
|
|
* disconnect events.
|
|
|
|
*
|
2014-03-04 08:59:53 +08:00
|
|
|
* @param intf the interface instance.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
rt_bool_t reconnect;
|
|
|
|
|
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(hub != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
/* get usb device instance */
|
2014-03-04 08:59:53 +08:00
|
|
|
for (i = 0; i < hub->num_ports; i++)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
rt_err_t ret;
|
2014-03-04 08:59:53 +08:00
|
|
|
struct uinstance* device;
|
2013-01-08 22:40:58 +08:00
|
|
|
rt_uint32_t pstatus = 0;
|
|
|
|
|
|
|
|
reconnect = RT_FALSE;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* get hub port status */
|
2017-12-14 03:14:44 +08:00
|
|
|
ret = rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(ret != RT_EOK) continue;
|
|
|
|
|
2017-12-14 03:14:44 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("port %d status 0x%x\n", i + 1, pstatus));
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* check port status change */
|
2021-03-08 18:19:04 +08:00
|
|
|
if (pstatus & PORT_CCSC)
|
|
|
|
{
|
2013-01-08 22:40:58 +08:00
|
|
|
/* clear port status change feature */
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_CONNECTION);
|
2013-01-08 22:40:58 +08:00
|
|
|
reconnect = RT_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pstatus & PORT_PESC)
|
|
|
|
{
|
2021-03-08 18:19:04 +08:00
|
|
|
rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_ENABLE);
|
2013-01-08 22:40:58 +08:00
|
|
|
reconnect = RT_TRUE;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
if(reconnect)
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2020-09-02 17:49:17 +08:00
|
|
|
if(hub->child[i] != RT_NULL && hub->child[i]->status != DEV_STATUS_IDLE)
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usbh_detach_instance(hub->child[i]);
|
2020-09-02 17:49:17 +08:00
|
|
|
|
|
|
|
/* Child device have been detach. Set hub->child[i] to NULL. */
|
|
|
|
hub->child[i] = RT_NULL;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
ret = rt_usbh_hub_port_debounce(hub, i + 1);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(ret != RT_EOK) continue;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* allocate an usb instance for new connected device */
|
2017-12-14 03:14:44 +08:00
|
|
|
device = rt_usbh_alloc_instance(hub->hcd);
|
2014-03-04 08:59:53 +08:00
|
|
|
if(device == RT_NULL) break;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* set usb device speed */
|
2014-03-04 08:59:53 +08:00
|
|
|
device->speed = (pstatus & PORT_LSDA) ? 1 : 0;
|
2021-03-08 18:19:04 +08:00
|
|
|
device->parent_hub = hub;
|
2014-03-04 08:59:53 +08:00
|
|
|
device->hcd = hub->hcd;
|
2018-10-23 20:03:18 +08:00
|
|
|
device->port = i + 1;
|
2014-03-04 08:59:53 +08:00
|
|
|
hub->child[i] = device;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* reset usb roothub port */
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usbh_hub_reset_port(hub, i + 1);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* attatch the usb instance to the hcd */
|
2021-03-08 18:19:04 +08:00
|
|
|
rt_usbh_attatch_instance(device);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return none.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
static void rt_usbh_hub_irq(void* context)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2017-12-14 03:14:44 +08:00
|
|
|
upipe_t pipe;
|
2014-03-04 08:59:53 +08:00
|
|
|
uhub_t hub;
|
2018-10-23 20:03:18 +08:00
|
|
|
int timeout = USB_TIMEOUT_BASIC;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
RT_ASSERT(context != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
pipe = (upipe_t)context;
|
2017-12-14 03:14:44 +08:00
|
|
|
hub = (uhub_t)pipe->user_data;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
if(pipe->status != UPIPE_STATUS_OK)
|
|
|
|
{
|
2017-12-14 03:14:44 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB,("hub irq error\n"));
|
2013-01-08 22:40:58 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usbh_hub_port_change(hub);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2017-12-14 22:45:12 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB,("hub int xfer...\n"));
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* parameter check */
|
2017-12-14 03:14:44 +08:00
|
|
|
RT_ASSERT(pipe->inst->hcd != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2017-12-14 03:14:44 +08:00
|
|
|
rt_usb_hcd_pipe_xfer(hub->self->hcd, pipe, hub->buffer, pipe->ep.wMaxPacketSize, timeout);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will run usb hub class driver when usb hub is detected and identified
|
|
|
|
* as a hub class device, it will continue to do the enumulate process.
|
|
|
|
*
|
|
|
|
* @param arg the argument.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2017-12-14 03:14:44 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
static rt_err_t rt_usbh_hub_enable(void *arg)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
rt_err_t ret = RT_EOK;
|
2018-09-10 19:40:08 +08:00
|
|
|
uep_desc_t ep_desc = RT_NULL;
|
2014-03-04 08:59:53 +08:00
|
|
|
uhub_t hub;
|
|
|
|
struct uinstance* device;
|
2017-12-13 15:58:11 +08:00
|
|
|
struct uhintf* intf = (struct uhintf*)arg;
|
2018-09-10 19:40:08 +08:00
|
|
|
upipe_t pipe_in = RT_NULL;
|
2018-10-23 20:03:18 +08:00
|
|
|
int timeout = USB_TIMEOUT_LONG;
|
2013-01-08 22:40:58 +08:00
|
|
|
/* paremeter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(intf != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_run\n"));
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* get usb device instance */
|
2014-03-04 08:59:53 +08:00
|
|
|
device = intf->device;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* create a hub instance */
|
2014-03-04 08:59:53 +08:00
|
|
|
hub = rt_malloc(sizeof(struct uhub));
|
2020-03-21 20:56:31 +08:00
|
|
|
RT_ASSERT(hub != RT_NULL);
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_memset(hub, 0, sizeof(struct uhub));
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* make interface instance's user data point to hub instance */
|
2014-03-04 08:59:53 +08:00
|
|
|
intf->user_data = (void*)hub;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* get hub descriptor head */
|
2014-03-04 08:59:53 +08:00
|
|
|
ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc, 8);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(ret != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_kprintf("get hub descriptor failed\n");
|
2021-03-08 18:19:04 +08:00
|
|
|
return -RT_ERROR;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get full hub descriptor */
|
2021-03-08 18:19:04 +08:00
|
|
|
ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc,
|
2014-03-04 08:59:53 +08:00
|
|
|
hub->hub_desc.length);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(ret != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_kprintf("get hub descriptor again failed\n");
|
2021-03-08 18:19:04 +08:00
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* get hub ports number */
|
2020-09-02 17:49:17 +08:00
|
|
|
/* If hub device supported ports over USB_HUB_PORT_NUM(Ex: 8 port hub). Set hub->num_ports to USB_HUB_PORT_NUM */
|
|
|
|
if(hub->hub_desc.num_ports > USB_HUB_PORT_NUM)
|
|
|
|
hub->num_ports = USB_HUB_PORT_NUM;
|
|
|
|
else
|
|
|
|
hub->num_ports = hub->hub_desc.num_ports;
|
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
hub->hcd = device->hcd;
|
|
|
|
hub->self = device;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* reset all hub ports */
|
2014-03-04 08:59:53 +08:00
|
|
|
for (i = 0; i < hub->num_ports; i++)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usbh_hub_set_port_feature(hub, i + 1, PORT_FEAT_POWER);
|
|
|
|
rt_thread_delay(hub->hub_desc.pwron_to_good
|
2013-01-08 22:40:58 +08:00
|
|
|
* 2 * RT_TICK_PER_SECOND / 1000 );
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
if(intf->intf_desc->bNumEndpoints != 1)
|
2013-01-08 22:40:58 +08:00
|
|
|
return -RT_ERROR;
|
|
|
|
|
|
|
|
/* get endpoint descriptor from interface descriptor */
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usbh_get_endpoint_descriptor(intf->intf_desc, 0, &ep_desc);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(ep_desc == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
/* the endpoint type of hub class should be interrupt */
|
2013-01-08 22:40:58 +08:00
|
|
|
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)
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2013-01-08 22:40:58 +08:00
|
|
|
/* allocate a pipe according to the endpoint type */
|
2017-12-14 03:14:44 +08:00
|
|
|
pipe_in = rt_usb_instance_find_pipe(device,ep_desc->bEndpointAddress);
|
|
|
|
if(pipe_in == RT_NULL)
|
|
|
|
{
|
|
|
|
return RT_ERROR;
|
|
|
|
}
|
|
|
|
rt_usb_pipe_add_callback(pipe_in,rt_usbh_hub_irq);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
else return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parameter check */
|
2017-12-14 03:14:44 +08:00
|
|
|
RT_ASSERT(device->hcd != RT_NULL);
|
|
|
|
pipe_in->user_data = hub;
|
2021-03-08 18:19:04 +08:00
|
|
|
rt_usb_hcd_pipe_xfer(hub->hcd, pipe_in, hub->buffer,
|
2017-12-14 03:14:44 +08:00
|
|
|
pipe_in->ep.wMaxPacketSize, timeout);
|
2013-01-08 22:40:58 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-08 18:19:04 +08:00
|
|
|
* This function will be invoked when usb hub plug out is detected and it would clean
|
2013-01-08 22:40:58 +08:00
|
|
|
* and release all hub class related resources.
|
|
|
|
*
|
|
|
|
* @param arg the argument.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
static rt_err_t rt_usbh_hub_disable(void* arg)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
int i;
|
2014-03-04 08:59:53 +08:00
|
|
|
uhub_t hub;
|
2017-12-14 03:14:44 +08:00
|
|
|
struct uhintf* intf = (struct uhintf*)arg;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* paremeter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(intf != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_stop\n"));
|
|
|
|
hub = (uhub_t)intf->user_data;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
for(i=0; i<hub->num_ports; i++)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-03-04 08:59:53 +08:00
|
|
|
if(hub->child[i] != RT_NULL)
|
|
|
|
rt_usbh_detach_instance(hub->child[i]);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
if(hub != RT_NULL) rt_free(hub);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will register hub class driver to the usb class driver manager.
|
|
|
|
* and it should be invoked in the usb system initialization.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
ucd_t rt_usbh_class_driver_hub(void)
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2013-01-08 22:40:58 +08:00
|
|
|
hub_driver.class_code = USB_CLASS_HUB;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
hub_driver.enable = rt_usbh_hub_enable;
|
|
|
|
hub_driver.disable = rt_usbh_hub_disable;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
return &hub_driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-08 18:19:04 +08:00
|
|
|
* This function is the main entry of usb hub thread, it is in charge of
|
|
|
|
* processing all messages received from the usb message buffer.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* @param parameter the parameter of the usb host thread.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
* @return none.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
static void rt_usbh_hub_thread_entry(void* parameter)
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2021-02-23 17:05:01 +08:00
|
|
|
uhcd_t hcd = (uhcd_t)parameter;
|
2013-01-08 22:40:58 +08:00
|
|
|
while(1)
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2013-01-08 22:40:58 +08:00
|
|
|
struct uhost_msg msg;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* receive message */
|
2021-09-28 10:36:54 +08:00
|
|
|
if(rt_mq_recv(hcd->usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER)
|
2013-01-08 22:40:58 +08:00
|
|
|
!= RT_EOK ) continue;
|
|
|
|
|
2017-11-11 10:51:47 +08:00
|
|
|
//RT_DEBUG_LOG(RT_DEBUG_USB, ("msg type %d\n", msg.type));
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
switch (msg.type)
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2013-01-08 22:40:58 +08:00
|
|
|
case USB_MSG_CONNECT_CHANGE:
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usbh_hub_port_change(msg.content.hub);
|
2013-01-08 22:40:58 +08:00
|
|
|
break;
|
|
|
|
case USB_MSG_CALLBACK:
|
|
|
|
/* invoke callback */
|
|
|
|
msg.content.cb.function(msg.content.cb.context);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2021-03-08 18:19:04 +08:00
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will post an message to the usb message queue,
|
|
|
|
*
|
|
|
|
* @param msg the message to be posted
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
|
|
|
* @return the error code, RT_EOK on successfully.
|
2013-01-08 22:40:58 +08:00
|
|
|
*/
|
2021-02-23 17:05:01 +08:00
|
|
|
rt_err_t rt_usbh_event_signal(uhcd_t hcd, struct uhost_msg* msg)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(msg != RT_NULL);
|
|
|
|
|
|
|
|
/* send message to usb message queue */
|
2021-02-23 17:05:01 +08:00
|
|
|
rt_mq_send(hcd->usb_mq, (void*)msg, sizeof(struct uhost_msg));
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will initialize usb hub thread.
|
|
|
|
*
|
|
|
|
* @return none.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2013-01-08 22:40:58 +08:00
|
|
|
*/
|
2017-12-14 03:14:44 +08:00
|
|
|
void rt_usbh_hub_init(uhcd_t hcd)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
rt_thread_t thread;
|
2021-02-23 17:05:01 +08:00
|
|
|
/* create root hub for hcd */
|
|
|
|
hcd->roothub = rt_malloc(sizeof(struct uhub));
|
|
|
|
rt_memset(hcd->roothub, 0, sizeof(struct uhub));
|
|
|
|
hcd->roothub->is_roothub = RT_TRUE;
|
|
|
|
hcd->roothub->hcd = hcd;
|
|
|
|
hcd->roothub->num_ports = hcd->num_ports;
|
2013-01-08 22:40:58 +08:00
|
|
|
/* create usb message queue */
|
2021-09-28 10:36:54 +08:00
|
|
|
|
2021-02-23 17:05:01 +08:00
|
|
|
hcd->usb_mq = rt_mq_create(hcd->parent.parent.name, 32, 16, RT_IPC_FLAG_FIFO);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* create usb hub thread */
|
2021-09-28 10:36:54 +08:00
|
|
|
thread = rt_thread_create(hcd->parent.parent.name, rt_usbh_hub_thread_entry, hcd,
|
2013-01-08 22:40:58 +08:00
|
|
|
USB_THREAD_STACK_SIZE, 8, 20);
|
|
|
|
if(thread != RT_NULL)
|
|
|
|
{
|
|
|
|
/* startup usb host thread */
|
|
|
|
rt_thread_startup(thread);
|
|
|
|
}
|
2022-01-26 11:00:16 +08:00
|
|
|
}
|