-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathcontrol.c
184 lines (160 loc) · 3.63 KB
/
control.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
#include "defs.h"
#include "usb.h"
#include "timers.h"
static const BYTE deviceDescriptor[] = { 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
0xFE, 0x13, 0x01, 0x52, 0x10, 0x01, 0x00, 0x00, 0x00, 0x01 };
static const BYTE configDescriptor[] = { 0x09, 0x02, sizeof(configDescriptor) & 0xFF, sizeof(configDescriptor) >> 8, 0x02, 0x01, 0x00, 0x80, 0x4B,
0x09, 0x04, 0x00, 0x00, 0x03, 0x08, 0x06, 0x50, 0x00,
0x07, 0x05, 0x81, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x83, 0x03, 0x08, 0x00, 0x00,
0x09, 0x04, 0x01, 0x00, 0x02, 0x03, 0x01, 0x01, 0x00,
0x09, 0x21, 0x01, 0x01, 0x00, 0x01, 0x22,
sizeof(HIDreportDescriptor) & 0xFF,
sizeof(HIDreportDescriptor) >> 8,
0x07, 0x05, 0x83, 0x03, 0x08, 0x00, 0x01,
//This is a dummy endpoint to make the descriptor != 0x40, because the controller is stupid.
0x07, 0x05, 0x04, 0x03, 0x08, 0x00, 0x01 };
static const BYTE HIDreportDescriptor[] = { 0x05, 0x01, 0x09, 0x06, 0xA1, 0x01, 0x05, 0x07,
0x19, 0xE0, 0x29, 0xE7, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01,
0x95, 0x08, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x01,
0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05,
0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x01, 0x95, 0x06,
0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, 0x07, 0x19, 0x00,
0x29, 0x65, 0x81, 0x00, 0xC0 };
static const BYTE deviceQualifierDescriptor[] = { 0x0A, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00 };
void EP0ACK()
{
EP0CS = bmEP0ACK;
}
static BYTE SetAddress()
{
BYTE ret = FALSE;
if (wValue < 0x7F)
{
EP0ACK();
ret = TRUE;
}
return ret;
}
static BYTE GetDescriptor()
{
BYTE type = (wValue >> 8) & 0xFF;
BYTE i, total;
BYTE ret = FALSE;
switch (type)
{
case 0x01:
{
for (i = 0; i < 0x12; i++)
{
EP0.fifo = deviceDescriptor[i];
}
SendControlResponse(wLength < 0x12 ? wLength : 0x12);
ret = TRUE;
break;
}
case 0x02:
{
total = wLength < sizeof(configDescriptor) ? wLength : sizeof(configDescriptor);
for (i = 0; i < total; i++)
{
EP0.fifo = configDescriptor[i];
}
SendControlResponse(total);
ret = TRUE;
break;
}
case 0x06:
{
for (i = 0; i < sizeof(deviceQualifierDescriptor); i++)
{
EP0.fifo = deviceQualifierDescriptor[i];
}
SendControlResponse(wLength < sizeof(deviceQualifierDescriptor) ? wLength : sizeof(deviceQualifierDescriptor));
ret = TRUE;
break;
}
case 0x22:
{
for (i = 0; i < sizeof(HIDreportDescriptor); i++)
{
EP0.fifo = HIDreportDescriptor[i];
}
SendControlResponse(wLength < sizeof(HIDreportDescriptor) ? wLength : sizeof(HIDreportDescriptor));
ret = TRUE;
break;
}
default:
{
break;
}
}
return ret;
}
static BYTE SetConfiguration()
{
BYTE ret = FALSE;
if (wValue <= 1)
{
EP0ACK();
ret = TRUE;
}
return ret;
}
BYTE HandleStandardRequest()
{
switch(bRequest)
{
case 0x05:
{
return SetAddress();
}
case 0x06:
{
return GetDescriptor();
}
case 0x09:
{
return SetConfiguration();
}
default:
{
return FALSE;
}
}
}
static BYTE GetMaxLUN()
{
EP0.fifo = 0x00;
SendControlResponse(wLength < 0x01 ? wLength : 0x01);
return TRUE;
}
BYTE HandleClassRequest()
{
switch(bRequest)
{
case 0x09:
{
EP0CS = 0x05;
return TRUE;
}
case 0x0A:
{
EP0ACK();
return TRUE;
}
case 0xFE:
{
return GetMaxLUN();
}
default:
{
return FALSE;
}
}
}
BYTE HandleVendorRequest()
{
return FALSE;
}