All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: "Samuel Thibault" <samuel.thibault@ens-lyon.org>,
	"Pavel Machek" <pavel@ucw.cz>,
	"Pali Rohár" <pali.rohar@gmail.com>
Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
	rpurdie@rpsys.net,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: [PATCH 2/3] tty/vt/keyboard: define LED triggers for VT LED states
Date: Mon,  8 Jun 2015 14:43:09 -0700	[thread overview]
Message-ID: <1433799790-31873-3-git-send-email-dmitry.torokhov@gmail.com> (raw)
In-Reply-To: <1433799790-31873-1-git-send-email-dmitry.torokhov@gmail.com>

From: Samuel Thibault <samuel.thibault@ens-lyon.org>

Now that input core allows controlling keyboards LEDs via standard LED
subsystem triggers let's switch VT keyboard code to make use of this
feature. We will define the following standard triggers: "kbd-scrollock",
"kbd-numlock", "kbd-capslock", and "kbd-kanalock" which are default
triggers for respective LEDs on keyboards.

Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/tty/vt/keyboard.c | 141 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 117 insertions(+), 24 deletions(-)

diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c
index 8a89f6e..cff193e 100644
--- a/drivers/tty/vt/keyboard.c
+++ b/drivers/tty/vt/keyboard.c
@@ -33,6 +33,7 @@
 #include <linux/string.h>
 #include <linux/init.h>
 #include <linux/slab.h>
+#include <linux/leds.h>
 
 #include <linux/kbd_kern.h>
 #include <linux/kbd_diacr.h>
@@ -961,6 +962,110 @@ static void k_brl(struct vc_data *vc, unsigned char value, char up_flag)
 	}
 }
 
+#if IS_ENABLED(CONFIG_INPUT_LEDS)
+
+struct kbd_led_trigger {
+	struct led_trigger trigger;
+	unsigned int mask;
+};
+
+static void kbd_led_trigger_activate(struct led_classdev *cdev)
+{
+	struct kbd_led_trigger *trigger =
+		container_of(cdev->trigger, struct kbd_led_trigger, trigger);
+
+	tasklet_disable(&keyboard_tasklet);
+	if (ledstate != 0xff)
+		led_trigger_event(&trigger->trigger,
+				  ledstate & trigger->mask ?
+					LED_FULL : LED_OFF);
+	tasklet_enable(&keyboard_tasklet);
+}
+
+#define KBD_LED_TRIGGER(_led_bit, _name) {			\
+		.trigger = {					\
+			.name = _name,				\
+			.activate = kbd_led_trigger_activate,	\
+		},						\
+		.mask	= BIT(_led_bit),			\
+	}
+
+static struct kbd_led_trigger kbd_led_triggers[] = {
+	KBD_LED_TRIGGER(VC_SCROLLOCK, "kbd-scrollock"),
+	KBD_LED_TRIGGER(VC_NUMLOCK,   "kbd-numlock"),
+	KBD_LED_TRIGGER(VC_CAPSLOCK,  "kbd-capslock"),
+	KBD_LED_TRIGGER(VC_KANALOCK,  "kbd-kanalock"),
+};
+
+static void kbd_propagate_led_state(unsigned int old_state,
+				    unsigned int new_state)
+{
+	struct kbd_led_trigger *trigger;
+	unsigned int changed = old_state ^ new_state;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); i++) {
+		trigger = &kbd_led_triggers[i];
+
+		if (changed & trigger->mask)
+			led_trigger_event(&trigger->trigger,
+					  new_state & trigger->mask ?
+						LED_FULL : LED_OFF);
+	}
+}
+
+static int kbd_update_leds_helper(struct input_handle *handle, void *data)
+{
+	unsigned int led_state = *(unsigned int *)data;
+
+	if (test_bit(EV_LED, handle->dev->evbit))
+		kbd_propagate_led_state(~led_state, led_state);
+
+	return 0;
+}
+
+static void kbd_init_leds(void)
+{
+	int error;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); i++) {
+		error = led_trigger_register(&kbd_led_triggers[i].trigger);
+		if (error)
+			pr_err("error %d while registering trigger %s\n",
+			       error, kbd_led_triggers[i].trigger.name);
+	}
+}
+
+#else
+
+static int kbd_update_leds_helper(struct input_handle *handle, void *data)
+{
+	unsigned int leds = *(unsigned int *)data;
+
+	if (test_bit(EV_LED, handle->dev->evbit)) {
+		input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & 0x01));
+		input_inject_event(handle, EV_LED, LED_NUML,    !!(leds & 0x02));
+		input_inject_event(handle, EV_LED, LED_CAPSL,   !!(leds & 0x04));
+		input_inject_event(handle, EV_SYN, SYN_REPORT, 0);
+	}
+
+	return 0;
+}
+
+static void kbd_propagate_led_state(unsigned int old_state,
+				    unsigned int new_state)
+{
+	input_handler_for_each_handle(&kbd_handler, &new_state,
+				      kbd_update_leds_helper);
+}
+
+static void kbd_init_leds(void)
+{
+}
+
+#endif
+
 /*
  * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
  * or (ii) whatever pattern of lights people want to show using KDSETLED,
@@ -995,20 +1100,6 @@ static inline unsigned char getleds(void)
 	return kb->ledflagstate;
 }
 
-static int kbd_update_leds_helper(struct input_handle *handle, void *data)
-{
-	unsigned char leds = *(unsigned char *)data;
-
-	if (test_bit(EV_LED, handle->dev->evbit)) {
-		input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & 0x01));
-		input_inject_event(handle, EV_LED, LED_NUML,    !!(leds & 0x02));
-		input_inject_event(handle, EV_LED, LED_CAPSL,   !!(leds & 0x04));
-		input_inject_event(handle, EV_SYN, SYN_REPORT, 0);
-	}
-
-	return 0;
-}
-
 /**
  *	vt_get_leds	-	helper for braille console
  *	@console: console to read
@@ -1085,24 +1176,22 @@ void vt_kbd_con_stop(int console)
 }
 
 /*
- * This is the tasklet that updates LED state on all keyboards
- * attached to the box. The reason we use tasklet is that we
- * need to handle the scenario when keyboard handler is not
- * registered yet but we already getting updates from the VT to
- * update led state.
+ * This is the tasklet that updates LED state of LEDs using standard
+ * keyboard triggers. The reason we use tasklet is that we need to
+ * handle the scenario when keyboard handler is not registered yet
+ * but we already getting updates from the VT to update led state.
  */
 static void kbd_bh(unsigned long dummy)
 {
 	unsigned char leds;
 	unsigned long flags;
-	
+
 	spin_lock_irqsave(&led_lock, flags);
 	leds = getleds();
 	spin_unlock_irqrestore(&led_lock, flags);
 
 	if (leds != ledstate) {
-		input_handler_for_each_handle(&kbd_handler, &leds,
-					      kbd_update_leds_helper);
+		kbd_propagate_led_state(ledstate, leds);
 		ledstate = leds;
 	}
 }
@@ -1450,8 +1539,10 @@ static void kbd_start(struct input_handle *handle)
 {
 	tasklet_disable(&keyboard_tasklet);
 
-	if (ledstate != 0xff)
-		kbd_update_leds_helper(handle, &ledstate);
+	if (ledstate != 0xff) {
+		unsigned int state = ledstate;
+		kbd_update_leds_helper(handle, &state);
+	}
 
 	tasklet_enable(&keyboard_tasklet);
 }
@@ -1497,6 +1588,8 @@ int __init kbd_init(void)
 		kbd_table[i].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
 	}
 
+	kbd_init_leds();
+
 	error = input_register_handler(&kbd_handler);
 	if (error)
 		return error;
-- 
2.2.0.rc0.207.ga3a616c


  parent reply	other threads:[~2015-06-08 21:44 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-08 21:43 [PATCH 0/3] Switch input leds over to standard LED class devices Dmitry Torokhov
2015-06-08 21:43 ` [PATCH 1/3] Input: export LEDs as class devices in sysfs Dmitry Torokhov
2015-06-09 13:19   ` Samuel Thibault
2015-06-09 13:19     ` Samuel Thibault
2015-06-09 13:27     ` Samuel Thibault
2015-06-09 13:27       ` Samuel Thibault
2015-06-09 16:50       ` Dmitry Torokhov
2015-06-09 16:50         ` Dmitry Torokhov
2015-06-09 17:16         ` Samuel Thibault
2015-06-09 17:16           ` Samuel Thibault
2015-06-09 16:49     ` Dmitry Torokhov
2015-06-09 16:49       ` Dmitry Torokhov
2015-06-09 17:22       ` Samuel Thibault
2015-06-09 17:22         ` Samuel Thibault
2015-06-09 17:32         ` Dmitry Torokhov
2015-06-09 17:32           ` Dmitry Torokhov
2015-06-10  6:34       ` Pavel Machek
2015-06-10  6:34         ` Pavel Machek
2015-06-09 17:42   ` [PATCH v2 " Dmitry Torokhov
2015-06-10  0:32     ` Samuel Thibault
2015-06-10  1:24       ` Dmitry Torokhov
2015-06-11 17:51         ` Pavel Machek
2015-06-15 10:03         ` Pavel Machek
2015-06-15 10:51           ` Pali Rohár
2015-07-21 11:14     ` Vlastimil Babka
2015-07-21 17:01       ` Dmitry Torokhov
2015-07-21 21:08         ` Pavel Machek
2015-07-22 13:12           ` Vlastimil Babka
2015-07-22 18:55             ` Jiri Kosina
2015-07-23  5:19               ` Vlastimil Babka
2015-07-23  5:42                 ` Jiri Kosina
2015-07-22 14:41         ` Vlastimil Babka
2015-07-22 19:49           ` Jiri Kosina
2015-07-22 21:47             ` Pavel Machek
2015-07-22 21:50               ` Jiri Kosina
2015-07-22 21:49             ` Dmitry Torokhov
2015-07-22 22:01               ` Jiri Kosina
2015-06-08 21:43 ` Dmitry Torokhov [this message]
2015-06-08 21:43 ` [PATCH 3/3] tty/vt/keyboard: define LED triggers for VT keyboard lock states Dmitry Torokhov
2015-06-08 22:58 ` [PATCH 0/3] Switch input leds over to standard LED class devices Bastien Nocera
2015-06-08 23:16   ` Dmitry Torokhov
2015-06-09 10:54 ` Pavel Machek
2015-06-09 11:12   ` Pavel Machek
2015-06-09 11:22     ` Pali Rohár
2015-06-09 11:22       ` Pali Rohár
2015-06-09 11:28       ` Pavel Machek
2015-06-09 11:28         ` Pavel Machek
2015-06-09 12:22       ` Samuel Thibault
2015-06-09 12:22         ` Samuel Thibault
2015-06-09 11:26     ` Pavel Machek
2015-06-09 16:40       ` Dmitry Torokhov
2015-06-09 12:20   ` Samuel Thibault
2015-06-09 12:20     ` Samuel Thibault
2015-06-09 16:18   ` Pavel Machek
2015-06-09 16:32     ` Dmitry Torokhov
2015-06-09 16:37   ` Dmitry Torokhov
2015-06-09 13:42 ` Samuel Thibault
2015-06-09 13:42   ` Samuel Thibault
2015-06-09 13:50   ` Pali Rohár
2015-06-09 13:50     ` Pali Rohár
2015-06-09 14:05     ` Samuel Thibault
     [not found] ` <20090205113908.GA14224@const.inria.fr>
2015-06-09 14:17   ` caps lock led does not show up Samuel Thibault
2015-06-09 14:17     ` Samuel Thibault
2015-06-09 16:03     ` Bug#514464: " Anton Zinoviev
2015-06-09 16:03       ` Anton Zinoviev
2015-06-11  8:08       ` Samuel Thibault
2015-06-11  8:08         ` Samuel Thibault
2015-06-11 14:28         ` Samuel Thibault
2015-06-11 14:28           ` Samuel Thibault
2015-06-11 15:37           ` Samuel Thibault
2015-06-25 15:41             ` Samuel Thibault
2015-07-02 16:39               ` Anton Zinoviev
2015-07-02 16:50                 ` Samuel Thibault
2015-08-31  8:33                 ` Samuel Thibault

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1433799790-31873-3-git-send-email-dmitry.torokhov@gmail.com \
    --to=dmitry.torokhov@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pali.rohar@gmail.com \
    --cc=pavel@ucw.cz \
    --cc=rpurdie@rpsys.net \
    --cc=samuel.thibault@ens-lyon.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.