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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 "descriptor.h"
  50. #include "lufa.h"
  51. uint8_t keyboard_idle = 0;
  52. /* 0: Boot Protocol, 1: Report Protocol(default) */
  53. uint8_t keyboard_protocol = 1;
  54. static uint8_t keyboard_led_stats = 0;
  55. static report_keyboard_t keyboard_report_sent;
  56. /* Host driver */
  57. static uint8_t keyboard_leds(void);
  58. static void send_keyboard(report_keyboard_t *report);
  59. static void send_mouse(report_mouse_t *report);
  60. static void send_system(uint16_t data);
  61. static void send_consumer(uint16_t data);
  62. host_driver_t lufa_driver = {
  63. keyboard_leds,
  64. send_keyboard,
  65. send_mouse,
  66. send_system,
  67. send_consumer
  68. };
  69. /*******************************************************************************
  70. * Console
  71. ******************************************************************************/
  72. #ifdef CONSOLE_ENABLE
  73. static void Console_Task(void)
  74. {
  75. /* Device must be connected and configured for the task to run */
  76. if (USB_DeviceState != DEVICE_STATE_Configured)
  77. return;
  78. uint8_t ep = Endpoint_GetCurrentEndpoint();
  79. #if 0
  80. // TODO: impl receivechar()/recvchar()
  81. Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
  82. /* Check to see if a packet has been sent from the host */
  83. if (Endpoint_IsOUTReceived())
  84. {
  85. /* Check to see if the packet contains data */
  86. if (Endpoint_IsReadWriteAllowed())
  87. {
  88. /* Create a temporary buffer to hold the read in report from the host */
  89. uint8_t ConsoleData[CONSOLE_EPSIZE];
  90. /* Read Console Report Data */
  91. Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
  92. /* Process Console Report Data */
  93. //ProcessConsoleHIDReport(ConsoleData);
  94. }
  95. /* Finalize the stream transfer to send the last packet */
  96. Endpoint_ClearOUT();
  97. }
  98. #endif
  99. /* IN packet */
  100. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  101. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  102. Endpoint_SelectEndpoint(ep);
  103. return;
  104. }
  105. // fill empty bank
  106. while (Endpoint_IsReadWriteAllowed())
  107. Endpoint_Write_8(0);
  108. // flash senchar packet
  109. if (Endpoint_IsINReady()) {
  110. Endpoint_ClearIN();
  111. }
  112. Endpoint_SelectEndpoint(ep);
  113. }
  114. #else
  115. static void Console_Task(void)
  116. {
  117. }
  118. #endif
  119. /*******************************************************************************
  120. * USB Events
  121. ******************************************************************************/
  122. /*
  123. * Event Order of Plug in:
  124. * 0) EVENT_USB_Device_Connect
  125. * 1) EVENT_USB_Device_Suspend
  126. * 2) EVENT_USB_Device_Reset
  127. * 3) EVENT_USB_Device_Wake
  128. */
  129. void EVENT_USB_Device_Connect(void)
  130. {
  131. print("[C]");
  132. /* For battery powered device */
  133. if (!USB_IsInitialized) {
  134. USB_Disable();
  135. USB_Init();
  136. USB_Device_EnableSOFEvents();
  137. }
  138. }
  139. void EVENT_USB_Device_Disconnect(void)
  140. {
  141. print("[D]");
  142. /* For battery powered device */
  143. USB_IsInitialized = false;
  144. /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
  145. if (USB_IsInitialized) {
  146. USB_Disable(); // Disable all interrupts
  147. USB_Controller_Enable();
  148. USB_INT_Enable(USB_INT_VBUSTI);
  149. }
  150. */
  151. }
  152. void EVENT_USB_Device_Reset(void)
  153. {
  154. print("[R]");
  155. }
  156. void EVENT_USB_Device_Suspend()
  157. {
  158. print("[S]");
  159. hook_usb_suspend_entry();
  160. }
  161. void EVENT_USB_Device_WakeUp()
  162. {
  163. print("[W]");
  164. hook_usb_wakeup();
  165. }
  166. #ifdef CONSOLE_ENABLE
  167. static bool console_flush = false;
  168. #define CONSOLE_FLUSH_SET(b) do { \
  169. uint8_t sreg = SREG; cli(); console_flush = b; SREG = sreg; \
  170. } while (0)
  171. // called every 1ms
  172. void EVENT_USB_Device_StartOfFrame(void)
  173. {
  174. static uint8_t count;
  175. if (++count % 50) return;
  176. count = 0;
  177. if (!console_flush) return;
  178. Console_Task();
  179. console_flush = false;
  180. }
  181. #endif
  182. /** Event handler for the USB_ConfigurationChanged event.
  183. * This is fired when the host sets the current configuration of the USB device after enumeration.
  184. *
  185. * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4,
  186. * it is safe to use singl bank for all endpoints.
  187. */
  188. void EVENT_USB_Device_ConfigurationChanged(void)
  189. {
  190. print("[c]");
  191. bool ConfigSuccess = true;
  192. /* Setup Keyboard HID Report Endpoints */
  193. ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  194. KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
  195. #ifdef MOUSE_ENABLE
  196. /* Setup Mouse HID Report Endpoint */
  197. ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  198. MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
  199. #endif
  200. #ifdef EXTRAKEY_ENABLE
  201. /* Setup Extra HID Report Endpoint */
  202. ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  203. EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
  204. #endif
  205. #ifdef CONSOLE_ENABLE
  206. /* Setup Console HID Report Endpoints */
  207. ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  208. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  209. #if 0
  210. ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  211. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  212. #endif
  213. #endif
  214. #ifdef NKRO_ENABLE
  215. /* Setup NKRO HID Report Endpoints */
  216. ConfigSuccess &= ENDPOINT_CONFIG(NKRO_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  217. NKRO_EPSIZE, ENDPOINT_BANK_SINGLE);
  218. #endif
  219. }
  220. /*
  221. Appendix G: HID Request Support Requirements
  222. The following table enumerates the requests that need to be supported by various types of HID class devices.
  223. Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  224. ------------------------------------------------------------------------------------------
  225. Boot Mouse Required Optional Optional Optional Required Required
  226. Non-Boot Mouse Required Optional Optional Optional Optional Optional
  227. Boot Keyboard Required Optional Required Required Required Required
  228. Non-Boot Keybrd Required Optional Required Required Optional Optional
  229. Other Device Required Optional Optional Optional Optional Optional
  230. */
  231. /** Event handler for the USB_ControlRequest event.
  232. * This is fired before passing along unhandled control requests to the library for processing internally.
  233. */
  234. void EVENT_USB_Device_ControlRequest(void)
  235. {
  236. print("[r]");
  237. uint8_t* ReportData = NULL;
  238. uint8_t ReportSize = 0;
  239. /* Handle HID Class specific requests */
  240. switch (USB_ControlRequest.bRequest)
  241. {
  242. case HID_REQ_GetReport:
  243. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  244. {
  245. Endpoint_ClearSETUP();
  246. // Interface
  247. switch (USB_ControlRequest.wIndex) {
  248. case KEYBOARD_INTERFACE:
  249. // TODO: test/check
  250. ReportData = (uint8_t*)&keyboard_report_sent;
  251. ReportSize = sizeof(keyboard_report_sent);
  252. break;
  253. }
  254. /* Write the report data to the control endpoint */
  255. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  256. Endpoint_ClearOUT();
  257. }
  258. break;
  259. case HID_REQ_SetReport:
  260. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  261. {
  262. // Interface
  263. switch (USB_ControlRequest.wIndex) {
  264. case KEYBOARD_INTERFACE:
  265. #ifdef NKRO_ENABLE
  266. case NKRO_INTERFACE:
  267. #endif
  268. Endpoint_ClearSETUP();
  269. while (!(Endpoint_IsOUTReceived())) {
  270. if (USB_DeviceState == DEVICE_STATE_Unattached)
  271. return;
  272. }
  273. keyboard_led_stats = Endpoint_Read_8();
  274. Endpoint_ClearOUT();
  275. Endpoint_ClearStatusStage();
  276. break;
  277. }
  278. }
  279. break;
  280. case HID_REQ_GetProtocol:
  281. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  282. {
  283. if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
  284. Endpoint_ClearSETUP();
  285. while (!(Endpoint_IsINReady()));
  286. Endpoint_Write_8(keyboard_protocol);
  287. Endpoint_ClearIN();
  288. Endpoint_ClearStatusStage();
  289. }
  290. }
  291. break;
  292. case HID_REQ_SetProtocol:
  293. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  294. {
  295. if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
  296. Endpoint_ClearSETUP();
  297. Endpoint_ClearStatusStage();
  298. keyboard_protocol = (USB_ControlRequest.wValue & 0xFF);
  299. clear_keyboard();
  300. }
  301. }
  302. break;
  303. case HID_REQ_SetIdle:
  304. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  305. {
  306. Endpoint_ClearSETUP();
  307. Endpoint_ClearStatusStage();
  308. keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
  309. }
  310. break;
  311. case HID_REQ_GetIdle:
  312. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  313. {
  314. Endpoint_ClearSETUP();
  315. while (!(Endpoint_IsINReady()));
  316. Endpoint_Write_8(keyboard_idle);
  317. Endpoint_ClearIN();
  318. Endpoint_ClearStatusStage();
  319. }
  320. break;
  321. }
  322. }
  323. /*******************************************************************************
  324. * Host driver
  325. ******************************************************************************/
  326. static uint8_t keyboard_leds(void)
  327. {
  328. return keyboard_led_stats;
  329. }
  330. static void send_keyboard(report_keyboard_t *report)
  331. {
  332. uint8_t timeout = 255;
  333. if (USB_DeviceState != DEVICE_STATE_Configured)
  334. return;
  335. /* Select the Keyboard Report Endpoint */
  336. #ifdef NKRO_ENABLE
  337. if (keyboard_protocol && keyboard_nkro) {
  338. /* Report protocol - NKRO */
  339. Endpoint_SelectEndpoint(NKRO_IN_EPNUM);
  340. /* Check if write ready for a polling interval around 1ms */
  341. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(4);
  342. if (!Endpoint_IsReadWriteAllowed()) return;
  343. /* Write Keyboard Report Data */
  344. Endpoint_Write_Stream_LE(report, NKRO_EPSIZE, NULL);
  345. }
  346. else
  347. #endif
  348. {
  349. /* Boot protocol */
  350. Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
  351. /* Check if write ready for a polling interval around 10ms */
  352. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  353. if (!Endpoint_IsReadWriteAllowed()) return;
  354. /* Write Keyboard Report Data */
  355. Endpoint_Write_Stream_LE(report, KEYBOARD_EPSIZE, NULL);
  356. }
  357. /* Finalize the stream transfer to send the last packet */
  358. Endpoint_ClearIN();
  359. keyboard_report_sent = *report;
  360. }
  361. static void send_mouse(report_mouse_t *report)
  362. {
  363. #ifdef MOUSE_ENABLE
  364. uint8_t timeout = 255;
  365. if (USB_DeviceState != DEVICE_STATE_Configured)
  366. return;
  367. /* Select the Mouse Report Endpoint */
  368. Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
  369. /* Check if write ready for a polling interval around 10ms */
  370. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  371. if (!Endpoint_IsReadWriteAllowed()) return;
  372. /* Write Mouse Report Data */
  373. Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
  374. /* Finalize the stream transfer to send the last packet */
  375. Endpoint_ClearIN();
  376. #endif
  377. }
  378. static void send_system(uint16_t data)
  379. {
  380. uint8_t timeout = 255;
  381. if (USB_DeviceState != DEVICE_STATE_Configured)
  382. return;
  383. report_extra_t r = {
  384. .report_id = REPORT_ID_SYSTEM,
  385. .usage = data
  386. };
  387. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  388. /* Check if write ready for a polling interval around 10ms */
  389. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  390. if (!Endpoint_IsReadWriteAllowed()) return;
  391. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  392. Endpoint_ClearIN();
  393. }
  394. static void send_consumer(uint16_t data)
  395. {
  396. uint8_t timeout = 255;
  397. if (USB_DeviceState != DEVICE_STATE_Configured)
  398. return;
  399. report_extra_t r = {
  400. .report_id = REPORT_ID_CONSUMER,
  401. .usage = data
  402. };
  403. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  404. /* Check if write ready for a polling interval around 10ms */
  405. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  406. if (!Endpoint_IsReadWriteAllowed()) return;
  407. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  408. Endpoint_ClearIN();
  409. }
  410. /*******************************************************************************
  411. * sendchar
  412. ******************************************************************************/
  413. #ifdef CONSOLE_ENABLE
  414. #define SEND_TIMEOUT 5
  415. int8_t sendchar(uint8_t c)
  416. {
  417. #ifdef LUFA_DEBUG_SUART
  418. xmit(c);
  419. #endif
  420. // Not wait once timeouted.
  421. // Because sendchar() is called so many times, waiting each call causes big lag.
  422. static bool timeouted = false;
  423. // prevents Console_Task() from running during sendchar() runs.
  424. // or char will be lost. These two function is mutually exclusive.
  425. CONSOLE_FLUSH_SET(false);
  426. if (USB_DeviceState != DEVICE_STATE_Configured)
  427. return -1;
  428. uint8_t ep = Endpoint_GetCurrentEndpoint();
  429. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  430. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  431. goto ERROR_EXIT;
  432. }
  433. if (timeouted && !Endpoint_IsReadWriteAllowed()) {
  434. goto ERROR_EXIT;
  435. }
  436. timeouted = false;
  437. uint8_t timeout = SEND_TIMEOUT;
  438. while (!Endpoint_IsReadWriteAllowed()) {
  439. if (USB_DeviceState != DEVICE_STATE_Configured) {
  440. goto ERROR_EXIT;
  441. }
  442. if (Endpoint_IsStalled()) {
  443. goto ERROR_EXIT;
  444. }
  445. if (!(timeout--)) {
  446. timeouted = true;
  447. goto ERROR_EXIT;
  448. }
  449. _delay_ms(1);
  450. }
  451. Endpoint_Write_8(c);
  452. // send when bank is full
  453. if (!Endpoint_IsReadWriteAllowed()) {
  454. while (!(Endpoint_IsINReady()));
  455. Endpoint_ClearIN();
  456. } else {
  457. CONSOLE_FLUSH_SET(true);
  458. }
  459. Endpoint_SelectEndpoint(ep);
  460. return 0;
  461. ERROR_EXIT:
  462. Endpoint_SelectEndpoint(ep);
  463. return -1;
  464. }
  465. #else
  466. int8_t sendchar(uint8_t c)
  467. {
  468. #ifdef LUFA_DEBUG_SUART
  469. xmit(c);
  470. #endif
  471. return 0;
  472. }
  473. #endif
  474. /*******************************************************************************
  475. * main
  476. ******************************************************************************/
  477. static void setup_mcu(void)
  478. {
  479. /* Disable watchdog if enabled by bootloader/fuses */
  480. MCUSR &= ~(1 << WDRF);
  481. wdt_disable();
  482. /* Disable clock division */
  483. clock_prescale_set(clock_div_1);
  484. }
  485. static void setup_usb(void)
  486. {
  487. // Leonardo needs. Without this USB device is not recognized.
  488. USB_Disable();
  489. USB_Init();
  490. // for Console_Task
  491. USB_Device_EnableSOFEvents();
  492. }
  493. int main(void) __attribute__ ((weak));
  494. int main(void)
  495. {
  496. #ifdef LUFA_DEBUG_SUART
  497. DDRD |= (1<<SUART_OUT_BIT);
  498. #endif
  499. print_set_sendchar(sendchar);
  500. setup_mcu();
  501. hook_early_init();
  502. keyboard_setup();
  503. setup_usb();
  504. sei();
  505. /* wait for USB startup & debug output */
  506. while (USB_DeviceState != DEVICE_STATE_Configured) {
  507. #if defined(INTERRUPT_CONTROL_ENDPOINT)
  508. ;
  509. #else
  510. USB_USBTask();
  511. #endif
  512. }
  513. print("USB configured.\n");
  514. /* init modules */
  515. keyboard_init();
  516. host_set_driver(&lufa_driver);
  517. #ifdef SLEEP_LED_ENABLE
  518. sleep_led_init();
  519. #endif
  520. print("Keyboard start.\n");
  521. hook_late_init();
  522. while (1) {
  523. while (USB_DeviceState == DEVICE_STATE_Suspended) {
  524. print("[s]");
  525. hook_usb_suspend_loop();
  526. }
  527. keyboard_task();
  528. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  529. USB_USBTask();
  530. #endif
  531. }
  532. }
  533. /* hooks */
  534. __attribute__((weak))
  535. void hook_early_init(void) {}
  536. __attribute__((weak))
  537. void hook_late_init(void) {}
  538. __attribute__((weak))
  539. void hook_usb_suspend_entry(void)
  540. {
  541. #ifdef SLEEP_LED_ENABLE
  542. sleep_led_enable();
  543. #endif
  544. }
  545. __attribute__((weak))
  546. void hook_usb_suspend_loop(void)
  547. {
  548. suspend_power_down();
  549. if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
  550. USB_Device_SendRemoteWakeup();
  551. }
  552. }
  553. __attribute__((weak))
  554. void hook_usb_wakeup(void)
  555. {
  556. suspend_wakeup_init();
  557. #ifdef SLEEP_LED_ENABLE
  558. sleep_led_disable();
  559. // NOTE: converters may not accept this
  560. led_set(host_keyboard_leds());
  561. #endif
  562. }