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