From nobody Sat Sep 26 11:47:53 2026 Received: from sender6-op-o12.zoho.com (sender6-op-o12.zoho.com [165.173.180.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6454848D860; Tue, 1 Sep 2026 18:11:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=165.173.180.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286303; cv=pass; b=LXieOKv25fobsyMyib91/PV8nyO9fp66ilDbPDY3NTy66ksawTYXveddMYPflUOJMDGZX7fhL5E31cm6hUC/EFwQtcjJK7qhFeeNqTQGQMTW00edquXPYfuWs9bN+qXOWTGFdjwR4ZLcbEOnukC4Iia2yHSeXqA5OiKYQI/yRc0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286303; c=relaxed/simple; bh=0AXEy5KeefClAqrLkM2xK+utd+ujlnIlxIBgrEFv0Kk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oi0A++Bcvlo2SVeKx+t+xQ4BO9AMyqXovHhmiltm01RHWYoB0H0ISLelJmy12m8sqIW6puI12ssTb7zNbAtcZg4STp0a0igms/hS+zLEE5iv+VOsx5EhD+Gw2DXjO86pORWum+gDlhSmSIPR/5ZoEXXLRvZhTalczUDb5gdZvnE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=ORtW3soV; arc=pass smtp.client-ip=165.173.180.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="ORtW3soV" ARC-Seal: i=1; a=rsa-sha256; t=1788286255; cv=none; d=zohomail.com; s=zohoarc; b=MCk4MCHQPr1w6mcvkXeKDSZt+7FCe74OjXvB1HzdkrL27A/BlQY/iQjNgn2lUfwSofWV7WnGsYRAR/qn33K+umcCWKp6RQTnpbNFUxFyDS0kED1lu9OihMaxxpyqUtb5HElaIKlhON9bN9rWciDNC0f+Tb1STlhq1VsN7b/V5iM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286255; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=4Mtz/AFM80jv4Dc2UsakzXZvBkH2sDnyNjioBHCW4Ms=; b=YMuayuKbkVdG7ouKJBSUIV46MC31W8dOloeBncxOvaTg4wO91w46gkFMsGqnwOIVk0y5mjmyJxjXGiewvkxEGUprklEBNVlQ+MRIkbDGlPLDl0BE9oooURUIBaouRiMF7naVuC5xS+RCHhVMDxcAiCfZdWgWdITQU8tSiYeZKTg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286255; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=4Mtz/AFM80jv4Dc2UsakzXZvBkH2sDnyNjioBHCW4Ms=; b=ORtW3soVbFlTo1XUW14e0bnycwZtA77jIh+Tq/5ySeYGnDevIXbnrj/sN9aq6cgC jV8rPRpq7q88I6ZmV3KqbQvcjbNSdji4Jsab6ckgzO7uOw76NaAxJ3+CKO3+DuKcTk7 BvmtHyLSaLLZhfRAvCteud1OwzIr3wpSHjm12VxcO6Gz6JU9ZtJvbMe/OSOkRXascfD TpC9ddIUzX9do7lFeWxFCEgWUISwRrran2bCUqNhvXRVnZJBmk8CFPe/pMTK1BvMdcE FC0N4h2g9qvl972MDpccn9YJz/5VwXK4WY+TghmeARPk5y4zpVVhumCKbkLt7Reielv LgmFYjc48A== Received: by mx.zohomail.com with SMTPS id 1788286254124334.97358507758634; Tue, 1 Sep 2026 11:10:54 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:20 +0800 Subject: [PATCH v6 01/12] leds: class: Always protect brightness_show() with led_access Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-1-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External The led_classdev_register_ext() protects transient states when registering with the led_access mutex and relies on all sensitive sysfs attribute callbacks holding the mutex to prevent exposing transient states. The Fixes: commit broke this as it accessed trigger_lock without holding led_access. Fix it by protecting brightness_show() with led_access in all code paths. Fixes: b819dc7d8fb2 ("leds: core: Report ENODATA for brightness of hardware= controlled LED") Signed-off-by: Rong Zhang Acked-by: Ike Panhc --- Changes in v6: - New patch in the series to address a concern from Sashiko - https://sashiko.dev/#/patchset/20260821-leds-trigger-hw-changed-v5-0-3e= 00d6c4a936@rong.moe?part=3D7 --- drivers/leds/led-class.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c index a51b0ed53886..4259533dafa1 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -43,13 +43,13 @@ static ssize_t brightness_show(struct device *dev, struct led_classdev *led_cdev =3D dev_get_drvdata(dev); unsigned int brightness; =20 - if (led_trigger_is_hw_controlled(led_cdev)) - return -ENODATA; + scoped_guard(mutex, &led_cdev->led_access) { + if (led_trigger_is_hw_controlled(led_cdev)) + return -ENODATA; =20 - mutex_lock(&led_cdev->led_access); - led_update_brightness(led_cdev); - brightness =3D led_cdev->brightness; - mutex_unlock(&led_cdev->led_access); + led_update_brightness(led_cdev); + brightness =3D led_cdev->brightness; + } =20 return sysfs_emit(buf, "%u\n", brightness); } --=20 2.55.0 From nobody Sat Sep 26 11:47:53 2026 Received: from sender6-op-o12.zoho.com (sender6-op-o12.zoho.com [165.173.180.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14AD749550F; Tue, 1 Sep 2026 18:12:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=165.173.180.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286326; cv=pass; b=OSNw1tMreYcYuHzpwedV4vtQBeoV3wFOtvxiWSaXSjlkd9Ce4ENdpwF5TPfziMk/CSNMCLEWI7pFnRBRM72cdvgYgHgPn85mrerl9JwWSK7OY42Er7eQDYNex3Swdta0bPg16CIt13NUZ/6C/s9mzuqiZ+pW9pmhaaSGv+GD3W8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286326; c=relaxed/simple; bh=7ZAsoBjFbWKnWetAs9AfEUvhkJPFN0slytnXiKl2zDQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=SzaHzN7w8mNrinzmxgvtx0Izwi2sHyu6ItsDr76xiyt+8IBahJCfSHVzaayOCjhX5Y1FCLF9jv4Y3zOLIhc6bqbruDNq0GSQTPIDSxHp0H4fYNUrCNDYLx7RtH4xqz9b1OglAyQ69QizVot7W/xrXD3f+sxbgQJkvWahT9uDKJU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=bkLsu7uv; arc=pass smtp.client-ip=165.173.180.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="bkLsu7uv" ARC-Seal: i=1; a=rsa-sha256; t=1788286261; cv=none; d=zohomail.com; s=zohoarc; b=QzZgcOnwPNjeiUGPwMxpESLAwjSfbFwTUVNoCw5JoRr3PyITW44XUY428q8gHKr+bHeYBh4ImaJIkYGsV61mhXntvCir/h+UPNX3ENmUashhBSlG4dxUeAv417vb4vaViKj8TylpWb2nQVtiP5w3Q5jCsQ3bZzSxbvm0X10hi/c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286261; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=aOkhjiTA152ajScLyQAGXJjOlyLqpZinwlJKaTnX7k4=; b=Gn9Dbjun+peV2Qn9qvpGpSoeKXNGou2kKffi1vqwlO3X7GlppO4I5SD33XNRvwnjs217Zm4AfRaDAaM/nbs5Ux8z7CLXMZpFM6F+coVh/j41K391RA/wTq4K7P3gajSfkZdTkHoP4R9pk2acmPpqHDq/Ekc9h1tIDl1++KnhgdU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286261; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=aOkhjiTA152ajScLyQAGXJjOlyLqpZinwlJKaTnX7k4=; b=bkLsu7uvD4wHNJelz503CiT7wjeQWaHyirNVgNuFNTjA2T3Cll9m5l1affgt2Sng ejvNJSMLMzXjgRr8CRsjAnrnzK4K7kDmjumI+4UN0eRb2gC4GCz0CkAFQeTOLlZY0P0 Byawu0D68AoSYy+tThvH8SZJsk0LB+CZahaMJ4Q0WuowTzIunLDKCzXoFuV5RJFGqOh 1ghV0J2J6f/wP4iOti709JsEys4C9pHLbjZQU9ddMogwmewX6eueAP84lI8RKxGlX+b cSMRa3OBT14gqI6bgrTEcigByhJ8oW9/94FHIGdRf6iuV0yZL9h3rBdfxCQD58CjDOr aTWP/Gyggg== Received: by mx.zohomail.com with SMTPS id 1788286259538207.61326754591994; Tue, 1 Sep 2026 11:10:59 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:21 +0800 Subject: [PATCH v6 02/12] leds: Move led_trigger_is_hw_controlled() to the right place Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-2-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External Currently led_trigger_is_hw_controlled() is placed at led-class.c, which is not an right place as it falls into the triggers namespace and does triggers stuff. Move it into led-triggers.c, and split it into locked and unlocked variant for convenience. Fixes: b819dc7d8fb2 ("leds: core: Report ENODATA for brightness of hardware= controlled LED") Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Changes in v3: - New patch in the series, the dependency of the following patches --- drivers/leds/led-class.c | 10 ---------- drivers/leds/led-triggers.c | 19 +++++++++++++++++++ include/linux/leds.h | 8 ++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c index 4259533dafa1..7110bfbe3b83 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -27,16 +27,6 @@ static LIST_HEAD(leds_lookup_list); =20 static struct workqueue_struct *leds_wq; =20 -static bool led_trigger_is_hw_controlled(struct led_classdev *led_cdev) -{ -#ifdef CONFIG_LEDS_TRIGGERS - guard(rwsem_read)(&led_cdev->trigger_lock); - return led_cdev->trigger && led_cdev->trigger->trigger_type; -#else - return false; -#endif -} - static ssize_t brightness_show(struct device *dev, struct device_attribute *attr, char *buf) { diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c index b1223218bda1..bf2543538ed0 100644 --- a/drivers/leds/led-triggers.c +++ b/drivers/leds/led-triggers.c @@ -7,9 +7,11 @@ * Author: Richard Purdie */ =20 +#include #include #include #include +#include #include #include #include @@ -33,6 +35,23 @@ trigger_relevant(struct led_classdev *led_cdev, struct l= ed_trigger *trig) return !trig->trigger_type || trig->trigger_type =3D=3D led_cdev->trigger= _type; } =20 +static bool __led_trigger_is_hw_controlled(struct led_classdev *led_cdev) +{ + lockdep_assert_held(&led_cdev->trigger_lock); + + if (!led_cdev->trigger) + return false; + + return led_cdev->trigger->trigger_type; +} + +bool led_trigger_is_hw_controlled(struct led_classdev *led_cdev) +{ + guard(rwsem_read)(&led_cdev->trigger_lock); + return __led_trigger_is_hw_controlled(led_cdev); +} +EXPORT_SYMBOL_GPL(led_trigger_is_hw_controlled); + ssize_t led_trigger_write(struct file *filp, struct kobject *kobj, const struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t count) diff --git a/include/linux/leds.h b/include/linux/leds.h index a515f075c29a..46ad2e03e1c6 100644 --- a/include/linux/leds.h +++ b/include/linux/leds.h @@ -534,6 +534,8 @@ void led_trigger_set_default(struct led_classdev *led_c= dev); int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *tri= gger); void led_trigger_remove(struct led_classdev *led_cdev); =20 +bool led_trigger_is_hw_controlled(struct led_classdev *led_cdev); + static inline void led_set_trigger_data(struct led_classdev *led_cdev, void *trigger_data) { @@ -584,6 +586,12 @@ static inline int led_trigger_set(struct led_classdev = *led_cdev, } =20 static inline void led_trigger_remove(struct led_classdev *led_cdev) {} + +static inline bool led_trigger_is_hw_controlled(struct led_classdev *led_c= dev) +{ + return false; +} + static inline void led_set_trigger_data(struct led_classdev *led_cdev) {} static inline void *led_get_trigger_data(struct led_classdev *led_cdev) { --=20 2.55.0 From nobody Sat Sep 26 11:47:53 2026 Received: from sender4-op-o12.zoho.com (sender4-op-o12.zoho.com [136.143.188.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC246495030; Tue, 1 Sep 2026 18:12:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286347; cv=pass; b=gvOYoC5Ugwd+ptXPhn0ZJX2/lBCLFhLRDyaM5FrjBoDjpEvTq+hDurajIbXU8Yl3Ja13AhyAZn6uFDTd5kEhYUVIVtIgGwU1jq15QJxJwlCKHtt4wWPrfZUNIEvzwDOqzAuJKHJOMOGKBo5RDfkfJ0U9km+HklPQKFmWaTuoU4s= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286347; c=relaxed/simple; bh=lnboi+ohp8VRcRfWzjJ5swmCtSfQdixjoOMTCsMww5M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XzkX3nTUeDUkQmFH1Dp6etkwZkBpI00gfOZ5/HHgworXxOpxL5lMk/5Sfqk44kBmXI4QXvntUpoBHvAJ/Y095A3c8/pFWYLoH4igCcFZRMu6HC7UghvnnjE5ZgkvkZGpG7TBdcFl/JT7GpakRm+mFz5U2+7CO6bNskDkCUMTIwY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=UWmpy31j; arc=pass smtp.client-ip=136.143.188.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="UWmpy31j" ARC-Seal: i=1; a=rsa-sha256; t=1788286267; cv=none; d=zohomail.com; s=zohoarc; b=nqIdoSJh5lGB0Rant59Kmh1WaBNXvgOz82qmzHEAG04g0taXNEGhcwDr2plBiSlNKLvAOskOMBVWswROyXnkqOGF3JJo9Lw62RNtsdjA7uog4RXHx696pcQl7WRBF0xoi+viypgtgbDS41O/U9TK5TLUuE06ha3GvSDeiZ52Eao= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286267; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=LmmU2kTCSF/V/CfHUReHwmdyiQm15OpRydNnG7BSJaU=; b=CZgx+onZyRrAG3lbRdaRxElDAhjjuvlXQQFAj9oU9yt7C7WYIfd7HfgGEGsnR9EI+ONzbNCjZJq/Vl4iiw2bc2UTq6ciryANzllYiH6LKi56IZ1DaZK7e2SZMDHpleF6XzXva12Z8UCHw+SiKcdeGepG9fbrNZQxDloWJEzoGwc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286267; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=LmmU2kTCSF/V/CfHUReHwmdyiQm15OpRydNnG7BSJaU=; b=UWmpy31jrLmSCLJGe3NJNgWGL7iIFtAI2wErh7n5LV9jBmf/HNk76qGjMTnsUxD5 jC2QSKT/CRn+pMmRdYFEGyux3sn7zyvYcX/y4QTwGD9Gc0PT2sLQnVuSXLaRXGnYsvt mjsbOOddAl3/734nnU1Cn1ZNNVyktsoZDfKV1BnjvFXwc1Y2VcJ1Dn8mjBpjxN6cbI1 vAM8REOV/5DkPDyz31jfrAIth2SfjK5+luvrkE7Olgq/gnLnG3www4RPa5rJrFSz5jb th934j9Q/iGt9TeDmKauLFNuKouByyrkiOdvcIa75b8OInK69wltYiNHf27I1V1hT4c VMiPT1rY5g== Received: by mx.zohomail.com with SMTPS id 1788286265118960.0913506634437; Tue, 1 Sep 2026 11:11:05 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:22 +0800 Subject: [PATCH v6 03/12] leds: class: Remove hardware control trigger when writing brightness Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-3-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External Since commit b819dc7d8fb2 ("leds: core: Report ENODATA for brightness of hardware controlled LED"), the brightness attribute becomes write-only when the LED is controlled fully by the hardware. A write-only attribute is very confusing. Moreover, most LED drivers set hardware brightness innocently with the side effect of disabling hardware control, but the hardware control trigger remains active, resulting in the software and hardware being out of sync. Fix it by removing the hardware control trigger when writing the brightness attribute. This should also match the semantics of hardware control: When the LED is in hw control, no software blink is possible and doing so will effectively disable hw control. Fixes: b819dc7d8fb2 ("leds: core: Report ENODATA for brightness of hardware= controlled LED") Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Changes in v3: - New patch in the series, integrated from https://lore.kernel.org/all/2026= 0712-leds-hw-control-brightness-set-v1-1-1de593b09d26@rong.moe/ - The following patches will improve __led_trigger_is_hw_controlled() to include offloaded generic triggers and take the advantage of it --- drivers/leds/led-class.c | 3 +++ drivers/leds/led-triggers.c | 9 +++++++++ include/linux/leds.h | 2 ++ 3 files changed, 14 insertions(+) diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c index 7110bfbe3b83..39cc2f3ea63f 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -64,6 +64,9 @@ static ssize_t brightness_store(struct device *dev, =20 if (state =3D=3D LED_OFF) led_trigger_remove(led_cdev); + else + led_trigger_remove_hw_control(led_cdev); + led_set_brightness(led_cdev, state); =20 ret =3D size; diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c index bf2543538ed0..804a04b326c4 100644 --- a/drivers/leds/led-triggers.c +++ b/drivers/leds/led-triggers.c @@ -287,6 +287,15 @@ void led_trigger_remove(struct led_classdev *led_cdev) } EXPORT_SYMBOL_GPL(led_trigger_remove); =20 +void led_trigger_remove_hw_control(struct led_classdev *led_cdev) +{ + guard(rwsem_write)(&led_cdev->trigger_lock); + + if (__led_trigger_is_hw_controlled(led_cdev)) + led_trigger_set(led_cdev, NULL); +} +EXPORT_SYMBOL_GPL(led_trigger_remove_hw_control); + static bool led_match_default_trigger(struct led_classdev *led_cdev, struct led_trigger *trig) { diff --git a/include/linux/leds.h b/include/linux/leds.h index 46ad2e03e1c6..d778709f5b1b 100644 --- a/include/linux/leds.h +++ b/include/linux/leds.h @@ -533,6 +533,7 @@ void led_trigger_blink_oneshot(struct led_trigger *trig= ger, void led_trigger_set_default(struct led_classdev *led_cdev); int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *tri= gger); void led_trigger_remove(struct led_classdev *led_cdev); +void led_trigger_remove_hw_control(struct led_classdev *led_cdev); =20 bool led_trigger_is_hw_controlled(struct led_classdev *led_cdev); =20 @@ -586,6 +587,7 @@ static inline int led_trigger_set(struct led_classdev *= led_cdev, } =20 static inline void led_trigger_remove(struct led_classdev *led_cdev) {} +static inline void led_trigger_remove_hw_control(struct led_classdev *led_= cdev) {} =20 static inline bool led_trigger_is_hw_controlled(struct led_classdev *led_c= dev) { --=20 2.55.0 From nobody Sat Sep 26 11:47:53 2026 Received: from sender4-op-o12.zoho.com (sender4-op-o12.zoho.com [136.143.188.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DEAC34968FD; Tue, 1 Sep 2026 18:12:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286369; cv=pass; b=RWF8wfXq6XkiqNlDapDl/9TIa4xXrMxKqcUAZ+VfEZysX65bGx5wOrQV2mKCZzHwVlcd9AJCnltvOfRGU3vFrvmcq8MO+6jcGBhjXkDvUvnupHRmg2O0BAX4p+bG1c24PFZvTCTrx4NF3xz0Qd8IwJaa6aNuFLoZHlUvZN1xKRo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286369; c=relaxed/simple; bh=518Nz52W6qUC1UOIq49YiftoYGeK5crIgvjjpqQNmm8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=r66Wmkb08NabowWwpw6Xv0yOBKpqJBYD9Np42vMrWUndnmP9LldRkM2j535zLuYJMTEKd4EkHSW9uiebaQQr6n/NzSoin+iIJDpwCgZ/JmSaoBlSRnGk1Yt4Wf8TMTf/VG4/IkbUC6EOLExca7OLx4WUynDdLeqjzpRQ0bnSAfM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=hVjEEeNw; arc=pass smtp.client-ip=136.143.188.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="hVjEEeNw" ARC-Seal: i=1; a=rsa-sha256; t=1788286271; cv=none; d=zohomail.com; s=zohoarc; b=DVzVzIOaroxv40jKLk3eCmCy6yu+JTIp8sTzuxxqIxYjk6Qq8/GJjl66gpxOBI3vy0wMKQAfWZLnOMAQhpq+slD+xZo4OOx0Un6JUcKf3/hJIYLFFk7QJFZWGPXVr5fka9BrHXMs1bsTsOZOe4VhIhLPhCMioiVSWSR+zQbrPD8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286271; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=2CdGNCz5RYPwyuZCp34XO8pz1Z+V5LDj2T5Nt4IzWgU=; b=cBS4BZt6e9PaMrIqwTPs0sIM7o6SUGrISDvA3Us7fjxO3O4XSK4deHvS5t3bQLCp+44EwobUXN7wSIMEzwp9U5azzoBa+FYuiNwSyrFbRNrqWpdy6UkrXakoy7eIpQCV11KiKOFPQZZet7NB6lmxqrJXNk2pXWrzhnK3Ze1SAJs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286271; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=2CdGNCz5RYPwyuZCp34XO8pz1Z+V5LDj2T5Nt4IzWgU=; b=hVjEEeNwS5oucwWKrbnteOQu5oBJN+SN8kv2k3oD1TAaHOUBSLw3rwxfI0PJw0CW HcjWyO26/7e0/VcDT4UyiRJNC6bLkoNUolRVWr75rsBQYUuAb8eMjk45c34+MsBW7zY EmYHTH/IIPm4YXUsyCy8vw+Moe9i2uyNEruFbdjcR9ynY0b+2xqlrnAVgZ04T3hB0ne IoTxD3VN4wDTm+yJEh8BneGezTY6EKghK29krBdwR/Np5rod53hg2llI6PQWOXE4sxu Q1LPvqiIdPQN3I0hpBWeJdaIoXH/lTYhsouW3xq1yZ/kD37sPU3UihKm8bvgmUwQjOn h06J6An4Nw== Received: by mx.zohomail.com with SMTPS id 1788286270575745.4873470355612; Tue, 1 Sep 2026 11:11:10 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:23 +0800 Subject: [PATCH v6 04/12] leds: trigger: Add offloaded() callback and provide trigger_may_offload attribute Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-4-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External There are multiple triggers implementing hardware control. However, the LED trigger core doesn't really know the hardware control (offloaded) state since the coordination is done directly between the trigger and the LED driver. It can only assume private triggers as offloaded and generic ones as not offloaded. Add an offloaded() callback so that triggers can report their offloaded states to the LED trigger core. When unimplemented, it defaults to true for private triggers and false for generic ones to keep the current behavior unchanged. With that, provide a new attribute "trigger_may_offload", so that userspace can determine: - if the LED device supports hardware control (supported =3D> visible) - which trigger is the hardware control trigger selected by the LED device - if the trigger is selected ("") - if the trigger is offloaded ("[foo_trigger]") Note: the documentation describes the attribute as "returning a list" despite the LED core currently only supports one hardware control trigger per LED device. This is intentional to make the attribute extensible in the future without breaking userspace. Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Changes in v6: - Update Date: and KernelVersion: for the document of /sys/class/leds//trigger_may_offload Changes in v3: - Rearrange the series so that the code using the offloaded() callback is introduced before the driver implementation (thanks Thomas Wei=C3=9Fschuh) - Reword documentation (ditto) - Adopt guard() and lockdep (ditto) - Adopt __led_trigger_is_hw_controlled() from newly-integrated PATCH 1 --- Documentation/ABI/testing/sysfs-class-led | 22 ++++++++++++++++++++++ Documentation/leds/leds-class.rst | 20 ++++++++++++++++++++ drivers/leds/led-class.c | 22 ++++++++++++++++++++++ drivers/leds/led-triggers.c | 30 +++++++++++++++++++++++++++= +++ drivers/leds/leds.h | 2 ++ include/linux/leds.h | 1 + 6 files changed, 97 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-class-led b/Documentation/ABI/= testing/sysfs-class-led index d4c918cc11a1..e3605fc55fa8 100644 --- a/Documentation/ABI/testing/sysfs-class-led +++ b/Documentation/ABI/testing/sysfs-class-led @@ -78,6 +78,28 @@ Description: (which would often be configured in the device tree for the hardware). =20 +What: /sys/class/leds//trigger_may_offload +Date: September 2026 +KernelVersion: 7.4 +Contact: linux-leds@vger.kernel.org +Description: + Names and states of triggers that may be offloaded to hardware. + Such triggers are also called "hardware control trigger" in some + context. + + Only exists when the LED supports trigger offload. + + Reading this file returns a list of triggers that are capable to + be offloaded. The optional brackets around the trigger name + indicate the state of the current trigger: + + - `foo_trigger`: the trigger is not selected. + - ``: the trigger is selected, but falls back to + software blink for some reason (e.g., incompatible trigger + parameters) + - `[foo_trigger]`: the trigger is selected and offloaded to + hardware. + What: /sys/class/leds//inverted Date: January 2011 KernelVersion: 2.6.38 diff --git a/Documentation/leds/leds-class.rst b/Documentation/leds/leds-cl= ass.rst index 3913966cfdac..2d41a6db602c 100644 --- a/Documentation/leds/leds-class.rst +++ b/Documentation/leds/leds-class.rst @@ -242,6 +242,9 @@ ops and needs to declare specific support for the suppo= rted triggers. =20 With hw control we refer to the LED driven by hardware. =20 +A sysfs attribute `trigger_may_offload` is provided for userspace to +query supported triggers and their states. + LED driver must define the following value to support hw control: =20 - hw_control_trigger: @@ -298,6 +301,15 @@ LED driver must implement the following API to support= hw control: Returns a pointer to a struct device or NULL if nothing is currently attached. =20 +LED trigger should implement the following API to indicate hw control: + - offloaded: + return a boolean indicating if the trigger is currently + offloaded to hardware. + + If a trigger doesn't implement this callback, the default + value will be true for private triggers and false for gene= ric + ones. + LED driver can activate additional modes by default to workaround the impossibility of supporting each different mode on the supported trigger. Examples are hardcoding the blink speed to a set interval, enable special @@ -311,6 +323,14 @@ the end use hw_control_set to activate hw control. A trigger can use hw_control_get to check if a LED is already in hw control and init their flags. =20 +Alternatively, a private trigger can be implemented along with the LED dri= ver if +the LED's hardware control doesn't fit any generic trigger. To associate t= he +private trigger with the LED classdev, their `trigger_type` must be the sa= me. To +declare that the private trigger provides hardware control for the associa= ted +LED classdev, set the `hw_control_trigger` string to the trigger's name. S= ince +both the LED classdev and the private trigger are in the same LED driver, = it's +not necessary for them to coordinate via `hw_control_*` callbacks. + When the LED is in hw control, no software blink is possible and doing so will effectively disable hw control. =20 diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c index 39cc2f3ea63f..7e571bd1de5b 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -96,8 +96,30 @@ static const struct bin_attribute *const led_trigger_bin= _attrs[] =3D { &bin_attr_trigger, NULL, }; + +static DEVICE_ATTR_RO(trigger_may_offload); +static struct attribute *led_trigger_attrs[] =3D { + &dev_attr_trigger_may_offload.attr, + NULL +}; + +static umode_t led_trigger_is_visible(struct kobject *kobj, + struct attribute *attr, + int idx) +{ + struct device *dev =3D kobj_to_dev(kobj); + struct led_classdev *led_cdev =3D dev_get_drvdata(dev); + + if (attr =3D=3D &dev_attr_trigger_may_offload.attr) + return led_cdev->hw_control_trigger ? attr->mode : 0; + + return attr->mode; +} + static const struct attribute_group led_trigger_group =3D { .bin_attrs =3D led_trigger_bin_attrs, + .attrs =3D led_trigger_attrs, + .is_visible =3D led_trigger_is_visible, }; #endif =20 diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c index 804a04b326c4..de17a8bbb4d4 100644 --- a/drivers/leds/led-triggers.c +++ b/drivers/leds/led-triggers.c @@ -42,6 +42,10 @@ static bool __led_trigger_is_hw_controlled(struct led_cl= assdev *led_cdev) if (!led_cdev->trigger) return false; =20 + if (led_cdev->trigger->offloaded) + return led_cdev->trigger->offloaded(led_cdev); + + /* Otherwise assume private triggers as always offloaded. */ return led_cdev->trigger->trigger_type; } =20 @@ -341,6 +345,32 @@ void led_trigger_set_default(struct led_classdev *led_= cdev) } EXPORT_SYMBOL_GPL(led_trigger_set_default); =20 +ssize_t trigger_may_offload_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct led_classdev *led_cdev =3D dev_get_drvdata(dev); + struct led_trigger *trig; + bool hit, offloaded; + int len; + + guard(mutex)(&led_cdev->led_access); + guard(rwsem_read)(&led_cdev->trigger_lock); + + trig =3D led_cdev->trigger; + + offloaded =3D __led_trigger_is_hw_controlled(led_cdev); + hit =3D offloaded || (trig && !strcmp(led_cdev->hw_control_trigger, trig-= >name)); + + /* [offloaded] inactive */ + len =3D sysfs_emit(buf, "%s%s%s\n", + offloaded ? "[" : (hit ? "<" : ""), + led_cdev->hw_control_trigger, + offloaded ? "]" : (hit ? ">" : "")); + + return len; +} +EXPORT_SYMBOL_GPL(trigger_may_offload_show); + /* LED Trigger Interface */ =20 int led_trigger_register(struct led_trigger *trig) diff --git a/drivers/leds/leds.h b/drivers/leds/leds.h index bee46651e068..b08a289397e4 100644 --- a/drivers/leds/leds.h +++ b/drivers/leds/leds.h @@ -27,6 +27,8 @@ ssize_t led_trigger_read(struct file *filp, struct kobjec= t *kobj, ssize_t led_trigger_write(struct file *filp, struct kobject *kobj, const struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t count); +ssize_t trigger_may_offload_show(struct device *dev, + struct device_attribute *attr, char *buf); =20 extern struct rw_semaphore leds_list_lock; extern struct list_head leds_list; diff --git a/include/linux/leds.h b/include/linux/leds.h index d778709f5b1b..bee2b4309a09 100644 --- a/include/linux/leds.h +++ b/include/linux/leds.h @@ -485,6 +485,7 @@ struct led_trigger { const char *name; int (*activate)(struct led_classdev *led_cdev); void (*deactivate)(struct led_classdev *led_cdev); + bool (*offloaded)(struct led_classdev *led_cdev); =20 /* Brightness set by led_trigger_event */ enum led_brightness brightness; --=20 2.55.0 From nobody Sat Sep 26 11:47:53 2026 Received: from sender5-op-o12.zoho.com (sender5-op-o12.zoho.com [165.173.182.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 83F6036729C; Tue, 1 Sep 2026 18:13:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=165.173.182.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286391; cv=pass; b=tisLq/ceV+YvOGBZb6/YUA/D6/TJcrPB9GByRYfv2Bzk406jKbAelx6HlmccrnBNOmZxMndzSa7lr3xNeV6bxzo7AMxKfpLnX8lJTcRHKUpkcEGPXAvizJZ7xIJcZD8PKV3Z0l3JZ18ukCdItvPpX4e1pYLqHLGXhaC+iEDmd4k= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286391; c=relaxed/simple; bh=akD2/+LKKQMVagYGejYsZ7sZ39xXmqgdGhdcQCZIxR0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=a1oOKGFQ7sxtZPm4lrNQpoVcvididuJhzJSl1FXre5w99HIsTrDxx1i+I7dmYZYUTPuzKXSVplpO+G/eXD18BJ3cbHzsg5QmuBbHTIp80WXZeVgK9p42nl7xbAY169lFHA0Lpa5h9RMyHWBTOysiRiZeCj83+C3uB/vvbMYb0B4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=ID1Jyz6A; arc=pass smtp.client-ip=165.173.182.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="ID1Jyz6A" ARC-Seal: i=1; a=rsa-sha256; t=1788286277; cv=none; d=zohomail.com; s=zohoarc; b=c0n18rirckW4JvY7WJIYnJn6P3HDpeUP9uLniJZhXE+X/UWewekpnYMyGDPXfIMD3NDEDPovuPbvSRSTeCROS8Lq5oq/LuICyiATFDEDOLuovMLScf+ZOHU489bMTRGNhBtQ35uqCFfG0/H6Ko1pMQn4BaAwBnuCKUj279YYQn8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286277; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=xwd/UBinkWgkOmV0UP/p5sYAbRYiCZgmzMgXpY3kiyA=; b=IFPgTJr47RdR1+BB83m+aJ1MN6EC/HRKqftgHKgKcMZumsuIS2g/FcTqR1gFrC3EZcuEBZtrK727bwuO8vh5lgPPd9wii4Av02ac8t4TaZfpv6pfSgxb9t5tx9s2a0Er1xd007LLsyFUfPX/VcrLOBvtiKHwFxJWe4u8Y3ytanA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286277; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=xwd/UBinkWgkOmV0UP/p5sYAbRYiCZgmzMgXpY3kiyA=; b=ID1Jyz6APJtt66N3CV5axZwe1x3yz/5Dem0BhLOI5Ny0PxS/fEJZZLyVUZXR0Ip8 p85As917veym+45qpKfUgmwlRwt2H/d9T/E39Zyv8ZObNg67IkpyV6DgKPeM8JJBKUF LuAOywiEJTnbf6t/0EnI14C83Nwh9Q77uyQOu+47ud7TeUh8AyrjXYSrgUjbl+GbzVD LoPCZMsV2GO1sjLmojpTF8ckin9RBjZRMmUtKcGSh+XEF5eQikcMzGC7yz0vvnemwnr OsxZRFPWctehof4FkZMaeOHxWwepAXnImPb0Q50sn57qivCsYDvYIvmDAfW6MvkNP3A RVSFDHdNTg== Received: by mx.zohomail.com with SMTPS id 1788286276014556.0662390402474; Tue, 1 Sep 2026 11:11:16 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:24 +0800 Subject: [PATCH v6 05/12] leds: cros_ec: Implement offloaded() trigger callback Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-5-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External "chromeos-auto" is a private hardware control trigger which always stays in hardware control. Implement offloaded() callback with its return value to be always true to reflect this. Reviewed-by: Thomas Wei=C3=9Fschuh Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Sashiko has reported this pre-existing issue in previous revisions: - [High] Registering a global static `led_trigger` structure in a driver's probe function causes list corruption and lock re- initialization if the driver is probed multiple times. The author is aware of it. Since the driver is very platform-specific and the relevant platforms never have more than one such device, the bug is never triggered on real devices. If a user manually binds the driver to other devices, it's their fault. Therefore, fixing it is far beyond the scope of the series. --- drivers/leds/leds-cros_ec.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/leds/leds-cros_ec.c b/drivers/leds/leds-cros_ec.c index 1844d0cd5f52..6db83d015277 100644 --- a/drivers/leds/leds-cros_ec.c +++ b/drivers/leds/leds-cros_ec.c @@ -85,12 +85,18 @@ static int cros_ec_led_trigger_activate(struct led_clas= sdev *led_cdev) return cros_ec_led_send_cmd(priv->cros_ec, &arg); } =20 +static bool cros_ec_led_trigger_offloaded(struct led_classdev *led_cdev) +{ + return true; +} + static struct led_hw_trigger_type cros_ec_led_trigger_type; =20 static struct led_trigger cros_ec_led_trigger =3D { .name =3D "chromeos-auto", .trigger_type =3D &cros_ec_led_trigger_type, .activate =3D cros_ec_led_trigger_activate, + .offloaded =3D cros_ec_led_trigger_offloaded, }; =20 static int cros_ec_led_brightness_set_blocking(struct led_classdev *led_cd= ev, --=20 2.55.0 From nobody Sat Sep 26 11:47:53 2026 Received: from sender4-op-o12.zoho.com (sender4-op-o12.zoho.com [136.143.188.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58AFF495026; Tue, 1 Sep 2026 18:13:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286416; cv=pass; b=TBLrxojuRpXM7emrgdyOTCsTXk2IZazTONBc/n86fbRzozPLA9dQo6g4rZXevtlpHQiNDIys1uaHVgyjM2CXYvJnBCbD1YEy8m9Kfu8VWgKSHlG29wmeYqpYLleg4Q7UW8J7L8o2bdAmPoul6xXgX/pW6W98dO1jvA44uz8Ka5g= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286416; c=relaxed/simple; bh=TejI76x3eGihmgm22rJqE1YBXJ0JwbqwBL0zLEQgm6E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=b6GIPSqv4vNRtPWN2nVEtuwA2+X67Jwry/OrgExEi6xFCj1XipWRj6pICTvHjNIOtf8yuDT5MbF0thqG2BADqEMlmWt3EgSNVQ7krWDz9SDo6rb7Ve8unH2aZr5zcnapg+XGiLZSGxwp/q39R2fD2J33eIaleaCyBMP1OgJ/Qqg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=Euc1559+; arc=pass smtp.client-ip=136.143.188.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="Euc1559+" ARC-Seal: i=1; a=rsa-sha256; t=1788286283; cv=none; d=zohomail.com; s=zohoarc; b=KMqpHPw2flfXpXt+G7bFynvIWo34Zkmo95smhq6AbQbJEAtQ02QH6RXQJwqXkUEfjN6HKI3q9vhmQzc243kXkfi6utl8MdFzg72bjIFPDujvo69FYPLCUcOZ8MNGeixCtgTv76kIP5vyHTA+n4UVTwZ4dWFIhkR/62j92hxVq6g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286283; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=1NxgZjL+W2/6USuGo83LwA7yJPTHsc/hFLx0S13UrGg=; b=YBQbXjyAwQ4oHHAJs5XZ0sa6XitiMe005tz3+ddjLei+h9DJg2pzReHokKjI8y795crrvnTu43JTg6BB0tn92iedTL4BInk9+CxzIR4fvsfL4TLl23o82IuaSPtDCaqYvWfcgDgtAJNmuTTjPPpCf9t4teypU2g1pMd5YG4gNaA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286283; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=1NxgZjL+W2/6USuGo83LwA7yJPTHsc/hFLx0S13UrGg=; b=Euc1559+r4TVFyfyvNWDzPYjpB2HjrfeQldPgQDrexvL+ctSkMg6WiDdSXOMPR0l 8O14XPIKgIwBakhG5z74Y1iAosplpvoxPjW5UinctGLMiCi2nmOiw9SsbpifZNQF4Ul MCHdB5AmoSGbRdjtLECHujB2w8CKCOXvW5497M/l0x2wsN+aEMRNIwFi/75W2fpWOak 5ntJ4duE/MUZPWZt+a1DukqwYcfmSnkF2m2VcAdUtZhuT1yonY5ZsxwMdm1gUSsjBWP ol/Qr23wUG6lgOa5ECz40AKK7zNGid+Cq2Q1kvShlHRbKohJ+W/oEL9e2lz2ZVSiEdC zHO9btbHEQ== Received: by mx.zohomail.com with SMTPS id 1788286281468437.9544778526488; Tue, 1 Sep 2026 11:11:21 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:25 +0800 Subject: [PATCH v6 06/12] leds: turris-omnia: Implement offloaded() trigger callback and declare hw_control_trigger Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-6-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External "omnia-mcu" is a private hardware control trigger which always stays in hardware control mode. Implement offloaded() callback with its return value to be always true to reflect this. Meanwhile, declare it as a hardware control trigger as it's forgotten before. Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Sashiko has reported this pre-existing issue in previous revisions: - [High] The statically allocated `omnia_hw_trigger` object is registered multiple times via `devm_led_trigger_register()` on probe if multiple driver instances are bound, causing lock and list corruption. The author is aware of it. Since the driver is very platform-specific and the relevant platforms never have more than one such device, the bug is never triggered on real devices. If a user manually binds the driver to other devices, it's their fault. Therefore, fixing it is far beyond the scope of the series. --- drivers/leds/leds-turris-omnia.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/leds/leds-turris-omnia.c b/drivers/leds/leds-turris-om= nia.c index ed6a47bbb44f..32d40d176d3f 100644 --- a/drivers/leds/leds-turris-omnia.c +++ b/drivers/leds/leds-turris-omnia.c @@ -195,10 +195,16 @@ static void omnia_hwtrig_deactivate(struct led_classd= ev *cdev) err); } =20 +static bool omnia_hwtrig_offloaded(struct led_classdev *cdev) +{ + return true; +} + static struct led_trigger omnia_hw_trigger =3D { .name =3D "omnia-mcu", .activate =3D omnia_hwtrig_activate, .deactivate =3D omnia_hwtrig_deactivate, + .offloaded =3D omnia_hwtrig_offloaded, .trigger_type =3D &omnia_hw_trigger_type, }; =20 @@ -251,6 +257,7 @@ static int omnia_led_register(struct i2c_client *client= , struct omnia_led *led, * by LED class from the linux,default-trigger property. */ cdev->default_trigger =3D omnia_hw_trigger.name; + cdev->hw_control_trigger =3D omnia_hw_trigger.name; =20 /* Put the LED into software mode */ ret =3D omnia_cmd_write_u8(client, OMNIA_CMD_LED_MODE, OMNIA_CMD_LED_MODE= _LED(led->reg) | --=20 2.55.0 From nobody Sat Sep 26 11:47:53 2026 Received: from sender6-op-o12.zoho.com (sender6-op-o12.zoho.com [165.173.180.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E4DE495033; Tue, 1 Sep 2026 18:13:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=165.173.180.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286438; cv=pass; b=qiDIdR9Y71ODcYT2ydpbnFgPv+VLdTNBpnjIRemrxDxd+ZXjzOmF46aII0z9u1I47i2yLEQbdnabEujgO4atb80hu94w4s/BAHcEYRQiwZmV0OIAQIPLhORLu4Ib6g9aFiUKYg6q8hAGV13XMFb4xeXCR1iSMuk9mkSsoG4jwVU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286438; c=relaxed/simple; bh=qYu8qu5/t/EIagET1LTn1tOXHGBdWGjy+kHn0HcWLd4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=s8hNQjxQwRAxyvSOr7/qD5Eb/9vrpoUIQZ+Q7ERrIGjqSYLOtXois39Izdndjj6nCS2A+cf5KKh3clLQA8fx6UuKPKIu1zoskR/GUeVnjcYmqtwii+RVguncjR0o+SeZ+6AjlhPS/dWKaerSSO3T8pdu5XjHHX5vKEFMXeZTClU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=ZGi2r8fw; arc=pass smtp.client-ip=165.173.180.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="ZGi2r8fw" ARC-Seal: i=1; a=rsa-sha256; t=1788286287; cv=none; d=zohomail.com; s=zohoarc; b=bDCj9eazrsTC/FVHeK6Tz0DKRx5zFcxGm31epPcnOb1s9xHTdNA1x+vsWBTaHJcLZJGf58V/rzx3tPsMOIZKi+WXC6j4fpec9cNyPryxWqKBD16mRWPvbacC3G19jargm7Io94OQiFAt+XhsypOb0G6bvbapplL8n4fMZiLPoZ0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286287; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=2nDIV65eMkICOawrxEA250AL4gFumckCh1QxsSQVGP4=; b=H2JLoCsB5B+0mVddvBRgSXERWrhHiVk/Xm2i7BMi/x4TCkIommTjrBur21PjWV1pDA9eO43jHcobE+UmGu1og7K4cTOxVQgbJIE2wFq7AquM9DX//ApKfKoVxMXREBNO6X+gIRsS4x9FMGfdmokF1IjP20s8E2g7/2xtFVXUylc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286287; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=2nDIV65eMkICOawrxEA250AL4gFumckCh1QxsSQVGP4=; b=ZGi2r8fw4FixBkcnzWZkNGpPmeVomG0Jo9ZMiqM/W5YQsNq3hoh3oPNNNUxujliP OVWFEsoqbsXlIUJ2VDuDdlyU/cJUI1nj3XdeH5ghUfy/AXWhkFq5Xul1wXR8yFgjomh lQUHgurpaQltNJJHJDRpYNwNAD+Wy5PrO8A14lFzAmTZk32tS8QWqn3B5hrlQK8oZ21 6tlxaBvB2iQHvV6E33LegasCB6foJdQNk+FYj83OXw6gVooAswxRkHJEyIIgvrggR1H iqVokV6liKOIU6oyOd3oHrjidh4lOqPK9MyyH/n9Xrztb6o3rTzFYXo00u4Pt/B7BEd TsDlVb9ijg== Received: by mx.zohomail.com with SMTPS id 1788286286947545.2271835817772; Tue, 1 Sep 2026 11:11:26 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:26 +0800 Subject: [PATCH v6 07/12] leds: trigger: netdev: Implement offloaded() callback Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-7-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External "netdev" can run in hardware control according to hardware capabilities and trigger options. Implement offloaded() callback to provide its hardware control state to the LED core, and document the relation between the custom "offloaded" attribute and the generic "trigger_may_offload" attribute. The callback mimics how the existing "offloaded" attribute does, i.e., locklessly reads hw_control, as it's just a hint and don't need to be accurate. Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Changes in v3: - Do not deprecate netdev's "offloaded" attribute (thanks Thomas Wei=C3=9Fschuh) - Document the relation between the custom "offloaded" attribute and the generic "trigger_may_offload" attribute (ditto) --- Sashiko has reported this pre-existing issue in previous revisions: - [High] Resource leak of `net_device` in `netdev_trig_activate()` error path The author is aware of it. Since the series focuses on hardware control stuff, fixing it is far beyond the scope of the series. --- Documentation/ABI/testing/sysfs-class-led | 3 +++ Documentation/ABI/testing/sysfs-class-led-trigger-netdev | 3 +++ drivers/leds/trigger/ledtrig-netdev.c | 8 ++++++++ 3 files changed, 14 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-class-led b/Documentation/ABI/= testing/sysfs-class-led index e3605fc55fa8..94c331d9c410 100644 --- a/Documentation/ABI/testing/sysfs-class-led +++ b/Documentation/ABI/testing/sysfs-class-led @@ -100,6 +100,9 @@ Description: - `[foo_trigger]`: the trigger is selected and offloaded to hardware. =20 + The "netdev" trigger also provides a custom attribute to + indicate its state, see `/sys/class/leds//offloaded`. + What: /sys/class/leds//inverted Date: January 2011 KernelVersion: 2.6.38 diff --git a/Documentation/ABI/testing/sysfs-class-led-trigger-netdev b/Doc= umentation/ABI/testing/sysfs-class-led-trigger-netdev index ed46b37ab8a2..a5146ea1e3e6 100644 --- a/Documentation/ABI/testing/sysfs-class-led-trigger-netdev +++ b/Documentation/ABI/testing/sysfs-class-led-trigger-netdev @@ -75,6 +75,9 @@ Description: If 1, the LED blinking in requested mode is offloaded to hardware. =20 + LED trigger core also provides a generic attribute for this + purpose, see `/sys/class/leds//trigger_may_offload`. + What: /sys/class/leds//link_10 Date: Jun 2023 KernelVersion: 6.5 diff --git a/drivers/leds/trigger/ledtrig-netdev.c b/drivers/leds/trigger/l= edtrig-netdev.c index 5b4e92c14dbb..2e9af48a74db 100644 --- a/drivers/leds/trigger/ledtrig-netdev.c +++ b/drivers/leds/trigger/ledtrig-netdev.c @@ -798,10 +798,18 @@ static void netdev_trig_deactivate(struct led_classde= v *led_cdev) kfree(trigger_data); } =20 +static bool netdev_trig_offloaded(struct led_classdev *led_cdev) +{ + struct led_netdev_data *trigger_data =3D led_get_trigger_data(led_cdev); + + return trigger_data->hw_control; +} + static struct led_trigger netdev_led_trigger =3D { .name =3D "netdev", .activate =3D netdev_trig_activate, .deactivate =3D netdev_trig_deactivate, + .offloaded =3D netdev_trig_offloaded, .groups =3D netdev_trig_groups, }; =20 --=20 2.55.0 From nobody Sat Sep 26 11:47:53 2026 Received: from sender4-op-o12.zoho.com (sender4-op-o12.zoho.com [136.143.188.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E4432D7398; Tue, 1 Sep 2026 18:14:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286459; cv=pass; b=ThA43cCiG8RQaaJvykB2hnCPXNdPFn7aCC6zvi0szSj94zehc0AX9l3nDB84fZp5tnVcKzgGbaBTPXVJtIpK1VzEhdG0X83nEzfIp0NMH2/7wXCEQNcgASJUyaCERxZ5nBSqY/gG9QGXycpdv9OtAEWJQPlxaCVjH4VCW6eQiO4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286459; c=relaxed/simple; bh=rP1FJCLNCgQ4/odsC5rOyOEqiN+aO/Z0ZBAZXBsiOTg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UJ/yM7ZeE5Ju2MPpZ0OugTI+Zb7DthlJW+3Gcf8cS2YjbgnCC5bDxpV3pc7aVt+MPzrk9cntqZ4jxreZbAtKkTn5oM28dS49w1fHexnEGu8REJNOJC5FjgeSlGGRjFMfPQAxY09yzQVbaChhyudjeRy08TyfX796YsuTf9SsQiE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=cPJm8HgB; arc=pass smtp.client-ip=136.143.188.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="cPJm8HgB" ARC-Seal: i=1; a=rsa-sha256; t=1788286294; cv=none; d=zohomail.com; s=zohoarc; b=nxO5KCjfICE4HdM0HppNRE1SBdlouk0pEp5VDceoMEDtf/2RzPQPKpq/qLBhMsy/akx4QBzrgJPzol4bm5Ql0U10t8qXg0XKnpS2XGlbNCH4v6vseA1/klM1Vaw4VQ/4Xn5DhPVlhyYybSUwqZ1kSkvi+KmtUmXdot6v/k4LNu0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286294; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=2j5a5rGmXReIsQy66ZTp+dwI3KKvaZ3uYw/Bf/oap+A=; b=n5yaoCjsowne5Fdt1a2PcLVTuBN6LJ1aT6T7R+F+MdUL/jcK8cSIL5IfCo8ze50IPNkcgzhvaEVtJZuFnG7zN66prfMYDD0ff5ewXnshdhu3ZdioPUbiEfbhfAZdcVze2HfRfxn3N51szjHZEzpNBRLioxZytl2HLzd1L0BM5is= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286293; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=2j5a5rGmXReIsQy66ZTp+dwI3KKvaZ3uYw/Bf/oap+A=; b=cPJm8HgBd1DJE7HqNQWGJIzRNxgcd1zZ7cS99Tfjr3lQv6vIQ7repx8sUfWXr30L IuUYjRTRGKm6aITKOtBK8YK4UfXlAADYM3j0PpTEtQP3g3+KPNjPMs1If9z+VletRIj MA5f81vfPQzQUMklPALqIchSGjNj6RqMVjPnaKZLGXddABRSMcFK7q7LhllnpyXSwPT YQMfLoWaQKnl77g6BdHZZs69qrTmr310iEUnApMGFrwBhPs+bFVGuVYpiM3UWtnAZfg 6W2b647cmifnzn0V/VKwX6XQe736fTjvogzTwjBkYloE78jwcnZal+534KOAvneZSmM uEH6HiLZ+Q== Received: by mx.zohomail.com with SMTPS id 1788286292417197.3370562977443; Tue, 1 Sep 2026 11:11:32 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:27 +0800 Subject: [PATCH v6 08/12] leds: trigger: Enforce strict checks in led_trigger_is_hw_controlled() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-8-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External With all existing triggers adopting the new interface, strict checks could be enforced to make the semantics of hardware control triggers clearer. In detail, a hardware control trigger should: - Implement offloaded() callback to indicate hardware control - Associate with the LED classdev's hw_control_trigger string Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Changes in v3: - New patch in the series, splitted from PATCH 3 (thanks Thomas Wei=C3=9Fschuh) --- drivers/leds/led-triggers.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c index de17a8bbb4d4..cb49a02a8b3c 100644 --- a/drivers/leds/led-triggers.c +++ b/drivers/leds/led-triggers.c @@ -42,9 +42,16 @@ static bool __led_trigger_is_hw_controlled(struct led_cl= assdev *led_cdev) if (!led_cdev->trigger) return false; =20 + if (!led_cdev->hw_control_trigger || + strcmp(led_cdev->hw_control_trigger, led_cdev->trigger->name)) + return false; + if (led_cdev->trigger->offloaded) return led_cdev->trigger->offloaded(led_cdev); =20 + dev_warn_once(led_cdev->dev, "hw control trigger %s doesn't implement off= loaded()\n", + led_cdev->trigger->name); + /* Otherwise assume private triggers as always offloaded. */ return led_cdev->trigger->trigger_type; } --=20 2.55.0 From nobody Sat Sep 26 11:47:53 2026 Received: from sender6-op-o12.zoho.com (sender6-op-o12.zoho.com [165.173.180.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEB5F471CFA; Tue, 1 Sep 2026 18:14:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=165.173.180.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286480; cv=pass; b=UQRiNztdLV3AgVNciDDm72xH+9e/Imk793bxeGzUN+VCKFRkSeRCz+raxctaML7qtsyYxqvSKmxmpTOw/Gmv7A50dGUAQmTpAVLVNkGBUy2YnXej0rsZquOYT5SUY6ecyPNe0bDW/NczYTfh1y4ibK2no3fAv3DYD9DV36YA9aw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286480; c=relaxed/simple; bh=iigfWwr7gioPFGeqdPNt2SusWPh0MWGQzQLVYFq9sGE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Z4BOUp67zzOzCH255tPhkxdFWG7dcxKzIbsUnjQIInlHjVfJidpd2cNVv+RZkcWs8Y4r0GWq6MsP1HrjnOafa0/9X6/CuUX1B5t7HskenwI1sthIfZchII7+WiLK3px9MriYJ9KC0DUwMF00TDGH6pEdTbzlaEVln+QwHtUpH5w= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=n8QotIu1; arc=pass smtp.client-ip=165.173.180.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="n8QotIu1" ARC-Seal: i=1; a=rsa-sha256; t=1788286300; cv=none; d=zohomail.com; s=zohoarc; b=hgJ6Zg3KT/jn7HkdoTYJlHwhtzvknciBWrlIzWVScPyquiM+tda/Kxe1BZLNL7ptX6v9dDhgGPe2ub1JvE9OBfwl2jMX3DfaYvwEuE5YaUIo2NinaNqp0nlUXL6yxw4uVHmxEwYOItHF9tl3hZH5cfk5+wN1Q8P70FCg04Na8oE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286300; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=xtIscYNx+aeZI0i+7JK2Pppd2dgg/IMnkiSG1/AbuCQ=; b=oIoyWzWeImBJvWg5EqGdGmbYmqBIdTDvn63tqYVnpBVXzF12iZyOkR5wQuEBV3S+bRuAxoB+fMOoA0NXTbLtIYFyDIwrVR2OXWjAsMqHx14a4A/ZWS6+gy+W0ScS2s6F8Uoq6a1lc1uAgUcxxa4JcGlQD++f+a6LoAK3EVBfwyk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286300; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=xtIscYNx+aeZI0i+7JK2Pppd2dgg/IMnkiSG1/AbuCQ=; b=n8QotIu14lESFCS93rlV26PgZOPOUP1db/KeGHszKuhHd99cDHD5k+1yX2cYSnKL kezDkaB5DM59Yl+dVM/v2UxbgiGQ+AguVyy0zShzbZ3UCqXAY1GVP8tBcbS7+JSokRe +wNAIp8inU4zstOU4zFx86sw/pW3H+0Vknyi8lOGpF5wrX9MVl+6PDeknEaouS4YmbG yzVpDbjqsdNAhkyBepFB0dMGYfh/d369i00J3fhepFY9ebnF3qi1+mmveEUNKdgT7Rx v/WBcVIkR++vJCv5xbHh/xBbzNJ/OXtsqa1exNtGMazwNHoeNb7QdWyYjuZGP6tRsQ1 fb4VeCG8rQ== Received: by mx.zohomail.com with SMTPS id 1788286297911412.9415746849211; Tue, 1 Sep 2026 11:11:37 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:28 +0800 Subject: [PATCH v6 09/12] leds: trigger: Add led_trigger_notify_hw_control_changed() interface Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-9-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External Some hardware can autonomously activate/deactivate hardware control. After that, the LED hardware notifies the LED driver. Currently, there is no mechanism for LED drivers to notify the LED core about such events and initiate a trigger transition to reflect the hardware state. Add a new interface called led_trigger_notify_hw_control_changed(), so that LED drivers can call it to notify the LED core about the transition. The interface only allows two transitions: 1. "none" =3D> private trigger 2. private trigger =3D> "none" If the current trigger is neither the private trigger nor "none", no transition will be made. This protects the currently selected software trigger. Note that LED_OFF won't be emitted during the #2 transition, as some hardware may have selected a new brightness level during its hardware state transition (e.g., laptop keyboards with a shortcut cycling through different backlight brightnesses and auto mode). The interface is designed as a void function as any failure should be non-fatal and the result of transition should not have any impact on the LED drivers' event handling procedures. To use the interface, the config LEDS_TRIGGERS_HW_CHANGED must be enabled, and the LED driver must set the LED_TRIG_HW_CHANGED flag for the classdev. By default, the config is enabled when LEDS_BRIGHTNESS_HW_CHANGED is enabled. Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Changes in v6: - Implement workqueue deferal mechanism - https://msgid.link/e2b081dfd8f96511a73b86ab3ec75e5cd759b79b.camel@rong.= moe Changes in v5: - Address a concern from Sashiko: - led_trigger_notify_hw_control_changed() might sleep, but without any internal deferral mechanism or annotation - Annotate the method with might_sleep(), since the very first users of the interface, i.e., ideapad-laptop and (supposedly) thinkpad_acpi, will call the interface from work contexts. It does not deserve the overhead of internal deferral mechanism - https://sashiko.dev/#/patchset/20260802-leds-trigger-hw-changed-v4-0-f9= 7e2ca976fe@rong.moe?part=3D9 Changes in v4: - Enable LEDS_TRIGGERS_HW_CHANGED by default when LEDS_BRIGHTNESS_HW_CHANGED is enabled Changes in v3: - Adopt guard() (Thanks Thomas Wei=C3=9Fschuh) - Reword documentations --- Documentation/leds/leds-class.rst | 52 ++++++++++++++++++ drivers/leds/led-class.c | 6 +++ drivers/leds/led-triggers.c | 108 ++++++++++++++++++++++++++++++++++= +++- drivers/leds/leds.h | 8 +++ drivers/leds/trigger/Kconfig | 10 ++++ include/linux/leds.h | 13 +++++ 6 files changed, 195 insertions(+), 2 deletions(-) diff --git a/Documentation/leds/leds-class.rst b/Documentation/leds/leds-cl= ass.rst index 2d41a6db602c..adbc57b9f49c 100644 --- a/Documentation/leds/leds-class.rst +++ b/Documentation/leds/leds-class.rst @@ -334,6 +334,58 @@ not necessary for them to coordinate via `hw_control_*= ` callbacks. When the LED is in hw control, no software blink is possible and doing so will effectively disable hw control. =20 +Hardware-initiated trigger transition +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Some hardware can autonomously activate/deactivate hardware control. After= that, +the LED hardware notifies the LED driver. + +If the driver can detect such transitions and thus wants to notify the LED= core +to update the current trigger then the `LED_TRIG_HW_CHANGED` flag must be = set in +flags before registering. To update the current trigger accordingly, call +`led_trigger_notify_hw_control_changed` on the LED classdev. + +This capability is restricted to the LED device's private trigger. The pri= vate +trigger must have been properly registered (see above) and named after +`hw_control_trigger`. + +Only two transitions are defined: + +- "none" =3D> private trigger: + This happens when the hardware autonomously activates hardware con= trol + and when "none" (i.e., no trigger) is currently active. If the pri= vate + trigger is already active when the method is called, this is essen= tially + a no-op. + + The activation sequence for the private trigger will be executed as + normal. + + The LED driver and its private trigger must be able to handle the + activation sequence even if the hardware is currently in hardware + control. + + If error occurs in the activation sequence, the LED Trigger core r= everts + the effective trigger to "none". + +- private trigger =3D> "none" + This happens when the hardware autonomously deactivates hardware c= ontrol + and when the private trigger is currently active. If "none" (i.e.,= no + trigger) is active when the method is called, this is essentially a + no-op. + + The deactivation sequence for the private trigger will be executed= as + normal, except that the current LED brightness is retained. The re= ason + for keeping the brightness unchanged is that some hardware may cho= ose a + specific brightness instead of simply turning off the LED after + autonomously deactivating hardware control. + + The LED driver and its private trigger must be able to handle the + deactivation sequence even if the hardware is not currently in har= dware + control. + +If the current trigger is neither the private trigger nor "none", no trans= ition +will be made. + Known Issues =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c index 7e571bd1de5b..3b438d8da5e0 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -611,6 +611,9 @@ int led_classdev_register_ext(struct device *parent, led_trigger_set_default(led_cdev); #endif =20 + if (led_cdev->flags & LED_TRIG_HW_CHANGED) + led_trigger_init_hw_changed(led_cdev); + mutex_unlock(&led_cdev->led_access); =20 dev_dbg(parent, "Registered led device: %s\n", @@ -631,6 +634,9 @@ void led_classdev_unregister(struct led_classdev *led_c= dev) if (IS_ERR_OR_NULL(led_cdev->dev)) return; =20 + if (led_cdev->flags & LED_TRIG_HW_CHANGED) + led_trigger_destroy_hw_changed(led_cdev); + #ifdef CONFIG_LEDS_TRIGGERS down_write(&led_cdev->trigger_lock); if (led_cdev->trigger) diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c index cb49a02a8b3c..a9d992a88616 100644 --- a/drivers/leds/led-triggers.c +++ b/drivers/leds/led-triggers.c @@ -7,7 +7,9 @@ * Author: Richard Purdie */ =20 +#include #include +#include #include #include #include @@ -193,7 +195,8 @@ ssize_t led_trigger_read(struct file *filp, struct kobj= ect *kobj, EXPORT_SYMBOL_GPL(led_trigger_read); =20 /* Caller must ensure led_cdev->trigger_lock held */ -int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *tri= g) +static int __led_trigger_set(struct led_classdev *led_cdev, struct led_tri= gger *trig, + bool hw_triggered) { char *event =3D NULL; char *envp[2]; @@ -224,7 +227,21 @@ int led_trigger_set(struct led_classdev *led_cdev, str= uct led_trigger *trig) led_cdev->trigger_data =3D NULL; led_cdev->activated =3D false; led_cdev->flags &=3D ~LED_INIT_DEFAULT_TRIGGER; - led_set_brightness(led_cdev, LED_OFF); + + /* + * Hardware may have selected a new brightness level during its + * hardware control transition, so only reset brightness if we + * are switching to another trigger or if the switching is not + * hardware triggered. + * + * Note that this does not apply to the error path, as running + * into the error path implies a none =3D> private trigger + * transition. This hints that the LED driver and its private + * trigger must have some fundamental bugs, so don't bother + * leaving the LED in an undefined state. + */ + if (trig || !hw_triggered) + led_set_brightness(led_cdev, LED_OFF); } if (trig) { spin_lock(&trig->leddev_list_lock); @@ -288,6 +305,11 @@ int led_trigger_set(struct led_classdev *led_cdev, str= uct led_trigger *trig) =20 return ret; } + +int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *tri= g) +{ + return __led_trigger_set(led_cdev, trig, false); +} EXPORT_SYMBOL_GPL(led_trigger_set); =20 void led_trigger_remove(struct led_classdev *led_cdev) @@ -468,6 +490,88 @@ int devm_led_trigger_register(struct device *dev, } EXPORT_SYMBOL_GPL(devm_led_trigger_register); =20 +#ifdef CONFIG_LEDS_TRIGGERS_HW_CHANGED + +static void led_trigger_do_hw_control_transition(struct led_classdev *led_= cdev, bool activate, + struct led_trigger *hc_trig) +{ + int err =3D 0; + + if (!led_cdev->trigger) { + /* "none" =3D> private trigger. */ + if (activate) + err =3D __led_trigger_set(led_cdev, hc_trig, true); + } else if (led_cdev->trigger =3D=3D hc_trig) { + /* private trigger =3D> "none". */ + if (!activate) + err =3D __led_trigger_set(led_cdev, NULL, true); + } else { + /* Other trigger is active. */ + dev_dbg(led_cdev->dev, + "Ignoring hw control transition (%s %s) while %s is active", + activate ? "activate" : "deactivate", hc_trig->name, + led_cdev->trigger->name); + + return; + } + + if (err) + dev_warn(led_cdev->dev, "Failed to %s %s in hw control transition: %d", + activate ? "activate" : "deactivate", hc_trig->name, err); +} + +static void led_trigger_hw_control_changed_worker(struct work_struct *work) +{ + struct led_classdev *led_cdev =3D + container_of(work, struct led_classdev, triggers_hw_changed_work); + bool activate =3D READ_ONCE(led_cdev->triggers_hw_changed); + + scoped_guard(rwsem_read, &triggers_list_lock) { + struct led_trigger *trig; + + list_for_each_entry(trig, &trigger_list, next_trig) { + if (trig->trigger_type =3D=3D led_cdev->trigger_type && + !strcmp(trig->name, led_cdev->hw_control_trigger)) { + guard(rwsem_write)(&led_cdev->trigger_lock); + + led_trigger_do_hw_control_transition(led_cdev, activate, trig); + return; + } + } + } + + dev_err(led_cdev->dev, + "%s() is called, but the private trigger (%s) is not properly registered= \n", + __func__, led_cdev->hw_control_trigger); +} + +void led_trigger_notify_hw_control_changed(struct led_classdev *led_cdev, = bool activate) +{ + /* Restricted to private triggers. */ + if (WARN_ON(!(led_cdev->flags & LED_TRIG_HW_CHANGED) || + !led_cdev->hw_control_trigger || !led_cdev->trigger_type)) + return; + + WRITE_ONCE(led_cdev->triggers_hw_changed, activate); + + schedule_work(&led_cdev->triggers_hw_changed_work); +} +EXPORT_SYMBOL_GPL(led_trigger_notify_hw_control_changed); + +void led_trigger_init_hw_changed(struct led_classdev *led_cdev) +{ + INIT_WORK(&led_cdev->triggers_hw_changed_work, led_trigger_hw_control_cha= nged_worker); +} +EXPORT_SYMBOL_GPL(led_trigger_init_hw_changed); + +void led_trigger_destroy_hw_changed(struct led_classdev *led_cdev) +{ + disable_work_sync(&led_cdev->triggers_hw_changed_work); +} +EXPORT_SYMBOL_GPL(led_trigger_destroy_hw_changed); + +#endif /* CONFIG_LEDS_TRIGGERS_HW_CHANGED */ + /* Simple LED Trigger Interface */ =20 void led_trigger_event(struct led_trigger *trig, diff --git a/drivers/leds/leds.h b/drivers/leds/leds.h index b08a289397e4..bdac2336012e 100644 --- a/drivers/leds/leds.h +++ b/drivers/leds/leds.h @@ -33,4 +33,12 @@ ssize_t trigger_may_offload_show(struct device *dev, extern struct rw_semaphore leds_list_lock; extern struct list_head leds_list; =20 +#ifdef CONFIG_LEDS_TRIGGERS_HW_CHANGED +void led_trigger_init_hw_changed(struct led_classdev *led_cdev); +void led_trigger_destroy_hw_changed(struct led_classdev *led_cdev); +#else /* !CONFIG_LEDS_TRIGGERS_HW_CHANGED */ +static inline void led_trigger_init_hw_changed(struct led_classdev *led_cd= ev) { } +static inline void led_trigger_destroy_hw_changed(struct led_classdev *led= _cdev) { } +#endif /* CONFIG_LEDS_TRIGGERS_HW_CHANGED */ + #endif /* __LEDS_H_INCLUDED */ diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig index c11282a74b5a..a11d04ce4ab2 100644 --- a/drivers/leds/trigger/Kconfig +++ b/drivers/leds/trigger/Kconfig @@ -9,6 +9,16 @@ menuconfig LEDS_TRIGGERS =20 if LEDS_TRIGGERS =20 +config LEDS_TRIGGERS_HW_CHANGED + bool "LED hardware-initiated trigger transition support" + default LEDS_BRIGHTNESS_HW_CHANGED + help + This option enables support for hardware initiated hardware control + transitions, where the LED hardware autonomously switches between + "none" (i.e., no trigger) and its private trigger. + + See Documentation/leds/leds-class.rst for details. + config LEDS_TRIGGER_TIMER tristate "LED Timer Trigger" help diff --git a/include/linux/leds.h b/include/linux/leds.h index bee2b4309a09..93b3fd7e5636 100644 --- a/include/linux/leds.h +++ b/include/linux/leds.h @@ -109,6 +109,7 @@ struct led_classdev { #define LED_INIT_DEFAULT_TRIGGER BIT(23) #define LED_REJECT_NAME_CONFLICT BIT(24) #define LED_MULTI_COLOR BIT(25) +#define LED_TRIG_HW_CHANGED BIT(26) =20 /* set_brightness_work / blink_timer flags, atomic, private. */ unsigned long work_flags; @@ -239,6 +240,11 @@ struct led_classdev { struct kernfs_node *brightness_hw_changed_kn; #endif =20 +#ifdef CONFIG_LEDS_TRIGGERS_HW_CHANGED + bool triggers_hw_changed; + struct work_struct triggers_hw_changed_work; +#endif + /* Ensures consistent access to the LED class device */ struct mutex led_access; }; @@ -609,6 +615,13 @@ led_trigger_get_brightness(const struct led_trigger *t= rigger) =20 #endif /* CONFIG_LEDS_TRIGGERS */ =20 +#ifdef CONFIG_LEDS_TRIGGERS_HW_CHANGED +void led_trigger_notify_hw_control_changed(struct led_classdev *led_cdev, = bool activate); +#else +static inline void led_trigger_notify_hw_control_changed(struct led_classd= ev *led_cdev, + bool activate) {} +#endif + /* Trigger specific enum */ enum led_trigger_netdev_modes { TRIGGER_NETDEV_LINK =3D 0, --=20 2.55.0 From nobody Sat Sep 26 11:47:53 2026 Received: from sender5-op-o12.zoho.com (sender5-op-o12.zoho.com [165.173.182.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6BEF848F848; Tue, 1 Sep 2026 18:15:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=165.173.182.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286501; cv=pass; b=i7I+dGL/OIQHeN5iUcAYCdS46dNrLO6STNSptRK7e84jnU6y4t2zkvdCHk8ZH134q3JmjtdNFKGl43tXfuLmouwgNaeQ8R50hubOLCmWU0PAoc2dtabvMJFVhq+fo6cV1kSLUGE5yjaf4IgSNiXDZxhtqpoDdDd6bXfTObWREm0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286501; c=relaxed/simple; bh=kbWO3d4W1V5pmzBTou6J2PpxmnVkxPo0U0r7mMtvvyI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=A55IcZp6PXhX8IdG59cvkCyptOQN1VMKxkFy12TAZEGDIBT4vCGG+11L3oSX2vYl8KEOuHULLsdjoucLMGHM4hxkPTIAnZm/kDDBYtC/25NTrcANe9xSd7pFrSgSS2eljsPXVCuyPsZzY2fe8v7WUmV/nM9gKXSfeA6pvO9cU8A= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=j7VktOIt; arc=pass smtp.client-ip=165.173.182.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="j7VktOIt" ARC-Seal: i=1; a=rsa-sha256; t=1788286306; cv=none; d=zohomail.com; s=zohoarc; b=Wks847Md3Fl7Y5yGmyWcr+Wb6AI+nMdN7A760QLrTto/680wnIM+jEEYVyjwWe/sZ8v3On5RsGaaJUuzk3aOwihxOhm4951Vy1ysbBAf4vIpaL/V/Wt7M+wyzzGofRV1xlIOfrVfMRg2MGsSAUYZ9maQEqgQIK7rrSzk/yQgwU4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286306; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=ToI18bR9XJY1UTByBrP+FM1o48uVmfnxpVfIbtodrQo=; b=hFf9rpTuwHEvVjMWVLeVCVPG970OVsZ+z5efl7yx9QZ7Z9m1CkKgf/1d+bLtHzisEq4zF9ITPV/jwZZqDMpIBf3TwOkNQhQd50kmVrM2U5fUxePkc1H0oYiFPkVmb7Zd92MMb4q+QmNEmYaZhBkahKS0EcMe3FrguMhjC6j2A3Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286306; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=ToI18bR9XJY1UTByBrP+FM1o48uVmfnxpVfIbtodrQo=; b=j7VktOItX7x9Z7RS/Fn+eEeodM16J6jXm/S5RSr3UuaidLhusDJhMrCPR8W3RkKo bddw4R28rOqOIdx85ncRhdyAUyR2nMKDPSzxYy8c7vf46Ukj/+uWQmrZ02JraC3M65B 60WVKFd6c6rFKY+0Yn5Tzg3mZHllCEAH+yk6Uc5N4RD9R+VsWpSZlgT6xTFy7ehQ/Ah LZUUHvk9Wopy3wMvRENc2v0F2IUUanoZ/ymL7aDbjryDMzzh85vvLPmZ7+lKeAmpFqM jLgh+p4lVdOWFKPU/jTbuAIxV3ADyAV3qRVdKQ1idhDK9a17C1igA9dRfyBgIcUbfm9 0xjy2XWuaQ== Received: by mx.zohomail.com with SMTPS id 1788286303729832.671594036358; Tue, 1 Sep 2026 11:11:43 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:29 +0800 Subject: [PATCH v6 10/12] platform/x86: ideapad-laptop: Serialize keyboard backlight tracking Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-10-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External ACPI notifications are delivered in dedicated work contexts and may arrive simultaneously. In the following change, much work will be done while handling the notification, which could lead to potential race conditions. Introduce a new mutex to serialize keyboard backlight tracking to prevent potential race conditions. Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Changes in v6: - Protects ideapad_kbd_bl_led_cdev_brightness_set() as well Changes in v4: - Remove needless #include (Thanks Ilpo J=C3=A4rvinen) --- drivers/platform/x86/lenovo/ideapad-laptop.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/platform/x86/lenovo/ideapad-laptop.c b/drivers/platfor= m/x86/lenovo/ideapad-laptop.c index 8213524504ee..684421ba53cb 100644 --- a/drivers/platform/x86/lenovo/ideapad-laptop.c +++ b/drivers/platform/x86/lenovo/ideapad-laptop.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -204,6 +205,7 @@ struct ideapad_private { int type; struct led_classdev led; unsigned int last_brightness; + struct mutex mutex; /* protects brightness tracking */ } kbd_bl; struct { bool initialized; @@ -1643,6 +1645,8 @@ static int ideapad_kbd_bl_brightness_set(struct ideap= ad_private *priv, unsigned unsigned long value; int type =3D priv->kbd_bl.type; =20 + guard(mutex)(&priv->kbd_bl.mutex); + if (ideapad_kbd_bl_check_tristate(type)) { if (brightness > priv->kbd_bl.led.max_brightness) return -EINVAL; @@ -1678,6 +1682,8 @@ static void ideapad_kbd_bl_notify(struct ideapad_priv= ate *priv) if (!priv->kbd_bl.initialized) return; =20 + guard(mutex)(&priv->kbd_bl.mutex); + brightness =3D ideapad_kbd_bl_brightness_get(priv); if (brightness < 0) return; @@ -1700,6 +1706,10 @@ static int ideapad_kbd_bl_init(struct ideapad_privat= e *priv) if (WARN_ON(priv->kbd_bl.initialized)) return -EEXIST; =20 + err =3D devm_mutex_init(&priv->platform_device->dev, &priv->kbd_bl.mutex); + if (err) + return err; + if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) priv->kbd_bl.led.max_brightness =3D 2; else --=20 2.55.0 From nobody Sat Sep 26 11:47:53 2026 Received: from sender6-op-o12.zoho.com (sender6-op-o12.zoho.com [165.173.180.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EDE1449505F; Tue, 1 Sep 2026 18:15:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=165.173.180.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286523; cv=pass; b=VOaqr+Vz81YIVDM8Ug8SXZjUOMeyl/D0H5Rygnrz1zJd1RG7pbtjb6VBCNiY6iClOKN+tj4VV62X0uH8ogLg0SFP1U1KDfAiAoBzKP9yq9qHfLTli7W5qLegNNmywBNkqj+26u5d7pPMDrb2mQ1VIZ5x5yoGhfNT7mEt16WsinY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286523; c=relaxed/simple; bh=f101wTkFUclXNaCiyQ+JhMGT1JvxAVQ6ArNG5G73WHg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ScqsUwsPahfV44riGg7blYmLJfFYcirzhtjmdC6MGhWIyUYm4GLRv8NAJZRAtjKJzkisH2Z+PrYnKn4WVf73VyAu7YUQF3tHNXXUoAie84SLDbZftALHaQX9hUVA/A8jLSEJNMWbhlx5vh+CpW0q27liTscrh5OTWTcesGuMAX4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=Gq5poiCv; arc=pass smtp.client-ip=165.173.180.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="Gq5poiCv" ARC-Seal: i=1; a=rsa-sha256; t=1788286310; cv=none; d=zohomail.com; s=zohoarc; b=eyWL8BugQYaE2N9ApT18RrAtOM0pWUdodoTxjVfdynLSN4m2Vad8K0Cb1zOxpWS6lRMQ/5zERzvEdlP+eoG3Sn47fPZoKR4YYdY3SWvYSEnzVChJqXIHYnxbB3S31VDyur4KJP721Ht4SQHzzufTXl9samJuhDQmqFINeLq6VEY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286310; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=CZ16ws7Fh7Y7atQRZIsxJEPtJiLcaui8Bpf+4818fRw=; b=EcELaGgol/pmKi1r1QfGGwg7cS5g57ZsLmfxLpIibxwbq8sKd4/s34Idp0bkvkJuXmdzql2YS9HRLGD6LsAJPsLCeMoVuGG9ulZ878WvbsYfxEk+yQYl5JTJb3b6ZqHU3Xuy6t7Oev56mYz9iOjy9oiMz81VGaFJ0ap0+s1llbc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286310; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=CZ16ws7Fh7Y7atQRZIsxJEPtJiLcaui8Bpf+4818fRw=; b=Gq5poiCv8ZCSnwLXSBkpfJE64swOUZWUCmf0KbF+ACobb2vJRFH82KA6rO45O2jP fBYmuCksDfyiEgBvuRhaseoWLSKy7KOFSL9SMiVyvN37auRhmhL/s6FMG1wdyCP8SeE LGgn6owGOuHJYAO4rfPEvWhKHsVyq7QFBkFRrbYrjXlHF92RaEUqMP4fhufDi18Q/HV rB8ZLZ46WdwHx9/IV2meGqinc2HeuY+lTy4FUaQ5f2J/abPemBjIrLPi0ONIt76kpNw kdCboH0hEIuVLc9QxY5TYFsgYSNstqUbudiawK7ZYHU3SZS/t8yg9PTY6eQedbJoUiv isZ76Bcukg== Received: by mx.zohomail.com with SMTPS id 1788286309160574.2907017834425; Tue, 1 Sep 2026 11:11:49 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:30 +0800 Subject: [PATCH v6 11/12] platform/x86: ideapad-laptop: Decouple hardware & classdev brightness for keyboard backlight Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-11-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External Some recent models come with an ambient light sensor (ALS). On these models, their EC will automatically set the keyboard backlight to an appropriate brightness when the effective "hardware brightness" is 3. "Hardware brightness" can't be perfectly mapped to an LED classdev brightness, but the EC does use this predefined brightness value to represent auto mode. Currently, the code processing keyboard backlight is coupled with LED classdev, making it hard to expose the auto brightness (ALS) mode to the userspace. As the first step toward the goal, decouple hardware brightness from LED classdev brightness, and update comments about corresponding backlight modes. To minimalize the diff set in upcoming changes, a trivial refactor also converts the initialization path into another equivalent form. Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Changes in v6: - No longer convert last_hw_brightness into atomic_t as it's not required now Changes in v4: - Add missing #include (Thanks Ilpo J=C3=A4rvinen) - Address concerns from Sashiko - Reject insane hardware brightness - https://sashiko.dev/#/patchset/20260719-leds-trigger-hw-changed-v3-0-5f= b55722e36e@rong.moe?part=3D9 --- drivers/platform/x86/lenovo/ideapad-laptop.c | 150 +++++++++++++++++++----= ---- 1 file changed, 106 insertions(+), 44 deletions(-) diff --git a/drivers/platform/x86/lenovo/ideapad-laptop.c b/drivers/platfor= m/x86/lenovo/ideapad-laptop.c index 684421ba53cb..712999b6900d 100644 --- a/drivers/platform/x86/lenovo/ideapad-laptop.c +++ b/drivers/platform/x86/lenovo/ideapad-laptop.c @@ -14,9 +14,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -135,10 +137,31 @@ enum { }; =20 /* - * These correspond to the number of supported states - 1 - * Future keyboard types may need a new system, if there's a collision - * KBD_BL_TRISTATE_AUTO has no way to report or set the auto state - * so it effectively has 3 states, but needs to handle 4 + * The enumeration has two purposes: + * - as an internal identifier for all known types of keyboard backlight + * - as a mandatory parameter of the KBLC command + * + * For each type, the hardware brightness values are defined as follows: + * +--------------------------+----------+-----+------+------+ + * | Hardware brightness | 0 | 1 | 2 | 3 | + * | Type | | | | | + * +--------------------------+----------+-----+------+------+ + * | KBD_BL_STANDARD | off | on | N/A | N/A | + * +--------------------------+----------+-----+------+------+ + * | KBD_BL_TRISTATE | off | low | high | N/A | + * +--------------------------+----------+-----+------+------+ + * | KBD_BL_TRISTATE_AUTO | off | low | high | auto | + * +--------------------------+----------+-----+------+------+ + * + * We map LED classdev brightness for KBD_BL_TRISTATE_AUTO as follows: + * +--------------------------+----------+-----+------+ + * | LED classdev brightness | 0 | 1 | 2 | + * | Operation | | | | + * +--------------------------+----------+-----+------+ + * | Read | off/auto | low | high | + * +--------------------------+----------+-----+------+ + * | Write | off | low | high | + * +--------------------------+----------+-----+------+ */ enum { KBD_BL_STANDARD =3D 1, @@ -146,6 +169,8 @@ enum { KBD_BL_TRISTATE_AUTO =3D 3, }; =20 +#define KBD_BL_AUTO_MODE_HW_BRIGHTNESS 3 + #define KBD_BL_QUERY_TYPE 0x1 #define KBD_BL_TRISTATE_TYPE 0x5 #define KBD_BL_TRISTATE_AUTO_TYPE 0x7 @@ -204,7 +229,7 @@ struct ideapad_private { bool initialized; int type; struct led_classdev led; - unsigned int last_brightness; + unsigned int last_hw_brightness; struct mutex mutex; /* protects brightness tracking */ } kbd_bl; struct { @@ -1594,7 +1619,24 @@ static int ideapad_kbd_bl_check_tristate(int type) return (type =3D=3D KBD_BL_TRISTATE) || (type =3D=3D KBD_BL_TRISTATE_AUTO= ); } =20 -static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv) +static int ideapad_kbd_bl_brightness_parse(struct ideapad_private *priv, u= nsigned int hw_brightness) +{ + /* Off, low or high */ + if (hw_brightness <=3D priv->kbd_bl.led.max_brightness) + return hw_brightness; + + /* Auto (controlled by EC according to ALS), report as off */ + if (priv->kbd_bl.type =3D=3D KBD_BL_TRISTATE_AUTO && + hw_brightness =3D=3D KBD_BL_AUTO_MODE_HW_BRIGHTNESS) + return 0; + + /* Unknown value */ + dev_warn(&priv->platform_device->dev, + "Unknown keyboard backlight value: %d", hw_brightness); + return -EINVAL; +} + +static int ideapad_kbd_bl_hw_brightness_get(struct ideapad_private *priv) { unsigned long value; int err; @@ -1608,21 +1650,7 @@ static int ideapad_kbd_bl_brightness_get(struct idea= pad_private *priv) if (err) return err; =20 - /* Convert returned value to brightness level */ - value =3D FIELD_GET(KBD_BL_GET_BRIGHTNESS, value); - - /* Off, low or high */ - if (value <=3D priv->kbd_bl.led.max_brightness) - return value; - - /* Auto, report as off */ - if (value =3D=3D priv->kbd_bl.led.max_brightness + 1) - return 0; - - /* Unknown value */ - dev_warn(&priv->platform_device->dev, - "Unknown keyboard backlight value: %lu", value); - return -EINVAL; + return FIELD_GET(KBD_BL_GET_BRIGHTNESS, value); } =20 err =3D eval_hals(priv->adev->handle, &value); @@ -1632,6 +1660,16 @@ static int ideapad_kbd_bl_brightness_get(struct idea= pad_private *priv) return !!test_bit(HALS_KBD_BL_STATE_BIT, &value); } =20 +static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv) +{ + int hw_brightness =3D ideapad_kbd_bl_hw_brightness_get(priv); + + if (hw_brightness < 0) + return hw_brightness; + + return ideapad_kbd_bl_brightness_parse(priv, hw_brightness); +} + static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct l= ed_classdev *led_cdev) { struct ideapad_private *priv =3D container_of(led_cdev, struct ideapad_pr= ivate, kbd_bl.led); @@ -1639,34 +1677,40 @@ static enum led_brightness ideapad_kbd_bl_led_cdev_= brightness_get(struct led_cla return ideapad_kbd_bl_brightness_get(priv); } =20 -static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, uns= igned int brightness) +static int ideapad_kbd_bl_hw_brightness_set(struct ideapad_private *priv, + unsigned int hw_brightness) { - int err; unsigned long value; int type =3D priv->kbd_bl.type; + int err; =20 guard(mutex)(&priv->kbd_bl.mutex); =20 if (ideapad_kbd_bl_check_tristate(type)) { - if (brightness > priv->kbd_bl.led.max_brightness) - return -EINVAL; - - value =3D FIELD_PREP(KBD_BL_SET_BRIGHTNESS, brightness) | + value =3D FIELD_PREP(KBD_BL_SET_BRIGHTNESS, hw_brightness) | FIELD_PREP(KBD_BL_COMMAND_TYPE, type) | KBD_BL_COMMAND_SET; err =3D exec_kblc(priv->adev->handle, value); } else { - err =3D exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS= _KBD_BL_OFF); + value =3D hw_brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF; + err =3D exec_sals(priv->adev->handle, value); } - if (err) return err; =20 - priv->kbd_bl.last_brightness =3D brightness; + priv->kbd_bl.last_hw_brightness =3D hw_brightness; =20 return 0; } =20 +static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, uns= igned int brightness) +{ + if (brightness > priv->kbd_bl.led.max_brightness) + return -EINVAL; + + return ideapad_kbd_bl_hw_brightness_set(priv, brightness); +} + static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led= _cdev, enum led_brightness brightness) { @@ -1677,28 +1721,32 @@ static int ideapad_kbd_bl_led_cdev_brightness_set(s= truct led_classdev *led_cdev, =20 static void ideapad_kbd_bl_notify(struct ideapad_private *priv) { - int brightness; + int hw_brightness, brightness; =20 if (!priv->kbd_bl.initialized) return; =20 guard(mutex)(&priv->kbd_bl.mutex); =20 - brightness =3D ideapad_kbd_bl_brightness_get(priv); - if (brightness < 0) + hw_brightness =3D ideapad_kbd_bl_hw_brightness_get(priv); + if (hw_brightness < 0) return; =20 - if (brightness =3D=3D priv->kbd_bl.last_brightness) + brightness =3D ideapad_kbd_bl_brightness_parse(priv, hw_brightness); + if (brightness < 0) + return; /* Reject insane values early. */ + + if (priv->kbd_bl.last_hw_brightness =3D=3D hw_brightness) return; =20 - priv->kbd_bl.last_brightness =3D brightness; + priv->kbd_bl.last_hw_brightness =3D hw_brightness; =20 led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness); } =20 static int ideapad_kbd_bl_init(struct ideapad_private *priv) { - int brightness, err; + int hw_brightness, err; =20 if (!priv->features.kbd_bl) return -ENODEV; @@ -1710,21 +1758,35 @@ static int ideapad_kbd_bl_init(struct ideapad_priva= te *priv) if (err) return err; =20 - if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) - priv->kbd_bl.led.max_brightness =3D 2; - else - priv->kbd_bl.led.max_brightness =3D 1; + hw_brightness =3D ideapad_kbd_bl_hw_brightness_get(priv); + if (hw_brightness < 0) + return hw_brightness; =20 - brightness =3D ideapad_kbd_bl_brightness_get(priv); - if (brightness < 0) - return brightness; + priv->kbd_bl.last_hw_brightness =3D hw_brightness; =20 - priv->kbd_bl.last_brightness =3D brightness; priv->kbd_bl.led.name =3D "platform::" LED_FUNCTION_KB= D_BACKLIGHT; priv->kbd_bl.led.brightness_get =3D ideapad_kbd_bl_led_cdev_brig= htness_get; priv->kbd_bl.led.brightness_set_blocking =3D ideapad_kbd_bl_led_cdev_brig= htness_set; priv->kbd_bl.led.flags =3D LED_BRIGHT_HW_CHANGED | LED_= RETAIN_AT_SHUTDOWN; =20 + switch (priv->kbd_bl.type) { + case KBD_BL_TRISTATE_AUTO: + case KBD_BL_TRISTATE: + priv->kbd_bl.led.max_brightness =3D 2; + break; + case KBD_BL_STANDARD: + priv->kbd_bl.led.max_brightness =3D 1; + break; + default: + /* This has already been validated by ideapad_check_features(). */ + unreachable(); + } + + /* Reject insane values. */ + err =3D ideapad_kbd_bl_brightness_parse(priv, hw_brightness); + if (err < 0) + return err; + err =3D led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.= led); if (err) return err; --=20 2.55.0 From nobody Sat Sep 26 11:47:53 2026 Received: from sender6-op-o12.zoho.com (sender6-op-o12.zoho.com [165.173.180.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18EB230FF31; Tue, 1 Sep 2026 18:15:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=165.173.180.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286544; cv=pass; b=CANjtSm8RaCYCScIPy3/7FcweZ5+opMUuDxyXZvHUDoPBS3U4U6m3nILHB2SAY7zr1mRQ1uquddfpt7cQNXujlyA6Bf7o4aLUm5phS+KDFJVEoF5XFa3hnbkVlNDbQz9bb6NXTObjBhfdD5iMjl+q/16IlbTlnlVrmui6jVZUp4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788286544; c=relaxed/simple; bh=1f0zvOu2jPLDdWuGAPb50wvl7cCDd8FWormwkoeNw3g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=iIsTifedW0LemgC1hXGkGAG1+f8HCyBDiCo5zxbRAl8vlGMtVcHhLw+mzAMEP6w/lvl0UXiWL4RlJ2AIt0pEPLq0lpfUYEZu3GZ0U/SjwusyV1PKK0tPhfCf68qD4N46s+No9v3U78GPdNgfXIW5/fGqIcL6KXQF97eMwKqVgu8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=ahK7dsdV; arc=pass smtp.client-ip=165.173.180.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="ahK7dsdV" ARC-Seal: i=1; a=rsa-sha256; t=1788286316; cv=none; d=zohomail.com; s=zohoarc; b=a6tzy+htiy5BOXZO1u0QV7BsU4e4jC0+AfdVOW2RWO1J3rC3H9DKyWXTXRQtb5YhU8+Onr9i5KW5JNJ1SocJ1sXlIm7CptT1YI7DaEAbQf1fGgHyQfsjtP4kq9Vr7bj78L3uAx6fFCif5tp0qJbnL+SsqZyQeWdWNGyCCSfjsf4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788286316; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=Ry2Ca6eVIobbdb3mjvfI+PPMW1jezxUtVw9TJE04NiQ=; b=cOgQ/C68LGJa5ncmZrENa1d5gNzQWcQQK7cZIIPfA2zjpAELWJm10aU64/ysNRMbhRUmiH9zN1/+Wrv6sIgMyaCrOJVE96W3smLgp7J7ZZoE2QJowHJUlTuhLpV0XfTUnpf/dyOTvMjZZcvSz2Xngz6UCJq2+ehuNZM8+BGUC2Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1788286316; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=Ry2Ca6eVIobbdb3mjvfI+PPMW1jezxUtVw9TJE04NiQ=; b=ahK7dsdVrtHFa9kH3uJp9ZDbRz7XbKUi9HxWL43F8vnel5iEy4FFzWt1rIrc8FmX Xsbs4mAWHx/OEbNCvNuoTWWjVuJ30eKuv6/n4W7Dkz1v+4bcla7zqZ32W62MHccXRJn VfgwBXevRlrdVDIpfd0yYHlhLh51HLSni06sCP49oibDZMTvf1FxaxmkGyNF9K0KiwG W9qlDF5kvnvjprhsu0iCsyw2sgco/FWJtt+fdB5DYW2H8YsoY98ryPtr0t02+/mkuHN aRRV1gJ7B31CfHvzwcGGh4uwNzHjHbPwRydZdUtWA4twTA1u0Pyed+lsXcn5F3H+jc3 0fHGP6mA9g== Received: by mx.zohomail.com with SMTPS id 1788286314577497.2575333775951; Tue, 1 Sep 2026 11:11:54 -0700 (PDT) From: Rong Zhang Date: Wed, 02 Sep 2026 02:09:31 +0800 Subject: [PATCH v6 12/12] platform/x86: ideapad-laptop: Fully support auto keyboard backlight Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260902-leds-trigger-hw-changed-v6-12-55693cd78877@rong.moe> References: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> In-Reply-To: <20260902-leds-trigger-hw-changed-v6-0-55693cd78877@rong.moe> To: Lee Jones , Pavel Machek , Jonathan Corbet , Shuah Khan , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , =?utf-8?q?Marek_Beh=C3=BAn?= , Mark Pearson , "Derek J. Clark" , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Ike Panhc Cc: Andrew Lunn , Jakub Kicinski , Vishnu Sankar , Vishnu Sankar , linux-leds@vger.kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, platform-driver-x86@vger.kernel.org, Rong Zhang X-Mailer: b4 0.17-dev-1f2f7 X-ZohoMailClient: External Currently, the auto brightness mode of keyboard backlight maps to brightness=3D0 in LED classdev. The only method to switch to such a mode is by pressing the manufacturer-defined shortcut (Fn+Space). However, 0 is a multiplexed brightness value; writing 0 simply results in the backlight being turned off. With brightness processing code decoupled from LED classdev, we can now fully support the auto brightness mode. In this mode, the keyboard backlight is controlled by the EC according to the ambient light sensor (ALS). To utilize this, a private hardware control trigger "ideapad-auto" is added, with the event handling procedure calling the led_trigger_notify_hw_control_changed() interface to activate/deactivate the private trigger according to the current LED trigger state. To align with LEDS_BRIGHTNESS_HW_CHANGED, the driver neither depends on LEDS_TRIGGERS_HW_CHANGED nor selects it. Meanwhile, block brightness changes on exit to prevent the side effect of LED device unregistration when the private trigger is active from resetting the brightness to zero, so that the state of auto mode can retain among boots. Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Changes in v6: - Simplify the auto mode state preservation logic by converting `priv->kbd_bl.initialized' into a tristate integer Changes in v4: - Add missing #include (Thanks Ilpo J=C3=A4rvinen) - Remove needless code alignment (ditto) - Address concerns from Sashiko - Do not notify the LED trigger core when the registration of the private trigger has failed - https://sashiko.dev/#/patchset/20260719-leds-trigger-hw-changed-v3-0-5f= b55722e36e@rong.moe?part=3D11 Changes in v3: - Address concerns from Sashiko - Fix a race condition in ideapad_kbd_bl_led_cdev_brightness_set() - Fix trigger re-registration of ideapad_kbd_bl_auto_trigger - https://sashiko.dev/#/patchset/20260618-leds-trigger-hw-changed-v2-0-c2= 8c44053cf3%40rong.moe - Make registration failures of ideapad_kbd_bl_auto_trigger non-fatal --- drivers/platform/x86/lenovo/Kconfig | 1 + drivers/platform/x86/lenovo/ideapad-laptop.c | 119 +++++++++++++++++++++++= +--- 2 files changed, 109 insertions(+), 11 deletions(-) diff --git a/drivers/platform/x86/lenovo/Kconfig b/drivers/platform/x86/len= ovo/Kconfig index 516594993073..a97852b0b228 100644 --- a/drivers/platform/x86/lenovo/Kconfig +++ b/drivers/platform/x86/lenovo/Kconfig @@ -16,6 +16,7 @@ config IDEAPAD_LAPTOP select INPUT_SPARSEKMAP select NEW_LEDS select LEDS_CLASS + select LEDS_TRIGGERS help This is a driver for Lenovo IdeaPad netbooks contains drivers for rfkill switch, hotkey, fan control and backlight control. diff --git a/drivers/platform/x86/lenovo/ideapad-laptop.c b/drivers/platfor= m/x86/lenovo/ideapad-laptop.c index 712999b6900d..f86c11f51d0b 100644 --- a/drivers/platform/x86/lenovo/ideapad-laptop.c +++ b/drivers/platform/x86/lenovo/ideapad-laptop.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -32,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -226,7 +228,10 @@ struct ideapad_private { bool ymc_ec_trigger : 1; } features; struct { - bool initialized; + int initialized; /* 0: initializion pending/ongoing/failed + * >0: initializion finished + * <0: unregisteration ongoing/finished + */ int type; struct led_classdev led; unsigned int last_hw_brightness; @@ -1716,14 +1721,68 @@ static int ideapad_kbd_bl_led_cdev_brightness_set(s= truct led_classdev *led_cdev, { struct ideapad_private *priv =3D container_of(led_cdev, struct ideapad_pr= ivate, kbd_bl.led); =20 + /* + * When unregistering: It must be the side effect of LED unregistration + * when our private trigger is active. We've set LED_RETAIN_AT_SHUTDOWN + * to retain led_cdev brightness level. To do the same for auto mode, + * gate changes and return early. + * + * It's needless to gate changes when initializing, as there may be a + * time margin between sysfs attribute creation and state change. + */ + if (unlikely(priv->kbd_bl.initialized < 0)) + return 0; + return ideapad_kbd_bl_brightness_set(priv, brightness); } =20 +static bool ideapad_kbd_bl_auto_trigger_offloaded(struct led_classdev *led= _cdev) +{ + struct ideapad_private *priv =3D container_of(led_cdev, struct ideapad_pr= ivate, kbd_bl.led); + + guard(mutex)(&priv->kbd_bl.mutex); + + return priv->kbd_bl.last_hw_brightness =3D=3D KBD_BL_AUTO_MODE_HW_BRIGHTN= ESS; +} + +static int ideapad_kbd_bl_auto_trigger_activate(struct led_classdev *led_c= dev) +{ + struct ideapad_private *priv =3D container_of(led_cdev, struct ideapad_pr= ivate, kbd_bl.led); + + return ideapad_kbd_bl_hw_brightness_set(priv, KBD_BL_AUTO_MODE_HW_BRIGHTN= ESS); +} + +static struct led_hw_trigger_type ideapad_kbd_bl_auto_trigger_type; + +static struct led_trigger ideapad_kbd_bl_auto_trigger =3D { + .name =3D "ideapad-auto", + .trigger_type =3D &ideapad_kbd_bl_auto_trigger_type, + .activate =3D ideapad_kbd_bl_auto_trigger_activate, + .offloaded =3D ideapad_kbd_bl_auto_trigger_offloaded, +}; + +static bool ideapad_kbd_bl_auto_trigger_registered; + +static void ideapad_kbd_bl_notify_hw_control(struct ideapad_private *priv, + unsigned int hw_brightness) +{ + bool hw_control, last_hw_control; + + if (!ideapad_kbd_bl_auto_trigger_registered || priv->kbd_bl.type !=3D KBD= _BL_TRISTATE_AUTO) + return; + + hw_control =3D hw_brightness =3D=3D KBD_BL_AUTO_MODE_HW_BRIGHTNESS; + last_hw_control =3D priv->kbd_bl.last_hw_brightness =3D=3D KBD_BL_AUTO_MO= DE_HW_BRIGHTNESS; + + if (hw_control !=3D last_hw_control) + led_trigger_notify_hw_control_changed(&priv->kbd_bl.led, hw_control); +} + static void ideapad_kbd_bl_notify(struct ideapad_private *priv) { int hw_brightness, brightness; =20 - if (!priv->kbd_bl.initialized) + if (unlikely(priv->kbd_bl.initialized <=3D 0)) return; =20 guard(mutex)(&priv->kbd_bl.mutex); @@ -1739,6 +1798,8 @@ static void ideapad_kbd_bl_notify(struct ideapad_priv= ate *priv) if (priv->kbd_bl.last_hw_brightness =3D=3D hw_brightness) return; =20 + ideapad_kbd_bl_notify_hw_control(priv, hw_brightness); + priv->kbd_bl.last_hw_brightness =3D hw_brightness; =20 led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness); @@ -1751,9 +1812,11 @@ static int ideapad_kbd_bl_init(struct ideapad_privat= e *priv) if (!priv->features.kbd_bl) return -ENODEV; =20 - if (WARN_ON(priv->kbd_bl.initialized)) + if (WARN_ON(priv->kbd_bl.initialized > 0)) return -EEXIST; =20 + priv->kbd_bl.initialized =3D 0; + err =3D devm_mutex_init(&priv->platform_device->dev, &priv->kbd_bl.mutex); if (err) return err; @@ -1771,6 +1834,24 @@ static int ideapad_kbd_bl_init(struct ideapad_privat= e *priv) =20 switch (priv->kbd_bl.type) { case KBD_BL_TRISTATE_AUTO: + priv->kbd_bl.led.max_brightness =3D 2; + + if (!ideapad_kbd_bl_auto_trigger_registered) { + dev_warn(&priv->platform_device->dev, + "Cannot provide LED trigger %s for keyboard backlight\n", + ideapad_kbd_bl_auto_trigger.name); + break; + } + + priv->kbd_bl.led.flags |=3D LED_TRIG_HW_CHANGED; + priv->kbd_bl.led.trigger_type =3D &ideapad_kbd_bl_auto_trigger_type; + priv->kbd_bl.led.hw_control_trigger =3D ideapad_kbd_bl_auto_trigger.name; + + /* Hardware remembers the last brightness level, including auto mode. */ + if (hw_brightness =3D=3D KBD_BL_AUTO_MODE_HW_BRIGHTNESS) + priv->kbd_bl.led.default_trigger =3D ideapad_kbd_bl_auto_trigger.name; + + break; case KBD_BL_TRISTATE: priv->kbd_bl.led.max_brightness =3D 2; break; @@ -1791,17 +1872,17 @@ static int ideapad_kbd_bl_init(struct ideapad_priva= te *priv) if (err) return err; =20 - priv->kbd_bl.initialized =3D true; + priv->kbd_bl.initialized =3D 1; =20 return 0; } =20 static void ideapad_kbd_bl_exit(struct ideapad_private *priv) { - if (!priv->kbd_bl.initialized) + if (priv->kbd_bl.initialized <=3D 0) return; =20 - priv->kbd_bl.initialized =3D false; + priv->kbd_bl.initialized =3D -1; =20 led_classdev_unregister(&priv->kbd_bl.led); } @@ -2620,17 +2701,30 @@ static int __init ideapad_laptop_init(void) { int err; =20 + err =3D led_trigger_register(&ideapad_kbd_bl_auto_trigger); + if (err) { + pr_warn("Failed to register LED trigger %s: %d\n", + ideapad_kbd_bl_auto_trigger.name, err); + } else { + ideapad_kbd_bl_auto_trigger_registered =3D true; + } + err =3D ideapad_wmi_driver_register(); if (err) - return err; + goto err_ledtrig; =20 err =3D platform_driver_register(&ideapad_acpi_driver); - if (err) { - ideapad_wmi_driver_unregister(); - return err; - } + if (err) + goto err_wmi; =20 return 0; + +err_wmi: + ideapad_wmi_driver_unregister(); +err_ledtrig: + if (ideapad_kbd_bl_auto_trigger_registered) + led_trigger_unregister(&ideapad_kbd_bl_auto_trigger); + return err; } module_init(ideapad_laptop_init) =20 @@ -2638,6 +2732,9 @@ static void __exit ideapad_laptop_exit(void) { platform_driver_unregister(&ideapad_acpi_driver); ideapad_wmi_driver_unregister(); + + if (ideapad_kbd_bl_auto_trigger_registered) + led_trigger_unregister(&ideapad_kbd_bl_auto_trigger); } module_exit(ideapad_laptop_exit) =20 --=20 2.55.0