3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 /** \ingroup Group_USB
32 * @defgroup Group_Descriptors USB Descriptors
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.
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.
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.
50 #ifndef __USBDESCRIPTORS_H__
51 #define __USBDESCRIPTORS_H__
54 #include <avr/pgmspace.h>
57 #include "../../../Common/Common.h"
61 #if defined(USB_CAN_BE_DEVICE)
62 #include "../LowLevel/Device.h"
65 /* Enable C linkage for C++ Compilers: */
66 #if defined(__cplusplus)
70 /* Public Interface - May be used in end-application: */
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.
76 #define NO_DESCRIPTOR 0
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.
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.
88 #define USE_INTERNAL_SERIAL 0xDC
90 #define USE_INTERNAL_SERIAL NO_DESCRIPTOR
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)
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.
99 #define USB_STRING_LEN(str) (sizeof(USB_Descriptor_Header_t) + (str << 1))
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.
105 #define VERSION_BCD(x) ((((VERSION_TENS(x) << 4) | VERSION_ONES(x)) << 8) | \
106 ((VERSION_TENTHS(x) << 4) | VERSION_HUNDREDTHS(x)))
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.
111 #define LANGUAGE_ID_ENG 0x0409
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
117 #define ENDPOINT_DESCRIPTOR_DIR_IN 0x80
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
123 #define ENDPOINT_DESCRIPTOR_DIR_OUT 0x00
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.
129 #define USB_CONFIG_ATTR_BUSPOWERED 0x80
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.
135 #define USB_CONFIG_ATTR_SELFPOWERED 0xC0
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
142 #define USB_CONFIG_ATTR_REMOTEWAKEUP 0xA0
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.
147 * \see The USB specification for more details on the possible Endpoint attributes.
149 #define ENDPOINT_ATTR_NO_SYNC (0 << 2)
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.
154 * \see The USB specification for more details on the possible Endpoint attributes.
156 #define ENDPOINT_ATTR_ASYNC (1 << 2)
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.
161 * \see The USB specification for more details on the possible Endpoint attributes.
163 #define ENDPOINT_ATTR_ADAPTIVE (2 << 2)
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.
168 * \see The USB specification for more details on the possible Endpoint attributes.
170 #define ENDPOINT_ATTR_SYNC (3 << 2)
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.
175 * \see The USB specification for more details on the possible Endpoint usage attributes.
177 #define ENDPOINT_USAGE_DATA (0 << 4)
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.
182 * \see The USB specification for more details on the possible Endpoint usage attributes.
184 #define ENDPOINT_USAGE_FEEDBACK (1 << 4)
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.
189 * \see The USB specification for more details on the possible Endpoint usage attributes.
191 #define ENDPOINT_USAGE_IMPLICIT_FEEDBACK (2 << 4)
194 /** Enum for the possible standard descriptor types, as given in each descriptor's header. */
195 enum USB_DescriptorTypes_t
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. */
209 /** Type define for all descriptor's header, indicating the descriptor's length and type.
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.
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.
224 uint8_t bDescriptorType
;
226 } USB_Descriptor_Header_t
;
228 /** Type define for a standard device descriptor.
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.
236 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
237 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
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. */
244 uint8_t Endpoint0Size
; /**< Size of the control (address 0) endpoint's bank in bytes. */
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. */
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.
254 * \note If no string supplied, use \ref NO_DESCRIPTOR.
256 uint8_t ProductStrIndex
; /**< String index for the product name/details.
258 * \see ManufacturerStrIndex structure entry.
260 uint8_t SerialNumStrIndex
; /**< String index for the product's globally unique hexadecimal
261 * serial number, in uppercase Unicode ASCII.
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
270 * \see ManufacturerStrIndex structure entry.
273 uint8_t NumberOfConfigurations
; /**< Total number of configurations supported by
278 uint8_t bDescriptorType
;
280 uint8_t bDeviceClass
;
281 uint8_t bDeviceSubClass
;
282 uint8_t bDeviceProtocol
;
283 uint8_t bMaxPacketSize0
;
287 uint8_t iManufacturer
;
289 uint8_t iSerialNumber
;
290 uint8_t bNumConfigurations
;
292 } USB_Descriptor_Device_t
;
294 /** Type define for a standard configuration descriptor.
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.
302 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
303 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
305 uint16_t TotalConfigurationSize
; /**< Size of the configuration descriptor header,
306 * and all sub descriptors inside the configuration.
308 uint8_t TotalInterfaces
; /**< Total number of interfaces in the configuration. */
310 uint8_t ConfigurationNumber
; /**< Configuration index of the current configuration. */
311 uint8_t ConfigurationStrIndex
; /**< Index of a string descriptor describing the configuration. */
313 uint8_t ConfigAttributes
; /**< Configuration attributes, comprised of a mask of zero or
314 * more USB_CONFIG_ATTR_* masks.
317 uint8_t MaxPowerConsumption
; /**< Maximum power consumption of the device while in the
318 * current configuration, calculated by the \ref USB_CONFIG_POWER_MA()
323 uint8_t bDescriptorType
;
324 uint16_t wTotalLength
;
325 uint8_t bNumInterfaces
;
326 uint8_t bConfigurationValue
;
327 uint8_t iConfiguration
;
328 uint8_t bmAttributes
;
331 } USB_Descriptor_Configuration_Header_t
;
333 /** Type define for a standard interface descriptor.
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.
341 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
342 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
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.
350 uint8_t TotalEndpoints
; /**< Total number of endpoints in the interface. */
352 uint8_t Class
; /**< Interface class ID. */
353 uint8_t SubClass
; /**< Interface subclass ID. */
354 uint8_t Protocol
; /**< Interface protocol ID. */
356 uint8_t InterfaceStrIndex
; /**< Index of the string descriptor describing the
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
;
370 } USB_Descriptor_Interface_t
;
372 /** Type define for a standard Interface Association descriptor.
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.
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.
386 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
387 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
389 uint8_t FirstInterfaceIndex
; /**< Index of the first associated interface. */
390 uint8_t TotalInterfaces
; /** Total number of associated interfaces. */
392 uint8_t Class
; /**< Interface class ID. */
393 uint8_t SubClass
; /**< Interface subclass ID. */
394 uint8_t Protocol
; /**< Interface protocol ID. */
396 uint8_t IADStrIndex
; /**< Index of the string descriptor describing the
397 * interface association.
401 uint8_t bDescriptorType
;
402 uint8_t bFirstInterface
;
403 uint8_t bInterfaceCount
;
404 uint8_t bFunctionClass
;
405 uint8_t bFunctionSubClass
;
406 uint8_t bFunctionProtocol
;
409 } USB_Descriptor_Interface_Association_t
;
411 /** Type define for a standard endpoint descriptor.
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.
419 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
420 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
422 uint8_t EndpointAddress
; /**< Logical address of the endpoint within the device
423 * for the current configuration, including direction
426 uint8_t Attributes
; /**< Endpoint attributes, comprised of a mask of the
427 * endpoint type (EP_TYPE_*) and attributes (ENDPOINT_ATTR_*)
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.
434 uint8_t PollingIntervalMS
; /**< Polling interval in milliseconds for the endpoint
435 * if it is an INTERRUPT or ISOCHRONOUS type.
439 uint8_t bDescriptorType
;
440 uint8_t bEndpointAddress
;
441 uint8_t bmAttributes
;
442 uint16_t wMaxPacketSize
;
445 } USB_Descriptor_Endpoint_t
;
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.
451 * This structure should also be used for string index 0, which contains the supported language IDs for
452 * the device as an array.
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.
460 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
461 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
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
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.
476 uint8_t bDescriptorType
;
479 } USB_Descriptor_String_t
;
481 /* Private Interface - For use in library only: */
482 #if !defined(__DOXYGEN__)
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)))
490 /* Disable C linkage for C++ Compilers: */
491 #if defined(__cplusplus)