Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin
@ 2026-11-29 17:52 Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 01/13] HID: haptic: Get rid of input_ff_destroy() in error paths Rong Zhang
                   ` (12 more replies)
  0 siblings, 13 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang, stable

Several weeks ago, I bought a Logitech MX Master 4 with haptic feedback.
The hid-haptic already supports the Haptic usage page, but it only works
with hid-multitouch. I am also working on adding support for the
LampArray usage page recently [1].

The two drivers have a common characteristic, i.e., they targets
specific HID usage pages instead of specific devices. This leads to
additional linkage dependences because the device drivers need to call
functions provided by the usage page drivers.

Logically, the dependence should be reversed, i.e., a usage page driver
should be an optional component of the associated device driver and is
only loaded by the device 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 in 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.

With the framework, convert hid-haptic to the first plugin. After the
conversion, it connects to hid-input and becomes a generic plugin.

The first few patches fix pre-existing bugs in hid-haptic and
hid-multitouch. Then the later patches refactor hid-haptic, implement
the hid-plugin framework and finish the conversion.

I volunteer to maintain the framework as well as its plugins, including
hid-haptic and future ones.

Thanks Benjamin Tissoires for the inspiration for the implementation.

[1]: I know Texudo folks are working on adding hid-lamparray to connect
it to hid-generic. I am not in favor of their design, so I decided to
write a new one. In my local tree, I've had an independent
implementation with HIDRAW sniff, autonomous mode as an LED trigger,
RGB/non-RGB LampArray support, per-key LED device support, etc. I will
submit it once this series settles.

Suggested-by: Benjamin Tissoires <bentiss@kernel.org>
Link: https://lore.kernel.org/linux-input/auypydfkhx2eg7vp764way4batdilzc35inqda3exwzs3tk3ff@oagat6g46zto/
Signed-off-by: Rong Zhang <i@rong.moe>
---
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 selects it, so that olddefconfig preserves HID_HAPTIC's
    old value
- Connect hid-haptic to hid-inpput
- Convert hid-haptic to a generic plugin
- Add a quirk flag for Logitech MX Master 4
- Link to v4: https://patch.msgid.link/20261125-hid-haptic-kmod-v4-0-03713a89f656@rong.moe

Changes in v4:
- Drop get_device()/put_device() and try_module_get()/module_put()
  completely, as they are needless now (thanks Dmitry Torokhov)
- Drop synchronize_rcu() during registration as other drivers do, as
  there is nothing to protect
- Link to v3: https://patch.msgid.link/20261113-hid-haptic-kmod-v3-0-e7dfa8766c0d@rong.moe

Changes in v3:
- Correct the Fixes: tag of PATCH 4 (thanks Sashiko)
- Add missing Return: to hid_plugin_register()'s kernel-doc (ditto)
- Fix the return value of hid_haptic_connected_to_input() when
  hid-haptic is disabled (ditto)
- Link to v2: https://patch.msgid.link/20261110-hid-haptic-kmod-v2-0-b835a067cb62@rong.moe

Changes in v2:
- Reject touchpad applications with non-force units (thanks Sashiko)
- Only call runtime methods if the event source is the same input device
  as the stored one (ditto)
- Rebase on top of v7.4-rc2 due to the devm_kzalloc_obj() changes
- Link to v1: https://patch.msgid.link/20261102-hid-haptic-kmod-v1-0-a3de5626dade@rong.moe

---
Rong Zhang (13):
      HID: haptic: Get rid of input_ff_destroy() in error paths
      HID: haptic: Fix use-after-free when draining workqueue
      HID: haptic, multitouch: Check haptic prerequisite properly
      HID: haptic: Postpone GET_REPORT to init
      HID: Add hid-plugin framework for loadable plugin modules
      HID: haptic: Convert to a loadable plugin module
      HID: haptic: Adopt pr_fmt, dev_fmt and hid_*() printk helpers
      HID: Add Ordinal page defines
      HID: haptic: Handle list reports separately and make auto trigger optional
      HID: haptic: Decouple from hid-multitouch and support other devices
      HID: haptic: Add quirk table
      HID: haptic: Add HID_HAPTIC_QUIRK_NO_RETRIGGER to disable retriggering
      HID: haptic: Add quirk flag for Logitech MX Master 4

 MAINTAINERS                  |   7 +
 drivers/hid/Kconfig          |  24 ++-
 drivers/hid/Makefile         |   4 +-
 drivers/hid/hid-core.c       |   3 +
 drivers/hid/hid-haptic.c     | 444 ++++++++++++++++++++++++++++---------------
 drivers/hid/hid-haptic.h     | 181 ++++++++++++------
 drivers/hid/hid-input.c      |  50 ++++-
 drivers/hid/hid-multitouch.c |  41 +---
 drivers/hid/hid-plugin.c     | 172 +++++++++++++++++
 drivers/hid/hid-plugin.h     | 208 ++++++++++++++++++++
 include/linux/hid.h          |  25 +++
 11 files changed, 906 insertions(+), 253 deletions(-)
---
base-commit: 4f069ab1ec243e6b60dc438772905855c60774e8
change-id: 9644469a-hid-haptic-kmod-4e8e420f8e5f

Thanks,
Rong


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v5 01/13] HID: haptic: Get rid of input_ff_destroy() in error paths
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 02/13] HID: haptic: Fix use-after-free when draining workqueue Rong Zhang
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang, stable

It's painful to have error paths after calling input_ff_create(),
because input_ff_destroy() frees too much things but does not restore
some other things. For example, every error path calling
input_ff_destroy() frees ff->private, while the others don't. This
divergence has caused quite a lot of trouble.

Additionally, the driver's get_device() bail-out path calls module_put()
right before hid_haptic_destroy() calling it again, causing refcount
underflow.

Since commit 2ae35f090d02 ("Input: destroy force-feedback device on
input unregistration"), the HID device is now guaranteed to have a
longer lifetime than the FF device, making get_device()/put_device()
unnecessary.

Due to the driver being unable to build as a module since commit
083a4f3f3cc7 ("HID: Kconfig: Fix build error from CONFIG_HID_HAPTIC"),
try_module_get()/module_put() is needless, too.

Remove the said calls, so that there is no error path after
input_ff_create(), eliminating the call to input_ff_destroy(). In this
manner, we don't need to restore dev->flush and dev->event either.

Fixes: 344ff3584957 ("HID: haptic: initialize haptic device")
Cc: stable@vger.kernel.org
Suggested-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Link: https://msgid.link/f44447354f28c295e79a025bbcab7a4e@gmail.com
Signed-off-by: Rong Zhang <i@rong.moe>
---
Changes in v4:
- Drop get_device()/put_device() and try_module_get()/module_put()
  completely, as they are needless now (thanks Dmitry Torokhov)
---
 drivers/hid/hid-haptic.c | 25 -------------------------
 1 file changed, 25 deletions(-)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index b7fadfbddc4c..c675e14f06e1 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -380,12 +380,8 @@ static int hid_haptic_erase(struct input_dev *dev, int effect_id)
 static void hid_haptic_destroy(struct ff_device *ff)
 {
 	struct hid_haptic_device *haptic = ff->private;
-	struct hid_device *hdev = haptic->hdev;
 	int r;
 
-	if (hdev)
-		put_device(&hdev->dev);
-
 	kfree(haptic->stop_effect.report_buf);
 	haptic->stop_effect.report_buf = NULL;
 
@@ -404,8 +400,6 @@ static void hid_haptic_destroy(struct ff_device *ff)
 
 	kfree(haptic->hid_usage_map);
 	haptic->hid_usage_map = NULL;
-
-	module_put(THIS_MODULE);
 }
 
 int hid_haptic_init(struct hid_device *hdev,
@@ -419,8 +413,6 @@ int hid_haptic_init(struct hid_device *hdev,
 	};
 	const char *prefix = "hid-haptic";
 	char *name;
-	int (*flush)(struct input_dev *dev, struct file *file);
-	int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
 
 	haptic->hdev = hdev;
 	haptic->max_waveform_id = max(2u, haptic->max_waveform_id);
@@ -499,8 +491,6 @@ int hid_haptic_init(struct hid_device *hdev,
 
 	input_set_capability(dev, EV_FF, FF_HAPTIC);
 
-	flush = dev->flush;
-	event = dev->event;
 	ret = input_ff_create(dev, FF_MAX_EFFECTS);
 	if (ret) {
 		dev_err(&hdev->dev, "Failed to create ff device.\n");
@@ -513,23 +503,8 @@ int hid_haptic_init(struct hid_device *hdev,
 	ff->playback = hid_haptic_playback;
 	ff->erase = hid_haptic_erase;
 	ff->destroy = hid_haptic_destroy;
-	if (!try_module_get(THIS_MODULE)) {
-		dev_err(&hdev->dev, "Failed to increase module count.\n");
-		goto input_free;
-	}
-	if (!get_device(&hdev->dev)) {
-		dev_err(&hdev->dev, "Failed to get hdev device.\n");
-		module_put(THIS_MODULE);
-		goto input_free;
-	}
 	return 0;
 
-input_free:
-	input_ff_destroy(dev);
-	/* Restore dev flush and event */
-	dev->flush = flush;
-	dev->event = event;
-	return ret;
 stop_buffer_free:
 	kfree(haptic->stop_effect.report_buf);
 	haptic->stop_effect.report_buf = NULL;

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 02/13] HID: haptic: Fix use-after-free when draining workqueue
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 01/13] HID: haptic: Get rid of input_ff_destroy() in error paths Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 03/13] HID: haptic, multitouch: Check haptic prerequisite properly Rong Zhang
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang, stable

The haptic->wq is destroyed after freeing effects and their report
buffers, which is too late. If any work is still pending or running
before the workqueue destruction, haptic_work_handler() will access the
just-freed contents, causing a use-after-free.

Fix it by destroying the workqueue before freeing haptic resources. The
allocation sequence is also adjusted to the reverse.

Since the workqueue allocation is anyway touched, also replace the
legacy creation API with the modern alloc_ordered_workqueue(). In this
manner, the temporary buffer on heap can be replaced by the API's
formatting.

Also call __clear_bit(EV_FF, dev->evbit) in the bail-out path to match
the behavior of input_ff_destroy().

Fixes: 344ff3584957 ("HID: haptic: initialize haptic device")
Cc: stable@vger.kernel.org
Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/hid/hid-haptic.c | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index c675e14f06e1..8312b8edc847 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -382,6 +382,9 @@ static void hid_haptic_destroy(struct ff_device *ff)
 	struct hid_haptic_device *haptic = ff->private;
 	int r;
 
+	destroy_workqueue(haptic->wq);
+	haptic->wq = NULL;
+
 	kfree(haptic->stop_effect.report_buf);
 	haptic->stop_effect.report_buf = NULL;
 
@@ -392,9 +395,6 @@ static void hid_haptic_destroy(struct ff_device *ff)
 	}
 	haptic->effect = NULL;
 
-	destroy_workqueue(haptic->wq);
-	haptic->wq = NULL;
-
 	kfree(haptic->duration_map);
 	haptic->duration_map = NULL;
 
@@ -411,8 +411,6 @@ int hid_haptic_init(struct hid_device *hdev,
 	struct ff_haptic_effect stop_effect = {
 		.hid_usage = HID_HP_WAVEFORMSTOP & HID_USAGE,
 	};
-	const char *prefix = "hid-haptic";
-	char *name;
 
 	haptic->hdev = hdev;
 	haptic->max_waveform_id = max(2u, haptic->max_waveform_id);
@@ -447,20 +445,11 @@ int hid_haptic_init(struct hid_device *hdev,
 	haptic->manual_trigger_report_len =
 		hid_report_len(haptic->manual_trigger_report);
 	mutex_init(&haptic->manual_trigger_mutex);
-	name = kmalloc(strlen(prefix) + strlen(hdev->name) + 2, GFP_KERNEL);
-	if (name) {
-		sprintf(name, "%s %s", prefix, hdev->name);
-		haptic->wq = create_singlethread_workqueue(name);
-		kfree(name);
-	}
-	if (!haptic->wq) {
-		ret = -ENOMEM;
-		goto duration_map;
-	}
+
 	haptic->effect = kzalloc_objs(struct hid_haptic_effect, FF_MAX_EFFECTS);
 	if (!haptic->effect) {
 		ret = -ENOMEM;
-		goto output_queue;
+		goto duration_map;
 	}
 	for (r = 0; r < FF_MAX_EFFECTS; r++) {
 		haptic->effect[r].report_buf =
@@ -489,12 +478,18 @@ int hid_haptic_init(struct hid_device *hdev,
 	fill_effect_buf(haptic, &stop_effect, &haptic->stop_effect,
 			HID_HAPTIC_ORDINAL_WAVEFORMSTOP);
 
+	haptic->wq = alloc_ordered_workqueue("hid-haptic %s", WQ_MEM_RECLAIM, hdev->name);
+	if (!haptic->wq) {
+		ret = -ENOMEM;
+		goto stop_buffer_free;
+	}
+
 	input_set_capability(dev, EV_FF, FF_HAPTIC);
 
 	ret = input_ff_create(dev, FF_MAX_EFFECTS);
 	if (ret) {
 		dev_err(&hdev->dev, "Failed to create ff device.\n");
-		goto stop_buffer_free;
+		goto reset_evbit;
 	}
 
 	ff = dev->ff;
@@ -505,6 +500,10 @@ int hid_haptic_init(struct hid_device *hdev,
 	ff->destroy = hid_haptic_destroy;
 	return 0;
 
+reset_evbit:
+	__clear_bit(EV_FF, dev->evbit);
+	destroy_workqueue(haptic->wq);
+	haptic->wq = NULL;
 stop_buffer_free:
 	kfree(haptic->stop_effect.report_buf);
 	haptic->stop_effect.report_buf = NULL;
@@ -513,9 +512,6 @@ int hid_haptic_init(struct hid_device *hdev,
 		kfree(haptic->effect[r].report_buf);
 	kfree(haptic->effect);
 	haptic->effect = NULL;
-output_queue:
-	destroy_workqueue(haptic->wq);
-	haptic->wq = NULL;
 duration_map:
 	kfree(haptic->duration_map);
 	haptic->duration_map = NULL;

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 03/13] HID: haptic, multitouch: Check haptic prerequisite properly
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 01/13] HID: haptic: Get rid of input_ff_destroy() in error paths Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 02/13] HID: haptic: Fix use-after-free when draining workqueue Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 18:10   ` sashiko-bot
  2026-11-29 17:52 ` [PATCH v5 04/13] HID: haptic: Postpone GET_REPORT to init Rong Zhang
                   ` (9 subsequent siblings)
  12 siblings, 1 reply; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang, stable

