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  *  USB Device Descriptors, for library use when in USB device mode. Descriptors are special  
  34  *  computer-readable structures which the host requests upon device enumeration, to determine 
  35  *  the device's capabilities and functions.   
  38 #include "Descriptors.h" 
  40 /** HID class report descriptor. This is a special descriptor constructed with values from the 
  41  *  USBIF HID class specification to describe the reports and capabilities of the HID device. This 
  42  *  descriptor is parsed by the host and its contents used to determine what data (and in what encoding) 
  43  *  the device will send, and what it may be sent back from the host. Refer to the HID specification for 
  44  *  more details on HID report descriptors. 
  46 USB_Descriptor_HIDReport_Datatype_t PROGMEM JoystickReport
[] = 
  48         0x05, 0x01,          /* Usage Page (Generic Desktop)                       */ 
  49         0x09, 0x04,          /* Usage (Joystick)                                   */ 
  50         0xa1, 0x01,          /* Collection (Application)                           */ 
  51         0x09, 0x01,          /*   Usage (Pointer)                                  */ 
  52         0xa1, 0x00,          /*   Collection (Physical)                            */ 
  53         0x05, 0x01,          /*     Usage Page (Generic Desktop)                   */ 
  54         0x09, 0x30,          /*     Usage (X)                                      */ 
  55         0x09, 0x31,          /*     Usage (Y)                                      */ 
  56         0x15, 0x9c,          /*     Logical Minimum (-100)                         */ 
  57         0x25, 0x64,          /*     Logical Maximum (100)                          */ 
  58         0x75, 0x08,          /*     Report Size (8)                                */ 
  59         0x95, 0x02,          /*     Report Count (2)                               */ 
  60         0x81, 0x82,          /*     Input (Data, Variable, Absolute, Volatile)     */ 
  61         0x05, 0x09,          /*     Usage Page (Button)                            */ 
  62         0x09, 0x02,          /*     Usage (Button 2)                               */ 
  63         0x09, 0x01,          /*     Usage (Button 1)                               */ 
  64         0x15, 0x00,          /*     Logical Minimum (0)                            */ 
  65         0x25, 0x01,          /*     Logical Maximum (1)                            */ 
  66         0x75, 0x01,          /*     Report Size (1)                                */ 
  67         0x95, 0x02,          /*     Report Count (2)                               */ 
  68         0x81, 0x02,          /*     Input (Data, Variable, Absolute)               */ 
  69         0x75, 0x06,          /*     Report Size (6)                                */ 
  70         0x95, 0x01,          /*     Report Count (1)                               */ 
  71         0x81, 0x01,          /*     Input (Constant)                               */ 
  72         0xc0,                /*   End Collection                                   */ 
  73         0xc0                 /* End Collection                                     */ 
  76 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall 
  77  *  device characteristics, including the supported USB version, control endpoint size and the 
  78  *  number of device configurations. The descriptor is read out by the USB host when the enumeration 
  81 USB_Descriptor_Device_t PROGMEM DeviceDescriptor 
= 
  83         .Header                 
= {.Size 
= sizeof(USB_Descriptor_Device_t
), .Type 
= DTYPE_Device
}, 
  85         .USBSpecification       
= VERSION_BCD(01.10), 
  90         .Endpoint0Size          
= FIXED_CONTROL_ENDPOINT_SIZE
, 
  94         .ReleaseNumber          
= 0x0000, 
  96         .ManufacturerStrIndex   
= 0x01, 
  97         .ProductStrIndex        
= 0x02, 
  98         .SerialNumStrIndex      
= NO_DESCRIPTOR
, 
 100         .NumberOfConfigurations 
= 1 
 103 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage 
 104  *  of the device in one of its supported configurations, including information about any device interfaces 
 105  *  and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting 
 106  *  a configuration so that the host may correctly communicate with the USB device. 
 108 USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor 
= 
 112                         .Header                 
= {.Size 
= sizeof(USB_Descriptor_Configuration_Header_t
), .Type 
= DTYPE_Configuration
}, 
 114                         .TotalConfigurationSize 
= sizeof(USB_Descriptor_Configuration_t
), 
 115                         .TotalInterfaces        
= 1, 
 117                         .ConfigurationNumber    
= 1, 
 118                         .ConfigurationStrIndex  
= NO_DESCRIPTOR
, 
 120                         .ConfigAttributes       
