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