]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/lufa/lufa.c
b1567fa9bdac242dfbe9a12395c7db8212177748
[max/tmk_keyboard.git] / tmk_core / protocol / lufa / lufa.c
1 /*
2  * Copyright 2012 Jun Wako <wakojun@gmail.com>
3  * This file is based on:
4  *     LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
5  *     LUFA-120219/Demos/Device/Lowlevel/GenericHID
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"
43 #include "action.h"
44 #include "led.h"
45 #include "sendchar.h"
46 #include "ringbuf.h"
47 #include "debug.h"
48 #ifdef SLEEP_LED_ENABLE
49 #include "sleep_led.h"
50 #endif
51 #include "suspend.h"
52 #include "hook.h"
53
54 #ifdef LUFA_DEBUG_SUART
55 #include "avr/suart.h"
56 #endif
57
58 #include "matrix.h"
59 #include "descriptor.h"
60 #include "lufa.h"
61
62
63 //#define LUFA_DEBUG
64
65
66 uint8_t keyboard_idle = 0;
67 /* 0: Boot Protocol, 1: Report Protocol(default) */
68 uint8_t keyboard_protocol = 1;
69 static uint8_t keyboard_led_stats = 0;
70
71 static report_keyboard_t keyboard_report_sent;
72
73
74 /* Host driver */
75 static uint8_t keyboard_leds(void);
76 static void send_keyboard(report_keyboard_t *report);
77 static void send_mouse(report_mouse_t *report);
78 static void send_system(uint16_t data);
79 static void send_consumer(uint16_t data);
80 host_driver_t lufa_driver = {
81     keyboard_leds,
82     send_keyboard,
83     send_mouse,
84     send_system,
85     send_consumer
86 };
87
88
89 /*******************************************************************************
90  * Console
91  ******************************************************************************/
92 #ifdef CONSOLE_ENABLE
93 #define SENDBUF_SIZE 256
94 static uint8_t sbuf[SENDBUF_SIZE];
95 static ringbuf_t sendbuf = {
96     .buffer = sbuf,
97     .head = 0,
98     .tail = 0,
99     .size_mask = SENDBUF_SIZE - 1
100 };
101
102 static bool console_putc(uint8_t c)
103 {
104     // return immediately if called while interrupt
105     if (!(SREG & (1<<SREG_I)))
106         goto EXIT;;
107
108     if (USB_DeviceState != DEVICE_STATE_Configured)
109         goto EXIT;;
110
111     uint8_t ep = Endpoint_GetCurrentEndpoint();
112
113     Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
114     if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
115         goto EXIT_RESTORE_EP;
116     }
117
118     // write from buffer to endpoint bank
119     while (!ringbuf_is_empty(&sendbuf) && Endpoint_IsReadWriteAllowed()) {
120         Endpoint_Write_8(ringbuf_get(&sendbuf));
121
122         // clear bank when it is full
123         if (!Endpoint_IsReadWriteAllowed() && Endpoint_IsINReady()) {
124             Endpoint_ClearIN();
125         }
126     }
127
128     // write c to bank directly if there is no others in buffer
129     if (ringbuf_is_empty(&sendbuf) && Endpoint_IsReadWriteAllowed()) {
130         Endpoint_Write_8(c);
131         Endpoint_SelectEndpoint(ep);
132         return true;
133     }
134
135 EXIT_RESTORE_EP:
136     Endpoint_SelectEndpoint(ep);
137 EXIT:
138     return ringbuf_put(&sendbuf, c);
139 }
140
141 static void console_flush(void)
142 {
143     if (USB_DeviceState != DEVICE_STATE_Configured)
144         return;
145
146     uint8_t ep = Endpoint_GetCurrentEndpoint();
147
148     Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
149     if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
150         Endpoint_SelectEndpoint(ep);
151         return;
152     }
153
154     // write from buffer to endpoint bank
155     while (!ringbuf_is_empty(&sendbuf) && Endpoint_IsReadWriteAllowed()) {
156         Endpoint_Write_8(ringbuf_get(&sendbuf));
157
158         // clear bank when it is full
159         if (!Endpoint_IsReadWriteAllowed() && Endpoint_IsINReady()) {
160             Endpoint_ClearIN();
161         }
162     }
163
164     // clear bank when there are chars in bank
165     if (Endpoint_BytesInEndpoint() && Endpoint_IsINReady()) {
166         // Windows needs to fill packet with 0
167         while (Endpoint_IsReadWriteAllowed()) {
168                 Endpoint_Write_8(0);
169         }
170         Endpoint_ClearIN();
171     }
172
173     Endpoint_SelectEndpoint(ep);
174 }
175
176 static void console_task(void)
177 {
178     static uint16_t fn = 0;
179     if (fn == USB_Device_GetFrameNumber()) {
180         return;
181     }
182     fn = USB_Device_GetFrameNumber();
183     console_flush();
184 }
185 #endif
186
187
188 /*******************************************************************************
189  * USB Events
190  ******************************************************************************/
191 /*
192  * Event Order of Plug in:
193  * 0) EVENT_USB_Device_Connect
194  * 1) EVENT_USB_Device_Suspend
195  * 2) EVENT_USB_Device_Reset
196  * 3) EVENT_USB_Device_Wake
197 */
198 void EVENT_USB_Device_Connect(void)
199 {
200     print("[C]");
201     /* For battery powered device */
202     if (!USB_IsInitialized) {
203         USB_Disable();
204         USB_Init();
205         USB_Device_EnableSOFEvents();
206     }
207 }
208
209 void EVENT_USB_Device_Disconnect(void)
210 {
211     print("[D]");
212     /* For battery powered device */
213     USB_IsInitialized = false;
214 /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
215     if (USB_IsInitialized) {
216         USB_Disable();  // Disable all interrupts
217         USB_Controller_Enable();
218         USB_INT_Enable(USB_INT_VBUSTI);
219     }
220 */
221 }
222
223 void EVENT_USB_Device_Reset(void)
224 {
225 #ifdef LUFA_DEBUG
226     print("[R]");
227 #endif
228 }
229
230 void EVENT_USB_Device_Suspend()
231 {
232 #ifdef LUFA_DEBUG
233     print("[S]");
234 #endif
235     hook_usb_suspend_entry();
236 }
237
238 void EVENT_USB_Device_WakeUp()
239 {
240 #ifdef LUFA_DEBUG
241     print("[W]");
242 #endif
243     hook_usb_wakeup();
244 }
245
246 // called every 1ms
247 void EVENT_USB_Device_StartOfFrame(void)
248 {
249 }
250
251 /** Event handler for the USB_ConfigurationChanged event.
252  * This is fired when the host sets the current configuration of the USB device after enumeration.
253  *
254  * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4,
255  * it is safe to use singl bank for all endpoints.
256  */
257 void EVENT_USB_Device_ConfigurationChanged(void)
258 {
259 #ifdef LUFA_DEBUG
260     print("[c]");
261 #endif
262     bool ConfigSuccess = true;
263
264     /* Setup Keyboard HID Report Endpoints */
265     ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
266                                      KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
267
268 #ifdef MOUSE_ENABLE
269     /* Setup Mouse HID Report Endpoint */
270     ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
271                                      MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
272 #endif
273
274 #ifdef EXTRAKEY_ENABLE
275     /* Setup Extra HID Report Endpoint */
276     ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
277                                      EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
278 #endif
279
280 #ifdef CONSOLE_ENABLE
281     /* Setup Console HID Report Endpoints */
282     ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
283                                      CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
284 #if 0
285     ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
286                                      CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
287 #endif
288 #endif
289
290 #ifdef NKRO_ENABLE
291     /* Setup NKRO HID Report Endpoints */
292     ConfigSuccess &= ENDPOINT_CONFIG(NKRO_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
293                                      NKRO_EPSIZE, ENDPOINT_BANK_SINGLE);
294 #endif
295 }
296
297 /*
298 Appendix G: HID Request Support Requirements
299
300 The following table enumerates the requests that need to be supported by various types of HID class devices.
301
302 Device type     GetReport   SetReport   GetIdle     SetIdle     GetProtocol SetProtocol
303 ------------------------------------------------------------------------------------------
304 Boot Mouse      Required    Optional    Optional    Optional    Required    Required
305 Non-Boot Mouse  Required    Optional    Optional    Optional    Optional    Optional
306 Boot Keyboard   Required    Optional    Required    Required    Required    Required
307 Non-Boot Keybrd Required    Optional    Required    Required    Optional    Optional
308 Other Device    Required    Optional    Optional    Optional    Optional    Optional
309 */
310 /** Event handler for the USB_ControlRequest event.
311  *  This is fired before passing along unhandled control requests to the library for processing internally.
312  */
313 void EVENT_USB_Device_ControlRequest(void)
314 {
315     uint8_t* ReportData = NULL;
316     uint8_t  ReportSize = 0;
317
318     /* Handle HID Class specific requests */
319     switch (USB_ControlRequest.bRequest)
320     {
321         case HID_REQ_GetReport:
322             if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
323             {
324                 Endpoint_ClearSETUP();
325
326                 // Interface
327                 switch (USB_ControlRequest.wIndex) {
328                 case KEYBOARD_INTERFACE:
329                     // TODO: test/check
330                     ReportData = (uint8_t*)&keyboard_report_sent;
331                     ReportSize = sizeof(keyboard_report_sent);
332                     break;
333                 }
334
335                 /* Write the report data to the control endpoint */
336                 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
337                 Endpoint_ClearOUT();
338 #ifdef LUFA_DEBUG
339                 xprintf("[r%d]", USB_ControlRequest.wIndex);
340 #endif
341             }
342
343             break;
344         case HID_REQ_SetReport:
345             if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
346             {
347
348                 // Interface
349                 switch (USB_ControlRequest.wIndex) {
350                 case KEYBOARD_INTERFACE:
351 #ifdef NKRO_ENABLE
352                 case NKRO_INTERFACE:
353 #endif
354                     Endpoint_ClearSETUP();
355
356                     while (!(Endpoint_IsOUTReceived())) {
357                         if (USB_DeviceState == DEVICE_STATE_Unattached)
358                           return;
359                     }
360                     keyboard_led_stats = Endpoint_Read_8();
361
362                     Endpoint_ClearOUT();
363                     Endpoint_ClearStatusStage();
364 #ifdef LUFA_DEBUG
365                     xprintf("[L%d]", USB_ControlRequest.wIndex);
366 #endif
367                     break;
368                 }
369
370             }
371
372             break;
373
374         case HID_REQ_GetProtocol:
375             if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
376             {
377                 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
378                     Endpoint_ClearSETUP();
379                     while (!(Endpoint_IsINReady()));
380                     Endpoint_Write_8(keyboard_protocol);
381                     Endpoint_ClearIN();
382                     Endpoint_ClearStatusStage();
383 #ifdef LUFA_DEBUG
384                     print("[p]");
385 #endif
386                 }
387             }
388
389             break;
390         case HID_REQ_SetProtocol:
391             if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
392             {
393                 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
394                     Endpoint_ClearSETUP();
395                     Endpoint_ClearStatusStage();
396
397                     keyboard_protocol = (USB_ControlRequest.wValue & 0xFF);
398                     clear_keyboard();
399 #ifdef LUFA_DEBUG
400                     print("[P]");
401 #endif
402                 }
403             }
404
405             break;
406         case HID_REQ_SetIdle:
407             if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
408             {
409                 Endpoint_ClearSETUP();
410                 Endpoint_ClearStatusStage();
411
412                 keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
413 #ifdef LUFA_DEBUG
414                 xprintf("[I%d]%d", USB_ControlRequest.wIndex, (USB_ControlRequest.wValue & 0xFF00) >> 8);
415 #endif
416             }
417
418             break;
419         case HID_REQ_GetIdle:
420             if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
421             {
422                 Endpoint_ClearSETUP();
423                 while (!(Endpoint_IsINReady()));
424                 Endpoint_Write_8(keyboard_idle);
425                 Endpoint_ClearIN();
426                 Endpoint_ClearStatusStage();
427 #ifdef LUFA_DEBUG
428                 print("[i]");
429 #endif
430             }
431
432             break;
433     }
434 }
435
436 /*******************************************************************************
437  * Host driver
438  ******************************************************************************/
439 static uint8_t keyboard_leds(void)
440 {
441     return keyboard_led_stats;
442 }
443
444 static void send_keyboard(report_keyboard_t *report)
445 {
446     uint8_t timeout = 128;
447
448     if (USB_DeviceState != DEVICE_STATE_Configured)
449         return;
450
451     /* Select the Keyboard Report Endpoint */
452 #ifdef NKRO_ENABLE
453     if (keyboard_protocol && keyboard_nkro) {
454         /* Report protocol - NKRO */
455         Endpoint_SelectEndpoint(NKRO_IN_EPNUM);
456
457         /* Check if write ready for a polling interval around 1ms */
458         while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(8);
459         if (!Endpoint_IsReadWriteAllowed()) return;
460
461         /* Write Keyboard Report Data */
462         Endpoint_Write_Stream_LE(report, NKRO_EPSIZE, NULL);
463     }
464     else
465 #endif
466     {
467         /* Boot protocol */
468         Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
469
470         /* Check if write ready for a polling interval around 10ms */
471         while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(80);
472         if (!Endpoint_IsReadWriteAllowed()) return;
473
474         /* Write Keyboard Report Data */
475         Endpoint_Write_Stream_LE(report, KEYBOARD_EPSIZE, NULL);
476     }
477
478     /* Finalize the stream transfer to send the last packet */
479     Endpoint_ClearIN();
480
481     keyboard_report_sent = *report;
482 }
483
484 static void send_mouse(report_mouse_t *report)
485 {
486 #ifdef MOUSE_ENABLE
487     uint8_t timeout = 255;
488
489     if (USB_DeviceState != DEVICE_STATE_Configured)
490         return;
491
492     /* Select the Mouse Report Endpoint */
493     Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
494
495     /* Check if write ready for a polling interval around 10ms */
496     while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
497     if (!Endpoint_IsReadWriteAllowed()) return;
498
499     /* Write Mouse Report Data */
500     Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
501
502     /* Finalize the stream transfer to send the last packet */
503     Endpoint_ClearIN();
504 #endif
505 }
506
507 static void send_system(uint16_t data)
508 {
509 #ifdef EXTRAKEY_ENABLE
510     uint8_t timeout = 255;
511
512     if (USB_DeviceState != DEVICE_STATE_Configured)
513         return;
514
515     report_extra_t r = {
516         .report_id = REPORT_ID_SYSTEM,
517         .usage = data
518     };
519     Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
520
521     /* Check if write ready for a polling interval around 10ms */
522     while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
523     if (!Endpoint_IsReadWriteAllowed()) return;
524
525     Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
526     Endpoint_ClearIN();
527 #endif
528 }
529
530 static void send_consumer(uint16_t data)
531 {
532 #ifdef EXTRAKEY_ENABLE
533     uint8_t timeout = 255;
534
535     if (USB_DeviceState != DEVICE_STATE_Configured)
536         return;
537
538     report_extra_t r = {
539         .report_id = REPORT_ID_CONSUMER,
540         .usage = data
541     };
542     Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
543
544     /* Check if write ready for a polling interval around 10ms */
545     while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
546     if (!Endpoint_IsReadWriteAllowed()) return;
547
548     Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
549     Endpoint_ClearIN();
550 #endif
551 }
552
553
554 /*******************************************************************************
555  * sendchar
556  ******************************************************************************/
557 #ifdef CONSOLE_ENABLE
558 int8_t sendchar(uint8_t c)
559 {
560     #ifdef LUFA_DEBUG_SUART
561     xmit(c);
562     #endif
563
564     bool r = console_putc(c);
565     return (r ? 0 : -1);
566 }
567 #else
568 int8_t sendchar(uint8_t c)
569 {
570     #ifdef LUFA_DEBUG_SUART
571     xmit(c);
572     #endif
573     return 0;
574 }
575 #endif
576
577
578 /*******************************************************************************
579  * main
580  ******************************************************************************/
581 static void setup_mcu(void)
582 {
583     /* Disable watchdog if enabled by bootloader/fuses */
584     MCUSR &= ~(1 << WDRF);
585     wdt_disable();
586
587     /* Disable clock division */
588     clock_prescale_set(clock_div_1);
589 }
590
591 static void setup_usb(void)
592 {
593     // Leonardo needs. Without this USB device is not recognized.
594     USB_Disable();
595
596     USB_Init();
597
598     USB_Device_EnableSOFEvents();
599 }
600
601 int main(void)  __attribute__ ((weak));
602 int main(void)
603 {
604     setup_mcu();
605
606 #ifdef LUFA_DEBUG_SUART
607     SUART_OUT_DDR |= (1<<SUART_OUT_BIT);
608     SUART_OUT_PORT |= (1<<SUART_OUT_BIT);
609 #endif
610
611     // setup sendchar: DO NOT USE print functions before this line
612     print_set_sendchar(sendchar);
613     host_set_driver(&lufa_driver);
614
615     print("Keyboard init.\n");
616     hook_early_init();
617     keyboard_setup();
618     setup_usb();
619 #ifdef SLEEP_LED_ENABLE
620     sleep_led_init();
621 #endif
622
623     sei();
624
625     /* wait for USB startup */
626     while (USB_DeviceState != DEVICE_STATE_Configured) {
627 #if defined(INTERRUPT_CONTROL_ENDPOINT)
628         ;
629 #else
630         USB_USBTask();
631 #endif
632     }
633
634     keyboard_init();
635
636     /* wait for Console startup */
637     // TODO: 2000ms delay often works anyhoo but proper startup would be better
638     // 1000ms delay of hid_listen affects this probably
639     #ifdef CONSOLE_ENABLE
640     if (debug_enable) {
641         uint16_t delay = 2000;
642         while (delay--) {
643             #ifndef INTERRUPT_CONTROL_ENDPOINT
644             USB_USBTask();
645             #endif
646             _delay_ms(1);
647         }
648     }
649     #endif
650
651     hook_late_init();
652
653     print("Keyboard start.\n");
654     while (1) {
655         while (USB_DeviceState == DEVICE_STATE_Suspended) {
656 #ifdef LUFA_DEBUG
657             print("[s]");
658 #endif
659             hook_usb_suspend_loop();
660         }
661
662         keyboard_task();
663
664 #ifdef CONSOLE_ENABLE
665         console_task();
666 #endif
667
668 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
669         USB_USBTask();
670 #endif
671     }
672 }
673
674
675 /* hooks */
676 __attribute__((weak))
677 void hook_early_init(void) {}
678
679 __attribute__((weak))
680 void hook_late_init(void) {}
681
682 static uint8_t _led_stats = 0;
683  __attribute__((weak))
684 void hook_usb_suspend_entry(void)
685 {
686     // Turn LED off to save power
687     // Set 0 with putting aside status before suspend and restore
688     // it after wakeup, then LED is updated at keyboard_task() in main loop
689     _led_stats = keyboard_led_stats;
690     keyboard_led_stats = 0;
691     led_set(keyboard_led_stats);
692
693     matrix_clear();
694     clear_keyboard();
695 #ifdef SLEEP_LED_ENABLE
696     sleep_led_enable();
697 #endif
698 }
699
700 __attribute__((weak))
701 void hook_usb_suspend_loop(void)
702 {
703     suspend_power_down();
704     if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
705         USB_Device_SendRemoteWakeup();
706     }
707 }
708
709 __attribute__((weak))
710 void hook_usb_wakeup(void)
711 {
712     suspend_wakeup_init();
713 #ifdef SLEEP_LED_ENABLE
714     sleep_led_disable();
715 #endif
716
717     // Restore LED status
718     // BIOS/grub won't recognize/enumerate if led_set() takes long(around 40ms?)
719     // Converters fall into the case and miss wakeup event(timeout to reply?) in the end.
720     //led_set(host_keyboard_leds());
721     // Instead, restore stats and update at keyboard_task() in main loop
722     keyboard_led_stats = _led_stats;
723 }