The td->is_haptic_touchpad boolean is multiplexed through during the
probe- and run-time procedures of hid-multitouch. However, the way it
flips is fragile on faulty devices.

MT touchpads have multiple HID_DG_TIPPRESSURE usages, but the value of
td->is_haptic_touchpad only depends on the last usage, and doesn't check
if all usages have the same critical annotations. Moreover, if the HID
device happens to have multiple HID_DG_TOUCHPAD applications, the haptic
device may be initialized more than once.

Reject annotation mismatches and duplicated HID_DG_TOUCHPAD input
devices to address the first issue. The annotation mismatch check only
compares unit, logical_minimum, and physical_minimum to adhere to the
previous intent as much as possible.

The relevant HID_DG_TOUCHPAD input device are cached when the check is
passed for the first time. Any check failures override it and gate
further checks, so the last call to hid_haptic_check_pressure_unit() can
precisely return the final result.

Later, hid_haptic_input_configured() continues the initialization only
if the input device matches the previously chosen one.

With these, the td->is_haptic_touchpad is converted into a one-way
(false => true) flipping manner. The MT_CLS check is also removed in
favor of the prerequisite checks.

At runtime, relevant haptic methods are called only if the event source
is the same input device as the chosen one.

Fixes: 7939f787f450 ("HID: haptic: move FF initialization into .input_configured()")
Cc: stable@vger.kernel.org
Signed-off-by: Rong Zhang <i@rong.moe>
---
Changes in v5:
- Remove the MT_CLS check
- Prepare for following changes
  - Return error code in hid_haptic_input_configured()
  - Return bool in hid_haptic_check_pressure_unit()
  - Move hid_haptic_input_configured() under hid_haptic_init()
  - Factor out hid_haptic_check_prerequisites()
  - Set INPUT_PROP_PRESSUREPAD only for the HID_DG_TOUCHPAD application

Changes in v2:
- Reject touchpad applications with non-force units (thanks Sashiko)
- Only call runtime methods if the event source is the same input device
  as the stored one (ditto)
