Abuse the the C preprocessor so that the HID macros can automatically encode data...
[pub/lufa.git] / Demos / Device / ClassDriver / KeyboardMouse / Descriptors.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 mouse HID interface's report structure.
47 */
48 USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =
49 {
50 HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
51 HID_RI_USAGE(8, 0x02), /* Mouse */
52 HID_RI_COLLECTION(8, 0x01), /* Application */
53 HID_RI_USAGE(8, 0x01), /* Pointer */
54 HID_RI_COLLECTION(8, 0x00), /* Physical */
55 HID_RI_USAGE_PAGE(8, 0x09), /* Button */
56 HID_RI_USAGE_MINIMUM(8, 0x01),
57 HID_RI_USAGE_MAXIMUM(8, 0x03),
58 HID_RI_LOGICAL_MINIMUM(8, 0x00),
59 HID_RI_LOGICAL_MAXIMUM(8, 0x01),
60 HID_RI_REPORT_COUNT(8, 0x03),
61 HID_RI_REPORT_SIZE(8, 0x01),
62 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_VOLATILE),
63 HID_RI_REPORT_COUNT(8, 0x01),
64 HID_RI_REPORT_SIZE(8, 0x05),
65 HID_RI_INPUT(8, HID_IOF_CONSTANT),
66 HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
67 HID_RI_USAGE(8, 0x30), /* Usage X */
68 HID_RI_USAGE(8, 0x31), /* Usage Y */
69 HID_RI_LOGICAL_MINIMUM(8, -1),
70 HID_RI_LOGICAL_MAXIMUM(8, 1),
71 HID_RI_PHYSICAL_MINIMUM(8, -1),
72 HID_RI_PHYSICAL_MAXIMUM(8, 1),
73 HID_RI_REPORT_COUNT(8, 0x02),
74 HID_RI_REPORT_SIZE(8, 0x08),
75 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE | HID_IOF_NON_VOLATILE),
76 HID_RI_END_COLLECTION(0),
77 HID_RI_END_COLLECTION(0),
78 };
79
80 /** Same as the MouseReport structure, but defines the keyboard HID interface's report structure. */
81 USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =
82 {
83 HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
84 HID_RI_USAGE(8, 0x06), /* Keyboard */
85 HID_RI_COLLECTION(8, 0x01), /* Application */
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 | HID_IOF_NON_VOLATILE),
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 | HID_IOF_NON_VOLATILE),
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 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 = 0x204D,
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 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 = 2,
157
158 .ConfigurationNumber = 1,
159 .ConfigurationStrIndex = NO_DESCRIPTOR,
160
161 .ConfigAttributes = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELFPOWERED),
162
163 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
164 },
165
166 .HID1_KeyboardInterface =
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_BootSubclass,
177 .Protocol = HID_CSCP_KeyboardBootProtocol,
178
179 .InterfaceStrIndex = NO_DESCRIPTOR
180 },
181
182 .HID1_KeyboardHID =
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(KeyboardReport)
191 },
192
193 .HID1_ReportINEndpoint =
194 {
195 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
196
197 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | KEYBOARD_IN_EPNUM),
198 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
199 .EndpointSize = HID_EPSIZE,
200 .PollingIntervalMS = 0x01
201 },
202
203 .HID2_MouseInterface =
204 {
205 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
206
207 .InterfaceNumber = 0x01,
208 .AlternateSetting = 0x00,
209
210 .TotalEndpoints = 1,
211
212 .Class = HID_CSCP_HIDClass,
213 .SubClass = HID_CSCP_BootSubclass,
214 .Protocol = HID_CSCP_MouseBootProtocol,
215
216 .InterfaceStrIndex = NO_DESCRIPTOR
217 },
218
219 .HID2_MouseHID =
220 {
221 .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
222
223 .HIDSpec = VERSION_BCD(01.11),
224 .CountryCode = 0x00,
225 .TotalReportDescriptors = 1,
226 .HIDReportType = HID_DTYPE_Report,
227 .HIDReportLength = sizeof(MouseReport)
228 },
229
230 .HID2_ReportINEndpoint =
231 {
232 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
233
234 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | MOUSE_IN_EPNUM),
235 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
236 .EndpointSize = HID_EPSIZE,
237 .PollingIntervalMS = 0x01
238 }
239 };
240
241 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
242 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
243 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
244 */
245 USB_Descriptor_String_t PROGMEM LanguageString =
246 {
247 .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
248
249 .UnicodeString = {LANGUAGE_ID_ENG}
250 };
251
252 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
253 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
254 * Descriptor.
255 */
256 USB_Descriptor_String_t PROGMEM ManufacturerString =
257 {
258 .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
259
260 .UnicodeString = L"Dean Camera"
261 };
262
263 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
264 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
265 * Descriptor.
266 */
267 USB_Descriptor_String_t PROGMEM ProductString =
268 {
269 .Header = {.Size = USB_STRING_LEN(28), .Type = DTYPE_String},
270
271 .UnicodeString = L"LUFA Mouse and Keyboard Demo"
272 };
273
274 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
275 * documentation) by the application code so that the address and size of a requested descriptor can be given
276 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
277 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
278 * USB host.
279 */
280 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
281 const uint8_t wIndex,
282 const void** const DescriptorAddress)
283 {
284 const uint8_t DescriptorType = (wValue >> 8);
285 const uint8_t DescriptorNumber = (wValue & 0xFF);
286
287 const void* Address = NULL;
288 uint16_t Size = NO_DESCRIPTOR;
289
290 switch (DescriptorType)
291 {
292 case DTYPE_Device:
293 Address = &DeviceDescriptor;
294 Size = sizeof(USB_Descriptor_Device_t);
295 break;
296 case DTYPE_Configuration:
297 Address = &ConfigurationDescriptor;
298 Size = sizeof(USB_Descriptor_Configuration_t);
299 break;
300 case DTYPE_String:
301 switch (DescriptorNumber)
302 {
303 case 0x00:
304 Address = &LanguageString;
305 Size = pgm_read_byte(&LanguageString.Header.Size);
306 break;
307 case 0x01:
308 Address = &ManufacturerString;
309 Size = pgm_read_byte(&ManufacturerString.Header.Size);
310 break;
311 case 0x02:
312 Address = &ProductString;
313 Size = pgm_read_byte(&ProductString.Header.Size);
314 break;
315 }
316
317 break;
318 case HID_DTYPE_HID:
319 if (!(wIndex))
320 {
321 Address = &ConfigurationDescriptor.HID1_KeyboardHID;
322 Size = sizeof(USB_HID_Descriptor_HID_t);
323 }
324 else
325 {
326 Address = &ConfigurationDescriptor.HID2_MouseHID;
327 Size = sizeof(USB_HID_Descriptor_HID_t);
328 }
329 break;
330 case HID_DTYPE_Report:
331 if (!(wIndex))
332 {
333 Address = &KeyboardReport;
334 Size = sizeof(KeyboardReport);
335 }
336 else
337 {
338 Address = &MouseReport;
339 Size = sizeof(MouseReport);
340 }
341
342 break;
343 }
344
345 *DescriptorAddress = Address;
346 return Size;
347 }
348