]>
Commit | Line | Data |
---|---|---|
54d7fcc6 MY |
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | #pragma once | |
3 | ||
a2124b35 | 4 | #include "strv.h" |
54d7fcc6 MY |
5 | #include "time-util.h" |
6 | ||
54d7fcc6 MY |
7 | typedef enum SleepOperation { |
8 | SLEEP_SUSPEND, | |
9 | SLEEP_HIBERNATE, | |
10 | SLEEP_HYBRID_SLEEP, | |
087a25d2 MY |
11 | _SLEEP_OPERATION_CONFIG_MAX, |
12 | /* The operations above require configuration for mode and state. The ones below are "combined" | |
13 | * operations that use config from those individual operations. */ | |
14 | ||
54d7fcc6 | 15 | SLEEP_SUSPEND_THEN_HIBERNATE, |
087a25d2 | 16 | |
54d7fcc6 MY |
17 | _SLEEP_OPERATION_MAX, |
18 | _SLEEP_OPERATION_INVALID = -EINVAL, | |
19 | } SleepOperation; | |
20 | ||
087a25d2 MY |
21 | const char* sleep_operation_to_string(SleepOperation s) _const_; |
22 | SleepOperation sleep_operation_from_string(const char *s) _pure_; | |
23 | ||
792dd6f4 | 24 | static inline bool SLEEP_OPERATION_IS_HIBERNATION(SleepOperation operation) { |
e024cdd2 MY |
25 | return IN_SET(operation, SLEEP_HIBERNATE, SLEEP_HYBRID_SLEEP); |
26 | } | |
27 | ||
54d7fcc6 MY |
28 | typedef struct SleepConfig { |
29 | bool allow[_SLEEP_OPERATION_MAX]; | |
563ef8f9 | 30 | |
087a25d2 | 31 | char **states[_SLEEP_OPERATION_CONFIG_MAX]; |
a2124b35 MY |
32 | char **modes[_SLEEP_OPERATION_CONFIG_MAX]; /* Power mode after writing hibernation image (/sys/power/disk) */ |
33 | char **mem_modes; /* /sys/power/mem_sleep */ | |
087a25d2 | 34 | |
54d7fcc6 MY |
35 | usec_t hibernate_delay_usec; |
36 | usec_t suspend_estimation_usec; | |
37 | } SleepConfig; | |
38 | ||
087a25d2 MY |
39 | SleepConfig* sleep_config_free(SleepConfig *sc); |
40 | DEFINE_TRIVIAL_CLEANUP_FUNC(SleepConfig*, sleep_config_free); | |
54d7fcc6 MY |
41 | |
42 | int parse_sleep_config(SleepConfig **sleep_config); | |
43 | ||
a2124b35 MY |
44 | static inline bool SLEEP_NEEDS_MEM_SLEEP(const SleepConfig *sc, SleepOperation operation) { |
45 | assert(sc); | |
46 | assert(operation >= 0 && operation < _SLEEP_OPERATION_CONFIG_MAX); | |
47 | ||
48 | /* As per https://docs.kernel.org/admin-guide/pm/sleep-states.html#basic-sysfs-interfaces-for-system-suspend-and-hibernation, | |
49 | * /sys/power/mem_sleep is honored if /sys/power/state is set to "mem" (common for suspend) | |
50 | * or /sys/power/disk is set to "suspend" (hybrid-sleep). */ | |
51 | ||
52 | return strv_contains(sc->states[operation], "mem") || | |
53 | strv_contains(sc->modes[operation], "suspend"); | |
54 | } | |
55 | ||
a0f6d74e MY |
56 | typedef enum SleepSupport { |
57 | SLEEP_SUPPORTED, | |
58 | SLEEP_DISABLED, /* Disabled in SleepConfig.allow */ | |
59 | SLEEP_NOT_CONFIGURED, /* SleepConfig.states is not configured */ | |
60 | SLEEP_STATE_OR_MODE_NOT_SUPPORTED, /* SleepConfig.states/modes are not supported by kernel */ | |
805deec0 | 61 | SLEEP_RESUME_NOT_SUPPORTED, |
40eb83a8 | 62 | SLEEP_RESUME_DEVICE_MISSING, /* resume= is specified, but the device cannot be found in /proc/swaps */ |
3fce141c | 63 | SLEEP_RESUME_MISCONFIGURED, /* resume= is not set yet resume_offset= is configured */ |
a0f6d74e MY |
64 | SLEEP_NOT_ENOUGH_SWAP_SPACE, |
65 | SLEEP_ALARM_NOT_SUPPORTED, /* CLOCK_BOOTTIME_ALARM is unsupported by kernel (only used by s2h) */ | |
66 | } SleepSupport; | |
67 | ||
68 | int sleep_supported_full(SleepOperation operation, SleepSupport *ret_support); | |
69 | static inline int sleep_supported(SleepOperation operation) { | |
70 | return sleep_supported_full(operation, NULL); | |
71 | } | |
23577f44 MY |
72 | |
73 | /* Only for test-sleep-config */ | |
b0e3b85a MY |
74 | int sleep_state_supported(char * const *states); |
75 | int sleep_mode_supported(const char *path, char * const *modes); |