Fixed GenericHIDHost demo report write routine incorrect for control type requests...
[pub/USBasp.git] / LUFA / Drivers / USB / HighLevel / StdDescriptors.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Standard USB device descriptor defines and retrieval routines, for USB devices. This module contains
34 * structures and macros for the easy creation of standard USB descriptors in USB device projects.
35 *
36 * All standard descriptors have their elements named in an identical manner to the official USB specification,
37 * however slightly more verbose alternate (non-standard) names are also supplied if the macro
38 * USE_NONSTANDARD_DESCRIPTOR_NAMES is defined in the user project makefile and passed to the compiler at
39 * compilation time using the -D option.
40 *
41 * The non-standard names are documented here - if USE_NONSTANDARD_DESCRIPTOR_NAMES is not defined, then all
42 * descriptors will contain elements named identically to the official USB specification. The alternately
43 * named descriptor elements are placed in the same order inside the descriptor structures as their officially
44 * named counterparts, thus they can be correlated easily with the official USB specification.
45 */
46
47 /** \ingroup Group_USB
48 * @defgroup Group_Descriptors USB Descriptors
49 *
50 * Functions, macros, variables, enums and types related to standard USB descriptors.
51 *
52 * @{
53 */
54
55 #ifndef __USBDESCRIPTORS_H__
56 #define __USBDESCRIPTORS_H__
57
58 /* Includes: */
59 #include <avr/pgmspace.h>
60 #include <stdbool.h>
61
62 #include "../../../Common/Common.h"
63 #include "USBMode.h"
64 #include "Events.h"
65
66 #if defined(USB_CAN_BE_DEVICE)
67 #include "../LowLevel/Device.h"
68 #endif
69
70 /* Enable C linkage for C++ Compilers: */
71 #if defined(__cplusplus)
72 extern "C" {
73 #endif
74
75 /* Public Interface - May be used in end-application: */
76 /* Macros: */
77 /** Indicates that a given descriptor does not exist in the device. This can be used inside descriptors
78 * for string descriptor indexes, or may be use as a return value for GetDescriptor when the specified
79 * descriptor does not exist.
80 */
81 #define NO_DESCRIPTOR 0
82
83 /** Macro to calculate the power value for the device descriptor, from a given number of milliamps. */
84 #define USB_CONFIG_POWER_MA(x) (x >> 1)
85
86 /** Macro to calculate the Unicode length of a string with a given number of Unicode characters.
87 * Should be used in string descriptor's headers for giving the string descriptor's byte length.
88 */
89 #define USB_STRING_LEN(x) (sizeof(USB_Descriptor_Header_t) + (x << 1))
90
91 /** Macro to encode a given four digit floating point version number (e.g. 01.23) into Binary Coded
92 * Decimal format for descriptor fields requiring BCD encoding, such as the USB version number in the
93 * standard device descriptor.
94 */
95 #define VERSION_BCD(x) ((((VERSION_TENS(x) << 4) | VERSION_ONES(x)) << 8) | \
96 ((VERSION_TENTHS(x) << 4) | VERSION_HUNDREDTHS(x)))
97
98 /** String language ID for the English language. Should be used in USB_Descriptor_Language_t descriptors
99 * to indicate that the English language is supported by the device in its string descriptors.
100 */
101 #define LANGUAGE_ID_ENG 0x0409
102
103 /** Can be masked with an endpoint address for a USB_Descriptor_Endpoint_t endpoint descriptor's
104 * EndpointAddress value to indicate to the host that the endpoint is of the IN direction (i.e, from
105 * device to host).
106 */
107 #define ENDPOINT_DESCRIPTOR_DIR_IN 0x80
108
109 /** Can be masked with an endpoint address for a USB_Descriptor_Endpoint_t endpoint descriptor's
110 * EndpointAddress value to indicate to the host that the endpoint is of the OUT direction (i.e, from
111 * host to device).
112 */
113 #define ENDPOINT_DESCRIPTOR_DIR_OUT 0x00
114
115 /** Can be masked with other configuration descriptor attributes for a USB_Descriptor_Configuration_Header_t
116 * descriptor's ConfigAttributes value to indicate that the specified configuration can draw its power
117 * from the host's VBUS line.
118 */
119 #define USB_CONFIG_ATTR_BUSPOWERED 0x80
120
121 /** Can be masked with other configuration descriptor attributes for a USB_Descriptor_Configuration_Header_t
122 * descriptor's ConfigAttributes value to indicate that the specified configuration can draw its power
123 * from the device's own power source.
124 */
125 #define USB_CONFIG_ATTR_SELFPOWERED 0xC0
126
127 /** Can be masked with other configuration descriptor attributes for a USB_Descriptor_Configuration_Header_t
128 * descriptor's ConfigAttributes value to indicate that the specified configuration supports the
129 * remote wakeup feature of the USB standard, allowing a suspended USB device to wake up the host upon
130 * request.
131 */
132 #define USB_CONFIG_ATTR_REMOTEWAKEUP 0xA0
133
134 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
135 * Attributes value to indicate that the specified endpoint is not synchronized.
136 *
137 * \see The USB specification for more details on the possible Endpoint attributes.
138 */
139 #define ENDPOINT_ATTR_NO_SYNC (0 << 2)
140
141 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
142 * Attributes value to indicate that the specified endpoint is asynchronous.
143 *
144 * \see The USB specification for more details on the possible Endpoint attributes.
145 */
146 #define ENDPOINT_ATTR_ASYNC (1 << 2)
147
148 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
149 * Attributes value to indicate that the specified endpoint is adaptive.
150 *
151 * \see The USB specification for more details on the possible Endpoint attributes.
152 */
153 #define ENDPOINT_ATTR_ADAPTIVE (2 << 2)
154
155 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
156 * Attributes value to indicate that the specified endpoint is synchronized.
157 *
158 * \see The USB specification for more details on the possible Endpoint attributes.
159 */
160 #define ENDPOINT_ATTR_SYNC (3 << 2)
161
162 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
163 * Attributes value to indicate that the specified endpoint is used for data transfers.
164 *
165 * \see The USB specification for more details on the possible Endpoint usage attributes.
166 */
167 #define ENDPOINT_USAGE_DATA (0 << 4)
168
169 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
170 * Attributes value to indicate that the specified endpoint is used for feedback.
171 *
172 * \see The USB specification for more details on the possible Endpoint usage attributes.
173 */
174 #define ENDPOINT_USAGE_FEEDBACK (1 << 4)
175
176 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
177 * Attributes value to indicate that the specified endpoint is used for implicit feedback.
178 *
179 * \see The USB specification for more details on the possible Endpoint usage attributes.
180 */
181 #define ENDPOINT_USAGE_IMPLICIT_FEEDBACK (2 << 4)
182
183 /** Gives a void pointer to the specified descriptor (of any type). */
184 #define DESCRIPTOR_ADDRESS(Descriptor) ((void*)&Descriptor)
185
186 /* Events: */
187 #if defined(USB_CAN_BE_DEVICE) || defined(__DOXYGEN__)
188 /** This module raises the Device Error event while in device mode, if the USB_GetDescriptor()
189 * routine is not hooked in the user application to properly return descriptors to the library.
190 *
191 * \see Events.h for more information on this event.
192 */
193 RAISES_EVENT(USB_DeviceError);
194 #endif
195
196 /* Enums: */
197 /** Enum for the possible standard descriptor types, as given in each descriptor's header. */
198 enum USB_DescriptorTypes_t
199 {
200 DTYPE_Device = 0x01, /**< Indicates that the descriptor is a device descriptor. */
201 DTYPE_Configuration = 0x02, /**< Indicates that the descriptor is a configuration descriptor. */
202 DTYPE_String = 0x03, /**< Indicates that the descriptor is a string descriptor. */
203 DTYPE_Interface = 0x04, /**< Indicates that the descriptor is an interface descriptor. */
204 DTYPE_Endpoint = 0x05, /**< Indicates that the descriptor is an endpoint descriptor. */
205 DTYPE_DeviceQualifier = 0x06, /**< Indicates that the descriptor is a device qualifier descriptor. */
206 DTYPE_Other = 0x07, /**< Indicates that the descriptor is of other type. */
207 DTYPE_InterfacePower = 0x08, /**< Indicates that the descriptor is an interface power descriptor. */
208 DTYPE_InterfaceAssociation = 0x0B, /**< Indicates that the descriptor is an interface association descriptor. */
209 };
210
211 /* Type Defines: */
212 /** Type define for all descriptor's header, indicating the descriptor's length and type.
213 *
214 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
215 * documentation for more information on the two descriptor naming schemes. If the
216 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
217 * identical to those listed in the USB standard.
218 */
219 typedef struct
220 {
221 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
222 uint8_t Size; /**< Size of the descriptor, in bytes. */
223 uint8_t Type; /**< Type of the descriptor, either a value in DescriptorTypes_t or a value
224 * given by the specific class.
225 */
226 #else
227 uint8_t bLength;
228 uint8_t bDescriptorType;
229 #endif
230 } USB_Descriptor_Header_t;
231
232 /** Type define for a standard device descriptor.
233 *
234 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
235 * documentation for more information on the two descriptor naming schemes. If the
236 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
237 * identical to those listed in the USB standard.
238 */
239 typedef struct
240 {
241 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
242 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
243
244 uint16_t USBSpecification; /**< BCD of the supported USB specification. */
245 uint8_t Class; /**< USB device class. */
246 uint8_t SubClass; /**< USB device subclass. */
247 uint8_t Protocol; /**< USB device protocol. */
248
249 uint8_t Endpoint0Size; /**< Size of the control (address 0) endpoint's bank in bytes. */
250
251 uint16_t VendorID; /**< Vendor ID for the USB product. */
252 uint16_t ProductID; /**< Unique product ID for the USB product. */
253 uint16_t ReleaseNumber; /**< Product release (version) number. */
254
255 uint8_t ManufacturerStrIndex; /**< String index for the manufacturer's name. The
256 * host will request this string via a separate
257 * control request for the string descriptor.
258 *
259 * \note If no string supplied, use NO_DESCRIPTOR.
260 */
261 uint8_t ProductStrIndex; /**< String index for the product name/details.
262 *
263 * \see ManufacturerStrIndex structure entry.
264 */
265 uint8_t SerialNumStrIndex; /**< String index for the product's globally unique hexadecimal
266 * serial number, in uppercase Unicode ASCII.
267 *
268 * \see ManufacturerStrIndex structure entry.
269 */
270
271 uint8_t NumberOfConfigurations; /**< Total number of configurations supported by
272 * the device.
273 */
274 #else
275 uint8_t bLength;
276 uint8_t bDescriptorType;
277 uint16_t bcdUSB;
278 uint8_t bDeviceClass;
279 uint8_t bDeviceSubClass;
280 uint8_t bDeviceProtocol;
281 uint8_t bMaxPacketSize0;
282 uint16_t idVendor;
283 uint16_t idProduct;
284 uint16_t bcdDevice;
285 uint8_t iManufacturer;
286 uint8_t iProduct;
287 uint8_t iSerialNumber;
288 uint8_t bNumConfigurations;
289 #endif
290 } USB_Descriptor_Device_t;
291
292 /** Type define for a standard configuration descriptor.
293 *
294 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
295 * documentation for more information on the two descriptor naming schemes. If the
296 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
297 * identical to those listed in the USB standard.
298 */
299 typedef struct
300 {
301 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
302 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
303
304 uint16_t TotalConfigurationSize; /**< Size of the configuration descriptor header,
305 * and all sub descriptors inside the configuration.
306 */
307 uint8_t TotalInterfaces; /**< Total number of interfaces in the configuration. */
308
309 uint8_t ConfigurationNumber; /**< Configuration index of the current configuration. */
310 uint8_t ConfigurationStrIndex; /**< Index of a string descriptor describing the configuration. */
311
312 uint8_t ConfigAttributes; /**< Configuration attributes, comprised of a mask of zero or
313 * more USB_CONFIG_ATTR_* masks.
314 */
315
316 uint8_t MaxPowerConsumption; /**< Maximum power consumption of the device while in the
317 * current configuration, calculated by the USB_CONFIG_POWER_MA()
318 * macro.
319 */
320 #else
321 uint8_t bLength;
322 uint8_t bDescriptorType;
323 uint16_t wTotalLength;
324 uint8_t bNumInterfaces;
325 uint8_t bConfigurationValue;
326 uint8_t iConfiguration;
327 uint8_t bmAttributes;
328 uint8_t bMaxPower;
329 #endif
330 } USB_Descriptor_Configuration_Header_t;
331
332 /** Type define for a standard interface descriptor.
333 *
334 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
335 * documentation for more information on the two descriptor naming schemes. If the
336 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
337 * identical to those listed in the USB standard.
338 */
339 typedef struct
340 {
341 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
342 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
343
344 uint8_t InterfaceNumber; /**< Index of the interface in the current configuration. */
345 uint8_t AlternateSetting; /**< Alternate setting for the interface number. The same
346 * interface number can have multiple alternate settings
347 * with different endpoint configurations, which can be
348 * selected by the host.
349 */
350 uint8_t TotalEndpoints; /**< Total number of endpoints in the interface. */
351
352 uint8_t Class; /**< Interface class ID. */
353 uint8_t SubClass; /**< Interface subclass ID. */
354 uint8_t Protocol; /**< Interface protocol ID. */
355
356 uint8_t InterfaceStrIndex; /**< Index of the string descriptor describing the
357 * interface.
358 */
359 #else
360 uint8_t bLength;
361 uint8_t bDescriptorType;
362 uint8_t bInterfaceNumber;
363 uint8_t bAlternateSetting;
364 uint8_t bNumEndpoints;
365 uint8_t bInterfaceClass;
366 uint8_t bInterfaceSubClass;
367 uint8_t bInterfaceProtocol;
368 uint8_t iInterface;
369 #endif
370 } USB_Descriptor_Interface_t;
371
372 /** Type define for a standard interface association descriptor.
373 *
374 * This descriptor has been added as a supplement to the USB2.0 standard, in the ECN located at
375 * <a>http://www.usb.org/developers/docs/InterfaceAssociationDescriptor_ecn.pdf</a>. It allows compound
376 * devices with multiple interfaces related to the same function to have the multiple interfaces bound
377 * together at the point of enumeration, loading one generic driver for all the interfaces in the single
378 * function. Read the ECN for more information.
379 *
380 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
381 * documentation for more information on the two descriptor naming schemes. If the
382 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
383 * identical to those listed in the USB standard.
384 */
385 typedef struct
386 {
387 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
388 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
389
390 uint8_t FirstInterfaceIndex; /**< Index of the first associated interface. */
391 uint8_t TotalInterfaces; /** Total number of associated interfaces. */
392
393 uint8_t Class; /**< Interface class ID. */
394 uint8_t SubClass; /**< Interface subclass ID. */
395 uint8_t Protocol; /**< Interface protocol ID. */
396
397 uint8_t IADStrIndex; /**< Index of the string descriptor describing the
398 * interface association.
399 */
400 #else
401 uint8_t bLength;
402 uint8_t bDescriptorType;
403 uint8_t bFirstInterface;
404 uint8_t bInterfaceCount;
405 uint8_t bFunctionClass;
406 uint8_t bFunctionSubClass;
407 uint8_t bFunctionProtocol;
408 uint8_t iFunction;
409 #endif
410 } USB_Descriptor_Interface_Association_t;
411
412 /** Type define for a standard endpoint descriptor.
413 *
414 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
415 * documentation for more information on the two descriptor naming schemes. If the
416 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
417 * identical to those listed in the USB standard.
418 */
419 typedef struct
420 {
421 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
422 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
423
424 uint8_t EndpointAddress; /**< Logical address of the endpoint within the device
425 * for the current configuration, including direction
426 * mask.
427 */
428 uint8_t Attributes; /**< Endpoint attributes, comprised of a mask of the
429 * endpoint type (EP_TYPE_*) and attributes (ENDPOINT_ATTR_*)
430 * masks.
431 */
432 uint16_t EndpointSize; /**< Size of the endpoint bank, in bytes. This indicates the
433 * maximum packet size that the endpoint can receive at a time.
434 */
435
436 uint8_t PollingIntervalMS; /**< Polling interval in milliseconds for the endpoint
437 * if it is an INTERRUPT or ISOCHRONOUS type.
438 */
439 #else
440 uint8_t bLength;
441 uint8_t bDescriptorType;
442 uint8_t bEndpointAddress;
443 uint8_t bmAttributes;
444 uint16_t wMaxPacketSize;
445 uint8_t bInterval;
446 #endif
447 } USB_Descriptor_Endpoint_t;
448
449 /** Type define for a standard string descriptor. Unlike other standard descriptors, the length
450 * of the descriptor for placement in the descriptor header must be determined by the USB_STRING_LEN()
451 * macro rather than by the size of the descriptor structure, as the length is not fixed.
452 *
453 * This structure should also be used for string index 0, which contains the supported language IDs for
454 * the device as an array.
455 *
456 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
457 * documentation for more information on the two descriptor naming schemes. If the
458 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
459 * identical to those listed in the USB standard.
460 */
461 typedef struct
462 {
463 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
464 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
465
466 int16_t UnicodeString[]; /**< String data, as unicode characters (alternatively,
467 * string language IDs). If normal ASCII characters are
468 * to be used, they must be added as an array of characters
469 * rather than a normal C string so that they are widened to
470 * Unicode size.
471 *
472 * Under GCC, strings prefixed with the "L" character (before
473 * the opening string quotation mark) are considered to be
474 * Unicode strings, and may be used instead of an explicit
475 * array of ASCII characters.
476 */
477 #else
478 uint8_t bLength;
479 uint8_t bDescriptorType;
480 int16_t bString[];
481 #endif
482 } USB_Descriptor_String_t;
483
484 /* Function Prototypes: */
485 /** Function to retrieve a given descriptor's size and memory location from the given descriptor type value,
486 * index and language ID. This function MUST be overridden in the user application (added with full, identical
487 * prototype and name except for the ATTR_WEAK attribute) so that the library can call it to retrieve descriptor
488 * data.
489 *
490 * \param wValue The type of the descriptor to retrieve in the upper byte, and the index in the
491 * lower byte (when more than one descriptor of the given type exists, such as the
492 * case of string descriptors). The type may be one of the standard types defined
493 * in the DescriptorTypes_t enum, or may be a class-specific descriptor type value.
494 * \param wIndex The language ID of the string to return if the wValue type indicates DTYPE_String,
495 * otherwise zero for standard descriptors, or as defined in a class-specific
496 * standards.
497 * \param DescriptorAddress Pointer to the descriptor in memory. This should be set by the routine to
498 * the location of the descriptor, found by the DESCRIPTOR_ADDRESS macro.
499 *
500 * \note By default, the library expects all descriptors to be located in flash memory via the PROGMEM attribute.
501 * If descriptors should be located in RAM or EEPROM instead (to speed up access in the case of RAM, or to
502 * allow the descriptors to be changed dynamically at runtime) either the USE_RAM_DESCRIPTORS or the
503 * USE_EEPROM_DESCRIPTORS tokens may be defined in the project makefile and passed to the compiler by the -D
504 * switch.
505 *
506 * \return Size in bytes of the descriptor if it exists, zero or NO_DESCRIPTOR otherwise
507 */
508 uint16_t USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress)
509 ATTR_WARN_UNUSED_RESULT ATTR_WEAK ATTR_NON_NULL_PTR_ARG(3);
510
511 /* Private Interface - For use in library only: */
512 #if !defined(__DOXYGEN__)
513 /* Macros: */
514 #define VERSION_TENS(x) (int)(x / 10)
515 #define VERSION_ONES(x) (int)(x - (10 * VERSION_TENS(x)))
516 #define VERSION_TENTHS(x) (int)((x - (int)x) * 10)
517 #define VERSION_HUNDREDTHS(x) (int)(((x - (int)x) * 100) - (10 * VERSION_TENTHS(x)))
518 #endif
519
520 /* Disable C linkage for C++ Compilers: */
521 #if defined(__cplusplus)
522 }
523 #endif
524
525 #endif
526
527 /** @} */