]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/usb_usb/usb_usb.cpp
lufa: Fix comment on INTERRUPT_CONTROL_ENDPOINT
[max/tmk_keyboard.git] / converter / usb_usb / usb_usb.cpp
1 /*
2 Copyright 2016 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdint.h>
19 #include <stdbool.h>
20
21 // USB HID host
22 #include "Usb.h"
23 #include "usbhub.h"
24 #include "usbhid.h"
25 #include "hidboot.h"
26 #include "parser.h"
27
28 #include "keycode.h"
29 #include "util.h"
30 #include "print.h"
31 #include "debug.h"
32 #include "timer.h"
33 #include "matrix.h"
34 #include "led.h"
35 #include "host.h"
36 #include "keyboard.h"
37
38 #include "hook.h"
39 #include "suspend.h"
40 #include "lufa.h"
41
42
43 /* KEY CODE to Matrix
44  *
45  * HID keycode(1 byte):
46  * Higher 5 bits indicates ROW and lower 3 bits COL.
47  *
48  *  7 6 5 4 3 2 1 0
49  * +---------------+
50  * |  ROW  |  COL  |
51  * +---------------+
52  *
53  * Matrix space(16 * 16):
54  *   r\c0123456789ABCDEF
55  *   0 +----------------+
56  *   : |                |
57  *   : |                |
58  *  16 +----------------+
59  */
60 #define ROW_MASK 0xF0
61 #define COL_MASK 0x0F
62 #define CODE(row, col)  (((row) << 4) | (col))
63 #define ROW(code)       (((code) & ROW_MASK) >> 4)
64 #define COL(code)       ((code) & COL_MASK)
65 #define ROW_BITS(code)  (1 << COL(code))
66
67
68 // Integrated key state of all keyboards
69 static report_keyboard_t keyboard_report;
70
71 static bool matrix_is_mod =false;
72
73 /*
74  * USB Host Shield HID keyboards
75  * This supports two cascaded hubs and four keyboards
76  */
77 USB usb_host;
78 HIDBoot<USB_HID_PROTOCOL_KEYBOARD>    kbd1(&usb_host);
79 HIDBoot<USB_HID_PROTOCOL_KEYBOARD>    kbd2(&usb_host);
80 HIDBoot<USB_HID_PROTOCOL_KEYBOARD>    kbd3(&usb_host);
81 HIDBoot<USB_HID_PROTOCOL_KEYBOARD>    kbd4(&usb_host);
82 KBDReportParser kbd_parser1;
83 KBDReportParser kbd_parser2;
84 KBDReportParser kbd_parser3;
85 KBDReportParser kbd_parser4;
86 USBHub hub1(&usb_host);
87 USBHub hub2(&usb_host);
88
89
90 uint8_t matrix_rows(void) { return MATRIX_ROWS; }
91 uint8_t matrix_cols(void) { return MATRIX_COLS; }
92 bool matrix_has_ghost(void) { return false; }
93 void matrix_init(void) {
94     debug_enable = true;
95     // USB Host Shield setup
96     usb_host.Init();
97     kbd1.SetReportParser(0, (HIDReportParser*)&kbd_parser1);
98     kbd2.SetReportParser(0, (HIDReportParser*)&kbd_parser2);
99     kbd3.SetReportParser(0, (HIDReportParser*)&kbd_parser3);
100     kbd4.SetReportParser(0, (HIDReportParser*)&kbd_parser4);
101 }
102
103 static void or_report(report_keyboard_t report) {
104     // integrate reports into keyboard_report
105     keyboard_report.mods |= report.mods;
106     for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
107         if (IS_ANY(report.keys[i])) {
108             for (uint8_t j = 0; j < KEYBOARD_REPORT_KEYS; j++) {
109                 if (! keyboard_report.keys[j]) {
110                     keyboard_report.keys[j] = report.keys[i];
111                     break;
112                 }
113             }
114         }
115     }
116 }
117
118 uint8_t matrix_scan(void) {
119     static uint16_t last_time_stamp1 = 0;
120     static uint16_t last_time_stamp2 = 0;
121     static uint16_t last_time_stamp3 = 0;
122     static uint16_t last_time_stamp4 = 0;
123
124     // check report came from keyboards
125     if (kbd_parser1.time_stamp != last_time_stamp1 ||
126         kbd_parser2.time_stamp != last_time_stamp2 ||
127         kbd_parser3.time_stamp != last_time_stamp3 ||
128         kbd_parser4.time_stamp != last_time_stamp4) {
129
130         last_time_stamp1 = kbd_parser1.time_stamp;
131         last_time_stamp2 = kbd_parser2.time_stamp;
132         last_time_stamp3 = kbd_parser3.time_stamp;
133         last_time_stamp4 = kbd_parser4.time_stamp;
134
135         // clear and integrate all reports
136         keyboard_report = {};
137         or_report(kbd_parser1.report);
138         or_report(kbd_parser2.report);
139         or_report(kbd_parser3.report);
140         or_report(kbd_parser4.report);
141
142         matrix_is_mod = true;
143     } else {
144         matrix_is_mod = false;
145     }
146
147     uint16_t timer;
148     timer = timer_read();
149     usb_host.Task();
150     timer = timer_elapsed(timer);
151     if (timer > 100) {
152         xprintf("host.Task: %d\n", timer);
153     }
154
155     static uint8_t usb_state = 0;
156     if (usb_state != usb_host.getUsbTaskState()) {
157         usb_state = usb_host.getUsbTaskState();
158         xprintf("usb_state: %02X\n", usb_state);
159
160         // restore LED state when keyboard comes up
161         if (usb_state == USB_STATE_RUNNING) {
162             xprintf("speed: %s\n", usb_host.getVbusState()==FSHOST ? "full" : "low");
163             keyboard_set_leds(host_keyboard_leds());
164         }
165     }
166     return 1;
167 }
168
169 bool matrix_is_modified(void) {
170     return matrix_is_mod;
171 }
172
173 bool matrix_is_on(uint8_t row, uint8_t col) {
174     uint8_t code = CODE(row, col);
175
176     if (IS_MOD(code)) {
177         if (keyboard_report.mods & ROW_BITS(code)) {
178             return true;
179         }
180     }
181     for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
182         if (keyboard_report.keys[i] == code) {
183             return true;
184         }
185     }
186     return false;
187 }
188
189 matrix_row_t matrix_get_row(uint8_t row) {
190     uint16_t row_bits = 0;
191
192     if (IS_MOD(CODE(row, 0)) && keyboard_report.mods) {
193         row_bits |= keyboard_report.mods;
194     }
195
196     for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
197         if (IS_ANY(keyboard_report.keys[i])) {
198             if (row == ROW(keyboard_report.keys[i])) {
199                 row_bits |= ROW_BITS(keyboard_report.keys[i]);
200             }
201         }
202     }
203     return row_bits;
204 }
205
206 uint8_t matrix_key_count(void) {
207     uint8_t count = 0;
208
209     count += bitpop(keyboard_report.mods);
210     for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
211         if (IS_ANY(keyboard_report.keys[i])) {
212             count++;
213         }
214     }
215     return count;
216 }
217
218 void matrix_print(void) {
219 }
220
221 void led_set(uint8_t usb_led)
222 {
223     if (kbd1.isReady()) kbd1.SetReport(0, 0, 2, 0, 1, &usb_led);
224     if (kbd2.isReady()) kbd2.SetReport(0, 0, 2, 0, 1, &usb_led);
225     if (kbd3.isReady()) kbd3.SetReport(0, 0, 2, 0, 1, &usb_led);
226     if (kbd4.isReady()) kbd4.SetReport(0, 0, 2, 0, 1, &usb_led);
227 }
228
229 // We need to keep doing UHS2 USB::Task() to initialize keyboard
230 // even before USB is not configured.
231 void hook_usb_startup_wait_loop(void)
232 {
233     matrix_scan();
234 }
235
236 // We need to keep doing UHS2 USB::Task() to initialize keyboard
237 // even during USB bus is suspended and remote wakeup is not enabled yet on LUFA side.
238 // This situation can happen just after pluging converter into USB port.
239 void hook_usb_suspend_loop(void)
240 {
241 #ifndef TMK_LUFA_DEBUG_UART
242     // This corrupts debug print when suspend
243     suspend_power_down();
244 #endif
245     if (USB_Device_RemoteWakeupEnabled) {
246         if (suspend_wakeup_condition()) {
247             USB_Device_SendRemoteWakeup();
248         }
249     } else {
250         matrix_scan();
251     }
252 }