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);
211 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
212 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
216 /* Setup NKRO HID Report Endpoints */
217 ConfigSuccess &= ENDPOINT_CONFIG(NKRO_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
218 NKRO_EPSIZE, ENDPOINT_BANK_SINGLE);
223 Appendix G: HID Request Support Requirements
225 The following table enumerates the requests that need to be supported by various types of HID class devices.
227 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
228 ------------------------------------------------------------------------------------------
229 Boot Mouse Required Optional Optional Optional Required Required
230 Non-Boot Mouse Required Optional Optional Optional Optional Optional
231 Boot Keyboard Required Optional Required Required Required Required
232 Non-Boot Keybrd Required Optional Required Required Optional Optional
233 Other Device Required Optional Optional Optional Optional Optional
235 /** Event handler for the USB_ControlRequest event.
236 * This is fired before passing along unhandled control requests to the library for processing internally.
238 void EVENT_USB_Device_ControlRequest(void)
240 uint8_t* ReportData = NULL;
241 uint8_t ReportSize = 0;
243 /* Handle HID Class specific requests */
244 switch (USB_ControlRequest.bRequest)
246 case HID_REQ_GetReport:
247 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
249 Endpoint_ClearSETUP();
252 switch (USB_ControlRequest.wIndex) {
253 case KEYBOARD_INTERFACE:
255 ReportData = (uint8_t*)&keyboard_report_sent;
256 ReportSize = sizeof(keyboard_report_sent);
260 /* Write the report data to the control endpoint */
261 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
266 case HID_REQ_SetReport:
267 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
271 switch (USB_ControlRequest.wIndex) {
272 case KEYBOARD_INTERFACE:
273 Endpoint_ClearSETUP();
275 while (!(Endpoint_IsOUTReceived())) {
276 if (USB_DeviceState == DEVICE_STATE_Unattached)
279 keyboard_led_stats = Endpoint_Read_8();
282 Endpoint_ClearStatusStage();
290 case HID_REQ_GetProtocol:
291 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
293 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
294 Endpoint_ClearSETUP();
295 while (!(Endpoint_IsINReady()));
296 Endpoint_Write_8(keyboard_protocol);
298 Endpoint_ClearStatusStage();
303 case HID_REQ_SetProtocol:
304 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
306 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
307 Endpoint_ClearSETUP();
308 Endpoint_ClearStatusStage();
310 keyboard_protocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
312 keyboard_nkro = !!keyboard_protocol;
319 case HID_REQ_SetIdle:
320 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
322 Endpoint_ClearSETUP();
323 Endpoint_ClearStatusStage();
325 keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
329 case HID_REQ_GetIdle:
330 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
332 Endpoint_ClearSETUP();
333 while (!(Endpoint_IsINReady()));
334 Endpoint_Write_8(keyboard_idle);
336 Endpoint_ClearStatusStage();
343 /*******************************************************************************
345 ******************************************************************************/
346 static uint8_t keyboard_leds(void)
348 return keyboard_led_stats;
351 static void send_keyboard(report_keyboard_t *report)
353 uint8_t timeout = 255;
355 if (USB_DeviceState != DEVICE_STATE_Configured)
358 /* Select the Keyboard Report Endpoint */
361 /* Report protocol - NKRO */
362 Endpoint_SelectEndpoint(NKRO_IN_EPNUM);
364 /* Check if write ready for a polling interval around 1ms */
365 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(4);
366 if (!Endpoint_IsReadWriteAllowed()) return;
368 /* Write Keyboard Report Data */
369 Endpoint_Write_Stream_LE(report, NKRO_EPSIZE, NULL);
375 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
377 /* Check if write ready for a polling interval around 10ms */
378 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
379 if (!Endpoint_IsReadWriteAllowed()) return;
381 /* Write Keyboard Report Data */
382 Endpoint_Write_Stream_LE(report, KEYBOARD_EPSIZE, NULL);
385 /* Finalize the stream transfer to send the last packet */
388 keyboard_report_sent = *report;
391 static void send_mouse(report_mouse_t *report)
394 uint8_t timeout = 255;
396 if (USB_DeviceState != DEVICE_STATE_Configured)
399 /* Select the Mouse Report Endpoint */
400 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
402 /* Check if write ready for a polling interval around 10ms */
403 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
404 if (!Endpoint_IsReadWriteAllowed()) return;
406 /* Write Mouse Report Data */
407 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
409 /* Finalize the stream transfer to send the last packet */
414 static void send_system(uint16_t data)
416 uint8_t timeout = 255;
418 if (USB_DeviceState != DEVICE_STATE_Configured)
422 .report_id = REPORT_ID_SYSTEM,
425 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
427 /* Check if write ready for a polling interval around 10ms */
428 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
429 if (!Endpoint_IsReadWriteAllowed()) return;
431 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
435 static void send_consumer(uint16_t data)
437 uint8_t timeout = 255;
439 if (USB_DeviceState != DEVICE_STATE_Configured)
443 .report_id = REPORT_ID_CONSUMER,
446 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
448 /* Check if write ready for a polling interval around 10ms */
449 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
450 if (!Endpoint_IsReadWriteAllowed()) return;
452 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
457 /*******************************************************************************
459 ******************************************************************************/
460 #ifdef CONSOLE_ENABLE
461 #define SEND_TIMEOUT 5
462 int8_t sendchar(uint8_t c)
464 // Not wait once timeouted.
465 // Because sendchar() is called so many times, waiting each call causes big lag.
466 static bool timeouted = false;
468 if (USB_DeviceState != DEVICE_STATE_Configured)
471 uint8_t ep = Endpoint_GetCurrentEndpoint();
472 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
473 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
474 Endpoint_SelectEndpoint(ep);
478 if (timeouted && !Endpoint_IsReadWriteAllowed()) {
479 Endpoint_SelectEndpoint(ep);
485 uint8_t timeout = SEND_TIMEOUT;
486 uint16_t prevFN = USB_Device_GetFrameNumber();
487 while (!Endpoint_IsReadWriteAllowed()) {
488 switch (USB_DeviceState) {
489 case DEVICE_STATE_Unattached:
490 case DEVICE_STATE_Suspended:
493 if (Endpoint_IsStalled()) {
494 Endpoint_SelectEndpoint(ep);
497 if (prevFN != USB_Device_GetFrameNumber()) {
500 Endpoint_SelectEndpoint(ep);
503 prevFN = USB_Device_GetFrameNumber();
509 // send when bank is full
510 if (!Endpoint_IsReadWriteAllowed())
513 Endpoint_SelectEndpoint(ep);
517 int8_t sendchar(uint8_t c)
524 /*******************************************************************************
526 ******************************************************************************/
527 static void SetupHardware(void)
529 /* Disable watchdog if enabled by bootloader/fuses */
530 MCUSR &= ~(1 << WDRF);
533 /* Disable clock division */
534 clock_prescale_set(clock_div_1);
536 // Leonardo needs. Without this USB device is not recognized.
542 USB_Device_EnableSOFEvents();
543 print_set_sendchar(sendchar);
546 int main(void) __attribute__ ((weak));
552 /* wait for USB startup & debug output */
553 while (USB_DeviceState != DEVICE_STATE_Configured) {
554 #if defined(INTERRUPT_CONTROL_ENDPOINT)
560 print("USB configured.\n");
564 host_set_driver(&lufa_driver);
565 #ifdef SLEEP_LED_ENABLE
569 print("Keyboard start.\n");
571 while (USB_DeviceState == DEVICE_STATE_Suspended) {
572 suspend_power_down();
573 if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
574 USB_Device_SendRemoteWakeup();
580 #if !defined(INTERRUPT_CONTROL_ENDPOINT)