]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/ibmpc_usb/ibmpc_usb.c
95d6c9d7ec0f85fc6202fc19f9d34ccb80aa80d6
[max/tmk_keyboard.git] / converter / ibmpc_usb / ibmpc_usb.c
1 /*
2 Copyright 2019,2020 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 #include "print.h"
21 #include "util.h"
22 #include "debug.h"
23 #include "ibmpc.h"
24 #include "host.h"
25 #include "led.h"
26 #include "matrix.h"
27 #include "timer.h"
28 #include "action.h"
29 #include "ibmpc_usb.h"
30 #include "ibmpc.h"
31
32
33 static void matrix_make(uint8_t code);
34 static void matrix_break(uint8_t code);
35
36 static int8_t process_cs1(void);
37 static int8_t process_cs2(void);
38 static int8_t process_cs3(void);
39
40
41 static uint8_t matrix[MATRIX_ROWS];
42 #define ROW(code)      ((code>>3)&0x0F)
43 #define COL(code)      (code&0x07)
44
45 static int16_t read_wait(uint16_t wait_ms)
46 {
47     uint16_t start = timer_read();
48     int16_t code;
49     while ((code = ibmpc_host_recv()) == -1 && timer_elapsed(start) < wait_ms);
50     return code;
51 }
52
53 static uint16_t read_keyboard_id(void)
54 {
55     uint16_t id = 0;
56     int16_t  code = 0;
57
58     // Disable
59     //code = ibmpc_host_send(0xF5);
60
61     // Read ID
62     code = ibmpc_host_send(0xF2);
63     if (code == -1) { id = 0xFFFF; goto DONE; }     // XT or No keyboard
64     if (code != 0xFA) { id = 0xFFFE; goto DONE; }   // Broken PS/2?
65
66     // ID takes 500ms max TechRef [8] 4-41
67     code = read_wait(500);
68     if (code == -1) { id = 0x0000; goto DONE; }     // AT
69     id = (code & 0xFF)<<8;
70
71     code = read_wait(500);
72     id |= code & 0xFF;
73
74 DONE:
75     // Enable
76     //code = ibmpc_host_send(0xF4);
77
78     return id;
79 }
80
81 void hook_early_init(void)
82 {
83     ibmpc_host_init();
84     ibmpc_host_enable();
85 }
86
87 void matrix_init(void)
88 {
89     debug_enable = true;
90
91     // initialize matrix state: all keys off
92     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
93
94     return;
95 }
96
97 /*
98  * keyboard recognition
99  *
100  * 1. Send F2 to get keyboard ID
101  *      a. no ACK(FA): XT keyobard
102  *      b. ACK but no ID: 84-key AT keyboard CodeSet2
103  *      c. ID is AB 83: PS/2 keyboard CodeSet2
104  *      d. ID is BF BF: Terminal keyboard CodeSet3
105  *      e. error on recv: maybe broken PS/2
106  */
107 uint16_t keyboard_id = 0x0000;
108 keyboard_kind_t keyboard_kind = NONE;
109 uint8_t matrix_scan(void)
110 {
111     // scan code reading states
112     static enum {
113         INIT,
114         XT_RESET,
115         XT_RESET_WAIT,
116         XT_RESET_DONE,
117         WAIT_AA,
118         WAIT_AABF,
119         WAIT_AABFBF,
120         READ_ID,
121         SETUP,
122         LOOP,
123     } state = INIT;
124     static uint16_t init_time;
125
126
127     if (ibmpc_error) {
128         xprintf("\nERR:%02X\n", ibmpc_error);
129
130         // when recv error, neither send error nor buffer full
131         if (!(ibmpc_error & (IBMPC_ERR_SEND | IBMPC_ERR_FULL))) {
132             // keyboard init again
133             if (state == LOOP) {
134                 xprintf("init\n");
135                 state = INIT;
136             }
137         }
138
139         // clear or process error
140         ibmpc_error = IBMPC_ERR_NONE;
141     }
142
143     switch (state) {
144         case INIT:
145             xprintf("I%u ", timer_read());
146             keyboard_kind = NONE;
147             keyboard_id = 0x0000;
148
149             matrix_clear();
150             clear_keyboard();
151
152             state = XT_RESET;
153             break;
154         case XT_RESET:
155             // Reset XT-initialize keyboard
156             // XT: hard reset 500ms for IBM XT Type-1 keyboard and clones
157             // XT: soft reset 20ms min(clock Lo)
158             ibmpc_host_disable();   // soft reset: inihibit(clock Lo/Data Hi)
159             IBMPC_RST_LO();         // hard reset: reset pin Lo
160
161             init_time = timer_read();
162             state = XT_RESET_WAIT;
163             break;
164         case XT_RESET_WAIT:
165             if (timer_elapsed(init_time) > 500) {
166                 state = XT_RESET_DONE;
167             }
168             break;
169         case XT_RESET_DONE:
170             IBMPC_RST_HIZ();        // hard reset: reset pin HiZ
171             ibmpc_host_isr_clear();
172             ibmpc_host_enable();    // soft reset: idle(clock Hi/Data Hi)
173
174             xprintf("X%u ", timer_read());
175             init_time = timer_read();
176             state = WAIT_AA;
177             break;
178         case WAIT_AA:
179             // 1) Read BAT code and ID on keybaord power-up
180             // For example, XT/AT sends 'AA' and Terminal sends 'AA BF BF' after BAT
181             // AT 84-key: POR and BAT can take 900-9900ms according to AT TechRef [8] 4-7
182             // AT 101/102-key: POR and BAT can take 450-2500ms according to AT TechRef [8] 4-39
183             // 2) Read key typed by user or anything after error on protocol or scan code
184             // This can happen in case of keyboard hotswap, unstable hardware, signal integrity problem or bug
185
186             /* wait until keyboard sends any code without 10000ms timeout
187             if (timer_elapsed(init_time) > 10000) {
188                 state = READ_ID;
189             }
190             */
191             if (ibmpc_host_recv() != -1) {  // wait for AA
192                 xprintf("W%u ", timer_read());
193                 init_time = timer_read();
194                 state = WAIT_AABF;
195             }
196             break;
197         case WAIT_AABF:
198             // NOTE: we can omit to wait BF BF
199             // ID takes 500ms max? TechRef [8] 4-41, though 1ms is enough for 122-key Terminal 6110345
200             if (timer_elapsed(init_time) > 500) {
201                 state = READ_ID;
202             }
203             if (ibmpc_host_recv() != -1) {  // wait for BF
204                 xprintf("W%u ", timer_read());
205                 init_time = timer_read();
206                 state = WAIT_AABFBF;
207             }
208             break;
209         case WAIT_AABFBF:
210             if (timer_elapsed(init_time) > 500) {
211                 state = READ_ID;
212             }
213             if (ibmpc_host_recv() != -1) {  // wait for BF
214                 xprintf("W%u ", timer_read());
215                 state = READ_ID;
216             }
217             break;
218         case READ_ID:
219             xprintf("R%u ", timer_read());
220
221             keyboard_id = read_keyboard_id();
222             if (ibmpc_error) {
223                 xprintf("\nERR:%02X\n", ibmpc_error);
224                 ibmpc_error = IBMPC_ERR_NONE;
225             }
226
227             if (0x0000 == keyboard_id) {            // CodeSet2 AT(IBM PC AT 84-key)
228                 keyboard_kind = PC_AT;
229             } else if (0xFFFF == keyboard_id) {     // CodeSet1 XT
230                 keyboard_kind = PC_XT;
231             } else if (0xFFFE == keyboard_id) {     // CodeSet2 PS/2 fails to response?
232                 keyboard_kind = PC_AT;
233             } else if (0x00FF == keyboard_id) {     // Mouse is not supported
234                 xprintf("Mouse: not supported\n");
235                 keyboard_kind = NONE;
236 #ifdef G80_2551_SUPPORT
237             } else if (0xAB86 == keyboard_id) {     // CodeSet2 PS/2 Terminal
238                 // For G80-2551 and other 122-key terminal keyboards
239                 // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#ab86
240                 if ((0xFA == ibmpc_host_send(0xF0)) &&
241                     (0xFA == ibmpc_host_send(0x03))) {
242                     // switch to code set 3
243                     keyboard_kind = PC_TERMINAL;
244                 } else {
245                     keyboard_kind = PC_AT;
246                 }
247 #endif
248             } else if (0xAB00 == (keyboard_id & 0xFF00)) {  // CodeSet2 PS/2
249                 keyboard_kind = PC_AT;
250             } else if (0xBF00 == (keyboard_id & 0xFF00)) {  // CodeSet3 Terminal
251                 keyboard_kind = PC_TERMINAL;
252             } else {
253                 keyboard_kind = PC_AT;
254             }
255
256             xprintf("ID:%04X(%d)\n", keyboard_id, keyboard_kind);
257
258             state = SETUP;
259             break;
260         case SETUP:
261             xprintf("S%u ", timer_read());
262             switch (keyboard_kind) {
263                 case PC_XT:
264                     break;
265                 case PC_AT:
266                     led_set(host_keyboard_leds());
267                     break;
268                 case PC_TERMINAL:
269                     ibmpc_host_send(0xF8);
270                     break;
271                 default:
272                     break;
273             }
274             state = LOOP;
275             xprintf("L%u ", timer_read());
276         case LOOP:
277             switch (keyboard_kind) {
278                 case PC_XT:
279                     if (process_cs1() == -1) state = INIT;
280                     break;
281                 case PC_AT:
282                     if (process_cs2() == -1) state = INIT;
283                     break;
284                 case PC_TERMINAL:
285                     if (process_cs3() == -1) state = INIT;
286                     break;
287                 default:
288                     break;
289             }
290             break;
291         default:
292             break;
293     }
294     return 1;
295 }
296
297 inline
298 bool matrix_is_on(uint8_t row, uint8_t col)
299 {
300     return (matrix[row] & (1<<col));
301 }
302
303 inline
304 uint8_t matrix_get_row(uint8_t row)
305 {
306     return matrix[row];
307 }
308
309 uint8_t matrix_key_count(void)
310 {
311     uint8_t count = 0;
312     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
313         count += bitpop(matrix[i]);
314     }
315     return count;
316 }
317
318
319 inline
320 static void matrix_make(uint8_t code)
321 {
322     if (!matrix_is_on(ROW(code), COL(code))) {
323         matrix[ROW(code)] |= 1<<COL(code);
324     }
325 }
326
327 inline
328 static void matrix_break(uint8_t code)
329 {
330     if (matrix_is_on(ROW(code), COL(code))) {
331         matrix[ROW(code)] &= ~(1<<COL(code));
332     }
333 }
334
335 void matrix_clear(void)
336 {
337     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
338 }
339
340 void led_set(uint8_t usb_led)
341 {
342     //if (keyboard_kind != PC_AT) return;
343
344     uint8_t ibmpc_led = 0;
345     if (usb_led &  (1<<USB_LED_SCROLL_LOCK))
346         ibmpc_led |= (1<<IBMPC_LED_SCROLL_LOCK);
347     if (usb_led &  (1<<USB_LED_NUM_LOCK))
348         ibmpc_led |= (1<<IBMPC_LED_NUM_LOCK);
349     if (usb_led &  (1<<USB_LED_CAPS_LOCK))
350         ibmpc_led |= (1<<IBMPC_LED_CAPS_LOCK);
351     ibmpc_host_set_led(ibmpc_led);
352 }
353
354
355 /*******************************************************************************
356  * XT: Scan Code Set 1
357  *
358  * See [3], [a]
359  *
360  * E0-escaped scan codes are translated into unused range of the matrix.(54-7F)
361  *
362  *     01-53: Normal codes used in original XT keyboard
363  *     54-7F: Not used in original XT keyboard
364  *
365  *         0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
366  *     50  -   -   -   -   *   *   x   x   x   x   *   *   *   *   *   *
367  *     60  *   *   *   *   x   x   x   x   x   x   x   x   x   x   x   *
368  *     70  x   *   *   x   *   *   x   *   *   x   *   x   *   x   x   *
369  *
370  * -: codes existed in original XT keyboard
371  * *: E0-escaped codes translated
372  * x: Non-espcaped codes(Some are not used in real keyboards probably)
373  *
374  * Codes assigned in range 54-7F:
375  *
376  *     50  -                60  Up*                 70  KANAx
377  *     51  -                61  Left*               71  Insert*
378  *     52  -                62  Down*               72  Delete*
379  *     53  -                63  Right*              73  ROx
380  *     54  PrintScr*        64  F13x                74  Home*
381  *     55  Pause*           65  F14x                75  End*
382  *     56  Euro2x           66  F15x                76  F24x
383  *     57  F11x             67  F16x                77  PageUp*
384  *     58  F12x             68  F17x                78  PageDown*
385  *     59  Keypad=x         69  F18x                79  HENKANx
386  *     5A  LGUI*            6A  F19x                7A  RCTL*
387  *     5B  RGUI*            6B  F20x                7B  MUHENKANx
388  *     5C  APP*             6C  F21x                7C  RALT*
389  *     5D  Mute*            6D  F22x                7D  JPYx
390  *     5E  Volume Down*     6E  F23x                7E  Keypad,x
391  *     5F  Volume Up*       6F  Keypad Enter*       7F  Keypad/ *
392  */
393 static uint8_t cs1_e0code(uint8_t code) {
394     switch(code) {
395         // Original IBM XT keyboard doesn't use E0-codes probably
396         // Some XT compatilble keyobards need these keys?
397         case 0x37: return 0x54; // Print Screen
398         case 0x46: return 0x55; // Ctrl + Pause
399         case 0x5B: return 0x5A; // Left  GUI
400         case 0x5C: return 0x5B; // Right GUI
401         case 0x5D: return 0x5C; // Application
402         case 0x20: return 0x5D; // Mute
403         case 0x2E: return 0x5E; // Volume Down
404         case 0x30: return 0x5F; // Volume Up
405         case 0x48: return 0x60; // Up
406         case 0x4B: return 0x61; // Left
407         case 0x50: return 0x62; // Down
408         case 0x4D: return 0x63; // Right
409         case 0x1C: return 0x6F; // Keypad Enter
410         case 0x52: return 0x71; // Insert
411         case 0x53: return 0x72; // Delete
412         case 0x47: return 0x74; // Home
413         case 0x4F: return 0x75; // End
414         case 0x49: return 0x77; // Page Up
415         case 0x51: return 0x78; // Page Down
416         case 0x1D: return 0x7A; // Right Ctrl
417         case 0x38: return 0x7C; // Right Alt
418         case 0x35: return 0x7F; // Keypad /
419
420         // Shared matrix cell with other keys
421         case 0x5E: return 0x70; // Power (KANA)
422         case 0x5F: return 0x79; // Sleep (HENKAN)
423         case 0x63: return 0x7B; // Wake  (MUHENKAN)
424
425         default:
426            xprintf("!CS1_E0_%02X!\n", code);
427            return code;
428     }
429     return 0x00;
430 }
431
432 static int8_t process_cs1(void)
433 {
434     static enum {
435         INIT,
436         E0,
437         // Pause: E1 1D 45, E1 9D C5 [a] (TODO: test)
438         E1,
439         E1_1D,
440         E1_9D,
441     } state = INIT;
442
443     uint16_t code = ibmpc_host_recv();
444     if (code == -1) {
445         return 0;
446     }
447
448     // Check invalid codes; 0x59-7F won't be used in real XT keyboards probably
449     // 0x62 is used to handle escape code E0 and E1
450     if ((code & 0x7F) >= 0x62) {
451         xprintf("!CS1_INV!\n");
452         state = INIT;
453         return -1;
454     }
455
456     switch (state) {
457         case INIT:
458             switch (code) {
459                 case 0x00:
460                 case 0xFF:  // Error/Overrun [3]p.26
461                     xprintf("!CS1_ERR!\n");
462                     return -1;
463                     break;
464                 case 0xE0:
465                     state = E0;
466                     break;
467                 case 0xE1:
468                     state = E1;
469                     break;
470                 default:
471                     if (code < 0x80)
472                         matrix_make(code);
473                     else
474                         matrix_break(code & 0x7F);
475                     break;
476             }
477             break;
478         case E0:
479             switch (code) {
480                 case 0x2A:
481                 case 0xAA:
482                 case 0x36:
483                 case 0xB6:
484                     //ignore fake shift
485                     state = INIT;
486                     break;
487                 default:
488                     if (code < 0x80)
489                         matrix_make(cs1_e0code(code));
490                     else
491                         matrix_break(cs1_e0code(code & 0x7F));
492                     state = INIT;
493                     break;
494             }
495             break;
496         case E1:
497             switch (code) {
498                 case 0x1D:
499                     state = E1_1D;
500                     break;
501                 case 0x9D:
502                     state = E1_9D;
503                     break;
504                 default:
505                     state = INIT;
506                     break;
507             }
508             break;
509         case E1_1D:
510             switch (code) {
511                 case 0x45:
512                     matrix_make(0x55);
513                     break;
514                 default:
515                     state = INIT;
516                     break;
517             }
518             break;
519         case E1_9D:
520             switch (code) {
521                 case 0x45:
522                     matrix_break(0x55);
523                     break;
524                 default:
525                     state = INIT;
526                     break;
527             }
528             break;
529         default:
530             state = INIT;
531     }
532     return 0;
533 }
534
535
536 /*******************************************************************************
537  * AT, PS/2: Scan Code Set 2
538  *
539  * Exceptional Handling
540  * --------------------
541  * Some keys should be handled exceptionally. See [b].
542  *
543  * Scan codes are varied or prefix/postfix'd depending on modifier key state.
544  *
545  * 1) Insert, Delete, Home, End, PageUp, PageDown, Up, Down, Right, Left
546  *     a) when Num Lock is off
547  *     modifiers | make                      | break
548  *     ----------+---------------------------+----------------------
549  *     Ohter     |                    <make> | <break>
550  *     LShift    | E0 F0 12           <make> | <break>  E0 12
551  *     RShift    | E0 F0 59           <make> | <break>  E0 59
552  *     L+RShift  | E0 F0 12  E0 F0 59 <make> | <break>  E0 59 E0 12
553  *
554  *     b) when Num Lock is on
555  *     modifiers | make                      | break
556  *     ----------+---------------------------+----------------------
557  *     Other     | E0 12              <make> | <break>  E0 F0 12
558  *     Shift'd   |                    <make> | <break>
559  *
560  *     Handling: These prefix/postfix codes are ignored.
561  *
562  *
563  * 2) Keypad /
564  *     modifiers | make                      | break
565  *     ----------+---------------------------+----------------------
566  *     Ohter     |                    <make> | <break>
567  *     LShift    | E0 F0 12           <make> | <break>  E0 12
568  *     RShift    | E0 F0 59           <make> | <break>  E0 59
569  *     L+RShift  | E0 F0 12  E0 F0 59 <make> | <break>  E0 59 E0 12
570  *
571  *     Handling: These prefix/postfix codes are ignored.
572  *
573  *
574  * 3) PrintScreen
575  *     modifiers | make         | break
576  *     ----------+--------------+-----------------------------------
577  *     Other     | E0 12  E0 7C | E0 F0 7C  E0 F0 12
578  *     Shift'd   |        E0 7C | E0 F0 7C
579  *     Control'd |        E0 7C | E0 F0 7C
580  *     Alt'd     |           84 | F0 84
581  *
582  *     Handling: These prefix/postfix codes are ignored, and both scan codes
583  *               'E0 7C' and 84 are seen as PrintScreen.
584  *
585  * 4) Pause
586  *     modifiers | make(no break code)
587  *     ----------+--------------------------------------------------
588  *     Other     | E1 14 77 E1 F0 14 F0 77
589  *     Control'd | E0 7E E0 F0 7E
590  *
591  *     Handling: Both code sequences are treated as a whole.
592  *               And we need a ad hoc 'pseudo break code' hack to get the key off
593  *               because it has no break code.
594  *
595  * Notes:
596  * 'Hanguel/English'(F1) and 'Hanja'(F2) have no break code. See [a].
597  * These two Korean keys need exceptional handling and are not supported for now.
598  *
599  */
600 static uint8_t cs2_e0code(uint8_t code) {
601     switch(code) {
602         // E0 prefixed codes translation See [a].
603         case 0x11: return 0x0F; // right alt
604         case 0x14: return 0x17; // right control
605         case 0x1F: return 0x19; // left GUI
606         case 0x27: return 0x1F; // right GUI
607         case 0x2F: return 0x5C; // apps
608         case 0x4A: return 0x60; // keypad /
609         case 0x5A: return 0x62; // keypad enter
610         case 0x69: return 0x27; // end
611         case 0x6B: return 0x53; // cursor left
612         case 0x6C: return 0x2F; // home
613         case 0x70: return 0x39; // insert
614         case 0x71: return 0x37; // delete
615         case 0x72: return 0x3F; // cursor down
616         case 0x74: return 0x47; // cursor right
617         case 0x75: return 0x4F; // cursor up
618         case 0x7A: return 0x56; // page down
619         case 0x7D: return 0x5E; // page up
620         case 0x7C: return 0x6F; // Print Screen
621         case 0x7E: return 0x00; // Control'd Pause
622
623         case 0x21: return 0x65; // volume down
624         case 0x32: return 0x6E; // volume up
625         case 0x23: return 0x7F; // mute
626         case 0x10: return 0x08; // (WWW search)     -> F13
627         case 0x18: return 0x10; // (WWW favourites) -> F14
628         case 0x20: return 0x18; // (WWW refresh)    -> F15
629         case 0x28: return 0x20; // (WWW stop)       -> F16
630         case 0x30: return 0x28; // (WWW forward)    -> F17
631         case 0x38: return 0x30; // (WWW back)       -> F18
632         case 0x3A: return 0x38; // (WWW home)       -> F19
633         case 0x40: return 0x40; // (my computer)    -> F20
634         case 0x48: return 0x48; // (email)          -> F21
635         case 0x2B: return 0x50; // (calculator)     -> F22
636         case 0x34: return 0x08; // (play/pause)     -> F13
637         case 0x3B: return 0x10; // (stop)           -> F14
638         case 0x15: return 0x18; // (previous track) -> F15
639         case 0x4D: return 0x20; // (next track)     -> F16
640         case 0x50: return 0x28; // (media select)   -> F17
641         case 0x5E: return 0x50; // (ACPI wake)      -> F22
642         case 0x3F: return 0x57; // (ACPI sleep)     -> F23
643         case 0x37: return 0x5F; // (ACPI power)     -> F24
644
645         // https://github.com/tmk/tmk_keyboard/pull/636
646         case 0x03: return 0x18; // Help        DEC LK411 -> F15
647         case 0x04: return 0x08; // F13         DEC LK411
648         case 0x0B: return 0x20; // Do          DEC LK411 -> F16
649         case 0x0C: return 0x10; // F14         DEC LK411
650         case 0x0D: return 0x19; // LCompose    DEC LK411 -> LGUI
651         case 0x79: return 0x6D; // KP-         DEC LK411 -> PCMM
652         case 0x83: return 0x28; // F17         DEC LK411
653         default: return (code & 0x7F);
654     }
655 }
656
657 static int8_t process_cs2(void)
658 {
659     // scan code reading states
660     static enum {
661         INIT,
662         F0,
663         E0,
664         E0_F0,
665         // Pause
666         E1,
667         E1_14,
668         E1_F0,
669         E1_F0_14,
670         E1_F0_14_F0,
671     } state = INIT;
672
673     uint16_t code = ibmpc_host_recv();
674     if (code == -1) {
675         return 0;
676     }
677
678     switch (state) {
679         case INIT:
680             switch (code) {
681                 case 0x00:  // Error/Overrun [3]p.26
682                     xprintf("!CS2_OVR!\n");
683                     matrix_clear();
684                     clear_keyboard();
685                     break;
686                 case 0xFF:
687                     matrix_clear();
688                     xprintf("!CS2_ERR!\n");
689                     state = INIT;
690                     return -1;
691                     break;
692                 case 0xE0:
693                     state = E0;
694                     break;
695                 case 0xF0:
696                     state = F0;
697                     break;
698                 case 0xE1:
699                     state = E1;
700                     break;
701                 case 0x83:  // F7
702                     matrix_make(0x02);
703                     state = INIT;
704                     break;
705                 case 0x84:  // Alt'd PrintScreen
706                     matrix_make(0x6F);
707                     state = INIT;
708                     break;
709                 case 0xAA:  // Self-test passed
710                 case 0xFC:  // Self-test failed
711                     // reset or plugin-in new keyboard
712                     state = INIT;
713                     return -1;
714                     break;
715                 default:    // normal key make
716                     state = INIT;
717                     if (code < 0x80) {
718                         matrix_make(code);
719                     } else {
720                         matrix_clear();
721                         xprintf("!CS2_INIT!\n");
722                         return -1;
723                     }
724             }
725             break;
726         case E0:    // E0-Prefixed
727             switch (code) {
728                 case 0x12:  // to be ignored
729                 case 0x59:  // to be ignored
730                     state = INIT;
731                     break;
732                 case 0xF0:
733                     state = E0_F0;
734                     break;
735                 default:
736                     state = INIT;
737                     if (code < 0x80) {
738                         matrix_make(cs2_e0code(code));
739                     } else {
740                         matrix_clear();
741                         xprintf("!CS2_E0!\n");
742                         return -1;
743                     }
744             }
745             break;
746         case F0:    // Break code
747             switch (code) {
748                 case 0x83:  // F7
749                     matrix_break(0x02);
750                     state = INIT;
751                     break;
752                 case 0x84:  // Alt'd PrintScreen
753                     matrix_break(0x6F);
754                     state = INIT;
755                     break;
756                 default:
757                     state = INIT;
758                     if (code < 0x80) {
759                         matrix_break(code);
760                     } else {
761                         matrix_clear();
762                         xprintf("!CS2_F0!\n");
763                         return -1;
764                     }
765             }
766             break;
767         case E0_F0: // Break code of E0-prefixed
768             switch (code) {
769                 case 0x12:  // to be ignored
770                 case 0x59:  // to be ignored
771                     state = INIT;
772                     break;
773                 default:
774                     state = INIT;
775                     if (code < 0x80) {
776                         matrix_break(cs2_e0code(code));
777                     } else {
778                         matrix_clear();
779                         xprintf("!CS2_E0_F0!\n");
780                         return -1;
781                     }
782             }
783             break;
784         // Pause make: E1 14 77
785         case E1:
786             switch (code) {
787                 case 0x14:
788                     state = E1_14;
789                     break;
790                 case 0xF0:
791                     state = E1_F0;
792                     break;
793                 default:
794                     state = INIT;
795             }
796             break;
797         case E1_14:
798             switch (code) {
799                 case 0x77:
800                     matrix_make(0x00);
801                     state = INIT;
802                     break;
803                 default:
804                     state = INIT;
805             }
806             break;
807         // Pause break: E1 F0 14 F0 77
808         case E1_F0:
809             switch (code) {
810                 case 0x14:
811                     state = E1_F0_14;
812                     break;
813                 default:
814                     state = INIT;
815             }
816             break;
817         case E1_F0_14:
818             switch (code) {
819                 case 0xF0:
820                     state = E1_F0_14_F0;
821                     break;
822                 default:
823                     state = INIT;
824             }
825             break;
826         case E1_F0_14_F0:
827             switch (code) {
828                 case 0x77:
829                     matrix_break(0x00);
830                     state = INIT;
831                     break;
832                 default:
833                     state = INIT;
834             }
835             break;
836         default:
837             state = INIT;
838     }
839     return 0;
840 }
841
842 /*
843  * Terminal: Scan Code Set 3
844  *
845  * See [3], [7]
846  *
847  * Scan code 0x83 and 0x84 are handled exceptioanally to fit into 1-byte range index.
848  */
849 static int8_t process_cs3(void)
850 {
851     static enum {
852         READY,
853         F0,
854 #ifdef G80_2551_SUPPORT
855         // G80-2551 four extra keys around cursor keys
856         G80,
857         G80_F0,
858 #endif
859     } state = READY;
860
861     uint16_t code = ibmpc_host_recv();
862     if (code == -1) {
863         return 0;
864     }
865
866     switch (state) {
867         case READY:
868             switch (code) {
869                 case 0x00:  // Error/Overrun [3]p.26
870                     xprintf("!CS3_OVR!\n");
871                     matrix_clear();
872                     clear_keyboard();
873                     break;
874                 case 0xFF:
875                     xprintf("!CS3_ERR!\n");
876                     return -1;
877                     break;
878                 case 0xF0:
879                     state = F0;
880                     break;
881                 case 0x83:  // PrintScreen
882                     matrix_make(0x02);
883                     break;
884                 case 0x84:  // Keypad *
885                     matrix_make(0x7F);
886                     break;
887                 case 0x85:  // Muhenkan
888                     matrix_make(0x0B);
889                     break;
890                 case 0x86:  // Henkan
891                     matrix_make(0x06);
892                     break;
893                 case 0x87:  // Hiragana
894                     matrix_make(0x00);
895                     break;
896                 case 0x8B:  // Left GUI
897                     matrix_make(0x01);
898                     break;
899                 case 0x8C:  // Right GUI
900                     matrix_make(0x09);
901                     break;
902                 case 0x8D:  // Application
903                     matrix_make(0x0A);
904                     break;
905 #ifdef G80_2551_SUPPORT
906                 case 0x80:  // G80-2551 four extra keys around cursor keys
907                     state = G80;
908                     break;
909 #endif
910                 default:    // normal key make
911                     if (code < 0x80) {
912                         matrix_make(code);
913                     } else {
914                         xprintf("!CS3_READY!\n");
915                         //return -1;
916                     }
917             }
918             break;
919         case F0:    // Break code
920             switch (code) {
921                 case 0x00:
922                     xprintf("!CS3_F0_OVR!\n");
923                     matrix_clear();
924                     clear_keyboard();
925                     state = READY;
926                     break;
927                 case 0xFF:
928                     xprintf("!CS3_F0_ERR!\n");
929                     state = READY;
930                     return -1;
931                     break;
932                 case 0x83:  // PrintScreen
933                     matrix_break(0x02);
934                     state = READY;
935                     break;
936                 case 0x84:  // Keypad *
937                     matrix_break(0x7F);
938                     state = READY;
939                     break;
940                 case 0x85:  // Muhenkan
941                     matrix_break(0x0B);
942                     state = READY;
943                     break;
944                 case 0x86:  // Henkan
945                     matrix_break(0x06);
946                     state = READY;
947                     break;
948                 case 0x87:  // Hiragana
949                     matrix_break(0x00);
950                     state = READY;
951                     break;
952                 case 0x8B:  // Left GUI
953                     matrix_break(0x01);
954                     state = READY;
955                     break;
956                 case 0x8C:  // Right GUI
957                     matrix_break(0x09);
958                     state = READY;
959                     break;
960                 case 0x8D:  // Application
961                     matrix_break(0x0A);
962                     state = READY;
963                     break;
964                 default:
965                     state = READY;
966                     if (code < 0x80) {
967                         matrix_break(code);
968                     } else {
969                         xprintf("!CS3_F0!\n");
970                         //return -1;
971                     }
972             }
973             break;
974 #ifdef G80_2551_SUPPORT
975         /*
976          * G80-2551 terminal keyboard support
977          * https://deskthority.net/wiki/Cherry_G80-2551
978          * https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#g80-2551-in-code-set-3
979          */
980         case G80:   // G80-2551 four extra keys around cursor keys
981             switch (code) {
982                 case (0x26):    // TD= -> JYEN
983                     matrix_make(0x5D);
984                     break;
985                 case (0x25):    // page with edge -> NUHS
986                     matrix_make(0x53);
987                     break;
988                 case (0x16):    // two pages -> RO
989                     matrix_make(0x51);
990                     break;
991                 case (0x1E):    // calc -> KANA
992                     matrix_make(0x00);
993                     break;
994                 case (0xF0):
995                     state = G80_F0;
996                     return 0;
997                 default:
998                     // Not supported
999                     matrix_clear();
1000                     break;
1001             }
1002             state = READY;
1003             break;
1004         case G80_F0:
1005             switch (code) {
1006                 case (0x26):    // TD= -> JYEN
1007                     matrix_break(0x5D);
1008                     break;
1009                 case (0x25):    // page with edge -> NUHS
1010                     matrix_break(0x53);
1011                     break;
1012                 case (0x16):    // two pages -> RO
1013                     matrix_break(0x51);
1014                     break;
1015                 case (0x1E):    // calc -> KANA
1016                     matrix_break(0x00);
1017                     break;
1018                 default:
1019                     // Not supported
1020                     matrix_clear();
1021                     break;
1022             }
1023             state = READY;
1024             break;
1025 #endif
1026     }
1027     return 0;
1028 }
1029
1030 /*
1031  * IBM PC Keyboard Protocol Resources:
1032  *
1033  * [a] Microsoft USB HID to PS/2 Translation Table - Scan Code Set 1 and 2
1034  * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/translate.pdf
1035  *
1036  * [b] Microsoft Keyboard Scan Code Specification - Special rules of Scan Code Set 1 and 2
1037  * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
1038  *
1039  * [1] PS/2 Reference Manuals - Collection of IBM Personal System/2 documents.
1040  * http://www.mcamafia.de/pdf/pdfref.htm
1041  *
1042  * [2] Keyboard and Auxiliary Device Controller - Signal Timing and Format
1043  * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
1044  *
1045  * [3] Keyboards(101- and 102-key) - Keyboard Layout, Scan Code Set, POR, and Commands.
1046  * http://www.mcamafia.de/pdf/ibm_hitrc11.pdf
1047  *
1048  * [4] IBM PC XT Keyboard Protocol
1049  * https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol
1050  *
1051  * [5] IBM Keyboard Scan Code by John Elliott - 83-key, 84-key, 102-key and 122-key
1052  * https://www.seasip.info/VintagePC/index.html
1053  *
1054  * [6] IBM 1391406 Keyboard - Scan Code Set 2 of 102-key PS/2 keyboard
1055  * https://www.seasip.info/VintagePC/ibm_1391406.html
1056  *
1057  * [7] The IBM 6110344 Keyboard - Scan Code Set 3 of 122-key terminal keyboard
1058  * https://www.seasip.info/VintagePC/ibm_6110344.html
1059  *
1060  * [y] TrackPoint Engineering Specifications for version 3E
1061  * https://web.archive.org/web/20100526161812/http://wwwcssrv.almaden.ibm.com/trackpoint/download.html
1062  *
1063  * [z] [Soarer's XT/AT/PS2/Terminal to USB converter]
1064  * https://geekhack.org/index.php?topic=17458.0
1065  *
1066  */