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

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