usb.h 22 KB

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