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