All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] HID: logitech-hidpp: add raw touchpad support for the K400 Plus
@ 2026-12-05 17:18 ` Damiano Ognissanti via B4 Relay
  0 siblings, 0 replies; 3+ messages in thread
From: Damiano Ognissanti @ 2026-12-05 17:18 UTC (permalink / raw)
  To: Filipe Laíns, Bastien Nocera, Jiri Kosina,
	Benjamin Tissoires, Henrik Rydberg
  Cc: linux-input, Damiano Ognissanti

The Logitech K400 Plus (0x404d) is a keyboard with an embedded
touchpad. By default the touchpad is exposed as a relative mouse and
all touch handling (tapping, two-finger scrolling) is done in firmware.
The firmware scrolling only reports whole wheel detents, only starts
after the fingers have moved some distance and ignores slow finger
movements, which makes it hard to control.

The keyboard also supports the HID++ feature 0x6100 (TouchPad Raw XY),
the same feature that is used by the wireless touchpads handled by the
WTP class. In raw mode it reports the absolute position of up to two
fingers (3570x2040 units at 1390 dpi) at about 83 Hz.

The WTP class cannot be reused as is, because it turns the whole HID
device into a touchpad and drops all other usages, while the K400 Plus
also needs its keyboard. Add a K400 Plus class instead, which:

 - keeps the keyboard HID inputs untouched,
 - enables raw reporting on connect and on every reconnect, and then
   creates a separate multitouch input node for the touchpad,
 - moves the physical left and right buttons from the regular mouse
   report to the touchpad node, like HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
   does, so that contacts and buttons are on one device; the mouse
   collection is not mapped anymore, as it carries nothing else in raw
   mode,
 - releases all contacts and buttons when the keyboard disconnects,
 - hands the touchpad back to the firmware when the driver is removed.

The new module parameter disable_raw_touchpad keeps the previous
firmware mouse behaviour, for setups without a touchpad-aware
userspace.

The configuration query and the raw report parsing are shared with the
WTP code.

Raw mode on the TK820 combo keyboard was reverted because of a firmware
bug causing delayed and repeated keys while fingers rest on the touch
sensor. This does not happen on the K400 Plus: typing, including
holding a key, works normally with a finger resting on the touchpad or
while moving the pointer.

With this, userspace (libinput) handles the touchpad like any other
touchpad, including smooth two-finger scrolling, tapping,
click-and-drag and gestures.

Tested on a K400 Plus (firmware RQK 63.02.B0016) paired to a Unifying
receiver (046d:c52b), on v7.3.1: pointer motion, tapping, two-finger
scrolling, physical buttons and click-and-drag, typing with fingers on
the touchpad, switching the keyboard off and on while touching,
disable_raw_touchpad=1, and unloading the module (the touchpad goes
back to firmware mode). Also builds without warnings (W=1) on v7.4-rc5
and on hid.git for-next.

The code and this changelog were written with an AI coding assistant
(Claude Opus 5.5, by Anthropic), which also analysed raw report dumps
from my keyboard to work out the report format. I reviewed the result
and tested it on my hardware as described above.

Signed-off-by: Damiano Ognissanti <damiano@chalmers.se>
---
This is my first kernel patch, feedback welcome.
---
Changes in v2:
- Move the physical buttons to the touchpad node and stop mapping the
  mouse collection, so that contacts and buttons are on one device.
- Add the disable_raw_touchpad module parameter to keep the firmware
  mouse mode.
- Enable raw mode before creating the touchpad node, and turn it off
  again if that fails.
- Free the input device and its name if creating the node fails.
- Publish the touchpad node with smp_store_release() and serialize
  report handling with a spinlock.
- Release all contacts and buttons on disconnect and reconnect.
- Cancel the connect work before leaving raw mode on removal, and send
  that command under send_mutex.
- Reject an invalid touchpad configuration.
- Tested for the TK820 firmware bug (delayed/repeated keys with fingers
  on the touchpad): not present on the K400 Plus.
- Link to v1: https://patch.msgid.link/20261205-k400plus-v1-1-d829edf99f6b@chalmers.se

To: Filipe Laíns <lains@riseup.net>
To: Bastien Nocera <hadess@hadess.net>
To: Jiri Kosina <jikos@kernel.org>
To: Benjamin Tissoires <bentiss@kernel.org>
To: Henrik Rydberg <rydberg@bitmath.org>
Cc: linux-input@vger.kernel.org
---
 drivers/hid/hid-logitech-hidpp.c | 303 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 303 insertions(+)

diff --git a/drivers/hid/hid-logitech-hidpp.c b/drivers/hid/hid-logitech-hidpp.c
index a170aaf63..9e64e3572 100644
--- a/drivers/hid/hid-logitech-hidpp.c
+++ b/drivers/hid/hid-logitech-hidpp.c
@@ -38,6 +38,11 @@ module_param(disable_tap_to_click, bool, 0644);
 MODULE_PARM_DESC(disable_tap_to_click,
 	"Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
 
+static bool disable_raw_touchpad;
+module_param(disable_raw_touchpad, bool, 0444);
+MODULE_PARM_DESC(disable_raw_touchpad,
+	"Keep the firmware mouse mode for keyboards with a raw touchpad (only on the K400 Plus currently).");
+
 /* Define a non-zero software ID to identify our own requests */
 #define LINUX_KERNEL_SW_ID			0x01
 
@@ -64,6 +69,7 @@ MODULE_PARM_DESC(disable_tap_to_click,
 #define HIDPP_QUIRK_CLASS_K400			BIT(2)
 #define HIDPP_QUIRK_CLASS_G920			BIT(3)
 #define HIDPP_QUIRK_CLASS_K750			BIT(4)
+#define HIDPP_QUIRK_CLASS_K400_PLUS		BIT(5)
 
 /* bits 2..20 are reserved for classes */
 /* #define HIDPP_QUIRK_CONNECT_EVENTS		BIT(21) disabled */
@@ -180,6 +186,7 @@ struct hidpp_scroll_counter {
 };
 
 struct hidpp_reprog_control_mapping;
+static struct input_dev *hidpp_allocate_input(struct hid_device *hdev);
 
 struct hidpp_device {
 	struct hid_device *hid_dev;
@@ -2938,6 +2945,9 @@ struct wtp_data {
 	u8 maxcontacts;
 	bool flip_y;
 	unsigned int resolution;
+	/* separate touchpad input node and its lock, only used by the K400 Plus */
+	struct input_dev *tp_input;
+	spinlock_t tp_lock;
 };
 
 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
@@ -3409,6 +3419,272 @@ static int k400_connect(struct hid_device *hdev)
 	return k400_disable_tap_to_click(hidpp);
 }
 
+/* ------------------------------------------------------------------------- */
+/* Logitech K400 Plus: keyboard with a raw touchpad                          */
+/* ------------------------------------------------------------------------- */
+
+/*
+ * The K400 Plus normally reports its touchpad as a relative mouse, with
+ * tap, scroll and gesture handling done in firmware. It also supports the
+ * HID++ feature 0x6100 (TouchPad Raw XY), like the T650/T651 touchpads.
+ *
+ * Unlike those, the K400 Plus is a keyboard too, so the regular HID inputs
+ * are kept as they are and the raw touch data is reported on a separate
+ * multitouch input node, so that userspace (libinput) can treat it as a
+ * real touchpad.
+ */
+
+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, taken over from the regular mouse report */
+	input_set_capability(input, EV_KEY, BTN_LEFT);
+	input_set_capability(input, EV_KEY, BTN_RIGHT);
+}
+
+static int k400p_allocate(struct hid_device *hdev,
+			  const struct hid_device_id *id)
+{
+	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
+	struct wtp_data *wd;
+	int ret;
+
+	ret = wtp_allocate(hdev, id);
+	if (ret)
+		return ret;
+
+	wd = hidpp->private_data;
+	spin_lock_init(&wd->tp_lock);
+
+	return 0;
+}
+
+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",
+				     hdev->name);
+	if (!input->name) {
+		ret = -ENOMEM;
+		goto err_free_input;
+	}
+
+	k400p_populate_input(wd, input);
+
+	ret = input_mt_init_slots(input, wd->maxcontacts,
+				  INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
+	if (ret)
+		goto err_free_name;
+
+	ret = input_register_device(input);
+	if (ret)
+		goto err_free_name;
+
+	/* pairs with smp_load_acquire() in k400p_raw_event() */
+	smp_store_release(&wd->tp_input, input);
+	return 0;
+
+err_free_name:
+	devm_kfree(&hdev->dev, input->name);
+err_free_input:
+	input_free_device(input);
+	return ret;
+}
+
+/* Release all contacts and buttons, e.g. when the keyboard disconnects. */
+static void k400p_release_contacts(struct wtp_data *wd)
+{
+	struct input_dev *input;
+	unsigned long flags;
+	int i;
+
+	if (!wd)
+		return;
+
+	/* pairs with smp_store_release() in k400p_create_touchpad() */
+	input = smp_load_acquire(&wd->tp_input);
+	if (!input)
+		return;
+
+	spin_lock_irqsave(&wd->tp_lock, flags);
+	for (i = 0; i < wd->maxcontacts; i++) {
+		input_mt_slot(input, i);
+		input_mt_report_slot_inactive(input);
+	}
+	input_mt_sync_frame(input);
+	input_report_key(input, BTN_LEFT, 0);
+	input_report_key(input, BTN_RIGHT, 0);
+	input_sync(input);
+	spin_unlock_irqrestore(&wd->tp_lock, flags);
+}
+
+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;
+
+	/* contacts from before a reconnect are stale */
+	k400p_release_contacts(wd);
+
+	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->x_size || !wd->y_size || !wd->maxcontacts) {
+			hid_err(hdev, "Invalid touchpad config\n");
+			wd->x_size = 0;
+			return -EINVAL;
+		}
+	}
+
+	ret = hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
+						  true, true);
+	if (ret) {
+		hid_err(hdev, "Can not enable raw touchpad reporting: %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);
+			hidpp_touchpad_set_raw_report_state(hidpp,
+					wd->mt_feature_index, false, false);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+static void k400p_touch_event(struct wtp_data *wd, struct input_dev *input,
+			      struct hidpp_touchpad_raw_xy_finger *finger)
+{
+	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;
+	struct input_dev *input;
+	unsigned long flags;
+	int i;
+
+	if (!wd)
+		return 0;
+
+	/* pairs with smp_store_release() in k400p_create_touchpad() */
+	input = smp_load_acquire(&wd->tp_input);
+	if (!input)
+		return 0;
+
+	switch (data[0]) {
+	case 0x02:
+		/*
+		 * Regular mouse report. In raw mode it only carries the
+		 * physical buttons, which belong to the touchpad.
+		 */
+		if (size < 2)
+			return 0;
+
+		spin_lock_irqsave(&wd->tp_lock, flags);
+		input_report_key(input, BTN_LEFT, !!(data[1] & 0x01));
+		input_report_key(input, BTN_RIGHT, !!(data[1] & 0x02));
+		input_sync(input);
+		spin_unlock_irqrestore(&wd->tp_lock, flags);
+		return 1;
+
+	case REPORT_ID_HIDPP_LONG:
+		/* size is already checked in hidpp_raw_event. */
+		if (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);
+
+		spin_lock_irqsave(&wd->tp_lock, flags);
+		for (i = 0; i < 2; i++)
+			k400p_touch_event(wd, input, &raw.fingers[i]);
+
+		/* fingers not reported in this frame have been lifted */
+		input_mt_sync_frame(input);
+		input_sync(input);
+		spin_unlock_irqrestore(&wd->tp_lock, flags);
+		return 1;
+	}
+
+	return 0;
+}
+
+/*
+ * Hand the touchpad back to the firmware when the driver is removed.
+ * Must be called after the connect work has been cancelled.
+ */
+static void k400p_stop_raw(struct hidpp_device *hidpp)
+{
+	struct wtp_data *wd = hidpp->private_data;
+	struct hidpp_report *message;
+
+	if (!wd || !wd->mt_feature_index)
+		return;
+
+	message = kzalloc_obj(struct hidpp_report);
+	if (!message)
+		return;
+
+	/* fire and forget: no answers are received while being removed */
+	message->report_id = REPORT_ID_HIDPP_LONG;
+	message->fap.feature_index = wd->mt_feature_index;
+	message->fap.funcindex_clientid = CMD_TOUCHPAD_SET_RAW_REPORT_STATE |
+					  LINUX_KERNEL_SW_ID;
+	message->fap.params[0] = 0;
+
+	mutex_lock(&hidpp->send_mutex);
+	__hidpp_send_report(hidpp->hid_dev, message);
+	mutex_unlock(&hidpp->send_mutex);
+
+	kfree(message);
+}
+
 /* ------------------------------------------------------------------------- */
 /* Logitech G920 Driving Force Racing Wheel for Xbox One                     */
 /* ------------------------------------------------------------------------- */
@@ -4032,6 +4308,10 @@ static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
 	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
 			field->application != HID_GD_MOUSE)
 		return m560_input_mapping(hdev, hi, field, usage, bit, max);
+	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400_PLUS &&
+		 field->application == HID_GD_MOUSE)
+		/* replaced by the touchpad input, see k400p_raw_event() */
+		return -1;
 
 	if (hdev->product == DINOVO_MINI_PRODUCT_ID)
 		return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
@@ -4273,6 +4553,8 @@ static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
 		return wtp_raw_event(hdev, data, size);
 	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
 		return m560_raw_event(hdev, data, size);
+	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400_PLUS)
+		return k400p_raw_event(hdev, data, size);
 
 	return 0;
 }
@@ -4470,6 +4752,8 @@ static void hidpp_connect_event(struct work_struct *work)
 	ret = hidpp_root_get_protocol_version(hidpp);
 	if (ret) {
 		hid_dbg(hidpp->hid_dev, "Disconnected\n");
+		if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400_PLUS)
+			k400p_release_contacts(hidpp->private_data);
 		if (hidpp->battery.ps) {
 			hidpp->battery.online = false;
 			hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
@@ -4491,6 +4775,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) {
@@ -4711,6 +4998,9 @@ static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
 	    hidpp_application_equals(hdev, HID_GD_KEYBOARD))
 		hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
 
+	if (disable_raw_touchpad)
+		hidpp->quirks &= ~HIDPP_QUIRK_CLASS_K400_PLUS;
+
 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
 		ret = wtp_allocate(hdev, id);
 		if (ret)
@@ -4719,6 +5009,10 @@ static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
 		ret = k400_allocate(hdev);
 		if (ret)
 			return ret;
+	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400_PLUS) {
+		ret = k400p_allocate(hdev, id);
+		if (ret)
+			return ret;
 	}
 
 	INIT_WORK(&hidpp->work, hidpp_connect_event);
@@ -4830,6 +5124,12 @@ 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) {
+		/* make sure a pending connect does not re-enable raw mode */
+		cancel_work_sync(&hidpp->work);
+		k400p_stop_raw(hidpp);
+	}
+
 	hid_hw_stop(hdev);
 	cancel_work_sync(&hidpp->work);
 	cancel_work_sync(&hidpp->reset_hi_res_work);
@@ -4868,6 +5168,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 },

---
base-commit: 47d0d5ce3cfca63a04eabcbeef7086d06240ff97
change-id: 20261205-k400plus-09053a83195f

Best regards,
--  
Damiano Ognissanti <damiano@chalmers.se>


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

end of thread, other threads:[~2026-12-05 17:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 17:18 [PATCH v2] HID: logitech-hidpp: add raw touchpad support for the K400 Plus Damiano Ognissanti
2026-12-05 17:18 ` Damiano Ognissanti via B4 Relay
2026-12-05 17:35 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.