1
1
/*
2
- * Copyright (c) 2022 Nordic Semiconductor ASA
2
+ * Copyright (c) 2022,2025 Nordic Semiconductor ASA
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*/
@@ -20,13 +20,27 @@ LOG_MODULE_REGISTER(uhs, CONFIG_USBH_LOG_LEVEL);
20
20
static K_KERNEL_STACK_DEFINE (usbh_stack , CONFIG_USBH_STACK_SIZE ) ;
21
21
static struct k_thread usbh_thread_data ;
22
22
23
+ static K_KERNEL_STACK_DEFINE (usbh_bus_stack , CONFIG_USBH_STACK_SIZE ) ;
24
+ static struct k_thread usbh_bus_thread_data ;
25
+
23
26
K_MSGQ_DEFINE (usbh_msgq , sizeof (struct uhc_event ),
24
27
CONFIG_USBH_MAX_UHC_MSG , sizeof (uint32_t ));
25
28
29
+ K_MSGQ_DEFINE (usbh_bus_msgq , sizeof (struct uhc_event ),
30
+ CONFIG_USBH_MAX_UHC_MSG , sizeof (uint32_t ));
31
+
26
32
static int usbh_event_carrier (const struct device * dev ,
27
33
const struct uhc_event * const event )
28
34
{
29
- return k_msgq_put (& usbh_msgq , event , K_NO_WAIT );
35
+ int err ;
36
+
37
+ if (event -> type == UHC_EVT_EP_REQUEST ) {
38
+ err = k_msgq_put (& usbh_msgq , event , K_NO_WAIT );
39
+ } else {
40
+ err = k_msgq_put (& usbh_bus_msgq , event , K_NO_WAIT );
41
+ }
42
+
43
+ return err ;
30
44
}
31
45
32
46
static int discard_ep_request (struct usbh_contex * const ctx ,
@@ -47,18 +61,6 @@ static ALWAYS_INLINE int usbh_event_handler(struct usbh_contex *const ctx,
47
61
{
48
62
int ret = 0 ;
49
63
50
- if (event -> type == UHC_EVT_EP_REQUEST ) {
51
- struct usb_device * const udev = event -> xfer -> udev ;
52
- usbh_udev_cb_t cb = event -> xfer -> cb ;
53
-
54
- if (event -> xfer -> cb ) {
55
- ret = cb (udev , event -> xfer );
56
- } else {
57
- ret = discard_ep_request (ctx , event -> xfer );
58
- }
59
- return ret ;
60
- }
61
-
62
64
switch (event -> type ) {
63
65
case UHC_EVT_DEV_CONNECTED_LS :
64
66
case UHC_EVT_DEV_CONNECTED_FS :
@@ -90,6 +92,23 @@ static ALWAYS_INLINE int usbh_event_handler(struct usbh_contex *const ctx,
90
92
return ret ;
91
93
}
92
94
95
+ static void usbh_bus_thread (void * p1 , void * p2 , void * p3 )
96
+ {
97
+ ARG_UNUSED (p1 );
98
+ ARG_UNUSED (p2 );
99
+ ARG_UNUSED (p3 );
100
+
101
+ struct usbh_contex * uhs_ctx ;
102
+ struct uhc_event event ;
103
+
104
+ while (true) {
105
+ k_msgq_get (& usbh_bus_msgq , & event , K_FOREVER );
106
+
107
+ uhs_ctx = (void * )uhc_get_event_ctx (event .dev );
108
+ usbh_event_handler (uhs_ctx , & event );
109
+ }
110
+ }
111
+
93
112
static void usbh_thread (void * p1 , void * p2 , void * p3 )
94
113
{
95
114
ARG_UNUSED (p1 );
@@ -98,12 +117,25 @@ static void usbh_thread(void *p1, void *p2, void *p3)
98
117
99
118
struct usbh_contex * uhs_ctx ;
100
119
struct uhc_event event ;
120
+ usbh_udev_cb_t cb ;
121
+ int ret ;
101
122
102
123
while (true) {
103
124
k_msgq_get (& usbh_msgq , & event , K_FOREVER );
104
125
126
+ __ASSERT (event .type == UHC_EVT_EP_REQUEST , "Wrong event type" );
105
127
uhs_ctx = (void * )uhc_get_event_ctx (event .dev );
106
- usbh_event_handler (uhs_ctx , & event );
128
+ cb = event .xfer -> cb ;
129
+
130
+ if (event .xfer -> cb ) {
131
+ ret = cb (event .xfer -> udev , event .xfer );
132
+ } else {
133
+ ret = discard_ep_request (uhs_ctx , event .xfer );
134
+ }
135
+
136
+ if (ret ) {
137
+ LOG_ERR ("Failed to handle request completion callback" );
138
+ }
107
139
}
108
140
}
109
141
@@ -138,6 +170,14 @@ static int uhs_pre_init(void)
138
170
139
171
k_thread_name_set (& usbh_thread_data , "usbh" );
140
172
173
+ k_thread_create (& usbh_bus_thread_data , usbh_bus_stack ,
174
+ K_KERNEL_STACK_SIZEOF (usbh_bus_stack ),
175
+ usbh_bus_thread ,
176
+ NULL , NULL , NULL ,
177
+ K_PRIO_COOP (9 ), 0 , K_NO_WAIT );
178
+
179
+ k_thread_name_set (& usbh_thread_data , "usbh_bus" );
180
+
141
181
return 0 ;
142
182
}
143
183
0 commit comments