]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/lufa.c
Add USB_Host_Shield_2.0 and arduino-1.0.1 in usb_hid.
[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 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_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 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
94 USB_USBTask();
95 #endif
96 }
97 }
98
99 void 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();
109
110 // for Console_Task
111 USB_Device_EnableSOFEvents();
112 }
113
114 #ifdef CONSOLE_ENABLE
115 static void Console_Task(void)
116 {
117 /* Device must be connected and configured for the task to run */
118 if (USB_DeviceState != DEVICE_STATE_Configured)
119 return;
120
121 uint8_t ep = Endpoint_GetCurrentEndpoint();
122
123 #if 0
124 // TODO: impl receivechar()/recvchar()
125 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
126
127 /* Check to see if a packet has been sent from the host */
128 if (Endpoint_IsOUTReceived())
129 {
130 /* Check to see if the packet contains data */
131 if (Endpoint_IsReadWriteAllowed())
132 {
133 /* Create a temporary buffer to hold the read in report from the host */
134 uint8_t ConsoleData[CONSOLE_EPSIZE];
135
136 /* Read Console Report Data */
137 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
138
139 /* Process Console Report Data */
140 //ProcessConsoleHIDReport(ConsoleData);
141 }
142
143 /* Finalize the stream transfer to send the last packet */
144 Endpoint_ClearOUT();
145 }
146 #endif
147
148 /* IN packet */
149 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
150
151 // fill empty bank
152 while (Endpoint_IsReadWriteAllowed())
153 Endpoint_Write_8(0);
154
155 // flash senchar packet
156 if (Endpoint_IsINReady()) {
157 Endpoint_ClearIN();
158 }
159
160 Endpoint_SelectEndpoint(ep);
161 }
162 #else
163 static void Console_Task(void)
164 {
165 }
166 #endif
167
168
169 /*******************************************************************************
170 * USB Events
171 ******************************************************************************/
172 /** Event handler for the USB_Connect event. */
173 void EVENT_USB_Device_Connect(void)
174 {
175 }
176
177 /** Event handler for the USB_Disconnect event. */
178 void EVENT_USB_Device_Disconnect(void)
179 {
180 }
181
182 void EVENT_USB_Device_StartOfFrame(void)
183 {
184 Console_Task();
185 }
186
187 /** Event handler for the USB_ConfigurationChanged event.
188 * This is fired when the host sets the current configuration of the USB device after enumeration.
189 */
190 void EVENT_USB_Device_ConfigurationChanged(void)
191 {
192 bool ConfigSuccess = true;
193
194 /* Setup Keyboard HID Report Endpoints */
195 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
196 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
197
198 #ifdef MOUSE_ENABLE
199 /* Setup Mouse HID Report Endpoint */
200 ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
201 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
202 #endif
203
204 #ifdef EXTRAKEY_ENABLE
205 /* Setup Extra HID Report Endpoint */
206 ConfigSuccess &= Endpoint_ConfigureEndpoint(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
207 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
208 #endif
209
210 #ifdef CONSOLE_ENABLE
211 /* Setup Console HID Report Endpoints */
212 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
213 CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
214 ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
215 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
216 #endif
217 }
218
219 /*
220 Appendix G: HID Request Support Requirements
221
222 The following table enumerates the requests that need to be supported by various types of HID class devices.
223
224 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
225 ------------------------------------------------------------------------------------------
226 Boot Mouse Required Optional Optional Optional Required Required
227 Non-Boot Mouse Required Optional Optional Optional Optional Optional
228 Boot Keyboard Required Optional Required Required Required Required
229 Non-Boot Keybrd Required Optional Required Required Optional Optional
230 Other Device Required Optional Optional Optional Optional Optional
231 */
232 /** Event handler for the USB_ControlRequest event.
233 * This is fired before passing along unhandled control requests to the library for processing internally.
234 */
235 void EVENT_USB_Device_ControlRequest(void)
236 {
237 uint8_t* ReportData = NULL;
238 uint8_t ReportSize = 0;
239
240 /* Handle HID Class specific requests */
241 switch (USB_ControlRequest.bRequest)
242 {
243 case HID_REQ_GetReport:
244 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
245 {
246 Endpoint_ClearSETUP();
247
248 // Interface
249 switch (USB_ControlRequest.wIndex) {
250 case KEYBOARD_INTERFACE:
251 // TODO: test/check
252 ReportData = (uint8_t*)&keyboard_report_sent;
253 ReportSize = sizeof(keyboard_report_sent);
254 break;
255 }
256
257 /* Write the report data to the control endpoint */
258 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
259 Endpoint_ClearOUT();
260 }
261
262 break;
263 case HID_REQ_SetReport:
264 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
265 {
266
267 // Interface
268 switch (USB_ControlRequest.wIndex) {
269 case KEYBOARD_INTERFACE:
270 Endpoint_ClearSETUP();
271
272 while (!(Endpoint_IsOUTReceived())) {
273 if (USB_DeviceState == DEVICE_STATE_Unattached)
274 return;
275 }
276 keyboard_led_stats = Endpoint_Read_8();
277
278 Endpoint_ClearOUT();
279 Endpoint_ClearStatusStage();
280 break;
281 }
282
283 }
284
285 break;
286
287 case HID_REQ_GetProtocol:
288 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
289 {
290 Endpoint_ClearSETUP();
291 while (!(Endpoint_IsINReady()));
292 Endpoint_Write_8(protocol_report);
293 Endpoint_ClearIN();
294 Endpoint_ClearStatusStage();
295 }
296
297 break;
298 case HID_REQ_SetProtocol:
299 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
300 {
301 Endpoint_ClearSETUP();
302 Endpoint_ClearStatusStage();
303
304 protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
305 }
306
307 break;
308 case HID_REQ_SetIdle:
309 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
310 {
311 Endpoint_ClearSETUP();
312 Endpoint_ClearStatusStage();
313
314 idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
315 }
316
317 break;
318 case HID_REQ_GetIdle:
319 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
320 {
321 Endpoint_ClearSETUP();
322 while (!(Endpoint_IsINReady()));
323 Endpoint_Write_8(idle_duration);
324 Endpoint_ClearIN();
325 Endpoint_ClearStatusStage();
326 }
327
328 break;
329 }
330 }
331
332 /*******************************************************************************
333 * Host driver
334 ******************************************************************************/
335 static uint8_t keyboard_leds(void)
336 {
337 return keyboard_led_stats;
338 }
339
340 static void send_keyboard(report_keyboard_t *report)
341 {
342 // TODO: handle NKRO report
343 /* Select the Keyboard Report Endpoint */
344 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
345
346 /* Check if Keyboard Endpoint Ready for Read/Write */
347 while (!Endpoint_IsReadWriteAllowed()) ;
348
349 /* Write Keyboard Report Data */
350 Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
351
352 /* Finalize the stream transfer to send the last packet */
353 Endpoint_ClearIN();
354
355 keyboard_report_sent = *report;
356 }
357
358 static void send_mouse(report_mouse_t *report)
359 {
360 #ifdef MOUSE_ENABLE
361 /* Select the Mouse Report Endpoint */
362 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
363
364 /* Check if Mouse Endpoint Ready for Read/Write */
365 while (!Endpoint_IsReadWriteAllowed()) ;
366
367 /* Write Mouse Report Data */
368 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
369
370 /* Finalize the stream transfer to send the last packet */
371 Endpoint_ClearIN();
372 #endif
373 }
374
375 static void send_system(uint16_t data)
376 {
377 report_extra_t r = {
378 .report_id = REPORT_ID_SYSTEM,
379 .usage = data
380 };
381 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
382 while (!Endpoint_IsReadWriteAllowed()) ;
383 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
384 Endpoint_ClearIN();
385 }
386
387 static void send_consumer(uint16_t data)
388 {
389 report_extra_t r = {
390 .report_id = REPORT_ID_CONSUMER,
391 .usage = data
392 };
393 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
394 while (!Endpoint_IsReadWriteAllowed()) ;
395 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
396 Endpoint_ClearIN();
397 }
398
399
400 /*******************************************************************************
401 * sendchar
402 ******************************************************************************/
403 #ifdef CONSOLE_ENABLE
404 #define SEND_TIMEOUT 5
405 int8_t sendchar(uint8_t c)
406 {
407 if (USB_DeviceState != DEVICE_STATE_Configured)
408 return -1;
409
410 uint8_t ep = Endpoint_GetCurrentEndpoint();
411 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
412
413 uint8_t timeout = SEND_TIMEOUT;
414 uint16_t prevFN = USB_Device_GetFrameNumber();
415 while (!Endpoint_IsReadWriteAllowed()) {
416 switch (USB_DeviceState) {
417 case DEVICE_STATE_Unattached:
418 case DEVICE_STATE_Suspended:
419 return -1;
420 }
421 if (Endpoint_IsStalled()) {
422 Endpoint_SelectEndpoint(ep);
423 return -1;
424 }
425 if (prevFN != USB_Device_GetFrameNumber()) {
426 if (!(timeout--)) {
427 Endpoint_SelectEndpoint(ep);
428 return -1;
429 }
430 prevFN = USB_Device_GetFrameNumber();
431 }
432 }
433
434 Endpoint_Write_8(c);
435
436 // send when bank is full
437 if (!Endpoint_IsReadWriteAllowed())
438 Endpoint_ClearIN();
439
440 Endpoint_SelectEndpoint(ep);
441 return 0;
442 }
443 #else
444 int8_t sendchar(uint8_t c)
445 {
446 return 0;
447 }
448 #endif
Imprint / Impressum