3 Copyright (C) Dean Camera, 2018.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 disclaims 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 * This descriptor describes the multiple possible reports of the HID interface's report structure.
48 const USB_Descriptor_HIDReport_Datatype_t PROGMEM HIDReport
[] =
51 HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
52 HID_RI_USAGE(8, 0x02), /* Mouse */
53 HID_RI_COLLECTION(8, 0x01), /* Application */
54 HID_RI_REPORT_ID(8, HID_REPORTID_MouseReport
),
55 HID_RI_USAGE(8, 0x01), /* Pointer */
56 HID_RI_COLLECTION(8, 0x00), /* Physical */
57 HID_RI_USAGE_PAGE(8, 0x09), /* Button */
58 HID_RI_USAGE_MINIMUM(8, 0x01),
59 HID_RI_USAGE_MAXIMUM(8, 0x03),
60 HID_RI_LOGICAL_MINIMUM(8, 0x00),
61 HID_RI_LOGICAL_MAXIMUM(8, 0x01),
62 HID_RI_REPORT_COUNT(8, 0x03),
63 HID_RI_REPORT_SIZE(8, 0x01),
64 HID_RI_INPUT(8, HID_IOF_DATA
| HID_IOF_VARIABLE
| HID_IOF_ABSOLUTE
),
65 HID_RI_REPORT_COUNT(8, 0x01),
66 HID_RI_REPORT_SIZE(8, 0x05),
67 HID_RI_INPUT(8, HID_IOF_CONSTANT
),
68 HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
69 HID_RI_USAGE(8, 0x30), /* Usage X */
70 HID_RI_USAGE(8, 0x31), /* Usage Y */
71 HID_RI_LOGICAL_MINIMUM(8, -1),
72 HID_RI_LOGICAL_MAXIMUM(8, 1),
73 HID_RI_PHYSICAL_MINIMUM(8, -1),
74 HID_RI_PHYSICAL_MAXIMUM(8, 1),
75 HID_RI_REPORT_COUNT(8, 0x02),
76 HID_RI_REPORT_SIZE(8, 0x08),
77 HID_RI_INPUT(8, HID_IOF_DATA
| HID_IOF_VARIABLE
| HID_IOF_RELATIVE
),
78 HID_RI_END_COLLECTION(0),
79 HID_RI_END_COLLECTION(0),
82 HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
83 HID_RI_USAGE(8, 0x06), /* Keyboard */
84 HID_RI_COLLECTION(8, 0x01), /* Application */
85 HID_RI_REPORT_ID(8, HID_REPORTID_KeyboardReport
),
86 HID_RI_USAGE_PAGE(8, 0x07), /* Key Codes */
87 HID_RI_USAGE_MINIMUM(8, 0xE0), /* Keyboard Left Control */
88 HID_RI_USAGE_MAXIMUM(8, 0xE7), /* Keyboard Right GUI */
89 HID_RI_LOGICAL_MINIMUM(8, 0x00),
90 HID_RI_LOGICAL_MAXIMUM(8, 0x01),
91 HID_RI_REPORT_SIZE(8, 0x01),
92 HID_RI_REPORT_COUNT(8, 0x08),
93 HID_RI_INPUT(8, HID_IOF_DATA
| HID_IOF_VARIABLE
| HID_IOF_ABSOLUTE
),
94 HID_RI_REPORT_COUNT(8, 0x01),
95 HID_RI_REPORT_SIZE(8, 0x08),
96 HID_RI_INPUT(8, HID_IOF_CONSTANT
),
97 HID_RI_USAGE_PAGE(8, 0x08), /* LEDs */
98 HID_RI_USAGE_MINIMUM(8, 0x01), /* Num Lock */
99 HID_RI_USAGE_MAXIMUM(8, 0x05), /* Kana */
100 HID_RI_REPORT_COUNT(8, 0x05),
101 HID_RI_REPORT_SIZE(8, 0x01),
102 HID_RI_OUTPUT(8, HID_IOF_DATA
| HID_IOF_VARIABLE
| HID_IOF_ABSOLUTE
| HID_IOF_NON_VOLATILE
),
103 HID_RI_REPORT_COUNT(8, 0x01),
104 HID_RI_REPORT_SIZE(8, 0x03),
105 HID_RI_OUTPUT(8, HID_IOF_CONSTANT
),
106 HID_RI_LOGICAL_MINIMUM(8, 0x00),
107 HID_RI_LOGICAL_MAXIMUM(8, 0x65),
108 HID_RI_USAGE_PAGE(8, 0x07), /* Keyboard */
109 HID_RI_USAGE_MINIMUM(8, 0x00), /* Reserved (no event indicated) */
110 HID_RI_USAGE_MAXIMUM(8, 0x65), /* Keyboard Application */
111 HID_RI_REPORT_COUNT(8, 0x06),
112 HID_RI_REPORT_SIZE(8, 0x08),
113 HID_RI_INPUT(8, HID_IOF_DATA
| HID_IOF_ARRAY
| HID_IOF_ABSOLUTE
),
114 HID_RI_END_COLLECTION(0),
117 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
118 * device characteristics, including the supported USB version, control endpoint size and the
119 * number of device configurations. The descriptor is read out by the USB host when the enumeration
122 const USB_Descriptor_Device_t PROGMEM DeviceDescriptor
=
124 .Header
= {.Size
= sizeof(USB_Descriptor_Device_t
), .Type
= DTYPE_Device
},
126 .USBSpecification
= VERSION_BCD(1,1,0),
127 .Class
= USB_CSCP_NoDeviceClass
,
128 .SubClass
= USB_CSCP_NoDeviceSubclass
,
129 .Protocol
= USB_CSCP_NoDeviceProtocol
,
131 .Endpoint0Size
= FIXED_CONTROL_ENDPOINT_SIZE
,
135 .ReleaseNumber
= VERSION_BCD(0,0,1),
137 .ManufacturerStrIndex
= STRING_ID_Manufacturer
,
138 .ProductStrIndex
= STRING_ID_Product
,
139 .SerialNumStrIndex
= NO_DESCRIPTOR
,
141 .NumberOfConfigurations
= FIXED_NUM_CONFIGURATIONS
144 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
145 * of the device in one of its supported configurations, including information about any device interfaces
146 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
147 * a configuration so that the host may correctly communicate with the USB device.
149 const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor
=
153 .Header
= {.Size
= sizeof(USB_Descriptor_Configuration_Header_t
), .Type
= DTYPE_Configuration
},
155 .TotalConfigurationSize
= sizeof(USB_Descriptor_Configuration_t
),
156 .TotalInterfaces
= 1,
158 .ConfigurationNumber
= 1,
159 .ConfigurationStrIndex
= NO_DESCRIPTOR
,
161 .ConfigAttributes
= (USB_CONFIG_ATTR_RESERVED
| USB_CONFIG_ATTR_SELFPOWERED
),
163 .MaxPowerConsumption
= USB_CONFIG_POWER_MA(100)
168 .Header
= {.Size
= sizeof(USB_Descriptor_Interface_t
), .Type
= DTYPE_Interface
},
170 .InterfaceNumber
= INTERFACE_ID_KeyboardAndMouse
,
171 .AlternateSetting
= 0x00,
175 .Class
= HID_CSCP_HIDClass
,
176 .SubClass
= HID_CSCP_NonBootSubclass
,
177 .Protocol
= HID_CSCP_NonBootProtocol
,
179 .InterfaceStrIndex
= NO_DESCRIPTOR
184 .Header
= {.Size
= sizeof(USB_HID_Descriptor_HID_t
), .Type
= HID_DTYPE_HID
},
186 .HIDSpec
= VERSION_BCD(1,1,1),
188 .TotalReportDescriptors
= 1,
189 .HIDReportType
= HID_DTYPE_Report
,
190 .HIDReportLength
= sizeof(HIDReport
)
193 .HID_ReportINEndpoint
=
195 .Header
= {.Size
= sizeof(USB_Descriptor_Endpoint_t
), .Type
= DTYPE_Endpoint
},
197 .EndpointAddress
= HID_IN_EPADDR
,
198 .Attributes
= (EP_TYPE_INTERRUPT
| ENDPOINT_ATTR_NO_SYNC
| ENDPOINT_USAGE_DATA
),
199 .EndpointSize
= HID_EPSIZE
,
200 .PollingIntervalMS
= 0x05
204 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
205 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
206 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
208 const USB_Descriptor_String_t PROGMEM LanguageString
= USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG
);
210 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
211 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
214 const USB_Descriptor_String_t PROGMEM ManufacturerString
= USB_STRING_DESCRIPTOR(L
"LUFA Library");
216 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
217 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
220 const USB_Descriptor_String_t PROGMEM ProductString
= USB_STRING_DESCRIPTOR(L
"LUFA Multi HID Report Demo");
222 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
223 * documentation) by the application code so that the address and size of a requested descriptor can be given
224 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
225 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
228 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue
,
229 const uint16_t wIndex
,
230 const void** const DescriptorAddress
)
232 const uint8_t DescriptorType
= (wValue
>> 8);
233 const uint8_t DescriptorNumber
= (wValue
& 0xFF);
235 const void* Address
= NULL
;
236 uint16_t Size
= NO_DESCRIPTOR
;
238 switch (DescriptorType
)
241 Address
= &DeviceDescriptor
;
242 Size
= sizeof(USB_Descriptor_Device_t
);
244 case DTYPE_Configuration
:
245 Address
= &ConfigurationDescriptor
;
246 Size
= sizeof(USB_Descriptor_Configuration_t
);
249 switch (DescriptorNumber
)
251 case STRING_ID_Language
:
252 Address
= &LanguageString
;
253 Size
= pgm_read_byte(&LanguageString
.Header
.Size
);
255 case STRING_ID_Manufacturer
:
256 Address
= &ManufacturerString
;
257 Size
= pgm_read_byte(&ManufacturerString
.Header
.Size
);
259 case STRING_ID_Product
:
260 Address
= &ProductString
;
261 Size
= pgm_read_byte(&ProductString
.Header
.Size
);
267 Address
= &ConfigurationDescriptor
.HID_HIDData
;
268 Size
= sizeof(USB_HID_Descriptor_HID_t
);
270 case HID_DTYPE_Report
:
271 Address
= &HIDReport
;
272 Size
= sizeof(HIDReport
);
276 *DescriptorAddress
= Address
;