Skip to content

Commit 9b44734

Browse files
authored
Merge pull request #4378 from LeeChunHei/usbh_construct
添加多過一個usb host的可能性
2 parents 32a5668 + 55a302c commit 9b44734

File tree

4 files changed

+51
-31
lines changed

4 files changed

+51
-31
lines changed

components/drivers/include/drivers/usb_host.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2011-3-12 Yi Qiu first version
9+
* 2021-02-23 Leslie Lee provide possibility for multi usb host
910
*/
1011

1112
#ifndef __RT_USB_HOST_H__
@@ -137,6 +138,7 @@ struct uhcd
137138
uhcd_ops_t ops;
138139
rt_uint8_t num_ports;
139140
uhub_t roothub;
141+
struct rt_messagequeue *usb_mq;
140142
};
141143
typedef struct uhcd* uhcd_t;
142144

@@ -163,7 +165,7 @@ struct uhost_msg
163165
typedef struct uhost_msg* uhost_msg_t;
164166

165167
/* usb host system interface */
166-
rt_err_t rt_usb_host_init(void);
168+
rt_err_t rt_usb_host_init(const char *name);
167169
void rt_usbh_hub_init(struct uhcd *hcd);
168170

169171
/* usb host core interface */
@@ -203,7 +205,7 @@ rt_err_t rt_usbh_hub_clear_port_feature(uhub_t uhub, rt_uint16_t port,
203205
rt_err_t rt_usbh_hub_set_port_feature(uhub_t uhub, rt_uint16_t port,
204206
rt_uint16_t feature);
205207
rt_err_t rt_usbh_hub_reset_port(uhub_t uhub, rt_uint16_t port);
206-
rt_err_t rt_usbh_event_signal(struct uhost_msg* msg);
208+
rt_err_t rt_usbh_event_signal(uhcd_t uhcd, struct uhost_msg* msg);
207209

208210

209211
void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS);
@@ -265,5 +267,3 @@ rt_inline int rt_usb_hcd_setup_xfer(uhcd_t hcd, upipe_t pipe, ureq_t setup, int
265267
#endif
266268

267269
#endif
268-
269-

components/drivers/usb/usbhost/core/driver.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2011-03-12 Yi Qiu first version
9+
* 2021-02-23 Leslie Lee provide possibility for multi usb host
910
*/
1011

1112
#include <rtthread.h>
1213
#include <rtservice.h>
1314
#include <drivers/usb_host.h>
1415

1516
static rt_list_t _driver_list;
17+
static rt_bool_t _driver_list_created = RT_FALSE;
1618

1719
/**
1820
* This function will initilize the usb class driver related data structure,
@@ -22,8 +24,11 @@ static rt_list_t _driver_list;
2224
*/
2325
rt_err_t rt_usbh_class_driver_init(void)
2426
{
25-
rt_list_init(&_driver_list);
26-
27+
if (_driver_list_created == RT_FALSE)
28+
{
29+
rt_list_init(&_driver_list);
30+
_driver_list_created = RT_TRUE;
31+
}
2732
return RT_EOK;
2833
}
2934

@@ -39,8 +44,11 @@ rt_err_t rt_usbh_class_driver_register(ucd_t drv)
3944
{
4045
if (drv == RT_NULL) return -RT_ERROR;
4146

42-
/* insert class driver into driver list */
43-
rt_list_insert_after(&_driver_list, &(drv->list));
47+
if (rt_usbh_class_driver_find(drv->class_code, drv->subclass_code) == RT_NULL)
48+
{
49+
/* insert class driver into driver list */
50+
rt_list_insert_after(&_driver_list, &(drv->list));
51+
}
4452

4553
return RT_EOK;
4654
}
@@ -136,5 +144,4 @@ ucd_t rt_usbh_class_driver_find(int class_code, int subclass_code)
136144

137145
/* not found */
138146
return RT_NULL;
139-
}
140-
147+
}

components/drivers/usb/usbhost/core/hub.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2011-12-12 Yi Qiu first version
9+
* 2021-02-23 Leslie Lee provide possibility for multi usb host
910
*/
1011

1112
#include <rtthread.h>
1213
#include <drivers/usb_host.h>
1314

1415
#define USB_THREAD_STACK_SIZE 4096
1516

16-
static struct rt_messagequeue *usb_mq;
17+
// static struct rt_messagequeue *usb_mq;
1718
static struct uclass_driver hub_driver;
18-
static struct uhub root_hub;
19+
// static struct uhub root_hub;
1920

2021
static rt_err_t root_hub_ctrl(struct uhcd *hcd, rt_uint16_t port, rt_uint8_t cmd, void *args)
2122
{
@@ -92,7 +93,7 @@ void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool
9293
{
9394
hcd->roothub->port_status[port - 1] |= PORT_LSDA;
9495
}
95-
rt_usbh_event_signal(&msg);
96+
rt_usbh_event_signal(hcd, &msg);
9697
}
9798

9899
void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port)
@@ -102,7 +103,7 @@ void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port)
102103
msg.content.hub = hcd->roothub;
103104
hcd->roothub->port_status[port - 1] |= PORT_CCSC;
104105
hcd->roothub->port_status[port - 1] &= ~PORT_CCS;
105-
rt_usbh_event_signal(&msg);
106+
rt_usbh_event_signal(hcd, &msg);
106107
}
107108

