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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

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