]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - keyboard/lufa/Descriptors.c
initial attempt for LUFA.
[max/tmk_keyboard.git] / keyboard / lufa / Descriptors.c
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11   Copyright 2010  Denver Gingerich (denver [at] ossguy [dot] com)\r
12 \r
13   Permission to use, copy, modify, distribute, and sell this\r
14   software and its documentation for any purpose is hereby granted\r
15   without fee, provided that the above copyright notice appear in\r
16   all copies and that both that the copyright notice and this\r
17   permission notice and warranty disclaimer appear in supporting\r
18   documentation, and that the name of the author not be used in\r
19   advertising or publicity pertaining to distribution of the\r
20   software without specific, written prior permission.\r
21 \r
22   The author disclaim all warranties with regard to this\r
23   software, including all implied warranties of merchantability\r
24   and fitness.  In no event shall the author be liable for any\r
25   special, indirect or consequential damages or any damages\r
26   whatsoever resulting from loss of use, data or profits, whether\r
27   in an action of contract, negligence or other tortious action,\r
28   arising out of or in connection with the use or performance of\r
29   this software.\r
30 */\r
31 \r
32 /** \file\r
33  *\r
34  *  USB Device Descriptors, for library use when in USB device mode. Descriptors are special\r
35  *  computer-readable structures which the host requests upon device enumeration, to determine\r
36  *  the device's capabilities and functions.\r
37  */\r
38 \r
39 #include "Descriptors.h"\r
40 \r
41 /** HID class report descriptor. This is a special descriptor constructed with values from the\r
42  *  USBIF HID class specification to describe the reports and capabilities of the HID device. This\r
43  *  descriptor is parsed by the host and its contents used to determine what data (and in what encoding)\r
44  *  the device will send, and what it may be sent back from the host. Refer to the HID specification for\r
45  *  more details on HID report descriptors.\r
46  *\r
47  *  This descriptor describes the mouse HID interface's report structure.\r
48  */\r
49 const USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =\r
50 {\r
51         HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */\r
52         HID_RI_USAGE(8, 0x02), /* Mouse */\r
53         HID_RI_COLLECTION(8, 0x01), /* Application */\r
54             HID_RI_USAGE(8, 0x01), /* Pointer */\r
55             HID_RI_COLLECTION(8, 0x00), /* Physical */\r
56                 HID_RI_USAGE_PAGE(8, 0x09), /* Button */\r
57                 HID_RI_USAGE_MINIMUM(8, 0x01),\r
58                 HID_RI_USAGE_MAXIMUM(8, 0x03),\r
59                 HID_RI_LOGICAL_MINIMUM(8, 0x00),\r
60                 HID_RI_LOGICAL_MAXIMUM(8, 0x01),\r
61                 HID_RI_REPORT_COUNT(8, 0x03),\r
62                 HID_RI_REPORT_SIZE(8, 0x01),\r
63                 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),\r
64                 HID_RI_REPORT_COUNT(8, 0x01),\r
65                 HID_RI_REPORT_SIZE(8, 0x05),\r
66                 HID_RI_INPUT(8, HID_IOF_CONSTANT),\r
67                 HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */\r
68                 HID_RI_USAGE(8, 0x30), /* Usage X */\r
69                 HID_RI_USAGE(8, 0x31), /* Usage Y */\r
70                 HID_RI_LOGICAL_MINIMUM(8, -1),\r
71                 HID_RI_LOGICAL_MAXIMUM(8, 1),\r
72                 HID_RI_PHYSICAL_MINIMUM(8, -1),\r
73                 HID_RI_PHYSICAL_MAXIMUM(8, 1),\r
74                 HID_RI_REPORT_COUNT(8, 0x02),\r
75                 HID_RI_REPORT_SIZE(8, 0x08),\r
76                 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),\r
77             HID_RI_END_COLLECTION(0),\r
78         HID_RI_END_COLLECTION(0),\r
79 };\r
80 \r
81 /** Same as the MouseReport structure, but defines the keyboard HID interface's report structure. */\r
82 const USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =\r
83 {\r
84         HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */\r
85         HID_RI_USAGE(8, 0x06), /* Keyboard */\r
86         HID_RI_COLLECTION(8, 0x01), /* Application */\r
87             HID_RI_USAGE_PAGE(8, 0x07), /* Key Codes */\r
88             HID_RI_USAGE_MINIMUM(8, 0xE0), /* Keyboard Left Control */\r
89             HID_RI_USAGE_MAXIMUM(8, 0xE7), /* Keyboard Right GUI */\r
90             HID_RI_LOGICAL_MINIMUM(8, 0x00),\r
91             HID_RI_LOGICAL_MAXIMUM(8, 0x01),\r
92             HID_RI_REPORT_SIZE(8, 0x01),\r
93             HID_RI_REPORT_COUNT(8, 0x08),\r
94             HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),\r
95             HID_RI_REPORT_COUNT(8, 0x01),\r
96             HID_RI_REPORT_SIZE(8, 0x08),\r
97             HID_RI_INPUT(8, HID_IOF_CONSTANT),\r
98             HID_RI_USAGE_PAGE(8, 0x08), /* LEDs */\r
99             HID_RI_USAGE_MINIMUM(8, 0x01), /* Num Lock */\r
100             HID_RI_USAGE_MAXIMUM(8, 0x05), /* Kana */\r
101             HID_RI_REPORT_COUNT(8, 0x05),\r
102             HID_RI_REPORT_SIZE(8, 0x01),\r
103             HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),\r
104             HID_RI_REPORT_COUNT(8, 0x01),\r
105             HID_RI_REPORT_SIZE(8, 0x03),\r
106             HID_RI_OUTPUT(8, HID_IOF_CONSTANT),\r
107             HID_RI_LOGICAL_MINIMUM(8, 0x00),\r
108             HID_RI_LOGICAL_MAXIMUM(8, 0x65),\r
109             HID_RI_USAGE_PAGE(8, 0x07), /* Keyboard */\r
110             HID_RI_USAGE_MINIMUM(8, 0x00), /* Reserved (no event indicated) */\r
111             HID_RI_USAGE_MAXIMUM(8, 0x65), /* Keyboard Application */\r
112             HID_RI_REPORT_COUNT(8, 0x06),\r
113             HID_RI_REPORT_SIZE(8, 0x08),\r
114             HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),\r
115         HID_RI_END_COLLECTION(0),\r
116 };\r
117 \r
118 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall\r
119  *  device characteristics, including the supported USB version, control endpoint size and the\r
120  *  number of device configurations. The descriptor is read out by the USB host when the enumeration\r
121  *  process begins.\r
122  */\r
123 const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =\r
124 {\r
125         .Header                 = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},\r
126 \r
127         .USBSpecification       = VERSION_BCD(01.10),\r
128         .Class                  = USB_CSCP_NoDeviceClass,\r
129         .SubClass               = USB_CSCP_NoDeviceSubclass,\r
130         .Protocol               = USB_CSCP_NoDeviceProtocol,\r
131 \r
132         .Endpoint0Size          = FIXED_CONTROL_ENDPOINT_SIZE,\r
133 \r
134         .VendorID               = 0x03EB,\r
135         .ProductID              = 0x204D,\r
136         .ReleaseNumber          = VERSION_BCD(00.01),\r
137 \r
138         .ManufacturerStrIndex   = 0x01,\r
139         .ProductStrIndex        = 0x02,\r
140         .SerialNumStrIndex      = NO_DESCRIPTOR,\r
141 \r
142         .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS\r
143 };\r
144 \r
145 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage\r
146  *  of the device in one of its supported configurations, including information about any device interfaces\r
147  *  and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting\r
148  *  a configuration so that the host may correctly communicate with the USB device.\r
149  */\r
150 const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =\r
151 {\r
152         .Config =\r
153                 {\r
154                         .Header                 = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},\r
155 \r
156                         .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),\r
157                         .TotalInterfaces        = 2,\r
158 \r
159                         .ConfigurationNumber    = 1,\r
160                         .ConfigurationStrIndex  = NO_DESCRIPTOR,\r
161 \r
162                         .ConfigAttributes       = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),\r
163 \r
164                         .MaxPowerConsumption    = USB_CONFIG_POWER_MA(100)\r
165                 },\r
166 \r
167         .HID1_KeyboardInterface =\r
168                 {\r
169                         .Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},\r
170 \r
171                         .InterfaceNumber        = 0x00,\r
172                         .AlternateSetting       = 0x00,\r
173 \r
174                         .TotalEndpoints         = 2,\r
175 \r
176                         .Class                  = HID_CSCP_HIDClass,\r
177                         .SubClass               = HID_CSCP_BootSubclass,\r
178                         .Protocol               = HID_CSCP_KeyboardBootProtocol,\r
179 \r
180                         .InterfaceStrIndex      = NO_DESCRIPTOR\r
181                 },\r
182 \r
183         .HID1_KeyboardHID =\r
184                 {\r
185                         .Header                 = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},\r
186 \r
187                         .HIDSpec                = VERSION_BCD(01.11),\r
188                         .CountryCode            = 0x00,\r
189                         .TotalReportDescriptors = 1,\r
190                         .HIDReportType          = HID_DTYPE_Report,\r
191                         .HIDReportLength        = sizeof(KeyboardReport)\r
192                 },\r
193 \r
194         .HID1_ReportINEndpoint =\r
195                 {\r
196                         .Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},\r
197 \r
198                         .EndpointAddress        = (ENDPOINT_DIR_IN | KEYBOARD_IN_EPNUM),\r
199                         .Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),\r
200                         .EndpointSize           = HID_EPSIZE,\r
201                         .PollingIntervalMS      = 0x01\r
202                 },\r
203 \r
204         .HID1_ReportOUTEndpoint =\r
205                 {\r
206                         .Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},\r
207 \r
208                         .EndpointAddress        = (ENDPOINT_DIR_OUT | KEYBOARD_OUT_EPNUM),\r
209                         .Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),\r
210                         .EndpointSize           = HID_EPSIZE,\r
211                         .PollingIntervalMS      = 0x01\r
212                 },\r
213 \r
214         .HID2_MouseInterface =\r
215                 {\r
216                         .Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},\r
217 \r
218                         .InterfaceNumber        = 0x01,\r
219                         .AlternateSetting       = 0x00,\r
220 \r
221                         .TotalEndpoints         = 1,\r
222 \r
223                         .Class                  = HID_CSCP_HIDClass,\r
224                         .SubClass               = HID_CSCP_BootSubclass,\r
225                         .Protocol               = HID_CSCP_MouseBootProtocol,\r
226 \r
227                         .InterfaceStrIndex      = NO_DESCRIPTOR\r
228                 },\r
229 \r
230         .HID2_MouseHID =\r
231                 {\r
232                         .Header                 = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},\r
233 \r
234                         .HIDSpec                = VERSION_BCD(01.11),\r
235                         .CountryCode            = 0x00,\r
236                         .TotalReportDescriptors = 1,\r
237                         .HIDReportType          = HID_DTYPE_Report,\r
238                         .HIDReportLength        = sizeof(MouseReport)\r
239                 },\r
240 \r
241         .HID2_ReportINEndpoint =\r
242                 {\r
243                         .Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},\r
244 \r
245                         .EndpointAddress        = (ENDPOINT_DIR_IN | MOUSE_IN_EPNUM),\r
246                         .Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),\r
247                         .EndpointSize           = HID_EPSIZE,\r
248                         .PollingIntervalMS      = 0x01\r
249                 }\r
250 };\r
251 \r
252 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests\r
253  *  the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate\r
254  *  via the language ID table available at USB.org what languages the device supports for its string descriptors.\r
255  */\r
256 const USB_Descriptor_String_t PROGMEM LanguageString =\r
257 {\r
258         .Header                 = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},\r
259 \r
260         .UnicodeString          = {LANGUAGE_ID_ENG}\r
261 };\r
262 \r
263 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable\r
264  *  form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device\r
265  *  Descriptor.\r
266  */\r
267 const USB_Descriptor_String_t PROGMEM ManufacturerString =\r
268 {\r
269         .Header                 = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},\r
270 \r
271         .UnicodeString          = L"Dean Camera"\r
272 };\r
273 \r
274 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,\r
275  *  and is read out upon request by the host when the appropriate string ID is requested, listed in the Device\r
276  *  Descriptor.\r
277  */\r
278 const USB_Descriptor_String_t PROGMEM ProductString =\r
279 {\r
280         .Header                 = {.Size = USB_STRING_LEN(28), .Type = DTYPE_String},\r
281 \r
282         .UnicodeString          = L"LUFA Mouse and Keyboard Demo"\r
283 };\r
284 \r
285 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"\r
286  *  documentation) by the application code so that the address and size of a requested descriptor can be given\r
287  *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function\r
288  *  is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the\r
289  *  USB host.\r
290  */\r
291 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,\r
292                                     const uint8_t wIndex,\r
293                                     const void** const DescriptorAddress)\r
294 {\r
295         const uint8_t  DescriptorType   = (wValue >> 8);\r
296         const uint8_t  DescriptorNumber = (wValue & 0xFF);\r
297 \r
298         const void* Address = NULL;\r
299         uint16_t    Size    = NO_DESCRIPTOR;\r
300 \r
301         switch (DescriptorType)\r
302         {\r
303                 case DTYPE_Device:\r
304                         Address = &DeviceDescriptor;\r
305                         Size    = sizeof(USB_Descriptor_Device_t);\r
306                         break;\r
307                 case DTYPE_Configuration:\r
308                         Address = &ConfigurationDescriptor;\r
309                         Size    = sizeof(USB_Descriptor_Configuration_t);\r
310                         break;\r
311                 case DTYPE_String:\r
312                         switch (DescriptorNumber)\r
313                         {\r
314                                 case 0x00:\r
315                                         Address = &LanguageString;\r
316                                         Size    = pgm_read_byte(&LanguageString.Header.Size);\r
317                                         break;\r
318                                 case 0x01:\r
319                                         Address = &ManufacturerString;\r
320                                         Size    = pgm_read_byte(&ManufacturerString.Header.Size);\r
321                                         break;\r
322                                 case 0x02:\r
323                                         Address = &ProductString;\r
324                                         Size    = pgm_read_byte(&ProductString.Header.Size);\r
325                                         break;\r
326                         }\r
327 \r
328                         break;\r
329                 case HID_DTYPE_HID:\r
330                         if (!(wIndex))\r
331                         {\r
332                                 Address = &ConfigurationDescriptor.HID1_KeyboardHID;\r
333                                 Size    = sizeof(USB_HID_Descriptor_HID_t);\r
334                         }\r
335                         else\r
336                         {\r
337                                 Address = &ConfigurationDescriptor.HID2_MouseHID;\r
338                                 Size    = sizeof(USB_HID_Descriptor_HID_t);\r
339                         }\r
340                         break;\r
341                 case HID_DTYPE_Report:\r
342                         if (!(wIndex))\r
343                         {\r
344                                 Address = &KeyboardReport;\r
345                                 Size    = sizeof(KeyboardReport);\r
346                         }\r
347                         else\r
348                         {\r
349                                 Address = &MouseReport;\r
350                                 Size    = sizeof(MouseReport);\r
351                         }\r
352 \r
353                         break;\r
354         }\r
355 \r
356         *DescriptorAddress = Address;\r
357         return Size;\r
358 }\r
359 \r