[PATCH v5 03/13] HID: haptic, multitouch: Check haptic prerequisite properly

From: Rong Zhang

Date: Sun Nov 29 2026 - 12:55:33 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: Rong Zhang <i@xxxxxxxx>
---
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