usb_dc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * SPDX-FileCopyrightText: 2016 Intel Corporation
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief USB device controller APIs
  9. *
  10. * This file contains the USB device controller APIs. All device controller
  11. * drivers should implement the APIs described in this file.
  12. */
  13. #pragma once
  14. #include <stdint.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /**
  19. * USB endpoint direction and number.
  20. */
  21. #define USB_EP_DIR_MASK 0x80
  22. #define USB_EP_DIR_IN 0x80
  23. #define USB_EP_DIR_OUT 0x00
  24. /**
  25. * USB Driver Status Codes
  26. */
  27. enum usb_dc_status_code {
  28. USB_DC_ERROR, /* USB error reported by the controller */
  29. USB_DC_RESET, /* USB reset */
  30. /* USB connection established, hardware enumeration is completed */
  31. USB_DC_CONNECTED,
  32. USB_DC_CONFIGURED, /* USB configuration done */
  33. USB_DC_DISCONNECTED, /* USB connection lost */
  34. USB_DC_SUSPEND, /* USB connection suspended by the HOST */
  35. USB_DC_RESUME, /* USB connection resumed by the HOST */
  36. USB_DC_INTERFACE, /* USB interface selected */
  37. USB_DC_SET_HALT, /* Set Feature ENDPOINT_HALT received */
  38. USB_DC_CLEAR_HALT, /* Clear Feature ENDPOINT_HALT received */
  39. USB_DC_UNKNOWN /* Initial USB connection status */
  40. };
  41. /**
  42. * USB Endpoint Callback Status Codes
  43. */
  44. enum usb_dc_ep_cb_status_code {
  45. USB_DC_EP_SETUP, /* SETUP received */
  46. /* Out transaction on this EP, data is available for read */
  47. USB_DC_EP_DATA_OUT,
  48. USB_DC_EP_DATA_IN, /* In transaction done on this EP */
  49. };
  50. /**
  51. * USB Endpoint type
  52. */
  53. enum usb_dc_ep_type {
  54. USB_DC_EP_CONTROL = 0, /* Control type endpoint */
  55. USB_DC_EP_ISOCHRONOUS, /* Isochronous type endpoint */
  56. USB_DC_EP_BULK, /* Bulk type endpoint */
  57. USB_DC_EP_INTERRUPT /* Interrupt type endpoint */
  58. };
  59. /**
  60. * USB Endpoint Configuration.
  61. */
  62. struct usb_dc_ep_cfg_data {
  63. /** The number associated with the EP in the device
  64. * configuration structure
  65. * IN EP = 0x80 | \<endpoint number\>
  66. * OUT EP = 0x00 | \<endpoint number\>
  67. */
  68. uint8_t ep_addr;
  69. uint16_t ep_mps; /** Endpoint max packet size */
  70. enum usb_dc_ep_type ep_type; /** Endpoint type */
  71. };
  72. /**
  73. * Callback function signature for the USB Endpoint status
  74. */
  75. typedef void (*usb_dc_ep_callback)(uint8_t ep,
  76. enum usb_dc_ep_cb_status_code cb_status);
  77. /**
  78. * Callback function signature for the device
  79. */
  80. typedef void (*usb_dc_status_callback)(enum usb_dc_status_code cb_status,
  81. uint8_t *param);
  82. /**
  83. * @brief attach USB for device connection
  84. *
  85. * Function to attach USB for device connection. Upon success, the USB PLL
  86. * is enabled, and the USB device is now capable of transmitting and receiving
  87. * on the USB bus and of generating interrupts.
  88. *
  89. * @return 0 on success, negative errno code on fail.
  90. */
  91. int usb_dc_attach(void);
  92. /**
  93. * @brief detach the USB device
  94. *
  95. * Function to detach the USB device. Upon success, the USB hardware PLL
  96. * is powered down and USB communication is disabled.
  97. *
  98. * @return 0 on success, negative errno code on fail.
  99. */
  100. int usb_dc_detach(void);
  101. /**
  102. * @brief reset the USB device
  103. *
  104. * This function returns the USB device and firmware back to it's initial state.
  105. * N.B. the USB PLL is handled by the usb_detach function
  106. *
  107. * @return 0 on success, negative errno code on fail.
  108. */
  109. int usb_dc_reset(void);
  110. /**
  111. * @brief set USB device address
  112. *
  113. * @param[in] addr device address
  114. *
  115. * @return 0 on success, negative errno code on fail.
  116. */
  117. int usb_dc_set_address(const uint8_t addr);
  118. /**
  119. * @brief set USB device controller status callback
  120. *
  121. * Function to set USB device controller status callback. The registered
  122. * callback is used to report changes in the status of the device controller.
  123. *
  124. * @param[in] cb callback function
  125. *
  126. * @return 0 on success, negative errno code on fail.
  127. */
  128. int usb_dc_set_status_callback(const usb_dc_status_callback cb);
  129. /**
  130. * @brief check endpoint capabilities
  131. *
  132. * Function to check capabilities of an endpoint. usb_dc_ep_cfg_data structure
  133. * provides the endpoint configuration parameters: endpoint address,
  134. * endpoint maximum packet size and endpoint type.
  135. * The driver should check endpoint capabilities and return 0 if the
  136. * endpoint configuration is possible.
  137. *
  138. * @param[in] cfg Endpoint config
  139. *
  140. * @return 0 on success, negative errno code on fail.
  141. */
  142. int usb_dc_ep_check_cap(const struct usb_dc_ep_cfg_data *const cfg);
  143. /**
  144. * @brief configure endpoint
  145. *
  146. * Function to configure an endpoint. usb_dc_ep_cfg_data structure provides
  147. * the endpoint configuration parameters: endpoint address, endpoint maximum
  148. * packet size and endpoint type.
  149. *
  150. * @param[in] cfg Endpoint config
  151. *
  152. * @return 0 on success, negative errno code on fail.
  153. */
  154. int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg);
  155. /**
  156. * @brief set stall condition for the selected endpoint
  157. *
  158. * @param[in] ep Endpoint address corresponding to the one
  159. * listed in the device configuration table
  160. *
  161. * @return 0 on success, negative errno code on fail.
  162. */
  163. int usb_dc_ep_set_stall(const uint8_t ep);
  164. /**
  165. * @brief clear stall condition for the selected endpoint
  166. *
  167. * @param[in] ep Endpoint address corresponding to the one
  168. * listed in the device configuration table
  169. *
  170. * @return 0 on success, negative errno code on fail.
  171. */
  172. int usb_dc_ep_clear_stall(const uint8_t ep);
  173. /**
  174. * @brief check if selected endpoint is stalled
  175. *
  176. * @param[in] ep Endpoint address corresponding to the one
  177. * listed in the device configuration table
  178. * @param[out] stalled Endpoint stall status
  179. *
  180. * @return 0 on success, negative errno code on fail.
  181. */
  182. int usb_dc_ep_is_stalled(const uint8_t ep, uint8_t *const stalled);
  183. /**
  184. * @brief halt the selected endpoint
  185. *
  186. * @param[in] ep Endpoint address corresponding to the one
  187. * listed in the device configuration table
  188. *
  189. * @return 0 on success, negative errno code on fail.
  190. */
  191. int usb_dc_ep_halt(const uint8_t ep);
  192. /**
  193. * @brief enable the selected endpoint
  194. *
  195. * Function to enable the selected endpoint. Upon success interrupts are
  196. * enabled for the corresponding endpoint and the endpoint is ready for
  197. * transmitting/receiving data.
  198. *
  199. * @param[in] ep Endpoint address corresponding to the one
  200. * listed in the device configuration table
  201. *
  202. * @return 0 on success, negative errno code on fail.
  203. */
  204. int usb_dc_ep_enable(const uint8_t ep);
  205. /**
  206. * @brief disable the selected endpoint
  207. *
  208. * Function to disable the selected endpoint. Upon success interrupts are
  209. * disabled for the corresponding endpoint and the endpoint is no longer able
  210. * for transmitting/receiving data.
  211. *
  212. * @param[in] ep Endpoint address corresponding to the one
  213. * listed in the device configuration table
  214. *
  215. * @return 0 on success, negative errno code on fail.
  216. */
  217. int usb_dc_ep_disable(const uint8_t ep);
  218. /**
  219. * @brief flush the selected endpoint
  220. *
  221. * @param[in] ep Endpoint address corresponding to the one
  222. * listed in the device configuration table
  223. *
  224. * @return 0 on success, negative errno code on fail.
  225. */
  226. int usb_dc_ep_flush(const uint8_t ep);
  227. /**
  228. * @brief write data to the specified endpoint
  229. *
  230. * This function is called to write data to the specified endpoint. The supplied
  231. * usb_ep_callback function will be called when data is transmitted out.
  232. *
  233. * @param[in] ep Endpoint address corresponding to the one
  234. * listed in the device configuration table
  235. * @param[in] data pointer to data to write
  236. * @param[in] data_len length of data requested to write. This may
  237. * be zero for a zero length status packet.
  238. * @param[out] ret_bytes bytes scheduled for transmission. This value
  239. * may be NULL if the application expects all
  240. * bytes to be written
  241. *
  242. * @return 0 on success, negative errno code on fail.
  243. */
  244. int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
  245. const uint32_t data_len, uint32_t *const ret_bytes);
  246. /**
  247. * @brief Indicate if the write to an IN endpoint (using usb_dc_ep_write) would block
  248. * to wait until the endpoint has enoug space
  249. *
  250. * @param[in] ep Endpoint address corresponding to the one
  251. * listed in the device configuration table
  252. *
  253. * @return 0 when writable, 0 when not, negative errno code on fail.
  254. */
  255. int usb_dc_ep_write_would_block(const uint8_t ep);
  256. /**
  257. * @brief read data from the specified endpoint
  258. *
  259. * This function is called by the Endpoint handler function, after an OUT
  260. * interrupt has been received for that EP. The application must only call this
  261. * function through the supplied usb_ep_callback function. This function clears
  262. * the ENDPOINT NAK, if all data in the endpoint FIFO has been read,
  263. * so as to accept more data from host.
  264. *
  265. * @param[in] ep Endpoint address corresponding to the one
  266. * listed in the device configuration table
  267. * @param[in] data pointer to data buffer to write to
  268. * @param[in] max_data_len max length of data to read
  269. * @param[out] read_bytes Number of bytes read. If data is NULL and
  270. * max_data_len is 0 the number of bytes
  271. * available for read should be returned.
  272. *
  273. * @return 0 on success, negative errno code on fail.
  274. */
  275. int usb_dc_ep_read(const uint8_t ep, uint8_t *const data,
  276. const uint32_t max_data_len, uint32_t *const read_bytes);
  277. /**
  278. * @brief set callback function for the specified endpoint
  279. *
  280. * Function to set callback function for notification of data received and
  281. * available to application or transmit done on the selected endpoint,
  282. * NULL if callback not required by application code.
  283. *
  284. * @param[in] ep Endpoint address corresponding to the one
  285. * listed in the device configuration table
  286. * @param[in] cb callback function
  287. *
  288. * @return 0 on success, negative errno code on fail.
  289. */
  290. int usb_dc_ep_set_callback(const uint8_t ep, const usb_dc_ep_callback cb);
  291. /**
  292. * @brief read data from the specified endpoint
  293. *
  294. * This is similar to usb_dc_ep_read, the difference being that, it doesn't
  295. * clear the endpoint NAKs so that the consumer is not bogged down by further
  296. * upcalls till he is done with the processing of the data. The caller should
  297. * reactivate ep by invoking usb_dc_ep_read_continue() do so.
  298. *
  299. * @param[in] ep Endpoint address corresponding to the one
  300. * listed in the device configuration table
  301. * @param[in] data pointer to data buffer to write to
  302. * @param[in] max_data_len max length of data to read
  303. * @param[out] read_bytes Number of bytes read. If data is NULL and
  304. * max_data_len is 0 the number of bytes
  305. * available for read should be returned.
  306. *
  307. * @return 0 on success, negative errno code on fail.
  308. */
  309. int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
  310. uint32_t *read_bytes);
  311. /**
  312. * @brief Continue reading data from the endpoint
  313. *
  314. * Clear the endpoint NAK and enable the endpoint to accept more data
  315. * from the host. Usually called after usb_dc_ep_read_wait() when the consumer
  316. * is fine to accept more data. Thus these calls together acts as flow control
  317. * mechanism.
  318. *
  319. * @param[in] ep Endpoint address corresponding to the one
  320. * listed in the device configuration table
  321. *
  322. * @return 0 on success, negative errno code on fail.
  323. */
  324. int usb_dc_ep_read_continue(uint8_t ep);
  325. /**
  326. * @brief Get endpoint max packet size
  327. *
  328. * @param[in] ep Endpoint address corresponding to the one
  329. * listed in the device configuration table
  330. *
  331. * @return enpoint max packet size (mps)
  332. */
  333. int usb_dc_ep_mps(uint8_t ep);
  334. /**
  335. * @brief Poll for interrupts that need to be handled
  336. *
  337. * When the USB interrupt is not hooked up to an actual CPU interrupt, you
  338. * can call this periodically to handle the USB events that need handling.
  339. */
  340. void usb_dc_check_poll_for_interrupts(void);
  341. /*
  342. * @brief Prepare for USB persist
  343. *
  344. * This takes the USB peripheral offline in such a way that it seems 'just busy' to the
  345. * host. This way, the chip can reboot (e.g. into bootloader mode) and pick up the USB
  346. * configuration again, without the conenction to the host being interrupted.
  347. *
  348. * @note Actual persistence is depending on USBDC_PERSIST_ENA being set in flags, as this
  349. * is also used to e.g. reboot into DFU mode.
  350. *
  351. * @note Please reboot soon after calling this.
  352. */
  353. int usb_dc_prepare_persist(void);
  354. /*
  355. * @brief USB interrupt handler
  356. *
  357. * This can be hooked up by the OS to the USB peripheral interrupt.
  358. */
  359. void usb_dw_isr_handler(void);
  360. /**
  361. * @brief Provide IDF with an interface to clear the static variable usb_dw_ctrl
  362. *
  363. *
  364. */
  365. void usb_dw_ctrl_deinit(void);
  366. #ifdef __cplusplus
  367. }
  368. #endif