1 /*
2  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __USB_SPEC_H__
10 #define __USB_SPEC_H__
11 
12 /*******************************************************************************
13  * Definitions
14  ******************************************************************************/
15 
16 /* USB speed (the value cannot be changed because EHCI QH use the value directly)*/
17 #define USB_SPEED_FULL (0x00U)
18 #define USB_SPEED_LOW (0x01U)
19 #define USB_SPEED_HIGH (0x02U)
20 #define USB_SPEED_SUPER (0x04U)
21 
22 /* Set up packet structure */
23 typedef struct _usb_setup_struct
24 {
25     uint8_t bmRequestType;
26     uint8_t bRequest;
27     uint16_t wValue;
28     uint16_t wIndex;
29     uint16_t wLength;
30 } usb_setup_struct_t;
31 
32 /* USB  standard descriptor endpoint type */
33 #define USB_ENDPOINT_CONTROL (0x00U)
34 #define USB_ENDPOINT_ISOCHRONOUS (0x01U)
35 #define USB_ENDPOINT_BULK (0x02U)
36 #define USB_ENDPOINT_INTERRUPT (0x03U)
37 
38 /* USB  standard descriptor transfer direction (cannot change the value because iTD use the value directly) */
39 #define USB_OUT (0U)
40 #define USB_IN (1U)
41 
42 /* USB standard descriptor length */
43 #define USB_DESCRIPTOR_LENGTH_DEVICE (0x12U)
44 #define USB_DESCRIPTOR_LENGTH_CONFIGURE (0x09U)
45 #define USB_DESCRIPTOR_LENGTH_INTERFACE (0x09U)
46 #define USB_DESCRIPTOR_LENGTH_ENDPOINT (0x07U)
47 #define USB_DESCRIPTOR_LENGTH_ENDPOINT_COMPANION (0x06U)
48 #define USB_DESCRIPTOR_LENGTH_DEVICE_QUALITIER (0x0AU)
49 #define USB_DESCRIPTOR_LENGTH_OTG_DESCRIPTOR (5U)
50 #define USB_DESCRIPTOR_LENGTH_BOS_DESCRIPTOR (5U)
51 #define USB_DESCRIPTOR_LENGTH_DEVICE_CAPABILITY_USB20_EXTENSION (0x07U)
52 #define USB_DESCRIPTOR_LENGTH_DEVICE_CAPABILITY_SUPERSPEED (0x0AU)
53 
54 /* USB Device Capability Type Codes */
55 #define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY_WIRELESS (0x01U)
56 #define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY_USB20_EXTENSION (0x02U)
57 #define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY_SUPERSPEED (0x03U)
58 
59 /* USB standard descriptor type */
60 #define USB_DESCRIPTOR_TYPE_DEVICE (0x01U)
61 #define USB_DESCRIPTOR_TYPE_CONFIGURE (0x02U)
62 #define USB_DESCRIPTOR_TYPE_STRING (0x03U)
63 #define USB_DESCRIPTOR_TYPE_INTERFACE (0x04U)
64 #define USB_DESCRIPTOR_TYPE_ENDPOINT (0x05U)
65 #define USB_DESCRIPTOR_TYPE_DEVICE_QUALITIER (0x06U)
66 #define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION (0x07U)
67 #define USB_DESCRIPTOR_TYPE_INTERFAACE_POWER (0x08U)
68 #define USB_DESCRIPTOR_TYPE_OTG (0x09U)
69 #define USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION (0x0BU)
70 #define USB_DESCRIPTOR_TYPE_BOS (0x0F)
71 #define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY (0x10)
72 
73 #define USB_DESCRIPTOR_TYPE_HID (0x21U)
74 #define USB_DESCRIPTOR_TYPE_HID_REPORT (0x22U)
75 #define USB_DESCRIPTOR_TYPE_HID_PHYSICAL (0x23U)
76 
77 #define USB_DESCRIPTOR_TYPE_ENDPOINT_COMPANION (0x30U)
78 
79 /* USB standard request type */
80 #define USB_REQUEST_TYPE_DIR_MASK (0x80U)
81 #define USB_REQUEST_TYPE_DIR_SHIFT (7U)
82 #define USB_REQUEST_TYPE_DIR_OUT (0x00U)
83 #define USB_REQUEST_TYPE_DIR_IN (0x80U)
84 
85 #define USB_REQUEST_TYPE_TYPE_MASK (0x60U)
86 #define USB_REQUEST_TYPE_TYPE_SHIFT (5U)
87 #define USB_REQUEST_TYPE_TYPE_STANDARD (0U)
88 #define USB_REQUEST_TYPE_TYPE_CLASS (0x20U)
89 #define USB_REQUEST_TYPE_TYPE_VENDOR (0x40U)
90 
91 #define USB_REQUEST_TYPE_RECIPIENT_MASK (0x1FU)
92 #define USB_REQUEST_TYPE_RECIPIENT_SHIFT (0U)
93 #define USB_REQUEST_TYPE_RECIPIENT_DEVICE (0x00U)
94 #define USB_REQUEST_TYPE_RECIPIENT_INTERFACE (0x01U)
95 #define USB_REQUEST_TYPE_RECIPIENT_ENDPOINT (0x02U)
96 #define USB_REQUEST_TYPE_RECIPIENT_OTHER (0x03U)
97 
98 /* USB standard request */
99 #define USB_REQUEST_STANDARD_GET_STATUS (0x00U)
100 #define USB_REQUEST_STANDARD_CLEAR_FEATURE (0x01U)
101 #define USB_REQUEST_STANDARD_SET_FEATURE (0x03U)
102 #define USB_REQUEST_STANDARD_SET_ADDRESS (0x05U)
103 #define USB_REQUEST_STANDARD_GET_DESCRIPTOR (0x06U)
104 #define USB_REQUEST_STANDARD_SET_DESCRIPTOR (0x07U)
105 #define USB_REQUEST_STANDARD_GET_CONFIGURATION (0x08U)
106 #define USB_REQUEST_STANDARD_SET_CONFIGURATION (0x09U)
107 #define USB_REQUEST_STANDARD_GET_INTERFACE (0x0AU)
108 #define USB_REQUEST_STANDARD_SET_INTERFACE (0x0BU)
109 #define USB_REQUEST_STANDARD_SYNCH_FRAME (0x0CU)
110 
111 /* USB standard request GET Status */
112 #define USB_REQUEST_STANDARD_GET_STATUS_DEVICE_SELF_POWERED_SHIFT (0U)
113 #define USB_REQUEST_STANDARD_GET_STATUS_DEVICE_REMOTE_WARKUP_SHIFT (1U)
114 
115 #define USB_REQUEST_STANDARD_GET_STATUS_ENDPOINT_HALT_MASK (0x01U)
116 #define USB_REQUEST_STANDARD_GET_STATUS_ENDPOINT_HALT_SHIFT (0U)
117 
118 #define USB_REQUEST_STANDARD_GET_STATUS_OTG_STATUS_SELECTOR (0xF000U)
119 
120 /* USB standard request CLEAR/SET feature */
121 #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_ENDPOINT_HALT (0U)
122 #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_DEVICE_REMOTE_WAKEUP (1U)
123 #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_DEVICE_TEST_MODE (2U)
124 #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_B_HNP_ENABLE (3U)
125 #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_A_HNP_SUPPORT (4U)
126 #define USB_REQUEST_STANDARD_FEATURE_SELECTOR_A_ALT_HNP_SUPPORT (5U)
127 
128 /* USB standard descriptor configure bmAttributes */
129 #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_D7_MASK (0x80U)
130 #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_D7_SHIFT (7U)
131 
132 #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_SELF_POWERED_MASK (0x40U)
133 #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_SELF_POWERED_SHIFT (6U)
134 
135 #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_REMOTE_WAKEUP_MASK (0x20U)
136 #define USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_REMOTE_WAKEUP_SHIFT (5U)
137 
138 /* USB standard descriptor endpoint bmAttributes */
139 #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK (0x80U)
140 #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT (7U)
141 #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT (0U)
142 #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN (0x80U)
143 
144 #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_MASK (0x0FU)
145 #define USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_SHFIT (0U)
146 
147 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_TYPE_MASK (0x03U)
148 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_NUMBER_SHFIT (0U)
149 
150 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_MASK (0x0CU)
151 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_SHFIT (2U)
152 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_NO_SYNC (0x00U)
153 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_ASYNC (0x04U)
154 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_ADAPTIVE (0x08U)
155 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_SYNC_TYPE_SYNC (0x0CU)
156 
157 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_USAGE_TYPE_MASK (0x30U)
158 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_USAGE_TYPE_SHFIT (4U)
159 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_USAGE_TYPE_DATA_ENDPOINT (0x00U)
160 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_USAGE_TYPE_FEEDBACK_ENDPOINT (0x10U)
161 #define USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_USAGE_TYPE_IMPLICIT_FEEDBACK_DATA_ENDPOINT (0x20U)
162 
163 #define USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_SIZE_MASK (0x07FFu)
164 #define USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_MULT_TRANSACTIONS_MASK (0x1800u)
165 #define USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_MULT_TRANSACTIONS_SHFIT (11U)
166 
167 /* USB standard descriptor otg bmAttributes */
168 #define USB_DESCRIPTOR_OTG_ATTRIBUTES_SRP_MASK (0x01u)
169 #define USB_DESCRIPTOR_OTG_ATTRIBUTES_HNP_MASK (0x02u)
170 #define USB_DESCRIPTOR_OTG_ATTRIBUTES_ADP_MASK (0x04u)
171 
172 /* USB standard descriptor device capability usb20 extension bmAttributes */
173 #define USB_DESCRIPTOR_DEVICE_CAPABILITY_USB20_EXTENSION_LPM_MASK (0x02U)
174 #define USB_DESCRIPTOR_DEVICE_CAPABILITY_USB20_EXTENSION_LPM_SHIFT (1U)
175 #define USB_DESCRIPTOR_DEVICE_CAPABILITY_USB20_EXTENSION_BESL_MASK (0x04U)
176 #define USB_DESCRIPTOR_DEVICE_CAPABILITY_USB20_EXTENSION_BESL_SHIFT (2U)
177 
178 /* Language structure */
179 typedef struct _usb_language
180 {
181     uint8_t **string;    /* The Strings descriptor array */
182     uint32_t *length;    /* The strings descriptor length array */
183     uint16_t languageId; /* The language id of current language */
184 } usb_language_t;
185 
186 typedef struct _usb_language_list
187 {
188     uint8_t *languageString;      /* The String 0U pointer */
189     uint32_t stringLength;        /* The String 0U Length */
190     usb_language_t *languageList; /* The language list */
191     uint8_t count;                /* The language count */
192 } usb_language_list_t;
193 
194 typedef struct _usb_descriptor_common
195 {
196     uint8_t bLength;         /* Size of this descriptor in bytes */
197     uint8_t bDescriptorType; /* DEVICE Descriptor Type */
198     uint8_t bData[1];        /* Data */
199 } usb_descriptor_common_t;
200 
201 typedef struct _usb_descriptor_device
202 {
203     uint8_t bLength;            /* Size of this descriptor in bytes */
204     uint8_t bDescriptorType;    /* DEVICE Descriptor Type */
205     uint8_t bcdUSB[2];          /* UUSB Specification Release Number in Binary-Coded Decimal, e.g. 0x0200U */
206     uint8_t bDeviceClass;       /* Class code */
207     uint8_t bDeviceSubClass;    /* Sub-Class code */
208     uint8_t bDeviceProtocol;    /* Protocol code */
209     uint8_t bMaxPacketSize0;    /* Maximum packet size for endpoint zero */
210     uint8_t idVendor[2];        /* Vendor ID (assigned by the USB-IF) */
211     uint8_t idProduct[2];       /* Product ID (assigned by the manufacturer) */
212     uint8_t bcdDevice[2];       /* Device release number in binary-coded decimal */
213     uint8_t iManufacturer;      /* Index of string descriptor describing manufacturer */
214     uint8_t iProduct;           /* Index of string descriptor describing product */
215     uint8_t iSerialNumber;      /* Index of string descriptor describing the device serial number */
216     uint8_t bNumConfigurations; /* Number of possible configurations */
217 } usb_descriptor_device_t;
218 
219 typedef struct _usb_descriptor_configuration
220 {
221     uint8_t bLength;             /* Descriptor size in bytes = 9U */
222     uint8_t bDescriptorType;     /* CONFIGURATION type = 2U or 7U */
223     uint8_t wTotalLength[2];     /* Length of concatenated descriptors */
224     uint8_t bNumInterfaces;      /* Number of interfaces, this configuration. */
225     uint8_t bConfigurationValue; /* Value to set this configuration. */
226     uint8_t iConfiguration;      /* Index to configuration string */
227     uint8_t bmAttributes;        /* Configuration characteristics */
228     uint8_t bMaxPower;           /* Maximum power from bus, 2 mA units */
229 } usb_descriptor_configuration_t;
230 
231 typedef struct _usb_descriptor_interface
232 {
233     uint8_t bLength;
234     uint8_t bDescriptorType;
235     uint8_t bInterfaceNumber;
236     uint8_t bAlternateSetting;
237     uint8_t bNumEndpoints;
238     uint8_t bInterfaceClass;
239     uint8_t bInterfaceSubClass;
240     uint8_t bInterfaceProtocol;
241     uint8_t iInterface;
242 } usb_descriptor_interface_t;
243 
244 typedef struct _usb_descriptor_endpoint
245 {
246     uint8_t bLength;
247     uint8_t bDescriptorType;
248     uint8_t bEndpointAddress;
249     uint8_t bmAttributes;
250     uint8_t wMaxPacketSize[2];
251     uint8_t bInterval;
252 } usb_descriptor_endpoint_t;
253 
254 typedef struct _usb_descriptor_endpoint_companion
255 {
256     uint8_t bLength;
257     uint8_t bDescriptorType;
258     uint8_t bMaxBurst;
259     uint8_t bmAttributes;
260     uint8_t wBytesPerInterval[2];
261 } usb_descriptor_endpoint_companion_t;
262 
263 typedef struct _usb_descriptor_binary_device_object_store
264 {
265     uint8_t bLength;         /* Descriptor size in bytes = 5U */
266     uint8_t bDescriptorType; /* BOS Descriptor type = 0FU*/
267     uint8_t wTotalLength[2]; /*Length of this descriptor and all of its sub descriptors*/
268     uint8_t bNumDeviceCaps;  /*The number of separate device capability descriptors in the BOS*/
269 } usb_descriptor_bos_t;
270 
271 typedef struct _usb_descriptor_usb20_extension
272 {
273     uint8_t bLength;            /* Descriptor size in bytes = 7U */
274     uint8_t bDescriptorType;    /* DEVICE CAPABILITY Descriptor type = 0x10U*/
275     uint8_t bDevCapabilityType; /*Length of this descriptor and all of its sub descriptors*/
276     uint8_t bmAttributes[4];    /*Bitmap encoding of supported device level features.*/
277 } usb_descriptor_usb20_extension_t;
278 typedef struct _usb_descriptor_super_speed_device_capability
279 {
280     uint8_t bLength;
281     uint8_t bDescriptorType;
282     uint8_t bDevCapabilityType;
283     uint8_t bmAttributes;
284     uint8_t wSpeedsSupported[2];
285     uint8_t bFunctionalitySupport;
286     uint8_t bU1DevExitLat;
287     uint8_t wU2DevExitLat[2];
288 } usb_bos_device_capability_susperspeed_desc_t;
289 typedef union _usb_descriptor_union
290 {
291     usb_descriptor_common_t common;                        /* Common descriptor */
292     usb_descriptor_device_t device;                        /* Device descriptor */
293     usb_descriptor_configuration_t configuration;          /* Configuration descriptor */
294     usb_descriptor_interface_t interface;                  /* Interface descriptor */
295     usb_descriptor_endpoint_t endpoint;                    /* Endpoint descriptor */
296     usb_descriptor_endpoint_companion_t endpointCompanion; /* Endpoint companion descriptor */
297 } usb_descriptor_union_t;
298 
299 #endif /* __USB_SPEC_H__ */
300