1 /*
2  * Copyright (c) 2022, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef USB_HC_H
7 #define USB_HC_H
8 
9 #include <stdint.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 typedef void (*usbh_complete_callback_t)(void *arg, int nbytes);
16 
17 struct usbh_bus;
18 
19 /**
20  * @brief USB Iso Configuration.
21  *
22  * Structure containing the USB Iso configuration.
23  */
24 struct usbh_iso_frame_packet {
25     uint8_t *transfer_buffer;
26     uint32_t transfer_buffer_length;
27     uint32_t actual_length;
28     int errorcode;
29 };
30 
31 /**
32  * @brief USB Urb Configuration.
33  *
34  * Structure containing the USB Urb configuration.
35  */
36 struct usbh_urb {
37     usb_slist_t list;
38     void *hcpriv;
39     struct usbh_hubport *hport;
40     struct usb_endpoint_descriptor *ep;
41     uint8_t data_toggle;
42     uint32_t interval;
43     struct usb_setup_packet *setup;
44     uint8_t *transfer_buffer;
45     uint32_t transfer_buffer_length;
46     int transfer_flags;
47     uint32_t actual_length;
48     uint32_t timeout;
49     int errorcode;
50     uint32_t num_of_iso_packets;
51     uint32_t start_frame;
52     usbh_complete_callback_t complete;
53     void *arg;
54 #if defined(__ICCARM__) || defined(__ICCRISCV__) || defined(__ICCRX__)
55     struct usbh_iso_frame_packet *iso_packet;
56 #else
57     struct usbh_iso_frame_packet iso_packet[0];
58 #endif
59 };
60 
61 /**
62  * @brief usb host controller hardware init.
63  *
64  * @return On success will return 0, and others indicate fail.
65  */
66 int usb_hc_init(struct usbh_bus *bus);
67 
68 /**
69  * @brief usb host controller hardware deinit.
70  *
71  * @return On success will return 0, and others indicate fail.
72  */
73 int usb_hc_deinit(struct usbh_bus *bus);
74 
75 /**
76  * @brief Get frame number.
77  *
78  * @return frame number.
79  */
80 uint16_t usbh_get_frame_number(struct usbh_bus *bus);
81 /**
82  * @brief control roothub.
83  *
84  * @param setup setup request buffer.
85  * @param buf buf for reading response or write data.
86  * @return On success will return 0, and others indicate fail.
87  */
88 int usbh_roothub_control(struct usbh_bus *bus, struct usb_setup_packet *setup, uint8_t *buf);
89 
90 /**
91  * @brief Submit a usb transfer request to an endpoint.
92  *
93  * If timeout is not zero, this function will be in poll transfer mode,
94  * otherwise will be in async transfer mode.
95  *
96  * @param urb Usb request block.
97  * @return  On success will return 0, and others indicate fail.
98  */
99 int usbh_submit_urb(struct usbh_urb *urb);
100 
101 /**
102  * @brief Cancel a transfer request.
103  *
104  * This function will call When calls usbh_submit_urb and return -USB_ERR_TIMEOUT or -USB_ERR_SHUTDOWN.
105  *
106  * @param urb Usb request block.
107  * @return  On success will return 0, and others indicate fail.
108  */
109 int usbh_kill_urb(struct usbh_urb *urb);
110 
111 /* called by user */
112 void USBH_IRQHandler(uint8_t busid);
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 
118 #endif /* USB_HC_H */
119