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