]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/backlight/backlight.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / backlight / backlight.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
3731acf1 2
b4bbcaa9
TA
3#include "libudev.h"
4
b5efdb8a 5#include "alloc-util.h"
4f5dd394
LP
6#include "def.h"
7#include "escape.h"
3731acf1 8#include "fileio.h"
4f5dd394 9#include "mkdir.h"
4e731273
LP
10#include "parse-util.h"
11#include "proc-cmdline.h"
07630cea 12#include "string-util.h"
1ca208fb 13#include "udev-util.h"
4f5dd394 14#include "util.h"
3731acf1 15
0f4ba83c
LP
16static struct udev_device *find_pci_or_platform_parent(struct udev_device *device) {
17 struct udev_device *parent;
18 const char *subsystem, *sysname;
19
20 assert(device);
21
22 parent = udev_device_get_parent(device);
23 if (!parent)
24 return NULL;
25
26 subsystem = udev_device_get_subsystem(parent);
27 if (!subsystem)
28 return NULL;
29
30 sysname = udev_device_get_sysname(parent);
31 if (!sysname)
32 return NULL;
33
34 if (streq(subsystem, "drm")) {
35 const char *c;
36
37 c = startswith(sysname, "card");
38 if (!c)
39 return NULL;
40
938d2699 41 c += strspn(c, DIGITS);
0f4ba83c
LP
42 if (*c == '-') {
43 /* A connector DRM device, let's ignore all but LVDS and eDP! */
44
45 if (!startswith(c, "-LVDS-") &&
46 !startswith(c, "-Embedded DisplayPort-"))
47 return NULL;
48 }
49
50 } else if (streq(subsystem, "pci")) {
51 const char *value;
52
53 value = udev_device_get_sysattr_value(parent, "class");
54 if (value) {
39883f62 55 unsigned long class = 0;
0f4ba83c
LP
56
57 if (safe_atolu(value, &class) < 0) {
938d2699
ZJS
58 log_warning("Cannot parse PCI class %s of device %s:%s.",
59 value, subsystem, sysname);
0f4ba83c
LP
60 return NULL;
61 }
62
63 /* Graphics card */
64 if (class == 0x30000)
65 return parent;
66 }
67
68 } else if (streq(subsystem, "platform"))
69 return parent;
70
71 return find_pci_or_platform_parent(parent);
72}
73
74static bool same_device(struct udev_device *a, struct udev_device *b) {
75 assert(a);
76 assert(b);
77
78 if (!streq_ptr(udev_device_get_subsystem(a), udev_device_get_subsystem(b)))
79 return false;
80
81 if (!streq_ptr(udev_device_get_sysname(a), udev_device_get_sysname(b)))
82 return false;
83
84 return true;
85}
86
87static bool validate_device(struct udev *udev, struct udev_device *device) {
8e766630 88 _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *enumerate = NULL;
0f4ba83c
LP
89 struct udev_list_entry *item = NULL, *first = NULL;
90 struct udev_device *parent;
91 const char *v, *subsystem;
92 int r;
93
94 assert(udev);
95 assert(device);
96
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. */
110
111 subsystem = udev_device_get_subsystem(device);
112 if (!streq_ptr(subsystem, "backlight"))
113 return true;
114
115 v = udev_device_get_sysattr_value(device, "type");
116 if (!streq_ptr(v, "raw"))
117 return true;
118
119 parent = find_pci_or_platform_parent(device);
120 if (!parent)
121 return true;
122
123 subsystem = udev_device_get_subsystem(parent);
124 if (!subsystem)
125 return true;
126
127 enumerate = udev_enumerate_new(udev);
128 if (!enumerate)
129 return true;
130
131 r = udev_enumerate_add_match_subsystem(enumerate, "backlight");
132 if (r < 0)
133 return true;
134
135 r = udev_enumerate_scan_devices(enumerate);
136 if (r < 0)
137 return true;
138
139 first = udev_enumerate_get_list_entry(enumerate);
140 udev_list_entry_foreach(item, first) {
8e766630 141 _cleanup_(udev_device_unrefp) struct udev_device *other;
0f4ba83c
LP
142 struct udev_device *other_parent;
143 const char *other_subsystem;
144
145 other = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
146 if (!other)
147 return true;
148
149 if (same_device(device, other))
150 continue;
151
152 v = udev_device_get_sysattr_value(other, "type");
1cf03a4f 153 if (!STRPTR_IN_SET(v, "platform", "firmware"))
0f4ba83c
LP
154 continue;
155
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
159 * ours. */
160 other_parent = find_pci_or_platform_parent(other);
161 if (!other_parent)
162 continue;
163
164 if (same_device(parent, other_parent)) {
165 /* Both have the same PCI parent, that means
166 * we are out. */
938d2699
ZJS
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));
0f4ba83c
LP
170 return false;
171 }
172
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
177 * are out. */
938d2699
ZJS
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));
0f4ba83c
LP
181 return false;
182 }
183 }
184
185 return true;
186}
187
3cadce7d 188static unsigned get_max_brightness(struct udev_device *device) {
7b909d74
JT
189 int r;
190 const char *max_brightness_str;
3cadce7d 191 unsigned max_brightness;
7b909d74
JT
192
193 max_brightness_str = udev_device_get_sysattr_value(device, "max_brightness");
194 if (!max_brightness_str) {
c7fdf44d 195 log_warning("Failed to read 'max_brightness' attribute.");
3cadce7d 196 return 0;
7b909d74
JT
197 }
198
3cadce7d 199 r = safe_atou(max_brightness_str, &max_brightness);
7b909d74 200 if (r < 0) {
da927ba9 201 log_warning_errno(r, "Failed to parse 'max_brightness' \"%s\": %m", max_brightness_str);
c7fdf44d
LP
202 return 0;
203 }
204
205 if (max_brightness <= 0) {
206 log_warning("Maximum brightness is 0, ignoring device.");
3cadce7d 207 return 0;
7b909d74
JT
208 }
209
3cadce7d
TB
210 return max_brightness;
211}
212
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
4cd2b2cf
DT
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. */
3cadce7d 218static void clamp_brightness(struct udev_device *device, char **value, unsigned max_brightness) {
0c9d8f1d 219 unsigned brightness, new_brightness, min_brightness;
4cd2b2cf 220 const char *subsystem;
3e44d24d 221 int r;
3cadce7d
TB
222
223 r = safe_atou(*value, &brightness);
7b909d74 224 if (r < 0) {
da927ba9 225 log_warning_errno(r, "Failed to parse brightness \"%s\": %m", *value);
7b909d74
JT
226 return;
227 }
228
4cd2b2cf
DT
229 subsystem = udev_device_get_subsystem(device);
230 if (streq_ptr(subsystem, "backlight"))
231 min_brightness = MAX(1U, max_brightness/20);
232 else
233 min_brightness = 0;
234
0c9d8f1d 235 new_brightness = CLAMP(brightness, min_brightness, max_brightness);
7b909d74
JT
236 if (new_brightness != brightness) {
237 char *old_value = *value;
238
239 r = asprintf(value, "%u", new_brightness);
240 if (r < 0) {
241 log_oom();
242 return;
243 }
244
0c9d8f1d
JN
245 log_info("Saved brightness %s %s to %s.", old_value,
246 new_brightness > brightness ?
247 "too low; increasing" : "too high; decreasing",
248 *value);
249
7b909d74
JT
250 free(old_value);
251 }
252}
253
4432b941
SR
254static bool shall_clamp(struct udev_device *d) {
255 const char *s;
256 int r;
257
258 assert(d);
259
260 s = udev_device_get_property_value(d, "ID_BACKLIGHT_CLAMP");
261 if (!s)
262 return true;
263
264 r = parse_boolean(s);
265 if (r < 0) {
266 log_debug_errno(r, "Failed to parse ID_BACKLIGHT_CLAMP property, ignoring: %m");
267 return true;
268 }
269
270 return r;
271}
272
3731acf1 273int main(int argc, char *argv[]) {
8e766630
LP
274 _cleanup_(udev_unrefp) struct udev *udev = NULL;
275 _cleanup_(udev_device_unrefp) struct udev_device *device = NULL;
3e44d24d
LP
276 _cleanup_free_ char *escaped_ss = NULL, *escaped_sysname = NULL, *escaped_path_id = NULL;
277 const char *sysname, *path_id, *ss, *saved;
3cadce7d 278 unsigned max_brightness;
3731acf1
LP
279 int r;
280
281 if (argc != 3) {
282 log_error("This program requires two arguments.");
283 return EXIT_FAILURE;
284 }
285
286 log_set_target(LOG_TARGET_AUTO);
287 log_parse_environment();
288 log_open();
289
290 umask(0022);
291
ef5bfcf6 292 r = mkdir_p("/var/lib/systemd/backlight", 0755);
3731acf1 293 if (r < 0) {
c33b3297 294 log_error_errno(r, "Failed to create backlight directory /var/lib/systemd/backlight: %m");
1ca208fb 295 return EXIT_FAILURE;
3731acf1
LP
296 }
297
298 udev = udev_new();
299 if (!udev) {
1ca208fb
ZJS
300 log_oom();
301 return EXIT_FAILURE;
3731acf1
LP
302 }
303
0f4ba83c
LP
304 sysname = strchr(argv[2], ':');
305 if (!sysname) {
938d2699 306 log_error("Requires a subsystem and sysname pair specifying a backlight device.");
0f4ba83c
LP
307 return EXIT_FAILURE;
308 }
309
3e44d24d 310 ss = strndupa(argv[2], sysname - argv[2]);
0f4ba83c
LP
311
312 sysname++;
313
3e44d24d 314 if (!STR_IN_SET(ss, "backlight", "leds")) {
0f4ba83c
LP
315 log_error("Not a backlight or LED device: '%s:%s'", ss, sysname);
316 return EXIT_FAILURE;
317 }
318
875c6e1b 319 errno = 0;
0f4ba83c 320 device = udev_device_new_from_subsystem_sysname(udev, ss, sysname);
3731acf1 321 if (!device) {
b3267152 322 if (errno > 0)
56f64d95 323 log_error_errno(errno, "Failed to get backlight or LED device '%s:%s': %m", ss, sysname);
1ca208fb 324 else
0f4ba83c 325 log_oom();
3731acf1 326
1ca208fb 327 return EXIT_FAILURE;
3731acf1
LP
328 }
329
3cadce7d
TB
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.
333 */
334 max_brightness = get_max_brightness(device);
335 if (max_brightness == 0)
336 return EXIT_SUCCESS;
337
be3f52f4
LP
338 escaped_ss = cescape(ss);
339 if (!escaped_ss) {
340 log_oom();
341 return EXIT_FAILURE;
342 }
343
344 escaped_sysname = cescape(sysname);
345 if (!escaped_sysname) {
346 log_oom();
347 return EXIT_FAILURE;
348 }
349
350 path_id = udev_device_get_property_value(device, "ID_PATH");
351 if (path_id) {
352 escaped_path_id = cescape(path_id);
353 if (!escaped_path_id) {
354 log_oom();
355 return EXIT_FAILURE;
356 }
357
3e44d24d 358 saved = strjoina("/var/lib/systemd/backlight/", escaped_path_id, ":", escaped_ss, ":", escaped_sysname);
be3f52f4 359 } else
3e44d24d 360 saved = strjoina("/var/lib/systemd/backlight/", escaped_ss, ":", escaped_sysname);
3731acf1 361
0f4ba83c
LP
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
73e231ab 366 * with a specific backlight. To deal with this, we will
0f4ba83c
LP
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. */
370
b76388e1 371 if (streq(argv[1], "load")) {
3731acf1 372 _cleanup_free_ char *value = NULL;
4432b941 373 bool clamp;
3731acf1 374
934ae16b 375 if (shall_restore_state() == 0)
b76388e1
MB
376 return EXIT_SUCCESS;
377
0f4ba83c
LP
378 if (!validate_device(udev, device))
379 return EXIT_SUCCESS;
380
4432b941
SR
381 clamp = shall_clamp(device);
382
3731acf1 383 r = read_one_line_file(saved, &value);
4432b941
SR
384 if (r == -ENOENT) {
385 const char *curval;
3731acf1 386
4432b941
SR
387 /* Fallback to clamping current brightness or exit early if
388 * clamping is not supported/enabled. */
389 if (!clamp)
1ca208fb 390 return EXIT_SUCCESS;
3731acf1 391
4432b941
SR
392 curval = udev_device_get_sysattr_value(device, "brightness");
393 if (!curval) {
394 log_warning("Failed to read 'brightness' attribute.");
395 return EXIT_FAILURE;
396 }
397
398 value = strdup(curval);
399 if (!value) {
400 log_oom();
401 return EXIT_FAILURE;
402 }
403 } else if (r < 0) {
da927ba9 404 log_error_errno(r, "Failed to read %s: %m", saved);
1ca208fb 405 return EXIT_FAILURE;
3731acf1
LP
406 }
407
4432b941 408 if (clamp)
bca81be7 409 clamp_brightness(device, &value, max_brightness);
7b909d74 410
3731acf1
LP
411 r = udev_device_set_sysattr_value(device, "brightness", value);
412 if (r < 0) {
c33b3297 413 log_error_errno(r, "Failed to write system 'brightness' attribute: %m");
1ca208fb 414 return EXIT_FAILURE;
3731acf1
LP
415 }
416
417 } else if (streq(argv[1], "save")) {
418 const char *value;
419
0f4ba83c
LP
420 if (!validate_device(udev, device)) {
421 unlink(saved);
422 return EXIT_SUCCESS;
423 }
424
3731acf1
LP
425 value = udev_device_get_sysattr_value(device, "brightness");
426 if (!value) {
938d2699 427 log_error("Failed to read system 'brightness' attribute");
1ca208fb 428 return EXIT_FAILURE;
3731acf1
LP
429 }
430
4c1fc3e4 431 r = write_string_file(saved, value, WRITE_STRING_FILE_CREATE);
3731acf1 432 if (r < 0) {
da927ba9 433 log_error_errno(r, "Failed to write %s: %m", saved);
1ca208fb 434 return EXIT_FAILURE;
3731acf1
LP
435 }
436
437 } else {
438 log_error("Unknown verb %s.", argv[1]);
1ca208fb 439 return EXIT_FAILURE;
3731acf1
LP
440 }
441
1ca208fb 442 return EXIT_SUCCESS;
3731acf1 443}