Keyboard firmwares for Atmel AVR and Cortex-M
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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