Skip to contentSkip to conversation

HID: input: map HID_GD_Z to ABS_DISTANCE for stylus/pen

PATCH In review Ping Cheng 2 participants 3 messages Oct 27, 2025 – Nov 20, 2025 HID

lore
Share & tools
lore

Ping Cheng

author
#0 lore raw

PATCH v2 1/2 HID: input: map HID_GD_Z to ABS_DISTANCE for stylus/pen

HID_GD_Z is mapped to ABS_Z for stylus and pen in hid-input.c. But HID_GD_Z should be used to report ABS_DISTANCE for stylus and pen as described at:
Documentation/input/event-codes.rst#n226

* ABS_DISTANCE:

- Used to describe the distance of a tool from an interaction surface. This event should only be emitted while the tool is hovering, meaning in close proximity of the device and while the value of the BTN_TOUCH code is 0. If the input device may be used freely in three dimensions, consider ABS_Z instead.
- BTN_TOOL_<name> should be set to 1 when the tool comes into detectable proximity and set to 0 when the tool leaves detectable proximity. BTN_TOOL_<name> signals the type of tool that is currently detected by the hardware and is otherwise independent of ABS_DISTANCE and/or BTN_TOUCH.

This patch makes the correct mapping. The ABS_DISTANCE is currently not mapped by any HID usage in hid-generic driver.

---
v2: updated pen/stylus checks

---
 drivers/hid/hid-input.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/hid/hid-input.c +17 -1
index f45f856a127f..141359de5ce5 100644
@@ -860,7 +860,7 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 
 		switch (usage->hid) {
 		/* These usage IDs map directly to the usage codes. */
-		case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
+		case HID_GD_X: case HID_GD_Y:
 		case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
 			if (field->flags & HID_MAIN_ITEM_RELATIVE)
 				map_rel(usage->hid & 0xf);
@@ -868,6 +868,22 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 				map_abs_clear(usage->hid & 0xf);
 			break;
 
+		case HID_GD_Z:
+			/* HID_GD_Z is mapped to ABS_DISTANCE for stylus/pen */
+			if (field->flags & HID_MAIN_ITEM_RELATIVE) {
+				map_rel(usage->hid & 0xf);
+			} else {
+				if (field->application == HID_DG_PEN ||
+				    field->physical == HID_DG_PEN ||
+				    field->logical == HID_DG_STYLUS ||
+				    field->physical == HID_DG_STYLUS ||
+				    field->application == HID_DG_DIGITIZER)
+					map_abs_clear(ABS_DISTANCE);
+				else
+					map_abs_clear(usage->hid & 0xf);
+			}
+			break;
+
 		case HID_GD_WHEEL:
 			if (field->flags & HID_MAIN_ITEM_RELATIVE) {
 				set_bit(REL_WHEEL, input->relbit);
Signature · 1 line
2.51.0
author
#1 lore raw

Hi Benjamin and Jiri,

Can one of you review and merge the patchset?

This patch was from me. The second patch for selftest followed Benjamin's suggestion

Cheers,
Ping.

Quoted 67 lines · Ping Cheng

HID_GD_Z is mapped to ABS_Z for stylus and pen in hid-input.c. But HID_GD_Z should be used to report ABS_DISTANCE for stylus and pen as described at:
Documentation/input/event-codes.rst#n226

* ABS_DISTANCE:

- Used to describe the distance of a tool from an interaction surface. This event should only be emitted while the tool is hovering, meaning in close proximity of the device and while the value of the BTN_TOUCH code is 0. If the input device may be used freely in three dimensions, consider ABS_Z instead.
- BTN_TOOL_<name> should be set to 1 when the tool comes into detectable proximity and set to 0 when the tool leaves detectable proximity. BTN_TOOL_<name> signals the type of tool that is currently detected by the hardware and is otherwise independent of ABS_DISTANCE and/or BTN_TOUCH.

This patch makes the correct mapping. The ABS_DISTANCE is currently not mapped by any HID usage in hid-generic driver.

---
v2: updated pen/stylus checks

---
 drivers/hid/hid-input.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/hid/hid-input.c +17 -2
index f45f856a127f..141359de5ce5 100644
@@ -860,7 +860,7 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 
                switch (usage->hid) {
                /* These usage IDs map directly to the usage codes. */
-               case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
+               case HID_GD_X: case HID_GD_Y:
                case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
                        if (field->flags & HID_MAIN_ITEM_RELATIVE)
                                map_rel(usage->hid & 0xf);
@@ -868,6 +868,22 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
                                map_abs_clear(usage->hid & 0xf);
                        break;
 
+               case HID_GD_Z:
+                       /* HID_GD_Z is mapped to ABS_DISTANCE for stylus/pen */
+                       if (field->flags & HID_MAIN_ITEM_RELATIVE) {
+                               map_rel(usage->hid & 0xf);
+                       } else {
+                               if (field->application == HID_DG_PEN ||
+                                   field->physical == HID_DG_PEN ||
+                                   field->logical == HID_DG_STYLUS ||
+                                   field->physical == HID_DG_STYLUS ||
+                                   field->application == HID_DG_DIGITIZER)
+                                       map_abs_clear(ABS_DISTANCE);
+                               else
+                                       map_abs_clear(usage->hid & 0xf);
+                       }
+                       break;
+
                case HID_GD_WHEEL:
                        if (field->flags & HID_MAIN_ITEM_RELATIVE) {
                                set_bit(REL_WHEEL, input->relbit);
--

2.51.0

maintainer
#2 lore raw
Quoted 6 lines · Ping Cheng

Hi Benjamin and Jiri,

Can one of you review and merge the patchset?

This patch was from me. The second patch for selftest followed Benjamin's suggestion

Sorry for the delay. This is now queued in hid.git#for-6.19/core.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs