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 static void Console_Task(void)
116 /* Device must be connected and configured for the task to run */
117 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
120 uint8_t ep
= Endpoint_GetCurrentEndpoint();
123 // TODO: impl receivechar()/recvchar()
124 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM
);
126 /* Check to see if a packet has been sent from the host */
127 if (Endpoint_IsOUTReceived())
129 /* Check to see if the packet contains data */
130 if (Endpoint_IsReadWriteAllowed())
132 /* Create a temporary buffer to hold the read in report from the host */
133 uint8_t ConsoleData
[CONSOLE_EPSIZE
];
135 /* Read Console Report Data */
136 Endpoint_Read_Stream_LE(&ConsoleData
, sizeof(ConsoleData
), NULL
);
138 /* Process Console Report Data */
139 //ProcessConsoleHIDReport(ConsoleData);
142 /* Finalize the stream transfer to send the last packet */
148 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM
);
149 // flash senchar packet
150 if (Endpoint_IsINReady()) {
154 Endpoint_SelectEndpoint(ep
);
158 /*******************************************************************************
160 ******************************************************************************/
161 /** Event handler for the USB_Connect event. */
162 void EVENT_USB_Device_Connect(void)
166 /** Event handler for the USB_Disconnect event. */
167 void EVENT_USB_Device_Disconnect(void)
171 #define CONSOLE_TASK_INTERVAL 50
172 void EVENT_USB_Device_StartOfFrame(void)
174 static uint8_t interval
;
175 if (++interval
== CONSOLE_TASK_INTERVAL
) {
181 /** Event handler for the USB_ConfigurationChanged event.
182 * This is fired when the host sets the current configuration of the USB device after enumeration.
184 void EVENT_USB_Device_ConfigurationChanged(void)
186 bool ConfigSuccess
= true;
188 /* Setup Keyboard HID Report Endpoints */
189 ConfigSuccess
&= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
,
190 KEYBOARD_EPSIZE
, ENDPOINT_BANK_SINGLE
);
193 /* Setup Mouse HID Report Endpoint */
194 ConfigSuccess
&= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
,
195 MOUSE_EPSIZE
, ENDPOINT_BANK_SINGLE
);
198 #ifdef EXTRAKEY_ENABLE
199 /* Setup Extra HID Report Endpoint */
200 ConfigSuccess
&= Endpoint_ConfigureEndpoint(EXTRA_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
,
201 EXTRA_EPSIZE
, ENDPOINT_BANK_SINGLE
);
204 /* Setup Console HID Report Endpoints */
205 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
,
206 CONSOLE_EPSIZE
, ENDPOINT_BANK_DOUBLE
);
207 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_OUT
,
208 CONSOLE_EPSIZE
, ENDPOINT_BANK_SINGLE
);
212 Appendix G: HID Request Support Requirements
214 The following table enumerates the requests that need to be supported by various types of HID class devices.
216 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
217 ------------------------------------------------------------------------------------------
218 Boot Mouse Required Optional Optional Optional Required Required
219 Non-Boot Mouse Required Optional Optional Optional Optional Optional
220 Boot Keyboard Required Optional Required Required Required Required
221 Non-Boot Keybrd Required Optional Required Required Optional Optional
222 Other Device Required Optional Optional Optional Optional Optional
224 /** Event handler for the USB_ControlRequest event.
225 * This is fired before passing along unhandled control requests to the library for processing internally.
227 void EVENT_USB_Device_ControlRequest(void)
229 uint8_t* ReportData
= NULL
;
230 uint8_t ReportSize
= 0;
232 /* Handle HID Class specific requests */
233 switch (USB_ControlRequest
.bRequest
)
235 case HID_REQ_GetReport
:
236 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
238 Endpoint_ClearSETUP();
241 switch (USB_ControlRequest
.wIndex
) {
242 case KEYBOARD_INTERFACE
:
244 ReportData
= (uint8_t*)&keyboard_report_sent
;
245 ReportSize
= sizeof(keyboard_report_sent
);
249 /* Write the report data to the control endpoint */
250 Endpoint_Write_Control_Stream_LE(ReportData
, ReportSize
);
255 case HID_REQ_SetReport
:
256 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
260 switch (USB_ControlRequest
.wIndex
) {
261 case KEYBOARD_INTERFACE
:
262 Endpoint_ClearSETUP();
264 while (!(Endpoint_IsOUTReceived())) {
265 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
268 keyboard_led_stats
= Endpoint_Read_8();
271 Endpoint_ClearStatusStage();
279 case HID_REQ_GetProtocol
:
280 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
282 Endpoint_ClearSETUP();
283 while (!(Endpoint_IsINReady()));
284 Endpoint_Write_8(protocol_report
);
286 Endpoint_ClearStatusStage();
290 case HID_REQ_SetProtocol
:
291 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
293 Endpoint_ClearSETUP();
294 Endpoint_ClearStatusStage();
296 protocol_report
= ((USB_ControlRequest
.wValue
& 0xFF) != 0x00);
300 case HID_REQ_SetIdle
:
301 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
303 Endpoint_ClearSETUP();
304 Endpoint_ClearStatusStage();
306 idle_duration
= ((USB_ControlRequest
.wValue
& 0xFF00) >> 8);
310 case HID_REQ_GetIdle
:
311 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
313 Endpoint_ClearSETUP();
314 while (!(Endpoint_IsINReady()));
315 Endpoint_Write_8(idle_duration
);
317 Endpoint_ClearStatusStage();
324 /*******************************************************************************
326 ******************************************************************************/
327 static uint8_t keyboard_leds(void)
329 return keyboard_led_stats
;
332 static void send_keyboard(report_keyboard_t
*report
)
334 // TODO: handle NKRO report
335 /* Select the Keyboard Report Endpoint */
336 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM
);
338 /* Check if Keyboard Endpoint Ready for Read/Write */
339 if (Endpoint_IsReadWriteAllowed())
341 /* Write Keyboard Report Data */
342 Endpoint_Write_Stream_LE(report
, sizeof(report_keyboard_t
), NULL
);
344 /* Finalize the stream transfer to send the last packet */
347 keyboard_report_sent
= *report
;
350 static void send_mouse(report_mouse_t
*report
)
353 /* Select the Mouse Report Endpoint */
354 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM
);
356 /* Check if Mouse Endpoint Ready for Read/Write */
357 if (Endpoint_IsReadWriteAllowed())
359 /* Write Mouse Report Data */
360 Endpoint_Write_Stream_LE(report
, sizeof(report_mouse_t
), NULL
);
362 /* Finalize the stream transfer to send the last packet */
368 static void send_system(uint16_t data
)
371 .report_id
= REPORT_ID_SYSTEM
,
374 Endpoint_SelectEndpoint(EXTRA_IN_EPNUM
);
375 if (Endpoint_IsReadWriteAllowed()) {
376 Endpoint_Write_Stream_LE(&r
, sizeof(report_extra_t
), NULL
);
381 static void send_consumer(uint16_t data
)
384 .report_id
= REPORT_ID_CONSUMER
,
387 Endpoint_SelectEndpoint(EXTRA_IN_EPNUM
);
388 if (Endpoint_IsReadWriteAllowed()) {
389 Endpoint_Write_Stream_LE(&r
, sizeof(report_extra_t
), NULL
);
395 /*******************************************************************************
397 ******************************************************************************/
398 #define SEND_TIMEOUT 10
399 int8_t sendchar(uint8_t c
)
401 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
404 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM
);
406 uint8_t timeout
= SEND_TIMEOUT
;
407 uint16_t prevFN
= USB_Device_GetFrameNumber();
408 while (!Endpoint_IsReadWriteAllowed()) {
409 switch (USB_DeviceState
) {
410 case DEVICE_STATE_Unattached
:
411 case DEVICE_STATE_Suspended
:
414 if (Endpoint_IsStalled())
416 if (prevFN
!= USB_Device_GetFrameNumber()) {
419 prevFN
= USB_Device_GetFrameNumber();
425 // send when bank is full
426 if (!Endpoint_IsReadWriteAllowed())