rt-thread-official/components/drivers/usb/usbhost/core/driver.c

148 lines
3.5 KiB
C
Raw Normal View History

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-03-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 <rtservice.h>
2017-11-11 10:51:47 +08:00
#include <drivers/usb_host.h>
2013-01-08 22:40:58 +08:00
static rt_list_t _driver_list;
2021-02-23 17:05:01 +08:00
static rt_bool_t _driver_list_created = RT_FALSE;
2013-01-08 22:40:58 +08:00
/**
* This function will initilize the usb class driver related data structure,
* 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.
*/
rt_err_t rt_usbh_class_driver_init(void)
2013-01-08 22:40:58 +08:00
{
2021-02-23 17:05:01 +08:00
if (_driver_list_created == RT_FALSE)
{
rt_list_init(&_driver_list);
_driver_list_created = RT_TRUE;
}
2021-03-08 18:19:04 +08:00
return RT_EOK;
2013-01-08 22:40:58 +08:00
}
/**
* This function will register an usb class driver to the class driver manager.
*
* @param drv the pointer of the usb class driver.
2021-03-08 18:19:04 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_class_driver_register(ucd_t drv)
2013-01-08 22:40:58 +08:00
{
if (drv == RT_NULL) return -RT_ERROR;
2021-02-23 17:05:01 +08:00
if (rt_usbh_class_driver_find(drv->class_code, drv->subclass_code) == RT_NULL)
{
/* insert class driver into driver list */
rt_list_insert_after(&_driver_list, &(drv->list));
}
2021-03-08 18:19:04 +08:00
return RT_EOK;
2013-01-08 22:40:58 +08:00
}
/**
* This function will removes a previously registed usb class driver.
*
* @param drv the pointer of the usb class driver structure.
2021-03-08 18:19:04 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_class_driver_unregister(ucd_t drv)
2013-01-08 22:40:58 +08:00
{
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.
2021-03-08 18:19:04 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_class_driver_enable(ucd_t drv, void* args)
2013-01-08 22:40:58 +08:00
{
RT_ASSERT(drv != RT_NULL);
if(drv->enable != RT_NULL)
drv->enable(args);
2013-01-08 22:40:58 +08:00
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.
2021-03-08 18:19:04 +08:00
*
2013-01-08 22:40:58 +08:00
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_class_driver_disable(ucd_t drv, void* args)
2013-01-08 22:40:58 +08:00
{
RT_ASSERT(drv != RT_NULL);
if(drv->disable != RT_NULL)
drv->disable(args);
2013-01-08 22:40:58 +08:00
return RT_EOK;
}
/**
* This function finds a usb class driver by specified class code and subclass code.
*
2021-03-08 18:19:04 +08:00
* @param class_code the usb class driver's class code.
2013-01-08 22:40:58 +08:00
* @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)
2013-01-08 22:40:58 +08:00
{
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)
{
2021-03-08 18:19:04 +08:00
ucd_t drv =
2013-01-08 22:40:58 +08:00
(ucd_t)rt_list_entry(node, struct uclass_driver, list);
if (drv->class_code == class_code)
{
/* 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;
2022-01-26 11:00:16 +08:00
}