]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/KeyboardMouse/Descriptors.c
Merge commit '22b6e15a179031afb7c3534cf7b109b0668b602c'
[max/tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Device / ClassDriver / KeyboardMouse / Descriptors.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  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 disclaims 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  *  This descriptor describes the mouse HID interface's report structure.
47  */
48 const USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =
49 {
50         /* Use the HID class driver's standard Mouse report.
51          *   Min X/Y Axis values: -1
52          *   Max X/Y Axis values:  1
53          *   Min physical X/Y Axis values (used to determine resolution): -1
54          *   Max physical X/Y Axis values (used to determine resolution):  1
55          *   Buttons: 3
56          *   Absolute screen coordinates: false
57          */
58         HID_DESCRIPTOR_MOUSE(-1, 1, -1, 1, 3, false)
59 };
60
61 /** Same as the MouseReport structure, but defines the keyboard HID interface's report structure. */
62 const USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =
63 {
64         /* Use the HID class driver's standard Keyboard report.
65          *   Max simultaneous keys: 6
66          */
67         HID_DESCRIPTOR_KEYBOARD(6)
68 };
69
70 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
71  *  device characteristics, including the supported USB version, control endpoint size and the
72  *  number of device configurations. The descriptor is read out by the USB host when the enumeration
73  *  process begins.
74  */
75 const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
76 {
77         .Header                 = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
78
79         .USBSpecification       = VERSION_BCD(1,1,0),
80         .Class                  = USB_CSCP_NoDeviceClass,
81         .SubClass               = USB_CSCP_NoDeviceSubclass,
82         .Protocol               = USB_CSCP_NoDeviceProtocol,
83
84         .Endpoint0Size          = FIXED_CONTROL_ENDPOINT_SIZE,
85
86         .VendorID               = 0x03EB,
87         .ProductID              = 0x204D,
88         .ReleaseNumber          = VERSION_BCD(0,0,1),
89
90         .ManufacturerStrIndex   = STRING_ID_Manufacturer,
91         .ProductStrIndex        = STRING_ID_Product,
92         .SerialNumStrIndex      = NO_DESCRIPTOR,
93
94         .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
95 };
96
97 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
98  *  of the device in one of its supported configurations, including information about any device interfaces
99  *  and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
100  *  a configuration so that the host may correctly communicate with the USB device.
101  */
102 const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
103 {
104         .Config =
105                 {
106                         .Header                 = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
107
108                         .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
109                         .TotalInterfaces        = 2,
110
111                         .ConfigurationNumber    = 1,
112                         .ConfigurationStrIndex  = NO_DESCRIPTOR,
113
114                         .ConfigAttributes       = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
115
116                         .MaxPowerConsumption    = USB_CONFIG_POWER_MA(100)
117                 },
118
119         .HID1_KeyboardInterface =
120                 {
121                         .Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
122
123                         .InterfaceNumber        = INTERFACE_ID_Keyboard,
124                         .AlternateSetting       = 0x00,
125
126                         .TotalEndpoints         = 1,
127
128                         .Class                  = HID_CSCP_HIDClass,
129                         .SubClass               = HID_CSCP_BootSubclass,
130                         .Protocol               = HID_CSCP_KeyboardBootProtocol,
131
132                         .InterfaceStrIndex      = NO_DESCRIPTOR
133                 },
134
135         .HID1_KeyboardHID =
136                 {
137                         .Header                 = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
138
139                         .HIDSpec                = VERSION_BCD(1,1,1),
140                         .CountryCode            = 0x00,
141                         .TotalReportDescriptors = 1,
142                         .HIDReportType          = HID_DTYPE_Report,
143                         .HIDReportLength        = sizeof(KeyboardReport)
144                 },
145
146         .HID1_ReportINEndpoint =
147                 {
148                         .Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
149
150                         .EndpointAddress        = KEYBOARD_IN_EPADDR,
151                         .Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
152                         .EndpointSize           = HID_EPSIZE,
153                         .PollingIntervalMS      = 0x05
154                 },
155
156         .HID2_MouseInterface =
157                 {
158                         .Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
159
160                         .InterfaceNumber        = INTERFACE_ID_Mouse,
161                         .AlternateSetting       = 0x00,
162
163                         .TotalEndpoints         = 1,
164
165                         .Class                  = HID_CSCP_HIDClass,
166                         .SubClass               = HID_CSCP_BootSubclass,
167                         .Protocol               = HID_CSCP_MouseBootProtocol,
168
169                         .InterfaceStrIndex      = NO_DESCRIPTOR
170                 },
171
172         .HID2_MouseHID =
173                 {
174                         .Header                 = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
175
176                         .HIDSpec                = VERSION_BCD(1,1,1),
177                         .CountryCode            = 0x00,
178                         .TotalReportDescriptors = 1,
179                         .HIDReportType          = HID_DTYPE_Report,
180                         .HIDReportLength        = sizeof(MouseReport)
181                 },
182
183         .HID2_ReportINEndpoint =
184                 {
185                         .Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
186
187                         .EndpointAddress        = MOUSE_IN_EPADDR,
188                         .Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
189                         .EndpointSize           = HID_EPSIZE,
190                         .PollingIntervalMS      = 0x05
191                 }
192 };
193
194 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
195  *  the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
196  *  via the language ID table available at USB.org what languages the device supports for its string descriptors.
197  */
198 const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
199
200 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
201  *  form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
202  *  Descriptor.
203  */
204 const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera");
205
206 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
207  *  and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
208  *  Descriptor.
209  */
210 const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA Mouse and Keyboard Demo");
211
212 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
213  *  documentation) by the application code so that the address and size of a requested descriptor can be given
214  *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
215  *  is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
216  *  USB host.
217  */
218 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
219                                     const uint8_t wIndex,
220                                     const void** const DescriptorAddress)
221 {
222         const uint8_t  DescriptorType   = (wValue >> 8);
223         const uint8_t  DescriptorNumber = (wValue & 0xFF);
224
225         const void* Address = NULL;
226         uint16_t    Size    = NO_DESCRIPTOR;
227
228         switch (DescriptorType)
229         {
230                 case DTYPE_Device:
231                         Address = &DeviceDescriptor;
232                         Size    = sizeof(USB_Descriptor_Device_t);
233                         break;
234                 case DTYPE_Configuration:
235                         Address = &ConfigurationDescriptor;
236                         Size    = sizeof(USB_Descriptor_Configuration_t);
237                         break;
238                 case DTYPE_String:
239                         switch (DescriptorNumber)
240                         {
241                                 case STRING_ID_Language:
242                                         Address = &LanguageString;
243                                         Size    = pgm_read_byte(&LanguageString.Header.Size);
244                                         break;
245                                 case STRING_ID_Manufacturer:
246                                         Address = &ManufacturerString;
247                                         Size    = pgm_read_byte(&ManufacturerString.Header.Size);
248                                         break;
249                                 case STRING_ID_Product:
250                                         Address = &ProductString;
251                                         Size    = pgm_read_byte(&ProductString.Header.Size);
252                                         break;
253                         }
254
255                         break;
256                 case HID_DTYPE_HID:
257                         switch (wIndex)
258                         {
259                                 case INTERFACE_ID_Keyboard:
260                                         Address = &ConfigurationDescriptor.HID1_KeyboardHID;
261                                         Size    = sizeof(USB_HID_Descriptor_HID_t);
262                                         break;
263                                 case INTERFACE_ID_Mouse:
264                                         Address = &ConfigurationDescriptor.HID2_MouseHID;
265                                         Size    = sizeof(USB_HID_Descriptor_HID_t);
266                                         break;
267                         }
268
269                         break;
270                 case HID_DTYPE_Report:
271                         switch (wIndex)
272                         {
273                                 case INTERFACE_ID_Keyboard:
274                                         Address = &KeyboardReport;
275                                         Size    = sizeof(KeyboardReport);
276                                         break;
277                                 case INTERFACE_ID_Mouse:
278                                         Address = &MouseReport;
279                                         Size    = sizeof(MouseReport);
280                                         break;
281                         }
282
283                         break;
284         }
285
286         *DescriptorAddress = Address;
287         return Size;
288 }
289