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