The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Siarhei Vishniakou <svv@google.com>
To: Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Jiri Kosina <jikos@kernel.org>,
	 Benjamin Tissoires <bentiss@kernel.org>,
	Ping Cheng <ping.cheng@wacom.com>,
	 Jason Gerecke <jason.gerecke@wacom.com>
Cc: Jason Gerecke <killertofu@gmail.com>,
	Jonathan Corbet <corbet@lwn.net>,
	 Shuah Khan <skhan@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	 Henrik Rydberg <rydberg@bitmath.org>,
	linux-input@vger.kernel.org,  linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	 Siarhei Vishniakou <svv@google.com>
Subject: [PATCH v2 3/3] HID: wacom: report ABS_TWIST alongside ABS_Z for pen twist
Date: Wed, 25 Nov 2026 18:43:37 -0800	[thread overview]
Message-ID: <20261126024337.4168845-4-svv@google.com> (raw)
In-Reply-To: <20261126024337.4168845-1-svv@google.com>

Historically, Wacom tablets reported stylus barrel rotation (twist) via
ABS_Z because a dedicated evdev axis did not exist, with the zero point
offset by +1/4 revolution so that 0 aligns with the tablet's left (-X)
edge for compatibility with legacy userspace.

Now that ABS_TWIST is defined, report pen twist on ABS_TWIST using the
standard HID coordinate system (zero at the top/-Y edge, increasing
clockwise), while keeping the shifted ABS_Z axis for backwards
compatibility with existing applications.

Signed-off-by: Siarhei Vishniakou <svv@google.com>
---
Changes since v1:
- Added comments explaining the forced angular resolution_code in
  wacom_map_usage(), the order-dependent double-mapping in
  wacom_wac_pen_usage_mapping(), and explicit ABS_Z emission in
  wacom_wac_pen_event() (Jason).
- Emit un-offset twist (zero at top/-Y, matching ABS_TWIST
  documentation and HID_DG_TWIST) on ABS_TWIST across all three pen
  rotation paths (wacom_intuos_general, wacom_intuos_pro2_bt_pen,
  wacom_wac_pen_event), while preserving the +1/4 revolution offset
  (zero at left/-X) on ABS_Z for backwards compatibility.

 drivers/hid/wacom_wac.c | 94 +++++++++++++++++++++++++++++++----------
 1 file changed, 72 insertions(+), 22 deletions(-)

diff --git a/drivers/hid/wacom_wac.c b/drivers/hid/wacom_wac.c
index ab89a11d6740..a57aa95b2d80 100644
--- a/drivers/hid/wacom_wac.c
+++ b/drivers/hid/wacom_wac.c
@@ -769,8 +769,10 @@ static void wacom_exit_report(struct wacom_wac *wacom)
 		input_report_key(input, BTN_STYLUS2, 0);
 		input_report_key(input, BTN_TOUCH, 0);
 		input_report_abs(input, ABS_WHEEL, 0);
-		if (features->type >= INTUOS3S)
+		if (features->type >= INTUOS3S) {
 			input_report_abs(input, ABS_Z, 0);
+			input_report_abs(input, ABS_TWIST, 0);
+		}
 	}
 	input_report_key(input, wacom->tool[idx], 0);
 	input_report_abs(input, ABS_MISC, 0); /* reset tool id */
