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