]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/KeyboardMouseMultiReport/Descriptors.c
Merge commit '657d9f23fe47fb88cf221adb23095082f191ba6a'
[max/tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Device / ClassDriver / KeyboardMouseMultiReport / 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 multiple possible reports of the HID interface's report structure.
47  */
48 const USB_Descriptor_HIDReport_Datatype_t PROGMEM HIDReport[] =
49 {
50         /* Mouse Report */
51         HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
52         HID_RI_USAGE(8, 0x02), /* Mouse */
53         HID_RI_COLLECTION(8, 0x01), /* Application */
54                 HID_RI_REPORT_ID(8, HID_REPORTID_MouseReport),
55                 HID_RI_USAGE(8, 0x01), /* Pointer */
56                 HID_RI_COLLECTION(8, 0x00), /* Physical */
57                         HID_RI_USAGE_PAGE(8, 0x09), /* Button */
58                         HID_RI_USAGE_MINIMUM(8, 0x01),
59                         HID_RI_USAGE_MAXIMUM(8, 0x03),
60                         HID_RI_LOGICAL_MINIMUM(8, 0x00),
61                         HID_RI_LOGICAL_MAXIMUM(8, 0x01),
62                         HID_RI_REPORT_COUNT(8, 0x03),
63                         HID_RI_REPORT_SIZE(8, 0x01),
64                         HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
65                         HID_RI_REPORT_COUNT(8, 0x01),
66                         HID_RI_REPORT_SIZE(8, 0x05),
67                         HID_RI_INPUT(8, HID_IOF_CONSTANT),
68                         HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
69                         HID_RI_USAGE(8, 0x30), /* Usage X */
70                         HID_RI_USAGE(8, 0x31), /* Usage Y */
71                         HID_RI_LOGICAL_MINIMUM(8, -1),
72                         HID_RI_LOGICAL_MAXIMUM(8, 1),
73                         HID_RI_PHYSICAL_MINIMUM(8, -1),
74                         HID_RI_PHYSICAL_MAXIMUM(8, 1),
75                         HID_RI_REPORT_COUNT(8, 0x02),
76                         HID_RI_REPORT_SIZE(8, 0x08),
77                         HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
78                 HID_RI_END_COLLECTION(0),
79         HID_RI_END_COLLECTION(0),
80
81         /* Keyboard Report */
82         HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
83         HID_RI_USAGE(8, 0x06), /* Keyboard */
84         HID_RI_COLLECTION(8, 0x01), /* Application */
85                 HID_RI_REPORT_ID(8, HID_REPORTID_KeyboardReport),
86                 HID_RI_USAGE_PAGE(8, 0x07), /* Key Codes */
87                 HID_RI_USAGE_MINIMUM(8, 0xE0), /* Keyboard Left Control */
88                 HID_RI_USAGE_MAXIMUM(8, 0xE7), /* Keyboard Right GUI */
89                 HID_RI_LOGICAL_MINIMUM(8, 0x00),
90                 HID_RI_LOGICAL_MAXIMUM(8, 0x01),
91                 HID_RI_REPORT_SIZE(8, 0x01),
92                 HID_RI_REPORT_COUNT(8, 0x08),
93                 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
94                 HID_RI_REPORT_COUNT(8, 0x01),
95                 HID_RI_REPORT_SIZE(8, 0x08),
96                 HID_RI_INPUT(8, HID_IOF_CONSTANT),
97                 HID_RI_USAGE_PAGE(8, 0x08), /* LEDs */
98                 HID_RI_USAGE_MINIMUM(8, 0x01), /* Num Lock */
99                 HID_RI_USAGE_MAXIMUM(8, 0x05), /* Kana */
100                 HID_RI_REPORT_COUNT(8, 0x05),
101                 HID_RI_REPORT_SIZE(8, 0x01),
102                 HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
103                 HID_RI_REPORT_COUNT(8, 0x01),
104                 HID_RI_REPORT_SIZE(8, 0x03),
105                 HID_RI_OUTPUT(8, HID_IOF_CONSTANT),
106                 HID_RI_LOGICAL_MINIMUM(8, 0x00),
107                 HID_RI_LOGICAL_MAXIMUM(8, 0x65),
108                 HID_RI_USAGE_PAGE(8, 0x07), /* Keyboard */
109                 HID_RI_USAGE_MINIMUM(8, 0x00), /* Reserved (no event indicated) */
110                 HID_RI_USAGE_MAXIMUM(8, 0x65), /* Keyboard Application */
111                 HID_RI_REPORT_COUNT(8, 0x06),
112                 HID_RI_REPORT_SIZE(8, 0x08),
113                 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
114         HID_RI_END_COLLECTION(0),
115 };
116
117 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
118  *  device characteristics, including the supported USB version, control endpoint size and the
119  *  number of device configurations. The descriptor is read out by the USB host when the enumeration
120  *  process begins.
121  */
122 const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
123 {
124         .Header                 = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
125
126         .USBSpecification       = VERSION_BCD(1,1,0),
127         .Class                  = USB_CSCP_NoDeviceClass,
128         .SubClass               = USB_CSCP_NoDeviceSubclass,
129         .Protocol               = USB_CSCP_NoDeviceProtocol,
130
131         .Endpoint0Size          = FIXED_CONTROL_ENDPOINT_SIZE,
132
133         .VendorID               = 0x03EB,
134         .ProductID              = 0x2066,
135         .ReleaseNumber          = VERSION_BCD(0,0,1),
136
137         .ManufacturerStrIndex   = STRING_ID_Manufacturer,
138         .ProductStrIndex        = STRING_ID_Product,
139         .SerialNumStrIndex      = NO_DESCRIPTOR,
140
141         .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
142 };
143
144 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
145  *  of the device in one of its supported configurations, including information about any device interfaces
146  *  and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
147  *  a configuration so that the host may correctly communicate with the USB device.
148  */
149 const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
150 {
151         .Config =
152                 {
153                         .Header                 = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
154
155                         .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
156                         .TotalInterfaces        = 1,
157
158                         .ConfigurationNumber    = 1,
159                         .ConfigurationStrIndex  = NO_DESCRIPTOR,
160
161                         .ConfigAttributes       = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
162
163                         .MaxPowerConsumption    = USB_CONFIG_POWER_MA(100)
164                 },
165
166         .HID_Interface =
167                 {
168                         .Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
169
170                         .InterfaceNumber        = INTERFACE_ID_KeyboardAndMouse,
171                         .AlternateSetting       = 0x00,
172
173                         .TotalEndpoints         = 1,
174
175                         .Class                  = HID_CSCP_HIDClass,
176                         .SubClass               = HID_CSCP_NonBootSubclass,
177                         .Protocol               = HID_CSCP_NonBootProtocol,
178
179                         .InterfaceStrIndex      = NO_DESCRIPTOR
180                 },
181
182         .HID_HIDData =
183                 {
184                         .Header                 = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
185
186                         .HIDSpec                = VERSION_BCD(1,1,1),
187                         .CountryCode            = 0x00,
188                         .TotalReportDescriptors = 1,
189                         .HIDReportType          = HID_DTYPE_Report,
190                         .HIDReportLength        = sizeof(HIDReport)
191                 },
192
193         .HID_ReportINEndpoint =
194                 {
195                         .Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
196
197                         .EndpointAddress        = HID_IN_EPADDR,
198                         .Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
199                         .EndpointSize           = HID_EPSIZE,
200                         .PollingIntervalMS      = 0x05
201                 },
202 };
203
204 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
205  *  the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
206  *  via the language ID table available at USB.org what languages the device supports for its string descriptors.
207  */
208 const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
209
210 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
211  *  form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
212  *  Descriptor.
213  */
214 const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera");
215
216 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
217  *  and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
218  *  Descriptor.
219  */
220 const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA Multi HID Report Demo");
221
222 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
223  *  documentation) by the application code so that the address and size of a requested descriptor can be given
224  *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
225  *  is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
226  *  USB host.
227  */
228 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
229                                     const uint8_t wIndex,
230                                     const void** const DescriptorAddress)
231 {
232         const uint8_t  DescriptorType   = (wValue >> 8);
233         const uint8_t  DescriptorNumber = (wValue & 0xFF);
234
235         const void* Address = NULL;
236         uint16_t    Size    = NO_DESCRIPTOR;
237
238         switch (DescriptorType)
239         {
240                 case DTYPE_Device:
241                         Address = &DeviceDescriptor;
242                         Size    = sizeof(USB_Descriptor_Device_t);
243                         break;
244                 case DTYPE_Configuration:
245                         Address = &ConfigurationDescriptor;
246                         Size    = sizeof(USB_Descriptor_Configuration_t);
247                         break;
248                 case DTYPE_String:
249                         switch (DescriptorNumber)
250                         {
251                                 case STRING_ID_Language:
252                                         Address = &LanguageString;
253                                         Size    = pgm_read_byte(&LanguageString.Header.Size);
254                                         break;
255                                 case STRING_ID_Manufacturer:
256                                         Address = &ManufacturerString;
257                                         Size    = pgm_read_byte(&ManufacturerString.Header.Size);
258                                         break;
259                                 case STRING_ID_Product:
260                                         Address = &ProductString;
261                                         Size    = pgm_read_byte(&ProductString.Header.Size);
262                                         break;
263                         }
264
265                         break;
266                 case HID_DTYPE_HID:
267                         Address = &ConfigurationDescriptor.HID_HIDData;
268                         Size    = sizeof(USB_HID_Descriptor_HID_t);
269                         break;
270                 case HID_DTYPE_Report:
271                         Address = &HIDReport;
272                         Size    = sizeof(HIDReport);
273                         break;
274         }
275
276         *DescriptorAddress = Address;
277         return Size;
278 }
279