Fix LUFA descriptor of Extra for Windows
[tmk_keyboard.git] / protocol / lufa / lufa.c
CommitLineData
dd93d291 1/*
2 * Copyright 2012 Jun Wako <wakojun@gmail.com>
3969ec09 3 * This file is based on:
4 * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
5 * LUFA-120219/Demos/Device/Lowlevel/GenericHID
dd93d291 6 */
7
8/*
9 LUFA Library
10 Copyright (C) Dean Camera, 2012.
11
12 dean [at] fourwalledcubicle [dot] com
13 www.lufa-lib.org
14*/
15
16/*
17 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
18 Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
19
20 Permission to use, copy, modify, distribute, and sell this
21 software and its documentation for any purpose is hereby granted
22 without fee, provided that the above copyright notice appear in
23 all copies and that both that the copyright notice and this
24 permission notice and warranty disclaimer appear in supporting
25 documentation, and that the name of the author not be used in
26 advertising or publicity pertaining to distribution of the
27 software without specific, written prior permission.
28
29 The author disclaim all warranties with regard to this
30 software, including all implied warranties of merchantability
31 and fitness. In no event shall the author be liable for any
32 special, indirect or consequential damages or any damages
33 whatsoever resulting from loss of use, data or profits, whether
34 in an action of contract, negligence or other tortious action,
35 arising out of or in connection with the use or performance of
36 this software.
37*/
38
39#include "report.h"
40#include "host.h"
41#include "host_driver.h"
42#include "keyboard.h"
3969ec09 43#include "sendchar.h"
44#include "debug.h"
effa5914 45
46#include "descriptor.h"
dd93d291 47#include "lufa.h"
48
a112f361 49static uint8_t idle_duration = 0;
50static uint8_t protocol_report = 1;
dd93d291 51static uint8_t keyboard_led_stats = 0;
effa5914 52
effa5914 53static report_keyboard_t keyboard_report_sent;
a112f361 54
dd93d291 55
56/* Host driver */
57static uint8_t keyboard_leds(void);
58static void send_keyboard(report_keyboard_t *report);
59static void send_mouse(report_mouse_t *report);
60static void send_system(uint16_t data);
61static void send_consumer(uint16_t data);
62static host_driver_t lufa_driver = {
63 keyboard_leds,
64 send_keyboard,
65 send_mouse,
66 send_system,
67 send_consumer
68};
69
70
effa5914 71static void SetupHardware(void);
a9a3610d 72static void Console_HID_Task(void);
effa5914 73
dd93d291 74int main(void)
75{
76 SetupHardware();
77 sei();
78
3969ec09 79 print_enable = true;
80 debug_enable = true;
81 debug_matrix = true;
82 debug_keyboard = true;
83 debug_mouse = true;
84
a112f361 85 // TODO: can't print here
86 debug("LUFA init\n");
3969ec09 87
dd93d291 88 keyboard_init();
89 host_set_driver(&lufa_driver);
90 while (1) {
91 keyboard_proc();
effa5914 92
a9a3610d 93 Console_HID_Task();
dd93d291 94 USB_USBTask();
95 }
96}
97
dd93d291 98void SetupHardware(void)
99{
100 /* Disable watchdog if enabled by bootloader/fuses */
101 MCUSR &= ~(1 << WDRF);
102 wdt_disable();
103
104 /* Disable clock division */
105 clock_prescale_set(clock_div_1);
106
107 USB_Init();
108}
109
a9a3610d 110static void Console_HID_Task(void)
effa5914 111{
112 /* Device must be connected and configured for the task to run */
113 if (USB_DeviceState != DEVICE_STATE_Configured)
114 return;
115
116 // TODO: impl receivechar()/recvchar()
3d81d522 117 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
effa5914 118
119 /* Check to see if a packet has been sent from the host */
120 if (Endpoint_IsOUTReceived())
121 {
122 /* Check to see if the packet contains data */
123 if (Endpoint_IsReadWriteAllowed())
124 {
125 /* Create a temporary buffer to hold the read in report from the host */
3d81d522 126 uint8_t ConsoleData[CONSOLE_EPSIZE];
effa5914 127
a9a3610d 128 /* Read Console Report Data */
129 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
effa5914 130
a9a3610d 131 /* Process Console Report Data */
132 //ProcessConsoleHIDReport(ConsoleData);
effa5914 133 }
134
135 /* Finalize the stream transfer to send the last packet */
136 Endpoint_ClearOUT();
137 }
138
139 /* IN packet */
3d81d522 140 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
effa5914 141 // send IN packet
142 if (Endpoint_IsINReady())
143 Endpoint_ClearIN();
144}
145
146
147/*******************************************************************************
148 * USB Events
149 ******************************************************************************/
dd93d291 150/** Event handler for the USB_Connect event. */
151void EVENT_USB_Device_Connect(void)
152{
153}
154
155/** Event handler for the USB_Disconnect event. */
156void EVENT_USB_Device_Disconnect(void)
157{
158}
159
160/** Event handler for the USB_ConfigurationChanged event.
161 * This is fired when the host sets the current configuration of the USB device after enumeration.
162 */
163void EVENT_USB_Device_ConfigurationChanged(void)
164{
165 bool ConfigSuccess = true;
166
167 /* Setup Keyboard HID Report Endpoints */
168 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
3d81d522 169 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
dd93d291 170
f2ebac10 171#ifdef MOUSE_ENABLE
dd93d291 172 /* Setup Mouse HID Report Endpoint */
173 ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
3d81d522 174 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
f2ebac10 175#endif
176
177#ifdef EXTRAKEY_ENABLE
178 /* Setup Extra HID Report Endpoint */
179 ConfigSuccess &= Endpoint_ConfigureEndpoint(EXTRA_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
180 EXTRA_EPSIZE, ENDPOINT_BANK_SINGLE);
181#endif
3969ec09 182
a9a3610d 183 /* Setup Console HID Report Endpoints */
3d81d522 184 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
185 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
186 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
187 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
dd93d291 188}
189
a9a3610d 190/*
191Appendix G: HID Request Support Requirements
192
193The following table enumerates the requests that need to be supported by various types of HID class devices.
194
195Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
196------------------------------------------------------------------------------------------
197Boot Mouse Required Optional Optional Optional Required Required
198Non-Boot Mouse Required Optional Optional Optional Optional Optional
199Boot Keyboard Required Optional Required Required Required Required
200Non-Boot Keybrd Required Optional Required Required Optional Optional
201Other Device Required Optional Optional Optional Optional Optional
202*/
dd93d291 203/** Event handler for the USB_ControlRequest event.
204 * This is fired before passing along unhandled control requests to the library for processing internally.
205 */
206void EVENT_USB_Device_ControlRequest(void)
207{
3969ec09 208 uint8_t* ReportData = NULL;
209 uint8_t ReportSize = 0;
dd93d291 210
211 /* Handle HID Class specific requests */
212 switch (USB_ControlRequest.bRequest)
213 {
214 case HID_REQ_GetReport:
215 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
216 {
217 Endpoint_ClearSETUP();
218
3969ec09 219 // Interface
220 switch (USB_ControlRequest.wIndex) {
effa5914 221 case KEYBOARD_INTERFACE:
222 // TODO: test/check
dd93d291 223 ReportData = (uint8_t*)&keyboard_report_sent;
224 ReportSize = sizeof(keyboard_report_sent);
3969ec09 225 break;
dd93d291 226 }
227
228 /* Write the report data to the control endpoint */
229 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
230 Endpoint_ClearOUT();
231 }
232
233 break;
234 case HID_REQ_SetReport:
235 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
236 {
dd93d291 237
3969ec09 238 // Interface
239 switch (USB_ControlRequest.wIndex) {
effa5914 240 case KEYBOARD_INTERFACE:
a112f361 241 Endpoint_ClearSETUP();
242
243 while (!(Endpoint_IsOUTReceived())) {
244 if (USB_DeviceState == DEVICE_STATE_Unattached)
245 return;
246 }
3969ec09 247 keyboard_led_stats = Endpoint_Read_8();
a112f361 248
249 Endpoint_ClearOUT();
250 Endpoint_ClearStatusStage();
f2ebac10 251 break;
3969ec09 252 }
dd93d291 253
a112f361 254 }
255
256 break;
257
258 case HID_REQ_GetProtocol:
259 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
260 {
261 Endpoint_ClearSETUP();
262 while (!(Endpoint_IsINReady()));
263 Endpoint_Write_8(protocol_report);
264 Endpoint_ClearIN();
265 Endpoint_ClearStatusStage();
266 }
267
268 break;
269 case HID_REQ_SetProtocol:
270 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
271 {
272 Endpoint_ClearSETUP();
273 Endpoint_ClearStatusStage();
274
275 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
276 }
277
278 break;
279 case HID_REQ_SetIdle:
280 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
281 {
282 Endpoint_ClearSETUP();
283 Endpoint_ClearStatusStage();
284
285 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
286 }
287
288 break;
289 case HID_REQ_GetIdle:
290 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
291 {
292 Endpoint_ClearSETUP();
293 while (!(Endpoint_IsINReady()));
294 Endpoint_Write_8(idle_duration);
295 Endpoint_ClearIN();
dd93d291 296 Endpoint_ClearStatusStage();
297 }
298
299 break;
300 }
301}
302
dd93d291 303/*******************************************************************************
304 * Host driver
305 ******************************************************************************/
306static uint8_t keyboard_leds(void)
307{
308 return keyboard_led_stats;
309}
310
311static void send_keyboard(report_keyboard_t *report)
312{
313 // TODO: handle NKRO report
314 /* Select the Keyboard Report Endpoint */
315 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
316
317 /* Check if Keyboard Endpoint Ready for Read/Write */
318 if (Endpoint_IsReadWriteAllowed())
319 {
320 /* Write Keyboard Report Data */
321 Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
322
323 /* Finalize the stream transfer to send the last packet */
324 Endpoint_ClearIN();
325 }
326 keyboard_report_sent = *report;
327}
328
329static void send_mouse(report_mouse_t *report)
330{
f2ebac10 331#ifdef MOUSE_ENABLE
dd93d291 332 /* Select the Mouse Report Endpoint */
333 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
334
335 /* Check if Mouse Endpoint Ready for Read/Write */
336 if (Endpoint_IsReadWriteAllowed())
337 {
338 /* Write Mouse Report Data */
3d81d522 339 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
dd93d291 340
341 /* Finalize the stream transfer to send the last packet */
342 Endpoint_ClearIN();
343 }
f2ebac10 344#endif
dd93d291 345}
346
347static void send_system(uint16_t data)
348{
a112f361 349 report_extra_t r = {
350 .report_id = REPORT_ID_SYSTEM,
351 .usage = data
352 };
3d81d522 353 Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
354 if (Endpoint_IsReadWriteAllowed()) {
3d81d522 355 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
356 Endpoint_ClearIN();
357 }
dd93d291 358}
359
360static void send_consumer(uint16_t data)
361{
a112f361 362 report_extra_t r = {
363 .report_id = REPORT_ID_CONSUMER,
364 .usage = data
365 };
3d81d522 366 Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
367 if (Endpoint_IsReadWriteAllowed()) {
3d81d522 368 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
369 Endpoint_ClearIN();
370 }
dd93d291 371}
3969ec09 372
373
374/*******************************************************************************
375 * sendchar
376 ******************************************************************************/
377int8_t sendchar(uint8_t c)
378{
379 if (USB_DeviceState != DEVICE_STATE_Configured)
380 return -1;
381
3d81d522 382 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
3969ec09 383
384 uint8_t timeout = 10;
385 uint16_t prevFN = USB_Device_GetFrameNumber();
386 while (!Endpoint_IsINReady()) {
387 switch (USB_DeviceState) {
388 case DEVICE_STATE_Unattached:
389 case DEVICE_STATE_Suspended:
390 return -1;
391 }
392 if (Endpoint_IsStalled())
393 return -1;
3969ec09 394 if (prevFN != USB_Device_GetFrameNumber()) {
395 if (!(timeout--))
396 return -1;
397 prevFN = USB_Device_GetFrameNumber();
398 }
399 }
400
401 Endpoint_Write_8(c);
402
403 // send when packet is full
404 if (!Endpoint_IsReadWriteAllowed())
405 Endpoint_ClearIN();
406
407 return 0;
408}
Imprint / Impressum