1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "device-util.h"
13 #include "main-func.h"
15 #include "parse-util.h"
16 #include "pretty-print.h"
17 #include "terminal-util.h"
18 #include "reboot-util.h"
19 #include "string-util.h"
23 static int help(void) {
24 _cleanup_free_
char *link
= NULL
;
27 r
= terminal_urlify_man("systemd-backlight", "8", &link
);
31 printf("%s save [backlight|leds]:DEVICE\n"
32 "%s load [backlight|leds]:DEVICE\n"
33 "\n%sSave and restore backlight brightness at shutdown and boot.%s\n\n"
34 " save Save current brightness\n"
35 " load Set brightness to be the previously saved value\n"
36 "\nSee the %s for details.\n"
37 , program_invocation_short_name
38 , program_invocation_short_name
39 , ansi_highlight(), ansi_normal()
46 static int find_pci_or_platform_parent(sd_device
*device
, sd_device
**ret
) {
47 const char *subsystem
, *sysname
, *value
;
54 r
= sd_device_get_parent(device
, &parent
);
58 r
= sd_device_get_subsystem(parent
, &subsystem
);
62 r
= sd_device_get_sysname(parent
, &sysname
);
66 if (streq(subsystem
, "drm")) {
69 c
= startswith(sysname
, "card");
73 c
+= strspn(c
, DIGITS
);
75 /* A connector DRM device, let's ignore all but LVDS and eDP! */
76 if (!STARTSWITH_SET(c
, "-LVDS-", "-Embedded DisplayPort-"))
80 } else if (streq(subsystem
, "pci") &&
81 sd_device_get_sysattr_value(parent
, "class", &value
) >= 0) {
82 unsigned long class = 0;
84 r
= safe_atolu(value
, &class);
86 return log_warning_errno(r
, "Cannot parse PCI class '%s' of device %s:%s: %m",
87 value
, subsystem
, sysname
);
90 if (class == 0x30000) {
95 } else if (streq(subsystem
, "platform")) {
100 return find_pci_or_platform_parent(parent
, ret
);
103 static int same_device(sd_device
*a
, sd_device
*b
) {
104 const char *a_val
, *b_val
;
110 r
= sd_device_get_subsystem(a
, &a_val
);
114 r
= sd_device_get_subsystem(b
, &b_val
);
118 if (!streq(a_val
, b_val
))
121 r
= sd_device_get_sysname(a
, &a_val
);
125 r
= sd_device_get_sysname(b
, &b_val
);
129 return streq(a_val
, b_val
);
132 static int validate_device(sd_device
*device
) {
133 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*enumerate
= NULL
;
134 const char *v
, *subsystem
;
135 sd_device
*parent
, *other
;
140 /* Verify whether we should actually care for a specific
141 * backlight device. For backlight devices there might be
142 * multiple ways to access the same control: "firmware"
143 * (i.e. ACPI), "platform" (i.e. via the machine's EC) and
144 * "raw" (via the graphics card). In general we should prefer
145 * "firmware" (i.e. ACPI) or "platform" access over "raw"
146 * access, in order not to confuse the BIOS/EC, and
147 * compatibility with possible low-level hotkey handling of
148 * screen brightness. The kernel will already make sure to
149 * expose only one of "firmware" and "platform" for the same
150 * device to userspace. However, we still need to make sure
151 * that we use "raw" only if no "firmware" or "platform"
152 * device for the same device exists. */
154 r
= sd_device_get_subsystem(device
, &subsystem
);
157 if (!streq(subsystem
, "backlight"))
160 r
= sd_device_get_sysattr_value(device
, "type", &v
);
163 if (!streq(v
, "raw"))
166 r
= find_pci_or_platform_parent(device
, &parent
);
170 r
= sd_device_get_subsystem(parent
, &subsystem
);
174 r
= sd_device_enumerator_new(&enumerate
);
178 r
= sd_device_enumerator_allow_uninitialized(enumerate
);
182 r
= sd_device_enumerator_add_match_subsystem(enumerate
, "backlight", true);
186 FOREACH_DEVICE(enumerate
, other
) {
187 const char *other_subsystem
;
188 sd_device
*other_parent
;
190 if (same_device(device
, other
) > 0)
193 if (sd_device_get_sysattr_value(other
, "type", &v
) < 0 ||
194 !STR_IN_SET(v
, "platform", "firmware"))
197 /* OK, so there's another backlight device, and it's a
198 * platform or firmware device, so, let's see if we
199 * can verify it belongs to the same device as ours. */
200 if (find_pci_or_platform_parent(other
, &other_parent
) < 0)
203 if (same_device(parent
, other_parent
)) {
204 const char *device_sysname
= NULL
, *other_sysname
= NULL
;
206 /* Both have the same PCI parent, that means we are out. */
208 (void) sd_device_get_sysname(device
, &device_sysname
);
209 (void) sd_device_get_sysname(other
, &other_sysname
);
211 log_debug("Skipping backlight device %s, since device %s is on same PCI device and takes precedence.",
212 device_sysname
, other_sysname
);
216 if (sd_device_get_subsystem(other_parent
, &other_subsystem
) < 0)
219 if (streq(other_subsystem
, "platform") && streq(subsystem
, "pci")) {
220 const char *device_sysname
= NULL
, *other_sysname
= NULL
;
222 /* The other is connected to the platform bus and we are a PCI device, that also means we are out. */
224 (void) sd_device_get_sysname(device
, &device_sysname
);
225 (void) sd_device_get_sysname(other
, &other_sysname
);
227 log_debug("Skipping backlight device %s, since device %s is a platform device and takes precedence.",
228 device_sysname
, other_sysname
);
236 static int get_max_brightness(sd_device
*device
, unsigned *ret
) {
237 const char *max_brightness_str
;
238 unsigned max_brightness
;
244 r
= sd_device_get_sysattr_value(device
, "max_brightness", &max_brightness_str
);
246 return log_device_warning_errno(device
, r
, "Failed to read 'max_brightness' attribute: %m");
248 r
= safe_atou(max_brightness_str
, &max_brightness
);
250 return log_device_warning_errno(device
, r
, "Failed to parse 'max_brightness' \"%s\": %m", max_brightness_str
);
252 if (max_brightness
<= 0)
253 return log_device_warning_errno(device
, SYNTHETIC_ERRNO(EINVAL
), "Maximum brightness is 0, ignoring device.");
255 *ret
= max_brightness
;
259 /* Some systems turn the backlight all the way off at the lowest levels.
260 * clamp_brightness clamps the saved brightness to at least 1 or 5% of
261 * max_brightness in case of 'backlight' subsystem. This avoids preserving
262 * an unreadably dim screen, which would otherwise force the user to
263 * disable state restoration. */
264 static int clamp_brightness(sd_device
*device
, char **value
, unsigned max_brightness
) {
265 unsigned brightness
, new_brightness
, min_brightness
;
266 const char *subsystem
;
272 r
= safe_atou(*value
, &brightness
);
274 return log_device_warning_errno(device
, r
, "Failed to parse brightness \"%s\": %m", *value
);
276 r
= sd_device_get_subsystem(device
, &subsystem
);
278 return log_device_warning_errno(device
, r
, "Failed to get device subsystem: %m");
280 if (streq(subsystem
, "backlight"))
281 min_brightness
= MAX(1U, max_brightness
/20);
285 new_brightness
= CLAMP(brightness
, min_brightness
, max_brightness
);
286 if (new_brightness
!= brightness
) {
289 r
= asprintf(&new_value
, "%u", new_brightness
);
293 log_device_info(device
, "Saved brightness %s %s to %s.", *value
,
294 new_brightness
> brightness
?
295 "too low; increasing" : "too high; decreasing",
298 free_and_replace(*value
, new_value
);
304 static bool shall_clamp(sd_device
*d
) {
310 r
= sd_device_get_property_value(d
, "ID_BACKLIGHT_CLAMP", &s
);
312 log_device_debug_errno(d
, r
, "Failed to get ID_BACKLIGHT_CLAMP property, ignoring: %m");
316 r
= parse_boolean(s
);
318 log_device_debug_errno(d
, r
, "Failed to parse ID_BACKLIGHT_CLAMP property, ignoring: %m");
325 static int read_brightness(sd_device
*device
, const char **ret
) {
326 const char *subsystem
;
332 r
= sd_device_get_subsystem(device
, &subsystem
);
334 return log_device_debug_errno(device
, r
, "Failed to get subsystem: %m");
336 if (streq(subsystem
, "backlight")) {
337 r
= sd_device_get_sysattr_value(device
, "actual_brightness", ret
);
341 return log_device_debug_errno(device
, r
, "Failed to read 'actual_brightness' attribute: %m");
343 log_device_debug_errno(device
, r
, "Failed to read 'actual_brightness' attribute, fall back to use 'brightness' attribute: %m");
346 r
= sd_device_get_sysattr_value(device
, "brightness", ret
);
348 return log_device_debug_errno(device
, r
, "Failed to read 'brightness' attribute: %m");
353 static int run(int argc
, char *argv
[]) {
354 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
355 _cleanup_free_
char *escaped_ss
= NULL
, *escaped_sysname
= NULL
, *escaped_path_id
= NULL
;
356 const char *sysname
, *path_id
, *ss
, *saved
;
357 unsigned max_brightness
;
362 if (strv_contains(strv_skip(argv
, 1), "--help"))
366 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program requires two arguments.");
370 r
= mkdir_p("/var/lib/systemd/backlight", 0755);
372 return log_error_errno(r
, "Failed to create backlight directory /var/lib/systemd/backlight: %m");
374 sysname
= strchr(argv
[2], ':');
376 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Requires a subsystem and sysname pair specifying a backlight device.");
378 ss
= strndupa(argv
[2], sysname
- argv
[2]);
382 if (!STR_IN_SET(ss
, "backlight", "leds"))
383 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a backlight or LED device: '%s:%s'", ss
, sysname
);
385 r
= sd_device_new_from_subsystem_sysname(&device
, ss
, sysname
);
387 return log_error_errno(r
, "Failed to get backlight or LED device '%s:%s': %m", ss
, sysname
);
389 /* If max_brightness is 0, then there is no actual backlight
390 * device. This happens on desktops with Asus mainboards
391 * that load the eeepc-wmi module. */
392 if (get_max_brightness(device
, &max_brightness
) < 0)
395 escaped_ss
= cescape(ss
);
399 escaped_sysname
= cescape(sysname
);
400 if (!escaped_sysname
)
403 if (sd_device_get_property_value(device
, "ID_PATH", &path_id
) >= 0) {
404 escaped_path_id
= cescape(path_id
);
405 if (!escaped_path_id
)
408 saved
= strjoina("/var/lib/systemd/backlight/", escaped_path_id
, ":", escaped_ss
, ":", escaped_sysname
);
410 saved
= strjoina("/var/lib/systemd/backlight/", escaped_ss
, ":", escaped_sysname
);
412 /* If there are multiple conflicting backlight devices, then
413 * their probing at boot-time might happen in any order. This
414 * means the validity checking of the device then is not
415 * reliable, since it might not see other devices conflicting
416 * with a specific backlight. To deal with this, we will
417 * actively delete backlight state files at shutdown (where
418 * device probing should be complete), so that the validity
419 * check at boot time doesn't have to be reliable. */
421 if (streq(argv
[1], "load")) {
422 _cleanup_free_
char *value
= NULL
;
425 if (shall_restore_state() == 0)
428 if (validate_device(device
) == 0)
431 clamp
= shall_clamp(device
);
433 r
= read_one_line_file(saved
, &value
);
434 if (IN_SET(r
, -ENOENT
, 0)) {
437 /* Fallback to clamping current brightness or exit early if
438 * clamping is not supported/enabled. */
442 r
= read_brightness(device
, &curval
);
444 return log_device_error_errno(device
, r
, "Failed to read current brightness: %m");
446 value
= strdup(curval
);
450 return log_error_errno(r
, "Failed to read %s: %m", saved
);
453 (void) clamp_brightness(device
, &value
, max_brightness
);
455 r
= sd_device_set_sysattr_value(device
, "brightness", value
);
457 return log_device_error_errno(device
, r
, "Failed to write system 'brightness' attribute: %m");
459 } else if (streq(argv
[1], "save")) {
462 if (validate_device(device
) == 0) {
463 (void) unlink(saved
);
467 r
= read_brightness(device
, &value
);
469 return log_device_error_errno(device
, r
, "Failed to read current brightness: %m");
471 r
= write_string_file(saved
, value
, WRITE_STRING_FILE_CREATE
);
473 return log_device_error_errno(device
, r
, "Failed to write %s: %m", saved
);
476 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown verb %s.", argv
[1]);
481 DEFINE_MAIN_FUNCTION(run
);