Add tag for the 120219 release.
[pub/USBasp.git] / Demos / Device / ClassDriver / KeyboardMouseMultiReport / Descriptors.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
36 */
37
38 #include "Descriptors.h"
39
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.
45 *
46 * This descriptor describes the multiple possible reports of the HID interface's report structure.
47 */
48 const USB_Descriptor_HIDReport_Datatype_t PROGMEM HIDReport[] =
49 {
50 /* Mouse Report */
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),
80
81 /* Keyboard Report */
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),
115 };
116
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
120 * process begins.
121 */
122 const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
123 {
124 .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
125
126 .USBSpecification = VERSION_BCD(01.10),
127 .Class = USB_CSCP_NoDeviceClass,
128 .SubClass = USB_CSCP_NoDeviceSubclass,
129 .Protocol = USB_CSCP_NoDeviceProtocol,
130
131 .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
132
133 .VendorID = 0x03EB,
134 .ProductID = 0x2066,
135 .ReleaseNumber = VERSION_BCD(00.01),
136
137 .ManufacturerStrIndex = 0x01,
138 .ProductStrIndex = 0x02,
139 .SerialNumStrIndex = NO_DESCRIPTOR,
140
141 .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
142 };
143
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.
148 */
149 const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
150 {
151 .Config =
152 {
153 .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
154
155 .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
156 .TotalInterfaces = 1,
157
158 .ConfigurationNumber = 1,
159 .ConfigurationStrIndex = NO_DESCRIPTOR,
160
161 .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
162
163 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
164 },
165
166 .HID_Interface =
167 {
168 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
169
170 .InterfaceNumber = 0x00,
171 .AlternateSetting = 0x00,
172
173 .TotalEndpoints = 1,
174
175 .Class = HID_CSCP_HIDClass,
176 .SubClass = HID_CSCP_NonBootSubclass,
177 .Protocol = HID_CSCP_NonBootProtocol,
178
179 .InterfaceStrIndex = NO_DESCRIPTOR
180 },
181
182 .HID_HIDData =
183 {
184 .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
185
186 .HIDSpec = VERSION_BCD(01.11),
187 .CountryCode = 0x00,
188 .TotalReportDescriptors = 1,
189 .HIDReportType = HID_DTYPE_Report,
190 .HIDReportLength = sizeof(HIDReport)
191 },
192
193 .HID_ReportINEndpoint =
194 {
195 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
196
197 .EndpointAddress = (ENDPOINT_DIR_IN | HID_IN_EPNUM),
198 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
199 .EndpointSize = HID_EPSIZE,
200 .PollingIntervalMS = 0x01
201 },
202 };
203
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.
207 */
208 const USB_Descriptor_String_t PROGMEM LanguageString =
209 {
210 .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
211
212 .UnicodeString = {LANGUAGE_ID_ENG}
213 };
214
215 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
216 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
217 * Descriptor.
218 */
219 const USB_Descriptor_String_t PROGMEM ManufacturerString =
220 {
221 .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
222
223 .UnicodeString = L"Dean Camera"
224 };
225
226 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
227 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
228 * Descriptor.
229 */
230 const USB_Descriptor_String_t PROGMEM ProductString =
231 {
232 .Header = {.Size = USB_STRING_LEN(26), .Type = DTYPE_String},
233
234 .UnicodeString = L"LUFA Multi HID Report Demo"
235 };
236
237 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
238 * documentation) by the application code so that the address and size of a requested descriptor can be given
239 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
240 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
241 * USB host.
242 */
243 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
244 const uint8_t wIndex,
245 const void** const DescriptorAddress)
246 {
247 const uint8_t DescriptorType = (wValue >> 8);
248 const uint8_t DescriptorNumber = (wValue & 0xFF);
249
250 const void* Address = NULL;
251 uint16_t Size = NO_DESCRIPTOR;
252
253 switch (DescriptorType)
254 {
255 case DTYPE_Device:
256 Address = &DeviceDescriptor;
257 Size = sizeof(USB_Descriptor_Device_t);
258 break;
259 case DTYPE_Configuration:
260 Address = &ConfigurationDescriptor;
261 Size = sizeof(USB_Descriptor_Configuration_t);
262 break;
263 case DTYPE_String:
264 switch (DescriptorNumber)
265 {
266 case 0x00:
267 Address = &LanguageString;
268 Size = pgm_read_byte(&LanguageString.Header.Size);
269 break;
270 case 0x01:
271 Address = &ManufacturerString;
272 Size = pgm_read_byte(&ManufacturerString.Header.Size);
273 break;
274 case 0x02:
275 Address = &ProductString;
276 Size = pgm_read_byte(&ProductString.Header.Size);
277 break;
278 }
279
280 break;
281 case HID_DTYPE_HID:
282 Address = &ConfigurationDescriptor.HID_HIDData;
283 Size = sizeof(USB_HID_Descriptor_HID_t);
284 break;
285 case HID_DTYPE_Report:
286 Address = &HIDReport;
287 Size = sizeof(HIDReport);
288 break;
289 }
290
291 *DescriptorAddress = Address;
292 return Size;
293 }
294