Initial commit: TXW82x FPV v2.7.0.7-42229 SDK + project sources
This commit is contained in:
155
sdk/lib/bus/rttusb/usbhost/core/driver.c
Normal file
155
sdk/lib/bus/rttusb/usbhost/core/driver.c
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-03-12 Yi Qiu first version
|
||||
* 2021-02-23 Leslie Lee provide possibility for multi usb host
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtservice.h>
|
||||
#include <include/rttusb_host.h>
|
||||
|
||||
static rt_list_t _driver_list;
|
||||
static rt_bool_t _driver_list_created = RT_FALSE;
|
||||
|
||||
/**
|
||||
* This function will initilize the usb class driver related data structure,
|
||||
* and it should be invoked in the usb system initialization.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_class_driver_init(void)
|
||||
{
|
||||
if (_driver_list_created == RT_FALSE)
|
||||
{
|
||||
rt_list_init(&_driver_list);
|
||||
_driver_list_created = RT_TRUE;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will register an usb class driver to the class driver manager.
|
||||
*
|
||||
* @param drv the pointer of the usb class driver.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
|
||||
rt_err_t rt_usbh_class_driver_register(ucd_t drv)
|
||||
{
|
||||
if (drv == RT_NULL) return -RT_ERROR;
|
||||
|
||||
if (rt_usbh_class_driver_find(drv->class_code, drv->subclass_code, drv->vendor_id) == RT_NULL)
|
||||
{
|
||||
/* insert class driver into driver list */
|
||||
rt_list_insert_after(&_driver_list, &(drv->list));
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will removes a previously registed usb class driver.
|
||||
*
|
||||
* @param drv the pointer of the usb class driver structure.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_class_driver_unregister(ucd_t drv)
|
||||
{
|
||||
RT_ASSERT(drv != RT_NULL);
|
||||
|
||||
/* remove class driver from driver list */
|
||||
rt_list_remove(&(drv->list));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will run an usb class driver.
|
||||
*
|
||||
* @param drv the pointer of usb class driver.
|
||||
* @param args the parameter of run function.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_class_driver_enable(ucd_t drv, void* args)
|
||||
{
|
||||
RT_ASSERT(drv != RT_NULL);
|
||||
|
||||
if(drv->enable != RT_NULL)
|
||||
drv->enable(args);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will stop a usb class driver.
|
||||
*
|
||||
* @param drv the pointer of usb class driver structure.
|
||||
* @param args the argument of the stop function.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_class_driver_disable(ucd_t drv, void* args)
|
||||
{
|
||||
RT_ASSERT(drv != RT_NULL);
|
||||
|
||||
if(drv->disable != RT_NULL)
|
||||
drv->disable(args);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This function finds a usb class driver by specified class code and subclass code.
|
||||
*
|
||||
* @param class_code the usb class driver's class code.
|
||||
* @param subclass_code the usb class driver's sub class code.
|
||||
*
|
||||
* @return the registered usb class driver on successful, or RT_NULL on failure.
|
||||
*/
|
||||
ucd_t rt_usbh_class_driver_find(int class_code, int subclass_code, int vendor_id)
|
||||
{
|
||||
struct rt_list_node *node;
|
||||
|
||||
/* enter critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_enter_critical();
|
||||
|
||||
/* try to find driver object */
|
||||
for (node = _driver_list.next; node != &_driver_list; node = node->next)
|
||||
{
|
||||
ucd_t drv =
|
||||
(ucd_t)rt_list_entry(node, struct uclass_driver, list);
|
||||
if (drv->class_code == class_code)
|
||||
{
|
||||
if (drv->class_code == USB_CLASS_VEND_SPECIFIC)
|
||||
{
|
||||
/* need check vendor id */
|
||||
if (drv->vendor_id != vendor_id)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/* leave critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_exit_critical();
|
||||
|
||||
return drv;
|
||||
}
|
||||
}
|
||||
|
||||
/* leave critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_exit_critical();
|
||||
|
||||
/* not found */
|
||||
return RT_NULL;
|
||||
}
|
||||
952
sdk/lib/bus/rttusb/usbhost/core/hub.c
Normal file
952
sdk/lib/bus/rttusb/usbhost/core/hub.c
Normal file
@@ -0,0 +1,952 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
* 2021-02-23 Leslie Lee provide possibility for multi usb host
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "dev/usb/hgusb20_v1_dev_api.h"
|
||||
|
||||
#define USB_THREAD_STACK_SIZE 2048
|
||||
|
||||
//#define DBG_TAG "usb.host.hub"
|
||||
//#define DBG_LVL DBG_INFO
|
||||
//#include <rtdbg.h>
|
||||
|
||||
|
||||
// static struct rt_messagequeue *usb_mq;
|
||||
static struct uclass_driver hub_driver;
|
||||
static rt_sem_t hub_sem = RT_NULL;
|
||||
static rt_thread_t usbh_hub_thread = RT_NULL;
|
||||
// static struct uhub root_hub;
|
||||
|
||||
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;
|
||||
}
|
||||
void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS)
|
||||
{
|
||||
struct uhost_msg msg;
|
||||
|
||||
if(hcd == NULL || hcd->roothub == NULL)
|
||||
{
|
||||
rt_kprintf("[%s]:hcd or hcd->roothub is NULL!!!\r\n",__FUNCTION__);
|
||||
return;
|
||||
}
|
||||
|
||||
msg.type = USB_MSG_CONNECT_IRQ;
|
||||
msg.content.hub = hcd->roothub;
|
||||
|
||||
rt_usbh_event_signal(hcd, &msg);
|
||||
}
|
||||
|
||||
void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port)
|
||||
{
|
||||
struct uhost_msg msg;
|
||||
|
||||
if(hcd == NULL || hcd->roothub == NULL)
|
||||
{
|
||||
rt_kprintf("[%s]:hcd or hcd->roothub is NULL!!!\r\n",__FUNCTION__);
|
||||
return;
|
||||
}
|
||||
|
||||
msg.type = USB_MSG_DISCONNECT_IRQ;
|
||||
msg.content.hub = hcd->roothub;
|
||||
|
||||
rt_usbh_event_signal(hcd, &msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
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;
|
||||
setup.wLength = nbytes;
|
||||
setup.wValue = USB_DESC_TYPE_HUB << 8;
|
||||
|
||||
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)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE;
|
||||
setup.bRequest = USB_REQ_GET_STATUS;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = 4;
|
||||
setup.wValue = 0;
|
||||
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)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
//LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
/* get roothub port status */
|
||||
if(hub->is_roothub)
|
||||
{
|
||||
root_hub_ctrl(hub->hcd, port, RH_GET_PORT_STATUS,
|
||||
(void*)buffer);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_OTHER;
|
||||
setup.bRequest = USB_REQ_GET_STATUS;
|
||||
setup.wIndex = port;
|
||||
setup.wLength = 4;
|
||||
setup.wValue = 0;
|
||||
|
||||
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)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(hub->hcd, hub->self->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
/* clear roothub feature */
|
||||
if(hub->is_roothub)
|
||||
{
|
||||
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 |
|
||||
USB_REQ_TYPE_OTHER;
|
||||
setup.bRequest = USB_REQ_CLEAR_FEATURE;
|
||||
setup.wIndex = port;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = feature;
|
||||
|
||||
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, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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_uint16_t feature)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(hub != RT_NULL);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
/* clear roothub feature */
|
||||
if(hub->is_roothub)
|
||||
{
|
||||
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 |
|
||||
USB_REQ_TYPE_OTHER;
|
||||
setup.bRequest = USB_REQ_SET_FEATURE;
|
||||
setup.wIndex = port;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = feature;
|
||||
|
||||
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, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will rest hub port, it is invoked when sub device attached to the hub 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);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
// rt_thread_delay(50);
|
||||
|
||||
/* reset hub port */
|
||||
ret = rt_usbh_hub_set_port_feature(hub, port, PORT_FEAT_RESET);
|
||||
if(ret != RT_EOK) return ret;
|
||||
|
||||
while(1)
|
||||
{
|
||||
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);
|
||||
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.
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
rt_err_t ret;
|
||||
int i = 0, times = 20;
|
||||
rt_uint32_t pstatus;
|
||||
rt_bool_t connect = RT_TRUE;
|
||||
int delayticks = USB_DEBOUNCE_TIME;
|
||||
if (delayticks < 1)
|
||||
delayticks = 1;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(hub != RT_NULL);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
for(i=0; i<times; i++)
|
||||
{
|
||||
ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
|
||||
if(ret != RT_EOK) return ret;
|
||||
|
||||
if(!(pstatus & PORT_CCS))
|
||||
{
|
||||
connect = RT_FALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
if((pstatus & PORT_CCS))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
rt_thread_delay(delayticks);
|
||||
}
|
||||
LOG_D("%s %d pstatus:%X\n",__FUNCTION__,__LINE__,pstatus);
|
||||
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.
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
int i;
|
||||
rt_bool_t reconnect;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(hub != RT_NULL);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
if(!hub->is_roothub){
|
||||
rt_usbh_hub_alloc_ep0_pipe(hub);
|
||||
}
|
||||
|
||||
/* get usb device instance */
|
||||
for (i = 0; i < hub->num_ports; i++)
|
||||
{
|
||||
rt_err_t ret;
|
||||
struct uinstance* device;
|
||||
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;
|
||||
|
||||
LOG_D("port %d status 0x%x", i + 1, pstatus);
|
||||
|
||||
/* check port status change */
|
||||
if (pstatus & PORT_CCSC)
|
||||
{
|
||||
/* clear port status change feature */
|
||||
rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_CONNECTION);
|
||||
rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
|
||||
reconnect = RT_TRUE;
|
||||
}
|
||||
|
||||
if(pstatus & PORT_PESC)
|
||||
{
|
||||
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;
|
||||
|
||||
/* reset usb roothub port */
|
||||
rt_usbh_hub_reset_port(hub, i + 1);
|
||||
|
||||
ret = rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
|
||||
if(ret != RT_EOK) continue;
|
||||
|
||||
ret = rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
|
||||
if(ret != RT_EOK) continue;
|
||||
|
||||
os_printf("pstatus:%x\n",pstatus);
|
||||
/* set usb device speed */
|
||||
if (pstatus & PORT_HSDA) {
|
||||
device->speed = RTTUSB_DEV_HighSpeed;
|
||||
} else if (pstatus & PORT_LSDA) {
|
||||
device->speed = RTTUSB_DEV_LowSpeed;
|
||||
} else {
|
||||
device->speed = RTTUSB_DEV_FullSpeed;
|
||||
}
|
||||
device->parent_hub = hub;
|
||||
device->hcd = hub->hcd;
|
||||
device->port = i + 1;
|
||||
hub->child[i] = device;
|
||||
|
||||
os_printf("hub device speed:%x device port:%x\n",device->speed,device->port);
|
||||
|
||||
if (!hub->is_roothub)
|
||||
{
|
||||
struct uhub_param hub_param;
|
||||
hub_param.hub_addr = 1;
|
||||
hub_param.hub_mtt_en = 0;
|
||||
hub_param.dev_addr = 0;
|
||||
hub_param.dev_port = device->port;
|
||||
hub_param.dev_speed = device->speed;
|
||||
rt_usb_hcd_hub_port_set(hub->hcd, &hub_param);
|
||||
}
|
||||
|
||||
/* attatch the usb instance to the hcd */
|
||||
rt_usbh_attatch_instance(device);
|
||||
}
|
||||
}
|
||||
os_printf("%s %d return!\n",__FUNCTION__,__LINE__);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//hub下行端点暂不支持热插拔
|
||||
void usbh_hub_thread_entry(void *arg)
|
||||
{
|
||||
upipe_t pipe;
|
||||
uhub_t hub;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
RT_ASSERT(arg != RT_NULL);
|
||||
|
||||
pipe = (upipe_t)arg;
|
||||
hub = (uhub_t)pipe->user_data;
|
||||
|
||||
while(1)
|
||||
{
|
||||
rt_sem_take(hub_sem, RT_WAITING_FOREVER);
|
||||
|
||||
LOG_D("hub int xfer...");
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(pipe->inst->hcd != RT_NULL);
|
||||
|
||||
rt_usb_hcd_pipe_xfer(hub->self->hcd, pipe, hub->buffer, pipe->ep.wMaxPacketSize, timeout);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
upipe_t pipe;
|
||||
uhub_t hub;
|
||||
struct uhost_msg msg;
|
||||
|
||||
RT_ASSERT(context != RT_NULL);
|
||||
|
||||
pipe = (upipe_t)context;
|
||||
hub = (uhub_t)pipe->user_data;
|
||||
|
||||
if(pipe->status != UPIPE_STATUS_OK)
|
||||
{
|
||||
LOG_D("hub irq error");
|
||||
rt_sem_release(hub_sem);
|
||||
return;
|
||||
}
|
||||
os_printf("%s %d\n",__FUNCTION__,__LINE__);
|
||||
msg.type = USB_MSG_CONNECT_CHANGE;
|
||||
msg.content.hub = hub;
|
||||
rt_usbh_event_signal(pipe->inst->hcd, &msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
static rt_err_t rt_usbh_hub_enable(void *arg)
|
||||
{
|
||||
int i = 0;
|
||||
rt_err_t ret = RT_EOK;
|
||||
uep_desc_t ep_desc = RT_NULL;
|
||||
uhub_t hub;
|
||||
upipe_t pipe;
|
||||
struct uinstance* device;
|
||||
struct uhintf* intf = (struct uhintf*)arg;
|
||||
upipe_t pipe_in = RT_NULL;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
/* paremeter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
|
||||
/* get usb device instance */
|
||||
device = intf->device;
|
||||
LOG_D("=======%s %d=======\n",__FUNCTION__,__LINE__);
|
||||
/* create a hub instance */
|
||||
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;
|
||||
|
||||
/* get hub descriptor head */
|
||||
ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc, 8);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("get hub descriptor failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* get full hub descriptor */
|
||||
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;
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
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;
|
||||
|
||||
hub->hcd = device->hcd;
|
||||
hub->self = device;
|
||||
|
||||
/* reset all hub ports */
|
||||
for (i = 0; i < hub->num_ports; i++)
|
||||
{
|
||||
rt_usbh_hub_set_port_feature(hub, i + 1, PORT_FEAT_POWER);
|
||||
rt_thread_delay(hub->hub_desc.pwron_to_good
|
||||
* 2 * RT_TICK_PER_SECOND / 1000 );
|
||||
}
|
||||
|
||||
if(intf->intf_desc->bNumEndpoints != 1)
|
||||
return -RT_ERROR;
|
||||
|
||||
/* get endpoint descriptor from interface descriptor */
|
||||
rt_usbh_get_endpoint_descriptor(intf->intf_desc, 0, &ep_desc);
|
||||
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)
|
||||
{
|
||||
analysis_usb_ep_desc(ep_desc);
|
||||
|
||||
if (rt_usb_hcd_alloc_pipe(device->hcd, &pipe, device, ep_desc) != RT_EOK) {
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_usb_instance_add_pipe(device, pipe);
|
||||
|
||||
/* allocate a pipe according to the endpoint type */
|
||||
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);
|
||||
}
|
||||
else return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(device->hcd != RT_NULL);
|
||||
pipe_in->user_data = hub;
|
||||
|
||||
|
||||
hub_sem = rt_sem_create("hub_sem", 0, RT_IPC_FLAG_FIFO);
|
||||
if (hub_sem == RT_NULL) {
|
||||
os_printf("hub sem create err!!!\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
usbh_hub_thread = rt_thread_create("usbh_hub_thread",
|
||||
usbh_hub_thread_entry,
|
||||
pipe_in,
|
||||
1024,
|
||||
OS_TASK_PRIORITY_BELOW_NORMAL,
|
||||
20);
|
||||
|
||||
if(usbh_hub_thread != RT_NULL)
|
||||
rt_thread_startup(usbh_hub_thread);
|
||||
|
||||
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
|
||||
* 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)
|
||||
{
|
||||
int i;
|
||||
uhub_t hub;
|
||||
struct uhintf* intf = (struct uhintf*)arg;
|
||||
|
||||
/* paremeter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
|
||||
LOG_D("rt_usbh_hub_stop");
|
||||
hub = (uhub_t)intf->user_data;
|
||||
|
||||
for(i=0; i<hub->num_ports; i++)
|
||||
{
|
||||
if(hub->child[i] != RT_NULL)
|
||||
rt_usbh_detach_instance(hub->child[i]);
|
||||
}
|
||||
|
||||
rt_thread_delete(usbh_hub_thread);
|
||||
usbh_hub_thread = RT_NULL;
|
||||
|
||||
rt_sem_delete(hub_sem);
|
||||
hub_sem = RT_NULL;
|
||||
|
||||
if(hub != RT_NULL) rt_free(hub);
|
||||
|
||||
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.
|
||||
*/
|
||||
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;
|
||||
|
||||
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.
|
||||
*/
|
||||
static void rt_usbh_hub_thread_entry(void* parameter)
|
||||
{
|
||||
uhcd_t hcd = (uhcd_t)parameter;
|
||||
while(1)
|
||||
{
|
||||
struct uhost_msg msg;
|
||||
|
||||
/* receive message */
|
||||
if (rt_mq_recv(hcd->usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER) < 0)
|
||||
continue;
|
||||
|
||||
//USB CONNECT 同步状态设置
|
||||
if (msg.type == USB_MSG_CONNECT_IRQ) {
|
||||
uhub_t hub = msg.content.hub;
|
||||
rt_bool_t isHS = RT_TRUE;
|
||||
rt_uint8_t port = 1;
|
||||
hub->port_status[port - 1] |= PORT_CCS | PORT_CCSC;
|
||||
if(isHS)
|
||||
{
|
||||
hub->port_status[port - 1] &= ~PORT_LSDA;
|
||||
}
|
||||
else
|
||||
{
|
||||
hub->port_status[port - 1] |= PORT_LSDA;
|
||||
}
|
||||
}
|
||||
|
||||
//USB DISCONNECT 同步状态设置
|
||||
if (msg.type == USB_MSG_DISCONNECT_IRQ) {
|
||||
uhub_t hub = msg.content.hub;
|
||||
rt_uint8_t port = 1;
|
||||
hub->port_status[port - 1] |= PORT_CCSC;
|
||||
hub->port_status[port - 1] &= ~PORT_CCS;
|
||||
}
|
||||
|
||||
switch (msg.type)
|
||||
{
|
||||
case USB_MSG_CONNECT_IRQ:
|
||||
case USB_MSG_DISCONNECT_IRQ:
|
||||
case USB_MSG_CONNECT_CHANGE:
|
||||
rt_usbh_hub_port_change(msg.content.hub);
|
||||
if(!msg.content.hub->is_roothub)
|
||||
{
|
||||
rt_sem_release(hub_sem);
|
||||
}
|
||||
break;
|
||||
case USB_MSG_CALLBACK:
|
||||
/* invoke callback */
|
||||
msg.content.cb.function(msg.content.cb.context);
|
||||
break;
|
||||
case USB_MSG_DELETE_THREAD:
|
||||
goto __exit_end;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
__exit_end:
|
||||
hcd->thread_status = 0;
|
||||
/* 挂起线程等待删除 */
|
||||
rt_thread_suspend(hcd->thread);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
rt_err_t rt_usbh_event_signal(uhcd_t hcd, struct uhost_msg* msg)
|
||||
{
|
||||
RT_ASSERT(msg != RT_NULL);
|
||||
|
||||
/* send message to usb message queue */
|
||||
rt_mq_send(hcd->usb_mq, (void*)msg, sizeof(struct uhost_msg));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will initialize usb hub thread.
|
||||
*
|
||||
* @return none.
|
||||
*
|
||||
*/
|
||||
void rt_usbh_hub_init(uhcd_t hcd, const char* dev)
|
||||
{
|
||||
rt_thread_t thread;
|
||||
/* create root hub for hcd */
|
||||
hcd->roothub = rt_malloc(sizeof(struct uhub));
|
||||
if(hcd->roothub == RT_NULL)
|
||||
{
|
||||
LOG_E("hcd->roothub: allocate buffer failed.");
|
||||
return;
|
||||
}
|
||||
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;
|
||||
/* create usb message queue */
|
||||
|
||||
if(hcd->usb_mq == RT_NULL)
|
||||
{
|
||||
hcd->usb_mq = rt_mq_create(NULL, 32, 16, RT_IPC_FLAG_FIFO);
|
||||
}
|
||||
|
||||
if(hcd->thread == RT_NULL)
|
||||
{
|
||||
/* create usb hub thread */
|
||||
thread = rt_thread_create((const char *)dev, rt_usbh_hub_thread_entry, hcd,
|
||||
USB_THREAD_STACK_SIZE, OS_TASK_PRIORITY_ABOVE_NORMAL, 20); //OS_TASK_PRIORITY_BELOW_NORMAL
|
||||
if(thread != RT_NULL)
|
||||
{
|
||||
/* startup usb host thread */
|
||||
rt_thread_startup(thread);
|
||||
}else{
|
||||
os_printf("hub thread start fail\n");
|
||||
}
|
||||
|
||||
hcd->thread = thread;
|
||||
hcd->thread_status = 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void rt_usbh_hub_deinit(uhcd_t hcd)
|
||||
{
|
||||
struct uhost_msg msg;
|
||||
rt_uint32_t timeout = 100;
|
||||
|
||||
if(hcd->thread != RT_NULL)
|
||||
{
|
||||
msg.type = USB_MSG_DELETE_THREAD;
|
||||
msg.content.hub = hcd->roothub;
|
||||
rt_usbh_event_signal(hcd, &msg);
|
||||
|
||||
do {
|
||||
os_sleep_ms(10);
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
os_printf("usbh wait hcd thread timeout!!!\n");
|
||||
break;
|
||||
}
|
||||
} while (hcd->thread_status);
|
||||
|
||||
|
||||
rt_thread_delete(hcd->thread);
|
||||
|
||||
|
||||
if(hcd->usb_mq)
|
||||
{
|
||||
rt_mq_delete(hcd->usb_mq);
|
||||
hcd->usb_mq = NULL;
|
||||
}
|
||||
|
||||
if(hcd->roothub)
|
||||
{
|
||||
rt_free(hcd->roothub);
|
||||
hcd->roothub = NULL;
|
||||
}
|
||||
|
||||
hcd->thread = NULL;
|
||||
|
||||
}
|
||||
}
|
||||
122
sdk/lib/bus/rttusb/usbhost/core/usbhost.c
Normal file
122
sdk/lib/bus/rttusb/usbhost/core/usbhost.c
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
* 2021-02-23 Leslie Lee provide possibility for multi usb host
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
|
||||
#define USB_HOST_CONTROLLER_NAME "usbh"
|
||||
|
||||
#if defined(RT_USBH_HID_KEYBOARD) || defined(RT_USBH_HID_MOUSE)
|
||||
#include <hid.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function will initialize the usb host stack, all the usb class driver and
|
||||
* host controller driver are also be initialized here.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
rt_err_t rt_usb_host_init(uint32 devid, const char *dev)
|
||||
{
|
||||
ucd_t drv;
|
||||
uhcd_t uhc;
|
||||
|
||||
uhc = (uhcd_t)dev_get(devid);
|
||||
if(uhc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("can't find usb host controller\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* initialize usb hub */
|
||||
uhc->devid = devid;
|
||||
rt_usbh_hub_init((uhcd_t)uhc, dev);
|
||||
|
||||
/* initialize class driver */
|
||||
rt_usbh_class_driver_init();
|
||||
|
||||
#ifdef RT_USBH_MSTORAGE
|
||||
/* register mass storage class driver */
|
||||
drv = rt_usbh_class_driver_storage();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_HID
|
||||
extern ucd_t rt_usbh_class_driver_hid(void);
|
||||
/* register mass storage class driver */
|
||||
drv = rt_usbh_class_driver_hid();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#ifdef RT_USBH_HID_MOUSE
|
||||
{
|
||||
extern uprotocal_t rt_usbh_hid_protocal_mouse(void);
|
||||
rt_usbh_hid_protocal_register(rt_usbh_hid_protocal_mouse());
|
||||
}
|
||||
#endif
|
||||
#ifdef RT_USBH_HID_KEYBOARD
|
||||
{
|
||||
extern uprotocal_t rt_usbh_hid_protocal_kbd(void);
|
||||
rt_usbh_hid_protocal_register(rt_usbh_hid_protocal_kbd());
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
#ifdef RT_USBH_CDC
|
||||
extern ucd_t rt_usbh_class_driver_cdc(void);
|
||||
drv = rt_usbh_class_driver_cdc();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_UVC
|
||||
extern ucd_t rt_usbh_class_driver_video(void);
|
||||
drv = rt_usbh_class_driver_video();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_UAC
|
||||
extern ucd_t rt_usbh_class_driver_audio(void);
|
||||
drv = rt_usbh_class_driver_audio();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_WIRELESS
|
||||
extern ucd_t rt_usbh_class_driver_wireless(void);
|
||||
drv = rt_usbh_class_driver_wireless();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_VENDOR_QUECTEL
|
||||
extern ucd_t rt_usbh_class_driver_quectel(void);
|
||||
drv = rt_usbh_class_driver_quectel();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_VENDOR_CHINAMOBILE
|
||||
extern ucd_t rt_usbh_class_driver_chinamobile(void);
|
||||
drv = rt_usbh_class_driver_chinamobile();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
|
||||
/* register hub class driver */
|
||||
drv = rt_usbh_class_driver_hub();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
|
||||
/* initialize usb host controller */
|
||||
rt_device_init(uhc);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_usb_host_deinit(uint32 devid)
|
||||
{
|
||||
uhcd_t uhc;
|
||||
|
||||
uhc = (uhcd_t)dev_get(devid);
|
||||
if(uhc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("can't find usb host controller\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_usbh_hub_deinit((uhcd_t)uhc);
|
||||
return RT_EOK;
|
||||
}
|
||||
878
sdk/lib/bus/rttusb/usbhost/core/usbhost_core.c
Normal file
878
sdk/lib/bus/rttusb/usbhost/core/usbhost_core.c
Normal file
@@ -0,0 +1,878 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
|
||||
//#define DBG_TAG "usbhost.core"
|
||||
//#define DBG_LVL DBG_INFO
|
||||
//#include <rtdbg.h>
|
||||
|
||||
static struct uinstance dev[USB_MAX_DEVICE];
|
||||
|
||||
/**
|
||||
* This function will allocate an usb device instance from system.
|
||||
*
|
||||
* @param parent the hub instance to which the new allocated device attached.
|
||||
* @param port the hub port.
|
||||
*
|
||||
* @return the allocate instance on successful, or RT_NULL on failure.
|
||||
*/
|
||||
uinst_t rt_usbh_alloc_instance(uhcd_t uhcd)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* lock scheduler */
|
||||
rt_enter_critical();
|
||||
|
||||
for(i=0; i<USB_MAX_DEVICE; i++)
|
||||
{
|
||||
/* to find an idle instance handle */
|
||||
if(dev[i].status != DEV_STATUS_IDLE) continue;
|
||||
|
||||
/* initialize the usb device instance */
|
||||
rt_memset(&dev[i], 0, sizeof(struct uinstance));
|
||||
|
||||
dev[i].status = DEV_STATUS_BUSY;
|
||||
dev[i].index = i + 1;
|
||||
dev[i].address = 0;
|
||||
dev[i].max_packet_size = 0x8;
|
||||
rt_list_init(&dev[i].pipe);
|
||||
dev[i].hcd = uhcd;
|
||||
/* unlock scheduler */
|
||||
rt_exit_critical();
|
||||
return &dev[i];
|
||||
}
|
||||
|
||||
/* unlock scheduler */
|
||||
rt_exit_critical();
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will attatch an usb device instance to a host controller,
|
||||
* and do device enumunation process.
|
||||
*
|
||||
* @param hcd the host controller driver.
|
||||
* @param device the usb device instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static struct uendpoint_descriptor ep0_out_desc =
|
||||
{
|
||||
/*endpoint descriptor*/
|
||||
USB_DESC_LENGTH_ENDPOINT,
|
||||
USB_DESC_TYPE_ENDPOINT,
|
||||
0x00 | USB_DIR_OUT,
|
||||
USB_EP_ATTR_CONTROL,
|
||||
0x00,
|
||||
0x00,
|
||||
};
|
||||
static struct uendpoint_descriptor ep0_in_desc =
|
||||
{
|
||||
/*endpoint descriptor*/
|
||||
USB_DESC_LENGTH_ENDPOINT,
|
||||
USB_DESC_TYPE_ENDPOINT,
|
||||
0x00 | USB_DIR_IN,
|
||||
USB_EP_ATTR_CONTROL,
|
||||
0x00,
|
||||
0x00,
|
||||
};
|
||||
|
||||
|
||||
void analysis_usb_dev_desc(udev_desc_t desc, uint32 desclen)
|
||||
{
|
||||
udev_desc_t dev_desc = (udev_desc_t)desc;
|
||||
os_printf("=================device desc=================\r\n");
|
||||
printf("bLength:%#x\n",dev_desc->bLength);
|
||||
printf("type:%#x\n",dev_desc->type);
|
||||
printf("bcdUSB:%#x\n",dev_desc->bcdUSB);
|
||||
printf("bDeviceClass:%#x\n",dev_desc->bDeviceClass);
|
||||
printf("bDeviceSubClass:%#x\n",dev_desc->bDeviceSubClass);
|
||||
printf("bDeviceProtocol:%#x\n",dev_desc->bDeviceProtocol);
|
||||
printf("bMaxPacketSize0:%#x\n",dev_desc->bMaxPacketSize0);
|
||||
printf("idVendor:%#x\n",dev_desc->idVendor);
|
||||
printf("idProduct:%#x\n",dev_desc->idProduct);
|
||||
printf("bcdDevice:%#x\n",dev_desc->bcdDevice);
|
||||
printf("iManufacturer:%#x\n",dev_desc->iManufacturer);
|
||||
printf("iProduct:%#x\n",dev_desc->iProduct);
|
||||
printf("iSerialNumber:%#x\n",dev_desc->iSerialNumber);
|
||||
printf("bNumConfigurations:%#x\n",dev_desc->bNumConfigurations);
|
||||
}
|
||||
|
||||
void analysis_usb_cfg_desc(ucfg_desc_t desc, uint32 desclen)
|
||||
{
|
||||
ucfg_desc_t cfg_desc = (ucfg_desc_t)desc;
|
||||
os_printf("=================config desc=================\r\n");
|
||||
printf("bLength:%#x\n",cfg_desc->bLength);
|
||||
printf("bDescriptorType:%#x\n",cfg_desc->type);
|
||||
printf("wTotalLength:%#x\n",cfg_desc->wTotalLength);
|
||||
printf("bNumInterfaces:%#x\n",cfg_desc->bNumInterfaces);
|
||||
printf("bConfigurationValue:%#x\n",cfg_desc->bConfigurationValue);
|
||||
printf("iConfiguration:%#x\n",cfg_desc->iConfiguration);
|
||||
printf("bmAttributes:%#x\n",cfg_desc->bmAttributes);
|
||||
printf("bMaxPower:%#x\n",cfg_desc->MaxPower);
|
||||
}
|
||||
|
||||
void analysis_usb_intf_desc(uintf_desc_t desc, uint32 desclen)
|
||||
{
|
||||
uintf_desc_t intf_desc = (uintf_desc_t)desc;
|
||||
os_printf("=================intf desc=================\r\n");
|
||||
printf("bLength:%#x\n",intf_desc->bLength);
|
||||
printf("bDescriptorType:%#x\n",intf_desc->type);
|
||||
printf("bInterfaceNumber:%#x\n",intf_desc->bInterfaceNumber);
|
||||
printf("bAlternateSetting:%d\n",intf_desc->bAlternateSetting);
|
||||
printf("bNumEndpoints:%d\n",intf_desc->bNumEndpoints);
|
||||
printf("bInterfaceClass:%#x\n",intf_desc->bInterfaceClass);
|
||||
printf("bInterfaceSubClass:%#x\n",intf_desc->bInterfaceSubClass);
|
||||
printf("bInterfaceProtocol:%d\n",intf_desc->bInterfaceProtocol);
|
||||
printf("iInterface:%#x\n",intf_desc->iInterface);
|
||||
}
|
||||
|
||||
void analysis_usb_ep_desc(uep_desc_t ep_desc)
|
||||
{
|
||||
os_printf("=================ep desc=================\r\n");
|
||||
printf("bLength:%#x\n",ep_desc->bLength);
|
||||
printf("type:%#x\n",ep_desc->type);
|
||||
printf("bEndpointAddress:%#x\n",ep_desc->bEndpointAddress);
|
||||
printf("bmAttributes:%#x\n",ep_desc->bmAttributes);
|
||||
printf("wMaxPacketSize:%#x\n",ep_desc->wMaxPacketSize);
|
||||
printf("bInterval:%#x\n",ep_desc->bInterval);
|
||||
}
|
||||
|
||||
void analysis_usb_iad_desc(uiad_desc_t iad_desc)
|
||||
{
|
||||
os_printf("=================iad desc=================\r\n");
|
||||
printf("bLength:%#x\n",iad_desc->bLength);
|
||||
printf("bDescriptorType:%#x\n",iad_desc->bDescriptorType);
|
||||
printf("bFirstInterface:%#x\n",iad_desc->bFirstInterface);
|
||||
printf("bInterfaceCount:%#x\n",iad_desc->bInterfaceCount);
|
||||
printf("bFunctionClass:%#x\n",iad_desc->bFunctionClass);
|
||||
printf("bFunctionSubClass:%#x\n",iad_desc->bFunctionSubClass);
|
||||
printf("bFunctionProtocol:%#x\n",iad_desc->bFunctionProtocol);
|
||||
printf("iFunction:%#x\n",iad_desc->iFunction);
|
||||
}
|
||||
|
||||
rt_err_t rt_usbh_hub_alloc_ep0_pipe(uhub_t hub)
|
||||
{
|
||||
if (!hub->is_roothub) {
|
||||
struct uinstance* device = hub->self;
|
||||
rt_usb_hub_ep0_open_pipe(device->hcd, &ep0_out_desc);
|
||||
rt_usb_hub_ep0_open_pipe(device->hcd, &ep0_in_desc);
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_usbh_attatch_instance(uinst_t device)
|
||||
{
|
||||
int i = 0;
|
||||
rt_err_t ret = RT_EOK;
|
||||
rt_uint8_t cfg_desc_buff[18];
|
||||
udev_desc_t dev_desc;
|
||||
uintf_desc_t intf_desc = RT_NULL;
|
||||
uiad_desc_t iad_desc;
|
||||
ucfg_desc_t cfg_desc;
|
||||
// uep_desc_t ep_desc;
|
||||
// rt_uint8_t ep_index;
|
||||
rt_uint8_t iad_index;
|
||||
// upipe_t pipe;
|
||||
ucd_t drv;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
rt_memset(cfg_desc_buff, 0, sizeof(cfg_desc_buff));
|
||||
cfg_desc = (ucfg_desc_t)cfg_desc_buff;
|
||||
dev_desc = &device->dev_desc;
|
||||
device->cfg_desc = RT_NULL;
|
||||
|
||||
/* alloc address 0 ep0 pipe*/
|
||||
ep0_out_desc.wMaxPacketSize = 8;
|
||||
ep0_in_desc.wMaxPacketSize = 8;
|
||||
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc);
|
||||
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc);
|
||||
|
||||
LOG_D("start enumnation");
|
||||
|
||||
/* get device descriptor head */
|
||||
ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, 8);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("get device descriptor head failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
analysis_usb_dev_desc(dev_desc, 8);//第一次获取设备描述符 打印设备描述符信息
|
||||
|
||||
/* reset bus */
|
||||
rt_usbh_hub_reset_port(device->parent_hub, device->port);
|
||||
rt_thread_delay(2);
|
||||
rt_usbh_hub_clear_port_feature(device->parent_hub, device->port, PORT_FEAT_C_CONNECTION);
|
||||
/* set device address */
|
||||
ret = rt_usbh_set_address(device);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("set device address failed\n");
|
||||
return ret;
|
||||
}
|
||||
/* free address 0 ep0 pipe*/
|
||||
|
||||
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out);
|
||||
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in);
|
||||
|
||||
/* set device max packet size */
|
||||
ep0_out_desc.wMaxPacketSize = device->dev_desc.bMaxPacketSize0;
|
||||
ep0_in_desc.wMaxPacketSize = device->dev_desc.bMaxPacketSize0;
|
||||
|
||||
/* alloc true address ep0 pipe*/
|
||||
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc);
|
||||
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc);
|
||||
LOG_D("get device descriptor length %d",
|
||||
dev_desc->bLength);
|
||||
|
||||
/* get full device descriptor again */
|
||||
ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, dev_desc->bLength);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("get full device descriptor failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
analysis_usb_dev_desc(dev_desc, dev_desc->bLength); //第二次获取设备描述符 打印设备描述符信息
|
||||
|
||||
LOG_D("Vendor ID 0x%x", dev_desc->idVendor);
|
||||
LOG_D("Product ID 0x%x", dev_desc->idProduct);
|
||||
|
||||
/* get configuration descriptor head */
|
||||
ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION, cfg_desc, 18);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("get configuration descriptor head failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
analysis_usb_cfg_desc(cfg_desc, 18); //第一次获取配置描述符 打印配置描述符信息
|
||||
|
||||
/* alloc memory for configuration descriptor */
|
||||
device->cfg_desc = (ucfg_desc_t)rt_zalloc(cfg_desc->wTotalLength);
|
||||
if(device->cfg_desc == RT_NULL)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
rt_memset(device->cfg_desc, 0, cfg_desc->wTotalLength);
|
||||
|
||||
/* get full configuration descriptor */
|
||||
ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION,
|
||||
device->cfg_desc, cfg_desc->wTotalLength);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("get full configuration descriptor failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
analysis_usb_cfg_desc(device->cfg_desc, device->cfg_desc->wTotalLength); //第二次获取配置描述符 打印配置描述符信息
|
||||
|
||||
/* set configuration */
|
||||
ret = rt_usbh_set_configure(device, 1);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
for(i=0; i<device->cfg_desc->bNumInterfaces; i++)
|
||||
{
|
||||
/* check IAD */
|
||||
rt_usbh_get_interface_associtaion_descriptor(device->cfg_desc, i, &iad_desc);
|
||||
if (iad_desc != RT_NULL)
|
||||
{
|
||||
analysis_usb_iad_desc(iad_desc);
|
||||
for (iad_index = iad_desc->bFirstInterface;
|
||||
iad_index < iad_desc->bFirstInterface + iad_desc->bInterfaceCount;
|
||||
++iad_index)
|
||||
{
|
||||
rt_usbh_get_interface_descriptor(device->cfg_desc, iad_index, &intf_desc);
|
||||
if (intf_desc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("rt_usb_get_interface_descriptor error\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
analysis_usb_intf_desc(intf_desc, 0);//获取接口描述符,打印接口描述符信息
|
||||
|
||||
LOG_D("interface class 0x%x, subclass 0x%x",
|
||||
intf_desc->bInterfaceClass,
|
||||
intf_desc->bInterfaceSubClass);
|
||||
#if 0 // 因为我们的USB端口不够用,不能每个EP都分配,改到driver里面分配
|
||||
/* alloc pipe*/
|
||||
for (ep_index = 0; ep_index < intf_desc->bNumEndpoints; ++ep_index)
|
||||
{
|
||||
rt_usbh_get_endpoint_descriptor(intf_desc, ep_index, &ep_desc);
|
||||
if(ep_desc != RT_NULL)
|
||||
{
|
||||
if(rt_usb_hcd_alloc_pipe(device->hcd, &pipe, device, ep_desc) != RT_EOK)
|
||||
{
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_usb_instance_add_pipe(device,pipe);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("get endpoint desc failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
device->intf[iad_index] = (struct uhintf *)rt_malloc(sizeof(struct uhintf));
|
||||
LOG_D("rt malloc intf[%d]\n",iad_index);
|
||||
if(device->intf[iad_index] == RT_NULL)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
device->intf[iad_index]->drv = RT_NULL;
|
||||
device->intf[iad_index]->device = device;
|
||||
device->intf[iad_index]->intf_desc = intf_desc;
|
||||
device->intf[iad_index]->user_data = RT_NULL;
|
||||
|
||||
}
|
||||
|
||||
i += iad_desc->bInterfaceCount - 1; // 前面for循环还会+1补回去
|
||||
// 通过接口关联class查找对应驱动
|
||||
drv = rt_usbh_class_driver_find(iad_desc->bFunctionClass,
|
||||
iad_desc->bFunctionSubClass, dev_desc->idVendor);
|
||||
if (drv != RT_NULL)
|
||||
{
|
||||
// 给接口关联的第一个接口添加驱动,后续就能通过怎么样找到?
|
||||
device->intf[iad_desc->bFirstInterface]->drv = drv;
|
||||
// 复合设备传递参数是指针的指针
|
||||
ret = rt_usbh_class_driver_enable(drv, (void *)&device->intf[iad_desc->bFirstInterface]);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("interface %d run class driver error\n", i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("find usb device driver failed\n");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* get interface descriptor through configuration descriptor */
|
||||
ret = rt_usbh_get_interface_descriptor(device->cfg_desc, i, &intf_desc);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("rt_usb_get_interface_descriptor error\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
analysis_usb_intf_desc(intf_desc, 0);//获取接口描述符,打印接口描述符信息
|
||||
LOG_D("interface class 0x%x, subclass 0x%x",
|
||||
intf_desc->bInterfaceClass,
|
||||
intf_desc->bInterfaceSubClass);
|
||||
#if 0 // 因为我们的USB端口不够用,不能每个EP都分配,改到driver里面分配
|
||||
/* alloc pipe*/
|
||||
for(ep_index = 0; ep_index < intf_desc->bNumEndpoints; ep_index++)
|
||||
{
|
||||
rt_usbh_get_endpoint_descriptor(intf_desc, ep_index, &ep_desc);
|
||||
if(ep_desc != RT_NULL)
|
||||
{
|
||||
if(rt_usb_hcd_alloc_pipe(device->hcd, &pipe, device, ep_desc) != RT_EOK)
|
||||
{
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_usb_instance_add_pipe(device,pipe);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("get endpoint desc failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* find driver by class code found in interface descriptor */
|
||||
drv = rt_usbh_class_driver_find(intf_desc->bInterfaceClass,
|
||||
intf_desc->bInterfaceSubClass, dev_desc->idVendor);
|
||||
|
||||
if(drv != RT_NULL)
|
||||
{
|
||||
/* allocate memory for interface device */
|
||||
device->intf[i] = (struct uhintf*)rt_malloc(sizeof(struct uhintf));
|
||||
if(device->intf[i] == RT_NULL)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
device->intf[i]->drv = drv;
|
||||
device->intf[i]->device = device;
|
||||
device->intf[i]->intf_desc = intf_desc;
|
||||
device->intf[i]->user_data = RT_NULL;
|
||||
|
||||
/* open usb class driver */
|
||||
// 非复合设备传递参数是指针
|
||||
ret = rt_usbh_class_driver_enable(drv, (void*)device->intf[i]);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("interface %d run class driver error\n", i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("find usb device driver failed\n");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will detach an usb device instance from its host controller,
|
||||
* and release all resource.
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_detach_instance(uinst_t device)
|
||||
{
|
||||
int i = 0;
|
||||
rt_list_t * l;
|
||||
if(device == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no usb instance to detach\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* free configration descriptor */
|
||||
if (device->cfg_desc) {
|
||||
for (i = 0; i < device->cfg_desc->bNumInterfaces; i++)
|
||||
{
|
||||
if (device->intf[i] == RT_NULL) continue;
|
||||
if (device->intf[i]->drv == RT_NULL)
|
||||
{
|
||||
LOG_D("rt free intf[%d]\n",i);
|
||||
rt_free(device->intf[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
RT_ASSERT(device->intf[i]->device == device);
|
||||
|
||||
LOG_D("free interface instance %d", i);
|
||||
rt_usbh_class_driver_disable(device->intf[i]->drv, (void*)device->intf[i]);
|
||||
rt_free(device->intf[i]);
|
||||
LOG_D("rt free intf[%d]\n",i);
|
||||
}
|
||||
rt_free(device->cfg_desc);
|
||||
}
|
||||
|
||||
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out);
|
||||
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in);
|
||||
|
||||
while(device->pipe.next!= &device->pipe)
|
||||
{
|
||||
l = device->pipe.next;
|
||||
rt_list_remove(l);
|
||||
rt_usb_hcd_free_pipe(device->hcd,rt_list_entry(l,struct upipe,list));
|
||||
}
|
||||
rt_memset(device, 0, sizeof(struct uinstance));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_GET_DESCRIPTO' bRequest for the usb device instance,
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
* @param type the type of descriptor bRequest.
|
||||
* @param buffer the data buffer to save requested data
|
||||
* @param nbytes the size of buffer
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_get_descriptor(uinst_t device, rt_uint8_t type, void* buffer,
|
||||
int nbytes)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_DEVICE;
|
||||
setup.bRequest = USB_REQ_GET_DESCRIPTOR;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = nbytes;
|
||||
setup.wValue = type << 8;
|
||||
|
||||
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)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_GET_DESCRIPTO' bRequest for the usb device instance,
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
* @param intf the interface number.
|
||||
* @param buffer the data buffer to save requested data
|
||||
* @param nbytes the size of buffer
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_get_string_descriptor(uinst_t device, int intf, void* buffer,
|
||||
int nbytes)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_DEVICE;
|
||||
setup.bRequest = USB_REQ_GET_DESCRIPTOR;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = nbytes;
|
||||
setup.wValue = (USB_DESC_TYPE_STRING << 8) | intf;
|
||||
|
||||
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) > 0)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set an address to the usb device.
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_set_address(uinst_t device)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
LOG_D("rt_usb_set_address");
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_DEVICE;
|
||||
setup.bRequest = USB_REQ_SET_ADDRESS;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = device->index;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
device->address = device->index;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set a configuration to the usb device.
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
* @param config the configuration number.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_set_configure(uinst_t device, int config)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_DEVICE;
|
||||
setup.bRequest = USB_REQ_SET_CONFIGURATION;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = config;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) != 0)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set an interface to the usb device.
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
* @param intf the interface number.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_set_interface(uinst_t device, int intf)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = USB_REQ_SET_INTERFACE;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = intf;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) != 0)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will clear feature for the endpoint of the usb device.
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
* @param endpoint the endpoint number of the usb device.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_clear_feature(uinst_t device, int endpoint, int feature)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_ENDPOINT;
|
||||
setup.bRequest = USB_REQ_CLEAR_FEATURE;
|
||||
setup.wIndex = endpoint;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = feature;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) != 0)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will get an interface descriptor from the configuration descriptor.
|
||||
*
|
||||
* @param cfg_desc the point of configuration descriptor structure.
|
||||
* @param num the number of interface descriptor.
|
||||
* @intf_desc the point of interface descriptor point.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_get_interface_descriptor(ucfg_desc_t cfg_desc, int num,
|
||||
uintf_desc_t* intf_desc)
|
||||
{
|
||||
rt_uint32_t ptr, depth = 0;
|
||||
udesc_t desc;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(cfg_desc != RT_NULL);
|
||||
|
||||
ptr = (rt_uint32_t)cfg_desc + cfg_desc->bLength;
|
||||
while(ptr < (rt_uint32_t)cfg_desc + cfg_desc->wTotalLength)
|
||||
{
|
||||
if(depth++ > 0x40)
|
||||
{
|
||||
*intf_desc = RT_NULL;
|
||||
return -RT_EIO;
|
||||
}
|
||||
desc = (udesc_t)ptr;
|
||||
if(desc->type == USB_DESC_TYPE_INTERFACE)
|
||||
{
|
||||
if(((uintf_desc_t)desc)->bInterfaceNumber == num)
|
||||
{
|
||||
*intf_desc = (uintf_desc_t)desc;
|
||||
|
||||
LOG_D("rt_usb_get_interface_descriptor: %d", num);
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
ptr = (rt_uint32_t)desc + desc->bLength;
|
||||
}
|
||||
|
||||
rt_kprintf("rt_usb_get_interface_descriptor %d failed\n", num);
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will get an interface associtaion descriptor from the configuration descriptor.
|
||||
*
|
||||
* @param cfg_desc the point of configuration descriptor structure.
|
||||
* @param num the number of interface descriptor.
|
||||
* @iad_desc the point of interface associtaion descriptor point.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_get_interface_associtaion_descriptor(ucfg_desc_t cfg_desc, int num,
|
||||
uiad_desc_t* iad_desc)
|
||||
{
|
||||
rt_uint32_t ptr, depth = 0;
|
||||
udesc_t desc;
|
||||
rt_uint16_t last_len = 0;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(cfg_desc != RT_NULL);
|
||||
|
||||
ptr = (rt_uint32_t)cfg_desc + cfg_desc->bLength;
|
||||
while(ptr < (rt_uint32_t)cfg_desc + cfg_desc->wTotalLength)
|
||||
{
|
||||
if(depth++ > 0x40)
|
||||
{
|
||||
*iad_desc = RT_NULL;
|
||||
return -RT_EIO;
|
||||
}
|
||||
desc = (udesc_t)ptr;
|
||||
// 寻找接口描述符
|
||||
if (desc->type == USB_DESC_TYPE_INTERFACE)
|
||||
{
|
||||
if (((uintf_desc_t)desc)->bInterfaceNumber == num)
|
||||
{
|
||||
// 找到目标接口描述后,检查上一个描述符是不是接口关联描述符
|
||||
ptr -= last_len;
|
||||
desc = (udesc_t)ptr;
|
||||
if (desc->type == USB_DESC_TYPE_IAD)
|
||||
{
|
||||
*iad_desc = (uiad_desc_t)desc;
|
||||
return RT_EOK;
|
||||
}
|
||||
else
|
||||
{
|
||||
*iad_desc = NULL;
|
||||
return -RT_EIO;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 解析下一个描述符
|
||||
ptr = (rt_uint32_t)desc + desc->bLength;
|
||||
last_len = desc->bLength;
|
||||
}
|
||||
// 无法解析
|
||||
*iad_desc = NULL;
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will get an endpoint descriptor from the interface descriptor.
|
||||
*
|
||||
* @param intf_desc the point of interface descriptor structure.
|
||||
* @param num the number of endpoint descriptor.
|
||||
* @param ep_desc the point of endpoint descriptor point.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num,
|
||||
uep_desc_t* ep_desc)
|
||||
{
|
||||
int count = 0, depth = 0;
|
||||
rt_uint32_t ptr;
|
||||
udesc_t desc;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(intf_desc != RT_NULL);
|
||||
RT_ASSERT(num < intf_desc->bNumEndpoints);
|
||||
*ep_desc = RT_NULL;
|
||||
|
||||
ptr = (rt_uint32_t)intf_desc + intf_desc->bLength;
|
||||
while(count < intf_desc->bNumEndpoints)
|
||||
{
|
||||
if(depth++ > 0x20)
|
||||
{
|
||||
*ep_desc = RT_NULL;
|
||||
return -RT_EIO;
|
||||
}
|
||||
desc = (udesc_t)ptr;
|
||||
if(desc->type == USB_DESC_TYPE_ENDPOINT)
|
||||
{
|
||||
if(num == count)
|
||||
{
|
||||
*ep_desc = (uep_desc_t)desc;
|
||||
|
||||
LOG_D("rt_usb_get_endpoint_descriptor: %d", num);
|
||||
return RT_EOK;
|
||||
}
|
||||
else count++;
|
||||
}
|
||||
ptr = (rt_uint32_t)desc + desc->bLength;
|
||||
}
|
||||
|
||||
rt_kprintf("rt_usb_get_endpoint_descriptor %d failed\n", num);
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int timeout)
|
||||
{
|
||||
int ret_size;
|
||||
rt_uint8_t * pbuffer = (rt_uint8_t *)buffer;
|
||||
|
||||
LOG_D("pipe transform remain size: %d", nbytes);
|
||||
ret_size = hcd->ops->pipe_xfer(pipe, USBH_PID_DATA, pbuffer, nbytes, timeout);
|
||||
if(ret_size <= nbytes)
|
||||
{
|
||||
return ret_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return nbytes;
|
||||
}
|
||||
|
||||
int rt_usb_hcd_hub_port_set(uhcd_t hcd, uhub_param_t param)
|
||||
{
|
||||
int ret = RT_ERROR;
|
||||
if (hcd->ops->hub_port_set) {
|
||||
ret = hcd->ops->hub_port_set(param);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user