Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lufa.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * Copyright 2012 Jun Wako <[email protected]>
  3. * This file is based on:
  4. * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
  5. * LUFA-120219/Demos/Device/Lowlevel/GenericHID
  6. */
  7. /*
  8. LUFA Library
  9. Copyright (C) Dean Camera, 2012.
  10. dean [at] fourwalledcubicle [dot] com
  11. www.lufa-lib.org
  12. */
  13. /*
  14. Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  15. Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
  16. Permission to use, copy, modify, distribute, and sell this
  17. software and its documentation for any purpose is hereby granted
  18. without fee, provided that the above copyright notice appear in
  19. all copies and that both that the copyright notice and this
  20. permission notice and warranty disclaimer appear in supporting
  21. documentation, and that the name of the author not be used in
  22. advertising or publicity pertaining to distribution of the
  23. software without specific, written prior permission.
  24. The author disclaim all warranties with regard to this
  25. software, including all implied warranties of merchantability
  26. and fitness. In no event shall the author be liable for any
  27. special, indirect or consequential damages or any damages
  28. whatsoever resulting from loss of use, data or profits, whether
  29. in an action of contract, negligence or other tortious action,
  30. arising out of or in connection with the use or performance of
  31. this software.
  32. */
  33. #include "report.h"
  34. #include "host.h"
  35. #include "host_driver.h"
  36. #include "keyboard.h"
  37. #include "action.h"
  38. #include "led.h"
  39. #include "sendchar.h"
  40. #include "debug.h"
  41. #ifdef SLEEP_LED_ENABLE
  42. #include "sleep_led.h"
  43. #endif
  44. #include "suspend.h"
  45. #include "hook.h"
  46. #ifdef LUFA_DEBUG_SUART
  47. #include "avr/suart.h"
  48. #endif
  49. #include "matrix.h"
  50. #include "descriptor.h"
  51. #include "lufa.h"
  52. //#define LUFA_DEBUG
  53. uint8_t keyboard_idle = 0;
  54. /* 0: Boot Protocol, 1: Report Protocol(default) */
  55. uint8_t keyboard_protocol = 1;
  56. static uint8_t keyboard_led_stats = 0;
  57. static report_keyboard_t keyboard_report_sent;
  58. /* Host driver */
  59. static uint8_t keyboard_leds(void);
  60. static void send_keyboard(report_keyboard_t *report);
  61. static void send_mouse(report_mouse_t *report);
  62. static void send_system(uint16_t data);
  63. static void send_consumer(uint16_t data);
  64. host_driver_t lufa_driver = {
  65. keyboard_leds,
  66. send_keyboard,
  67. send_mouse,
  68. send_system,
  69. send_consumer
  70. };
  71. /*******************************************************************************
  72. * Console
  73. ******************************************************************************/
  74. #ifdef CONSOLE_ENABLE
  75. static void Console_Task(void)
  76. {
  77. /* Device must be connected and configured for the task to run */
  78. if (USB_DeviceState != DEVICE_STATE_Configured)
  79. return;
  80. uint8_t ep = Endpoint_GetCurrentEndpoint();
  81. #if 0
  82. // TODO: impl receivechar()/recvchar()
  83. Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
  84. /* Check to see if a packet has been sent from the host */
  85. if (Endpoint_IsOUTReceived())
  86. {
  87. /* Check to see if the packet contains data */
  88. if (Endpoint_IsReadWriteAllowed())
  89. {
  90. /* Create a temporary buffer to hold the read in report from the host */
  91. uint8_t ConsoleData[CONSOLE_EPSIZE];
  92. /* Read Console Report Data */
  93. Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
  94. /* Process Console Report Data */
  95. //ProcessConsoleHIDReport(ConsoleData);
  96. }
  97. /* Finalize the stream transfer to send the last packet */
  98. Endpoint_ClearOUT();
  99. }
  100. #endif
  101. /* IN packet */
  102. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  103. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  104. Endpoint_SelectEndpoint(ep);
  105. return;
  106. }
  107. // fill empty bank
  108. while (Endpoint_IsReadWriteAllowed())
  109. Endpoint_Write_8(0);
  110. // flash senchar packet
  111. if (Endpoint_IsINReady()) {
  112. Endpoint_ClearIN();
  113. }
  114. Endpoint_SelectEndpoint(ep);
  115. }
  116. #else
  117. static void Console_Task(void)
  118. {
  119. }
  120. #endif
  121. /*******************************************************************************
  122. * USB Events
  123. ******************************************************************************/
  124. /*
  125. * Event Order of Plug in:
  126. * 0) EVENT_USB_Device_Connect
  127. * 1) EVENT_USB_Device_Suspend
  128. * 2) EVENT_USB_Device_Reset
  129. * 3) EVENT_USB_Device_Wake
  130. */
  131. void EVENT_USB_Device_Connect(void)
  132. {
  133. print("[C]");
  134. /* For battery powered device */
  135. if (!USB_IsInitialized) {
  136. USB_Disable();
  137. USB_Init();
  138. USB_Device_EnableSOFEvents();
  139. }
  140. }
  141. void EVENT_USB_Device_Disconnect(void)
  142. {
  143. print("[D]");
  144. /* For battery powered device */
  145. USB_IsInitialized = false;
  146. /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
  147. if (USB_IsInitialized) {
  148. USB_Disable(); // Disable all interrupts
  149. USB_Controller_Enable();
  150. USB_INT_Enable(USB_INT_VBUSTI);
  151. }
  152. */
  153. }
  154. void EVENT_USB_Device_Reset(void)
  155. {
  156. #ifdef LUFA_DEBUG
  157. print("[R]");
  158. #endif
  159. }
  160. void EVENT_USB_Device_Suspend()
  161. {
  162. #ifdef LUFA_DEBUG
  163. print("[S]");
  164. #endif
  165. hook_usb_suspend_entry();
  166. }
  167. void EVENT_USB_Device_WakeUp()
  168. {
  169. #ifdef LUFA_DEBUG
  170. print("[W]");
  171. #endif
  172. hook_usb_wakeup();
  173. }
  174. #ifdef CONSOLE_ENABLE
  175. static bool console_flush = false;
  176. #define CONSOLE_FLUSH_SET(b) do { \
  177. uint8_t sreg = SREG; cli(); console_flush = b; SREG = sreg; \
  178. } while (0)
  179. // called every 1ms
  180. void EVENT_USB_Device_StartOfFrame(void)
  181. {
  182. static uint8_t count;
  183. if (++count % 50) return;
  184. count = 0;
  185. if (!console_flush) return;
  186. Console_Task();
  187. console_flush = false;
  188. }
  189. #endif
  190. /** Event handler for the USB_ConfigurationChanged event.
  191. * This is fired when the host sets the current configuration of the USB device after enumeration.
  192. *
  193. * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4,
  194. * it is safe to use singl bank for all endpoints.
  195. */
  196. void EVENT_USB_Device_ConfigurationChanged(void)
  197. {
  198. #ifdef LUFA_DEBUG
  199. print("[c]");
  200. #endif
  201. bool ConfigSuccess = true;
  202. /* Setup Keyboard HID Report Endpoints */
  203. ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  204. KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
  205. #ifdef MOUSE_ENABLE
  206. /* Setup Mouse HID Report Endpoint */
  207. ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  208. MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
  209. #endif
  210. #ifdef EXTRAKEY_ENABLE
  211. /* Setup Extra HID Report Endpoint */
  212. ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  213. EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
  214. #endif
  215. #ifdef CONSOLE_ENABLE
  216. /* Setup Console HID Report Endpoints */
  217. ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  218. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  219. #if 0
  220. ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  221. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  222. #endif
  223. #endif
  224. #ifdef NKRO_ENABLE
  225. /* Setup NKRO HID Report Endpoints */
  226. ConfigSuccess &= ENDPOINT_CONFIG(NKRO_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  227. NKRO_EPSIZE, ENDPOINT_BANK_SINGLE);
  228. #endif
  229. }
  230. /*
  231. Appendix G: HID Request Support Requirements
  232. The following table enumerates the requests that need to be supported by various types of HID class devices.
  233. Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  234. ------------------------------------------------------------------------------------------
  235. Boot Mouse Required Optional Optional Optional Required Required
  236. Non-Boot Mouse Required Optional Optional Optional Optional Optional
  237. Boot Keyboard Required Optional Required Required Required Required
  238. Non-Boot Keybrd Required Optional Required Required Optional Optional
  239. Other Device Required Optional Optional Optional Optional Optional
  240. */
  241. /** Event handler for the USB_ControlRequest event.
  242. * This is fired before passing along unhandled control requests to the library for processing internally.
  243. */
  244. void EVENT_USB_Device_ControlRequest(void)
  245. {
  246. uint8_t* ReportData = NULL;
  247. uint8_t ReportSize = 0;
  248. /* Handle HID Class specific requests */
  249. switch (USB_ControlRequest.bRequest)
  250. {
  251. case HID_REQ_GetReport:
  252. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  253. {
  254. Endpoint_ClearSETUP();
  255. // Interface
  256. switch (USB_ControlRequest.wIndex) {
  257. case KEYBOARD_INTERFACE:
  258. // TODO: test/check
  259. ReportData = (uint8_t*)&keyboard_report_sent;
  260. ReportSize = sizeof(keyboard_report_sent);
  261. break;
  262. }
  263. /* Write the report data to the control endpoint */
  264. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  265. Endpoint_ClearOUT();
  266. #ifdef LUFA_DEBUG
  267. xprintf("[r%d]", USB_ControlRequest.wIndex);
  268. #endif
  269. }
  270. break;
  271. case HID_REQ_SetReport:
  272. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  273. {
  274. // Interface
  275. switch (USB_ControlRequest.wIndex) {
  276. case KEYBOARD_INTERFACE:
  277. #ifdef NKRO_ENABLE
  278. case NKRO_INTERFACE:
  279. #endif
  280. Endpoint_ClearSETUP();
  281. while (!(Endpoint_IsOUTReceived())) {
  282. if (USB_DeviceState == DEVICE_STATE_Unattached)
  283. return;
  284. }
  285. keyboard_led_stats = Endpoint_Read_8();
  286. Endpoint_ClearOUT();
  287. Endpoint_ClearStatusStage();
  288. #ifdef LUFA_DEBUG
  289. xprintf("[L%d]", USB_ControlRequest.wIndex);
  290. #endif
  291. break;
  292. }
  293. }
  294. break;
  295. case HID_REQ_GetProtocol:
  296. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  297. {
  298. if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
  299. Endpoint_ClearSETUP();
  300. while (!(Endpoint_IsINReady()));
  301. Endpoint_Write_8(keyboard_protocol);
  302. Endpoint_ClearIN();
  303. Endpoint_ClearStatusStage();
  304. #ifdef LUFA_DEBUG
  305. print("[p]");
  306. #endif
  307. }
  308. }
  309. break;
  310. case HID_REQ_SetProtocol:
  311. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  312. {
  313. if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
  314. Endpoint_ClearSETUP();
  315. Endpoint_ClearStatusStage();
  316. keyboard_protocol = (USB_ControlRequest.wValue & 0xFF);
  317. clear_keyboard();
  318. #ifdef LUFA_DEBUG
  319. print("[P]");
  320. #endif
  321. }
  322. }
  323. break;
  324. case HID_REQ_SetIdle:
  325. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  326. {
  327. Endpoint_ClearSETUP();
  328. Endpoint_ClearStatusStage();
  329. keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
  330. #ifdef LUFA_DEBUG
  331. xprintf("[I%d]%d", USB_ControlRequest.wIndex, (USB_ControlRequest.wValue & 0xFF00) >> 8);
  332. #endif
  333. }
  334. break;
  335. case HID_REQ_GetIdle:
  336. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  337. {
  338. Endpoint_ClearSETUP();
  339. while (!(Endpoint_IsINReady()));
  340. Endpoint_Write_8(keyboard_idle);
  341. Endpoint_ClearIN();
  342. Endpoint_ClearStatusStage();
  343. #ifdef LUFA_DEBUG
  344. print("[i]");
  345. #endif
  346. }
  347. break;
  348. }
  349. }
  350. /*******************************************************************************
  351. * Host driver
  352. ******************************************************************************/
  353. static uint8_t keyboard_leds(void)
  354. {
  355. return keyboard_led_stats;
  356. }
  357. static void send_keyboard(report_keyboard_t *report)
  358. {
  359. uint8_t timeout = 255;
  360. if (USB_DeviceState != DEVICE_STATE_Configured)
  361. return;
  362. /* Select the Keyboard Report Endpoint */
  363. #ifdef NKRO_ENABLE
  364. if (keyboard_protocol && keyboard_nkro) {
  365. /* Report protocol - NKRO */
  366. Endpoint_SelectEndpoint(NKRO_IN_EPNUM);
  367. /* Check if write ready for a polling interval around 1ms */
  368. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(4);
  369. if (!Endpoint_IsReadWriteAllowed()) return;
  370. /* Write Keyboard Report Data */
  371. Endpoint_Write_Stream_LE(report, NKRO_EPSIZE, NULL);
  372. }
  373. else
  374. #endif
  375. {
  376. /* Boot protocol */
  377. Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
  378. /* Check if write ready for a polling interval around 10ms */
  379. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  380. if (!Endpoint_IsReadWriteAllowed()) return;
  381. /* Write Keyboard Report Data */
  382. Endpoint_Write_Stream_LE(report, KEYBOARD_EPSIZE, NULL);
  383. }
  384. /* Finalize the stream transfer to send the last packet */
  385. Endpoint_ClearIN();
  386. keyboard_report_sent = *report;
  387. }
  388. static void send_mouse(report_mouse_t *report)
  389. {
  390. #ifdef MOUSE_ENABLE
  391. uint8_t timeout = 255;
  392. if (USB_DeviceState != DEVICE_STATE_Configured)
  393. return;
  394. /* Select the Mouse Report Endpoint */
  395. Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
  396. /* Check if write ready for a polling interval around 10ms */
  397. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  398. if (!Endpoint_IsReadWriteAllowed()) return;
  399. /* Write Mouse Report Data */
  400. Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
  401. /* Finalize the stream transfer to send the last packet */
  402. Endpoint_ClearIN();
  403. #endif
  404. }
  405. static void send_system(uint16_t data)
  406. {
  407. uint8_t timeout = 255;
  408. if (USB_DeviceState != DEVICE_STATE_Configured)
  409. return;
  410. report_extra_t r = {
  411. .report_id = REPORT_ID_SYSTEM,
  412. .usage = data
  413. };
  414. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  415. /* Check if write ready for a polling interval around 10ms */
  416. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  417. if (!Endpoint_IsReadWriteAllowed()) return;
  418. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  419. Endpoint_ClearIN();
  420. }
  421. static void send_consumer(uint16_t data)
  422. {
  423. uint8_t timeout = 255;
  424. if (USB_DeviceState != DEVICE_STATE_Configured)
  425. return;
  426. report_extra_t r = {
  427. .report_id = REPORT_ID_CONSUMER,
  428. .usage = data
  429. };
  430. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  431. /* Check if write ready for a polling interval around 10ms */
  432. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  433. if (!Endpoint_IsReadWriteAllowed()) return;
  434. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  435. Endpoint_ClearIN();
  436. }
  437. /*******************************************************************************
  438. * sendchar
  439. ******************************************************************************/
  440. #ifdef CONSOLE_ENABLE
  441. #define SEND_TIMEOUT 5
  442. int8_t sendchar(uint8_t c)
  443. {
  444. #ifdef LUFA_DEBUG_SUART
  445. xmit(c);
  446. #endif
  447. // Not wait once timeouted.
  448. // Because sendchar() is called so many times, waiting each call causes big lag.
  449. static bool timeouted = false;
  450. // prevents Console_Task() from running during sendchar() runs.
  451. // or char will be lost. These two function is mutually exclusive.
  452. CONSOLE_FLUSH_SET(false);
  453. if (USB_DeviceState != DEVICE_STATE_Configured)
  454. return -1;
  455. uint8_t ep = Endpoint_GetCurrentEndpoint();
  456. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  457. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  458. goto ERROR_EXIT;
  459. }
  460. if (timeouted && !Endpoint_IsReadWriteAllowed()) {
  461. goto ERROR_EXIT;
  462. }
  463. timeouted = false;
  464. uint8_t timeout = SEND_TIMEOUT;
  465. while (!Endpoint_IsReadWriteAllowed()) {
  466. if (USB_DeviceState != DEVICE_STATE_Configured) {
  467. goto ERROR_EXIT;
  468. }
  469. if (Endpoint_IsStalled()) {
  470. goto ERROR_EXIT;
  471. }
  472. if (!(timeout--)) {
  473. timeouted = true;
  474. goto ERROR_EXIT;
  475. }
  476. _delay_ms(1);
  477. }
  478. Endpoint_Write_8(c);
  479. // send when bank is full
  480. if (!Endpoint_IsReadWriteAllowed()) {
  481. while (!(Endpoint_IsINReady()));
  482. Endpoint_ClearIN();
  483. } else {
  484. CONSOLE_FLUSH_SET(true);
  485. }
  486. Endpoint_SelectEndpoint(ep);
  487. return 0;
  488. ERROR_EXIT:
  489. Endpoint_SelectEndpoint(ep);
  490. return -1;
  491. }
  492. #else
  493. int8_t sendchar(uint8_t c)
  494. {
  495. #ifdef LUFA_DEBUG_SUART
  496. xmit(c);
  497. #endif
  498. return 0;
  499. }
  500. #endif
  501. /*******************************************************************************
  502. * main
  503. ******************************************************************************/
  504. static void setup_mcu(void)
  505. {
  506. /* Disable watchdog if enabled by bootloader/fuses */
  507. MCUSR &= ~(1 << WDRF);
  508. wdt_disable();
  509. /* Disable clock division */
  510. clock_prescale_set(clock_div_1);
  511. }
  512. static void setup_usb(void)
  513. {
  514. // Leonardo needs. Without this USB device is not recognized.
  515. USB_Disable();
  516. USB_Init();
  517. // for Console_Task
  518. USB_Device_EnableSOFEvents();
  519. }
  520. int main(void) __attribute__ ((weak));
  521. int main(void)
  522. {
  523. setup_mcu();
  524. #ifdef LUFA_DEBUG_SUART
  525. SUART_OUT_DDR |= (1<<SUART_OUT_BIT);
  526. SUART_OUT_PORT |= (1<<SUART_OUT_BIT);
  527. #endif
  528. print_set_sendchar(sendchar);
  529. print("\r\ninit\n");
  530. hook_early_init();
  531. keyboard_setup();
  532. setup_usb();
  533. sei();
  534. /* wait for USB startup & debug output */
  535. while (USB_DeviceState != DEVICE_STATE_Configured) {
  536. #if defined(INTERRUPT_CONTROL_ENDPOINT)
  537. ;
  538. #else
  539. USB_USBTask();
  540. #endif
  541. }
  542. print("USB configured.\n");
  543. /* init modules */
  544. keyboard_init();
  545. host_set_driver(&lufa_driver);
  546. #ifdef SLEEP_LED_ENABLE
  547. sleep_led_init();
  548. #endif
  549. print("Keyboard start.\n");
  550. hook_late_init();
  551. while (1) {
  552. while (USB_DeviceState == DEVICE_STATE_Suspended) {
  553. #ifdef LUFA_DEBUG
  554. print("[s]");
  555. #endif
  556. hook_usb_suspend_loop();
  557. }
  558. keyboard_task();
  559. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  560. USB_USBTask();
  561. #endif
  562. }
  563. }
  564. /* hooks */
  565. __attribute__((weak))
  566. void hook_early_init(void) {}
  567. __attribute__((weak))
  568. void hook_late_init(void) {}
  569. static uint8_t _led_stats = 0;
  570. __attribute__((weak))
  571. void hook_usb_suspend_entry(void)
  572. {
  573. // Turn LED off to save power
  574. // Set 0 with putting aside status before suspend and restore
  575. // it after wakeup, then LED is updated at keyboard_task() in main loop
  576. _led_stats = keyboard_led_stats;
  577. keyboard_led_stats = 0;
  578. led_set(keyboard_led_stats);
  579. matrix_clear();
  580. clear_keyboard();
  581. #ifdef SLEEP_LED_ENABLE
  582. sleep_led_enable();
  583. #endif
  584. }
  585. __attribute__((weak))
  586. void hook_usb_suspend_loop(void)
  587. {
  588. suspend_power_down();
  589. if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
  590. USB_Device_SendRemoteWakeup();
  591. }
  592. }
  593. __attribute__((weak))
  594. void hook_usb_wakeup(void)
  595. {
  596. suspend_wakeup_init();
  597. #ifdef SLEEP_LED_ENABLE
  598. sleep_led_disable();
  599. #endif
  600. // Restore LED status
  601. // BIOS/grub won't recognize/enumerate if led_set() takes long(around 40ms?)
  602. // Converters fall into the case and miss wakeup event(timeout to reply?) in the end.
  603. //led_set(host_keyboard_leds());
  604. // Instead, restore stats and update at keyboard_task() in main loop
  605. keyboard_led_stats = _led_stats;
  606. }