11/*
2- * Copyright (c) 2022 , sakumisu
2+ * Copyright (c) 2025 , sakumisu
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 */
66#include "usbh_core.h"
77#include "usbh_hub.h"
8- #include "usb_xxx_reg.h"
98
10- struct dwc2_pipe {
11- bool inuse ;
12- uint32_t xfrd ;
13- volatile bool waiter ;
14- usb_osal_sem_t waitsem ;
15- struct usbh_hubport * hport ;
16- struct usbh_urb * urb ;
17- };
18-
19- struct dwc2_hcd {
20- struct dwc2_pipe pipe_pool [CONFIG_USBHOST_PIPE_NUM ];
21- } g_dwc2_hcd ;
22-
23- static int dwc2_chan_alloc (void )
24- {
25- int chidx ;
26-
27- for (chidx = 0 ; chidx < CONFIG_USBHOST_PIPE_NUM ; chidx ++ ) {
28- if (!g_dwc2_hcd .pipe_pool [chidx ].inuse ) {
29- g_dwc2_hcd .pipe_pool [chidx ].inuse = true;
30- return chidx ;
31- }
32- }
33-
34- return -1 ;
35- }
36-
37- static void dwc2_chan_free (struct dwc2_pipe * pipe )
38- {
39- pipe -> inuse = false;
40- }
41-
42- static int usbh_reset_port (const uint8_t port )
9+ int usb_hc_init (struct usbh_bus * bus )
4310{
4411 return 0 ;
4512}
4613
47- static uint8_t usbh_get_port_speed (const uint8_t port )
48- {
49- return USB_SPEED_UNKNOWN ;
50- }
51-
52- __WEAK void usb_hc_low_level_init (void )
14+ int usb_hc_deinit (struct usbh_bus * bus )
5315{
54- }
55-
56- int usb_hc_init (void )
57- {
58- int ret ;
59-
60- memset (& g_dwc2_hcd , 0 , sizeof (struct dwc2_hcd ));
61-
62- for (uint8_t chidx = 0 ; chidx < CONFIG_USBHOST_PIPE_NUM ; chidx ++ ) {
63- g_dwc2_hcd .pipe_pool [chidx ].waitsem = usb_osal_sem_create (0 );
64- }
65-
66- usb_hc_low_level_init ();
67-
6816 return 0 ;
6917}
7018
71- uint16_t usbh_get_frame_number (void )
19+ uint16_t usbh_get_frame_number (struct usbh_bus * bus )
7220{
7321 return 0 ;
7422}
7523
76- int usbh_roothub_control (struct usb_setup_packet * setup , uint8_t * buf )
24+ int usbh_roothub_control (struct usbh_bus * bus , struct usb_setup_packet * setup , uint8_t * buf )
7725{
7826 uint8_t nports ;
7927 uint8_t port ;
@@ -150,7 +98,6 @@ int usbh_roothub_control(struct usb_setup_packet *setup, uint8_t *buf)
15098 case HUB_PORT_FEATURE_POWER :
15199 break ;
152100 case HUB_PORT_FEATURE_RESET :
153- usbh_reset_port (port );
154101 break ;
155102
156103 default :
@@ -173,123 +120,12 @@ int usbh_roothub_control(struct usb_setup_packet *setup, uint8_t *buf)
173120
174121int usbh_submit_urb (struct usbh_urb * urb )
175122{
176- struct dwc2_chan * chan ;
177- size_t flags ;
178- int ret = 0 ;
179- int chidx ;
180-
181- if (!urb || !urb -> hport || !urb -> ep ) {
182- return - USB_ERR_INVAL ;
183- }
184-
185- if (!urb -> hport -> connected ) {
186- return - USB_ERR_NOTCONN ;
187- }
188-
189- if (urb -> errorcode == - USB_ERR_BUSY ) {
190- return - USB_ERR_BUSY ;
191- }
192-
193- flags = usb_osal_enter_critical_section ();
194-
195- chidx = dwc2_chan_alloc ();
196- if (chidx == -1 ) {
197- usb_osal_leave_critical_section (flags );
198- return - USB_ERR_NOMEM ;
199- }
200-
201- chan = & g_dwc2_hcd .chan_pool [chidx ];
202- chan -> chidx = chidx ;
203- chan -> urb = urb ;
204-
205- urb -> hcpriv = chan ;
206- urb -> errorcode = - USB_ERR_BUSY ;
207- urb -> actual_length = 0 ;
208-
209- usb_osal_leave_critical_section (flags );
210-
211- switch (USB_GET_ENDPOINT_TYPE (urb -> ep -> bmAttributes )) {
212- case USB_ENDPOINT_TYPE_CONTROL :
213- break ;
214- case USB_ENDPOINT_TYPE_BULK :
215- break ;
216- case USB_ENDPOINT_TYPE_INTERRUPT :
217- break ;
218- case USB_ENDPOINT_TYPE_ISOCHRONOUS :
219- break ;
220- default :
221- break ;
222- }
223-
224- if (urb -> timeout > 0 ) {
225- /* wait until timeout or sem give */
226- ret = usb_osal_sem_take (chan -> waitsem , urb -> timeout );
227- if (ret < 0 ) {
228- goto errout_timeout ;
229- }
230- urb -> timeout = 0 ;
231- ret = urb -> errorcode ;
232- /* we can free chan when waitsem is done */
233- dwc2_chan_free (chan );
234- }
235- return ret ;
236- errout_timeout :
237- urb -> timeout = 0 ;
238- usbh_kill_urb (urb );
239- return ret ;
123+ return - USB_ERR_NOTSUPP ;
240124}
241125
242126int usbh_kill_urb (struct usbh_urb * urb )
243127{
244- struct dwc2_chan * chan ;
245- size_t flags ;
246-
247- if (!urb || !urb -> hcpriv ) {
248- return - USB_ERR_INVAL ;
249- }
250-
251- flags = usb_osal_enter_critical_section ();
252-
253- chan = (struct dwc2_chan * )urb -> hcpriv ;
254-
255- chan -> urb = NULL ;
256- urb -> hcpriv = NULL ;
257-
258- if (urb -> timeout ) {
259- urb -> timeout = 0 ;
260- urb -> errorcode = - USB_ERR_SHUTDOWN ;
261- usb_osal_sem_give (chan -> waitsem );
262- } else {
263- dwc2_chan_free (chan );
264- }
265-
266- usb_osal_leave_critical_section (flags );
267-
268- return 0 ;
269- }
270-
271- static inline void dwc2_urb_waitup (struct usbh_urb * urb )
272- {
273- struct dwc2_chan * chan ;
274-
275- chan = (struct dwc2_chan * )urb -> hcpriv ;
276- chan -> urb = NULL ;
277- urb -> hcpriv = NULL ;
278-
279- if (urb -> timeout ) {
280- urb -> timeout = 0 ;
281- usb_osal_sem_give (chan -> waitsem );
282- } else {
283- dwc2_chan_free (chan );
284- }
285-
286- if (urb -> complete ) {
287- if (urb -> errorcode < 0 ) {
288- urb -> complete (urb -> arg , urb -> errorcode );
289- } else {
290- urb -> complete (urb -> arg , urb -> actual_length );
291- }
292- }
128+ return - USB_ERR_NOTSUPP ;
293129}
294130
295131void USBH_IRQHandler (uint8_t busid )
0 commit comments