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