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

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