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_HID_Task(void);
82 debug_keyboard = true;
85 // TODO: can't print here
89 host_set_driver(&lufa_driver);
98 void SetupHardware(void)
100 /* Disable watchdog if enabled by bootloader/fuses */
101 MCUSR &= ~(1 << WDRF);
104 /* Disable clock division */
105 clock_prescale_set(clock_div_1);
110 static void Console_HID_Task(void)
112 /* Device must be connected and configured for the task to run */
113 if (USB_DeviceState != DEVICE_STATE_Configured)
116 // TODO: impl receivechar()/recvchar()
117 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
119 /* Check to see if a packet has been sent from the host */
120 if (Endpoint_IsOUTReceived())
122 /* Check to see if the packet contains data */
123 if (Endpoint_IsReadWriteAllowed())
125 /* Create a temporary buffer to hold the read in report from the host */
126 uint8_t ConsoleData[CONSOLE_EPSIZE];
128 /* Read Console Report Data */
129 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
131 /* Process Console Report Data */
132 //ProcessConsoleHIDReport(ConsoleData);
135 /* Finalize the stream transfer to send the last packet */
140 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
142 if (Endpoint_IsINReady())
147 /*******************************************************************************
149 ******************************************************************************/
150 /** Event handler for the USB_Connect event. */
151 void EVENT_USB_Device_Connect(void)
155 /** Event handler for the USB_Disconnect event. */
156 void EVENT_USB_Device_Disconnect(void)
160 /** Event handler for the USB_ConfigurationChanged event.
161 * This is fired when the host sets the current configuration of the USB device after enumeration.
163 void EVENT_USB_Device_ConfigurationChanged(void)
165 bool ConfigSuccess = true;
167 /* Setup Keyboard HID Report Endpoints */
168 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
169 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
172 /* Setup Mouse HID Report Endpoint */
173 ConfigSuccess &= Endpoint_ConfigureEndpoint(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_ConfigureEndpoint(EXTRA_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
180 EXTRA_EPSIZE, ENDPOINT_BANK_SINGLE);
183 /* Setup Console HID Report Endpoints */
184 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
185 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
186 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
187 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
191 Appendix G: HID Request Support Requirements
193 The following table enumerates the requests that need to be supported by various types of HID class devices.
195 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
196 ------------------------------------------------------------------------------------------
197 Boot Mouse Required Optional Optional Optional Required Required
198 Non-Boot Mouse Required Optional Optional Optional Optional Optional
199 Boot Keyboard Required Optional Required Required Required Required
200 Non-Boot Keybrd Required Optional Required Required Optional Optional
201 Other Device Required Optional Optional Optional Optional Optional
203 /** Event handler for the USB_ControlRequest event.
204 * This is fired before passing along unhandled control requests to the library for processing internally.
206 void EVENT_USB_Device_ControlRequest(void)
208 uint8_t* ReportData = NULL;
209 uint8_t ReportSize = 0;
211 /* Handle HID Class specific requests */
212 switch (USB_ControlRequest.bRequest)
214 case HID_REQ_GetReport:
215 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
217 Endpoint_ClearSETUP();
220 switch (USB_ControlRequest.wIndex) {
221 case KEYBOARD_INTERFACE:
223 ReportData = (uint8_t*)&keyboard_report_sent;
224 ReportSize = sizeof(keyboard_report_sent);
228 /* Write the report data to the control endpoint */
229 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
234 case HID_REQ_SetReport:
235 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
239 switch (USB_ControlRequest.wIndex) {
240 case KEYBOARD_INTERFACE:
241 Endpoint_ClearSETUP();
243 while (!(Endpoint_IsOUTReceived())) {
244 if (USB_DeviceState == DEVICE_STATE_Unattached)
247 keyboard_led_stats = Endpoint_Read_8();
250 Endpoint_ClearStatusStage();
258 case HID_REQ_GetProtocol:
259 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
261 Endpoint_ClearSETUP();
262 while (!(Endpoint_IsINReady()));
263 Endpoint_Write_8(protocol_report);
265 Endpoint_ClearStatusStage();
269 case HID_REQ_SetProtocol:
270 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
272 Endpoint_ClearSETUP();
273 Endpoint_ClearStatusStage();
275 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
279 case HID_REQ_SetIdle:
280 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
282 Endpoint_ClearSETUP();
283 Endpoint_ClearStatusStage();
285 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
289 case HID_REQ_GetIdle:
290 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
292 Endpoint_ClearSETUP();
293 while (!(Endpoint_IsINReady()));
294 Endpoint_Write_8(idle_duration);
296 Endpoint_ClearStatusStage();
303 /*******************************************************************************
305 ******************************************************************************/
306 static uint8_t keyboard_leds(void)
308 return keyboard_led_stats;
311 static void send_keyboard(report_keyboard_t *report)
313 // TODO: handle NKRO report
314 /* Select the Keyboard Report Endpoint */
315 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
317 /* Check if Keyboard Endpoint Ready for Read/Write */
318 if (Endpoint_IsReadWriteAllowed())
320 /* Write Keyboard Report Data */
321 Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
323 /* Finalize the stream transfer to send the last packet */
326 keyboard_report_sent = *report;
329 static void send_mouse(report_mouse_t *report)
332 /* Select the Mouse Report Endpoint */
333 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
335 /* Check if Mouse Endpoint Ready for Read/Write */
336 if (Endpoint_IsReadWriteAllowed())
338 /* Write Mouse Report Data */
339 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
341 /* Finalize the stream transfer to send the last packet */
347 static void send_system(uint16_t data)
350 .report_id = REPORT_ID_SYSTEM,
353 Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
354 if (Endpoint_IsReadWriteAllowed()) {
355 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
360 static void send_consumer(uint16_t data)
363 .report_id = REPORT_ID_CONSUMER,
366 Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
367 if (Endpoint_IsReadWriteAllowed()) {
368 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
374 /*******************************************************************************
376 ******************************************************************************/
377 int8_t sendchar(uint8_t c)
379 if (USB_DeviceState != DEVICE_STATE_Configured)
382 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
384 uint8_t timeout = 10;
385 uint16_t prevFN = USB_Device_GetFrameNumber();
386 while (!Endpoint_IsINReady()) {
387 switch (USB_DeviceState) {
388 case DEVICE_STATE_Unattached:
389 case DEVICE_STATE_Suspended:
392 if (Endpoint_IsStalled())
394 if (prevFN != USB_Device_GetFrameNumber()) {
397 prevFN = USB_Device_GetFrameNumber();
403 // send when packet is full
404 if (!Endpoint_IsReadWriteAllowed())