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