]> git.friedersdorff.com Git - max/tmk_keyboard.git/blobdiff - converter/ibmpc_usb/ibmpc_usb.c
ibmpc_usb: Add support for G80-2551
[max/tmk_keyboard.git] / converter / ibmpc_usb / ibmpc_usb.c
index 6deb1a7e195608f16eca7e5e5b156d95b53e370e..89afa081e260a6427987b419e0013ff075789b6f 100644 (file)
@@ -1,5 +1,5 @@
 /*
-Copyright 2019 Jun Wako <wakojun@gmail.com>
+Copyright 2019,2020 Jun Wako <wakojun@gmail.com>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -63,11 +63,12 @@ static uint16_t read_keyboard_id(void)
     if (code == -1) { id = 0xFFFF; goto DONE; }     // XT or No keyboard
     if (code != 0xFA) { id = 0xFFFE; goto DONE; }   // Broken PS/2?
 
-    code = read_wait(1000);
+    // ID takes 500ms max TechRef [8] 4-41
+    code = read_wait(500);
     if (code == -1) { id = 0x0000; goto DONE; }     // AT
     id = (code & 0xFF)<<8;
 
-    code = read_wait(1000);
+    code = read_wait(500);
     id |= code & 0xFF;
 
 DONE:
@@ -77,10 +78,15 @@ DONE:
     return id;
 }
 
+void hook_early_init(void)
+{
+    ibmpc_host_init();
+    ibmpc_host_enable();
+}
+
 void matrix_init(void)
 {
     debug_enable = true;
-    ibmpc_host_init();
 
     // initialize matrix state: all keys off
     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
@@ -105,17 +111,21 @@ uint8_t matrix_scan(void)
     // scan code reading states
     static enum {
         INIT,
-        WAIT_STARTUP,
+        XT_RESET,
+        XT_RESET_WAIT,
+        XT_RESET_DONE,
+        WAIT_AA,
+        WAIT_AABF,
+        WAIT_AABFBF,
         READ_ID,
-        LED_SET,
+        SETUP,
         LOOP,
-        END
     } state = INIT;
     static uint16_t init_time;
 
 
     if (ibmpc_error) {
-        xprintf("\nERR: %02X\n", ibmpc_error);
+        xprintf("\nERR:%02X\n", ibmpc_error);
 
         // when recv error, neither send error nor buffer full
         if (!(ibmpc_error & (IBMPC_ERR_SEND | IBMPC_ERR_FULL))) {
@@ -124,10 +134,6 @@ uint8_t matrix_scan(void)
                 xprintf("init\n");
                 state = INIT;
             }
-            // probably XT signal, not AT
-            if (state == WAIT_STARTUP) {
-                state = READ_ID;
-            }
         }
 
         // clear or process error
@@ -136,104 +142,125 @@ uint8_t matrix_scan(void)
 
     switch (state) {
         case INIT:
-            ibmpc_protocol = IBMPC_PROTOCOL_AT;
+            xprintf("I%u ", timer_read());
             keyboard_kind = NONE;
             keyboard_id = 0x0000;
-            init_time = timer_read();
-            xprintf("I%u\n", init_time);
 
-            // re-initialize keyboard
+            matrix_clear();
+            clear_keyboard();
+
+            state = XT_RESET;
+            break;
+        case XT_RESET:
+            // Reset XT-initialize keyboard
             // XT: hard reset 500ms for IBM XT Type-1 keyboard and clones
             // XT: soft reset 20ms min(clock Lo)
             ibmpc_host_disable();   // soft reset: inihibit(clock Lo/Data Hi)
-            IBMPC_RESET();          // hard reset
-            ibmpc_host_enable();    // soft reset: idle(clock Hi/Data Hi)
+            IBMPC_RST_LO();         // hard reset: reset pin Lo
 
-            // TODO: should in while disabling interrupt?
-            // clear ISR state after protocol recognition
+            init_time = timer_read();
+            state = XT_RESET_WAIT;
+            break;
+        case XT_RESET_WAIT:
+            if (timer_elapsed(init_time) > 500) {
+                state = XT_RESET_DONE;
+            }
+            break;
+        case XT_RESET_DONE:
+            IBMPC_RST_HIZ();        // hard reset: reset pin HiZ
             ibmpc_host_isr_clear();
+            ibmpc_host_enable();    // soft reset: idle(clock Hi/Data Hi)
 
-            matrix_clear();
-            clear_keyboard();
-
-            state = WAIT_STARTUP;
+            xprintf("X%u ", timer_read());
+            init_time = timer_read();
+            state = WAIT_AA;
             break;
-        case WAIT_STARTUP:
-            // 1) Read and ignore BAT code and ID when power-up
+        case WAIT_AA:
+            // 1) Read BAT code and ID on keybaord power-up
             // For example, XT/AT sends 'AA' and Terminal sends 'AA BF BF' after BAT
             // AT 84-key: POR and BAT can take 900-9900ms according to AT TechRef [8] 4-7
             // AT 101/102-key: POR and BAT can take 450-2500ms according to AT TechRef [8] 4-39
-            // 2) Read and ignore key input by user when signal handling/protocol error occurs
+            // 2) Read key typed by user or anything after error on protocol or scan code
             // This can happen in case of keyboard hotswap, unstable hardware, signal integrity problem or bug
 
-            if (ibmpc_host_recv() != -1 || timer_elapsed(init_time) > 10000) {
-                // 500ms max wait for ID after AA TechRef [8] 4-41
-                // 122-key Terminal 6110345: 1ms wait is enough
-                wait_ms(100); ibmpc_host_recv();
-                wait_ms(100); ibmpc_host_recv();
+            /* wait until keyboard sends any code without 10000ms timeout
+            if (timer_elapsed(init_time) > 10000) {
+                state = READ_ID;
+            }
+            */
+            if (ibmpc_host_recv() != -1) {  // wait for AA
+                xprintf("W%u ", timer_read());
+                init_time = timer_read();
+                state = WAIT_AABF;
+            }
+            break;
+        case WAIT_AABF:
+            // NOTE: we can omit to wait BF BF
+            // ID takes 500ms max? TechRef [8] 4-41, though 1ms is enough for 122-key Terminal 6110345
+            if (timer_elapsed(init_time) > 500) {
+                state = READ_ID;
+            }
+            if (ibmpc_host_recv() != -1) {  // wait for BF
+                xprintf("W%u ", timer_read());
+                init_time = timer_read();
+                state = WAIT_AABFBF;
+            }
+            break;
+        case WAIT_AABFBF:
+            if (timer_elapsed(init_time) > 500) {
+                state = READ_ID;
+            }
+            if (ibmpc_host_recv() != -1) {  // wait for BF
+                xprintf("W%u ", timer_read());
                 state = READ_ID;
             }
