Skip to contentSkip to conversation

tty: Fix the keyboard led light display problem

PATCH Superseded lianzhi chang 4 participants 5 messages Nov 29, 2021 – Nov 30, 2021 TTY layer and serial drivers

v18 of 22 Superseded Latest is v22 →

lore
Share & tools
lore
#0 lore raw

PATCH v18 1/1 tty: Fix the keyboard led light display problem

Use the "ctrl+alt+Fn" key combination to switch the system from tty to desktop or switch the system from desktop to tty. After the switch is completed, it is found that the state of the keyboard lock is inconsistent with the state of the keyboard Led light.The reasons are as follows:

* The desktop environment (Xorg and other services) is bound to a tty (assuming it is tty1), and the kb->kbdmode attribute value of tty1 will be set to VC_OFF. According to the current code logic, in the desktop environment, the values of ledstate and kb->ledflagstate of tty1 will not be modified anymore, so they are always 0.

* When switching between each tty, the final value of ledstate set by the previous tty is compared with the kb->ledflagstate value of the current tty to determine whether to set the state of the keyboard light. The process of switching between desktop and tty is also the process of switching between tty1 and other ttys. There are two situations:

- (1) In the desktop environment, tty1 will not set the ledstate, which will cause when switching from the desktop to other ttys, if the desktop lights up the keyboard's led, after the switch is completed, the keyboard's led light will always be on;

- (2) When switching from another tty to the desktop, this mechanism will trigger tty1 to set the led state. If other tty lights up the led of the keyboard before switching to the desktop, the led will be forcibly turned off. This situation should be avoided.

* The current patch is to solve these problems: by judging the kb->kbdmode value of the current tty, it is determined whether the led state is forced to be set when the VT switch is completed.

---
v18:
(1) Modify the description information.
(2) The judgment condition of kb->kbdmode should be "!=", correct it.
(3) Modify code indentation.

 drivers/tty/vt/keyboard.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/tty/vt/keyboard.c +19 -1
index c7fbbcdcc346..bcf8a858afd2 100644
@@ -153,6 +153,7 @@ static int shift_state = 0;
 
 static unsigned int ledstate = -1U;			/* undefined */
 static unsigned char ledioctl;
+static bool vt_switch;
 
 /*
  * Notifier list for console keyboard events
@@ -412,9 +413,21 @@ static void do_compute_shiftstate(void)
 /* We still have to export this method to vt.c */
 void vt_set_leds_compute_shiftstate(void)
 {
+	struct kbd_struct *kb;
 	unsigned long flags;
 
-	set_leds();
+	/*
+	 * When switching VT, according to the value of kb->kbdmode,
+	 * judge whether it is necessary to force the keyboard light
+	 * state to be issued.
+	 */
+	kb = kbd_table + fg_console;
+	if (kb->kbdmode != VC_RAW ||
+		 kb->kbdmode != VC_MEDIUMRAW ||
+		 kb->kbdmode != VC_OFF) {
+		vt_switch = true;
+		set_leds();
+	}
 
 	spin_lock_irqsave(&kbd_event_lock, flags);
 	do_compute_shiftstate();
@@ -1255,6 +1268,11 @@ static void kbd_bh(struct tasklet_struct *unused)
 	leds |= (unsigned int)kbd->lockstate << 8;
 	spin_unlock_irqrestore(&led_lock, flags);
 
+	if (vt_switch) {
+		ledstate = ~leds;
+		vt_switch = false;
+	}
+
 	if (leds != ledstate) {
 		kbd_propagate_led_state(ledstate, leds);
 		ledstate = leds;
Signature · 1 line
2.20.1
#1 lore raw

Hi,

drivers/tty/vt/keyboard.c static void do_compute_shiftstate(void) · line 427

Quoted 9 lines · lianzhi chang
drivers/tty/vt/keyboard.c +9 -0
+	/*
+	 * When switching VT, according to the value of kb->kbdmode,
+	 * judge whether it is necessary to force the keyboard light
+	 * state to be issued.
+	 */
+	kb = kbd_table + fg_console;
+	if (kb->kbdmode != VC_RAW ||
+		 kb->kbdmode != VC_MEDIUMRAW ||
+		 kb->kbdmode != VC_OFF) {

Please do not do that. Even if kbdmode is one of those states if might be set up to show LED pattern set by ioctls (KDSETLED), so you still want to restore LEDs.

Also, you are trying optimize something that happens pretty infrequently and it is really easy to get it wrong. It is much safer to let the kernel [re]set LEDs regardless of kbdmode and then let X do its own thing.

Thanks.

Signature · 1 line
Dmitry
#2 lore raw
In reply to lianzhi chang
Quoted 30 lines · lianzhi chang

Use the "ctrl+alt+Fn" key combination to switch the system from tty to desktop or switch the system from desktop to tty. After the switch is completed, it is found that the state of the keyboard lock is inconsistent with the state of the keyboard Led light.The reasons are as follows:

* The desktop environment (Xorg and other services) is bound to a tty (assuming it is tty1), and the kb->kbdmode attribute value of tty1 will be set to VC_OFF. According to the current code logic, in the desktop environment, the values of ledstate and kb->ledflagstate of tty1 will not be modified anymore, so they are always 0.

* When switching between each tty, the final value of ledstate set by the previous tty is compared with the kb->ledflagstate value of the current tty to determine whether to set the state of the keyboard light. The process of switching between desktop and tty is also the process of switching between tty1 and other ttys. There are two situations:

- (1) In the desktop environment, tty1 will not set the ledstate, which will cause when switching from the desktop to other ttys, if the desktop lights up the keyboard's led, after the switch is completed, the keyboard's led light will always be on;

- (2) When switching from another tty to the desktop, this mechanism will trigger tty1 to set the led state. If other tty lights up the led of the keyboard before switching to the desktop, the led will be forcibly turned off. This situation should be avoided.

Quoted 3 lines

* The current patch is to solve these problems: by judging the kb->kbdmode value of the current tty, it is determined whether the led state is forced to be set when the VT switch is completed.

Thanks for the update, looks pretty much good!

Quoted 9 lines

---
v18:
(1) Modify the description information.
(2) The judgment condition of kb->kbdmode should be "!=", correct it.

(3) Modify code indentation.

It's still not updated.

Quoted 23 lines
 drivers/tty/vt/keyboard.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/tty/vt/keyboard.c +2 -1
index c7fbbcdcc346..bcf8a858afd2 100644
@@ -153,6 +153,7 @@ static int shift_state = 0;
 
 static unsigned int ledstate = -1U;			/* undefined */
 static unsigned char ledioctl;
+static bool vt_switch;
 
 /*
  * Notifier list for console keyboard events
@@ -412,9 +413,21 @@ static void do_compute_shiftstate(void)
 /* We still have to export this method to vt.c */
 void vt_set_leds_compute_shiftstate(void)
 {
+	struct kbd_struct *kb;
 	unsigned long flags;
 
-	set_leds();

drivers/tty/vt/keyboard.c static void do_compute_shiftstate(void) · line 428

Quoted 10 lines
drivers/tty/vt/keyboard.c +9 -0
+	/*
+	 * When switching VT, according to the value of kb->kbdmode,
+	 * judge whether it is necessary to force the keyboard light
+	 * state to be issued.
+	 */
+	kb = kbd_table + fg_console;
+	if (kb->kbdmode != VC_RAW ||
 
+		 kb->kbdmode != VC_MEDIUMRAW ||
+		 kb->kbdmode != VC_OFF) {

These two are still too right shifted.

Quoted 20 lines
drivers/tty/vt/keyboard.c +3 -0
+		vt_switch = true;
+		set_leds();
+	}
 
 	spin_lock_irqsave(&kbd_event_lock, flags);
drivers/tty/vt/keyboard.c +5 -0
 	do_compute_shiftstate();
@@ -1255,6 +1268,11 @@ static void kbd_bh(struct tasklet_struct *unused)
 	leds |= (unsigned int)kbd->lockstate << 8;
 	spin_unlock_irqrestore(&led_lock, flags);
 
+	if (vt_switch) {
+		ledstate = ~leds;
+		vt_switch = false;
+	}
+
 	if (leds != ledstate) {
 		kbd_propagate_led_state(ledstate, leds);
 		ledstate = leds;
Signature · 1 line
2.20.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
build bot
#3 lore raw
In reply to lianzhi chang
CC: kbuild-all(a)lists.01.org In-Reply-To: <[email protected]> References: <[email protected]> TO: lianzhi chang…

CC: kbuild-all(a)lists.01.org
In-Reply-To: <[email protected]>
References: <[email protected]>
TO: lianzhi chang <[email protected]>
TO: linux-kernel(a)vger.kernel.org
CC: dmitry.torokhov(a)gmail.com
CC: gregkh(a)linuxfoundation.org
CC: jirislaby(a)kernel.org
CC: andriy.shevchenko(a)linux.intel.com
CC: 282827961(a)qq.com
CC: lianzhi chang <[email protected]>

Hi lianzhi,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on tty/tty-testing]
[also build test WARNING on linux/master linus/master v5.16-rc3 next-20211126] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/lianzhi-chang/tty-Fix-the-keyboard-led-light-display-problem/20211129-120853
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git tty-testing

:::::: branch date: 8 hours ago
:::::: commit date: 8 hours ago
config: i386-randconfig-m021-20211128 (https://download.01.org/0day-ci/archive/20211129/202111291912.gGbp6mtv-lkp(a)intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

If you fix the issue, kindly add following tag as appropriate

New smatch warnings:
drivers/tty/vt/keyboard.c:425 vt_set_leds_compute_shiftstate() warn: was && intended here instead of ||?

Old smatch warnings:
drivers/tty/vt/keyboard.c:2091 vt_do_kdgkb_ioctl() warn: possible info leak 'kbs' drivers/tty/vt/keyboard.c:2113 vt_do_kdgkb_ioctl() error: uninitialized symbol 'kbs'. drivers/tty/vt/keyboard.c:2115 vt_do_kdgkb_ioctl() error: uninitialized symbol 'ret'.

vim +425 drivers/tty/vt/keyboard.c

^1da177e4c3f41 drivers/char/keyboard.c   Linus Torvalds 2005-04-16  412  
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox       2012-02-28  413  /* We still have to export this method to vt.c */
63f24a7fafd448 drivers/tty/vt/keyboard.c Jiri Slaby     2021-01-05  414  void vt_set_leds_compute_shiftstate(void)
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox       2012-02-28  415  {
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  416  	struct kbd_struct *kb;
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox       2012-02-28  417  	unsigned long flags;
63f24a7fafd448 drivers/tty/vt/keyboard.c Jiri Slaby     2021-01-05  418  
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  419  	/*
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  420  	 * When switching VT, according to the value of kb->kbdmode,
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  421  	 * judge whether it is necessary to force the keyboard light
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  422  	 * state to be issued.
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  423  	 */
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  424  	kb = kbd_table + fg_console;
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29 @425  	if (kb->kbdmode != VC_RAW ||
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  426  		 kb->kbdmode != VC_MEDIUMRAW ||
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  427  		 kb->kbdmode != VC_OFF) {
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  428  		vt_switch = true;
63f24a7fafd448 drivers/tty/vt/keyboard.c Jiri Slaby     2021-01-05  429  		set_leds();
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  430  	}
63f24a7fafd448 drivers/tty/vt/keyboard.c Jiri Slaby     2021-01-05  431  
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox       2012-02-28  432  	spin_lock_irqsave(&kbd_event_lock, flags);
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox       2012-02-28  433  	do_compute_shiftstate();
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox       2012-02-28  434  	spin_unlock_irqrestore(&kbd_event_lock, flags);
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox       2012-02-28  435  }
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox       2012-02-28  436  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#4 lore raw
Quoted 7 lines · Dan Carpenter

Hi lianzhi,

url:    https://github.com/0day-ci/linux/commits/lianzhi-chang/tty-Fix-the-keyboard-led-light-display-problem/20211129-120853
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git tty-testing
config: i386-randconfig-m021-20211128 (https://download.01.org/0day-ci/archive/20211129/202111291912.gGbp6mtv-lkp(a)intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
Quoted 6 lines

If you fix the issue, kindly add following tag as appropriate

New smatch warnings:
drivers/tty/vt/keyboard.c:425 vt_set_leds_compute_shiftstate() warn: was && intended here instead of ||?

Indeed, how this had been tested?

Quoted 9 lines
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29 @425  	if (kb->kbdmode != VC_RAW ||
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  426  		 kb->kbdmode != VC_MEDIUMRAW ||
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  427  		 kb->kbdmode != VC_OFF) {

&& was intended instead of ||.

560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  428  		vt_switch = true;
63f24a7fafd448 drivers/tty/vt/keyboard.c Jiri Slaby     2021-01-05  429  		set_leds();
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang  2021-11-29  430  	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko