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