1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2011-12-12     Yi Qiu      first version
9  * 2021-02-23     Leslie Lee  provide possibility for multi usb host
10  */
11 #include <rtthread.h>
12 #include <drivers/usb_host.h>
13 
14 #define USB_HOST_CONTROLLER_NAME      "usbh"
15 
16 #if defined(RT_USBH_HID_KEYBOARD) || defined(RT_USBH_HID_MOUSE)
17 #include <hid.h>
18 #endif
19 
20 /**
21  * This function will initialize the usb host stack, all the usb class driver and
22  * host controller driver are also be initialized here.
23  *
24  * @return none.
25  */
rt_usb_host_init(const char * name)26 rt_err_t rt_usb_host_init(const char *name)
27 {
28     ucd_t drv;
29     rt_device_t uhc;
30 
31     uhc = rt_device_find(name);
32     if(uhc == RT_NULL)
33     {
34         rt_kprintf("can't find usb host controller %s\n", name);
35         return -RT_ERROR;
36     }
37 
38     /* initialize usb hub */
39     rt_usbh_hub_init((uhcd_t)uhc);
40 
41     /* initialize class driver */
42     rt_usbh_class_driver_init();
43 
44 #ifdef RT_USBH_MSTORAGE
45     /* register mass storage class driver */
46     drv = rt_usbh_class_driver_storage();
47     rt_usbh_class_driver_register(drv);
48 #endif
49 #ifdef RT_USBH_HID
50     extern ucd_t rt_usbh_class_driver_hid(void);
51     /* register mass storage class driver */
52     drv = rt_usbh_class_driver_hid();
53     rt_usbh_class_driver_register(drv);
54 #ifdef RT_USBH_HID_MOUSE
55     {
56         extern uprotocal_t rt_usbh_hid_protocal_mouse(void);
57         rt_usbh_hid_protocal_register(rt_usbh_hid_protocal_mouse());
58     }
59 #endif
60 #ifdef RT_USBH_HID_KEYBOARD
61     {
62         extern uprotocal_t rt_usbh_hid_protocal_kbd(void);
63         rt_usbh_hid_protocal_register(rt_usbh_hid_protocal_kbd());
64     }
65 #endif
66 #endif
67 
68     /* register hub class driver */
69     drv = rt_usbh_class_driver_hub();
70     rt_usbh_class_driver_register(drv);
71 
72     /* initialize usb host controller */
73     rt_device_init(uhc);
74 
75     return RT_EOK;
76 }
77