= (USB_CONFIG_ATTR_BUSPOWERED 
| USB_CONFIG_ATTR_SELFPOWERED
), 
 122                         .MaxPowerConsumption    
= USB_CONFIG_POWER_MA(100) 
 127                         .Header                 
= {.Size 
= sizeof(USB_Descriptor_Interface_t
), .Type 
= DTYPE_Interface
}, 
 129                         .InterfaceNumber        
= 0x00, 
 130                         .AlternateSetting       
= 0x00, 
 138                         .InterfaceStrIndex      
= NO_DESCRIPTOR
 
 143                         .Header                 
= {.Size 
= sizeof(USB_HID_Descriptor_t
), .Type 
= DTYPE_HID
}, 
 145                         .HIDSpec                
= VERSION_BCD(01.11), 
 147                         .TotalReportDescriptors 
= 1, 
 148                         .HIDReportType          
= DTYPE_Report
, 
 149                         .HIDReportLength        
= sizeof(JoystickReport
) 
 154                         .Header                 
= {.Size 
= sizeof(USB_Descriptor_Endpoint_t
), .Type 
= DTYPE_Endpoint
}, 
 156                         .EndpointAddress        
= (ENDPOINT_DESCRIPTOR_DIR_IN 
| JOYSTICK_EPNUM
), 
 157                         .Attributes             
= EP_TYPE_INTERRUPT
, 
 158                         .EndpointSize           
= JOYSTICK_EPSIZE
, 
 159                         .PollingIntervalMS      
= 0x02 
 163 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests 
 164  *  the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate 
 165  *  via the language ID table available at USB.org what languages the device supports for its string descriptors. 
 167 USB_Descriptor_String_t PROGMEM LanguageString 
= 
 169         .Header                 
= {.Size 
= USB_STRING_LEN(1), .Type 
= DTYPE_String
}, 
 171         .UnicodeString          
= {LANGUAGE_ID_ENG
} 
 174 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable 
 175  *  form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device 
 178 USB_Descriptor_String_t PROGMEM ManufacturerString 
= 
 180         .Header                 
= {.Size 
= USB_STRING_LEN(11), .Type 
= DTYPE_String
}, 
 182         .UnicodeString          
= L
"Dean Camera" 
 185 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form, 
 186  *  and is read out upon request by the host when the appropriate string ID is requested, listed in the Device 
 189 USB_Descriptor_String_t PROGMEM ProductString 
= 
 191         .Header                 
= {.Size 
= USB_STRING_LEN(18), .Type 
= DTYPE_String
}, 
 193         .UnicodeString          
= L
"LUFA Joystick Demo" 
 196 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors" 
 197  *  documentation) by the application code so that the address and size of a requested descriptor can be given 
 198  *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function 
 199  *  is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the 
 202 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue
, const uint8_t wIndex
, void** const DescriptorAddress
) 
 204         const uint8_t  DescriptorType   
= (wValue 
>> 8); 
 205         const uint8_t  DescriptorNumber 
= (wValue 
& 0xFF); 
 207         void*    Address 
= NULL
; 
 208         uint16_t Size    
= NO_DESCRIPTOR
; 
 210         switch (DescriptorType
) 
 213                         Address 
= (void*)&DeviceDescriptor
; 
 214                         Size    
= sizeof(USB_Descriptor_Device_t
); 
 216                 case DTYPE_Configuration
:  
 217                         Address 
= (void*)&ConfigurationDescriptor
; 
 218                         Size    
= sizeof(USB_Descriptor_Configuration_t
); 
 221                         switch (DescriptorNumber
) 
 224                                         Address 
= (void*)&LanguageString
; 
 225                                         Size    
= pgm_read_byte(&LanguageString
.Header
.Size
); 
 228                                         Address 
= (void*)&ManufacturerString
; 
 229                                         Size    
= pgm_read_byte(&ManufacturerString
.Header
.Size
); 
 232                                         Address 
= (void*)&ProductString
; 
 233                                         Size    
= pgm_read_byte(&ProductString
.Header
.Size
); 
 239                         Address 
= (void*)&ConfigurationDescriptor
.JoystickHID
; 
 240                         Size    
= sizeof(USB_HID_Descriptor_t
); 
 243                         Address 
= (void*)&JoystickReport
; 
 244                         Size    
= sizeof(JoystickReport
); 
 248         *DescriptorAddress 
= Address
;