usb_hc.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * @file usb_hc.h
  3. * @brief
  4. *
  5. * Copyright (c) 2022 sakumisu
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #ifndef _USB_HC_H
  24. #define _USB_HC_H
  25. #include <stdint.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. typedef void (*usbh_asynch_callback_t)(void *arg, int nbytes);
  30. typedef void *usbh_epinfo_t;
  31. /**
  32. * @brief USB Endpoint Configuration.
  33. *
  34. * Structure containing the USB endpoint configuration.
  35. */
  36. struct usbh_endpoint_cfg {
  37. struct usbh_hubport *hport;
  38. /** The number associated with the EP in the device
  39. * configuration structure
  40. * IN EP = 0x80 | \<endpoint number\>
  41. * OUT EP = 0x00 | \<endpoint number\>
  42. */
  43. uint8_t ep_addr;
  44. /** Endpoint Transfer Type.
  45. * May be Bulk, Interrupt, Control or Isochronous
  46. */
  47. uint8_t ep_type;
  48. uint8_t ep_interval;
  49. /** Endpoint max packet size */
  50. uint16_t ep_mps;
  51. };
  52. /**
  53. * @brief USB Host Core Layer API
  54. * @defgroup _usb_host_core_api USB Host Core API
  55. * @{
  56. */
  57. /**
  58. * @brief usb host software init, used for global reset.
  59. *
  60. * @return int
  61. */
  62. int usb_hc_sw_init(void);
  63. /**
  64. * @brief usb host controller hardware init.
  65. *
  66. * @return int
  67. */
  68. int usb_hc_hw_init(void);
  69. /**
  70. * @brief get port connect status
  71. *
  72. * @param port
  73. * @return true
  74. * @return false
  75. */
  76. bool usbh_get_port_connect_status(const uint8_t port);
  77. /**
  78. * @brief reset roothub port
  79. *
  80. * @param port port index
  81. * @return int
  82. */
  83. int usbh_reset_port(const uint8_t port);
  84. /**
  85. * @brief get roothub port speed
  86. *
  87. * @param port port index
  88. * @return return 1 means USB_SPEED_LOW, 2 means USB_SPEED_FULL and 3 means USB_SPEED_HIGH.
  89. */
  90. uint8_t usbh_get_port_speed(const uint8_t port);
  91. /**
  92. * @brief reconfig control endpoint.
  93. *
  94. * @param ep A memory location provided by the caller.
  95. * @param dev_addr device address.
  96. * @param ep_mps control endpoint max packet size.
  97. * @param speed port speed
  98. * @return On success will return 0, and others indicate fail.
  99. */
  100. int usbh_ep0_reconfigure(usbh_epinfo_t ep, uint8_t dev_addr, uint8_t ep_mps, uint8_t speed);
  101. /**
  102. * @brief Allocate and config endpoint
  103. *
  104. * @param ep A memory location provided by the caller in which to save the allocated endpoint info.
  105. * @param ep_cfg Describes the endpoint info to be allocated.
  106. * @return On success will return 0, and others indicate fail.
  107. */
  108. int usbh_ep_alloc(usbh_epinfo_t *ep, const struct usbh_endpoint_cfg *ep_cfg);
  109. /**
  110. * @brief Free a memory in which saves endpoint info.
  111. *
  112. * @param ep A memory location provided by the caller in which to free the allocated endpoint info.
  113. * @return On success will return 0, and others indicate fail.
  114. */
  115. int usbh_ep_free(usbh_epinfo_t ep);
  116. /**
  117. * @brief Perform a control transfer.
  118. * This is a blocking method; this method will not return until the transfer has completed.
  119. *
  120. * @param ep The control endpoint to send/receive the control request.
  121. * @param setup Setup packet to be sent.
  122. * @param buffer buffer used for sending the request and for returning any responses. This buffer must be large enough to hold the length value
  123. * in the request description.
  124. * @return On success will return 0, and others indicate fail.
  125. */
  126. int usbh_control_transfer(usbh_epinfo_t ep, struct usb_setup_packet *setup, uint8_t *buffer);
  127. /**
  128. * @brief Process a request to handle a transfer descriptor. This method will
  129. * enqueue the transfer request and wait for it to complete. Only one transfer may be queued;
  130. * This is a blocking method; this method will not return until the transfer has completed.
  131. *
  132. * @param ep The IN or OUT endpoint descriptor for the device endpoint on which to perform the transfer.
  133. * @param buffer A buffer containing the data to be sent (OUT endpoint) or received (IN endpoint).
  134. * @param buflen The length of the data to be sent or received.
  135. * @param timeout Timeout for transfer, unit is ms.
  136. * @return On success, a non-negative value is returned that indicates the number
  137. * of bytes successfully transferred. On a failure, a negated errno value
  138. * is returned that indicates the nature of the failure:
  139. *
  140. * -EAGAIN - If devices NAKs the transfer (or NYET or other error where
  141. * it may be appropriate to restart the entire transaction).
  142. * -EPERM - If the endpoint stalls
  143. * -EIO - On a TX or data toggle error
  144. * -EPIPE - Overrun errors
  145. * -ETIMEDOUT - Sem wait timeout
  146. *
  147. */
  148. int usbh_ep_bulk_transfer(usbh_epinfo_t ep, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
  149. /**
  150. * @brief Process a request to handle a transfer descriptor. This method will
  151. * enqueue the transfer request and wait for it to complete. Only one transfer may be queued;
  152. * This is a blocking method; this method will not return until the transfer has completed.
  153. *
  154. * @param ep The IN or OUT endpoint descriptor for the device endpoint on which to perform the transfer.
  155. * @param buffer A buffer containing the data to be sent (OUT endpoint) or received (IN endpoint).
  156. * @param buflen The length of the data to be sent or received.
  157. * @param timeout Timeout for transfer, unit is ms.
  158. * @return On success, a non-negative value is returned that indicates the number
  159. * of bytes successfully transferred. On a failure, a negated errno value
  160. * is returned that indicates the nature of the failure:
  161. *
  162. * -EAGAIN - If devices NAKs the transfer (or NYET or other error where
  163. * it may be appropriate to restart the entire transaction).
  164. * -EPERM - If the endpoint stalls
  165. * -EIO - On a TX or data toggle error
  166. * -EPIPE - Overrun errors
  167. * -ETIMEDOUT - Sem wait timeout
  168. *
  169. */
  170. int usbh_ep_intr_transfer(usbh_epinfo_t ep, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
  171. /**
  172. * @brief Process a request to handle a transfer asynchronously. This method
  173. * will enqueue the transfer request and return immediately. Only one transfer may be queued on a given endpoint
  174. * When the transfer completes, the callback will be invoked with the provided argument.
  175. *
  176. * This method is useful for receiving interrupt transfers which may come infrequently.
  177. *
  178. * @param ep The IN or OUT endpoint descriptor for the device endpoint on which to perform the transfer.
  179. * @param buffer A buffer containing the data to be sent (OUT endpoint) or received (IN endpoint).
  180. * @param buflen The length of the data to be sent or received.
  181. * @param callback This function will be called when the transfer completes.
  182. * @param arg The arbitrary parameter that will be passed to the callback function when the transfer completes.
  183. *
  184. * @return On success will return 0, and others indicate fail.
  185. */
  186. int usbh_ep_bulk_async_transfer(usbh_epinfo_t ep, uint8_t *buffer, uint32_t buflen, usbh_asynch_callback_t callback, void *arg);
  187. /**
  188. * @brief Process a request to handle a transfer asynchronously. This method
  189. * will enqueue the transfer request and return immediately. Only one transfer may be queued on a given endpoint
  190. * When the transfer completes, the callback will be invoked with the provided argument.
  191. *
  192. * This method is useful for receiving interrupt transfers which may come infrequently.
  193. *
  194. * @param ep The IN or OUT endpoint descriptor for the device endpoint on which to perform the transfer.
  195. * @param buffer A buffer containing the data to be sent (OUT endpoint) or received (IN endpoint).
  196. * @param buflen The length of the data to be sent or received.
  197. * @param callback This function will be called when the transfer completes.
  198. * @param arg The arbitrary parameter that will be passed to the callback function when the transfer completes.
  199. *
  200. * @return On success will return 0, and others indicate fail.
  201. */
  202. int usbh_ep_intr_async_transfer(usbh_epinfo_t ep, uint8_t *buffer, uint32_t buflen, usbh_asynch_callback_t callback, void *arg);
  203. /**
  204. * @brief Cancel any pending syncrhonous or asynchronous transfer on an endpoint.
  205. *
  206. * @param ep The IN or OUT endpoint descriptor for the device endpoint on which to cancel.
  207. * @return On success will return 0, and others indicate fail.
  208. */
  209. int usb_ep_cancel(usbh_epinfo_t ep);
  210. #ifdef __cplusplus
  211. }
  212. #endif
  213. #endif