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

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