usb_device.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c) 2021-2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef USB_DEVICE_H
  7. #define USB_DEVICE_H
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <lib/utils_def.h>
  11. #define USBD_MAX_NUM_INTERFACES 1U
  12. #define USBD_MAX_NUM_CONFIGURATION 1U
  13. #define USB_LEN_DEV_QUALIFIER_DESC 0x0AU
  14. #define USB_LEN_DEV_DESC 0x12U
  15. #define USB_LEN_CFG_DESC 0x09U
  16. #define USB_LEN_IF_DESC 0x09U
  17. #define USB_LEN_EP_DESC 0x07U
  18. #define USB_LEN_OTG_DESC 0x03U
  19. #define USB_LEN_LANGID_STR_DESC 0x04U
  20. #define USB_LEN_OTHER_SPEED_DESC_SIZ 0x09U
  21. #define USBD_IDX_LANGID_STR 0x00U
  22. #define USBD_IDX_MFC_STR 0x01U
  23. #define USBD_IDX_PRODUCT_STR 0x02U
  24. #define USBD_IDX_SERIAL_STR 0x03U
  25. #define USBD_IDX_CONFIG_STR 0x04U
  26. #define USBD_IDX_INTERFACE_STR 0x05U
  27. #define USBD_IDX_USER0_STR 0x06U
  28. #define USB_REQ_TYPE_STANDARD 0x00U
  29. #define USB_REQ_TYPE_CLASS 0x20U
  30. #define USB_REQ_TYPE_VENDOR 0x40U
  31. #define USB_REQ_TYPE_MASK 0x60U
  32. #define USB_REQ_RECIPIENT_DEVICE 0x00U
  33. #define USB_REQ_RECIPIENT_INTERFACE 0x01U
  34. #define USB_REQ_RECIPIENT_ENDPOINT 0x02U
  35. #define USB_REQ_RECIPIENT_MASK 0x1FU
  36. #define USB_REQ_DIRECTION 0x80U
  37. #define USB_REQ_GET_STATUS 0x00U
  38. #define USB_REQ_CLEAR_FEATURE 0x01U
  39. #define USB_REQ_SET_FEATURE 0x03U
  40. #define USB_REQ_SET_ADDRESS 0x05U
  41. #define USB_REQ_GET_DESCRIPTOR 0x06U
  42. #define USB_REQ_SET_DESCRIPTOR 0x07U
  43. #define USB_REQ_GET_CONFIGURATION 0x08U
  44. #define USB_REQ_SET_CONFIGURATION 0x09U
  45. #define USB_REQ_GET_INTERFACE 0x0AU
  46. #define USB_REQ_SET_INTERFACE 0x0BU
  47. #define USB_REQ_SYNCH_FRAME 0x0CU
  48. #define USB_DESC_TYPE_DEVICE 0x01U
  49. #define USB_DESC_TYPE_CONFIGURATION 0x02U
  50. #define USB_DESC_TYPE_STRING 0x03U
  51. #define USB_DESC_TYPE_INTERFACE 0x04U
  52. #define USB_DESC_TYPE_ENDPOINT 0x05U
  53. #define USB_DESC_TYPE_DEVICE_QUALIFIER 0x06U
  54. #define USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION 0x07U
  55. #define USB_DESC_TYPE_BOS 0x0FU
  56. #define USB_CONFIG_REMOTE_WAKEUP 2U
  57. #define USB_CONFIG_SELF_POWERED 1U
  58. #define USB_MAX_EP0_SIZE 64U
  59. /* Device Status */
  60. #define USBD_STATE_DEFAULT 1U
  61. #define USBD_STATE_ADDRESSED 2U
  62. #define USBD_STATE_CONFIGURED 3U
  63. #define USBD_STATE_SUSPENDED 4U
  64. /* EP0 State */
  65. #define USBD_EP0_IDLE 0U
  66. #define USBD_EP0_SETUP 1U
  67. #define USBD_EP0_DATA_IN 2U
  68. #define USBD_EP0_DATA_OUT 3U
  69. #define USBD_EP0_STATUS_IN 4U
  70. #define USBD_EP0_STATUS_OUT 5U
  71. #define USBD_EP0_STALL 6U
  72. #define USBD_EP_TYPE_CTRL 0U
  73. #define USBD_EP_TYPE_ISOC 1U
  74. #define USBD_EP_TYPE_BULK 2U
  75. #define USBD_EP_TYPE_INTR 3U
  76. #define USBD_OUT_EPNUM_MASK GENMASK(15, 0)
  77. #define USBD_OUT_COUNT_MASK GENMASK(31, 16)
  78. #define USBD_OUT_COUNT_SHIFT 16U
  79. /* Number of EP supported, allow to reduce footprint: default max = 15 */
  80. #ifndef CONFIG_USBD_EP_NB
  81. #define USBD_EP_NB 15U
  82. #else
  83. #define USBD_EP_NB CONFIG_USBD_EP_NB
  84. #endif
  85. #define LOBYTE(x) ((uint8_t)((x) & 0x00FF))
  86. #define HIBYTE(x) ((uint8_t)(((x) & 0xFF00) >> 8))
  87. struct usb_setup_req {
  88. uint8_t bm_request;
  89. uint8_t b_request;
  90. uint16_t value;
  91. uint16_t index;
  92. uint16_t length;
  93. };
  94. struct usb_handle;
  95. struct usb_class {
  96. uint8_t (*init)(struct usb_handle *pdev, uint8_t cfgidx);
  97. uint8_t (*de_init)(struct usb_handle *pdev, uint8_t cfgidx);
  98. /* Control Endpoints */
  99. uint8_t (*setup)(struct usb_handle *pdev, struct usb_setup_req *req);
  100. uint8_t (*ep0_tx_sent)(struct usb_handle *pdev);
  101. uint8_t (*ep0_rx_ready)(struct usb_handle *pdev);
  102. /* Class Specific Endpoints */
  103. uint8_t (*data_in)(struct usb_handle *pdev, uint8_t epnum);
  104. uint8_t (*data_out)(struct usb_handle *pdev, uint8_t epnum);
  105. uint8_t (*sof)(struct usb_handle *pdev);
  106. uint8_t (*iso_in_incomplete)(struct usb_handle *pdev, uint8_t epnum);
  107. uint8_t (*iso_out_incomplete)(struct usb_handle *pdev, uint8_t epnum);
  108. };
  109. /* Following USB Device status */
  110. enum usb_status {
  111. USBD_OK = 0U,
  112. USBD_BUSY,
  113. USBD_FAIL,
  114. USBD_TIMEOUT
  115. };
  116. /* Action to do after IT handling */
  117. enum usb_action {
  118. USB_NOTHING = 0U,
  119. USB_DATA_OUT,
  120. USB_DATA_IN,
  121. USB_SETUP,
  122. USB_ENUM_DONE,
  123. USB_READ_DATA_PACKET,
  124. USB_READ_SETUP_PACKET,
  125. USB_RESET,
  126. USB_RESUME,
  127. USB_SUSPEND,
  128. USB_LPM,
  129. USB_SOF,
  130. USB_DISCONNECT,
  131. USB_WRITE_EMPTY
  132. };
  133. /* USB Device descriptors structure */
  134. struct usb_desc {
  135. uint8_t *(*get_device_desc)(uint16_t *length);
  136. uint8_t *(*get_lang_id_desc)(uint16_t *length);
  137. uint8_t *(*get_manufacturer_desc)(uint16_t *length);
  138. uint8_t *(*get_product_desc)(uint16_t *length);
  139. uint8_t *(*get_serial_desc)(uint16_t *length);
  140. uint8_t *(*get_configuration_desc)(uint16_t *length);
  141. uint8_t *(*get_interface_desc)(uint16_t *length);
  142. uint8_t *(*get_usr_desc)(uint8_t index, uint16_t *length);
  143. uint8_t *(*get_config_desc)(uint16_t *length);
  144. uint8_t *(*get_device_qualifier_desc)(uint16_t *length);
  145. /* optional: high speed capable device operating at its other speed */
  146. uint8_t *(*get_other_speed_config_desc)(uint16_t *length);
  147. };
  148. /* USB Device handle structure */
  149. struct usb_endpoint {
  150. uint32_t status;
  151. uint32_t total_length;
  152. uint32_t rem_length;
  153. uint32_t maxpacket;
  154. };
  155. /*
  156. * EndPoint descriptor
  157. * num : Endpoint number, between 0 and 15 (limited by USBD_EP_NB)
  158. * is_in: Endpoint direction
  159. * type : Endpoint type
  160. * maxpacket: Endpoint Max packet size: between 0 and 64KB
  161. * xfer_buff: Pointer to transfer buffer
  162. * xfer_len: Current transfer lengt
  163. * hxfer_count: Partial transfer length in case of multi packet transfer
  164. */
  165. struct usbd_ep {
  166. uint8_t num;
  167. bool is_in;
  168. uint8_t type;
  169. uint32_t maxpacket;
  170. uint8_t *xfer_buff;
  171. uint32_t xfer_len;
  172. uint32_t xfer_count;
  173. };
  174. enum pcd_lpm_state {
  175. LPM_L0 = 0x00U, /* on */
  176. LPM_L1 = 0x01U, /* LPM L1 sleep */
  177. LPM_L2 = 0x02U, /* suspend */
  178. LPM_L3 = 0x03U, /* off */
  179. };
  180. /* USB Device descriptors structure */
  181. struct usb_driver {
  182. enum usb_status (*ep0_out_start)(void *handle);
  183. enum usb_status (*ep_start_xfer)(void *handle, struct usbd_ep *ep);
  184. enum usb_status (*ep0_start_xfer)(void *handle, struct usbd_ep *ep);
  185. enum usb_status (*write_packet)(void *handle, uint8_t *src,
  186. uint8_t ch_ep_num, uint16_t len);
  187. void *(*read_packet)(void *handle, uint8_t *dest, uint16_t len);
  188. enum usb_status (*ep_set_stall)(void *handle, struct usbd_ep *ep);
  189. enum usb_status (*start_device)(void *handle);
  190. enum usb_status (*stop_device)(void *handle);
  191. enum usb_status (*set_address)(void *handle, uint8_t address);
  192. enum usb_status (*write_empty_tx_fifo)(void *handle,
  193. uint32_t epnum, uint32_t xfer_len,
  194. uint32_t *xfer_count,
  195. uint32_t maxpacket,
  196. uint8_t **xfer_buff);
  197. enum usb_action (*it_handler)(void *handle, uint32_t *param);
  198. };
  199. /* USB Peripheral Controller Drivers */
  200. struct pcd_handle {
  201. void *instance; /* Register base address */
  202. struct usbd_ep in_ep[USBD_EP_NB]; /* IN endpoint parameters */
  203. struct usbd_ep out_ep[USBD_EP_NB]; /* OUT endpoint parameters */
  204. uint32_t setup[12]; /* Setup packet buffer */
  205. enum pcd_lpm_state lpm_state; /* LPM State */
  206. };
  207. /* USB Device handle structure */
  208. struct usb_handle {
  209. uint8_t id;
  210. uint32_t dev_config;
  211. uint32_t dev_config_status;
  212. struct usb_endpoint ep_in[USBD_EP_NB];
  213. struct usb_endpoint ep_out[USBD_EP_NB];
  214. uint32_t ep0_state;
  215. uint32_t ep0_data_len;
  216. uint8_t dev_state;
  217. uint8_t dev_old_state;
  218. uint8_t dev_address;
  219. uint32_t dev_remote_wakeup;
  220. struct usb_setup_req request;
  221. const struct usb_desc *desc;
  222. struct usb_class *class;
  223. void *class_data;
  224. void *user_data;
  225. struct pcd_handle *data;
  226. const struct usb_driver *driver;
  227. };
  228. enum usb_status usb_core_handle_it(struct usb_handle *pdev);
  229. enum usb_status usb_core_receive(struct usb_handle *pdev, uint8_t ep_addr,
  230. uint8_t *p_buf, uint32_t len);
  231. enum usb_status usb_core_transmit(struct usb_handle *pdev, uint8_t ep_addr,
  232. uint8_t *p_buf, uint32_t len);
  233. enum usb_status usb_core_receive_ep0(struct usb_handle *pdev, uint8_t *p_buf,
  234. uint32_t len);
  235. enum usb_status usb_core_transmit_ep0(struct usb_handle *pdev, uint8_t *p_buf,
  236. uint32_t len);
  237. void usb_core_ctl_error(struct usb_handle *pdev);
  238. enum usb_status usb_core_start(struct usb_handle *pdev);
  239. enum usb_status usb_core_stop(struct usb_handle *pdev);
  240. enum usb_status register_usb_driver(struct usb_handle *pdev,
  241. struct pcd_handle *pcd_handle,
  242. const struct usb_driver *driver,
  243. void *driver_handle);
  244. enum usb_status register_platform(struct usb_handle *pdev,
  245. const struct usb_desc *plat_call_back);
  246. #endif /* USB_DEVICE_H */