usbh.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. * @note This function can block
  96. * @return esp_err_t
  97. */
  98. esp_err_t usbh_process(void);
  99. // ------------------------------------------------ Device Functions ---------------------------------------------------
  100. // --------------------- Device Pool -----------------------
  101. /**
  102. * @brief Fill list with address of currently connected devices
  103. *
  104. * - This function fills the provided list with the address of current connected devices
  105. * - Device address can then be used in usbh_dev_open()
  106. * - If there are more devices than the list_len, this function will only fill
  107. * up to list_len number of devices.
  108. *
  109. * @param[in] list_len Length of empty list
  110. * @param[inout] dev_addr_list Empty list to be filled
  111. * @param[out] num_dev_ret Number of devices filled into list
  112. * @return esp_err_t
  113. */
  114. esp_err_t usbh_dev_addr_list_fill(int list_len, uint8_t *dev_addr_list, int *num_dev_ret);
  115. /**
  116. * @brief Open a device by address
  117. *
  118. * A device must be opened before it can be used
  119. *
  120. * @param[in] dev_addr Device address
  121. * @param[out] dev_hdl Device handle
  122. * @return esp_err_t
  123. */
  124. esp_err_t usbh_dev_open(uint8_t dev_addr, usb_device_handle_t *dev_hdl);
  125. /**
  126. * @brief CLose a device
  127. *
  128. * Device can be opened by calling usbh_dev_open()
  129. *
  130. * @param[in] dev_hdl Device handle
  131. * @return esp_err_t
  132. */
  133. esp_err_t usbh_dev_close(usb_device_handle_t dev_hdl);
  134. /**
  135. * @brief Mark that all devices should be freed at the next possible opportunity
  136. *
  137. * A device marked as free will not be freed until the last client using the device has called usbh_dev_close()
  138. *
  139. * @return
  140. * - ESP_OK: There were no devices to free to begin with. Current state is all free
  141. * - ESP_ERR_NOT_FINISHED: One or more devices still need to be freed (but have been marked "to be freed")
  142. */
  143. esp_err_t usbh_dev_mark_all_free(void);
  144. // ------------------- Single Device ----------------------
  145. /**
  146. * @brief Get a device's address
  147. *
  148. * @note Can be called without opening the device
  149. *
  150. * @param[in] dev_hdl Device handle
  151. * @param[out] dev_addr Device's address
  152. * @return esp_err_t
  153. */
  154. esp_err_t usbh_dev_get_addr(usb_device_handle_t dev_hdl, uint8_t *dev_addr);
  155. /**
  156. * @brief Get a device's information
  157. *
  158. * @note This function can block
  159. * @param[in] dev_hdl Device handle
  160. * @param[out] dev_info Device information
  161. * @return esp_err_t
  162. */
  163. esp_err_t usbh_dev_get_info(usb_device_handle_t dev_hdl, usb_device_info_t *dev_info);
  164. /**
  165. * @brief Get a device's device descriptor
  166. *
  167. * - The device descriptor is cached when the device is created by the Hub driver
  168. *
  169. * @param[in] dev_hdl Device handle
  170. * @param[out] dev_desc_ret Device descriptor
  171. * @return esp_err_t
  172. */
  173. esp_err_t usbh_dev_get_desc(usb_device_handle_t dev_hdl, const usb_device_desc_t **dev_desc_ret);
  174. /**
  175. * @brief Get a device's active configuration descriptor
  176. *
  177. * Simply returns a reference to the internally cached configuration descriptor
  178. *
  179. * @note This function can block
  180. * @param[in] dev_hdl Device handle
  181. * @param config_desc_ret
  182. * @return esp_err_t
  183. */
  184. esp_err_t usbh_dev_get_config_desc(usb_device_handle_t dev_hdl, const usb_config_desc_t **config_desc_ret);
  185. /**
  186. * @brief Submit a control transfer (URB) to a device
  187. *
  188. * @param[in] dev_hdl Device handle
  189. * @param[in] urb URB
  190. * @return esp_err_t
  191. */
  192. esp_err_t usbh_dev_submit_ctrl_urb(usb_device_handle_t dev_hdl, urb_t *urb);
  193. // ----------------------------------------------- Endpoint Functions -------------------------------------------------
  194. /**
  195. * @brief Allocate an endpoint on a device
  196. *
  197. * Clients that have opened a device must call this function to allocate all endpoints in an interface that is claimed.
  198. * The pipe handle of the endpoint is returned so that clients can use and control the pipe directly.
  199. *
  200. * @note This function can block
  201. * @note Default pipes are owned by the USBH. For control transfers, use usbh_dev_submit_ctrl_urb() instead
  202. * @note Device must be opened by the client first
  203. *
  204. * @param[in] dev_hdl Device handle
  205. * @param[in] ep_config
  206. * @param[out] pipe_hdl_ret Pipe handle
  207. * @return esp_err_t
  208. */
  209. 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);
  210. /**
  211. * @brief Free and endpoint on a device
  212. *
  213. * Free an endpoint previously opened by usbh_ep_alloc()
  214. *
  215. * @note This function can block
  216. * @param[in] dev_hdl Device handle
  217. * @param[in] bEndpointAddress Endpoint's address
  218. * @return esp_err_t
  219. */
  220. esp_err_t usbh_ep_free(usb_device_handle_t dev_hdl, uint8_t bEndpointAddress);
  221. /**
  222. * @brief Get the context of an endpoint
  223. *
  224. * Get the context variable assigned to and endpoint on allocation.
  225. *
  226. * @note This function can block
  227. * @param[in] dev_hdl Device handle
  228. * @param[in] bEndpointAddress Endpoint's address
  229. * @param[out] context_ret Context variable
  230. * @return esp_err_t
  231. */
  232. esp_err_t usbh_ep_get_context(usb_device_handle_t dev_hdl, uint8_t bEndpointAddress, void **context_ret);
  233. // -------------------------------------------------- Hub Functions ----------------------------------------------------
  234. // ------------------- Device Related ----------------------
  235. /**
  236. * @brief Indicates to USBH that the Hub driver is installed
  237. *
  238. * - The Hub driver must call this function in its installation to indicate the the USBH that it has been installed.
  239. * - This should only be called after the USBH has already be installed
  240. *
  241. * @note Hub Driver only
  242. * @param[in] hub_callback Hub callback
  243. * @param[in] callback_arg Callback argument
  244. * @return esp_err_t
  245. */
  246. esp_err_t usbh_hub_is_installed(usbh_hub_cb_t hub_callback, void *callback_arg);
  247. /**
  248. * @brief Indicates to USBH the start of enumeration for a device
  249. *
  250. * - The Hub driver calls this function before it starts enumerating a new device.
  251. * - The USBH will allocate a new device that will be initialized by the Hub driver using the remaining hub enumeration
  252. * functions.
  253. * - The new device's default pipe handle is returned to all the Hub driver to be used during enumeration.
  254. *
  255. * @note Hub Driver only
  256. * @param[in] port_hdl Handle of the port that the device is connected to
  257. * @param[in] dev_speed Device's speed
  258. * @param[out] new_dev_hdl Device's handle
  259. * @param[out] default_pipe_hdl Device's default pipe handle
  260. * @return esp_err_t
  261. */
  262. 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);
  263. /**
  264. * @brief Indicate that a device is gone
  265. *
  266. * This Hub driver must call this function to indicate that a device is gone. A device is gone when:
  267. * - It suddenly disconnects
  268. * - Its upstream port or device has an error or is also gone.
  269. * Marking a device as gone will:
  270. * - Trigger a USBH_EVENT_DEV_GONE
  271. * - Prevent further transfers to the device
  272. * - Trigger the device's cleanup if it is already closed
  273. * - When the last client closes the device via usbh_dev_close(), the device's resources will be cleaned up
  274. *
  275. * @note Hub Driver only
  276. * @param[in] dev_hdl Device handle
  277. * @return esp_err_t
  278. */
  279. esp_err_t usbh_hub_mark_dev_gone(usb_device_handle_t dev_hdl);
  280. /**
  281. * @brief Indicate that a device's port has been disabled
  282. *
  283. * - The Hub driver must call this function once it has disabled the port of a particular device
  284. * - The Hub driver disables a device's port when requested by the USBH via the USBH_HUB_EVENT_DISABLE_PORT
  285. * - This function will trigger the device's cleanup.
  286. *
  287. * @note Hub Driver only
  288. * @param[in] dev_hdl Device handle
  289. * @return esp_err_t
  290. */
  291. esp_err_t usbh_hub_dev_port_disabled(usb_device_handle_t dev_hdl);
  292. // ----------------- Enumeration Related -------------------
  293. /**
  294. * @brief Fill the enumerating device's descriptor
  295. *
  296. * @note Hub Driver only
  297. * @note Must call in sequence
  298. * @param[in] dev_hdl Device handle
  299. * @param device_desc
  300. * @return esp_err_t
  301. */
  302. esp_err_t usbh_hub_enum_fill_dev_desc(usb_device_handle_t dev_hdl, const usb_device_desc_t *device_desc);
  303. /**
  304. * @brief Assign the enumerating device's address
  305. *
  306. * @note Hub Driver only
  307. * @note Must call in sequence
  308. * @param[in] dev_hdl Device handle
  309. * @param dev_addr
  310. * @return esp_err_t
  311. */
  312. esp_err_t usbh_hub_enum_fill_dev_addr(usb_device_handle_t dev_hdl, uint8_t dev_addr);
  313. /**
  314. * @brief Fill the enumerating device's active configuration descriptor
  315. *
  316. * @note Hub Driver only
  317. * @note Must call in sequence
  318. * @note This function can block
  319. * @param[in] dev_hdl Device handle
  320. * @param config_desc_full
  321. * @return esp_err_t
  322. */
  323. esp_err_t usbh_hub_enum_fill_config_desc(usb_device_handle_t dev_hdl, const usb_config_desc_t *config_desc_full);
  324. /**
  325. * @brief Assign the enumerating device's active configuration number
  326. *
  327. * @note Hub Driver only
  328. * @note Must call in sequence
  329. * @param[in] dev_hdl Device handle
  330. * @param bConfigurationValue
  331. * @return esp_err_t
  332. */
  333. esp_err_t usbh_hub_enum_fill_config_num(usb_device_handle_t dev_hdl, uint8_t bConfigurationValue);
  334. /**
  335. * @brief Indicate the device enumeration is completed
  336. *
  337. * This will all the device to be opened by clients, and also trigger a USBH_EVENT_DEV_NEW event.
  338. *
  339. * @note Hub Driver only
  340. * @note Must call in sequence
  341. * @note This function can block
  342. * @param[in] dev_hdl Device handle
  343. * @return esp_err_t
  344. */
  345. esp_err_t usbh_hub_enum_done(usb_device_handle_t dev_hdl);
  346. /**
  347. * @brief Indicate that device enumeration has failed
  348. *
  349. * This will cause the enumerating device's resources to be cleaned up
  350. * The Hub Driver must guarantee that the enumerating device's default pipe is already halted, flushed, and dequeued.
  351. *
  352. * @note Hub Driver only
  353. * @note Must call in sequence
  354. * @note This function can block
  355. * @param[in] dev_hdl Device handle
  356. * @return esp_err_t
  357. */
  358. esp_err_t usbh_hub_enum_failed(usb_device_handle_t dev_hdl);
  359. #ifdef __cplusplus
  360. }
  361. #endif