usb_spec.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __USB_SPEC_H__
  31. #define __USB_SPEC_H__
  32. /*******************************************************************************
  33. * Definitions
  34. ******************************************************************************/
  35. /* USB speed (the value cannot be changed because EHCI QH use the value directly)*/
  36. #define USB_SPEED_FULL (0x00U)
  37. #define USB_SPEED_LOW (0x01U)
  38. #define USB_SPEED_HIGH (0x02U)
  39. /* Set up packet structure */
  40. typedef struct _usb_setup_struct
  41. {
  42. uint8_t bmRequestType;
  43. uint8_t bRequest;
  44. uint16_t wValue;
  45. uint16_t wIndex;
  46. uint16_t wLength;
  47. } usb_setup_struct_t;
  48. /* USB standard descriptor endpoint type */
  49. #define USB_ENDPOINT_CONTROL (0x00U)
  50. #define USB_ENDPOINT_ISOCHRONOUS (0x01U)
  51. #define USB_ENDPOINT_BULK (0x02U)
  52. #define USB_ENDPOINT_INTERRUPT (0x03U)
  53. /* USB standard descriptor transfer direction (cannot change the value because iTD use the value directly) */
  54. #define USB_OUT (0U)
  55. #define USB_IN (1U)
  56. /* USB standard descriptor length */
  57. #define USB_DESCRIPTOR_LENGTH_DEVICE (0x12U)
  58. #define USB_DESCRIPTOR_LENGTH_CONFIGURE (0x09U)
  59. #define USB_DESCRIPTOR_LENGTH_INTERFACE (0x09U)
  60. #define USB_DESCRIPTOR_LENGTH_ENDPOINT (0x07U)
  61. #define USB_DESCRIPTOR_LENGTH_DEVICE_QUALITIER (0x0AU)
  62. #define USB_DESCRIPTOR_LENGTH_OTG_DESCRIPTOR (5U)
  63. #define USB_DESCRIPTOR_LENGTH_BOS_DESCRIPTOR (5U)
  64. /* USB Device Capability Type Codes */
  65. #define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY_WIRELESS (0x01U)
  66. #define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY_USB20_EXTENSION (0x02U)
  67. #define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY_SUPERSPEED (0x03U)
  68. /* USB standard descriptor type */
  69. #define USB_DESCRIPTOR_TYPE_DEVICE (0x01U)
  70. #define USB_DESCRIPTOR_TYPE_CONFIGURE (0x02U)
  71. #define USB_DESCRIPTOR_TYPE_STRING (0x03U)
  72. #define USB_DESCRIPTOR_TYPE_INTERFACE (0x04U)
  73. #define USB_DESCRIPTOR_TYPE_ENDPOINT (0x05U)
  74. #define USB_DESCRIPTOR_TYPE_DEVICE_QUALITIER (0x06U)
  75. #define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION (0x07U)
  76. #define USB_DESCRIPTOR_TYPE_INTERFAACE_POWER (0x08U)
  77. #define USB_DESCRIPTOR_TYPE_OTG (0x09U)
  78. #define USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION (0x0BU)
  79. #define USB_DESCRIPTOR_TYPE_BOS (0x0F)
  80. #define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY (0x10)
  81. #define USB_DESCRIPTOR_TYPE_HID (0x21U)
  82. #define USB_DESCRIPTOR_TYPE_HID_REPORT (0x22U)
  83. #define USB_DESCRIPTOR_TYPE_HID_PHYSICAL (0x23U)
  84. /* USB standard request type */
  85. #define USB_REQUEST_TYPE_DIR_MASK (0x80U)
  86. #define USB_REQUEST_TYPE_DIR_SHIFT (7U)
  87. #define USB_REQUEST_TYPE_DIR_OUT (0x00U)
  88. #define USB_REQUEST_TYPE_DIR_IN (0x80U)
  89. #define USB_REQUEST_TYPE_TYPE_MASK (0x60U)
  90. #define USB_REQUEST_TYPE_TYPE_SHIFT (5U)
  91. #define USB_REQUEST_TYPE_TYPE_STANDARD (0U)
  92. #define USB_REQUEST_TYPE_TYPE_CLASS (0x20U)
  93. #define USB_REQUEST_TYPE_TYPE_VENDOR (0x40U)
  94. #define USB_REQUEST_TYPE_RECIPIENT_MASK (0x1FU)
  95. #define USB_REQUEST_TYPE_RECIPIENT_SHIFT (0U)
  96. #define USB_REQUEST_TYPE_RECIPIENT_DEVICE (0x00U)
  97. #define USB_REQUEST_TYPE_RECIPIENT_INTERFACE (0x01U)
  98. #define USB_REQUEST_TYPE_RECIPIENT_ENDPOINT (0x02U)
  99. #define USB_REQUEST_TYPE_RECIPIENT_OTHER (0x03U)
  100. /* USB standard request */
  101. #define USB_REQUEST_STANDARD_GET_STATUS (0x00U)
  102. #define USB_REQUEST_STANDARD_CLEAR_FEATURE (0x01U)
  103. #define USB_REQUEST_STANDARD_SET_FEATURE (0x03U)
  104. #define USB_REQUEST_STANDARD_SET_ADDRESS (0x05U)
  105. #define USB_REQUEST_STANDARD_GET_DESCRIPTOR (0x06U)
  106. #define USB_REQUEST_STANDARD_SET_DESCRIPTOR (0x07U)
  107. #define USB_REQUEST_STANDARD_GET_CONFIGURATION (0x08U)
  108. #define USB_REQUEST_STANDARD_SET_CONFIGURATION (0x09U)
  109. #define USB_REQUEST_STANDARD_GET_INTERFACE (0x0AU)
  110. #define USB_REQUEST_STANDARD_SET_INTERFACE (0x0BU)
  111. #define USB_REQUEST_STANDARD_SYNCH_FRAME (0x0CU)
  112. /* USB standard request GET Status */
  113. #define USB_REQUEST_STANDARD_GET_STATUS_DEVICE_SELF_POWERED_SHIFT (0U)
  114. #define USB_REQUEST_STANDARD_GET_STATUS_DEVICE_REMOTE_WARKUP_SHIFT (1U)
  115. #define USB_REQUEST_STANDARD_GET_STATUS_ENDPOINT_HALT_MASK (0x01U)
  116. #define USB_REQUEST_STANDARD_GET_STATUS_ENDPOINT_HALT_SHIFT (0U)
  117. #define USB_REQUEST_STANDARD_GET_STATUS_OTG_STATUS_SELECTOR (0xF000U)
  118. /* USB standard request CLEAR/SET feature */
  119. #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_ENDPOINT_HALT (0U)
  120. #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_DEVICE_REMOTE_WAKEUP (1U)
  121. #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_DEVICE_TEST_MODE (2U)
  122. #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_B_HNP_ENABLE (3U)
  123. #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_A_HNP_SUPPORT (4U)
  124. #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_A_ALT_HNP_SUPPORT (5U)
  125. /* USB standard descriptor configure bmAttributes */
  126. #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_D7_MASK (0x80U)
  127. #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_D7_SHIFT (7U)
  128. #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_SELF_POWERED_MASK (0x40U)
  129. #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_SELF_POWERED_SHIFT (6U)
  130. #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_REMOTE_WAKEUP_MASK (0x20U)
  131. #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_REMOTE_WAKEUP_SHIFT (5U)
  132. /* USB standard descriptor endpoint bmAttributes */
  133. #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK (0x80U)
  134. #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT (7U)
  135. #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT (0U)
  136. #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN (0x80U)
  137. #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_MASK (0x0FU)
  138. #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_SHFIT (0U)
  139. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_TYPE_MASK (0x03U)
  140. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_NUMBER_SHFIT (0U)
  141. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_MASK (0x0CU)
  142. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_SHFIT (2U)
  143. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_NO_SYNC (0x00U)
  144. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_ASYNC (0x04U)
  145. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_ADAPTIVE (0x08U)
  146. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_SYNC (0x0CU)
  147. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_USAGE_TYPE_MASK (0x30U)
  148. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_USAGE_TYPE_SHFIT (4U)
  149. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_USAGE_TYPE_DATA_ENDPOINT (0x00U)
  150. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_USAGE_TYPE_FEEDBACK_ENDPOINT (0x10U)
  151. #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_USAGE_TYPE_IMPLICIT_FEEDBACK_DATA_ENDPOINT (0x20U)
  152. #define USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_SIZE_MASK (0x07FFu)
  153. #define USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_MULT_TRANSACTIONS_MASK (0x1800u)
  154. #define USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_MULT_TRANSACTIONS_SHFIT (11U)
  155. /* USB standard descriptor otg bmAttributes */
  156. #define USB_DESCRIPTOR_OTG_ATTRIBUTES_SRP_MASK (0x01u)
  157. #define USB_DESCRIPTOR_OTG_ATTRIBUTES_HNP_MASK (0x02u)
  158. #define USB_DESCRIPTOR_OTG_ATTRIBUTES_ADP_MASK (0x04u)
  159. /* USB standard descriptor device capability usb20 extension bmAttributes */
  160. #define USB_DESCRIPTOR_DEVICE_CAPABILITY_USB20_EXTENSION_LPM_MASK (0x02U)
  161. #define USB_DESCRIPTOR_DEVICE_CAPABILITY_USB20_EXTENSION_LPM_SHIFT (1U)
  162. #define USB_DESCRIPTOR_DEVICE_CAPABILITY_USB20_EXTENSION_BESL_MASK (0x04U)
  163. #define USB_DESCRIPTOR_DEVICE_CAPABILITY_USB20_EXTENSION_BESL_SHIFT (2U)
  164. /* Language structure */
  165. typedef struct _usb_language
  166. {
  167. uint8_t **string; /* The Strings descriptor array */
  168. uint32_t *length; /* The strings descriptor length array */
  169. uint16_t languageId; /* The language id of current language */
  170. } usb_language_t;
  171. typedef struct _usb_language_list
  172. {
  173. uint8_t *languageString; /* The String 0U pointer */
  174. uint32_t stringLength; /* The String 0U Length */
  175. usb_language_t *languageList; /* The language list */
  176. uint8_t count; /* The language count */
  177. } usb_language_list_t;
  178. typedef struct _usb_descriptor_common
  179. {
  180. uint8_t bLength; /* Size of this descriptor in bytes */
  181. uint8_t bDescriptorType; /* DEVICE Descriptor Type */
  182. uint8_t bData[1]; /* Data */
  183. } usb_descriptor_common_t;
  184. typedef struct _usb_descriptor_device
  185. {
  186. uint8_t bLength; /* Size of this descriptor in bytes */
  187. uint8_t bDescriptorType; /* DEVICE Descriptor Type */
  188. uint8_t bcdUSB[2]; /* UUSB Specification Release Number in Binary-Coded Decimal, e.g. 0x0200U */
  189. uint8_t bDeviceClass; /* Class code */
  190. uint8_t bDeviceSubClass; /* Sub-Class code */
  191. uint8_t bDeviceProtocol; /* Protocol code */
  192. uint8_t bMaxPacketSize0; /* Maximum packet size for endpoint zero */
  193. uint8_t idVendor[2]; /* Vendor ID (assigned by the USB-IF) */
  194. uint8_t idProduct[2]; /* Product ID (assigned by the manufacturer) */
  195. uint8_t bcdDevice[2]; /* Device release number in binary-coded decimal */
  196. uint8_t iManufacturer; /* Index of string descriptor describing manufacturer */
  197. uint8_t iProduct; /* Index of string descriptor describing product */
  198. uint8_t iSerialNumber; /* Index of string descriptor describing the device serial number */
  199. uint8_t bNumConfigurations; /* Number of possible configurations */
  200. } usb_descriptor_device_t;
  201. typedef struct _usb_descriptor_configuration
  202. {
  203. uint8_t bLength; /* Descriptor size in bytes = 9U */
  204. uint8_t bDescriptorType; /* CONFIGURATION type = 2U or 7U */
  205. uint8_t wTotalLength[2]; /* Length of concatenated descriptors */
  206. uint8_t bNumInterfaces; /* Number of interfaces, this configuration. */
  207. uint8_t bConfigurationValue; /* Value to set this configuration. */
  208. uint8_t iConfiguration; /* Index to configuration string */
  209. uint8_t bmAttributes; /* Configuration characteristics */
  210. uint8_t bMaxPower; /* Maximum power from bus, 2 mA units */
  211. } usb_descriptor_configuration_t;
  212. typedef struct _usb_descriptor_interface
  213. {
  214. uint8_t bLength;
  215. uint8_t bDescriptorType;
  216. uint8_t bInterfaceNumber;
  217. uint8_t bAlternateSetting;
  218. uint8_t bNumEndpoints;
  219. uint8_t bInterfaceClass;
  220. uint8_t bInterfaceSubClass;
  221. uint8_t bInterfaceProtocol;
  222. uint8_t iInterface;
  223. } usb_descriptor_interface_t;
  224. typedef struct _usb_descriptor_endpoint
  225. {
  226. uint8_t bLength;
  227. uint8_t bDescriptorType;
  228. uint8_t bEndpointAddress;
  229. uint8_t bmAttributes;
  230. uint8_t wMaxPacketSize[2];
  231. uint8_t bInterval;
  232. } usb_descriptor_endpoint_t;
  233. typedef struct _usb_descriptor_binary_device_object_store
  234. {
  235. uint8_t bLength; /* Descriptor size in bytes = 5U */
  236. uint8_t bDescriptorType; /* BOS Descriptor type = 0FU*/
  237. uint8_t wTotalLength[2]; /*Length of this descriptor and all of its sub descriptors*/
  238. uint8_t bNumDeviceCaps; /*The number of separate device capability descriptors in the BOS*/
  239. } usb_descriptor_bos_t;
  240. typedef struct _usb_descriptor_usb20_extension
  241. {
  242. uint8_t bLength; /* Descriptor size in bytes = 7U */
  243. uint8_t bDescriptorType; /* DEVICE CAPABILITY Descriptor type = 0x10U*/
  244. uint8_t bDevCapabilityType; /*Length of this descriptor and all of its sub descriptors*/
  245. uint8_t bmAttributes[4]; /*Bitmap encoding of supported device level features.*/
  246. } usb_descriptor_usb20_extension_t;
  247. typedef union _usb_descriptor_union
  248. {
  249. usb_descriptor_common_t common; /* Common descriptor */
  250. usb_descriptor_device_t device; /* Device descriptor */
  251. usb_descriptor_configuration_t configuration; /* Configuration descriptor */
  252. usb_descriptor_interface_t interface; /* Interface descriptor */
  253. usb_descriptor_endpoint_t endpoint; /* Endpoint descriptor */
  254. } usb_descriptor_union_t;
  255. #endif /* __USB_SPEC_H__ */