Abuse the the C preprocessor so that the HID macros can automatically encode data...
[pub/lufa.git] / Demos / Device / ClassDriver / VirtualSerialMouse / 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 /* On some devices, there is a factory set internal serial number which can be automatically sent to the host as
41 * the device's serial number when the Device Descriptor's .SerialNumStrIndex entry is set to USE_INTERNAL_SERIAL.
42 * This allows the host to track a device across insertions on different ports, allowing them to retain allocated
43 * resources like COM port numbers and drivers. On demos using this feature, give a warning on unsupported devices
44 * so that the user can supply their own serial number descriptor instead or remove the USE_INTERNAL_SERIAL value
45 * from the Device Descriptor (forcing the host to generate a serial number for each device from the VID, PID and
46 * port location).
47 */
48 #if (USE_INTERNAL_SERIAL == NO_DESCRIPTOR)
49 #warning USE_INTERNAL_SERIAL is not available on this AVR - please manually construct a device serial descriptor.
50 #endif
51
52 /** HID class report descriptor. This is a special descriptor constructed with values from the
53 * USBIF HID class specification to describe the reports and capabilities of the HID device. This
54 * descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
55 * the device will send, and what it may be sent back from the host. Refer to the HID specification for
56 * more details on HID report descriptors.
57 */
58 USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =
59 {
60 HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
61 HID_RI_USAGE(8, 0x02), /* Mouse */
62 HID_RI_COLLECTION(8, 0x01), /* Application */
63 HID_RI_USAGE(8, 0x01), /* Pointer */
64 HID_RI_COLLECTION(8, 0x00), /* Physical */
65 HID_RI_USAGE_PAGE(8, 0x09), /* Button */
66 HID_RI_USAGE_MINIMUM(8, 0x01),
67 HID_RI_USAGE_MAXIMUM(8, 0x03),
68 HID_RI_LOGICAL_MINIMUM(8, 0x00),
69 HID_RI_LOGICAL_MAXIMUM(8, 0x01),
70 HID_RI_REPORT_COUNT(8, 0x03),
71 HID_RI_REPORT_SIZE(8, 0x01),
72 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_VOLATILE),
73 HID_RI_REPORT_COUNT(8, 0x01),
74 HID_RI_REPORT_SIZE(8, 0x05),
75 HID_RI_INPUT(8, HID_IOF_CONSTANT),
76 HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
77 HID_RI_USAGE(8, 0x30), /* Usage X */
78 HID_RI_USAGE(8, 0x31), /* Usage Y */
79 HID_RI_LOGICAL_MINIMUM(8, -1),
80 HID_RI_LOGICAL_MAXIMUM(8, 1),
81 HID_RI_PHYSICAL_MINIMUM(8, -1),
82 HID_RI_PHYSICAL_MAXIMUM(8, 1),
83 HID_RI_REPORT_COUNT(8, 0x02),
84 HID_RI_REPORT_SIZE(8, 0x08),
85 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE | HID_IOF_NON_VOLATILE),
86 HID_RI_END_COLLECTION(0),
87 HID_RI_END_COLLECTION(0),
88 };
89
90 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
91 * device characteristics, including the supported USB version, control endpoint size and the
92 * number of device configurations. The descriptor is read out by the USB host when the enumeration
93 * process begins.
94 */
95 USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
96 {
97 .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
98
99 .USBSpecification = VERSION_BCD(01.10),
100 .Class = USB_CSCP_IADDeviceClass,
101 .SubClass = USB_CSCP_IADDeviceSubclass,
102 .Protocol = USB_CSCP_IADDeviceProtocol,
103
104 .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
105
106 .VendorID = 0x03EB,
107 .ProductID = 0x2062,
108 .ReleaseNumber = VERSION_BCD(00.01),
109
110 .ManufacturerStrIndex = 0x01,
111 .ProductStrIndex = 0x02,
112 .SerialNumStrIndex = USE_INTERNAL_SERIAL,
113
114 .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
115 };
116
117 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
118 * of the device in one of its supported configurations, including information about any device interfaces
119 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
120 * a configuration so that the host may correctly communicate with the USB device.
121 */
122 USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
123 {
124 .Config =
125 {
126 .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
127
128 .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
129 .TotalInterfaces = 3,
130
131 .ConfigurationNumber = 1,
132 .ConfigurationStrIndex = NO_DESCRIPTOR,
133
134 .ConfigAttributes = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELFPOWERED),
135
136 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
137 },
138
139 .CDC_IAD =
140 {
141 .Header = {.Size = sizeof(USB_Descriptor_Interface_Association_t), .Type = DTYPE_InterfaceAssociation},
142
143 .FirstInterfaceIndex = 0,
144 .TotalInterfaces = 2,
145
146 .Class = CDC_CSCP_CDCClass,
147 .SubClass = CDC_CSCP_ACMSubclass,
148 .Protocol = CDC_CSCP_ATCommandProtocol,
149
150 .IADStrIndex = NO_DESCRIPTOR
151 },
152
153 .CDC_CCI_Interface =
154 {
155 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
156
157 .InterfaceNumber = 0,
158 .AlternateSetting = 0,
159
160 .TotalEndpoints = 1,
161
162 .Class = CDC_CSCP_CDCClass,
163 .SubClass = CDC_CSCP_ACMSubclass,
164 .Protocol = CDC_CSCP_ATCommandProtocol,
165
166 .InterfaceStrIndex = NO_DESCRIPTOR
167 },
168
169 .CDC_Functional_Header =
170 {
171 .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = DTYPE_CSInterface},
172 .Subtype = CDC_DSUBTYPE_CSInterface_Header,
173
174 .CDCSpecification = VERSION_BCD(01.10),
175 },
176
177 .CDC_Functional_ACM =
178 {
179 .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = DTYPE_CSInterface},
180 .Subtype = CDC_DSUBTYPE_CSInterface_ACM,
181
182 .Capabilities = 0x06,
183 },
184
185 .CDC_Functional_Union =
186 {
187 .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = DTYPE_CSInterface},
188 .Subtype = CDC_DSUBTYPE_CSInterface_Union,
189
190 .MasterInterfaceNumber = 0,
191 .SlaveInterfaceNumber = 1,
192 },
193
194 .CDC_NotificationEndpoint =
195 {
196 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
197
198 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | CDC_NOTIFICATION_EPNUM),
199 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
200 .EndpointSize = CDC_NOTIFICATION_EPSIZE,
201 .PollingIntervalMS = 0xFF
202 },
203
204 .CDC_DCI_Interface =
205 {
206 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
207
208 .InterfaceNumber = 1,
209 .AlternateSetting = 0,
210
211 .TotalEndpoints = 2,
212
213 .Class = CDC_CSCP_CDCDataClass,
214 .SubClass = CDC_CSCP_NoDataSubclass,
215 .Protocol = CDC_CSCP_NoDataProtocol,
216
217 .InterfaceStrIndex = NO_DESCRIPTOR
218 },
219
220 .CDC_DataOutEndpoint =
221 {
222 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
223
224 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_OUT | CDC_RX_EPNUM),
225 .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
226 .EndpointSize = CDC_TXRX_EPSIZE,
227 .PollingIntervalMS = 0x01
228 },
229
230 .CDC_DataInEndpoint =
231 {
232 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
233
234 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | CDC_TX_EPNUM),
235 .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
236 .EndpointSize = CDC_TXRX_EPSIZE,
237 .PollingIntervalMS = 0x01
238 },
239
240 .HID_Interface =
241 {
242 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
243
244 .InterfaceNumber = 2,
245 .AlternateSetting = 0,
246
247 .TotalEndpoints = 1,
248
249 .Class = HID_CSCP_HIDClass,
250 .SubClass = HID_CSCP_BootSubclass,
251 .Protocol = HID_CSCP_MouseBootProtocol,
252
253 .InterfaceStrIndex = NO_DESCRIPTOR
254 },
255
256 .HID_MouseHID =
257 {
258 .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
259
260 .HIDSpec = VERSION_BCD(01.11),
261 .CountryCode = 0x00,
262 .TotalReportDescriptors = 1,
263 .HIDReportType = HID_DTYPE_Report,
264 .HIDReportLength = sizeof(MouseReport)
265 },
266
267 .HID_ReportINEndpoint =
268 {
269 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
270
271 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | MOUSE_EPNUM),
272 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
273 .EndpointSize = MOUSE_EPSIZE,
274 .PollingIntervalMS = 0x01
275 }
276 };
277
278 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
279 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
280 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
281 */
282 USB_Descriptor_String_t PROGMEM LanguageString =
283 {
284 .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
285
286 .UnicodeString = {LANGUAGE_ID_ENG}
287 };
288
289 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
290 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
291 * Descriptor.
292 */
293 USB_Descriptor_String_t PROGMEM ManufacturerString =
294 {
295 .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
296
297 .UnicodeString = L"Dean Camera"
298 };
299
300 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
301 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
302 * Descriptor.
303 */
304 USB_Descriptor_String_t PROGMEM ProductString =
305 {
306 .Header = {.Size = USB_STRING_LEN(23), .Type = DTYPE_String},
307
308 .UnicodeString = L"LUFA CDC and Mouse Demo"
309 };
310
311 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
312 * documentation) by the application code so that the address and size of a requested descriptor can be given
313 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
314 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
315 * USB host.
316 */
317 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
318 const uint8_t wIndex,
319 const void** const DescriptorAddress)
320 {
321 const uint8_t DescriptorType = (wValue >> 8);
322 const uint8_t DescriptorNumber = (wValue & 0xFF);
323
324 const void* Address = NULL;
325 uint16_t Size = NO_DESCRIPTOR;
326
327 switch (DescriptorType)
328 {
329 case DTYPE_Device:
330 Address = &DeviceDescriptor;
331 Size = sizeof(USB_Descriptor_Device_t);
332 break;
333 case DTYPE_Configuration:
334 Address = &ConfigurationDescriptor;
335 Size = sizeof(USB_Descriptor_Configuration_t);
336 break;
337 case DTYPE_String:
338 switch (DescriptorNumber)
339 {
340 case 0x00:
341 Address = &LanguageString;
342 Size = pgm_read_byte(&LanguageString.Header.Size);
343 break;
344 case 0x01:
345 Address = &ManufacturerString;
346 Size = pgm_read_byte(&ManufacturerString.Header.Size);
347 break;
348 case 0x02:
349 Address = &ProductString;
350 Size = pgm_read_byte(&ProductString.Header.Size);
351 break;
352 }
353
354 break;
355 case HID_DTYPE_HID:
356 Address = &ConfigurationDescriptor.HID_MouseHID;
357 Size = sizeof(USB_HID_Descriptor_HID_t);
358 break;
359 case HID_DTYPE_Report:
360 Address = &MouseReport;
361 Size = sizeof(MouseReport);
362 break;
363 }
364
365 *DescriptorAddress = Address;
366 return Size;
367 }
368