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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 keyboard_led_stats = 0;
  42. // TODO: impl Control Request GET_REPORT
  43. static report_keyboard_t keyboard_report_sent;
  44. static report_mouse_t mouse_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. _delay_ms(5000);
  71. USB_USBTask();
  72. print("abcdefg\n");
  73. USB_USBTask();
  74. */
  75. keyboard_init();
  76. host_set_driver(&lufa_driver);
  77. while (1) {
  78. keyboard_proc();
  79. Console_HID_Task();
  80. USB_USBTask();
  81. }
  82. }
  83. void SetupHardware(void)
  84. {
  85. /* Disable watchdog if enabled by bootloader/fuses */
  86. MCUSR &= ~(1 << WDRF);
  87. wdt_disable();
  88. /* Disable clock division */
  89. clock_prescale_set(clock_div_1);
  90. USB_Init();
  91. }
  92. static void Console_HID_Task(void)
  93. {
  94. /* Device must be connected and configured for the task to run */
  95. if (USB_DeviceState != DEVICE_STATE_Configured)
  96. return;
  97. // TODO: impl receivechar()/recvchar()
  98. Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
  99. /* Check to see if a packet has been sent from the host */
  100. if (Endpoint_IsOUTReceived())
  101. {
  102. /* Check to see if the packet contains data */
  103. if (Endpoint_IsReadWriteAllowed())
  104. {
  105. /* Create a temporary buffer to hold the read in report from the host */
  106. uint8_t ConsoleData[CONSOLE_EPSIZE];
  107. /* Read Console Report Data */
  108. Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
  109. /* Process Console Report Data */
  110. //ProcessConsoleHIDReport(ConsoleData);
  111. }
  112. /* Finalize the stream transfer to send the last packet */
  113. Endpoint_ClearOUT();
  114. }
  115. /* IN packet */
  116. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  117. // send IN packet
  118. if (Endpoint_IsINReady())
  119. Endpoint_ClearIN();
  120. }
  121. /*******************************************************************************
  122. * USB Events
  123. ******************************************************************************/
  124. /** Event handler for the USB_Connect event. */
  125. void EVENT_USB_Device_Connect(void)
  126. {
  127. }
  128. /** Event handler for the USB_Disconnect event. */
  129. void EVENT_USB_Device_Disconnect(void)
  130. {
  131. }
  132. /** Event handler for the USB_ConfigurationChanged event.
  133. * This is fired when the host sets the current configuration of the USB device after enumeration.
  134. */
  135. void EVENT_USB_Device_ConfigurationChanged(void)
  136. {
  137. bool ConfigSuccess = true;
  138. /* Setup Keyboard HID Report Endpoints */
  139. ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  140. KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
  141. /* Setup Mouse HID Report Endpoint */
  142. ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  143. MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
  144. /* Setup Console HID Report Endpoints */
  145. ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  146. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  147. ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  148. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  149. /* Setup Extra HID Report Endpoint */
  150. ConfigSuccess &= Endpoint_ConfigureEndpoint(EXTRA_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  151. EXTRA_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. case MOUSE_INTERFACE:
  186. // TODO: test/check
  187. ReportData = (uint8_t*)&mouse_report_sent;
  188. ReportSize = sizeof(mouse_report_sent);
  189. break;
  190. case CONSOLE_INTERFACE:
  191. break;
  192. case EXTRA_INTERFACE:
  193. break;
  194. }
  195. /* Write the report data to the control endpoint */
  196. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  197. Endpoint_ClearOUT();
  198. }
  199. break;
  200. case HID_REQ_SetReport:
  201. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  202. {
  203. Endpoint_ClearSETUP();
  204. /* Wait until the LED report has been sent by the host */
  205. while (!(Endpoint_IsOUTReceived()))
  206. {
  207. if (USB_DeviceState == DEVICE_STATE_Unattached)
  208. return;
  209. }
  210. // Interface
  211. switch (USB_ControlRequest.wIndex) {
  212. case KEYBOARD_INTERFACE:
  213. // TODO: test/check
  214. /* Read in the LED report from the host */
  215. keyboard_led_stats = Endpoint_Read_8();
  216. break;
  217. case MOUSE_INTERFACE:
  218. break;
  219. case CONSOLE_INTERFACE:
  220. break;
  221. case EXTRA_INTERFACE:
  222. break;
  223. }
  224. Endpoint_ClearOUT();
  225. Endpoint_ClearStatusStage();
  226. }
  227. break;
  228. }
  229. }
  230. /*******************************************************************************
  231. * Host driver
  232. ******************************************************************************/
  233. static uint8_t keyboard_leds(void)
  234. {
  235. return keyboard_led_stats;
  236. }
  237. static void send_keyboard(report_keyboard_t *report)
  238. {
  239. // TODO: handle NKRO report
  240. /* Select the Keyboard Report Endpoint */
  241. Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
  242. /* Check if Keyboard Endpoint Ready for Read/Write */
  243. if (Endpoint_IsReadWriteAllowed())
  244. {
  245. /* Write Keyboard Report Data */
  246. Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
  247. /* Finalize the stream transfer to send the last packet */
  248. Endpoint_ClearIN();
  249. }
  250. keyboard_report_sent = *report;
  251. }
  252. static void send_mouse(report_mouse_t *report)
  253. {
  254. /* Select the Mouse Report Endpoint */
  255. Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
  256. /* Check if Mouse Endpoint Ready for Read/Write */
  257. if (Endpoint_IsReadWriteAllowed())
  258. {
  259. /* Write Mouse Report Data */
  260. Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
  261. /* Finalize the stream transfer to send the last packet */
  262. Endpoint_ClearIN();
  263. }
  264. mouse_report_sent = *report;
  265. }
  266. typedef struct {
  267. uint8_t report_id;
  268. uint16_t usage;
  269. } __attribute__ ((packed)) report_extra_t;
  270. static void send_system(uint16_t data)
  271. {
  272. Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
  273. if (Endpoint_IsReadWriteAllowed()) {
  274. report_extra_t r = {
  275. .report_id = REPORT_ID_SYSTEM,
  276. .usage = data
  277. };
  278. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  279. Endpoint_ClearIN();
  280. }
  281. }
  282. static void send_consumer(uint16_t data)
  283. {
  284. Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
  285. if (Endpoint_IsReadWriteAllowed()) {
  286. report_extra_t r = {
  287. .report_id = REPORT_ID_CONSUMER,
  288. .usage = data
  289. };
  290. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  291. Endpoint_ClearIN();
  292. }
  293. }
  294. /*******************************************************************************
  295. * sendchar
  296. ******************************************************************************/
  297. int8_t sendchar(uint8_t c)
  298. {
  299. if (USB_DeviceState != DEVICE_STATE_Configured)
  300. return -1;
  301. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  302. uint8_t timeout = 10;
  303. uint16_t prevFN = USB_Device_GetFrameNumber();
  304. while (!Endpoint_IsINReady()) {
  305. switch (USB_DeviceState) {
  306. case DEVICE_STATE_Unattached:
  307. case DEVICE_STATE_Suspended:
  308. return -1;
  309. }
  310. if (Endpoint_IsStalled())
  311. return -1;
  312. if (prevFN != USB_Device_GetFrameNumber()) {
  313. if (!(timeout--))
  314. return -1;
  315. prevFN = USB_Device_GetFrameNumber();
  316. }
  317. }
  318. Endpoint_Write_8(c);
  319. // send when packet is full
  320. if (!Endpoint_IsReadWriteAllowed())
  321. Endpoint_ClearIN();
  322. return 0;
  323. }