[PATCH v5 05/13] HID: Add hid-plugin framework for loadable plugin modules
From: Rong Zhang
Date: Sun Nov 29 2026 - 12:56:11 EST
Some devices support some additional HID usage pages which enhance their
capabilities. The support code for these usage pages is called "HID
plugin".
The corresponding HID driver calls functions provided by the plugin to
take the advantage of such enhancements. Without the framework, the HID
driver must have a linkage dependence on the plugin, which is not
optimal because the plugin is always loaded into memory even if no
device actually supports it.
Logically, the dependence should be reversed, i.e., the plugin should be
an optional component of the HID driver and is only loaded by the HID
driver when the device has the usage page.
Introduce hid-plugin framework for loadable plugin modules to achieve
the said logical dependence. Each plugin has a unique hid_plugin_id when
enabled, so that HID drivers can connect to it accordingly. The
framework loads the plugin module when needed. Once loaded (or if the
plugin is builtin), plugin connection only requires an RCU dereference
plusing an module refcount increment. Once connected, the plugin's
private data is initialized with its pointer stored to hid_device.
On HID driver removal, all HID plugins are automatically disconnected.
When a plugin turns out to be unsuitable for the associated HID device,
it's disconnected early to release the resources.
When a plugin is disabled, its ID maps to HID_PLUGIN_DISABLED, and calls
to the plugin helper functions are eliminated by the compiler.
When all plugins are disabled or when the plugin framework is disabled,
NR_HID_PLUGINS equals 0, and all plugin infrastructures as well as their
call sites are eliminated by the compiler. Note that in this case,
hid_device.plugins[NR_HID_PLUGINS] is still safe as it's not a flexible
array member thanks to -fstrict-flex-arrays=3. Thus, no #ifdef is added
around it.
The Kconfig option HID_PLUGIN is solely used to provide a menuconfig.
Plugins need to select HID_PLUGIN_CORE to really enable the framework.
This aims to ease the migration of existing Kconfig options, so that
olddefconfig won't accidentally disable them after the following
conversion. This also makes sure the hid-plugin framework is not built
when no plugin is enabled.
I volunteer to maintain the framework as well as plugins.
Thanks Benjamin Tissoires for the inspiration for the implementation.
Suggested-by: Benjamin Tissoires <bentiss@xxxxxxxxxx>
Link: https://lore.kernel.org/linux-input/auypydfkhx2eg7vp764way4batdilzc35inqda3exwzs3tk3ff@oagat6g46zto/
Signed-off-by: Rong Zhang <i@xxxxxxxx>
---
Changes in v5:
- Manage HID plugins' private data in the framework
- Disconnect all connected HID plugins on HID driver removal
- Add debug printk()s to track (dis)connection and (un)registration
- Only use HID_PLUGIN to build Kconfig menuconfig, rather than having
plugins depend on it
- Enable HID_PLUGIN by default to ease Kconfig migration
- Build the hid-plugin framework only if HID_PLUGIN_CORE is selected
- HID_HAPTIC will select it in the following change, so that
olddefconfig preserves HID_HAPTIC's value
- HID_PLUGIN shows an empty menu yet. if-endif will be added later
Changes in v4:
- Drop synchronize_rcu() during registration as other drivers do, as
there is nothing to protect
Changes in v3:
- Add missing Return: to hid_plugin_register()'s kernel-doc (thanks
Sashiko)
---
MAINTAINERS | 6 ++
drivers/hid/Kconfig | 16 ++++
drivers/hid/Makefile | 2 +
drivers/hid/hid-core.c | 3 +
drivers/hid/hid-plugin.c | 169 ++++++++++++++++++++++++++++++++++++++
drivers/hid/hid-plugin.h | 208 +++++++++++++++++++++++++++++++++++++++++++++++
include/linux/hid.h | 14 ++++
7 files changed, 418 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 84cb98ef4c49..6b2a4a96a333 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11902,6 +11902,12 @@ L: linux-input@xxxxxxxxxxxxxxx
S: Supported
F: drivers/hid/hid-playstation.c
+HID PLUGINS
+M: Rong Zhang <i@xxxxxxxx>
+L: linux-input@xxxxxxxxxxxxxxx
+S: Maintained
+F: drivers/hid/hid-plugin.*
+
HID PULSAR DRIVER
M: Nikolas Koesling <nikolas@xxxxxxxxxxxxx>
L: linux-input@xxxxxxxxxxxxxxx
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 2481bc01c435..f9f4fb4de986 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -92,6 +92,22 @@ config HID_GENERIC
If unsure, say Y.
+menuconfig HID_PLUGIN
+ bool "HID plugins"
+ default y
+ help
+ Support for HID plugins, which enhance associated HID drivers by
+ providing support for additional HID usage pages.
+
+ Plugins can be either builtin or modules. A plugin compiled as a module
+ is made available to HID drivers by automatically request the module
+ when the HID driver connects to the plugin.
+
+ If unsure, say Y.
+
+config HID_PLUGIN_CORE
+ bool
+
config HID_HAPTIC
bool "Haptic touchpad support"
default n
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index 43a8eb0f389b..b39ccc9a360b 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -4,6 +4,8 @@
#
hid-y := hid-core.o hid-input.o hid-quirks.o
hid-$(CONFIG_DEBUG_FS) += hid-debug.o
+hid-$(CONFIG_HID_PLUGIN_CORE) += hid-plugin.o
+
hid-$(CONFIG_HID_HAPTIC) += hid-haptic.o
obj-$(CONFIG_HID_BPF) += bpf/
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 4a47deb9d833..98c3b006fff9 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -34,6 +34,7 @@
#include <linux/hidraw.h>
#include "hid-ids.h"
+#include "hid-plugin.h"
/*
* Version Information
@@ -2858,6 +2859,7 @@ static int __hid_device_probe(struct hid_device *hdev, struct hid_driver *hdrv)
hid_device_io_stop(hdev);
devres_release_group(&hdev->dev, hdev->devres_group_id);
hid_close_report(hdev);
+ hid_plugin_cleanup(hdev);
hdev->driver = NULL;
}
@@ -2904,6 +2906,7 @@ static void hid_device_remove(struct device *dev)
devres_release_group(&hdev->dev, hdev->devres_group_id);
hid_close_report(hdev);
+ hid_plugin_cleanup(hdev);
hdev->driver = NULL;
}
diff --git a/drivers/hid/hid-plugin.c b/drivers/hid/hid-plugin.c
new file mode 100644
index 000000000000..f93753d3ee12
--- /dev/null
+++ b/drivers/hid/hid-plugin.c
@@ -0,0 +1,169 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * HID plugin helpers
+ *
+ * Copyright (c) 2026 Rong Zhang <i@xxxxxxxx>
+ */
+
+#define pr_fmt(fmt) "hid-plugin: " fmt
+#define dev_fmt pr_fmt
+
+#include <linux/bug.h>
+#include <linux/cleanup.h>
+#include <linux/compiler.h>
+#include <linux/err.h>
+#include <linux/hid.h>
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/rcupdate.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+
+#include "hid-plugin.h"
+
+static const char * const hid_plugin_mod_names[NR_HID_PLUGINS] = {
+};
+
+static struct hid_plugin __rcu *hid_plugins[NR_HID_PLUGINS];
+
+static inline struct hid_plugin *hid_plugin_try_module_get(enum hid_plugin_id id)
+{
+ struct hid_plugin *plugin;
+
+ guard(rcu)();
+
+ plugin = rcu_dereference(hid_plugins[id]);
+ if (!plugin)
+ return NULL;
+
+ /* Is the module unloading? */
+ return try_module_get(plugin->owner) ? plugin : ERR_PTR(-ENOENT);
+}
+
+static inline struct hid_plugin *hid_plugin_request(struct hid_device *hdev, enum hid_plugin_id id)
+{
+ struct hid_plugin *plugin;
+ const char *mod_name;
+ int ret;
+
+ plugin = hid_plugin_try_module_get(id);
+ if (plugin)
+ return plugin;
+
+ mod_name = hid_plugin_mod_names[id];
+ if (WARN_ON(!mod_name)) /* Huh? Builtin plugins can't reach here. */
+ return ERR_PTR(-ENXIO);
+
+ hid_dbg(hdev, "Requesting [%d] %s\n", id, mod_name);
+
+ ret = request_module(mod_name);
+ if (ret) {
+ hid_warn(hdev, "Failed to request [%d] %s: %d\n", id, mod_name, ret);
+ return ERR_PTR(-ENXIO); /* Don't return positive error code. */
+ }
+
+ plugin = hid_plugin_try_module_get(id);
+ return plugin ?: ERR_PTR(-ENOENT);
+}
+
+void *__hid_plugin_connect(struct hid_device *hdev, enum hid_plugin_id id)
+{
+ struct hid_plugin *plugin;
+ void *priv = NULL;
+
+ if (WARN_ON(hdev->plugins[id]))
+ return ERR_PTR(-EEXIST);
+
+ hid_dbg(hdev, "Connecting to [%d]\n", id);
+
+ plugin = hid_plugin_request(hdev, id);
+ if (IS_ERR(plugin)) {
+ hdev->plugins[id] = ERR_CAST(plugin);
+ return ERR_CAST(plugin);
+ }
+
+ if (plugin->connect)
+ priv = plugin->connect(hdev);
+
+ /* Missing the callback or allocating nothing is a bug. */
+ if (WARN_ON(!priv))
+ priv = ERR_PTR(-ENOTCONN);
+ if (IS_ERR(priv))
+ module_put(plugin->owner);
+
+ hdev->plugins[id] = priv;
+ return priv;
+}
+EXPORT_SYMBOL_GPL(__hid_plugin_connect);
+
+static void hid_plugin_disconnect_with_err(struct hid_device *hdev, enum hid_plugin_id id, int err)
+{
+ struct hid_plugin *plugin;
+
+ if (IS_ERR_OR_NULL(hdev->plugins[id])) {
+ hdev->plugins[id] = ERR_PTR(err);
+ return;
+ }
+
+ hid_dbg(hdev, "Disconnecting from [%d]: %d\n", id, err);
+
+ /*
+ * Reaching here means the HID device still holds the plugin's module
+ * refcount and protects our access.
+ */
+ plugin = rcu_dereference_protected(hid_plugins[id], true);
+
+ if (plugin->disconnect)
+ plugin->disconnect(hdev, hdev->plugins[id]);
+
+ module_put(plugin->owner);
+
+ kfree(hdev->plugins[id]);
+ hdev->plugins[id] = ERR_PTR(err);
+}
+
+void __hid_plugin_disconnect(struct hid_device *hdev, enum hid_plugin_id id)
+{
+ hid_plugin_disconnect_with_err(hdev, id, -EIDRM);
+}
+EXPORT_SYMBOL_GPL(__hid_plugin_disconnect);
+
+/* Not exported. Internal use only. */
+void __hid_plugin_cleanup(struct hid_device *hdev)
+{
+ for (int i = 0; i < NR_HID_PLUGINS; i++)
+ hid_plugin_disconnect_with_err(hdev, i, 0);
+}
+
+int __hid_plugin_register(enum hid_plugin_id id, struct hid_plugin *plugin)
+{
+ /*
+ * The corresponding plugin is the only writer of hid_plugins[id], so
+ * there is no need to protect the our access during registration, as
+ * any reader in prior must have got NULL, i.e., there is nothing to
+ * protect or synchronize.
+ *
+ * However, try our best to do some sanity checks.
+ */
+ if (WARN_ON(rcu_access_pointer(hid_plugins[id])))
+ return -EEXIST;
+
+ pr_debug("Registering [%d]\n", id);
+
+ rcu_assign_pointer(hid_plugins[id], plugin);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(__hid_plugin_register);
+
+void __hid_plugin_unregister(enum hid_plugin_id id, struct hid_plugin *plugin)
+{
+ if (WARN_ON(rcu_access_pointer(hid_plugins[id]) != plugin))
+ return;
+
+ pr_debug("Unregistering [%d]\n", id);
+
+ rcu_assign_pointer(hid_plugins[id], NULL);
+ synchronize_rcu();
+}
+EXPORT_SYMBOL_GPL(__hid_plugin_unregister);
diff --git a/drivers/hid/hid-plugin.h b/drivers/hid/hid-plugin.h
new file mode 100644
index 000000000000..b4a790bdeb86
--- /dev/null
+++ b/drivers/hid/hid-plugin.h
@@ -0,0 +1,208 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * HID plugin helpers
+ *
+ * Copyright (c) 2026 Rong Zhang <i@xxxxxxxx>
+ */
+
+#ifndef __HID_PLUGIN_H
+#define __HID_PLUGIN_H
+
+#include <linux/build_bug.h>
+#include <linux/compiler.h>
+#include <linux/err.h>
+#include <linux/hid.h>
+#include <linux/init.h>
+#include <linux/module.h>
+
+/**
+ * struct hid_plugin - HID plugin
+ *
+ * @owner: Plugin's owner module
+ * @connect: Called on plugin connection to initialize the plugin's private
+ * data. Usually the private data needs store a pointer to the
+ * plugin itself.
+ * @disconnect: Called on plugin disconnection (optional, the private data is
+ * always freed by the framework).
+ */
+struct hid_plugin {
+ struct module *owner;
+ void *(*connect)(struct hid_device *hdev);
+ void (*disconnect)(struct hid_device *hdev, void *priv);
+};
+
+void *__hid_plugin_connect(struct hid_device *hdev, enum hid_plugin_id id);
+void __hid_plugin_disconnect(struct hid_device *hdev, enum hid_plugin_id id);
+void __hid_plugin_cleanup(struct hid_device *hdev);
+int __hid_plugin_register(enum hid_plugin_id id, struct hid_plugin *plugin);
+void __hid_plugin_unregister(enum hid_plugin_id id, struct hid_plugin *plugin);
+
+/**
+ * hid_plugin_is_enabled() - Check if a plugin is enabled
+ * @id: Unique ID of the desired HID plugin, must be a constant expression
+ *
+ * Return: Boolean.
+ */
+static __always_inline bool hid_plugin_is_enabled(enum hid_plugin_id id)
+{
+ BUILD_BUG_ON(!__builtin_constant_p(id) || id >= NR_HID_PLUGINS || id < HID_PLUGIN_DISABLED);
+
+ return id != HID_PLUGIN_DISABLED;
+}
+
+/**
+ * hid_plugin_to_priv() - Retrieve a plugin's private data
+ * @hdev: HID device
+ * @id: Unique ID of the HID plugin
+ *
+ * Retrieve the private data of a connected HID plugin.
+ *
+ * Context: Any context. Concurrent calls to hid_plugin_connect() or
+ * hid_plugin_disconnect() are forbidden, see also the said methods' context.
+ *
+ * Return: Pointer to the private data of the HID plugin. NULL if the plugin has
+ * not been connected. ERR_PTR() if the plugin is disabled, disconnected early,
+ * or failed to connect.
+ */
+static __always_inline void *hid_plugin_to_priv(struct hid_device *hdev, enum hid_plugin_id id)
+{
+ if (!hid_plugin_is_enabled(id))
+ return ERR_PTR(-EOPNOTSUPP);
+
+ return hdev->plugins[id];
+}
+
+/**
+ * hid_plugin_connect() - Connect a plugin to an HID device
+ * @hdev: HID device
+ * @id: Unique ID of the HID plugin
+ *
+ * Connect the plugin to @hdev with the plugin's module refcount incremented.
+ * The module will be loaded when required. The plugin's private data is
+ * initialized on success.
+ *
+ * Must call hid_plugin_to_priv() in prior to try retrieving the plugin's
+ * private data, and only call this when it turns out to be not connected.
+ *
+ * Context: Process context. Can only be called while probing @hdev driver.
+ *
+ * Return: Pointer to the private data of the HID plugin, or ERR_PTR(). In the
+ * latter case, subsequent calls to hid_plugin_to_priv() also returns ERR_PTR()
+ * to prevent further attempts to connect.
+ */
+static __always_inline void *hid_plugin_connect(struct hid_device *hdev, enum hid_plugin_id id)
+{
+ if (!hid_plugin_is_enabled(id))
+ return ERR_PTR(-EOPNOTSUPP);
+
+ return __hid_plugin_connect(hdev, id);
+}
+
+/**
+ * hid_plugin_disconnect() - Disconnect a plugin from an HID device early
+ * @hdev: HID device
+ * @id: Unique ID of the HID plugin
+ *
+ * Disconnect a connected HID plugin from @hdev, and decrement the plugin's
+ * module refcount. Calling this when the plugin is not currently connected is a
+ * no-op, except that ERR_PTR(-EIDRM) is still propagated (see below).
+ *
+ * Usually plugins are automatically disconnected by the HID core. This is
+ * provided to disconnect a plugin to release resources early, e.g., when the
+ * plugin is unsuitable for @hdev. After disconnection, the plugin's private
+ * data is freed and any subsequent calls to hid_plugin_to_priv() will return
+ * ERR_PTR(-EIDRM) to prevent further attempts to connect.
+ *
+ * Context: Any context outside the plugin module. Can only be called while
+ * probing or removing @hdev driver.
+ */
+static __always_inline void hid_plugin_disconnect(struct hid_device *hdev, enum hid_plugin_id id)
+{
+ if (!hid_plugin_is_enabled(id))
+ return;
+
+ __hid_plugin_disconnect(hdev, id);
+}
+
+/**
+ * hid_plugin_cleanup() - Disconnect all plugins from an HID device
+ * @hdev: HID device
+ *
+ * Disconnect all connected HID plugins from @hdev. Unlike
+ * hid_plugin_disconnect(), this permits (or reenables) further connection
+ * attempts.
+ *
+ * Context: Any context. Called while HID core is removing @hdev driver.
+ * Plugins should use hid_plugin_disconnect() instead.
+ */
+static __always_inline void hid_plugin_cleanup(struct hid_device *hdev)
+{
+ if (!NR_HID_PLUGINS)
+ return;
+
+ __hid_plugin_cleanup(hdev);
+}
+
+/**
+ * hid_plugin_register() - Register a plugin
+ * @id: Unique ID of @plugin
+ * @plugin: Pointer to the HID plugin
+ *
+ * Context: Any context.
+ *
+ * Return: 0 on success, or an error.
+ */
+static __always_inline int hid_plugin_register(enum hid_plugin_id id, struct hid_plugin *plugin)
+{
+ BUILD_BUG_ON(!hid_plugin_is_enabled(id));
+
+ return __hid_plugin_register(id, plugin);
+}
+
+/**
+ * hid_plugin_unregister() - Unregister a plugin
+ * @id: Unique ID of @plugin
+ * @plugin: Pointer to the HID plugin
+ *
+ * Context: Process context.
+ */
+static __always_inline void hid_plugin_unregister(enum hid_plugin_id id, struct hid_plugin *plugin)
+{
+ BUILD_BUG_ON(!hid_plugin_is_enabled(id));
+
+ __hid_plugin_unregister(id, plugin);
+}
+
+/*
+ * Usage:
+ *
+ * struct hid_foo_plugin {
+ * struct hid_plugin hp;
+ * void (*bar)(struct hid_device *hdev);
+ * };
+ *
+ * static struct hid_foo_plugin hid_foo_plugin = {
+ * .hp = {
+ * .owner = THIS_MODULE,
+ * .connect = foo_plugin_connect,
+ * .disconnect = foo_plugin_disconnect,
+ * },
+ * .bar = hid_foo_bar,
+ * };
+ * module_hid_plugin(hid_foo, HID_PLUGIN_FOO, &hid_foo_plugin.hp);
+ *
+ * Must also update hid_plugin_id and hid_plugin_mod_names.
+ */
+#define module_hid_plugin(_name, _id, _plugin) \
+static int __init hid_plugin_##_name##_init(void) \
+{ \
+ return hid_plugin_register(_id, _plugin); \
+} \
+subsys_initcall(hid_plugin_##_name##_init); \
+static void __exit hid_plugin_##_name##_exit(void) \
+{ \
+ hid_plugin_unregister(_id, _plugin); \
+} \
+module_exit(hid_plugin_##_name##_exit)
+
+#endif /* __HID_PLUGIN_H */
diff --git a/include/linux/hid.h b/include/linux/hid.h
index 8d17b741638c..2444aabcb604 100644
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -15,6 +15,7 @@
#include <linux/bitops.h>
+#include <linux/build_bug.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/list.h>
@@ -22,6 +23,7 @@
#include <linux/timer.h>
#include <linux/workqueue.h>
#include <linux/input.h>
+#include <linux/kconfig.h>
#include <linux/semaphore.h>
#include <linux/mutex.h>
#include <linux/power_supply.h>
@@ -671,6 +673,16 @@ struct hid_battery {
struct hid_driver;
struct hid_ll_driver;
+enum hid_plugin_id {
+ HID_PLUGIN_DISABLED = -1,
+
+ NR_HID_PLUGINS,
+};
+
+#if !IS_ENABLED(CONFIG_HID_PLUGIN_CORE)
+static_assert(NR_HID_PLUGINS == 0);
+#endif
+
struct hid_device {
const __u8 *dev_rdesc; /* device report descriptor */
const __u8 *bpf_rdesc; /* bpf modified report descriptor, if any */
@@ -752,6 +764,8 @@ struct hid_device {
#ifdef CONFIG_HID_BPF
struct hid_bpf bpf; /* hid-bpf data */
#endif /* CONFIG_HID_BPF */
+
+ struct hid_plugin_private *plugins[NR_HID_PLUGINS];
};
void hiddev_free(struct kref *ref);
--
2.55.0