Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH] HID: logitech-hidpp: add raw touchpad support for the K400 Plus
@ 2026-12-05  9:45 Damiano Ognissanti via B4 Relay
  2026-12-05 10:03 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Damiano Ognissanti via B4 Relay @ 2026-12-05  9:45 UTC (permalink / raw)
  To: Filipe Laíns, Bastien Nocera, Jiri Kosina,
	Benjamin Tissoires, Henrik Rydberg
  Cc: linux-input, Damiano Ognissanti

From: Damiano Ognissanti <damiano@chalmers.se>

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 regular HID inputs (keyboard and mouse) untouched,
 - creates a separate multitouch input node for the touchpad,
 - enables raw reporting on connect and on every reconnect,
 - hands the touchpad back to the firmware when the driver is removed.

The configuration query and the raw report parsing are shared with the
WTP code. The physical buttons keep being reported through the regular
mouse input.

With this, userspace (libinput) handles the touchpad like any other
touchpad, including smooth two-finger scrolling, tapping 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, the keyboard, and unloading the module (the touchpad goes
back to firmware mode). Also builds without warnings (W=1) on v7.4-rc5.

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.
---
 drivers/hid/hid-logitech-hidpp.c | 182 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 182 insertions(+)

diff --git a/drivers/hid/hid-logitech-hidpp.c b/drivers/hid/hid-logitech-hidpp.c
index a170aaf63..ea3a9b094 100644
--- a/drivers/hid/hid-logitech-hidpp.c
+++ b/drivers/hid/hid-logitech-hidpp.c
@@ -64,6 +64,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 +181,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 +2940,8 @@ struct wtp_data {
 	u8 maxcontacts;
 	bool flip_y;
 	unsigned int resolution;
+	/* separate touchpad input node, only used by the K400 Plus */
+	struct input_dev *tp_input;
 };
 
 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
@@ -3409,6 +3413,169 @@ 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 are reported by the regular mouse input */
+	input_set_capability(input, EV_KEY, BTN_LEFT);
+	input_set_capability(input, EV_KEY, BTN_RIGHT);
+}
+
+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;
+}
+
+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);
+}
+
+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;
+}
+
+/* Hand the touchpad back to the firmware, e.g. when the module is unloaded. */
+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;
+	__hidpp_send_report(hidpp->hid_dev, message);
+	kfree(message);
+}
+
 /* ------------------------------------------------------------------------- */
 /* Logitech G920 Driving Force Racing Wheel for Xbox One                     */
 /* ------------------------------------------------------------------------- */
@@ -4273,6 +4440,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;
 }
@@ -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) {
@@ -4719,6 +4891,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 = wtp_allocate(hdev, id);
+		if (ret)
+			return ret;
 	}
 
 	INIT_WORK(&hidpp->work, hidpp_connect_event);
@@ -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);
@@ -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 },

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

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



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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox