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"
46 #include "descriptor.h"
49 static uint8_t idle_duration = 0;
50 static uint8_t protocol_report = 1;
51 static uint8_t keyboard_led_stats = 0;
53 static report_keyboard_t keyboard_report_sent;
57 static uint8_t keyboard_leds(void);
58 static void send_keyboard(report_keyboard_t *report);
59 static void send_mouse(report_mouse_t *report);
60 static void send_system(uint16_t data);
61 static void send_consumer(uint16_t data);
62 host_driver_t lufa_driver = {
71 /*******************************************************************************
73 ******************************************************************************/
75 static void Console_Task(void)
77 /* Device must be connected and configured for the task to run */
78 if (USB_DeviceState != DEVICE_STATE_Configured)
81 uint8_t ep = Endpoint_GetCurrentEndpoint();
84 // TODO: impl receivechar()/recvchar()
85 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
87 /* Check to see if a packet has been sent from the host */
88 if (Endpoint_IsOUTReceived())
90 /* Check to see if the packet contains data */
91 if (Endpoint_IsReadWriteAllowed())
93 /* Create a temporary buffer to hold the read in report from the host */
94 uint8_t ConsoleData[CONSOLE_EPSIZE];
96 /* Read Console Report Data */
97 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
99 /* Process Console Report Data */
100 //ProcessConsoleHIDReport(ConsoleData);
103 /* Finalize the stream transfer to send the last packet */
109 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
110 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
111 Endpoint_SelectEndpoint(ep);
116 while (Endpoint_IsReadWriteAllowed())
119 // flash senchar packet
120 if (Endpoint_IsINReady()) {
124 Endpoint_SelectEndpoint(ep);
127 static void Console_Task(void)
133 /*******************************************************************************
135 ******************************************************************************/
136 /** Event handler for the USB_Connect event. */
137 void EVENT_USB_Device_Connect(void)
141 /** Event handler for the USB_Disconnect event. */
142 void EVENT_USB_Device_Disconnect(void)
146 void EVENT_USB_Device_StartOfFrame(void)
151 /** Event handler for the USB_ConfigurationChanged event.
152 * This is fired when the host sets the current configuration of the USB device after enumeration.
154 void EVENT_USB_Device_ConfigurationChanged(void)
156 bool ConfigSuccess = true;
158 /* Setup Keyboard HID Report Endpoints */
159 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
160 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
163 /* Setup Mouse HID Report Endpoint */
164 ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
165 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
168 #ifdef EXTRAKEY_ENABLE
169 /* Setup Extra HID Report Endpoint */
170 ConfigSuccess &= Endpoint_ConfigureEndpoint(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
171 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
174 #ifdef CONSOLE_ENABLE
175 /* Setup Console HID Report Endpoints */
176 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
177 CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
178 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
179 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
184 Appendix G: HID Request Support Requirements
186 The following table enumerates the requests that need to be supported by various types of HID class devices.
188 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
189 ------------------------------------------------------------------------------------------
190 Boot Mouse Required Optional Optional Optional Required Required
191 Non-Boot Mouse Required Optional Optional Optional Optional Optional
192 Boot Keyboard Required Optional Required Required Required Required
193 Non-Boot Keybrd Required Optional Required Required Optional Optional
194 Other Device Required Optional Optional Optional Optional Optional
196 /** Event handler for the USB_ControlRequest event.
197 * This is fired before passing along unhandled control requests to the library for processing internally.
199 void EVENT_USB_Device_ControlRequest(void)
201 uint8_t* ReportData = NULL;
202 uint8_t ReportSize = 0;
204 /* Handle HID Class specific requests */
205 switch (USB_ControlRequest.bRequest)
207 case HID_REQ_GetReport:
208 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
210 Endpoint_ClearSETUP();
213 switch (USB_ControlRequest.wIndex) {
214 case KEYBOARD_INTERFACE:
216 ReportData = (uint8_t*)&keyboard_report_sent;
217 ReportSize = sizeof(keyboard_report_sent);
221 /* Write the report data to the control endpoint */
222 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
227 case HID_REQ_SetReport:
228 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
232 switch (USB_ControlRequest.wIndex) {
233 case KEYBOARD_INTERFACE:
234 Endpoint_ClearSETUP();
236 while (!(Endpoint_IsOUTReceived())) {
237 if (USB_DeviceState == DEVICE_STATE_Unattached)
240 keyboard_led_stats = Endpoint_Read_8();
243 Endpoint_ClearStatusStage();
251 case HID_REQ_GetProtocol:
252 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
254 Endpoint_ClearSETUP();
255 while (!(Endpoint_IsINReady()));
256 Endpoint_Write_8(protocol_report);
258 Endpoint_ClearStatusStage();
262 case HID_REQ_SetProtocol:
263 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
265 Endpoint_ClearSETUP();
266 Endpoint_ClearStatusStage();
268 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
272 case HID_REQ_SetIdle:
273 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
275 Endpoint_ClearSETUP();
276 Endpoint_ClearStatusStage();
278 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
282 case HID_REQ_GetIdle:
283 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
285 Endpoint_ClearSETUP();
286 while (!(Endpoint_IsINReady()));
287 Endpoint_Write_8(idle_duration);
289 Endpoint_ClearStatusStage();
296 /*******************************************************************************
298 ******************************************************************************/
299 static uint8_t keyboard_leds(void)
301 return keyboard_led_stats;
304 static void send_keyboard(report_keyboard_t *report)
308 // TODO: handle NKRO report
309 /* Select the Keyboard Report Endpoint */
310 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
312 /* Check if Keyboard Endpoint Ready for Read/Write */
313 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
315 /* Write Keyboard Report Data */
316 Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
318 /* Finalize the stream transfer to send the last packet */
321 keyboard_report_sent = *report;
324 static void send_mouse(report_mouse_t *report)
329 /* Select the Mouse Report Endpoint */
330 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
332 /* Check if Mouse Endpoint Ready for Read/Write */
333 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
335 /* Write Mouse Report Data */
336 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
338 /* Finalize the stream transfer to send the last packet */
343 static void send_system(uint16_t data)
348 .report_id = REPORT_ID_SYSTEM,
351 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
352 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
353 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
357 static void send_consumer(uint16_t data)
362 .report_id = REPORT_ID_CONSUMER,
365 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
366 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
367 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
372 /*******************************************************************************
374 ******************************************************************************/
375 #ifdef CONSOLE_ENABLE
376 #define SEND_TIMEOUT 5
377 int8_t sendchar(uint8_t c)
379 // Not wait once timeouted.
380 // Because sendchar() is called so many times, waiting each call causes big lag.
381 static bool timeouted = false;
383 if (USB_DeviceState != DEVICE_STATE_Configured)
386 uint8_t ep = Endpoint_GetCurrentEndpoint();
387 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
388 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
389 Endpoint_SelectEndpoint(ep);
393 if (timeouted && !Endpoint_IsReadWriteAllowed()) {
394 Endpoint_SelectEndpoint(ep);
400 uint8_t timeout = SEND_TIMEOUT;
401 uint16_t prevFN = USB_Device_GetFrameNumber();
402 while (!Endpoint_IsReadWriteAllowed()) {
403 switch (USB_DeviceState) {
404 case DEVICE_STATE_Unattached:
405 case DEVICE_STATE_Suspended:
408 if (Endpoint_IsStalled()) {
409 Endpoint_SelectEndpoint(ep);
412 if (prevFN != USB_Device_GetFrameNumber()) {
415 Endpoint_SelectEndpoint(ep);
418 prevFN = USB_Device_GetFrameNumber();
424 // send when bank is full
425 if (!Endpoint_IsReadWriteAllowed())
428 Endpoint_SelectEndpoint(ep);
432 int8_t sendchar(uint8_t c)
439 /*******************************************************************************
441 ******************************************************************************/
442 static void SetupHardware(void)
444 /* Disable watchdog if enabled by bootloader/fuses */
445 MCUSR &= ~(1 << WDRF);
448 /* Disable clock division */
449 clock_prescale_set(clock_div_1);
451 // Leonardo needs. Without this USB device is not recognized.
457 USB_Device_EnableSOFEvents();
460 int main(void) __attribute__ ((weak));
469 debug_keyboard = true;
472 // TODO: can't print here
473 debug("LUFA init\n");
476 host_set_driver(&lufa_driver);
480 #if !defined(INTERRUPT_CONTROL_ENDPOINT)