usbh.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <sys/queue.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "hcd.h"
  12. #include "usb/usb_types_ch9.h"
  13. #include "usb/usb_types_stack.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. // ------------------------------------------------------ Types --------------------------------------------------------
  18. // ----------------------- Events --------------------------
  19. typedef enum {
  20. USBH_EVENT_DEV_NEW, /**< A new device has been enumerated and added to the device pool */
  21. USBH_EVENT_DEV_GONE, /**< A device is gone. Clients should close the device */
  22. USBH_EVENT_DEV_ALL_FREE, /**< All devices have been freed */
  23. } usbh_event_t;
  24. typedef enum {
  25. USBH_HUB_EVENT_CLEANUP_PORT, /**< Indicate to the Hub driver that it should clean up the port of a device (occurs after a gone device has been freed) */
  26. USBH_HUB_EVENT_DISABLE_PORT, /**< Indicate to the Hub driver that it should disable the port of a device (occurs after a device has been freed) */
  27. } usbh_hub_event_t;
  28. // ---------------------- Callbacks ------------------------
  29. /**
  30. * @brief Callback used to indicate completion of control transfers submitted usbh_dev_submit_ctrl_urb()
  31. * @note This callback is called from within usbh_process()
  32. */
  33. typedef void (*usbh_ctrl_xfer_cb_t)(usb_device_handle_t dev_hdl, urb_t *urb, void *arg);
  34. /**
  35. * @brief Callback used to indicate that the USBH has an event
  36. *
  37. * @note This callback is called from within usbh_process()
  38. * @note On a USBH_EVENT_DEV_ALL_FREE event, the dev_hdl argument is set to NULL
  39. */
  40. typedef void (*usbh_event_cb_t)(usb_device_handle_t dev_hdl, usbh_event_t usbh_event, void *arg);
  41. /**
  42. * @brief Callback used by the USBH to request actions from the Hub driver
  43. * @note This callback is called from within usbh_process()
  44. */
  45. typedef void (*usbh_hub_cb_t)(hcd_port_handle_t port_hdl, usbh_hub_event_t hub_event, void *arg);
  46. // ----------------------- Objects -------------------------
  47. /**
  48. * @brief Configuration for an endpoint being allocated using usbh_ep_alloc()
  49. */
  50. typedef struct {
  51. const usb_ep_desc_t *ep_desc; /**< Endpoint descriptor */
  52. hcd_pipe_callback_t pipe_cb; /**< Endpoint's pipe callback */
  53. void *pipe_cb_arg; /**< Pipe callback argument */
  54. void *context; /**< Pipe context */
  55. } usbh_ep_config_t;
  56. /**
  57. * @brief USBH configuration used in usbh_install()
  58. */
  59. typedef struct {
  60. usb_notif_cb_t notif_cb; /**< Notification callback */
  61. void *notif_cb_arg; /**< Notification callback argument */
  62. usbh_ctrl_xfer_cb_t ctrl_xfer_cb; /**< Control transfer callback */
  63. void *ctrl_xfer_cb_arg; /**< Control transfer callback argument */
  64. usbh_event_cb_t event_cb; /**< USBH event callback */
  65. void *event_cb_arg; /**< USBH event callback argument */
  66. hcd_config_t hcd_config; /**< HCD configuration */
  67. } usbh_config_t;
  68. // ------------------------------------------------- USBH Functions ----------------------------------------------------
  69. /**
  70. * @brief Installs the USBH driver
  71. *
  72. * - This function will internally install the HCD
  73. * - This must be called before calling any Hub driver functions
  74. *
  75. * @param usbh_config USBH driver configuration
  76. * @return esp_err_t
  77. */
  78. esp_err_t usbh_install(const usbh_config_t *usbh_config);
  79. /**
  80. * @brief Uninstall the USBH driver
  81. *
  82. * - This function will uninstall the HCD
  83. * - The Hub driver must be uninstalled before calling this function
  84. *
  85. * @return esp_err_t
  86. */
  87. esp_err_t usbh_uninstall(void);
  88. /**
  89. * @brief USBH processing function
  90. *
  91. * - USBH processing function that must be called repeatedly to process USBH events
  92. * - If blocking, the caller can block until a USB_NOTIF_SOURCE_USBH notification is received before running this
  93. * function
  94. *
  95. * @return esp_err_t
  96. */
  97. esp_err_t usbh_process(void);
  98. // ------------------------------------------------ Device Functions ---------------------------------------------------
  99. // --------------------- Device Pool -----------------------
  100. /**
  101. * @brief Open a device by address
  102. *
  103. * A device must be opened before it can be used
  104. *
  105. * @param[in] dev_addr Device address
  106. * @param[out] dev_hdl Device handle
  107. * @return esp_err_t
  108. */
  109. esp_err_t usbh_dev_open(uint8_t dev_addr, usb_device_handle_t *dev_hdl);
  110. /**
  111. * @brief CLose a device
  112. *
  113. * Device can be opened by calling usbh_dev_open()
  114. *
  115. * @param[in] dev_hdl Device handle
  116. * @return esp_err_t
  117. */
  118. esp_err_t usbh_dev_close(usb_device_handle_t dev_hdl);
  119. /**
  120. * @brief Mark that all devices should be freed at the next possible opportunity
  121. *
  122. * A device marked as free will not be freed until the last client using the device has called usbh_dev_close()
  123. *
  124. * @return esp_err_t
  125. */
  126. esp_err_t usbh_dev_mark_all_free(void);
  127. // ------------------- Single Device ----------------------
  128. /**
  129. * @brief Get a device's address
  130. *
  131. * @note Can be called without opening the device
  132. *
  133. * @param[in] dev_hdl Device handle
  134. * @param[out] dev_addr Device's address
  135. * @return esp_err_t
  136. */
  137. esp_err_t usbh_dev_get_addr(usb_device_handle_t dev_hdl, uint8_t *dev_addr);
  138. /**
  139. * @brief Get a device's information
  140. *
  141. * @param[in] dev_hdl Device handle
  142. * @param[out] dev_info Device information
  143. * @return esp_err_t
  144. */
  145. esp_err_t usbh_dev_get_info(usb_device_handle_t dev_hdl, usb_device_info_t *dev_info);
  146. /**
  147. * @brief Get a device's device descriptor
  148. *
  149. * - The device descriptor is cached when the device is created by the Hub driver
  150. *
  151. * @param[in] dev_hdl Device handle
  152. * @param[out] dev_desc_ret Device descriptor
  153. * @return esp_err_t
  154. */
  155. esp_err_t usbh_dev_get_desc(usb_device_handle_t dev_hdl, const usb_device_desc_t **dev_desc_ret);
  156. /**
  157. * @brief Get a device's active configuration descriptor
  158. *
  159. * Simply returns a reference to the internally cached configuration descriptor
  160. *
  161. * @param[in] dev_hdl Device handle
  162. * @param config_desc_ret
  163. * @return esp_err_t
  164. */
  165. esp_err_t usbh_dev_get_config_desc(usb_device_handle_t dev_hdl, const usb_config_desc_t **config_desc_ret);
  166. /**
  167. * @brief Submit a control transfer (URB) to a device
  168. *
  169. * @param[in] dev_hdl Device handle
  170. * @param[in] urb URB
  171. * @return esp_err_t
  172. */
  173. esp_err_t usbh_dev_submit_ctrl_urb(usb_device_handle_t dev_hdl, urb_t *urb);
  174. // ----------------------------------------------- Endpoint Functions -------------------------------------------------
  175. /**
  176. * @brief Allocate an endpoint on a device
  177. *
  178. * Clients that have opened a device must call this function to allocate all endpoints in an interface that is claimed.
  179. * The pipe handle of the endpoint is returned so that clients can use and control the pipe directly.
  180. *
  181. * @note Default pipes are owned by the USBH. For control transfers, use usbh_dev_submit_ctrl_urb() instead
  182. * @note Device must be opened by the client first
  183. *
  184. * @param[in] dev_hdl Device handle
  185. * @param[in] ep_config
  186. * @param[out] pipe_hdl_ret Pipe handle
  187. * @return esp_err_t
  188. */
  189. esp_err_t usbh_ep_alloc(usb_device_handle_t dev_hdl, usbh_ep_config_t *ep_config, hcd_pipe_handle_t *pipe_hdl_ret);
  190. /**
  191. * @brief Free and endpoint on a device
  192. *
  193. * Free an endpoint previously opened by usbh_ep_alloc()
  194. *
  195. * @param[in] dev_hdl Device handle
  196. * @param[in] bEndpointAddress Endpoint's address
  197. * @return esp_err_t
  198. */
  199. esp_err_t usbh_ep_free(usb_device_handle_t dev_hdl, uint8_t bEndpointAddress);
  200. /**
  201. * @brief Get the context of an endpoint
  202. *
  203. * Get the context variable assigned to and endpoint on allocation.
  204. *
  205. * @param[in] dev_hdl Device handle
  206. * @param[in] bEndpointAddress Endpoint's address
  207. * @param[out] context_ret Context variable
  208. * @return esp_err_t
  209. */
  210. esp_err_t usbh_ep_get_context(usb_device_handle_t dev_hdl, uint8_t bEndpointAddress, void **context_ret);
  211. // -------------------------------------------------- Hub Functions ----------------------------------------------------
  212. // ------------------- Device Related ----------------------
  213. /**
  214. * @brief Indicates to USBH that the Hub driver is installed
  215. *
  216. * - The Hub driver must call this function in its installation to indicate the the USBH that it has been installed.
  217. * - This should only be called after the USBH has already be installed
  218. *
  219. * @note Hub Driver only
  220. * @param[in] hub_callback Hub callback
  221. * @param[in] callback_arg Callback argument
  222. * @return esp_err_t
  223. */
  224. esp_err_t usbh_hub_is_installed(usbh_hub_cb_t hub_callback, void *callback_arg);
  225. /**
  226. * @brief Indicates to USBH the start of enumeration for a device
  227. *
  228. * - The Hub driver calls this function before it starts enumerating a new device.
  229. * - The USBH will allocate a new device that will be initialized by the Hub driver using the remaining hub enumeration
  230. * functions.
  231. * - The new device's default pipe handle is returned to all the Hub driver to be used during enumeration.
  232. *
  233. * @note Hub Driver only
  234. * @param[in] port_hdl Handle of the port that the device is connected to
  235. * @param[in] dev_speed Device's speed
  236. * @param[out] new_dev_hdl Device's handle
  237. * @param[out] default_pipe_hdl Device's default pipe handle
  238. * @return esp_err_t
  239. */
  240. esp_err_t usbh_hub_add_dev(hcd_port_handle_t port_hdl, usb_speed_t dev_speed, usb_device_handle_t *new_dev_hdl, hcd_pipe_handle_t *default_pipe_hdl);
  241. /**
  242. * @brief Indicate that a device is gone
  243. *
  244. * This Hub driver must call this function to indicate that a device is gone. A device is gone when:
  245. * - It suddenly disconnects
  246. * - Its upstream port or device has an error or is also gone.
  247. * Marking a device as gone will:
  248. * - Trigger a USBH_EVENT_DEV_GONE
  249. * - Prevent further transfers to the device
  250. * - Trigger the device's cleanup if it is already closed
  251. * - When the last client closes the device via usbh_dev_close(), the device's resources will be cleaned up
  252. *
  253. * @note Hub Driver only
  254. * @param[in] dev_hdl Device handle
  255. * @return esp_err_t
  256. */
  257. esp_err_t usbh_hub_mark_dev_gone(usb_device_handle_t dev_hdl);
  258. /**
  259. * @brief Indicate that a device's port has been disabled
  260. *
  261. * - The Hub driver must call this function once it has disabled the port of a particular device
  262. * - The Hub driver disables a device's port when requested by the USBH via the USBH_HUB_EVENT_DISABLE_PORT
  263. * - This function will trigger the device's cleanup.
  264. *
  265. * @note Hub Driver only
  266. * @param[in] dev_hdl Device handle
  267. * @return esp_err_t
  268. */
  269. esp_err_t usbh_hub_dev_port_disabled(usb_device_handle_t dev_hdl);
  270. // ----------------- Enumeration Related -------------------
  271. /**
  272. * @brief Fill the enumerating device's descriptor
  273. *
  274. * @note Hub Driver only
  275. * @note Must call in sequence
  276. * @param[in] dev_hdl Device handle
  277. * @param device_desc
  278. * @return esp_err_t
  279. */
  280. esp_err_t usbh_hub_enum_fill_dev_desc(usb_device_handle_t dev_hdl, const usb_device_desc_t *device_desc);
  281. /**
  282. * @brief Assign the enumerating device's address
  283. *
  284. * @note Hub Driver only
  285. * @note Must call in sequence
  286. * @param[in] dev_hdl Device handle
  287. * @param dev_addr
  288. * @return esp_err_t
  289. */
  290. esp_err_t usbh_hub_enum_fill_dev_addr(usb_device_handle_t dev_hdl, uint8_t dev_addr);
  291. /**
  292. * @brief Fill the enumerating device's active configuration descriptor
  293. *
  294. * @note Hub Driver only
  295. * @note Must call in sequence
  296. * @param[in] dev_hdl Device handle
  297. * @param config_desc_full
  298. * @return esp_err_t
  299. */
  300. esp_err_t usbh_hub_enum_fill_config_desc(usb_device_handle_t dev_hdl, const usb_config_desc_t *config_desc_full);
  301. /**
  302. * @brief Assign the enumerating device's active configuration number
  303. *
  304. * @note Hub Driver only
  305. * @note Must call in sequence
  306. * @param[in] dev_hdl Device handle
  307. * @param bConfigurationValue
  308. * @return esp_err_t
  309. */
  310. esp_err_t usbh_hub_enum_fill_config_num(usb_device_handle_t dev_hdl, uint8_t bConfigurationValue);
  311. /**
  312. * @brief Indicate the device enumeration is completed
  313. *
  314. * This will all the device to be opened by clients, and also trigger a USBH_EVENT_DEV_NEW event.
  315. *
  316. * @note Hub Driver only
  317. * @note Must call in sequence
  318. * @param[in] dev_hdl Device handle
  319. * @return esp_err_t
  320. */
  321. esp_err_t usbh_hub_enum_done(usb_device_handle_t dev_hdl);
  322. /**
  323. * @brief Indicate that device enumeration has failed
  324. *
  325. * This will cause the enumerating device's resources to be cleaned up
  326. * The Hub Driver must guarantee that the enumerating device's default pipe is already halted, flushed, and dequeued.
  327. *
  328. * @note Hub Driver only
  329. * @note Must call in sequence
  330. * @param[in] dev_hdl Device handle
  331. * @return esp_err_t
  332. */
  333. esp_err_t usbh_hub_enum_failed(usb_device_handle_t dev_hdl);
  334. #ifdef __cplusplus
  335. }
  336. #endif