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