You are not logged in.

#1 2012-11-16 01:44:47

tyler.heck
Member
Registered: 2012-01-28
Posts: 15

Brightness control at startup with systemd

I finally upgraded to systemd (I know... a bit later than I should have), and the transistion has been smooth, except for enabling brightness controll at boot.

I currently have a bit of a hack working to enable brightness control on my Dell XPS 13 ultrabook. I have two backlight interfaces (/sys/class/backlight/intel_backlight and /sys/class/backlight/acpi_video0), and I have found that the only way to alter my screen brightness is to set the intel_backlight brightness value to 0 and then alter the brightness file under the acpi_video0 interface. I must always write the 0 to the intel_backlight/brightness file, even if it already contains 0, to enable brightness control through acpi_video0.

I had had this code in my rc.local previously, and it worked exactly as I needed to enable brightness control:

#!/bin/bash

# set intel interface to 0, allowing acpi interface to take precedence
echo 0 > /sys/class/backlight/intel_backlight/brightness

# set the permissions of the acpi brightness file for video group (me) regular user modification
chgrp video /sys/class/backlight/acpi_video0/brightness
chmod 664 /sys/class/backlight/acpi_video0/brightness

# initialize brightness to a low level
echo 3 > /sys/class/backlight/acpi_video0/brightness

exit 0

Currently when I run this script normally it grants me the brightness control I desire through the modification of /sys/class/backlight/acpi_video0/brightness. I have made this script into a systemd service initiated at startup, with the service file below:

[Unit]
Description=Brightness Control Initialization

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/bright_init
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

My issue is that although the service is initiated at startup without error, my brightness control does not function like it does when I run the script manually myself. I have verified that systemd considers the service to have run successfully after startup, and I have also enabled both the multi-user.target and above service.

My questions are twofold:
1. Is there a better way to allow brightness control other than the somewhat hacky method of zeroing the /sys/class/backlight/intel_backlight/brightness file with the above script?
2. And does anyone have an idea as to why my service doesn't work, even though I can run the script after startup and it functions as I desire?

Last edited by tyler.heck (2012-11-16 01:45:46)

Offline

#2 2012-11-16 01:49:02

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,412

Re: Brightness control at startup with systemd

I use a udev rule.  Though I was also able to get it working with tmpfiles.d.  Tmpfiles.d would fail to work about 1 in 10 times though, which is why I started using a udev rule.  See the udev wiki page for a link to a good article on how to write udev rules.  The article is old so the command in the examples has changed to udevadm, but the rest of the info is still valid.

Offline

#3 2012-11-16 02:40:01

bzhb
Member
Registered: 2010-11-17
Posts: 32

Re: Brightness control at startup with systemd

I had the same problem. I think it is because systemd starts everything in parallel, something related to acpi must not be ready when the script is called. I delayed the execution of the script with
sleep 0.3
at the beginning. It is a bit ugly but it works.

Offline

#4 2012-11-16 02:43:24

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,412

Re: Brightness control at startup with systemd

Instead of using hacks, here is my udev rule.  Of course yours may be different with different hardware.  It is saved as /etc/udev/rules.d/44-backlight.rules

ACTION=="add", KERNEL=="acpi_video0", SUBSYSTEM=="backlight", SUBSYSTEMS=="pci", DRIVERS=="i915", ATTR{brightness}="2"

Use this to get the neceeary parameters:

$ udevadm info -a -p /sys/class/backlight/acpi_video0 

I know this is probably considered hand holding, but it pains me to see you doing it the way you are.

Offline

#5 2012-11-16 04:04:54

tyler.heck
Member
Registered: 2012-01-28
Posts: 15

Re: Brightness control at startup with systemd

So the udev rule works great during the boot process itself. Thanks for posting the helpful hints, this has been my first encounter with writing udev rules and your example was extremely helpful. Sadly when the boot process switches from the boot log over to the plain vanilla Arch console login the brightness is reverted to maximum. The cruddy intel_backlight brightness needs to be set to 0 (again! udev did once when the intel_backlight was added) before the acpi_video0 settings regain effect.

Any tips on an ACTION type other than "add" that will help me reset the intel_backlight brightness again when control is moved from startup to login? I would guess that the act of setting that brightness to 0 triggers some event, although I'm at a loss of figuring out exactly what mechanism is activated when I set the intel_backlight brightness to 0.

Offline

#6 2012-11-16 04:16:47

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,412

Re: Brightness control at startup with systemd

Add a udev rule to the file that sets intel_backlight to 0 and use ACTION=="add|change"

Offline

#7 2012-11-16 04:31:30

tyler.heck
Member
Registered: 2012-01-28
Posts: 15

Re: Brightness control at startup with systemd

Updating the udev rule for the intel_backlight from ACTION=="add" to "add|change" didn't stop the brightness from reverting at the switch from boot to login screen. The intel_backlight isn't actually changing on the switch to login. The intel_backlight remains 0 during the switch, it's the actual act of writing that causes brightness to work the second time, the 0 is already there.

I have found a patch in AUR for the Dell that I may try: https://aur.archlinux.org/packages.php?ID=60736

EDIT: Maybe not... I don't know how I feel about installing the kernel from AUR.

Last edited by tyler.heck (2012-11-16 04:38:33)

Offline

#8 2013-03-01 15:27:15

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: Brightness control at startup with systemd

It is interesting to see that this is working for you. I had to add OWNER="root" to the rule. Otherwise when testing the rule with `udevadm test` I always recieved a permission denied error. But even with OWNER="root" Udev did not actually write the ATTR{brightness}="2" into the file.

Manually doing that by echo 2 > ... works well. I searched a bit on this topic and found a thread on the Gentoo forum where they say that it is not wise to use Udev for writing anything to /sys. Instead you should use Udev to run a script that does that:

[orschiro@thinkpad ~]$ cat /etc/udev/rules.d/50-powersave.rules 
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/home/orschiro/Scripts/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/home/orschiro/Scripts/powersaving AC"

[orschiro@thinkpad ~]$ cat /usr/local/bin/powersaving 
#!/bin/sh

case $1 in
    battery)
		echo "Running powersaving on AC in 3 seconds"
		sleep 3
		# screen power saving"
		echo 200000 > /sys/class/backlight/intel_backlight/brightness
    ;;
    AC)
		echo "Running powersaving on battery in 3 seconds"
	    	sleep 3
		# screen power saving
		echo 4270725 > /sys/class/backlight/intel_backlight/brightness
    ;;
esac

Last edited by orschiro (2013-03-01 15:29:02)

Offline

#9 2013-03-01 16:39:59

elkoraco
Member
Registered: 2013-02-18
Posts: 122

Re: Brightness control at startup with systemd

Why don't you just try to boot with the kernel parameter acpi_video=backlight
I mean, It Works For Me, the parameter lets some subsystem (pm-utils? kernel acpi routines? the video driver? don't know, don't care) handle the backlight, so I can use the Fn keys and it goes to 50 percent on battery.

Offline

#10 2013-03-02 08:58:16

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: Brightness control at startup with systemd

The point here was not to use Fn keys for manual brightness change but possibilities to control brightness automatically with Udev rules or custom scripts.

Offline

Board footer

Powered by FluxBB