X-Git-Url: https://git.friedersdorff.com/?a=blobdiff_plain;ds=sidebyside;f=converter%2Fibmpc_usb%2Fibmpc_usb.c;h=cf1ab0b747bf5ffb1e519722a2c10ffba6988043;hb=c6a6049bc85609b05a6502dbbed524d669b2b7a5;hp=4267c21128bfb6e63d66c66e47e3292dd96a662c;hpb=aa276d55c4c9b40300bfa82b74dd9f5d5edb07da;p=max%2Ftmk_keyboard.git
diff --git a/converter/ibmpc_usb/ibmpc_usb.c b/converter/ibmpc_usb/ibmpc_usb.c
index 4267c211..cf1ab0b7 100644
--- a/converter/ibmpc_usb/ibmpc_usb.c
+++ b/converter/ibmpc_usb/ibmpc_usb.c
@@ -33,9 +33,9 @@ along with this program. If not, see .
static void matrix_make(uint8_t code);
static void matrix_break(uint8_t code);
-static int8_t process_cs1(void);
-static int8_t process_cs2(void);
-static int8_t process_cs3(void);
+static int8_t process_cs1(uint8_t code);
+static int8_t process_cs2(uint8_t code);
+static int8_t process_cs3(uint8_t code);
static uint8_t matrix[MATRIX_ROWS];
@@ -105,6 +105,7 @@ void matrix_init(void)
* d. ID is BF BF: Terminal keyboard CodeSet3
* e. error on recv: maybe broken PS/2
*/
+uint8_t current_protocol = 0;
uint16_t keyboard_id = 0x0000;
keyboard_kind_t keyboard_kind = NONE;
uint8_t matrix_scan(void)
@@ -112,6 +113,8 @@ uint8_t matrix_scan(void)
// scan code reading states
static enum {
INIT,
+ WAIT_SETTLE,
+ AT_RESET,
XT_RESET,
XT_RESET_WAIT,
XT_RESET_DONE,
@@ -126,23 +129,45 @@ uint8_t matrix_scan(void)
if (ibmpc_error) {
- xprintf("\nERR:%02X\n", ibmpc_error);
+ xprintf("\nERR:%02X ISR:%04X ", ibmpc_error, ibmpc_isr_debug);
// when recv error, neither send error nor buffer full
if (!(ibmpc_error & (IBMPC_ERR_SEND | IBMPC_ERR_FULL))) {
// keyboard init again
if (state == LOOP) {
- xprintf("init\n");
+ xprintf("[RST] ");
state = INIT;
}
}
// clear or process error
ibmpc_error = IBMPC_ERR_NONE;
+ ibmpc_isr_debug = 0;
+ }
+
+ // check protocol change AT/XT
+ if (ibmpc_protocol && ibmpc_protocol != current_protocol) {
+ xprintf("\nPRT:%02X ISR:%04X ", ibmpc_protocol, ibmpc_isr_debug);
+
+ // protocol change between AT and XT indicates that
+ // keyboard is hotswapped or something goes wrong.
+ // This requires initializing keyboard again probably.
+ if (((current_protocol&IBMPC_PROTOCOL_XT) && (ibmpc_protocol&IBMPC_PROTOCOL_AT)) ||
+ ((current_protocol&IBMPC_PROTOCOL_AT) && (ibmpc_protocol&IBMPC_PROTOCOL_XT))) {
+ if (state == LOOP) {
+ xprintf("[CHG] ");
+ state = INIT;
+ }
+ }
+
+ current_protocol = ibmpc_protocol;
+ ibmpc_isr_debug = 0;
}
switch (state) {
case INIT:
+ ibmpc_host_disable();
+
xprintf("I%u ", timer_read());
keyboard_kind = NONE;
keyboard_id = 0x0000;
@@ -150,7 +175,31 @@ uint8_t matrix_scan(void)
matrix_clear();
clear_keyboard();
- state = XT_RESET;
+ init_time = timer_read();
+ state = WAIT_SETTLE;
+ break;
+ case WAIT_SETTLE:
+ // wait for keyboard to settle after plugin
+ if (timer_elapsed(init_time) > 1000) {
+ state = AT_RESET;
+ }
+ break;
+ case AT_RESET:
+ ibmpc_host_isr_clear();
+ ibmpc_host_enable();
+ wait_ms(1); // keyboard can't respond to command without this
+
+ // SKIDATA-2-DE(and some other keyboards?) stores 'Code Set' setting in nonvlatile memory
+ // and keeps it until receiving reset. Sending reset here may be useful to clear it, perhaps.
+ // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#select-alternate-scan-codesf0
+
+ // reset command
+ if (0xFA == ibmpc_host_send(0xFF)) {
+ state = WAIT_AA;
+ } else {
+ state = XT_RESET;
+ }
+ xprintf("A%u ", timer_read());
break;
case XT_RESET:
// Reset XT-initialize keyboard
@@ -217,19 +266,8 @@ uint8_t matrix_scan(void)
}
break;
case READ_ID:
- xprintf("R%u ", timer_read());
-
- // SKIDATA-2-DE(and some other keyboards?) stores 'Code Set' setting in nonvlatile memory
- // and keeps it until receiving reset. Sending reset here may be useful to clear it, perhaps.
- // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#select-alternate-scan-codesf0
- //ibmpc_host_send(0xFF); // reset command
- //read_wait(500); // BAT takes 600-900ms(84-key) or 300-500ms(101/102-key) [8] 4-7, 4-39
-
keyboard_id = read_keyboard_id();
- if (ibmpc_error) {
- xprintf("\nERR:%02X\n", ibmpc_error);
- ibmpc_error = IBMPC_ERR_NONE;
- }
+ xprintf("R%u ", timer_read());
if (0x0000 == keyboard_id) { // CodeSet2 AT(IBM PC AT 84-key)
keyboard_kind = PC_AT;
@@ -241,9 +279,11 @@ uint8_t matrix_scan(void)
xprintf("Mouse: not supported\n");
keyboard_kind = NONE;
#ifdef G80_2551_SUPPORT
- } else if (0xAB86 == keyboard_id) { // CodeSet2 PS/2 Terminal
- // For G80-2551 and other 122-key terminal keyboards
+ } else if (0xAB86 == keyboard_id ||
+ 0xAB85 == keyboard_id) { // For G80-2551 and other 122-key terminal
// https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#ab86
+ // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#ab85
+
if ((0xFA == ibmpc_host_send(0xF0)) &&
(0xFA == ibmpc_host_send(0x03))) {
// switch to code set 3
@@ -252,6 +292,11 @@ uint8_t matrix_scan(void)
keyboard_kind = PC_AT;
}
#endif
+ } else if (0xBFB0 == keyboard_id) { // IBM RT Keyboard
+ // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#bfb0
+ // TODO: LED indicator fix
+ //keyboard_kind = PC_TERMINAL_IBM_RT;
+ keyboard_kind = PC_TERMINAL;
} else if (0xAB00 == (keyboard_id & 0xFF00)) { // CodeSet2 PS/2
keyboard_kind = PC_AT;
} else if (0xBF00 == (keyboard_id & 0xFF00)) { // CodeSet3 Terminal
@@ -260,7 +305,7 @@ uint8_t matrix_scan(void)
keyboard_kind = PC_AT;
}
- xprintf("ID:%04X(%d)\n", keyboard_id, keyboard_kind);
+ xprintf("\nID:%04X(%d) ", keyboard_id, keyboard_kind);
state = SETUP;
break;
@@ -275,6 +320,8 @@ uint8_t matrix_scan(void)
case PC_TERMINAL:
// Set all keys to make/break type
ibmpc_host_send(0xF8);
+ // This should not be harmful
+ led_set(host_keyboard_leds());
break;
default:
break;
@@ -282,18 +329,39 @@ uint8_t matrix_scan(void)
state = LOOP;
xprintf("L%u ", timer_read());
case LOOP:
- switch (keyboard_kind) {
- case PC_XT:
- if (process_cs1() == -1) state = INIT;
- break;
- case PC_AT:
- if (process_cs2() == -1) state = INIT;
+ {
+ uint16_t code = ibmpc_host_recv();
+ if (code == -1) {
+ // no code
break;
- case PC_TERMINAL:
- if (process_cs3() == -1) state = INIT;
- break;
- default:
+ }
+
+ // Keyboard Error/Overrun([3]p.26) or Buffer full
+ // Scan Code Set 1: 0xFF
+ // Scan Code Set 2 and 3: 0x00
+ // Buffer full(IBMPC_ERR_FULL): 0xFF
+ if (code == 0x00 || code == 0xFF) {
+ // clear stuck keys
+ matrix_clear();
+ clear_keyboard();
+
+ xprintf("\n[OVR] ");
break;
+ }
+
+ switch (keyboard_kind) {
+ case PC_XT:
+ if (process_cs1(code) == -1) state = INIT;
+ break;
+ case PC_AT:
+ if (process_cs2(code) == -1) state = INIT;
+ break;
+ case PC_TERMINAL:
+ if (process_cs3(code) == -1) state = INIT;
+ break;
+ default:
+ break;
+ }
}
break;
default:
@@ -347,8 +415,19 @@ void matrix_clear(void)
void led_set(uint8_t usb_led)
{
- //if (keyboard_kind != PC_AT) return;
+ // Sending before keyboard recognition may be harmful for XT keyboard
+ if (keyboard_kind == NONE) return;
+
+ // XT keyobard doesn't support any command and it is harmful perhaps
+ // https://github.com/tmk/tmk_keyboard/issues/635#issuecomment-626993437
+ if (keyboard_kind == PC_XT) return;
+
+ // It should be safe to send the command to keyboards with AT protocol
+ // - IBM Terminal doesn't support the command and response with 0xFE but it is not harmful.
+ // - Some other Terminals like G80-2551 supports the command.
+ // https://geekhack.org/index.php?topic=103648.msg2894921#msg2894921
+ // TODO: PC_TERMINAL_IBM_RT support
uint8_t ibmpc_led = 0;
if (usb_led & (1<= 0x62) {
- xprintf("!CS1_INV!\n");
- state = INIT;
- return -1;
- }
-
switch (state) {
case INIT:
switch (code) {
- case 0x00:
- case 0xFF: // Error/Overrun [3]p.26
- xprintf("!CS1_ERR!\n");
- return -1;
- break;
case 0xE0:
state = E0;
break;
@@ -625,12 +686,12 @@ static uint8_t cs2_e0code(uint8_t code) {
case 0x75: return 0x4F; // cursor up
case 0x7A: return 0x56; // page down
case 0x7D: return 0x5E; // page up
- case 0x7C: return 0x6F; // Print Screen
+ case 0x7C: return 0x7F; // Print Screen
case 0x7E: return 0x00; // Control'd Pause
case 0x21: return 0x65; // volume down
case 0x32: return 0x6E; // volume up
- case 0x23: return 0x7F; // mute
+ case 0x23: return 0x6F; // mute
case 0x10: return 0x08; // (WWW search) -> F13
case 0x18: return 0x10; // (WWW favourites) -> F14
case 0x20: return 0x18; // (WWW refresh) -> F15
@@ -662,7 +723,7 @@ static uint8_t cs2_e0code(uint8_t code) {
}
}
-static int8_t process_cs2(void)
+static int8_t process_cs2(uint8_t code)
{
// scan code reading states
static enum {
@@ -678,25 +739,9 @@ static int8_t process_cs2(void)
E1_F0_14_F0,
} state = INIT;
- uint16_t code = ibmpc_host_recv();
- if (code == -1) {
- return 0;
- }
-
switch (state) {
case INIT:
switch (code) {
- case 0x00: // Error/Overrun [3]p.26
- xprintf("!CS2_OVR!\n");
- matrix_clear();
- clear_keyboard();
- break;
- case 0xFF:
- matrix_clear();
- xprintf("!CS2_ERR!\n");
- state = INIT;
- return -1;
- break;
case 0xE0:
state = E0;
break;
@@ -711,15 +756,12 @@ static int8_t process_cs2(void)
state = INIT;
break;
case 0x84: // Alt'd PrintScreen
- matrix_make(0x6F);
+ matrix_make(0x7F);
state = INIT;
break;
case 0xAA: // Self-test passed
case 0xFC: // Self-test failed
- // reset or plugin-in new keyboard
- state = INIT;
- return -1;
- break;
+ // replug or unstable connection probably
default: // normal key make
state = INIT;
if (code < 0x80) {
@@ -758,7 +800,7 @@ static int8_t process_cs2(void)
state = INIT;
break;
case 0x84: // Alt'd PrintScreen
- matrix_break(0x6F);
+ matrix_break(0x7F);
state = INIT;
break;
default:
@@ -850,11 +892,10 @@ static int8_t process_cs2(void)
/*
* Terminal: Scan Code Set 3
*
- * See [3], [7]
- *
- * Scan code 0x83 and 0x84 are handled exceptioanally to fit into 1-byte range index.
+ * See [3], [7] and
+ * https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#scan-code-set-3
*/
-static int8_t process_cs3(void)
+static int8_t process_cs3(uint8_t code)
{
static enum {
READY,
@@ -866,23 +907,9 @@ static int8_t process_cs3(void)
#endif
} state = READY;
- uint16_t code = ibmpc_host_recv();
- if (code == -1) {
- return 0;
- }
-
switch (state) {
case READY:
switch (code) {
- case 0x00: // Error/Overrun [3]p.26
- xprintf("!CS3_OVR!\n");
- matrix_clear();
- clear_keyboard();
- break;
- case 0xFF:
- xprintf("!CS3_ERR!\n");
- return -1;
- break;
case 0xF0:
state = F0;
break;
@@ -920,23 +947,12 @@ static int8_t process_cs3(void)
matrix_make(code);
} else {
xprintf("!CS3_READY!\n");
- //return -1;
+ return -1;
}
}
break;
case F0: // Break code
switch (code) {
- case 0x00:
- xprintf("!CS3_F0_OVR!\n");
- matrix_clear();
- clear_keyboard();
- state = READY;
- break;
- case 0xFF:
- xprintf("!CS3_F0_ERR!\n");
- state = READY;
- return -1;
- break;
case 0x83: // PrintScreen
matrix_break(0x02);
state = READY;
@@ -975,7 +991,7 @@ static int8_t process_cs3(void)
matrix_break(code);
} else {
xprintf("!CS3_F0!\n");
- //return -1;
+ return -1;
}
}
break;