* [PATCH v4 1/9] HID: haptic: Get rid of input_ff_destroy() in error paths
2026-11-24 16:44 [PATCH v4 0/9] HID: Add hid-plugin framework & convert haptic into a plugin Rong Zhang
@ 2026-11-24 16:44 ` Rong Zhang
2026-11-24 17:04 ` sashiko-bot
2026-11-24 16:44 ` [PATCH v4 2/9] HID: haptic: Fix use-after-free when draining workqueue Rong Zhang
` (7 subsequent siblings)
8 siblings, 1 reply; 13+ messages in thread
From: Rong Zhang @ 2026-11-24 16:44 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: 083a4f3f3cc7 ("HID: Kconfig: Fix build error from CONFIG_HID_HAPTIC")
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] 13+ messages in thread* Re: [PATCH v4 1/9] HID: haptic: Get rid of input_ff_destroy() in error paths
2026-11-24 16:44 ` [PATCH v4 1/9] HID: haptic: Get rid of input_ff_destroy() in error paths Rong Zhang
@ 2026-11-24 17:04 ` sashiko-bot
2026-11-24 17:07 ` Rong Zhang
0 siblings, 1 reply; 13+ messages in thread
From: sashiko-bot @ 2026-11-24 17:04 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:
- [Low] HID: haptic: inaccurate Fixes trailer in hid_haptic_init() fix
--
commit e6d3c5552e5a0755862fb250069c54d74d75f7cb
Author: Rong Zhang <i@rong.moe>
> HID: haptic: Get rid of input_ff_destroy() in error paths
>
> 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: 083a4f3f3cc7 ("HID: Kconfig: Fix build error from CONFIG_HID_HAPTIC")
> 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>
[Severity: Low]
083a4f3f3cc7 ("HID: Kconfig: Fix build error from CONFIG_HID_HAPTIC") only
changed CONFIG_HID_HAPTIC from tristate to bool in drivers/hid/Kconfig.
The broken input_free error path and double module_put() in
hid_haptic_init() were introduced in 344ff3584957 ("HID: haptic:
initialize haptic device").
Should the Fixes: tag point to 344ff3584957 ("HID: haptic: initialize
haptic device") instead?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-hid-haptic-kmod-v4-0-03713a89f656@rong.moe?part=1
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v4 1/9] HID: haptic: Get rid of input_ff_destroy() in error paths
2026-11-24 17:04 ` sashiko-bot
@ 2026-11-24 17:07 ` Rong Zhang
0 siblings, 0 replies; 13+ messages in thread
From: Rong Zhang @ 2026-11-24 17:07 UTC (permalink / raw)
To: sashiko-reviews; +Cc: linux-input, dmitry.torokhov
Hi All,
On Tue, 2026-11-24 at 17:04 +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Low] HID: haptic: inaccurate Fixes trailer in hid_haptic_init() fix
> --
>
> commit e6d3c5552e5a0755862fb250069c54d74d75f7cb
> Author: Rong Zhang <i@rong.moe>
>
> > HID: haptic: Get rid of input_ff_destroy() in error paths
> >
> > 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: 083a4f3f3cc7 ("HID: Kconfig: Fix build error from CONFIG_HID_HAPTIC")
> > 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>
>
> [Severity: Low]
> 083a4f3f3cc7 ("HID: Kconfig: Fix build error from CONFIG_HID_HAPTIC") only
> changed CONFIG_HID_HAPTIC from tristate to bool in drivers/hid/Kconfig.
> The broken input_free error path and double module_put() in
> hid_haptic_init() were introduced in 344ff3584957 ("HID: haptic:
> initialize haptic device").
>
> Should the Fixes: tag point to 344ff3584957 ("HID: haptic: initialize
> haptic device") instead?
No. This commit removes try_module_get()/module_put(), and thus depends
on the current Fixes: commit.
Thanks,
Rong
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v4 2/9] HID: haptic: Fix use-after-free when draining workqueue
2026-11-24 16:44 [PATCH v4 0/9] HID: Add hid-plugin framework & convert haptic into a plugin Rong Zhang
2026-11-24 16:44 ` [PATCH v4 1/9] HID: haptic: Get rid of input_ff_destroy() in error paths Rong Zhang
@ 2026-11-24 16:44 ` Rong Zhang
2026-11-24 16:44 ` [PATCH v4 3/9] HID: haptic, multitouch: Check haptic prerequisite properly Rong Zhang
` (6 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Rong Zhang @ 2026-11-24 16:44 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 putting the HID device and
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] 13+ messages in thread* [PATCH v4 3/9] HID: haptic, multitouch: Check haptic prerequisite properly
2026-11-24 16:44 [PATCH v4 0/9] HID: Add hid-plugin framework & convert haptic into a plugin Rong Zhang
2026-11-24 16:44 ` [PATCH v4 1/9] HID: haptic: Get rid of input_ff_destroy() in error paths Rong Zhang
2026-11-24 16:44 ` [PATCH v4 2/9] HID: haptic: Fix use-after-free when draining workqueue Rong Zhang
@ 2026-11-24 16:44 ` Rong Zhang
2026-11-24 16:44 ` [PATCH v4 4/9] HID: haptic, multitouch: Fix haptic device double-free and unnecessary allocation Rong Zhang
` (5 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Rong Zhang @ 2026-11-24 16:44 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.
Fix them by storing the relevant HID_DG_TOUCHPAD input device (must have
a pressure sensor) during hid_haptic_check_pressure_unit(), with proper
sanity checks that reject annotation mismatches and duplicated
HID_DG_TOUCHPAD input devices. Later, hid_haptic_input_configured()
continues the initialization only if the input device matches the
previously stored one.
With that, the td->is_haptic_touchpad is converted into a one-way (false
=> true) flipping manner. All calls to hid_haptic_*() during the
probe-time procedures are now guarded by the nullability of td->haptic
instead of td->is_haptic_touchpad or no guard, preparing to skip
allocating td->haptic in !CONFIG_HID_HAPTIC builds in the following
changes.
At runtime, relevant haptic methods are called only if the event source
is the same input device as the stored 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 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 | 65 ++++++++++++++++++++++++++------------------
drivers/hid/hid-haptic.h | 35 ++++++++++++++----------
drivers/hid/hid-multitouch.c | 36 ++++++++++++------------
3 files changed, 77 insertions(+), 59 deletions(-)
diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index 8312b8edc847..3ef2bce67671 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -48,17 +48,40 @@ 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,
+void 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);
- return true;
+ if (hi->application != HID_DG_TOUCHPAD)
+ return;
+
+ if (IS_ERR(haptic->input_dev))
+ return;
+
+ 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;
}
- return false;
+
+ if (hid_haptic_connected_to_input(haptic, 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;
+
+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);
}
EXPORT_SYMBOL_GPL(hid_haptic_check_pressure_unit);
@@ -78,28 +101,17 @@ 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)
+bool 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;
+ if (IS_ERR_OR_NULL(haptic->input_dev) ||
+ !hid_haptic_connected_to_input(haptic, hi->input) ||
+ !haptic->auto_trigger_report || !haptic->manual_trigger_report)
+ return false;
__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;
+ return hid_haptic_init(hdev, haptic, hi->input) == 0;
}
EXPORT_SYMBOL_GPL(hid_haptic_input_configured);
@@ -519,6 +531,7 @@ int hid_haptic_init(struct hid_device *hdev,
kfree(haptic->hid_usage_map);
haptic->hid_usage_map = NULL;
exit:
+ dev_warn(&hdev->dev, "Cannot allocate haptic for %s: %d\n", hdev->name, ret);
return ret;
}
EXPORT_SYMBOL_GPL(hid_haptic_init);
diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index 6332991a7844..cd4d9c74d2dc 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,20 +54,25 @@ 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
*usage);
-bool hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
+void 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);
+bool 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);
@@ -76,6 +80,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,
@@ -83,11 +93,9 @@ void hid_haptic_feature_mapping(struct hid_device *hdev,
*usage)
{}
static inline
-bool hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
+void hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
struct hid_input *hi, struct hid_field *field)
-{
- return false;
-}
+{}
static inline
int hid_haptic_input_mapping(struct hid_device *hdev,
struct hid_haptic_device *haptic,
@@ -98,11 +106,10 @@ int hid_haptic_input_mapping(struct hid_device *hdev,
return 0;
}
static inline
-int hid_haptic_input_configured(struct hid_device *hdev,
- struct hid_haptic_device *haptic,
- struct hid_input *hi)
+bool hid_haptic_input_configured(struct hid_device *hdev,
+ struct hid_haptic_device *haptic, struct hid_input *hi)
{
- return 0;
+ return false;
}
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 3afca07025ea..5eea774dee44 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -605,7 +605,8 @@ static void mt_feature_mapping(struct hid_device *hdev,
break;
}
- hid_haptic_feature_mapping(hdev, td->haptic, field, usage);
+ if (td->haptic)
+ hid_haptic_feature_mapping(hdev, td->haptic, field, usage);
}
static void set_abs(struct input_dev *input, unsigned int code,
@@ -961,9 +962,8 @@ 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);
+ if (td->haptic)
+ hid_haptic_check_pressure_unit(td->haptic, hi, field);
MT_STORE_FIELD(p);
return 1;
case HID_DG_SCANTIME:
@@ -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)
@@ -1553,10 +1553,11 @@ 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;
+ if (td->haptic) {
+ ret = hid_haptic_input_mapping(hdev, td->haptic, hi, field, usage, bit, max);
+ if (ret)
+ return ret;
+ }
/* let hid-core decide for the others */
return 0;
@@ -1954,14 +1955,11 @@ 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->haptic && !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))
+ 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] 13+ messages in thread* [PATCH v4 4/9] HID: haptic, multitouch: Fix haptic device double-free and unnecessary allocation
2026-11-24 16:44 [PATCH v4 0/9] HID: Add hid-plugin framework & convert haptic into a plugin Rong Zhang
` (2 preceding siblings ...)
2026-11-24 16:44 ` [PATCH v4 3/9] HID: haptic, multitouch: Check haptic prerequisite properly Rong Zhang
@ 2026-11-24 16:44 ` Rong Zhang
2026-11-24 16:44 ` [PATCH v4 5/9] HID: haptic: Postpone GET_REPORT to init Rong Zhang
` (4 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Rong Zhang @ 2026-11-24 16:44 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
Dmitry Torokhov
Cc: linux-input, linux-kernel, Rong Zhang, stable
The haptic device is allocated by devm_kzalloc(), which will be freed by
devres on HID device release. At that time, the ff core has already
freed it on input device release previously, causing double-free.
Allocate the haptic device using kzalloc_obj() to ensure its release can
be safely delegates to the ff core.
The allocation code is also moved to hid-haptic.h, so that it can be
replaced by a stub in !CONFIG_HID_HAPTIC builds, eliminating the
meaningless alloc-and-free sequence.
Fixes: 8d0bf7908b5a ("HID: multitouch: add haptic multitouch support")
Cc: stable@vger.kernel.org
Signed-off-by: Rong Zhang <i@rong.moe>
---
Changes in v3:
- Correct the Fixes: tag (thanks Sashiko)
---
drivers/hid/hid-haptic.h | 17 +++++++++++++++++
drivers/hid/hid-multitouch.c | 39 +++++++++++++++++++++++++++++----------
2 files changed, 46 insertions(+), 10 deletions(-)
diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index cd4d9c74d2dc..7878cd73ccdb 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/err.h>
#include <linux/hid.h>
#define HID_HAPTIC_ORDINAL_WAVEFORMNONE 1
@@ -54,6 +55,17 @@ struct hid_haptic_device {
};
#if IS_ENABLED(CONFIG_HID_HAPTIC)
+static inline struct hid_haptic_device *hid_haptic_alloc(struct hid_device *hdev)
+{
+ struct hid_haptic_device *haptic;
+
+ haptic = kzalloc_obj(struct hid_haptic_device);
+ if (haptic)
+ haptic->hdev = hdev;
+
+ return haptic;
+}
+
static inline bool hid_haptic_connected_to_input(struct hid_haptic_device *haptic,
struct input_dev *input)
{
@@ -80,6 +92,11 @@ 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 struct hid_haptic_device *hid_haptic_alloc(struct hid_device *hdev)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+
static inline bool hid_haptic_connected_to_input(struct hid_haptic_device *haptic,
struct input_dev *input)
{
diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index 5eea774dee44..a9c8396c804e 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -34,6 +34,7 @@
#include <linux/bitmap.h>
#include <linux/bits.h>
#include <linux/device.h>
+#include <linux/err.h>
#include <linux/hid.h>
#include <linux/module.h>
#include <linux/slab.h>
@@ -2129,11 +2130,18 @@ 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)
+
+ td->haptic = hid_haptic_alloc(hdev);
+ if (IS_ERR(td->haptic)) {
+ ret = PTR_ERR(td->haptic);
+ if (ret == -EOPNOTSUPP)
+ td->haptic = NULL;
+ else
+ return ret;
+ } else if (!td->haptic) {
return -ENOMEM;
+ }
- td->haptic->hdev = hdev;
td->hdev = hdev;
td->mtclass = *mtclass;
td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
@@ -2178,12 +2186,14 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
ret = hid_parse(hdev);
if (ret != 0)
- return ret;
+ goto out;
if (mtclass->name == MT_CLS_APPLE_TOUCHBAR &&
!hid_find_field(hdev, HID_INPUT_REPORT,
- HID_DG_TOUCHPAD, HID_DG_TRANSDUCER_INDEX))
- return -ENODEV;
+ HID_DG_TOUCHPAD, HID_DG_TRANSDUCER_INDEX)) {
+ ret = -ENODEV;
+ goto out;
+ }
if (mtclass->quirks & MT_QUIRK_FIX_CONST_CONTACT_ID)
mt_fix_const_fields(hdev, HID_DG_CONTACTID);
@@ -2193,7 +2203,7 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
if (ret)
- return ret;
+ goto out;
ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
if (ret)
@@ -2202,10 +2212,19 @@ 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);
+ ret = 0;
- return 0;
+out:
+ /*
+ * Don't double free when the haptic device has been delegated to the ff
+ * core.
+ */
+ if (td->haptic && !td->is_haptic_touchpad) {
+ kfree(td->haptic);
+ td->haptic = NULL;
+ }
+
+ return ret;
}
static int mt_suspend(struct hid_device *hdev, pm_message_t state)
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v4 5/9] HID: haptic: Postpone GET_REPORT to init
2026-11-24 16:44 [PATCH v4 0/9] HID: Add hid-plugin framework & convert haptic into a plugin Rong Zhang
` (3 preceding siblings ...)
2026-11-24 16:44 ` [PATCH v4 4/9] HID: haptic, multitouch: Fix haptic device double-free and unnecessary allocation Rong Zhang
@ 2026-11-24 16:44 ` Rong Zhang
2026-11-24 16:44 ` [PATCH v4 6/9] HID: haptic: Convert simple methods into static inline ones Rong Zhang
` (3 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Rong Zhang @ 2026-11-24 16:44 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>
---
drivers/hid/hid-haptic.c | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index 3ef2bce67671..ba3d94fb9334 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;
@@ -147,6 +140,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;
@@ -450,6 +446,12 @@ int hid_haptic_init(struct hid_device *hdev,
HID_HP_WAVEFORMSTOP & HID_USAGE;
mutex_init(&haptic->auto_trigger_mutex);
+
+ 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);
+
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] 13+ messages in thread* [PATCH v4 6/9] HID: haptic: Convert simple methods into static inline ones
2026-11-24 16:44 [PATCH v4 0/9] HID: Add hid-plugin framework & convert haptic into a plugin Rong Zhang
` (4 preceding siblings ...)
2026-11-24 16:44 ` [PATCH v4 5/9] HID: haptic: Postpone GET_REPORT to init Rong Zhang
@ 2026-11-24 16:44 ` Rong Zhang
2026-11-24 16:44 ` [PATCH v4 7/9] HID: Add hid-plugin framework for loadable plugin modules Rong Zhang
` (2 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Rong Zhang @ 2026-11-24 16:44 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
Dmitry Torokhov
Cc: linux-input, linux-kernel, Rong Zhang
Some methods of hid-haptic are meant to check the prerequisites before
really initializing the haptic device. Most of them are quite simple and
straightforward. To prepare for the following changes that convert
hid-haptic into a loadable plugin module, factor them out of the module
and convert them into static inline functions.
Some unused declarations and stubs in the header are also removed, i.e.,
hid_haptic_handle_press_release(), hid_haptic_reset(),
hid_haptic_handle_input(). They seemed to be remnants from the patch
revisioning process.
No functional change intended.
Signed-off-by: Rong Zhang <i@rong.moe>
---
drivers/hid/hid-haptic.c | 111 ---------------------------------------
drivers/hid/hid-haptic.h | 131 +++++++++++++++++++++++++++++++++++++----------
2 files changed, 104 insertions(+), 138 deletions(-)
diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index ba3d94fb9334..cbe834a77fd4 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -10,104 +10,6 @@
#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)
-{
- u16 usage_hid;
-
- if (usage->hid == HID_HP_AUTOTRIGGER) {
- if (usage->usage_index >= field->report_count) {
- dev_err(&hdev->dev,
- "HID_HP_AUTOTRIGGER out of range\n");
- return;
- }
- haptic->auto_trigger_report = field->report;
- } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ORDINAL) {
- usage_hid = usage->hid & HID_USAGE;
- switch (field->logical) {
- case HID_HP_WAVEFORMLIST:
- if (usage_hid > haptic->max_waveform_id)
- haptic->max_waveform_id = usage_hid;
- break;
- case HID_HP_DURATIONLIST:
- if (usage_hid > haptic->max_duration_id)
- haptic->max_duration_id = usage_hid;
- break;
- default:
- break;
- }
- }
-}
-EXPORT_SYMBOL_GPL(hid_haptic_feature_mapping);
-
-void hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
- struct hid_input *hi, struct hid_field *field)
-{
- if (hi->application != HID_DG_TOUCHPAD)
- return;
-
- if (IS_ERR(haptic->input_dev))
- return;
-
- 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;
- }
-
- if (hid_haptic_connected_to_input(haptic, 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;
-
-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);
-}
-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);
-
-bool hid_haptic_input_configured(struct hid_device *hdev,
- struct hid_haptic_device *haptic, struct hid_input *hi)
-{
- if (IS_ERR_OR_NULL(haptic->input_dev) ||
- !hid_haptic_connected_to_input(haptic, hi->input) ||
- !haptic->auto_trigger_report || !haptic->manual_trigger_report)
- return false;
-
- __set_bit(INPUT_PROP_PRESSUREPAD, hi->input->propbit);
-
- return hid_haptic_init(hdev, haptic, hi->input) == 0;
-}
-EXPORT_SYMBOL_GPL(hid_haptic_input_configured);
-
static void parse_auto_trigger_field(struct hid_haptic_device *haptic,
struct hid_field *field)
{
@@ -537,16 +439,3 @@ int hid_haptic_init(struct hid_device *hdev,
return ret;
}
EXPORT_SYMBOL_GPL(hid_haptic_init);
-
-void hid_haptic_pressure_reset(struct hid_haptic_device *haptic)
-{
- haptic->pressure_sum = 0;
-}
-EXPORT_SYMBOL_GPL(hid_haptic_pressure_reset);
-
-void hid_haptic_pressure_increase(struct hid_haptic_device *haptic,
- __s32 pressure)
-{
- haptic->pressure_sum += pressure;
-}
-EXPORT_SYMBOL_GPL(hid_haptic_pressure_increase);
diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index 7878cd73ccdb..fd7093e1764a 100644
--- a/drivers/hid/hid-haptic.h
+++ b/drivers/hid/hid-haptic.h
@@ -72,25 +72,112 @@ static inline bool hid_haptic_connected_to_input(struct hid_haptic_device *hapti
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);
-void 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);
-bool hid_haptic_input_configured(struct hid_device *hdev,
- struct hid_haptic_device *haptic, struct hid_input *hi);
+static inline 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;
+
+ if (usage->hid == HID_HP_AUTOTRIGGER) {
+ if (usage->usage_index >= field->report_count) {
+ dev_err(&hdev->dev, "HID_HP_AUTOTRIGGER out of range\n");
+ return;
+ }
+
+ haptic->auto_trigger_report = field->report;
+ } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ORDINAL) {
+ usage_hid = usage->hid & HID_USAGE;
+ switch (field->logical) {
+ case HID_HP_WAVEFORMLIST:
+ if (usage_hid > haptic->max_waveform_id)
+ haptic->max_waveform_id = usage_hid;
+ break;
+ case HID_HP_DURATIONLIST:
+ if (usage_hid > haptic->max_duration_id)
+ haptic->max_duration_id = usage_hid;
+ break;
+ default:
+ break;
+ }
+ }
+}
+
+static inline void hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
+ struct hid_input *hi, struct hid_field *field)
+{
+ if (hi->application != HID_DG_TOUCHPAD)
+ return;
+
+ if (IS_ERR(haptic->input_dev))
+ return;
+
+ 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;
+ }
+
+ if (hid_haptic_connected_to_input(haptic, 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;
+
+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);
+}
+
+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)
+{
+ 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;
+}
+
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);
+
+static inline bool hid_haptic_input_configured(struct hid_device *hdev,
+ struct hid_haptic_device *haptic,
+ struct hid_input *hi)
+{
+ if (IS_ERR_OR_NULL(haptic->input_dev) ||
+ !hid_haptic_connected_to_input(haptic, hi->input) ||
+ !haptic->auto_trigger_report || !haptic->manual_trigger_report)
+ return false;
+
+ __set_bit(INPUT_PROP_PRESSUREPAD, hi->input->propbit);
+
+ return hid_haptic_init(hdev, haptic, hi->input) == 0;
+}
+
+static inline void hid_haptic_pressure_reset(struct hid_haptic_device *haptic)
+{
+ haptic->pressure_sum = 0;
+}
+
+static inline void hid_haptic_pressure_increase(struct hid_haptic_device *haptic,
+ __s32 pressure)
+{
+ haptic->pressure_sum += pressure;
+}
#else
static inline struct hid_haptic_device *hid_haptic_alloc(struct hid_device *hdev)
{
@@ -129,22 +216,12 @@ bool hid_haptic_input_configured(struct hid_device *hdev,
return false;
}
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)
{
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)
-{
- return false;
-}
-static inline
void hid_haptic_pressure_reset(struct hid_haptic_device *haptic) {}
static inline
void hid_haptic_pressure_increase(struct hid_haptic_device *haptic,
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v4 7/9] HID: Add hid-plugin framework for loadable plugin modules
2026-11-24 16:44 [PATCH v4 0/9] HID: Add hid-plugin framework & convert haptic into a plugin Rong Zhang
` (5 preceding siblings ...)
2026-11-24 16:44 ` [PATCH v4 6/9] HID: haptic: Convert simple methods into static inline ones Rong Zhang
@ 2026-11-24 16:44 ` Rong Zhang
2026-11-24 16:44 ` [PATCH v4 8/9] HID: haptic: Convert into a loadable plugin module Rong Zhang
2026-11-24 16:44 ` [PATCH v4 9/9] HID: haptic: Remove stubs in favor of inlined dead code elimination Rong Zhang
8 siblings, 0 replies; 13+ messages in thread
From: Rong Zhang @ 2026-11-24 16:44 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, which is only loaded when the
driver finds that the device needs it.
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 request it accordingly. The framework
aims to be lightweight. It loads the plugin module when needed. Once
loaded (or if the plugin is builtin), requesting the plugin is just an
RCU dereference plusing an module refcount increment.
When a plugin is disabled, its id maps to HID_PLUGIN_DISABLED, and calls
to the plugin helper functions are eliminated by the compiler.
I volunteer to maintain the framework as well as its 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 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 | 14 +++++
drivers/hid/Makefile | 2 +
drivers/hid/hid-plugin.c | 105 +++++++++++++++++++++++++++++++++++
drivers/hid/hid-plugin.h | 141 +++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 268 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 44540a1eb2b8..24bcc700ccfe 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..604683b4a4a9 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -92,6 +92,20 @@ config HID_GENERIC
If unsure, say Y.
+menuconfig HID_PLUGIN
+ bool "HID plugins"
+ help
+ Support for HID plugins.
+
+ Say Y here if you want to enhance HID drivers with additional
+ functionalities provided by corresponding plugins.
+
+ Plugins can be either builtin or modules. A plugin compiled as a module
+ is made available to HID drivers by automatically loading it when
+ requested.
+
+ If unsure, say N.
+
config HID_HAPTIC
bool "Haptic touchpad support"
default n
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index 43a8eb0f389b..b41aa3f52a94 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) += hid-plugin.o
+
hid-$(CONFIG_HID_HAPTIC) += hid-haptic.o
obj-$(CONFIG_HID_BPF) += bpf/
diff --git a/drivers/hid/hid-plugin.c b/drivers/hid/hid-plugin.c
new file mode 100644
index 000000000000..288dedd21253
--- /dev/null
+++ b/drivers/hid/hid-plugin.c
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * HID plugin helpers
+ *
+ * Copyright (c) 2026 Rong Zhang <i@rong.moe>
+ */
+
+#define pr_fmt(fmt) "hid-plugin: " fmt
+
+#include <linux/bug.h>
+#include <linux/cleanup.h>
+#include <linux/compiler.h>
+#include <linux/err.h>
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/rcupdate.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 struct hid_plugin *hid_plugin_try_module_get(enum hid_plugin_id id)
+{
+ guard(rcu)();
+
+ struct hid_plugin *plugin = rcu_dereference(hid_plugins[id]);
+
+ if (!plugin)
+ return NULL;
+
+ /* Is the module unloading? */
+ return try_module_get(plugin->owner) ? plugin : ERR_PTR(-ENXIO);
+}
+
+struct hid_plugin *__hid_plugin_get(enum hid_plugin_id id)
+{
+ struct hid_plugin *plugin;
+ const char *mod_name;
+ int ret;
+
+ /*
+ * Compile the function as a stub if plugin support is enabled but no
+ * plugin is actually enabled. Utilizing dead code elimination is the
+ * best thing we can do as it's impossible to evaluate an enumeration in
+ * macros.
+ */
+ if (NR_HID_PLUGINS <= 0)
+ return ERR_PTR(-ENOENT);
+
+ plugin = hid_plugin_try_module_get(id);
+ if (plugin)
+ return plugin;
+
+ mod_name = hid_plugin_mod_names[id];
+ if (WARN_ON(!mod_name)) /* Huh? Builtin plugins can't reach here. */
+ return ERR_PTR(-ENXIO);
+
+ ret = request_module(mod_name);
+ if (ret) {
+ pr_warn("failed to load [%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(-ENXIO);
+}
+EXPORT_SYMBOL_GPL(__hid_plugin_get);
+
+int __hid_plugin_register(enum hid_plugin_id id, struct hid_plugin *plugin)
+{
+ if (NR_HID_PLUGINS <= 0)
+ return -ENOENT;
+
+ /*
+ * The corresponding plugin is the only writer of hid_plugins[id], so
+ * there is no need to protect the our access during registration, as
+ * any reader in prior must have got NULL, i.e., there is nothing to
+ * protect or synchronize.
+ *
+ * However, try our best to do some sanity checks.
+ */
+ if (WARN_ON(rcu_access_pointer(hid_plugins[id])))
+ return -EEXIST;
+
+ rcu_assign_pointer(hid_plugins[id], plugin);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(__hid_plugin_register);
+
+void __hid_plugin_unregister(enum hid_plugin_id id, struct hid_plugin *plugin)
+{
+ if (NR_HID_PLUGINS <= 0)
+ return;
+
+ if (WARN_ON(rcu_access_pointer(hid_plugins[id]) != plugin))
+ return;
+
+ 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..e7bc98639d9f
--- /dev/null
+++ b/drivers/hid/hid-plugin.h
@@ -0,0 +1,141 @@
+/* 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/bug.h>
+#include <linux/build_bug.h>
+#include <linux/compiler.h>
+#include <linux/err.h>
+#include <linux/kconfig.h>
+#include <linux/init.h>
+#include <linux/module.h>
+
+enum hid_plugin_id {
+ HID_PLUGIN_DISABLED = -1,
+
+ NR_HID_PLUGINS,
+};
+
+#if !IS_ENABLED(CONFIG_HID_PLUGIN)
+static_assert(NR_HID_PLUGINS == 0);
+#endif
+
+struct hid_plugin {
+ struct module *owner;
+};
+
+struct hid_plugin *__hid_plugin_get(enum hid_plugin_id id);
+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_enabled() - Check if an HID plugin is enabled.
+ * @id: Unique ID of the desired HID plugin
+ *
+ * Return: boolean.
+ */
+static __always_inline bool hid_plugin_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_get() - Get an HID plugin and increment its refcount.
+ * @id: Unique ID of the desired HID plugin
+ *
+ * If the plugin is builtin or loaded, a pointer to the plugin is returned with
+ * the plugin's module refcount incremented. If the plugin is unloading during
+ * the process, ERR_PTR(-ENXIO) is returned.
+ *
+ * If the plugin is disabled, ERR_PTR(-ENOENT) is returned.
+ *
+ * Otherwise, try loading the module with request_module() and then increment
+ * its refcount. If the plugin fails to load, ERR_PTR(-ENXIO) is returned.
+ *
+ * Return: The HID plugin corresponding to @id on success, or an ERR_PTR().
+ * Never NULL.
+ */
+static __always_inline struct hid_plugin *hid_plugin_get(enum hid_plugin_id id)
+{
+ if (hid_plugin_enabled(id))
+ return __hid_plugin_get(id);
+
+ return ERR_PTR(-ENOENT);
+}
+
+/**
+ * hid_plugin_put() - Decrement an HID plugin's refcount.
+ * @id: Unique ID of @plugin
+ * @plugin: Pointer to the HID plugin
+ *
+ * Decrement @plugin's refcount, unless it's disabled.
+ */
+static __always_inline void hid_plugin_put(enum hid_plugin_id id, struct hid_plugin *plugin)
+{
+ if (hid_plugin_enabled(id) && !IS_ERR_OR_NULL(plugin))
+ module_put(plugin->owner);
+}
+
+/**
+ * hid_plugin_register() - Register an HID plugin.
+ * @id: Unique ID of @plugin
+ * @plugin: Pointer to the HID plugin
+ *
+ * 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_enabled(id));
+
+ return __hid_plugin_register(id, plugin);
+}
+
+/**
+ * hid_plugin_unregister() - Unregister an HID plugin.
+ * @id: Unique ID of @plugin
+ * @plugin: Pointer to the HID plugin
+ */
+static __always_inline void hid_plugin_unregister(enum hid_plugin_id id, struct hid_plugin *plugin)
+{
+ BUILD_BUG_ON(!hid_plugin_enabled(id));
+
+ __hid_plugin_unregister(id, plugin);
+}
+
+/*
+ * Usage:
+ *
+ * struct foo_plugin {
+ * struct hid_plugin hp;
+ * void (*bar)(struct hid_device *hdev);
+ * };
+ *
+ * static struct foo_plugin foo_plugin = {
+ * .hp = {
+ * .owner = THIS_MODULE,
+ * },
+ * .bar = hid_foo_bar,
+ * };
+ * module_hid_plugin(foo, HID_PLUGIN_FOO, &foo_plugin.hp);
+ */
+#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 */
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v4 8/9] HID: haptic: Convert into a loadable plugin module
2026-11-24 16:44 [PATCH v4 0/9] HID: Add hid-plugin framework & convert haptic into a plugin Rong Zhang
` (6 preceding siblings ...)
2026-11-24 16:44 ` [PATCH v4 7/9] HID: Add hid-plugin framework for loadable plugin modules Rong Zhang
@ 2026-11-24 16:44 ` Rong Zhang
2026-11-24 17:04 ` sashiko-bot
2026-11-24 16:44 ` [PATCH v4 9/9] HID: haptic: Remove stubs in favor of inlined dead code elimination Rong Zhang
8 siblings, 1 reply; 13+ messages in thread
From: Rong Zhang @ 2026-11-24 16:44 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, hid-haptic can be easily converted
into a loadable plugin module, so that hid-multitouch only requests it
on demand.
The redaundent `default n' statement is also removed for tidiness since
we are anyway touching its context.
Suggested-by: Benjamin Tissoires <bentiss@kernel.org>
Link: https://lore.kernel.org/linux-input/auypydfkhx2eg7vp764way4batdilzc35inqda3exwzs3tk3ff@oagat6g46zto/
Signed-off-by: Rong Zhang <i@rong.moe>
---
MAINTAINERS | 1 +
drivers/hid/Kconfig | 7 +++++--
drivers/hid/Makefile | 2 +-
drivers/hid/hid-haptic.c | 23 +++++++++++++++++++----
drivers/hid/hid-haptic.h | 29 +++++++++++++++++++++++++++--
drivers/hid/hid-plugin.c | 3 +++
drivers/hid/hid-plugin.h | 6 ++++++
7 files changed, 62 insertions(+), 9 deletions(-)
diff --git a/MAINTAINERS b/MAINTAINERS
index 24bcc700ccfe..68f79c0f2ecd 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 604683b4a4a9..97cb4caf9d7e 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -106,9 +106,10 @@ menuconfig HID_PLUGIN
If unsure, say N.
+if HID_PLUGIN
+
config HID_HAPTIC
- bool "Haptic touchpad support"
- default n
+ tristate "Haptic touchpad support"
help
Support for touchpads with force sensors and haptic actuators instead of a
traditional button.
@@ -117,6 +118,8 @@ config HID_HAPTIC
If unsure, say N.
+endif # HID_PLUGIN
+
menu "Special HID drivers"
config HID_A4TECH
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index b41aa3f52a94..3360347c1cb8 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -6,7 +6,7 @@ hid-y := hid-core.o hid-input.o hid-quirks.o
hid-$(CONFIG_DEBUG_FS) += hid-debug.o
hid-$(CONFIG_HID_PLUGIN) += hid-plugin.o
-hid-$(CONFIG_HID_HAPTIC) += hid-haptic.o
+obj-$(CONFIG_HID_HAPTIC) += hid-haptic.o
obj-$(CONFIG_HID_BPF) += bpf/
diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index cbe834a77fd4..50d6673c7c21 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -310,11 +310,12 @@ static void hid_haptic_destroy(struct ff_device *ff)
kfree(haptic->hid_usage_map);
haptic->hid_usage_map = NULL;
+
+ hid_plugin_put(HID_PLUGIN_HAPTIC, &haptic->self_plugin->hp);
}
-int hid_haptic_init(struct hid_device *hdev,
- struct hid_haptic_device *haptic,
- struct input_dev *dev)
+static int __hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *haptic,
+ struct input_dev *dev)
{
struct ff_device *ff;
int ret = 0, r;
@@ -436,6 +437,20 @@ int hid_haptic_init(struct hid_device *hdev,
haptic->hid_usage_map = NULL;
exit:
dev_warn(&hdev->dev, "Cannot allocate haptic for %s: %d\n", hdev->name, ret);
+ hid_plugin_put(HID_PLUGIN_HAPTIC, &haptic->self_plugin->hp);
return ret;
}
-EXPORT_SYMBOL_GPL(hid_haptic_init);
+
+static struct hid_plugin_haptic hid_plugin_haptic = {
+ .hp = {
+ .owner = THIS_MODULE,
+ },
+ .init = __hid_haptic_init,
+};
+module_hid_plugin(haptic, HID_PLUGIN_HAPTIC, &hid_plugin_haptic.hp);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Angela Czubak <aczubak@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 fd7093e1764a..a459c5eb9580 100644
--- a/drivers/hid/hid-haptic.h
+++ b/drivers/hid/hid-haptic.h
@@ -5,9 +5,12 @@
* Copyright (c) 2021 Angela Czubak <acz@semihalf.com>
*/
+#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
@@ -52,6 +55,14 @@ struct hid_haptic_device {
u16 release_ordinal;
struct hid_haptic_effect *effect;
struct hid_haptic_effect stop_effect;
+
+ struct hid_plugin_haptic *self_plugin;
+};
+
+struct hid_plugin_haptic {
+ struct hid_plugin hp;
+ int (*init)(struct hid_device *hdev, struct hid_haptic_device *haptic,
+ struct input_dev *dev);
};
#if IS_ENABLED(CONFIG_HID_HAPTIC)
@@ -151,8 +162,22 @@ static inline int hid_haptic_input_mapping(struct hid_device *hdev,
return 0;
}
-int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *haptic,
- struct input_dev *dev);
+static inline int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *haptic,
+ struct input_dev *dev)
+{
+ struct hid_plugin_haptic *self_plugin;
+ struct hid_plugin *plugin;
+
+ plugin = hid_plugin_get(HID_PLUGIN_HAPTIC);
+ if (IS_ERR(plugin))
+ return PTR_ERR(plugin);
+
+ self_plugin = container_of(plugin, struct hid_plugin_haptic, hp);
+
+ haptic->self_plugin = self_plugin;
+
+ return self_plugin->init(hdev, haptic, dev);
+}
static inline bool hid_haptic_input_configured(struct hid_device *hdev,
struct hid_haptic_device *haptic,
diff --git a/drivers/hid/hid-plugin.c b/drivers/hid/hid-plugin.c
index 288dedd21253..a84cff20ca11 100644
--- a/drivers/hid/hid-plugin.c
+++ b/drivers/hid/hid-plugin.c
@@ -18,6 +18,9 @@
#include "hid-plugin.h"
static const char * const hid_plugin_mod_names[NR_HID_PLUGINS] = {
+#if IS_MODULE(CONFIG_HID_HAPTIC)
+ [HID_PLUGIN_HAPTIC] = "hid-haptic",
+#endif
};
static struct hid_plugin __rcu *hid_plugins[NR_HID_PLUGINS];
diff --git a/drivers/hid/hid-plugin.h b/drivers/hid/hid-plugin.h
index e7bc98639d9f..e2e157b23b69 100644
--- a/drivers/hid/hid-plugin.h
+++ b/drivers/hid/hid-plugin.h
@@ -19,6 +19,12 @@
enum hid_plugin_id {
HID_PLUGIN_DISABLED = -1,
+#if IS_ENABLED(CONFIG_HID_HAPTIC)
+ HID_PLUGIN_HAPTIC,
+#else
+#define HID_PLUGIN_HAPTIC HID_PLUGIN_DISABLED
+#endif
+
NR_HID_PLUGINS,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH v4 8/9] HID: haptic: Convert into a loadable plugin module
2026-11-24 16:44 ` [PATCH v4 8/9] HID: haptic: Convert into a loadable plugin module Rong Zhang
@ 2026-11-24 17:04 ` sashiko-bot
0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-11-24 17:04 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:
- [Medium] HID: haptic: module unload race via hid_plugin_put() in
hid-haptic.ko
--
commit 36f36535430850c43124dc68d1bb0a9b639c5f30
Author: Rong Zhang <i@rong.moe>
HID: haptic: Convert into a loadable plugin module
This commit converts hid-haptic into a loadable module using the
hid-plugin framework so that hid-multitouch can load it on demand.
Link: https://lore.kernel.org/linux-input/auypydfkhx2eg7vp764way4batdilzc35inqda3exwzs3tk3ff@oagat6g46zto/
> diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
> index cbe834a77fd47..50d6673c7c211 100644
> --- a/drivers/hid/hid-haptic.c
> +++ b/drivers/hid/hid-haptic.c
> @@ -310,11 +310,12 @@ static void hid_haptic_destroy(struct ff_device *ff)
>
> kfree(haptic->hid_usage_map);
> haptic->hid_usage_map = NULL;
> +
> + hid_plugin_put(HID_PLUGIN_HAPTIC, &haptic->self_plugin->hp);
> }
>
> -int hid_haptic_init(struct hid_device *hdev,
> - struct hid_haptic_device *haptic,
> - struct input_dev *dev)
> +static int __hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *haptic,
> + struct input_dev *dev)
> {
> struct ff_device *ff;
> int ret = 0, r;
> @@ -436,6 +437,20 @@ int hid_haptic_init(struct hid_device *hdev,
> haptic->hid_usage_map = NULL;
> exit:
> dev_warn(&hdev->dev, "Cannot allocate haptic for %s: %d\n", hdev->name, ret);
> + hid_plugin_put(HID_PLUGIN_HAPTIC, &haptic->self_plugin->hp);
> return ret;
> }
[Severity: Medium]
Can calling hid_plugin_put() from inside hid_haptic_destroy() and
__hid_haptic_init() allow hid-haptic.ko to be unmapped while a CPU is
still executing inside it?
hid_plugin_put() is an inline helper that calls module_put(plugin->owner),
where plugin->owner is THIS_MODULE (hid-haptic.ko). Because
hid-multitouch holds no direct symbol dependency on hid-haptic.ko, the
module refcount acquired by hid_plugin_get() is the only thing keeping the
module loaded:
CPU0 CPU1
input_ff_destroy()
ff->destroy() [hid_haptic_destroy]
hid_plugin_put()
module_put(THIS_MODULE)
atomic_dec_if_positive()
delete_module("hid_haptic")
try_stop_module()
mod->exit()
free_module()
trace_module_put()
return; <-- page fault on unmapped hid-haptic.ko text
The same race exists on the error path of __hid_haptic_init(). To avoid
executing module text after dropping the last reference, the caller
outside hid-haptic.ko should call hid_plugin_put() after
self_plugin->init() fails or after the destroy callback returns.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-hid-haptic-kmod-v4-0-03713a89f656@rong.moe?part=8
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v4 9/9] HID: haptic: Remove stubs in favor of inlined dead code elimination
2026-11-24 16:44 [PATCH v4 0/9] HID: Add hid-plugin framework & convert haptic into a plugin Rong Zhang
` (7 preceding siblings ...)
2026-11-24 16:44 ` [PATCH v4 8/9] HID: haptic: Convert into a loadable plugin module Rong Zhang
@ 2026-11-24 16:44 ` Rong Zhang
8 siblings, 0 replies; 13+ messages in thread
From: Rong Zhang @ 2026-11-24 16:44 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, Angela Czubak, Henrik Rydberg,
Dmitry Torokhov
Cc: linux-input, linux-kernel, Rong Zhang
All stubs in hid-haptic.h can be replaced by equivalent
!hid_plugin_enabled(HID_PLUGIN_HAPTIC) checks. The compiler will
eliminate calls to all methods when the plugin is disabled.
Signed-off-by: Rong Zhang <i@rong.moe>
---
Changes in v3:
- Fix the return value of hid_haptic_connected_to_input() when
hid-haptic is disabled (thanks Sashiko)
---
drivers/hid/hid-haptic.h | 83 +++++++++++++++++++-----------------------------
1 file changed, 32 insertions(+), 51 deletions(-)
diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index a459c5eb9580..375891d1d5de 100644
--- a/drivers/hid/hid-haptic.h
+++ b/drivers/hid/hid-haptic.h
@@ -65,11 +65,18 @@ struct hid_plugin_haptic {
struct input_dev *dev);
};
-#if IS_ENABLED(CONFIG_HID_HAPTIC)
+static inline bool hid_haptic_enabled(void)
+{
+ return hid_plugin_enabled(HID_PLUGIN_HAPTIC);
+}
+
static inline struct hid_haptic_device *hid_haptic_alloc(struct hid_device *hdev)
{
struct hid_haptic_device *haptic;
+ if (!hid_haptic_enabled())
+ return ERR_PTR(-EOPNOTSUPP);
+
haptic = kzalloc_obj(struct hid_haptic_device);
if (haptic)
haptic->hdev = hdev;
@@ -80,6 +87,9 @@ static inline struct hid_haptic_device *hid_haptic_alloc(struct hid_device *hdev
static inline bool hid_haptic_connected_to_input(struct hid_haptic_device *haptic,
struct input_dev *input)
{
+ if (!hid_haptic_enabled())
+ return false;
+
return haptic->input_dev == input;
}
@@ -89,6 +99,9 @@ static inline void hid_haptic_feature_mapping(struct hid_device *hdev,
{
u16 usage_hid;
+ if (!hid_haptic_enabled())
+ return;
+
if (usage->hid == HID_HP_AUTOTRIGGER) {
if (usage->usage_index >= field->report_count) {
dev_err(&hdev->dev, "HID_HP_AUTOTRIGGER out of range\n");
@@ -116,6 +129,9 @@ static inline void hid_haptic_feature_mapping(struct hid_device *hdev,
static inline void hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
struct hid_input *hi, struct hid_field *field)
{
+ if (!hid_haptic_enabled())
+ return;
+
if (hi->application != HID_DG_TOUCHPAD)
return;
@@ -154,6 +170,9 @@ static inline int hid_haptic_input_mapping(struct hid_device *hdev,
struct hid_field *field, struct hid_usage *usage,
unsigned long **bit, int *max)
{
+ if (!hid_haptic_enabled())
+ return 0;
+
if (usage->hid == HID_HP_MANUALTRIGGER) {
haptic->manual_trigger_report = field->report;
/* we don't really want to map these fields */
@@ -168,6 +187,9 @@ static inline int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_dev
struct hid_plugin_haptic *self_plugin;
struct hid_plugin *plugin;
+ if (!hid_haptic_enabled())
+ return 0;
+
plugin = hid_plugin_get(HID_PLUGIN_HAPTIC);
if (IS_ERR(plugin))
return PTR_ERR(plugin);
@@ -183,6 +205,9 @@ static inline bool hid_haptic_input_configured(struct hid_device *hdev,
struct hid_haptic_device *haptic,
struct hid_input *hi)
{
+ if (!hid_haptic_enabled())
+ return false;
+
if (IS_ERR_OR_NULL(haptic->input_dev) ||
!hid_haptic_connected_to_input(haptic, hi->input) ||
!haptic->auto_trigger_report || !haptic->manual_trigger_report)
@@ -195,61 +220,17 @@ static inline bool hid_haptic_input_configured(struct hid_device *hdev,
static inline void hid_haptic_pressure_reset(struct hid_haptic_device *haptic)
{
+ if (!hid_haptic_enabled())
+ return;
+
haptic->pressure_sum = 0;
}
static inline void hid_haptic_pressure_increase(struct hid_haptic_device *haptic,
__s32 pressure)
{
- haptic->pressure_sum += pressure;
-}
-#else
-static inline struct hid_haptic_device *hid_haptic_alloc(struct hid_device *hdev)
-{
- return ERR_PTR(-EOPNOTSUPP);
-}
-
-static inline bool hid_haptic_connected_to_input(struct hid_haptic_device *haptic,
- struct input_dev *input)
-{
- return false;
-}
+ if (!hid_haptic_enabled())
+ return;
-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
-void hid_haptic_check_pressure_unit(struct hid_haptic_device *haptic,
- struct hid_input *hi, struct hid_field *field)
-{}
-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)
-{
- return 0;
-}
-static inline
-bool hid_haptic_input_configured(struct hid_device *hdev,
- struct hid_haptic_device *haptic, struct hid_input *hi)
-{
- return false;
-}
-static inline
-int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device *haptic,
- struct input_dev *dev)
-{
- return 0;
+ 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
--
2.55.0
^ permalink raw reply related [flat|nested] 13+ messages in thread