2013-01-08 22:40:58 +08:00
|
|
|
/*
|
|
|
|
* File : hub.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2011, RT-Thread Development Team
|
|
|
|
*
|
2013-06-28 00:36:54 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2011-12-12 Yi Qiu first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <drivers/usb_host.h>
|
|
|
|
|
|
|
|
#define USB_THREAD_STACK_SIZE 2048
|
|
|
|
|
|
|
|
static struct rt_messagequeue *usb_mq;
|
|
|
|
static struct uclass_driver hub_driver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will do USB_REQ_GET_DESCRIPTOR request for the device instance
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer,
|
2013-01-08 22:40:58 +08:00
|
|
|
rt_size_t nbytes)
|
|
|
|
{
|
2015-03-19 16:52:28 +08:00
|
|
|
struct urequest setup;
|
2013-01-08 22:40:58 +08:00
|
|
|
int timeout = 100;
|
|
|
|
|
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
|
|
|
|
USB_REQ_TYPE_DEVICE;
|
|
|
|
setup.request = USB_REQ_GET_DESCRIPTOR;
|
|
|
|
setup.index = 0;
|
|
|
|
setup.length = nbytes;
|
|
|
|
setup.value = USB_DESC_TYPE_HUB << 8;
|
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, nbytes,
|
2013-01-08 22:40:58 +08:00
|
|
|
timeout) == nbytes) return RT_EOK;
|
|
|
|
else return -RT_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will do USB_REQ_GET_STATUS request for the device instance
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint8_t* buffer)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2015-03-19 16:52:28 +08:00
|
|
|
struct urequest setup;
|
2013-01-08 22:40:58 +08:00
|
|
|
int timeout = 100;
|
|
|
|
int length = 4;
|
|
|
|
|
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
|
|
|
|
USB_REQ_TYPE_DEVICE;
|
|
|
|
setup.request = USB_REQ_GET_STATUS;
|
|
|
|
setup.index = 0;
|
|
|
|
setup.length = length;
|
|
|
|
setup.value = 0;
|
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, length,
|
2013-01-08 22:40:58 +08:00
|
|
|
timeout) == length) return RT_EOK;
|
|
|
|
else return -RT_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will do USB_REQ_GET_STATUS request for the device instance
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port,
|
2013-01-08 22:40:58 +08:00
|
|
|
rt_uint8_t* buffer)
|
|
|
|
{
|
2015-03-19 16:52:28 +08:00
|
|
|
struct urequest setup;
|
2013-01-08 22:40:58 +08:00
|
|
|
int timeout = 100;
|
|
|
|
int length = 4;
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usb_hcd_hub_control(hub->hcd, port, RH_GET_PORT_STATUS,
|
2013-01-08 22:40:58 +08:00
|
|
|
(void*)buffer);
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
|
|
|
|
USB_REQ_TYPE_OTHER;
|
|
|
|
setup.request = USB_REQ_GET_STATUS;
|
|
|
|
setup.index = port;
|
|
|
|
setup.length = 4;
|
|
|
|
setup.value = 0;
|
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
if(rt_usb_hcd_control_xfer(hub->hcd, hub->self, &setup, buffer,
|
2013-01-08 22:40:58 +08:00
|
|
|
length, timeout) == timeout) return RT_EOK;
|
|
|
|
else return -RT_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will do USB_REQ_CLEAR_FEATURE request for the device instance
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_err_t rt_usbh_hub_clear_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;
|
2013-01-08 22:40:58 +08:00
|
|
|
int timeout = 100;
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usb_hcd_hub_control(hub->hcd, port, RH_CLEAR_PORT_FEATURE,
|
2013-01-08 22:40:58 +08:00
|
|
|
(void*)feature);
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
|
|
|
|
USB_REQ_TYPE_OTHER;
|
|
|
|
setup.request = USB_REQ_CLEAR_FEATURE;
|
|
|
|
setup.index = port;
|
|
|
|
setup.length = 0;
|
|
|
|
setup.value = feature;
|
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
if(rt_usb_hcd_control_xfer(hub->hcd, hub->self, &setup, RT_NULL, 0,
|
2013-01-08 22:40:58 +08:00
|
|
|
timeout) == 0) return RT_EOK;
|
|
|
|
else return -RT_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will do USB_REQ_SET_FEATURE request for the device instance
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2014-03-04 08:59:53 +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;
|
2013-01-08 22:40:58 +08:00
|
|
|
int timeout = 100;
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usb_hcd_hub_control(hub->hcd, port, RH_SET_PORT_FEATURE,
|
2013-01-08 22:40:58 +08:00
|
|
|
(void*)feature);
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
|
|
|
|
USB_REQ_TYPE_OTHER;
|
|
|
|
setup.request = USB_REQ_SET_FEATURE;
|
|
|
|
setup.index = port;
|
|
|
|
setup.length = 0;
|
|
|
|
setup.value = feature;
|
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
if(rt_usb_hcd_control_xfer(hub->hcd, hub->self, &setup, RT_NULL, 0,
|
2013-01-08 22:40:58 +08:00
|
|
|
timeout) == 0) return RT_EOK;
|
|
|
|
else return -RT_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(hub != RT_NULL);
|
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)
|
|
|
|
{
|
2014-03-04 08:59:53 +08:00
|
|
|
ret = rt_usbh_hub_get_port_status(hub, port, (rt_uint8_t*)&pstatus);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(!(pstatus & PORT_PRS)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear port reset feature */
|
2014-03-04 08:59:53 +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;
|
|
|
|
|
|
|
|
rt_thread_delay(50);
|
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
/* 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++)
|
|
|
|
{
|
2014-03-04 08:59:53 +08:00
|
|
|
ret = rt_usbh_hub_get_port_status(hub, port, (rt_uint8_t*)&pstatus);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(ret != RT_EOK) return ret;
|
|
|
|
|
|
|
|
if(!(pstatus & PORT_CCS))
|
|
|
|
{
|
|
|
|
connect = RT_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_thread_delay(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
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
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* get hub port status */
|
2014-03-04 08:59:53 +08:00
|
|
|
ret = rt_usbh_hub_get_port_status(hub, i + 1, (rt_uint8_t*)&pstatus);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(ret != RT_EOK) continue;
|
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("port %d status 0x%x\n", i, pstatus));
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* check port status change */
|
|
|
|
if ((pstatus & PORT_CCSC))
|
|
|
|
{
|
|
|
|
/* 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)
|
|
|
|
{
|
2014-03-04 08:59:53 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(reconnect)
|
|
|
|
{
|
2014-03-04 08:59:53 +08:00
|
|
|
if(hub->child[i]->status != DEV_STATUS_IDLE)
|
|
|
|
rt_usbh_detach_instance(hub->child[i]);
|
2013-01-08 22:40:58 +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;
|
|
|
|
|
|
|
|
/* allocate an usb instance for new connected device */
|
2014-03-04 08:59:53 +08:00
|
|
|
device = rt_usbh_alloc_instance();
|
|
|
|
if(device == RT_NULL) break;
|
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;
|
|
|
|
device->parent = hub;
|
|
|
|
device->hcd = hub->hcd;
|
|
|
|
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);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* attatch the usb instance to the hcd */
|
2014-03-04 08:59:53 +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.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
{
|
|
|
|
upipe_t pipe;
|
2014-03-04 08:59:53 +08:00
|
|
|
struct uintf* intf;
|
|
|
|
uhub_t hub;
|
2013-01-08 22:40:58 +08:00
|
|
|
int timeout = 100;
|
|
|
|
|
|
|
|
RT_ASSERT(context != RT_NULL);
|
|
|
|
|
|
|
|
pipe = (upipe_t)context;
|
2014-03-04 08:59:53 +08:00
|
|
|
intf = pipe->intf;
|
|
|
|
hub = (uhub_t)intf->user_data;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
if(pipe->status != UPIPE_STATUS_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("hub irq error\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usbh_hub_port_change(hub);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
rt_kprintf("hub int xfer...\n");
|
|
|
|
|
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(pipe->intf->device->hcd != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usb_hcd_int_xfer(intf->device->hcd, pipe, hub->buffer,
|
2013-01-08 22:40:58 +08:00
|
|
|
pipe->ep.wMaxPacketSize, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
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;
|
|
|
|
uep_desc_t ep_desc;
|
2014-03-04 08:59:53 +08:00
|
|
|
uhub_t hub;
|
|
|
|
struct uinstance* device;
|
|
|
|
struct uintf* intf = (struct uintf*)arg;
|
2013-01-08 22:40:58 +08:00
|
|
|
int timeout = 300;
|
|
|
|
|
|
|
|
/* 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_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));
|
|
|
|
rt_memset(hub, 0, sizeof(struct uhub));
|
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");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get full hub descriptor */
|
2014-03-04 08:59:53 +08:00
|
|
|
ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc,
|
|
|
|
hub->hub_desc.length);
|
2013-01-08 22:40:58 +08:00
|
|
|
if(ret != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_kprintf("get hub descriptor again failed\n");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get hub ports number */
|
2014-03-04 08:59:53 +08:00
|
|
|
hub->num_ports = hub->hub_desc.num_ports;
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2014-03-04 08:59:53 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usb_hcd_alloc_pipe(device->hcd, &hub->pipe_in, intf,
|
|
|
|
ep_desc, rt_usbh_hub_irq);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
else return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parameter check */
|
2014-03-04 08:59:53 +08:00
|
|
|
RT_ASSERT(device->hcd != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_usb_hcd_int_xfer(device->hcd, hub->pipe_in, hub->buffer,
|
|
|
|
hub->pipe_in->ep.wMaxPacketSize, timeout);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
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;
|
|
|
|
struct uinstance* device;
|
|
|
|
struct uintf* intf = (struct uintf*)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"));
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
device = intf->device;
|
|
|
|
hub = (uhub_t)intf->user_data;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
if(hub->pipe_in != RT_NULL)
|
|
|
|
rt_usb_hcd_free_pipe(device->hcd, hub->pipe_in);
|
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
|
|
|
}
|
|
|
|
|
2014-03-04 08:59:53 +08:00
|
|
|
if(hub != RT_NULL) rt_free(hub);
|
|
|
|
if(intf != RT_NULL) rt_free(intf);
|
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.
|
|
|
|
*
|
|
|
|
* @return the error code, RT_EOK on successfully.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
ucd_t rt_usbh_class_driver_hub(void)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
hub_driver.class_code = USB_CLASS_HUB;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
static void rt_usbh_hub_thread_entry(void* parameter)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
struct uhost_msg msg;
|
|
|
|
|
|
|
|
/* receive message */
|
|
|
|
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:
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_err_t rt_usbh_event_signal(struct uhost_msg* msg)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(msg != RT_NULL);
|
|
|
|
|
|
|
|
/* send message to usb message queue */
|
2014-03-04 08:59:53 +08:00
|
|
|
rt_mq_send(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.
|
|
|
|
*
|
|
|
|
*/
|
2014-03-04 08:59:53 +08:00
|
|
|
void rt_usbh_hub_init(void)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
rt_thread_t thread;
|
|
|
|
|
|
|
|
/* create usb message queue */
|
|
|
|
usb_mq = rt_mq_create("usbh", 32, 16, RT_IPC_FLAG_FIFO);
|
|
|
|
|
|
|
|
/* create usb hub thread */
|
2014-03-04 08:59:53 +08:00
|
|
|
thread = rt_thread_create("usbh", rt_usbh_hub_thread_entry, RT_NULL,
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|