forked from raphnet/wusbmote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
419 lines (361 loc) · 12 KB
/
main.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* wusbmote: Wiimote accessory to USB Adapter
* Copyright (C) 2012-2014 Raphaël Assénat
*
* Based on:
* VBoy2USB: Virtual Boy controller to USB Adapter
* Copyright (C) 2009 Raphaël Assénat
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* The author may be contacted at raph@raphnet.net
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include <string.h>
#include "usbdrv.h"
#include "gamepad.h"
#include "eeprom.h"
#include "config.h"
#include "wusbmote_requests.h"
#include "i2c_gamepad.h"
#include "i2c_mouse.h"
#include "i2c_generic.h"
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168A__) || \
defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328__) || \
defined(__AVR_ATmega328P__) || defined(__AVR_ATmega88__) || \
defined(__AVR_ATmega88A__) || defined(__AVR_ATmega88P__) || \
defined(__AVR_ATmega88PA__)
#define AT168_COMPATIBLE
#endif
static uchar const *rt_usbHidReportDescriptor=NULL;
static uchar rt_usbHidReportDescriptorSize=0;
static uchar const *rt_usbDeviceDescriptor=NULL;
static uchar rt_usbDeviceDescriptorSize=0;
char usbDescriptorConfiguration[] = { 0 }; // dummy
const uchar dataHidReport[] PROGMEM = {
0x06, 0x00, 0xff, // USAGE_PAGE (Generic Desktop)
0x09, 0x01, // USAGE (Vendor Usage 1)
0xa1, 0x01, // COLLECTION (Application)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x05, // REPORT_COUNT (5)
0x09, 0x01, // USAGE (Vendor defined)
0xB1, 0x00, // FEATURE (Data,Ary,Abs)
0xc0 // END_COLLECTION
};
uchar my_usbDescriptorConfiguration[] = { /* USB configuration descriptor */
9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
USBDESCR_CONFIG, /* descriptor type */
18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 9 + 9 + 9 + 7, 0,
/* total length of data returned (including inlined descriptors) */
2, /* number of interfaces in this configuration */
1, /* index of this configuration */
0, /* configuration name string index */
#if USB_CFG_IS_SELF_POWERED
USBATTR_SELFPOWER, /* attributes */
#else
USBATTR_BUSPOWER | 0x80, /* attributes */
#endif
USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
/* interface descriptor follows inline: */
9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
USBDESCR_INTERFACE, /* descriptor type */
0, /* index of this interface */
0, /* alternate setting for this interface */
USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
USB_CFG_INTERFACE_CLASS,
USB_CFG_INTERFACE_SUBCLASS,
USB_CFG_INTERFACE_PROTOCOL,
0, /* string index for interface */
9, /* sizeof(usbDescrHID): length of descriptor in bytes */
USBDESCR_HID, /* descriptor type: HID */
0x01, 0x01, /* BCD representation of HID version */
0x00, /* target country code */
0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
0x22, /* descriptor type: report */
USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH, 0, /* total length of report descriptor */
/* endpoint descriptor for endpoint 1 */
7, /* sizeof(usbDescrEndpoint) */
USBDESCR_ENDPOINT, /* descriptor type = endpoint */
0x81, /* IN endpoint number 1 */
0x03, /* attrib: Interrupt endpoint */
8, 0, /* maximum packet size */
USB_CFG_INTR_POLL_INTERVAL, /* in ms */
/**** Interface 1 *****/
/* interface descriptor follows inline: */
9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
USBDESCR_INTERFACE, /* descriptor type */
1, /* index of this interface */
0, /* alternate setting for this interface */
USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
USB_CFG_INTERFACE_CLASS,
USB_CFG_INTERFACE_SUBCLASS,
USB_CFG_INTERFACE_PROTOCOL,
0, /* string index for interface */
9, /* sizeof(usbDescrHID): length of descriptor in bytes */
USBDESCR_HID, /* descriptor type: HID */
0x01, 0x01, /* BCD representation of HID version */
0x00, /* target country code */
0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
0x22, /* descriptor type: report */
sizeof(dataHidReport), 0, /* total length of report descriptor */
#if 1
/* endpoint descriptor for endpoint 3 */
7, /* sizeof(usbDescrEndpoint) */
USBDESCR_ENDPOINT, /* descriptor type = endpoint */
0x83, /* IN endpoint number 3 */
0x03, /* attrib: Interrupt endpoint */
8, 0, /* maximum packet size */
USB_CFG_INTR_POLL_INTERVAL, /* in ms */
#endif
};
static Gamepad *curGamepad;
/* ----------------------- hardware I/O abstraction ------------------------ */
static void hardwareInit(void)
{
// init port C as input with pullup on all but SCL/SDA pins. Those are external to 3.3v
DDRC = 0x00;
PORTC = 0x0f;
// Port B does nothing in this project
DDRB = 0x00;
PORTB = 0xff;
/*
* For port D, activate pull-ups on all lines except the D+, D- and bit 1.
*
* For historical reasons (a mistake on an old PCB), bit 1
* is now always connected with bit 0. So bit 1 configured
* as an input without pullup.
*
* Usb pin are init as output, low. (device reset). They are released
* later when usbReset() is called.
*/
PORTD = 0xf8;
DDRD = 0x01 | 0x04;
/* Configure timers */
#if defined(AT168_COMPATIBLE)
TCCR0A = 0; // normal
TCCR0B = 5;
TCCR2A = (1<<WGM21);
TCCR2B =(1<<CS22)|(1<<CS21)|(1<<CS20);
OCR2A= 196; // for 60 hz
#else
/* configure timer 0 for a rate of 12M/(1024 * 256) = 45.78 Hz (~22ms) */
TCCR0 = 5; /* timer 0 prescaler: 1024 */
TCCR2 = (1<<WGM21)|(1<<CS22)|(1<<CS21)|(1<<CS20);
OCR2 = 196; // for 60 hz
#endif
}
#if defined(AT168_COMPATIBLE)
#define mustPollControllers() (TIFR2 & (1<<OCF2A))
#define clrPollControllers() do { TIFR2 = 1<<OCF2A; } while(0)
#else
#define mustPollControllers() (TIFR & (1<<OCF2))
#define clrPollControllers() do { TIFR = 1<<OCF2; } while(0)
#endif
static void usbReset(void)
{
/* [...] a single ended zero or SE0 can be used to signify a device
reset if held for more than 10mS. A SE0 is generated by holding
both th D- and D+ low (< 0.3V).
*/
PORTD &= ~(0x01 | 0x04); // Set D+ and D- to 0
DDRD |= 0x01 | 0x04;
_delay_ms(15);
DDRD &= ~(0x01 | 0x04);
}
static uchar reportBuffer[10]; /* buffer for HID reports */
/* ------------------------------------------------------------------------- */
/* ----------------------------- USB interface ----------------------------- */
/* ------------------------------------------------------------------------- */
uchar usbFunctionDescriptor(struct usbRequest *rq)
{
switch(rq->bmRequestType & USBRQ_TYPE_MASK)
{
case USBRQ_TYPE_STANDARD:
if (rq->bRequest == USBRQ_GET_DESCRIPTOR)
{
// USB spec 9.4.3, high byte is descriptor type
switch (rq->wValue.bytes[1])
{
case USBDESCR_DEVICE:
usbMsgPtr = (void*)rt_usbDeviceDescriptor;
return rt_usbDeviceDescriptorSize;
case USBDESCR_HID_REPORT:
// HID 1.1 : 7.1.1 Get_Descriptor request. wIndex is the interface number.
if (rq->wIndex.word==1) {
usbMsgPtr = (void*)dataHidReport;
return sizeof(dataHidReport);
} else {
usbMsgPtr = (void*)rt_usbHidReportDescriptor;
return rt_usbHidReportDescriptorSize;
}
case USBDESCR_CONFIG:
usbMsgPtr = my_usbDescriptorConfiguration;
return sizeof(my_usbDescriptorConfiguration);
}
}
break;
case USBRQ_TYPE_VENDOR:
break;
}
return 0;
}
static uchar g_set_report_interface = 0;
uchar usbFunctionSetup(uchar data[8])
{
uchar rqdata[4];
static uchar replybuf[8];
usbRequest_t *rq = (void *)data;
usbMsgPtr = reportBuffer;
switch (rq->bmRequestType & USBRQ_TYPE_MASK)
{
case USBRQ_TYPE_CLASS:
switch (rq->bRequest)
{
case USBRQ_HID_GET_REPORT:
/* wValue: ReportType (highbyte), ReportID (lowbyte) */
/* we only have one report type, so don't look at wValue */
if (rq->wValue.bytes[1] == 0x03) // Feature report
{
if (curGamepad->getFeatureReport)
return curGamepad->getFeatureReport(reportBuffer);
return 0;
}
else {
curGamepad->buildReport(reportBuffer);
}
return curGamepad->report_size;
case USBRQ_HID_SET_REPORT:
/* wValue: Report Type (high), ReportID (low) */
// wIndex : Interface
// Note: Report type not checked (we only have feature reports, not out reports)
g_set_report_interface = rq->wIndex.word;
return USB_NO_MSG; // usbFunctionWrite will be called
}
break;
case USBRQ_TYPE_VENDOR:
rqdata[0] = rq->wValue.bytes[0];
rqdata[1] = rq->wValue.bytes[1];
rqdata[2] = rq->wIndex.bytes[0];
rqdata[3] = rq->wIndex.bytes[1];
usbMsgPtr = replybuf;
return config_handleCommand(rq->bRequest, rqdata, replybuf);
}
return 0;
}
uchar usbFunctionWrite(uchar *data, uchar len)
{
unsigned char dst[8];
if (g_set_report_interface==0)
{
if (curGamepad->setFeatureReport)
{
if (curGamepad->setFeatureReport(data, len) < 0)
return 0xff;
return 1;
}
return 1;
}
else {
if (len != 5) {
return 0xff;
}
if (!config_handleCommand(data[0], data+1, dst)) {
return 0xff;
}
}
return 1;
}
/* ------------------------------------------------------------------------- */
void transferGamepadReport(void)
{
int i;
int todo = curGamepad->report_size;
curGamepad->buildReport(reportBuffer);
for (i=0; i<=curGamepad->report_size; i+=8)
{
while (!usbInterruptIsReady()) {
usbPoll(); wdt_reset();
}
usbSetInterrupt(reportBuffer + i, todo > 8 ? 8 : todo);
todo -= 8;
}
}
int main(void)
{
char must_report = 0, first_run = 1;
hardwareInit();
eeprom_init();
switch (g_eeprom_data.cfg.mode)
{
default:
g_eeprom_data.cfg.mode = CFG_MODE_JOYSTICK;
eeprom_commit();
// fallthrough to Joystick mode
case CFG_MODE_JOYSTICK:
curGamepad = i2cGamepad_GetGamepad();
break;
case CFG_MODE_MOUSE:
curGamepad = i2cMouse_GetGamepad();
break;
case CFG_MODE_I2C_RAW:
curGamepad = rawi2c_GetGamepad();
break;
}
// configure report descriptor according to
// the current gamepad
rt_usbHidReportDescriptor = curGamepad->reportDescriptor;
rt_usbHidReportDescriptorSize = curGamepad->reportDescriptorSize;
rt_usbDeviceDescriptor = (void*)curGamepad->deviceDescriptor;
rt_usbDeviceDescriptorSize = curGamepad->deviceDescriptorSize;
// patch the config descriptor with the HID report descriptor size
my_usbDescriptorConfiguration[25] = rt_usbHidReportDescriptorSize;
my_usbDescriptorConfiguration[26] = rt_usbHidReportDescriptorSize >> 8;
//wdt_enable(WDTO_2S);
curGamepad->init();
usbReset();
usbInit();
sei();
for(;;){ /* main event loop */
wdt_reset();
// this must be called at each 50 ms or less
usbPoll();
if (first_run) {
curGamepad->update();
first_run = 0;
}
if (mustPollControllers())
{
if (!must_report)
{
curGamepad->update();
if (curGamepad->changed()) {
must_report = 1;
}
}
clrPollControllers();
}
if(must_report && usbInterruptIsReady())
{
transferGamepadReport();
must_report = 0;
}
}
return 0;
}