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