-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUSB_HID.c
137 lines (120 loc) · 4.57 KB
/
USB_HID.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
#include "USB_HID.h"
static DS3Report ds3ReportData;
static SwitchInputReport switchReportData;
static XInputReport xinputReportData;
static void *reportData;
static uint8_t reportSize;
static uint8_t lastReportBytes[64] = { 0 };
// Configures hardware and peripherals, such as the USB peripherals.
void SetupHardware(InputMode inputMode) {
//set xinput_mode for descriptors.h/.c
currentInputMode = inputMode;
SetupDescriptor(inputMode);
// We need to disable watchdog if enabled by bootloader/fuses.
MCUSR &= ~(1 << WDRF);
wdt_disable();
// We need to disable clock division before initializing the USB hardware.
clock_prescale_set(clock_div_1);
// We can then initialize our hardware and peripherals, including the USB stack.
// The USB stack should be initialized last.
USB_Init();
}
// Fired to indicate that the device is enumerating.
void EVENT_USB_Device_Connect(void) {
// We can indicate that we're enumerating here (via status LEDs, sound, etc.).
}
// Fired to indicate that the device is no longer connected to a host.
void EVENT_USB_Device_Disconnect(void) {
// We can indicate that our device is not ready (via status LEDs, sound, etc.).
}
// Fired when the host set the current configuration of the USB device after enumeration.
void EVENT_USB_Device_ConfigurationChanged(void) {
switch (currentInputMode) {
case DUALSHOCK3:
Endpoint_ConfigureEndpoint(JOYSTICK_OUT_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE_DS3, 1);
Endpoint_ConfigureEndpoint(JOYSTICK_IN_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE_DS3, 1);
break;
case SWITCH:
Endpoint_ConfigureEndpoint(JOYSTICK_OUT_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE_SWITCH, 1);
Endpoint_ConfigureEndpoint(JOYSTICK_IN_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE_SWITCH, 1);
break;
case XINPUT:
default:
Endpoint_ConfigureEndpoint((ENDPOINT_DIR_IN | 3), EP_TYPE_INTERRUPT, 32, 1);
Endpoint_ConfigureEndpoint(JOYSTICK_IN_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE_XINPUT, 1);
break;
}
}
// Process control requests sent to the device from the USB host.
void EVENT_USB_Device_ControlRequest(void) {
//No controlRequest received from the switch, so only handled in xinput mode
if (currentInputMode == XINPUT){
/* Handle HID Class specific requests */
switch (USB_ControlRequest.bRequest) {
case HID_REQ_GetReport:
if (USB_ControlRequest.bmRequestType
== (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) {
Endpoint_ClearSETUP();
/* Write the report data to the control endpoint */
switch (currentInputMode) {
case XINPUT:
Endpoint_Write_Control_Stream_LE(&xinputReportData, 20);
break;
case DUALSHOCK3:
Endpoint_Write_Control_Stream_LE(&ds3ReportData, JOYSTICK_EPSIZE_DS3);
break;
}
Endpoint_ClearOUT();
}
break;
}
}
}
// Process and deliver data from IN and OUT endpoints.
void HID_Task(void) {
/* Device must be connected and configured for the task to run */
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
//no OUT endpoint for xinput in this firmware
bool isXInput = currentInputMode == XINPUT;
if (!isXInput){
// We'll start with the OUT endpoint.
Endpoint_SelectEndpoint(JOYSTICK_OUT_EPADDR);
// We'll check to see if we received something on the OUT endpoint.
if (Endpoint_IsOUTReceived())
{
// If we did, and the packet has data, we'll react to it.
if (Endpoint_IsReadWriteAllowed())
{
// We'll create a place to store our data received from the host.
SwitchOutputReport JoystickOutputData;
// We'll then take in that data, setting it up in our storage.
Endpoint_Read_Stream_LE(&JoystickOutputData, sizeof(JoystickOutputData), NULL);
// At this point, we can react to this data.
// However, since we're not doing anything with this data, we abandon it.
}
// Regardless of whether we reacted to the data, we acknowledge an OUT packet on this endpoint.
Endpoint_ClearOUT();
}
}
/* Select the Joystick Report Endpoint */
Endpoint_SelectEndpoint(JOYSTICK_IN_EPADDR);
/* Check to see if the host is ready for another packet */
if (Endpoint_IsINReady()) {
/* Write Joystick Report Data */
Endpoint_Write_Stream_LE(reportData, reportSize, NULL);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearIN();
/* Store last report bytes for comparison later */
memcpy(lastReportBytes, reportData, reportSize);
/* Clear the report data afterwards */
memset(reportData, 0, reportSize);
}
}
void sendReport(void *data, uint8_t size) {
reportData = data;
reportSize = size;
if (memcmp(lastReportBytes, reportData, reportSize) != 0)
HID_Task();
USB_USBTask();
}