Add CONSOLE_ENABLE build option to LUFA.
[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);
89470299 72static void Console_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
89470299 93#if !defined(INTERRUPT_CONTROL_ENDPOINT)
dd93d291 94 USB_USBTask();
89470299 95#endif
dd93d291 96 }
97}
98
dd93d291 99void SetupHardware(void)
100{
101 /* Disable watchdog if enabled by bootloader/fuses */
102 MCUSR &= ~(1 << WDRF);
103 wdt_disable();
104
105 /* Disable clock division */
106 clock_prescale_set(clock_div_1);
107
108 USB_Init();
89470299 109
110 // for Console_Task
111 USB_Device_EnableSOFEvents();
dd93d291 112}
113
e075361b 114#ifdef CONSOLE_ENABLE
89470299 115static void Console_Task(void)
effa5914 116{
89470299 117 /* Device must be connected and configured for the task to run */
118 if (USB_DeviceState != DEVICE_STATE_Configured)
119 return;
120
121 uint8_t ep = Endpoint_GetCurrentEndpoint();
122
123#if 0
124 // TODO: impl receivechar()/recvchar()
125 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
126
127 /* Check to see if a packet has been sent from the host */
128 if (Endpoint_IsOUTReceived())
129 {
130 /* Check to see if the packet contains data */
131 if (Endpoint_IsReadWriteAllowed())
132 {
133 /* Create a temporary buffer to hold the read in report from the host */
134 uint8_t ConsoleData[CONSOLE_EPSIZE];
135
136 /* Read Console Report Data */
137 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
138
139 /* Process Console Report Data */
140 //ProcessConsoleHIDReport(ConsoleData);
141 }
142
143 /* Finalize the stream transfer to send the last packet */
144 Endpoint_ClearOUT();
145 }
146#endif
147
148 /* IN packet */
149 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
e075361b 150
151 // fill empty bank
152 while (Endpoint_IsReadWriteAllowed())
153 Endpoint_Write_8(0);
154
89470299 155 // flash senchar packet
156 if (Endpoint_IsINReady()) {
157 Endpoint_ClearIN();
158 }
159
160 Endpoint_SelectEndpoint(ep);
effa5914 161}
e075361b 162#else
163static void Console_Task(void)
164{
165}
166#endif
effa5914 167
168
169/*******************************************************************************
170 * USB Events
171 ******************************************************************************/
dd93d291 172/** Event handler for the USB_Connect event. */
173void EVENT_USB_Device_Connect(void)
174{
175}
176
177/** Event handler for the USB_Disconnect event. */
178void EVENT_USB_Device_Disconnect(void)
179{
180}
181
89470299 182void EVENT_USB_Device_StartOfFrame(void)
183{
e075361b 184 Console_Task();
89470299 185}
186
dd93d291 187/** Event handler for the USB_ConfigurationChanged event.
188 * This is fired when the host sets the current configuration of the USB device after enumeration.
189 */
190void EVENT_USB_Device_ConfigurationChanged(void)
191{
192 bool ConfigSuccess = true;
193
194 /* Setup Keyboard HID Report Endpoints */
195 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
3d81d522 196 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
dd93d291 197
f2ebac10 198#ifdef MOUSE_ENABLE
dd93d291 199 /* Setup Mouse HID Report Endpoint */
200 ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
3d81d522 201 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
f2ebac10 202#endif
203
204#ifdef EXTRAKEY_ENABLE
205 /* Setup Extra HID Report Endpoint */
e075361b 206 ConfigSuccess &= Endpoint_ConfigureEndpoint(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
207 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
f2ebac10 208#endif
3969ec09 209
e075361b 210#ifdef CONSOLE_ENABLE
a9a3610d 211 /* Setup Console HID Report Endpoints */
3d81d522 212 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
89470299 213 CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
3d81d522 214 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
215 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
e075361b 216#endif
dd93d291 217}
218
a9a3610d 219/*
220Appendix G: HID Request Support Requirements
221
222The following table enumerates the requests that need to be supported by various types of HID class devices.
223
224Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
225------------------------------------------------------------------------------------------
226Boot Mouse Required Optional Optional Optional Required Required
227Non-Boot Mouse Required Optional Optional Optional Optional Optional
228Boot Keyboard Required Optional Required Required Required Required
229Non-Boot Keybrd Required Optional Required Required Optional Optional
230Other Device Required Optional Optional Optional Optional Optional
231*/
dd93d291 232/** Event handler for the USB_ControlRequest event.
233 * This is fired before passing along unhandled control requests to the library for processing internally.
234 */
235void EVENT_USB_Device_ControlRequest(void)
236{
3969ec09 237 uint8_t* ReportData = NULL;
238 uint8_t ReportSize = 0;
dd93d291 239
240 /* Handle HID Class specific requests */
241 switch (USB_ControlRequest.bRequest)
242 {
243 case HID_REQ_GetReport:
244 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
245 {
246 Endpoint_ClearSETUP();
247
3969ec09 248 // Interface
249 switch (USB_ControlRequest.wIndex) {
effa5914 250 case KEYBOARD_INTERFACE:
251 // TODO: test/check
dd93d291 252 ReportData = (uint8_t*)&keyboard_report_sent;
253 ReportSize = sizeof(keyboard_report_sent);
3969ec09 254 break;
dd93d291 255 }
256
257 /* Write the report data to the control endpoint */
258 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
259 Endpoint_ClearOUT();
260 }
261
262 break;
263 case HID_REQ_SetReport:
264 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
265 {
dd93d291 266
3969ec09 267 // Interface
268 switch (USB_ControlRequest.wIndex) {
effa5914 269 case KEYBOARD_INTERFACE:
a112f361 270 Endpoint_ClearSETUP();
271
272 while (!(Endpoint_IsOUTReceived())) {
273 if (USB_DeviceState == DEVICE_STATE_Unattached)
274 return;
275 }
3969ec09 276 keyboard_led_stats = Endpoint_Read_8();
a112f361 277
278 Endpoint_ClearOUT();
279 Endpoint_ClearStatusStage();
f2ebac10 280 break;
3969ec09 281 }
dd93d291 282
a112f361 283 }
284
285 break;
286
287 case HID_REQ_GetProtocol:
288 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
289 {
290 Endpoint_ClearSETUP();
291 while (!(Endpoint_IsINReady()));
292 Endpoint_Write_8(protocol_report);
293 Endpoint_ClearIN();
294 Endpoint_ClearStatusStage();
295 }
296
297 break;
298 case HID_REQ_SetProtocol:
299 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
300 {
301 Endpoint_ClearSETUP();
302 Endpoint_ClearStatusStage();
303
304 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
305 }
306
307 break;
308 case HID_REQ_SetIdle:
309 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
310 {
311 Endpoint_ClearSETUP();
312 Endpoint_ClearStatusStage();
313
314 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
315 }
316
317 break;
318 case HID_REQ_GetIdle:
319 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
320 {
321 Endpoint_ClearSETUP();
322 while (!(Endpoint_IsINReady()));
323 Endpoint_Write_8(idle_duration);
324 Endpoint_ClearIN();
dd93d291 325 Endpoint_ClearStatusStage();
326 }
327
328 break;
329 }
330}
331
dd93d291 332/*******************************************************************************
333 * Host driver
334 ******************************************************************************/
335static uint8_t keyboard_leds(void)
336{
337 return keyboard_led_stats;
338}
339
340static void send_keyboard(report_keyboard_t *report)
341{
342 // TODO: handle NKRO report
343 /* Select the Keyboard Report Endpoint */
344 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
345
346 /* Check if Keyboard Endpoint Ready for Read/Write */
347 if (Endpoint_IsReadWriteAllowed())
348 {
349 /* Write Keyboard Report Data */
350 Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
351
352 /* Finalize the stream transfer to send the last packet */
353 Endpoint_ClearIN();
354 }
355 keyboard_report_sent = *report;
356}
357
358static void send_mouse(report_mouse_t *report)
359{
f2ebac10 360#ifdef MOUSE_ENABLE
dd93d291 361 /* Select the Mouse Report Endpoint */
362 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
363
364 /* Check if Mouse Endpoint Ready for Read/Write */
365 if (Endpoint_IsReadWriteAllowed())
366 {
367 /* Write Mouse Report Data */
3d81d522 368 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
dd93d291 369
370 /* Finalize the stream transfer to send the last packet */
371 Endpoint_ClearIN();
372 }
f2ebac10 373#endif
dd93d291 374}
375
376static void send_system(uint16_t data)
377{
a112f361 378 report_extra_t r = {
379 .report_id = REPORT_ID_SYSTEM,
380 .usage = data
381 };
e075361b 382 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
3d81d522 383 if (Endpoint_IsReadWriteAllowed()) {
3d81d522 384 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
385 Endpoint_ClearIN();
386 }
dd93d291 387}
388
389static void send_consumer(uint16_t data)
390{
a112f361 391 report_extra_t r = {
392 .report_id = REPORT_ID_CONSUMER,
393 .usage = data
394 };
e075361b 395 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
3d81d522 396 if (Endpoint_IsReadWriteAllowed()) {
3d81d522 397 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
398 Endpoint_ClearIN();
399 }
dd93d291 400}
3969ec09 401
402
403/*******************************************************************************
404 * sendchar
405 ******************************************************************************/
e075361b 406#ifdef CONSOLE_ENABLE
407#define SEND_TIMEOUT 5
3969ec09 408int8_t sendchar(uint8_t c)
409{
410 if (USB_DeviceState != DEVICE_STATE_Configured)
411 return -1;
412
e075361b 413 uint8_t ep = Endpoint_GetCurrentEndpoint();
3d81d522 414 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
3969ec09 415
89470299 416 uint8_t timeout = SEND_TIMEOUT;
3969ec09 417 uint16_t prevFN = USB_Device_GetFrameNumber();
89470299 418 while (!Endpoint_IsReadWriteAllowed()) {
3969ec09 419 switch (USB_DeviceState) {
420 case DEVICE_STATE_Unattached:
421 case DEVICE_STATE_Suspended:
422 return -1;
423 }
e075361b 424 if (Endpoint_IsStalled()) {
425 Endpoint_SelectEndpoint(ep);
3969ec09 426 return -1;
e075361b 427 }
3969ec09 428 if (prevFN != USB_Device_GetFrameNumber()) {
e075361b 429 if (!(timeout--)) {
430 Endpoint_SelectEndpoint(ep);
3969ec09 431 return -1;
e075361b 432 }
3969ec09 433 prevFN = USB_Device_GetFrameNumber();
434 }
435 }
436
437 Endpoint_Write_8(c);
438
89470299 439 // send when bank is full
3969ec09 440 if (!Endpoint_IsReadWriteAllowed())
441 Endpoint_ClearIN();
442
e075361b 443 Endpoint_SelectEndpoint(ep);
444 return 0;
445}
446#else
447int8_t sendchar(uint8_t c)
448{
3969ec09 449 return 0;
450}
e075361b 451#endif
Imprint / Impressum