linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Tissoires <benjamin.tissoires@redhat.com>
To: "Jiri Kosina" <jikos@kernel.org>,
	"Dmitry Torokhov" <dmitry.torokhov@gmail.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Ahelenia Ziemiańska" <nabijaczleweli@nabijaczleweli.xyz>,
	"Ping Cheng" <pinglinux@gmail.com>,
	"Aaron Armstrong Skomra" <skomra@gmail.com>,
	"Jason Gerecke" <killertofu@gmail.com>,
	"Peter Hutterer" <peter.hutterer@who-t.net>
Cc: linux-input@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Benjamin Tissoires <benjamin.tissoires@redhat.com>
Subject: [PATCH 10/12] HID: input: remove the need for HID_QUIRK_INVERT
Date: Wed, 26 Jan 2022 17:18:30 +0100	[thread overview]
Message-ID: <20220126161832.3193805-11-benjamin.tissoires@redhat.com> (raw)
In-Reply-To: <20220126161832.3193805-1-benjamin.tissoires@redhat.com>

HID_QUIRK_INVERT is kind of complex to deal with and was bogus.

Furthermore, it didn't make sense to use a global per struct hid_device
quirk for something dynamic as the current state.

Store the current tool information in the report itself, and re-order
the processing of the fields to enforce having all the tablet "state"
fields before getting to In Range and other input fields.

This way, we now have all the information whether a tool is present
or not while processing In Range.

This new behavior enforces that only one tool gets forwarded to userspace
at the same time, and that if either eraser or invert is set, we enforce
BTN_TOOL_RUBBER.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
---
 drivers/hid/hid-input.c | 66 +++++++++++++++++++++++++++++++++++------
 include/linux/hid.h     |  6 +++-
 2 files changed, 62 insertions(+), 10 deletions(-)

diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
index 61d91117f4ae..2d13d3ad9d3c 100644
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -63,8 +63,11 @@ static const struct {
  * This still leaves us 65535 individual priority values.
  */
 static const __u32 hidinput_usages_priorities[] = {
+	HID_DG_ERASER,		/* Eraser (eraser touching) must always come before tipswitch */
 	HID_DG_INVERT,		/* Invert must always come before In Range */
-	HID_DG_INRANGE,
+	HID_DG_TIPSWITCH,	/* Is the tip of the tool touching? */
+	HID_DG_TIPPRESSURE,	/* Tip Pressure might emulate tip switch */
+	HID_DG_INRANGE,		/* In Range needs to come after the other tool states */
 };
 
 #define map_abs(c)	hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
@@ -1368,6 +1371,7 @@ static void hidinput_handle_scroll(struct hid_usage *usage,
 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
 {
 	struct input_dev *input;
+	struct hid_report *report = field->report;
 	unsigned *quirks = &hid->quirks;
 
 	if (!usage->type)
@@ -1418,25 +1422,69 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
 	}
 
 	switch (usage->hid) {
+	case HID_DG_ERASER:
+		report->tool_active |= !!value;
+
+		/*
+		 * if eraser is set, we must enforce BTN_TOOL_RUBBER
+		 * to accommodate for devices not following the spec.
+		 */
+		if (value)
+			report->tool = BTN_TOOL_RUBBER;
+
+		/* let hid-input set BTN_TOUCH */
+		break;
+
 	case HID_DG_INVERT:
-		*quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
+		report->tool_active |= !!value;
+
+		/*
+		 * If invert is set, we store BTN_TOOL_RUBBER.
+		 */
+		if (value)
+			report->tool = BTN_TOOL_RUBBER;
+
+		/* no further processing */
 		return;
 
 	case HID_DG_INRANGE:
-		if (value) {
-			input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
-			return;
-		}
-		input_event(input, usage->type, usage->code, 0);
-		input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
+		report->tool_active |= !!value;
+
+		/*
+		 * If the tool is in used (any of TipSwitch, Erase, Invert,
+		 * InRange), and if tool is not set, store our mapping
+		 */
+		if (report->tool_active && !report->tool)
+			report->tool = usage->code;
+
+		input_event(input, EV_KEY, usage->code, report->tool == usage->code);
+		input_event(input, EV_KEY, BTN_TOOL_RUBBER, report->tool == BTN_TOOL_RUBBER);
+
+		/* reset tool and tool_active for the next event */
+		report->tool = 0;
+		report->tool_active = false;
+
+		/* no further processing */
 		return;
 
+	case HID_DG_TIPSWITCH:
+		report->tool_active |= !!value;
+
+		/* if tool is set we should ignore the current value */
+		if (report->tool)
+			return;
+
+		break;
+
 	case HID_DG_TIPPRESSURE:
 		if (*quirks & HID_QUIRK_NOTOUCH) {
 			int a = field->logical_minimum;
 			int b = field->logical_maximum;
 
-			input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
+			if (value > a + ((b - a) >> 3)) {
+				input_event(input, EV_KEY, BTN_TOUCH, 1);
+				report->tool_active = true;
+			}
 		}
 		break;
 
diff --git a/include/linux/hid.h b/include/linux/hid.h
index eaad0655b05c..feb8df61168f 100644
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -347,7 +347,7 @@ struct hid_item {
  */
 #define MAX_USBHID_BOOT_QUIRKS 4
 
-#define HID_QUIRK_INVERT			BIT(0)
+/* BIT(0) reserved for backward compatibility, was HID_QUIRK_INVERT */
 #define HID_QUIRK_NOTOUCH			BIT(1)
 #define HID_QUIRK_IGNORE			BIT(2)
 #define HID_QUIRK_NOGET				BIT(3)
@@ -515,6 +515,10 @@ struct hid_report {
 	unsigned maxfield;				/* maximum valid field index */
 	unsigned size;					/* size of the report (bits) */
 	struct hid_device *device;			/* associated device */
+
+	/* tool related state */
+	bool tool_active;				/* whether the current tool is active */
+	unsigned int tool;				/* BTN_TOOL_* */
 };
 
 #define HID_MAX_IDS 256
-- 
2.33.1


  parent reply	other threads:[~2022-01-26 16:19 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-26 16:18 [PATCH 00/12] HID: fix for generic input processing Benjamin Tissoires
2022-01-26 16:18 ` [PATCH 01/12] HID: core: statically allocate read buffers Benjamin Tissoires
2022-01-26 16:18 ` [PATCH 02/12] HID: core: de-duplicate some code in hid_input_field() Benjamin Tissoires
2022-01-26 16:18 ` [PATCH 03/12] HID: core: split data fetching from processing " Benjamin Tissoires
2022-01-26 16:18 ` [PATCH 04/12] HID: input: tag touchscreens as such if the physical is not there Benjamin Tissoires
2022-01-26 16:18 ` [PATCH 05/12] HID: input: rework spaghetti code with switch statements Benjamin Tissoires
2022-01-26 16:18 ` [PATCH 06/12] HID: input: move up out-of-range processing of input values Benjamin Tissoires
2022-01-26 16:18 ` [PATCH 07/12] HID: compute an ordered list of input fields to process Benjamin Tissoires
2022-01-26 16:18 ` [PATCH 08/12] HID: core: for input reports, process the usages by priority list Benjamin Tissoires
2022-01-26 16:18 ` [PATCH 09/12] HID: input: enforce Invert usage to be processed before InRange Benjamin Tissoires
2022-01-26 16:18 ` Benjamin Tissoires [this message]
     [not found]   ` <CAF8JNhLCXT7N4DubYYT12eMphDH-6U69ci7zFisJfZwsviJGkQ@mail.gmail.com>
2022-02-02  9:54     ` [PATCH 10/12] HID: input: remove the need for HID_QUIRK_INVERT Benjamin Tissoires
2022-01-26 16:18 ` [PATCH 11/12] HID: input: accommodate priorities for slotted devices Benjamin Tissoires
2022-01-26 16:18 ` [PATCH 12/12] Input: docs: add more details on the use of BTN_TOOL Benjamin Tissoires
2022-02-09  6:22   ` Peter Hutterer
2022-02-14 10:48     ` Benjamin Tissoires
2022-02-08 19:19 ` [PATCH 00/12] HID: fix for generic input processing Angela Czubak
2022-02-14 10:51   ` Angela Czubak
2022-02-14 11:03     ` Benjamin Tissoires
2022-02-14 10:53   ` Benjamin Tissoires

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=20220126161832.3193805-11-benjamin.tissoires@redhat.com \
    --to=benjamin.tissoires@redhat.com \
    --cc=corbet@lwn.net \
    --cc=dmitry.torokhov@gmail.com \
    --cc=jikos@kernel.org \
    --cc=killertofu@gmail.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nabijaczleweli@nabijaczleweli.xyz \
    --cc=peter.hutterer@who-t.net \
    --cc=pinglinux@gmail.com \
    --cc=skomra@gmail.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).