Blame SOURCES/0318-at_keyboard-use-set-1-when-keyboard-is-in-Translate-.patch

dbe756
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
dbe756
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
dbe756
Date: Thu, 3 Dec 2020 09:13:24 +0100
dbe756
Subject: [PATCH] at_keyboard: use set 1 when keyboard is in Translate mode
dbe756
MIME-Version: 1.0
dbe756
Content-Type: text/plain; charset=UTF-8
dbe756
Content-Transfer-Encoding: 8bit
dbe756
dbe756
When keyboard controller acts in Translate mode (0x40 mask), then use
dbe756
set 1 since translation is done.
dbe756
Otherwise use the mode queried from the controller (usually set 2).
dbe756
dbe756
Added "atkeyb" debugging messages in at_keyboard module as well.
dbe756
dbe756
Resolves: rhbz#1897587
dbe756
dbe756
Tested on:
dbe756
- Asus N53SN (set 1 used)
dbe756
- Dell Precision (set 1 used)
dbe756
- HP Elitebook (set 2 used)
dbe756
- HP G5430 (set 1 used, keyboard in XT mode!)
dbe756
- Lenovo P71 & Lenovo T460s (set 2 used)
dbe756
- QEMU/KVM (set 1 used)
dbe756
dbe756
Signed-off-by: Renaud Métrich <rmetrich@redhat.com>
dbe756
---
dbe756
 grub-core/term/at_keyboard.c | 29 ++++++++++++++++++++++++-----
dbe756
 include/grub/at_keyboard.h   |  4 ++++
dbe756
 2 files changed, 28 insertions(+), 5 deletions(-)
dbe756
dbe756
diff --git a/grub-core/term/at_keyboard.c b/grub-core/term/at_keyboard.c
dbe756
index f0a986eb176..69d99b61df5 100644
dbe756
--- a/grub-core/term/at_keyboard.c
dbe756
+++ b/grub-core/term/at_keyboard.c
dbe756
@@ -135,20 +135,28 @@ query_mode (void)
dbe756
   int e;
dbe756
 
dbe756
   e = write_mode (0);
dbe756
-  if (!e)
dbe756
+  if (!e) {
dbe756
+    grub_dprintf("atkeyb", "query_mode: write_mode(0) failed\n");
dbe756
     return 0;
dbe756
+  }
dbe756
 
dbe756
   do {
dbe756
     keyboard_controller_wait_until_ready ();
dbe756
     ret = grub_inb (KEYBOARD_REG_DATA);
dbe756
   } while (ret == GRUB_AT_ACK);
dbe756
   /* QEMU translates the set even in no-translate mode.  */
dbe756
-  if (ret == 0x43 || ret == 1)
dbe756
+  if (ret == 0x43 || ret == 1) {
dbe756
+    grub_dprintf("atkeyb", "query_mode: returning 1 (ret=0x%x)\n", ret);
dbe756
     return 1;
dbe756
-  if (ret == 0x41 || ret == 2)
dbe756
+  }
dbe756
+  if (ret == 0x41 || ret == 2) {
dbe756
+    grub_dprintf("atkeyb", "query_mode: returning 2 (ret=0x%x)\n", ret);
dbe756
     return 2;
dbe756
-  if (ret == 0x3f || ret == 3)
dbe756
+  }
dbe756
+  if (ret == 0x3f || ret == 3) {
dbe756
+    grub_dprintf("atkeyb", "query_mode: returning 3 (ret=0x%x)\n", ret);
dbe756
     return 3;
dbe756
+  }
dbe756
   return 0;
dbe756
 }
dbe756
 
dbe756
@@ -165,7 +173,13 @@ set_scancodes (void)
dbe756
     }
dbe756
 
dbe756
 #if !USE_SCANCODE_SET
dbe756
-  ps2_state.current_set = 1;
dbe756
+  if ((grub_keyboard_controller_orig & KEYBOARD_AT_TRANSLATE) == KEYBOARD_AT_TRANSLATE) {
dbe756
+    grub_dprintf ("atkeyb", "queried set is %d but keyboard in Translate mode, so actually in set 1\n", grub_keyboard_orig_set);
dbe756
+    ps2_state.current_set = 1;
dbe756
+  } else {
dbe756
+    grub_dprintf ("atkeyb", "using queried set %d\n", grub_keyboard_orig_set);
dbe756
+    ps2_state.current_set = grub_keyboard_orig_set;
dbe756
+  }
dbe756
   return;
dbe756
 #else
dbe756
 
dbe756
@@ -266,6 +280,7 @@ grub_keyboard_controller_init (void)
dbe756
   grub_keyboard_orig_set = 2;
dbe756
 #else
dbe756
   grub_keyboard_controller_orig = grub_keyboard_controller_read ();
dbe756
+  grub_dprintf ("atkeyb", "grub_keyboard_controller_orig = 0x%x\n", grub_keyboard_controller_orig);
dbe756
   grub_keyboard_orig_set = query_mode ();
dbe756
 #endif
dbe756
   set_scancodes ();
dbe756
@@ -275,11 +290,15 @@ grub_keyboard_controller_init (void)
dbe756
 static grub_err_t
dbe756
 grub_keyboard_controller_fini (struct grub_term_input *term __attribute__ ((unused)))
dbe756
 {
dbe756
+/* In !USE_SCANCODE_SET mode, we didn't change anything, so nothing to restore */
dbe756
+#if USE_SCANCODE_SET
dbe756
   if (ps2_state.current_set == 0)
dbe756
     return GRUB_ERR_NONE;
dbe756
+  grub_dprintf ("atkeyb", "restoring set %d, controller 0x%x\n", grub_keyboard_orig_set, grub_keyboard_controller_orig);
dbe756
   if (grub_keyboard_orig_set)
dbe756
     write_mode (grub_keyboard_orig_set);
dbe756
   grub_keyboard_controller_write (grub_keyboard_controller_orig);
dbe756
+#endif
dbe756
   return GRUB_ERR_NONE;
dbe756
 }
dbe756
 
dbe756
diff --git a/include/grub/at_keyboard.h b/include/grub/at_keyboard.h
dbe756
index bcb4d9ba78f..9414dc1b996 100644
dbe756
--- a/include/grub/at_keyboard.h
dbe756
+++ b/include/grub/at_keyboard.h
dbe756
@@ -19,6 +19,10 @@
dbe756
 #ifndef GRUB_AT_KEYBOARD_HEADER
dbe756
 #define GRUB_AT_KEYBOARD_HEADER	1
dbe756
 
dbe756
+/*
dbe756
+ * Refer to https://wiki.osdev.org/%228042%22_PS/2_Controller for details.
dbe756
+ */
dbe756
+
dbe756
 /* Used for sending commands to the controller.  */
dbe756
 #define KEYBOARD_COMMAND_ISREADY(x)	!((x) & 0x02)
dbe756
 #define KEYBOARD_COMMAND_READ		0x20