Fix connected sleep/wake lockup, s2idle drain, and USB fuel-gauge taper jump

This commit is contained in:
Luxferre committed 2026-11-26 08:42:18 +02:00
1 parent 3ea7d35aba
commit e8065bb014
8 files changed
+396 -28

No files matched your search

@@ -8,6 +8,7 @@ void geminux_policy_init(geminux_power_policy_t *p) {
memset(p, 0, sizeof(*p));
p->profile = POWER_PROFILE_BALANCED;
p->lid_suspend_enabled = true;
p->inhibit_ac_lid = true;
}
bool geminux_policy_set_profile_name(geminux_power_policy_t *p, const char *name) {
+116 -27
View File
@@ -46,6 +46,7 @@ typedef struct {
int srv_fd;
int tmr_fd;
int ev_fds[MAX_EVDEVS];
bool ev_is_pmic_pwr[MAX_EVDEVS];
int num_ev;
int saved_brightness;
uint64_t last_input_ms;
@@ -66,6 +67,7 @@ typedef struct {
pid_t radio_worker_pid;
bool consys_suspended;
bool suspend_ebusy_on_ac;
bool low_power_standby;
bool airplane_mode;
bool air_prev_wifi;
bool air_prev_bt_loaded;
@@ -241,6 +243,8 @@ static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) {
static void lock_x11_session(void);
static void trigger_suspend(powerd_ctx_t *ctx);
static void restore_cpus_after_resume(powerd_ctx_t *ctx);
static void exit_low_power_standby(powerd_ctx_t *ctx);
static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force);
static void apply_cpu_policy(powerd_ctx_t *ctx, bool force);
@@ -350,6 +354,7 @@ static void evaluate_idle(powerd_ctx_t *ctx) {
}
static void note_user_activity(powerd_ctx_t *ctx) {
exit_low_power_standby(ctx);
if (ctx->consys_suspended && !ctx->policy.lid_closed) {
restore_consys_after_resume(ctx);
}
@@ -363,6 +368,7 @@ static void note_user_activity(powerd_ctx_t *ctx) {
}
static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force) {
if (ctx->low_power_standby) return;
bool a72_ready = (access("/sys/module/mt6797_a72", F_OK) == 0);
bool cpufreq_present = (access("/sys/devices/system/cpu/cpufreq/policy0", F_OK) == 0);
bool interactive_boost = (ctx->policy.profile == POWER_PROFILE_BALANCED &&
@@ -1080,25 +1086,71 @@ static void restore_cpus_after_resume(powerd_ctx_t *ctx) {
ctx->policy_applied = false;
}
static bool can_enter_deep_mem_sleep(const powerd_ctx_t *ctx) {
if (ctx->policy.on_ac_power) return false;
char st[16] = {0};
if (read_sysfs_line("/sys/class/mt6797_ccci/md1/stage", st, sizeof(st)) == 0 && atoi(st) > 0) {
return false;
}
return true;
}
static void exit_low_power_standby(powerd_ctx_t *ctx) {
if (!ctx->low_power_standby) return;
ctx->low_power_standby = false;
restore_cpus_after_resume(ctx);
}
static bool fd_is_pmic_pwr(const powerd_ctx_t *ctx, int fd) {
for (int i = 0; i < ctx->num_ev; i++) {
if (ctx->ev_fds[i] == fd) return ctx->ev_is_pmic_pwr[i];
}
return false;
}
static void trigger_suspend(powerd_ctx_t *ctx) {
arm_timer_ms(ctx->tmr_fd, 0);
refresh_ac_and_hdmi(ctx);
lock_x11_session();
toggle_backlight(ctx, false);
save_fuel_gauge_state(ctx);
if (ctx->policy.on_ac_power) {
ctx->suspend_ebusy_on_ac = true;
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
apply_cpu_sysfs(ctx, false);
return;
}
exit_low_power_standby(ctx);
prepare_consys_for_suspend(ctx);
prepare_cpus_for_suspend();
write_sysfs_str("/sys/power/mem_sleep", "deep");
bool suspended = (write_sysfs_str("/sys/power/state", "mem") == 0 ||
write_sysfs_str("/sys/power/state", "freeze") == 0);
bool can_deep = can_enter_deep_mem_sleep(ctx);
bool suspended = false;
if (can_deep) {
write_sysfs_str("/sys/power/mem_sleep", "deep");
suspended = (write_sysfs_str("/sys/power/state", "mem") == 0);
}
if (!suspended) {
refresh_lid_state(ctx);
refresh_ac_and_hdmi(ctx);
if (!ctx->policy.on_ac_power && !ctx->policy.hdmi_connected &&
ctx->policy.inhibit_count == 0 &&
(ctx->policy.lid_closed ? geminux_should_suspend_on_lid(&ctx->policy) : true)) {
ctx->low_power_standby = true;
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
arm_timer_ms(ctx->tmr_fd, 0);
return;
}
}
ctx->low_power_standby = false;
restore_cpus_after_resume(ctx);
bool saw_wake_input = wait_post_resume_lid_events(ctx, 90);
bool saw_wake_input = suspended ? wait_post_resume_lid_events(ctx, 90) : !ctx->policy.lid_closed;
refresh_ac_and_hdmi(ctx);
ctx->suspend_ebusy_on_ac = (!suspended && ctx->policy.on_ac_power);
if (!ctx->policy.lid_closed || !geminux_should_suspend_on_lid(&ctx->policy) ||
ctx->suspend_ebusy_on_ac) {
restore_consys_after_resume(ctx);
}
if ((suspended || saw_wake_input) && !ctx->policy.lid_closed) {
if (!ctx->policy.lid_closed && (suspended || saw_wake_input)) {
ctx->suspend_ebusy_on_ac = false;
ctx->idle_stage = IDLE_STAGE_ACTIVE;
ctx->last_input_ms = monotonic_ms();
@@ -1108,7 +1160,7 @@ static void trigger_suspend(powerd_ctx_t *ctx) {
} else {
toggle_backlight(ctx, false);
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
if (!ctx->suspend_ebusy_on_ac &&
if (suspended && !ctx->suspend_ebusy_on_ac &&
(ctx->policy.lid_closed ? geminux_should_suspend_on_lid(&ctx->policy)
: (!ctx->policy.on_ac_power && !saw_wake_input))) {
arm_timer_ms(ctx->tmr_fd, 2000);
@@ -1125,6 +1177,7 @@ static void scan_input_devices(powerd_ctx_t *ctx) {
close(ctx->ev_fds[i]);
}
ctx->num_ev = 0;
memset(ctx->ev_is_pmic_pwr, 0, sizeof(ctx->ev_is_pmic_pwr));
ctx->policy.lid_closed = false;
for (int i = 0; i < 32 && ctx->num_ev < MAX_EVDEVS; i++) {
@@ -1145,6 +1198,9 @@ static void scan_input_devices(powerd_ctx_t *ctx) {
}
struct epoll_event ev = { .events = EPOLLIN, .data.fd = fd };
if (epoll_ctl(ctx->epfd, EPOLL_CTL_ADD, fd, &ev) == 0) {
char name[128] = {0};
if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) name[0] = '\0';
ctx->ev_is_pmic_pwr[ctx->num_ev] = (strstr(name, "pmic-keys") != NULL);
ctx->ev_fds[ctx->num_ev++] = fd;
continue;
}
@@ -1172,6 +1228,7 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) {
} else if (strncmp(buf, "inhibit add ", 12) == 0) {
geminux_policy_add_inhibit(&ctx->policy, buf + 12);
if (!ctx->policy.lid_closed || !geminux_should_suspend_on_lid(&ctx->policy)) {
exit_low_power_standby(ctx);
restore_consys_after_resume(ctx);
}
apply_cpu_policy(ctx, false);
@@ -1185,6 +1242,7 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) {
dprintf(cfd, "OK lid_suspend=on\n");
} else if (strcmp(buf, "lid-suspend off") == 0) {
ctx->policy.lid_suspend_enabled = false;
exit_low_power_standby(ctx);
restore_consys_after_resume(ctx);
dprintf(cfd, "OK lid_suspend=off\n");
} else if (strcmp(buf, "wifi-restore") == 0) {
@@ -1257,6 +1315,31 @@ static int setup_uevent_socket(void) {
return fd;
}
static void handle_lid_closed(powerd_ctx_t *ctx) {
lock_x11_session();
evaluate_idle(ctx);
if (geminux_should_suspend_on_lid(&ctx->policy)) {
trigger_suspend(ctx);
} else {
exit_low_power_standby(ctx);
restore_consys_after_resume(ctx);
toggle_backlight(ctx, false);
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
apply_cpu_sysfs(ctx, false);
}
}
static void handle_lid_open(powerd_ctx_t *ctx) {
ctx->suspend_ebusy_on_ac = false;
exit_low_power_standby(ctx);
restore_consys_after_resume(ctx);
ctx->idle_stage = IDLE_STAGE_ACTIVE;
ctx->last_input_ms = monotonic_ms();
toggle_backlight(ctx, true);
refresh_ac_and_hdmi(ctx);
apply_cpu_policy(ctx, true);
}
int main(void) {
signal(SIGPIPE, SIG_IGN);
powerd_ctx_t ctx;
@@ -1309,6 +1392,7 @@ int main(void) {
if (errno == EINTR) continue;
break;
}
bool ev_fds_rescan = false;
for (int i = 0; i < n; i++) {
int fd = events[i].data.fd;
if (fd == ctx.sig_fd) {
@@ -1337,6 +1421,7 @@ int main(void) {
if (ctx.policy.lid_closed && geminux_should_suspend_on_lid(&ctx.policy)) {
trigger_suspend(&ctx);
} else {
exit_low_power_standby(&ctx);
restore_consys_after_resume(&ctx);
evaluate_idle(&ctx);
}
@@ -1357,7 +1442,16 @@ int main(void) {
need_ps_or_drm = true;
}
}
if (need_ev_rescan) scan_input_devices(&ctx);
if (need_ev_rescan) {
bool prev_lid = ctx.policy.lid_closed;
scan_input_devices(&ctx);
ev_fds_rescan = true;
if (prev_lid && !ctx.policy.lid_closed) {
handle_lid_open(&ctx);
} else if (!prev_lid && ctx.policy.lid_closed) {
handle_lid_closed(&ctx);
}
}
if (need_ps) {
if (!ctx.fg_restored) {
restore_fuel_gauge_state(&ctx);
@@ -1370,6 +1464,7 @@ int main(void) {
bool prev_hdmi = ctx.policy.hdmi_connected;
refresh_ac_and_hdmi(&ctx);
if (!ctx.policy.lid_closed || !geminux_should_suspend_on_lid(&ctx.policy)) {
exit_low_power_standby(&ctx);
restore_consys_after_resume(&ctx);
}
if (prev_ac != ctx.policy.on_ac_power || prev_hdmi != ctx.policy.hdmi_connected) {
@@ -1388,6 +1483,8 @@ int main(void) {
}
}
} else {
if (ev_fds_rescan) continue;
bool is_pmic_pwr = fd_is_pmic_pwr(&ctx, fd);
struct input_event iev;
bool saw_activity = false;
bool power_pressed = false;
@@ -1395,29 +1492,21 @@ int main(void) {
if (iev.type == EV_SW && iev.code == SW_LID) {
ctx.policy.lid_closed = (iev.value != 0);
if (ctx.policy.lid_closed) {
lock_x11_session();
evaluate_idle(&ctx);
if (geminux_should_suspend_on_lid(&ctx.policy)) {
trigger_suspend(&ctx);
} else {
restore_consys_after_resume(&ctx);
toggle_backlight(&ctx, false);
}
handle_lid_closed(&ctx);
} else {
ctx.suspend_ebusy_on_ac = false;
restore_consys_after_resume(&ctx);
ctx.idle_stage = IDLE_STAGE_ACTIVE;
ctx.last_input_ms = monotonic_ms();
toggle_backlight(&ctx, true);
refresh_ac_and_hdmi(&ctx);
apply_cpu_policy(&ctx, true);
evaluate_idle(&ctx);
handle_lid_open(&ctx);
}
} else if (iev.type == EV_KEY && iev.code == KEY_POWER && iev.value == 1) {
if (ctx.idle_stage == IDLE_STAGE_LOCKED_BLANK) {
note_user_activity(&ctx);
} else {
} else if (iev.type == EV_KEY && iev.value == 1 &&
(iev.code == KEY_POWER || (is_pmic_pwr && iev.code == KEY_ESC))) {
refresh_lid_state(&ctx);
if (ctx.idle_stage != IDLE_STAGE_ACTIVE || ctx.low_power_standby) {
if (!ctx.policy.lid_closed) {
note_user_activity(&ctx);
}
} else if (iev.code == KEY_POWER) {
power_pressed = true;
} else {
saw_activity = true;
}
} else if (iev.type == EV_KEY || iev.type == EV_REL || iev.type == EV_ABS) {
saw_activity = true;