From: sashiko-bot@kernel.org
To: "Damiano Ognissanti" <damiano@chalmers.se>
Cc: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH] HID: logitech-hidpp: add raw touchpad support for the K400 Plus
Date: Sat, 05 Dec 2026 10:03:45 +0000 [thread overview]
Message-ID: <sashiko-outbox-251361@kernel.org> (raw)
In-Reply-To: <20261205-k400plus-v1-1-d829edf99f6b@chalmers.se>
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] HID: logitech-hidpp: unused BTN_LEFT/BTN_RIGHT on K400 Plus
touchpad node
- [Medium] HID: logitech-hidpp: unconditional raw mode breaks K400 Plus
mouse default
- [Medium] HID: logitech-hidpp: k400p_stop_raw() races with
hidpp_connect_event()
- [Medium] HID: logitech-hidpp: missing acquire/release ordering on
wd->tp_input
- [Medium] HID: logitech-hidpp: leak devm input_dev and name in
k400p_create_touchpad()
- [Low] HID: logitech-hidpp: k400p_connect() registers input before raw
mode succeeds
- [Low] HID: logitech-hidpp: K400 Plus MT contacts not cleared on
disconnect
--
commit 8d01bffe9e1a34145aae727352972da3d2cb885f
Author: Damiano Ognissanti <damiano@chalmers.se>
HID: logitech-hidpp: add raw touchpad support for the K400 Plus
This commit adds a HIDPP_QUIRK_CLASS_K400_PLUS class to enable HID++
0x6100 raw XY touchpad reporting on the Logitech K400 Plus keyboard and
expose it through a separate multitouch input device.
> diff --git a/drivers/hid/hid-logitech-hidpp.c b/drivers/hid/hid-logitech-hidpp.c
> index a170aaf635b0e..ea3a9b094a340 100644
> --- a/drivers/hid/hid-logitech-hidpp.c
> +++ b/drivers/hid/hid-logitech-hidpp.c
[ ... ]
> @@ -3409,6 +3413,169 @@ static int k400_connect(struct hid_device *hdev)
[ ... ]
> +static void k400p_populate_input(struct wtp_data *wd, struct input_dev *input)
> +{
> + __set_bit(EV_ABS, input->evbit);
> + __set_bit(EV_KEY, input->evbit);
> +
> + input_set_abs_params(input, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
> + input_abs_set_res(input, ABS_MT_POSITION_X, wd->resolution);
> + input_set_abs_params(input, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
> + input_abs_set_res(input, ABS_MT_POSITION_Y, wd->resolution);
> +
> + /* the physical buttons are reported by the regular mouse input */
> + input_set_capability(input, EV_KEY, BTN_LEFT);
> + input_set_capability(input, EV_KEY, BTN_RIGHT);
> +}
[Severity: Medium]
Should wd->tp_input advertise BTN_LEFT and BTN_RIGHT when no code path
ever emits them on this device?
k400p_raw_event() only handles HID++ raw XY touch reports, so physical
button clicks stay on the separate regular HID mouse input node.
Splitting contacts and buttons across two evdev nodes breaks userspace
features that correlate both on one device, such as libinput
middle-button emulation, palm detection during clicks, and
click-and-drag.
In wtp_raw_event(), HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS intercepts mouse
report 0x02 and routes BTN_LEFT and BTN_RIGHT onto the touchpad input
node so buttons and contacts share a single device.
> +static int k400p_create_touchpad(struct hidpp_device *hidpp)
> +{
> + struct hid_device *hdev = hidpp->hid_dev;
> + struct wtp_data *wd = hidpp->private_data;
> + struct input_dev *input;
> + int ret;
> +
> + input = hidpp_allocate_input(hdev);
> + if (!input)
> + return -ENOMEM;
> +
> + input->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s Touchpad",
> + hidpp->name);
> + if (!input->name)
> + return -ENOMEM;
> +
> + k400p_populate_input(wd, input);
> +
> + ret = input_mt_init_slots(input, wd->maxcontacts,
> + INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
> + if (ret)
> + return ret;
> +
> + ret = input_register_device(input);
> + if (ret)
> + return ret;
> +
> + wd->tp_input = input;
> + return 0;
> +}
[Severity: Medium]
Do these error paths leak the devres-managed input_dev and name string
until driver removal?
k400p_create_touchpad() runs from hidpp_connect_event() after probe has
succeeded, so devm allocations attached to &hdev->dev are not freed
until hid_device_remove(). Because wd->tp_input stays NULL and
hidpp_connect_event() ignores k400p_connect() failures, each wireless
reconnect retries k400p_create_touchpad() and can accumulate orphaned
allocations.
Calling input_free_device(input) (as the delayed_input path in
hidpp_connect_event() already does) and devm_kfree(&hdev->dev,
input->name) on error releases them immediately.
> +static int k400p_connect(struct hid_device *hdev)
> +{
> + struct hidpp_device *hidpp = hid_get_drvdata(hdev);
> + struct wtp_data *wd = hidpp->private_data;
> + int ret;
> +
> + if (!wd->x_size) {
> + ret = wtp_get_config(hidpp);
> + if (ret) {
> + hid_err(hdev, "Can not get touchpad config: %d\n", ret);
> + return ret;
> + }
> + }
> +
> + if (!wd->tp_input) {
> + ret = k400p_create_touchpad(hidpp);
> + if (ret) {
> + hid_err(hdev, "Can not create touchpad input: %d\n", ret);
> + return ret;
> + }
> + }
> +
> + return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
> + true, true);
> +}
[Severity: Low]
Can k400p_connect() leave a non-functional Touchpad input device
registered if enabling raw mode fails?
k400p_create_touchpad() registers wd->tp_input before
hidpp_touchpad_set_raw_report_state() is called, and neither
k400p_connect() nor hidpp_connect_event() logs or rolls back on failure.
If the HID++ command times out or returns an error, userspace sees a
dead touchpad node while the hardware remains in firmware mouse mode.
Enabling raw reporting before registering wd->tp_input avoids this.
Separately, hidpp_connect_event() calls k400p_connect() before
hidpp_get_device_name() updates hidpp->name, so wd->tp_input gets a
different name depending on whether creation succeeds on first connect
or on a later reconnect.
> +static void k400p_touch_event(struct wtp_data *wd,
> + struct hidpp_touchpad_raw_xy_finger *finger)
> +{
> + struct input_dev *input = wd->tp_input;
> + bool touching = finger->contact_status == 1;
> + int slot;
> +
> + if (!finger->finger_id || finger->contact_type)
> + /* no actual data */
> + return;
> +
> + slot = input_mt_get_slot_by_key(input, finger->finger_id);
> + if (slot < 0)
> + return;
> +
> + input_mt_slot(input, slot);
> + input_mt_report_slot_state(input, MT_TOOL_FINGER, touching);
> + if (touching) {
> + input_event(input, EV_ABS, ABS_MT_POSITION_X, finger->x);
> + input_event(input, EV_ABS, ABS_MT_POSITION_Y,
> + wd->flip_y ? wd->y_size - finger->y : finger->y);
> + }
> +}
> +
> +static int k400p_raw_event(struct hid_device *hdev, u8 *data, int size)
> +{
> + struct hidpp_device *hidpp = hid_get_drvdata(hdev);
> + struct wtp_data *wd = hidpp->private_data;
> + struct hidpp_report *report = (struct hidpp_report *)data;
> + struct hidpp_touchpad_raw_xy raw;
> + int i;
> +
> + if (!wd || !wd->tp_input || !wd->mt_feature_index)
> + return 0;
> +
> + /* size is already checked in hidpp_raw_event. */
> + if (data[0] != REPORT_ID_HIDPP_LONG ||
> + report->fap.feature_index != wd->mt_feature_index ||
> + report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY)
> + return 0;
> +
> + hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
> +
> + for (i = 0; i < 2; i++)
> + k400p_touch_event(wd, &raw.fingers[i]);
> +
> + /* fingers not reported in this frame have been lifted */
> + input_mt_sync_frame(wd->tp_input);
> + input_sync(wd->tp_input);
> +
> + return 1;
> +}
[Severity: Medium]
Is there a data race and missing memory ordering on wd->tp_input and
the sibling fields in struct wtp_data?
Because HIDPP_QUIRK_CLASS_K400_PLUS does not set
HIDPP_QUIRK_DELAYED_INIT, HID reports are already active when
hidpp_connect_event() runs on the workqueue:
CPU0 (kworker) CPU1 (<softirq>)
k400p_connect() hidpp_raw_event()
wtp_get_config() k400p_raw_event()
wd->mt_feature_index = ... if (!wd->tp_input) ...
wd->y_size = ...
wd->flip_y = ...
k400p_create_touchpad()
wd->tp_input = input /* sees non-NULL wd->tp_input */
k400p_touch_event()
wd->flip_y, wd->y_size
<-- stale zero values
Because wd->mt_feature_index, wd->flip_y and wd->y_size have no address
dependency on wd->tp_input, a weakly-ordered CPU can observe a non-NULL
wd->tp_input while reading stale values for those fields, corrupting
coordinates or misrouting reports. Publishing wd->tp_input with
smp_store_release() and loading it once with smp_load_acquire() in
k400p_raw_event() orders all prior initialization.
Also, guarding wtp_get_config() only with !wd->x_size allows reconnects
to overwrite struct wtp_data fields concurrently with k400p_raw_event()
if a device reports x_size == 0.
[ ... ]
> @@ -4491,6 +4660,9 @@ static void hidpp_connect_event(struct work_struct *work)
> ret = k400_connect(hdev);
> if (ret)
> return;
> + } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400_PLUS) {
> + /* the keyboard keeps working even if this fails */
> + k400p_connect(hdev);
> }
>
> if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
[Severity: Low]
Are active multitouch contacts on wd->tp_input left stuck when the
keyboard disconnects or powers off during a touch?
On RF link loss, hid-logitech-dj only synthesizes null reports for
standard DJ reports, which k400p_raw_event() ignores, and the disconnect
path in hidpp_connect_event() returns without clearing active slots on
wd->tp_input. BTN_TOUCH then stays pressed while disconnected, and on
reconnect a matching finger_id can reuse the stale tracking ID and cause
a cursor jump (or drop the first frame if all slots are stuck active).
Clearing active MT slots on disconnect or reconnect (synchronized with
k400p_raw_event(), which runs under hdev->driver_input_lock) would avoid
stuck contacts.
[ ... ]
> @@ -4830,6 +5006,9 @@ static void hidpp_remove(struct hid_device *hdev)
>
> sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
>
> + if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400_PLUS)
> + k400p_stop_raw(hidpp);
> +
> hid_hw_stop(hdev);
> cancel_work_sync(&hidpp->work);
> cancel_work_sync(&hidpp->reset_hi_res_work);
[Severity: Medium]
Can k400p_stop_raw() race with an already-queued or running
hidpp_connect_event() work item?
hid_device_remove() prevents new work from being scheduled, but
hidpp_remove() calls k400p_stop_raw() before draining hidpp->work and
hidpp->reset_hi_res_work:
CPU0 (rmmod/unbind) CPU1 (kworker)
hidpp_remove() hidpp_connect_event()
k400p_stop_raw() k400p_connect()
__hidpp_send_report(off)
hid_hw_stop()
cancel_work_sync(&hidpp->work) hidpp_touchpad_set_raw_report_state(
..., true, true)
<-- re-enables raw mode
This can leave the touchpad stuck in raw mode and unusable as a firmware
mouse after driver removal. There is also an unsynchronized data race
on wd->mt_feature_index between wtp_get_config() and k400p_stop_raw(),
and k400p_stop_raw() calls __hidpp_send_report() without holding
hidpp->send_mutex while the workers may concurrently transmit under it.
Calling cancel_work_sync(&hidpp->work) and
cancel_work_sync(&hidpp->reset_hi_res_work) before k400p_stop_raw()
closes all three races.
> @@ -4868,6 +5047,9 @@ static const struct hid_device_id hidpp_devices[] = {
> { /* Keyboard logitech K400 */
> LDJ_DEVICE(0x4024),
> .driver_data = HIDPP_QUIRK_CLASS_K400 },
> + { /* Keyboard Logitech K400 Plus */
> + LDJ_DEVICE(0x404d),
> + .driver_data = HIDPP_QUIRK_CLASS_K400_PLUS },
> { /* Solar Keyboard Logitech K750 */
> LDJ_DEVICE(0x4002),
> .driver_data = HIDPP_QUIRK_CLASS_K750 },
[Severity: Medium]
Does unconditionally switching the K400 Plus into raw mode regress
existing users who rely on the firmware mouse default?
Previously, 0x404d operated as a standard relative mouse, and enabling
HID++ 0x6100 raw mode with no opt-out stops relative motion on the
existing mouse node and disables firmware features such as the Fn +
Left Click tap-to-click toggle. Environments without a touchpad stack
like libinput (for example minimal X11 with xf86-input-evdev or VM
evdev passthrough of the mouse node) lose pointer movement.
Note also that raw touchpad mode on another Logitech combo keyboard
was reverted in 5006c1052aaf ("Revert \"HID: logitech-hidpp: support
combo keyboard touchpad TK820\"") due to a firmware bug causing delayed
keys and key-repeat when typing while resting fingers on the touch
sensor; it is worth verifying whether the K400 Plus is unaffected.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-k400plus-v1-1-d829edf99f6b@chalmers.se?part=1
prev parent reply other threads:[~2026-12-05 10:03 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 9:45 [PATCH] HID: logitech-hidpp: add raw touchpad support for the K400 Plus Damiano Ognissanti via B4 Relay
2026-12-05 10:03 ` sashiko-bot [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-251361@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=damiano@chalmers.se \
--cc=dmitry.torokhov@gmail.com \
--cc=linux-input@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox