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