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 #ifdef SERIAL_MOUSE_ENABLE
53 #include "serial_mouse.h"
56 #include "descriptor.h"
59 uint8_t keyboard_idle = 0;
60 uint8_t keyboard_protocol = 1;
61 static uint8_t keyboard_led_stats = 0;
63 static report_keyboard_t keyboard_report_sent;
67 static uint8_t keyboard_leds(void);
68 static void send_keyboard(report_keyboard_t *report);
69 static void send_mouse(report_mouse_t *report);
70 static void send_system(uint16_t data);
71 static void send_consumer(uint16_t data);
72 host_driver_t lufa_driver = {
81 /*******************************************************************************
83 ******************************************************************************/
85 static void Console_Task(void)
87 /* Device must be connected and configured for the task to run */
88 if (USB_DeviceState != DEVICE_STATE_Configured)
91 uint8_t ep = Endpoint_GetCurrentEndpoint();
94 // TODO: impl receivechar()/recvchar()
95 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
97 /* Check to see if a packet has been sent from the host */
98 if (Endpoint_IsOUTReceived())
100 /* Check to see if the packet contains data */
101 if (Endpoint_IsReadWriteAllowed())
103 /* Create a temporary buffer to hold the read in report from the host */
104 uint8_t ConsoleData[CONSOLE_EPSIZE];
106 /* Read Console Report Data */
107 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
109 /* Process Console Report Data */
110 //ProcessConsoleHIDReport(ConsoleData);
113 /* Finalize the stream transfer to send the last packet */
119 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
120 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
121 Endpoint_SelectEndpoint(ep);
126 while (Endpoint_IsReadWriteAllowed())
129 // flash senchar packet
130 if (Endpoint_IsINReady()) {
134 Endpoint_SelectEndpoint(ep);
137 static void Console_Task(void)
143 /*******************************************************************************
145 ******************************************************************************/
147 * Event Order of Plug in:
148 * 0) EVENT_USB_Device_Connect
149 * 1) EVENT_USB_Device_Suspend
150 * 2) EVENT_USB_Device_Reset
151 * 3) EVENT_USB_Device_Wake
153 void EVENT_USB_Device_Connect(void)
157 void EVENT_USB_Device_Disconnect(void)
161 void EVENT_USB_Device_Reset(void)
165 void EVENT_USB_Device_Suspend()
167 #ifdef SLEEP_LED_ENABLE
172 void EVENT_USB_Device_WakeUp()
174 suspend_wakeup_init();
176 #ifdef SLEEP_LED_ENABLE
178 // NOTE: converters may not accept this
179 led_set(host_keyboard_leds());
183 void EVENT_USB_Device_StartOfFrame(void)
188 /** Event handler for the USB_ConfigurationChanged event.
189 * This is fired when the host sets the current configuration of the USB device after enumeration.
191 void EVENT_USB_Device_ConfigurationChanged(void)
193 bool ConfigSuccess = true;
195 /* Setup Keyboard HID Report Endpoints */
196 ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
197 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
200 /* Setup Mouse HID Report Endpoint */
201 ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
202 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
205 #ifdef EXTRAKEY_ENABLE
206 /* Setup Extra HID Report Endpoint */
207 ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
208 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
211 #ifdef CONSOLE_ENABLE
212 /* Setup Console HID Report Endpoints */
213 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
214 CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
216 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
217 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
222 /* Setup NKRO HID Report Endpoints */
223 ConfigSuccess &= ENDPOINT_CONFIG(NKRO_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
224 NKRO_EPSIZE, ENDPOINT_BANK_SINGLE);
229 Appendix G: HID Request Support Requirements
231 The following table enumerates the requests that need to be supported by various types of HID class devices.
233 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
234 ------------------------------------------------------------------------------------------
235 Boot Mouse Required Optional Optional Optional Required Required
236 Non-Boot Mouse Required Optional Optional Optional Optional Optional
237 Boot Keyboard Required Optional Required Required Required Required
238 Non-Boot Keybrd Required Optional Required Required Optional Optional
239 Other Device Required Optional Optional Optional Optional Optional
241 /** Event handler for the USB_ControlRequest event.
242 * This is fired before passing along unhandled control requests to the library for processing internally.
244 void EVENT_USB_Device_ControlRequest(void)
246 uint8_t* ReportData = NULL;
247 uint8_t ReportSize = 0;
249 /* Handle HID Class specific requests */
250 switch (USB_ControlRequest.bRequest)
252 case HID_REQ_GetReport:
253 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
255 Endpoint_ClearSETUP();
258 switch (USB_ControlRequest.wIndex) {
259 case KEYBOARD_INTERFACE:
261 ReportData = (uint8_t*)&keyboard_report_sent;
262 ReportSize = sizeof(keyboard_report_sent);
266 /* Write the report data to the control endpoint */
267 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
272 case HID_REQ_SetReport:
273 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
277 switch (USB_ControlRequest.wIndex) {
278 case KEYBOARD_INTERFACE:
282 Endpoint_ClearSETUP();
284 while (!(Endpoint_IsOUTReceived())) {
285 if (USB_DeviceState == DEVICE_STATE_Unattached)
288 keyboard_led_stats = Endpoint_Read_8();
291 Endpoint_ClearStatusStage();
299 case HID_REQ_GetProtocol:
300 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
302 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
303 Endpoint_ClearSETUP();
304 while (!(Endpoint_IsINReady()));
305 Endpoint_Write_8(keyboard_protocol);
307 Endpoint_ClearStatusStage();
312 case HID_REQ_SetProtocol:
313 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
315 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
316 Endpoint_ClearSETUP();
317 Endpoint_ClearStatusStage();
319 keyboard_protocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
321 keyboard_nkro = !!keyboard_protocol;
328 case HID_REQ_SetIdle:
329 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
331 Endpoint_ClearSETUP();
332 Endpoint_ClearStatusStage();
334 keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
338 case HID_REQ_GetIdle:
339 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
341 Endpoint_ClearSETUP();
342 while (!(Endpoint_IsINReady()));
343 Endpoint_Write_8(keyboard_idle);
345 Endpoint_ClearStatusStage();
352 /*******************************************************************************
354 ******************************************************************************/
355 static uint8_t keyboard_leds(void)
357 return keyboard_led_stats;
360 static void send_keyboard(report_keyboard_t *report)
362 uint8_t timeout = 255;
364 if (USB_DeviceState != DEVICE_STATE_Configured)
367 /* Select the Keyboard Report Endpoint */
370 /* Report protocol - NKRO */
371 Endpoint_SelectEndpoint(NKRO_IN_EPNUM);
373 /* Check if write ready for a polling interval around 1ms */
374 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(4);
375 if (!Endpoint_IsReadWriteAllowed()) return;
377 /* Write Keyboard Report Data */
378 Endpoint_Write_Stream_LE(report, NKRO_EPSIZE, NULL);
384 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
386 /* Check if write ready for a polling interval around 10ms */
387 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
388 if (!Endpoint_IsReadWriteAllowed()) return;
390 /* Write Keyboard Report Data */
391 Endpoint_Write_Stream_LE(report, KEYBOARD_EPSIZE, NULL);
394 /* Finalize the stream transfer to send the last packet */
397 keyboard_report_sent = *report;
400 static void send_mouse(report_mouse_t *report)
403 uint8_t timeout = 255;
405 if (USB_DeviceState != DEVICE_STATE_Configured)
408 /* Select the Mouse Report Endpoint */
409 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
411 /* Check if write ready for a polling interval around 10ms */
412 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
413 if (!Endpoint_IsReadWriteAllowed()) return;
415 /* Write Mouse Report Data */
416 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
418 /* Finalize the stream transfer to send the last packet */
423 static void send_system(uint16_t data)
425 uint8_t timeout = 255;
427 if (USB_DeviceState != DEVICE_STATE_Configured)
431 .report_id = REPORT_ID_SYSTEM,
434 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
436 /* Check if write ready for a polling interval around 10ms */
437 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
438 if (!Endpoint_IsReadWriteAllowed()) return;
440 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
444 static void send_consumer(uint16_t data)
446 uint8_t timeout = 255;
448 if (USB_DeviceState != DEVICE_STATE_Configured)
452 .report_id = REPORT_ID_CONSUMER,
455 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
457 /* Check if write ready for a polling interval around 10ms */
458 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
459 if (!Endpoint_IsReadWriteAllowed()) return;
461 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
466 /*******************************************************************************
468 ******************************************************************************/
469 #ifdef CONSOLE_ENABLE
470 #define SEND_TIMEOUT 5
471 int8_t sendchar(uint8_t c)
473 // Not wait once timeouted.
474 // Because sendchar() is called so many times, waiting each call causes big lag.
475 static bool timeouted = false;
477 if (USB_DeviceState != DEVICE_STATE_Configured)
480 uint8_t ep = Endpoint_GetCurrentEndpoint();
481 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
482 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
483 Endpoint_SelectEndpoint(ep);
487 if (timeouted && !Endpoint_IsReadWriteAllowed()) {
488 Endpoint_SelectEndpoint(ep);
494 uint8_t timeout = SEND_TIMEOUT;
495 uint16_t prevFN = USB_Device_GetFrameNumber();
496 while (!Endpoint_IsReadWriteAllowed()) {
497 switch (USB_DeviceState) {
498 case DEVICE_STATE_Unattached:
499 case DEVICE_STATE_Suspended:
502 if (Endpoint_IsStalled()) {
503 Endpoint_SelectEndpoint(ep);
506 if (prevFN != USB_Device_GetFrameNumber()) {
509 Endpoint_SelectEndpoint(ep);
512 prevFN = USB_Device_GetFrameNumber();
518 // send when bank is full
519 if (!Endpoint_IsReadWriteAllowed())
522 Endpoint_SelectEndpoint(ep);
526 int8_t sendchar(uint8_t c)
533 /*******************************************************************************
535 ******************************************************************************/
536 static void SetupHardware(void)
538 /* Disable watchdog if enabled by bootloader/fuses */
539 MCUSR &= ~(1 << WDRF);
542 /* Disable clock division */
543 clock_prescale_set(clock_div_1);
545 // Leonardo needs. Without this USB device is not recognized.
551 USB_Device_EnableSOFEvents();
552 print_set_sendchar(sendchar);
555 int main(void) __attribute__ ((weak));
561 /* wait for USB startup & debug output */
562 while (USB_DeviceState != DEVICE_STATE_Configured) {
563 #if defined(INTERRUPT_CONTROL_ENDPOINT)
569 print("USB configured.\n");
573 host_set_driver(&lufa_driver);
574 #ifdef SLEEP_LED_ENABLE
578 #ifdef SERIAL_MOUSE_ENABLE
582 print("Keyboard start.\n");
584 while (USB_DeviceState == DEVICE_STATE_Suspended) {
585 suspend_power_down();
586 if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
587 USB_Device_SendRemoteWakeup();
593 #ifdef SERIAL_MOUSE_ENABLE
597 #if !defined(INTERRUPT_CONTROL_ENDPOINT)