]> git.donarmstrong.com Git - tmk_firmware.git/blob - protocol/lufa/lufa.c
confirm SetReport LED.
[tmk_firmware.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 static host_driver_t lufa_driver = {
63     keyboard_leds,
64     send_keyboard,
65     send_mouse,
66     send_system,
67     send_consumer
68 };
69
70
71 static void SetupHardware(void);
72 static void Console_HID_Task(void);
73
74 int main(void)
75 {
76     SetupHardware();
77     sei();
78
79     print_enable = true;
80     debug_enable = true;
81     debug_matrix = true;
82     debug_keyboard = true;
83     debug_mouse = true;
84
85     // TODO: can't print here
86     debug("LUFA init\n");
87
88     keyboard_init();
89     host_set_driver(&lufa_driver);
90     while (1) {
91         keyboard_proc();
92
93         Console_HID_Task();
94         USB_USBTask();
95     }
96 }
97
98 void SetupHardware(void)
99 {
100     /* Disable watchdog if enabled by bootloader/fuses */
101     MCUSR &= ~(1 << WDRF);
102     wdt_disable();
103
104     /* Disable clock division */
105     clock_prescale_set(clock_div_1);
106
107     USB_Init();
108 }
109
110 static void Console_HID_Task(void)
111 {
112         /* Device must be connected and configured for the task to run */
113         if (USB_DeviceState != DEVICE_STATE_Configured)
114           return;
115
116         // TODO: impl receivechar()/recvchar()
117         Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
118
119         /* Check to see if a packet has been sent from the host */
120         if (Endpoint_IsOUTReceived())
121         {
122                 /* Check to see if the packet contains data */
123                 if (Endpoint_IsReadWriteAllowed())
124                 {
125                         /* Create a temporary buffer to hold the read in report from the host */
126                         uint8_t ConsoleData[CONSOLE_EPSIZE];
127
128                         /* Read Console Report Data */
129                         Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
130
131                         /* Process Console Report Data */
132                         //ProcessConsoleHIDReport(ConsoleData);
133                 }
134
135                 /* Finalize the stream transfer to send the last packet */
136                 Endpoint_ClearOUT();
137         }
138
139         /* IN packet */
140         Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
141         // send IN packet
142         if (Endpoint_IsINReady())
143             Endpoint_ClearIN();
144 }
145
146
147 /*******************************************************************************
148  * USB Events
149  ******************************************************************************/
150 /** Event handler for the USB_Connect event. */
151 void EVENT_USB_Device_Connect(void)
152 {
153 }
154
155 /** Event handler for the USB_Disconnect event. */
156 void EVENT_USB_Device_Disconnect(void)
157 {
158 }
159
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.
162  */
163 void EVENT_USB_Device_ConfigurationChanged(void)
164 {
165     bool ConfigSuccess = true;
166
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);
170
171 #ifdef MOUSE_ENABLE
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);
175 #endif
176
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);
181 #endif
182
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);
188 }
189
190 /*
191 Appendix G: HID Request Support Requirements
192
193 The following table enumerates the requests that need to be supported by various types of HID class devices.
194
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
202 */
203 /** Event handler for the USB_ControlRequest event.
204  *  This is fired before passing along unhandled control requests to the library for processing internally.
205  */
206 void EVENT_USB_Device_ControlRequest(void)
207 {
208     uint8_t* ReportData = NULL;
209     uint8_t  ReportSize = 0;
210
211     /* Handle HID Class specific requests */
212     switch (USB_ControlRequest.bRequest)
213     {
214         case HID_REQ_GetReport:
215             if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
216             {
217                 Endpoint_ClearSETUP();
218
219                 // Interface
220                 switch (USB_ControlRequest.wIndex) {
221                 case KEYBOARD_INTERFACE:
222                     // TODO: test/check
223                     ReportData = (uint8_t*)&keyboard_report_sent;
224                     ReportSize = sizeof(keyboard_report_sent);
225                     break;
226                 }
227
228                 /* Write the report data to the control endpoint */
229                 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
230                 Endpoint_ClearOUT();
231             }
232
233             break;
234         case HID_REQ_SetReport:
235             if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
236             {
237
238                 // Interface
239                 switch (USB_ControlRequest.wIndex) {
240                 case KEYBOARD_INTERFACE:
241                     Endpoint_ClearSETUP();
242
243                     while (!(Endpoint_IsOUTReceived())) {
244                         if (USB_DeviceState == DEVICE_STATE_Unattached)
245                           return;
246                     }
247                     keyboard_led_stats = Endpoint_Read_8();
248
249                     Endpoint_ClearOUT();
250                     Endpoint_ClearStatusStage();
251                     break;
252                 }
253
254             }
255
256             break;
257
258         case HID_REQ_GetProtocol:
259             if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
260             {
261                 Endpoint_ClearSETUP();
262                 while (!(Endpoint_IsINReady()));
263                 Endpoint_Write_8(protocol_report);
264                 Endpoint_ClearIN();
265                 Endpoint_ClearStatusStage();
266             }
267
268             break;
269         case HID_REQ_SetProtocol:
270             if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
271             {
272                 Endpoint_ClearSETUP();
273                 Endpoint_ClearStatusStage();
274
275                 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
276             }
277
278             break;
279         case HID_REQ_SetIdle:
280             if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
281             {
282                 Endpoint_ClearSETUP();
283                 Endpoint_ClearStatusStage();
284
285                 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
286             }
287
288             break;
289         case HID_REQ_GetIdle:
290             if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
291             {
292                 Endpoint_ClearSETUP();
293                 while (!(Endpoint_IsINReady()));
294                 Endpoint_Write_8(idle_duration);
295                 Endpoint_ClearIN();
296                 Endpoint_ClearStatusStage();
297             }
298
299             break;
300     }
301 }
302
303 /*******************************************************************************
304  * Host driver 
305  ******************************************************************************/
306 static uint8_t keyboard_leds(void)
307 {
308     return keyboard_led_stats;
309 }
310
311 static void send_keyboard(report_keyboard_t *report)
312 {
313     // TODO: handle NKRO report
314     /* Select the Keyboard Report Endpoint */
315     Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
316
317     /* Check if Keyboard Endpoint Ready for Read/Write */
318     if (Endpoint_IsReadWriteAllowed())
319     {
320         /* Write Keyboard Report Data */
321         Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
322
323         /* Finalize the stream transfer to send the last packet */
324         Endpoint_ClearIN();
325     }
326     keyboard_report_sent = *report;
327 }
328
329 static void send_mouse(report_mouse_t *report)
330 {
331 #ifdef MOUSE_ENABLE
332     /* Select the Mouse Report Endpoint */
333     Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
334
335     /* Check if Mouse Endpoint Ready for Read/Write */
336     if (Endpoint_IsReadWriteAllowed())
337     {
338         /* Write Mouse Report Data */
339         Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
340
341         /* Finalize the stream transfer to send the last packet */
342         Endpoint_ClearIN();
343     }
344 #endif
345 }
346
347 static void send_system(uint16_t data)
348 {
349     report_extra_t r = {
350         .report_id = REPORT_ID_SYSTEM,
351         .usage = data
352     };
353     Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
354     if (Endpoint_IsReadWriteAllowed()) {
355         Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
356         Endpoint_ClearIN();
357     }
358 }
359
360 static void send_consumer(uint16_t data)
361 {
362     report_extra_t r = {
363         .report_id = REPORT_ID_CONSUMER,
364         .usage = data
365     };
366     Endpoint_SelectEndpoint(EXTRA_IN_EPNUM);
367     if (Endpoint_IsReadWriteAllowed()) {
368         Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
369         Endpoint_ClearIN();
370     }
371 }
372
373
374 /*******************************************************************************
375  * sendchar
376  ******************************************************************************/
377 int8_t sendchar(uint8_t c)
378 {
379     if (USB_DeviceState != DEVICE_STATE_Configured)
380       return -1;
381
382     Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
383
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:
390             return -1;
391         }
392         if (Endpoint_IsStalled())
393             return -1;
394         if (prevFN != USB_Device_GetFrameNumber()) {
395             if (!(timeout--))
396                 return -1;
397             prevFN = USB_Device_GetFrameNumber();
398         }
399     }
400
401     Endpoint_Write_8(c);
402
403     // send when packet is full
404     if (!Endpoint_IsReadWriteAllowed())
405         Endpoint_ClearIN();
406
407     return 0;
408 }