-
-            // XT's 'AA' can not be handled correctly because protocol is configured as AT at this point.
-            // TODO: Check ISR timeout error for XT AA?
             break;
         case READ_ID:
-            xprintf("R%u\n", timer_read());
+            xprintf("R%u ", timer_read());
+
             keyboard_id = read_keyboard_id();
             if (ibmpc_error) {
-                xprintf("\nERR: %02X\n", ibmpc_error);
+                xprintf("\nERR:%02X\n", ibmpc_error);
                 ibmpc_error = IBMPC_ERR_NONE;
             }
-            xprintf("ID: %04X\n", keyboard_id);
-            if (0xAB00 == (keyboard_id & 0xFF00)) {
-                // CodeSet2 PS/2
+
+            if (0xAB00 == (keyboard_id & 0xFF00)) {         // CodeSet2 PS/2
                 keyboard_kind = PC_AT;
-            } else if (0xBF00 == (keyboard_id & 0xFF00)) {
-                // CodeSet3 Terminal
+            } else if (0xBF00 == (keyboard_id & 0xFF00)) {  // CodeSet3 Terminal
                 keyboard_kind = PC_TERMINAL;
-            } else if (0x0000 == keyboard_id) {
-                // CodeSet2 AT
+            } else if (0x0000 == keyboard_id) {             // CodeSet2 AT
                 keyboard_kind = PC_AT;
-            } else if (0xFFFF == keyboard_id) {
-                // CodeSet1 XT
+            } else if (0xFFFF == keyboard_id) {             // CodeSet1 XT
                 keyboard_kind = PC_XT;
-            } else if (0xFFFE == keyboard_id) {
-                // CodeSet2 PS/2 fails to response?
+            } else if (0xFFFE == keyboard_id) {             // CodeSet2 PS/2 fails to response?
                 keyboard_kind = PC_AT;
-            } else if (0x00FF == keyboard_id) {
-                // Mouse is not supported
+            } else if (0x00FF == keyboard_id) {             // Mouse is not supported
                 xprintf("Mouse: not supported\n");
                 keyboard_kind = NONE;
             } else {
                 keyboard_kind = PC_AT;
             }
 
-            // protocol
-            if (keyboard_kind == PC_XT) {
-                xprintf("kbd: XT\n");
-                ibmpc_protocol = IBMPC_PROTOCOL_XT;
-            } else if (keyboard_kind == PC_AT) {
-                xprintf("kbd: AT\n");
-                ibmpc_protocol = IBMPC_PROTOCOL_AT;
-            } else if (keyboard_kind == PC_TERMINAL) {
-                xprintf("kbd: Terminal\n");
-                ibmpc_protocol = IBMPC_PROTOCOL_AT;
-                // Set all keys - make/break [3]p.23
-                ibmpc_host_send(0xF8);
-            } else {
-                xprintf("kbd: Unknown\n");
-                ibmpc_protocol = IBMPC_PROTOCOL_AT;
-            }
+            xprintf("ID:%04X(%d)\n", keyboard_id, keyboard_kind);
 
-            // clear ISR state after protocol recognition
-            ibmpc_host_isr_clear();
-
-            state = LED_SET;
+            state = SETUP;
             break;
-        case LED_SET:
-            xprintf("L%u\n", timer_read());
-            led_set(host_keyboard_leds());
+        case SETUP:
+            xprintf("S%u ", timer_read());
+            switch (keyboard_kind) {
+                case PC_XT:
+                    break;
+                case PC_AT:
+                    led_set(host_keyboard_leds());
+                    break;
+                case PC_TERMINAL:
+                    ibmpc_host_send(0xF8);
+                    break;
+                default:
+                    break;
+            }
             state = LOOP;
+            xprintf("L%u ", timer_read());
         case LOOP:
             switch (keyboard_kind) {
                 case PC_XT:
@@ -384,7 +411,7 @@ static uint8_t cs1_e0code(uint8_t code) {
         case 0x63: return 0x7B; // Wake  (MUHENKAN)
 
         default:
-           xprintf("!CS1_?!\n");
+           xprintf("!CS1_E0_%02X!\n", code);
            return code;
     }
     return 0x00;
@@ -395,7 +422,7 @@ static int8_t process_cs1(void)
     static enum {
         INIT,
         E0,
-        // Pause: E1 1D 45, E1 9D C5
+        // Pause: E1 1D 45, E1 9D C5 [a] (TODO: test)
         E1,
         E1_1D,
         E1_9D,
@@ -406,9 +433,22 @@ static int8_t process_cs1(void)
         return 0;
     }
 
+    // Check invalid codes; 0x59-7F won't be used in real XT keyboards probably
+    // 0x62 is used to handle escape code E0 and E1
+    if ((code & 0x7F) >= 0x62) {
+        xprintf("!CS1_INV!\n");
+        state = INIT;
+        return -1;
+    }
+
     switch (state) {
         case INIT:
             switch (code) {
+                case 0x00:
+                case 0xFF:  // Error/Overrun [3]p.26
+                    xprintf("!CS1_ERR!\n");
+                    return -1;
+                    break;
                 case 0xE0:
                     state = E0;
                     break;
@@ -616,6 +656,11 @@ static int8_t process_cs2(void)
         E1_F0,
         E1_F0_14,
         E1_F0_14_F0,
+#ifdef G80_2551_SUPPORT
+        // G80-2551 four extra keys around cursor keys
+        G80,
+        G80_F0,
+#endif
     } state = INIT;
 
     uint16_t code = ibmpc_host_recv();
@@ -626,6 +671,17 @@ static int8_t process_cs2(void)
     switch (state) {
         case INIT:
             switch (code) {
+                case 0x00:  // Error/Overrun [3]p.26
+                    xprintf("!CS2_OVR!\n");
+                    matrix_clear();
+                    clear_keyboard();
+                    break;
+                case 0xFF:
+                    matrix_clear();
+                    xprintf("!CS2_ERR!\n");
+                    state = INIT;
+                    return -1;
+                    break;
                 case 0xE0:
                     state = E0;
                     break;
@@ -643,25 +699,59 @@ static int8_t process_cs2(void)
                     matrix_make(0x6F);
                     state = INIT;
                     break;
-                case 0x00:  // Overrun [3]p.26
-                    matrix_clear();
-                    xprintf("!CS2_OVERRUN!\n");
-                    state = INIT;
-                    break;
                 case 0xAA:  // Self-test passed
                 case 0xFC:  // Self-test failed
                     // reset or plugin-in new keyboard
                     state = INIT;
                     return -1;
                     break;
+#ifdef G80_2551_SUPPORT
+                /*
+                 * G80-2551 terminal keyboard support
+                 */
+                case 0x80:  // G80-2551 four extra keys around cursor keys
+                    state = G80;
+                    break;
+                case 0x19:
+                    matrix_make(0x7F);  // MUTE
+                    break;
+                case 0x39:
+                    matrix_make(0x6E);  // VOLU
+                    break;
+                case 0x53:
+                    matrix_make(0x65);  // VOLD
+                    break;
+                case 0x6F:
+                    matrix_make(0x5C);  // APP
+                    break;
+                case 0x5C:
+                    matrix_make(0x19);  // LGUI
+                    break;
+                case 0x1F:
+                    matrix_make(0x1F);  // RGUI
+                    break;
+                case 0x27:
+                    matrix_make(0x67);  // MHEN
+                    break;
+                case 0x2F:
+                    matrix_make(0x57);  // F23
+                    break;
+                case 0x5E:
+                    matrix_make(0x64);  // HENK
+                    break;
+                case 0x17:
+                    matrix_make(0x77);  // NLCK
+                    break;
+#endif
                 default:    // normal key make
+                    state = INIT;
                     if (code < 0x80) {
                         matrix_make(code);
                     } else {
                         matrix_clear();
                         xprintf("!CS2_INIT!\n");
+                        return -1;
                     }
-                    state = INIT;
             }
             break;
         case E0:    // E0-Prefixed
@@ -674,13 +764,14 @@ static int8_t process_cs2(void)
                     state = E0_F0;
                     break;
                 default:
+                    state = INIT;
                     if (code < 0x80) {
                         matrix_make(cs2_e0code(code));
                     } else {
                         matrix_clear();
                         xprintf("!CS2_E0!\n");
+                        return -1;
                     }
-                    state = INIT;
             }
             break;
         case F0:    // Break code
@@ -693,14 +784,63 @@ static int8_t process_cs2(void)
                     matrix_break(0x6F);
                     state = INIT;
                     break;
+#ifdef G80_2551_SUPPORT
+                /*
+                 * G80-2551 terminal keyboard support
+                 * https://deskthority.net/wiki/Cherry_G80-2551
+                 * https://geekhack.org/index.php?topic=103648.msg2893404#msg2893404
+                 * https://gist.github.com/tmk/22cb8680ca8ef854630ecd1953268c5b
+                 */
+                case 0x19:
+                    matrix_break(0x7F);  // MUTE
+                    state = INIT;
+                    break;
+                case 0x39:
+                    matrix_break(0x6E);  // VOLU
+                    state = INIT;
+                    break;
+                case 0x53:
+                    matrix_break(0x65);  // VOLD
+                    state = INIT;
+                    break;
+                case 0x6F:
+                    matrix_break(0x5C);  // APP
+                    state = INIT;
+                    break;
+                case 0x5C:
+                    matrix_break(0x19);  // LGUI
+                    state = INIT;
+                    break;
+                case 0x1F:
+                    matrix_break(0x1F);  // RGUI
+                    state = INIT;
+                    break;
+                case 0x27:
+                    matrix_break(0x67);  // MHEN
+                    state = INIT;
+                    break;
+                case 0x2F:
+                    matrix_break(0x57);  // F23
+                    state = INIT;
+                    break;
+                case 0x5E:
+                    matrix_break(0x64);  // HENK
+                    state = INIT;
+                    break;
+                case 0x17:
+                    matrix_break(0x77);  // NLCK
+                    state = INIT;
+                    break;
+#endif
                 default:
+                    state = INIT;
                     if (code < 0x80) {
                         matrix_break(code);
                     } else {
                         matrix_clear();
                         xprintf("!CS2_F0!\n");
+                        return -1;
                     }
-                    state = INIT;
             }
             break;
         case E0_F0: // Break code of E0-prefixed
@@ -710,13 +850,14 @@ static int8_t process_cs2(void)
                     state = INIT;
                     break;
                 default:
+                    state = INIT;
                     if (code < 0x80) {
                         matrix_break(cs2_e0code(code));
                     } else {
                         matrix_clear();
                         xprintf("!CS2_E0_F0!\n");
+                        return -1;
                     }
-                    state = INIT;
             }
             break;
         // Pause make: E1 14 77
@@ -771,6 +912,53 @@ static int8_t process_cs2(void)
                     state = INIT;
             }
             break;
