3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  11   Copyright 2010  Matthias Hullin (lufa [at] matthias [dot] hullin [dot] net) 
  13   Permission to use, copy, modify, distribute, and sell this  
  14   software and its documentation for any purpose is hereby granted 
  15   without fee, provided that the above copyright notice appear in  
  16   all copies and that both that the copyright notice and this 
  17   permission notice and warranty disclaimer appear in supporting  
  18   documentation, and that the name of the author not be used in  
  19   advertising or publicity pertaining to distribution of the  
  20   software without specific, written prior permission. 
  22   The author disclaim all warranties with regard to this 
  23   software, including all implied warranties of merchantability 
  24   and fitness.  In no event shall the author be liable for any 
  25   special, indirect or consequential damages or any damages 
  26   whatsoever resulting from loss of use, data or profits, whether 
  27   in an action of contract, negligence or other tortious action, 
  28   arising out of or in connection with the use or performance of 
  34  *  USB Device Descriptors, for library use when in USB device mode. Descriptors are special  
  35  *  computer-readable structures which the host requests upon device enumeration, to determine 
  36  *  the device's capabilities and functions.   
  39 #include "Descriptors.h" 
  41 /* On some devices, there is a factory set internal serial number which can be automatically sent to the host as 
  42  * the device's serial number when the Device Descriptor's .SerialNumStrIndex entry is set to USE_INTERNAL_SERIAL. 
  43  * This allows the host to track a device across insertions on different ports, allowing them to retain allocated 
  44  * resources like COM port numbers and drivers. On demos using this feature, give a warning on unsupported devices 
  45  * so that the user can supply their own serial number descriptor instead or remove the USE_INTERNAL_SERIAL value 
  46  * from the Device Descriptor (forcing the host to generate a serial number for each device from the VID, PID and 
  49 #if (USE_INTERNAL_SERIAL == NO_DESCRIPTOR) 
  50         #warning USE_INTERNAL_SERIAL is not available on this AVR - please manually construct a device serial descriptor. 
  53 /** HID class report descriptor. This is a special descriptor constructed with values from the 
  54  *  USBIF HID class specification to describe the reports and capabilities of the HID device. This 
  55  *  descriptor is parsed by the host and its contents used to determine what data (and in what encoding) 
  56  *  the device will send, and what it may be sent back from the host. Refer to the HID specification for 
  57  *  more details on HID report descriptors. 
  59 USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport
[] = 
  61         0x05, 0x01,          /* Usage Page (Generic Desktop)                    */ 
  62         0x09, 0x06,          /* Usage (Keyboard)                                */ 
  63         0xa1, 0x01,          /* Collection (Application)                        */ 
  64         0x75, 0x01,          /*   Report Size (1)                               */ 
  65         0x95, 0x08,          /*   Report Count (8)                              */ 
  66         0x05, 0x07,          /*   Usage Page (Key Codes)                        */ 
  67         0x19, 0xe0,          /*   Usage Minimum (Keyboard LeftControl)          */ 
  68         0x29, 0xe7,          /*   Usage Maximum (Keyboard Right GUI)            */ 
  69         0x15, 0x00,          /*   Logical Minimum (0)                           */ 
  70         0x25, 0x01,          /*   Logical Maximum (1)                           */ 
  71         0x81, 0x02,          /*   Input (Data, Variable, Absolute)              */ 
  72         0x95, 0x01,          /*   Report Count (1)                              */ 
  73         0x75, 0x08,          /*   Report Size (8)                               */ 
  74         0x81, 0x03,          /*   Input (Const, Variable, Absolute)             */ 
  75         0x95, 0x05,          /*   Report Count (5)                              */ 
  76         0x75, 0x01,          /*   Report Size (1)                               */ 
  77         0x05, 0x08,          /*   Usage Page (LEDs)                             */ 
  78         0x19, 0x01,          /*   Usage Minimum (Num Lock)                      */ 
  79         0x29, 0x05,          /*   Usage Maximum (Kana)                          */ 
  80         0x91, 0x02,          /*   Output (Data, Variable, Absolute)             */ 
  81         0x95, 0x01,          /*   Report Count (1)                              */ 
  82         0x75, 0x03,          /*   Report Size (3)                               */ 
  83         0x91, 0x03,          /*   Output (Const, Variable, Absolute)            */ 
  84         0x95, 0x06,          /*   Report Count (6)                              */ 
  85         0x75, 0x08,          /*   Report Size (8)                               */ 
  86         0x15, 0x00,          /*   Logical Minimum (0)                           */ 
  87         0x25, 0x65,          /*   Logical Maximum (101)                         */ 
  88         0x05, 0x07,          /*   Usage Page (Keyboard)                         */ 
  89         0x19, 0x00,          /*   Usage Minimum (Reserved (no event indicated)) */ 
  90         0x29, 0x65,          /*   Usage Maximum (Keyboard Application)          */ 
  91         0x81, 0x00,          /*   Input (Data, Array, Absolute)                 */ 
  92         0xc0                 /* End Collection                                  */ 
  95 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall 
  96  *  device characteristics, including the supported USB version, control endpoint size and the 
  97  *  number of device configurations. The descriptor is read out by the USB host when the enumeration 
 100 USB_Descriptor_Device_t PROGMEM DeviceDescriptor 
= 
 102         .Header                 
= {.Size 
= sizeof(USB_Descriptor_Device_t
), .Type 
= DTYPE_Device
}, 
 104         .USBSpecification       
= VERSION_BCD(01.10), 
 109         .Endpoint0Size          
= FIXED_CONTROL_ENDPOINT_SIZE
, 
 113         .ReleaseNumber          
= 0x0000, 
 115         .ManufacturerStrIndex   
= 0x01, 
 116         .ProductStrIndex        
= 0x02, 
 117         .SerialNumStrIndex      
= USE_INTERNAL_SERIAL
, 
 119         .NumberOfConfigurations 
= FIXED_NUM_CONFIGURATIONS
 
 122 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage 
 123  *  of the device in one of its supported configurations, including information about any device interfaces 
 124  *  and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting 
 125  *  a configuration so that the host may correctly communicate with the USB device. 
 127 USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor 
= 
 131                         .Header                 
= {.Size 
= sizeof(USB_Descriptor_Configuration_Header_t
), .Type 
= DTYPE_Configuration
}, 
 133                         .TotalConfigurationSize 
= sizeof(USB_Descriptor_Configuration_t
), 
 134                         .TotalInterfaces        
= 2, 
 136                         .ConfigurationNumber    
= 1, 
 137                         .ConfigurationStrIndex  
= NO_DESCRIPTOR
, 
 139                         .ConfigAttributes       
= USB_CONFIG_ATTR_BUSPOWERED
, 
 141                         .MaxPowerConsumption    
= USB_CONFIG_POWER_MA(100) 
 146                         .Header                 
= {.Size 
= sizeof(USB_Descriptor_Interface_t
), .Type 
= DTYPE_Interface
}, 
 148                         .InterfaceNumber        
= 0, 
 149                         .AlternateSetting       
= 0, 
 157                         .InterfaceStrIndex      
= NO_DESCRIPTOR
 
 162                         .Header                 
= {.Size 
= sizeof(USB_Descriptor_Endpoint_t
), .Type 
= DTYPE_Endpoint
}, 
 164                         .EndpointAddress        
= (ENDPOINT_DESCRIPTOR_DIR_IN 
| MASS_STORAGE_IN_EPNUM
), 
 165                         .Attributes             
= EP_TYPE_BULK
, 
 166                         .EndpointSize           
= MASS_STORAGE_IO_EPSIZE
, 
 167                         .PollingIntervalMS      
= 0x00 
 170         .MS_DataOutEndpoint 
=  
 172                         .Header                 
= {.Size 
= sizeof(USB_Descriptor_Endpoint_t
), .Type 
= DTYPE_Endpoint
}, 
 174                         .EndpointAddress        
= (ENDPOINT_DESCRIPTOR_DIR_OUT 
| MASS_STORAGE_OUT_EPNUM
), 
 175                         .Attributes             
= EP_TYPE_BULK
, 
 176                         .EndpointSize           
= MASS_STORAGE_IO_EPSIZE
, 
 177                         .PollingIntervalMS      
= 0x00 
 180         .HID_KeyboardInterface 
=  
 182                         .Header                 
