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
33 * Standard USB device descriptor defines and retrieval routines, for USB devices. This module contains
34 * structures and macros for the easy creation of standard USB descriptors in USB device projects.
36 * All standard descriptors have their elements named in an identical manner to the official USB specification,
37 * however slightly more verbose alternate (non-standard) names are also supplied if the macro
38 * USE_NONSTANDARD_DESCRIPTOR_NAMES is defined in the user project makefile and passed to the compiler at
39 * compilation time using the -D option.
41 * The non-standard names are documented here - if USE_NONSTANDARD_DESCRIPTOR_NAMES is not defined, then all
42 * descriptors will contain elements named identically to the official USB specification. The alternately
43 * named descriptor elements are placed in the same order inside the descriptor structures as their officially
44 * named counterparts, thus they can be correlated easily with the official USB specification.
47 #ifndef __USBDESCRIPTORS_H__
48 #define __USBDESCRIPTORS_H__
51 #include <avr/pgmspace.h>
54 #include "../../../Common/Common.h"
58 #if defined(USB_CAN_BE_DEVICE)
59 #include "../LowLevel/Device.h"
62 /* Enable C linkage for C++ Compilers: */
63 #if defined(__cplusplus)
67 /* Public Interface - May be used in end-application: */
69 /** Indicates that a given descriptor does not exist in the device. This can be used inside descriptors
70 * for string descriptor indexes, or may be use as a return value for GetDescriptor when the specified
71 * descriptor does not exist.
73 #define NO_DESCRIPTOR 0
75 /** Macro to calculate the power value for the device descriptor, from a given number of milliamps. */
76 #define USB_CONFIG_POWER_MA(x) (x >> 1)
78 /** Macro to calculate the Unicode length of a string with a given number of Unicode characters.
79 * Should be used in string descriptor's headers for giving the string descriptor's byte length.
81 #define USB_STRING_LEN(x) (sizeof(USB_Descriptor_Header_t) + (x << 1))
83 /** Macro to encode a given four digit floating point version number (e.g. 01.23) into Binary Coded
84 * Decimal format for descriptor fields requiring BCD encoding, such as the USB version number in the
85 * standard device descriptor.
87 #define VERSION_BCD(x) ((((VERSION_TENS(x) << 4) | VERSION_ONES(x)) << 8) | \
88 ((VERSION_TENTHS(x) << 4) | VERSION_HUNDREDTHS(x)))
90 /** String language ID for the English language. Should be used in USB_Descriptor_Language_t descriptors
91 * to indicate that the English language is supported by the device in its string descriptors.
93 #define LANGUAGE_ID_ENG 0x0409
95 /** Can be masked with an endpoint address for a USB_Descriptor_Endpoint_t endpoint descriptor's
96 * EndpointAddress value to indicate to the host that the endpoint is of the IN direction (i.e, from
99 #define ENDPOINT_DESCRIPTOR_DIR_IN 0x80
101 /** Can be masked with an endpoint address for a USB_Descriptor_Endpoint_t endpoint descriptor's
102 * EndpointAddress value to indicate to the host that the endpoint is of the OUT direction (i.e, from
105 #define ENDPOINT_DESCRIPTOR_DIR_OUT 0x00
107 /** Can be masked with other configuration descriptor attributes for a USB_Descriptor_Configuration_Header_t
108 * descriptor's ConfigAttributes value to indicate that the specified configuration can draw its power
109 * from the host's VBUS line.
111 #define USB_CONFIG_ATTR_BUSPOWERED 0b10000000
113 /** Can be masked with other configuration descriptor attributes for a USB_Descriptor_Configuration_Header_t
114 * descriptor's ConfigAttributes value to indicate that the specified configuration can draw its power
115 * from the device's own power source.
117 #define USB_CONFIG_ATTR_SELFPOWERED 0b11000000
119 /** Can be masked with other configuration descriptor attributes for a USB_Descriptor_Configuration_Header_t
120 * descriptor's ConfigAttributes value to indicate that the specified configuration supports the
121 * remote wakeup feature of the USB standard, allowing a suspended USB device to wake up the host upon
124 #define USB_CONFIG_ATTR_REMOTEWAKEUP 0b10100000
126 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
127 * Attributes value to indicate that the specified endpoint is not synchronized.
129 * \see The USB specification for more details on the possible Endpoint attributes.
131 #define ENDPOINT_ATTR_NO_SYNC (0b00 << 2)
133 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
134 * Attributes value to indicate that the specified endpoint is asynchronous.
136 * \see The USB specification for more details on the possible Endpoint attributes.
138 #define ENDPOINT_ATTR_ASYNC (0b01 << 2)
140 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
141 * Attributes value to indicate that the specified endpoint is adaptive.
143 * \see The USB specification for more details on the possible Endpoint attributes.
145 #define ENDPOINT_ATTR_ADAPTIVE (0b10 << 2)
147 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
148 * Attributes value to indicate that the specified endpoint is synchronized.
150 * \see The USB specification for more details on the possible Endpoint attributes.
152 #define ENDPOINT_ATTR_SYNC (0b11 << 2)
154 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
155 * Attributes value to indicate that the specified endpoint is used for data transfers.
157 * \see The USB specification for more details on the possible Endpoint usage attributes.
159 #define ENDPOINT_USAGE_DATA (0b00 << 4)
161 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
162 * Attributes value to indicate that the specified endpoint is used for feedback.
164 * \see The USB specification for more details on the possible Endpoint usage attributes.
166 #define ENDPOINT_USAGE_FEEDBACK (0b01 << 4)
168 /** Can be masked with other endpoint descriptor attributes for a USB_Descriptor_Endpoint_t descriptor's
169 * Attributes value to indicate that the specified endpoint is used for implicit feedback.
171 * \see The USB specification for more details on the possible Endpoint usage attributes.
173 #define ENDPOINT_USAGE_IMPLICIT_FEEDBACK (0b10 << 4)
175 /** Gives a void pointer to the specified descriptor (of any type). */
176 #define DESCRIPTOR_ADDRESS(Descriptor) ((void*)&Descriptor)
179 #if defined(USB_CAN_BE_DEVICE) || defined(__DOXYGEN__)
180 /** This module raises the Device Error event while in device mode, if the USB_GetDescriptor()
181 * routine is not hooked in the user application to properly return descriptors to the library.
183 * \see Events.h for more information on this event.
185 RAISES_EVENT(USB_DeviceError
);
189 /** Enum for the possible standard descriptor types, as given in each descriptor's header. */
190 enum USB_DescriptorTypes_t
192 DTYPE_Device
= 0x01, /**< Indicates that the descriptor is a device descriptor. */
193 DTYPE_Configuration
= 0x02, /**< Indicates that the descriptor is a configuration descriptor. */
194 DTYPE_String
= 0x03, /**< Indicates that the descriptor is a string descriptor. */
195 DTYPE_Interface
= 0x04, /**< Indicates that the descriptor is an interface descriptor. */
196 DTYPE_Endpoint
= 0x05, /**< Indicates that the descriptor is an endpoint descriptor. */
197 DTYPE_DeviceQualifier
= 0x06, /**< Indicates that the descriptor is a device qualifier descriptor. */
198 DTYPE_Other
= 0x07, /**< Indicates that the descriptor is of other type. */
199 DTYPE_InterfacePower
= 0x08, /**< Indicates that the descriptor is an interface power descriptor. */
200 DTYPE_InterfaceAssociation
= 0x0B, /**< Indicates that the descriptor is an interface association descriptor. */
204 /** Type define for all descriptor's header, indicating the descriptor's length and type.
206 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
207 * documentation for more information on the two descriptor naming schemes. If the
208 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
209 * identical to those listed in the USB standard.
213 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
214 uint8_t Size
; /**< Size of the descriptor, in bytes. */
215 uint8_t Type
; /**< Type of the descriptor, either a value in DescriptorTypes_t or a value
216 * given by the specific class.
220 uint8_t bDescriptorType
;
222 } USB_Descriptor_Header_t
;
224 /** Type define for a standard device descriptor.
226 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
227 * documentation for more information on the two descriptor naming schemes. If the
228 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
229 * identical to those listed in the USB standard.
233 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
234 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
236 uint16_t USBSpecification
; /**< BCD of the supported USB specification. */
237 uint8_t Class
; /**< USB device class. */
238 uint8_t SubClass
; /**< USB device subclass. */
239 uint8_t Protocol
; /**< USB device protocol. */
241 uint8_t Endpoint0Size
; /**< Size of the control (address 0) endpoint's bank in bytes. */
243 uint16_t VendorID
; /**< Vendor ID for the USB product. */
244 uint16_t ProductID
; /**< Unique product ID for the USB product. */
245 uint16_t ReleaseNumber
; /**< Product release (version) number. */
247 uint8_t ManufacturerStrIndex
; /**< String index for the manufacturer's name. The
248 * host will request this string via a separate
249 * control request for the string descriptor.
251 * \note If no string supplied, use NO_DESCRIPTOR.
253 uint8_t ProductStrIndex
; /**< String index for the product name/details.
255 * \see ManufacturerStrIndex structure entry.
257 uint8_t SerialNumStrIndex
; /**< String index for the product's globally unique hexadecimal
258 * serial number, in uppercase Unicode ASCII.
260 * \see ManufacturerStrIndex structure entry.
263 uint8_t NumberOfConfigurations
; /**< Total number of configurations supported by
268 uint8_t bDescriptorType
;
270 uint8_t bDeviceClass
;
271 uint8_t bDeviceSubClass
;
272 uint8_t bDeviceProtocol
;
273 uint8_t bMaxPacketSize0
;
277 uint8_t iManufacturer
;
279 uint8_t iSerialNumber
;
280 uint8_t bNumConfigurations
;
282 } USB_Descriptor_Device_t
;
284 /** Type define for a standard configuration descriptor.
286 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
287 * documentation for more information on the two descriptor naming schemes. If the
288 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
289 * identical to those listed in the USB standard.
293 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
294 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
296 uint16_t TotalConfigurationSize
; /**< Size of the configuration descriptor header,
297 * and all sub descriptors inside the configuration.
299 uint8_t TotalInterfaces
; /**< Total number of interfaces in the configuration. */
301 uint8_t ConfigurationNumber
; /**< Configuration index of the current configuration. */
302 uint8_t ConfigurationStrIndex
; /**< Index of a string descriptor describing the configuration. */
304 uint8_t ConfigAttributes
; /**< Configuration attributes, comprised of a mask of zero or
305 * more USB_CONFIG_ATTR_* masks.
308 uint8_t MaxPowerConsumption
; /**< Maximum power consumption of the device while in the
309 * current configuration, calculated by the USB_CONFIG_POWER_MA()
314 uint8_t bDescriptorType
;
315 uint16_t wTotalLength
;
316 uint8_t bNumInterfaces
;
317 uint8_t bConfigurationValue
;
318 uint8_t iConfiguration
;
319 uint8_t bmAttributes
;
322 } USB_Descriptor_Configuration_Header_t
;
324 /** Type define for a standard interface descriptor.
326 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
327 * documentation for more information on the two descriptor naming schemes. If the
328 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
329 * identical to those listed in the USB standard.
333 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
334 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
336 uint8_t InterfaceNumber
; /**< Index of the interface in the current configuration. */
337 uint8_t AlternateSetting
; /**< Alternate setting for the interface number. The same
338 * interface number can have multiple alternate settings
339 * with different endpoint configurations, which can be
340 * selected by the host.
342 uint8_t TotalEndpoints
; /**< Total number of endpoints in the interface. */
344 uint8_t Class
; /**< Interface class ID. */
345 uint8_t SubClass
; /**< Interface subclass ID. */
346 uint8_t Protocol
; /**< Interface protocol ID. */
348 uint8_t InterfaceStrIndex
; /**< Index of the string descriptor describing the
353 uint8_t bDescriptorType
;
354 uint8_t bInterfaceNumber
;
355 uint8_t bAlternateSetting
;
356 uint8_t bNumEndpoints
;
357 uint8_t bInterfaceClass
;
358 uint8_t bInterfaceSubClass
;
359 uint8_t bInterfaceProtocol
;
362 } USB_Descriptor_Interface_t
;
364 /** Type define for a standard interface association descriptor.
366 * This descriptor has been added as a supplement to the USB2.0 standard, in the ECN located at
367 * <a>http://www.usb.org/developers/docs/InterfaceAssociationDescriptor_ecn.pdf</a>. It allows compound
368 * devices with multiple interfaces related to the same function to have the multiple interfaces bound
369 * together at the point of enumeration, loading one generic driver for all the interfaces in the single
370 * function. Read the ECN for more information.
372 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
373 * documentation for more information on the two descriptor naming schemes. If the
374 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
375 * identical to those listed in the USB standard.
379 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
380 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
382 uint8_t FirstInterfaceIndex
; /**< Index of the first associated interface. */
383 uint8_t TotalInterfaces
; /** Total number of associated interfaces. */
385 uint8_t Class
; /**< Interface class ID. */
386 uint8_t SubClass
; /**< Interface subclass ID. */
387 uint8_t Protocol
; /**< Interface protocol ID. */
389 uint8_t IADStrIndex
; /**< Index of the string descriptor describing the
390 * interface association.
394 uint8_t bDescriptorType
;
395 uint8_t bFirstInterface
;
396 uint8_t bInterfaceCount
;
397 uint8_t bFunctionClass
;
398 uint8_t bFunctionSubClass
;
399 uint8_t bFunctionProtocol
;
402 } USB_Descriptor_Interface_Association_t
;
404 /** Type define for a standard endpoint descriptor.
406 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
407 * documentation for more information on the two descriptor naming schemes. If the
408 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
409 * identical to those listed in the USB standard.
413 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
414 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
416 uint8_t EndpointAddress
; /**< Logical address of the endpoint within the device
417 * for the current configuration, including direction
420 uint8_t Attributes
; /**< Endpoint attributes, comprised of a mask of the
421 * endpoint type (EP_TYPE_*) and attributes (ENDPOINT_ATTR_*)
424 uint16_t EndpointSize
; /**< Size of the endpoint bank, in bytes. This indicates the
425 * maximum packet size that the endpoint can receive at a time.
428 uint8_t PollingIntervalMS
; /**< Polling interval in milliseconds for the endpoint
429 * if it is an INTERRUPT or ISOCHRONOUS type.
433 uint8_t bDescriptorType
;
434 uint8_t bEndpointAddress
;
435 uint8_t bmAttributes
;
436 uint16_t wMaxPacketSize
;
439 } USB_Descriptor_Endpoint_t
;
441 /** Type define for a standard string descriptor. Unlike other standard descriptors, the length
442 * of the descriptor for placement in the descriptor header must be determined by the USB_STRING_LEN()
443 * macro rather than by the size of the descriptor structure, as the length is not fixed.
445 * This structure should also be used for string index 0, which contains the supported language IDs for
446 * the device as an array.
448 * \note The non-standard structure element names are documented here - see the StdDescriptors.h file
449 * documentation for more information on the two descriptor naming schemes. If the
450 * USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements with names
451 * identical to those listed in the USB standard.
455 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__)
456 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */
458 int16_t UnicodeString
[]; /**< String data, as unicode characters (alternatively,
459 * string language IDs). If normal ASCII characters are
460 * to be used, they must be added as an array of characters
461 * rather than a normal C string so that they are widened to
464 * Under GCC, strings prefixed with the "L" character (before
465 * the opening string quotation mark) are considered to be
466 * Unicode strings, and may be used instead of an explicit
467 * array of ASCII characters.
471 uint8_t bDescriptorType
;
474 } USB_Descriptor_String_t
;
480 } USB_Descriptor_Details_t
;
482 /* Function Prototypes: */
483 /** Function to retrieve a given descriptor's size and memory location from the given descriptor type value,
484 * index and language ID. This function MUST be overridden in the user application (added with full, identical
485 * prototype and name except for the ATTR_WEAK attribute) so that the library can call it to retrieve descriptor
488 * \param wValue The type of the descriptor to retrieve in the upper byte, and the index in the
489 * lower byte (when more than one descriptor of the given type exists, such as the
490 * case of string descriptors). The type may be one of the standard types defined
491 * in the DescriptorTypes_t enum, or may be a class-specific descriptor type value.
492 * \param wIndex The language ID of the string to return if the wValue type indicates DTYPE_String,
493 * otherwise zero for standard descriptors, or as defined in a class-specific
495 * \param DescriptorAddress Pointer to the descriptor in memory. This should be set by the routine to
496 * the location of the descriptor, found by the DESCRIPTOR_ADDRESS macro.
498 * \note By default, the library expects all descriptors to be located in flash memory via the PROGMEM attribute.
499 * If descriptors should be located in RAM or EEPROM instead (to speed up access in the case of RAM, or to
500 * allow the descriptors to be changed dynamically at runtime) either the USE_RAM_DESCRIPTORS or the
501 * USE_EEPROM_DESCRIPTORS tokens may be defined in the project makefile and passed to the compiler by the -D
504 * \return Size in bytes of the descriptor if it exists, zero or NO_DESCRIPTOR otherwise
506 uint16_t USB_GetDescriptor(const uint16_t wValue
, const uint8_t wIndex
, void** const DescriptorAddress
)
507 ATTR_WARN_UNUSED_RESULT ATTR_WEAK
ATTR_NON_NULL_PTR_ARG(3);
509 /* Private Interface - For use in library only: */
510 #if !defined(__DOXYGEN__)
512 #define VERSION_TENS(x) (int)(x / 10)
513 #define VERSION_ONES(x) (int)(x - (10 * VERSION_TENS(x)))
514 #define VERSION_TENTHS(x) (int)((x - (int)x) * 10)
515 #define VERSION_HUNDREDTHS(x) (int)(((x - (int)x) * 100) - (10 * VERSION_TENTHS(x)))
518 /* Disable C linkage for C++ Compilers: */
519 #if defined(__cplusplus)