+#ifdef G80_2551_SUPPORT
+        case G80:   // G80-2551 four extra keys around cursor keys
+            switch (code) {
+                case (0x26):    // TD= -> JYEN
+                    matrix_make(0x6A);
+                    break;
+                case (0x25):    // page with edge -> NUHS
+                    matrix_make(0x68);
+                    break;
+                case (0x16):    // two pages -> RO
+                    matrix_make(0x51);
+                    break;
+                case (0x1E):    // calc -> KANA
+                    matrix_make(0x13);
+                    break;
+                case (0xF0):
+                    state = G80_F0;
+                    return 0;
+                default:
+                    // Not supported
+                    matrix_clear();
+                    break;
+            }
+            state = INIT;
+            break;
+        case G80_F0:
+            switch (code) {
+                case (0x26):    // TD= -> JYEN
+                    matrix_break(0x6A);
+                    break;
+                case (0x25):    // page with edge -> NUHS
+                    matrix_break(0x68);
+                    break;
+                case (0x16):    // two pages -> RO
+                    matrix_break(0x51);
+                    break;
+                case (0x1E):    // calc -> KANA
+                    matrix_break(0x13);
+                    break;
+                default:
+                    // Not supported
+                    matrix_clear();
+                    break;
+            }
+            state = INIT;
+            break;
+#endif
         default:
             state = INIT;
     }
