Linux Documentation
 help / color / mirror / Atom feed
From: "Tomasz Pakuła" <tomasz.pakula.oficjalny@gmail.com>
To: dmitry.torokhov@gmail.com, corbet@lwn.net, jikos@kernel.org,
	bentiss@kernel.org
Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-doc@vger.kernel.org, vi@endrift.com,
	linux-kernel@altimeter.info, peter.hutterer@who-t.net
Subject: [RFC PATCH 4/6] Input: Assign EV_BTN event to HID Joysticks
Date: Sun,  4 Jan 2026 22:31:30 +0100	[thread overview]
Message-ID: <20260104213132.163904-5-tomasz.pakula.oficjalny@gmail.com> (raw)
In-Reply-To: <20260104213132.163904-1-tomasz.pakula.oficjalny@gmail.com>

Joysticks will now fire EV_BTN for every of their buttons, even buttons
that were previously ignored because they were cut off by KEY_MAX. This
in turns enables joysticks to finally report buttons above 80 which was
the previous limitation.

Signed-off-by: Tomasz Pakuła <tomasz.pakula.oficjalny@gmail.com>
---
 drivers/hid/hid-input.c | 27 +++++++++++++++++++++------
 include/linux/input.h   |  2 ++
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
index 900a6fc9813e..1c11077b1577 100644
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -760,11 +760,15 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 		case HID_GD_MOUSE:
 		case HID_GD_POINTER:  code += BTN_MOUSE; break;
 		case HID_GD_JOYSTICK:
-				if (code <= 0xf)
-					code += BTN_JOYSTICK;
-				else
-					code += BTN_TRIGGER_HAPPY - 0x10;
-				break;
+			if (input->button_count >= INPUT_MAX_BUTTONS)
+				goto ignore;
+
+			code += BTN_JOYSTICK;
+			if (code > BTN_DEAD)
+				code += BTN_TRIGGER_HAPPY - BTN_DEAD - 1;
+			if (code > KEY_MAX)
+				code = KEY_RESERVED;
+			break;
 		case HID_GD_GAMEPAD:
 				if (code <= 0xf)
 					code += BTN_GAMEPAD;
@@ -1379,7 +1383,6 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 	}
 
 	set_bit(usage->type, input->evbit);
-
 	/*
 	 * This part is *really* controversial:
 	 * - HID aims at being generic so we should do our best to export
@@ -1390,12 +1393,18 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 	 *   *_MISC+N to overwrite a legitimate even, which confuses userspace
 	 *   (for instance ABS_MISC + 7 is ABS_MT_SLOT, which has a different
 	 *   processing)
+	 * - Joysticks can have arbitrary number of buttons without defined
+	 *   usages. Buttons that extend beyond KEY_MAX are assigned to
+	 *   KEY_RESERVED thus deduplication must be disabled for them.
 	 *
 	 * If devices still want to use this (at their own risk), they will
 	 * have to use the quirk HID_QUIRK_INCREMENT_USAGE_ON_DUPLICATE, but
 	 * the default should be a reliable mapping.
 	 */
 	while (usage->code <= max && test_and_set_bit(usage->code, bit)) {
+		if (field->application == HID_GD_JOYSTICK && usage->code == KEY_RESERVED)
+			break;
+
 		if (device->quirks & HID_QUIRK_INCREMENT_USAGE_ON_DUPLICATE) {
 			usage->code = find_next_zero_bit(bit,
 							 max + 1,
@@ -1455,6 +1464,12 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 	if (usage->type == EV_KEY) {
 		set_bit(EV_MSC, input->evbit);
 		set_bit(MSC_SCAN, input->mscbit);
+
+		if (field->application == HID_GD_JOYSTICK &&
+		    (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
+			set_bit(EV_BTN, input->evbit);
+			++input->button_count;
+		}
 	}
 
 	return;
diff --git a/include/linux/input.h b/include/linux/input.h
index f6389de4a4d1..7f39c663fa85 100644
--- a/include/linux/input.h
+++ b/include/linux/input.h
@@ -214,6 +214,8 @@ struct input_dev {
 };
 #define to_input_dev(d) container_of(d, struct input_dev, dev)
 
+#define INPUT_MAX_BUTTONS 2048 /* A sane limit of supported device buttons */
+
 /*
  * Verify that we are in sync with input_device_id mod_devicetable.h #defines
  */
-- 
2.52.0


  parent reply	other threads:[~2026-01-04 21:31 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-04 21:31 [RFC PATCH 0/6] Input: New EV_BTN event for generic buttons Tomasz Pakuła
2026-01-04 21:31 ` [RFC PATCH 1/6] Input: Introduce " Tomasz Pakuła
2026-01-04 22:30   ` Randy Dunlap
2026-01-04 21:31 ` [RFC PATCH 2/6] Input: Add info about EV_BTN Tomasz Pakuła
2026-01-04 21:31 ` [RFC PATCH 3/6] Input: Fire EV_BTN if found in ev_bit Tomasz Pakuła
2026-01-08 10:48   ` Benjamin Tissoires
2026-01-04 21:31 ` Tomasz Pakuła [this message]
2026-01-08 10:50   ` [RFC PATCH 4/6] Input: Assign EV_BTN event to HID Joysticks Benjamin Tissoires
2026-01-04 21:31 ` [RFC PATCH 5/6] Input: Realign rest of the HID_UP_BUTTON cases Tomasz Pakuła
2026-01-04 21:31 ` [RFC PATCH 6/6] Input: Add EVIOCGBTNCNT Tomasz Pakuła
2026-01-07  5:44 ` [RFC PATCH 0/6] Input: New EV_BTN event for generic buttons Ivan Gorinov
2026-01-08  3:24 ` Peter Hutterer

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=20260104213132.163904-5-tomasz.pakula.oficjalny@gmail.com \
    --to=tomasz.pakula.oficjalny@gmail.com \
    --cc=bentiss@kernel.org \
    --cc=corbet@lwn.net \
    --cc=dmitry.torokhov@gmail.com \
    --cc=jikos@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@altimeter.info \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peter.hutterer@who-t.net \
    --cc=vi@endrift.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