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 #if LUFA_VERSION_INTEGER < 0x120730
156 #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint(epnum, eptype, epdir, epsize, epbank)
158 /* new API >= 120730 */
159 #define ENDPOINT_BANK_SINGLE 1
160 #define ENDPOINT_BANK_DOUBLE 2
161 #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum) , eptype, epsize, epbank)
163 void EVENT_USB_Device_ConfigurationChanged(void)
165 bool ConfigSuccess = true;
167 /* Setup Keyboard HID Report Endpoints */
168 ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
169 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
172 /* Setup Mouse HID Report Endpoint */
173 ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
174 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
177 #ifdef EXTRAKEY_ENABLE
178 /* Setup Extra HID Report Endpoint */
179 ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
180 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
183 #ifdef CONSOLE_ENABLE
184 /* Setup Console HID Report Endpoints */
185 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
186 CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
187 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
188 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
193 Appendix G: HID Request Support Requirements
195 The following table enumerates the requests that need to be supported by various types of HID class devices.
197 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
198 ------------------------------------------------------------------------------------------
199 Boot Mouse Required Optional Optional Optional Required Required
200 Non-Boot Mouse Required Optional Optional Optional Optional Optional
201 Boot Keyboard Required Optional Required Required Required Required
202 Non-Boot Keybrd Required Optional Required Required Optional Optional
203 Other Device Required Optional Optional Optional Optional Optional
205 /** Event handler for the USB_ControlRequest event.
206 * This is fired before passing along unhandled control requests to the library for processing internally.
208 void EVENT_USB_Device_ControlRequest(void)
210 uint8_t* ReportData = NULL;
211 uint8_t ReportSize = 0;
213 /* Handle HID Class specific requests */
214 switch (USB_ControlRequest.bRequest)
216 case HID_REQ_GetReport:
217 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
219 Endpoint_ClearSETUP();
222 switch (USB_ControlRequest.wIndex) {
223 case KEYBOARD_INTERFACE:
225 ReportData = (uint8_t*)&keyboard_report_sent;
226 ReportSize = sizeof(keyboard_report_sent);
230 /* Write the report data to the control endpoint */
231 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
236 case HID_REQ_SetReport:
237 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
241 switch (USB_ControlRequest.wIndex) {
242 case KEYBOARD_INTERFACE:
243 Endpoint_ClearSETUP();
245 while (!(Endpoint_IsOUTReceived())) {
246 if (USB_DeviceState == DEVICE_STATE_Unattached)
249 keyboard_led_stats = Endpoint_Read_8();
252 Endpoint_ClearStatusStage();
260 case HID_REQ_GetProtocol:
261 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
263 Endpoint_ClearSETUP();
264 while (!(Endpoint_IsINReady()));
265 Endpoint_Write_8(protocol_report);
267 Endpoint_ClearStatusStage();
271 case HID_REQ_SetProtocol:
272 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
274 Endpoint_ClearSETUP();
275 Endpoint_ClearStatusStage();
277 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
281 case HID_REQ_SetIdle:
282 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
284 Endpoint_ClearSETUP();
285 Endpoint_ClearStatusStage();
287 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
291 case HID_REQ_GetIdle:
292 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
294 Endpoint_ClearSETUP();
295 while (!(Endpoint_IsINReady()));
296 Endpoint_Write_8(idle_duration);
298 Endpoint_ClearStatusStage();
305 /*******************************************************************************
307 ******************************************************************************/
308 static uint8_t keyboard_leds(void)
310 return keyboard_led_stats;
313 static void send_keyboard(report_keyboard_t *report)
317 // TODO: handle NKRO report
318 /* Select the Keyboard Report Endpoint */
319 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
321 /* Check if Keyboard Endpoint Ready for Read/Write */
322 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
324 /* Write Keyboard Report Data */
325 Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
327 /* Finalize the stream transfer to send the last packet */
330 keyboard_report_sent = *report;
333 static void send_mouse(report_mouse_t *report)
338 /* Select the Mouse Report Endpoint */
339 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
341 /* Check if Mouse Endpoint Ready for Read/Write */
342 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
344 /* Write Mouse Report Data */
345 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
347 /* Finalize the stream transfer to send the last packet */
352 static void send_system(uint16_t data)
357 .report_id = REPORT_ID_SYSTEM,
360 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
361 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
362 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
366 static void send_consumer(uint16_t data)
371 .report_id = REPORT_ID_CONSUMER,
374 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
375 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
376 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
381 /*******************************************************************************
383 ******************************************************************************/
384 #ifdef CONSOLE_ENABLE
385 #define SEND_TIMEOUT 5
386 int8_t sendchar(uint8_t c)
388 // Not wait once timeouted.
389 // Because sendchar() is called so many times, waiting each call causes big lag.
390 static bool timeouted = false;
392 if (USB_DeviceState != DEVICE_STATE_Configured)
395 uint8_t ep = Endpoint_GetCurrentEndpoint();
396 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
397 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
398 Endpoint_SelectEndpoint(ep);
402 if (timeouted && !Endpoint_IsReadWriteAllowed()) {
403 Endpoint_SelectEndpoint(ep);
409 uint8_t timeout = SEND_TIMEOUT;
410 uint16_t prevFN = USB_Device_GetFrameNumber();
411 while (!Endpoint_IsReadWriteAllowed()) {
412 switch (USB_DeviceState) {
413 case DEVICE_STATE_Unattached:
414 case DEVICE_STATE_Suspended:
417 if (Endpoint_IsStalled()) {
418 Endpoint_SelectEndpoint(ep);
421 if (prevFN != USB_Device_GetFrameNumber()) {
424 Endpoint_SelectEndpoint(ep);
427 prevFN = USB_Device_GetFrameNumber();
433 // send when bank is full
434 if (!Endpoint_IsReadWriteAllowed())
437 Endpoint_SelectEndpoint(ep);
441 int8_t sendchar(uint8_t c)
448 /*******************************************************************************
450 ******************************************************************************/
451 static void SetupHardware(void)
453 /* Disable watchdog if enabled by bootloader/fuses */
454 MCUSR &= ~(1 << WDRF);
457 /* Disable clock division */
458 clock_prescale_set(clock_div_1);
460 // Leonardo needs. Without this USB device is not recognized.
466 USB_Device_EnableSOFEvents();
469 int main(void) __attribute__ ((weak));
474 host_set_driver(&lufa_driver);
477 // TODO: can't print here
478 debug("LUFA init\n");
482 #if !defined(INTERRUPT_CONTROL_ENDPOINT)