---
 drivers/hid/hid-haptic.c     | 96 ++++++++++++++++++++++++++++++--------------
 drivers/hid/hid-haptic.h     | 17 ++++++--
 drivers/hid/hid-multitouch.c | 16 +++-----
 3 files changed, 85 insertions(+), 44 deletions(-)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index 8312b8edc847..06d1785a7d8c 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -51,13 +51,38 @@ EXPORT_SYMBOL_GPL(hid_haptic_feature_mapping);
 bool hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
 				    struct hid_input *hi, struct hid_field *field)
 {
-	if (field->unit == HID_UNIT_GRAM || field->unit == HID_UNIT_NEWTON) {
-		haptic->force_logical_minimum = field->logical_minimum;
-		haptic->force_physical_minimum = field->physical_minimum;
-		haptic->force_resolution = input_abs_get_res(hi->input,
-							     ABS_MT_PRESSURE);
+	if (IS_ERR(haptic->input_dev))
+		return false;
+
+	/* Don't care, return the cache result. */
+	if (hi->application != HID_DG_TOUCHPAD)
+		return !!haptic->input_dev;
+
+	if (field->unit != HID_UNIT_GRAM && field->unit != HID_UNIT_NEWTON)
+		goto reject;
+
+	if (!haptic->input_dev) {
+		haptic->input_dev = hi->input;
+		haptic->pressure_field = field;
+		haptic->force_resolution = input_abs_get_res(hi->input, ABS_MT_PRESSURE);
 		return true;
 	}
+
+	if (haptic->input_dev == hi->input &&
+	    haptic->pressure_field->unit == field->unit &&
+	    haptic->pressure_field->logical_minimum == field->logical_minimum &&
+	    haptic->pressure_field->physical_minimum == field->physical_minimum &&
+	    haptic->force_resolution == input_abs_get_res(hi->input, ABS_MT_PRESSURE))
+		return true;
+
+reject:
+	/*
+	 * Reject if:
+	 * - there are multiple input devices with pressure sensors
+	 * - there is any touchpad application with a non-force unit
+	 * - pressure annotations mismatch among HID_DG_TIPPRESSURE usages
+	 */
+	haptic->input_dev = ERR_PTR(-EPROTO);
 	return false;
 }
 EXPORT_SYMBOL_GPL(hid_haptic_check_pressure_unit);
@@ -78,31 +103,6 @@ int hid_haptic_input_mapping(struct hid_device *hdev,
 }
 EXPORT_SYMBOL_GPL(hid_haptic_input_mapping);
 
-int hid_haptic_input_configured(struct hid_device *hdev,
-				struct hid_haptic_device *haptic,
-				struct hid_input *hi)
-{
-	int error;
-
-	if (hi->application != HID_DG_TOUCHPAD)
-		return -1;
-
-	if (!haptic->auto_trigger_report || !haptic->manual_trigger_report)
-		return 0;
-
-	__set_bit(INPUT_PROP_PRESSUREPAD, hi->input->propbit);
-
-	error = hid_haptic_init(hdev, haptic, hi->input);
-	if (error) {
-		dev_warn(&hdev->dev, "Cannot allocate haptic for %s\n",
-			 hdev->name);
-		return 0;
-	}
-
-	return 1;
-}
-EXPORT_SYMBOL_GPL(hid_haptic_input_configured);
-
 static void parse_auto_trigger_field(struct hid_haptic_device *haptic,
 				     struct hid_field *field)
 {
@@ -519,10 +519,46 @@ int hid_haptic_init(struct hid_device *hdev,
 	kfree(haptic->hid_usage_map);
 	haptic->hid_usage_map = NULL;
 exit:
+	hid_warn(hdev, "Cannot allocate haptic for %s: %d\n", hdev->name, ret);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(hid_haptic_init);
 
+static int hid_haptic_check_prerequisites(struct hid_device *hdev,
+					  struct hid_haptic_device *haptic, struct hid_input *hi)
+{
+	if (!haptic->auto_trigger_report || !haptic->manual_trigger_report)
+		return -ENOENT;
+
+	if (IS_ERR_OR_NULL(haptic->input_dev))
+		return PTR_ERR(haptic->input_dev) ?: -EXDEV;
+
+	if (haptic->input_dev == hi->input)
+		return 0;
+
+	return 1; /* Waiting for the relevant input device. */
+}
+
+int hid_haptic_input_configured(struct hid_device *hdev,
+				struct hid_haptic_device *haptic, struct hid_input *hi)
+{
+	int ret;
+
+	ret = hid_haptic_check_prerequisites(hdev, haptic, hi);
+	if (ret)
+		return ret;
+
+	ret = hid_haptic_init(hdev, haptic, hi->input);
+	if (ret)
+		return ret;
+
+	if (hi->application == HID_DG_TOUCHPAD)
+		__set_bit(INPUT_PROP_PRESSUREPAD, hi->input->propbit);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(hid_haptic_input_configured);
+
 void hid_haptic_pressure_reset(struct hid_haptic_device *haptic)
 {
 	haptic->pressure_sum = 0;
diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index 6332991a7844..e39104f75229 100644
--- a/drivers/hid/hid-haptic.h
+++ b/drivers/hid/hid-haptic.h
@@ -33,12 +33,11 @@ struct hid_haptic_device {
 	struct mutex auto_trigger_mutex;
 	struct workqueue_struct *wq;
 	struct hid_report *manual_trigger_report;
+	struct hid_field *pressure_field;
 	struct mutex manual_trigger_mutex;
 	size_t manual_trigger_report_len;
 	int pressed_state;
 	s32 pressure_sum;
-	s32 force_logical_minimum;
-	s32 force_physical_minimum;
 	s32 force_resolution;
 	u32 mode;
 	u32 default_auto_trigger;
@@ -55,6 +54,12 @@ struct hid_haptic_device {
 };
 
 #if IS_ENABLED(CONFIG_HID_HAPTIC)
+static inline bool hid_haptic_connected_to_input(struct hid_haptic_device *haptic,
+						 struct input_dev *input)
+{
+	return haptic->input_dev == input;
+}
+
 void hid_haptic_feature_mapping(struct hid_device *hdev,
 				struct hid_haptic_device *haptic,
 				struct hid_field *field, struct hid_usage
@@ -76,6 +81,12 @@ void hid_haptic_pressure_reset(struct hid_haptic_device *haptic);
 void hid_haptic_pressure_increase(struct hid_haptic_device *haptic,
 				  __s32 pressure);
 #else
+static inline bool hid_haptic_connected_to_input(struct hid_haptic_device *haptic,
+						 struct input_dev *input)
+{
+	return false;
+}
+
 static inline
 void hid_haptic_feature_mapping(struct hid_device *hdev,
 				struct hid_haptic_device *haptic,
@@ -102,7 +113,7 @@ int hid_haptic_input_configured(struct hid_device *hdev,
 				struct hid_haptic_device *haptic,
 				struct hid_input *hi)
 {
-	return 0;
+	return -EOPNOTSUPP;
 }
 static inline
 void hid_haptic_reset(struct hid_device *hdev, struct hid_haptic_device *haptic)
diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index f63d6f6550bd..e017eff9b784 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -1085,7 +1085,7 @@ static void mt_sync_frame(struct mt_device *td, struct mt_application *app,
 
 	app->num_received = 0;
 	app->left_button_state = 0;
-	if (td->is_haptic_touchpad)
+	if (td->is_haptic_touchpad && hid_haptic_connected_to_input(td->haptic, input))
 		hid_haptic_pressure_reset(td->haptic);
 }
 
@@ -1238,7 +1238,7 @@ static int mt_process_slot(struct mt_device *td, struct input_dev *input,
 			minor = minor >> 1;
 		}
 
-		if (td->is_haptic_touchpad)
+		if (td->is_haptic_touchpad && hid_haptic_connected_to_input(td->haptic, input))
 			hid_haptic_pressure_increase(td->haptic, *slot->p);
 
 		x = hdev->quirks & HID_QUIRK_X_INVERT ?
@@ -1441,7 +1441,7 @@ static int mt_touch_input_configured(struct hid_device *hdev,
 	if (cls->is_indirect)
 		app->mt_flags |= INPUT_MT_POINTER;
 
-	if (td->is_haptic_touchpad)
+	if (td->is_haptic_touchpad && hid_haptic_connected_to_input(td->haptic, input))
 		app->mt_flags |= INPUT_MT_TOTAL_FORCE;
 
 	if (app->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
@@ -1954,14 +1954,8 @@ static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
 	struct hid_report *report;
 	int ret;
 
-	if (td->is_haptic_touchpad && (td->mtclass.name == MT_CLS_WIN_8 ||
-	    td->mtclass.name == MT_CLS_WIN_8_FORCE_MULTI_INPUT ||
-	    td->mtclass.name == MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE)) {
-		if (hid_haptic_input_configured(hdev, td->haptic, hi) == 0)
-			td->is_haptic_touchpad = false;
-	} else {
-		td->is_haptic_touchpad = false;
-	}
+	if (!td->is_haptic_touchpad)
+		td->is_haptic_touchpad = !hid_haptic_input_configured(hdev, td->haptic, hi);
 
 	list_for_each_entry(report, &hi->reports, hidinput_list) {
 		rdata = mt_find_report_data(td, report);

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 04/13] HID: haptic: Postpone GET_REPORT to init
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
                   ` (2 preceding siblings ...)
  2026-11-29 17:52 ` [PATCH v5 03/13] HID: haptic, multitouch: Check haptic prerequisite properly Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 05/13] HID: Add hid-plugin framework for loadable plugin modules Rong Zhang
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang

Currently, the driver calls GET_REPORT within the feature_mapping
callback. This is not optimal, as the driver works only if manual
trigger is also available and the if the device is a touchpad. It makes
no sense to initiate device IO if the device is not supported by the
driver.

Hence, postpone GET_REPORT to hid_haptic_init(), and parse default
trigger in parse_auto_trigger_field() where many fields are already
parsed there.

Signed-off-by: Rong Zhang <i@rong.moe>
---
Changes in v5:
- Factor out a helper function
---
 drivers/hid/hid-haptic.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index 06d1785a7d8c..2e9631fa2f69 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -22,13 +22,6 @@ void hid_haptic_feature_mapping(struct hid_device *hdev,
 				"HID_HP_AUTOTRIGGER out of range\n");
 			return;
 		}
-
-		hid_device_io_start(hdev);
-		hid_hw_request(hdev, field->report, HID_REQ_GET_REPORT);
-		hid_hw_wait(hdev);
-		hid_device_io_stop(hdev);
-		haptic->default_auto_trigger =
-			field->value[usage->usage_index];
 		haptic->auto_trigger_report = field->report;
 	} else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ORDINAL) {
 		usage_hid = usage->hid & HID_USAGE;
@@ -135,6 +128,9 @@ static void parse_auto_trigger_field(struct hid_haptic_device *haptic,
 			break;
 		case HID_UP_HAPTIC:
 			switch (field->usage[n].hid) {
+			case HID_HP_AUTOTRIGGER:
+				haptic->default_auto_trigger = field->value[n];
+				break;
 			case HID_HP_WAVEFORMVENDORID:
 				haptic->vendor_id = field->value[n];
 				break;
@@ -402,6 +398,16 @@ static void hid_haptic_destroy(struct ff_device *ff)
 	haptic->hid_usage_map = NULL;
 }
 
+static void hid_haptic_get_reports(struct hid_device *hdev, struct hid_haptic_device *haptic)
+{
+	hid_device_io_start(hdev);
+
+	hid_hw_request(hdev, haptic->auto_trigger_report, HID_REQ_GET_REPORT);
+
+	hid_hw_wait(hdev);
+	hid_device_io_stop(hdev);
+}
+
 int hid_haptic_init(struct hid_device *hdev,
 		    struct hid_haptic_device *haptic,
 		    struct input_dev *dev)
@@ -438,6 +444,9 @@ int hid_haptic_init(struct hid_device *hdev,
 		HID_HP_WAVEFORMSTOP & HID_USAGE;
 
 	mutex_init(&haptic->auto_trigger_mutex);
+
+	hid_haptic_get_reports(hdev, haptic);
+
 	for (r = 0; r < haptic->auto_trigger_report->maxfield; r++)
 		parse_auto_trigger_field(haptic, haptic->auto_trigger_report->field[r]);
 

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 05/13] HID: Add hid-plugin framework for loadable plugin modules
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
                   ` (3 preceding siblings ...)
  2026-11-29 17:52 ` [PATCH v5 04/13] HID: haptic: Postpone GET_REPORT to init Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 06/13] HID: haptic: Convert to a loadable plugin module Rong Zhang
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang

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@kernel.org>
Link: https://lore.kernel.org/linux-input/auypydfkhx2eg7vp764way4batdilzc35inqda3exwzs3tk3ff@oagat6g46zto/
Signed-off-by: Rong Zhang <i@rong.moe>
---
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@vger.kernel.org
 S:	Supported
 F:	drivers/hid/hid-playstation.c
 
+HID PLUGINS
+M:	Rong Zhang <i@rong.moe>
+L:	linux-input@vger.kernel.org
+S:	Maintained
+F:	drivers/hid/hid-plugin.*
+
 HID PULSAR DRIVER
 M:	Nikolas Koesling <nikolas@koesling.info>
 L:	linux-input@vger.kernel.org
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@rong.moe>
+ */
+
+#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@rong.moe>
+ */
+
+#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


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 06/13] HID: haptic: Convert to a loadable plugin module
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
                   ` (4 preceding siblings ...)
  2026-11-29 17:52 ` [PATCH v5 05/13] HID: Add hid-plugin framework for loadable plugin modules Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 07/13] HID: haptic: Adopt pr_fmt, dev_fmt and hid_*() printk helpers Rong Zhang
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang

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, convert hid-haptic into a loadable
plugin module, which is only requested on demand.

The plugin provides complicated methods as callbacks, which can be
called by using the associated static inline wrappers. Simple methods
are moved to the header file as static inline ones, too.

The plugin now connects to hid-input instead of hid-multitouch, only
leaving several hooks in the latter. Despite that, it still only works
on hid-multitouch devices. The limitation will be lifted in the future.

If the HID driver provides EV_FF itself or ask the HID core not to
register FF devices, no haptic FF device is registered. Otherwise,
prefer hid-haptic over hid-pidff.

When hid-haptic is disabled, the hid-plugin framework's helper functions
will ensure that all calls to its methods are subject to dead code
elimination during compilation.

The redaundent `default n' statement in Kconfig is also removed for
tidiness since we are anyway touching its context.

Suggested-by: Benjamin Tissoires <bentiss@kernel.org>
Link: https://lore.kernel.org/linux-input/auypydfkhx2eg7vp764way4batdilzc35inqda3exwzs3tk3ff@oagat6g46zto/
Signed-off-by: Rong Zhang <i@rong.moe>
---
Changes in v5:
- Adopt the new hid-plugin design
- Connect to hid-input
- Alloc hid_haptic_device in hid_haptic_feature_mapping()
- Don't move complicated methods to the header file
---
 MAINTAINERS                  |   1 +
 drivers/hid/Kconfig          |   8 +-
 drivers/hid/Makefile         |   2 +-
 drivers/hid/hid-haptic.c     |  82 +++++++++++----------
 drivers/hid/hid-haptic.h     | 172 ++++++++++++++++++++++++++-----------------
 drivers/hid/hid-input.c      |  50 ++++++++++++-
 drivers/hid/hid-multitouch.c |  35 ++-------
 drivers/hid/hid-plugin.c     |   3 +
 include/linux/hid.h          |   8 +-
 9 files changed, 221 insertions(+), 140 deletions(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index 6b2a4a96a333..b09007428c43 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11906,6 +11906,7 @@ HID PLUGINS
 M:	Rong Zhang <i@rong.moe>
 L:	linux-input@vger.kernel.org
 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 f9f4fb4de986..80b950eb54d9 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -105,12 +105,14 @@ menuconfig HID_PLUGIN
 
 	If unsure, say Y.
 
+if HID_PLUGIN
+
 config HID_PLUGIN_CORE
 	bool
 
 config HID_HAPTIC
-	bool "Haptic touchpad support"
-	default n
+	tristate "Haptic touchpad support"
+	select HID_PLUGIN_CORE
 	help
 	Support for touchpads with force sensors and haptic actuators instead of a
 	traditional button.
@@ -119,6 +121,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 b39ccc9a360b..29b983517ac0 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_CORE)	+= 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 2e9631fa2f69..c8c1c7fddaa6 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -5,14 +5,14 @@
  *  Copyright (c) 2021 Angela Czubak <acz@semihalf.com>
  */
 
+#include <linux/err.h>
 #include <linux/input/mt.h>
 #include <linux/module.h>
 
 #include "hid-haptic.h"
 
-void hid_haptic_feature_mapping(struct hid_device *hdev,
-				struct hid_haptic_device *haptic,
-				struct hid_field *field, struct hid_usage *usage)
+static void __hid_haptic_feature_mapping(struct hid_device *hdev, struct hid_haptic_device *haptic,
+					 struct hid_field *field, struct hid_usage *usage)
 {
 	u16 usage_hid;
 
@@ -39,10 +39,9 @@ void hid_haptic_feature_mapping(struct hid_device *hdev,
 		}
 	}
 }
-EXPORT_SYMBOL_GPL(hid_haptic_feature_mapping);
 
-bool hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
-				    struct hid_input *hi, struct hid_field *field)
+static bool __hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
+					     struct hid_input *hi, struct hid_field *field)
 {
 	if (IS_ERR(haptic->input_dev))
 		return false;
@@ -78,23 +77,6 @@ bool hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
 	haptic->input_dev = ERR_PTR(-EPROTO);
 	return false;
 }
-EXPORT_SYMBOL_GPL(hid_haptic_check_pressure_unit);
-
-int hid_haptic_input_mapping(struct hid_device *hdev,
-			     struct hid_haptic_device *haptic,
-			     struct hid_input *hi,
-			     struct hid_field *field, struct hid_usage *usage,
-			     unsigned long **bit, int *max)
-{
-	if (usage->hid == HID_HP_MANUALTRIGGER) {
-		haptic->manual_trigger_report = field->report;
-		/* we don't really want to map these fields */
-		return -1;
-	}
-
-	return 0;
-}
-EXPORT_SYMBOL_GPL(hid_haptic_input_mapping);
 
 static void parse_auto_trigger_field(struct hid_haptic_device *haptic,
 				     struct hid_field *field)
@@ -396,6 +378,9 @@ static void hid_haptic_destroy(struct ff_device *ff)
 
 	kfree(haptic->hid_usage_map);
 	haptic->hid_usage_map = NULL;
+
+	/* Our private data is managed by hid-plugin. */
+	ff->private = NULL;
 }
 
 static void hid_haptic_get_reports(struct hid_device *hdev, struct hid_haptic_device *haptic)
@@ -408,9 +393,8 @@ static void hid_haptic_get_reports(struct hid_device *hdev, struct hid_haptic_de
 	hid_device_io_stop(hdev);
 }
 
-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;
@@ -531,7 +515,6 @@ int hid_haptic_init(struct hid_device *hdev,
 	hid_warn(hdev, "Cannot allocate haptic for %s: %d\n", hdev->name, ret);
 	return ret;
 }
-EXPORT_SYMBOL_GPL(hid_haptic_init);
 
 static int hid_haptic_check_prerequisites(struct hid_device *hdev,
 					  struct hid_haptic_device *haptic, struct hid_input *hi)
@@ -542,14 +525,18 @@ static int hid_haptic_check_prerequisites(struct hid_device *hdev,
 	if (IS_ERR_OR_NULL(haptic->input_dev))
 		return PTR_ERR(haptic->input_dev) ?: -EXDEV;
 
+	/* Check the prerequisites only. */
+	if (!hi)
+		return 1;
+
 	if (haptic->input_dev == hi->input)
 		return 0;
 
 	return 1; /* Waiting for the relevant input device. */
 }
 
-int hid_haptic_input_configured(struct hid_device *hdev,
-				struct hid_haptic_device *haptic, struct hid_input *hi)
+static int __hid_haptic_input_configured(struct hid_device *hdev,
+					 struct hid_haptic_device *haptic, struct hid_input *hi)
 {
 	int ret;
 
@@ -566,17 +553,34 @@ int hid_haptic_input_configured(struct hid_device *hdev,
 
 	return 0;
 }
-EXPORT_SYMBOL_GPL(hid_haptic_input_configured);
 
-void hid_haptic_pressure_reset(struct hid_haptic_device *haptic)
-{
-	haptic->pressure_sum = 0;
-}
-EXPORT_SYMBOL_GPL(hid_haptic_pressure_reset);
+static struct hid_plugin_haptic hid_plugin_haptic;
 
-void hid_haptic_pressure_increase(struct hid_haptic_device *haptic,
-				 __s32 pressure)
+static void *hid_haptic_plugin_connect(struct hid_device *hdev)
 {
-	haptic->pressure_sum += pressure;
+	struct hid_haptic_device *haptic = kzalloc_obj(*haptic);
+
+	if (!haptic)
+		return ERR_PTR(-ENOMEM);
+
+	haptic->hdev = hdev;
+	haptic->plugin = &hid_plugin_haptic;
+	return haptic;
 }
-EXPORT_SYMBOL_GPL(hid_haptic_pressure_increase);
+
+static struct hid_plugin_haptic hid_plugin_haptic = {
+	.hp = {
+		.owner = THIS_MODULE,
+		.connect = hid_haptic_plugin_connect,
+	},
+	.feature_mapping = __hid_haptic_feature_mapping,
+	.check_pressure_unit = __hid_haptic_check_pressure_unit,
+	.input_configured = __hid_haptic_input_configured,
+};
+module_hid_plugin(haptic, HID_PLUGIN_HAPTIC, &hid_plugin_haptic.hp);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Angela Czubak <aczubak@google.com>");
+MODULE_AUTHOR("Jonathan Denose <jdenose@google.com>");
+MODULE_AUTHOR("Rong Zhang <i@rong.moe>");
+MODULE_DESCRIPTION("HID helpers for Haptics Page");
diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index e39104f75229..836520bf8dc7 100644
--- a/drivers/hid/hid-haptic.h
+++ b/drivers/hid/hid-haptic.h
@@ -5,8 +5,13 @@
  *  Copyright (c) 2021 Angela Czubak <acz@semihalf.com>
  */
 
+#include <linux/compiler.h>
+#include <linux/container_of.h>
+#include <linux/err.h>
 #include <linux/hid.h>
 
+#include "hid-plugin.h"
+
 #define HID_HAPTIC_ORDINAL_WAVEFORMNONE 1
 #define HID_HAPTIC_ORDINAL_WAVEFORMSTOP 2
 
@@ -51,90 +56,123 @@ struct hid_haptic_device {
 	u16 release_ordinal;
 	struct hid_haptic_effect *effect;
 	struct hid_haptic_effect stop_effect;
+
+	struct hid_plugin_haptic *plugin;
+};
+
+struct hid_plugin_haptic {
+	struct hid_plugin hp;
+	void (*feature_mapping)(struct hid_device *hdev, struct hid_haptic_device *haptic,
+				struct hid_field *field, struct hid_usage *usage);
+	bool (*check_pressure_unit)(struct hid_haptic_device *haptic,
+				    struct hid_input *hi, struct hid_field *field);
+	int (*input_configured)(struct hid_device *hdev, struct hid_haptic_device *haptic,
+				struct hid_input *hi);
 };
 
-#if IS_ENABLED(CONFIG_HID_HAPTIC)
-static inline bool hid_haptic_connected_to_input(struct hid_haptic_device *haptic,
-						 struct input_dev *input)
+static inline struct hid_haptic_device *hid_plugin_to_haptic(struct hid_device *hdev)
 {
+	return hid_plugin_to_priv(hdev, HID_PLUGIN_HAPTIC);
+}
+
+static inline bool hid_haptic_connected_to_input(struct hid_device *hdev, struct input_dev *input)
+{
+	struct hid_haptic_device *haptic = hid_plugin_to_haptic(hdev);
+
+	if (IS_ERR_OR_NULL(haptic))
+		return false;
+
 	return haptic->input_dev == input;
 }
 
-void hid_haptic_feature_mapping(struct hid_device *hdev,
-				struct hid_haptic_device *haptic,
-				struct hid_field *field, struct hid_usage
-				*usage);
-bool hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
-				    struct hid_input *hi, struct hid_field *field);
-int hid_haptic_input_mapping(struct hid_device *hdev,
-			     struct hid_haptic_device *haptic,
-			     struct hid_input *hi,
-			     struct hid_field *field, struct hid_usage *usage,
-			     unsigned long **bit, int *max);
-int hid_haptic_input_configured(struct hid_device *hdev,
-				struct hid_haptic_device *haptic,
-				struct hid_input *hi);
-int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *haptic,
-		    struct input_dev *dev);
-void hid_haptic_handle_press_release(struct hid_haptic_device *haptic);
-void hid_haptic_pressure_reset(struct hid_haptic_device *haptic);
-void hid_haptic_pressure_increase(struct hid_haptic_device *haptic,
-				  __s32 pressure);
-#else
-static inline bool hid_haptic_connected_to_input(struct hid_haptic_device *haptic,
-						 struct input_dev *input)
+static inline struct hid_haptic_device *hid_haptic_connect(struct hid_device *hdev)
 {
-	return false;
+	return hid_plugin_connect(hdev, HID_PLUGIN_HAPTIC);
 }
 
-static inline
-void hid_haptic_feature_mapping(struct hid_device *hdev,
-				struct hid_haptic_device *haptic,
-				struct hid_field *field, struct hid_usage
-				*usage)
-{}
-static inline
-bool hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
-				    struct hid_input *hi, struct hid_field *field)
+static inline void hid_haptic_disconnect(struct hid_device *hdev)
 {
-	return false;
+	hid_plugin_disconnect(hdev, HID_PLUGIN_HAPTIC);
 }
-static inline
-int hid_haptic_input_mapping(struct hid_device *hdev,
-			     struct hid_haptic_device *haptic,
-			     struct hid_input *hi,
-			     struct hid_field *field, struct hid_usage *usage,
-			     unsigned long **bit, int *max)
+
+static inline void hid_haptic_feature_mapping(struct hid_device *hdev, struct hid_field *field,
+					      struct hid_usage *usage)
 {
-	return 0;
+	struct hid_haptic_device *haptic;
+
+	if ((field->logical & HID_USAGE_PAGE) != HID_UP_HAPTIC &&
+	    (usage->hid & HID_USAGE_PAGE) != HID_UP_HAPTIC)
+		return;
+
+	haptic = hid_plugin_to_haptic(hdev);
+	if (unlikely(!haptic))
+		haptic = hid_haptic_connect(hdev);
+	if (IS_ERR(haptic))
+		return;
+
+	haptic->plugin->feature_mapping(hdev, haptic, field, usage);
 }
-static inline
-int hid_haptic_input_configured(struct hid_device *hdev,
-				struct hid_haptic_device *haptic,
-				struct hid_input *hi)
+
+static inline bool hid_haptic_check_pressure_unit(struct hid_device *hdev, struct hid_input *hi,
+						  struct hid_field *field)
 {
-	return -EOPNOTSUPP;
+	struct hid_haptic_device *haptic = hid_plugin_to_haptic(hdev);
+
+	if (IS_ERR_OR_NULL(haptic))
+		return false;
+
+	return haptic->plugin->check_pressure_unit(haptic, hi, field);
 }
-static inline
-void hid_haptic_reset(struct hid_device *hdev, struct hid_haptic_device *haptic)
-{}
-static inline
-int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *haptic,
-		    struct input_dev *dev)
+
+static inline int hid_haptic_input_mapping(struct hid_device *hdev, struct hid_input *hi,
+					   struct hid_field *field, struct hid_usage *usage,
+					   unsigned long **bit, int *max)
 {
+	struct hid_haptic_device *haptic = hid_plugin_to_haptic(hdev);
+
+	if (IS_ERR_OR_NULL(haptic))
+		return 0;
+
+	if (usage->hid == HID_HP_MANUALTRIGGER) {
+		haptic->manual_trigger_report = field->report;
+		/* we don't really want to map these fields */
+		return -1;
+	}
+
 	return 0;
 }
-static inline
-void hid_haptic_handle_press_release(struct hid_haptic_device *haptic) {}
-static inline
-bool hid_haptic_handle_input(struct hid_haptic_device *haptic)
+
+static inline int hid_haptic_input_configured(struct hid_device *hdev, struct hid_input *hi)
 {
-	return false;
+	struct hid_haptic_device *haptic = hid_plugin_to_haptic(hdev);
+	int ret;
+
+	if (IS_ERR_OR_NULL(haptic))
+		return -EOPNOTSUPP;
+
+	ret = haptic->plugin->input_configured(hdev, haptic, hi);
+	if (ret < 0)
+		hid_haptic_disconnect(hdev);
+
+	return ret;
+}
+
+static inline void hid_haptic_pressure_reset(struct hid_device *hdev)
+{
+	struct hid_haptic_device *haptic = hid_plugin_to_haptic(hdev);
+
+	if (IS_ERR_OR_NULL(haptic))
+		return;
+
+	haptic->pressure_sum = 0;
+}
+
+static inline void hid_haptic_pressure_increase(struct hid_device *hdev, __s32 pressure)
+{
+	struct hid_haptic_device *haptic = hid_plugin_to_haptic(hdev);
+
+	if (IS_ERR_OR_NULL(haptic))
+		return;
+
+	haptic->pressure_sum += pressure;
 }
-static inline
-void hid_haptic_pressure_reset(struct hid_haptic_device *haptic) {}
-static inline
-void hid_haptic_pressure_increase(struct hid_haptic_device *haptic,
-				  __s32 pressure)
-{}
-#endif
diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
index a4d3536c7af2..e9a25a3bfa1b 100644
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -20,6 +20,7 @@
 #include <linux/hid.h>
 #include <linux/hid-debug.h>
 
+#include "hid-haptic.h"
 #include "hid-ids.h"
 
 #define unk	KEY_UNKNOWN
@@ -1353,6 +1354,17 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 		}
 		break;
 
+	case HID_UP_HAPTIC: {
+		int ret = hid_haptic_input_mapping(device, hidinput, field, usage, &bit, &max);
+
+		if (ret > 0)
+			goto mapped;
+		if (ret < 0)
+			goto ignore;
+
+		break;
+	}
+
 	case HID_UP_HPVENDOR:	/* Reported on a Dutch layout HP5308 */
 		set_bit(EV_REP, input->evbit);
 		switch (usage->hid & HID_USAGE) {
@@ -2054,6 +2066,8 @@ static void report_features(struct hid_device *hid)
 					hidinput_setup_battery(hid, HID_FEATURE_REPORT,
 							       rep->field[i], false);
 
+				hid_haptic_feature_mapping(hid, rep->field[i], usage);
+
 				if (drv->feature_mapping)
 					drv->feature_mapping(hid, rep->field[i], usage);
 			}
@@ -2346,6 +2360,7 @@ int hidinput_connect(struct hid_device *hid, unsigned int connect_mask)
 	struct hid_driver *drv = hid->driver;
 	struct hid_report *report;
 	struct hid_input *next, *hidinput = NULL;
+	bool haptic_ff, driver_ff = false;
 	unsigned int application;
 	int i, k;
 
@@ -2409,20 +2424,42 @@ int hidinput_connect(struct hid_device *hid, unsigned int connect_mask)
 
 	hidinput_change_resolution_multipliers(hid);
 
+	haptic_ff = hid_haptic_input_configured(hid, NULL) >= 0;
+
 	list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
+		bool first = list_is_first(&hidinput->list, &hid->inputs);
+
 		if (drv->input_configured &&
 		    drv->input_configured(hid, hidinput))
 			goto out_unwind;
 
+		if (first) {
+			driver_ff = hid_has_ff_input(hid) || !(connect_mask & HID_CONNECT_FF);
+			if (haptic_ff && driver_ff) {
+				hid_info(hid, "prefer driver's FF over haptic\n");
+				hid_haptic_disconnect(hid);
+				haptic_ff = false;
+			} else if (haptic_ff) {
+				/*
+				 * Prefer hid-haptic over hid-pidff.
+				 *
+				 * Skip hid-pidff even if hid-haptic somehow fails
+				 * to initialize, so that the choice is constant.
+				 */
+				driver_ff = true;
+			}
+		}
+
+		if (haptic_ff)
+			haptic_ff = hid_haptic_input_configured(hid, hidinput) >= 0;
+
 		if (!hidinput_has_been_populated(hidinput)) {
 			/* no need to register an input device not populated */
 			hidinput_cleanup_hidinput(hid, hidinput);
 			continue;
 		}
 
-		if (list_is_first(&hidinput->list, &hid->inputs) &&
-		    (connect_mask & HID_CONNECT_FF) && hid->ff_init &&
-		    !hid_has_ff_input(hid))
+		if (first && !driver_ff && hid->ff_init)
 			hid->ff_init(hid);
 
 		if (input_register_device(hidinput->input))
@@ -2445,6 +2482,13 @@ int hidinput_connect(struct hid_device *hid, unsigned int connect_mask)
 	/* unwind the ones we already registered */
 	hidinput_disconnect(hid);
 
+	/*
+	 * On failure, the HID device may still keep connected to the HID
+	 * driver. To prevent holding useless resources during runtime, don't
+	 * wait until driver removal and disconnect hid-haptic early.
+	 */
+	hid_haptic_disconnect(hid);
+
 	return -1;
 }
 EXPORT_SYMBOL_GPL(hidinput_connect);
diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index e017eff9b784..b8299a1a2bb9 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -173,7 +173,6 @@ struct mt_report_data {
 struct mt_device {
 	struct mt_class mtclass;	/* our mt device class */
 	struct timer_list release_timer;	/* to release sticky fingers */
-	struct hid_haptic_device *haptic;	/* haptic related configuration */
 	struct hid_device *hdev;	/* hid_device we're attached to */
 	unsigned long mt_io_flags;	/* mt flags (MT_IO_FLAGS_RUNNING) */
 	unsigned long *active_slots;	/* bitmap of slots with an active
@@ -604,8 +603,6 @@ static void mt_feature_mapping(struct hid_device *hdev,
 			mt_get_feature(hdev, field->report);
 		break;
 	}
-
-	hid_haptic_feature_mapping(hdev, td->haptic, field, usage);
 }
 
 static void set_abs(struct input_dev *input, unsigned int code,
@@ -961,9 +958,7 @@ static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
 		case HID_DG_TIPPRESSURE:
 			set_abs(hi->input, ABS_MT_PRESSURE, field,
 				cls->sn_pressure);
-			td->is_haptic_touchpad =
-				hid_haptic_check_pressure_unit(td->haptic,
-							       hi, field);
+			td->is_haptic_touchpad = hid_haptic_check_pressure_unit(hdev, hi, field);
 			MT_STORE_FIELD(p);
 			return 1;
 		case HID_DG_SCANTIME:
@@ -1074,6 +1069,8 @@ static void mt_release_pending_palms(struct mt_device *td,
 static void mt_sync_frame(struct mt_device *td, struct mt_application *app,
 			  struct input_dev *input)
 {
+	struct hid_device *hdev = td->hdev;
+
 	if (app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS)
 		input_event(input, EV_KEY, BTN_LEFT, app->left_button_state);
 
@@ -1085,8 +1082,8 @@ static void mt_sync_frame(struct mt_device *td, struct mt_application *app,
 
 	app->num_received = 0;
 	app->left_button_state = 0;
-	if (td->is_haptic_touchpad && hid_haptic_connected_to_input(td->haptic, input))
-		hid_haptic_pressure_reset(td->haptic);
+	if (td->is_haptic_touchpad && hid_haptic_connected_to_input(hdev, input))
+		hid_haptic_pressure_reset(hdev);
 }
 
 static int mt_compute_timestamp(struct mt_application *app, __s32 value)
@@ -1238,8 +1235,8 @@ static int mt_process_slot(struct mt_device *td, struct input_dev *input,
 			minor = minor >> 1;
 		}
 
-		if (td->is_haptic_touchpad && hid_haptic_connected_to_input(td->haptic, input))
-			hid_haptic_pressure_increase(td->haptic, *slot->p);
+		if (td->is_haptic_touchpad && hid_haptic_connected_to_input(hdev, input))
+			hid_haptic_pressure_increase(hdev, *slot->p);
 
 		x = hdev->quirks & HID_QUIRK_X_INVERT ?
 			input_abs_get_max(input, ABS_MT_POSITION_X) - *slot->x :
@@ -1441,7 +1438,7 @@ static int mt_touch_input_configured(struct hid_device *hdev,
 	if (cls->is_indirect)
 		app->mt_flags |= INPUT_MT_POINTER;
 
-	if (td->is_haptic_touchpad && hid_haptic_connected_to_input(td->haptic, input))
+	if (td->is_haptic_touchpad && hid_haptic_connected_to_input(hdev, input))
 		app->mt_flags |= INPUT_MT_TOTAL_FORCE;
 
 	if (app->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
@@ -1490,7 +1487,6 @@ static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
 	struct mt_device *td = hid_get_drvdata(hdev);
 	struct mt_application *application;
 	struct mt_report_data *rdata;
-	int ret;
 
 	rdata = mt_find_report_data(td, field->report);
 	if (!rdata) {
@@ -1553,11 +1549,6 @@ static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
 	if (field->physical == HID_DG_STYLUS)
 		hi->application = HID_DG_STYLUS;
 
-	ret = hid_haptic_input_mapping(hdev, td->haptic, hi, field, usage, bit,
-				       max);
-	if (ret != 0)
-		return ret;
-
 	/* let hid-core decide for the others */
 	return 0;
 }
@@ -1954,9 +1945,6 @@ static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
 	struct hid_report *report;
 	int ret;
 
-	if (!td->is_haptic_touchpad)
-		td->is_haptic_touchpad = !hid_haptic_input_configured(hdev, td->haptic, hi);
-
 	list_for_each_entry(report, &hi->reports, hidinput_list) {
 		rdata = mt_find_report_data(td, report);
 		if (!rdata) {
@@ -2125,11 +2113,7 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
 		dev_err(&hdev->dev, "cannot allocate multitouch data\n");
 		return -ENOMEM;
 	}
-	td->haptic = devm_kzalloc_obj(&hdev->dev, *(td->haptic));
-	if (!td->haptic)
-		return -ENOMEM;
 
-	td->haptic->hdev = hdev;
 	td->hdev = hdev;
 	td->mtclass = *mtclass;
 	td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
@@ -2198,9 +2182,6 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
 
 	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
 
-	if (!td->is_haptic_touchpad)
-		devm_kfree(&hdev->dev, td->haptic);
-
 	return 0;
 }
 
diff --git a/drivers/hid/hid-plugin.c b/drivers/hid/hid-plugin.c
index f93753d3ee12..b4de74e70021 100644
--- a/drivers/hid/hid-plugin.c
+++ b/drivers/hid/hid-plugin.c
@@ -22,6 +22,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/include/linux/hid.h b/include/linux/hid.h
index 2444aabcb604..585b3336f4a7 100644
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -676,6 +676,12 @@ struct hid_ll_driver;
 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,
 };
 
@@ -765,7 +771,7 @@ struct hid_device {
 	struct hid_bpf bpf;						/* hid-bpf data */
 #endif /* CONFIG_HID_BPF */
 
-	struct hid_plugin_private *plugins[NR_HID_PLUGINS];
+	void *plugins[NR_HID_PLUGINS];
 };
 
 void hiddev_free(struct kref *ref);

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 07/13] HID: haptic: Adopt pr_fmt, dev_fmt and hid_*() printk helpers
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
                   ` (5 preceding siblings ...)
  2026-11-29 17:52 ` [PATCH v5 06/13] HID: haptic: Convert to a loadable plugin module Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 08/13] HID: Add Ordinal page defines Rong Zhang
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang

Now that hid-haptic has been converted into a plugin and will support
all sorts of devices in the following changes, printing messages under
other HID driver's hood may be confusing.

Add pr_fmt and dev_fmt to help identify the source of printk messages.

Meanwhile, migrate from the tedious dev_*() printk helpers to tidier
hid_*() ones.

Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/hid/hid-haptic.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index c8c1c7fddaa6..3edda5d6018b 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -5,6 +5,9 @@
  *  Copyright (c) 2021 Angela Czubak <acz@semihalf.com>
  */
 
+#define pr_fmt(fmt) "hid-haptic: " fmt
+#define dev_fmt pr_fmt
+
 #include <linux/err.h>
 #include <linux/input/mt.h>
 #include <linux/module.h>
@@ -18,8 +21,7 @@ static void __hid_haptic_feature_mapping(struct hid_device *hdev, struct hid_hap
 
 	if (usage->hid == HID_HP_AUTOTRIGGER) {
 		if (usage->usage_index >= field->report_count) {
-			dev_err(&hdev->dev,
-				"HID_HP_AUTOTRIGGER out of range\n");
+			hid_err(hdev, "HID_HP_AUTOTRIGGER out of range\n");
 			return;
 		}
 		haptic->auto_trigger_report = field->report;
@@ -418,7 +420,7 @@ static int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *ha
 	}
 
 	if (haptic->max_waveform_id != haptic->max_duration_id)
-		dev_warn(&hdev->dev,
+		hid_warn(hdev,
 			 "Haptic duration and waveform lists have different max id (%u and %u).\n",
 			 haptic->max_duration_id, haptic->max_waveform_id);
 
@@ -449,8 +451,7 @@ static int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *ha
 			hid_alloc_report_buf(haptic->manual_trigger_report,
 					     GFP_KERNEL);
 		if (!haptic->effect[r].report_buf) {
-			dev_err(&hdev->dev,
-				"Failed to allocate a buffer for an effect.\n");
+			hid_err(hdev, "Failed to allocate a buffer for an effect.\n");
 			ret = -ENOMEM;
 			goto buffer_free;
 		}
@@ -461,8 +462,7 @@ static int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *ha
 		hid_alloc_report_buf(haptic->manual_trigger_report,
 				     GFP_KERNEL);
 	if (!haptic->stop_effect.report_buf) {
-		dev_err(&hdev->dev,
-			"Failed to allocate a buffer for stop effect.\n");
+		hid_err(hdev, "Failed to allocate a buffer for stop effect.\n");
 		ret = -ENOMEM;
 		goto buffer_free;
 	}
@@ -481,7 +481,7 @@ static int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *ha
 
 	ret = input_ff_create(dev, FF_MAX_EFFECTS);
 	if (ret) {
-		dev_err(&hdev->dev, "Failed to create ff device.\n");
+		hid_err(hdev, "Failed to create ff device.\n");
 		goto reset_evbit;
 	}
 

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 08/13] HID: Add Ordinal page defines
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
                   ` (6 preceding siblings ...)
  2026-11-29 17:52 ` [PATCH v5 07/13] HID: haptic: Adopt pr_fmt, dev_fmt and hid_*() printk helpers Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 09/13] HID: haptic: Handle list reports separately and make auto trigger optional Rong Zhang
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang

Currently, the only macro for the HID Ordinal Page is HID_UP_ORDINAL,
which only matches the usage page itself.

This leads to inconvenience when drivers need to match both Ordinal and
non-Ordinal HID usages at once. They must use either nested switch
statements (usage pages, and then usages) or mixed if-else and switch
statements, which are hard to read and maintain.

Meanwhile, only usage 0x1-0xffff within the page is defined, and usage 0
is reserved and should be rejected, so simply matching the Ordinal page
and extracting the usage ID is essentially not the correct way to match
valid Ordinal usages.

Introduce HID_OD_MINIMUM and HID_OD_MAXIMUM to properly describe the
valid range of Ordinal usages, as well as providing HID_OD_RANGE for
switch statements to match Ordinal usages with ease.

Link: https://usb.org/sites/default/files/hut1_7.pdf
Signed-off-by: Rong Zhang <i@rong.moe>
---
 include/linux/hid.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/linux/hid.h b/include/linux/hid.h
index 585b3336f4a7..7af2666ef426 100644
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -231,6 +231,11 @@ struct hid_item {
 
 #define HID_DC_BATTERYSTRENGTH	0x00060020
 
+#define HID_OD_MINIMUM		0x000a0001
+#define HID_OD_MAXIMUM		0x000affff
+/* Use in switch statements. */
+#define HID_OD_RANGE		HID_OD_MINIMUM ... HID_OD_MAXIMUM
+
 #define HID_CP_CONSUMER_CONTROL	0x000c0001
 #define HID_CP_AC_PAN		0x000c0238
 

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 09/13] HID: haptic: Handle list reports separately and make auto trigger optional
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
                   ` (7 preceding siblings ...)
  2026-11-29 17:52 ` [PATCH v5 08/13] HID: Add Ordinal page defines Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 10/13] HID: haptic: Decouple from hid-multitouch and support other devices Rong Zhang
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang

The Waveform List and Duration List collections are read-only and
inherently different from the Auto Trigger freture, which is read-write.
The latter may use a dedicated report or even missing [1] on some device
due to the difference.

Handle list reports separately to decouple them from the auto trigger
report, so that the latter becomes optional.

According to [1][2], Waveform Vendor Page and ID shall be declared
within the Waveform List. The requirement is now enforced to
intentionally ignore malformed Vendor Page and ID and further simplify
the code to make it more readable.

Also adopt the newly introduced HID_OD_RANGE macro to get rid of mixed
if-else and switch statements.

Link: https://learn.microsoft.com/en-us/windows-hardware/design/component-guidelines/input-haptics-implementation-guide [1]
Link: https://usb.org/sites/default/files/hutrr63b_-_haptics_page_redline_0.pdf [2]
Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/hid/hid-haptic.c | 134 ++++++++++++++++++++++++++++++++---------------
 drivers/hid/hid-haptic.h |   2 +
 2 files changed, 93 insertions(+), 43 deletions(-)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index 3edda5d6018b..84b35ec8365a 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -8,9 +8,11 @@
 #define pr_fmt(fmt) "hid-haptic: " fmt
 #define dev_fmt pr_fmt
 
+#include <linux/bitmap.h>
 #include <linux/err.h>
 #include <linux/input/mt.h>
 #include <linux/module.h>
+#include <linux/types.h>
 
 #include "hid-haptic.h"
 
@@ -19,26 +21,33 @@ static void __hid_haptic_feature_mapping(struct hid_device *hdev, struct hid_hap
 {
 	u16 usage_hid;
 
-	if (usage->hid == HID_HP_AUTOTRIGGER) {
+	switch (usage->hid) {
+	case HID_HP_AUTOTRIGGER:
 		if (usage->usage_index >= field->report_count) {
 			hid_err(hdev, "HID_HP_AUTOTRIGGER out of range\n");
 			return;
 		}
 		haptic->auto_trigger_report = field->report;
-	} else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ORDINAL) {
+		break;
+	case HID_OD_RANGE:
 		usage_hid = usage->hid & HID_USAGE;
 		switch (field->logical) {
 		case HID_HP_WAVEFORMLIST:
+			haptic->waveform_list_report = field->report;
 			if (usage_hid > haptic->max_waveform_id)
 				haptic->max_waveform_id = usage_hid;
 			break;
 		case HID_HP_DURATIONLIST:
+			haptic->duration_list_report = field->report;
 			if (usage_hid > haptic->max_duration_id)
 				haptic->max_duration_id = usage_hid;
 			break;
 		default:
 			break;
 		}
+		break;
+	default:
+		break;
 	}
 }
 
@@ -80,53 +89,69 @@ static bool __hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
 	return false;
 }
 
-static void parse_auto_trigger_field(struct hid_haptic_device *haptic,
-				     struct hid_field *field)
+static void parse_waveform_list(struct hid_haptic_device *haptic, struct hid_field *field)
 {
 	int count = field->report_count;
 	int n;
 	u16 usage_hid;
 
+	if (field->logical != HID_HP_WAVEFORMLIST)
+		return;
+
 	for (n = 0; n < count; n++) {
-		switch (field->usage[n].hid & HID_USAGE_PAGE) {
-		case HID_UP_ORDINAL:
+		switch (field->usage[n].hid) {
+		case HID_OD_RANGE:
 			usage_hid = field->usage[n].hid & HID_USAGE;
-			switch (field->logical) {
-			case HID_HP_WAVEFORMLIST:
-				haptic->hid_usage_map[usage_hid] = field->value[n];
-				if (field->value[n] ==
-				    (HID_HP_WAVEFORMPRESS & HID_USAGE)) {
-					haptic->press_ordinal = usage_hid;
-				} else if (field->value[n] ==
-					   (HID_HP_WAVEFORMRELEASE & HID_USAGE)) {
-					haptic->release_ordinal = usage_hid;
-				}
-				break;
-			case HID_HP_DURATIONLIST:
-				haptic->duration_map[usage_hid] =
-					field->value[n];
-				break;
-			default:
-				break;
-			}
+			haptic->hid_usage_map[usage_hid] = field->value[n];
+			if (field->value[n] == (HID_HP_WAVEFORMPRESS & HID_USAGE))
+				haptic->press_ordinal = usage_hid;
+			else if (field->value[n] == (HID_HP_WAVEFORMRELEASE & HID_USAGE))
+				haptic->release_ordinal = usage_hid;
 			break;
-		case HID_UP_HAPTIC:
-			switch (field->usage[n].hid) {
-			case HID_HP_AUTOTRIGGER:
-				haptic->default_auto_trigger = field->value[n];
-				break;
-			case HID_HP_WAVEFORMVENDORID:
-				haptic->vendor_id = field->value[n];
-				break;
-			case HID_HP_WAVEFORMVENDORPAGE:
-				haptic->vendor_page = field->value[n];
-				break;
-			default:
-				break;
-			}
+		case HID_HP_WAVEFORMVENDORID:
+			haptic->vendor_id = field->value[n];
+			break;
+		case HID_HP_WAVEFORMVENDORPAGE:
+			haptic->vendor_page = field->value[n];
+			break;
+		default:
+			break;
+		}
+	}
+}
+
+static void parse_duration_list(struct hid_haptic_device *haptic, struct hid_field *field)
+{
+	int count = field->report_count;
+	int n;
+	u16 usage_hid;
+
+	if (field->logical != HID_HP_DURATIONLIST)
+		return;
+
+	for (n = 0; n < count; n++) {
+		switch (field->usage[n].hid) {
+		case HID_OD_RANGE:
+			usage_hid = field->usage[n].hid & HID_USAGE;
+			haptic->duration_map[usage_hid] = field->value[n];
+			break;
+		default:
+			break;
+		}
+	}
+}
+
+static void parse_auto_trigger_field(struct hid_haptic_device *haptic, struct hid_field *field)
+{
+	int count = field->report_count;
+	int n;
+
+	for (n = 0; n < count; n++) {
+		switch (field->usage[n].hid) {
+		case HID_HP_AUTOTRIGGER:
+			haptic->default_auto_trigger = field->value[n];
 			break;
 		default:
-			/* Should not really happen */
 			break;
 		}
 	}
@@ -194,6 +219,9 @@ static void switch_mode(struct hid_device *hdev, struct hid_haptic_device *hapti
 	s32 value;
 	int i, j;
 
+	if (!rep)
+		return;
+
 	if (mode == HID_HAPTIC_MODE_HOST)
 		value = HID_HAPTIC_ORDINAL_WAVEFORMSTOP;
 	else
@@ -387,11 +415,26 @@ static void hid_haptic_destroy(struct ff_device *ff)
 
 static void hid_haptic_get_reports(struct hid_device *hdev, struct hid_haptic_device *haptic)
 {
+	DECLARE_BITMAP(usage_map, HID_MAX_IDS) = {};
+	int i;
+
+	__set_bit(haptic->waveform_list_report->id, usage_map);
+	__set_bit(haptic->duration_list_report->id, usage_map);
+	if (haptic->auto_trigger_report)
+		__set_bit(haptic->auto_trigger_report->id, usage_map);
+
 	hid_device_io_start(hdev);
 
-	hid_hw_request(hdev, haptic->auto_trigger_report, HID_REQ_GET_REPORT);
+	/*
+	 * The three collections/usages may share the same report, or use
+	 * dedicated ones. Skip duplicate GET_REPORT requests.
+	 */
+	for_each_set_bit(i, usage_map, HID_MAX_IDS) {
+		hid_hw_request(hdev, hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[i],
+			       HID_REQ_GET_REPORT);
+		hid_hw_wait(hdev);
+	}
 
-	hid_hw_wait(hdev);
 	hid_device_io_stop(hdev);
 }
 
@@ -433,7 +476,11 @@ static int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *ha
 
 	hid_haptic_get_reports(hdev, haptic);
 
-	for (r = 0; r < haptic->auto_trigger_report->maxfield; r++)
+	for (r = 0; r < haptic->waveform_list_report->maxfield; r++)
+		parse_waveform_list(haptic, haptic->waveform_list_report->field[r]);
+	for (r = 0; r < haptic->duration_list_report->maxfield; r++)
+		parse_duration_list(haptic, haptic->duration_list_report->field[r]);
+	for (r = 0; haptic->auto_trigger_report && r < haptic->auto_trigger_report->maxfield; r++)
 		parse_auto_trigger_field(haptic, haptic->auto_trigger_report->field[r]);
 
 	haptic->input_dev = dev;
@@ -519,7 +566,8 @@ static int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *ha
 static int hid_haptic_check_prerequisites(struct hid_device *hdev,
 					  struct hid_haptic_device *haptic, struct hid_input *hi)
 {
-	if (!haptic->auto_trigger_report || !haptic->manual_trigger_report)
+	if (!haptic->waveform_list_report || !haptic->duration_list_report ||
+	    !haptic->manual_trigger_report)
 		return -ENOENT;
 
 	if (IS_ERR_OR_NULL(haptic->input_dev))
diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index 836520bf8dc7..ad2aa267677f 100644
--- a/drivers/hid/hid-haptic.h
+++ b/drivers/hid/hid-haptic.h
@@ -34,6 +34,8 @@ struct hid_haptic_effect_node {
 struct hid_haptic_device {
 	struct input_dev *input_dev;
 	struct hid_device *hdev;
+	struct hid_report *waveform_list_report;
+	struct hid_report *duration_list_report;
 	struct hid_report *auto_trigger_report;
 	struct mutex auto_trigger_mutex;
 	struct workqueue_struct *wq;

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 10/13] HID: haptic: Decouple from hid-multitouch and support other devices
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
                   ` (8 preceding siblings ...)
  2026-11-29 17:52 ` [PATCH v5 09/13] HID: haptic: Handle list reports separately and make auto trigger optional Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 18:29   ` sashiko-bot
  2026-11-29 17:52 ` [PATCH v5 11/13] HID: haptic: Add quirk table Rong Zhang
                   ` (2 subsequent siblings)
  12 siblings, 1 reply; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang

Currently, hid-haptic only works on multitouch devices that have passed
the pressure unit check.

According to [1], a Simple Haptic Controller may be either a Collection
Application (CA) or a Collection Logical (CL):

A CA is a dedicated HID application, i.e., not associated with other HID
applications. Choose the first HID input device to attach the FF device,
as other FF drivers do. Haptic Mouse usually falls into this category as
long as it follows the example in [2].

A CL is contained within another HID application. The HID input device
handling the manual trigger report must be the one handling the
associated HID application. Haptic Touchpad and Stylus usually fall into
this category as long as they follow the examples in [2][3].

When HID_QUIRK_MULTI_INPUT is set, the quirky collection hierarchy makes
no sense anyway, so the first input device is chosen instead.

Fall back to the above strategy when hid-multitouch didn't choose an
input device due to pressure unit check failure or non-multitouch
device. In this case, hid-haptic works without the integration with
hid-multitouch.

With these, hid-plugin now becomes a generic plugin, so also add an info
message for device identification after haptic is connected.

Link: https://usb.org/sites/default/files/hutrr63b_-_haptics_page_redline_0.pdf [1]
Link: https://learn.microsoft.com/en-us/windows-hardware/design/component-guidelines/input-haptics-implementation-guide [2]
Link: https://learn.microsoft.com/en-us/windows-hardware/design/component-guidelines/haptic-pen-implementation-guide [3]
Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/hid/hid-haptic.c | 53 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 47 insertions(+), 6 deletions(-)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index 84b35ec8365a..eab7bbaaf504 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -566,19 +566,55 @@ static int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *ha
 static int hid_haptic_check_prerequisites(struct hid_device *hdev,
 					  struct hid_haptic_device *haptic, struct hid_input *hi)
 {
+	struct hid_report *report;
+
 	if (!haptic->waveform_list_report || !haptic->duration_list_report ||
 	    !haptic->manual_trigger_report)
 		return -ENOENT;
 
-	if (IS_ERR_OR_NULL(haptic->input_dev))
-		return PTR_ERR(haptic->input_dev) ?: -EXDEV;
-
 	/* Check the prerequisites only. */
 	if (!hi)
 		return 1;
 
-	if (haptic->input_dev == hi->input)
-		return 0;
+	/*
+	 * Honor the HID driver's choice.
+	 *
+	 * If the driver didn't choose an input device or failed its
+	 * prerequisites check, initialize the haptic device without the
+	 * integration with the driver.
+	 */
+	if (IS_ERR_OR_NULL(haptic->input_dev))
+		haptic->input_dev = NULL;
+	else
+		return haptic->input_dev != hi->input;
+
+	/*
+	 * A Simple Haptic Controller may be either a Collection Application
+	 * (CA) or a Collection Logical (CL):
+	 *
+	 * A CA is a dedicated HID application, i.e., not associated with other
+	 * HID applications. Choose the first HID input device to attach the FF
+	 * device, as other FF drivers do. Haptic Mouse usually falls into this
+	 * category.
+	 *
+	 * A CL is contained within another HID application. The HID input
+	 * device handling the manual trigger report must be the one handling
+	 * the associated HID application. Haptic Touchpad and Stylus usually
+	 * fall into this category.
+	 *
+	 * When HID_QUIRK_MULTI_INPUT is set, the quirky collection hierarchy
+	 * makes no sense anyway, so the first input device is chosen instead.
+	 */
+	if (haptic->manual_trigger_report->application == HID_HP_SIMPLECONTROLLER ||
+	    hdev->quirks & HID_QUIRK_MULTI_INPUT) {
+		if (list_is_first(&hi->list, &hdev->inputs))
+			return 0;
+	} else {
+		list_for_each_entry(report, &hi->reports, hidinput_list) {
+			if (report == haptic->manual_trigger_report)
+				return 0;
+		}
+	}
 
 	return 1; /* Waiting for the relevant input device. */
 }
@@ -586,17 +622,22 @@ static int hid_haptic_check_prerequisites(struct hid_device *hdev,
 static int __hid_haptic_input_configured(struct hid_device *hdev,
 					 struct hid_haptic_device *haptic, struct hid_input *hi)
 {
+	bool driver_integration;
 	int ret;
 
 	ret = hid_haptic_check_prerequisites(hdev, haptic, hi);
 	if (ret)
 		return ret;
 
+	driver_integration = haptic->input_dev == hi->input;
+
 	ret = hid_haptic_init(hdev, haptic, hi->input);
 	if (ret)
 		return ret;
 
-	if (hi->application == HID_DG_TOUCHPAD)
+	hid_info(hdev, "Haptic connected to %s\n", hi->input->name);
+
+	if (driver_integration && hi->application == HID_DG_TOUCHPAD)
 		__set_bit(INPUT_PROP_PRESSUREPAD, hi->input->propbit);
 
 	return 0;

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 11/13] HID: haptic: Add quirk table
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
                   ` (9 preceding siblings ...)
  2026-11-29 17:52 ` [PATCH v5 10/13] HID: haptic: Decouple from hid-multitouch and support other devices Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 12/13] HID: haptic: Add HID_HAPTIC_QUIRK_NO_RETRIGGER to disable retriggering Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 13/13] HID: haptic: Add quirk flag for Logitech MX Master 4 Rong Zhang
  12 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang

In the following changes, per-device quirk flags are needed to handle
faulty devices.

Add a quirk table and match it on init.

Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/hid/hid-haptic.c | 16 ++++++++++++++++
 drivers/hid/hid-haptic.h |  4 ++++
 2 files changed, 20 insertions(+)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index eab7bbaaf504..1c314c273316 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -16,6 +16,20 @@
 
 #include "hid-haptic.h"
 
+static const struct hid_device_id hid_haptic_quirks[] = {
+	{ }
+};
+
+static u32 hid_haptic_match_quirks(struct hid_device *hdev)
+{
+	const struct hid_device_id *id = hid_match_id(hdev, hid_haptic_quirks);
+	u32 quirks;
+
+	quirks = id ? id->driver_data : 0;
+
+	return quirks;
+}
+
 static void __hid_haptic_feature_mapping(struct hid_device *hdev, struct hid_haptic_device *haptic,
 					 struct hid_field *field, struct hid_usage *usage)
 {
@@ -447,6 +461,8 @@ static int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *ha
 		.hid_usage = HID_HP_WAVEFORMSTOP & HID_USAGE,
 	};
 
+	haptic->quirks = hid_haptic_match_quirks(hdev);
+
 	haptic->hdev = hdev;
 	haptic->max_waveform_id = max(2u, haptic->max_waveform_id);
 	haptic->max_duration_id = max(2u, haptic->max_duration_id);
diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index ad2aa267677f..350948ae6866 100644
--- a/drivers/hid/hid-haptic.h
+++ b/drivers/hid/hid-haptic.h
@@ -5,6 +5,7 @@
  *  Copyright (c) 2021 Angela Czubak <acz@semihalf.com>
  */
 
+#include <linux/bits.h>
 #include <linux/compiler.h>
 #include <linux/container_of.h>
 #include <linux/err.h>
@@ -56,6 +57,9 @@ struct hid_haptic_device {
 	u32 *duration_map;
 	u16 press_ordinal;
 	u16 release_ordinal;
+
+	u32 quirks;
+
 	struct hid_haptic_effect *effect;
 	struct hid_haptic_effect stop_effect;
 

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 12/13] HID: haptic: Add HID_HAPTIC_QUIRK_NO_RETRIGGER to disable retriggering
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
                   ` (10 preceding siblings ...)
  2026-11-29 17:52 ` [PATCH v5 11/13] HID: haptic: Add quirk table Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  2026-11-29 17:52 ` [PATCH v5 13/13] HID: haptic: Add quirk flag for Logitech MX Master 4 Rong Zhang
  12 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang

Some devices do not support retriggering. If the relevant fields are
set, they do not trigger at all, as if no FF effect is played.

Add a new quirk flag HID_HAPTIC_QUIRK_NO_RETRIGGER to zero the relevant
fields before filling the effect buf, so that faulty devices is always
triggered normally.

Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/hid/hid-haptic.c | 8 ++++++++
 drivers/hid/hid-haptic.h | 2 ++
 2 files changed, 10 insertions(+)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index 1c314c273316..a8762799249c 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -27,6 +27,9 @@ static u32 hid_haptic_match_quirks(struct hid_device *hdev)
 
 	quirks = id ? id->driver_data : 0;
 
+	if (quirks & HID_HAPTIC_QUIRK_NO_RETRIGGER)
+		hid_notice(hdev, "Device does not support retriggering\n");
+
 	return quirks;
 }
 
@@ -286,6 +289,11 @@ static int hid_haptic_upload_effect(struct input_dev *dev, struct ff_effect *eff
 	if (ordinal < 1)
 		return -EINVAL;
 
+	if (haptic->quirks & HID_HAPTIC_QUIRK_NO_RETRIGGER) {
+		effect->u.haptic.repeat_count = 0;
+		effect->u.haptic.retrigger_period = 0;
+	}
+
 	/* Fill the buffer for the effect id */
 	fill_effect_buf(haptic, &effect->u.haptic, &haptic->effect[effect->id],
 			ordinal);
diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index 350948ae6866..cc6d8e2e5e71 100644
--- a/drivers/hid/hid-haptic.h
+++ b/drivers/hid/hid-haptic.h
@@ -19,6 +19,8 @@
 #define HID_HAPTIC_MODE_DEVICE 0
 #define HID_HAPTIC_MODE_HOST 1
 
+#define HID_HAPTIC_QUIRK_NO_RETRIGGER	BIT(0)
+
 struct hid_haptic_effect {
 	u8 *report_buf;
 	struct input_dev *input_dev;

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 13/13] HID: haptic: Add quirk flag for Logitech MX Master 4
  2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
                   ` (11 preceding siblings ...)
  2026-11-29 17:52 ` [PATCH v5 12/13] HID: haptic: Add HID_HAPTIC_QUIRK_NO_RETRIGGER to disable retriggering Rong Zhang
@ 2026-11-29 17:52 ` Rong Zhang
  12 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 17:52 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
	Dmitry Torokhov
  Cc: linux-input, linux-kernel, Rong Zhang

The Logitech MX Master 4 is a mouse with haptic feedback. Its report
descriptor mimics the example in [1], and thus declares Repeat Count and
Retrigger Period usages.

However, its retriggering functionality is completely broken. When
filling Repeat Count with a non-zero value, it simply ignores the report
and plays nothing.

Add HID_HAPTIC_QUIRK_NO_RETRIGGER for Logitech MX Master 4 to prevent
the said field from breaking the FF device.

Note that the Logitech Bolt receiver is also matched, as no other
Logitech device supports HID haptic.

Link: https://learn.microsoft.com/en-us/windows-hardware/design/component-guidelines/input-haptics-implementation-guide [1]
Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/hid/hid-haptic.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index a8762799249c..9f2eb9caa9b6 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -15,8 +15,15 @@
 #include <linux/types.h>
 
 #include "hid-haptic.h"
+#include "hid-ids.h"
 
 static const struct hid_device_id hid_haptic_quirks[] = {
+	/* Logitech MX Master 4 over Logitech Bolt receiver */
+	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_BOLT_RECEIVER),
+		.driver_data = HID_HAPTIC_QUIRK_NO_RETRIGGER },
+	/* Logitech MX Master 4 over Bluetooth */
+	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb042),
+		.driver_data = HID_HAPTIC_QUIRK_NO_RETRIGGER },
 	{ }
 };
 

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 03/13] HID: haptic, multitouch: Check haptic prerequisite properly
  2026-11-29 17:52 ` [PATCH v5 03/13] HID: haptic, multitouch: Check haptic prerequisite properly Rong Zhang
@ 2026-11-29 18:10   ` sashiko-bot
  2026-11-29 18:14     ` Rong Zhang
  0 siblings, 1 reply; 18+ messages in thread
From: sashiko-bot @ 2026-11-29 18:10 UTC (permalink / raw)
  To: Rong Zhang; +Cc: linux-input, dmitry.torokhov

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] HID: multitouch: inverted guard in mt_input_configured() skips
  haptic init
--

commit 9de6cdc8393811c21adee3b9cba77ef4b2490c87
Author: Rong Zhang <i@rong.moe>

HID: haptic, multitouch: Check haptic prerequisite properly

This commit tightens haptic touchpad prerequisite checks by validating that
all HID_DG_TIPPRESSURE usages share matching force units and annotations on a
single HID_DG_TOUCHPAD input device, and gates runtime haptic calls on the
matched input device.

> diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
> index f63d6f6550bd8..e017eff9b784f 100644
> --- a/drivers/hid/hid-multitouch.c
> +++ b/drivers/hid/hid-multitouch.c
[ ... ]
> @@ -1954,14 +1954,8 @@ static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
>  	struct hid_report *report;
>  	int ret;
>  
> -	if (td->is_haptic_touchpad && (td->mtclass.name == MT_CLS_WIN_8 ||
> -	    td->mtclass.name == MT_CLS_WIN_8_FORCE_MULTI_INPUT ||
> -	    td->mtclass.name == MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE)) {
> -		if (hid_haptic_input_configured(hdev, td->haptic, hi) == 0)
> -			td->is_haptic_touchpad = false;
> -	} else {
> -		td->is_haptic_touchpad = false;
> -	}
> +	if (!td->is_haptic_touchpad)
> +		td->is_haptic_touchpad = !hid_haptic_input_configured(hdev, td->haptic, hi);

[Severity: High]
Does the !td->is_haptic_touchpad check invert the condition and prevent
hid_haptic_input_configured() from initializing haptic touchpads?

During input mapping, mt_touch_input_mapping() already sets
td->is_haptic_touchpad from hid_haptic_check_pressure_unit():

drivers/hid/hid-multitouch.c:mt_touch_input_mapping() {
    ...
		case HID_DG_TIPPRESSURE:
			set_abs(hi->input, ABS_MT_PRESSURE, field,
				cls->sn_pressure);
			td->is_haptic_touchpad =
				hid_haptic_check_pressure_unit(td->haptic,
							       hi, field);
    ...
}

When hid_haptic_check_pressure_unit() succeeds and caches haptic->input_dev,
td->is_haptic_touchpad is already true on entry to mt_input_configured(), so
hid_haptic_input_configured() and hid_haptic_init() are skipped.  Conversely,
when td->is_haptic_touchpad is false on entry, haptic->input_dev is NULL or
ERR_PTR(-EPROTO), so hid_haptic_check_prerequisites() fails and
hid_haptic_init() is unreachable.  In addition, non-haptic touchpads that
report HID_DG_TIPPRESSURE in grams or newtons without haptic trigger reports
are never cleared by hid_haptic_check_prerequisites(), leaving
td->is_haptic_touchpad true on an uninitialized haptic device.

This call is later removed in commit "HID: haptic: Convert to a loadable
plugin module", but it breaks haptic initialization at this point in the
series and in stable backports of this commit.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-hid-haptic-kmod-v5-0-4903f73d8cac@rong.moe?part=3

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 03/13] HID: haptic, multitouch: Check haptic prerequisite properly
  2026-11-29 18:10   ` sashiko-bot
