forked from 48productions/piuio-pico
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusb_descriptors.c
233 lines (182 loc) · 7.47 KB
/
usb_descriptors.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*******************************************************************************************/
/* SPDX-License-Identifier: MIT */
/* SPDX-FileCopyrightText: Copyright (c) 2023 48productions, therathatter, dj505, sugoku */
/* SPDX-FileCopyrightText: Copyright (c) 2021 Jason Skuby (mytechtoybox.com) */
/* SPDX-FileCopyrightText: Copyright (c) 2019 Ha Thach (tinyusb.org) */
/* https://github.com/sugoku/piuio-pico-brokeIO */
/*******************************************************************************************/
/*
* SPDX-FileCopyrightText: Copyright (c) 2023 sugoku
* SPDX-FileCopyrightText: Copyright (c) 2021 Jason Skuby (mytechtoybox.com)
* SPDX-FileCopyrightText: Copyright (c) 2019 Ha Thach (tinyusb.org)
*/
#include "tusb.h"
#include "usb_descriptors.h"
#include "input_mode.h"
#include "descriptors/hid_desc.h"
#include "descriptors/keyboard_desc.h"
#include "descriptors/lxio_desc.h"
#include "descriptors/piuio_desc.h"
#include "descriptors/switch_desc.h"
#include "descriptors/xinput_desc.h"
#include "descriptors/gamecube_desc.h"
#include "descriptors/serial_desc.h"
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
*
* Auto ProductID layout's Bitmap:
* [MSB] MIDI | HID | MSC | CDC [LSB]
*/
// #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
// #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
// _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
//--------------------------------------------------------------------+
// Device Descriptors
//--------------------------------------------------------------------+
// Invoked when received GET DEVICE DESCRIPTOR
// Application return pointer to descriptor
uint8_t const *tud_descriptor_device_cb(void)
{
switch (get_input_mode())
{
case INPUT_MODE_PIUIO:
return (uint8_t const*)&piuio_device_descriptor;
case INPUT_MODE_GAMEPAD:
return hid_device_descriptor;
case INPUT_MODE_LXIO:
return (uint8_t const*)&lxio_device_descriptor;
case INPUT_MODE_KEYBOARD:
return keyboard_device_descriptor;
case INPUT_MODE_XINPUT:
return xinput_device_descriptor;
case INPUT_MODE_SWITCH:
return switch_device_descriptor;
case INPUT_MODE_GAMECUBE:
return gamecube_device_descriptor;
case INPUT_MODE_SERIAL:
return (uint8_t const*)&serial_device_descriptor;
default:
return (uint8_t const*)&piuio_device_descriptor;
}
}
//--------------------------------------------------------------------+
// Configuration Descriptor
//--------------------------------------------------------------------+
// Invoked when received GET HID REPORT DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf)
{
(void) itf;
switch (get_input_mode())
{
case INPUT_MODE_GAMEPAD:
return hid_report_descriptor;
case INPUT_MODE_LXIO:
return lxio_report_descriptor;
case INPUT_MODE_KEYBOARD:
return keyboard_report_descriptor;
case INPUT_MODE_SWITCH:
return switch_report_descriptor;
case INPUT_MODE_GAMECUBE:
return gamecube_report_descriptor;
default:
return hid_report_descriptor;
}
}
// Invoked when received GET CONFIGURATION DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const *tud_descriptor_configuration_cb(uint8_t index)
{
switch (get_input_mode())
{
case INPUT_MODE_GAMEPAD:
return hid_configuration_descriptor;
case INPUT_MODE_LXIO:
return lxio_configuration_descriptor;
case INPUT_MODE_KEYBOARD:
return keyboard_configuration_descriptor;
case INPUT_MODE_XINPUT:
return xinput_configuration_descriptor;
case INPUT_MODE_SWITCH:
return switch_configuration_descriptor;
case INPUT_MODE_GAMECUBE:
return gamecube_configuration_descriptor;
case INPUT_MODE_SERIAL:
return serial_configuration_descriptor;
default:
return piuio_configuration_descriptor;
}
}
//--------------------------------------------------------------------+
// String Descriptors
//--------------------------------------------------------------------+
// array of pointer to string descriptors
char const* brokeio_string_desc_arr [] =
{
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
"sugoku", // 1: Manufacturer
"brokeIO", // 2: Product
"727", // 3: Serials, should use chip ID
"PIUIO-pico" // 4: Vendor Interface
};
static uint16_t _desc_str[32];
// Invoked when received GET STRING DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
// based on GP2040-CE implementation
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
{
(void) langid;
uint8_t chr_count;
const char** string_desc_arr;
switch (get_input_mode())
{
case INPUT_MODE_GAMEPAD:
string_desc_arr = (const char**)hid_string_descriptors;
break;
case INPUT_MODE_LXIO:
string_desc_arr = (const char**)lxio_string_descriptors;
break;
case INPUT_MODE_KEYBOARD:
string_desc_arr = (const char**)keyboard_string_descriptors;
break;
case INPUT_MODE_XINPUT:
string_desc_arr = (const char**)xinput_string_descriptors;
break;
case INPUT_MODE_SWITCH:
string_desc_arr = (const char**)switch_string_descriptors;
break;
case INPUT_MODE_GAMECUBE:
string_desc_arr = (const char**)gamecube_string_descriptors;
break;
case INPUT_MODE_SERIAL:
string_desc_arr = (const char**)serial_string_descriptors;
break;
default:
string_desc_arr = (const char**)piuio_string_descriptors;
break;
}
if ( index == 0)
{
memcpy(&_desc_str[1], string_desc_arr[0], 2);
chr_count = 1;
}else
{
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
//if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
const char* str = string_desc_arr[index];
// Cap at max char
chr_count = (uint8_t) strlen(str);
if ( chr_count > 31 ) chr_count = 31;
// Convert ASCII string into UTF-16
for(uint8_t i=0; i<chr_count; i++)
{
_desc_str[1+i] = str[i];
}
}
// first byte is length (including header), second byte is string type
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
return _desc_str;
}