New HID report item macros (with HID_RI_ prefix) to allow for easy creation and editi...
[pub/lufa.git] / Demos / Device / ClassDriver / MassStorageKeyboard / 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 Copyright 2010 Matthias Hullin (lufa [at] matthias [dot] hullin [dot] net)
12
13 Permission to use, copy, modify, distribute, and sell this
14 software and its documentation for any purpose is hereby granted
15 without fee, provided that the above copyright notice appear in
16 all copies and that both that the copyright notice and this
17 permission notice and warranty disclaimer appear in supporting
18 documentation, and that the name of the author not be used in
19 advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
21
22 The author disclaim all warranties with regard to this
23 software, including all implied warranties of merchantability
24 and fitness. In no event shall the author be liable for any
25 special, indirect or consequential damages or any damages
26 whatsoever resulting from loss of use, data or profits, whether
27 in an action of contract, negligence or other tortious action,
28 arising out of or in connection with the use or performance of
29 this software.
30 */
31
32 /** \file
33 *
34 * USB Device Descriptors, for library use when in USB device mode. Descriptors are special
35 * computer-readable structures which the host requests upon device enumeration, to determine
36 * the device's capabilities and functions.
37 */
38
39 #include "Descriptors.h"
40
41 /* On some devices, there is a factory set internal serial number which can be automatically sent to the host as
42 * the device's serial number when the Device Descriptor's .SerialNumStrIndex entry is set to USE_INTERNAL_SERIAL.
43 * This allows the host to track a device across insertions on different ports, allowing them to retain allocated
44 * resources like COM port numbers and drivers. On demos using this feature, give a warning on unsupported devices
45 * so that the user can supply their own serial number descriptor instead or remove the USE_INTERNAL_SERIAL value
46 * from the Device Descriptor (forcing the host to generate a serial number for each device from the VID, PID and
47 * port location).
48 */
49 #if (USE_INTERNAL_SERIAL == NO_DESCRIPTOR)
50 #warning USE_INTERNAL_SERIAL is not available on this AVR - please manually construct a device serial descriptor.
51 #endif
52
53 /** HID class report descriptor. This is a special descriptor constructed with values from the
54 * USBIF HID class specification to describe the reports and capabilities of the HID device. This
55 * descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
56 * the device will send, and what it may be sent back from the host. Refer to the HID specification for
57 * more details on HID report descriptors.
58 */
59 USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =
60 {
61 HID_RI_USAGE_PAGE(8), 0x01, /* Generic Desktop */
62 HID_RI_USAGE(8), 0x06, /* Keyboard */
63 HID_RI_COLLECTION(8), 0x01, /* Application */
64 HID_RI_USAGE_PAGE(8), 0x07, /* Key Codes */
65 HID_RI_USAGE_MINIMUM(8), 0xE0, /* Keyboard Left Control */
66 HID_RI_USAGE_MAXIMUM(8), 0xE7, /* Keyboard Right GUI */
67 HID_RI_LOGICAL_MINIMUM(8), 0,
68 HID_RI_LOGICAL_MAXIMUM(8), 1,
69 HID_RI_REPORT_SIZE(8), 1,
70 HID_RI_REPORT_COUNT(8), 8,
71 HID_RI_INPUT(8), (HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
72
73 HID_RI_REPORT_COUNT(8), 1,
74 HID_RI_REPORT_SIZE(8), 8,
75 HID_RI_INPUT(8), HID_IOF_CONSTANT,
76
77 HID_RI_USAGE_PAGE(8), 0x08, /* LEDs */
78 HID_RI_USAGE_MINIMUM(8), 0x01, /* Num Lock */
79 HID_RI_USAGE_MAXIMUM(8), 0x05, /* Kana */
80 HID_RI_REPORT_COUNT(8), 5,
81 HID_RI_REPORT_SIZE(8), 1,
82 HID_RI_OUTPUT(8), (HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
83 HID_RI_REPORT_COUNT(8), 1,
84 HID_RI_REPORT_SIZE(8), 3,
85 HID_RI_OUTPUT(8), HID_IOF_CONSTANT,
86
87 HID_RI_LOGICAL_MINIMUM(8), 0x00,
88 HID_RI_LOGICAL_MAXIMUM(8), 0x65,
89 HID_RI_USAGE_PAGE(8), 0x07, /* Keyboard */
90 HID_RI_USAGE_MINIMUM(8), 0x00, /* Reserved (no event indicated) */
91 HID_RI_USAGE_MAXIMUM(8), 0x65, /* Keyboard Application */
92 HID_RI_REPORT_COUNT(8), 6,
93 HID_RI_REPORT_SIZE(8), 8,
94 HID_RI_INPUT(8), (HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
95 HID_RI_END_COLLECTION(0),
96 };
97
98 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
99 * device characteristics, including the supported USB version, control endpoint size and the
100 * number of device configurations. The descriptor is read out by the USB host when the enumeration
101 * process begins.
102 */
103 USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
104 {
105 .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
106
107 .USBSpecification = VERSION_BCD(01.10),
108 .Class = USB_CSCP_NoDeviceClass,
109 .SubClass = USB_CSCP_NoDeviceSubclass,
110 .Protocol = USB_CSCP_NoDeviceProtocol,
111
112 .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
113
114 .VendorID = 0x03EB,
115 .ProductID = 0x2061,
116 .ReleaseNumber = VERSION_BCD(00.01),
117
118 .ManufacturerStrIndex = 0x01,
119 .ProductStrIndex = 0x02,
120 .SerialNumStrIndex = USE_INTERNAL_SERIAL,
121
122 .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
123 };
124
125 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
126 * of the device in one of its supported configurations, including information about any device interfaces
127 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
128 * a configuration so that the host may correctly communicate with the USB device.
129 */
130 USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
131 {
132 .Config =
133 {
134 .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
135
136 .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
137 .TotalInterfaces = 2,
138
139 .ConfigurationNumber = 1,
140 .ConfigurationStrIndex = NO_DESCRIPTOR,
141
142 .ConfigAttributes = USB_CONFIG_ATTR_BUSPOWERED,
143
144 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
145 },
146
147 .MS_Interface =
148 {
149 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
150
151 .InterfaceNumber = 0,
152 .AlternateSetting = 0,
153
154 .TotalEndpoints = 2,
155
156 .Class = MS_CSCP_MassStorageClass,
157 .SubClass = MS_CSCP_SCSITransparentSubclass,
158 .Protocol = MS_CSCP_BulkOnlyTransportProtocol,
159
160 .InterfaceStrIndex = NO_DESCRIPTOR
161 },
162
163 .MS_DataInEndpoint =
164 {
165 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
166
167 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | MASS_STORAGE_IN_EPNUM),
168 .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
169 .EndpointSize = MASS_STORAGE_IO_EPSIZE,
170 .PollingIntervalMS = 0x01
171 },
172
173 .MS_DataOutEndpoint =
174 {
175 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
176
177 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_OUT | MASS_STORAGE_OUT_EPNUM),
178 .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
179 .EndpointSize = MASS_STORAGE_IO_EPSIZE,
180 .PollingIntervalMS = 0x01
181 },
182
183 .HID_KeyboardInterface =
184 {
185 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
186
187 .InterfaceNumber = 1,
188 .AlternateSetting = 0,
189
190 .TotalEndpoints = 1,
191
192 .Class = HID_CSCP_HIDClass,
193 .SubClass = HID_CSCP_BootSubclass,
194 .Protocol = HID_CSCP_KeyboardBootProtocol,
195
196 .InterfaceStrIndex = NO_DESCRIPTOR
197 },
198
199 .HID_KeyboardHID =
200 {
201 .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
202
203 .HIDSpec = VERSION_BCD(01.11),
204 .CountryCode = 0x00,
205 .TotalReportDescriptors = 1,
206 .HIDReportType = HID_DTYPE_Report,
207 .HIDReportLength = sizeof(KeyboardReport)
208 },
209
210 .HID_ReportINEndpoint =
211 {
212 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
213
214 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | KEYBOARD_EPNUM),
215 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
216 .EndpointSize = KEYBOARD_EPSIZE,
217 .PollingIntervalMS = 0x01
218 },
219 };
220
221 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
222 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
223 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
224 */
225 USB_Descriptor_String_t PROGMEM LanguageString =
226 {
227 .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
228
229 .UnicodeString = {LANGUAGE_ID_ENG}
230 };
231
232 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
233 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
234 * Descriptor.
235 */
236 USB_Descriptor_String_t PROGMEM ManufacturerString =
237 {
238 .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
239
240 .UnicodeString = L"Dean Camera"
241 };
242
243 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
244 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
245 * Descriptor.
246 */
247 USB_Descriptor_String_t PROGMEM ProductString =
248 {
249 .Header = {.Size = USB_STRING_LEN(35), .Type = DTYPE_String},
250
251 .UnicodeString = L"LUFA Mass Storage and Keyboard Demo"
252 };
253
254 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
255 * documentation) by the application code so that the address and size of a requested descriptor can be given
256 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
257 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
258 * USB host.
259 */
260 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
261 const uint8_t wIndex,
262 const void** const DescriptorAddress)
263 {
264 const uint8_t DescriptorType = (wValue >> 8);
265 const uint8_t DescriptorNumber = (wValue & 0xFF);
266
267 const void* Address = NULL;
268 uint16_t Size = NO_DESCRIPTOR;
269
270 switch (DescriptorType)
271 {
272 case DTYPE_Device:
273 Address = &DeviceDescriptor;
274 Size = sizeof(USB_Descriptor_Device_t);
275 break;
276 case DTYPE_Configuration:
277 Address = &ConfigurationDescriptor;
278 Size = sizeof(USB_Descriptor_Configuration_t);
279 break;
280 case DTYPE_String:
281 switch (DescriptorNumber)
282 {
283 case 0x00:
284 Address = &LanguageString;
285 Size = pgm_read_byte(&LanguageString.Header.Size);
286 break;
287 case 0x01:
288 Address = &ManufacturerString;
289 Size = pgm_read_byte(&ManufacturerString.Header.Size);
290 break;
291 case 0x02:
292 Address = &ProductString;
293 Size = pgm_read_byte(&ProductString.Header.Size);
294 break;
295 }
296
297 break;
298 case HID_DTYPE_HID:
299 Address = &ConfigurationDescriptor.HID_KeyboardHID;
300 Size = sizeof(USB_HID_Descriptor_HID_t);
301 break;
302 case HID_DTYPE_Report:
303 Address = &KeyboardReport;
304 Size = sizeof(KeyboardReport);
305 break;
306 }
307
308 *DescriptorAddress = Address;
309 return Size;
310 }
311