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

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