@ 2026-11-29 18:14     ` Rong Zhang
  2026-11-29 18:19       ` Rong Zhang
  0 siblings, 1 reply; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 18:14 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: linux-input, dmitry.torokhov

Hi All,

On Sun, 2026-11-29 at 18:10 +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [High] HID: multitouch: inverted guard in mt_input_configured() skips
>   haptic init
> --
> 
> commit 9de6cdc8393811c21adee3b9cba77ef4b2490c87
> Author: Rong Zhang <i@rong.moe>
> 
> HID: haptic, multitouch: Check haptic prerequisite properly
> 
> This commit tightens haptic touchpad prerequisite checks by validating that
> all HID_DG_TIPPRESSURE usages share matching force units and annotations on a
> single HID_DG_TOUCHPAD input device, and gates runtime haptic calls on the
> matched input device.
> 
> > diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
> > index f63d6f6550bd8..e017eff9b784f 100644
> > --- a/drivers/hid/hid-multitouch.c
> > +++ b/drivers/hid/hid-multitouch.c
> [ ... ]
> > @@ -1954,14 +1954,8 @@ static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
> >  	struct hid_report *report;
> >  	int ret;
> >  
> > -	if (td->is_haptic_touchpad && (td->mtclass.name == MT_CLS_WIN_8 ||
> > -	    td->mtclass.name == MT_CLS_WIN_8_FORCE_MULTI_INPUT ||
> > -	    td->mtclass.name == MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE)) {
> > -		if (hid_haptic_input_configured(hdev, td->haptic, hi) == 0)
> > -			td->is_haptic_touchpad = false;
> > -	} else {
> > -		td->is_haptic_touchpad = false;
> > -	}
> > +	if (!td->is_haptic_touchpad)
> > +		td->is_haptic_touchpad = !hid_haptic_input_configured(hdev, td->haptic, hi);
> 
> [Severity: High]
> Does the !td->is_haptic_touchpad check invert the condition and prevent
> hid_haptic_input_configured() from initializing haptic touchpads?

