All of lore.kernel.org
 help / color / mirror / Atom feed
From: Allen Ballway <ballway@chromium.org>
To: dmitry.torokhov@gmail.com
Cc: ballway@chromium.org, benjamin.tissoires@redhat.com,
	dtor@chromium.org, jikos@kernel.org, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org, rydberg@bitmath.org
Subject: [PATCH v3] HID: multitouch: Add quirks for flipped axes
Date: Tue, 13 Dec 2022 01:01:12 +0000	[thread overview]
Message-ID: <20221213010112.3394537-1-ballway@chromium.org> (raw)
In-Reply-To: <Y5d29JwIxku9ubVb@google.com>

Certain touchscreen devices, such as the ELAN9034, are oriented
incorrectly and report touches on opposite points on the X and Y axes.
For example, a 100x200 screen touched at (10,20) would report (90, 180)
and vice versa.

This changed fixes the issue by adding device quirks to transform
the touch points into the correct spaces, from X -> MAX(X) - X,
and Y -> MAX(Y) - Y. These quirks are added in hid-quirks checking
both DMI information and device vendor and product IDs. The quirk
is handled in hid-multitouch to do the actual transformation.

Signed-off-by: Allen Ballway <ballway@chromium.org>
---
V2 -> V3: Use existing HID_QUIRK_*_INVERT and match the quirk in
hid-quirk, passing down to hid-multitouch through the hid device.

V1 -> V2: Address review comments, change to use DMI match. Confirmed
MT_TOOL_X/Y require transformation and update orientation based on
flipped axes.


 drivers/hid/hid-multitouch.c | 43 ++++++++++++++++++++++++++++++++----
 drivers/hid/hid-quirks.c     | 33 +++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 4 deletions(-)

diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index 91a4d3fc30e08..1f4c2aa511359 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -71,6 +71,8 @@ MODULE_LICENSE("GPL");
 #define MT_QUIRK_SEPARATE_APP_REPORT	BIT(19)
 #define MT_QUIRK_FORCE_MULTI_INPUT	BIT(20)
 #define MT_QUIRK_DISABLE_WAKEUP		BIT(21)
+#define MT_QUIRK_X_INVERT		BIT(22)
+#define MT_QUIRK_Y_INVERT		BIT(23)

 #define MT_INPUTMODE_TOUCHSCREEN	0x02
 #define MT_INPUTMODE_TOUCHPAD		0x03