108109
/**
@@ -647,12 +648,13 @@ ucd_t rt_usbh_class_driver_hub(void)
647648
*/
648649
static void rt_usbh_hub_thread_entry(void* parameter)
649650
{
651+
uhcd_t hcd = (uhcd_t)parameter;
650652
while(1)
651653
{
652654
struct uhost_msg msg;
653655

654656
/* receive message */
655-
if(rt_mq_recv(usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER)
657+
if(rt_mq_recv(hcd->usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER)
656658
!= RT_EOK ) continue;
657659

658660
//RT_DEBUG_LOG(RT_DEBUG_USB, ("msg type %d\n", msg.type));
@@ -679,12 +681,12 @@ static void rt_usbh_hub_thread_entry(void* parameter)
679681
*
680682
* @return the error code, RT_EOK on successfully.
681683
*/
682-
rt_err_t rt_usbh_event_signal(struct uhost_msg* msg)
684+
rt_err_t rt_usbh_event_signal(uhcd_t hcd, struct uhost_msg* msg)
683685
{
684686
RT_ASSERT(msg != RT_NULL);
685687

686688
/* send message to usb message queue */
687-
rt_mq_send(usb_mq, (void*)msg, sizeof(struct uhost_msg));
689+
rt_mq_send(hcd->usb_mq, (void*)msg, sizeof(struct uhost_msg));
688690

689691
return RT_EOK;
690692
}
@@ -698,21 +700,22 @@ rt_err_t rt_usbh_event_signal(struct uhost_msg* msg)
698700
void rt_usbh_hub_init(uhcd_t hcd)
699701
{
700702
rt_thread_t thread;
701-
/* link root hub to hcd */
702-
root_hub.is_roothub = RT_TRUE;
703-
hcd->roothub = &root_hub;
704-
root_hub.hcd = hcd;
705-
root_hub.num_ports = hcd->num_ports;
703+
/* create root hub for hcd */
704+
hcd->roothub = rt_malloc(sizeof(struct uhub));
705+
rt_memset(hcd->roothub, 0, sizeof(struct uhub));
706+
hcd->roothub->is_roothub = RT_TRUE;
707+
hcd->roothub->hcd = hcd;
708+
hcd->roothub->num_ports = hcd->num_ports;
706709
/* create usb message queue */
707-
usb_mq = rt_mq_create("usbh", 32, 16, RT_IPC_FLAG_FIFO);
710+
711+
hcd->usb_mq = rt_mq_create(hcd->parent.parent.name, 32, 16, RT_IPC_FLAG_FIFO);
708712

709713
/* create usb hub thread */
710-
thread = rt_thread_create("usbh", rt_usbh_hub_thread_entry, RT_NULL,
714+
thread = rt_thread_create(hcd->parent.parent.name, rt_usbh_hub_thread_entry, hcd,
711715
USB_THREAD_STACK_SIZE, 8, 20);
712716
if(thread != RT_NULL)
713717
{
714718
/* startup usb host thread */
715719
rt_thread_startup(thread);
716720
}
717-
}
718-
721+
}

components/drivers/usb/usbhost/core/usbhost.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2011-12-12 Yi Qiu first version
9+
* 2021-02-23 Leslie Lee provide possibility for multi usb host
910
*/
1011
#include <rtthread.h>
1112
#include <drivers/usb_host.h>
@@ -22,15 +23,15 @@
2223
*
2324
* @return none.
2425
*/
25-
rt_err_t rt_usb_host_init(void)
26+
rt_err_t rt_usb_host_init(const char *name)
2627
{
2728
ucd_t drv;
2829
rt_device_t uhc;
2930

30-
uhc = rt_device_find(USB_HOST_CONTROLLER_NAME);
31+
uhc = rt_device_find(name);
3132
if(uhc == RT_NULL)
3233
{
33-
rt_kprintf("can't find usb host controller %s\n", USB_HOST_CONTROLLER_NAME);
34+
rt_kprintf("can't find usb host controller %s\n", name);
3435
return -RT_ERROR;
3536
}
3637

@@ -44,6 +45,16 @@ rt_err_t rt_usb_host_init(void)
4445
/* register mass storage class driver */
4546
drv = rt_usbh_class_driver_storage();
4647
rt_usbh_class_driver_register(drv);
48+
#endif
49+
#ifdef RT_USBH_HID
50+
/* register mass storage class driver */
51+
drv = rt_usbh_class_driver_hid();
52+
rt_usbh_class_driver_register(drv);
53+
#ifdef RT_USBH_HID_MOUSE
54+
uprotocal_t protocal;
55+
protocal = rt_usbh_hid_protocal_mouse();
56+
rt_usbh_hid_protocal_register(protocal);
57+
#endif
4758
#endif
4859

4960
/* register hub class driver */
@@ -54,5 +65,4 @@ rt_err_t rt_usb_host_init(void)
5465
rt_device_init(uhc);
5566

5667
return RT_EOK;
57-
}
58-
68+
}

0 commit comments

Comments
 (0)