-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathKeyboardReporter.cpp
238 lines (204 loc) · 8.49 KB
/
KeyboardReporter.cpp
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
/*
Copyright (c) 2014-2015 NicoHood
See the readme for credit to other people.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "KeyboardReporter.h"
static const uint8_t _hidReportDescriptorKeyboard__[] PROGMEM = {
// Keyboard
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) 47 */
0x09, 0x06, /* USAGE (Keyboard) */
0xa1, 0x01, /* COLLECTION (Application) */
0x05, 0x07, /* USAGE_PAGE (Keyboard) */
/* Keyboard Modifiers (shift, alt, ...) */
0x19, 0xe0, /* USAGE_MINIMUM (Keyboard LeftControl) */
0x29, 0xe7, /* USAGE_MAXIMUM (Keyboard Right GUI) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x08, /* REPORT_COUNT (8) */
0x81, 0x02, /* INPUT (Data,Var,Abs) */
/* Reserved byte, used for consumer reports, only works with linux */
0x05, 0x0C, /* Usage Page (Consumer) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
0x19, 0x00, /* USAGE_MINIMUM (0) */
0x29, 0xFF, /* USAGE_MAXIMUM (255) */
0x81, 0x00, /* INPUT (Data,Ary,Abs) */
/* 5 LEDs for num lock etc, 3 left for advanced, custom usage */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */
0x29, 0x08, /* USAGE_MAXIMUM (Kana + 3 custom)*/
0x95, 0x08, /* REPORT_COUNT (8) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
/* 6 Keyboard keys */
0x05, 0x07, /* USAGE_PAGE (Keyboard) */
0x95, 0x06, /* REPORT_COUNT (6) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x26, 0xE7, 0x00, /* LOGICAL_MAXIMUM (231) */
0x19, 0x00, /* USAGE_MINIMUM (Reserved (no event indicated)) */
0x29, 0xE7, /* USAGE_MAXIMUM (Keyboard Right GUI) */
0x81, 0x00, /* INPUT (Data,Ary,Abs) */
/* End */
0xc0 /* END_COLLECTION */
};
static const uint8_t _hidReportDescriptorKeyboard[] PROGMEM = {
0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x85, 0x01, 0x05, 0x07, 0x15, 0x00,
0x25, 0x01, 0x19, 0xe0, 0x29, 0xe7, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02,
0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02,
0x95, 0x01, 0x75, 0x03, 0x91, 0x03, 0x95, 0x08, 0x75, 0x01, 0x15, 0x00,
0x25, 0x01, 0x06, 0x00, 0xff, 0x09, 0x03, 0x81, 0x03, 0x95, 0x06, 0x75,
0x08, 0x15, 0x00, 0x25, 0x65, 0x05, 0x07, 0x19, 0x00, 0x29, 0x65, 0x81,
0x00, 0x95, 0x01, 0x75, 0x01, 0x15, 0x00, 0x25, 0x01, 0x05, 0x0c, 0x09,
0xb8, 0x81, 0x02, 0x95, 0x01, 0x75, 0x01, 0x06, 0x00, 0xff, 0x09, 0x03,
0x81, 0x02, 0x95, 0x01, 0x75, 0x06, 0x81, 0x03, 0x06, 0x02, 0xff, 0x09,
0x55, 0x85, 0x55, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x40,
0xb1, 0xa2, 0xc0, 0x06, 0x00, 0xff, 0x09, 0x14, 0xa1, 0x01, 0x85, 0x90,
0x05, 0x84, 0x75, 0x01, 0x95, 0x03, 0x15, 0x00, 0x25, 0x01, 0x09, 0x61,
0x05, 0x85, 0x09, 0x44, 0x09, 0x46, 0x81, 0x02, 0x95, 0x05, 0x81, 0x01,
0x75, 0x08, 0x95, 0x01, 0x15, 0x00, 0x26, 0xff, 0x00, 0x09, 0x65, 0x81,
0x02, 0xc0 // apple magic keyboard descriper.
};
KeyboardReporter_::KeyboardReporter_(void) : PluggableUSBModule(1, 1, epType), protocol(HID_REPORT_PROTOCOL), idle(1), leds(0), featureReport(NULL), featureLength(0)
{
epType[0] = EP_TYPE_INTERRUPT_IN;
PluggableUSB().plug(this);
}
int KeyboardReporter_::getInterface(uint8_t* interfaceCount)
{
*interfaceCount += 1; // uses 1
HIDDescriptor hidInterface = {
D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_BOOT_INTERFACE, HID_PROTOCOL_KEYBOARD),
D_HIDREPORT(sizeof(_hidReportDescriptorKeyboard)),
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)
};
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
}
int KeyboardReporter_::getDescriptor(USBSetup& setup)
{
// In a HID Class Descriptor wIndex cointains the interface number
if (setup.wIndex != pluggedInterface) { return 0; }
// Check if this is a HID Class Descriptor request
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
if (setup.wValueH == HID_HID_DESCRIPTOR_TYPE) {
// Apple UEFI and USBCV wants it
HIDDescDescriptor desc = D_HIDREPORT(sizeof(_hidReportDescriptorKeyboard));
return USB_SendControl(0, &desc, sizeof(desc));
} else if (setup.wValueH == HID_REPORT_DESCRIPTOR_TYPE) {
// Reset the protocol on reenumeration. Normally the host should not assume the state of the protocol
// due to the USB specs, but Windows and Linux just assumes its in report mode.
protocol = HID_REPORT_PROTOCOL;
return USB_SendControl(TRANSFER_PGM, _hidReportDescriptorKeyboard, sizeof(_hidReportDescriptorKeyboard));
}
return 0;
}
bool KeyboardReporter_::setup(USBSetup& setup)
{
if (pluggedInterface != setup.wIndex) {
return false;
}
uint8_t request = setup.bRequest;
uint8_t requestType = setup.bmRequestType;
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
{
if (request == HID_GET_REPORT) {
// TODO: HID_GetReport();
return true;
}
if (request == HID_GET_PROTOCOL) {
// TODO improve
#ifdef __AVR__
UEDATX = protocol;
#endif
return true;
}
if (request == HID_GET_IDLE) {
// TODO improve
#ifdef __AVR__
UEDATX = idle;
#endif
return true;
}
}
if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
{
if (request == HID_SET_PROTOCOL) {
protocol = setup.wValueL;
return true;
}
if (request == HID_SET_IDLE) {
idle = setup.wValueH;
return true;
}
if (request == HID_SET_REPORT)
{
// Check if data has the correct length afterwards
int length = setup.wLength;
// Feature (set feature report)
if(setup.wValueH == HID_REPORT_TYPE_FEATURE){
// No need to check for negative featureLength values,
// except the host tries to send more then 32k bytes.
// We dont have that much ram anyways.
if (length == featureLength) {
USB_RecvControl(featureReport, featureLength);
// Block until data is read (make length negative)
disableFeatureReport();
return true;
}
// TODO fake clear data?
}
// Output (set led states)
else if(setup.wValueH == HID_REPORT_TYPE_OUTPUT){
if(length == sizeof(leds)){
USB_RecvControl(&leds, length);
return true;
}
}
// Input (set HID report)
else if(setup.wValueH == HID_REPORT_TYPE_INPUT)
{
if(length == sizeof(_keyReport)){
USB_RecvControl(&_keyReport, length);
return true;
}
}
}
}
return false;
}
uint8_t KeyboardReporter_::getLeds(void){
return leds;
}
uint8_t KeyboardReporter_::getProtocol(void){
return protocol;
}
int KeyboardReporter_::send(void){
return USB_Send(pluggedEndpoint | TRANSFER_RELEASE, &_keyReport, sizeof(_keyReport));
}
int KeyboardReporter_::sendReport(uint8_t *buf, size_t len) {
return USB_Send(pluggedEndpoint | TRANSFER_RELEASE, buf, len);
}
void KeyboardReporter_::wakeupHost(void){
#ifdef __AVR__
USBDevice.wakeupHost();
#endif
}
KeyboardReporter_ KeyboardReporter;