usb.h 22 KB

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