1 /*---------------------------------------------------------------------------*
2   Project:     USB Host Stack (UHS)
3   File:        uhs_ch9.h
4   Description: USB constants and structures that are needed for public
5                USB device APIs.  These are used by the USB device model, which
6                is defined in chapter 9 of the USB 2.0 specification.
7 
8   Copyright (C) Nintendo.  All rights reserved.
9 
10   These coded instructions, statements, and computer programs contain
11   proprietary information of Nintendo of America Inc. and/or Nintendo
12   Company Ltd., and are protected by Federal copyright law.  They may
13   not be disclosed to third parties or copied or duplicated in any form,
14   in whole or in part, without the prior written consent of Nintendo.
15 
16   *---------------------------------------------------------------------------*/
17 #ifndef __UHS_CH9_H__
18 #define __UHS_CH9_H__
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23     PACKED_STRUCT_BEGIN
24 
25 
26 /*
27  *
28  * CONTROL REQUESTS
29  *
30  */
31 
32 /* USB directions, used in endpoint descriptor bEndpointAddress field
33    and in control requests bRequestType. */
34 #define UHS_DIR_OUT         0       /* to device */
35 #define UHS_DIR_IN          0x80    /* to host */
36 
37 /*  USB types, the second of three bRequestType fields */
38 #define UHS_TYPE_MASK               (0x03 << 5)
39 #define UHS_TYPE_STANDARD           (0x00 << 5)
40 #define UHS_TYPE_CLASS              (0x01 << 5)
41 #define UHS_TYPE_VENDOR             (0x02 << 5)
42 #define UHS_TYPE_RESERVED           (0x03 << 5)
43 
44 /* USB recipients, the third of three bRequestType fields */
45 #define UHS_RECIP_MASK              0x1f
46 #define UHS_RECIP_DEVICE            0x00
47 #define UHS_RECIP_INTERFACE         0x01
48 #define UHS_RECIP_ENDPOINT          0x02
49 #define UHS_RECIP_OTHER             0x03
50 #define UHS_RECIP_PORT              0x04
51 #define UHS_RECIP_RPIPE             0x05
52 
53 /* Standard requests, for the bRequest field of a SETUP packet. */
54 #define UHS_REQ_GET_STATUS          0x00
55 #define UHS_REQ_CLEAR_FEATURE       0x01
56 #define UHS_REQ_SET_FEATURE         0x03
57 #define UHS_REQ_SET_ADDRESS         0x05
58 #define UHS_REQ_GET_DESCRIPTOR      0x06
59 #define UHS_REQ_SET_DESCRIPTOR      0x07
60 #define UHS_REQ_GET_CONFIGURATION   0x08
61 #define UHS_REQ_SET_CONFIGURATION   0x09
62 #define UHS_REQ_GET_INTERFACE       0x0A
63 #define UHS_REQ_SET_INTERFACE       0x0B
64 #define UHS_REQ_SYNCH_FRAME         0x0C
65 #define UHS_REQ_SET_ENCRYPTION      0x0D
66 #define UHS_REQ_GET_ENCRYPTION      0x0E
67 #define UHS_REQ_RPIPE_ABORT         0x0E
68 #define UHS_REQ_SET_HANDSHAKE       0x0F
69 #define UHS_REQ_RPIPE_RESET         0x0F
70 #define UHS_REQ_GET_HANDSHAKE       0x10
71 #define UHS_REQ_SET_CONNECTION      0x11
72 #define UHS_REQ_SET_SECURITY_DATA   0x12
73 #define UHS_REQ_GET_SECURITY_DATA   0x13
74 #define UHS_REQ_SET_WUHS_DATA       0x14
75 #define UHS_REQ_LOOPBACK_DATA_WRITE 0x15
76 #define UHS_REQ_LOOPBACK_DATA_READ  0x16
77 #define UHS_REQ_SET_INTERFACE_DS    0x17
78 
79 
80 /**
81  *
82  * FEATURE SELECTORS
83  *
84  */
85 #define UHS_DEVICE_SELF_POWERED         0   /* (read only) */
86 #define UHS_DEVICE_REMOTE_WAKEUP        1   /* dev may initiate wakeup */
87 #define UHS_DEVICE_TEST_MODE            2   /* (wired high speed only) */
88 #define UHS_DEVICE_BATTERY              2   /* (wireless) */
89 #define UHS_DEVICE_B_HNP_ENABLE         3   /* (otg) dev may initiate HNP */
90 #define UHS_DEVICE_WUHS_DEVICE          3   /* (wireless)*/
91 #define UHS_DEVICE_A_HNP_SUPPORT        4   /* (otg) RH port supports HNP */
92 #define UHS_DEVICE_A_ALT_HNP_SUPPORT    5   /* (otg) other RH port does */
93 #define UHS_DEVICE_DEBUG_MODE           6   /* (special devices only) */
94 #define UHS_DEVICE_U1_ENABLE            48  /* dev may initiate U1 transition */
95 #define UHS_DEVICE_U2_ENABLE            49  /* dev may initiate U2 transition */
96 #define UHS_DEVICE_LTM_ENABLE           50  /* dev may send LTM */
97 #define UHS_INTRF_FUNC_SUSPEND          0   /* function suspend */
98 #define UHS_INTR_FUNC_SUSPEND_OPT_MASK  0xFF00
99 #define UHS_ENDPOINT_HALT               0   /* IN/OUT will STALL */
100 
101 /* SETUP data for a USB device control request */
102     typedef struct {
103         u8  bRequestType;
104         u8  bRequest;
105         u16 wValue;
106         u16 wIndex;
107         u16 wLength;
108     } PACKED_STRUCT_ATTRIBUTE UhsCtrlRequest;
109 
110 /*
111  *
112  * STANDARD DESCRIPTORS
113  * Note that all multi-byte values are encoded in little endian byte order,
114  * as they appear on the bus.
115  *
116  */
117 
118 /* Descriptor types, USB 2.0 spec table 9.5 */
119 #define UHS_DT_DEVICE                   0x01
120 #define UHS_DT_CONFIG                   0x02
121 #define UHS_DT_STRING                   0x03
122 #define UHS_DT_INTERFACE                0x04
123 #define UHS_DT_ENDPOINT                 0x05
124 #define UHS_DT_DEVICE_QUALIFIER         0x06
125 #define UHS_DT_OTHER_SPEED_CONFIG       0x07
126 #define UHS_DT_INTERFACE_POWER          0x08
127 /* these are from a minor USB 2.0 revision (ECN) */
128 #define UHS_DT_OTG                      0x09
129 #define UHS_DT_DEBUG                    0x0a
130 #define UHS_DT_INTERFACE_ASSOCIATION    0x0b
131 /* these are from the Wireless USB spec */
132 #define UHS_DT_SECURITY                 0x0c
133 #define UHS_DT_KEY                      0x0d
134 #define UHS_DT_ENCRYPTION_TYPE          0x0e
135 #define UHS_DT_BOS                      0x0f
136 #define UHS_DT_DEVICE_CAPABILITY        0x10
137 #define UHS_DT_WIRELESS_ENDPOINT_COMP   0x11
138 #define UHS_DT_WIRE_ADAPTER             0x21
139 #define UHS_DT_RPIPE                    0x22
140 #define UHS_DT_CS_RADIO_CONTROL         0x23
141 
142 /* Conventional codes for class-specific descriptors.  The convention is
143  * defined in the UHS "Common Class" Spec (3.11).  Individual class specs
144  * are authoritative for their usage, not the "common class" writeup.
145  */
146 #define UHS_DT_CS_DEVICE        (UHS_TYPE_CLASS | UHS_DT_DEVICE)
147 #define UHS_DT_CS_CONFIG        (UHS_TYPE_CLASS | UHS_DT_CONFIG)
148 #define UHS_DT_CS_STRING        (UHS_TYPE_CLASS | UHS_DT_STRING)
149 #define UHS_DT_CS_INTERFACE     (UHS_TYPE_CLASS | UHS_DT_INTERFACE)
150 #define UHS_DT_CS_ENDPOINT      (UHS_TYPE_CLASS | UHS_DT_ENDPOINT)
151 
152 /* All standard descriptors have these 2 fields at the beginning */
153     typedef struct {
154         u8  bLength;
155         u8  bDescriptorType;
156     } PACKED_STRUCT_ATTRIBUTE UhsDescriptorHeader;
157 #define UHS_DT_HEADER_SIZE 2
158 
159 /* Device descriptor, UHS_DT_DEVICE */
160     typedef struct {
161         u8  bLength;
162         u8  bDescriptorType;
163         u16 bcdUSB;
164         u8  bDeviceClass;
165         u8  bDeviceSubClass;
166         u8  bDeviceProtocol;
167         u8  bMaxPacketSize0;
168         u16 idVendor;
169         u16 idProduct;
170         u16 bcdDevice;
171         u8  iManufacturer;
172         u8  iProduct;
173         u8  iSerialNumber;
174         u8  bNumConfigurations;
175     } PACKED_STRUCT_ATTRIBUTE UhsDeviceDescriptor;
176 #define UHS_DT_DEVICE_SIZE      18
177 
178 /* Device and/or Interface Class codes in bDeviceClass or bInterfaceClass */
179 #define UHS_CLASS_PER_INTERFACE         0   /* for DeviceClass */
180 #define UHS_CLASS_AUDIO                 1
181 #define UHS_CLASS_COMM                  2
182 #define UHS_CLASS_HID                   3
183 #define UHS_CLASS_PHYSICAL              5
184 #define UHS_CLASS_STILL_IMAGE           6
185 #define UHS_CLASS_PRINTER               7
186 #define UHS_CLASS_MASS_STORAGE          8
187 #define UHS_CLASS_HUB                   9
188 #define UHS_CLASS_CDC_DATA              0x0a
189 #define UHS_CLASS_CSCID                 0x0b  /* chip+ smart card */
190 #define UHS_CLASS_CONTENT_SEC           0x0d  /* content security */
191 #define UHS_CLASS_VIDEO                 0x0e
192 #define UHS_CLASS_WIRELESS_CONTROLLER   0xe0
193 #define UHS_CLASS_MISC                  0xef
194 #define UHS_CLASS_APP_SPEC              0xfe
195 #define UHS_CLASS_VENDOR_SPEC           0xff
196 #define UHS_SUBCLASS_VENDOR_SPEC        0xff
197 
198 /* Configuration descriptor, UHS_DT_CONFIG */
199     typedef struct {
200         u8  bLength;
201         u8  bDescriptorType;
202         u16 wTotalLength;
203         u8  bNumInterfaces;
204         u8  bConfigurationValue;
205         u8  iConfiguration;
206         u8  bmAttributes;
207         u8  bMaxPower;
208     } PACKED_STRUCT_ATTRIBUTE UhsConfigDescriptor;
209 
210 #define UHS_DT_CONFIG_SIZE      9
211 
212 /* from config descriptor bmAttributes */
213 #define UHS_CONFIG_ATT_ONE          (1 << 7)    /* must be set */
214 #define UHS_CONFIG_ATT_SELFPOWER    (1 << 6)    /* self powered */
215 #define UHS_CONFIG_ATT_WAKEUP       (1 << 5)    /* can wake up */
216 #define UHS_CONFIG_ATT_BATTERY      (1 << 4)    /* battery powered */
217 
218 /* String descriptor, UHS_DT_STRING */
219     typedef struct {
220         u8  bLength;
221         u8  bDescriptorType;
222         u16 wData[1];        /* UTF-16LE encoded */
223     } PACKED_STRUCT_ATTRIBUTE UhsStringDescriptor;
224 
225 /* Interface descriptor, UHS_DT_INTERFACE */
226     typedef struct {
227         u8  bLength;
228         u8  bDescriptorType;
229         u8  bInterfaceNumber;
230         u8  bAlternateSetting;
231         u8  bNumEndpoints;
232         u8  bInterfaceClass;
233         u8  bInterfaceSubClass;
234         u8  bInterfaceProtocol;
235         u8  iInterface;
236     } PACKED_STRUCT_ATTRIBUTE UhsInterfaceDescriptor;
237 #define UHS_DT_INTERFACE_SIZE       9
238 
239 /* Endpoint descriptor, UHS_DT_ENDPOINT */
240     typedef struct {
241         u8  bLength;
242         u8  bDescriptorType;
243         u8  bEndpointAddress;
244         u8  bmAttributes;
245         u16 wMaxPacketSize;
246         u8  bInterval;
247 
248         /* NOTE:  these two are _only_ in audio endpoints.
249            use UHS_DT_ENDPOINT_AUDIO_SIZE in bLength, not sizeof. */
250         u8  bRefresh;
251         u8  bSynchAddress;
252     } PACKED_STRUCT_ATTRIBUTE UhsEndpointDescriptor;
253 
254 #define UHS_DT_ENDPOINT_SIZE        7
255 #define UHS_DT_ENDPOINT_AUDIO_SIZE  9   /* Audio extension */
256 
257 
258 /* Endpoints */
259 #define UHS_ENDPOINT_NUMBER_MAX     15   /* bEndpointAddress */
260 #define UHS_ENDPOINT_NUMBER_SHIFT   0
261 #define UHS_ENDPOINT_NUMBER_MASK    (0x0f<<UHS_ENDPOINT_NUMBER_SHIFT)
262 #define UHS_ENDPOINT_DIR_SHIFT      7
263 #define UHS_ENDPOINT_DIR_MASK       (1<<UHS_ENDPOINT_DIR_SHIFT)
264 #define UHS_ENDPOINT_DIR_IN         1
265 #define UHS_ENDPOINT_DIR_OUT        0
266 
267 #define UHS_ENDPOINT_SYNCTYPE       0x0c
268 #define UHS_ENDPOINT_SYNC_NONE      (0 << 2)
269 #define UHS_ENDPOINT_SYNC_ASYNC     (1 << 2)
270 #define UHS_ENDPOINT_SYNC_ADAPTIVE  (2 << 2)
271 #define UHS_ENDPOINT_SYNC_SYNC      (3 << 2)
272 
273 
274 #define UHS_ENDPOINT_XFERTYPE_SHIFT 0     /* bmAttributes */
275 #define UHS_ENDPOINT_XFERTYPE_MASK  (0x03<<UHS_ENDPOINT_XFERTYPE_SHIFT)
276 #define UHS_ENDPOINT_XFER_CONTROL   0
277 #define UHS_ENDPOINT_XFER_ISOC      1
278 #define UHS_ENDPOINT_XFER_BULK      2
279 #define UHS_ENDPOINT_XFER_INT       3
280 #define UHS_ENDPOINT_MAX_ADJUSTABLE 0x80
281 
282 #define UHS_ENDPOINT_TXN_PER_UFRAME_SHIFT 11   /* wMaxPacketSize */
283 #define UHS_ENDPOINT_TXN_PER_UFRAME_MASK  (0x3<< UHS_ENDPOINT_TXN_PER_UFRAME_SHIFT)
284 #define UHS_ENDPOINT_MAX_PAYLOAD_SHIFT    0
285 #define UHS_ENDPOINT_MAX_PAYLOAD_MASK     (0x7ff<<UHS_ENDPOINT_MAX_PAYLOAD_SHIFT)
286 
287 
288 
289 /* USB on-the-go, UHS_DT_OTG */
290     typedef struct {
291         u8  bLength;
292         u8  bDescriptorType;
293         u8  bmAttributes;     /* support for HNP, SRP, etc. */
294     } PACKED_STRUCT_ATTRIBUTE UhsOtgDescriptor;
295 
296     /* from usb_otg_descriptor.bmAttributes */
297 #define UHS_OTG_SRP             (1 << 0)
298 #define UHS_OTG_HNP             (1 << 1)
299 
300 
301     /* UHS_DT_DEBUG  */
302     typedef struct {
303         u8  bLength;
304         u8  bDescriptorType;
305 
306         /* bulk endpoints with 8-byte maxpacket */
307         u8  bDebugInEndpoint;
308         u8  bDebugOutEndpoint;
309     } PACKED_STRUCT_ATTRIBUTE UhsDebugDescriptor;
310 
311     /* Multiple interfaces performing related function, UHS_DT_INTERFACE_ASSOCIATION  */
312     typedef struct {
313         u8  bLength;
314         u8  bDescriptorType;
315 
316         u8  bFirstInterface;
317         u8  bInterfaceCount;
318         u8  bFunctionClass;
319         u8  bFunctionSubClass;
320         u8  bFunctionProtocol;
321         u8  iFunction;
322     } PACKED_STRUCT_ATTRIBUTE UhsInterfaceAssociationDescriptor;
323 
324 
325 
326 
327     PACKED_STRUCT_END
328 
329 #ifdef __cplusplus
330 }
331 #endif
332 
333 #endif // __UHS_CH9_H__
334 
335 
336 
337 
338 
339 
340 
341 
342 
343