Change keymap of hhkb/macway.
[tmk_keyboard.git] / protocol / lufa / lufa.c
CommitLineData
dd93d291 1/*
2 * Copyright 2012 Jun Wako <wakojun@gmail.com>
3969ec09 3 * This file is based on:
4 * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
5 * LUFA-120219/Demos/Device/Lowlevel/GenericHID
dd93d291 6 */
7
8/*
9 LUFA Library
10 Copyright (C) Dean Camera, 2012.
11
12 dean [at] fourwalledcubicle [dot] com
13 www.lufa-lib.org
14*/
15
16/*
17 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
18 Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
19
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.
28
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
36 this software.
37*/
38
39#include "report.h"
40#include "host.h"
41#include "host_driver.h"
42#include "keyboard.h"
3969ec09 43#include "sendchar.h"
44#include "debug.h"
effa5914 45
46#include "descriptor.h"
dd93d291 47#include "lufa.h"
48
a112f361 49static uint8_t idle_duration = 0;
50static uint8_t protocol_report = 1;
dd93d291 51static uint8_t keyboard_led_stats = 0;
effa5914 52
effa5914 53static report_keyboard_t keyboard_report_sent;
a112f361 54
dd93d291 55
56/* Host driver */
57static uint8_t keyboard_leds(void);
58static void send_keyboard(report_keyboard_t *report);
59static void send_mouse(report_mouse_t *report);
60static void send_system(uint16_t data);
61static void send_consumer(uint16_t data);
62static host_driver_t lufa_driver = {
63 keyboard_leds,
64 send_keyboard,
65 send_mouse,
66 send_system,
67 send_consumer
68};
69
70
effa5914 71static void SetupHardware(void);
89470299 72static void Console_Task(void);
effa5914 73
dd93d291 74int main(void)
75{
76 SetupHardware();
77 sei();
78
3969ec09 79 print_enable = true;
80 debug_enable = true;
81 debug_matrix = true;
82 debug_keyboard = true;
83 debug_mouse = true;
84
a112f361 85 // TODO: can't print here
86 debug("LUFA init\n");
3969ec09 87
dd93d291 88 keyboard_init();
89 host_set_driver(&lufa_driver);
90 while (1) {
91 keyboard_proc();
effa5914 92
89470299 93#if !defined(INTERRUPT_CONTROL_ENDPOINT)
dd93d291 94 USB_USBTask();
89470299 95#endif
dd93d291 96 }
97}
98
dd93d291 99void SetupHardware(void)
100{
101 /* Disable watchdog if enabled by bootloader/fuses */
102 MCUSR &= ~(1 << WDRF);
103 wdt_disable();
104
105 /* Disable clock division */
106 clock_prescale_set(clock_div_1);
107
108 USB_Init();
89470299 109
110 // for Console_Task
111 USB_Device_EnableSOFEvents();
dd93d291 112}
113
89470299 114static void Console_Task(void)
effa5914 115{
89470299 116 /* Device must be connected and configured for the task to run */
117 if (USB_DeviceState != DEVICE_STATE_Configured)
118 return;
119
120 uint8_t ep = Endpoint_GetCurrentEndpoint();
121
122#if 0
123 // TODO: impl receivechar()/recvchar()
124 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
125
126 /* Check to see if a packet has been sent from the host */
127 if (Endpoint_IsOUTReceived())
128 {
129 /* Check to see if the packet contains data */
130 if (Endpoint_IsReadWriteAllowed())
131 {
132 /* Create a temporary buffer to hold the read in report from the host */
133 uint8_t ConsoleData[CONSOLE_EPSIZE];
134
135 /* Read Console Report Data */
136 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
137
138 /* Process Console Report Data */
139 //ProcessConsoleHIDReport(ConsoleData);
140 }
141
142 /* Finalize the stream transfer to send the last packet */
143 Endpoint_ClearOUT();
144 }
145#endif
146
147 /* IN packet */
148 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
149 // flash senchar packet
150 if (Endpoint_IsINReady()) {
151 Endpoint_ClearIN();
152 }
153
154 Endpoint_SelectEndpoint(ep);
effa5914 155}
156
157
158/*******************************************************************************
159 * USB Events
160 ******************************************************************************/
dd93d291 161/** Event handler for the USB_Connect event. */
162void EVENT_USB_Device_Connect(void)
163{
164}
165
166/** Event handler for the USB_Disconnect event. */
167void EVENT_USB_Device_Disconnect(void)
168{
169}
170
89470299 171#define CONSOLE_TASK_INTERVAL 50
172void EVENT_USB_Device_StartOfFrame(void)
173{
174 static uint8_t interval;
175 if (++interval == CONSOLE_TASK_INTERVAL) {
176 Console_Task();
177 interval = 0;
178 }
179}
180
dd93d291 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.
183 */
184void EVENT_USB_Device_ConfigurationChanged(void)
185{
186 bool ConfigSuccess = true;
187
188 /* Setup Keyboard HID Report Endpoints */
189 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
3d81d522 190 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
dd93d291 191
f2ebac10 192#ifdef MOUSE_ENABLE
dd93d291 193 /* Setup Mouse HID Report Endpoint */
194 ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
3d81d522 195 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
f2ebac10 196#endif
197
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);
202#endif
3969ec09 203
a9a3610d 204 /* Setup Console HID Report Endpoints */
3d81d522 205 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
89470299 206 CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
3d81d522 207 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
208 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
dd93d291 209}
210
a9a3610d 211/*
212Appendix G: HID Request Support Requirements
213
214The following table enumerates the requests that need to be supported by various types of HID class devices.
215
216Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
217------------------------------------------------------------------------------------------
218Boot Mouse Required Optional Optional Optional Required Required
219Non-Boot Mouse Required Optional Optional Optional Optional Optional
220Boot Keyboard Required Optional Required Required Required Required
221Non-Boot Keybrd Required Optional Required Required Optional Optional
222Other Device Required Optional Optional Optional Optional Optional
223*/
dd93d291 224/** Event handler for the USB_ControlRequest event.
225 * This is fired before passing along unhandled control requests to the library for processing internally.
226 */
227void EVENT_USB_Device_ControlRequest(void)
228{
3969ec09 229 uint8_t* ReportData = NULL;
230 uint8_t ReportSize = 0;
dd93d291 231
232 /* Handle HID Class specific requests */
233 switch (USB_ControlRequest.bRequest)
234 {
235 case HID_REQ_GetReport:
236 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
237 {
238 Endpoint_ClearSETUP();
239
3969ec09 240 // Interface
241 switch (USB_ControlRequest.wIndex) {
effa5914 242 case KEYBOARD_INTERFACE:
243 // TODO: test/check
dd93d291 244 ReportData = (uint8_t*)&keyboard_report_sent;
245 ReportSize = sizeof(keyboard_report_sent);
3969ec09 246 break;
dd93d291 247 }
248
249 /* Write the report data to the control endpoint */
250 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
251 Endpoint_ClearOUT();
252 }
253
254 break;
255 case HID_REQ_SetReport:
256 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
257 {
dd93d291 258
3969ec09 259 // Interface
260 switch (USB_ControlRequest.wIndex) {
effa5914 261 case KEYBOARD_INTERFACE:
a112f361 262 Endpoint_ClearSETUP();
263
264 while (!(Endpoint_IsOUTReceived())) {
265 if (USB_DeviceState == DEVICE_STATE_Unattached)
266 return;
267 }
3969ec09 268 keyboard_led_stats = Endpoint_Read_8();
a112f361 269
270 Endpoint_ClearOUT();
271 Endpoint_ClearStatusStage();
f2ebac10 272 break;
3969ec09 273 }
dd93d291 274
a112f361 275 }
276
277 break;
278
279 case HID_REQ_GetProtocol:
280 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
281 {
282 Endpoint_ClearSETUP();
283 while (!(Endpoint_IsINReady()));
284 Endpoint_Write_8(protocol_report);
285 Endpoint_ClearIN();
286 Endpoint_ClearStatusStage();
287 }
288
289 break;
290 case HID_REQ_SetProtocol:
291 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
292 {
293 Endpoint_ClearSETUP();
294 Endpoint_ClearStatusStage();
295
296 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
297 }
298
299 break;
300 case HID_REQ_SetIdle:
301 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
302 {
303 Endpoint_ClearSETUP();
304 Endpoint_ClearStatusStage();
305
306 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
307 }
308
309 break;
310 case HID_REQ_GetIdle:
311 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
312 {
313 Endpoint_ClearSETUP();
314 while (!(Endpoint_IsINReady()));
315 Endpoint_Write_8(idle_duration);
316 Endpoint_ClearIN();
dd93d291 317 Endpoint_ClearStatusStage();
318 }
319
320 break;
321 }
322}
323
dd93d291 324/*******************************************************************************
325 * Host driver
326 ******************************************************************************/
327static uint8_t keyboard_leds(void)
328{
329 return keyboard_led_stats;
330}
331
332static void send_keyboard(report_keyboard_t *report)
333{
334 // TODO: handle NKRO report
335 /* Select the Keyboard Report Endpoint */
336 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
337
338 /* Check if Keyboard Endpoint Ready for Read/Write */
339 if (Endpoint_IsReadWriteAllowed())
340 {
341 /* Write Keyboard Report Data */
342 Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
343
344 /* Finalize the stream transfer to send the last packet */
345 Endpoint_ClearIN();
346 }
347 keyboard_report_sent = *report;
348}
349
350static void send_mouse(report_mouse_t *report)
351{
f2ebac10 352#ifdef MOUSE_ENABLE
dd93d291 353 /* Select the Mouse Report Endpoint */
354 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
355
356 /* Check if Mouse Endpoint Ready for Read/Write */
357 if (Endpoint_IsReadWriteAllowed())
358 {
359 /* Write Mouse Report Data */
3d81d522 360 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
dd93d291 361
362 /* Finalize the stream transfer to send the last packet */
363 Endpoint_ClearIN();
364 }
f2ebac10 365#endif
dd93d291 366}
367
368static void send_system(uint16_t data)
369{
a112f361 370 report_extra_t r = {
371 .report_id = REPORT_ID_SYSTEM,
372 .usage = data
373 };
3d81d522 374 Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
375 if (Endpoint_IsReadWriteAllowed()) {
3d81d522 376 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
377 Endpoint_ClearIN();
378 }
dd93d291 379}
380
381static void send_consumer(uint16_t data)
382{
a112f361 383 report_extra_t r = {
384 .report_id = REPORT_ID_CONSUMER,
385 .usage = data
386 };
3d81d522 387 Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
388 if (Endpoint_IsReadWriteAllowed()) {
3d81d522 389 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
390 Endpoint_ClearIN();
391 }
dd93d291 392}
3969ec09 393
394
395/*******************************************************************************
396 * sendchar
397 ******************************************************************************/
89470299 398#define SEND_TIMEOUT 10
3969ec09 399int8_t sendchar(uint8_t c)
400{
401 if (USB_DeviceState != DEVICE_STATE_Configured)
402 return -1;
403
3d81d522 404 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
3969ec09 405
89470299 406 uint8_t timeout = SEND_TIMEOUT;
3969ec09 407 uint16_t prevFN = USB_Device_GetFrameNumber();
89470299 408 while (!Endpoint_IsReadWriteAllowed()) {
3969ec09 409 switch (USB_DeviceState) {
410 case DEVICE_STATE_Unattached:
411 case DEVICE_STATE_Suspended:
412 return -1;
413 }
414 if (Endpoint_IsStalled())
415 return -1;
3969ec09 416 if (prevFN != USB_Device_GetFrameNumber()) {
417 if (!(timeout--))
418 return -1;
419 prevFN = USB_Device_GetFrameNumber();
420 }
421 }
422
423 Endpoint_Write_8(c);
424
89470299 425 // send when bank is full
3969ec09 426 if (!Endpoint_IsReadWriteAllowed())
427 Endpoint_ClearIN();
428
429 return 0;
430}
Imprint / Impressum