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