source: avrstuff/grip2hid/Descriptors.c@ 02817db

main
Last change on this file since 02817db was 02817db, checked in by PulkoMandy <pulkomandy@…>, 4 months ago

grip2hid: tweak HID report to match PS3 controller layout

The grip2hid is now better usable on hte PS3 console!

  • Property mode set to 100644
File size: 14.7 KB
Line 
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#define BLOCK1 \
41 HID_RI_USAGE(8, 9), \
42 HID_RI_USAGE(8, 10), \
43 HID_RI_USAGE(8, 8), \
44 HID_RI_USAGE(8, 4), \
45 HID_RI_REPORT_SIZE(8, 0x01), \
46 HID_RI_REPORT_COUNT(8, 4), \
47 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
48
49#define BLOCK2 \
50 HID_RI_USAGE(8, 7), \
51 HID_RI_USAGE(8, 3), \
52 HID_RI_USAGE(8, 2), \
53 HID_RI_USAGE(8, 1), \
54 HID_RI_REPORT_SIZE(8, 0x01), \
55 HID_RI_REPORT_COUNT(8, 4), \
56 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
57
58/** HID class report descriptor. This is a special descriptor constructed with values from the
59 * USBIF HID class specification to describe the reports and capabilities of the HID device. This
60 * descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
61 * the device will send, and what it may be sent back from the host. Refer to the HID specification for
62 * more details on HID report descriptors.
63 */
64const USB_Descriptor_HIDReport_Datatype_t PROGMEM JoystickReport[] =
65{
66 HID_RI_USAGE_PAGE(8, 0x01),
67 HID_RI_USAGE(8, 0x04),
68 HID_RI_COLLECTION(8, 0x01),
69
70 0x85, 0x01, // Report ID 1
71
72 // 15 unused bits (the report is 32 bits to simplify/optimize some things, with
73 // extra padding at the start (8 bits), followed by the sync sequence 0111110
74 // (7 bits)
75 HID_RI_REPORT_SIZE(8, 15),
76 HID_RI_REPORT_COUNT(8, 0x01),
77 HID_RI_INPUT(8, HID_IOF_CONSTANT),
78
79 // Let's define some buttons
80 HID_RI_USAGE_PAGE(8, 0x09),
81 HID_RI_LOGICAL_MINIMUM(8, 0x00),
82 HID_RI_LOGICAL_MAXIMUM(8, 0x01),
83
84 // 9, 10, 8, 4 (Select, Start, R2, Blue)
85 BLOCK1
86
87 // One unused bit (always 0)
88 HID_RI_REPORT_SIZE(8, 1),
89 HID_RI_REPORT_COUNT(8, 0x01),
90 HID_RI_INPUT(8, HID_IOF_CONSTANT),
91
92 // 7, 3, 2, 1 (L2, Green, Yellow, Red)
93 BLOCK2
94
95 HID_RI_REPORT_SIZE(8, 1),
96 HID_RI_REPORT_COUNT(8, 0x01),
97 HID_RI_INPUT(8, HID_IOF_CONSTANT),
98
99 // 5, 6 (L1, R1)
100 HID_RI_USAGE_MINIMUM(8, 5),
101 HID_RI_USAGE_MAXIMUM(8, 6),
102 HID_RI_REPORT_SIZE(8, 0x01),
103 HID_RI_REPORT_COUNT(8, 2),
104 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
105
106 HID_RI_USAGE_PAGE(8, 0x01),
107 HID_RI_USAGE(8, 0x01),
108 HID_RI_COLLECTION(8, 0x00),
109 HID_RI_USAGE(8, 0x31),
110 HID_RI_LOGICAL_MINIMUM(8, -2),
111 HID_RI_LOGICAL_MAXIMUM(8, 1),
112 HID_RI_REPORT_COUNT(8, 1),
113 HID_RI_REPORT_SIZE(8, 2),
114 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
115
116 HID_RI_REPORT_SIZE(8, 1),
117 HID_RI_REPORT_COUNT(8, 0x01),
118 HID_RI_INPUT(8, HID_IOF_CONSTANT),
119
120 HID_RI_USAGE(8, 0x30),
121 HID_RI_LOGICAL_MINIMUM(8, -2),
122 HID_RI_LOGICAL_MAXIMUM(8, 1),
123 HID_RI_REPORT_COUNT(8, 1),
124 HID_RI_REPORT_SIZE(8, 2),
125 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
126 HID_RI_END_COLLECTION(0),
127
128 HID_RI_END_COLLECTION(0),
129
130 HID_RI_USAGE_PAGE(8, 0x01),
131 HID_RI_USAGE(8, 0x04),
132 HID_RI_COLLECTION(8, 0x01),
133
134 0x85, 0x02, // Report ID 2
135
136 // 15 constant bytes
137 HID_RI_REPORT_SIZE(8, 15),
138 HID_RI_REPORT_COUNT(8, 0x01),
139 HID_RI_INPUT(8, HID_IOF_CONSTANT),
140
141 HID_RI_USAGE_PAGE(8, 0x09),
142 HID_RI_LOGICAL_MINIMUM(8, 0x00),
143 HID_RI_LOGICAL_MAXIMUM(8, 0x01),
144
145 BLOCK1
146
147 HID_RI_REPORT_SIZE(8, 1),
148 HID_RI_REPORT_COUNT(8, 0x01),
149 HID_RI_INPUT(8, HID_IOF_CONSTANT),
150
151 BLOCK2
152
153 HID_RI_REPORT_SIZE(8, 1),
154 HID_RI_REPORT_COUNT(8, 0x01),
155 HID_RI_INPUT(8, HID_IOF_CONSTANT),
156
157 HID_RI_USAGE_MINIMUM(8, 5),
158 HID_RI_USAGE_MAXIMUM(8, 6),
159 HID_RI_REPORT_SIZE(8, 0x01),
160 HID_RI_REPORT_COUNT(8, 2),
161 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
162
163 HID_RI_USAGE_PAGE(8, 0x01),
164 HID_RI_USAGE(8, 0x01),
165 HID_RI_COLLECTION(8, 0x00),
166 HID_RI_USAGE(8, 0x31),
167 HID_RI_LOGICAL_MINIMUM(8, -2),
168 HID_RI_LOGICAL_MAXIMUM(8, 1),
169 HID_RI_REPORT_COUNT(8, 1),
170 HID_RI_REPORT_SIZE(8, 2),
171 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
172
173 HID_RI_REPORT_SIZE(8, 1),
174 HID_RI_REPORT_COUNT(8, 0x01),
175 HID_RI_INPUT(8, HID_IOF_CONSTANT),
176
177 HID_RI_USAGE(8, 0x30),
178 HID_RI_LOGICAL_MINIMUM(8, -2),
179 HID_RI_LOGICAL_MAXIMUM(8, 1),
180 HID_RI_REPORT_COUNT(8, 1),
181 HID_RI_REPORT_SIZE(8, 2),
182 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
183 HID_RI_END_COLLECTION(0),
184
185 HID_RI_END_COLLECTION(0),
186
187 HID_RI_USAGE_PAGE(8, 0x01),
188 HID_RI_USAGE(8, 0x04),
189 HID_RI_COLLECTION(8, 0x01),
190
191 0x85, 0x03, // Report ID 3
192
193 // 15 constant bytes
194 HID_RI_REPORT_SIZE(8, 15),
195 HID_RI_REPORT_COUNT(8, 0x01),
196 HID_RI_INPUT(8, HID_IOF_CONSTANT),
197
198 HID_RI_USAGE_PAGE(8, 0x09),
199 HID_RI_LOGICAL_MINIMUM(8, 0x00),
200 HID_RI_LOGICAL_MAXIMUM(8, 0x01),
201
202 BLOCK1
203
204 HID_RI_REPORT_SIZE(8, 1),
205 HID_RI_REPORT_COUNT(8, 0x01),
206 HID_RI_INPUT(8, HID_IOF_CONSTANT),
207
208 BLOCK2
209
210 HID_RI_REPORT_SIZE(8, 1),
211 HID_RI_REPORT_COUNT(8, 0x01),
212 HID_RI_INPUT(8, HID_IOF_CONSTANT),
213
214 HID_RI_USAGE_MINIMUM(8, 5),
215 HID_RI_USAGE_MAXIMUM(8, 6),
216 HID_RI_REPORT_SIZE(8, 0x01),
217 HID_RI_REPORT_COUNT(8, 2),
218 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
219
220 HID_RI_USAGE_PAGE(8, 0x01),
221 HID_RI_USAGE(8, 0x01),
222 HID_RI_COLLECTION(8, 0x00),
223 HID_RI_USAGE(8, 0x31),
224 HID_RI_LOGICAL_MINIMUM(8, -2),
225 HID_RI_LOGICAL_MAXIMUM(8, 1),
226 HID_RI_REPORT_COUNT(8, 1),
227 HID_RI_REPORT_SIZE(8, 2),
228 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
229
230 HID_RI_REPORT_SIZE(8, 1),
231 HID_RI_REPORT_COUNT(8, 0x01),
232 HID_RI_INPUT(8, HID_IOF_CONSTANT),
233
234 HID_RI_USAGE(8, 0x30),
235 HID_RI_LOGICAL_MINIMUM(8, -2),
236 HID_RI_LOGICAL_MAXIMUM(8, 1),
237 HID_RI_REPORT_COUNT(8, 1),
238 HID_RI_REPORT_SIZE(8, 2),
239 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
240 HID_RI_END_COLLECTION(0),
241
242 HID_RI_END_COLLECTION(0),
243
244 HID_RI_USAGE_PAGE(8, 0x01),
245 HID_RI_USAGE(8, 0x04),
246 HID_RI_COLLECTION(8, 0x01),
247
248 0x85, 0x04, // Report ID 4
249
250 // 15 constant bytes
251 HID_RI_REPORT_SIZE(8, 15),
252 HID_RI_REPORT_COUNT(8, 0x01),
253 HID_RI_INPUT(8, HID_IOF_CONSTANT),
254
255 HID_RI_USAGE_PAGE(8, 0x09),
256 HID_RI_LOGICAL_MINIMUM(8, 0x00),
257 HID_RI_LOGICAL_MAXIMUM(8, 0x01),
258
259 BLOCK1
260
261 HID_RI_REPORT_SIZE(8, 1),
262 HID_RI_REPORT_COUNT(8, 0x01),
263 HID_RI_INPUT(8, HID_IOF_CONSTANT),
264
265 BLOCK2
266
267 HID_RI_REPORT_SIZE(8, 1),
268 HID_RI_REPORT_COUNT(8, 0x01),
269 HID_RI_INPUT(8, HID_IOF_CONSTANT),
270
271 HID_RI_USAGE_MINIMUM(8, 5),
272 HID_RI_USAGE_MAXIMUM(8, 6),
273 HID_RI_REPORT_SIZE(8, 0x01),
274 HID_RI_REPORT_COUNT(8, 2),
275 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
276
277 HID_RI_USAGE_PAGE(8, 0x01),
278 HID_RI_USAGE(8, 0x01),
279 HID_RI_COLLECTION(8, 0x00),
280 HID_RI_USAGE(8, 0x31),
281 HID_RI_LOGICAL_MINIMUM(8, -2),
282 HID_RI_LOGICAL_MAXIMUM(8, 1),
283 HID_RI_REPORT_COUNT(8, 1),
284 HID_RI_REPORT_SIZE(8, 2),
285 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
286
287 HID_RI_REPORT_SIZE(8, 1),
288 HID_RI_REPORT_COUNT(8, 0x01),
289 HID_RI_INPUT(8, HID_IOF_CONSTANT),
290
291 HID_RI_USAGE(8, 0x30),
292 HID_RI_LOGICAL_MINIMUM(8, -2),
293 HID_RI_LOGICAL_MAXIMUM(8, 1),
294 HID_RI_REPORT_COUNT(8, 1),
295 HID_RI_REPORT_SIZE(8, 2),
296 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
297 HID_RI_END_COLLECTION(0),
298
299 HID_RI_END_COLLECTION(0)
300};
301
302/** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
303 * device characteristics, including the supported USB version, control endpoint size and the
304 * number of device configurations. The descriptor is read out by the USB host when the enumeration
305 * process begins.
306 */
307const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
308{
309 .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
310
311 .USBSpecification = VERSION_BCD(1,1,0),
312 .Class = USB_CSCP_NoDeviceClass,
313 .SubClass = USB_CSCP_NoDeviceSubclass,
314 .Protocol = USB_CSCP_NoDeviceProtocol,
315
316 .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
317
318 .VendorID = 0x16c0,
319 .ProductID = 0x27dc,
320 .ReleaseNumber = VERSION_BCD(0,0,1),
321
322 .ManufacturerStrIndex = STRING_ID_Manufacturer,
323 .ProductStrIndex = STRING_ID_Product,
324 .SerialNumStrIndex = NO_DESCRIPTOR,
325
326 .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
327};
328
329/** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
330 * of the device in one of its supported configurations, including information about any device interfaces
331 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
332 * a configuration so that the host may correctly communicate with the USB device.
333 */
334const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
335{
336 .Config =
337 {
338 .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
339
340 .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
341 .TotalInterfaces = 1,
342
343 .ConfigurationNumber = 1,
344 .ConfigurationStrIndex = NO_DESCRIPTOR,
345
346 .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
347
348 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
349 },
350
351 .HID_Interface =
352 {
353 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
354
355 .InterfaceNumber = INTERFACE_ID_Joystick,
356 .AlternateSetting = 0x00,
357
358 .TotalEndpoints = 1,
359
360 .Class = HID_CSCP_HIDClass,
361 .SubClass = HID_CSCP_NonBootSubclass,
362 .Protocol = HID_CSCP_NonBootProtocol,
363
364 .InterfaceStrIndex = NO_DESCRIPTOR
365 },
366
367 .HID_JoystickHID =
368 {
369 .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
370
371 .HIDSpec = VERSION_BCD(1,1,1),
372 .CountryCode = 0x00,
373 .TotalReportDescriptors = 1,
374 .HIDReportType = HID_DTYPE_Report,
375 .HIDReportLength = sizeof(JoystickReport)
376 },
377
378 .HID_ReportINEndpoint =
379 {
380 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
381
382 .EndpointAddress = JOYSTICK_EPADDR,
383 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
384 .EndpointSize = JOYSTICK_EPSIZE,
385 .PollingIntervalMS = 0x05
386 }
387};
388
389/** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
390 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
391 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
392 */
393const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
394
395/** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
396 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
397 * Descriptor.
398 */
399const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"PulkoTronics");
400
401/** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
402 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
403 * Descriptor.
404 */
405const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"Gravis GrIP gamepads");
406
407/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
408 * documentation) by the application code so that the address and size of a requested descriptor can be given
409 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
410 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
411 * USB host.
412 */
413uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
414 const uint16_t wIndex,
415 const void** const DescriptorAddress)
416{
417 const uint8_t DescriptorType = (wValue >> 8);
418 const uint8_t DescriptorNumber = (wValue & 0xFF);
419
420 const void* Address = NULL;
421 uint16_t Size = NO_DESCRIPTOR;
422
423 switch (DescriptorType)
424 {
425 case DTYPE_Device:
426 Address = &DeviceDescriptor;
427 Size = sizeof(USB_Descriptor_Device_t);
428 break;
429 case DTYPE_Configuration:
430 Address = &ConfigurationDescriptor;
431 Size = sizeof(USB_Descriptor_Configuration_t);
432 break;
433 case DTYPE_String:
434 switch (DescriptorNumber)
435 {
436 case STRING_ID_Language:
437 Address = &LanguageString;
438 Size = pgm_read_byte(&LanguageString.Header.Size);
439 break;
440 case STRING_ID_Manufacturer:
441 Address = &ManufacturerString;
442 Size = pgm_read_byte(&ManufacturerString.Header.Size);
443 break;
444 case STRING_ID_Product:
445 Address = &ProductString;
446 Size = pgm_read_byte(&ProductString.Header.Size);
447 break;
448 }
449
450 break;
451 case HID_DTYPE_HID:
452 Address = &ConfigurationDescriptor.HID_JoystickHID;
453 Size = sizeof(USB_HID_Descriptor_HID_t);
454 break;
455 case HID_DTYPE_Report:
456 Address = &JoystickReport;
457 Size = sizeof(JoystickReport);
458 break;
459 }
460
461 *DescriptorAddress = Address;
462 return Size;
463}
464
Note: See TracBrowser for help on using the repository browser.