linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vishnu Sankar <vishnuocv@gmail.com>
To: dmitry.torokhov@gmail.com, hmh@hmh.eng.br, hansg@kernel.org,
	ilpo.jarvinen@linux.intel.com
Cc: mpearson-lenovo@squebb.ca, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	ibm-acpi-devel@lists.sourceforge.net,
	platform-driver-x86@vger.kernel.org, vsankar@lenovo.com,
	Vishnu Sankar <vishnuocv@gmail.com>
Subject: [PATCH v2 2/2] platform/x86: thinkpad_acpi: Use trackpoint doubletap interface via sysfs
Date: Fri, 25 Jul 2025 05:23:48 +0900	[thread overview]
Message-ID: <20250724202349.11200-2-vishnuocv@gmail.com> (raw)
In-Reply-To: <20250724202349.11200-1-vishnuocv@gmail.com>

TrackPoint devices supporting doubletap expose a sysfs attribute under
/sys/devices/.../trackpoint/doubletap_enabled. This patch enables
thinkpad_acpi to detect if the system has a TrackPoint device with
doubletap capability, and allows toggling the feature via sysfs.

This avoids direct linking between subsystems and relies on sysfs
as the interface for coordination between input and platform drivers.

Signed-off-by: Vishnu Sankar <vishnuocv@gmail.com>
Suggested-by: Mark Pearson <mpearson-lenovo@squebb.ca>
Suggested-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
Changes in v2:
- Updated commit message to clarify dependency on trackpoint driver
- Now handling sysfs read/write of trackpoint driver using file read/write
- Removed sysfs attribute creation of trackpoint double tap here.
- Reversed the logic and return false right away
- Dropped unnecessary debug messages
- Using dev_dbg() instead of pr_xxxx()
---
 drivers/platform/x86/thinkpad_acpi.c | 155 +++++++++++++++++++++++++--
 1 file changed, 147 insertions(+), 8 deletions(-)

diff --git a/drivers/platform/x86/thinkpad_acpi.c b/drivers/platform/x86/thinkpad_acpi.c
index b59b4d90b0c7..cb981de9bbb2 100644
--- a/drivers/platform/x86/thinkpad_acpi.c
+++ b/drivers/platform/x86/thinkpad_acpi.c
@@ -72,6 +72,13 @@
 #include <linux/units.h>
 #include <linux/workqueue.h>
 
+#include <linux/fs.h>
+#include <linux/file.h>
+#include <linux/err.h>
+#include <linux/fcntl.h>
+#include <linux/namei.h>
+#include <linux/kernel_read_file.h>
+
 #include <acpi/battery.h>
 #include <acpi/video.h>
 
@@ -373,7 +380,8 @@ static struct {
 	u32 hotkey_poll_active:1;
 	u32 has_adaptive_kbd:1;
 	u32 kbd_lang:1;
-	u32 trackpoint_doubletap:1;
+	u32 trackpoint_doubletap_state:1;
+	u32 trackpoint_doubletap_capable:1;
 	struct quirk_entry *quirks;
 } tp_features;
 
@@ -2879,6 +2887,107 @@ static DEVICE_ATTR_RW(hotkey_poll_freq);
 
 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
 
+/*
+ * Trackpoint doubletap handlers
+ * These set of functions will communicate with the sysfs attributes of TrackPoint driver
+ * Attribute : /sys/bus/serio/devices/seriox/doubletap_enabled
+ */
+
+/* Global buffer to reuse path */
+static char trackpoint_doubletap_path[128];
+
+/* Function to find the correct serio path with TrackPoint attribute "doubletap_enabled" */
+static int thinkpad_find_trackpoint_path(void)
+{
+	struct path serio_path;
+	char path_buf[128];
+	int i;
+
+	for (i = 0; i < 10; i++) {
+		snprintf(path_buf, sizeof(path_buf),
+		"/sys/bus/serio/devices/serio%d/doubletap_enabled", i);
+
+		if (!kern_path(path_buf, LOOKUP_FOLLOW, &serio_path)) {
+			path_put(&serio_path);
+			snprintf(trackpoint_doubletap_path, sizeof(trackpoint_doubletap_path),
+				"%s", path_buf);
+			pr_info("ThinkPad ACPI: TrackPoint doubletap found at %s\n",
+				trackpoint_doubletap_path);
+			return 0;
+		}
+	}
+	return -ENODEV;
+}
+
+/* Writing to the sysfs attribute of Trackpoint "doubletap_enabled" */
+static int write_doubletap_sysfs_value(const void *buf, size_t count, loff_t *pos)
+{
+	struct file *filp;
+	ssize_t written;
+
+	if (!buf)
+		return -EINVAL;
+
+	filp = filp_open(trackpoint_doubletap_path, O_WRONLY | O_CREAT, 0644);
+	if (IS_ERR(filp))
+		return PTR_ERR(filp);
+
+	/* Required to avoid EINVAL from vfs checks in some cases */
+	if (!(filp->f_mode & FMODE_CAN_WRITE)) {
+		filp_close(filp, NULL);
+		return -EINVAL;
+	}
+
+	/* Write using kernel_write */
+	written = kernel_write(filp, buf, count, pos);
+	filp_close(filp, NULL);
+
+	return written < 0 ? written : 0;
+}
+
+/* Function to read the TrackPoint doubletap status */
+static int trackpoint_read_doubletap_status(bool *enabled)
+{
+	struct file *filp;
+	loff_t pos = 0;
+	char buf[8];
+	ssize_t ret;
+
+	if (!enabled)
+		return -EINVAL;
+
+	if (!trackpoint_doubletap_path[0])
+		return -ENODEV;
+
+	filp = filp_open(trackpoint_doubletap_path, O_RDONLY, 0);
+	if (IS_ERR(filp))
+		return PTR_ERR(filp);
+
+	ret = kernel_read(filp, buf, sizeof(buf) - 1, &pos);
+	filp_close(filp, NULL);
+
+	if (ret < 0)
+		return ret;
+
+	buf[ret] = '\0'; // Safe: ret < sizeof(buf)
+
+	*enabled = (buf[0] == '1');
+
+	return 0;
+}
+
+/* Function to check the TrackPoint doubletap status */
+static int thinkpad_set_doubletap_status(bool enable)
+{
+	const char *val = enable ? "1" : "0";
+	loff_t pos = 0;
+
+	if (!trackpoint_doubletap_path[0])
+		return -ENODEV;
+
+	return write_doubletap_sysfs_value(val, strlen(val), &pos);
+}
+
 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
 static ssize_t hotkey_radio_sw_show(struct device *dev,
 			   struct device_attribute *attr,
@@ -3326,6 +3435,8 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
 	bool radiosw_state  = false;
 	bool tabletsw_state = false;
 	int hkeyv, res, status, camera_shutter_state;
+	bool dt_state;
+	int rc;
 
 	vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
 			"initializing hotkey subdriver\n");
