2 * Copyright 2012 Jun Wako <wakojun@gmail.com>
3 * This file is based on:
4 * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
5 * LUFA-120219/Demos/Device/Lowlevel/GenericHID
10 Copyright (C) Dean Camera, 2012.
12 dean [at] fourwalledcubicle [dot] com
17 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
18 Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
20 Permission to use, copy, modify, distribute, and sell this
21 software and its documentation for any purpose is hereby granted
22 without fee, provided that the above copyright notice appear in
23 all copies and that both that the copyright notice and this
24 permission notice and warranty disclaimer appear in supporting
25 documentation, and that the name of the author not be used in
26 advertising or publicity pertaining to distribution of the
27 software without specific, written prior permission.
29 The author disclaim all warranties with regard to this
30 software, including all implied warranties of merchantability
31 and fitness. In no event shall the author be liable for any
32 special, indirect or consequential damages or any damages
33 whatsoever resulting from loss of use, data or profits, whether
34 in an action of contract, negligence or other tortious action,
35 arising out of or in connection with the use or performance of
41 #include "host_driver.h"
47 #ifdef SLEEP_LED_ENABLE
48 #include "sleep_led.h"
52 #include "descriptor.h"
55 uint8_t keyboard_idle = 0;
56 uint8_t keyboard_protocol = 1;
57 static uint8_t keyboard_led_stats = 0;
59 static report_keyboard_t keyboard_report_sent;
63 static uint8_t keyboard_leds(void);
64 static void send_keyboard(report_keyboard_t *report);
65 static void send_mouse(report_mouse_t *report);
66 static void send_system(uint16_t data);
67 static void send_consumer(uint16_t data);
68 host_driver_t lufa_driver = {
77 /*******************************************************************************
79 ******************************************************************************/
81 static void Console_Task(void)
83 /* Device must be connected and configured for the task to run */
84 if (USB_DeviceState != DEVICE_STATE_Configured)
87 uint8_t ep = Endpoint_GetCurrentEndpoint();
90 // TODO: impl receivechar()/recvchar()
91 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
93 /* Check to see if a packet has been sent from the host */
94 if (Endpoint_IsOUTReceived())
96 /* Check to see if the packet contains data */
97 if (Endpoint_IsReadWriteAllowed())
99 /* Create a temporary buffer to hold the read in report from the host */
100 uint8_t ConsoleData[CONSOLE_EPSIZE];
102 /* Read Console Report Data */
103 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
105 /* Process Console Report Data */
106 //ProcessConsoleHIDReport(ConsoleData);
109 /* Finalize the stream transfer to send the last packet */
115 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
116 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
117 Endpoint_SelectEndpoint(ep);
122 while (Endpoint_IsReadWriteAllowed())
125 // flash senchar packet
126 if (Endpoint_IsINReady()) {
130 Endpoint_SelectEndpoint(ep);
133 static void Console_Task(void)
139 /*******************************************************************************
141 ******************************************************************************/
143 * Event Order of Plug in:
144 * 0) EVENT_USB_Device_Connect
145 * 1) EVENT_USB_Device_Suspend
146 * 2) EVENT_USB_Device_Reset
147 * 3) EVENT_USB_Device_Wake
149 void EVENT_USB_Device_Connect(void)
153 void EVENT_USB_Device_Disconnect(void)
157 void EVENT_USB_Device_Reset(void)
161 void EVENT_USB_Device_Suspend()
163 #ifdef SLEEP_LED_ENABLE
168 void EVENT_USB_Device_WakeUp()
170 suspend_wakeup_init();
172 #ifdef SLEEP_LED_ENABLE
174 // NOTE: converters may not accept this
175 led_set(host_keyboard_leds());
179 void EVENT_USB_Device_StartOfFrame(void)
184 /** Event handler for the USB_ConfigurationChanged event.
185 * This is fired when the host sets the current configuration of the USB device after enumeration.
187 void EVENT_USB_Device_ConfigurationChanged(void)
189 bool ConfigSuccess = true;
191 /* Setup Keyboard HID Report Endpoints */
192 ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
193 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
196 /* Setup Mouse HID Report Endpoint */
197 ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
198 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
201 #ifdef EXTRAKEY_ENABLE
202 /* Setup Extra HID Report Endpoint */
203 ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
204 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
207 #ifdef CONSOLE_ENABLE
208 /* Setup Console HID Report Endpoints */
209 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
210 CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
212 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
213 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
218 /* Setup NKRO HID Report Endpoints */
219 ConfigSuccess &= ENDPOINT_CONFIG(NKRO_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
220 NKRO_EPSIZE, ENDPOINT_BANK_SINGLE);
225 Appendix G: HID Request Support Requirements
227 The following table enumerates the requests that need to be supported by various types of HID class devices.
229 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
230 ------------------------------------------------------------------------------------------
231 Boot Mouse Required Optional Optional Optional Required Required
232 Non-Boot Mouse Required Optional Optional Optional Optional Optional
233 Boot Keyboard Required Optional Required Required Required Required
234 Non-Boot Keybrd Required Optional Required Required Optional Optional
235 Other Device Required Optional Optional Optional Optional Optional
237 /** Event handler for the USB_ControlRequest event.
238 * This is fired before passing along unhandled control requests to the library for processing internally.
240 void EVENT_USB_Device_ControlRequest(void)
242 uint8_t* ReportData = NULL;
243 uint8_t ReportSize = 0;
245 /* Handle HID Class specific requests */
246 switch (USB_ControlRequest.bRequest)
248 case HID_REQ_GetReport:
249 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
251 Endpoint_ClearSETUP();
254 switch (USB_ControlRequest.wIndex) {
255 case KEYBOARD_INTERFACE:
257 ReportData = (uint8_t*)&keyboard_report_sent;
258 ReportSize = sizeof(keyboard_report_sent);
262 /* Write the report data to the control endpoint */
263 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
268 case HID_REQ_SetReport:
269 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
273 switch (USB_ControlRequest.wIndex) {
274 case KEYBOARD_INTERFACE:
275 Endpoint_ClearSETUP();
277 while (!(Endpoint_IsOUTReceived())) {
278 if (USB_DeviceState == DEVICE_STATE_Unattached)
281 keyboard_led_stats = Endpoint_Read_8();
284 Endpoint_ClearStatusStage();
292 case HID_REQ_GetProtocol:
293 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
295 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
296 Endpoint_ClearSETUP();
297 while (!(Endpoint_IsINReady()));
298 Endpoint_Write_8(keyboard_protocol);
300 Endpoint_ClearStatusStage();
305 case HID_REQ_SetProtocol:
306 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
308 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
309 Endpoint_ClearSETUP();
310 Endpoint_ClearStatusStage();
312 keyboard_protocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
314 keyboard_nkro = !!keyboard_protocol;
321 case HID_REQ_SetIdle:
322 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
324 Endpoint_ClearSETUP();
325 Endpoint_ClearStatusStage();
327 keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
331 case HID_REQ_GetIdle:
332 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
334 Endpoint_ClearSETUP();
335 while (!(Endpoint_IsINReady()));
336 Endpoint_Write_8(keyboard_idle);
338 Endpoint_ClearStatusStage();
345 /*******************************************************************************
347 ******************************************************************************/
348 static uint8_t keyboard_leds(void)
350 return keyboard_led_stats;
353 static void send_keyboard(report_keyboard_t *report)
355 uint8_t timeout = 255;
357 if (USB_DeviceState != DEVICE_STATE_Configured)
360 /* Select the Keyboard Report Endpoint */
363 /* Report protocol - NKRO */
364 Endpoint_SelectEndpoint(NKRO_IN_EPNUM);
366 /* Check if write ready for a polling interval around 1ms */
367 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(4);
368 if (!Endpoint_IsReadWriteAllowed()) return;
370 /* Write Keyboard Report Data */
371 Endpoint_Write_Stream_LE(report, NKRO_EPSIZE, NULL);
377 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
379 /* Check if write ready for a polling interval around 10ms */
380 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
381 if (!Endpoint_IsReadWriteAllowed()) return;
383 /* Write Keyboard Report Data */
384 Endpoint_Write_Stream_LE(report, KEYBOARD_EPSIZE, NULL);
387 /* Finalize the stream transfer to send the last packet */
390 keyboard_report_sent = *report;
393 static void send_mouse(report_mouse_t *report)
396 uint8_t timeout = 255;
398 if (USB_DeviceState != DEVICE_STATE_Configured)
401 /* Select the Mouse Report Endpoint */
402 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
404 /* Check if write ready for a polling interval around 10ms */
405 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
406 if (!Endpoint_IsReadWriteAllowed()) return;
408 /* Write Mouse Report Data */
409 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
411 /* Finalize the stream transfer to send the last packet */
416 static void send_system(uint16_t data)
418 uint8_t timeout = 255;
420 if (USB_DeviceState != DEVICE_STATE_Configured)
424 .report_id = REPORT_ID_SYSTEM,
427 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
429 /* Check if write ready for a polling interval around 10ms */
430 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
431 if (!Endpoint_IsReadWriteAllowed()) return;
433 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
437 static void send_consumer(uint16_t data)
439 uint8_t timeout = 255;
441 if (USB_DeviceState != DEVICE_STATE_Configured)
445 .report_id = REPORT_ID_CONSUMER,
448 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
450 /* Check if write ready for a polling interval around 10ms */
451 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
452 if (!Endpoint_IsReadWriteAllowed()) return;
454 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
459 /*******************************************************************************
461 ******************************************************************************/
462 #ifdef CONSOLE_ENABLE
463 #define SEND_TIMEOUT 5
464 int8_t sendchar(uint8_t c)
466 // Not wait once timeouted.
467 // Because sendchar() is called so many times, waiting each call causes big lag.
468 static bool timeouted = false;
470 if (USB_DeviceState != DEVICE_STATE_Configured)
473 uint8_t ep = Endpoint_GetCurrentEndpoint();
474 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
475 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
476 Endpoint_SelectEndpoint(ep);
480 if (timeouted && !Endpoint_IsReadWriteAllowed()) {
481 Endpoint_SelectEndpoint(ep);
487 uint8_t timeout = SEND_TIMEOUT;
488 uint16_t prevFN = USB_Device_GetFrameNumber();
489 while (!Endpoint_IsReadWriteAllowed()) {
490 switch (USB_DeviceState) {
491 case DEVICE_STATE_Unattached:
492 case DEVICE_STATE_Suspended:
495 if (Endpoint_IsStalled()) {
496 Endpoint_SelectEndpoint(ep);
499 if (prevFN != USB_Device_GetFrameNumber()) {
502 Endpoint_SelectEndpoint(ep);
505 prevFN = USB_Device_GetFrameNumber();
511 // send when bank is full
512 if (!Endpoint_IsReadWriteAllowed())
515 Endpoint_SelectEndpoint(ep);
519 int8_t sendchar(uint8_t c)
526 /*******************************************************************************
528 ******************************************************************************/
529 static void SetupHardware(void)
531 /* Disable watchdog if enabled by bootloader/fuses */
532 MCUSR &= ~(1 << WDRF);
535 /* Disable clock division */
536 clock_prescale_set(clock_div_1);
538 // Leonardo needs. Without this USB device is not recognized.
544 USB_Device_EnableSOFEvents();
545 print_set_sendchar(sendchar);
548 int main(void) __attribute__ ((weak));
554 /* wait for USB startup & debug output */
555 while (USB_DeviceState != DEVICE_STATE_Configured) {
556 #if defined(INTERRUPT_CONTROL_ENDPOINT)
562 print("USB configured.\n");
566 host_set_driver(&lufa_driver);
567 #ifdef SLEEP_LED_ENABLE
571 print("Keyboard start.\n");
573 while (USB_DeviceState == DEVICE_STATE_Suspended) {
574 suspend_power_down();
575 if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
576 USB_Device_SendRemoteWakeup();
582 #if !defined(INTERRUPT_CONTROL_ENDPOINT)