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