= {.Size 
= sizeof(USB_Descriptor_Interface_t
), .Type 
= DTYPE_Interface
}, 
 184                         .InterfaceNumber        
= 1, 
 185                         .AlternateSetting       
= 0, 
 193                         .InterfaceStrIndex      
= NO_DESCRIPTOR
 
 198                         .Header                 
= {.Size 
= sizeof(USB_HID_Descriptor_t
), .Type 
= DTYPE_HID
}, 
 200                         .HIDSpec                
= VERSION_BCD(01.11), 
 202                         .TotalReportDescriptors 
= 1, 
 203                         .HIDReportType          
= DTYPE_Report
, 
 204                         .HIDReportLength        
= sizeof(KeyboardReport
) 
 207         .HID_ReportINEndpoint 
=  
 209                         .Header                 
= {.Size 
= sizeof(USB_Descriptor_Endpoint_t
), .Type 
= DTYPE_Endpoint
}, 
 211                         .EndpointAddress        
= (ENDPOINT_DESCRIPTOR_DIR_IN 
| KEYBOARD_EPNUM
), 
 212                         .Attributes             
= EP_TYPE_INTERRUPT
, 
 213                         .EndpointSize           
= KEYBOARD_EPSIZE
, 
 214                         .PollingIntervalMS      
= 0x04 
 218 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests 
 219  *  the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate 
 220  *  via the language ID table available at USB.org what languages the device supports for its string descriptors. 
 222 USB_Descriptor_String_t PROGMEM LanguageString 
= 
 224         .Header                 
= {.Size 
= USB_STRING_LEN(1), .Type 
= DTYPE_String
}, 
 226         .UnicodeString          
= {LANGUAGE_ID_ENG
} 
 229 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable 
 230  *  form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device 
 233 USB_Descriptor_String_t PROGMEM ManufacturerString 
= 
 235         .Header                 
= {.Size 
= USB_STRING_LEN(11), .Type 
= DTYPE_String
}, 
 237         .UnicodeString          
= L
"Dean Camera" 
 240 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form, 
 241  *  and is read out upon request by the host when the appropriate string ID is requested, listed in the Device 
 244 USB_Descriptor_String_t PROGMEM ProductString 
= 
 246         .Header                 
= {.Size 
= USB_STRING_LEN(35), .Type 
= DTYPE_String
}, 
 248         .UnicodeString          
= L
"LUFA Mass Storage and Keyboard Demo" 
 251 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors" 
 252  *  documentation) by the application code so that the address and size of a requested descriptor can be given 
 253  *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function 
 254  *  is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the 
 257 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue
, const uint8_t wIndex
, void** const DescriptorAddress
) 
 259         const uint8_t  DescriptorType   
= (wValue 
>> 8); 
 260         const uint8_t  DescriptorNumber 
= (wValue 
& 0xFF); 
 262         void*    Address 
= NULL
; 
 263         uint16_t Size    
= NO_DESCRIPTOR
; 
 265         switch (DescriptorType
) 
 268                         Address 
= (void*)&DeviceDescriptor
; 
 269                         Size    
= sizeof(USB_Descriptor_Device_t
); 
 271                 case DTYPE_Configuration
:  
 272                         Address 
= (void*)&ConfigurationDescriptor
; 
 273                         Size    
= sizeof(USB_Descriptor_Configuration_t
); 
 276                         switch (DescriptorNumber
) 
 279                                         Address 
= (void*)&LanguageString
; 
 280                                         Size    
= pgm_read_byte(&LanguageString
.Header
.Size
); 
 283                                         Address 
= (void*)&ManufacturerString
; 
 284                                         Size    
= pgm_read_byte(&ManufacturerString
.Header
.Size
); 
 287                                         Address 
= (void*)&ProductString
; 
 288                                         Size    
= pgm_read_byte(&ProductString
.Header
.Size
); 
 294                         Address 
= (void*)&ConfigurationDescriptor
.HID_KeyboardHID
; 
 295                         Size    
= sizeof(USB_HID_Descriptor_t
); 
 298                         Address 
= (void*)&KeyboardReport
; 
 299                         Size    
= sizeof(KeyboardReport
); 
 303         *DescriptorAddress 
= Address
;