usb.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**************************************************************************//**
  2. * @file usb.h
  3. * @version V1.00
  4. * @brief USB Host library header file.
  5. * @note
  6. * SPDX-License-Identifier: Apache-2.0
  7. * Copyright (C) 2017 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef _USBH_H_
  10. #define _USBH_H_
  11. #include "config.h"
  12. #include "usbh_lib.h"
  13. #include "ehci.h"
  14. #include "ohci.h"
  15. /// @cond HIDDEN_SYMBOLS
  16. struct utr_t;
  17. struct udev_t;
  18. struct hub_dev_t;
  19. struct iface_t;
  20. struct ep_info_t;
  21. /*----------------------------------------------------------------------------------*/
  22. /* USB device request setup packet */
  23. /*----------------------------------------------------------------------------------*/
  24. typedef struct __attribute__((__packed__))
  25. {
  26. uint8_t bmRequestType;
  27. uint8_t bRequest;
  28. uint16_t wValue;
  29. uint16_t wIndex;
  30. uint16_t wLength;
  31. }
  32. DEV_REQ_T;
  33. /*
  34. * bmRequestType[7] - Data transfer direction
  35. */
  36. #define REQ_TYPE_OUT 0x00
  37. #define REQ_TYPE_IN 0x80
  38. /*
  39. * bmRequestType[6:5] - Type
  40. */
  41. #define REQ_TYPE_STD_DEV 0x00
  42. #define REQ_TYPE_CLASS_DEV 0x20
  43. #define REQ_TYPE_VENDOR_DEV 0x40
  44. /*
  45. * bmRequestType[4:0] - Recipient
  46. */
  47. #define REQ_TYPE_TO_DEV 0x00
  48. #define REQ_TYPE_TO_IFACE 0x01
  49. #define REQ_TYPE_TO_EP 0x02
  50. #define REQ_TYPE_TO_OTHER 0x03
  51. /*
  52. * Standard Requests
  53. */
  54. #define USB_REQ_GET_STATUS 0x00
  55. #define USB_REQ_CLEAR_FEATURE 0x01
  56. #define USB_REQ_SET_FEATURE 0x03
  57. #define USB_REQ_SET_ADDRESS 0x05
  58. #define USB_REQ_GET_DESCRIPTOR 0x06
  59. #define USB_REQ_SET_CONFIGURATION 0x09
  60. #define USB_REQ_SET_INTERFACE 0x0B
  61. /*
  62. * Descriptor Types
  63. */
  64. #define USB_DT_STANDARD 0x00
  65. #define USB_DT_CLASS 0x20
  66. #define USB_DT_VENDOR 0x40
  67. #define USB_DT_DEVICE 0x01
  68. #define USB_DT_CONFIGURATION 0x02
  69. #define USB_DT_STRING 0x03
  70. #define USB_DT_INTERFACE 0x04
  71. #define USB_DT_ENDPOINT 0x05
  72. #define USB_DT_DEVICE_QUALIFIER 0x06
  73. #define USB_DT_OTHER_SPEED_CONF 0x07
  74. #define USB_DT_IFACE_POWER 0x08
  75. /*----------------------------------------------------------------------------------*/
  76. /* USB standard descriptors */
  77. /*----------------------------------------------------------------------------------*/
  78. /* Descriptor header */
  79. typedef struct __attribute__((__packed__))
  80. {
  81. uint8_t bLength;
  82. uint8_t bDescriptorType;
  83. }
  84. DESC_HDR_T;
  85. /*----------------------------------------------------------------------------------*/
  86. /* USB device descriptor */
  87. /*----------------------------------------------------------------------------------*/
  88. typedef struct __attribute__((__packed__)) /*!< device descriptor structure */
  89. {
  90. uint8_t bLength; /*!< Length of device descriptor */
  91. uint8_t bDescriptorType; /*!< Device descriptor type */
  92. uint16_t bcdUSB; /*!< USB version number */
  93. uint8_t bDeviceClass; /*!< Device class code */
  94. uint8_t bDeviceSubClass; /*!< Device subclass code */
  95. uint8_t bDeviceProtocol; /*!< Device protocol code */
  96. uint8_t bMaxPacketSize0; /*!< Maximum packet size of control endpoint*/
  97. uint16_t idVendor; /*!< Vendor ID */
  98. uint16_t idProduct; /*!< Product ID */
  99. uint16_t bcdDevice; /*!< Device ID */
  100. uint8_t iManufacturer; /*!< Manufacture description string ID */
  101. uint8_t iProduct; /*!< Product description string ID */
  102. uint8_t iSerialNumber; /*!< Serial number description string ID */
  103. uint8_t bNumConfigurations; /*!< Total number of configurations */
  104. }
  105. DESC_DEV_T; /*!< device descriptor structure */
  106. /*
  107. * Configuration Descriptor
  108. */
  109. typedef struct __attribute__((__packed__)) usb_config_descriptor /*!< Configuration descriptor structure */
  110. {
  111. uint8_t bLength; /*!< Length of configuration descriptor */
  112. uint8_t bDescriptorType; /*!< Descriptor type */
  113. uint16_t wTotalLength; /*!< Total length of this configuration */
  114. uint8_t bNumInterfaces; /*!< Total number of interfaces */
  115. uint8_t bConfigurationValue; /*!< Configuration descriptor number */
  116. uint8_t iConfiguration; /*!< String descriptor ID */
  117. uint8_t bmAttributes; /*!< Configuration characteristics */
  118. uint8_t MaxPower; /*!< Maximum power consumption */
  119. } DESC_CONF_T; /*!< Configuration descriptor structure */
  120. /*
  121. * Interface Descriptor
  122. */
  123. typedef struct __attribute__((__packed__))usb_interface_descriptor /*!< Interface descriptor structure */
  124. {
  125. uint8_t bLength; /*!< Length of interface descriptor */
  126. uint8_t bDescriptorType; /*!< Descriptor type */
  127. uint8_t bInterfaceNumber; /*!< Interface number */
  128. uint8_t bAlternateSetting; /*!< Alternate setting number */
  129. uint8_t bNumEndpoints; /*!< Number of endpoints */
  130. uint8_t bInterfaceClass; /*!< Interface class code */
  131. uint8_t bInterfaceSubClass; /*!< Interface subclass code */
  132. uint8_t bInterfaceProtocol; /*!< Interface protocol code */
  133. uint8_t iInterface; /*!< Interface ID */
  134. } DESC_IF_T; /*!< Interface descriptor structure */
  135. /*
  136. * Endpoint Descriptor
  137. */
  138. typedef struct __attribute__((__packed__)) usb_endpoint_descriptor /*!< Endpoint descriptor structure */
  139. {
  140. uint8_t bLength; /*!< Length of endpoint descriptor */
  141. uint8_t bDescriptorType; /*!< Descriptor type */
  142. uint8_t bEndpointAddress; /*!< Endpoint address */
  143. uint8_t bmAttributes; /*!< Endpoint attribute */
  144. uint16_t wMaxPacketSize; /*!< Maximum packet size */
  145. uint8_t bInterval; /*!< Synchronous transfer interval */
  146. uint8_t bRefresh; /*!< Refresh */
  147. uint8_t bSynchAddress; /*!< Sync address */
  148. } DESC_EP_T; /*!< Endpoint descriptor structure */
  149. /*
  150. * Endpoint descriptor bEndpointAddress[7] - direction
  151. */
  152. #define EP_ADDR_DIR_MASK 0x80
  153. #define EP_ADDR_DIR_IN 0x80
  154. #define EP_ADDR_DIR_OUT 0x00
  155. /*
  156. * Endpoint descriptor bmAttributes[1:0] - transfer type
  157. */
  158. #define EP_ATTR_TT_MASK 0x03
  159. #define EP_ATTR_TT_CTRL 0x00
  160. #define EP_ATTR_TT_ISO 0x01
  161. #define EP_ATTR_TT_BULK 0x02
  162. #define EP_ATTR_TT_INT 0x03
  163. /*----------------------------------------------------------------------------------*/
  164. /* USB Host controller driver */
  165. /*----------------------------------------------------------------------------------*/
  166. typedef struct
  167. {
  168. int (*init)(void);
  169. void (*shutdown)(void);
  170. void (*suspend)(void);
  171. void (*resume)(void);
  172. int (*ctrl_xfer)(struct utr_t *utr);
  173. int (*bulk_xfer)(struct utr_t *utr);
  174. int (*int_xfer)(struct utr_t *utr);
  175. int (*iso_xfer)(struct utr_t *utr);
  176. int (*quit_xfer)(struct utr_t *utr, struct ep_info_t *ep);
  177. /* root hub support */
  178. int (*rthub_port_reset)(int port);
  179. int (*rthub_polling)(void);
  180. } HC_DRV_T;
  181. /*----------------------------------------------------------------------------------*/
  182. /* USB device driver */
  183. /*----------------------------------------------------------------------------------*/
  184. typedef struct
  185. {
  186. int (*probe)(struct iface_t *iface);
  187. void (*disconnect)(struct iface_t *iface);
  188. void (*suspend)(struct iface_t *iface);
  189. void (*resume)(struct iface_t *iface);
  190. } UDEV_DRV_T;
  191. /*----------------------------------------------------------------------------------*/
  192. /* USB device */
  193. /*----------------------------------------------------------------------------------*/
  194. typedef enum
  195. {
  196. SPEED_LOW,
  197. SPEED_FULL,
  198. SPEED_HIGH
  199. } SPEED_E;
  200. typedef struct ep_info_t
  201. {
  202. uint8_t bEndpointAddress;
  203. uint8_t bmAttributes;
  204. uint8_t bInterval;
  205. uint8_t bToggle;
  206. uint16_t wMaxPacketSize;
  207. void *hw_pipe; /*!< point to the HC assocaied endpoint \hideinitializer */
  208. } EP_INFO_T;
  209. typedef struct udev_t
  210. {
  211. DESC_DEV_T descriptor; /*!< Device descriptor. \hideinitializer */
  212. struct hub_dev_t *parent; /*!< parent hub device \hideinitializer */
  213. uint8_t port_num; /*!< The hub port this device connected on \hideinitializer */
  214. uint8_t dev_num; /*!< device number \hideinitializer */
  215. int8_t cur_conf; /*!< Currentll selected configuration \hideinitializer */
  216. SPEED_E speed; /*!< device speed (low/full/high) \hideinitializer */
  217. /*
  218. * The followings are lightweight USB stack internal used .
  219. */
  220. uint8_t *cfd_buff; /*!< Configuration descriptor buffer. \hideinitializer */
  221. EP_INFO_T ep0; /*!< Endpoint 0 \hideinitializer */
  222. HC_DRV_T *hc_driver; /*!< host controller driver \hideinitializer */
  223. struct iface_t *iface_list; /*!< Working interface list \hideinitializer */
  224. struct udev_t *next; /*!< link for global usb device list \hideinitializer */
  225. } UDEV_T;
  226. typedef struct alt_iface_t
  227. {
  228. DESC_IF_T *ifd; /*!< point to the location of this alternative interface descriptor in UDEV_T->cfd_buff */
  229. EP_INFO_T ep[MAX_EP_PER_IFACE]; /*!< endpoints of this alternative interface */
  230. } ALT_IFACE_T;
  231. typedef struct iface_t
  232. {
  233. UDEV_T *udev; /*!< USB device \hideinitializer */
  234. uint8_t if_num; /*!< Interface number \hideinitializer */
  235. uint8_t num_alt; /*!< Number of alternative interface \hideinitializer */
  236. ALT_IFACE_T *aif; /*!< Point to the active alternative interface */
  237. ALT_IFACE_T alt[MAX_ALT_PER_IFACE]; /*!< List of alternative interface \hideinitializer */
  238. UDEV_DRV_T *driver; /*!< Interface associated driver \hideinitializer */
  239. void *context; /*!< Reference to device context \hideinitializer */
  240. struct iface_t *next; /*!< Point to next interface of the same device. Started from UDEV_T->iface_list \hideinitializer */
  241. } IFACE_T;
  242. /*----------------------------------------------------------------------------------*/
  243. /* URB (USB Request Block) */
  244. /*----------------------------------------------------------------------------------*/
  245. #define IF_PER_UTR 8 /* number of frames per UTR isochronous transfer (DO NOT modify it!) */
  246. typedef void (*FUNC_UTR_T)(struct utr_t *);
  247. typedef struct utr_t
  248. {
  249. UDEV_T *udev; /*!< point to associated USB device \hideinitializer */
  250. DEV_REQ_T setup; /*!< buffer for setup packet \hideinitializer */
  251. EP_INFO_T *ep; /*!< associated endpoint \hideinitializer */
  252. uint8_t *buff; /*!< transfer buffer \hideinitializer */
  253. uint8_t bIsTransferDone; /*!< tansfer done? \hideinitializer */
  254. uint32_t data_len; /*!< length of data to be transferred \hideinitializer */
  255. uint32_t xfer_len; /*!< length of transferred data \hideinitializer */
  256. uint8_t bIsoNewSched; /*!< New schedule isochronous transfer \hideinitializer */
  257. uint16_t iso_sf; /*!< Isochronous start frame number \hideinitializer */
  258. uint16_t iso_xlen[IF_PER_UTR]; /*!< transfer length of isochronous frames \hideinitializer */
  259. uint8_t *iso_buff[IF_PER_UTR]; /*!< transfer buffer address of isochronous frames \hideinitializer */
  260. int iso_status[IF_PER_UTR]; /*!< transfer status of isochronous frames \hideinitializer */
  261. int td_cnt; /*!< number of transfer descriptors \hideinitializer */
  262. int status; /*!< return status \hideinitializer */
  263. int interval; /*!< interrupt/isochronous interval \hideinitializer */
  264. void *context; /*!< point to deivce proprietary data area \hideinitializer */
  265. FUNC_UTR_T func; /*!< tansfer done call-back function \hideinitializer */
  266. struct utr_t *next; /* point to the next UTR of the same endpoint. \hideinitializer */
  267. } UTR_T;
  268. /*----------------------------------------------------------------------------------*/
  269. /* Global variables */
  270. /*----------------------------------------------------------------------------------*/
  271. extern USBH_T *_ohci;
  272. extern HSUSBH_T *_ehci;
  273. extern HC_DRV_T ohci_driver;
  274. extern HC_DRV_T ehci_driver;
  275. extern UDEV_T *g_udev_list;
  276. extern volatile int _IsInUsbInterrupt;
  277. /*----------------------------------------------------------------------------------*/
  278. /* USB stack exported functions */
  279. /*----------------------------------------------------------------------------------*/
  280. extern void usbh_delay_ms(int msec);
  281. extern void dump_ohci_regs(void);
  282. extern void dump_ohci_ports(void);
  283. extern void dump_ohci_int_table(void);
  284. extern void dump_ehci_regs(void);
  285. extern void dump_ehci_qtd(qTD_T *qtd);
  286. extern void dump_ehci_asynclist(void);
  287. extern void dump_ehci_period_frame_list_simple(void);
  288. extern void usbh_dump_buff_bytes(uint8_t *buff, int nSize);
  289. extern void usbh_dump_interface_descriptor(DESC_IF_T *if_desc);
  290. extern void usbh_dump_endpoint_descriptor(DESC_EP_T *ep_desc);
  291. extern void usbh_dump_iface(IFACE_T *iface);
  292. extern void usbh_dump_ep_info(EP_INFO_T *ep);
  293. /*
  294. * Memory management functions
  295. */
  296. extern void USB_InitializeMemoryPool(void);
  297. extern void *USB_malloc(int wanted_size, int boundary);
  298. extern void USB_free(void *);
  299. extern int USB_available_memory(void);
  300. extern int USB_allocated_memory(void);
  301. extern void usbh_memory_init(void);
  302. extern uint32_t usbh_memory_used(void);
  303. extern void *usbh_alloc_mem(int size);
  304. extern void usbh_free_mem(void *p, int size);
  305. extern int alloc_dev_address(void);
  306. extern void free_dev_address(int dev_addr);
  307. extern UDEV_T *alloc_device(void);
  308. extern void free_device(UDEV_T *udev);
  309. extern UTR_T *alloc_utr(UDEV_T *udev);
  310. extern void free_utr(UTR_T *utr);
  311. extern ED_T *alloc_ohci_ED(void);
  312. extern void free_ohci_ED(ED_T *ed);
  313. extern TD_T *alloc_ohci_TD(UTR_T *utr);
  314. extern void free_ohci_TD(TD_T *td);
  315. extern QH_T *alloc_ehci_QH(void);
  316. extern void free_ehci_QH(QH_T *qh);
  317. extern qTD_T *alloc_ehci_qTD(UTR_T *utr);
  318. extern void free_ehci_qTD(qTD_T *qtd);
  319. extern iTD_T *alloc_ehci_iTD(void);
  320. extern void free_ehci_iTD(iTD_T *itd);
  321. extern siTD_T *alloc_ehci_siTD(void);
  322. extern void free_ehci_siTD(siTD_T *sitd);
  323. extern void usbh_hub_init(void);
  324. extern int usbh_connect_device(UDEV_T *);
  325. extern void usbh_disconnect_device(UDEV_T *);
  326. extern int usbh_register_driver(UDEV_DRV_T *driver);
  327. extern EP_INFO_T *usbh_iface_find_ep(IFACE_T *iface, uint8_t ep_addr, uint8_t dir_type);
  328. extern int usbh_reset_device(UDEV_T *);
  329. extern int usbh_reset_port(UDEV_T *);
  330. /*
  331. * USB Standard Request functions
  332. */
  333. extern int usbh_get_device_descriptor(UDEV_T *udev, DESC_DEV_T *desc_buff);
  334. extern int usbh_get_config_descriptor(UDEV_T *udev, uint8_t *desc_buff, int buff_len);
  335. extern int usbh_set_configuration(UDEV_T *udev, uint8_t conf_val);
  336. extern int usbh_set_interface(IFACE_T *iface, uint16_t alt_setting);
  337. extern int usbh_clear_halt(UDEV_T *udev, uint16_t ep_addr);
  338. extern int usbh_ctrl_xfer(UDEV_T *udev, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength, uint8_t *buff, uint32_t *xfer_len, uint32_t timeout);
  339. extern int usbh_bulk_xfer(UTR_T *utr);
  340. extern int usbh_int_xfer(UTR_T *utr);
  341. extern int usbh_iso_xfer(UTR_T *utr);
  342. extern int usbh_quit_utr(UTR_T *utr);
  343. extern int usbh_quit_xfer(UDEV_T *udev, EP_INFO_T *ep);
  344. /// @endcond HIDDEN_SYMBOLS
  345. #endif /* _USBH_H_ */