Linux Input/HID development
 help / color / mirror / Atom feed
From: Cristian Mazzotta <cmmazzo@icloud.com>
To: aer@tuxedocomputers.com
Cc: W_Armin@gmx.de, bentiss@kernel.org, jikos@kernel.org,
	linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
	wse@tuxedocomputers.com, Cristian Mazzotta <cmmazzo@icloud.com>
Subject: [PATCH 3/4] HID: lamparray: transfer control when use_leds_uapi changes
Date: Wed,  9 Sep 2026 11:52:56 -0500	[thread overview]
Message-ID: <20260909165257.352597-4-cmmazzo@icloud.com> (raw)
In-Reply-To: <20260909165257.352597-1-cmmazzo@icloud.com>

Autonomous mode is disabled once in lamparray_register() and never
changed again. Writing 0 to use_leds_uapi unregisters the LED class
device but leaves the device under host control, and writing 1
re-registers the class device without taking the device back, so lamp
updates sent afterwards are ignored.

On an Acer Predator PT14-52T (USB keyboard 05AF:767A) the device stays
with whatever last drove it: after writing 0, running a userspace tool
that sets a hardware effect, then writing 1, the LED class device
reports the cached values despite the hardware still running an effect.
Reading AutonomousMode back returns the last value written by the host
rather than the state the device is actually in, so the driver cannot
detect this.

Disable autonomous mode before registering the LED class device and
re-enable it after unregistering, so control is handed over in both
directions. Unregister the class device before handing the hardware
back, so userspace cannot write lamp updates to a device that is no
longer accepting them.

Failure to re-enable autonomous mode on the disable path is logged but
not propagated since the class device is already gone and the write
has otherwise succeeded. The restore failure path drops back to
hid_warn() for the same reason: it is recoverable, and the attribute
reverts to its previous value.

Signed-off-by: Cristian Mazzotta <cmmazzo@icloud.com>
---
 drivers/hid/hid-lamparray.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/drivers/hid/hid-lamparray.c b/drivers/hid/hid-lamparray.c
index 70c596f292a2..bbde006d119b 100644
--- a/drivers/hid/hid-lamparray.c
+++ b/drivers/hid/hid-lamparray.c
@@ -659,25 +659,43 @@ static ssize_t use_leds_uapi_store(struct device *dev,
 	ldev->use_leds_uapi = val;
 	mutex_unlock(&ldev->dev_lock);
 
+	/*
+	 * Take the device out of autonomous mode before exposing the LED
+	 * class device, and hand it back afterwards, so that control is
+	 * transferred in both directions rather than only at probe.
+	 */
 	if (val == 1) {
+		ret = lamparray_hw_set_autonomous(ldev, false);
+		if (ret)
+			goto err_revert;
 		ret = lamparray_register_led(ldev);
 		if (ret) {
-			mutex_lock(&ldev->dev_lock);
-			ldev->use_leds_uapi = old_val;
-			mutex_unlock(&ldev->dev_lock);
-			return ret;
+			lamparray_hw_set_autonomous(ldev, true);
+			goto err_revert;
 		}
 		ret = lamparray_restore_state(ldev);
 		if (ret) {
-			hid_err(ldev->hdev, "Could not restore state: %d\n", ret);
-			return ret;
+			hid_warn(ldev->hdev, "Could not restore state: %d\n", ret);
+			lamparray_unregister_led(ldev);
+			lamparray_hw_set_autonomous(ldev, true);
+			goto err_revert;
 		}
-
 	} else {
 		lamparray_unregister_led(ldev);
+		ret = lamparray_hw_set_autonomous(ldev, true);
+		if (ret) {
+			hid_warn(ldev->hdev, "Could not enable autonomous mode: %d\n", ret);
+			return count;
+		}
 	}
 
 	return count;
+
+err_revert:
+	mutex_lock(&ldev->dev_lock);
+	ldev->use_leds_uapi = old_val;
+	mutex_unlock(&ldev->dev_lock);
+	return ret;
 }
 static DEVICE_ATTR_RW(use_leds_uapi);
 
-- 
2.55.0


  parent reply	other threads:[~2026-09-09 16:53 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-03  7:35 [PATCH v5 0/2] HID: generic: add LampArray support via hid-lamparray helper Aaron Erhardt
2026-09-03  7:35 ` [PATCH v5 1/2] HID: lamparray: add new LampArray helper module Aaron Erhardt
2026-09-03  7:48   ` sashiko-bot
2026-09-03 20:07   ` Werner Sembach
2026-09-04  8:51     ` Aaron Erhardt
2026-09-04 21:30   ` Armin Wolf
2026-09-07 16:13     ` Aaron Erhardt
2026-09-03  7:35 ` [PATCH v5 2/2] HID: generic: add LampArray support via hid-lamparray helper Aaron Erhardt
2026-09-03  7:46   ` sashiko-bot
2026-09-04 20:49 ` [PATCH v5 0/2] " Armin Wolf
2026-09-07 16:30   ` Aaron Erhardt
2026-09-09 16:52     ` [PATCH 0/4] HID: lamparray: fixes from testing on Acer Predator PT14-52T Cristian Mazzotta
2026-09-09 16:52       ` [PATCH 1/4] HID: lamparray: read attribute reports synchronously Cristian Mazzotta
2026-09-09 16:52       ` [PATCH 2/4] HID: lamparray: raise log level of fatal probe errors Cristian Mazzotta
2026-09-09 16:52       ` Cristian Mazzotta [this message]
2026-09-09 16:52       ` [PATCH 4/4] HID: lamparray: blank lamps across suspend and restore on resume Cristian Mazzotta
2026-09-11 10:38       ` [PATCH 0/4] HID: lamparray: fixes from testing on Acer Predator PT14-52T Aaron Erhardt
2026-09-09 22:36     ` [PATCH v5 0/2] HID: generic: add LampArray support via hid-lamparray helper Armin Wolf
2026-09-11 10:01       ` Aaron Erhardt

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=20260909165257.352597-4-cmmazzo@icloud.com \
    --to=cmmazzo@icloud.com \
    --cc=W_Armin@gmx.de \
    --cc=aer@tuxedocomputers.com \
    --cc=bentiss@kernel.org \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=wse@tuxedocomputers.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