@@ -1086,6 +1088,10 @@ static int mt_process_slot(struct mt_device *td, struct input_dev *input,
 		int orientation = wide;
 		int max_azimuth;
 		int azimuth;
+		int x;
+		int y;
+		int cx;
+		int cy;

 		if (slot->a != DEFAULT_ZERO) {
 			/*
@@ -1104,6 +1110,16 @@ static int mt_process_slot(struct mt_device *td, struct input_dev *input,
 			if (azimuth > max_azimuth * 2)
 				azimuth -= max_azimuth * 4;
 			orientation = -azimuth;
+
+			/* Orientation is inverted if the X or Y axes are
+			 * flipped, but normalized if both are inverted.
+			 */
+			if (quirks & (MT_QUIRK_X_INVERT | MT_QUIRK_Y_INVERT) &&
+			    !((quirks & MT_QUIRK_X_INVERT)
+			      && (quirks & MT_QUIRK_Y_INVERT))) {
+				orientation = -orientation;
+			}
+
 		}

 		if (quirks & MT_QUIRK_TOUCH_SIZE_SCALING) {
@@ -1115,10 +1131,23 @@ static int mt_process_slot(struct mt_device *td, struct input_dev *input,
 			minor = minor >> 1;
 		}

-		input_event(input, EV_ABS, ABS_MT_POSITION_X, *slot->x);
-		input_event(input, EV_ABS, ABS_MT_POSITION_Y, *slot->y);
-		input_event(input, EV_ABS, ABS_MT_TOOL_X, *slot->cx);
-		input_event(input, EV_ABS, ABS_MT_TOOL_Y, *slot->cy);
+		x = quirks & MT_QUIRK_X_INVERT ?
+			input_abs_get_max(input, ABS_MT_POSITION_X) - *slot->x :
+			*slot->x;
+		y = quirks & MT_QUIRK_Y_INVERT ?
+			input_abs_get_max(input, ABS_MT_POSITION_Y) - *slot->y :
+			*slot->y;
+		cx = quirks & MT_QUIRK_X_INVERT ?
+			input_abs_get_max(input, ABS_MT_POSITION_X) - *slot->cx :
+			*slot->cx;
+		cy = quirks & MT_QUIRK_Y_INVERT ?
+			input_abs_get_max(input, ABS_MT_POSITION_Y) - *slot->cy :
+			*slot->cy;
+
+		input_event(input, EV_ABS, ABS_MT_POSITION_X, x);
+		input_event(input, EV_ABS, ABS_MT_POSITION_Y, y);
+		input_event(input, EV_ABS, ABS_MT_TOOL_X, cx);
+		input_event(input, EV_ABS, ABS_MT_TOOL_Y, cy);
 		input_event(input, EV_ABS, ABS_MT_DISTANCE, !*slot->tip_state);
 		input_event(input, EV_ABS, ABS_MT_ORIENTATION, orientation);
 		input_event(input, EV_ABS, ABS_MT_PRESSURE, *slot->p);
@@ -1735,6 +1764,12 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
 	if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
 		td->serial_maybe = true;

+	if (hdev->quirks & HID_QUIRK_X_INVERT)
+		td->mtclass.quirks |= MT_QUIRK_X_INVERT;
+
+	if (hdev->quirks & HID_QUIRK_Y_INVERT)
+		td->mtclass.quirks |= MT_QUIRK_Y_INVERT;
+
 	/* This allows the driver to correctly support devices
 	 * that emit events over several HID messages.
 	 */
diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c
index 0e9702c7f7d6c..47c6cd62f019a 100644
--- a/drivers/hid/hid-quirks.c
+++ b/drivers/hid/hid-quirks.c
@@ -16,6 +16,7 @@
 #include <linux/export.h>
 #include <linux/slab.h>
 #include <linux/mutex.h>
+#include <linux/dmi.h>
 #include <linux/input/elan-i2c-ids.h>

 #include "hid-ids.h"
@@ -957,6 +958,29 @@ static const struct hid_device_id hid_mouse_ignore_list[] = {
 	{ }
 };

+static const struct hid_device_id elan_flipped_quirks[] = {
+	{ HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8, USB_VENDOR_ID_ELAN, 0x2dcd),
+		HID_QUIRK_X_INVERT | HID_QUIRK_Y_INVERT },
+	{ }
+};
+
+/*
+ * This list contains devices which have specific issues based on the system
+ * they're on and not just the device itself. The driver_data will have a
+ * specific hid device to match against.
+ */
+static const struct dmi_system_id dmi_override_table[] = {
+	{
+		.ident = "DynaBook K50/FR",
+		.matches = {
+			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dynabook Inc."),
+			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "dynabook K50/FR"),
+		},
+		.driver_data = (void *)elan_flipped_quirks,
+	},
+	{ }	/* Terminate list */
+};
+
 bool hid_ignore(struct hid_device *hdev)
 {
 	int i;
@@ -1238,6 +1262,7 @@ static unsigned long hid_gets_squirk(const struct hid_device *hdev)
 {
 	const struct hid_device_id *bl_entry;
 	unsigned long quirks = 0;
+	const struct dmi_system_id *system_id;

 	if (hid_match_id(hdev, hid_ignore_list))
 		quirks |= HID_QUIRK_IGNORE;
@@ -1249,6 +1274,14 @@ static unsigned long hid_gets_squirk(const struct hid_device *hdev)
 	if (bl_entry != NULL)
 		quirks |= bl_entry->driver_data;

+	system_id = dmi_first_match(dmi_override_table);
+	if (system_id != NULL) {
+		bl_entry = hid_match_id(hdev, system_id->driver_data);
+		if (bl_entry != NULL)
+			quirks |= bl_entry->driver_data;
+	}
+
+
 	if (quirks)
 		dbg_hid("Found squirk 0x%lx for HID device 0x%04x:0x%04x\n",
 			quirks, hdev->vendor, hdev->product);
--
2.39.0.rc1.256.g54fd8350bd-goog


  reply	other threads:[~2022-12-13  1:01 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-06 17:38 [PATCH] HID: multitouch: Add quirks for flipped axes Allen Ballway
2022-12-06 20:11 ` Dmitry Torokhov
2022-12-08 16:58   ` [PATCH v2] " Allen Ballway
2022-12-12 18:46     ` Dmitry Torokhov
2022-12-13  1:01       ` Allen Ballway [this message]
2022-12-13  9:19         ` [PATCH v3] " Benjamin Tissoires
2022-12-13 16:45           ` Allen Ballway
2022-12-13 21:46             ` Allen Ballway
2022-12-14 18:13               ` [PATCH v4] " Allen Ballway
2022-12-14 22:26                 ` kernel test robot
2022-12-14 22:57                 ` kernel test robot
2022-12-14 23:45                 ` [PATCH v5] " Allen Ballway
2022-12-15  8:02                   ` kernel test robot
2022-12-15 12:35                   ` kernel test robot
2022-12-15 17:26                   ` [PATCH v6] " Allen Ballway
2023-01-10 20:25                     ` [PATCH v6 RESEND] " Allen Ballway
2023-01-18  9:09                       ` Jiri Kosina
2023-01-31 19:46                       ` [PATCH v7] " Allen Ballway
2022-12-15  0:17                 ` [PATCH v4] " kernel test robot

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=20221213010112.3394537-1-ballway@chromium.org \
    --to=ballway@chromium.org \
    --cc=benjamin.tissoires@redhat.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=dtor@chromium.org \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rydberg@bitmath.org \
    /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 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.