Fix USB Descriptors
[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"
f0c5f1b9 43#include "action.h"
f0c5f1b9 44#include "led.h"
3969ec09 45#include "sendchar.h"
46#include "debug.h"
f0c5f1b9 47#ifdef SLEEP_LED_ENABLE
48#include "sleep_led.h"
49#endif
afc3148e 50#include "suspend.h"
effa5914 51
52#include "descriptor.h"
dd93d291 53#include "lufa.h"
54
a112f361 55static uint8_t idle_duration = 0;
56static uint8_t protocol_report = 1;
dd93d291 57static uint8_t keyboard_led_stats = 0;
effa5914 58
effa5914 59static report_keyboard_t keyboard_report_sent;
a112f361 60
dd93d291 61
62/* Host driver */
63static uint8_t keyboard_leds(void);
64static void send_keyboard(report_keyboard_t *report);
65static void send_mouse(report_mouse_t *report);
66static void send_system(uint16_t data);
67static void send_consumer(uint16_t data);
c5060ea8 68host_driver_t lufa_driver = {
dd93d291 69 keyboard_leds,
70 send_keyboard,
71 send_mouse,
72 send_system,
73 send_consumer
74};
75
76
c5060ea8 77/*******************************************************************************
78 * Console
79 ******************************************************************************/
e075361b 80#ifdef CONSOLE_ENABLE
89470299 81static void Console_Task(void)
effa5914 82{
89470299 83 /* Device must be connected and configured for the task to run */
84 if (USB_DeviceState != DEVICE_STATE_Configured)
45ea1874 85 return;
89470299 86
87 uint8_t ep = Endpoint_GetCurrentEndpoint();
88
89#if 0
90 // TODO: impl receivechar()/recvchar()
91 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
92
93 /* Check to see if a packet has been sent from the host */
94 if (Endpoint_IsOUTReceived())
95 {
96 /* Check to see if the packet contains data */
97 if (Endpoint_IsReadWriteAllowed())
98 {
99 /* Create a temporary buffer to hold the read in report from the host */
100 uint8_t ConsoleData[CONSOLE_EPSIZE];
101
102 /* Read Console Report Data */
103 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
104
105 /* Process Console Report Data */
106 //ProcessConsoleHIDReport(ConsoleData);
107 }
108
109 /* Finalize the stream transfer to send the last packet */
110 Endpoint_ClearOUT();
111 }
112#endif
113
114 /* IN packet */
115 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
45ea1874 116 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
117 Endpoint_SelectEndpoint(ep);
118 return;
119 }
e075361b 120
121 // fill empty bank
122 while (Endpoint_IsReadWriteAllowed())
123 Endpoint_Write_8(0);
124
89470299 125 // flash senchar packet
126 if (Endpoint_IsINReady()) {
127 Endpoint_ClearIN();
128 }
129
130 Endpoint_SelectEndpoint(ep);
effa5914 131}
e075361b 132#else
133static void Console_Task(void)
134{
135}
136#endif
effa5914 137
138
139/*******************************************************************************
140 * USB Events
141 ******************************************************************************/
42f6ff08 142/*
143 * Event Order of Plug in:
144 * 0) EVENT_USB_Device_Connect
145 * 1) EVENT_USB_Device_Suspend
146 * 2) EVENT_USB_Device_Reset
147 * 3) EVENT_USB_Device_Wake
148*/
dd93d291 149void EVENT_USB_Device_Connect(void)
150{
f0c5f1b9 151 led_set(0x1f); // all on
dd93d291 152}
153
dd93d291 154void EVENT_USB_Device_Disconnect(void)
155{
156}
157
c8cfde01 158void EVENT_USB_Device_Reset(void)
159{
160}
161
162void EVENT_USB_Device_Suspend()
163{
f0c5f1b9 164#ifdef SLEEP_LED_ENABLE
165 sleep_led_enable();
166#endif
c8cfde01 167}
168
169void EVENT_USB_Device_WakeUp()
170{
afc3148e 171 suspend_wakeup_init();
42f6ff08 172
f0c5f1b9 173#ifdef SLEEP_LED_ENABLE
174 sleep_led_disable();
175#endif
176 led_set(host_keyboard_leds());
c8cfde01 177}
178
89470299 179void EVENT_USB_Device_StartOfFrame(void)
180{
e075361b 181 Console_Task();
89470299 182}
183
dd93d291 184/** Event handler for the USB_ConfigurationChanged event.
185 * This is fired when the host sets the current configuration of the USB device after enumeration.
186 */
e9e4c0ed 187#if LUFA_VERSION_INTEGER < 0x120730
188 /* old API 120219 */
189 #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint(epnum, eptype, epdir, epsize, epbank)
190#else
191 /* new API >= 120730 */
192 #define ENDPOINT_BANK_SINGLE 1
193 #define ENDPOINT_BANK_DOUBLE 2
194 #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum) , eptype, epsize, epbank)
195#endif
dd93d291 196void EVENT_USB_Device_ConfigurationChanged(void)
197{
198 bool ConfigSuccess = true;
199
200 /* Setup Keyboard HID Report Endpoints */
e9e4c0ed 201 ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
202 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
dd93d291 203
f2ebac10 204#ifdef MOUSE_ENABLE
dd93d291 205 /* Setup Mouse HID Report Endpoint */
e9e4c0ed 206 ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
207 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
f2ebac10 208#endif
209
210#ifdef EXTRAKEY_ENABLE
211 /* Setup Extra HID Report Endpoint */
e9e4c0ed 212 ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
213 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
f2ebac10 214#endif
3969ec09 215
e075361b 216#ifdef CONSOLE_ENABLE
a9a3610d 217 /* Setup Console HID Report Endpoints */
e9e4c0ed 218 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
219 CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
220 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
221 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
e075361b 222#endif
dd93d291 223}
224
a9a3610d 225/*
226Appendix G: HID Request Support Requirements
227
228The following table enumerates the requests that need to be supported by various types of HID class devices.
229
230Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
231------------------------------------------------------------------------------------------
232Boot Mouse Required Optional Optional Optional Required Required
233Non-Boot Mouse Required Optional Optional Optional Optional Optional
234Boot Keyboard Required Optional Required Required Required Required
235Non-Boot Keybrd Required Optional Required Required Optional Optional
236Other Device Required Optional Optional Optional Optional Optional
237*/
dd93d291 238/** Event handler for the USB_ControlRequest event.
239 * This is fired before passing along unhandled control requests to the library for processing internally.
240 */
241void EVENT_USB_Device_ControlRequest(void)
242{
3969ec09 243 uint8_t* ReportData = NULL;
244 uint8_t ReportSize = 0;
dd93d291 245
246 /* Handle HID Class specific requests */
247 switch (USB_ControlRequest.bRequest)
248 {
249 case HID_REQ_GetReport:
250 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
251 {
252 Endpoint_ClearSETUP();
253
3969ec09 254 // Interface
255 switch (USB_ControlRequest.wIndex) {
effa5914 256 case KEYBOARD_INTERFACE:
257 // TODO: test/check
dd93d291 258 ReportData = (uint8_t*)&keyboard_report_sent;
259 ReportSize = sizeof(keyboard_report_sent);
3969ec09 260 break;
dd93d291 261 }
262
263 /* Write the report data to the control endpoint */
264 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
265 Endpoint_ClearOUT();
266 }
267
268 break;
269 case HID_REQ_SetReport:
270 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
271 {
dd93d291 272
3969ec09 273 // Interface
274 switch (USB_ControlRequest.wIndex) {
effa5914 275 case KEYBOARD_INTERFACE:
a112f361 276 Endpoint_ClearSETUP();
277
278 while (!(Endpoint_IsOUTReceived())) {
279 if (USB_DeviceState == DEVICE_STATE_Unattached)
280 return;
281 }
3969ec09 282 keyboard_led_stats = Endpoint_Read_8();
a112f361 283
284 Endpoint_ClearOUT();
285 Endpoint_ClearStatusStage();
f2ebac10 286 break;
3969ec09 287 }
dd93d291 288
a112f361 289 }
290
291 break;
292
293 case HID_REQ_GetProtocol:
294 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
295 {
296 Endpoint_ClearSETUP();
297 while (!(Endpoint_IsINReady()));
298 Endpoint_Write_8(protocol_report);
299 Endpoint_ClearIN();
300 Endpoint_ClearStatusStage();
301 }
302
303 break;
304 case HID_REQ_SetProtocol:
305 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
306 {
307 Endpoint_ClearSETUP();
308 Endpoint_ClearStatusStage();
309
310 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
311 }
312
313 break;
314 case HID_REQ_SetIdle:
315 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
316 {
317 Endpoint_ClearSETUP();
318 Endpoint_ClearStatusStage();
319
320 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
321 }
322
323 break;
324 case HID_REQ_GetIdle:
325 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
326 {
327 Endpoint_ClearSETUP();
328 while (!(Endpoint_IsINReady()));
329 Endpoint_Write_8(idle_duration);
330 Endpoint_ClearIN();
dd93d291 331 Endpoint_ClearStatusStage();
332 }
333
334 break;
335 }
336}
337
dd93d291 338/*******************************************************************************
339 * Host driver
340 ******************************************************************************/
341static uint8_t keyboard_leds(void)
342{
343 return keyboard_led_stats;
344}
345
346static void send_keyboard(report_keyboard_t *report)
347{
45ea1874 348 uint8_t timeout = 0;
349
dd93d291 350 // TODO: handle NKRO report
351 /* Select the Keyboard Report Endpoint */
352 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
353
354 /* Check if Keyboard Endpoint Ready for Read/Write */
45ea1874 355 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
7350b7c6 356
357 /* Write Keyboard Report Data */
358 Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
359
360 /* Finalize the stream transfer to send the last packet */
361 Endpoint_ClearIN();
dd93d291 362
dd93d291 363 keyboard_report_sent = *report;
364}
365
366static void send_mouse(report_mouse_t *report)
367{
f2ebac10 368#ifdef MOUSE_ENABLE
45ea1874 369 uint8_t timeout = 0;
370
dd93d291 371 /* Select the Mouse Report Endpoint */
372 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
373
374 /* Check if Mouse Endpoint Ready for Read/Write */
45ea1874 375 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
dd93d291 376
7350b7c6 377 /* Write Mouse Report Data */
378 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
379
380 /* Finalize the stream transfer to send the last packet */
381 Endpoint_ClearIN();
f2ebac10 382#endif
dd93d291 383}
384
385static void send_system(uint16_t data)
386{
45ea1874 387 uint8_t timeout = 0;
388
a112f361 389 report_extra_t r = {
390 .report_id = REPORT_ID_SYSTEM,
391 .usage = data
392 };
e075361b 393 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
45ea1874 394 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
7350b7c6 395 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
396 Endpoint_ClearIN();
dd93d291 397}
398
399static void send_consumer(uint16_t data)
400{
45ea1874 401 uint8_t timeout = 0;
402
a112f361 403 report_extra_t r = {
404 .report_id = REPORT_ID_CONSUMER,
405 .usage = data
406 };
e075361b 407 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
45ea1874 408 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
7350b7c6 409 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
410 Endpoint_ClearIN();
dd93d291 411}
3969ec09 412
413
414/*******************************************************************************
415 * sendchar
416 ******************************************************************************/
e075361b 417#ifdef CONSOLE_ENABLE
418#define SEND_TIMEOUT 5
3969ec09 419int8_t sendchar(uint8_t c)
420{
45ea1874 421 // Not wait once timeouted.
422 // Because sendchar() is called so many times, waiting each call causes big lag.
423 static bool timeouted = false;
424
3969ec09 425 if (USB_DeviceState != DEVICE_STATE_Configured)
45ea1874 426 return -1;
3969ec09 427
e075361b 428 uint8_t ep = Endpoint_GetCurrentEndpoint();
3d81d522 429 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
45ea1874 430 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
431 Endpoint_SelectEndpoint(ep);
432 return -1;
433 }
434
435 if (timeouted && !Endpoint_IsReadWriteAllowed()) {
436 Endpoint_SelectEndpoint(ep);
437 return - 1;
438 }
439
440 timeouted = false;
3969ec09 441
89470299 442 uint8_t timeout = SEND_TIMEOUT;
3969ec09 443 uint16_t prevFN = USB_Device_GetFrameNumber();
89470299 444 while (!Endpoint_IsReadWriteAllowed()) {
3969ec09 445 switch (USB_DeviceState) {
446 case DEVICE_STATE_Unattached:
447 case DEVICE_STATE_Suspended:
448 return -1;
449 }
e075361b 450 if (Endpoint_IsStalled()) {
451 Endpoint_SelectEndpoint(ep);
3969ec09 452 return -1;
e075361b 453 }
3969ec09 454 if (prevFN != USB_Device_GetFrameNumber()) {
e075361b 455 if (!(timeout--)) {
45ea1874 456 timeouted = true;
e075361b 457 Endpoint_SelectEndpoint(ep);
3969ec09 458 return -1;
e075361b 459 }
3969ec09 460 prevFN = USB_Device_GetFrameNumber();
461 }
462 }
463
464 Endpoint_Write_8(c);
465
89470299 466 // send when bank is full
3969ec09 467 if (!Endpoint_IsReadWriteAllowed())
468 Endpoint_ClearIN();
469
e075361b 470 Endpoint_SelectEndpoint(ep);
471 return 0;
472}
473#else
474int8_t sendchar(uint8_t c)
475{
3969ec09 476 return 0;
477}
e075361b 478#endif
c5060ea8 479
480
481/*******************************************************************************
482 * main
483 ******************************************************************************/
484static void SetupHardware(void)
485{
486 /* Disable watchdog if enabled by bootloader/fuses */
487 MCUSR &= ~(1 << WDRF);
488 wdt_disable();
489
490 /* Disable clock division */
491 clock_prescale_set(clock_div_1);
492
493 // Leonardo needs. Without this USB device is not recognized.
494 USB_Disable();
495
496 USB_Init();
497
498 // for Console_Task
499 USB_Device_EnableSOFEvents();
500}
501
502int main(void) __attribute__ ((weak));
503int main(void)
504{
505 SetupHardware();
16a583d7 506 keyboard_init();
507 host_set_driver(&lufa_driver);
f0c5f1b9 508#ifdef SLEEP_LED_ENABLE
509 sleep_led_init();
510#endif
c5060ea8 511 sei();
512
c5060ea8 513 while (1) {
c8cfde01 514 while (USB_DeviceState == DEVICE_STATE_Suspended) {
afc3148e 515 suspend_power_down();
516 if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
c8cfde01 517 USB_Device_SendRemoteWakeup();
c8cfde01 518 }
519 }
520
4ae979f6 521 keyboard_task();
c5060ea8 522
523#if !defined(INTERRUPT_CONTROL_ENDPOINT)
524 USB_USBTask();
525#endif
526 }
527}
Imprint / Impressum