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