git: c45bdb39fe98 - main - rc: add a backlight service to save/restore backlight levels

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Wed, 02 Sep 2026 03:51:50 UTC
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=c45bdb39fe985b43afc63b7cbd989fe7a61673b7

commit c45bdb39fe985b43afc63b7cbd989fe7a61673b7
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-09-02 03:51:41 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-09-02 03:51:41 +0000

    rc: add a backlight service to save/restore backlight levels
    
    The default on my laptop is annoyingly bright, and this is a useful
    feature to mitigate that.  The backlight script is largely a copy of the
    mixer service which provides the same value for mixers, but this one is
    specifically dependant on kld to allow DRM drivers a chance to attach.
    
    Note that it's off by default to avoid interference with DEs, and
    document the capability in backlight(8).  Set backlight_enable=YES in
    rc.conf(5) to enable save/restore.
    
    Relnotes:       maybe
    Reviewed by:    bapt, ivy, manu, ziaee
    Differential Revision:  https://reviews.freebsd.org/D59296
---
 etc/mtree/BSD.var.dist        |  2 +
 libexec/rc/rc.conf            |  1 +
 libexec/rc/rc.d/Makefile      |  1 +
 libexec/rc/rc.d/backlight     | 88 +++++++++++++++++++++++++++++++++++++++++++
 share/man/man5/rc.conf.5      |  7 +++-
 usr.bin/backlight/backlight.8 | 11 +++++-
 6 files changed, 108 insertions(+), 2 deletions(-)

diff --git a/etc/mtree/BSD.var.dist b/etc/mtree/BSD.var.dist
index b55c0150848e..2594067d1b4e 100644
--- a/etc/mtree/BSD.var.dist
+++ b/etc/mtree/BSD.var.dist
@@ -37,6 +37,8 @@
     ..
 /set mode=0755
     db
+        backlight       tags=package=rc
+        ..
         entropy         uname=operator gname=operator mode=0700
         ..
         freebsd-update  mode=0700
diff --git a/libexec/rc/rc.conf b/libexec/rc/rc.conf
index 12da003978e8..aaeb65e92264 100644
--- a/libexec/rc/rc.conf
+++ b/libexec/rc/rc.conf
@@ -42,6 +42,7 @@ always_force_depends="NO"	# Set to check that indicated dependencies are
 apm_enable="NO"		# Set to YES to enable APM BIOS functions (or NO).
 apmd_enable="NO"	# Run apmd to handle APM event from userland.
 apmd_flags=""		# Flags to apmd (if enabled).
+backlight_enable="NO"	# Save/restore backlight levels
 ddb_enable="NO"		# Set to YES to load ddb scripts at boot.
 ddb_config="/etc/ddb.conf"	# ddb(8) config file.
 devd_enable="YES" 	# Run devd, to trigger programs on device tree changes.
diff --git a/libexec/rc/rc.d/Makefile b/libexec/rc/rc.d/Makefile
index f6646208dc03..a84f3e04381e 100644
--- a/libexec/rc/rc.d/Makefile
+++ b/libexec/rc/rc.d/Makefile
@@ -11,6 +11,7 @@ CONFS=	DAEMON \
 	NETWORKING \
 	SERVERS \
 	adjkerntz \
+	backlight \
 	bgfsck \
 	bridge \
 	cfumass \
diff --git a/libexec/rc/rc.d/backlight b/libexec/rc/rc.d/backlight
new file mode 100755
index 000000000000..6833f7b92aff
--- /dev/null
+++ b/libexec/rc/rc.d/backlight
@@ -0,0 +1,88 @@
+#!/bin/sh -
+#
+# Copyright (c) 2026 Kyle Evans <kevans@FreeBSD.org>
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+
+# PROVIDE: backlight
+# REQUIRE: FILESYSTEMS kld
+# KEYWORD: nojail shutdown
+
+. /etc/rc.subr
+
+name="backlight"
+desc="Save and restore backlight values"
+rcvar="backlight_enable"
+stop_cmd="backlight_stop"
+start_cmd="backlight_start"
+reload_cmd="backlight_start"
+extra_commands="reload"
+
+backlightdb="/var/db/backlight"
+
+#
+# List current backlight devices to stdout.
+#
+list_backlights()
+{
+	( cd /dev/backlight ; ls backlight* 2>/dev/null )
+}
+
+#
+# Save state of an individual backlight specified as $1
+#
+backlight_save()
+{
+	local dev
+
+	dev="/dev/backlight/$1"
+	if [ -r "$dev" ]; then
+		/usr/bin/backlight -f "$dev" -q > "$backlightdb/$1-state" 2>/dev/null
+	fi
+}
+
+#
+# Restore the state of an individual backlight specified as $1
+#
+backlight_restore()
+{
+	local file dev
+
+	dev="/dev/backlight/$1"
+	file="$backlightdb/$1-state"
+	if [ -r "$dev" -a -r "$file" ]; then
+		/usr/bin/backlight -f "$dev" $(cat "$file") > /dev/null
+	fi
+}
+
+#
+# Restore state of all backlights
+#
+backlight_start()
+{
+	local backlight
+
+	for backlight in $(list_backlights); do
+		backlight_restore "$backlight"
+	done
+}
+
+#
+# Save the state of all backlights
+#
+backlight_stop()
+{
+	local backlight
+
+	for backlight in $(list_backlights); do
+		backlight_save "$backlight"
+	done
+}
+
+load_rc_config $name
+
+# doesn't make sense to run in a svcj: config setting
+backlight_svcj="NO"
+
+run_rc_command "$1"
diff --git a/share/man/man5/rc.conf.5 b/share/man/man5/rc.conf.5
index 586e33980fd8..4db1586f2fcc 100644
--- a/share/man/man5/rc.conf.5
+++ b/share/man/man5/rc.conf.5
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd August 30, 2026
+.Dd September 1, 2026
 .Dt RC.CONF 5
 .Os
 .Sh NAME
@@ -4433,6 +4433,11 @@ For example:
 autobridge_interfaces="bridge0"
 autobridge_bridge0="tap* dc0 vlan[345]"
 .Ed
+.It Va backlight_enable
+.Pq Vt bool
+If set to
+.Dq Li YES ,
+enable saving and restoring backlight levels at shutdown and boot time.
 .It Va mixer_enable
 .Pq Vt bool
 If set to
diff --git a/usr.bin/backlight/backlight.8 b/usr.bin/backlight/backlight.8
index 291d1628d5ea..1800446a341e 100644
--- a/usr.bin/backlight/backlight.8
+++ b/usr.bin/backlight/backlight.8
@@ -20,7 +20,7 @@
 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd July 19, 2022
+.Dd September 1, 2026
 .Dt BACKLIGHT 8
 .Os
 .Sh NAME
@@ -79,6 +79,15 @@ If no value is specified a default of 10 percent is used.
 Decrement the backlight level.
 If no value is specified a default of 10 percent is used.
 .El
+.Pp
+Brightness levels may not be preserved across a reboot unless
+.Va backlight_enable
+is set to
+.Dq Li YES
+in
+.Xr rc.conf 5 .
+This is disabled by default to avoid intefering with desktop environments that
+do their own backlight management.
 .Sh EXAMPLES
 Show the current brightness level:
 .Bd -literal -offset indent