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(mA)            (mA >> 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(str)               (sizeof(USB_Descriptor_Header_t) + (str << 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) 
 179                         /** Enum for the possible standard descriptor types, as given in each descriptor's header. */ 
 180                         enum USB_DescriptorTypes_t
 
 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. */ 
 194                         /** Type define for all descriptor's header, indicating the descriptor's length and type. 
 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. 
 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. 
 209                                 uint8_t bDescriptorType
; 
 211                         } USB_Descriptor_Header_t
; 
 213                         /** Type define for a standard device descriptor. 
 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. 
 221                                 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__) 
 222                                 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */ 
 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. */ 
 229                                 uint8_t                 Endpoint0Size
; /**< Size of the control (address 0) endpoint's bank in bytes. */ 
 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. */ 
 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. 
 239                                                                                *   \note If no string supplied, use \ref NO_DESCRIPTOR. 
 241                                 uint8_t                 ProductStrIndex
; /**< String index for the product name/details. 
 243                                                                           *  \see ManufacturerStrIndex structure entry. 
 245                                 uint8_t                 SerialNumStrIndex
; /**< String index for the product's globally unique hexadecimal 
 246                                                                             *   serial number, in uppercase Unicode ASCII. 
 248                                                                             *  \see ManufacturerStrIndex structure entry. 
 251                                 uint8_t                 NumberOfConfigurations
; /**< Total number of configurations supported by 
 256                                 uint8_t                 bDescriptorType
; 
 258                                 uint8_t                 bDeviceClass
; 
 259                                 uint8_t                 bDeviceSubClass
; 
 260                                 uint8_t                 bDeviceProtocol
; 
 261                                 uint8_t                 bMaxPacketSize0
; 
 265                                 uint8_t                 iManufacturer
; 
 267                                 uint8_t                 iSerialNumber
; 
 268                                 uint8_t                 bNumConfigurations
; 
 270                         } USB_Descriptor_Device_t
; 
 272                         /** Type define for a standard configuration descriptor. 
 274                          *  \note The non-standard structure element names are documented here. If the 
 275                          *        USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements  
 276                          *        with names identical to those listed in the USB standard. 
 280                                 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__) 
 281                                 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */ 
 283                                 uint16_t                TotalConfigurationSize
; /**< Size of the configuration descriptor header, 
 284                                                                                  *   and all sub descriptors inside the configuration. 
 286                                 uint8_t                 TotalInterfaces
; /**< Total number of interfaces in the configuration. */ 
 288                                 uint8_t                 ConfigurationNumber
; /**< Configuration index of the current configuration. */ 
 289                                 uint8_t                 ConfigurationStrIndex
; /**< Index of a string descriptor describing the configuration. */ 
 291                                 uint8_t                 ConfigAttributes
; /**< Configuration attributes, comprised of a mask of zero or 
 292                                                                            *   more USB_CONFIG_ATTR_* masks. 
 295                                 uint8_t                 MaxPowerConsumption
; /**< Maximum power consumption of the device while in the 
 296                                                                               *   current configuration, calculated by the \ref USB_CONFIG_POWER_MA() 
 301                                 uint8_t                 bDescriptorType
; 
 302                                 uint16_t                wTotalLength
; 
 303                                 uint8_t                 bNumInterfaces
; 
 304                                 uint8_t                 bConfigurationValue
; 
 305                                 uint8_t                 iConfiguration
; 
 306                                 uint8_t                 bmAttributes
; 
 309                         } USB_Descriptor_Configuration_Header_t
; 
 311                         /** Type define for a standard interface descriptor. 
 313                          *  \note The non-standard structure element names are documented here. If the 
 314                          *        USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements  
 315                          *        with names identical to those listed in the USB standard. 
 319                                 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__) 
 320                                 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */ 
 322                                 uint8_t                 InterfaceNumber
; /**< Index of the interface in the current configuration. */ 
 323                                 uint8_t                 AlternateSetting
; /**< Alternate setting for the interface number. The same 
 324                                                                            *   interface number can have multiple alternate settings 
 325                                                                            *   with different endpoint configurations, which can be 
 326                                                                            *   selected by the host. 
 328                                 uint8_t                 TotalEndpoints
; /**< Total number of endpoints in the interface. */ 
 330                                 uint8_t                 Class
; /**< Interface class ID. */ 
 331                                 uint8_t                 SubClass
; /**< Interface subclass ID. */ 
 332                                 uint8_t                 Protocol
; /**< Interface protocol ID. */ 
 334                                 uint8_t                 InterfaceStrIndex
; /**< Index of the string descriptor describing the 
 339                                 uint8_t                 bDescriptorType
; 
 340                                 uint8_t                 bInterfaceNumber
; 
 341                                 uint8_t                 bAlternateSetting
; 
 342                                 uint8_t                 bNumEndpoints
; 
 343                                 uint8_t                 bInterfaceClass
; 
 344                                 uint8_t                 bInterfaceSubClass
; 
 345                                 uint8_t                 bInterfaceProtocol
; 
 348                         } USB_Descriptor_Interface_t
; 
 350                         /** Type define for a standard Interface Association descriptor. 
 352                          *  This descriptor has been added as a supplement to the USB2.0 standard, in the ECN located at 
 353                          *  <a>http://www.usb.org/developers/docs/InterfaceAssociationDescriptor_ecn.pdf</a>. It allows compound 
 354                          *  devices with multiple interfaces related to the same function to have the multiple interfaces bound 
 355                          *  together at the point of enumeration, loading one generic driver for all the interfaces in the single 
 356                          *  function. Read the ECN for more information. 
 358                          *  \note The non-standard structure element names are documented here. If the 
 359                          *        USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements  
 360                          *        with names identical to those listed in the USB standard. 
 364                                 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__) 
 365                                 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */ 
 367                                 uint8_t                 FirstInterfaceIndex
; /**< Index of the first associated interface. */ 
 368                                 uint8_t                 TotalInterfaces
; /** Total number of associated interfaces. */ 
 370                                 uint8_t                 Class
; /**< Interface class ID. */ 
 371                                 uint8_t                 SubClass
; /**< Interface subclass ID. */ 
 372                                 uint8_t                 Protocol
; /**< Interface protocol ID. */ 
 374                                 uint8_t                 IADStrIndex
; /**< Index of the string descriptor describing the 
 375                                                                       *   interface association. 
 379                                 uint8_t                 bDescriptorType
; 
 380                                 uint8_t                 bFirstInterface
; 
 381                                 uint8_t                 bInterfaceCount
; 
 382                                 uint8_t                 bFunctionClass
; 
 383                                 uint8_t                 bFunctionSubClass
; 
 384                                 uint8_t                 bFunctionProtocol
; 
 387                         } USB_Descriptor_Interface_Association_t
; 
 389                         /** Type define for a standard endpoint descriptor. 
 391                          *  \note The non-standard structure element names are documented here. If the 
 392                          *        USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements  
 393                          *        with names identical to those listed in the USB standard. 
 397                                 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__) 
 398                                 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */ 
 400                                 uint8_t                 EndpointAddress
; /**< Logical address of the endpoint within the device 
 401                                                                           *   for the current configuration, including direction 
 404                                 uint8_t                 Attributes
; /**< Endpoint attributes, comprised of a mask of the 
 405                                                                      *   endpoint type (EP_TYPE_*) and attributes (ENDPOINT_ATTR_*) 
 408                                 uint16_t                EndpointSize
; /**< Size of the endpoint bank, in bytes. This indicates the 
 409                                                                        *   maximum packet size that the endpoint can receive at a time. 
 412                                 uint8_t                 PollingIntervalMS
; /**< Polling interval in milliseconds for the endpoint 
 413                                                                             *   if it is an INTERRUPT or ISOCHRONOUS type. 
 417                                 uint8_t                 bDescriptorType
; 
 418                                 uint8_t                 bEndpointAddress
; 
 419                                 uint8_t                 bmAttributes
; 
 420                                 uint16_t                wMaxPacketSize
; 
 423                         } USB_Descriptor_Endpoint_t
; 
 425                         /** Type define for a standard string descriptor. Unlike other standard descriptors, the length 
 426                          *  of the descriptor for placement in the descriptor header must be determined by the \ref USB_STRING_LEN() 
 427                          *  macro rather than by the size of the descriptor structure, as the length is not fixed. 
 429                          *  This structure should also be used for string index 0, which contains the supported language IDs for 
 430                          *  the device as an array. 
 432                          *  \note The non-standard structure element names are documented here. If the 
 433                          *        USE_NONSTANDARD_DESCRIPTOR_NAMES token is not set, this structure contains elements  
 434                          *        with names identical to those listed in the USB standard. 
 438                                 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) || defined(__DOXYGEN__) 
 439                                 USB_Descriptor_Header_t Header
; /**< Descriptor header, including type and size. */ 
 441                                 int16_t                 UnicodeString
[]; /**< String data, as unicode characters (alternatively, 
 442                                                                           *   string language IDs). If normal ASCII characters are 
 443                                                                           *   to be used, they must be added as an array of characters 
 444                                                                           *   rather than a normal C string so that they are widened to 
 447                                                                           *   Under GCC, strings prefixed with the "L" character (before 
 448                                                                           *   the opening string quotation mark) are considered to be 
 449                                                                           *   Unicode strings, and may be used instead of an explicit 
 450                                                                           *   array of ASCII characters. 
 454                                 uint8_t                 bDescriptorType
; 
 457                         } USB_Descriptor_String_t
; 
 459         /* Private Interface - For use in library only: */ 
 460         #if !defined(__DOXYGEN__) 
 462                         #define VERSION_TENS(x)                   (int)(x / 10) 
 463                         #define VERSION_ONES(x)                   (int)(x - (10 * VERSION_TENS(x))) 
 464                         #define VERSION_TENTHS(x)                 (int)((x - (int)x) * 10) 
 465                         #define VERSION_HUNDREDTHS(x)             (int)(((x - (int)x) * 100) - (10 * VERSION_TENTHS(x))) 
 468         /* Disable C linkage for C++ Compilers: */ 
 469                 #if defined(__cplusplus)