Fix redefinition warning when USB_DEVICE_ONLY is set on an AVR which does not support...
[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 #if (!defined(NO_INTERNAL_SERIAL) && (defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))) || defined(__DOXYGEN__)
79 /** String descriptor index for the device's unique serial number string descriptor within the device.
80 * This unique serial number is used by the host to associate resources to the device (such as drivers or COM port
81 * number allocations) to a device regardless of the port it is plugged in to on the host. Some USB AVRs contain
82 * a unique serial number internally, and setting the device descriptors serial number string index to this value
83 * will cause it to use the internal serial number.
84 *
85 * On unsupported devices, this will evaluate to NO_DESCRIPTOR and so will force the host to create a pseduo-serial
86 * number for the device.
87 */
88 #define USE_INTERNAL_SERIAL 0xDC
89 #else
90 #define USE_INTERNAL_SERIAL NO_DESCRIPTOR
91 #endif
92
93 /** Macro to calculate the power value for the device descriptor, from a given number of milliamps. */
94 #define USB_CONFIG_POWER_MA(mA) (mA >> 1)
95
96 /** Macro to calculate the Unicode length of a string with a given number of Unicode characters.
97 * Should be used in string descriptor's headers for giving the string descriptor's byte length.
98 */
99 #define USB_STRING_LEN(str) (sizeof(USB_Descriptor_Header_t) + (str << 1))
100
101 /** Macro to encode a given four digit floating point version number (e.g. 01.23) into Binary Coded
102 * Decimal format for descriptor fields requiring BCD encoding, such as the USB version number in the
103 * standard device descriptor.
104 */
105 #define VERSION_BCD(x) ((((VERSION_TENS(x) << 4) | VERSION_ONES(x)) << 8) | \
106 ((VERSION_TENTHS(x) << 4) | VERSION_HUNDREDTHS(x)))
107
108 /** String language ID for the English language. Should be used in \ref USB_Descriptor_String_t descriptors
109 * to indicate that the English language is supported by the device in its string descriptors.
110 */
111 #define LANGUAGE_ID_ENG 0x0409
112
113 /** Can be masked with an endpoint address for a \ref USB_Descriptor_Endpoint_t endpoint descriptor's
114 * EndpointAddress value to indicate to the host that the endpoint is of the IN direction (i.e, from
115 * device to host).
116 */
117 #define ENDPOINT_DESCRIPTOR_DIR_IN 0x80
118
119 /** Can be masked with an endpoint address for a \ref USB_Descriptor_Endpoint_t endpoint descriptor's
120 * EndpointAddress value to indicate to the host that the endpoint is of the OUT direction (i.e, from
121 * host to device).
122 */
123 #define ENDPOINT_DESCRIPTOR_DIR_OUT 0x00
124
125 /** Can be masked with other configuration descriptor attributes for a \ref USB_Descriptor_Configuration_Header_t
126 * descriptor's ConfigAttributes value to indicate that the specified configuration can draw its power
127 * from the host's VBUS line.
128 */
129 #define USB_CONFIG_ATTR_BUSPOWERED 0x80
130
131 /** Can be masked with other configuration descriptor attributes for a \ref USB_Descriptor_Configuration_Header_t
132 * descriptor's ConfigAttributes value to indicate that the specified configuration can draw its power
133 * from the device's own power source.
134 */
135 #define USB_CONFIG_ATTR_SELFPOWERED 0xC0
136
137 /** Can be masked with other configuration descriptor attributes for a \ref USB_Descriptor_Configuration_Header_t
138 * descriptor's ConfigAttributes value to indicate that the specified configuration supports the
139 * remote wakeup feature of the USB standard, allowing a suspended USB device to wake up the host upon
140 * request.
141 */
142 #define USB_CONFIG_ATTR_REMOTEWAKEUP 0xA0
143
144 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
145 * Attributes value to indicate that the specified endpoint is not synchronized.
146 *
147 * \see The USB specification for more details on the possible Endpoint attributes.
148 */
149 #define ENDPOINT_ATTR_NO_SYNC (0 << 2)
150
151 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
152 * Attributes value to indicate that the specified endpoint is asynchronous.
153 *
154 * \see The USB specification for more details on the possible Endpoint attributes.
155 */
156 #define ENDPOINT_ATTR_ASYNC (1 << 2)
157
158 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
159 * Attributes value to indicate that the specified endpoint is adaptive.
160 *
161 * \see The USB specification for more details on the possible Endpoint attributes.
162 */
163 #define ENDPOINT_ATTR_ADAPTIVE (2 << 2)
164
165 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
166 * Attributes value to indicate that the specified endpoint is synchronized.
167 *
168 * \see The USB specification for more details on the possible Endpoint attributes.
169 */
170 #define ENDPOINT_ATTR_SYNC (3 << 2)
171
172 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
173 * Attributes value to indicate that the specified endpoint is used for data transfers.
174 *
175 * \see The USB specification for more details on the possible Endpoint usage attributes.
176 */
177 #define ENDPOINT_USAGE_DATA (0 << 4)
178
179 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
180 * Attributes value to indicate that the specified endpoint is used for feedback.
181 *
182 * \see The USB specification for more details on the possible Endpoint usage attributes.
183 */
184 #define ENDPOINT_USAGE_FEEDBACK (1 << 4)
185
186 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
187 * Attributes value to indicate that the specified endpoint is used for implicit feedback.
188 *
189 * \see The USB specification for more details on the possible Endpoint usage attributes.
190 */
191 #define ENDPOINT_USAGE_IMPLICIT_FEEDBACK (2 << 4)
192
193 /* Enums: */
194 /** Enum for the possible standard descriptor types, as given in each descriptor's header. */
195 enum USB_DescriptorTypes_t
196 {
197 DTYPE_Device = 0x01, /**< Indicates that the descriptor is a device descriptor. */
198 DTYPE_Configuration = 0x02, /**< Indicates that the descriptor is a configuration descriptor. */
199 DTYPE_String = 0x03, /**< Indicates that the descriptor is a string descriptor. */
200 DTYPE_Interface = 0x04, /**< Indicates that the descriptor is an interface descriptor. */
201 DTYPE_Endpoint = 0x05, /**< Indicates that the descriptor is an endpoint descriptor. */
202 DTYPE_DeviceQualifier = 0x06, /**< Indicates that the descriptor is a device qualifier descriptor. */
203 DTYPE_Other = 0x07, /**< Indicates that the descriptor is of other type. */
204 DTYPE_InterfacePower = 0x08, /**< Indicates that the descriptor is an interface power descriptor. */
205 DTYPE_InterfaceAssociation = 0x0B, /**< Indicates that the descriptor is an interface association descriptor. */
206 };
207
208 /* Type Defines: */
209 /** Type define for all descriptor's header, indicating the descriptor's length and type.
210 *
211 * \note The non-standard structure element names are documented here. If the
212 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
213 * with names identical to those listed in the USB standard.
214 */
215 typedef struct
216 {
217 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
218 uint8_t Size; /**< Size of the descriptor, in bytes. */
219 uint8_t Type; /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a value
220 * given by the specific class.
221 */
222 #else
223 uint8_t bLength;
224 uint8_t bDescriptorType;
225 #endif
226 } USB_Descriptor_Header_t;
227
228 /** Type define for a standard device descriptor.
229 *
230 * \note The non-standard structure element names are documented here. If the
231 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
232 * with names identical to those listed in the USB standard.
233 */
234 typedef struct
235 {
236 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
237 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
238
239 uint16_t USBSpecification; /**< BCD of the supported USB specification. */
240 uint8_t Class; /**< USB device class. */
241 uint8_t SubClass; /**< USB device subclass. */
242 uint8_t Protocol; /**< USB device protocol. */
243
244 uint8_t Endpoint0Size; /**< Size of the control (address 0) endpoint's bank in bytes. */
245
246 uint16_t VendorID; /**< Vendor ID for the USB product. */
247 uint16_t ProductID; /**< Unique product ID for the USB product. */
248 uint16_t ReleaseNumber; /**< Product release (version) number. */
249
250 uint8_t ManufacturerStrIndex; /**< String index for the manufacturer's name. The
251 * host will request this string via a separate
252 * control request for the string descriptor.
253 *
254 * \note If no string supplied, use \ref NO_DESCRIPTOR.
255 */
256 uint8_t ProductStrIndex; /**< String index for the product name/details.
257 *
258 * \see ManufacturerStrIndex structure entry.
259 */
260 uint8_t SerialNumStrIndex; /**< String index for the product's globally unique hexadecimal
261 * serial number, in uppercase Unicode ASCII.
262 *
263 * \note On some AVR models, there is an embedded serial number
264 * in the chip which can be used for the device serial number.
265 * To use this serial number, set this to USE_INTERNAL_SERIAL.
266 * On unsupported devices, this will evaluate to 0 and will cause
267 * the host to generate a pseudo-unique value for the device upon
268 * insertion.
269 *
270 * \see ManufacturerStrIndex structure entry.
271 */
272
273 uint8_t NumberOfConfigurations; /**< Total number of configurations supported by
274 * the device.
275 */
276 #else
277 uint8_t bLength;
278 uint8_t bDescriptorType;
279 uint16_t bcdUSB;
280 uint8_t bDeviceClass;
281 uint8_t bDeviceSubClass;
282 uint8_t bDeviceProtocol;
283 uint8_t bMaxPacketSize0;
284 uint16_t idVendor;
285 uint16_t idProduct;
286 uint16_t bcdDevice;
287 uint8_t iManufacturer;
288 uint8_t iProduct;
289 uint8_t iSerialNumber;
290 uint8_t bNumConfigurations;
291 #endif
292 } USB_Descriptor_Device_t;
293
294 /** Type define for a standard configuration descriptor.
295 *
296 * \note The non-standard structure element names are documented here. If the
297 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
298 * with names identical to those listed in the USB standard.
299 */
300 typedef struct
301 {
302 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
303 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
304
305 uint16_t TotalConfigurationSize; /**< Size of the configuration descriptor header,
306 * and all sub descriptors inside the configuration.
307 */
308 uint8_t TotalInterfaces; /**< Total number of interfaces in the configuration. */
309
310 uint8_t ConfigurationNumber; /**< Configuration index of the current configuration. */
311 uint8_t ConfigurationStrIndex; /**< Index of a string descriptor describing the configuration. */
312
313 uint8_t ConfigAttributes; /**< Configuration attributes, comprised of a mask of zero or
314 * more USB_CONFIG_ATTR_* masks.
315 */
316
317 uint8_t MaxPowerConsumption; /**< Maximum power consumption of the device while in the
318 * current configuration, calculated by the \ref USB_CONFIG_POWER_MA()
319 * macro.
320 */
321 #else
322 uint8_t bLength;
323 uint8_t bDescriptorType;
324 uint16_t wTotalLength;
325 uint8_t bNumInterfaces;
326 uint8_t bConfigurationValue;
327 uint8_t iConfiguration;
328 uint8_t bmAttributes;
329 uint8_t bMaxPower;
330 #endif
331 } USB_Descriptor_Configuration_Header_t;
332
333 /** Type define for a standard interface descriptor.
334 *
335 * \note The non-standard structure element names are documented here. If the
336 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
337 * with names 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. If the
381 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
382 * with names identical to those listed in the USB standard.
383 */
384 typedef struct
385 {
386 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
387 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
388
389 uint8_t FirstInterfaceIndex; /**< Index of the first associated interface. */
390 uint8_t TotalInterfaces; /** Total number of associated interfaces. */
391
392 uint8_t Class; /**< Interface class ID. */
393 uint8_t SubClass; /**< Interface subclass ID. */
394 uint8_t Protocol; /**< Interface protocol ID. */
395
396 uint8_t IADStrIndex; /**< Index of the string descriptor describing the
397 * interface association.
398 */
399 #else
400 uint8_t bLength;
401 uint8_t bDescriptorType;
402 uint8_t bFirstInterface;
403 uint8_t bInterfaceCount;
404 uint8_t bFunctionClass;
405 uint8_t bFunctionSubClass;
406 uint8_t bFunctionProtocol;
407 uint8_t iFunction;
408 #endif
409 } USB_Descriptor_Interface_Association_t;
410
411 /** Type define for a standard endpoint descriptor.
412 *
413 * \note The non-standard structure element names are documented here. If the
414 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
415 * with names identical to those listed in the USB standard.
416 */
417 typedef struct
418 {
419 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
420 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
421
422 uint8_t EndpointAddress; /**< Logical address of the endpoint within the device
423 * for the current configuration, including direction
424 * mask.
425 */
426 uint8_t Attributes; /**< Endpoint attributes, comprised of a mask of the
427 * endpoint type (EP_TYPE_*) and attributes (ENDPOINT_ATTR_*)
428 * masks.
429 */
430 uint16_t EndpointSize; /**< Size of the endpoint bank, in bytes. This indicates the
431 * maximum packet size that the endpoint can receive at a time.
432 */
433
434 uint8_t PollingIntervalMS; /**< Polling interval in milliseconds for the endpoint
435 * if it is an INTERRUPT or ISOCHRONOUS type.
436 */
437 #else
438 uint8_t bLength;
439 uint8_t bDescriptorType;
440 uint8_t bEndpointAddress;
441 uint8_t bmAttributes;
442 uint16_t wMaxPacketSize;
443 uint8_t bInterval;
444 #endif
445 } USB_Descriptor_Endpoint_t;
446
447 /** Type define for a standard string descriptor. Unlike other standard descriptors, the length
448 * of the descriptor for placement in the descriptor header must be determined by the \ref USB_STRING_LEN()
449 * macro rather than by the size of the descriptor structure, as the length is not fixed.
450 *
451 * This structure should also be used for string index 0, which contains the supported language IDs for
452 * the device as an array.
453 *
454 * \note The non-standard structure element names are documented here. If the
455 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
456 * with names identical to those listed in the USB standard.
457 */
458 typedef struct
459 {
460 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
461 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
462
463 int16_t UnicodeString[]; /**< String data, as unicode characters (alternatively,
464 * string language IDs). If normal ASCII characters are
465 * to be used, they must be added as an array of characters
466 * rather than a normal C string so that they are widened to
467 * Unicode size.
468 *
469 * Under GCC, strings prefixed with the "L" character (before
470 * the opening string quotation mark) are considered to be
471 * Unicode strings, and may be used instead of an explicit
472 * array of ASCII characters.
473 */
474 #else
475 uint8_t bLength;
476 uint8_t bDescriptorType;
477 int16_t bString[];
478 #endif
479 } USB_Descriptor_String_t;
480
481 /* Private Interface - For use in library only: */
482 #if !defined(__DOXYGEN__)
483 /* Macros: */
484 #define VERSION_TENS(x) (int)(x / 10)
485 #define VERSION_ONES(x) (int)(x - (10 * VERSION_TENS(x)))
486 #define VERSION_TENTHS(x) (int)((x - (int)x) * 10)
487 #define VERSION_HUNDREDTHS(x) (int)(((x - (int)x) * 100) - (10 * VERSION_TENTHS(x)))
488 #endif
489
490 /* Disable C linkage for C++ Compilers: */
491 #if defined(__cplusplus)
492 }
493 #endif
494
495 #endif
496
497 /** @} */