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