usb_device.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * SPDX-FileCopyrightText: 2006 Bertrik Sikken (bertrik@sikken.nl)
  3. * SPDX-FileContributor: 2016 Intel Corporation
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. *
  7. * LPCUSB, an USB device driver for LPC microcontrollers
  8. */
  9. /**
  10. * @file
  11. * @brief USB device core layer APIs and structures
  12. *
  13. * This file contains the USB device core layer APIs and structures.
  14. */
  15. #pragma once
  16. #include <stddef.h>
  17. #include <sys/cdefs.h>
  18. #include "usb_dc.h"
  19. #include "esp_assert.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /*************************************************************************
  24. * USB configuration
  25. **************************************************************************/
  26. #define MAX_PACKET_SIZE0 64 /**< maximum packet size for EP 0 */
  27. //Note: for FS this should be 8, 16, 32, 64 bytes. HS can go up to 512.
  28. /*************************************************************************
  29. * USB application interface
  30. **************************************************************************/
  31. /** setup packet definitions */
  32. struct usb_setup_packet {
  33. uint8_t bmRequestType; /**< characteristics of the specific request */
  34. uint8_t bRequest; /**< specific request */
  35. uint16_t wValue; /**< request specific parameter */
  36. uint16_t wIndex; /**< request specific parameter */
  37. uint16_t wLength; /**< length of data transferred in data phase */
  38. } __packed;
  39. ESP_STATIC_ASSERT(sizeof(struct usb_setup_packet) == 8, "USB setup packet struct size error");
  40. /**
  41. * Callback function signature for the device
  42. */
  43. typedef void (*usb_status_callback)(enum usb_dc_status_code status_code,
  44. uint8_t *param);
  45. /**
  46. * Callback function signature for the USB Endpoint status
  47. */
  48. typedef void (*usb_ep_callback)(uint8_t ep,
  49. enum usb_dc_ep_cb_status_code cb_status);
  50. /**
  51. * Function which handles Class specific requests corresponding to an
  52. * interface number specified in the device descriptor table
  53. */
  54. typedef int (*usb_request_handler) (struct usb_setup_packet *detup,
  55. int32_t *transfer_len, uint8_t **payload_data);
  56. /**
  57. * Function for interface runtime configuration
  58. */
  59. typedef void (*usb_interface_config)(uint8_t bInterfaceNumber);
  60. /*
  61. * USB Endpoint Configuration
  62. */
  63. struct usb_ep_cfg_data {
  64. /**
  65. * Callback function for notification of data received and
  66. * available to application or transmit done, NULL if callback
  67. * not required by application code
  68. */
  69. usb_ep_callback ep_cb;
  70. /**
  71. * The number associated with the EP in the device configuration
  72. * structure
  73. * IN EP = 0x80 | \<endpoint number\>
  74. * OUT EP = 0x00 | \<endpoint number\>
  75. */
  76. uint8_t ep_addr;
  77. };
  78. /**
  79. * USB Interface Configuration
  80. */
  81. struct usb_interface_cfg_data {
  82. /** Handler for USB Class specific Control (EP 0) communications */
  83. usb_request_handler class_handler;
  84. /** Handler for USB Vendor specific commands */
  85. usb_request_handler vendor_handler;
  86. /**
  87. * The custom request handler gets a first chance at handling
  88. * the request before it is handed over to the 'chapter 9' request
  89. * handler
  90. */
  91. usb_request_handler custom_handler;
  92. /**
  93. * This data area, allocated by the application, is used to store
  94. * Class specific command data and must be large enough to store the
  95. * largest payload associated with the largest supported Class'
  96. * command set. This data area may be used for USB IN or OUT
  97. * communications
  98. */
  99. uint8_t *payload_data;
  100. /**
  101. * This data area, allocated by the application, is used to store
  102. * Vendor specific payload
  103. */
  104. uint8_t *vendor_data;
  105. };
  106. /*
  107. * @brief USB device configuration
  108. *
  109. * The Application instantiates this with given parameters added
  110. * using the "usb_set_config" function. Once this function is called
  111. * changes to this structure will result in undefined behaviour. This structure
  112. * may only be updated after calls to usb_deconfig
  113. */
  114. struct usb_cfg_data {
  115. /**
  116. * USB device description, see
  117. * http://www.beyondlogic.org/usbnutshell/usb5.shtml#DeviceDescriptors
  118. */
  119. const uint8_t *usb_device_description;
  120. /** Pointer to interface descriptor */
  121. const void *interface_descriptor;
  122. /** Function for interface runtime configuration */
  123. usb_interface_config interface_config;
  124. /** Callback to be notified on USB connection status change */
  125. usb_status_callback cb_usb_status;
  126. /** USB interface (Class) handler and storage space */
  127. struct usb_interface_cfg_data interface;
  128. /** Number of individual endpoints in the device configuration */
  129. uint8_t num_endpoints;
  130. /**
  131. * Pointer to an array of endpoint structs of length equal to the
  132. * number of EP associated with the device description,
  133. * not including control endpoints
  134. */
  135. struct usb_ep_cfg_data *endpoint;
  136. };
  137. /*
  138. * @brief configure USB controller
  139. *
  140. * Function to configure USB controller.
  141. * Configuration parameters must be valid or an error is returned
  142. *
  143. * @param[in] config Pointer to configuration structure
  144. *
  145. * @return 0 on success, negative errno code on fail
  146. */
  147. int usb_set_config(struct usb_cfg_data *config);
  148. /*
  149. * @brief return the USB device to it's initial state
  150. *
  151. * @return 0 on success, negative errno code on fail
  152. */
  153. int usb_deconfig(void);
  154. /*
  155. * @brief enable USB for host/device connection
  156. *
  157. * Function to enable USB for host/device connection.
  158. * Upon success, the USB module is no longer clock gated in hardware,
  159. * it is now capable of transmitting and receiving on the USB bus and
  160. * of generating interrupts.
  161. *
  162. * @return 0 on success, negative errno code on fail.
  163. */
  164. int usb_enable(struct usb_cfg_data *config);
  165. /*
  166. * @brief disable the USB device.
  167. *
  168. * Function to disable the USB device.
  169. * Upon success, the specified USB interface is clock gated in hardware,
  170. * it is no longer capable of generating interrupts.
  171. *
  172. * @return 0 on success, negative errno code on fail
  173. */
  174. int usb_disable(void);
  175. /*
  176. * @brief Check if a write to an in ep would block until there is enough space
  177. * in the fifo
  178. *
  179. * @param[in] ep Endpoint address corresponding to the one listed in the
  180. * device configuration table
  181. *
  182. * @return 0 if free to write, 1 if a write would block, negative errno code on fail
  183. */
  184. int usb_write_would_block(uint8_t ep);
  185. /*
  186. * @brief write data to the specified endpoint
  187. *
  188. * Function to write data to the specified endpoint. The supplied
  189. * usb_ep_callback will be called when transmission is done.
  190. *
  191. * @param[in] ep Endpoint address corresponding to the one listed in the
  192. * device configuration table
  193. * @param[in] data Pointer to data to write
  194. * @param[in] data_len Length of data requested to write. This may be zero for
  195. * a zero length status packet.
  196. * @param[out] bytes_ret Bytes written to the EP FIFO. This value may be NULL if
  197. * the application expects all bytes to be written
  198. *
  199. * @return 0 on success, negative errno code on fail
  200. */
  201. int usb_write(uint8_t ep, const uint8_t *data, uint32_t data_len,
  202. uint32_t *bytes_ret);
  203. /*
  204. * @brief read data from the specified endpoint
  205. *
  206. * This function is called by the Endpoint handler function, after an
  207. * OUT interrupt has been received for that EP. The application must
  208. * only call this function through the supplied usb_ep_callback function.
  209. *
  210. * @param[in] ep Endpoint address corresponding to the one listed in
  211. * the device configuration table
  212. * @param[in] data Pointer to data buffer to write to
  213. * @param[in] max_data_len Max length of data to read
  214. * @param[out] ret_bytes Number of bytes read. If data is NULL and
  215. * max_data_len is 0 the number of bytes available
  216. * for read is returned.
  217. *
  218. * @return 0 on success, negative errno code on fail
  219. */
  220. int usb_read(uint8_t ep, uint8_t *data, uint32_t max_data_len,
  221. uint32_t *ret_bytes);
  222. /*
  223. * @brief set STALL condition on the specified endpoint
  224. *
  225. * This function is called by USB device class handler code to set stall
  226. * conditionin on endpoint.
  227. *
  228. * @param[in] ep Endpoint address corresponding to the one listed in
  229. * the device configuration table
  230. *
  231. * @return 0 on success, negative errno code on fail
  232. */
  233. int usb_ep_set_stall(uint8_t ep);
  234. /*
  235. * @brief clears STALL condition on the specified endpoint
  236. *
  237. * This function is called by USB device class handler code to clear stall
  238. * conditionin on endpoint.
  239. *
  240. * @param[in] ep Endpoint address corresponding to the one listed in
  241. * the device configuration table
  242. *
  243. * @return 0 on success, negative errno code on fail
  244. */
  245. int usb_ep_clear_stall(uint8_t ep);
  246. /**
  247. * @brief read data from the specified endpoint
  248. *
  249. * This is similar to usb_ep_read, the difference being that, it doesn't
  250. * clear the endpoint NAKs so that the consumer is not bogged down by further
  251. * upcalls till he is done with the processing of the data. The caller should
  252. * reactivate ep by invoking usb_ep_read_continue() do so.
  253. *
  254. * @param[in] ep Endpoint address corresponding to the one
  255. * listed in the device configuration table
  256. * @param[in] data pointer to data buffer to write to
  257. * @param[in] max_data_len max length of data to read
  258. * @param[out] read_bytes Number of bytes read. If data is NULL and
  259. * max_data_len is 0 the number of bytes
  260. * available for read should be returned.
  261. *
  262. * @return 0 on success, negative errno code on fail.
  263. */
  264. int usb_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
  265. uint32_t *read_bytes);
  266. /**
  267. * @brief Continue reading data from the endpoint
  268. *
  269. * Clear the endpoint NAK and enable the endpoint to accept more data
  270. * from the host. Usually called after usb_ep_read_wait() when the consumer
  271. * is fine to accept more data. Thus these calls together acts as flow control
  272. * mechanism.
  273. *
  274. * @param[in] ep Endpoint address corresponding to the one
  275. * listed in the device configuration table
  276. *
  277. * @return 0 on success, negative errno code on fail.
  278. */
  279. int usb_ep_read_continue(uint8_t ep);
  280. /**
  281. * Callback function signature for transfer completion.
  282. */
  283. typedef void (*usb_transfer_callback)(uint8_t ep, int tsize, void *priv);
  284. /* USB transfer flags */
  285. #define USB_TRANS_READ BIT(0) /** Read transfer flag */
  286. #define USB_TRANS_WRITE BIT(1) /** Write transfer flag */
  287. #define USB_TRANS_NO_ZLP BIT(2) /** No zero-length packet flag */
  288. /**
  289. * @brief Transfer management endpoint callback
  290. *
  291. * If a USB class driver wants to use high-level transfer functions, driver
  292. * needs to register this callback as usb endpoint callback.
  293. */
  294. void usb_transfer_ep_callback(uint8_t ep, enum usb_dc_ep_cb_status_code);
  295. /**
  296. * @brief Start a transfer
  297. *
  298. * Start a usb transfer to/from the data buffer. This function is asynchronous
  299. * and can be executed in IRQ context. The provided callback will be called
  300. * on transfer completion (or error) in thread context.
  301. *
  302. * @param[in] ep Endpoint address corresponding to the one
  303. * listed in the device configuration table
  304. * @param[in] data Pointer to data buffer to write-to/read-from
  305. * @param[in] dlen Size of data buffer
  306. * @param[in] flags Transfer flags (USB_TRANS_READ, USB_TRANS_WRITE...)
  307. * @param[in] cb Function called on transfer completion/failure
  308. * @param[in] priv Data passed back to the transfer completion callback
  309. *
  310. * @return 0 on success, negative errno code on fail.
  311. */
  312. int usb_transfer(uint8_t ep, uint8_t *data, size_t dlen, unsigned int flags,
  313. usb_transfer_callback cb, void *priv);
  314. /**
  315. * @brief Start a transfer and block-wait for completion
  316. *
  317. * Synchronous version of usb_transfer, wait for transfer completion before
  318. * returning.
  319. *
  320. * @param[in] ep Endpoint address corresponding to the one
  321. * listed in the device configuration table
  322. * @param[in] data Pointer to data buffer to write-to/read-from
  323. * @param[in] dlen Size of data buffer
  324. * @param[in] flags Transfer flags
  325. *
  326. * @return number of bytes transferred on success, negative errno code on fail.
  327. */
  328. int usb_transfer_sync(uint8_t ep, uint8_t *data, size_t dlen, unsigned int flags);
  329. /**
  330. * @brief Cancel any ongoing transfer on the specified endpoint
  331. *
  332. * @param[in] ep Endpoint address corresponding to the one
  333. * listed in the device configuration table
  334. *
  335. * @return 0 on success, negative errno code on fail.
  336. */
  337. void usb_cancel_transfer(uint8_t ep);
  338. /**
  339. * @brief Provide IDF with an interface to clear the static variable usb_dev
  340. *
  341. *
  342. */
  343. void usb_dev_deinit(void);
  344. void usb_dev_resume(int configuration);
  345. int usb_dev_get_configuration(void);
  346. #ifdef __cplusplus
  347. }
  348. #endif