]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/backlight/backlight.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
10 #include "parse-util.h"
11 #include "proc-cmdline.h"
12 #include "string-util.h"
13 #include "udev-util.h"
16 static struct udev_device
*find_pci_or_platform_parent(struct udev_device
*device
) {
17 struct udev_device
*parent
;
18 const char *subsystem
, *sysname
;
22 parent
= udev_device_get_parent(device
);
26 subsystem
= udev_device_get_subsystem(parent
);
30 sysname
= udev_device_get_sysname(parent
);
34 if (streq(subsystem
, "drm")) {
37 c
= startswith(sysname
, "card");
41 c
+= strspn(c
, DIGITS
);
43 /* A connector DRM device, let's ignore all but LVDS and eDP! */
45 if (!startswith(c
, "-LVDS-") &&
46 !startswith(c
, "-Embedded DisplayPort-"))
50 } else if (streq(subsystem
, "pci")) {
53 value
= udev_device_get_sysattr_value(parent
, "class");
55 unsigned long class = 0;
57 if (safe_atolu(value
, &class) < 0) {
58 log_warning("Cannot parse PCI class %s of device %s:%s.",
59 value
, subsystem
, sysname
);
68 } else if (streq(subsystem
, "platform"))
71 return find_pci_or_platform_parent(parent
);
74 static bool same_device(struct udev_device
*a
, struct udev_device
*b
) {
78 if (!streq_ptr(udev_device_get_subsystem(a
), udev_device_get_subsystem(b
)))
81 if (!streq_ptr(udev_device_get_sysname(a
), udev_device_get_sysname(b
)))
87 static bool validate_device(struct udev
*udev
, struct udev_device
*device
) {
88 _cleanup_(udev_enumerate_unrefp
) struct udev_enumerate
*enumerate
= NULL
;
89 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
90 struct udev_device
*parent
;
91 const char *v
, *subsystem
;
97 /* Verify whether we should actually care for a specific
98 * backlight device. For backlight devices there might be
99 * multiple ways to access the same control: "firmware"
100 * (i.e. ACPI), "platform" (i.e. via the machine's EC) and
101 * "raw" (via the graphics card). In general we should prefer
102 * "firmware" (i.e. ACPI) or "platform" access over "raw"
103 * access, in order not to confuse the BIOS/EC, and
104 * compatibility with possible low-level hotkey handling of
105 * screen brightness. The kernel will already make sure to
106 * expose only one of "firmware" and "platform" for the same
107 * device to userspace. However, we still need to make sure
108 * that we use "raw" only if no "firmware" or "platform"
109 * device for the same device exists. */
111 subsystem
= udev_device_get_subsystem(device
);
112 if (!streq_ptr(subsystem
, "backlight"))
115 v
= udev_device_get_sysattr_value(device
, "type");
116 if (!streq_ptr(v
, "raw"))
119 parent
= find_pci_or_platform_parent(device
);
123 subsystem
= udev_device_get_subsystem(parent
);
127 enumerate
= udev_enumerate_new(udev
);
131 r
= udev_enumerate_add_match_subsystem(enumerate
, "backlight");
135 r
= udev_enumerate_scan_devices(enumerate
);
139 first
= udev_enumerate_get_list_entry(enumerate
);
140 udev_list_entry_foreach(item
, first
) {
141 _cleanup_(udev_device_unrefp
) struct udev_device
*other
;
142 struct udev_device
*other_parent
;
143 const char *other_subsystem
;
145 other
= udev_device_new_from_syspath(udev
, udev_list_entry_get_name(item
));
149 if (same_device(device
, other
))
152 v
= udev_device_get_sysattr_value(other
, "type");
153 if (!STRPTR_IN_SET(v
, "platform", "firmware"))
156 /* OK, so there's another backlight device, and it's a
157 * platform or firmware device, so, let's see if we
158 * can verify it belongs to the same device as
160 other_parent
= find_pci_or_platform_parent(other
);
164 if (same_device(parent
, other_parent
)) {
165 /* Both have the same PCI parent, that means
167 log_debug("Skipping backlight device %s, since device %s is on same PCI device and takes precedence.",
168 udev_device_get_sysname(device
),
169 udev_device_get_sysname(other
));
173 other_subsystem
= udev_device_get_subsystem(other_parent
);
174 if (streq_ptr(other_subsystem
, "platform") && streq_ptr(subsystem
, "pci")) {
175 /* The other is connected to the platform bus
176 * and we are a PCI device, that also means we
178 log_debug("Skipping backlight device %s, since device %s is a platform device and takes precedence.",
179 udev_device_get_sysname(device
),
180 udev_device_get_sysname(other
));
188 static unsigned get_max_brightness(struct udev_device
*device
) {
190 const char *max_brightness_str
;
191 unsigned max_brightness
;
193 max_brightness_str
= udev_device_get_sysattr_value(device
, "max_brightness");
194 if (!max_brightness_str
) {
195 log_warning("Failed to read 'max_brightness' attribute.");
199 r
= safe_atou(max_brightness_str
, &max_brightness
);
201 log_warning_errno(r
, "Failed to parse 'max_brightness' \"%s\": %m", max_brightness_str
);
205 if (max_brightness
<= 0) {
206 log_warning("Maximum brightness is 0, ignoring device.");
210 return max_brightness
;
213 /* Some systems turn the backlight all the way off at the lowest levels.
214 * clamp_brightness clamps the saved brightness to at least 1 or 5% of
215 * max_brightness in case of 'backlight' subsystem. This avoids preserving
216 * an unreadably dim screen, which would otherwise force the user to
217 * disable state restoration. */
218 static void clamp_brightness(struct udev_device
*device
, char **value
, unsigned max_brightness
) {
219 unsigned brightness
, new_brightness
, min_brightness
;
220 const char *subsystem
;
223 r
= safe_atou(*value
, &brightness
);
225 log_warning_errno(r
, "Failed to parse brightness \"%s\": %m", *value
);
229 subsystem
= udev_device_get_subsystem(device
);
230 if (streq_ptr(subsystem
, "backlight"))
231 min_brightness
= MAX(1U, max_brightness
/20);
235 new_brightness
= CLAMP(brightness
, min_brightness
, max_brightness
);
236 if (new_brightness
!= brightness
) {
237 char *old_value
= *value
;
239 r
= asprintf(value
, "%u", new_brightness
);
245 log_info("Saved brightness %s %s to %s.", old_value
,
246 new_brightness
> brightness
?
247 "too low; increasing" : "too high; decreasing",
254 static bool shall_clamp(struct udev_device
*d
) {
260 s
= udev_device_get_property_value(d
, "ID_BACKLIGHT_CLAMP");
264 r
= parse_boolean(s
);
266 log_debug_errno(r
, "Failed to parse ID_BACKLIGHT_CLAMP property, ignoring: %m");
273 int main(int argc
, char *argv
[]) {
274 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
275 _cleanup_(udev_device_unrefp
) struct udev_device
*device
= NULL
;
276 _cleanup_free_
char *escaped_ss
= NULL
, *escaped_sysname
= NULL
, *escaped_path_id
= NULL
;
277 const char *sysname
, *path_id
, *ss
, *saved
;
278 unsigned max_brightness
;
282 log_error("This program requires two arguments.");
286 log_set_target(LOG_TARGET_AUTO
);
287 log_parse_environment();
292 r
= mkdir_p("/var/lib/systemd/backlight", 0755);
294 log_error_errno(r
, "Failed to create backlight directory /var/lib/systemd/backlight: %m");
304 sysname
= strchr(argv
[2], ':');
306 log_error("Requires a subsystem and sysname pair specifying a backlight device.");
310 ss
= strndupa(argv
[2], sysname
- argv
[2]);
314 if (!STR_IN_SET(ss
, "backlight", "leds")) {
315 log_error("Not a backlight or LED device: '%s:%s'", ss
, sysname
);
320 device
= udev_device_new_from_subsystem_sysname(udev
, ss
, sysname
);
323 log_error_errno(errno
, "Failed to get backlight or LED device '%s:%s': %m", ss
, sysname
);
330 /* If max_brightness is 0, then there is no actual backlight
331 * device. This happens on desktops with Asus mainboards
332 * that load the eeepc-wmi module.
334 max_brightness
= get_max_brightness(device
);
335 if (max_brightness
== 0)
338 escaped_ss
= cescape(ss
);
344 escaped_sysname
= cescape(sysname
);
345 if (!escaped_sysname
) {
350 path_id
= udev_device_get_property_value(device
, "ID_PATH");
352 escaped_path_id
= cescape(path_id
);
353 if (!escaped_path_id
) {
358 saved
= strjoina("/var/lib/systemd/backlight/", escaped_path_id
, ":", escaped_ss
, ":", escaped_sysname
);
360 saved
= strjoina("/var/lib/systemd/backlight/", escaped_ss
, ":", escaped_sysname
);
362 /* If there are multiple conflicting backlight devices, then
363 * their probing at boot-time might happen in any order. This
364 * means the validity checking of the device then is not
365 * reliable, since it might not see other devices conflicting
366 * with a specific backlight. To deal with this, we will
367 * actively delete backlight state files at shutdown (where
368 * device probing should be complete), so that the validity
369 * check at boot time doesn't have to be reliable. */
371 if (streq(argv
[1], "load")) {
372 _cleanup_free_
char *value
= NULL
;
375 if (shall_restore_state() == 0)
378 if (!validate_device(udev
, device
))
381 clamp
= shall_clamp(device
);
383 r
= read_one_line_file(saved
, &value
);
387 /* Fallback to clamping current brightness or exit early if
388 * clamping is not supported/enabled. */
392 curval
= udev_device_get_sysattr_value(device
, "brightness");
394 log_warning("Failed to read 'brightness' attribute.");
398 value
= strdup(curval
);
404 log_error_errno(r
, "Failed to read %s: %m", saved
);
409 clamp_brightness(device
, &value
, max_brightness
);
411 r
= udev_device_set_sysattr_value(device
, "brightness", value
);
413 log_error_errno(r
, "Failed to write system 'brightness' attribute: %m");
417 } else if (streq(argv
[1], "save")) {
420 if (!validate_device(udev
, device
)) {
425 value
= udev_device_get_sysattr_value(device
, "brightness");
427 log_error("Failed to read system 'brightness' attribute");
431 r
= write_string_file(saved
, value
, WRITE_STRING_FILE_CREATE
);
433 log_error_errno(r
, "Failed to write %s: %m", saved
);
438 log_error("Unknown verb %s.", argv
[1]);