xref: /nrf52832-nimble/rt-thread/components/drivers/usb/usbhost/core/driver.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2011-03-12     Yi Qiu      first version
9  */
10 
11 #include <rtthread.h>
12 #include <rtservice.h>
13 #include <drivers/usb_host.h>
14 
15 static rt_list_t _driver_list;
16 
17 /**
18  * This function will initilize the usb class driver related data structure,
19  * and it should be invoked in the usb system initialization.
20  *
21  * @return the error code, RT_EOK on successfully.
22  */
rt_usbh_class_driver_init(void)23 rt_err_t rt_usbh_class_driver_init(void)
24 {
25     rt_list_init(&_driver_list);
26 
27     return RT_EOK;
28 }
29 
30 /**
31  * This function will register an usb class driver to the class driver manager.
32  *
33  * @param drv the pointer of the usb class driver.
34  *
35  * @return the error code, RT_EOK on successfully.
36  */
37 
rt_usbh_class_driver_register(ucd_t drv)38 rt_err_t rt_usbh_class_driver_register(ucd_t drv)
39 {
40     if (drv == RT_NULL) return -RT_ERROR;
41 
42     /* insert class driver into driver list */
43     rt_list_insert_after(&_driver_list, &(drv->list));
44 
45     return RT_EOK;
46 }
47 
48 /**
49  * This function will removes a previously registed usb class driver.
50  *
51  * @param drv the pointer of the usb class driver structure.
52  *
53  * @return the error code, RT_EOK on successfully.
54  */
rt_usbh_class_driver_unregister(ucd_t drv)55 rt_err_t rt_usbh_class_driver_unregister(ucd_t drv)
56 {
57     RT_ASSERT(drv != RT_NULL);
58 
59     /* remove class driver from driver list */
60     rt_list_remove(&(drv->list));
61 
62     return RT_EOK;
63 }
64 
65 /**
66  * This function will run an usb class driver.
67  *
68  * @param drv the pointer of usb class driver.
69  * @param args the parameter of run function.
70  *
71  * @return the error code, RT_EOK on successfully.
72  */
rt_usbh_class_driver_enable(ucd_t drv,void * args)73 rt_err_t rt_usbh_class_driver_enable(ucd_t drv, void* args)
74 {
75     RT_ASSERT(drv != RT_NULL);
76 
77     if(drv->enable != RT_NULL)
78         drv->enable(args);
79 
80     return RT_EOK;
81 }
82 
83 /**
84  * This function will stop a usb class driver.
85  *
86  * @param drv the pointer of usb class driver structure.
87  * @param args the argument of the stop function.
88  *
89  * @return the error code, RT_EOK on successfully.
90  */
rt_usbh_class_driver_disable(ucd_t drv,void * args)91 rt_err_t rt_usbh_class_driver_disable(ucd_t drv, void* args)
92 {
93     RT_ASSERT(drv != RT_NULL);
94 
95     if(drv->disable != RT_NULL)
96         drv->disable(args);
97 
98     return RT_EOK;
99 }
100 
101 
102 /**
103  * This function finds a usb class driver by specified class code and subclass code.
104  *
105  * @param class_code the usb class driver's class code.
106  * @param subclass_code the usb class driver's sub class code.
107  *
108  * @return the registered usb class driver on successful, or RT_NULL on failure.
109  */
rt_usbh_class_driver_find(int class_code,int subclass_code)110 ucd_t rt_usbh_class_driver_find(int class_code, int subclass_code)
111 {
112     struct rt_list_node *node;
113 
114     /* enter critical */
115     if (rt_thread_self() != RT_NULL)
116         rt_enter_critical();
117 
118     /* try to find driver object */
119     for (node = _driver_list.next; node != &_driver_list; node = node->next)
120     {
121         ucd_t drv =
122             (ucd_t)rt_list_entry(node, struct uclass_driver, list);
123         if (drv->class_code == class_code)
124         {
125             /* leave critical */
126             if (rt_thread_self() != RT_NULL)
127                 rt_exit_critical();
128 
129             return drv;
130         }
131     }
132 
133     /* leave critical */
134     if (rt_thread_self() != RT_NULL)
135         rt_exit_critical();
136 
137     /* not found */
138     return RT_NULL;
139 }
140 
141