b779ea342c2a1f18098e2b6c66400b527fbd3910
[pub/USBasp.git] / Demos / Device / ClassDriver / MassStorageKeyboard / 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 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 #if (USE_INTERNAL_SERIAL == NO_DESCRIPTOR)
33 #warning USE_INTERNAL_SERIAL is not available on this AVR - please manually construct a device serial descriptor.
34 #endif
35
36 /** \file
37 *
38 * USB Device Descriptors, for library use when in USB device mode. Descriptors are special
39 * computer-readable structures which the host requests upon device enumeration, to determine
40 * the device's capabilities and functions.
41 */
42
43 #include "Descriptors.h"
44
45 /** HID class report descriptor. This is a special descriptor constructed with values from the
46 * USBIF HID class specification to describe the reports and capabilities of the HID device. This
47 * descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
48 * the device will send, and what it may be sent back from the host. Refer to the HID specification for
49 * more details on HID report descriptors.
50 */
51 USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =
52 {
53 0x05, 0x01, /* Usage Page (Generic Desktop) */
54 0x09, 0x06, /* Usage (Keyboard) */
55 0xa1, 0x01, /* Collection (Application) */
56 0x75, 0x01, /* Report Size (1) */
57 0x95, 0x08, /* Report Count (8) */
58 0x05, 0x07, /* Usage Page (Key Codes) */
59 0x19, 0xe0, /* Usage Minimum (Keyboard LeftControl) */
60 0x29, 0xe7, /* Usage Maximum (Keyboard Right GUI) */
61 0x15, 0x00, /* Logical Minimum (0) */
62 0x25, 0x01, /* Logical Maximum (1) */
63 0x81, 0x02, /* Input (Data, Variable, Absolute) */
64 0x95, 0x01, /* Report Count (1) */
65 0x75, 0x08, /* Report Size (8) */
66 0x81, 0x03, /* Input (Const, Variable, Absolute) */
67 0x95, 0x05, /* Report Count (5) */
68 0x75, 0x01, /* Report Size (1) */
69 0x05, 0x08, /* Usage Page (LEDs) */
70 0x19, 0x01, /* Usage Minimum (Num Lock) */
71 0x29, 0x05, /* Usage Maximum (Kana) */
72 0x91, 0x02, /* Output (Data, Variable, Absolute) */
73 0x95, 0x01, /* Report Count (1) */
74 0x75, 0x03, /* Report Size (3) */
75 0x91, 0x03, /* Output (Const, Variable, Absolute) */
76 0x95, 0x06, /* Report Count (6) */
77 0x75, 0x08, /* Report Size (8) */
78 0x15, 0x00, /* Logical Minimum (0) */
79 0x25, 0x65, /* Logical Maximum (101) */
80 0x05, 0x07, /* Usage Page (Keyboard) */
81 0x19, 0x00, /* Usage Minimum (Reserved (no event indicated)) */
82 0x29, 0x65, /* Usage Maximum (Keyboard Application) */
83 0x81, 0x00, /* Input (Data, Array, Absolute) */
84 0xc0 /* End Collection */
85 };
86
87 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
88 * device characteristics, including the supported USB version, control endpoint size and the
89 * number of device configurations. The descriptor is read out by the USB host when the enumeration
90 * process begins.
91 */
92 USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
93 {
94 .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
95
96 .USBSpecification = VERSION_BCD(01.10),
97 .Class = 0x00,
98 .SubClass = 0x00,
99 .Protocol = 0x00,
100
101 .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
102
103 .VendorID = 0x03EB,
104 .ProductID = 0x2061,
105 .ReleaseNumber = 0x0000,
106
107 .ManufacturerStrIndex = 0x01,
108 .ProductStrIndex = 0x02,
109 .SerialNumStrIndex = USE_INTERNAL_SERIAL,
110
111 .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
112 };
113
114 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
115 * of the device in one of its supported configurations, including information about any device interfaces
116 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
117 * a configuration so that the host may correctly communicate with the USB device.
118 */
119 USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
120 {
121 .Config =
122 {
123 .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
124
125 .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
126 .TotalInterfaces = 2,
127
128 .ConfigurationNumber = 1,
129 .ConfigurationStrIndex = NO_DESCRIPTOR,
130
131 .ConfigAttributes = USB_CONFIG_ATTR_BUSPOWERED,
132
133 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
134 },
135
136 .MassStorageInterface =
137 {
138 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
139
140 .InterfaceNumber = 0,
141 .AlternateSetting = 0,
142
143 .TotalEndpoints = 2,
144
145 .Class = 0x08,
146 .SubClass = 0x06,
147 .Protocol = 0x50,
148
149 .InterfaceStrIndex = NO_DESCRIPTOR
150 },
151
152 .MassStorageDataInEndpoint =
153 {
154 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
155
156 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | MASS_STORAGE_IN_EPNUM),
157 .Attributes = EP_TYPE_BULK,
158 .EndpointSize = MASS_STORAGE_IO_EPSIZE,
159 .PollingIntervalMS = 0x00
160 },
161
162 .MassStorageDataOutEndpoint =
163 {
164 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
165
166 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_OUT | MASS_STORAGE_OUT_EPNUM),
167 .Attributes = EP_TYPE_BULK,
168 .EndpointSize = MASS_STORAGE_IO_EPSIZE,
169 .PollingIntervalMS = 0x00
170 },
171
172 .KeyboardInterface =
173 {
174 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
175
176 .InterfaceNumber = 0x01,
177 .AlternateSetting = 0x00,
178
179 .TotalEndpoints = 1,
180
181 .Class = 0x03,
182 .SubClass = 0x01,
183 .Protocol = 0x01,
184
185 .InterfaceStrIndex = NO_DESCRIPTOR
186 },
187
188 .KeyboardHID =
189 {
190 .Header = {.Size = sizeof(USB_HID_Descriptor_t), .Type = DTYPE_HID},
191
192 .HIDSpec = VERSION_BCD(01.11),
193 .CountryCode = 0x00,
194 .TotalReportDescriptors = 1,
195 .HIDReportType = DTYPE_Report,
196 .HIDReportLength = sizeof(KeyboardReport)
197 },
198
199 .KeyboardEndpoint =
200 {
201 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
202
203 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | KEYBOARD_EPNUM),
204 .Attributes = EP_TYPE_INTERRUPT,
205 .EndpointSize = KEYBOARD_EPSIZE,
206 .PollingIntervalMS = 0x04
207 },
208 };
209
210 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
211 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
212 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
213 */
214 USB_Descriptor_String_t PROGMEM LanguageString =
215 {
216 .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
217
218 .UnicodeString = {LANGUAGE_ID_ENG}
219 };
220
221 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
222 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
223 * Descriptor.
224 */
225 USB_Descriptor_String_t PROGMEM ManufacturerString =
226 {
227 .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
228
229 .UnicodeString = L"Dean Camera"
230 };
231
232 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
233 * 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 ProductString =
237 {
238 .Header = {.Size = USB_STRING_LEN(35), .Type = DTYPE_String},
239
240 .UnicodeString = L"LUFA Mass Storage and Keyboard Demo"
241 };
242
243 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
244 * documentation) by the application code so that the address and size of a requested descriptor can be given
245 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
246 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
247 * USB host.
248 */
249 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress)
250 {
251 const uint8_t DescriptorType = (wValue >> 8);
252 const uint8_t DescriptorNumber = (wValue & 0xFF);
253
254 void* Address = NULL;
255 uint16_t Size = NO_DESCRIPTOR;
256
257 switch (DescriptorType)
258 {
259 case DTYPE_Device:
260 Address = (void*)&DeviceDescriptor;
261 Size = sizeof(USB_Descriptor_Device_t);
262 break;
263 case DTYPE_Configuration:
264 Address = (void*)&ConfigurationDescriptor;
265 Size = sizeof(USB_Descriptor_Configuration_t);
266 break;
267 case DTYPE_String:
268 switch (DescriptorNumber)
269 {
270 case 0x00:
271 Address = (void*)&LanguageString;
272 Size = pgm_read_byte(&LanguageString.Header.Size);
273 break;
274 case 0x01:
275 Address = (void*)&ManufacturerString;
276 Size = pgm_read_byte(&ManufacturerString.Header.Size);
277 break;
278 case 0x02:
279 Address = (void*)&ProductString;
280 Size = pgm_read_byte(&ProductString.Header.Size);
281 break;
282 }
283
284 break;
285 case DTYPE_HID:
286 Address = (void*)&ConfigurationDescriptor.KeyboardHID;
287 Size = sizeof(USB_HID_Descriptor_t);
288 break;
289 case DTYPE_Report:
290 Address = (void*)&KeyboardReport;
291 Size = sizeof(KeyboardReport);
292 break;
293 }
294
295 *DescriptorAddress = Address;
296 return Size;
297 }