All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Ahelenia Ziemiańska" <nabijaczleweli@nabijaczleweli.xyz>
To: unlisted-recipients:; (no To-header on input)
Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>,
	Peter Hutterer <peter.hutterer@who-t.net>,
	Jiri Kosina <jikos@kernel.org>,
	linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 4/4] HID: input: work around Win8 stylus-on-touchscreen reporting
Date: Mon, 8 Mar 2021 18:42:18 +0100	[thread overview]
Message-ID: <2ca91ac7cf92e3048a236db3cd519f04e12c1e61.1615224800.git.nabijaczleweli@nabijaczleweli.xyz> (raw)
In-Reply-To: <cover.1615224800.git.nabijaczleweli@nabijaczleweli.xyz>

[-- Attachment #1: Type: text/plain, Size: 3352 bytes --]

With this, these devices now behave as tablets as expected by userspace

The search in hidinput_is_win8_touching() terminates at f=0, u=0
on Goodix screens (27C6:0111, 27C6:0113), but I expect it
to have negligible impact on devices that don't have TipSwitch
as the first report as well

Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
---

Notes:
    changes in v2:
      * hidinput_fixup_win8_inrange() became hidinput_is_win8_touching()
      * BarrelSwitch now anded with TipSwitch

 drivers/hid/hid-input.c | 48 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
index a5ba92978473..aee1f1283c1d 100644
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -1273,6 +1273,42 @@ static void hidinput_handle_scroll(struct hid_usage *usage,
 	input_event(input, EV_REL, usage->code, hi_res);
 }
 
+/*
+ * Win8 tablet stylus devices send, in order:
+ *   HID_DG_TIPSWITCH (BTN_TOUCH)
+ *   HID_DG_INVERT    (BTN_TOOL_RUBBER)
+ *   HID_DG_ERASER    (BTN_TOUCH)
+ *   HID_DG_INRANGE   (BTN_TOOL_PEN)
+ *
+ * For each of these states:
+ *   hover     :                         INRANGE
+ *   touching  : TIPSWITCH
+ *   hover+2   :           INVERT        INRANGE
+ *   touching+2:                  ERASER INRANGE
+ *
+ * Which means we'd send BTN_TOUCH=0 + BTN_TOOL_PEN=1 on proximity,
+ * then BTN_TOUCH=1 and BTN_TOOL_PEN=0 in consecutive groups when touched,
+ * indicating the stylus leaving the screen as soon as the two meet.
+ *
+ * Additionally, HID_DG_BARRELSWITCH corresponds directly to the button,
+ * regardless of the tip switch, making it borderline impossible to use precisely.
+ */
+static bool hidinput_is_win8_touching(struct hid_device *hid, struct hid_field *field)
+{
+	unsigned f, u;
+	struct hid_field *rfield;
+
+	for (f = 0; f < field->report->maxfield; ++f) {
+		rfield = field->report->field[f];
+		for (u = 0; u < rfield->maxusage; ++u) {
+			if (rfield->usage[u].hid == HID_DG_TIPSWITCH)
+				return rfield->value[u];
+		}
+	}
+
+	return false;
+}
+
 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
 {
 	struct input_dev *input;
@@ -1306,7 +1342,13 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
 		return;
 	}
 
+	if (usage->hid == HID_DG_ERASER && value)
+		*quirks |= HID_QUIRK_INVERT;
+
 	if (usage->hid == HID_DG_INRANGE) {
+		if (hid->group == HID_GROUP_MULTITOUCH_WIN_8)
+			value = value || hidinput_is_win8_touching(hid, field);
+
 		if (value) {
 			input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
 			return;
@@ -1322,6 +1364,12 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
 		input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
 	}
 
+	if (usage->hid == HID_DG_BARRELSWITCH && hid->group == HID_GROUP_MULTITOUCH_WIN_8) {
+		value = value && hidinput_is_win8_touching(hid, field);
+		input_event(input, usage->type, usage->code, value);
+		return;
+	}
+
 	if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
 		dbg_hid("Maximum Effects - %d\n",value);
 		return;
-- 
2.20.1

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2021-03-08 17:43 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-08 17:41 [PATCH v2 0/4] Stylus-on-touchscreen device support наб
2021-03-08 17:42 ` [PATCH v2 1/4] HID: multitouch: require Finger field to mark Win8 reports as MT Ahelenia Ziemiańska
2021-03-08 17:42 ` [PATCH v2 2/4] HID: multitouch: set Stylus suffix for Stylus-application devices, too Ahelenia Ziemiańska
2021-03-08 17:42 ` [PATCH v2 3/4] HID: input: replace outdated HID numbers+comments with macros Ahelenia Ziemiańska
2021-03-08 17:42 ` Ahelenia Ziemiańska [this message]
2021-04-20 13:17 ` [PATCH v2 0/4] Stylus-on-touchscreen device support наб
2021-05-03  9:11   ` Jiri Kosina
2021-05-03  9:39     ` Benjamin Tissoires
2021-05-03  9:52       ` Benjamin Tissoires
2021-05-03 13:00         ` наб
2021-05-04 13:35           ` Benjamin Tissoires
2021-05-05 13:07         ` Jiri Kosina

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=2ca91ac7cf92e3048a236db3cd519f04e12c1e61.1615224800.git.nabijaczleweli@nabijaczleweli.xyz \
    --to=nabijaczleweli@nabijaczleweli.xyz \
    --cc=benjamin.tissoires@redhat.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peter.hutterer@who-t.net \
    /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.