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 static host_driver_t lufa_driver = {
71 static void SetupHardware(void);
72 static void Console_Task(void);
82 debug_keyboard = true;
85 // TODO: can't print here
89 host_set_driver(&lufa_driver);
93 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
99 void SetupHardware(void)
101 /* Disable watchdog if enabled by bootloader/fuses */
102 MCUSR &= ~(1 << WDRF);
105 /* Disable clock division */
106 clock_prescale_set(clock_div_1);
111 USB_Device_EnableSOFEvents();
114 #ifdef CONSOLE_ENABLE
115 static void Console_Task(void)
117 /* Device must be connected and configured for the task to run */
118 if (USB_DeviceState != DEVICE_STATE_Configured)
121 uint8_t ep = Endpoint_GetCurrentEndpoint();
124 // TODO: impl receivechar()/recvchar()
125 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
127 /* Check to see if a packet has been sent from the host */
128 if (Endpoint_IsOUTReceived())
130 /* Check to see if the packet contains data */
131 if (Endpoint_IsReadWriteAllowed())
133 /* Create a temporary buffer to hold the read in report from the host */
134 uint8_t ConsoleData[CONSOLE_EPSIZE];
136 /* Read Console Report Data */
137 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
139 /* Process Console Report Data */
140 //ProcessConsoleHIDReport(ConsoleData);
143 /* Finalize the stream transfer to send the last packet */
149 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
152 while (Endpoint_IsReadWriteAllowed())
155 // flash senchar packet
156 if (Endpoint_IsINReady()) {
160 Endpoint_SelectEndpoint(ep);
163 static void Console_Task(void)
169 /*******************************************************************************
171 ******************************************************************************/
172 /** Event handler for the USB_Connect event. */
173 void EVENT_USB_Device_Connect(void)
177 /** Event handler for the USB_Disconnect event. */
178 void EVENT_USB_Device_Disconnect(void)
182 void EVENT_USB_Device_StartOfFrame(void)
187 /** Event handler for the USB_ConfigurationChanged event.
188 * This is fired when the host sets the current configuration of the USB device after enumeration.
190 void EVENT_USB_Device_ConfigurationChanged(void)
192 bool ConfigSuccess = true;
194 /* Setup Keyboard HID Report Endpoints */
195 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
196 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
199 /* Setup Mouse HID Report Endpoint */
200 ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
201 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
204 #ifdef EXTRAKEY_ENABLE
205 /* Setup Extra HID Report Endpoint */
206 ConfigSuccess &= Endpoint_ConfigureEndpoint(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
207 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
210 #ifdef CONSOLE_ENABLE
211 /* Setup Console HID Report Endpoints */
212 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
213 CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
214 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
215 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
220 Appendix G: HID Request Support Requirements
222 The following table enumerates the requests that need to be supported by various types of HID class devices.
224 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
225 ------------------------------------------------------------------------------------------
226 Boot Mouse Required Optional Optional Optional Required Required
227 Non-Boot Mouse Required Optional Optional Optional Optional Optional
228 Boot Keyboard Required Optional Required Required Required Required
229 Non-Boot Keybrd Required Optional Required Required Optional Optional
230 Other Device Required Optional Optional Optional Optional Optional
232 /** Event handler for the USB_ControlRequest event.
233 * This is fired before passing along unhandled control requests to the library for processing internally.
235 void EVENT_USB_Device_ControlRequest(void)
237 uint8_t* ReportData = NULL;
238 uint8_t ReportSize = 0;
240 /* Handle HID Class specific requests */
241 switch (USB_ControlRequest.bRequest)
243 case HID_REQ_GetReport:
244 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
246 Endpoint_ClearSETUP();
249 switch (USB_ControlRequest.wIndex) {
250 case KEYBOARD_INTERFACE:
252 ReportData = (uint8_t*)&keyboard_report_sent;
253 ReportSize = sizeof(keyboard_report_sent);
257 /* Write the report data to the control endpoint */
258 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
263 case HID_REQ_SetReport:
264 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
268 switch (USB_ControlRequest.wIndex) {
269 case KEYBOARD_INTERFACE:
270 Endpoint_ClearSETUP();
272 while (!(Endpoint_IsOUTReceived())) {
273 if (USB_DeviceState == DEVICE_STATE_Unattached)
276 keyboard_led_stats = Endpoint_Read_8();
279 Endpoint_ClearStatusStage();
287 case HID_REQ_GetProtocol:
288 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
290 Endpoint_ClearSETUP();
291 while (!(Endpoint_IsINReady()));
292 Endpoint_Write_8(protocol_report);
294 Endpoint_ClearStatusStage();
298 case HID_REQ_SetProtocol:
299 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
301 Endpoint_ClearSETUP();
302 Endpoint_ClearStatusStage();
304 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
308 case HID_REQ_SetIdle:
309 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
311 Endpoint_ClearSETUP();
312 Endpoint_ClearStatusStage();
314 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
318 case HID_REQ_GetIdle:
319 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
321 Endpoint_ClearSETUP();
322 while (!(Endpoint_IsINReady()));
323 Endpoint_Write_8(idle_duration);
325 Endpoint_ClearStatusStage();
332 /*******************************************************************************
334 ******************************************************************************/
335 static uint8_t keyboard_leds(void)
337 return keyboard_led_stats;
340 static void send_keyboard(report_keyboard_t *report)
342 // TODO: handle NKRO report
343 /* Select the Keyboard Report Endpoint */
344 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
346 /* Check if Keyboard Endpoint Ready for Read/Write */
347 while (!Endpoint_IsReadWriteAllowed()) ;
349 /* Write Keyboard Report Data */
350 Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
352 /* Finalize the stream transfer to send the last packet */
355 keyboard_report_sent = *report;
358 static void send_mouse(report_mouse_t *report)
361 /* Select the Mouse Report Endpoint */
362 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
364 /* Check if Mouse Endpoint Ready for Read/Write */
365 while (!Endpoint_IsReadWriteAllowed()) ;
367 /* Write Mouse Report Data */
368 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
370 /* Finalize the stream transfer to send the last packet */
375 static void send_system(uint16_t data)
378 .report_id = REPORT_ID_SYSTEM,
381 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
382 while (!Endpoint_IsReadWriteAllowed()) ;
383 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
387 static void send_consumer(uint16_t data)
390 .report_id = REPORT_ID_CONSUMER,
393 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
394 while (!Endpoint_IsReadWriteAllowed()) ;
395 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
400 /*******************************************************************************
402 ******************************************************************************/
403 #ifdef CONSOLE_ENABLE
404 #define SEND_TIMEOUT 5
405 int8_t sendchar(uint8_t c)
407 if (USB_DeviceState != DEVICE_STATE_Configured)
410 uint8_t ep = Endpoint_GetCurrentEndpoint();
411 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
413 uint8_t timeout = SEND_TIMEOUT;
414 uint16_t prevFN = USB_Device_GetFrameNumber();
415 while (!Endpoint_IsReadWriteAllowed()) {
416 switch (USB_DeviceState) {
417 case DEVICE_STATE_Unattached:
418 case DEVICE_STATE_Suspended:
421 if (Endpoint_IsStalled()) {
422 Endpoint_SelectEndpoint(ep);
425 if (prevFN != USB_Device_GetFrameNumber()) {
427 Endpoint_SelectEndpoint(ep);
430 prevFN = USB_Device_GetFrameNumber();
436 // send when bank is full
437 if (!Endpoint_IsReadWriteAllowed())
440 Endpoint_SelectEndpoint(ep);
444 int8_t sendchar(uint8_t c)