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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 "sendchar.h"
  38. #include "debug.h"
  39. #include "descriptor.h"
  40. #include "lufa.h"
  41. static uint8_t idle_duration = 0;
  42. static uint8_t protocol_report = 1;
  43. static uint8_t keyboard_led_stats = 0;
  44. static report_keyboard_t keyboard_report_sent;
  45. /* Host driver */
  46. static uint8_t keyboard_leds(void);
  47. static void send_keyboard(report_keyboard_t *report);
  48. static void send_mouse(report_mouse_t *report);
  49. static void send_system(uint16_t data);
  50. static void send_consumer(uint16_t data);
  51. static host_driver_t lufa_driver = {
  52. keyboard_leds,
  53. send_keyboard,
  54. send_mouse,
  55. send_system,
  56. send_consumer
  57. };
  58. static void SetupHardware(void);
  59. static void Console_HID_Task(void);
  60. int main(void)
  61. {
  62. SetupHardware();
  63. sei();
  64. print_enable = true;
  65. debug_enable = true;
  66. debug_matrix = true;
  67. debug_keyboard = true;
  68. debug_mouse = true;
  69. // TODO: can't print here
  70. debug("LUFA init\n");
  71. keyboard_init();
  72. host_set_driver(&lufa_driver);
  73. while (1) {
  74. keyboard_proc();
  75. Console_HID_Task();
  76. USB_USBTask();
  77. }
  78. }
  79. void SetupHardware(void)
  80. {
  81. /* Disable watchdog if enabled by bootloader/fuses */
  82. MCUSR &= ~(1 << WDRF);
  83. wdt_disable();
  84. /* Disable clock division */
  85. clock_prescale_set(clock_div_1);
  86. USB_Init();
  87. }
  88. static void Console_HID_Task(void)
  89. {
  90. /* Device must be connected and configured for the task to run */
  91. if (USB_DeviceState != DEVICE_STATE_Configured)
  92. return;
  93. // TODO: impl receivechar()/recvchar()
  94. Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
  95. /* Check to see if a packet has been sent from the host */
  96. if (Endpoint_IsOUTReceived())
  97. {
  98. /* Check to see if the packet contains data */
  99. if (Endpoint_IsReadWriteAllowed())
  100. {
  101. /* Create a temporary buffer to hold the read in report from the host */
  102. uint8_t ConsoleData[CONSOLE_EPSIZE];
  103. /* Read Console Report Data */
  104. Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
  105. /* Process Console Report Data */
  106. //ProcessConsoleHIDReport(ConsoleData);
  107. }
  108. /* Finalize the stream transfer to send the last packet */
  109. Endpoint_ClearOUT();
  110. }
  111. /* IN packet */
  112. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  113. // send IN packet
  114. if (Endpoint_IsINReady())
  115. Endpoint_ClearIN();
  116. }
  117. /*******************************************************************************
  118. * USB Events
  119. ******************************************************************************/
  120. /** Event handler for the USB_Connect event. */
  121. void EVENT_USB_Device_Connect(void)
  122. {
  123. }
  124. /** Event handler for the USB_Disconnect event. */
  125. void EVENT_USB_Device_Disconnect(void)
  126. {
  127. }
  128. /** Event handler for the USB_ConfigurationChanged event.
  129. * This is fired when the host sets the current configuration of the USB device after enumeration.
  130. */
  131. void EVENT_USB_Device_ConfigurationChanged(void)
  132. {
  133. bool ConfigSuccess = true;
  134. /* Setup Keyboard HID Report Endpoints */
  135. ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  136. KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
  137. #ifdef MOUSE_ENABLE
  138. /* Setup Mouse HID Report Endpoint */
  139. ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  140. MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
  141. #endif
  142. #ifdef EXTRAKEY_ENABLE
  143. /* Setup Extra HID Report Endpoint */
  144. ConfigSuccess &= Endpoint_ConfigureEndpoint(EXTRA_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  145. EXTRA_EPSIZE, ENDPOINT_BANK_SINGLE);
  146. #endif
  147. /* Setup Console HID Report Endpoints */
  148. ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  149. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  150. ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  151. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  152. }
  153. /*
  154. Appendix G: HID Request Support Requirements
  155. The following table enumerates the requests that need to be supported by various types of HID class devices.
  156. Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  157. ------------------------------------------------------------------------------------------
  158. Boot Mouse Required Optional Optional Optional Required Required
  159. Non-Boot Mouse Required Optional Optional Optional Optional Optional
  160. Boot Keyboard Required Optional Required Required Required Required
  161. Non-Boot Keybrd Required Optional Required Required Optional Optional
  162. Other Device Required Optional Optional Optional Optional Optional
  163. */
  164. /** Event handler for the USB_ControlRequest event.
  165. * This is fired before passing along unhandled control requests to the library for processing internally.
  166. */
  167. void EVENT_USB_Device_ControlRequest(void)
  168. {
  169. uint8_t* ReportData = NULL;
  170. uint8_t ReportSize = 0;
  171. /* Handle HID Class specific requests */
  172. switch (USB_ControlRequest.bRequest)
  173. {
  174. case HID_REQ_GetReport:
  175. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  176. {
  177. Endpoint_ClearSETUP();
  178. // Interface
  179. switch (USB_ControlRequest.wIndex) {
  180. case KEYBOARD_INTERFACE:
  181. // TODO: test/check
  182. ReportData = (uint8_t*)&keyboard_report_sent;
  183. ReportSize = sizeof(keyboard_report_sent);
  184. break;
  185. }
  186. /* Write the report data to the control endpoint */
  187. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  188. Endpoint_ClearOUT();
  189. }
  190. break;
  191. case HID_REQ_SetReport:
  192. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  193. {
  194. // Interface
  195. switch (USB_ControlRequest.wIndex) {
  196. case KEYBOARD_INTERFACE:
  197. Endpoint_ClearSETUP();
  198. while (!(Endpoint_IsOUTReceived())) {
  199. if (USB_DeviceState == DEVICE_STATE_Unattached)
  200. return;
  201. }
  202. keyboard_led_stats = Endpoint_Read_8();
  203. Endpoint_ClearOUT();
  204. Endpoint_ClearStatusStage();
  205. break;
  206. }
  207. }
  208. break;
  209. case HID_REQ_GetProtocol:
  210. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  211. {
  212. Endpoint_ClearSETUP();
  213. while (!(Endpoint_IsINReady()));
  214. Endpoint_Write_8(protocol_report);
  215. Endpoint_ClearIN();
  216. Endpoint_ClearStatusStage();
  217. }
  218. break;
  219. case HID_REQ_SetProtocol:
  220. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  221. {
  222. Endpoint_ClearSETUP();
  223. Endpoint_ClearStatusStage();
  224. protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
  225. }
  226. break;
  227. case HID_REQ_SetIdle:
  228. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  229. {
  230. Endpoint_ClearSETUP();
  231. Endpoint_ClearStatusStage();
  232. idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
  233. }
  234. break;
  235. case HID_REQ_GetIdle:
  236. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  237. {
  238. Endpoint_ClearSETUP();
  239. while (!(Endpoint_IsINReady()));
  240. Endpoint_Write_8(idle_duration);
  241. Endpoint_ClearIN();
  242. Endpoint_ClearStatusStage();
  243. }
  244. break;
  245. }
  246. }
  247. /*******************************************************************************
  248. * Host driver
  249. ******************************************************************************/
  250. static uint8_t keyboard_leds(void)
  251. {
  252. return keyboard_led_stats;
  253. }
  254. static void send_keyboard(report_keyboard_t *report)
  255. {
  256. // TODO: handle NKRO report
  257. /* Select the Keyboard Report Endpoint */
  258. Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
  259. /* Check if Keyboard Endpoint Ready for Read/Write */
  260. if (Endpoint_IsReadWriteAllowed())
  261. {
  262. /* Write Keyboard Report Data */
  263. Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
  264. /* Finalize the stream transfer to send the last packet */
  265. Endpoint_ClearIN();
  266. }
  267. keyboard_report_sent = *report;
  268. }
  269. static void send_mouse(report_mouse_t *report)
  270. {
  271. #ifdef MOUSE_ENABLE
  272. /* Select the Mouse Report Endpoint */
  273. Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
  274. /* Check if Mouse Endpoint Ready for Read/Write */
  275. if (Endpoint_IsReadWriteAllowed())
  276. {
  277. /* Write Mouse Report Data */
  278. Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
  279. /* Finalize the stream transfer to send the last packet */
  280. Endpoint_ClearIN();
  281. }
  282. #endif
  283. }
  284. static void send_system(uint16_t data)
  285. {
  286. report_extra_t r = {
  287. .report_id = REPORT_ID_SYSTEM,
  288. .usage = data
  289. };
  290. Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
  291. if (Endpoint_IsReadWriteAllowed()) {
  292. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  293. Endpoint_ClearIN();
  294. }
  295. }
  296. static void send_consumer(uint16_t data)
  297. {
  298. report_extra_t r = {
  299. .report_id = REPORT_ID_CONSUMER,
  300. .usage = data
  301. };
  302. Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
  303. if (Endpoint_IsReadWriteAllowed()) {
  304. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  305. Endpoint_ClearIN();
  306. }
  307. }
  308. /*******************************************************************************
  309. * sendchar
  310. ******************************************************************************/
  311. int8_t sendchar(uint8_t c)
  312. {
  313. if (USB_DeviceState != DEVICE_STATE_Configured)
  314. return -1;
  315. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  316. uint8_t timeout = 10;
  317. uint16_t prevFN = USB_Device_GetFrameNumber();
  318. while (!Endpoint_IsINReady()) {
  319. switch (USB_DeviceState) {
  320. case DEVICE_STATE_Unattached:
  321. case DEVICE_STATE_Suspended:
  322. return -1;
  323. }
  324. if (Endpoint_IsStalled())
  325. return -1;
  326. if (prevFN != USB_Device_GetFrameNumber()) {
  327. if (!(timeout--))
  328. return -1;
  329. prevFN = USB_Device_GetFrameNumber();
  330. }
  331. }
  332. Endpoint_Write_8(c);
  333. // send when packet is full
  334. if (!Endpoint_IsReadWriteAllowed())
  335. Endpoint_ClearIN();
  336. return 0;
  337. }