e7f256215626e3f9966782f9e1d55cccbd45330f
[pub/USBasp.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), 0,
59 HID_RI_LOGICAL_MAXIMUM(8), 1,
60 HID_RI_REPORT_COUNT(8), 3,
61 HID_RI_REPORT_SIZE(8), 1,
62 HID_RI_INPUT(8), (HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_VOLATILE),
63
64 HID_RI_REPORT_COUNT(8), 1,
65 HID_RI_REPORT_SIZE(8), 5,
66 HID_RI_INPUT(8), HID_IOF_CONSTANT,
67
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), 2,
76 HID_RI_REPORT_SIZE(8), 8,
77 HID_RI_INPUT(8), (HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE | HID_IOF_NON_VOLATILE),
78 HID_RI_END_COLLECTION(0),
79 HID_RI_END_COLLECTION(0),
80 };
81
82 /** Same as the MouseReport structure, but defines the keyboard HID interface's report structure. */
83 USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =
84 {
85 HID_RI_USAGE_PAGE(8), 0x01, /* Generic Desktop */
86 HID_RI_USAGE(8), 0x06, /* Keyboard */
87 HID_RI_COLLECTION(8), 0x01, /* Application */
88 HID_RI_USAGE_PAGE(8), 0x07, /* Key Codes */
89 HID_RI_USAGE_MINIMUM(8), 0xE0, /* Keyboard Left Control */
90 HID_RI_USAGE_MAXIMUM(8), 0xE7, /* Keyboard Right GUI */
91 HID_RI_LOGICAL_MINIMUM(8), 0,
92 HID_RI_LOGICAL_MAXIMUM(8), 1,
93 HID_RI_REPORT_SIZE(8), 1,
94 HID_RI_REPORT_COUNT(8), 8,
95 HID_RI_INPUT(8), (HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
96
97 HID_RI_REPORT_COUNT(8), 1,
98 HID_RI_REPORT_SIZE(8), 8,
99 HID_RI_INPUT(8), HID_IOF_CONSTANT,
100
101 HID_RI_USAGE_PAGE(8), 0x08, /* LEDs */
102 HID_RI_USAGE_MINIMUM(8), 0x01, /* Num Lock */
103 HID_RI_USAGE_MAXIMUM(8), 0x05, /* Kana */
104 HID_RI_REPORT_COUNT(8), 5,
105 HID_RI_REPORT_SIZE(8), 1,
106 HID_RI_OUTPUT(8), (HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
107 HID_RI_REPORT_COUNT(8), 1,
108 HID_RI_REPORT_SIZE(8), 3,
109 HID_RI_OUTPUT(8), HID_IOF_CONSTANT,
110
111 HID_RI_LOGICAL_MINIMUM(8), 0x00,
112 HID_RI_LOGICAL_MAXIMUM(8), 0x65,
113 HID_RI_USAGE_PAGE(8), 0x07, /* Keyboard */
114 HID_RI_USAGE_MINIMUM(8), 0x00, /* Reserved (no event indicated) */
115 HID_RI_USAGE_MAXIMUM(8), 0x65, /* Keyboard Application */
116 HID_RI_REPORT_COUNT(8), 6,
117 HID_RI_REPORT_SIZE(8), 8,
118 HID_RI_INPUT(8), (HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
119 HID_RI_END_COLLECTION(0),
120 };
121
122 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
123 * device characteristics, including the supported USB version, control endpoint size and the
124 * number of device configurations. The descriptor is read out by the USB host when the enumeration
125 * process begins.
126 */
127 USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
128 {
129 .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
130
131 .USBSpecification = VERSION_BCD(01.10),
132 .Class = USB_CSCP_NoDeviceClass,
133 .SubClass = USB_CSCP_NoDeviceSubclass,
134 .Protocol = USB_CSCP_NoDeviceProtocol,
135
136 .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
137
138 .VendorID = 0x03EB,
139 .ProductID = 0x204D,
140 .ReleaseNumber = VERSION_BCD(00.01),
141
142 .ManufacturerStrIndex = 0x01,
143 .ProductStrIndex = 0x02,
144 .SerialNumStrIndex = NO_DESCRIPTOR,
145
146 .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
147 };
148
149 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
150 * of the device in one of its supported configurations, including information about any device interfaces
151 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
152 * a configuration so that the host may correctly communicate with the USB device.
153 */
154 USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
155 {
156 .Config =
157 {
158 .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
159
160 .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
161 .TotalInterfaces = 2,
162
163 .ConfigurationNumber = 1,
164 .ConfigurationStrIndex = NO_DESCRIPTOR,
165
166 .ConfigAttributes = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELFPOWERED),
167
168 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
169 },
170
171 .HID1_KeyboardInterface =
172 {
173 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
174
175 .InterfaceNumber = 0x00,
176 .AlternateSetting = 0x00,
177
178 .TotalEndpoints = 1,
179
180 .Class = HID_CSCP_HIDClass,
181 .SubClass = HID_CSCP_BootSubclass,
182 .Protocol = HID_CSCP_KeyboardBootProtocol,
183
184 .InterfaceStrIndex = NO_DESCRIPTOR
185 },
186
187 .HID1_KeyboardHID =
188 {
189 .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
190
191 .HIDSpec = VERSION_BCD(01.11),
192 .CountryCode = 0x00,
193 .TotalReportDescriptors = 1,
194 .HIDReportType = HID_DTYPE_Report,
195 .HIDReportLength = sizeof(KeyboardReport)
196 },
197
198 .HID1_ReportINEndpoint =
199 {
200 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
201
202 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | KEYBOARD_IN_EPNUM),
203 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
204 .EndpointSize = HID_EPSIZE,
205 .PollingIntervalMS = 0x01
206 },
207
208 .HID2_MouseInterface =
209 {
210 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
211
212 .InterfaceNumber = 0x01,
213 .AlternateSetting = 0x00,
214
215 .TotalEndpoints = 1,
216
217 .Class = HID_CSCP_HIDClass,
218 .SubClass = HID_CSCP_BootSubclass,
219 .Protocol = HID_CSCP_MouseBootProtocol,
220
221 .InterfaceStrIndex = NO_DESCRIPTOR
222 },
223
224 .HID2_MouseHID =
225 {
226 .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
227
228 .HIDSpec = VERSION_BCD(01.11),
229 .CountryCode = 0x00,
230 .TotalReportDescriptors = 1,
231 .HIDReportType = HID_DTYPE_Report,
232 .HIDReportLength = sizeof(MouseReport)
233 },
234
235 .HID2_ReportINEndpoint =
236 {
237 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
238
239 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | MOUSE_IN_EPNUM),
240 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
241 .EndpointSize = HID_EPSIZE,
242 .PollingIntervalMS = 0x01
243 }
244 };
245
246 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
247 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
248 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
249 */
250 USB_Descriptor_String_t PROGMEM LanguageString =
251 {
252 .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
253
254 .UnicodeString = {LANGUAGE_ID_ENG}
255 };
256
257 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
258 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
259 * Descriptor.
260 */
261 USB_Descriptor_String_t PROGMEM ManufacturerString =
262 {
263 .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
264
265 .UnicodeString = L"Dean Camera"
266 };
267
268 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
269 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
270 * Descriptor.
271 */
272 USB_Descriptor_String_t PROGMEM ProductString =
273 {
274 .Header = {.Size = USB_STRING_LEN(28), .Type = DTYPE_String},
275
276 .UnicodeString = L"LUFA Mouse and Keyboard Demo"
277 };
278
279 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
280 * documentation) by the application code so that the address and size of a requested descriptor can be given
281 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
282 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
283 * USB host.
284 */
285 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
286 const uint8_t wIndex,
287 const void** const DescriptorAddress)
288 {
289 const uint8_t DescriptorType = (wValue >> 8);
290 const uint8_t DescriptorNumber = (wValue & 0xFF);
291
292 const void* Address = NULL;
293 uint16_t Size = NO_DESCRIPTOR;
294
295 switch (DescriptorType)
296 {
297 case DTYPE_Device:
298 Address = &DeviceDescriptor;
299 Size = sizeof(USB_Descriptor_Device_t);
300 break;
301 case DTYPE_Configuration:
302 Address = &ConfigurationDescriptor;
303 Size = sizeof(USB_Descriptor_Configuration_t);
304 break;
305 case DTYPE_String:
306 switch (DescriptorNumber)
307 {
308 case 0x00:
309 Address = &LanguageString;
310 Size = pgm_read_byte(&LanguageString.Header.Size);
311 break;
312 case 0x01:
313 Address = &ManufacturerString;
314 Size = pgm_read_byte(&ManufacturerString.Header.Size);
315 break;
316 case 0x02:
317 Address = &ProductString;
318 Size = pgm_read_byte(&ProductString.Header.Size);
319 break;
320 }
321
322 break;
323 case HID_DTYPE_HID:
324 if (!(wIndex))
325 {
326 Address = &ConfigurationDescriptor.HID1_KeyboardHID;
327 Size = sizeof(USB_HID_Descriptor_HID_t);
328 }
329 else
330 {
331 Address = &ConfigurationDescriptor.HID2_MouseHID;
332 Size = sizeof(USB_HID_Descriptor_HID_t);
333 }
334 break;
335 case HID_DTYPE_Report:
336 if (!(wIndex))
337 {
338 Address = &KeyboardReport;
339 Size = sizeof(KeyboardReport);
340 }
341 else
342 {
343 Address = &MouseReport;
344 Size = sizeof(MouseReport);
345 }
346
347 break;
348 }
349
350 *DescriptorAddress = Address;
351 return Size;
352 }
353