1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  * This file holds USB constants and structures that are needed for
4  * USB device APIs.  These are used by the USB device model, which is
5  * defined in chapter 9 of the USB 2.0 specification and in the
6  * Wireless USB 1.0 spec (now defunct).  Linux has several APIs in C that
7  * need these:
8  *
9  * - the master/host side Linux-USB kernel driver API;
10  * - the "usbfs" user space API; and
11  * - the Linux "gadget" slave/device/peripheral side driver API.
12  *
13  * USB 2.0 adds an additional "On The Go" (OTG) mode, which lets systems
14  * act either as a USB master/host or as a USB slave/device.  That means
15  * the master and slave side APIs benefit from working well together.
16  *
17  * Note all descriptors are declared '__attribute__((packed))' so that:
18  *
19  * [a] they never get padded, either internally (USB spec writers
20  *     probably handled that) or externally;
21  *
22  * [b] so that accessing bigger-than-a-bytes fields will never
23  *     generate bus errors on any platform, even when the location of
24  *     its descriptor inside a bundle isn't "naturally aligned", and
25  *
26  * [c] for consistency, removing all doubt even when it appears to
27  *     someone that the two other points are non-issues for that
28  *     particular descriptor type.
29  */
30 
31 #ifndef _UAPI__LINUX_USB_CH9_H
32 #define _UAPI__LINUX_USB_CH9_H
33 
34 #include <linux/types.h>	/* __u8 etc */
35 #include <asm/byteorder.h>	/* le16_to_cpu */
36 
37 /*-------------------------------------------------------------------------*/
38 
39 /* CONTROL REQUEST SUPPORT */
40 
41 /*
42  * USB directions
43  *
44  * This bit flag is used in endpoint descriptors' bEndpointAddress field.
45  * It's also one of three fields in control requests bRequestType.
46  */
47 #define USB_DIR_OUT			0		/* to device */
48 #define USB_DIR_IN			0x80		/* to host */
49 
50 /*
51  * USB types, the second of three bRequestType fields
52  */
53 #define USB_TYPE_MASK			(0x03 << 5)
54 #define USB_TYPE_STANDARD		(0x00 << 5)
55 #define USB_TYPE_CLASS			(0x01 << 5)
56 #define USB_TYPE_VENDOR			(0x02 << 5)
57 #define USB_TYPE_RESERVED		(0x03 << 5)
58 
59 /*
60  * USB recipients, the third of three bRequestType fields
61  */
62 #define USB_RECIP_MASK			0x1f
63 #define USB_RECIP_DEVICE		0x00
64 #define USB_RECIP_INTERFACE		0x01
65 #define USB_RECIP_ENDPOINT		0x02
66 #define USB_RECIP_OTHER			0x03
67 /* From Wireless USB 1.0 */
68 #define USB_RECIP_PORT			0x04
69 #define USB_RECIP_RPIPE		0x05
70 
71 /*
72  * Standard requests, for the bRequest field of a SETUP packet.
73  *
74  * These are qualified by the bRequestType field, so that for example
75  * TYPE_CLASS or TYPE_VENDOR specific feature flags could be retrieved
76  * by a GET_STATUS request.
77  */
78 #define USB_REQ_GET_STATUS		0x00
79 #define USB_REQ_CLEAR_FEATURE		0x01
80 #define USB_REQ_SET_FEATURE		0x03
81 #define USB_REQ_SET_ADDRESS		0x05
82 #define USB_REQ_GET_DESCRIPTOR		0x06
83 #define USB_REQ_SET_DESCRIPTOR		0x07
84 #define USB_REQ_GET_CONFIGURATION	0x08
85 #define USB_REQ_SET_CONFIGURATION	0x09
86 #define USB_REQ_GET_INTERFACE		0x0A
87 #define USB_REQ_SET_INTERFACE		0x0B
88 #define USB_REQ_SYNCH_FRAME		0x0C
89 #define USB_REQ_SET_SEL			0x30
90 #define USB_REQ_SET_ISOCH_DELAY		0x31
91 
92 #define USB_REQ_SET_ENCRYPTION		0x0D	/* Wireless USB */
93 #define USB_REQ_GET_ENCRYPTION		0x0E
94 #define USB_REQ_RPIPE_ABORT		0x0E
95 #define USB_REQ_SET_HANDSHAKE		0x0F
96 #define USB_REQ_RPIPE_RESET		0x0F
97 #define USB_REQ_GET_HANDSHAKE		0x10
98 #define USB_REQ_SET_CONNECTION		0x11
99 #define USB_REQ_SET_SECURITY_DATA	0x12
100 #define USB_REQ_GET_SECURITY_DATA	0x13
101 #define USB_REQ_SET_WUSB_DATA		0x14
102 #define USB_REQ_LOOPBACK_DATA_WRITE	0x15
103 #define USB_REQ_LOOPBACK_DATA_READ	0x16
104 #define USB_REQ_SET_INTERFACE_DS	0x17
105 
106 /* specific requests for USB Power Delivery */
107 #define USB_REQ_GET_PARTNER_PDO		20
108 #define USB_REQ_GET_BATTERY_STATUS	21
109 #define USB_REQ_SET_PDO			22
110 #define USB_REQ_GET_VDM			23
111 #define USB_REQ_SEND_VDM		24
112 
113 /* The Link Power Management (LPM) ECN defines USB_REQ_TEST_AND_SET command,
114  * used by hubs to put ports into a new L1 suspend state, except that it
115  * forgot to define its number ...
116  */
117 
118 /*
119  * USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and
120  * are read as a bit array returned by USB_REQ_GET_STATUS.  (So there
121  * are at most sixteen features of each type.)  Hubs may also support a
122  * new USB_REQ_TEST_AND_SET_FEATURE to put ports into L1 suspend.
123  */
124 #define USB_DEVICE_SELF_POWERED		0	/* (read only) */
125 #define USB_DEVICE_REMOTE_WAKEUP	1	/* dev may initiate wakeup */
126 #define USB_DEVICE_TEST_MODE		2	/* (wired high speed only) */
127 #define USB_DEVICE_BATTERY		2	/* (wireless) */
128 #define USB_DEVICE_B_HNP_ENABLE		3	/* (otg) dev may initiate HNP */
129 #define USB_DEVICE_WUSB_DEVICE		3	/* (wireless)*/
130 #define USB_DEVICE_A_HNP_SUPPORT	4	/* (otg) RH port supports HNP */
131 #define USB_DEVICE_A_ALT_HNP_SUPPORT	5	/* (otg) other RH port does */
132 #define USB_DEVICE_DEBUG_MODE		6	/* (special devices only) */
133 
134 /*
135  * Test Mode Selectors
136  * See USB 2.0 spec Table 9-7
137  */
138 #define	USB_TEST_J		1
139 #define	USB_TEST_K		2
140 #define	USB_TEST_SE0_NAK	3
141 #define	USB_TEST_PACKET		4
142 #define	USB_TEST_FORCE_ENABLE	5
143 
144 /* Status Type */
145 #define USB_STATUS_TYPE_STANDARD	0
146 #define USB_STATUS_TYPE_PTM		1
147 
148 /*
149  * New Feature Selectors as added by USB 3.0
150  * See USB 3.0 spec Table 9-7
151  */
152 #define USB_DEVICE_U1_ENABLE	48	/* dev may initiate U1 transition */
153 #define USB_DEVICE_U2_ENABLE	49	/* dev may initiate U2 transition */
154 #define USB_DEVICE_LTM_ENABLE	50	/* dev may send LTM */
155 #define USB_INTRF_FUNC_SUSPEND	0	/* function suspend */
156 
157 #define USB_INTR_FUNC_SUSPEND_OPT_MASK	0xFF00
158 /*
159  * Suspend Options, Table 9-8 USB 3.0 spec
160  */
161 #define USB_INTRF_FUNC_SUSPEND_LP	(1 << (8 + 0))
162 #define USB_INTRF_FUNC_SUSPEND_RW	(1 << (8 + 1))
163 
164 /*
165  * Interface status, Figure 9-5 USB 3.0 spec
166  */
167 #define USB_INTRF_STAT_FUNC_RW_CAP     1
168 #define USB_INTRF_STAT_FUNC_RW         2
169 
170 #define USB_ENDPOINT_HALT		0	/* IN/OUT will STALL */
171 
172 /* Bit array elements as returned by the USB_REQ_GET_STATUS request. */
173 #define USB_DEV_STAT_U1_ENABLED		2	/* transition into U1 state */
174 #define USB_DEV_STAT_U2_ENABLED		3	/* transition into U2 state */
175 #define USB_DEV_STAT_LTM_ENABLED	4	/* Latency tolerance messages */
176 
177 /*
178  * Feature selectors from Table 9-8 USB Power Delivery spec
179  */
180 #define USB_DEVICE_BATTERY_WAKE_MASK	40
181 #define USB_DEVICE_OS_IS_PD_AWARE	41
182 #define USB_DEVICE_POLICY_MODE		42
183 #define USB_PORT_PR_SWAP		43
184 #define USB_PORT_GOTO_MIN		44
185 #define USB_PORT_RETURN_POWER		45
186 #define USB_PORT_ACCEPT_PD_REQUEST	46
187 #define USB_PORT_REJECT_PD_REQUEST	47
188 #define USB_PORT_PORT_PD_RESET		48
189 #define USB_PORT_C_PORT_PD_CHANGE	49
190 #define USB_PORT_CABLE_PD_RESET		50
191 #define USB_DEVICE_CHARGING_POLICY	54
192 
193 /**
194  * struct usb_ctrlrequest - SETUP data for a USB device control request
195  * @bRequestType: matches the USB bmRequestType field
196  * @bRequest: matches the USB bRequest field
197  * @wValue: matches the USB wValue field (le16 byte order)
198  * @wIndex: matches the USB wIndex field (le16 byte order)
199  * @wLength: matches the USB wLength field (le16 byte order)
200  *
201  * This structure is used to send control requests to a USB device.  It matches
202  * the different fields of the USB 2.0 Spec section 9.3, table 9-2.  See the
203  * USB spec for a fuller description of the different fields, and what they are
204  * used for.
205  *
206  * Note that the driver for any interface can issue control requests.
207  * For most devices, interfaces don't coordinate with each other, so
208  * such requests may be made at any time.
209  */
210 struct usb_ctrlrequest {
211 	__u8 bRequestType;
212 	__u8 bRequest;
213 	__le16 wValue;
214 	__le16 wIndex;
215 	__le16 wLength;
216 } __attribute__ ((packed));
217 
218 /*-------------------------------------------------------------------------*/
219 
220 /*
221  * STANDARD DESCRIPTORS ... as returned by GET_DESCRIPTOR, or
222  * (rarely) accepted by SET_DESCRIPTOR.
223  *
224  * Note that all multi-byte values here are encoded in little endian
225  * byte order "on the wire".  Within the kernel and when exposed
226  * through the Linux-USB APIs, they are not converted to cpu byte
227  * order; it is the responsibility of the client code to do this.
228  * The single exception is when device and configuration descriptors (but
229  * not other descriptors) are read from character devices
230  * (i.e. /dev/bus/usb/BBB/DDD);
231  * in this case the fields are converted to host endianness by the kernel.
232  */
233 
234 /*
235  * Descriptor types ... USB 2.0 spec table 9.5
236  */
237 #define USB_DT_DEVICE			0x01
238 #define USB_DT_CONFIG			0x02
239 #define USB_DT_STRING			0x03
240 #define USB_DT_INTERFACE		0x04
241 #define USB_DT_ENDPOINT			0x05
242 #define USB_DT_DEVICE_QUALIFIER		0x06
243 #define USB_DT_OTHER_SPEED_CONFIG	0x07
244 #define USB_DT_INTERFACE_POWER		0x08
245 /* these are from a minor usb 2.0 revision (ECN) */
246 #define USB_DT_OTG			0x09
247 #define USB_DT_DEBUG			0x0a
248 #define USB_DT_INTERFACE_ASSOCIATION	0x0b
249 /* these are from the Wireless USB spec */
250 #define USB_DT_SECURITY			0x0c
251 #define USB_DT_KEY			0x0d
252 #define USB_DT_ENCRYPTION_TYPE		0x0e
253 #define USB_DT_BOS			0x0f
254 #define USB_DT_DEVICE_CAPABILITY	0x10
255 #define USB_DT_WIRELESS_ENDPOINT_COMP	0x11
256 /* From the eUSB2 spec */
257 #define USB_DT_EUSB2_ISOC_ENDPOINT_COMP	0x12
258 /* From Wireless USB spec */
259 #define USB_DT_WIRE_ADAPTER		0x21
260 /* From USB Device Firmware Upgrade Specification, Revision 1.1 */
261 #define USB_DT_DFU_FUNCTIONAL		0x21
262 /* these are from the Wireless USB spec */
263 #define USB_DT_RPIPE			0x22
264 #define USB_DT_CS_RADIO_CONTROL		0x23
265 /* From the T10 UAS specification */
266 #define USB_DT_PIPE_USAGE		0x24
267 /* From the USB 3.0 spec */
268 #define	USB_DT_SS_ENDPOINT_COMP		0x30
269 /* From the USB 3.1 spec */
270 #define	USB_DT_SSP_ISOC_ENDPOINT_COMP	0x31
271 
272 /* Conventional codes for class-specific descriptors.  The convention is
273  * defined in the USB "Common Class" Spec (3.11).  Individual class specs
274  * are authoritative for their usage, not the "common class" writeup.
275  */
276 #define USB_DT_CS_DEVICE		(USB_TYPE_CLASS | USB_DT_DEVICE)
277 #define USB_DT_CS_CONFIG		(USB_TYPE_CLASS | USB_DT_CONFIG)
278 #define USB_DT_CS_STRING		(USB_TYPE_CLASS | USB_DT_STRING)
279 #define USB_DT_CS_INTERFACE		(USB_TYPE_CLASS | USB_DT_INTERFACE)
280 #define USB_DT_CS_ENDPOINT		(USB_TYPE_CLASS | USB_DT_ENDPOINT)
281 
282 /* All standard descriptors have these 2 fields at the beginning */
283 struct usb_descriptor_header {
284 	__u8  bLength;
285 	__u8  bDescriptorType;
286 } __attribute__ ((packed));
287 
288 
289 /*-------------------------------------------------------------------------*/
290 
291 /* USB_DT_DEVICE: Device descriptor */
292 struct usb_device_descriptor {
293 	__u8  bLength;
294 	__u8  bDescriptorType;
295 
296 	__le16 bcdUSB;
297 	__u8  bDeviceClass;
298 	__u8  bDeviceSubClass;
299 	__u8  bDeviceProtocol;
300 	__u8  bMaxPacketSize0;
301 	__le16 idVendor;
302 	__le16 idProduct;
303 	__le16 bcdDevice;
304 	__u8  iManufacturer;
305 	__u8  iProduct;
306 	__u8  iSerialNumber;
307 	__u8  bNumConfigurations;
308 } __attribute__ ((packed));
309 
310 #define USB_DT_DEVICE_SIZE		18
311 
312 
313 /*
314  * Device and/or Interface Class codes
315  * as found in bDeviceClass or bInterfaceClass
316  * and defined by www.usb.org documents
317  */
318 #define USB_CLASS_PER_INTERFACE		0	/* for DeviceClass */
319 #define USB_CLASS_AUDIO			1
320 #define USB_CLASS_COMM			2
321 #define USB_CLASS_HID			3
322 #define USB_CLASS_PHYSICAL		5
323 #define USB_CLASS_STILL_IMAGE		6
324 #define USB_CLASS_PRINTER		7
325 #define USB_CLASS_MASS_STORAGE		8
326 #define USB_CLASS_HUB			9
327 #define USB_CLASS_CDC_DATA		0x0a
328 #define USB_CLASS_CSCID			0x0b	/* chip+ smart card */
329 #define USB_CLASS_CONTENT_SEC		0x0d	/* content security */
330 #define USB_CLASS_VIDEO			0x0e
331 #define USB_CLASS_WIRELESS_CONTROLLER	0xe0
332 #define USB_CLASS_PERSONAL_HEALTHCARE	0x0f
333 #define USB_CLASS_AUDIO_VIDEO		0x10
334 #define USB_CLASS_BILLBOARD		0x11
335 #define USB_CLASS_USB_TYPE_C_BRIDGE	0x12
336 #define USB_CLASS_MCTP			0x14
337 #define USB_CLASS_MISC			0xef
338 #define USB_CLASS_APP_SPEC		0xfe
339 #define USB_SUBCLASS_DFU			0x01
340 
341 #define USB_CLASS_VENDOR_SPEC		0xff
342 #define USB_SUBCLASS_VENDOR_SPEC		0xff
343 
344 /*-------------------------------------------------------------------------*/
345 
346 /* USB_DT_CONFIG: Configuration descriptor information.
347  *
348  * USB_DT_OTHER_SPEED_CONFIG is the same descriptor, except that the
349  * descriptor type is different.  Highspeed-capable devices can look
350  * different depending on what speed they're currently running.  Only
351  * devices with a USB_DT_DEVICE_QUALIFIER have any OTHER_SPEED_CONFIG
352  * descriptors.
353  */
354 struct usb_config_descriptor {
355 	__u8  bLength;
356 	__u8  bDescriptorType;
357 
358 	__le16 wTotalLength;
359 	__u8  bNumInterfaces;
360 	__u8  bConfigurationValue;
361 	__u8  iConfiguration;
362 	__u8  bmAttributes;
363 	__u8  bMaxPower;
364 } __attribute__ ((packed));
365 
366 #define USB_DT_CONFIG_SIZE		9
367 
368 /* from config descriptor bmAttributes */
369 #define USB_CONFIG_ATT_ONE		(1 << 7)	/* must be set */
370 #define USB_CONFIG_ATT_SELFPOWER	(1 << 6)	/* self powered */
371 #define USB_CONFIG_ATT_WAKEUP		(1 << 5)	/* can wakeup */
372 #define USB_CONFIG_ATT_BATTERY		(1 << 4)	/* battery powered */
373 
374 /*-------------------------------------------------------------------------*/
375 
376 /* USB String descriptors can contain at most 126 characters. */
377 #define USB_MAX_STRING_LEN	126
378 
379 /* USB_DT_STRING: String descriptor */
380 struct usb_string_descriptor {
381 	__u8  bLength;
382 	__u8  bDescriptorType;
383 
384 	union {
385 		__le16 legacy_padding;
386 		__DECLARE_FLEX_ARRAY(__le16, wData);	/* UTF-16LE encoded */
387 	};
388 } __attribute__ ((packed));
389 
390 /* note that "string" zero is special, it holds language codes that
391  * the device supports, not Unicode characters.
392  */
393 
394 /*-------------------------------------------------------------------------*/
395 
396 /* USB_DT_INTERFACE: Interface descriptor */
397 struct usb_interface_descriptor {
398 	__u8  bLength;
399 	__u8  bDescriptorType;
400 
401 	__u8  bInterfaceNumber;
402 	__u8  bAlternateSetting;
403 	__u8  bNumEndpoints;
404 	__u8  bInterfaceClass;
405 	__u8  bInterfaceSubClass;
406 	__u8  bInterfaceProtocol;
407 	__u8  iInterface;
408 } __attribute__ ((packed));
409 
410 #define USB_DT_INTERFACE_SIZE		9
411 
412 /*-------------------------------------------------------------------------*/
413 
414 /* USB_DT_ENDPOINT: Endpoint descriptor */
415 struct usb_endpoint_descriptor {
416 	__u8  bLength;
417 	__u8  bDescriptorType;
418 
419 	__u8  bEndpointAddress;
420 	__u8  bmAttributes;
421 	__le16 wMaxPacketSize;
422 	__u8  bInterval;
423 
424 	/* NOTE:  these two are _only_ in audio endpoints. */
425 	/* use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof. */
426 	__u8  bRefresh;
427 	__u8  bSynchAddress;
428 } __attribute__ ((packed));
429 
430 #define USB_DT_ENDPOINT_SIZE		7
431 #define USB_DT_ENDPOINT_AUDIO_SIZE	9	/* Audio extension */
432 
433 
434 /*
435  * Endpoints
436  */
437 #define USB_ENDPOINT_NUMBER_MASK	0x0f	/* in bEndpointAddress */
438 #define USB_ENDPOINT_DIR_MASK		0x80
439 
440 #define USB_ENDPOINT_XFERTYPE_MASK	0x03	/* in bmAttributes */
441 #define USB_ENDPOINT_XFER_CONTROL	0
442 #define USB_ENDPOINT_XFER_ISOC		1
443 #define USB_ENDPOINT_XFER_BULK		2
444 #define USB_ENDPOINT_XFER_INT		3
445 #define USB_ENDPOINT_MAX_ADJUSTABLE	0x80
446 
447 #define USB_ENDPOINT_MAXP_MASK	0x07ff
448 #define USB_EP_MAXP_MULT_SHIFT	11
449 #define USB_EP_MAXP_MULT_MASK	(3 << USB_EP_MAXP_MULT_SHIFT)
450 #define USB_EP_MAXP_MULT(m) \
451 	(((m) & USB_EP_MAXP_MULT_MASK) >> USB_EP_MAXP_MULT_SHIFT)
452 
453 /* The USB 3.0 spec redefines bits 5:4 of bmAttributes as interrupt ep type. */
454 #define USB_ENDPOINT_INTRTYPE		0x30
455 #define USB_ENDPOINT_INTR_PERIODIC	(0 << 4)
456 #define USB_ENDPOINT_INTR_NOTIFICATION	(1 << 4)
457 
458 #define USB_ENDPOINT_SYNCTYPE		0x0c
459 #define USB_ENDPOINT_SYNC_NONE		(0 << 2)
460 #define USB_ENDPOINT_SYNC_ASYNC		(1 << 2)
461 #define USB_ENDPOINT_SYNC_ADAPTIVE	(2 << 2)
462 #define USB_ENDPOINT_SYNC_SYNC		(3 << 2)
463 
464 #define USB_ENDPOINT_USAGE_MASK		0x30
465 #define USB_ENDPOINT_USAGE_DATA		0x00
466 #define USB_ENDPOINT_USAGE_FEEDBACK	0x10
467 #define USB_ENDPOINT_USAGE_IMPLICIT_FB	0x20	/* Implicit feedback Data endpoint */
468 
469 /*-------------------------------------------------------------------------*/
470 
471 /**
472  * usb_endpoint_num - get the endpoint's number
473  * @epd: endpoint to be checked
474  *
475  * Returns @epd's number: 0 to 15.
476  */
usb_endpoint_num(const struct usb_endpoint_descriptor * epd)477 static inline int usb_endpoint_num(const struct usb_endpoint_descriptor *epd)
478 {
479 	return epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
480 }
481 
482 /**
483  * usb_endpoint_type - get the endpoint's transfer type
484  * @epd: endpoint to be checked
485  *
486  * Returns one of USB_ENDPOINT_XFER_{CONTROL, ISOC, BULK, INT} according
487  * to @epd's transfer type.
488  */
usb_endpoint_type(const struct usb_endpoint_descriptor * epd)489 static inline int usb_endpoint_type(const struct usb_endpoint_descriptor *epd)
490 {
491 	return epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
492 }
493 
494 /**
495  * usb_endpoint_dir_in - check if the endpoint has IN direction
496  * @epd: endpoint to be checked
497  *
498  * Returns true if the endpoint is of type IN, otherwise it returns false.
499  */
usb_endpoint_dir_in(const struct usb_endpoint_descriptor * epd)500 static inline int usb_endpoint_dir_in(const struct usb_endpoint_descriptor *epd)
501 {
502 	return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN);
503 }
504 
505 /**
506  * usb_endpoint_dir_out - check if the endpoint has OUT direction
507  * @epd: endpoint to be checked
508  *
509  * Returns true if the endpoint is of type OUT, otherwise it returns false.
510  */
usb_endpoint_dir_out(const struct usb_endpoint_descriptor * epd)511 static inline int usb_endpoint_dir_out(
512 				const struct usb_endpoint_descriptor *epd)
513 {
514 	return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
515 }
516 
517 /**
518  * usb_endpoint_xfer_bulk - check if the endpoint has bulk transfer type
519  * @epd: endpoint to be checked
520  *
521  * Returns true if the endpoint is of type bulk, otherwise it returns false.
522  */
usb_endpoint_xfer_bulk(const struct usb_endpoint_descriptor * epd)523 static inline int usb_endpoint_xfer_bulk(
524 				const struct usb_endpoint_descriptor *epd)
525 {
526 	return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
527 		USB_ENDPOINT_XFER_BULK);
528 }
529 
530 /**
531  * usb_endpoint_xfer_control - check if the endpoint has control transfer type
532  * @epd: endpoint to be checked
533  *
534  * Returns true if the endpoint is of type control, otherwise it returns false.
535  */
usb_endpoint_xfer_control(const struct usb_endpoint_descriptor * epd)536 static inline int usb_endpoint_xfer_control(
537 				const struct usb_endpoint_descriptor *epd)
538 {
539 	return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
540 		USB_ENDPOINT_XFER_CONTROL);
541 }
542 
543 /**
544  * usb_endpoint_xfer_int - check if the endpoint has interrupt transfer type
545  * @epd: endpoint to be checked
546  *
547  * Returns true if the endpoint is of type interrupt, otherwise it returns
548  * false.
549  */
usb_endpoint_xfer_int(const struct usb_endpoint_descriptor * epd)550 static inline int usb_endpoint_xfer_int(
551 				const struct usb_endpoint_descriptor *epd)
552 {
553 	return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
554 		USB_ENDPOINT_XFER_INT);
555 }
556 
557 /**
558  * usb_endpoint_xfer_isoc - check if the endpoint has isochronous transfer type
559  * @epd: endpoint to be checked
560  *
561  * Returns true if the endpoint is of type isochronous, otherwise it returns
562  * false.
563  */
usb_endpoint_xfer_isoc(const struct usb_endpoint_descriptor * epd)564 static inline int usb_endpoint_xfer_isoc(
565 				const struct usb_endpoint_descriptor *epd)
566 {
567 	return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
568 		USB_ENDPOINT_XFER_ISOC);
569 }
570 
571 /**
572  * usb_endpoint_is_bulk_in - check if the endpoint is bulk IN
573  * @epd: endpoint to be checked
574  *
575  * Returns true if the endpoint has bulk transfer type and IN direction,
576  * otherwise it returns false.
577  */
usb_endpoint_is_bulk_in(const struct usb_endpoint_descriptor * epd)578 static inline int usb_endpoint_is_bulk_in(
579 				const struct usb_endpoint_descriptor *epd)
580 {
581 	return usb_endpoint_xfer_bulk(epd) && usb_endpoint_dir_in(epd);
582 }
583 
584 /**
585  * usb_endpoint_is_bulk_out - check if the endpoint is bulk OUT
586  * @epd: endpoint to be checked
587  *
588  * Returns true if the endpoint has bulk transfer type and OUT direction,
589  * otherwise it returns false.
590  */
usb_endpoint_is_bulk_out(const struct usb_endpoint_descriptor * epd)591 static inline int usb_endpoint_is_bulk_out(
592 				const struct usb_endpoint_descriptor *epd)
593 {
594 	return usb_endpoint_xfer_bulk(epd) && usb_endpoint_dir_out(epd);
595 }
596 
597 /**
598  * usb_endpoint_is_int_in - check if the endpoint is interrupt IN
599  * @epd: endpoint to be checked
600  *
601  * Returns true if the endpoint has interrupt transfer type and IN direction,
602  * otherwise it returns false.
603  */
usb_endpoint_is_int_in(const struct usb_endpoint_descriptor * epd)604 static inline int usb_endpoint_is_int_in(
605 				const struct usb_endpoint_descriptor *epd)
606 {
607 	return usb_endpoint_xfer_int(epd) && usb_endpoint_dir_in(epd);
608 }
609 
610 /**
611  * usb_endpoint_is_int_out - check if the endpoint is interrupt OUT
612  * @epd: endpoint to be checked
613  *
614  * Returns true if the endpoint has interrupt transfer type and OUT direction,
615  * otherwise it returns false.
616  */
usb_endpoint_is_int_out(const struct usb_endpoint_descriptor * epd)617 static inline int usb_endpoint_is_int_out(
618 				const struct usb_endpoint_descriptor *epd)
619 {
620 	return usb_endpoint_xfer_int(epd) && usb_endpoint_dir_out(epd);
621 }
622 
623 /**
624  * usb_endpoint_is_isoc_in - check if the endpoint is isochronous IN
625  * @epd: endpoint to be checked
626  *
627  * Returns true if the endpoint has isochronous transfer type and IN direction,
628  * otherwise it returns false.
629  */
usb_endpoint_is_isoc_in(const struct usb_endpoint_descriptor * epd)630 static inline int usb_endpoint_is_isoc_in(
631 				const struct usb_endpoint_descriptor *epd)
632 {
633 	return usb_endpoint_xfer_isoc(epd) && usb_endpoint_dir_in(epd);
634 }
635 
636 /**
637  * usb_endpoint_is_isoc_out - check if the endpoint is isochronous OUT
638  * @epd: endpoint to be checked
639  *
640  * Returns true if the endpoint has isochronous transfer type and OUT direction,
641  * otherwise it returns false.
642  */
usb_endpoint_is_isoc_out(const struct usb_endpoint_descriptor * epd)643 static inline int usb_endpoint_is_isoc_out(
644 				const struct usb_endpoint_descriptor *epd)
645 {
646 	return usb_endpoint_xfer_isoc(epd) && usb_endpoint_dir_out(epd);
647 }
648 
649 /**
650  * usb_endpoint_maxp - get endpoint's max packet size
651  * @epd: endpoint to be checked
652  *
653  * Returns @epd's max packet bits [10:0]
654  */
usb_endpoint_maxp(const struct usb_endpoint_descriptor * epd)655 static inline int usb_endpoint_maxp(const struct usb_endpoint_descriptor *epd)
656 {
657 	return __le16_to_cpu(epd->wMaxPacketSize) & USB_ENDPOINT_MAXP_MASK;
658 }
659 
660 /**
661  * usb_endpoint_maxp_mult - get endpoint's transactional opportunities
662  * @epd: endpoint to be checked
663  *
664  * Return @epd's wMaxPacketSize[12:11] + 1
665  */
666 static inline int
usb_endpoint_maxp_mult(const struct usb_endpoint_descriptor * epd)667 usb_endpoint_maxp_mult(const struct usb_endpoint_descriptor *epd)
668 {
669 	int maxp = __le16_to_cpu(epd->wMaxPacketSize);
670 
671 	return USB_EP_MAXP_MULT(maxp) + 1;
672 }
673 
usb_endpoint_interrupt_type(const struct usb_endpoint_descriptor * epd)674 static inline int usb_endpoint_interrupt_type(
675 		const struct usb_endpoint_descriptor *epd)
676 {
677 	return epd->bmAttributes & USB_ENDPOINT_INTRTYPE;
678 }
679 
680 /*-------------------------------------------------------------------------*/
681 
682 /* USB_DT_EUSB2_ISOC_ENDPOINT_COMP: eUSB2 Isoch Endpoint Companion descriptor */
683 struct usb_eusb2_isoc_ep_comp_descriptor {
684 	__u8	bLength;
685 	__u8	bDescriptorType;
686 	__le16	wMaxPacketSize;
687 	__le32	dwBytesPerInterval;
688 } __attribute__ ((packed));
689 
690 #define USB_DT_EUSB2_ISOC_EP_COMP_SIZE	8
691 
692 /*-------------------------------------------------------------------------*/
693 
694 /* USB_DT_SSP_ISOC_ENDPOINT_COMP: SuperSpeedPlus Isochronous Endpoint Companion
695  * descriptor
696  */
697 struct usb_ssp_isoc_ep_comp_descriptor {
698 	__u8  bLength;
699 	__u8  bDescriptorType;
700 	__le16 wReseved;
701 	__le32 dwBytesPerInterval;
702 } __attribute__ ((packed));
703 
704 #define USB_DT_SSP_ISOC_EP_COMP_SIZE		8
705 
706 /*-------------------------------------------------------------------------*/
707 
708 /* USB_DT_SS_ENDPOINT_COMP: SuperSpeed Endpoint Companion descriptor */
709 struct usb_ss_ep_comp_descriptor {
710 	__u8  bLength;
711 	__u8  bDescriptorType;
712 
713 	__u8  bMaxBurst;
714 	__u8  bmAttributes;
715 	__le16 wBytesPerInterval;
716 } __attribute__ ((packed));
717 
718 #define USB_DT_SS_EP_COMP_SIZE		6
719 
720 /* Bits 4:0 of bmAttributes if this is a bulk endpoint */
721 static inline int
usb_ss_max_streams(const struct usb_ss_ep_comp_descriptor * comp)722 usb_ss_max_streams(const struct usb_ss_ep_comp_descriptor *comp)
723 {
724 	int		max_streams;
725 
726 	if (!comp)
727 		return 0;
728 
729 	max_streams = comp->bmAttributes & 0x1f;
730 
731 	if (!max_streams)
732 		return 0;
733 
734 	max_streams = 1 << max_streams;
735 
736 	return max_streams;
737 }
738 
739 /* Bits 1:0 of bmAttributes if this is an isoc endpoint */
740 #define USB_SS_MULT(p)			(1 + ((p) & 0x3))
741 /* Bit 7 of bmAttributes if a SSP isoc endpoint companion descriptor exists */
742 #define USB_SS_SSP_ISOC_COMP(p)		((p) & (1 << 7))
743 
744 /*-------------------------------------------------------------------------*/
745 
746 /* USB_DT_DEVICE_QUALIFIER: Device Qualifier descriptor */
747 struct usb_qualifier_descriptor {
748 	__u8  bLength;
749 	__u8  bDescriptorType;
750 
751 	__le16 bcdUSB;
752 	__u8  bDeviceClass;
753 	__u8  bDeviceSubClass;
754 	__u8  bDeviceProtocol;
755 	__u8  bMaxPacketSize0;
756 	__u8  bNumConfigurations;
757 	__u8  bRESERVED;
758 } __attribute__ ((packed));
759 
760 
761 /*-------------------------------------------------------------------------*/
762 
763 /* USB_DT_OTG (from OTG 1.0a supplement) */
764 struct usb_otg_descriptor {
765 	__u8  bLength;
766 	__u8  bDescriptorType;
767 
768 	__u8  bmAttributes;	/* support for HNP, SRP, etc */
769 } __attribute__ ((packed));
770 
771 /* USB_DT_OTG (from OTG 2.0 supplement) */
772 struct usb_otg20_descriptor {
773 	__u8  bLength;
774 	__u8  bDescriptorType;
775 
776 	__u8  bmAttributes;	/* support for HNP, SRP and ADP, etc */
777 	__le16 bcdOTG;		/* OTG and EH supplement release number
778 				 * in binary-coded decimal(i.e. 2.0 is 0200H)
779 				 */
780 } __attribute__ ((packed));
781 
782 /* from usb_otg_descriptor.bmAttributes */
783 #define USB_OTG_SRP		(1 << 0)
784 #define USB_OTG_HNP		(1 << 1)	/* swap host/device roles */
785 #define USB_OTG_ADP		(1 << 2)	/* support ADP */
786 /* OTG 3.0 */
787 #define USB_OTG_RSP		(1 << 3)	/* support RSP */
788 
789 #define OTG_STS_SELECTOR	0xF000		/* OTG status selector */
790 /*-------------------------------------------------------------------------*/
791 
792 /* USB_DT_DEBUG:  for special highspeed devices, replacing serial console */
793 struct usb_debug_descriptor {
794 	__u8  bLength;
795 	__u8  bDescriptorType;
796 
797 	/* bulk endpoints with 8 byte maxpacket */
798 	__u8  bDebugInEndpoint;
799 	__u8  bDebugOutEndpoint;
800 } __attribute__((packed));
801 
802 /*-------------------------------------------------------------------------*/
803 
804 /* USB_DT_INTERFACE_ASSOCIATION: groups interfaces */
805 struct usb_interface_assoc_descriptor {
806 	__u8  bLength;
807 	__u8  bDescriptorType;
808 
809 	__u8  bFirstInterface;
810 	__u8  bInterfaceCount;
811 	__u8  bFunctionClass;
812 	__u8  bFunctionSubClass;
813 	__u8  bFunctionProtocol;
814 	__u8  iFunction;
815 } __attribute__ ((packed));
816 
817 #define USB_DT_INTERFACE_ASSOCIATION_SIZE	8
818 
819 /*-------------------------------------------------------------------------*/
820 
821 /* USB_DT_SECURITY:  group of wireless security descriptors, including
822  * encryption types available for setting up a CC/association.
823  */
824 struct usb_security_descriptor {
825 	__u8  bLength;
826 	__u8  bDescriptorType;
827 
828 	__le16 wTotalLength;
829 	__u8  bNumEncryptionTypes;
830 } __attribute__((packed));
831 
832 /*-------------------------------------------------------------------------*/
833 
834 /* USB_DT_KEY:  used with {GET,SET}_SECURITY_DATA; only public keys
835  * may be retrieved.
836  */
837 struct usb_key_descriptor {
838 	__u8  bLength;
839 	__u8  bDescriptorType;
840 
841 	__u8  tTKID[3];
842 	__u8  bReserved;
843 	__u8  bKeyData[];
844 } __attribute__((packed));
845 
846 /*-------------------------------------------------------------------------*/
847 
848 /* USB_DT_ENCRYPTION_TYPE:  bundled in DT_SECURITY groups */
849 struct usb_encryption_descriptor {
850 	__u8  bLength;
851 	__u8  bDescriptorType;
852 
853 	__u8  bEncryptionType;
854 #define	USB_ENC_TYPE_UNSECURE		0
855 #define	USB_ENC_TYPE_WIRED		1	/* non-wireless mode */
856 #define	USB_ENC_TYPE_CCM_1		2	/* aes128/cbc session */
857 #define	USB_ENC_TYPE_RSA_1		3	/* rsa3072/sha1 auth */
858 	__u8  bEncryptionValue;		/* use in SET_ENCRYPTION */
859 	__u8  bAuthKeyIndex;
860 } __attribute__((packed));
861 
862 
863 /*-------------------------------------------------------------------------*/
864 
865 /* USB_DT_BOS:  group of device-level capabilities */
866 struct usb_bos_descriptor {
867 	__u8  bLength;
868 	__u8  bDescriptorType;
869 
870 	__le16 wTotalLength;
871 	__u8  bNumDeviceCaps;
872 } __attribute__((packed));
873 
874 #define USB_DT_BOS_SIZE		5
875 /*-------------------------------------------------------------------------*/
876 
877 /* USB_DT_DEVICE_CAPABILITY:  grouped with BOS */
878 struct usb_dev_cap_header {
879 	__u8  bLength;
880 	__u8  bDescriptorType;
881 	__u8  bDevCapabilityType;
882 } __attribute__((packed));
883 
884 #define	USB_CAP_TYPE_WIRELESS_USB	1
885 
886 struct usb_wireless_cap_descriptor {	/* Ultra Wide Band */
887 	__u8  bLength;
888 	__u8  bDescriptorType;
889 	__u8  bDevCapabilityType;
890 
891 	__u8  bmAttributes;
892 #define	USB_WIRELESS_P2P_DRD		(1 << 1)
893 #define	USB_WIRELESS_BEACON_MASK	(3 << 2)
894 #define	USB_WIRELESS_BEACON_SELF	(1 << 2)
895 #define	USB_WIRELESS_BEACON_DIRECTED	(2 << 2)
896 #define	USB_WIRELESS_BEACON_NONE	(3 << 2)
897 	__le16 wPHYRates;	/* bit rates, Mbps */
898 #define	USB_WIRELESS_PHY_53		(1 << 0)	/* always set */
899 #define	USB_WIRELESS_PHY_80		(1 << 1)
900 #define	USB_WIRELESS_PHY_107		(1 << 2)	/* always set */
901 #define	USB_WIRELESS_PHY_160		(1 << 3)
902 #define	USB_WIRELESS_PHY_200		(1 << 4)	/* always set */
903 #define	USB_WIRELESS_PHY_320		(1 << 5)
904 #define	USB_WIRELESS_PHY_400		(1 << 6)
905 #define	USB_WIRELESS_PHY_480		(1 << 7)
906 	__u8  bmTFITXPowerInfo;	/* TFI power levels */
907 	__u8  bmFFITXPowerInfo;	/* FFI power levels */
908 	__le16 bmBandGroup;
909 	__u8  bReserved;
910 } __attribute__((packed));
911 
912 #define USB_DT_USB_WIRELESS_CAP_SIZE	11
913 
914 /* USB 2.0 Extension descriptor */
915 #define	USB_CAP_TYPE_EXT		2
916 
917 struct usb_ext_cap_descriptor {		/* Link Power Management */
918 	__u8  bLength;
919 	__u8  bDescriptorType;
920 	__u8  bDevCapabilityType;
921 	__le32 bmAttributes;
922 #define USB_LPM_SUPPORT			(1 << 1)	/* supports LPM */
923 #define USB_BESL_SUPPORT		(1 << 2)	/* supports BESL */
924 #define USB_BESL_BASELINE_VALID		(1 << 3)	/* Baseline BESL valid*/
925 #define USB_BESL_DEEP_VALID		(1 << 4)	/* Deep BESL valid */
926 #define USB_SET_BESL_BASELINE(p)	(((p) & 0xf) << 8)
927 #define USB_SET_BESL_DEEP(p)		(((p) & 0xf) << 12)
928 #define USB_GET_BESL_BASELINE(p)	(((p) & (0xf << 8)) >> 8)
929 #define USB_GET_BESL_DEEP(p)		(((p) & (0xf << 12)) >> 12)
930 } __attribute__((packed));
931 
932 #define USB_DT_USB_EXT_CAP_SIZE	7
933 
934 /*
935  * SuperSpeed USB Capability descriptor: Defines the set of SuperSpeed USB
936  * specific device level capabilities
937  */
938 #define		USB_SS_CAP_TYPE		3
939 struct usb_ss_cap_descriptor {		/* Link Power Management */
940 	__u8  bLength;
941 	__u8  bDescriptorType;
942 	__u8  bDevCapabilityType;
943 	__u8  bmAttributes;
944 #define USB_LTM_SUPPORT			(1 << 1) /* supports LTM */
945 	__le16 wSpeedSupported;
946 #define USB_LOW_SPEED_OPERATION		(1)	 /* Low speed operation */
947 #define USB_FULL_SPEED_OPERATION	(1 << 1) /* Full speed operation */
948 #define USB_HIGH_SPEED_OPERATION	(1 << 2) /* High speed operation */
949 #define USB_5GBPS_OPERATION		(1 << 3) /* Operation at 5Gbps */
950 	__u8  bFunctionalitySupport;
951 	__u8  bU1devExitLat;
952 	__le16 bU2DevExitLat;
953 } __attribute__((packed));
954 
955 #define USB_DT_USB_SS_CAP_SIZE	10
956 
957 /*
958  * Container ID Capability descriptor: Defines the instance unique ID used to
959  * identify the instance across all operating modes
960  */
961 #define	CONTAINER_ID_TYPE	4
962 struct usb_ss_container_id_descriptor {
963 	__u8  bLength;
964 	__u8  bDescriptorType;
965 	__u8  bDevCapabilityType;
966 	__u8  bReserved;
967 	__u8  ContainerID[16]; /* 128-bit number */
968 } __attribute__((packed));
969 
970 #define USB_DT_USB_SS_CONTN_ID_SIZE	20
971 
972 /*
973  * Platform Device Capability descriptor: Defines platform specific device
974  * capabilities
975  */
976 #define	USB_PLAT_DEV_CAP_TYPE	5
977 struct usb_plat_dev_cap_descriptor {
978 	__u8  bLength;
979 	__u8  bDescriptorType;
980 	__u8  bDevCapabilityType;
981 	__u8  bReserved;
982 	__u8  UUID[16];
983 	__u8  CapabilityData[];
984 } __attribute__((packed));
985 
986 #define USB_DT_USB_PLAT_DEV_CAP_SIZE(capability_data_size)	(20 + capability_data_size)
987 
988 /*
989  * SuperSpeed Plus USB Capability descriptor: Defines the set of
990  * SuperSpeed Plus USB specific device level capabilities
991  */
992 #define	USB_SSP_CAP_TYPE	0xa
993 struct usb_ssp_cap_descriptor {
994 	__u8  bLength;
995 	__u8  bDescriptorType;
996 	__u8  bDevCapabilityType;
997 	__u8  bReserved;
998 	__le32 bmAttributes;
999 #define USB_SSP_SUBLINK_SPEED_ATTRIBS	(0x1f << 0) /* sublink speed entries */
1000 #define USB_SSP_SUBLINK_SPEED_IDS	(0xf << 5)  /* speed ID entries */
1001 	__le16  wFunctionalitySupport;
1002 #define USB_SSP_MIN_SUBLINK_SPEED_ATTRIBUTE_ID	(0xf)
1003 #define USB_SSP_MIN_RX_LANE_COUNT		(0xf << 8)
1004 #define USB_SSP_MIN_TX_LANE_COUNT		(0xf << 12)
1005 	__le16 wReserved;
1006 	union {
1007 		__le32 legacy_padding;
1008 		/* list of sublink speed attrib entries */
1009 		__DECLARE_FLEX_ARRAY(__le32, bmSublinkSpeedAttr);
1010 	};
1011 #define USB_SSP_SUBLINK_SPEED_SSID	(0xf)		/* sublink speed ID */
1012 #define USB_SSP_SUBLINK_SPEED_LSE	(0x3 << 4)	/* Lanespeed exponent */
1013 #define USB_SSP_SUBLINK_SPEED_LSE_BPS		0
1014 #define USB_SSP_SUBLINK_SPEED_LSE_KBPS		1
1015 #define USB_SSP_SUBLINK_SPEED_LSE_MBPS		2
1016 #define USB_SSP_SUBLINK_SPEED_LSE_GBPS		3
1017 
1018 #define USB_SSP_SUBLINK_SPEED_ST	(0x3 << 6)	/* Sublink type */
1019 #define USB_SSP_SUBLINK_SPEED_ST_SYM_RX		0
1020 #define USB_SSP_SUBLINK_SPEED_ST_ASYM_RX	1
1021 #define USB_SSP_SUBLINK_SPEED_ST_SYM_TX		2
1022 #define USB_SSP_SUBLINK_SPEED_ST_ASYM_TX	3
1023 
1024 #define USB_SSP_SUBLINK_SPEED_RSVD	(0x3f << 8)	/* Reserved */
1025 #define USB_SSP_SUBLINK_SPEED_LP	(0x3 << 14)	/* Link protocol */
1026 #define USB_SSP_SUBLINK_SPEED_LP_SS		0
1027 #define USB_SSP_SUBLINK_SPEED_LP_SSP		1
1028 
1029 #define USB_SSP_SUBLINK_SPEED_LSM	(0xff << 16)	/* Lanespeed mantissa */
1030 } __attribute__((packed));
1031 
1032 /*
1033  * USB Power Delivery Capability Descriptor:
1034  * Defines capabilities for PD
1035  */
1036 /* Defines the various PD Capabilities of this device */
1037 #define USB_PD_POWER_DELIVERY_CAPABILITY	0x06
1038 /* Provides information on each battery supported by the device */
1039 #define USB_PD_BATTERY_INFO_CAPABILITY		0x07
1040 /* The Consumer characteristics of a Port on the device */
1041 #define USB_PD_PD_CONSUMER_PORT_CAPABILITY	0x08
1042 /* The provider characteristics of a Port on the device */
1043 #define USB_PD_PD_PROVIDER_PORT_CAPABILITY	0x09
1044 
1045 struct usb_pd_cap_descriptor {
1046 	__u8  bLength;
1047 	__u8  bDescriptorType;
1048 	__u8  bDevCapabilityType; /* set to USB_PD_POWER_DELIVERY_CAPABILITY */
1049 	__u8  bReserved;
1050 	__le32 bmAttributes;
1051 #define USB_PD_CAP_BATTERY_CHARGING	(1 << 1) /* supports Battery Charging specification */
1052 #define USB_PD_CAP_USB_PD		(1 << 2) /* supports USB Power Delivery specification */
1053 #define USB_PD_CAP_PROVIDER		(1 << 3) /* can provide power */
1054 #define USB_PD_CAP_CONSUMER		(1 << 4) /* can consume power */
1055 #define USB_PD_CAP_CHARGING_POLICY	(1 << 5) /* supports CHARGING_POLICY feature */
1056 #define USB_PD_CAP_TYPE_C_CURRENT	(1 << 6) /* supports power capabilities defined in the USB Type-C Specification */
1057 
1058 #define USB_PD_CAP_PWR_AC		(1 << 8)
1059 #define USB_PD_CAP_PWR_BAT		(1 << 9)
1060 #define USB_PD_CAP_PWR_USE_V_BUS	(1 << 14)
1061 
1062 	__le16 bmProviderPorts; /* Bit zero refers to the UFP of the device */
1063 	__le16 bmConsumerPorts;
1064 	__le16 bcdBCVersion;
1065 	__le16 bcdPDVersion;
1066 	__le16 bcdUSBTypeCVersion;
1067 } __attribute__((packed));
1068 
1069 struct usb_pd_cap_battery_info_descriptor {
1070 	__u8 bLength;
1071 	__u8 bDescriptorType;
1072 	__u8 bDevCapabilityType;
1073 	/* Index of string descriptor shall contain the user friendly name for this battery */
1074 	__u8 iBattery;
1075 	/* Index of string descriptor shall contain the Serial Number String for this battery */
1076 	__u8 iSerial;
1077 	__u8 iManufacturer;
1078 	__u8 bBatteryId; /* uniquely identifies this battery in status Messages */
1079 	__u8 bReserved;
1080 	/*
1081 	 * Shall contain the Battery Charge value above which this
1082 	 * battery is considered to be fully charged but not necessarily
1083 	 * “topped off.”
1084 	 */
1085 	__le32 dwChargedThreshold; /* in mWh */
1086 	/*
1087 	 * Shall contain the minimum charge level of this battery such
1088 	 * that above this threshold, a device can be assured of being
1089 	 * able to power up successfully (see Battery Charging 1.2).
1090 	 */
1091 	__le32 dwWeakThreshold; /* in mWh */
1092 	__le32 dwBatteryDesignCapacity; /* in mWh */
1093 	__le32 dwBatteryLastFullchargeCapacity; /* in mWh */
1094 } __attribute__((packed));
1095 
1096 struct usb_pd_cap_consumer_port_descriptor {
1097 	__u8 bLength;
1098 	__u8 bDescriptorType;
1099 	__u8 bDevCapabilityType;
1100 	__u8 bReserved;
1101 	__u8 bmCapabilities;
1102 /* port will oerate under: */
1103 #define USB_PD_CAP_CONSUMER_BC		(1 << 0) /* BC */
1104 #define USB_PD_CAP_CONSUMER_PD		(1 << 1) /* PD */
1105 #define USB_PD_CAP_CONSUMER_TYPE_C	(1 << 2) /* USB Type-C Current */
1106 	__le16 wMinVoltage; /* in 50mV units */
1107 	__le16 wMaxVoltage; /* in 50mV units */
1108 	__u16 wReserved;
1109 	__le32 dwMaxOperatingPower; /* in 10 mW - operating at steady state */
1110 	__le32 dwMaxPeakPower; /* in 10mW units - operating at peak power */
1111 	__le32 dwMaxPeakPowerTime; /* in 100ms units - duration of peak */
1112 #define USB_PD_CAP_CONSUMER_UNKNOWN_PEAK_POWER_TIME 0xffff
1113 } __attribute__((packed));
1114 
1115 struct usb_pd_cap_provider_port_descriptor {
1116 	__u8 bLength;
1117 	__u8 bDescriptorType;
1118 	__u8 bDevCapabilityType;
1119 	__u8 bReserved1;
1120 	__u8 bmCapabilities;
1121 /* port will oerate under: */
1122 #define USB_PD_CAP_PROVIDER_BC		(1 << 0) /* BC */
1123 #define USB_PD_CAP_PROVIDER_PD		(1 << 1) /* PD */
1124 #define USB_PD_CAP_PROVIDER_TYPE_C	(1 << 2) /* USB Type-C Current */
1125 	__u8 bNumOfPDObjects;
1126 	__u8 bReserved2;
1127 	__le32 wPowerDataObject[];
1128 } __attribute__((packed));
1129 
1130 /*
1131  * Precision time measurement capability descriptor: advertised by devices and
1132  * hubs that support PTM
1133  */
1134 #define	USB_PTM_CAP_TYPE	0xb
1135 struct usb_ptm_cap_descriptor {
1136 	__u8  bLength;
1137 	__u8  bDescriptorType;
1138 	__u8  bDevCapabilityType;
1139 } __attribute__((packed));
1140 
1141 #define USB_DT_USB_PTM_ID_SIZE		3
1142 /*
1143  * The size of the descriptor for the Sublink Speed Attribute Count
1144  * (SSAC) specified in bmAttributes[4:0]. SSAC is zero-based
1145  */
1146 #define USB_DT_USB_SSP_CAP_SIZE(ssac)	(12 + (ssac + 1) * 4)
1147 
1148 /*-------------------------------------------------------------------------*/
1149 
1150 /* USB_DT_WIRELESS_ENDPOINT_COMP:  companion descriptor associated with
1151  * each endpoint descriptor for a wireless device
1152  */
1153 struct usb_wireless_ep_comp_descriptor {
1154 	__u8  bLength;
1155 	__u8  bDescriptorType;
1156 
1157 	__u8  bMaxBurst;
1158 	__u8  bMaxSequence;
1159 	__le16 wMaxStreamDelay;
1160 	__le16 wOverTheAirPacketSize;
1161 	__u8  bOverTheAirInterval;
1162 	__u8  bmCompAttributes;
1163 #define USB_ENDPOINT_SWITCH_MASK	0x03	/* in bmCompAttributes */
1164 #define USB_ENDPOINT_SWITCH_NO		0
1165 #define USB_ENDPOINT_SWITCH_SWITCH	1
1166 #define USB_ENDPOINT_SWITCH_SCALE	2
1167 } __attribute__((packed));
1168 
1169 /*-------------------------------------------------------------------------*/
1170 
1171 /* USB_REQ_SET_HANDSHAKE is a four-way handshake used between a wireless
1172  * host and a device for connection set up, mutual authentication, and
1173  * exchanging short lived session keys.  The handshake depends on a CC.
1174  */
1175 struct usb_handshake {
1176 	__u8 bMessageNumber;
1177 	__u8 bStatus;
1178 	__u8 tTKID[3];
1179 	__u8 bReserved;
1180 	__u8 CDID[16];
1181 	__u8 nonce[16];
1182 	__u8 MIC[8];
1183 } __attribute__((packed));
1184 
1185 /*-------------------------------------------------------------------------*/
1186 
1187 /* USB_REQ_SET_CONNECTION modifies or revokes a connection context (CC).
1188  * A CC may also be set up using non-wireless secure channels (including
1189  * wired USB!), and some devices may support CCs with multiple hosts.
1190  */
1191 struct usb_connection_context {
1192 	__u8 CHID[16];		/* persistent host id */
1193 	__u8 CDID[16];		/* device id (unique w/in host context) */
1194 	__u8 CK[16];		/* connection key */
1195 } __attribute__((packed));
1196 
1197 /*-------------------------------------------------------------------------*/
1198 
1199 /* USB 2.0 defines three speeds, here's how Linux identifies them */
1200 
1201 enum usb_device_speed {
1202 	USB_SPEED_UNKNOWN = 0,			/* enumerating */
1203 	USB_SPEED_LOW, USB_SPEED_FULL,		/* usb 1.1 */
1204 	USB_SPEED_HIGH,				/* usb 2.0 */
1205 	USB_SPEED_WIRELESS,			/* wireless (usb 2.5) */
1206 	USB_SPEED_SUPER,			/* usb 3.0 */
1207 	USB_SPEED_SUPER_PLUS,			/* usb 3.1 */
1208 };
1209 
1210 
1211 enum usb_device_state {
1212 	/* NOTATTACHED isn't in the USB spec, and this state acts
1213 	 * the same as ATTACHED ... but it's clearer this way.
1214 	 */
1215 	USB_STATE_NOTATTACHED = 0,
1216 
1217 	/* chapter 9 and authentication (wireless) device states */
1218 	USB_STATE_ATTACHED,
1219 	USB_STATE_POWERED,			/* wired */
1220 	USB_STATE_RECONNECTING,			/* auth */
1221 	USB_STATE_UNAUTHENTICATED,		/* auth */
1222 	USB_STATE_DEFAULT,			/* limited function */
1223 	USB_STATE_ADDRESS,
1224 	USB_STATE_CONFIGURED,			/* most functions */
1225 
1226 	USB_STATE_SUSPENDED
1227 
1228 	/* NOTE:  there are actually four different SUSPENDED
1229 	 * states, returning to POWERED, DEFAULT, ADDRESS, or
1230 	 * CONFIGURED respectively when SOF tokens flow again.
1231 	 * At this level there's no difference between L1 and L2
1232 	 * suspend states.  (L2 being original USB 1.1 suspend.)
1233 	 */
1234 };
1235 
1236 enum usb3_link_state {
1237 	USB3_LPM_U0 = 0,
1238 	USB3_LPM_U1,
1239 	USB3_LPM_U2,
1240 	USB3_LPM_U3
1241 };
1242 
1243 /*
1244  * A U1 timeout of 0x0 means the parent hub will reject any transitions to U1.
1245  * 0xff means the parent hub will accept transitions to U1, but will not
1246  * initiate a transition.
1247  *
1248  * A U1 timeout of 0x1 to 0x7F also causes the hub to initiate a transition to
1249  * U1 after that many microseconds.  Timeouts of 0x80 to 0xFE are reserved
1250  * values.
1251  *
1252  * A U2 timeout of 0x0 means the parent hub will reject any transitions to U2.
1253  * 0xff means the parent hub will accept transitions to U2, but will not
1254  * initiate a transition.
1255  *
1256  * A U2 timeout of 0x1 to 0xFE also causes the hub to initiate a transition to
1257  * U2 after N*256 microseconds.  Therefore a U2 timeout value of 0x1 means a U2
1258  * idle timer of 256 microseconds, 0x2 means 512 microseconds, 0xFE means
1259  * 65.024ms.
1260  */
1261 #define USB3_LPM_DISABLED		0x0
1262 #define USB3_LPM_U1_MAX_TIMEOUT		0x7F
1263 #define USB3_LPM_U2_MAX_TIMEOUT		0xFE
1264 #define USB3_LPM_DEVICE_INITIATED	0xFF
1265 
1266 struct usb_set_sel_req {
1267 	__u8	u1_sel;
1268 	__u8	u1_pel;
1269 	__le16	u2_sel;
1270 	__le16	u2_pel;
1271 } __attribute__ ((packed));
1272 
1273 /*
1274  * The Set System Exit Latency control transfer provides one byte each for
1275  * U1 SEL and U1 PEL, so the max exit latency is 0xFF.  U2 SEL and U2 PEL each
1276  * are two bytes long.
1277  */
1278 #define USB3_LPM_MAX_U1_SEL_PEL		0xFF
1279 #define USB3_LPM_MAX_U2_SEL_PEL		0xFFFF
1280 
1281 /*-------------------------------------------------------------------------*/
1282 
1283 /*
1284  * As per USB compliance update, a device that is actively drawing
1285  * more than 100mA from USB must report itself as bus-powered in
1286  * the GetStatus(DEVICE) call.
1287  * https://compliance.usb.org/index.asp?UpdateFile=Electrical&Format=Standard#34
1288  */
1289 #define USB_SELF_POWER_VBUS_MAX_DRAW		100
1290 
1291 #endif /* _UAPI__LINUX_USB_CH9_H */
1292