Whoops, missed this when I was restoring is_haptic_touchpad in the code
path below. I will fix it in the next revision.

Thanks,
Rong

> 
> During input mapping, mt_touch_input_mapping() already sets
> td->is_haptic_touchpad from hid_haptic_check_pressure_unit():
> 
> drivers/hid/hid-multitouch.c:mt_touch_input_mapping() {
>     ...
> 		case HID_DG_TIPPRESSURE:
> 			set_abs(hi->input, ABS_MT_PRESSURE, field,
> 				cls->sn_pressure);
> 			td->is_haptic_touchpad =
> 				hid_haptic_check_pressure_unit(td->haptic,
> 							       hi, field);
>     ...
> }
> 
> When hid_haptic_check_pressure_unit() succeeds and caches haptic->input_dev,
> td->is_haptic_touchpad is already true on entry to mt_input_configured(), so
> hid_haptic_input_configured() and hid_haptic_init() are skipped.  Conversely,
> when td->is_haptic_touchpad is false on entry, haptic->input_dev is NULL or
> ERR_PTR(-EPROTO), so hid_haptic_check_prerequisites() fails and
> hid_haptic_init() is unreachable.  In addition, non-haptic touchpads that
> report HID_DG_TIPPRESSURE in grams or newtons without haptic trigger reports
> are never cleared by hid_haptic_check_prerequisites(), leaving
> td->is_haptic_touchpad true on an uninitialized haptic device.
> 
> This call is later removed in commit "HID: haptic: Convert to a loadable
> plugin module", but it breaks haptic initialization at this point in the
> series and in stable backports of this commit.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 03/13] HID: haptic, multitouch: Check haptic prerequisite properly
  2026-11-29 18:14     ` Rong Zhang
@ 2026-11-29 18:19       ` Rong Zhang
  0 siblings, 0 replies; 18+ messages in thread
