[PATCH 8/9] HID: haptic: Convert into a loadable plugin
From: Rong Zhang
Date: Sun Nov 01 2026 - 12:44:02 EST
Commit 083a4f3f3cc7 ("HID: Kconfig: Fix build error from
CONFIG_HID_HAPTIC") built hid-haptic into vmlinux or hid.ko. This is not
optimal for generic kernel images, as a platform with no multitouch
device doesn't need it anyway. It's also a bit harsh for enabling it in
distribution kernels.
With the new hid-plugin framework, hid-haptic can be easily converted
into a loadable plugin module, so that hid-multitouch only requests it
on demand.
The redaundent `default n' statement is also removed for tidiness since
we are anyway touching its context.
Suggested-by: Benjamin Tissoires <bentiss@xxxxxxxxxx>
Link: https://lore.kernel.org/linux-input/auypydfkhx2eg7vp764way4batdilzc35inqda3exwzs3tk3ff@oagat6g46zto/
Signed-off-by: Rong Zhang <i@xxxxxxxx>
---
MAINTAINERS | 1 +
drivers/hid/Kconfig | 7 +++++--
drivers/hid/Makefile | 2 +-
drivers/hid/hid-haptic.c | 28 ++++++++++++++++++----------
drivers/hid/hid-haptic.h | 29 +++++++++++++++++++++++++++--
drivers/hid/hid-plugin.c | 3 +++
drivers/hid/hid-plugin.h | 6 ++++++
7 files changed, 61 insertions(+), 15 deletions(-)
diff --git a/MAINTAINERS b/MAINTAINERS
index fdec2f6b640d..a7d4953c3844 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11906,6 +11906,7 @@ HID PLUGINS
M: Rong Zhang <i@xxxxxxxx>
L: linux-input@xxxxxxxxxxxxxxx
S: Maintained
+F: drivers/hid/hid-haptic.*
F: drivers/hid/hid-plugin.*
HID PULSAR DRIVER
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 604683b4a4a9..97cb4caf9d7e 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -106,9 +106,10 @@ menuconfig HID_PLUGIN
If unsure, say N.
+if HID_PLUGIN
+
config HID_HAPTIC
- bool "Haptic touchpad support"
- default n
+ tristate "Haptic touchpad support"
help
Support for touchpads with force sensors and haptic actuators instead of a
traditional button.
@@ -117,6 +118,8 @@ config HID_HAPTIC
If unsure, say N.
+endif # HID_PLUGIN
+
menu "Special HID drivers"
config HID_A4TECH
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index b41aa3f52a94..3360347c1cb8 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -6,7 +6,7 @@ hid-y := hid-core.o hid-input.o hid-quirks.o
hid-$(CONFIG_DEBUG_FS) += hid-debug.o
hid-$(CONFIG_HID_PLUGIN) += hid-plugin.o
-hid-$(CONFIG_HID_HAPTIC) += hid-haptic.o
+obj-$(CONFIG_HID_HAPTIC) += hid-haptic.o
obj-$(CONFIG_HID_BPF) += bpf/
diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index dcecb89e8fd5..d5e33b470e36 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -315,12 +315,11 @@ static void hid_haptic_destroy(struct ff_device *ff)
if (hdev)
put_device(&hdev->dev);
- module_put(THIS_MODULE);
+ hid_plugin_put(HID_PLUGIN_HAPTIC, &haptic->self_plugin->hp);
}
-int hid_haptic_init(struct hid_device *hdev,
- struct hid_haptic_device *haptic,
- struct input_dev *dev)
+static int __hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *haptic,
+ struct input_dev *dev)
{
struct ff_device *ff;
int ret = 0, r;
@@ -328,10 +327,6 @@ int hid_haptic_init(struct hid_device *hdev,
.hid_usage = HID_HP_WAVEFORMSTOP & HID_USAGE,
};
- if (!try_module_get(THIS_MODULE)) {
- dev_err(&hdev->dev, "Failed to increase module count.\n");
- return -ENXIO;
- }
if (!get_device(&hdev->dev)) {
dev_err(&hdev->dev, "Failed to get hdev device.\n");
ret = -ENXIO;
@@ -456,7 +451,20 @@ int hid_haptic_init(struct hid_device *hdev,
put_device(&hdev->dev);
exit:
dev_warn(&hdev->dev, "Cannot allocate haptic for %s: %d\n", hdev->name, ret);
- module_put(THIS_MODULE);
+ hid_plugin_put(HID_PLUGIN_HAPTIC, &haptic->self_plugin->hp);
return ret;
}
-EXPORT_SYMBOL_GPL(hid_haptic_init);
+
+static struct hid_plugin_haptic hid_plugin_haptic = {
+ .hp = {
+ .owner = THIS_MODULE,
+ },
+ .init = __hid_haptic_init,
+};
+module_hid_plugin(haptic, HID_PLUGIN_HAPTIC, &hid_plugin_haptic.hp);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Angela Czubak <aczubak@xxxxxxxxxx>");
+MODULE_AUTHOR("Jonathan Denose <jdenose@xxxxxxxxxx>");
+MODULE_AUTHOR("Rong Zhang <i@xxxxxxxx>");
+MODULE_DESCRIPTION("HID helpers for Haptics Page");
diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index d09068689a5e..d3e8fb9aa3b4 100644
--- a/drivers/hid/hid-haptic.h
+++ b/drivers/hid/hid-haptic.h
@@ -5,10 +5,13 @@
* Copyright (c) 2021 Angela Czubak <acz@xxxxxxxxxxxx>
*/
+#include <linux/container_of.h>
#include <linux/err.h>
#include <linux/hid.h>
#include <linux/slab.h>
+#include "hid-plugin.h"
+
#define HID_HAPTIC_ORDINAL_WAVEFORMNONE 1
#define HID_HAPTIC_ORDINAL_WAVEFORMSTOP 2
@@ -53,6 +56,14 @@ struct hid_haptic_device {
u16 release_ordinal;
struct hid_haptic_effect *effect;
struct hid_haptic_effect stop_effect;
+
+ struct hid_plugin_haptic *self_plugin;
+};
+
+struct hid_plugin_haptic {
+ struct hid_plugin hp;
+ int (*init)(struct hid_device *hdev, struct hid_haptic_device *haptic,
+ struct input_dev *dev);
};
#if IS_ENABLED(CONFIG_HID_HAPTIC)
@@ -147,8 +158,22 @@ static inline int hid_haptic_input_mapping(struct hid_device *hdev,
return 0;
}
-int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *haptic,
- struct input_dev *dev);
+static inline int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *haptic,
+ struct input_dev *dev)
+{
+ struct hid_plugin_haptic *self_plugin;
+ struct hid_plugin *plugin;
+
+ plugin = hid_plugin_get(HID_PLUGIN_HAPTIC);
+ if (IS_ERR(plugin))
+ return PTR_ERR(plugin);
+
+ self_plugin = container_of(plugin, struct hid_plugin_haptic, hp);
+
+ haptic->self_plugin = self_plugin;
+
+ return self_plugin->init(hdev, haptic, dev);
+}
static inline bool hid_haptic_input_configured(struct hid_device *hdev,
struct hid_haptic_device *haptic,
diff --git a/drivers/hid/hid-plugin.c b/drivers/hid/hid-plugin.c
index a83275df038c..7357f81c3aac 100644
--- a/drivers/hid/hid-plugin.c
+++ b/drivers/hid/hid-plugin.c
@@ -18,6 +18,9 @@
#include "hid-plugin.h"
static const char * const hid_plugin_mod_names[NR_HID_PLUGINS] = {
+#if IS_MODULE(CONFIG_HID_HAPTIC)
+ [HID_PLUGIN_HAPTIC] = "hid-haptic",
+#endif
};
static struct hid_plugin __rcu *hid_plugins[NR_HID_PLUGINS];
diff --git a/drivers/hid/hid-plugin.h b/drivers/hid/hid-plugin.h
index 4038325c51a0..20d1b80a98c8 100644
--- a/drivers/hid/hid-plugin.h
+++ b/drivers/hid/hid-plugin.h
@@ -19,6 +19,12 @@
enum hid_plugin_id {
HID_PLUGIN_DISABLED = -1,
+#if IS_ENABLED(CONFIG_HID_HAPTIC)
+ HID_PLUGIN_HAPTIC,
+#else
+#define HID_PLUGIN_HAPTIC HID_PLUGIN_DISABLED
+#endif
+
NR_HID_PLUGINS,
};
--
2.55.0