]> git.gir.st - tmk_keyboard.git/blob - protocol/pjrc/usb_keyboard.c
Add NKRO support for LUFA
[tmk_keyboard.git] / protocol / pjrc / usb_keyboard.c
1 /* USB Keyboard Plus Debug Channel Example for Teensy USB Development Board
2 * http://www.pjrc.com/teensy/usb_keyboard.html
3 * Copyright (c) 2009 PJRC.COM, LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24 #include <avr/interrupt.h>
25 #include <avr/pgmspace.h>
26 #include "keycode.h"
27 #include "usb_keyboard.h"
28 #include "print.h"
29 #include "debug.h"
30 #include "util.h"
31 #include "host.h"
32
33
34 // protocol setting from the host. We use exactly the same report
35 // either way, so this variable only stores the setting since we
36 // are required to be able to report which setting is in use.
37 uint8_t usb_keyboard_protocol=1;
38
39 // the idle configuration, how often we send the report to the
40 // host (ms * 4) even when it hasn't changed
41 // Windows and Linux set 0 while OS X sets 6(24ms) by SET_IDLE request.
42 uint8_t usb_keyboard_idle_config=125;
43
44 // count until idle timeout
45 uint8_t usb_keyboard_idle_count=0;
46
47 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
48 volatile uint8_t usb_keyboard_leds=0;
49
50
51 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
52
53
54 int8_t usb_keyboard_send_report(report_keyboard_t *report)
55 {
56 int8_t result = 0;
57
58 #ifdef NKRO_ENABLE
59 if (keyboard_nkro)
60 result = send_report(report, KBD2_ENDPOINT, 0, KBD2_SIZE);
61 else
62 #endif
63 {
64 if (usb_keyboard_protocol)
65 result = send_report(report, KBD_ENDPOINT, 0, KBD_SIZE);
66 else
67 result = send_report(report, KBD_ENDPOINT, 0, 6);
68 }
69
70 if (result) return result;
71 usb_keyboard_idle_count = 0;
72 usb_keyboard_print_report(report);
73 return 0;
74 }
75
76 void usb_keyboard_print_report(report_keyboard_t *report)
77 {
78 if (!debug_keyboard) return;
79 print("keys: ");
80 for (int i = 0; i < REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
81 print(" mods: "); phex(report->mods); print("\n");
82 }
83
84
85 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
86 {
87 uint8_t intr_state, timeout;
88
89 if (!usb_configured()) return -1;
90 intr_state = SREG;
91 cli();
92 UENUM = endpoint;
93 timeout = UDFNUML + 50;
94 while (1) {
95 // are we ready to transmit?
96 if (UEINTX & (1<<RWAL)) break;
97 SREG = intr_state;
98 // has the USB gone offline?
99 if (!usb_configured()) return -1;
100 // have we waited too long?
101 if (UDFNUML == timeout) return -1;
102 // get ready to try checking again
103 intr_state = SREG;
104 cli();
105 UENUM = endpoint;
106 }
107 for (uint8_t i = keys_start; i < keys_end; i++) {
108 UEDATX = report->raw[i];
109 }
110 UEINTX = 0x3A;
111 SREG = intr_state;
112 return 0;
113 }
Imprint / Impressum