From: Rong Zhang @ 2026-11-29 18:19 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: linux-input, dmitry.torokhov

Hi All,

On Mon, 2026-11-30 at 02:14 +0800, Rong Zhang wrote:
> Hi All,
> 
> On Sun, 2026-11-29 at 18:10 +0000, sashiko-bot@kernel.org wrote:
> > Thank you for your contribution! Sashiko AI review found 1 potential
> > issue to consider:
> > - [High] HID: multitouch: inverted guard in mt_input_configured() skips
> >   haptic init
> > --
> > 
> > commit 9de6cdc8393811c21adee3b9cba77ef4b2490c87
> > Author: Rong Zhang <i@rong.moe>
> > 
> > HID: haptic, multitouch: Check haptic prerequisite properly
> > 
> > This commit tightens haptic touchpad prerequisite checks by validating that
> > all HID_DG_TIPPRESSURE usages share matching force units and annotations on a
> > single HID_DG_TOUCHPAD input device, and gates runtime haptic calls on the
> > matched input device.
> > 
> > > diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
> > > index f63d6f6550bd8..e017eff9b784f 100644
> > > --- a/drivers/hid/hid-multitouch.c
> > > +++ b/drivers/hid/hid-multitouch.c
> > [ ... ]
> > > @@ -1954,14 +1954,8 @@ static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
> > >  	struct hid_report *report;
> > >  	int ret;
> > >  
> > > -	if (td->is_haptic_touchpad && (td->mtclass.name == MT_CLS_WIN_8 ||
> > > -	    td->mtclass.name == MT_CLS_WIN_8_FORCE_MULTI_INPUT ||
> > > -	    td->mtclass.name == MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE)) {
> > > -		if (hid_haptic_input_configured(hdev, td->haptic, hi) == 0)
> > > -			td->is_haptic_touchpad = false;
> > > -	} else {
> > > -		td->is_haptic_touchpad = false;
> > > -	}
> > > +	if (!td->is_haptic_touchpad)
> > > +		td->is_haptic_touchpad = !hid_haptic_input_configured(hdev, td->haptic, hi);
> > 
> > [Severity: High]
> > Does the !td->is_haptic_touchpad check invert the condition and prevent
> > hid_haptic_input_configured() from initializing haptic touchpads?
> 
> Whoops, missed this when I was restoring is_haptic_touchpad in the code
> path below. I will fix it in the next revision.