@@ -943,11 +945,22 @@ static int wacom_intuos_general(struct wacom_wac *wacom)
 	case 0x05:
 		/* Rotation packet */
 		if (features->type >= INTUOS3S) {
+			int twist, rot;
+
 			/* I3 marker pen rotation */
 			t = (data[6] << 3) | ((data[7] >> 5) & 7);
-			t = (data[7] & 0x20) ? ((t > 900) ? ((t-1) / 2 - 1350) :
-				((t-1) / 2 + 450)) : (450 - t / 2) ;
-			input_report_abs(input, ABS_Z, t);
+			twist = (data[7] & 0x20) ? (int)((t - 1) / 2) :
+				-(int)(t / 2);
+			input_report_abs(input, ABS_TWIST, twist);
+
+			/*
+			 * Shift zero point from top (-Y) to left (-X) for
+			 * legacy ABS_Z compatibility.
+			 */
+			rot = twist + 450;
+			if (rot > 899)
+				rot -= 1800;
+			input_report_abs(input, ABS_Z, rot);
 		} else {
 			/* 4D mouse 2nd packet */
 			t = (data[6] << 3) | ((data[7] >> 5) & 7);
@@ -1365,10 +1378,12 @@ static void wacom_intuos_pro2_bt_pen(struct wacom_wac *wacom)
 
 			if (wacom->features.type == INTUOSP2_BT ||
 			    wacom->features.type == INTUOSP2S_BT) {
-				/* Fix rotation alignment: userspace expects zero at left */
-				int16_t rotation =
-					(int16_t)get_unaligned_le16(&frame[9]);
-				rotation += 1800/4;
+				s16 twist = (s16)get_unaligned_le16(&frame[9]);
+				/*
+				 * Legacy ABS_Z expects zero at left (-X), while
+				 * hardware and ABS_TWIST use zero at top (-Y).
+				 */
+				int rotation = twist + 1800 / 4;
 
 				if (rotation > 899)
 					rotation -= 1800;
@@ -1377,6 +1392,7 @@ static void wacom_intuos_pro2_bt_pen(struct wacom_wac *wacom)
 						 (signed char)frame[7]);
 				input_report_abs(pen_input, ABS_TILT_Y,
 						 (signed char)frame[8]);
+				input_report_abs(pen_input, ABS_TWIST, twist);
 				input_report_abs(pen_input, ABS_Z, rotation);
 				input_report_abs(pen_input, ABS_WHEEL,
 						 get_unaligned_le16(&frame[11]));
@@ -1914,9 +1930,14 @@ static void wacom_map_usage(struct input_dev *input, struct hid_usage *usage,
 	int resolution_code = code;
 	int resolution;
 
-	if (equivalent_usage == HID_DG_TWIST) {
-		resolution_code = ABS_RZ;
-	}
+	/*
+	 * HID_DG_TWIST is double-mapped to both ABS_Z (for backwards
+	 * compatibility) and ABS_TWIST. Force a radial axis code here so
+	 * hidinput_calc_abs_res() computes angular resolution (units/radian)
+	 * even when configuring ABS_Z, which it otherwise treats as linear.
+	 */
+	if (equivalent_usage == HID_DG_TWIST)
+		resolution_code = ABS_TWIST;
 
 	resolution = hidinput_calc_abs_res(field, resolution_code);
 
@@ -2385,7 +2406,16 @@ static void wacom_wac_pen_usage_mapping(struct hid_device *hdev,
 		wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_Y, 0);
 		break;
 	case HID_DG_TWIST:
+		/*
+		 * Map HID_DG_TWIST twice for backwards compatibility with
+		 * legacy userspace that expects pen twist on ABS_Z. The order
+		 * matters: the latter call overwrites usage->type and
+		 * usage->code to point to ABS_TWIST, which prevents
+		 * wacom_wac_pen_event() from automatically emitting ABS_Z;
+		 * ABS_Z is emitted explicitly there instead.
+		 */
 		wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
+		wacom_map_usage(input, usage, field, EV_ABS, ABS_TWIST, 0);
 		break;
 	case HID_DG_ERASER:
 		input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
@@ -2492,15 +2522,9 @@ static void wacom_wac_pen_event(struct hid_device *hdev, struct hid_field *field
 		}
 		return;
 	case HID_DG_TWIST:
-		/* don't modify the value if the pen doesn't support the feature */
-		if (!wacom_is_art_pen(wacom_wac->id[0])) return;
-
-		/*
-		 * Userspace expects pen twist to have its zero point when
-		 * the buttons/finger is on the tablet's left. HID values
-		 * are zero when buttons are toward the top.
-		 */
-		value = wacom_offset_rotation(input, usage, value, 1, 4);
+		/* don't report twist if the pen doesn't support the feature */
+		if (!wacom_is_art_pen(wacom_wac->id[0]))
+			return;
 		break;
 	case WACOM_HID_WD_SENSE:
 		wacom_wac->hid_data.sense_state = value;
@@ -2588,10 +2612,29 @@ static void wacom_wac_pen_event(struct hid_device *hdev, struct hid_field *field
 		return;
 
 	/* send pen events only when the pen is in range */
-	if (wacom_wac->hid_data.inrange_state)
+	if (wacom_wac->hid_data.inrange_state) {
 		input_event(input, usage->type, usage->code, value);
-	else if (wacom_wac->shared->stylus_in_proximity && !wacom_wac->hid_data.sense_state)
+		if (equivalent_usage == HID_DG_TWIST) {
+			/*
+			 * wacom_wac_pen_usage_mapping() double-maps
+			 * HID_DG_TWIST to both ABS_Z and ABS_TWIST, but
+			 * usage->type and usage->code can only hold one
+			 * mapping (ABS_TWIST). The input_event() above emits
+			 * ABS_TWIST using the un-offset HID value (zero when
+			 * buttons are toward the top, -Y). Also report on
+			 * ABS_Z for backwards compatibility, shifted so its
+			 * zero point is on the tablet's left (-X) as legacy
+			 * userspace expects.
+			 */
+			value = wacom_offset_rotation(input, usage, value, 1, 4);
+			input_report_abs(input, ABS_Z, value);
+		}
+	} else if (wacom_wac->shared->stylus_in_proximity &&
+		   !wacom_wac->hid_data.sense_state) {
 		input_event(input, usage->type, usage->code, 0);
+		if (equivalent_usage == HID_DG_TWIST)
+			input_report_abs(input, ABS_Z, 0);
+	}
 }
 
 static void wacom_wac_pen_pre_report(struct hid_device *hdev,
@@ -3807,6 +3850,7 @@ void wacom_setup_device_quirks(struct wacom *wacom)
 		__clear_bit(BTN_TOOL_PENCIL, wacom_wac->pen_input->keybit);
 		__clear_bit(BTN_TOOL_AIRBRUSH, wacom_wac->pen_input->keybit);
 		__clear_bit(ABS_Z, wacom_wac->pen_input->absbit);
+		__clear_bit(ABS_TWIST, wacom_wac->pen_input->absbit);
 		__clear_bit(ABS_DISTANCE, wacom_wac->pen_input->absbit);
 		__clear_bit(ABS_TILT_X, wacom_wac->pen_input->absbit);
 		__clear_bit(ABS_TILT_Y, wacom_wac->pen_input->absbit);
@@ -3889,6 +3933,8 @@ int wacom_setup_pen_input_capabilities(struct input_dev *input_dev,
 	case CINTIQ_COMPANION_2:
 		input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
 		input_abs_set_res(input_dev, ABS_Z, 287);
+		input_set_abs_params(input_dev, ABS_TWIST, -900, 899, 0, 0);
+		input_abs_set_res(input_dev, ABS_TWIST, 287);
 		wacom_setup_cintiq(wacom_wac);
 		break;
 
@@ -3901,6 +3947,8 @@ int wacom_setup_pen_input_capabilities(struct input_dev *input_dev,
 	case INTUOS4S:
 		input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
 		input_abs_set_res(input_dev, ABS_Z, 287);
+		input_set_abs_params(input_dev, ABS_TWIST, -900, 899, 0, 0);
+		input_abs_set_res(input_dev, ABS_TWIST, 287);
 		fallthrough;
 
 	case INTUOS:
@@ -3921,6 +3969,8 @@ int wacom_setup_pen_input_capabilities(struct input_dev *input_dev,
 
 		input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
 		input_abs_set_res(input_dev, ABS_Z, 287);
+		input_set_abs_params(input_dev, ABS_TWIST, -900, 899, 0, 0);
+		input_abs_set_res(input_dev, ABS_TWIST, 287);
 
 		wacom_setup_intuos(wacom_wac);
 		break;
-- 
2.56.1.911.g883d8bcb20-goog


      parent reply	other threads:[~2026-11-26  2:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  2:43 [PATCH v2 0/3] Input: add ABS_TWIST and support it in HID and Wacom Siarhei Vishniakou
2026-11-26  2:43 ` [PATCH v2 1/3] Input: add ABS_TWIST event code Siarhei Vishniakou
2026-11-26  5:53   ` Dmitry Torokhov
2026-11-26  2:43 ` [PATCH v2 2/3] HID: input: map Digitizers Twist usage to ABS_TWIST Siarhei Vishniakou
2026-11-26  2:43 ` Siarhei Vishniakou [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=20261126024337.4168845-4-svv@google.com \
    --to=svv@google.com \
    --cc=bentiss@kernel.org \
    --cc=corbet@lwn.net \
    --cc=dmitry.torokhov@gmail.com \
    --cc=jason.gerecke@wacom.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=ping.cheng@wacom.com \
    --cc=rdunlap@infradead.org \
    --cc=rydberg@bitmath.org \
    --cc=skhan@linuxfoundation.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox