linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Denose <jdenose@google.com>
To: Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Jiri Kosina <jikos@kernel.org>,
	 Benjamin Tissoires <bentiss@kernel.org>
Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Jonathan Denose <jdenose@google.com>
Subject: [PATCH v2 1/2] Input: Add lid switch notifier
Date: Tue, 11 Nov 2025 21:34:06 +0000	[thread overview]
Message-ID: <20251111-lid-switch-notifier-v2-1-789723d78d89@google.com> (raw)
In-Reply-To: <20251111-lid-switch-notifier-v2-0-789723d78d89@google.com>

This change creates a new input handler which can be included in the
build via a new Kconfig option CONFIG_INPUT_LID_NOTIFIER. This input
handler listens for lid switch events and publishes them through an
atomic notification chain. Other modules may register for events
through this notification chain with register_lid_notifier.

Signed-off-by: Jonathan Denose <jdenose@google.com>
---
 drivers/input/Kconfig        | 11 +++++
 drivers/input/Makefile       |  1 +
 drivers/input/lid-notifier.c | 98 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/input.h        |  2 +
 4 files changed, 112 insertions(+)

diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig
index 88ecdf5218ee9ba35e1efec6341f8605b621bd49..16f6d24fd04ac8cb5af9d36cc47155ea9be0e177 100644
--- a/drivers/input/Kconfig
+++ b/drivers/input/Kconfig
@@ -38,6 +38,17 @@ config INPUT_LEDS
 	  To compile this driver as a module, choose M here: the
 	  module will be called input-leds.
 
+config INPUT_LID_NOTIFIER
+	tristate "Include notifier for lid switch events"
+	help
+	  Say Y here if you would like to create a notifier to publish lid switch
+		events.
+
+	  If unsure, say N.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called lid-notifier.
+
 config INPUT_FF_MEMLESS
 	tristate "Support for memoryless force-feedback devices"
 	help
diff --git a/drivers/input/Makefile b/drivers/input/Makefile
index 2cd6e1c9a77844fe09cd3d99533e5d3efb038c7d..1efdba04f79a97e2a122b9198341b18a1855b4b9 100644
--- a/drivers/input/Makefile
+++ b/drivers/input/Makefile
@@ -15,6 +15,7 @@ obj-$(CONFIG_INPUT_MATRIXKMAP)	+= matrix-keymap.o
 obj-$(CONFIG_INPUT_VIVALDIFMAP)	+= vivaldi-fmap.o
 
 obj-$(CONFIG_INPUT_LEDS)	+= input-leds.o
+obj-$(CONFIG_INPUT_LID_NOTIFIER)	+= lid-notifier.o
 obj-$(CONFIG_INPUT_MOUSEDEV)	+= mousedev.o
 obj-$(CONFIG_INPUT_JOYDEV)	+= joydev.o
 obj-$(CONFIG_INPUT_EVDEV)	+= evdev.o
diff --git a/drivers/input/lid-notifier.c b/drivers/input/lid-notifier.c
new file mode 100644
index 0000000000000000000000000000000000000000..954b9855532dbd0514860e309d0b76982e947673
--- /dev/null
+++ b/drivers/input/lid-notifier.c
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ *  Lid event notifier
+ *
+ *  Copyright (c) 2025 Jonathan Denose <jdenose@google.com>
+ */
+
+#include <linux/device.h>
+#include <linux/input.h>
+#include <linux/notifier.h>
+
+static struct input_handler lid_handler;
+static struct atomic_notifier_head input_notifier_head;
+
+int register_lid_notifier(struct notifier_block *notifier)
+{
+	return atomic_notifier_chain_register(&input_notifier_head, notifier);
+}
+EXPORT_SYMBOL(register_lid_notifier);
+
+static int lid_handler_connect(struct input_handler *handler,
+		struct input_dev *input_dev, const struct input_device_id *id)
+{
+	struct input_handle *handle;
+	int error;
+
+	handle = devm_kzalloc(&input_dev->dev, sizeof(struct input_handle), GFP_KERNEL);
+	if (!handle)
+		return -ENOMEM;
+
+	handle->dev = input_dev;
+	handle->handler = handler;
+	handle->name = "lid";
+
+	error = input_register_handle(handle);
+	if (error)
+		goto err_free_handle;
+
+	error = input_open_device(handle);
+	if (error)
+		goto err_unregister_handle;
+
+	return 0;
+
+ err_unregister_handle:
+	input_unregister_handle(handle);
+ err_free_handle:
+	kfree(handle);
+	return error;
+}
+
+static void lid_handler_disconnect(struct input_handle *handle)
+{
+	input_close_device(handle);
+	input_unregister_handle(handle);
+}
+
+static void lid_handler_event(struct input_handle *handle, unsigned int type,
+		unsigned int code, int value)
+{
+	if (type == EV_SW && code == SW_LID)
+		atomic_notifier_call_chain(&input_notifier_head, value, handle->dev);
+}
+
+static const struct input_device_id lid_handler_ids[] = {
+	{
+		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT
+						| INPUT_DEVICE_ID_MATCH_BUS,
+		.evbit = { BIT_MASK(EV_SW) },
+		.swbit = { [BIT_WORD(SW_LID)] = BIT_MASK(SW_LID) },
+		.bustype = 0x19
+	},
+	{ },
+};
+
+static struct input_handler lid_handler = {
+	.connect = lid_handler_connect,
+	.disconnect = lid_handler_disconnect,
+	.event = lid_handler_event,
+	.name = "lid",
+	.id_table = lid_handler_ids
+};
+
+static int __init lid_notifier_init(void)
+{
+	return input_register_handler(&lid_handler);
+}
+module_init(lid_notifier_init);
+
+static void __exit lid_notifier_exit(void)
+{
+	input_unregister_handler(&lid_handler);
+}
+module_exit(lid_notifier_exit);
+
+MODULE_AUTHOR("Jonathan Denose <jdenose@google.com>");
+MODULE_DESCRIPTION("Lid event notifier");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/input.h b/include/linux/input.h
index 7d7cb0593a63e93c4906c49cde430188db2d1ab5..023eb92c77d9e8721d482b9787632a671671de08 100644
--- a/include/linux/input.h
+++ b/include/linux/input.h
@@ -592,3 +592,5 @@ int input_ff_create_memless(struct input_dev *dev, void *data,
 		int (*play_effect)(struct input_dev *, void *, struct ff_effect *));
 
 #endif
+
+int register_lid_notifier(struct notifier_block *notifier);

-- 
2.51.2.1041.gc1ab5b90ca-goog


  reply	other threads:[~2025-11-11 21:34 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-11 21:34 [PATCH v2 0/2] Implement lid-notifier for lid switch events Jonathan Denose
2025-11-11 21:34 ` Jonathan Denose [this message]
2025-11-11 22:34   ` [PATCH v2 1/2] Input: Add lid switch notifier Dmitry Torokhov
2025-11-12 23:45     ` Jonathan Denose
2025-11-11 21:34 ` [PATCH v2 2/2] HID: multitouch: Toggle touch surface on Elan touchpad on lid event Jonathan Denose
2025-11-11 22:37   ` Dmitry Torokhov
2025-11-12 23:49     ` Jonathan Denose
2025-11-18  7:12       ` Dmitry Torokhov
2025-11-13  4:49   ` kernel test robot
2025-11-13  4:49   ` kernel test robot

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=20251111-lid-switch-notifier-v2-1-789723d78d89@google.com \
    --to=jdenose@google.com \
    --cc=bentiss@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).