@@ -3557,9 +3668,22 @@ static int __init hotkey_init(struct ibm_init_struct *iibm)
 
 	hotkey_poll_setup_safe(true);
 
-	/* Enable doubletap by default */
-	tp_features.trackpoint_doubletap = 1;
+	/* Checking doubletap status by default */
+	rc = thinkpad_find_trackpoint_path();
+	if (rc) {
+		dev_dbg(&tpacpi_pdev->dev, "Could not find TrackPoint doubletap sysfs path\n");
+		tp_features.trackpoint_doubletap_capable = false;
+		return 0;
+	}
+	tp_features.trackpoint_doubletap_capable = true;
 
+	rc = trackpoint_read_doubletap_status(&dt_state);
+	if (rc) {
+		/* Disable if access to register fails */
+		dt_state = false;
+		dev_dbg(&tpacpi_pdev->dev, "Doubletap failed to check status\n");
+	}
+	tp_features.trackpoint_doubletap_state = dt_state;
 	return 0;
 }
 
@@ -3863,9 +3987,7 @@ static bool hotkey_notify_8xxx(const u32 hkey, bool *send_acpi_ev)
 {
 	switch (hkey) {
 	case TP_HKEY_EV_TRACK_DOUBLETAP:
-		if (tp_features.trackpoint_doubletap)
-			tpacpi_input_send_key(hkey, send_acpi_ev);
-
+		*send_acpi_ev = true;
 		return true;
 	default:
 		return false;
@@ -11194,6 +11316,7 @@ static struct platform_driver tpacpi_hwmon_pdriver = {
 static bool tpacpi_driver_event(const unsigned int hkey_event)
 {
 	int camera_shutter_state;
+	int rc;
 
 	switch (hkey_event) {
 	case TP_HKEY_EV_BRGHT_UP:
@@ -11285,8 +11408,24 @@ static bool tpacpi_driver_event(const unsigned int hkey_event)
 		mutex_unlock(&tpacpi_inputdev_send_mutex);
 		return true;
 	case TP_HKEY_EV_DOUBLETAP_TOGGLE:
-		tp_features.trackpoint_doubletap = !tp_features.trackpoint_doubletap;
-		return true;
+		if (tp_features.trackpoint_doubletap_capable) {
+			rc = thinkpad_set_doubletap_status(!tp_features.trackpoint_doubletap_state);
+
+			if (rc) {
+				dev_dbg(&tpacpi_pdev->dev, "Trackpoint doubletap toggle failed\n");
+			} else {
+				tp_features.trackpoint_doubletap_state =
+					!tp_features.trackpoint_doubletap_state;
+				dev_dbg(&tpacpi_pdev->dev, "Trackpoint doubletap is %s\n",
+						tp_features.trackpoint_doubletap_state ? "enabled" : "disabled");
+				return true;
+			}
+		}
+		/*
+		 * Suppress the event if Doubletap is not supported
+		 * or if the trackpoint_set_doubletap_status() is failing
+		 */
+		return false;
 	case TP_HKEY_EV_PROFILE_TOGGLE:
 	case TP_HKEY_EV_PROFILE_TOGGLE2:
 		platform_profile_cycle();
-- 
2.48.1


      reply	other threads:[~2025-07-24 20:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-24 20:23 [PATCH v2 1/2] input: mouse: trackpoint: Add doubletap enable/disable support Vishnu Sankar
2025-07-24 20:23 ` Vishnu Sankar [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=20250724202349.11200-2-vishnuocv@gmail.com \
    --to=vishnuocv@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=hansg@kernel.org \
    --cc=hmh@hmh.eng.br \
    --cc=ibm-acpi-devel@lists.sourceforge.net \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpearson-lenovo@squebb.ca \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=vsankar@lenovo.com \
    /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;
as well as URLs for NNTP newsgroup(s).