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