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