1 /*
2  * This file holds USB constants and structures that are needed for USB
3  * device APIs.  These are used by the USB device model, which is defined
4  * in chapter 9 of the USB 2.0 specification.  Linux has several APIs in C
5  * that need these:
6  *
7  * - the master/host side Linux-USB kernel driver API;
8  * - the "usbfs" user space API; and
9  * - the Linux "gadget" slave/device/peripheral side driver API.
10  *
11  * USB 2.0 adds an additional "On The Go" (OTG) mode, which lets systems
12  * act either as a USB master/host or as a USB slave/device.  That means
13  * the master and slave side APIs benefit from working well together.
14  *
15  * There's also "Wireless USB", using low power short range radios for
16  * peripheral interconnection but otherwise building on the USB framework.
17  */
18 
19 
20 #ifndef _USB_CH9_H_
21 #define _USB_CH9_H_
22 
23 #include "basic_types.h"
24 //#include <linux/types.h>	/* __u8 etc */
25 //#include "../otg/osk/sys-support.h"
26 
27 /*-------------------------------------------------------------------------*/
28 
29 /* CONTROL REQUEST SUPPORT */
30 
31 /*
32  * USB directions
33  *
34  * This bit flag is used in endpoint descriptors' bEndpointAddress field.
35  * It's also one of three fields in control requests bRequestType.
36  */
37 //#define USB_DIR_OUT			0		/* to device */
38 //#define USB_DIR_IN			0x80		/* to host */
39 
40 /*
41  * USB types, the second of three bRequestType fields
42  */
43 #define USB_TYPE_MASK			(0x03 << 5)
44 #define USB_TYPE_STANDARD		(0x00 << 5)
45 #define USB_TYPE_CLASS			(0x01 << 5)
46 #define USB_TYPE_VENDOR			(0x02 << 5)
47 #define USB_TYPE_RESERVED		(0x03 << 5)
48 
49 /*
50  * USB recipients, the third of three bRequestType fields
51  */
52 #define USB_RECIP_MASK			0x1f
53 #define USB_RECIP_DEVICE		0x00
54 #define USB_RECIP_INTERFACE		0x01
55 #define USB_RECIP_ENDPOINT		0x02
56 #define USB_RECIP_OTHER			0x03
57 /* From Wireless USB 1.0 */
58 #define USB_RECIP_PORT 			0x04
59 #define USB_RECIP_RPIPE 		0x05
60 
61 /*
62  * Standard requests, for the bRequest field of a SETUP packet.
63  *
64  * These are qualified by the bRequestType field, so that for example
65  * TYPE_CLASS or TYPE_VENDOR specific feature flags could be retrieved
66  * by a GET_STATUS request.
67  */
68 #define USB_REQ_GET_STATUS		0x00
69 #define USB_REQ_CLEAR_FEATURE		0x01
70 #define USB_REQ_SET_FEATURE		0x03
71 #define USB_REQ_SET_ADDRESS		0x05
72 #define USB_REQ_GET_DESCRIPTOR		0x06
73 #define USB_REQ_SET_DESCRIPTOR		0x07
74 #define USB_REQ_GET_CONFIGURATION	0x08
75 #define USB_REQ_SET_CONFIGURATION	0x09
76 #define USB_REQ_GET_INTERFACE		0x0A
77 #define USB_REQ_SET_INTERFACE		0x0B
78 #define USB_REQ_SYNCH_FRAME		0x0C
79 
80 #define USB_REQ_SET_ENCRYPTION		0x0D	/* Wireless USB */
81 #define USB_REQ_GET_ENCRYPTION		0x0E
82 #define USB_REQ_RPIPE_ABORT		0x0E
83 #define USB_REQ_SET_HANDSHAKE		0x0F
84 #define USB_REQ_RPIPE_RESET		0x0F
85 #define USB_REQ_GET_HANDSHAKE		0x10
86 #define USB_REQ_SET_CONNECTION		0x11
87 #define USB_REQ_SET_SECURITY_DATA	0x12
88 #define USB_REQ_GET_SECURITY_DATA	0x13
89 #define USB_REQ_SET_WUSB_DATA		0x14
90 #define USB_REQ_LOOPBACK_DATA_WRITE	0x15
91 #define USB_REQ_LOOPBACK_DATA_READ	0x16
92 #define USB_REQ_SET_INTERFACE_DS	0x17
93 
94 /*
95  * USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and
96  * are read as a bit array returned by USB_REQ_GET_STATUS.  (So there
97  * are at most sixteen features of each type.)
98  */
99 #define USB_DEVICE_SELF_POWERED		0	/* (read only) */
100 #define USB_DEVICE_REMOTE_WAKEUP	1	/* dev may initiate wakeup */
101 #define USB_DEVICE_TEST_MODE		2	/* (wired high speed only) */
102 #define USB_DEVICE_BATTERY		2	/* (wireless) */
103 #define USB_DEVICE_B_HNP_ENABLE		3	/* (otg) dev may initiate HNP */
104 #define USB_DEVICE_WUSB_DEVICE		3	/* (wireless)*/
105 #define USB_DEVICE_A_HNP_SUPPORT	4	/* (otg) RH port supports HNP */
106 #define USB_DEVICE_A_ALT_HNP_SUPPORT	5	/* (otg) other RH port does */
107 #define USB_DEVICE_DEBUG_MODE		6	/* (special devices only) */
108 
109 #define USB_ENDPOINT_HALT		0	/* IN/OUT will STALL */
110 
111 
112 /**
113  * struct usb_ctrlrequest - SETUP data for a USB device control request
114  * @bRequestType: matches the USB bmRequestType field
115  * @bRequest: matches the USB bRequest field
116  * @wValue: matches the USB wValue field (le16 byte order)
117  * @wIndex: matches the USB wIndex field (le16 byte order)
118  * @wLength: matches the USB wLength field (le16 byte order)
119  *
120  * This structure is used to send control requests to a USB device.  It matches
121  * the different fields of the USB 2.0 Spec section 9.3, table 9-2.  See the
122  * USB spec for a fuller description of the different fields, and what they are
123  * used for.
124  *
125  * Note that the driver for any interface can issue control requests.
126  * For most devices, interfaces don't coordinate with each other, so
127  * such requests may be made at any time.
128  */
129 struct usb_ctrlrequest {
130 	u8 bRequestType;
131 	u8 bRequest;
132 	u16 wValue;
133 	u16 wIndex;
134 	u16 wLength;
135 };
136 
137 /*-------------------------------------------------------------------------*/
138 
139 /*
140  * STANDARD DESCRIPTORS ... as returned by GET_DESCRIPTOR, or
141  * (rarely) accepted by SET_DESCRIPTOR.
142  *
143  * Note that all multi-byte values here are encoded in little endian
144  * byte order "on the wire".  But when exposed through Linux-USB APIs,
145  * they've been converted to cpu byte order.
146  */
147 
148 /*
149  * Descriptor types ... USB 2.0 spec table 9.5
150  */
151 #define USB_DT_DEVICE			0x01
152 #define USB_DT_CONFIG			0x02
153 #define USB_DT_STRING			0x03
154 #define USB_DT_INTERFACE		0x04
155 #define USB_DT_ENDPOINT			0x05
156 #define USB_DT_DEVICE_QUALIFIER		0x06
157 #define USB_DT_OTHER_SPEED_CONFIG	0x07
158 #define USB_DT_INTERFACE_POWER		0x08
159 /* these are from a minor usb 2.0 revision (ECN) */
160 #define USB_DT_OTG			0x09
161 #define USB_DT_DEBUG			0x0a
162 #define USB_DT_INTERFACE_ASSOCIATION	0x0b
163 /* these are from the Wireless USB spec */
164 #define USB_DT_SECURITY			0x0c
165 #define USB_DT_KEY			0x0d
166 #define USB_DT_ENCRYPTION_TYPE		0x0e
167 #define USB_DT_BOS			0x0f
168 #define USB_DT_DEVICE_CAPABILITY	0x10
169 #define USB_DT_WIRELESS_ENDPOINT_COMP	0x11
170 #define USB_DT_WIRE_ADAPTER		0x21
171 #define USB_DT_RPIPE			0x22
172 
173 /* conventional codes for class-specific descriptors */
174 #define USB_DT_CS_DEVICE		0x21
175 #define USB_DT_CS_CONFIG		0x22
176 #define USB_DT_CS_STRING		0x23
177 #define USB_DT_CS_INTERFACE		0x24
178 #define USB_DT_CS_ENDPOINT		0x25
179 
180 /* All standard descriptors have these 2 fields at the beginning */
181 struct usb_descriptor_header {
182 	u8  bLength;
183 	u8  bDescriptorType;
184 };
185 
186 
187 /*-------------------------------------------------------------------------*/
188 
189 /* USB_DT_DEVICE: Device descriptor */
190 struct usb_device_descriptor {
191 	u8  bLength;
192 	u8  bDescriptorType;
193 
194 	u16 bcdUSB;
195 	u8  bDeviceClass;
196 	u8  bDeviceSubClass;
197 	u8  bDeviceProtocol;
198 	u8  bMaxPacketSize0;
199 	u16 idVendor;
200 	u16 idProduct;
201 	u16 bcdDevice;
202 	u8  iManufacturer;
203 	u8  iProduct;
204 	u8  iSerialNumber;
205 	u8  bNumConfigurations;
206 };
207 
208 #define USB_DT_DEVICE_SIZE		18
209 
210 
211 /*
212  * Device and/or Interface Class codes
213  * as found in bDeviceClass or bInterfaceClass
214  * and defined by www.usb.org documents
215  */
216 #define USB_CLASS_PER_INTERFACE		0	/* for DeviceClass */
217 #define USB_CLASS_AUDIO			1
218 #define USB_CLASS_COMM			2
219 #define USB_CLASS_HID			3
220 #define USB_CLASS_PHYSICAL		5
221 #define USB_CLASS_STILL_IMAGE		6
222 #define USB_CLASS_PRINTER		7
223 #define USB_CLASS_MASS_STORAGE		8
224 #define USB_CLASS_HUB			9
225 #define USB_CLASS_CDC_DATA		0x0a
226 #define USB_CLASS_CSCID			0x0b	/* chip+ smart card */
227 #define USB_CLASS_CONTENT_SEC		0x0d	/* content security */
228 #define USB_CLASS_VIDEO			0x0e
229 #define USB_CLASS_WIRELESS_CONTROLLER	0xe0
230 #define USB_CLASS_APP_SPEC		0xfe
231 #define USB_CLASS_VENDOR_SPEC		0xff
232 
233 /*-------------------------------------------------------------------------*/
234 
235 /* USB_DT_CONFIG: Configuration descriptor information.
236  *
237  * USB_DT_OTHER_SPEED_CONFIG is the same descriptor, except that the
238  * descriptor type is different.  Highspeed-capable devices can look
239  * different depending on what speed they're currently running.  Only
240  * devices with a USB_DT_DEVICE_QUALIFIER have any OTHER_SPEED_CONFIG
241  * descriptors.
242  */
243 struct usb_config_descriptor {
244 	u8  bLength;
245 	u8  bDescriptorType;
246 
247 	u16 wTotalLength;
248 	u8  bNumInterfaces;
249 	u8  bConfigurationValue;
250 	u8  iConfiguration;
251 	u8  bmAttributes;
252 	u8  bMaxPower;
253 };
254 
255 #define USB_DT_CONFIG_SIZE		9
256 
257 /* from config descriptor bmAttributes */
258 #define USB_CONFIG_ATT_ONE		(1 << 7)	/* must be set */
259 #define USB_CONFIG_ATT_SELFPOWER	(1 << 6)	/* self powered */
260 #define USB_CONFIG_ATT_WAKEUP		(1 << 5)	/* can wakeup */
261 #define USB_CONFIG_ATT_BATTERY		(1 << 4)	/* battery powered */
262 
263 /*-------------------------------------------------------------------------*/
264 
265 /* USB_DT_STRING: String descriptor */
266 struct usb_string_descriptor {
267 	u8  bLength;
268 	u8  bDescriptorType;
269 
270 	u16 wData[1];		/* UTF-16LE encoded */
271 };
272 
273 /* note that "string" zero is special, it holds language codes that
274  * the device supports, not Unicode characters.
275  */
276 
277 /*-------------------------------------------------------------------------*/
278 
279 /* USB_DT_INTERFACE: Interface descriptor */
280 struct usb_interface_descriptor {
281 	u8  bLength;
282 	u8  bDescriptorType;
283 
284 	u8  bInterfaceNumber;
285 	u8  bAlternateSetting;
286 	u8  bNumEndpoints;
287 	u8  bInterfaceClass;
288 	u8  bInterfaceSubClass;
289 	u8  bInterfaceProtocol;
290 	u8  iInterface;
291 };
292 
293 #define USB_DT_INTERFACE_SIZE		9
294 
295 /*-------------------------------------------------------------------------*/
296 
297 /* Endpoint descriptor */
298 struct usb_endpoint_descriptor {
299 	u8  bLength;
300 	u8  bDescriptorType;
301 	u8  bEndpointAddress;
302 	u8  bmAttributes;
303 	u16 wMaxPacketSize;
304 	u8  bInterval;
305 	u8  bRefresh;
306 	u8  bSynchAddress;
307 
308    	unsigned char *extra;   /* Extra descriptors */
309 	int extralen;
310 };
311 
312 #define USB_DT_ENDPOINT_SIZE		7
313 #define USB_DT_ENDPOINT_AUDIO_SIZE	9	/* Audio extension */
314 
315 
316 /*
317  * Endpoints
318  */
319 #if 0
320 #define USB_ENDPOINT_NUMBER_MASK	0x0f	/* in bEndpointAddress */
321 #define USB_ENDPOINT_DIR_MASK		0x80
322 
323 #define USB_ENDPOINT_XFERTYPE_MASK	0x03	/* in bmAttributes */
324 #define USB_ENDPOINT_XFER_CONTROL	0
325 #define USB_ENDPOINT_XFER_ISOC		1
326 #define USB_ENDPOINT_XFER_BULK		2
327 #define USB_ENDPOINT_XFER_INT		3
328 #define USB_ENDPOINT_MAX_ADJUSTABLE	0x80
329 #endif
330 
331 /*-------------------------------------------------------------------------*/
332 
333 /* USB_DT_DEVICE_QUALIFIER: Device Qualifier descriptor */
334 struct usb_qualifier_descriptor {
335 	u8  bLength;
336 	u8  bDescriptorType;
337 
338 	u16 bcdUSB;
339 	u8  bDeviceClass;
340 	u8  bDeviceSubClass;
341 	u8  bDeviceProtocol;
342 	u8  bMaxPacketSize0;
343 	u8  bNumConfigurations;
344 	u8  bRESERVED;
345 };
346 
347 
348 /*-------------------------------------------------------------------------*/
349 
350 /* USB_DT_OTG (from OTG 1.0a supplement) */
351 struct usb_otg_descriptor {
352 	u8  bLength;
353 	u8  bDescriptorType;
354 
355 	u8  bmAttributes;	/* support for HNP, SRP, etc */
356 };
357 
358 /* from usb_otg_descriptor.bmAttributes */
359 #define USB_OTG_SRP		(1 << 0)
360 #define USB_OTG_HNP		(1 << 1)	/* swap host/device roles */
361 
362 /*-------------------------------------------------------------------------*/
363 
364 /* USB_DT_DEBUG:  for special highspeed devices, replacing serial console */
365 struct usb_debug_descriptor {
366 	u8  bLength;
367 	u8  bDescriptorType;
368 
369 	/* bulk endpoints with 8 byte maxpacket */
370 	u8  bDebugInEndpoint;
371 	u8  bDebugOutEndpoint;
372 };
373 
374 /*-------------------------------------------------------------------------*/
375 
376 /* USB_DT_INTERFACE_ASSOCIATION: groups interfaces */
377 struct usb_interface_assoc_descriptor {
378 	u8  bLength;
379 	u8  bDescriptorType;
380 
381 	u8  bFirstInterface;
382 	u8  bInterfaceCount;
383 	u8  bFunctionClass;
384 	u8  bFunctionSubClass;
385 	u8  bFunctionProtocol;
386 	u8  iFunction;
387 };
388 
389 
390 /*-------------------------------------------------------------------------*/
391 
392 /* USB_DT_SECURITY:  group of wireless security descriptors, including
393  * encryption types available for setting up a CC/association.
394  */
395 struct usb_security_descriptor {
396 	u8  bLength;
397 	u8  bDescriptorType;
398 
399 	u16 wTotalLength;
400 	u8  bNumEncryptionTypes;
401 };
402 
403 /*-------------------------------------------------------------------------*/
404 
405 /* USB_DT_KEY:  used with {GET,SET}_SECURITY_DATA; only public keys
406  * may be retrieved.
407  */
408 struct usb_key_descriptor {
409 	u8  bLength;
410 	u8  bDescriptorType;
411 
412 	u8  tTKID[3];
413 	u8  bReserved;
414 	u8  bKeyData[0];
415 };
416 
417 /*-------------------------------------------------------------------------*/
418 
419 /* USB_DT_ENCRYPTION_TYPE:  bundled in DT_SECURITY groups */
420 struct usb_encryption_descriptor {
421 	u8  bLength;
422 	u8  bDescriptorType;
423 
424 	u8  bEncryptionType;
425 #define	USB_ENC_TYPE_UNSECURE		0
426 #define	USB_ENC_TYPE_WIRED		1	/* non-wireless mode */
427 #define	USB_ENC_TYPE_CCM_1		2	/* aes128/cbc session */
428 #define	USB_ENC_TYPE_RSA_1		3	/* rsa3072/sha1 auth */
429 	u8  bEncryptionValue;		/* use in SET_ENCRYPTION */
430 	u8  bAuthKeyIndex;
431 };
432 
433 
434 /*-------------------------------------------------------------------------*/
435 
436 /* USB_DT_BOS:  group of wireless capabilities */
437 struct usb_bos_descriptor {
438 	u8  bLength;
439 	u8  bDescriptorType;
440 
441 	u16 wTotalLength;
442 	u8  bNumDeviceCaps;
443 };
444 
445 /*-------------------------------------------------------------------------*/
446 
447 /* USB_DT_DEVICE_CAPABILITY:  grouped with BOS */
448 struct usb_dev_cap_header {
449 	u8  bLength;
450 	u8  bDescriptorType;
451 	u8  bDevCapabilityType;
452 };
453 
454 #define	USB_CAP_TYPE_WIRELESS_USB	1
455 
456 struct usb_wireless_cap_descriptor {	/* Ultra Wide Band */
457 	u8  bLength;
458 	u8  bDescriptorType;
459 	u8  bDevCapabilityType;
460 
461 	u8  bmAttributes;
462 #define	USB_WIRELESS_P2P_DRD		(1 << 1)
463 #define	USB_WIRELESS_BEACON_MASK	(3 << 2)
464 #define	USB_WIRELESS_BEACON_SELF	(1 << 2)
465 #define	USB_WIRELESS_BEACON_DIRECTED	(2 << 2)
466 #define	USB_WIRELESS_BEACON_NONE	(3 << 2)
467 	u16 wPHYRates;	/* bit rates, Mbps */
468 #define	USB_WIRELESS_PHY_53		(1 << 0)	/* always set */
469 #define	USB_WIRELESS_PHY_80		(1 << 1)
470 #define	USB_WIRELESS_PHY_107		(1 << 2)	/* always set */
471 #define	USB_WIRELESS_PHY_160		(1 << 3)
472 #define	USB_WIRELESS_PHY_200		(1 << 4)	/* always set */
473 #define	USB_WIRELESS_PHY_320		(1 << 5)
474 #define	USB_WIRELESS_PHY_400		(1 << 6)
475 #define	USB_WIRELESS_PHY_480		(1 << 7)
476 	u8  bmTFITXPowerInfo;	/* TFI power levels */
477 	u8  bmFFITXPowerInfo;	/* FFI power levels */
478 	u16 bmBandGroup;
479 	u8  bReserved;
480 };
481 
482 /*-------------------------------------------------------------------------*/
483 
484 /* USB_DT_WIRELESS_ENDPOINT_COMP:  companion descriptor associated with
485  * each endpoint descriptor for a wireless device
486  */
487 struct usb_wireless_ep_comp_descriptor {
488 	u8  bLength;
489 	u8  bDescriptorType;
490 
491 	u8  bMaxBurst;
492 	u8  bMaxSequence;
493 	u16 wMaxStreamDelay;
494 	u16 wOverTheAirPacketSize;
495 	u8  bOverTheAirInterval;
496 	u8  bmCompAttributes;
497 #define USB_ENDPOINT_SWITCH_MASK	0x03	/* in bmCompAttributes */
498 #define USB_ENDPOINT_SWITCH_NO		0
499 #define USB_ENDPOINT_SWITCH_SWITCH	1
500 #define USB_ENDPOINT_SWITCH_SCALE	2
501 };
502 
503 /*-------------------------------------------------------------------------*/
504 
505 /* USB_REQ_SET_HANDSHAKE is a four-way handshake used between a wireless
506  * host and a device for connection set up, mutual authentication, and
507  * exchanging short lived session keys.  The handshake depends on a CC.
508  */
509 struct usb_handshake {
510 	u8 bMessageNumber;
511 	u8 bStatus;
512 	u8 tTKID[3];
513 	u8 bReserved;
514 	u8 CDID[16];
515 	u8 nonce[16];
516 	u8 MIC[8];
517 };
518 
519 
520 /*-------------------------------------------------------------------------*/
521 
522 /* USB_REQ_SET_CONNECTION modifies or revokes a connection context (CC).
523  * A CC may also be set up using non-wireless secure channels (including
524  * wired USB!), and some devices may support CCs with multiple hosts.
525  */
526 struct usb_connection_context {
527 	u8 CHID[16];		/* persistent host id */
528 	u8 CDID[16];		/* device id (unique w/in host context) */
529 	u8 CK[16];		/* connection key */
530 };
531 
532 /*-------------------------------------------------------------------------*/
533 
534 #if 1
535 
536 
537 
538 
539 enum usb_device_state {
540 	/* NOTATTACHED isn't in the USB spec, and this state acts
541 	 * the same as ATTACHED ... but it's clearer this way.
542 	 */
543 	USB_STATE_NOTATTACHED = 0,
544 
545 	/* chapter 9 and authentication (wireless) device states */
546 	USB_STATE_ATTACHED,
547 	USB_STATE_POWERED,			/* wired */
548 	USB_STATE_UNAUTHENTICATED,		/* auth */
549 	USB_STATE_RECONNECTING,			/* auth */
550 	USB_STATE_DEFAULT,			/* limited function */
551 	USB_STATE_ADDRESS,
552 	USB_STATE_CONFIGURED,			/* most functions */
553 
554 	USB_STATE_SUSPENDED
555 
556 	/* NOTE:  there are actually four different SUSPENDED
557 	 * states, returning to POWERED, DEFAULT, ADDRESS, or
558 	 * CONFIGURED respectively when SOF tokens flow again.
559 	 */
560 };
561 #endif
562 #endif	/* __LINUX_USB_CH9_H */
563 
564