]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/lufa.c
Fix usage of debug flag
[tmk_keyboard.git] / protocol / lufa / lufa.c
1 /*
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
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"
43 #include "sendchar.h"
44 #include "debug.h"
45
46 #include "descriptor.h"
47 #include "lufa.h"
48
49 static uint8_t idle_duration = 0;
50 static uint8_t protocol_report = 1;
51 static uint8_t keyboard_led_stats = 0;
52
53 static report_keyboard_t keyboard_report_sent;
54
55
56 /* Host driver */
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 = {
63 keyboard_leds,
64 send_keyboard,
65 send_mouse,
66 send_system,
67 send_consumer
68 };
69
70
71 /*******************************************************************************
72 * Console
73 ******************************************************************************/
74 #ifdef CONSOLE_ENABLE
75 static void Console_Task(void)
76 {
77 /* Device must be connected and configured for the task to run */
78 if (USB_DeviceState != DEVICE_STATE_Configured)
79 return;
80
81 uint8_t ep = Endpoint_GetCurrentEndpoint();
82
83 #if 0
84 // TODO: impl receivechar()/recvchar()
85 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
86
87 /* Check to see if a packet has been sent from the host */
88 if (Endpoint_IsOUTReceived())
89 {
90 /* Check to see if the packet contains data */
91 if (Endpoint_IsReadWriteAllowed())
92 {
93 /* Create a temporary buffer to hold the read in report from the host */
94 uint8_t ConsoleData[CONSOLE_EPSIZE];
95
96 /* Read Console Report Data */
97 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
98
99 /* Process Console Report Data */
100 //ProcessConsoleHIDReport(ConsoleData);
101 }
102
103 /* Finalize the stream transfer to send the last packet */
104 Endpoint_ClearOUT();
105 }
106 #endif
107
108 /* IN packet */
109 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
110 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
111 Endpoint_SelectEndpoint(ep);
112 return;
113 }
114
115 // fill empty bank
116 while (Endpoint_IsReadWriteAllowed())
117 Endpoint_Write_8(0);
118
119 // flash senchar packet
120 if (Endpoint_IsINReady()) {
121 Endpoint_ClearIN();
122 }
123
124 Endpoint_SelectEndpoint(ep);
125 }
126 #else
127 static void Console_Task(void)
128 {
129 }
130 #endif
131
132
133 /*******************************************************************************
134 * USB Events
135 ******************************************************************************/
136 /** Event handler for the USB_Connect event. */
137 void EVENT_USB_Device_Connect(void)
138 {
139 }
140
141 /** Event handler for the USB_Disconnect event. */
142 void EVENT_USB_Device_Disconnect(void)
143 {
144 }
145
146 void EVENT_USB_Device_StartOfFrame(void)
147 {
148 Console_Task();
149 }
150
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.
153 */
154 void EVENT_USB_Device_ConfigurationChanged(void)
155 {
156 bool ConfigSuccess = true;
157
158 /* Setup Keyboard HID Report Endpoints */
159 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
160 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
161
162 #ifdef MOUSE_ENABLE
163 /* Setup Mouse HID Report Endpoint */
164 ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
165 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
166 #endif
167
168 #ifdef EXTRAKEY_ENABLE
169 /* Setup Extra HID Report Endpoint */
170 ConfigSuccess &= Endpoint_ConfigureEndpoint(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
171 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
172 #endif
173
174 #ifdef CONSOLE_ENABLE
175 /* Setup Console HID Report Endpoints */
176 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
177 CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
178 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
179 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
180 #endif
181 }
182
183 /*
184 Appendix G: HID Request Support Requirements
185
186 The following table enumerates the requests that need to be supported by various types of HID class devices.
187
188 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
189 ------------------------------------------------------------------------------------------
190 Boot Mouse Required Optional Optional Optional Required Required
191 Non-Boot Mouse Required Optional Optional Optional Optional Optional
192 Boot Keyboard Required Optional Required Required Required Required
193 Non-Boot Keybrd Required Optional Required Required Optional Optional
194 Other Device Required Optional Optional Optional Optional Optional
195 */
196 /** Event handler for the USB_ControlRequest event.
197 * This is fired before passing along unhandled control requests to the library for processing internally.
198 */
199 void EVENT_USB_Device_ControlRequest(void)
200 {
201 uint8_t* ReportData = NULL;
202 uint8_t ReportSize = 0;
203
204 /* Handle HID Class specific requests */
205 switch (USB_ControlRequest.bRequest)
206 {
207 case HID_REQ_GetReport:
208 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
209 {
210 Endpoint_ClearSETUP();
211
212 // Interface
213 switch (USB_ControlRequest.wIndex) {
214 case KEYBOARD_INTERFACE:
215 // TODO: test/check
216 ReportData = (uint8_t*)&keyboard_report_sent;
217 ReportSize = sizeof(keyboard_report_sent);
218 break;
219 }
220
221 /* Write the report data to the control endpoint */
222 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
223 Endpoint_ClearOUT();
224 }
225
226 break;
227 case HID_REQ_SetReport:
228 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
229 {
230
231 // Interface
232 switch (USB_ControlRequest.wIndex) {
233 case KEYBOARD_INTERFACE:
234 Endpoint_ClearSETUP();
235
236 while (!(Endpoint_IsOUTReceived())) {
237 if (USB_DeviceState == DEVICE_STATE_Unattached)
238 return;
239 }
240 keyboard_led_stats = Endpoint_Read_8();
241
242 Endpoint_ClearOUT();
243 Endpoint_ClearStatusStage();
244 break;
245 }
246
247 }
248
249 break;
250
251 case HID_REQ_GetProtocol:
252 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
253 {
254 Endpoint_ClearSETUP();
255 while (!(Endpoint_IsINReady()));
256 Endpoint_Write_8(protocol_report);
257 Endpoint_ClearIN();
258 Endpoint_ClearStatusStage();
259 }
260
261 break;
262 case HID_REQ_SetProtocol:
263 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
264 {
265 Endpoint_ClearSETUP();
266 Endpoint_ClearStatusStage();
267
268 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
269 }
270
271 break;
272 case HID_REQ_SetIdle:
273 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
274 {
275 Endpoint_ClearSETUP();
276 Endpoint_ClearStatusStage();
277
278 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
279 }
280
281 break;
282 case HID_REQ_GetIdle:
283 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
284 {
285 Endpoint_ClearSETUP();
286 while (!(Endpoint_IsINReady()));
287 Endpoint_Write_8(idle_duration);
288 Endpoint_ClearIN();
289 Endpoint_ClearStatusStage();
290 }
291
292 break;
293 }
294 }
295
296 /*******************************************************************************
297 * Host driver
298 ******************************************************************************/
299 static uint8_t keyboard_leds(void)
300 {
301 return keyboard_led_stats;
302 }
303
304 static void send_keyboard(report_keyboard_t *report)
305 {
306 uint8_t timeout = 0;
307
308 // TODO: handle NKRO report
309 /* Select the Keyboard Report Endpoint */
310 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
311
312 /* Check if Keyboard Endpoint Ready for Read/Write */
313 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
314
315 /* Write Keyboard Report Data */
316 Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
317
318 /* Finalize the stream transfer to send the last packet */
319 Endpoint_ClearIN();
320
321 keyboard_report_sent = *report;
322 }
323
324 static void send_mouse(report_mouse_t *report)
325 {
326 #ifdef MOUSE_ENABLE
327 uint8_t timeout = 0;
328
329 /* Select the Mouse Report Endpoint */
330 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
331
332 /* Check if Mouse Endpoint Ready for Read/Write */
333 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
334
335 /* Write Mouse Report Data */
336 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
337
338 /* Finalize the stream transfer to send the last packet */
339 Endpoint_ClearIN();
340 #endif
341 }
342
343 static void send_system(uint16_t data)
344 {
345 uint8_t timeout = 0;
346
347 report_extra_t r = {
348 .report_id = REPORT_ID_SYSTEM,
349 .usage = data
350 };
351 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
352 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
353 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
354 Endpoint_ClearIN();
355 }
356
357 static void send_consumer(uint16_t data)
358 {
359 uint8_t timeout = 0;
360
361 report_extra_t r = {
362 .report_id = REPORT_ID_CONSUMER,
363 .usage = data
364 };
365 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
366 while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
367 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
368 Endpoint_ClearIN();
369 }
370
371
372 /*******************************************************************************
373 * sendchar
374 ******************************************************************************/
375 #ifdef CONSOLE_ENABLE
376 #define SEND_TIMEOUT 5
377 int8_t sendchar(uint8_t c)
378 {
379 // Not wait once timeouted.
380 // Because sendchar() is called so many times, waiting each call causes big lag.
381 static bool timeouted = false;
382
383 if (USB_DeviceState != DEVICE_STATE_Configured)
384 return -1;
385
386 uint8_t ep = Endpoint_GetCurrentEndpoint();
387 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
388 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
389 Endpoint_SelectEndpoint(ep);
390 return -1;
391 }
392
393 if (timeouted && !Endpoint_IsReadWriteAllowed()) {
394 Endpoint_SelectEndpoint(ep);
395 return - 1;
396 }
397
398 timeouted = false;
399
400 uint8_t timeout = SEND_TIMEOUT;
401 uint16_t prevFN = USB_Device_GetFrameNumber();
402 while (!Endpoint_IsReadWriteAllowed()) {
403 switch (USB_DeviceState) {
404 case DEVICE_STATE_Unattached:
405 case DEVICE_STATE_Suspended:
406 return -1;
407 }
408 if (Endpoint_IsStalled()) {
409 Endpoint_SelectEndpoint(ep);
410 return -1;
411 }
412 if (prevFN != USB_Device_GetFrameNumber()) {
413 if (!(timeout--)) {
414 timeouted = true;
415 Endpoint_SelectEndpoint(ep);
416 return -1;
417 }
418 prevFN = USB_Device_GetFrameNumber();
419 }
420 }
421
422 Endpoint_Write_8(c);
423
424 // send when bank is full
425 if (!Endpoint_IsReadWriteAllowed())
426 Endpoint_ClearIN();
427
428 Endpoint_SelectEndpoint(ep);
429 return 0;
430 }
431 #else
432 int8_t sendchar(uint8_t c)
433 {
434 return 0;
435 }
436 #endif
437
438
439 /*******************************************************************************
440 * main
441 ******************************************************************************/
442 static void SetupHardware(void)
443 {
444 /* Disable watchdog if enabled by bootloader/fuses */
445 MCUSR &= ~(1 << WDRF);
446 wdt_disable();
447
448 /* Disable clock division */
449 clock_prescale_set(clock_div_1);
450
451 // Leonardo needs. Without this USB device is not recognized.
452 USB_Disable();
453
454 USB_Init();
455
456 // for Console_Task
457 USB_Device_EnableSOFEvents();
458 }
459
460 int main(void) __attribute__ ((weak));
461 int main(void)
462 {
463 SetupHardware();
464 keyboard_init();
465 host_set_driver(&lufa_driver);
466 sei();
467
468 // TODO: can't print here
469 debug("LUFA init\n");
470 while (1) {
471 keyboard_task();
472
473 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
474 USB_USBTask();
475 #endif
476 }
477 }
Imprint / Impressum