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 static uint8_t idle_duration = 0;
56 static uint8_t protocol_report = 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)
151 led_set(0x1f); // all on
154 void EVENT_USB_Device_Disconnect(void)
158 void EVENT_USB_Device_Reset(void)
162 void EVENT_USB_Device_Suspend()
164 #ifdef SLEEP_LED_ENABLE
169 void EVENT_USB_Device_WakeUp()
171 suspend_wakeup_init();
173 #ifdef SLEEP_LED_ENABLE
176 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 #if LUFA_VERSION_INTEGER < 0x120730
189 #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint(epnum, eptype, epdir, epsize, epbank)
191 /* new API >= 120730 */
192 #define ENDPOINT_BANK_SINGLE 1
193 #define ENDPOINT_BANK_DOUBLE 2
194 #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum) , eptype, epsize, epbank)
196 void EVENT_USB_Device_ConfigurationChanged(void)
198 bool ConfigSuccess = true;
200 /* Setup Keyboard HID Report Endpoints */
201 ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
202 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
205 /* Setup Mouse HID Report Endpoint */
206 ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
207 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
210 #ifdef EXTRAKEY_ENABLE
211 /* Setup Extra HID Report Endpoint */
212 ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
213 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
216 #ifdef CONSOLE_ENABLE
217 /* Setup Console HID Report Endpoints */
218 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
219 CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
220 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
221 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
225 /* Setup NKRO HID Report Endpoints */
226 ConfigSuccess &= ENDPOINT_CONFIG(NKRO_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
227 NKRO_EPSIZE, ENDPOINT_BANK_SINGLE);
232 Appendix G: HID Request Support Requirements
234 The following table enumerates the requests that need to be supported by various types of HID class devices.
236 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
237 ------------------------------------------------------------------------------------------
238 Boot Mouse Required Optional Optional Optional Required Required
239 Non-Boot Mouse Required Optional Optional Optional Optional Optional
240 Boot Keyboard Required Optional Required Required Required Required
241 Non-Boot Keybrd Required Optional Required Required Optional Optional
242 Other Device Required Optional Optional Optional Optional Optional
244 /** Event handler for the USB_ControlRequest event.
245 * This is fired before passing along unhandled control requests to the library for processing internally.
247 void EVENT_USB_Device_ControlRequest(void)
249 uint8_t* ReportData = NULL;
250 uint8_t ReportSize = 0;
252 /* Handle HID Class specific requests */
253 switch (USB_ControlRequest.bRequest)
255 case HID_REQ_GetReport:
256 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
258 Endpoint_ClearSETUP();
261 switch (USB_ControlRequest.wIndex) {
262 case KEYBOARD_INTERFACE:
264 ReportData = (uint8_t*)&keyboard_report_sent;
265 ReportSize = sizeof(keyboard_report_sent);
269 /* Write the report data to the control endpoint */
270 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
275 case HID_REQ_SetReport:
276 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
280 switch (USB_ControlRequest.wIndex) {
281 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 Endpoint_ClearSETUP();
303 while (!(Endpoint_IsINReady()));
304 Endpoint_Write_8(protocol_report);
306 Endpoint_ClearStatusStage();
310 case HID_REQ_SetProtocol:
311 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
313 Endpoint_ClearSETUP();
314 Endpoint_ClearStatusStage();
316 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
320 case HID_REQ_SetIdle:
321 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
323 Endpoint_ClearSETUP();
324 Endpoint_ClearStatusStage();
326 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
330 case HID_REQ_GetIdle:
331 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
333 Endpoint_ClearSETUP();
334 while (!(Endpoint_IsINReady()));
335 Endpoint_Write_8(idle_duration);
337 Endpoint_ClearStatusStage();
344 /*******************************************************************************
346 ******************************************************************************/
347 static uint8_t keyboard_leds(void)
349 return keyboard_led_stats;
352 static void send_keyboard(report_keyboard_t *report)
356 if (USB_DeviceState != DEVICE_STATE_Configured)
359 /* Select the Keyboard Report Endpoint */
362 Endpoint_SelectEndpoint(NKRO_IN_EPNUM);
367 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
370 /* Check if Keyboard Endpoint Ready for Read/Write */
371 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
373 /* Write Keyboard Report Data */
376 Endpoint_Write_Stream_LE(report, NKRO_EPSIZE, NULL);
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)
396 if (USB_DeviceState != DEVICE_STATE_Configured)
399 /* Select the Mouse Report Endpoint */
400 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
402 /* Check if Mouse Endpoint Ready for Read/Write */
403 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
405 /* Write Mouse Report Data */
406 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
408 /* Finalize the stream transfer to send the last packet */
413 static void send_system(uint16_t data)
417 if (USB_DeviceState != DEVICE_STATE_Configured)
421 .report_id = REPORT_ID_SYSTEM,
424 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
425 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
426 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
430 static void send_consumer(uint16_t data)
434 if (USB_DeviceState != DEVICE_STATE_Configured)
438 .report_id = REPORT_ID_CONSUMER,
441 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
442 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
443 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
448 /*******************************************************************************
450 ******************************************************************************/
451 #ifdef CONSOLE_ENABLE
452 #define SEND_TIMEOUT 5
453 int8_t sendchar(uint8_t c)
455 // Not wait once timeouted.
456 // Because sendchar() is called so many times, waiting each call causes big lag.
457 static bool timeouted = false;
459 if (USB_DeviceState != DEVICE_STATE_Configured)
462 uint8_t ep = Endpoint_GetCurrentEndpoint();
463 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
464 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
465 Endpoint_SelectEndpoint(ep);
469 if (timeouted && !Endpoint_IsReadWriteAllowed()) {
470 Endpoint_SelectEndpoint(ep);
476 uint8_t timeout = SEND_TIMEOUT;
477 uint16_t prevFN = USB_Device_GetFrameNumber();
478 while (!Endpoint_IsReadWriteAllowed()) {
479 switch (USB_DeviceState) {
480 case DEVICE_STATE_Unattached:
481 case DEVICE_STATE_Suspended:
484 if (Endpoint_IsStalled()) {
485 Endpoint_SelectEndpoint(ep);
488 if (prevFN != USB_Device_GetFrameNumber()) {
491 Endpoint_SelectEndpoint(ep);
494 prevFN = USB_Device_GetFrameNumber();
500 // send when bank is full
501 if (!Endpoint_IsReadWriteAllowed())
504 Endpoint_SelectEndpoint(ep);
508 int8_t sendchar(uint8_t c)
515 /*******************************************************************************
517 ******************************************************************************/
518 static void SetupHardware(void)
520 /* Disable watchdog if enabled by bootloader/fuses */
521 MCUSR &= ~(1 << WDRF);
524 /* Disable clock division */
525 clock_prescale_set(clock_div_1);
527 // Leonardo needs. Without this USB device is not recognized.
533 USB_Device_EnableSOFEvents();
536 int main(void) __attribute__ ((weak));
541 host_set_driver(&lufa_driver);
542 #ifdef SLEEP_LED_ENABLE
548 while (USB_DeviceState == DEVICE_STATE_Suspended) {
549 suspend_power_down();
550 if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
551 USB_Device_SendRemoteWakeup();
557 #if !defined(INTERRUPT_CONTROL_ENDPOINT)