@@ -799,25 +987,29 @@ static int8_t process_cs3(void)
     switch (state) {
         case READY:
             switch (code) {
-                case 0x00:
+                case 0x00:  // Error/Overrun [3]p.26
+                    xprintf("!CS3_OVR!\n");
+                    matrix_clear();
+                    clear_keyboard();
+                    break;
                 case 0xFF:
-                    xprintf("!CS3_%02X!\n", code);
+                    xprintf("!CS3_ERR!\n");
                     return -1;
                     break;
                 case 0xF0:
                     state = F0;
                     break;
-                case 0x83:  // F7
+                case 0x83:
                     matrix_make(0x02);
                     break;
-                case 0x84:  // keypad -
+                case 0x84:
                     matrix_make(0x7F);
                     break;
                 default:    // normal key make
                     if (code < 0x80) {
                         matrix_make(code);
                     } else {
-                        xprintf("!CS3_%02X!\n", code);
+                        xprintf("!CS3_READY!\n");
                         return -1;
                     }
             }
@@ -825,16 +1017,21 @@ static int8_t process_cs3(void)
         case F0:    // Break code
             switch (code) {
                 case 0x00:
+                    xprintf("!CS3_F0_OVR!\n");
+                    matrix_clear();
+                    clear_keyboard();
+                    state = READY;
+                    break;
                 case 0xFF:
-                    xprintf("!CS3_F0_%02X!\n", code);
+                    xprintf("!CS3_F0_ERR!\n");
                     state = READY;
                     return -1;
                     break;
-                case 0x83:  // F7
+                case 0x83:
                     matrix_break(0x02);
                     state = READY;
                     break;
-                case 0x84:  // keypad -
+                case 0x84:
                     matrix_break(0x7F);
                     state = READY;
                     break;
@@ -843,7 +1040,7 @@ static int8_t process_cs3(void)
                     if (code < 0x80) {
                         matrix_break(code);
                     } else {
-                        xprintf("!CS3_F0_%02X!\n", code);
+                        xprintf("!CS3_F0!\n");
                         return -1;
                     }
             }