usbh_lib.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**************************************************************************//**
  2. * @file usbh_lib.h
  3. * @version V1.10
  4. * @brief USB Host library exported header file.
  5. *
  6. * @note
  7. * SPDX-License-Identifier: Apache-2.0
  8. * Copyright (C) 2019-2020 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #ifndef _USBH_LIB_H_
  11. #define _USBH_LIB_H_
  12. #include "NuMicro.h"
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. /** @addtogroup LIBRARY Library
  18. @{
  19. */
  20. /** @addtogroup USBH_Library USB Host Library
  21. @{
  22. */
  23. /** @addtogroup USBH_EXPORTED_CONSTANTS USB Host Exported Constants
  24. @{
  25. */
  26. #define USBH_OK 0 /*!< No error. */
  27. #define USBH_ERR_MEMORY_OUT -10 /*!< Out of memory. */
  28. #define USBH_ERR_IF_ALT_LIMIT -11 /*!< Number of alternative interface > MAX_ALT_PER_IFACE */
  29. #define USBH_ERR_IF_EP_LIMIT -15 /*!< Number of endpoints > MAX_EP_PER_IFACE */
  30. #define USBH_ERR_NOT_SUPPORTED -101 /*!< Device/Class/Transfer not supported */
  31. #define USBH_ERR_NOT_MATCHED -103 /*!< Not macthed */
  32. #define USBH_ERR_NOT_EXPECTED -104 /*!< Unknown or unexpected */
  33. #define USBH_ERR_INVALID_PARAM -105 /*!< Invalid parameter */
  34. #define USBH_ERR_NOT_FOUND -106 /*!< Device or interface not found */
  35. #define USBH_ERR_EP_NOT_FOUND -107 /*!< Endpoint not found */
  36. #define USBH_ERR_DESCRIPTOR -137 /*!< Failed to parse USB descriptors */
  37. #define USBH_ERR_SET_DEV_ADDR -139 /*!< Failed to set device address */
  38. #define USBH_ERR_SET_CONFIG -151 /*!< Failed to set device configuration */
  39. #define USBH_ERR_TRANSFER -201 /*!< USB transfer error */
  40. #define USBH_ERR_TIMEOUT -203 /*!< USB transfer time-out */
  41. #define USBH_ERR_ABORT -205 /*!< USB transfer aborted due to disconnect or reset */
  42. #define USBH_ERR_PORT_RESET -255 /*!< Hub port reset failed */
  43. #define USBH_ERR_SCH_OVERRUN -257 /*!< USB isochronous schedule overrun */
  44. #define USBH_ERR_DISCONNECTED -259 /*!< USB device was disconnected */
  45. #define USBH_ERR_TRANSACTION -271 /*!< USB transaction timeout, CRC, Bad PID, etc. */
  46. #define USBH_ERR_BABBLE_DETECTED -272 /*!< A 'babble' is detected during the transaction */
  47. #define USBH_ERR_DATA_BUFF -274 /*!< Data buffer overrun or underrun */
  48. #define USBH_ERR_CC_NO_ERR -280 /*!< OHCI CC code - no error */
  49. #define USBH_ERR_CRC -281 /*!< USB trasfer CRC error */
  50. #define USBH_ERR_BIT_STUFF -282 /*!< USB transfer bit stuffing error */
  51. #define USBH_ERR_DATA_TOGGLE -283 /*!< USB trasfer data toggle error */
  52. #define USBH_ERR_STALL -284 /*!< USB trasfer STALL error */
  53. #define USBH_ERR_DEV_NO_RESP -285 /*!< USB trasfer device no response error */
  54. #define USBH_ERR_PID_CHECK -286 /*!< USB trasfer PID check failure */
  55. #define USBH_ERR_UNEXPECT_PID -287 /*!< USB trasfer unexpected PID error */
  56. #define USBH_ERR_DATA_OVERRUN -288 /*!< USB trasfer data overrun error */
  57. #define USBH_ERR_DATA_UNDERRUN -289 /*!< USB trasfer data underrun error */
  58. #define USBH_ERR_BUFF_OVERRUN -292 /*!< USB trasfer buffer overrun error */
  59. #define USBH_ERR_BUFF_UNDERRUN -293 /*!< USB trasfer buffer underrun error */
  60. #define USBH_ERR_NOT_ACCESS0 -294 /*!< USB trasfer not accessed error */
  61. #define USBH_ERR_NOT_ACCESS1 -295 /*!< USB trasfer not accessed error */
  62. #define USBH_ERR_OHCI_INIT -301 /*!< Failed to initialize OHIC controller. */
  63. #define USBH_ERR_OHCI_EP_BUSY -303 /*!< The endpoint is under transfer. */
  64. #define USBH_ERR_EHCI_INIT -501 /*!< Failed to initialize EHCI controller. */
  65. #define USBH_ERR_EHCI_QH_BUSY -503 /*!< the Queue Head is busy. */
  66. #define UMAS_OK 0 /*!< No error. */
  67. #define UMAS_ERR_NO_DEVICE -1031 /*!< No Mass Stroage Device found. */
  68. #define UMAS_ERR_IO -1033 /*!< Device read/write failed. */
  69. #define UMAS_ERR_INIT_DEVICE -1035 /*!< failed to init MSC device */
  70. #define UMAS_ERR_CMD_STATUS -1037 /*!< SCSI command status failed */
  71. #define UMAS_ERR_IVALID_PARM -1038 /*!< Invalid parameter. */
  72. #define UMAS_ERR_DRIVE_NOT_FOUND -1039 /*!< drive not found */
  73. #define HID_RET_OK 0 /*!< Return with no errors. */
  74. #define HID_RET_DEV_NOT_FOUND -1081 /*!< HID device not found or removed. */
  75. #define HID_RET_IO_ERR -1082 /*!< USB transfer failed. */
  76. #define HID_RET_INVALID_PARAMETER -1083 /*!< Invalid parameter. */
  77. #define HID_RET_OUT_OF_MEMORY -1084 /*!< Out of memory. */
  78. #define HID_RET_NOT_SUPPORTED -1085 /*!< Function not supported. */
  79. #define HID_RET_EP_NOT_FOUND -1086 /*!< Endpoint not found. */
  80. #define HID_RET_PARSING -1087 /*!< Failed to parse HID descriptor */
  81. #define HID_RET_XFER_IS_RUNNING -1089 /*!< The transfer has been enabled. */
  82. #define HID_RET_REPORT_NOT_FOUND -1090 /*!< The transfer has been enabled. */
  83. #define UAC_RET_OK 0 /*!< Return with no errors. */
  84. #define UAC_RET_DEV_NOT_FOUND -2001 /*!< Audio Class device not found or removed. */
  85. #define UAC_RET_FUNC_NOT_FOUND -2002 /*!< Audio device has no this function. */
  86. #define UAC_RET_IO_ERR -2003 /*!< USB transfer failed. */
  87. #define UAC_RET_DATA_LEN -2004 /*!< Unexpected transfer length */
  88. #define UAC_RET_INVALID -2005 /*!< Invalid parameter or usage. */
  89. #define UAC_RET_OUT_OF_MEMORY -2007 /*!< Out of memory. */
  90. #define UAC_RET_DRV_NOT_SUPPORTED -2009 /*!< Function not supported by this UAC driver. */
  91. #define UAC_RET_DEV_NOT_SUPPORTED -2011 /*!< Function not supported by the UAC device. */
  92. #define UAC_RET_PARSER -2013 /*!< Failed to parse UAC descriptor */
  93. #define UAC_RET_IS_STREAMING -2015 /*!< Audio pipe is on streaming. */
  94. /*@}*/ /* end of group USBH_EXPORTED_CONSTANTS */
  95. /** @addtogroup USBH_EXPORTED_TYPEDEF USB Host Typedef
  96. @{
  97. */
  98. struct udev_t;
  99. typedef void (CONN_FUNC)(struct udev_t *udev, int param);
  100. struct line_coding_t;
  101. struct cdc_dev_t;
  102. typedef void (CDC_CB_FUNC)(struct cdc_dev_t *cdev, uint8_t *rdata, int data_len);
  103. struct usbhid_dev;
  104. typedef void (HID_IR_FUNC)(struct usbhid_dev *hdev, uint16_t ep_addr, int status, uint8_t *rdata, uint32_t data_len); /*!< interrupt in callback function \hideinitializer */
  105. typedef void (HID_IW_FUNC)(struct usbhid_dev *hdev, uint16_t ep_addr, int status, uint8_t *wbuff, uint32_t *data_len); /*!< interrupt out callback function \hideinitializer */
  106. struct uac_dev_t;
  107. typedef int (UAC_CB_FUNC)(struct uac_dev_t *dev, uint8_t *data, int len); /*!< audio in callback function \hideinitializer */
  108. /*@}*/ /* end of group USBH_EXPORTED_STRUCT */
  109. /** @addtogroup USBH_EXPORTED_FUNCTIONS USB Host Exported Functions
  110. @{
  111. */
  112. /*------------------------------------------------------------------*/
  113. /* */
  114. /* USB Core Library APIs */
  115. /* */
  116. /*------------------------------------------------------------------*/
  117. extern void usbh_core_init(void);
  118. extern int usbh_polling_root_hubs(void);
  119. extern void usbh_install_conn_callback(CONN_FUNC *conn_func, CONN_FUNC *disconn_func);
  120. extern void usbh_suspend(void);
  121. extern void usbh_resume(void);
  122. extern struct udev_t *usbh_find_device(char *hub_id, int port);
  123. /**
  124. * @brief A function return current tick count.
  125. * @return Current tick.
  126. * @details User application must provide this function to return current tick.
  127. * The tick should increase by 1 for every 10 ms.
  128. */
  129. extern uint32_t usbh_get_ticks(void); /* This function must be provided by user application. */
  130. extern uint32_t usbh_tick_from_millisecond(uint32_t msec); /* This function must be provided by user application. */
  131. /*------------------------------------------------------------------*/
  132. /* */
  133. /* USB Communication Device Class Library APIs */
  134. /* */
  135. /*------------------------------------------------------------------*/
  136. extern void usbh_cdc_init(void);
  137. extern struct cdc_dev_t *usbh_cdc_get_device_list(void);
  138. /// @cond HIDDEN_SYMBOLS
  139. extern int32_t usbh_cdc_get_line_coding(struct cdc_dev_t *cdev, struct line_coding_t *line_code);
  140. extern int32_t usbh_cdc_set_line_coding(struct cdc_dev_t *cdev, struct line_coding_t *line_code);
  141. /// @endcond HIDDEN_SYMBOLS
  142. extern int32_t usbh_cdc_set_control_line_state(struct cdc_dev_t *cdev, int active_carrier, int DTE_present);
  143. extern int32_t usbh_cdc_start_polling_status(struct cdc_dev_t *cdev, CDC_CB_FUNC *func);
  144. extern int32_t usbh_cdc_start_to_receive_data(struct cdc_dev_t *cdev, CDC_CB_FUNC *func);
  145. extern int32_t usbh_cdc_send_data(struct cdc_dev_t *cdev, uint8_t *buff, int buff_len);
  146. /*------------------------------------------------------------------*/
  147. /* */
  148. /* USB Human Interface Class Library APIs */
  149. /* */
  150. /*------------------------------------------------------------------*/
  151. extern void usbh_hid_init(void);
  152. extern struct usbhid_dev *usbh_hid_get_device_list(void);
  153. extern int32_t usbh_hid_get_report_descriptor(struct usbhid_dev *hdev, uint8_t *desc_buf, int buf_max_len);
  154. extern int32_t usbh_hid_get_report(struct usbhid_dev *hdev, int rtp_typ, int rtp_id, uint8_t *data, int len);
  155. extern int32_t usbh_hid_set_report(struct usbhid_dev *hdev, int rtp_typ, int rtp_id, uint8_t *data, int len);
  156. extern int32_t usbh_hid_get_idle(struct usbhid_dev *hdev, int rtp_id, uint8_t *idle_rate);
  157. extern int32_t usbh_hid_set_idle(struct usbhid_dev *hdev, int rtp_id, uint8_t idle_rate);
  158. extern int32_t usbh_hid_get_protocol(struct usbhid_dev *hdev, uint8_t *protocol);
  159. extern int32_t usbh_hid_set_protocol(struct usbhid_dev *hdev, uint8_t protocol);
  160. extern int32_t usbh_hid_start_int_read(struct usbhid_dev *hdev, uint8_t ep_addr, HID_IR_FUNC *func);
  161. extern int32_t usbh_hid_stop_int_read(struct usbhid_dev *hdev, uint8_t ep_addr);
  162. extern int32_t usbh_hid_start_int_write(struct usbhid_dev *hdev, uint8_t ep_addr, HID_IW_FUNC *func);
  163. extern int32_t usbh_hid_stop_int_write(struct usbhid_dev *hdev, uint8_t ep_addr);
  164. /*------------------------------------------------------------------*/
  165. /* */
  166. /* USB Mass Storage Class Library APIs */
  167. /* */
  168. /*------------------------------------------------------------------*/
  169. extern int usbh_umas_init(void);
  170. extern int usbh_umas_disk_status(int drv_no);
  171. extern int usbh_umas_read(int drv_no, uint32_t sec_no, int sec_cnt, uint8_t *buff);
  172. extern int usbh_umas_write(int drv_no, uint32_t sec_no, int sec_cnt, uint8_t *buff);
  173. extern int usbh_umas_ioctl(int drv_no, int cmd, void *buff);
  174. /// @cond HIDDEN_SYMBOLS
  175. extern int usbh_umas_reset_disk(int drv_no);
  176. /// @endcond HIDDEN_SYMBOLS
  177. /*------------------------------------------------------------------*/
  178. /* */
  179. /* USB Audio Class Library APIs */
  180. /* */
  181. /*------------------------------------------------------------------*/
  182. extern void usbh_uac_init(void);
  183. extern int usbh_uac_open(struct uac_dev_t *audev);
  184. extern struct uac_dev_t *usbh_uac_get_device_list(void);
  185. extern int usbh_uac_get_channel_number(struct uac_dev_t *audev, uint8_t target);
  186. extern int usbh_uac_get_bit_resolution(struct uac_dev_t *audev, uint8_t target, uint8_t *byte_cnt);
  187. extern int usbh_uac_get_sampling_rate(struct uac_dev_t *audev, uint8_t target, uint32_t *srate_list, int max_cnt, uint8_t *type);
  188. extern int usbh_uac_sampling_rate_control(struct uac_dev_t *audev, uint8_t target, uint8_t req, uint32_t *srate);
  189. extern int usbh_uac_mute_control(struct uac_dev_t *audev, uint8_t target, uint8_t req, uint16_t chn, uint8_t *mute);
  190. extern int usbh_uac_vol_control(struct uac_dev_t *audev, uint8_t target, uint8_t req, uint16_t chn, uint16_t *volume);
  191. extern int usbh_uac_auto_gain_control(struct uac_dev_t *audev, uint8_t target, uint8_t req, uint16_t chn, uint8_t *bAGC);
  192. extern int usbh_uac_start_audio_in(struct uac_dev_t *uac, UAC_CB_FUNC *func);
  193. extern int usbh_uac_stop_audio_in(struct uac_dev_t *audev);
  194. extern int usbh_uac_start_audio_out(struct uac_dev_t *uac, UAC_CB_FUNC *func);
  195. extern int usbh_uac_stop_audio_out(struct uac_dev_t *audev);
  196. /// @cond HIDDEN_SYMBOLS
  197. extern void dump_ohci_regs(void);
  198. extern void dump_ehci_regs(void);
  199. extern void dump_ohci_ports(void);
  200. extern void dump_ehci_ports(void);
  201. extern uint32_t usbh_memory_used(void);
  202. /// @endcond HIDDEN_SYMBOLS
  203. /*@}*/ /* end of group USBH_EXPORTED_FUNCTIONS */
  204. /*@}*/ /* end of group USBH_Library */
  205. /*@}*/ /* end of group LIBRARY */
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif /* _USBH_LIB_H_ */
  210. /*** (C) COPYRIGHT 2019-2020 Nuvoton Technology Corp. ***/