]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/backlight/backlight.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / backlight / backlight.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "sd-device.h"
4
5 #include "alloc-util.h"
6 #include "device-util.h"
7 #include "escape.h"
8 #include "fileio.h"
9 #include "mkdir.h"
10 #include "parse-util.h"
11 #include "proc-cmdline.h"
12 #include "string-util.h"
13 #include "strv.h"
14 #include "util.h"
15
16 static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) {
17 const char *subsystem, *sysname, *value;
18 sd_device *parent;
19 int r;
20
21 assert(device);
22 assert(ret);
23
24 r = sd_device_get_parent(device, &parent);
25 if (r < 0)
26 return r;
27
28 r = sd_device_get_subsystem(parent, &subsystem);
29 if (r < 0)
30 return r;
31
32 r = sd_device_get_sysname(parent, &sysname);
33 if (r < 0)
34 return r;
35
36 if (streq(subsystem, "drm")) {
37 const char *c;
38
39 c = startswith(sysname, "card");
40 if (!c)
41 return -ENODATA;
42
43 c += strspn(c, DIGITS);
44 if (*c == '-') {
45 /* A connector DRM device, let's ignore all but LVDS and eDP! */
46
47 if (!startswith(c, "-LVDS-") &&
48 !startswith(c, "-Embedded DisplayPort-"))
49 return -EOPNOTSUPP;
50 }
51
52 } else if (streq(subsystem, "pci") &&
53 sd_device_get_sysattr_value(parent, "class", &value) >= 0) {
54 unsigned long class = 0;
55
56 r = safe_atolu(value, &class);
57 if (r < 0)
58 return log_warning_errno(r, "Cannot parse PCI class '%s' of device %s:%s: %m",
59 value, subsystem, sysname);
60
61 /* Graphics card */
62 if (class == 0x30000) {
63 *ret = parent;
64 return 0;
65 }
66
67 } else if (streq(subsystem, "platform")) {
68 *ret = parent;
69 return 0;
70 }
71
72 return find_pci_or_platform_parent(parent, ret);
73 }
74
75 static int same_device(sd_device *a, sd_device *b) {
76 const char *a_val, *b_val;
77 int r;
78
79 assert(a);
80 assert(b);
81
82 r = sd_device_get_subsystem(a, &a_val);
83 if (r < 0)
84 return r;
85
86 r = sd_device_get_subsystem(b, &b_val);
87 if (r < 0)
88 return r;
89
90 if (!streq(a_val, b_val))
91 return false;
92
93 r = sd_device_get_sysname(a, &a_val);
94 if (r < 0)
95 return r;
96
97 r = sd_device_get_sysname(b, &b_val);
98 if (r < 0)
99 return r;
100
101 return streq(a_val, b_val);
102 }
103
104 static int validate_device(sd_device *device) {
105 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *enumerate = NULL;
106 const char *v, *subsystem;
107 sd_device *parent, *other;
108 int r;
109
110 assert(device);
111
112 /* Verify whether we should actually care for a specific
113 * backlight device. For backlight devices there might be
114 * multiple ways to access the same control: "firmware"
115 * (i.e. ACPI), "platform" (i.e. via the machine's EC) and
116 * "raw" (via the graphics card). In general we should prefer
117 * "firmware" (i.e. ACPI) or "platform" access over "raw"
118 * access, in order not to confuse the BIOS/EC, and
119 * compatibility with possible low-level hotkey handling of
120 * screen brightness. The kernel will already make sure to
121 * expose only one of "firmware" and "platform" for the same
122 * device to userspace. However, we still need to make sure
123 * that we use "raw" only if no "firmware" or "platform"
124 * device for the same device exists. */
125
126 r = sd_device_get_subsystem(device, &subsystem);
127 if (r < 0)
128 return r;
129 if (!streq(subsystem, "backlight"))
130 return true;
131
132 r = sd_device_get_sysattr_value(device, "type", &v);
133 if (r < 0)
134 return r;
135 if (!streq(v, "raw"))
136 return true;
137
138 r = find_pci_or_platform_parent(device, &parent);
139 if (r < 0)
140 return r;
141
142 r = sd_device_get_subsystem(parent, &subsystem);
143 if (r < 0)
144 return r;
145
146 r = sd_device_enumerator_new(&enumerate);
147 if (r < 0)
148 return r;
149
150 r = sd_device_enumerator_allow_uninitialized(enumerate);
151 if (r < 0)
152 return r;
153
154 r = sd_device_enumerator_add_match_subsystem(enumerate, "backlight", true);
155 if (r < 0)
156 return r;
157
158 FOREACH_DEVICE(enumerate, other) {
159 const char *other_subsystem;
160 sd_device *other_parent;
161
162 if (same_device(device, other) > 0)
163 continue;
164
165 if (sd_device_get_sysattr_value(other, "type", &v) < 0 ||
166 !STR_IN_SET(v, "platform", "firmware"))
167 continue;
168
169 /* OK, so there's another backlight device, and it's a
170 * platform or firmware device, so, let's see if we
171 * can verify it belongs to the same device as ours. */
172 if (find_pci_or_platform_parent(other, &other_parent) < 0)
173 continue;
174
175 if (same_device(parent, other_parent)) {
176 const char *device_sysname = NULL, *other_sysname = NULL;
177
178 /* Both have the same PCI parent, that means we are out. */
179
180 (void) sd_device_get_sysname(device, &device_sysname);
181 (void) sd_device_get_sysname(other, &other_sysname);
182
183 log_debug("Skipping backlight device %s, since device %s is on same PCI device and takes precedence.",
184 device_sysname, other_sysname);
185 return false;
186 }
187
188 if (sd_device_get_subsystem(other_parent, &other_subsystem) < 0)
189 continue;
190
191 if (streq(other_subsystem, "platform") && streq(subsystem, "pci")) {
192 const char *device_sysname = NULL, *other_sysname = NULL;
193
194 /* The other is connected to the platform bus and we are a PCI device, that also means we are out. */
195
196 (void) sd_device_get_sysname(device, &device_sysname);
197 (void) sd_device_get_sysname(other, &other_sysname);
198
199 log_debug("Skipping backlight device %s, since device %s is a platform device and takes precedence.",
200 device_sysname, other_sysname);
201 return false;
202 }
203 }
204
205 return true;
206 }
207
208 static int get_max_brightness(sd_device *device, unsigned *ret) {
209 const char *max_brightness_str;
210 unsigned max_brightness;
211 int r;
212
213 assert(device);
214 assert(ret);
215
216 r = sd_device_get_sysattr_value(device, "max_brightness", &max_brightness_str);
217 if (r < 0)
218 return log_device_warning_errno(device, r, "Failed to read 'max_brightness' attribute: %m");
219
220 r = safe_atou(max_brightness_str, &max_brightness);
221 if (r < 0)
222 return log_device_warning_errno(device, r, "Failed to parse 'max_brightness' \"%s\": %m", max_brightness_str);
223
224 if (max_brightness <= 0) {
225 log_device_warning(device, "Maximum brightness is 0, ignoring device.");
226 return -EINVAL;
227 }
228
229 *ret = max_brightness;
230 return 0;
231 }
232
233 /* Some systems turn the backlight all the way off at the lowest levels.
234 * clamp_brightness clamps the saved brightness to at least 1 or 5% of
235 * max_brightness in case of 'backlight' subsystem. This avoids preserving
236 * an unreadably dim screen, which would otherwise force the user to
237 * disable state restoration. */
238 static int clamp_brightness(sd_device *device, char **value, unsigned max_brightness) {
239 unsigned brightness, new_brightness, min_brightness;
240 const char *subsystem;
241 int r;
242
243 assert(value);
244 assert(*value);
245
246 r = safe_atou(*value, &brightness);
247 if (r < 0)
248 return log_device_warning_errno(device, r, "Failed to parse brightness \"%s\": %m", *value);
249
250 r = sd_device_get_subsystem(device, &subsystem);
251 if (r < 0)
252 return log_device_warning_errno(device, r, "Failed to get device subsystem: %m");
253
254 if (streq(subsystem, "backlight"))
255 min_brightness = MAX(1U, max_brightness/20);
256 else
257 min_brightness = 0;
258
259 new_brightness = CLAMP(brightness, min_brightness, max_brightness);
260 if (new_brightness != brightness) {
261 char *new_value;
262
263 r = asprintf(&new_value, "%u", new_brightness);
264 if (r < 0)
265 return log_oom();
266
267 log_device_info(device, "Saved brightness %s %s to %s.", *value,
268 new_brightness > brightness ?
269 "too low; increasing" : "too high; decreasing",
270 new_value);
271
272 free_and_replace(*value, new_value);
273 }
274
275 return 0;
276 }
277
278 static bool shall_clamp(sd_device *d) {
279 const char *s;
280 int r;
281
282 assert(d);
283
284 r = sd_device_get_property_value(d, "ID_BACKLIGHT_CLAMP", &s);
285 if (r < 0) {
286 log_device_debug_errno(d, r, "Failed to get ID_BACKLIGHT_CLAMP property, ignoring: %m");
287 return true;
288 }
289
290 r = parse_boolean(s);
291 if (r < 0) {
292 log_device_debug_errno(d, r, "Failed to parse ID_BACKLIGHT_CLAMP property, ignoring: %m");
293 return true;
294 }
295
296 return r;
297 }
298
299 int main(int argc, char *argv[]) {
300 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
301 _cleanup_free_ char *escaped_ss = NULL, *escaped_sysname = NULL, *escaped_path_id = NULL;
302 const char *sysname, *path_id, *ss, *saved;
303 unsigned max_brightness;
304 int r;
305
306 if (argc != 3) {
307 log_error("This program requires two arguments.");
308 return EXIT_FAILURE;
309 }
310
311 log_set_target(LOG_TARGET_AUTO);
312 log_parse_environment();
313 log_open();
314
315 umask(0022);
316
317 r = mkdir_p("/var/lib/systemd/backlight", 0755);
318 if (r < 0) {
319 log_error_errno(r, "Failed to create backlight directory /var/lib/systemd/backlight: %m");
320 return EXIT_FAILURE;
321 }
322
323 sysname = strchr(argv[2], ':');
324 if (!sysname) {
325 log_error("Requires a subsystem and sysname pair specifying a backlight device.");
326 return EXIT_FAILURE;
327 }
328
329 ss = strndupa(argv[2], sysname - argv[2]);
330
331 sysname++;
332
333 if (!STR_IN_SET(ss, "backlight", "leds")) {
334 log_error("Not a backlight or LED device: '%s:%s'", ss, sysname);
335 return EXIT_FAILURE;
336 }
337
338 r = sd_device_new_from_subsystem_sysname(&device, ss, sysname);
339 if (r < 0) {
340 log_error_errno(r, "Failed to get backlight or LED device '%s:%s': %m", ss, sysname);
341 return EXIT_FAILURE;
342 }
343
344 /* If max_brightness is 0, then there is no actual backlight
345 * device. This happens on desktops with Asus mainboards
346 * that load the eeepc-wmi module. */
347 r = get_max_brightness(device, &max_brightness);
348 if (r < 0)
349 return EXIT_SUCCESS;
350
351 escaped_ss = cescape(ss);
352 if (!escaped_ss) {
353 log_oom();
354 return EXIT_FAILURE;
355 }
356
357 escaped_sysname = cescape(sysname);
358 if (!escaped_sysname) {
359 log_oom();
360 return EXIT_FAILURE;
361 }
362
363 if (sd_device_get_property_value(device, "ID_PATH", &path_id) >= 0) {
364 escaped_path_id = cescape(path_id);
365 if (!escaped_path_id) {
366 log_oom();
367 return EXIT_FAILURE;
368 }
369
370 saved = strjoina("/var/lib/systemd/backlight/", escaped_path_id, ":", escaped_ss, ":", escaped_sysname);
371 } else
372 saved = strjoina("/var/lib/systemd/backlight/", escaped_ss, ":", escaped_sysname);
373
374 /* If there are multiple conflicting backlight devices, then
375 * their probing at boot-time might happen in any order. This
376 * means the validity checking of the device then is not
377 * reliable, since it might not see other devices conflicting
378 * with a specific backlight. To deal with this, we will
379 * actively delete backlight state files at shutdown (where
380 * device probing should be complete), so that the validity
381 * check at boot time doesn't have to be reliable. */
382
383 if (streq(argv[1], "load")) {
384 _cleanup_free_ char *value = NULL;
385 bool clamp;
386
387 if (shall_restore_state() == 0)
388 return EXIT_SUCCESS;
389
390 if (validate_device(device) == 0)
391 return EXIT_SUCCESS;
392
393 clamp = shall_clamp(device);
394
395 r = read_one_line_file(saved, &value);
396 if (r == -ENOENT) {
397 const char *curval;
398
399 /* Fallback to clamping current brightness or exit early if
400 * clamping is not supported/enabled. */
401 if (!clamp)
402 return EXIT_SUCCESS;
403
404 r = sd_device_get_sysattr_value(device, "brightness", &curval);
405 if (r < 0) {
406 log_device_warning_errno(device, r, "Failed to read 'brightness' attribute: %m");
407 return EXIT_FAILURE;
408 }
409
410 value = strdup(curval);
411 if (!value) {
412 log_oom();
413 return EXIT_FAILURE;
414 }
415 } else if (r < 0) {
416 log_error_errno(r, "Failed to read %s: %m", saved);
417 return EXIT_FAILURE;
418 }
419
420 if (clamp)
421 (void) clamp_brightness(device, &value, max_brightness);
422
423 r = sd_device_set_sysattr_value(device, "brightness", value);
424 if (r < 0) {
425 log_device_error_errno(device, r, "Failed to write system 'brightness' attribute: %m");
426 return EXIT_FAILURE;
427 }
428
429 } else if (streq(argv[1], "save")) {
430 const char *value;
431
432 if (validate_device(device) == 0) {
433 unlink(saved);
434 return EXIT_SUCCESS;
435 }
436
437 r = sd_device_get_sysattr_value(device, "brightness", &value);
438 if (r < 0) {
439 log_device_error_errno(device, r, "Failed to read system 'brightness' attribute: %m");
440 return EXIT_FAILURE;
441 }
442
443 r = write_string_file(saved, value, WRITE_STRING_FILE_CREATE);
444 if (r < 0) {
445 log_device_error_errno(device, r, "Failed to write %s: %m", saved);
446 return EXIT_FAILURE;
447 }
448
449 } else {
450 log_error("Unknown verb %s.", argv[1]);
451 return EXIT_FAILURE;
452 }
453
454 return EXIT_SUCCESS;
455 }