[PATCH v6 05/13] HID: Add hid-plugin framework for loadable plugin modules
From: Rong Zhang
Date: Wed Dec 02 2026 - 13:36:16 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.
Plugins can be either builtin or modules. A plugin module is requested
asynchronously when a HID device tries to use it. If the module is not
available yet, the device is probed without the plugin and reprobed once
the plugin registers itself. The reprobe procedure is inspired by commit
c17a7476e4c4 ("HID: core: rewrite the hid-generic automatic unbind").
Once a plugin is fully loaded (or if the plugin is builtin), subsequent
plugin connection attempts only require RCU dereferences and module
refcount increments, without the need of requesting a module.
The plugin's private data is automatically initialized on connection
with its pointer stored into hid_device. Disconnection also frees it
automatically. When the HID device is about to finish probe, unbound
plugins are automatically disconnected, and HID driver removal
disconnects all plugins. When a plugin turns out to be unsuitable for
the associated HID device, it can also be disconnected early to prevent
further use of the plugin.
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 v6:
- Use request_module_nowait() instead of request_module(), as the latter
can't be used within aysnc context where some i2c-hid drivers add
devices
- Disconnect unbound plugins automatically by consulting the
newly-introduced bound() callback, so that the following patch doesn't
need hid_haptic_disconnect() in hid-input
- Free plugins' private data in their disconnect() callback for correct
semantics, i.e., release resources wherever they were allocated
- Check mandatory callbacks on registration
- Add device_lock_assert() annotations
- Constify struct hid_plugin
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 | 17 +++
drivers/hid/Makefile | 2 +
drivers/hid/hid-core.c | 6 +
drivers/hid/hid-plugin.c | 301 +++++++++++++++++++++++++++++++++++++++++++++++
drivers/hid/hid-plugin.h | 257 ++++++++++++++++++++++++++++++++++++++++
include/linux/hid.h | 14 +++
7 files changed, 603 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index a5b2219bf508..cf24f0499ee2 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11908,6 +11908,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 51d31368125c..b500b1e392c8 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -92,6 +92,23 @@ 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 module is requested
+ asynchronously when a HID device tries to use it. If the module is not
+ available yet, the device is probed without the plugin and reprobed once
+ the plugin registers itself.
+
+ 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 2257eb2ee2e4..315e337b9ec7 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 ceff3c80d996..318ddb9c76cb 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -34,6 +34,7 @@
#include "hid-core.h"
#include "hid-ids.h"
+#include "hid-plugin.h"
#include "hidraw.h"
/*
@@ -2857,7 +2858,10 @@ 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;
+ } else {
+ hid_plugin_prune(hdev);
}
return ret;
@@ -2903,6 +2907,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;
}
@@ -3279,6 +3284,7 @@ static void __exit hid_exit(void)
#ifdef CONFIG_HID_BPF
hid_ops = NULL;
#endif
+ hid_plugin_exit();
hid_debug_exit();
hidraw_exit();
bus_unregister(&hid_bus_type);
diff --git a/drivers/hid/hid-plugin.c b/drivers/hid/hid-plugin.c
new file mode 100644
index 000000000000..63e4afca4f3a
--- /dev/null
+++ b/drivers/hid/hid-plugin.c
@@ -0,0 +1,301 @@
+// 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/bitmap.h>
+#include <linux/bug.h>
+#include <linux/cleanup.h>
+#include <linux/compiler.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/hid.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/rcupdate.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+#include <linux/workqueue.h>
+
+#include "hid-plugin.h"
+
+static const char * const hid_plugin_mod_names[NR_HID_PLUGINS] = {
+};
+
+static const struct hid_plugin __rcu *hid_plugins[NR_HID_PLUGINS];
+
+static const struct hid_plugin *hid_plugin_try_module_get(enum hid_plugin_id id)
+{
+ const struct hid_plugin *plugin;
+
+ guard(rcu)();
+
+ plugin = rcu_dereference(hid_plugins[id]);
+ if (!plugin)
+ return NULL;
+
+ /*
+ * If the module is unloading, defer the plugin without requesting the
+ * module again, so that the device can still be reprobed once the
+ * plugin module is somehow loaded by others.
+ */
+ return try_module_get(plugin->owner) ? plugin : ERR_PTR(-EAGAIN);
+}
+
+static const struct hid_plugin *hid_plugin_request(struct hid_device *hdev, enum hid_plugin_id id)
+{
+ const 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);
+
+ ret = request_module_nowait("%s", mod_name);
+
+ /*
+ * Errors could be transient, so always return -EAGAIN even on error.
+ * See also hid_plugin_try_module_get().
+ */
+ if (ret)
+ hid_warn(hdev, "Failed to request [%d]%s: %d\n", id, mod_name, ret);
+ else
+ hid_dbg(hdev, "Requested [%d]%s\n", id, mod_name);
+
+ /*
+ * There is a chance that the module may have been ready due to others
+ * loading the same module at the same time or reschedule latency. Retry
+ * optimistically, otherwise defer the plugin.
+ */
+ plugin = hid_plugin_try_module_get(id);
+
+ return plugin ?: ERR_PTR(-EAGAIN);
+}
+
+void *__hid_plugin_connect(struct hid_device *hdev, enum hid_plugin_id id)
+{
+ const struct hid_plugin *plugin;
+ void *priv;
+
+ device_lock_assert(&hdev->dev);
+
+ if (WARN_ON(hdev->plugins[id]))
+ return ERR_PTR(-EEXIST);
+
+ plugin = hid_plugin_request(hdev, id);
+ if (IS_ERR(plugin)) {
+ hdev->plugins[id] = ERR_CAST(plugin);
+ return ERR_CAST(plugin);
+ }
+
+ priv = plugin->connect(hdev);
+ if (WARN_ON(!priv))
+ priv = ERR_PTR(-ENOTCONN);
+ if (IS_ERR(priv)) {
+ module_put(plugin->owner);
+ hid_warn(hdev, "Failed to connect [%d]: %pe\n", id, priv);
+ } else {
+ hid_dbg(hdev, "Connected [%d]\n", id);
+ }
+
+ 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, bool force)
+{
+ const struct hid_plugin *plugin;
+
+ if (IS_ERR_OR_NULL(hdev->plugins[id])) {
+ if (force)
+ hdev->plugins[id] = ERR_PTR(err);
+ return;
+ }
+
+ /*
+ * The HID device still holds the plugin's module refcount and protects
+ * our access.
+ */
+ plugin = rcu_dereference_protected(hid_plugins[id], true);
+
+ if (!force && plugin->bound(hdev, hdev->plugins[id]))
+ return;
+
+ plugin->disconnect(hdev, hdev->plugins[id]);
+ hdev->plugins[id] = ERR_PTR(err);
+ module_put(plugin->owner);
+
+ hid_dbg(hdev, "Disconnected [%d]: %d\n", id, err);
+}
+
+void __hid_plugin_disconnect(struct hid_device *hdev, enum hid_plugin_id id)
+{
+ device_lock_assert(&hdev->dev);
+
+ hid_plugin_disconnect_with_err(hdev, id, -EIDRM, true);
+}
+EXPORT_SYMBOL_GPL(__hid_plugin_disconnect);
+
+void __hid_plugin_prune(struct hid_device *hdev)
+{
+ device_lock_assert(&hdev->dev);
+
+ for (int i = 0; i < NR_HID_PLUGINS; i++)
+ hid_plugin_disconnect_with_err(hdev, i, -EIDRM, false);
+}
+
+void __hid_plugin_cleanup(struct hid_device *hdev)
+{
+ device_lock_assert(&hdev->dev);
+
+ for (int i = 0; i < NR_HID_PLUGINS; i++)
+ hid_plugin_disconnect_with_err(hdev, i, 0, true);
+}
+
+static unsigned long hid_plugin_reprobe_mask;
+static_assert(NR_HID_PLUGINS > 0 && NR_HID_PLUGINS <= BITS_PER_LONG);
+
+static bool hid_plugin_is_deferred(struct hid_device *hdev, unsigned long *mask)
+{
+ int i;
+
+ /*
+ * Driver probe and exit may manipulate hdev->plugins. We need to wait
+ * until their values stabilize to prevent missing any deferral.
+ */
+ scoped_guard(device, &hdev->dev) {
+ for_each_set_bit(i, mask, NR_HID_PLUGINS) {
+ void *priv = hdev->plugins[i];
+
+ if (IS_ERR(priv) && PTR_ERR(priv) == -EAGAIN)
+ return true;
+ }
+ }
+
+ return false;
+}
+
+static int hid_plugin_reprobe_waiting(struct device *dev, void *data)
+{
+ struct hid_device *hdev = to_hid_device(dev);
+ unsigned long *mask = data;
+ int ret;
+
+ if (!hid_plugin_is_deferred(hdev, mask))
+ return 0;
+
+ /*
+ * In the small window here, the device lock is dropped and the device
+ * may be reprobed due to other reasons. The rare extra reprobe should
+ * be fine as long as no deferral is missed.
+ */
+
+ ret = device_reprobe(dev);
+ hid_dbg(hdev, "Reprobed due to deferred plugin registration: %d\n", ret);
+
+ /*
+ * Ignore errors. The device can still be used without the plugin, and
+ * a later plugin (re)registration or driver binding will retry probing.
+ */
+ return 0;
+}
+
+static void hid_plugin_reprobe_worker(struct work_struct *work)
+{
+ unsigned long mask = xchg(&hid_plugin_reprobe_mask, 0);
+ int ret, i;
+
+ /*
+ * Rule out unregistering plugins, otherwise temporarily prohibit
+ * module unloading until we've done.
+ */
+ for_each_set_bit(i, &mask, NR_HID_PLUGINS) {
+ ret = IS_ERR_OR_NULL(hid_plugin_try_module_get(i));
+ if (ret)
+ __clear_bit(i, &mask);
+ }
+
+ if (!mask)
+ return;
+
+ ret = bus_for_each_dev(&hid_bus_type, NULL, &mask, hid_plugin_reprobe_waiting);
+ if (ret)
+ pr_warn("Failed to scan devices for [%*pbl] registration: %d\n",
+ NR_HID_PLUGINS, &mask, ret);
+
+ for_each_set_bit(i, &mask, NR_HID_PLUGINS) {
+ /* Protected by module refcount. */
+ const struct hid_plugin *plugin = rcu_dereference_protected(hid_plugins[i], true);
+
+ module_put(plugin->owner);
+ }
+}
+
+#define REPROBE_DELAY msecs_to_jiffies(200)
+static DECLARE_DELAYED_WORK(hid_plugin_reprobe_dwork, hid_plugin_reprobe_worker);
+
+int __hid_plugin_register(enum hid_plugin_id id, const struct hid_plugin *plugin)
+{
+ if (WARN_ON(!plugin->connect || !plugin->disconnect || !plugin->bound))
+ return -EINVAL;
+
+ /*
+ * 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;
+
+ rcu_assign_pointer(hid_plugins[id], plugin);
+
+ /* Builtin plugins are registered before driver probe. */
+ if (plugin->owner) {
+ /*
+ * If an HID device happens to request multiple plugins, wait
+ * for a while to hopefully merge successive reprobe requests.
+ */
+ WARN_ON(test_and_set_bit(id, &hid_plugin_reprobe_mask));
+ mod_delayed_work(system_dfl_wq, &hid_plugin_reprobe_dwork, REPROBE_DELAY);
+ }
+
+ pr_debug("Registered [%d]\n", id);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(__hid_plugin_register);
+
+void __hid_plugin_unregister(enum hid_plugin_id id, const struct hid_plugin *plugin)
+{
+ if (WARN_ON(rcu_access_pointer(hid_plugins[id]) != plugin))
+ return;
+
+ clear_bit(id, &hid_plugin_reprobe_mask);
+
+ rcu_assign_pointer(hid_plugins[id], NULL);
+ synchronize_rcu();
+
+ pr_debug("Unregistered [%d]\n", id);
+}
+EXPORT_SYMBOL_GPL(__hid_plugin_unregister);
+
+void __hid_plugin_exit(void)
+{
+ disable_delayed_work_sync(&hid_plugin_reprobe_dwork);
+}
diff --git a/drivers/hid/hid-plugin.h b/drivers/hid/hid-plugin.h
new file mode 100644
index 000000000000..2d411e970285
--- /dev/null
+++ b/drivers/hid/hid-plugin.h
@@ -0,0 +1,257 @@
+/* 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/device.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 allocate and initialize the
+ * plugin's private data. Usually the private data stores a pointer
+ * to the plugin itself to expose plugin-specific methods. Context:
+ * process context during driver probe.
+ * @disconnect: Called on plugin disconnection to release all allocated
+ * resources. Must be safe to call no matter the plugin is
+ * currently bound or not. Context: process context during driver
+ * probe or removal.
+ * @bound: Called when driver probe is about to finish. Return true if the
+ * plugin has bound something to the HID device, otherwise the
+ * plugin framework will automatically disconnect the plugin.
+ * Context: process context during driver probe.
+ */
+struct hid_plugin {
+ struct module *owner;
+ void *(*connect)(struct hid_device *hdev);
+ void (*disconnect)(struct hid_device *hdev, void *priv);
+ bool (*bound)(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_prune(struct hid_device *hdev);
+void __hid_plugin_cleanup(struct hid_device *hdev);
+int __hid_plugin_register(enum hid_plugin_id id, const struct hid_plugin *plugin);
+void __hid_plugin_unregister(enum hid_plugin_id id, const struct hid_plugin *plugin);
+void __hid_plugin_exit(void);
+
+/**
+ * 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,
+ * failed to connect, or is just requested.
+ */
+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.
+ * If the plugin's module is not loaded yet, it is requested asynchronously and
+ * the device continues to probe without the plugin. The device is reprobed once
+ * the plugin registers, at which point the plugin is connected.
+ *
+ * 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 connection attempts.
+ */
+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 early to prevent further use of the plugin.
+ * After disconnection, the plugin's private data is freed and any subsequent
+ * calls to hid_plugin_to_priv() will return ERR_PTR(-EIDRM).
+ *
+ * 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_prune() - Disconnect unbound plugins from an HID device
+ * @hdev: HID device
+ *
+ * Disconnect all unbound HID plugins from @hdev. Similar to calling
+ * hid_plugin_disconnect() on every connected but unbound plugin. IOW, this does
+ * not affect any plugin that is not currently connected.
+ *
+ * Context: Any context. Called while HID core is about to finish @hdev probe.
+ * Plugins should use hid_plugin_disconnect() instead.
+ */
+static __always_inline void hid_plugin_prune(struct hid_device *hdev)
+{
+ if (!NR_HID_PLUGINS)
+ return;
+
+ __hid_plugin_prune(hdev);
+}
+
+/**
+ * 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
+ *
+ * This also reprobes all HID devices that have requested the plugin, so that
+ * they can connect to it.
+ *
+ * Context: Any context.
+ *
+ * Return: 0 on success, or an error.
+ */
+static __always_inline int hid_plugin_register(enum hid_plugin_id id,
+ const 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,
+ const struct hid_plugin *plugin)
+{
+ BUILD_BUG_ON(!hid_plugin_is_enabled(id));
+
+ __hid_plugin_unregister(id, plugin);
+}
+
+/**
+ * hid_plugin_exit() - Disable device reprobe on plugin registration
+ *
+ * Context: Any context. Called before removing the HID bus.
+ */
+static __always_inline void hid_plugin_exit(void)
+{
+ if (!NR_HID_PLUGINS)
+ return;
+
+ __hid_plugin_exit();
+}
+
+/*
+ * Usage:
+ *
+ * struct hid_foo_plugin {
+ * struct hid_plugin hp;
+ * void (*bar)(struct hid_device *hdev);
+ * };
+ *
+ * static const struct hid_foo_plugin hid_foo_plugin = {
+ * .hp = {
+ * .owner = THIS_MODULE,
+ * .connect = foo_plugin_connect,
+ * .disconnect = foo_plugin_disconnect,
+ * .bound = foo_plugin_bound,
+ * },
+ * .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 4b58e9b4ae43..f881f5631cfe 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