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 /** Macro to calculate the power value for the device descriptor, from a given number of milliamps. */
79 #define USB_CONFIG_POWER_MA(x) (x >> 1)
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.
84 #define USB_STRING_LEN(x) (sizeof(USB_Descriptor_Header_t) + (x << 1))
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.
90 #define VERSION_BCD(x) ((((VERSION_TENS(x) << 4) | VERSION_ONES(x)) << 8) | \
91 ((VERSION_TENTHS(x) << 4) | VERSION_HUNDREDTHS(x)))
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.
96 #define LANGUAGE_ID_ENG 0x0409
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
102 #define ENDPOINT_DESCRIPTOR_DIR_IN 0x80
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
108 #define ENDPOINT_DESCRIPTOR_DIR_OUT 0x00
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.
114 #define USB_CONFIG_ATTR_BUSPOWERED 0x80
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.
120 #define USB_CONFIG_ATTR_SELFPOWERED 0xC0
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
127 #define USB_CONFIG_ATTR_REMOTEWAKEUP 0xA0
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.
132 * \see The USB specification for more details on the possible Endpoint attributes.
134 #define ENDPOINT_ATTR_NO_SYNC (0 << 2)
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.
139 * \see The USB specification for more details on the possible Endpoint attributes.
141 #define ENDPOINT_ATTR_ASYNC (1 << 2)
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.
146 * \see The USB specification for more details on the possible Endpoint attributes.
148 #define ENDPOINT_ATTR_ADAPTIVE (2 << 2)
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.
153 * \see The USB specification for more details on the possible Endpoint attributes.
155 #define ENDPOINT_ATTR_SYNC (3 << 2)
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.
160 * \see The USB specification for more details on the possible Endpoint usage attributes.
162 #define ENDPOINT_USAGE_DATA (0 << 4)
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.
167 * \see The USB specification for more details on the possible Endpoint usage attributes.
169 #define ENDPOINT_USAGE_FEEDBACK (1 << 4)
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.
174 * \see The USB specification for more details on the possible Endpoint usage attributes.
176 #define ENDPOINT_USAGE_IMPLICIT_FEEDBACK (2 << 4)
178 /** Gives a void pointer to the specified descriptor (of any type). */
179 #define DESCRIPTOR_ADDRESS(Descriptor) ((void*)&Descriptor)
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.
186 * \see Events.h for more information on this event.
188 RAISES_EVENT(USB_DeviceError
);
192 /** Enum for the possible standard descriptor types, as given in each descriptor's header. */
193 enum USB_DescriptorTypes_t
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. */
207 /** Type define for all descriptor's header, indicating the descriptor's length and type.
209 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
210 * documentation for more information on the two descriptor naming schemes. If the
211 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
212 * identical to those listed in the USB standard.
216 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
217 uint8_t Size
; /**< Size of the descriptor, in bytes. */
218 uint8_t Type
; /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a value
219 * given by the specific class.
223 uint8_t bDescriptorType
;
225 } USB_Descriptor_Header_t
;
227 /** Type define for a standard device descriptor.
229 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
230 * documentation for more information on the two descriptor naming schemes. If the
231 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
232 * 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 * \see ManufacturerStrIndex structure entry.
266 uint8_t NumberOfConfigurations
; /**< Total number of configurations supported by
271 uint8_t bDescriptorType
;
273 uint8_t bDeviceClass
;
274 uint8_t bDeviceSubClass
;
275 uint8_t bDeviceProtocol
;
276 uint8_t bMaxPacketSize0
;
280 uint8_t iManufacturer
;
282 uint8_t iSerialNumber
;
283 uint8_t bNumConfigurations
;
285 } USB_Descriptor_Device_t
;
287 /** Type define for a standard configuration descriptor.
289 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
290 * documentation for more information on the two descriptor naming schemes. If the
291 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
292 * identical to those listed in the USB standard.
296 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
297 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
299 uint16_t TotalConfigurationSize
; /**< Size of the configuration descriptor header,
300 * and all sub descriptors inside the configuration.
302 uint8_t TotalInterfaces
; /**< Total number of interfaces in the configuration. */
304 uint8_t ConfigurationNumber
; /**< Configuration index of the current configuration. */
305 uint8_t ConfigurationStrIndex
; /**< Index of a string descriptor describing the configuration. */
307 uint8_t ConfigAttributes
; /**< Configuration attributes, comprised of a mask of zero or
308 * more USB_CONFIG_ATTR_* masks.
311 uint8_t MaxPowerConsumption
; /**< Maximum power consumption of the device while in the
312 * current configuration, calculated by the \ref USB_CONFIG_POWER_MA()
317 uint8_t bDescriptorType
;
318 uint16_t wTotalLength
;
319 uint8_t bNumInterfaces
;
320 uint8_t bConfigurationValue
;
321 uint8_t iConfiguration
;
322 uint8_t bmAttributes
;
325 } USB_Descriptor_Configuration_Header_t
;
327 /** Type define for a standard interface descriptor.
329 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
330 * documentation for more information on the two descriptor naming schemes. If the
331 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
332 * identical to those listed in the USB standard.
336 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
337 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
339 uint8_t InterfaceNumber
; /**< Index of the interface in the current configuration. */
340 uint8_t AlternateSetting
; /**< Alternate setting for the interface number. The same
341 * interface number can have multiple alternate settings
342 * with different endpoint configurations, which can be
343 * selected by the host.
345 uint8_t TotalEndpoints
; /**< Total number of endpoints in the interface. */
347 uint8_t Class
; /**< Interface class ID. */
348 uint8_t SubClass
; /**< Interface subclass ID. */
349 uint8_t Protocol
; /**< Interface protocol ID. */
351 uint8_t InterfaceStrIndex
; /**< Index of the string descriptor describing the
356 uint8_t bDescriptorType
;
357 uint8_t bInterfaceNumber
;
358 uint8_t bAlternateSetting
;
359 uint8_t bNumEndpoints
;
360 uint8_t bInterfaceClass
;
361 uint8_t bInterfaceSubClass
;
362 uint8_t bInterfaceProtocol
;
365 } USB_Descriptor_Interface_t
;
367 /** Type define for a standard Interface Association descriptor.
369 * This descriptor has been added as a supplement to the USB2.0 standard, in the ECN located at
370 * <a>http://www.usb.org/developers/docs/InterfaceAssociationDescriptor_ecn.pdf</a>. It allows compound
371 * devices with multiple interfaces related to the same function to have the multiple interfaces bound
372 * together at the point of enumeration, loading one generic driver for all the interfaces in the single
373 * function. Read the ECN for more information.
375 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
376 * documentation for more information on the two descriptor naming schemes. If the
377 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
378 * identical to those listed in the USB standard.
382 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
383 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
385 uint8_t FirstInterfaceIndex
; /**< Index of the first associated interface. */
386 uint8_t TotalInterfaces
; /** Total number of associated interfaces. */
388 uint8_t Class
; /**< Interface class ID. */
389 uint8_t SubClass
; /**< Interface subclass ID. */
390 uint8_t Protocol
; /**< Interface protocol ID. */
392 uint8_t IADStrIndex
; /**< Index of the string descriptor describing the
393 * interface association.
397 uint8_t bDescriptorType
;
398 uint8_t bFirstInterface
;
399 uint8_t bInterfaceCount
;
400 uint8_t bFunctionClass
;
401 uint8_t bFunctionSubClass
;
402 uint8_t bFunctionProtocol
;
405 } USB_Descriptor_Interface_Association_t
;
407 /** Type define for a standard endpoint descriptor.
409 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
410 * documentation for more information on the two descriptor naming schemes. If the
411 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
412 * identical to those listed in the USB standard.
416 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
417 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
419 uint8_t EndpointAddress
; /**< Logical address of the endpoint within the device
420 * for the current configuration, including direction
423 uint8_t Attributes
; /**< Endpoint attributes, comprised of a mask of the
424 * endpoint type (EP_TYPE_*) and attributes (ENDPOINT_ATTR_*)
427 uint16_t EndpointSize
; /**< Size of the endpoint bank, in bytes. This indicates the
428 * maximum packet size that the endpoint can receive at a time.
431 uint8_t PollingIntervalMS
; /**< Polling interval in milliseconds for the endpoint
432 * if it is an INTERRUPT or ISOCHRONOUS type.
436 uint8_t bDescriptorType
;
437 uint8_t bEndpointAddress
;
438 uint8_t bmAttributes
;
439 uint16_t wMaxPacketSize
;
442 } USB_Descriptor_Endpoint_t
;
444 /** Type define for a standard string descriptor. Unlike other standard descriptors, the length
445 * of the descriptor for placement in the descriptor header must be determined by the \ref USB_STRING_LEN()
446 * macro rather than by the size of the descriptor structure, as the length is not fixed.
448 * This structure should also be used for string index 0, which contains the supported language IDs for
449 * the device as an array.
451 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
452 * documentation for more information on the two descriptor naming schemes. If the
453 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
454 * identical to those listed in the USB standard.
458 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
459 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
461 int16_t UnicodeString
[]; /**< String data, as unicode characters (alternatively,
462 * string language IDs). If normal ASCII characters are
463 * to be used, they must be added as an array of characters
464 * rather than a normal C string so that they are widened to
467 * Under GCC, strings prefixed with the "L" character (before
468 * the opening string quotation mark) are considered to be
469 * Unicode strings, and may be used instead of an explicit
470 * array of ASCII characters.
474 uint8_t bDescriptorType
;
477 } USB_Descriptor_String_t
;
479 /* Function Prototypes: */
480 /** Function to retrieve a given descriptor's size and memory location from the given descriptor type value,
481 * index and language ID. This function MUST be overridden in the user application (added with full, identical
482 * prototype and name except for the \ref ATTR_WEAK attribute) so that the library can call it to retrieve descriptor
485 * \param wValue The type of the descriptor to retrieve in the upper byte, and the index in the
486 * lower byte (when more than one descriptor of the given type exists, such as the
487 * case of string descriptors). The type may be one of the standard types defined
488 * in the DescriptorTypes_t enum, or may be a class-specific descriptor type value.
489 * \param wIndex The language ID of the string to return if the wValue type indicates DTYPE_String,
490 * otherwise zero for standard descriptors, or as defined in a class-specific
492 * \param DescriptorAddress Pointer to the descriptor in memory. This should be set by the routine to
493 * the location of the descriptor, found by the \ref DESCRIPTOR_ADDRESS() macro.
495 * \note By default, the library expects all descriptors to be located in flash memory via the PROGMEM attribute.
496 * If descriptors should be located in RAM or EEPROM instead (to speed up access in the case of RAM, or to
497 * allow the descriptors to be changed dynamically at runtime) either the USE_RAM_DESCRIPTORS or the
498 * USE_EEPROM_DESCRIPTORS tokens may be defined in the project makefile and passed to the compiler by the -D
501 * \return Size in bytes of the descriptor if it exists, zero or \ref NO_DESCRIPTOR otherwise
503 uint16_t USB_GetDescriptor(const uint16_t wValue
, const uint8_t wIndex
, void** const DescriptorAddress
)
504 ATTR_WARN_UNUSED_RESULT ATTR_WEAK
ATTR_NON_NULL_PTR_ARG(3);
506 /* Private Interface - For use in library only: */
507 #if !defined(__DOXYGEN__)
509 #define VERSION_TENS(x) (int)(x / 10)
510 #define VERSION_ONES(x) (int)(x - (10 * VERSION_TENS(x)))
511 #define VERSION_TENTHS(x) (int)((x - (int)x) * 10)
512 #define VERSION_HUNDREDTHS(x) (int)(((x - (int)x) * 100) - (10 * VERSION_TENTHS(x)))
515 /* Disable C linkage for C++ Compilers: */
516 #if defined(__cplusplus)