Added USE_INTERNAL_SERIAL compile time option to automatically read out the internal...
[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 /* Enums: */
179 /** Enum for the possible standard descriptor types, as given in each descriptor's header. */
180 enum USB_DescriptorTypes_t
181 {
182 DTYPE_Device = 0x01, /**< Indicates that the descriptor is a device descriptor. */
183 DTYPE_Configuration = 0x02, /**< Indicates that the descriptor is a configuration descriptor. */
184 DTYPE_String = 0x03, /**< Indicates that the descriptor is a string descriptor. */
185 DTYPE_Interface = 0x04, /**< Indicates that the descriptor is an interface descriptor. */
186 DTYPE_Endpoint = 0x05, /**< Indicates that the descriptor is an endpoint descriptor. */
187 DTYPE_DeviceQualifier = 0x06, /**< Indicates that the descriptor is a device qualifier descriptor. */
188 DTYPE_Other = 0x07, /**< Indicates that the descriptor is of other type. */
189 DTYPE_InterfacePower = 0x08, /**< Indicates that the descriptor is an interface power descriptor. */
190 DTYPE_InterfaceAssociation = 0x0B, /**< Indicates that the descriptor is an interface association descriptor. */
191 };
192
193 /* Type Defines: */
194 /** Type define for all descriptor's header, indicating the descriptor's length and type.
195 *
196 * \note The non-standard structure element names are documented here. If the
197 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
198 * with names identical to those listed in the USB standard.
199 */
200 typedef struct
201 {
202 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
203 uint8_t Size; /**< Size of the descriptor, in bytes. */
204 uint8_t Type; /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a value
205 * given by the specific class.
206 */
207 #else
208 uint8_t bLength;
209 uint8_t bDescriptorType;
210 #endif
211 } USB_Descriptor_Header_t;
212
213 /** Type define for a standard device descriptor.
214 *
215 * \note The non-standard structure element names are documented here. If the
216 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
217 * with names identical to those listed in the USB standard.
218 */
219 typedef struct
220 {
221 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
222 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
223
224 uint16_t USBSpecification; /**< BCD of the supported USB specification. */
225 uint8_t Class; /**< USB device class. */
226 uint8_t SubClass; /**< USB device subclass. */
227 uint8_t Protocol; /**< USB device protocol. */
228
229 uint8_t Endpoint0Size; /**< Size of the control (address 0) endpoint's bank in bytes. */
230
231 uint16_t VendorID; /**< Vendor ID for the USB product. */
232 uint16_t ProductID; /**< Unique product ID for the USB product. */
233 uint16_t ReleaseNumber; /**< Product release (version) number. */
234
235 uint8_t ManufacturerStrIndex; /**< String index for the manufacturer's name. The
236 * host will request this string via a separate
237 * control request for the string descriptor.
238 *
239 * \note If no string supplied, use \ref NO_DESCRIPTOR.
240 */
241 uint8_t ProductStrIndex; /**< String index for the product name/details.
242 *
243 * \see ManufacturerStrIndex structure entry.
244 */
245 uint8_t SerialNumStrIndex; /**< String index for the product's globally unique hexadecimal
246 * serial number, in uppercase Unicode ASCII.
247 *
248 * \note On some AVR models, there is an embedded serial number
249 * in the chip which can be used for the device serial number.
250 * To use this serial number, define USE_INTERNAL_SERIAL to a
251 * unique string index number in the project makefile and set
252 * this value to USE_INTERNAL_SERIAL.
253 *
254 * \see ManufacturerStrIndex structure entry.
255 */
256
257 uint8_t NumberOfConfigurations; /**< Total number of configurations supported by
258 * the device.
259 */
260 #else
261 uint8_t bLength;
262 uint8_t bDescriptorType;
263 uint16_t bcdUSB;
264 uint8_t bDeviceClass;
265 uint8_t bDeviceSubClass;
266 uint8_t bDeviceProtocol;
267 uint8_t bMaxPacketSize0;
268 uint16_t idVendor;
269 uint16_t idProduct;
270 uint16_t bcdDevice;
271 uint8_t iManufacturer;
272 uint8_t iProduct;
273 uint8_t iSerialNumber;
274 uint8_t bNumConfigurations;
275 #endif
276 } USB_Descriptor_Device_t;
277
278 /** Type define for a standard configuration descriptor.
279 *
280 * \note The non-standard structure element names are documented here. If the
281 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
282 * with names identical to those listed in the USB standard.
283 */
284 typedef struct
285 {
286 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
287 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
288
289 uint16_t TotalConfigurationSize; /**< Size of the configuration descriptor header,
290 * and all sub descriptors inside the configuration.
291 */
292 uint8_t TotalInterfaces; /**< Total number of interfaces in the configuration. */
293
294 uint8_t ConfigurationNumber; /**< Configuration index of the current configuration. */
295 uint8_t ConfigurationStrIndex; /**< Index of a string descriptor describing the configuration. */
296
297 uint8_t ConfigAttributes; /**< Configuration attributes, comprised of a mask of zero or
298 * more USB_CONFIG_ATTR_* masks.
299 */
300
301 uint8_t MaxPowerConsumption; /**< Maximum power consumption of the device while in the
302 * current configuration, calculated by the \ref USB_CONFIG_POWER_MA()
303 * macro.
304 */
305 #else
306 uint8_t bLength;
307 uint8_t bDescriptorType;
308 uint16_t wTotalLength;
309 uint8_t bNumInterfaces;
310 uint8_t bConfigurationValue;
311 uint8_t iConfiguration;
312 uint8_t bmAttributes;
313 uint8_t bMaxPower;
314 #endif
315 } USB_Descriptor_Configuration_Header_t;
316
317 /** Type define for a standard interface descriptor.
318 *
319 * \note The non-standard structure element names are documented here. If the
320 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
321 * with names identical to those listed in the USB standard.
322 */
323 typedef struct
324 {
325 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
326 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
327
328 uint8_t InterfaceNumber; /**< Index of the interface in the current configuration. */
329 uint8_t AlternateSetting; /**< Alternate setting for the interface number. The same
330 * interface number can have multiple alternate settings
331 * with different endpoint configurations, which can be
332 * selected by the host.
333 */
334 uint8_t TotalEndpoints; /**< Total number of endpoints in the interface. */
335
336 uint8_t Class; /**< Interface class ID. */
337 uint8_t SubClass; /**< Interface subclass ID. */
338 uint8_t Protocol; /**< Interface protocol ID. */
339
340 uint8_t InterfaceStrIndex; /**< Index of the string descriptor describing the
341 * interface.
342 */
343 #else
344 uint8_t bLength;
345 uint8_t bDescriptorType;
346 uint8_t bInterfaceNumber;
347 uint8_t bAlternateSetting;
348 uint8_t bNumEndpoints;
349 uint8_t bInterfaceClass;
350 uint8_t bInterfaceSubClass;
351 uint8_t bInterfaceProtocol;
352 uint8_t iInterface;
353 #endif
354 } USB_Descriptor_Interface_t;
355
356 /** Type define for a standard Interface Association descriptor.
357 *
358 * This descriptor has been added as a supplement to the USB2.0 standard, in the ECN located at
359 * <a>http://www.usb.org/developers/docs/InterfaceAssociationDescriptor_ecn.pdf</a>. It allows compound
360 * devices with multiple interfaces related to the same function to have the multiple interfaces bound
361 * together at the point of enumeration, loading one generic driver for all the interfaces in the single
362 * function. Read the ECN for more information.
363 *
364 * \note The non-standard structure element names are documented here. If the
365 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
366 * with names identical to those listed in the USB standard.
367 */
368 typedef struct
369 {
370 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
371 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
372
373 uint8_t FirstInterfaceIndex; /**< Index of the first associated interface. */
374 uint8_t TotalInterfaces; /** Total number of associated interfaces. */
375
376 uint8_t Class; /**< Interface class ID. */
377 uint8_t SubClass; /**< Interface subclass ID. */
378 uint8_t Protocol; /**< Interface protocol ID. */
379
380 uint8_t IADStrIndex; /**< Index of the string descriptor describing the
381 * interface association.
382 */
383 #else
384 uint8_t bLength;
385 uint8_t bDescriptorType;
386 uint8_t bFirstInterface;
387 uint8_t bInterfaceCount;
388 uint8_t bFunctionClass;
389 uint8_t bFunctionSubClass;
390 uint8_t bFunctionProtocol;
391 uint8_t iFunction;
392 #endif
393 } USB_Descriptor_Interface_Association_t;
394
395 /** Type define for a standard endpoint descriptor.
396 *
397 * \note The non-standard structure element names are documented here. If the
398 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
399 * with names identical to those listed in the USB standard.
400 */
401 typedef struct
402 {
403 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
404 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
405
406 uint8_t EndpointAddress; /**< Logical address of the endpoint within the device
407 * for the current configuration, including direction
408 * mask.
409 */
410 uint8_t Attributes; /**< Endpoint attributes, comprised of a mask of the
411 * endpoint type (EP_TYPE_*) and attributes (ENDPOINT_ATTR_*)
412 * masks.
413 */
414 uint16_t EndpointSize; /**< Size of the endpoint bank, in bytes. This indicates the
415 * maximum packet size that the endpoint can receive at a time.
416 */
417
418 uint8_t PollingIntervalMS; /**< Polling interval in milliseconds for the endpoint
419 * if it is an INTERRUPT or ISOCHRONOUS type.
420 */
421 #else
422 uint8_t bLength;
423 uint8_t bDescriptorType;
424 uint8_t bEndpointAddress;
425 uint8_t bmAttributes;
426 uint16_t wMaxPacketSize;
427 uint8_t bInterval;
428 #endif
429 } USB_Descriptor_Endpoint_t;
430
431 /** Type define for a standard string descriptor. Unlike other standard descriptors, the length
432 * of the descriptor for placement in the descriptor header must be determined by the \ref USB_STRING_LEN()
433 * macro rather than by the size of the descriptor structure, as the length is not fixed.
434 *
435 * This structure should also be used for string index 0, which contains the supported language IDs for
436 * the device as an array.
437 *
438 * \note The non-standard structure element names are documented here. If the
439 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements
440 * with names identical to those listed in the USB standard.
441 */
442 typedef struct
443 {
444 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
445 USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
446
447 int16_t UnicodeString[]; /**< String data, as unicode characters (alternatively,
448 * string language IDs). If normal ASCII characters are
449 * to be used, they must be added as an array of characters
450 * rather than a normal C string so that they are widened to
451 * Unicode size.
452 *
453 * Under GCC, strings prefixed with the "L" character (before
454 * the opening string quotation mark) are considered to be
455 * Unicode strings, and may be used instead of an explicit
456 * array of ASCII characters.
457 */
458 #else
459 uint8_t bLength;
460 uint8_t bDescriptorType;
461 int16_t bString[];
462 #endif
463 } USB_Descriptor_String_t;
464
465 /* Private Interface - For use in library only: */
466 #if !defined(__DOXYGEN__)
467 /* Macros: */
468 #define VERSION_TENS(x) (int)(x / 10)
469 #define VERSION_ONES(x) (int)(x - (10 * VERSION_TENS(x)))
470 #define VERSION_TENTHS(x) (int)((x - (int)x) * 10)
471 #define VERSION_HUNDREDTHS(x) (int)(((x - (int)x) * 100) - (10 * VERSION_TENTHS(x)))
472 #endif
473
474 /* Disable C linkage for C++ Compilers: */
475 #if defined(__cplusplus)
476 }
477 #endif
478
479 #endif
480
481 /** @} */