diff options
Diffstat (limited to 'src/backlight')
-rw-r--r-- | src/backlight/backlight.c | 417 |
1 files changed, 237 insertions, 180 deletions
diff --git a/src/backlight/backlight.c b/src/backlight/backlight.c index b2032ad..2de6c20 100644 --- a/src/backlight/backlight.c +++ b/src/backlight/backlight.c @@ -11,7 +11,6 @@ #include "escape.h" #include "fileio.h" #include "main-func.h" -#include "mkdir.h" #include "parse-util.h" #include "percent-util.h" #include "pretty-print.h" @@ -20,6 +19,7 @@ #include "string-util.h" #include "strv.h" #include "terminal-util.h" +#include "verbs.h" #define PCI_CLASS_GRAPHICS_CARD 0x30000 @@ -91,8 +91,8 @@ static int has_multiple_graphics_cards(void) { } static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) { - const char *subsystem, *sysname, *value; sd_device *parent; + const char *s; int r; assert(device); @@ -102,34 +102,29 @@ static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) { if (r < 0) return r; - r = sd_device_get_subsystem(parent, &subsystem); - if (r < 0) - return r; - - r = sd_device_get_sysname(parent, &sysname); - if (r < 0) - return r; + if (device_in_subsystem(parent, "drm")) { - if (streq(subsystem, "drm")) { - const char *c; + r = sd_device_get_sysname(parent, &s); + if (r < 0) + return r; - c = startswith(sysname, "card"); - if (!c) + s = startswith(s, "card"); + if (!s) return -ENODATA; - c += strspn(c, DIGITS); - if (*c == '-' && !STARTSWITH_SET(c, "-LVDS-", "-Embedded DisplayPort-", "-eDP-")) + s += strspn(s, DIGITS); + if (*s == '-' && !STARTSWITH_SET(s, "-LVDS-", "-Embedded DisplayPort-", "-eDP-")) /* A connector DRM device, let's ignore all but LVDS and eDP! */ return -EOPNOTSUPP; - } else if (streq(subsystem, "pci") && - sd_device_get_sysattr_value(parent, "class", &value) >= 0) { + } else if (device_in_subsystem(parent, "pci") && + sd_device_get_sysattr_value(parent, "class", &s)) { + unsigned long class; - r = safe_atolu(value, &class); + r = safe_atolu(s, &class); if (r < 0) - return log_warning_errno(r, "Cannot parse PCI class '%s' of device %s:%s: %m", - value, subsystem, sysname); + return log_device_warning_errno(parent, r, "Cannot parse PCI class '%s': %m", s); /* Graphics card */ if (class == PCI_CLASS_GRAPHICS_CARD) { @@ -137,7 +132,7 @@ static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) { return 0; } - } else if (streq(subsystem, "platform")) { + } else if (device_in_subsystem(parent, "platform")) { *ret = parent; return 0; } @@ -176,7 +171,7 @@ static int same_device(sd_device *a, sd_device *b) { static int validate_device(sd_device *device) { _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *enumerate = NULL; - const char *v, *sysname, *subsystem; + const char *v, *sysname; sd_device *parent; int r; @@ -184,7 +179,7 @@ static int validate_device(sd_device *device) { /* Verify whether we should actually care for a specific backlight device. For backlight devices * there might be multiple ways to access the same control: "firmware" (i.e. ACPI), "platform" - * (i.e. via the machine's EC) and "raw" (via the graphics card). In general we should prefer + * (i.e. via the machine's EC), and "raw" (via the graphics card). In general we should prefer * "firmware" (i.e. ACPI) or "platform" access over "raw" access, in order not to confuse the * BIOS/EC, and compatibility with possible low-level hotkey handling of screen brightness. The * kernel will already make sure to expose only one of "firmware" and "platform" for the same @@ -195,11 +190,8 @@ static int validate_device(sd_device *device) { if (r < 0) return log_device_debug_errno(device, r, "Failed to get sysname: %m"); - r = sd_device_get_subsystem(device, &subsystem); - if (r < 0) - return log_device_debug_errno(device, r, "Failed to get subsystem: %m"); - if (!streq(subsystem, "backlight")) - return true; + if (!device_in_subsystem(device, "backlight")) + return true; /* We assume LED device is always valid. */ r = sd_device_get_sysattr_value(device, "type", &v); if (r < 0) @@ -211,15 +203,12 @@ static int validate_device(sd_device *device) { if (r < 0) return log_device_debug_errno(device, r, "Failed to find PCI or platform parent: %m"); - r = sd_device_get_subsystem(parent, &subsystem); - if (r < 0) - return log_device_debug_errno(parent, r, "Failed to get subsystem: %m"); - if (DEBUG_LOGGING) { - const char *s = NULL; + const char *s = NULL, *subsystem = NULL; (void) sd_device_get_syspath(parent, &s); - log_device_debug(device, "Found %s parent device: %s", subsystem, strna(s)); + (void) sd_device_get_subsystem(parent, &subsystem); + log_device_debug(device, "Found %s parent device: %s", strna(subsystem), strna(s)); } r = sd_device_enumerator_new(&enumerate); @@ -246,7 +235,7 @@ static int validate_device(sd_device *device) { if (r < 0) return log_debug_errno(r, "Failed to add sysattr match: %m"); - if (streq(subsystem, "pci")) { + if (device_in_subsystem(parent, "pci")) { r = has_multiple_graphics_cards(); if (r < 0) return log_debug_errno(r, "Failed to check if the system has multiple graphics cards: %m"); @@ -254,8 +243,8 @@ static int validate_device(sd_device *device) { /* If the system has multiple graphics cards, then we cannot associate platform * devices on non-PCI bus (especially WMI bus) with PCI devices. Let's ignore all * backlight devices that do not have the same parent PCI device. */ - log_debug("Found multiple graphics cards on PCI bus. " - "Skipping to associate platform backlight devices on non-PCI bus."); + log_debug("Found multiple graphics cards on PCI bus; " + "skipping deduplication of platform backlight devices not on PCI bus."); r = sd_device_enumerator_add_match_parent(enumerate, parent); if (r < 0) @@ -264,7 +253,6 @@ static int validate_device(sd_device *device) { } FOREACH_DEVICE(enumerate, other) { - const char *other_subsystem; sd_device *other_parent; /* OK, so there's another backlight device, and it's a platform or firmware device. @@ -289,13 +277,7 @@ static int validate_device(sd_device *device) { return false; } - r = sd_device_get_subsystem(other_parent, &other_subsystem); - if (r < 0) { - log_device_debug_errno(other_parent, r, "Failed to get subsystem, ignoring: %m"); - continue; - } - - if (streq(other_subsystem, "platform") && streq(subsystem, "pci")) { + if (device_in_subsystem(other_parent, "platform") && device_in_subsystem(parent, "pci")) { /* The other is connected to the platform bus and we are a PCI device, that also means we are out. */ if (DEBUG_LOGGING) { const char *other_sysname = NULL, *other_type = NULL; @@ -313,7 +295,8 @@ static int validate_device(sd_device *device) { return true; } -static int get_max_brightness(sd_device *device, unsigned *ret) { +static int read_max_brightness(sd_device *device, unsigned *ret) { + unsigned max_brightness; const char *s; int r; @@ -324,11 +307,22 @@ static int get_max_brightness(sd_device *device, unsigned *ret) { if (r < 0) return log_device_warning_errno(device, r, "Failed to read 'max_brightness' attribute: %m"); - r = safe_atou(s, ret); + r = safe_atou(s, &max_brightness); if (r < 0) return log_device_warning_errno(device, r, "Failed to parse 'max_brightness' \"%s\": %m", s); - return 0; + /* If max_brightness is 0, then there is no actual backlight device. This happens on desktops + * with Asus mainboards that load the eeepc-wmi module. */ + if (max_brightness == 0) { + log_device_warning(device, "Maximum brightness is 0, ignoring device."); + *ret = 0; + return 0; + } + + log_device_debug(device, "Maximum brightness is %u", max_brightness); + + *ret = max_brightness; + return 1; /* valid max brightness */ } static int clamp_brightness( @@ -339,8 +333,6 @@ static int clamp_brightness( unsigned *brightness) { unsigned new_brightness, min_brightness; - const char *subsystem; - int r; assert(device); assert(brightness); @@ -350,14 +342,9 @@ static int clamp_brightness( * avoids preserving an unreadably dim screen, which would otherwise force the user to disable * state restoration. */ - r = sd_device_get_subsystem(device, &subsystem); - if (r < 0) - return log_device_warning_errno(device, r, "Failed to get device subsystem: %m"); - - if (streq(subsystem, "backlight")) - min_brightness = MAX(1U, (unsigned) ((double) max_brightness * percent / 100)); - else - min_brightness = 0; + min_brightness = (unsigned) ((double) max_brightness * percent / 100); + if (device_in_subsystem(device, "backlight")) + min_brightness = MAX(1U, min_brightness); new_brightness = CLAMP(*brightness, min_brightness, max_brightness); if (new_brightness != *brightness) @@ -372,19 +359,28 @@ static int clamp_brightness( return 0; } -static bool shall_clamp(sd_device *d, unsigned *ret) { - const char *s; +static bool shall_clamp(sd_device *device, unsigned *ret) { + const char *property, *s; + unsigned default_percent; int r; - assert(d); + assert(device); assert(ret); - r = sd_device_get_property_value(d, "ID_BACKLIGHT_CLAMP", &s); + if (device_in_subsystem(device, "backlight")) { + property = "ID_BACKLIGHT_CLAMP"; + default_percent = 5; + } else { + property = "ID_LEDS_CLAMP"; + default_percent = 0; + } + + r = sd_device_get_property_value(device, property, &s); if (r < 0) { if (r != -ENOENT) - log_device_debug_errno(d, r, "Failed to get ID_BACKLIGHT_CLAMP property, ignoring: %m"); - *ret = 5; /* defaults to 5% */ - return true; + log_device_debug_errno(device, r, "Failed to get %s property, ignoring: %m", property); + *ret = default_percent; + return default_percent > 0; } r = parse_boolean(s); @@ -395,9 +391,9 @@ static bool shall_clamp(sd_device *d, unsigned *ret) { r = parse_percent(s); if (r < 0) { - log_device_debug_errno(d, r, "Failed to parse ID_BACKLIGHT_CLAMP property, ignoring: %m"); - *ret = 5; - return true; + log_device_debug_errno(device, r, "Failed to parse %s property, ignoring: %m", property); + *ret = default_percent; + return default_percent > 0; } *ret = r; @@ -405,18 +401,14 @@ static bool shall_clamp(sd_device *d, unsigned *ret) { } static int read_brightness(sd_device *device, unsigned max_brightness, unsigned *ret_brightness) { - const char *subsystem, *value; + const char *value; unsigned brightness; int r; assert(device); assert(ret_brightness); - r = sd_device_get_subsystem(device, &subsystem); - if (r < 0) - return log_device_debug_errno(device, r, "Failed to get subsystem: %m"); - - if (streq(subsystem, "backlight")) { + if (device_in_subsystem(device, "backlight")) { r = sd_device_get_sysattr_value(device, "actual_brightness", &value); if (r == -ENOENT) { log_device_debug_errno(device, r, "Failed to read 'actual_brightness' attribute, " @@ -463,154 +455,219 @@ use_brightness: return 0; } -static int run(int argc, char *argv[]) { - _cleanup_(sd_device_unrefp) sd_device *device = NULL; - _cleanup_free_ char *escaped_ss = NULL, *escaped_sysname = NULL, *escaped_path_id = NULL; - const char *sysname, *path_id, *ss, *saved; - unsigned max_brightness, brightness; +static int build_save_file_path(sd_device *device, char **ret) { + _cleanup_free_ char *escaped_subsystem = NULL, *escaped_sysname = NULL, *path = NULL; + const char *s; int r; - log_setup(); + assert(device); + assert(ret); - if (argv_looks_like_help(argc, argv)) - return help(); + r = sd_device_get_subsystem(device, &s); + if (r < 0) + return log_device_error_errno(device, r, "Failed to get subsystem: %m"); - if (argc != 3) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires two arguments."); + escaped_subsystem = cescape(s); + if (!escaped_subsystem) + return log_oom(); - if (!STR_IN_SET(argv[1], "load", "save")) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown verb %s.", argv[1]); + r = sd_device_get_sysname(device, &s); + if (r < 0) + return log_device_error_errno(device, r, "Failed to get sysname: %m"); - umask(0022); + escaped_sysname = cescape(s); + if (!escaped_sysname) + return log_oom(); - r = mkdir_p("/var/lib/systemd/backlight", 0755); - if (r < 0) - return log_error_errno(r, "Failed to create backlight directory /var/lib/systemd/backlight: %m"); + if (sd_device_get_property_value(device, "ID_PATH", &s) >= 0) { + _cleanup_free_ char *escaped_path_id = cescape(s); + if (!escaped_path_id) + return log_oom(); - sysname = strchr(argv[2], ':'); - if (!sysname) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Requires a subsystem and sysname pair specifying a backlight device."); + path = strjoin("/var/lib/systemd/backlight/", escaped_path_id, ":", escaped_subsystem, ":", escaped_sysname); + } else + path = strjoin("/var/lib/systemd/backlight/", escaped_subsystem, ":", escaped_sysname); + if (!path) + return log_oom(); - ss = strndupa_safe(argv[2], sysname - argv[2]); + *ret = TAKE_PTR(path); + return 0; +} - sysname++; +static int read_saved_brightness(sd_device *device, unsigned *ret) { + _cleanup_free_ char *path = NULL, *value = NULL; + int r; - if (!STR_IN_SET(ss, "backlight", "leds")) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a backlight or LED device: '%s:%s'", ss, sysname); + assert(device); + assert(ret); + + r = build_save_file_path(device, &path); + if (r < 0) + return r; - r = sd_device_new_from_subsystem_sysname(&device, ss, sysname); + r = read_one_line_file(path, &value); if (r < 0) { - bool ignore = r == -ENODEV; + if (r != -ENOENT) + log_device_error_errno(device, r, "Failed to read %s: %m", path); + return r; + } - /* Some drivers, e.g. for AMD GPU, removes acpi backlight device soon after it is added. - * See issue #21997. */ - log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r, - "Failed to get backlight or LED device '%s:%s'%s: %m", - ss, sysname, ignore ? ", ignoring" : ""); - return ignore ? 0 : r; + r = safe_atou(value, ret); + if (r < 0) { + log_device_warning_errno(device, r, + "Failed to parse saved brightness '%s', removing %s.", + value, path); + (void) unlink(path); + return r; } - /* If max_brightness is 0, then there is no actual backlight device. This happens on desktops - * with Asus mainboards that load the eeepc-wmi module. */ - if (get_max_brightness(device, &max_brightness) < 0) - return 0; + log_device_debug(device, "Using saved brightness %u.", *ret); + return 0; +} - if (max_brightness == 0) { - log_device_warning(device, "Maximum brightness is 0, ignoring device."); - return 0; - } +static int device_new_from_arg(const char *s, sd_device **ret) { + _cleanup_(sd_device_unrefp) sd_device *device = NULL; + _cleanup_free_ char *subsystem = NULL; + const char *sysname; + int r; - log_device_debug(device, "Maximum brightness is %u", max_brightness); + assert(s); + assert(ret); - escaped_ss = cescape(ss); - if (!escaped_ss) - return log_oom(); + sysname = strchr(s, ':'); + if (!sysname) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Requires a subsystem and sysname pair specifying a backlight or LED device."); - escaped_sysname = cescape(sysname); - if (!escaped_sysname) + subsystem = strndup(s, sysname - s); + if (!subsystem) return log_oom(); - if (sd_device_get_property_value(device, "ID_PATH", &path_id) >= 0) { - escaped_path_id = cescape(path_id); - if (!escaped_path_id) - return log_oom(); + sysname++; - saved = strjoina("/var/lib/systemd/backlight/", escaped_path_id, ":", escaped_ss, ":", escaped_sysname); - } else - saved = strjoina("/var/lib/systemd/backlight/", escaped_ss, ":", escaped_sysname); + if (!STR_IN_SET(subsystem, "backlight", "leds")) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Not a backlight or LED device: '%s:%s'", + subsystem, sysname); - /* If there are multiple conflicting backlight devices, then their probing at boot-time might - * happen in any order. This means the validity checking of the device then is not reliable, - * since it might not see other devices conflicting with a specific backlight. To deal with - * this, we will actively delete backlight state files at shutdown (where device probing should - * be complete), so that the validity check at boot time doesn't have to be reliable. */ + r = sd_device_new_from_subsystem_sysname(&device, subsystem, sysname); + if (r == -ENODEV) { + /* Some drivers, e.g. for AMD GPU, removes acpi backlight device soon after it is added. + * See issue #21997. */ + log_debug_errno(r, "Failed to get backlight or LED device '%s:%s', ignoring: %m", subsystem, sysname); + *ret = NULL; + return 0; + } + if (r < 0) + return log_error_errno(r, "Failed to get backlight or LED device '%s:%s': %m", subsystem, sysname); - if (streq(argv[1], "load")) { - _cleanup_free_ char *value = NULL; - unsigned percent; - bool clamp; + *ret = TAKE_PTR(device); + return 1; /* Found. */ +} - if (!shall_restore_state()) - return 0; +static int verb_load(int argc, char *argv[], void *userdata) { + _cleanup_(sd_device_unrefp) sd_device *device = NULL; + unsigned max_brightness, brightness, percent; + bool clamp; + int r; - if (validate_device(device) == 0) - return 0; + assert(argc == 2); - clamp = shall_clamp(device, &percent); + if (!shall_restore_state()) + return 0; - r = read_one_line_file(saved, &value); - if (r < 0 && r != -ENOENT) - return log_error_errno(r, "Failed to read %s: %m", saved); - if (r > 0) { - r = safe_atou(value, &brightness); - if (r < 0) { - log_warning_errno(r, "Failed to parse saved brightness '%s', removing %s.", - value, saved); - (void) unlink(saved); - } else { - log_debug("Using saved brightness %u.", brightness); - if (clamp) - (void) clamp_brightness(device, percent, /* saved = */ true, max_brightness, &brightness); - - /* Do not fall back to read current brightness below. */ - r = 1; - } - } - if (r <= 0) { - /* Fallback to clamping current brightness or exit early if clamping is not - * supported/enabled. */ - if (!clamp) - return 0; + r = device_new_from_arg(argv[1], &device); + if (r <= 0) + return r; - r = read_brightness(device, max_brightness, &brightness); - if (r < 0) - return log_device_error_errno(device, r, "Failed to read current brightness: %m"); + r = read_max_brightness(device, &max_brightness); + if (r <= 0) + return r; - (void) clamp_brightness(device, percent, /* saved = */ false, max_brightness, &brightness); - } + /* Ignore any errors in validation, and use the device as is. */ + if (validate_device(device) == 0) + return 0; - r = sd_device_set_sysattr_valuef(device, "brightness", "%u", brightness); - if (r < 0) - return log_device_error_errno(device, r, "Failed to write system 'brightness' attribute: %m"); + clamp = shall_clamp(device, &percent); - } else if (streq(argv[1], "save")) { - if (validate_device(device) == 0) { - (void) unlink(saved); + r = read_saved_brightness(device, &brightness); + if (r < 0) { + /* Fallback to clamping current brightness or exit early if clamping is not + * supported/enabled. */ + if (!clamp) return 0; - } r = read_brightness(device, max_brightness, &brightness); if (r < 0) return log_device_error_errno(device, r, "Failed to read current brightness: %m"); - r = write_string_filef(saved, WRITE_STRING_FILE_CREATE, "%u", brightness); - if (r < 0) - return log_device_error_errno(device, r, "Failed to write %s: %m", saved); + (void) clamp_brightness(device, percent, /* saved = */ false, max_brightness, &brightness); + } else if (clamp) + (void) clamp_brightness(device, percent, /* saved = */ true, max_brightness, &brightness); - } else - assert_not_reached(); + r = sd_device_set_sysattr_valuef(device, "brightness", "%u", brightness); + if (r < 0) + return log_device_error_errno(device, r, "Failed to write system 'brightness' attribute: %m"); + + return 0; +} + +static int verb_save(int argc, char *argv[], void *userdata) { + _cleanup_(sd_device_unrefp) sd_device *device = NULL; + _cleanup_free_ char *path = NULL; + unsigned max_brightness, brightness; + int r; + + assert(argc == 2); + + r = device_new_from_arg(argv[1], &device); + if (r <= 0) + return r; + + r = read_max_brightness(device, &max_brightness); + if (r <= 0) + return r; + + r = build_save_file_path(device, &path); + if (r < 0) + return r; + + /* If there are multiple conflicting backlight devices, then their probing at boot-time might + * happen in any order. This means the validity checking of the device then is not reliable, + * since it might not see other devices conflicting with a specific backlight. To deal with + * this, we will actively delete backlight state files at shutdown (where device probing should + * be complete), so that the validity check at boot time doesn't have to be reliable. */ + if (validate_device(device) == 0) { + (void) unlink(path); + return 0; + } + + r = read_brightness(device, max_brightness, &brightness); + if (r < 0) + return log_device_error_errno(device, r, "Failed to read current brightness: %m"); + + r = write_string_filef(path, WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_MKDIR_0755, "%u", brightness); + if (r < 0) + return log_device_error_errno(device, r, "Failed to write %s: %m", path); return 0; } +static int run(int argc, char *argv[]) { + static const Verb verbs[] = { + { "load", 2, 2, VERB_ONLINE_ONLY, verb_load }, + { "save", 2, 2, VERB_ONLINE_ONLY, verb_save }, + {} + }; + + log_setup(); + + if (argv_looks_like_help(argc, argv)) + return help(); + + umask(0022); + + return dispatch_verb(argc, argv, verbs, NULL); +} + DEFINE_MAIN_FUNCTION(run); |