1184610Salfred/* $FreeBSD$ */
2184610Salfred/*-
3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4184610Salfred * Copyright (c) 2007-2008 Daniel Drake.  All rights reserved.
5184610Salfred * Copyright (c) 2001 Johannes Erdfelt.  All rights reserved.
6184610Salfred *
7184610Salfred * Redistribution and use in source and binary forms, with or without
8184610Salfred * modification, are permitted provided that the following conditions
9184610Salfred * are met:
10184610Salfred * 1. Redistributions of source code must retain the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer.
12184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
13184610Salfred *    notice, this list of conditions and the following disclaimer in the
14184610Salfred *    documentation and/or other materials provided with the distribution.
15184610Salfred *
16184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26184610Salfred * SUCH DAMAGE.
27184610Salfred */
28184610Salfred
29184610Salfred/*
30184610Salfred * NOTE: This file contains the definition of some standard USB
31184610Salfred * structures. All structures which name ends by *DECODED use host byte
32184610Salfred * order.
33184610Salfred */
34184610Salfred
35184610Salfred/*
36184610Salfred * NOTE: This file uses a lot of macros. If you want to see what the
37184610Salfred * macros become when they are expanded then run the following
38184610Salfred * commands from your shell:
39184610Salfred *
40184610Salfred * cpp libusb20_desc.h > temp.h
41184610Salfred * indent temp.h
42184610Salfred * less temp.h
43184610Salfred */
44184610Salfred
45184610Salfred#ifndef _LIBUSB20_DESC_H_
46184610Salfred#define	_LIBUSB20_DESC_H_
47184610Salfred
48184610Salfred#ifdef __cplusplus
49184610Salfredextern	"C" {
50184610Salfred#endif
51184610Salfred#if 0
52184610Salfred};					/* style */
53184610Salfred
54184610Salfred#endif
55184610Salfred/* basic macros */
56184610Salfred
57184610Salfred#define	LIBUSB20__NOT(...) __VA_ARGS__
58184610Salfred#define	LIBUSB20_NOT(arg) LIBUSB20__NOT(LIBUSB20_YES arg(() LIBUSB20_NO))
59184610Salfred#define	LIBUSB20_YES(...) __VA_ARGS__
60184610Salfred#define	LIBUSB20_NO(...)
61184610Salfred#define	LIBUSB20_END(...) __VA_ARGS__
62184610Salfred#define	LIBUSB20_MAX(a,b) (((a) > (b)) ? (a) : (b))
63184610Salfred#define	LIBUSB20_MIN(a,b) (((a) < (b)) ? (a) : (b))
64184610Salfred
65184610Salfred#define	LIBUSB20_ADD_BYTES(ptr,off) \
66185087Salfred  ((void *)(((const uint8_t *)(ptr)) + (off) - ((const uint8_t *)0)))
67184610Salfred
68184610Salfred/* basic message elements */
69184610Salfredenum {
70184610Salfred	LIBUSB20_ME_INT8,
71184610Salfred	LIBUSB20_ME_INT16,
72184610Salfred	LIBUSB20_ME_INT32,
73184610Salfred	LIBUSB20_ME_INT64,
74184610Salfred	LIBUSB20_ME_STRUCT,
75184610Salfred	LIBUSB20_ME_MAX,		/* used to indicate end */
76184610Salfred};
77184610Salfred
78184610Salfred/* basic message element modifiers */
79184610Salfredenum {
80184610Salfred	LIBUSB20_ME_IS_UNSIGNED = 0x00,
81184610Salfred	LIBUSB20_ME_IS_SIGNED = 0x80,
82184610Salfred	LIBUSB20_ME_MASK = 0x7F,
83184610Salfred};
84184610Salfred
85184610Salfredenum {
86184610Salfred	LIBUSB20_ME_IS_RAW,		/* structure excludes length field
87184610Salfred					 * (hardcoded value) */
88184610Salfred	LIBUSB20_ME_IS_ENCODED,		/* structure includes length field */
89184610Salfred	LIBUSB20_ME_IS_EMPTY,		/* no structure */
90184610Salfred	LIBUSB20_ME_IS_DECODED,		/* structure is recursive */
91184610Salfred};
92184610Salfred
93184610Salfred/* basic helper structures and macros */
94184610Salfred
95184610Salfred#define	LIBUSB20_ME_STRUCT_ALIGN sizeof(void *)
96184610Salfred
97184610Salfredstruct libusb20_me_struct {
98184610Salfred	void   *ptr;			/* data pointer */
99184610Salfred	uint16_t len;			/* defaults to zero */
100184610Salfred	uint16_t type;			/* defaults to LIBUSB20_ME_IS_EMPTY */
101184610Salfred} __aligned(LIBUSB20_ME_STRUCT_ALIGN);
102184610Salfred
103184610Salfredstruct libusb20_me_format {
104184610Salfred	const uint8_t *format;		/* always set */
105184610Salfred	const char *desc;		/* optionally set */
106184610Salfred	const char *fields;		/* optionally set */
107184610Salfred};
108184610Salfred
109184610Salfred#define	LIBUSB20_ME_STRUCT(n, field, arg, ismeta)		\
110184610Salfred  ismeta ( LIBUSB20_ME_STRUCT, 1, 0, )			\
111184610Salfred  LIBUSB20_NOT(ismeta) ( struct libusb20_me_struct field; )
112184610Salfred
113184610Salfred#define	LIBUSB20_ME_STRUCT_ARRAY(n, field, arg, ismeta)	\
114184610Salfred  ismeta ( LIBUSB20_ME_STRUCT , (arg) & 0xFF,		\
115184610Salfred	   ((arg) / 0x100) & 0xFF, )			\
116184610Salfred  LIBUSB20_NOT(ismeta) ( struct libusb20_me_struct field [arg]; )
117184610Salfred
118184610Salfred#define	LIBUSB20_ME_INTEGER(n, field, ismeta, un, u, bits, a, size)	\
119184610Salfred  ismeta ( LIBUSB20_ME_INT##bits |					\
120184610Salfred	   LIBUSB20_ME_IS_##un##SIGNED ,				\
121184610Salfred	   (size) & 0xFF, ((size) / 0x100) & 0xFF, )		\
122184610Salfred  LIBUSB20_NOT(ismeta) ( u##int##bits##_t				\
123184610Salfred		    __aligned((bits) / 8) field a; )
124184610Salfred
125184610Salfred#define	LIBUSB20_ME_UINT8_T(n, field, arg, ismeta) \
126184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 8, , 1)
127184610Salfred
128184610Salfred#define	LIBUSB20_ME_UINT8_ARRAY_T(n, field, arg, ismeta) \
129184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 8, [arg], arg)
130184610Salfred
131184610Salfred#define	LIBUSB20_ME_SINT8_T(n, field, arg, ismeta) \
132184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta,,, 8, , 1)
133184610Salfred
134184610Salfred#define	LIBUSB20_ME_SINT8_ARRAY_T(n, field, arg, ismeta) \
135184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta,,, 8, [arg], arg)
136184610Salfred
137184610Salfred#define	LIBUSB20_ME_UINT16_T(n, field, arg, ismeta) \
138184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 16, , 1)
139184610Salfred
140184610Salfred#define	LIBUSB20_ME_UINT16_ARRAY_T(n, field, arg, ismeta) \
141184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 16, [arg], arg)
142184610Salfred
143184610Salfred#define	LIBUSB20_ME_SINT16_T(n, field, arg, ismeta) \
144184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta,,, 16, , 1)
145184610Salfred
146184610Salfred#define	LIBUSB20_ME_SINT16_ARRAY_T(n, field, arg, ismeta) \
147184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta,,, 16, [arg], arg)
148184610Salfred
149184610Salfred#define	LIBUSB20_ME_UINT32_T(n, field, arg, ismeta) \
150184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 32, , 1)
151184610Salfred
152184610Salfred#define	LIBUSB20_ME_UINT32_ARRAY_T(n, field, arg, ismeta) \
153184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 32, [arg], arg)
154184610Salfred
155184610Salfred#define	LIBUSB20_ME_SINT32_T(n, field, arg, ismeta) \
156184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta,,, 32, , 1)
157184610Salfred
158184610Salfred#define	LIBUSB20_ME_SINT32_ARRAY_T(n, field, arg, ismeta) \
159184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta,,, 32, [arg], arg)
160184610Salfred
161184610Salfred#define	LIBUSB20_ME_UINT64_T(n, field, arg, ismeta) \
162184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 64, , 1)
163184610Salfred
164184610Salfred#define	LIBUSB20_ME_UINT64_ARRAY_T(n, field, arg, ismeta) \
165184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 64, [arg], arg)
166184610Salfred
167184610Salfred#define	LIBUSB20_ME_SINT64_T(n, field, arg, ismeta) \
168184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta,,, 64, , 1)
169184610Salfred
170184610Salfred#define	LIBUSB20_ME_SINT64_ARRAY_T(n, field, arg, ismeta) \
171184610Salfred  LIBUSB20_ME_INTEGER(n, field, ismeta,,, 64, [arg], arg)
172184610Salfred
173184610Salfred#define	LIBUSB20_MAKE_DECODED_FIELD(n, type, field, arg) \
174184610Salfred  LIBUSB20_ME_##type (n, field, arg, LIBUSB20_NO)
175184610Salfred
176184610Salfred#define	LIBUSB20_MAKE_STRUCT(name)			\
177184610Salfred  extern const struct libusb20_me_format			\
178184610Salfred	 name##_FORMAT[1];				\
179184610Salfred  struct name##_DECODED {				\
180184610Salfred    const struct libusb20_me_format *name##_FORMAT;	\
181184610Salfred    name (LIBUSB20_MAKE_DECODED_FIELD,)			\
182184610Salfred  }
183184610Salfred
184184610Salfred#define	LIBUSB20_MAKE_STRUCT_FORMAT(name)		\
185184610Salfred  const struct libusb20_me_format			\
186184610Salfred    name##_FORMAT[1] = {{			\
187184610Salfred      .format = LIBUSB20_MAKE_FORMAT(name),	\
188184610Salfred      .desc = #name,				\
189184610Salfred      .fields = NULL,				\
190184610Salfred  }}
191184610Salfred
192184610Salfred#define	LIBUSB20_MAKE_FORMAT_SUB(n, type, field, arg) \
193184610Salfred  LIBUSB20_ME_##type (n, field, arg, LIBUSB20_YES)
194184610Salfred
195184610Salfred#define	LIBUSB20_MAKE_FORMAT(what) (const uint8_t []) \
196184610Salfred  { what (LIBUSB20_MAKE_FORMAT_SUB, ) LIBUSB20_ME_MAX, 0, 0 }
197184610Salfred
198184610Salfred#define	LIBUSB20_INIT(what, ptr) do {		\
199184610Salfred    memset(ptr, 0, sizeof(*(ptr)));		\
200184610Salfred    (ptr)->what##_FORMAT = what##_FORMAT;	\
201184610Salfred} while (0)
202184610Salfred
203184610Salfred#define	LIBUSB20_DEVICE_DESC(m,n) \
204184610Salfred  m(n, UINT8_T, bLength, ) \
205184610Salfred  m(n, UINT8_T, bDescriptorType, ) \
206184610Salfred  m(n, UINT16_T, bcdUSB, ) \
207184610Salfred  m(n, UINT8_T, bDeviceClass, ) \
208184610Salfred  m(n, UINT8_T, bDeviceSubClass, ) \
209184610Salfred  m(n, UINT8_T, bDeviceProtocol, ) \
210184610Salfred  m(n, UINT8_T, bMaxPacketSize0, ) \
211184610Salfred  m(n, UINT16_T, idVendor, ) \
212184610Salfred  m(n, UINT16_T, idProduct, ) \
213184610Salfred  m(n, UINT16_T, bcdDevice, ) \
214184610Salfred  m(n, UINT8_T, iManufacturer, ) \
215184610Salfred  m(n, UINT8_T, iProduct, ) \
216184610Salfred  m(n, UINT8_T, iSerialNumber, ) \
217184610Salfred  m(n, UINT8_T, bNumConfigurations, ) \
218184610Salfred
219184610SalfredLIBUSB20_MAKE_STRUCT(LIBUSB20_DEVICE_DESC);
220184610Salfred
221184610Salfred#define	LIBUSB20_ENDPOINT_DESC(m,n) \
222184610Salfred  m(n, UINT8_T,  bLength, ) \
223184610Salfred  m(n, UINT8_T,  bDescriptorType, ) \
224184610Salfred  m(n, UINT8_T,  bEndpointAddress, ) \
225184610Salfred  m(n, UINT8_T,  bmAttributes, ) \
226184610Salfred  m(n, UINT16_T, wMaxPacketSize, ) \
227184610Salfred  m(n, UINT8_T,  bInterval, ) \
228184610Salfred  m(n, UINT8_T,  bRefresh, ) \
229184610Salfred  m(n, UINT8_T,  bSynchAddress, ) \
230184610Salfred
231184610SalfredLIBUSB20_MAKE_STRUCT(LIBUSB20_ENDPOINT_DESC);
232184610Salfred
233184610Salfred#define	LIBUSB20_INTERFACE_DESC(m,n) \
234184610Salfred  m(n, UINT8_T,  bLength, ) \
235184610Salfred  m(n, UINT8_T,  bDescriptorType, ) \
236184610Salfred  m(n, UINT8_T,  bInterfaceNumber, ) \
237184610Salfred  m(n, UINT8_T,  bAlternateSetting, ) \
238184610Salfred  m(n, UINT8_T,  bNumEndpoints, ) \
239184610Salfred  m(n, UINT8_T,  bInterfaceClass, ) \
240184610Salfred  m(n, UINT8_T,  bInterfaceSubClass, ) \
241184610Salfred  m(n, UINT8_T,  bInterfaceProtocol, ) \
242184610Salfred  m(n, UINT8_T,  iInterface, ) \
243184610Salfred
244184610SalfredLIBUSB20_MAKE_STRUCT(LIBUSB20_INTERFACE_DESC);
245184610Salfred
246184610Salfred#define	LIBUSB20_CONFIG_DESC(m,n) \
247184610Salfred  m(n, UINT8_T,  bLength, ) \
248184610Salfred  m(n, UINT8_T,  bDescriptorType, ) \
249184610Salfred  m(n, UINT16_T, wTotalLength, ) \
250184610Salfred  m(n, UINT8_T,  bNumInterfaces, ) \
251184610Salfred  m(n, UINT8_T,  bConfigurationValue, ) \
252184610Salfred  m(n, UINT8_T,  iConfiguration, ) \
253184610Salfred  m(n, UINT8_T,  bmAttributes, ) \
254184610Salfred  m(n, UINT8_T,  bMaxPower, ) \
255184610Salfred
256184610SalfredLIBUSB20_MAKE_STRUCT(LIBUSB20_CONFIG_DESC);
257184610Salfred
258184610Salfred#define	LIBUSB20_CONTROL_SETUP(m,n) \
259184610Salfred  m(n, UINT8_T,  bmRequestType, ) \
260184610Salfred  m(n, UINT8_T,  bRequest, ) \
261184610Salfred  m(n, UINT16_T, wValue, ) \
262184610Salfred  m(n, UINT16_T, wIndex, ) \
263184610Salfred  m(n, UINT16_T, wLength, ) \
264184610Salfred
265184610SalfredLIBUSB20_MAKE_STRUCT(LIBUSB20_CONTROL_SETUP);
266184610Salfred
267229122Shselasky#define	LIBUSB20_SS_ENDPT_COMP_DESC(m,n) \
268229122Shselasky  m(n, UINT8_T,  bLength, ) \
269229122Shselasky  m(n, UINT8_T,  bDescriptorType, ) \
270229122Shselasky  m(n, UINT8_T,  bMaxBurst, ) \
271229122Shselasky  m(n, UINT8_T,  bmAttributes, ) \
272229122Shselasky  m(n, UINT16_T, wBytesPerInterval, ) \
273229122Shselasky
274229122ShselaskyLIBUSB20_MAKE_STRUCT(LIBUSB20_SS_ENDPT_COMP_DESC);
275229122Shselasky
276229122Shselasky#define	LIBUSB20_USB_20_DEVCAP_DESC(m,n) \
277229122Shselasky  m(n, UINT8_T,  bLength, ) \
278229122Shselasky  m(n, UINT8_T,  bDescriptorType, ) \
279229122Shselasky  m(n, UINT8_T,  bDevCapabilityType, ) \
280229122Shselasky  m(n, UINT32_T, bmAttributes, ) \
281229122Shselasky
282229122ShselaskyLIBUSB20_MAKE_STRUCT(LIBUSB20_USB_20_DEVCAP_DESC);
283229122Shselasky
284229122Shselasky#define	LIBUSB20_SS_USB_DEVCAP_DESC(m,n) \
285229122Shselasky  m(n, UINT8_T,  bLength, ) \
286229122Shselasky  m(n, UINT8_T,  bDescriptorType, ) \
287229122Shselasky  m(n, UINT8_T,  bDevCapabilityType, ) \
288229122Shselasky  m(n, UINT8_T,  bmAttributes, ) \
289229122Shselasky  m(n, UINT16_T, wSpeedSupported, ) \
290229122Shselasky  m(n, UINT8_T,  bFunctionalitySupport, ) \
291229122Shselasky  m(n, UINT8_T,  bU1DevExitLat, ) \
292229122Shselasky  m(n, UINT16_T, wU2DevExitLat, ) \
293229122Shselasky
294229122ShselaskyLIBUSB20_MAKE_STRUCT(LIBUSB20_SS_USB_DEVCAP_DESC);
295229122Shselasky
296229122Shselasky#define	LIBUSB20_BOS_DESCRIPTOR(m,n) \
297229122Shselasky  m(n, UINT8_T,  bLength, ) \
298229122Shselasky  m(n, UINT8_T,  bDescriptorType, ) \
299229122Shselasky  m(n, UINT16_T, wTotalLength, ) \
300229122Shselasky  m(n, UINT8_T,  bNumDeviceCapabilities, ) \
301229122Shselasky
302229122ShselaskyLIBUSB20_MAKE_STRUCT(LIBUSB20_BOS_DESCRIPTOR);
303229122Shselasky
304184610Salfred/* standard USB stuff */
305184610Salfred
306184610Salfred/** \ingroup desc
307184610Salfred * Device and/or Interface Class codes */
308184610Salfredenum libusb20_class_code {
309184610Salfred	/** In the context of a \ref LIBUSB20_DEVICE_DESC "device
310184610Salfred	 * descriptor", this bDeviceClass value indicates that each
311184610Salfred	 * interface specifies its own class information and all
312184610Salfred	 * interfaces operate independently.
313184610Salfred	 */
314184610Salfred	LIBUSB20_CLASS_PER_INTERFACE = 0,
315184610Salfred
316184610Salfred	/** Audio class */
317184610Salfred	LIBUSB20_CLASS_AUDIO = 1,
318184610Salfred
319184610Salfred	/** Communications class */
320184610Salfred	LIBUSB20_CLASS_COMM = 2,
321184610Salfred
322184610Salfred	/** Human Interface Device class */
323184610Salfred	LIBUSB20_CLASS_HID = 3,
324184610Salfred
325184610Salfred	/** Printer dclass */
326184610Salfred	LIBUSB20_CLASS_PRINTER = 7,
327184610Salfred
328184610Salfred	/** Picture transfer protocol class */
329184610Salfred	LIBUSB20_CLASS_PTP = 6,
330184610Salfred
331184610Salfred	/** Mass storage class */
332184610Salfred	LIBUSB20_CLASS_MASS_STORAGE = 8,
333184610Salfred
334184610Salfred	/** Hub class */
335184610Salfred	LIBUSB20_CLASS_HUB = 9,
336184610Salfred
337184610Salfred	/** Data class */
338184610Salfred	LIBUSB20_CLASS_DATA = 10,
339184610Salfred
340184610Salfred	/** Class is vendor-specific */
341184610Salfred	LIBUSB20_CLASS_VENDOR_SPEC = 0xff,
342184610Salfred};
343184610Salfred
344184610Salfred/** \ingroup desc
345184610Salfred * Descriptor types as defined by the USB specification. */
346184610Salfredenum libusb20_descriptor_type {
347184610Salfred	/** Device descriptor. See LIBUSB20_DEVICE_DESC. */
348184610Salfred	LIBUSB20_DT_DEVICE = 0x01,
349184610Salfred
350184610Salfred	/** Configuration descriptor. See LIBUSB20_CONFIG_DESC. */
351184610Salfred	LIBUSB20_DT_CONFIG = 0x02,
352184610Salfred
353184610Salfred	/** String descriptor */
354184610Salfred	LIBUSB20_DT_STRING = 0x03,
355184610Salfred
356184610Salfred	/** Interface descriptor. See LIBUSB20_INTERFACE_DESC. */
357184610Salfred	LIBUSB20_DT_INTERFACE = 0x04,
358184610Salfred
359184610Salfred	/** Endpoint descriptor. See LIBUSB20_ENDPOINT_DESC. */
360184610Salfred	LIBUSB20_DT_ENDPOINT = 0x05,
361184610Salfred
362184610Salfred	/** HID descriptor */
363184610Salfred	LIBUSB20_DT_HID = 0x21,
364184610Salfred
365184610Salfred	/** HID report descriptor */
366184610Salfred	LIBUSB20_DT_REPORT = 0x22,
367184610Salfred
368184610Salfred	/** Physical descriptor */
369184610Salfred	LIBUSB20_DT_PHYSICAL = 0x23,
370184610Salfred
371184610Salfred	/** Hub descriptor */
372184610Salfred	LIBUSB20_DT_HUB = 0x29,
373229122Shselasky
374229122Shselasky	/** Binary Object Store, BOS */
375229122Shselasky	LIBUSB20_DT_BOS = 0x0f,
376229122Shselasky
377229122Shselasky	/** Device Capability */
378229122Shselasky	LIBUSB20_DT_DEVICE_CAPABILITY = 0x10,
379229122Shselasky
380229122Shselasky	/** SuperSpeed endpoint companion */
381229122Shselasky	LIBUSB20_DT_SS_ENDPOINT_COMPANION = 0x30,
382184610Salfred};
383184610Salfred
384229122Shselasky/** \ingroup desc
385229122Shselasky * Device capability types as defined by the USB specification. */
386229122Shselaskyenum libusb20_device_capability_type {
387229122Shselasky	LIBUSB20_WIRELESS_USB_DEVICE_CAPABILITY = 0x1,
388229122Shselasky	LIBUSB20_USB_2_0_EXTENSION_DEVICE_CAPABILITY = 0x2,
389229122Shselasky	LIBUSB20_SS_USB_DEVICE_CAPABILITY = 0x3,
390229122Shselasky	LIBUSB20_CONTAINER_ID_DEVICE_CAPABILITY = 0x4,
391229122Shselasky};
392229122Shselasky
393184610Salfred/* Descriptor sizes per descriptor type */
394184610Salfred#define	LIBUSB20_DT_DEVICE_SIZE			18
395184610Salfred#define	LIBUSB20_DT_CONFIG_SIZE			9
396184610Salfred#define	LIBUSB20_DT_INTERFACE_SIZE		9
397184610Salfred#define	LIBUSB20_DT_ENDPOINT_SIZE		7
398184610Salfred#define	LIBUSB20_DT_ENDPOINT_AUDIO_SIZE		9	/* Audio extension */
399184610Salfred#define	LIBUSB20_DT_HUB_NONVAR_SIZE		7
400229122Shselasky#define	LIBUSB20_DT_SS_ENDPOINT_COMPANION_SIZE	6
401229122Shselasky#define	LIBUSB20_DT_BOS_SIZE		5
402229122Shselasky#define	LIBUSB20_USB_2_0_EXTENSION_DEVICE_CAPABILITY_SIZE	7
403229122Shselasky#define	LIBUSB20_SS_USB_DEVICE_CAPABILITY_SIZE	10
404184610Salfred
405184610Salfred#define	LIBUSB20_ENDPOINT_ADDRESS_MASK	0x0f	/* in bEndpointAddress */
406184610Salfred#define	LIBUSB20_ENDPOINT_DIR_MASK	0x80
407184610Salfred
408184610Salfred/** \ingroup desc
409184610Salfred * Endpoint direction. Values for bit 7 of the
410184610Salfred * \ref LIBUSB20_ENDPOINT_DESC::bEndpointAddress "endpoint address" scheme.
411184610Salfred */
412184610Salfredenum libusb20_endpoint_direction {
413184610Salfred	/** In: device-to-host */
414184610Salfred	LIBUSB20_ENDPOINT_IN = 0x80,
415184610Salfred
416184610Salfred	/** Out: host-to-device */
417184610Salfred	LIBUSB20_ENDPOINT_OUT = 0x00,
418184610Salfred};
419184610Salfred
420184610Salfred#define	LIBUSB20_TRANSFER_TYPE_MASK	0x03	/* in bmAttributes */
421184610Salfred
422184610Salfred/** \ingroup desc
423184610Salfred * Endpoint transfer type. Values for bits 0:1 of the
424184610Salfred * \ref LIBUSB20_ENDPOINT_DESC::bmAttributes "endpoint attributes" field.
425184610Salfred */
426184610Salfredenum libusb20_transfer_type {
427184610Salfred	/** Control endpoint */
428184610Salfred	LIBUSB20_TRANSFER_TYPE_CONTROL = 0,
429184610Salfred
430184610Salfred	/** Isochronous endpoint */
431184610Salfred	LIBUSB20_TRANSFER_TYPE_ISOCHRONOUS = 1,
432184610Salfred
433184610Salfred	/** Bulk endpoint */
434184610Salfred	LIBUSB20_TRANSFER_TYPE_BULK = 2,
435184610Salfred
436184610Salfred	/** Interrupt endpoint */
437184610Salfred	LIBUSB20_TRANSFER_TYPE_INTERRUPT = 3,
438184610Salfred};
439184610Salfred
440184610Salfred/** \ingroup misc
441184610Salfred * Standard requests, as defined in table 9-3 of the USB2 specifications */
442184610Salfredenum libusb20_standard_request {
443184610Salfred	/** Request status of the specific recipient */
444184610Salfred	LIBUSB20_REQUEST_GET_STATUS = 0x00,
445184610Salfred
446184610Salfred	/** Clear or disable a specific feature */
447184610Salfred	LIBUSB20_REQUEST_CLEAR_FEATURE = 0x01,
448184610Salfred
449184610Salfred	/* 0x02 is reserved */
450184610Salfred
451184610Salfred	/** Set or enable a specific feature */
452184610Salfred	LIBUSB20_REQUEST_SET_FEATURE = 0x03,
453184610Salfred
454184610Salfred	/* 0x04 is reserved */
455184610Salfred
456184610Salfred	/** Set device address for all future accesses */
457184610Salfred	LIBUSB20_REQUEST_SET_ADDRESS = 0x05,
458184610Salfred
459184610Salfred	/** Get the specified descriptor */
460184610Salfred	LIBUSB20_REQUEST_GET_DESCRIPTOR = 0x06,
461184610Salfred
462184610Salfred	/** Used to update existing descriptors or add new descriptors */
463184610Salfred	LIBUSB20_REQUEST_SET_DESCRIPTOR = 0x07,
464184610Salfred
465184610Salfred	/** Get the current device configuration value */
466184610Salfred	LIBUSB20_REQUEST_GET_CONFIGURATION = 0x08,
467184610Salfred
468184610Salfred	/** Set device configuration */
469184610Salfred	LIBUSB20_REQUEST_SET_CONFIGURATION = 0x09,
470184610Salfred
471184610Salfred	/** Return the selected alternate setting for the specified
472184610Salfred	 * interface */
473184610Salfred	LIBUSB20_REQUEST_GET_INTERFACE = 0x0A,
474184610Salfred
475184610Salfred	/** Select an alternate interface for the specified interface */
476184610Salfred	LIBUSB20_REQUEST_SET_INTERFACE = 0x0B,
477184610Salfred
478184610Salfred	/** Set then report an endpoint's synchronization frame */
479184610Salfred	LIBUSB20_REQUEST_SYNCH_FRAME = 0x0C,
480184610Salfred};
481184610Salfred
482184610Salfred/** \ingroup misc
483184610Salfred * Request type bits of the
484184610Salfred * \ref libusb20_control_setup::bmRequestType "bmRequestType" field in
485184610Salfred * control transfers. */
486184610Salfredenum libusb20_request_type {
487184610Salfred	/** Standard */
488184610Salfred	LIBUSB20_REQUEST_TYPE_STANDARD = (0x00 << 5),
489184610Salfred
490184610Salfred	/** Class */
491184610Salfred	LIBUSB20_REQUEST_TYPE_CLASS = (0x01 << 5),
492184610Salfred
493184610Salfred	/** Vendor */
494184610Salfred	LIBUSB20_REQUEST_TYPE_VENDOR = (0x02 << 5),
495184610Salfred
496184610Salfred	/** Reserved */
497184610Salfred	LIBUSB20_REQUEST_TYPE_RESERVED = (0x03 << 5),
498184610Salfred};
499184610Salfred
500184610Salfred/** \ingroup misc
501184610Salfred * Recipient bits of the
502184610Salfred * \ref libusb20_control_setup::bmRequestType "bmRequestType" field in
503184610Salfred * control transfers. Values 4 through 31 are reserved. */
504184610Salfredenum libusb20_request_recipient {
505184610Salfred	/** Device */
506184610Salfred	LIBUSB20_RECIPIENT_DEVICE = 0x00,
507184610Salfred
508184610Salfred	/** Interface */
509184610Salfred	LIBUSB20_RECIPIENT_INTERFACE = 0x01,
510184610Salfred
511184610Salfred	/** Endpoint */
512184610Salfred	LIBUSB20_RECIPIENT_ENDPOINT = 0x02,
513184610Salfred
514184610Salfred	/** Other */
515184610Salfred	LIBUSB20_RECIPIENT_OTHER = 0x03,
516184610Salfred};
517184610Salfred
518184610Salfred#define	LIBUSB20_ISO_SYNC_TYPE_MASK		0x0C
519184610Salfred
520184610Salfred/** \ingroup desc
521184610Salfred * Synchronization type for isochronous endpoints. Values for bits 2:3
522184610Salfred * of the \ref LIBUSB20_ENDPOINT_DESC::bmAttributes "bmAttributes"
523184610Salfred * field in LIBUSB20_ENDPOINT_DESC.
524184610Salfred */
525184610Salfredenum libusb20_iso_sync_type {
526184610Salfred	/** No synchronization */
527184610Salfred	LIBUSB20_ISO_SYNC_TYPE_NONE = 0,
528184610Salfred
529184610Salfred	/** Asynchronous */
530184610Salfred	LIBUSB20_ISO_SYNC_TYPE_ASYNC = 1,
531184610Salfred
532184610Salfred	/** Adaptive */
533184610Salfred	LIBUSB20_ISO_SYNC_TYPE_ADAPTIVE = 2,
534184610Salfred
535184610Salfred	/** Synchronous */
536184610Salfred	LIBUSB20_ISO_SYNC_TYPE_SYNC = 3,
537184610Salfred};
538184610Salfred
539184610Salfred#define	LIBUSB20_ISO_USAGE_TYPE_MASK 0x30
540184610Salfred
541184610Salfred/** \ingroup desc
542184610Salfred * Usage type for isochronous endpoints. Values for bits 4:5 of the
543184610Salfred * \ref LIBUSB20_ENDPOINT_DESC::bmAttributes "bmAttributes" field in
544184610Salfred * LIBUSB20_ENDPOINT_DESC.
545184610Salfred */
546184610Salfredenum libusb20_iso_usage_type {
547184610Salfred	/** Data endpoint */
548184610Salfred	LIBUSB20_ISO_USAGE_TYPE_DATA = 0,
549184610Salfred
550184610Salfred	/** Feedback endpoint */
551184610Salfred	LIBUSB20_ISO_USAGE_TYPE_FEEDBACK = 1,
552184610Salfred
553184610Salfred	/** Implicit feedback Data endpoint */
554184610Salfred	LIBUSB20_ISO_USAGE_TYPE_IMPLICIT = 2,
555184610Salfred};
556184610Salfred
557184610Salfredstruct libusb20_endpoint {
558184610Salfred	struct LIBUSB20_ENDPOINT_DESC_DECODED desc;
559184610Salfred	struct libusb20_me_struct extra;
560184610Salfred} __aligned(sizeof(void *));
561184610Salfred
562184610Salfredstruct libusb20_interface {
563184610Salfred	struct LIBUSB20_INTERFACE_DESC_DECODED desc;
564184610Salfred	struct libusb20_me_struct extra;
565184610Salfred	struct libusb20_interface *altsetting;
566184610Salfred	struct libusb20_endpoint *endpoints;
567184610Salfred	uint8_t	num_altsetting;
568184610Salfred	uint8_t	num_endpoints;
569184610Salfred} __aligned(sizeof(void *));
570184610Salfred
571184610Salfredstruct libusb20_config {
572184610Salfred	struct LIBUSB20_CONFIG_DESC_DECODED desc;
573184610Salfred	struct libusb20_me_struct extra;
574184610Salfred	struct libusb20_interface *interface;
575184610Salfred	uint8_t	num_interface;
576184610Salfred} __aligned(sizeof(void *));
577184610Salfred
578184610Salfreduint8_t	libusb20_me_get_1(const struct libusb20_me_struct *ie, uint16_t offset);
579184610Salfreduint16_t libusb20_me_get_2(const struct libusb20_me_struct *ie, uint16_t offset);
580184610Salfreduint16_t libusb20_me_encode(void *ptr, uint16_t len, const void *pd);
581184610Salfreduint16_t libusb20_me_decode(const void *ptr, uint16_t len, void *pd);
582184610Salfredconst uint8_t *libusb20_desc_foreach(const struct libusb20_me_struct *pdesc, const uint8_t *psubdesc);
583184610Salfredstruct libusb20_config *libusb20_parse_config_desc(const void *config_desc);
584184610Salfred
585184610Salfred#if 0
586184610Salfred{					/* style */
587184610Salfred#endif
588184610Salfred#ifdef __cplusplus
589184610Salfred}
590184610Salfred
591184610Salfred#endif
592184610Salfred
593184610Salfred#endif					/* _LIBUSB20_DESC_H_ */
594