]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - pjrc/usb_keyboard.c
FIX: send last report when idle timeouts. (pjrc)
[max/tmk_keyboard.git] / pjrc / usb_keyboard.c
1 #include <avr/interrupt.h>
2 #include <avr/pgmspace.h>
3 #include "usb_keycodes.h"
4 #include "usb_keyboard.h"
5 #include "print.h"
6 #include "debug.h"
7 #include "util.h"
8 #include "host.h"
9
10
11 // protocol setting from the host.  We use exactly the same report
12 // either way, so this variable only stores the setting since we
13 // are required to be able to report which setting is in use.
14 uint8_t usb_keyboard_protocol=1;
15
16 // the idle configuration, how often we send the report to the
17 // host (ms * 4) even when it hasn't changed
18 // Windows and Linux set 0 while OS X sets 6(24ms) by SET_IDLE request.
19 uint8_t usb_keyboard_idle_config=125;
20
21 // count until idle timeout
22 uint8_t usb_keyboard_idle_count=0;
23
24 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
25 volatile uint8_t usb_keyboard_leds=0;
26
27
28 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
29
30
31 int8_t usb_keyboard_send_report(report_keyboard_t *report)
32 {
33     int8_t result = 0;
34
35 #ifdef USB_NKRO_ENABLE
36     if (keyboard_nkro)
37         result = send_report(report, KBD2_ENDPOINT, 0, KBD2_REPORT_KEYS);
38     else
39 #endif
40     {
41         if (usb_keyboard_protocol)
42             result = send_report(report, KBD_ENDPOINT, 0, KBD_REPORT_KEYS);
43         else
44             result = send_report(report, KBD_ENDPOINT, 0, 6);
45     }
46
47     if (result) return result;
48     usb_keyboard_idle_count = 0;
49     usb_keyboard_print_report(report);
50     return 0;
51 }
52
53 void usb_keyboard_print_report(report_keyboard_t *report)
54 {
55     if (!debug_keyboard) return;
56     print("keys: ");
57     for (int i = 0; i < REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
58     print(" mods: "); phex(report->mods); print("\n");
59 }
60
61
62 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
63 {
64     uint8_t intr_state, timeout;
65
66     if (!usb_configured()) return -1;
67     intr_state = SREG;
68     cli();
69     UENUM = endpoint;
70     timeout = UDFNUML + 50;
71     while (1) {
72             // are we ready to transmit?
73             if (UEINTX & (1<<RWAL)) break;
74             SREG = intr_state;
75             // has the USB gone offline?
76             if (!usb_configured()) return -1;
77             // have we waited too long?
78             if (UDFNUML == timeout) return -1;
79             // get ready to try checking again
80             intr_state = SREG;
81             cli();
82             UENUM = endpoint;
83     }
84     UEDATX = report->mods;
85 #ifdef USB_NKRO_ENABLE
86     if (!keyboard_nkro)
87         UEDATX = 0;
88 #else
89     UEDATX = 0;
90 #endif
91     for (uint8_t i = keys_start; i < keys_end; i++) {
92             UEDATX = report->keys[i];
93     }
94     UEINTX = 0x3A;
95     SREG = intr_state;
96     return 0;
97 }