]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/pjrc/usb_keyboard.c
Merge branch 'master' into coderkun_neo2
[qmk_firmware.git] / tmk_core / 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 #ifdef NKRO_ENABLE
34   #include "keycode_config.h"
35
36   extern keymap_config_t keymap_config;
37 #endif
38
39
40 // protocol setting from the host.  We use exactly the same report
41 // either way, so this variable only stores the setting since we
42 // are required to be able to report which setting is in use.
43 uint8_t keyboard_protocol=1;
44
45 // the idle configuration, how often we send the report to the
46 // host (ms * 4) even when it hasn't changed
47 // Windows and Linux set 0 while OS X sets 6(24ms) by SET_IDLE request.
48 uint8_t keyboard_idle=125;
49
50 // count until idle timeout
51 uint8_t usb_keyboard_idle_count=0;
52
53 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
54 volatile uint8_t usb_keyboard_leds=0;
55
56
57 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
58
59
60 int8_t usb_keyboard_send_report(report_keyboard_t *report)
61 {
62     int8_t result = 0;
63
64 #ifdef NKRO_ENABLE
65     if (keymap_config.nkro)
66         result = send_report(report, KBD2_ENDPOINT, 0, KBD2_SIZE);
67     else
68 #endif
69     {
70         result = send_report(report, KBD_ENDPOINT, 0, KBD_SIZE);
71     }
72
73     if (result) return result;
74     usb_keyboard_idle_count = 0;
75     usb_keyboard_print_report(report);
76     return 0;
77 }
78
79 void usb_keyboard_print_report(report_keyboard_t *report)
80 {
81     if (!debug_keyboard) return;
82     print("keys: ");
83     for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
84     print(" mods: "); phex(report->mods); print("\n");
85 }
86
87
88 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
89 {
90     uint8_t intr_state, timeout;
91
92     if (!usb_configured()) return -1;
93     intr_state = SREG;
94     cli();
95     UENUM = endpoint;
96     timeout = UDFNUML + 50;
97     while (1) {
98             // are we ready to transmit?
99             if (UEINTX & (1<<RWAL)) break;
100             SREG = intr_state;
101             // has the USB gone offline?
102             if (!usb_configured()) return -1;
103             // have we waited too long?
104             if (UDFNUML == timeout) return -1;
105             // get ready to try checking again
106             intr_state = SREG;
107             cli();
108             UENUM = endpoint;
109     }
110     for (uint8_t i = keys_start; i < keys_end; i++) {
111             UEDATX = report->raw[i];
112     }
113     UEINTX = 0x3A;
114     SREG = intr_state;
115     return 0;
116 }