1 /*
2  * Copyright (c) 2022, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef USB_DC_H
7 #define USB_DC_H
8 
9 #include <stdint.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /**
16  * @brief init device controller registers.
17  * @return On success will return 0, and others indicate fail.
18  */
19 int usb_dc_init(uint8_t busid);
20 
21 /**
22  * @brief deinit device controller registers.
23  * @return On success will return 0, and others indicate fail.
24  */
25 int usb_dc_deinit(uint8_t busid);
26 
27 /**
28  * @brief Set USB device address
29  *
30  * @param[in] addr Device address
31  *
32  * @return On success will return 0, and others indicate fail.
33  */
34 int usbd_set_address(uint8_t busid, const uint8_t addr);
35 
36 /**
37  * @brief Set remote wakeup feature
38  *
39  * @return On success will return 0, and others indicate fail.
40  */
41 int usbd_set_remote_wakeup(uint8_t busid);
42 
43 /**
44  * @brief Get USB device speed
45  *
46  * @param[in] busid bus index
47  *
48  * @return port speed, USB_SPEED_LOW or USB_SPEED_FULL or USB_SPEED_HIGH
49  */
50 uint8_t usbd_get_port_speed(uint8_t busid);
51 
52 /**
53  * @brief configure and enable endpoint.
54  *
55  * @param [in]  ep_cfg Endpoint config.
56  *
57  * @return On success will return 0, and others indicate fail.
58  */
59 int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep);
60 
61 /**
62  * @brief Disable the selected endpoint
63  *
64  * @param[in] ep Endpoint address
65  *
66  * @return On success will return 0, and others indicate fail.
67  */
68 int usbd_ep_close(uint8_t busid, const uint8_t ep);
69 
70 /**
71  * @brief Set stall condition for the selected endpoint
72  *
73  * @param[in] ep Endpoint address
74  *
75  *
76  * @return On success will return 0, and others indicate fail.
77  */
78 int usbd_ep_set_stall(uint8_t busid, const uint8_t ep);
79 
80 /**
81  * @brief Clear stall condition for the selected endpoint
82  *
83  * @param[in] ep Endpoint address corresponding to the one
84  *               listed in the device configuration table
85  *
86  * @return On success will return 0, and others indicate fail.
87  */
88 int usbd_ep_clear_stall(uint8_t busid, const uint8_t ep);
89 
90 /**
91  * @brief Check if the selected endpoint is stalled
92  *
93  * @param[in]  ep       Endpoint address
94  *
95  * @param[out] stalled  Endpoint stall status
96  *
97  * @return On success will return 0, and others indicate fail.
98  */
99 int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled);
100 
101 /**
102  * @brief Setup in ep transfer setting and start transfer.
103  *
104  * This function is asynchronous.
105  * This function is similar to uart with tx dma.
106  *
107  * This function is called to write data to the specified endpoint. The
108  * supplied usbd_endpoint_callback function will be called when data is transmitted
109  * out.
110  *
111  * @param[in]  ep        Endpoint address corresponding to the one
112  *                       listed in the device configuration table
113  * @param[in]  data      Pointer to data to write
114  * @param[in]  data_len  Length of the data requested to write. This may
115  *                       be zero for a zero length status packet.
116  * @return 0 on success, negative errno code on fail.
117  */
118 int usbd_ep_start_write(uint8_t busid, const uint8_t ep, const uint8_t *data, uint32_t data_len);
119 
120 /**
121  * @brief Setup out ep transfer setting and start transfer.
122  *
123  * This function is asynchronous.
124  * This function is similar to uart with rx dma.
125  *
126  * This function is called to read data to the specified endpoint. The
127  * supplied usbd_endpoint_callback function will be called when data is received
128  * in.
129  *
130  * @param[in]  ep        Endpoint address corresponding to the one
131  *                       listed in the device configuration table
132  * @param[in]  data      Pointer to data to read
133  * @param[in]  data_len  Max length of the data requested to read.
134  *
135  * @return 0 on success, negative errno code on fail.
136  */
137 int usbd_ep_start_read(uint8_t busid, const uint8_t ep, uint8_t *data, uint32_t data_len);
138 
139 /* usb dcd irq callback, called by user */
140 
141 /**
142  * @brief Usb sof irq callback.
143  */
144 void usbd_event_sof_handler(uint8_t busid);
145 
146 /**
147  * @brief Usb connect irq callback.
148  */
149 void usbd_event_connect_handler(uint8_t busid);
150 
151 /**
152  * @brief Usb disconnect irq callback.
153  */
154 void usbd_event_disconnect_handler(uint8_t busid);
155 
156 /**
157  * @brief Usb resume irq callback.
158  */
159 void usbd_event_resume_handler(uint8_t busid);
160 
161 /**
162  * @brief Usb suspend irq callback.
163  */
164 void usbd_event_suspend_handler(uint8_t busid);
165 
166 /**
167  * @brief Usb reset irq callback.
168  */
169 void usbd_event_reset_handler(uint8_t busid);
170 
171 /**
172  * @brief Usb setup packet recv irq callback.
173  * @param[in]  psetup  setup packet.
174  */
175 void usbd_event_ep0_setup_complete_handler(uint8_t busid, uint8_t *psetup);
176 
177 /**
178  * @brief In ep transfer complete irq callback.
179  * @param[in]  ep        Endpoint address corresponding to the one
180  *                       listed in the device configuration table
181  * @param[in]  nbytes    How many nbytes have transferred.
182  */
183 void usbd_event_ep_in_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes);
184 
185 /**
186  * @brief Out ep transfer complete irq callback.
187  * @param[in]  ep        Endpoint address corresponding to the one
188  *                       listed in the device configuration table
189  * @param[in]  nbytes    How many nbytes have transferred.
190  */
191 void usbd_event_ep_out_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes);
192 
193 #ifdef CONFIG_USBDEV_TEST_MODE
194 /**
195  * @brief Usb execute test mode
196  * @param[in]  busid     device busid
197  * @param[in]  test_mode usb test mode
198  */
199 void usbd_execute_test_mode(uint8_t busid, uint8_t test_mode);
200 #endif
201 
202 /* called by user */
203 void USBD_IRQHandler(uint8_t busid);
204 
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #endif /* USB_DC_H */
210