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