]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Device/LowLevel/GenericHID/Descriptors.c
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Device / LowLevel / GenericHID / 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 const USB_Descriptor_HIDReport_Datatype_t PROGMEM GenericReport[] =
47 {
48 HID_RI_USAGE_PAGE(16, 0xFF00), /* Vendor Page 0 */
49 HID_RI_USAGE(8, 0x01), /* Vendor Usage 1 */
50 HID_RI_COLLECTION(8, 0x01), /* Vendor Usage 1 */
51 HID_RI_USAGE(8, 0x02), /* Vendor Usage 2 */
52 HID_RI_LOGICAL_MINIMUM(8, 0x00),
53 HID_RI_LOGICAL_MAXIMUM(8, 0xFF),
54 HID_RI_REPORT_SIZE(8, 0x08),
55 HID_RI_REPORT_COUNT(8, GENERIC_REPORT_SIZE),
56 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
57 HID_RI_USAGE(8, 0x03), /* Vendor Usage 3 */
58 HID_RI_LOGICAL_MINIMUM(8, 0x00),
59 HID_RI_LOGICAL_MAXIMUM(8, 0xFF),
60 HID_RI_REPORT_SIZE(8, 0x08),
61 HID_RI_REPORT_COUNT(8, GENERIC_REPORT_SIZE),
62 HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
63 HID_RI_END_COLLECTION(0),
64 };
65
66 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
67 * device characteristics, including the supported USB version, control endpoint size and the
68 * number of device configurations. The descriptor is read out by the USB host when the enumeration
69 * process begins.
70 */
71 const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
72 {
73 .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
74
75 .USBSpecification = VERSION_BCD(1,1,0),
76 .Class = USB_CSCP_NoDeviceClass,
77 .SubClass = USB_CSCP_NoDeviceSubclass,
78 .Protocol = USB_CSCP_NoDeviceProtocol,
79
80 .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
81
82 .VendorID = 0x03EB,
83 .ProductID = 0x204F,
84 .ReleaseNumber = VERSION_BCD(0,0,1),
85
86 .ManufacturerStrIndex = STRING_ID_Manufacturer,
87 .ProductStrIndex = STRING_ID_Product,
88 .SerialNumStrIndex = NO_DESCRIPTOR,
89
90 .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
91 };
92
93 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
94 * of the device in one of its supported configurations, including information about any device interfaces
95 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
96 * a configuration so that the host may correctly communicate with the USB device.
97 */
98 const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
99 {
100 .Config =
101 {
102 .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
103
104 .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
105 .TotalInterfaces = 1,
106
107 .ConfigurationNumber = 1,
108 .ConfigurationStrIndex = NO_DESCRIPTOR,
109
110 .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
111
112 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
113 },
114
115 .HID_Interface =
116 {
117 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
118
119 .InterfaceNumber = INTERFACE_ID_GenericHID,
120 .AlternateSetting = 0x00,
121
122 .TotalEndpoints = 2,
123
124 .Class = HID_CSCP_HIDClass,
125 .SubClass = HID_CSCP_NonBootSubclass,
126 .Protocol = HID_CSCP_NonBootProtocol,
127
128 .InterfaceStrIndex = NO_DESCRIPTOR
129 },
130
131 .HID_GenericHID =
132 {
133 .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
134
135 .HIDSpec = VERSION_BCD(1,1,1),
136 .CountryCode = 0x00,
137 .TotalReportDescriptors = 1,
138 .HIDReportType = HID_DTYPE_Report,
139 .HIDReportLength = sizeof(GenericReport)
140 },
141
142 .HID_ReportINEndpoint =
143 {
144 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
145
146 .EndpointAddress = GENERIC_IN_EPADDR,
147 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
148 .EndpointSize = GENERIC_EPSIZE,
149 .PollingIntervalMS = 0x05
150 },
151
152 .HID_ReportOUTEndpoint =
153 {
154 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
155
156 .EndpointAddress = GENERIC_OUT_EPADDR,
157 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
158 .EndpointSize = GENERIC_EPSIZE,
159 .PollingIntervalMS = 0x05
160 }
161 };
162
163 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
164 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
165 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
166 */
167 const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
168
169 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
170 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
171 * Descriptor.
172 */
173 const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera");
174
175 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
176 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
177 * Descriptor.
178 */
179 const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA Generic HID Demo");
180
181 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
182 * documentation) by the application code so that the address and size of a requested descriptor can be given
183 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
184 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
185 * USB host.
186 */
187 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
188 const uint8_t wIndex,
189 const void** const DescriptorAddress)
190 {
191 const uint8_t DescriptorType = (wValue >> 8);
192 const uint8_t DescriptorNumber = (wValue & 0xFF);
193
194 const void* Address = NULL;
195 uint16_t Size = NO_DESCRIPTOR;
196
197 switch (DescriptorType)
198 {
199 case DTYPE_Device:
200 Address = &DeviceDescriptor;
201 Size = sizeof(USB_Descriptor_Device_t);
202 break;
203 case DTYPE_Configuration:
204 Address = &ConfigurationDescriptor;
205 Size = sizeof(USB_Descriptor_Configuration_t);
206 break;
207 case DTYPE_String:
208 switch (DescriptorNumber)
209 {
210 case STRING_ID_Language:
211 Address = &LanguageString;
212 Size = pgm_read_byte(&LanguageString.Header.Size);
213 break;
214 case STRING_ID_Manufacturer:
215 Address = &ManufacturerString;
216 Size = pgm_read_byte(&ManufacturerString.Header.Size);
217 break;
218 case STRING_ID_Product:
219 Address = &ProductString;
220 Size = pgm_read_byte(&ProductString.Header.Size);
221 break;
222 }
223
224 break;
225 case HID_DTYPE_HID:
226 Address = &ConfigurationDescriptor.HID_GenericHID;
227 Size = sizeof(USB_HID_Descriptor_HID_t);
228 break;
229 case HID_DTYPE_Report:
230 Address = &GenericReport;
231 Size = sizeof(GenericReport);
232 break;
233 }
234
235 *DescriptorAddress = Address;
236 return Size;
237 }
238
Imprint / Impressum