Err, I meant "above". I must be too tired...

Sorry for the confusion,
Rong

> 
> Thanks,
> Rong
> 
> > 
> > During input mapping, mt_touch_input_mapping() already sets
> > td->is_haptic_touchpad from hid_haptic_check_pressure_unit():
> > 
> > drivers/hid/hid-multitouch.c:mt_touch_input_mapping() {
> >     ...
> > 		case HID_DG_TIPPRESSURE:
> > 			set_abs(hi->input, ABS_MT_PRESSURE, field,
> > 				cls->sn_pressure);
> > 			td->is_haptic_touchpad =
> > 				hid_haptic_check_pressure_unit(td->haptic,
> > 							       hi, field);
> >     ...
> > }
> > 
> > When hid_haptic_check_pressure_unit() succeeds and caches haptic->input_dev,
> > td->is_haptic_touchpad is already true on entry to mt_input_configured(), so
> > hid_haptic_input_configured() and hid_haptic_init() are skipped.  Conversely,
> > when td->is_haptic_touchpad is false on entry, haptic->input_dev is NULL or
> > ERR_PTR(-EPROTO), so hid_haptic_check_prerequisites() fails and
> > hid_haptic_init() is unreachable.  In addition, non-haptic touchpads that
> > report HID_DG_TIPPRESSURE in grams or newtons without haptic trigger reports
> > are never cleared by hid_haptic_check_prerequisites(), leaving
> > td->is_haptic_touchpad true on an uninitialized haptic device.
> > 
> > This call is later removed in commit "HID: haptic: Convert to a loadable
> > plugin module", but it breaks haptic initialization at this point in the
> > series and in stable backports of this commit.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 10/13] HID: haptic: Decouple from hid-multitouch and support other devices
  2026-11-29 17:52 ` [PATCH v5 10/13] HID: haptic: Decouple from hid-multitouch and support other devices Rong Zhang
@ 2026-11-29 18:29   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-11-29 18:29 UTC (permalink / raw)
  To: Rong Zhang; +Cc: linux-input, dmitry.torokhov

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] HID: haptic: hid_haptic_check_prerequisites() attaches FF to
  unpopulated input
- [Medium] HID: haptic: broken stylus matching on composite
  hid-multitouch devices
--

commit 5e7c8d80793026eea1a44d7350febde68e4e4d18
Author: Rong Zhang <i@rong.moe>

HID: haptic: Decouple from hid-multitouch and support other devices

This commit allows hid-haptic to fall back to selecting an input device
based on collection type or multi-input quirks when the HID driver has
not chosen one, enabling haptic support on devices other than multitouch
touchpads.

Link: https://usb.org/sites/default/files/hutrr63b_-_haptics_page_redline_0.pdf [1]
Link: https://learn.microsoft.com/en-us/windows-hardware/design/component-guidelines/input-haptics-implementation-guide [2]
Link: https://learn.microsoft.com/en-us/windows-hardware/design/component-guidelines/haptic-pen-implementation-guide [3]

> diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
> index 84b35ec8365a3..eab7bbaaf504a 100644
> --- a/drivers/hid/hid-haptic.c
> +++ b/drivers/hid/hid-haptic.c
> @@ -566,19 +566,55 @@ static int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *ha
>  static int hid_haptic_check_prerequisites(struct hid_device *hdev,
>  					  struct hid_haptic_device *haptic, struct hid_input *hi)
>  {
> +	struct hid_report *report;
> +
>  	if (!haptic->waveform_list_report || !haptic->duration_list_report ||
>  	    !haptic->manual_trigger_report)
>  		return -ENOENT;
>  
> -	if (IS_ERR_OR_NULL(haptic->input_dev))
> -		return PTR_ERR(haptic->input_dev) ?: -EXDEV;
> -
>  	/* Check the prerequisites only. */
>  	if (!hi)
>  		return 1;
>  
> -	if (haptic->input_dev == hi->input)
> -		return 0;
> +	/*
> +	 * Honor the HID driver's choice.
> +	 *
> +	 * If the driver didn't choose an input device or failed its
> +	 * prerequisites check, initialize the haptic device without the
> +	 * integration with the driver.
> +	 */
> +	if (IS_ERR_OR_NULL(haptic->input_dev))
> +		haptic->input_dev = NULL;
> +	else
> +		return haptic->input_dev != hi->input;
> +
> +	/*
> +	 * A Simple Haptic Controller may be either a Collection Application
> +	 * (CA) or a Collection Logical (CL):
> +	 *
> +	 * A CA is a dedicated HID application, i.e., not associated with other
> +	 * HID applications. Choose the first HID input device to attach the FF
> +	 * device, as other FF drivers do. Haptic Mouse usually falls into this
> +	 * category.
> +	 *
> +	 * A CL is contained within another HID application. The HID input
> +	 * device handling the manual trigger report must be the one handling
> +	 * the associated HID application. Haptic Touchpad and Stylus usually
> +	 * fall into this category.
> +	 *
> +	 * When HID_QUIRK_MULTI_INPUT is set, the quirky collection hierarchy
> +	 * makes no sense anyway, so the first input device is chosen instead.
> +	 */
> +	if (haptic->manual_trigger_report->application == HID_HP_SIMPLECONTROLLER ||
> +	    hdev->quirks & HID_QUIRK_MULTI_INPUT) {
> +		if (list_is_first(&hi->list, &hdev->inputs))
> +			return 0;

[Severity: High]
Can this attach the force-feedback device to an unpopulated hid_input?

In hidinput_connect(), hid_haptic_input_configured() runs before
hidinput_has_been_populated() removes and frees empty hid_input entries
from hdev->inputs:

drivers/hid/hid-input.c:hidinput_connect() {
    ...
		if (haptic_ff)
			haptic_ff = hid_haptic_input_configured(hid, hidinput) >= 0;

		if (!hidinput_has_been_populated(hidinput)) {
			/* no need to register an input device not populated */
			hidinput_cleanup_hidinput(hid, hidinput);
			continue;
		}
    ...
}

If the first hid_input in hdev->inputs has no mapped input usages (such
as an unmapped vendor collection or battery report), matching it here
causes hid_haptic_init() to set EV_FF and FF_HAPTIC on hi->input.
hidinput_has_been_populated() then sees EV_FF set and registers an empty,
EV_FF-only input device while leaving the actual input device without
haptic support.  Checking hidinput_has_been_populated(hi) before running
hid_haptic_input_configured() (or inside hid_haptic_check_prerequisites())
would avoid selecting unpopulated hid_input instances.

> +	} else {
> +		list_for_each_entry(report, &hi->reports, hidinput_list) {
> +			if (report == haptic->manual_trigger_report)
> +				return 0;
> +		}
> +	}

[Severity: Medium]
Does matching hi->reports against haptic->manual_trigger_report work for a
haptic stylus on a composite hid-multitouch device?

On composite touchscreen and pen devices bound to hid-multitouch
(HID_QUIRK_INPUT_PER_APP with maxapplication > 1), the pen top-level
collection has application HID_DG_PEN, while its stylus input fields have
physical usage HID_DG_STYLUS.  When hidinput_connect() processes
HID_INPUT_REPORT first, mt_input_mapping() changes hi->application on the
pen hid_input from HID_DG_PEN to HID_DG_STYLUS:

drivers/hid/hid-multitouch.c:mt_input_mapping() {
    ...
	if (field->physical == HID_DG_STYLUS)
		hi->application = HID_DG_STYLUS;
    ...
}

When hidinput_connect() then processes HID_OUTPUT_REPORT,
manual_trigger_report still has report->application == HID_DG_PEN, so
hidinput_match_application() does not match the stylus hid_input (now
HID_DG_STYLUS) and instead allocates a second, unpopulated hid_input for
manual_trigger_report.  Searching hi->reports here then matches that
second hid_input rather than the stylus hid_input, registering a separate
EV_FF-only input device and leaving the stylus input device without haptic
support.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-hid-haptic-kmod-v5-0-4903f73d8cac@rong.moe?part=10

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2026-11-29 18:29 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 17:52 [PATCH v5 00/13] HID: Add hid-plugin framework & convert hid-haptic into a generic plugin Rong Zhang
2026-11-29 17:52 ` [PATCH v5 01/13] HID: haptic: Get rid of input_ff_destroy() in error paths Rong Zhang
2026-11-29 17:52 ` [PATCH v5 02/13] HID: haptic: Fix use-after-free when draining workqueue Rong Zhang
2026-11-29 17:52 ` [PATCH v5 03/13] HID: haptic, multitouch: Check haptic prerequisite properly Rong Zhang
2026-11-29 18:10   ` sashiko-bot
2026-11-29 18:14     ` Rong Zhang
2026-11-29 18:19       ` Rong Zhang
2026-11-29 17:52 ` [PATCH v5 04/13] HID: haptic: Postpone GET_REPORT to init Rong Zhang
2026-11-29 17:52 ` [PATCH v5 05/13] HID: Add hid-plugin framework for loadable plugin modules Rong Zhang
2026-11-29 17:52 ` [PATCH v5 06/13] HID: haptic: Convert to a loadable plugin module Rong Zhang
2026-11-29 17:52 ` [PATCH v5 07/13] HID: haptic: Adopt pr_fmt, dev_fmt and hid_*() printk helpers Rong Zhang
2026-11-29 17:52 ` [PATCH v5 08/13] HID: Add Ordinal page defines Rong Zhang
2026-11-29 17:52 ` [PATCH v5 09/13] HID: haptic: Handle list reports separately and make auto trigger optional Rong Zhang
2026-11-29 17:52 ` [PATCH v5 10/13] HID: haptic: Decouple from hid-multitouch and support other devices Rong Zhang
2026-11-29 18:29   ` sashiko-bot
2026-11-29 17:52 ` [PATCH v5 11/13] HID: haptic: Add quirk table Rong Zhang
2026-11-29 17:52 ` [PATCH v5 12/13] HID: haptic: Add HID_HAPTIC_QUIRK_NO_RETRIGGER to disable retriggering Rong Zhang
2026-11-29 17:52 ` [PATCH v5 13/13] HID: haptic: Add quirk flag for Logitech MX Master 4 Rong Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox