Fix deep-sleep battery drainers, enable MD1 mem suspend, and update docs
This commit is contained in:
1 parent
180d0a91e6
commit
29314ca9e6
20 files changed
+996
-75
No files matched your search
@@ -11,10 +11,12 @@
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/signalfd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/wait.h>
|
||||
#include <termios.h>
|
||||
@@ -38,6 +40,10 @@ typedef struct {
|
||||
int srv_fd;
|
||||
int at_fd;
|
||||
int dbus_fd;
|
||||
int ino_fd;
|
||||
int ino_wd;
|
||||
int retry_tmr_fd;
|
||||
uint64_t dbus_retry_ms;
|
||||
pid_t dbus_pid;
|
||||
int dbus_event; /* 1=Voice.CallAdded, 2=Messaging.Added */
|
||||
char dbus_line[512];
|
||||
@@ -345,8 +351,22 @@ static void open_at_port(modem_ctx_t *ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
static void arm_retry_timer(modem_ctx_t *ctx, uint64_t wait_ms) {
|
||||
if (ctx->retry_tmr_fd < 0) return;
|
||||
struct itimerspec its;
|
||||
memset(&its, 0, sizeof(its));
|
||||
if (wait_ms > 0) {
|
||||
its.it_value.tv_sec = (time_t)(wait_ms / 1000ULL);
|
||||
its.it_value.tv_nsec = (long)((wait_ms % 1000ULL) * 1000000ULL);
|
||||
}
|
||||
timerfd_settime(ctx->retry_tmr_fd, 0, &its, NULL);
|
||||
}
|
||||
|
||||
static void start_dbus_monitor(modem_ctx_t *ctx) {
|
||||
if (ctx->dbus_fd >= 0 || access("/usr/bin/dbus-monitor", X_OK) != 0) return;
|
||||
if (is_airplane_active() || ctx->dbus_fd >= 0 ||
|
||||
access("/usr/bin/dbus-monitor", X_OK) != 0) {
|
||||
return;
|
||||
}
|
||||
int pfd[2];
|
||||
if (pipe2(pfd, O_NONBLOCK | O_CLOEXEC) != 0) return;
|
||||
|
||||
@@ -379,6 +399,8 @@ static void start_dbus_monitor(modem_ctx_t *ctx) {
|
||||
ctx->dbus_pid = pid;
|
||||
ctx->dbus_event = 0;
|
||||
ctx->dbus_len = 0;
|
||||
ctx->dbus_retry_ms = 0;
|
||||
arm_retry_timer(ctx, 0);
|
||||
} else {
|
||||
close(pfd[0]);
|
||||
kill(pid, SIGTERM);
|
||||
@@ -386,6 +408,19 @@ static void start_dbus_monitor(modem_ctx_t *ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
static void schedule_dbus_retry(modem_ctx_t *ctx) {
|
||||
if (is_airplane_active() || ctx->dbus_fd >= 0) {
|
||||
ctx->dbus_retry_ms = 0;
|
||||
arm_retry_timer(ctx, 0);
|
||||
return;
|
||||
}
|
||||
if (ctx->dbus_retry_ms < 5000) ctx->dbus_retry_ms = 5000;
|
||||
else if (ctx->dbus_retry_ms < 15000) ctx->dbus_retry_ms = 15000;
|
||||
else if (ctx->dbus_retry_ms < 30000) ctx->dbus_retry_ms = 30000;
|
||||
else ctx->dbus_retry_ms = 60000;
|
||||
arm_retry_timer(ctx, ctx->dbus_retry_ms);
|
||||
}
|
||||
|
||||
static void stop_dbus_monitor(modem_ctx_t *ctx) {
|
||||
if (ctx->dbus_fd >= 0) {
|
||||
epoll_ctl(ctx->epfd, EPOLL_CTL_DEL, ctx->dbus_fd, NULL);
|
||||
@@ -731,6 +766,9 @@ int main(void) {
|
||||
ctx.srv_fd = -1;
|
||||
ctx.at_fd = -1;
|
||||
ctx.dbus_fd = -1;
|
||||
ctx.ino_fd = -1;
|
||||
ctx.ino_wd = -1;
|
||||
ctx.retry_tmr_fd = -1;
|
||||
ctx.dbus_pid = -1;
|
||||
mkdir("/run/geminux", 0755);
|
||||
int lock_fd = open("/tmp/geminux-modem-ctl.lock", O_CREAT | O_RDWR | O_CLOEXEC, 0600);
|
||||
@@ -747,10 +785,16 @@ int main(void) {
|
||||
ctx.sig_fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
|
||||
|
||||
ctx.srv_fd = bind_modem_socket(&ctx);
|
||||
ctx.retry_tmr_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
|
||||
ctx.ino_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
||||
if (ctx.ino_fd >= 0) {
|
||||
ctx.ino_wd = inotify_add_watch(ctx.ino_fd, "/run/geminux",
|
||||
IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE | IN_DELETE);
|
||||
}
|
||||
|
||||
ctx.epfd = epoll_create1(EPOLL_CLOEXEC);
|
||||
int fixed[2] = {ctx.sig_fd, ctx.srv_fd};
|
||||
for (int i = 0; i < 2; i++) {
|
||||
int fixed[4] = {ctx.sig_fd, ctx.srv_fd, ctx.ino_fd, ctx.retry_tmr_fd};
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (fixed[i] >= 0) {
|
||||
struct epoll_event ev = { .events = EPOLLIN, .data.fd = fixed[i] };
|
||||
epoll_ctl(ctx.epfd, EPOLL_CTL_ADD, fixed[i], &ev);
|
||||
@@ -758,25 +802,55 @@ int main(void) {
|
||||
}
|
||||
open_at_port(&ctx);
|
||||
start_dbus_monitor(&ctx);
|
||||
schedule_dbus_retry(&ctx);
|
||||
write_state(&ctx);
|
||||
|
||||
struct epoll_event events[8];
|
||||
bool running = true;
|
||||
while (running) {
|
||||
int n = epoll_wait(ctx.epfd, events, 8, 15000);
|
||||
int n = epoll_wait(ctx.epfd, events, 8, -1);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
break;
|
||||
}
|
||||
if (n == 0) {
|
||||
write_state(&ctx);
|
||||
if (ctx.dbus_fd < 0) start_dbus_monitor(&ctx);
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int fd = events[i].data.fd;
|
||||
if (fd == ctx.sig_fd) {
|
||||
running = false;
|
||||
} else if (fd == ctx.ino_fd) {
|
||||
char ibuf[1024] __attribute__((aligned(__alignof__(struct inotify_event))));
|
||||
bool relevant = false;
|
||||
ssize_t ir;
|
||||
while ((ir = read(ctx.ino_fd, ibuf, sizeof(ibuf))) > 0) {
|
||||
for (char *ptr = ibuf; ptr < ibuf + ir; ) {
|
||||
const struct inotify_event *ev = (const struct inotify_event *)ptr;
|
||||
if (ev->len > 0 &&
|
||||
(strcmp(ev->name, "airplane-mode") == 0 ||
|
||||
strcmp(ev->name, "ims-state") == 0)) {
|
||||
relevant = true;
|
||||
}
|
||||
ptr += sizeof(*ev) + ev->len;
|
||||
}
|
||||
}
|
||||
if (relevant) {
|
||||
write_state(&ctx);
|
||||
if (is_airplane_active()) {
|
||||
stop_dbus_monitor(&ctx);
|
||||
ctx.dbus_retry_ms = 0;
|
||||
arm_retry_timer(&ctx, 0);
|
||||
} else if (ctx.dbus_fd < 0) {
|
||||
start_dbus_monitor(&ctx);
|
||||
schedule_dbus_retry(&ctx);
|
||||
}
|
||||
}
|
||||
} else if (fd == ctx.retry_tmr_fd) {
|
||||
uint64_t exp = 0;
|
||||
while (read(ctx.retry_tmr_fd, &exp, sizeof(exp)) > 0) {}
|
||||
write_state(&ctx);
|
||||
if (!is_airplane_active() && ctx.dbus_fd < 0) {
|
||||
start_dbus_monitor(&ctx);
|
||||
}
|
||||
schedule_dbus_retry(&ctx);
|
||||
} else if (fd == ctx.srv_fd) {
|
||||
if (events[i].events & (EPOLLERR | EPOLLHUP)) {
|
||||
epoll_ctl(ctx.epfd, EPOLL_CTL_DEL, ctx.srv_fd, NULL);
|
||||
@@ -834,6 +908,7 @@ int main(void) {
|
||||
}
|
||||
if (dead || (events[i].events & (EPOLLHUP | EPOLLERR))) {
|
||||
stop_dbus_monitor(&ctx);
|
||||
schedule_dbus_retry(&ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -841,6 +916,8 @@ int main(void) {
|
||||
|
||||
stop_hostless_speech(&ctx);
|
||||
stop_dbus_monitor(&ctx);
|
||||
if (ctx.retry_tmr_fd >= 0) close(ctx.retry_tmr_fd);
|
||||
if (ctx.ino_fd >= 0) close(ctx.ino_fd);
|
||||
if (ctx.sock_path[0]) unlink(ctx.sock_path);
|
||||
close(ctx.epfd);
|
||||
return 0;
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int read_str(const char *path, char *buf, size_t sz) {
|
||||
FILE *f = fopen(path, "r");
|
||||
@@ -18,6 +21,44 @@ static int read_str(const char *path, char *buf, size_t sz) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void query_powerd_status(char *buf, size_t sz) {
|
||||
snprintf(buf, sz, "unavailable");
|
||||
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fd < 0) return;
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
snprintf(addr.sun_path, sizeof(addr.sun_path), "/run/geminux/powerd.sock");
|
||||
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
|
||||
const char *req = "status\n";
|
||||
if (write(fd, req, strlen(req)) > 0) {
|
||||
shutdown(fd, SHUT_WR);
|
||||
char tmp[256] = {0};
|
||||
ssize_t r = read(fd, tmp, sizeof(tmp) - 1);
|
||||
if (r > 0) {
|
||||
while (r > 0 && (tmp[r - 1] == '\n' || tmp[r - 1] == '\r')) tmp[--r] = '\0';
|
||||
snprintf(buf, sz, "%s", tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static void json_escape(const char *in, char *out, size_t out_sz) {
|
||||
size_t j = 0;
|
||||
for (size_t i = 0; in[i] && j + 2 < out_sz; i++) {
|
||||
unsigned char ch = (unsigned char)in[i];
|
||||
if (ch == '"' || ch == '\\') {
|
||||
if (j + 3 >= out_sz) break;
|
||||
out[j++] = '\\';
|
||||
out[j++] = (char)ch;
|
||||
} else if (ch >= 0x20) {
|
||||
out[j++] = (char)ch;
|
||||
}
|
||||
}
|
||||
out[j] = '\0';
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
bool json = (argc > 1 && strcmp(argv[1], "--json") == 0);
|
||||
|
||||
@@ -27,6 +68,66 @@ int main(int argc, char **argv) {
|
||||
read_str("/sys/block/zram0/comp_algorithm", zram_algo, sizeof(zram_algo));
|
||||
read_str("/run/geminux/power-profile", profile, sizeof(profile));
|
||||
|
||||
char air_raw[16] = "";
|
||||
const char *airplane_mode = "unknown";
|
||||
if (read_str("/run/geminux/airplane-mode", air_raw, sizeof(air_raw)) == 0 && air_raw[0]) {
|
||||
airplane_mode = (atoi(air_raw) != 0) ? "on" : "off";
|
||||
}
|
||||
|
||||
char md1_stage[32] = "absent";
|
||||
read_str("/sys/class/mt6797_ccci/md1/stage", md1_stage, sizeof(md1_stage));
|
||||
|
||||
const char *consys_loaded =
|
||||
(access("/sys/module/mt6797_consys", F_OK) == 0) ? "loaded" : "unloaded";
|
||||
|
||||
char spm_pmic_modes[16] = "unknown", spm_allow_md1[16] = "unknown";
|
||||
char spm_last_wake_r12[32] = "0";
|
||||
read_str("/sys/module/mt6797_spm_sleep/parameters/pmic_modes",
|
||||
spm_pmic_modes, sizeof(spm_pmic_modes));
|
||||
read_str("/sys/module/mt6797_spm_sleep/parameters/allow_md1",
|
||||
spm_allow_md1, sizeof(spm_allow_md1));
|
||||
if (read_str("/sys/module/mt6797_spm_sleep/parameters/last_wake_r12",
|
||||
spm_last_wake_r12, sizeof(spm_last_wake_r12)) != 0 ||
|
||||
!spm_last_wake_r12[0]) {
|
||||
snprintf(spm_last_wake_r12, sizeof(spm_last_wake_r12), "0");
|
||||
}
|
||||
|
||||
char gpu_rail_suspends[32] = "unknown";
|
||||
read_str("/sys/module/panfrost/parameters/mt6797_rail_suspends",
|
||||
gpu_rail_suspends, sizeof(gpu_rail_suspends));
|
||||
|
||||
char suspend_ok[32] = "0", suspend_fail[32] = "0", last_failed_dev[64] = "none";
|
||||
read_str("/sys/power/suspend_stats/success", suspend_ok, sizeof(suspend_ok));
|
||||
read_str("/sys/power/suspend_stats/fail", suspend_fail, sizeof(suspend_fail));
|
||||
if (read_str("/sys/power/suspend_stats/last_failed_dev",
|
||||
last_failed_dev, sizeof(last_failed_dev)) != 0 ||
|
||||
!last_failed_dev[0]) {
|
||||
snprintf(last_failed_dev, sizeof(last_failed_dev), "none");
|
||||
}
|
||||
|
||||
char vgpu_state[32] = "absent", vgpu_uv[32] = "unknown";
|
||||
DIR *rd = opendir("/sys/class/regulator");
|
||||
if (rd) {
|
||||
struct dirent *de;
|
||||
while ((de = readdir(rd)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320], name[64] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/regulator/%s/name", de->d_name);
|
||||
if (read_str(p, name, sizeof(name)) == 0 &&
|
||||
(strcmp(name, "vgpu") == 0 || strstr(name, "rt5735") != NULL)) {
|
||||
snprintf(p, sizeof(p), "/sys/class/regulator/%s/state", de->d_name);
|
||||
read_str(p, vgpu_state, sizeof(vgpu_state));
|
||||
snprintf(p, sizeof(p), "/sys/class/regulator/%s/microvolts", de->d_name);
|
||||
read_str(p, vgpu_uv, sizeof(vgpu_uv));
|
||||
break;
|
||||
}
|
||||
}
|
||||
closedir(rd);
|
||||
}
|
||||
|
||||
char powerd_status[256];
|
||||
query_powerd_status(powerd_status, sizeof(powerd_status));
|
||||
|
||||
char bat_cap[32] = "?", bat_status[32] = "Unknown", bat_uv[32] = "?", bat_ua[32] = "?";
|
||||
DIR *d = opendir("/sys/class/power_supply");
|
||||
if (d) {
|
||||
@@ -87,25 +188,45 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
if (json) {
|
||||
char pwr_esc[384], dev_esc[128];
|
||||
json_escape(powerd_status, pwr_esc, sizeof(pwr_esc));
|
||||
json_escape(last_failed_dev, dev_esc, sizeof(dev_esc));
|
||||
printf("{\"profile\":\"%s\",\"battery_pct\":\"%s\",\"battery_status\":\"%s\","
|
||||
"\"voltage_uv\":\"%s\",\"current_ua\":\"%s\",\"online_cpus\":%d,"
|
||||
"\"a72_online\":%d,\"governor\":\"%s\",\"policy0_max_khz\":\"%s\","
|
||||
"\"policy4_max_khz\":\"%s\",\"bt_rfkill_soft\":\"%s\","
|
||||
"\"zram_algo\":\"%s\",\"mem_sleep\":\"%s\"}\n",
|
||||
"\"zram_algo\":\"%s\",\"mem_sleep\":\"%s\","
|
||||
"\"airplane_mode\":\"%s\",\"md1_stage\":\"%s\",\"consys\":\"%s\","
|
||||
"\"spm_pmic_modes\":\"%s\",\"spm_allow_md1\":\"%s\","
|
||||
"\"spm_last_wake_r12\":\"%s\",\"gpu_rail_suspends\":\"%s\","
|
||||
"\"vgpu_state\":\"%s\",\"vgpu_uv\":\"%s\","
|
||||
"\"suspend_success\":\"%s\",\"suspend_fail\":\"%s\","
|
||||
"\"last_failed_dev\":\"%s\",\"powerd_status\":\"%s\"}\n",
|
||||
profile, bat_cap, bat_status, bat_uv, bat_ua, online_cpus, a72_online,
|
||||
gov0, max0, max4, bt_state, zram_algo, mem_sleep);
|
||||
gov0, max0, max4, bt_state, zram_algo, mem_sleep,
|
||||
airplane_mode, md1_stage, consys_loaded, spm_pmic_modes,
|
||||
spm_allow_md1, spm_last_wake_r12, gpu_rail_suspends, vgpu_state,
|
||||
vgpu_uv, suspend_ok, suspend_fail, dev_esc, pwr_esc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("Geminux Power & Efficiency Audit\n");
|
||||
printf("----------------------------------------\n");
|
||||
printf("Power Profile : %s\n", profile);
|
||||
printf("Airplane Mode : %s (MD1 stage=%s, CONSYS=%s)\n",
|
||||
airplane_mode, md1_stage, consys_loaded);
|
||||
printf("Battery Status : %s%% (%s, V=%s uV, I=%s uA)\n", bat_cap, bat_status, bat_uv, bat_ua);
|
||||
printf("CPUs Online : %d total (%d A72 big cores online)\n", online_cpus, a72_online);
|
||||
printf("CPUfreq Governor : %s\n", gov0);
|
||||
printf("CPUfreq Caps : policy0=%s kHz, policy4=%s kHz\n", max0, max4);
|
||||
printf("Bluetooth RFKill : %s\n", bt_state);
|
||||
printf("ZRAM Compression : %s\n", zram_algo);
|
||||
printf("Suspend Modes : %s\n", mem_sleep);
|
||||
printf("Suspend Modes : %s (pmic_modes=%s, allow_md1=%s, last_wake_r12=%s)\n",
|
||||
mem_sleep, spm_pmic_modes, spm_allow_md1, spm_last_wake_r12);
|
||||
printf("Suspend Stats : ok=%s, fail=%s (last_failed=%s)\n",
|
||||
suspend_ok, suspend_fail, last_failed_dev);
|
||||
printf("GPU VGPU Rail : %s (%s uV, rail_suspends=%s)\n",
|
||||
vgpu_state, vgpu_uv, gpu_rail_suspends);
|
||||
printf("Powerd Status : %s\n", powerd_status);
|
||||
return 0;
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <sys/signalfd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/wait.h>
|
||||
@@ -40,6 +41,9 @@
|
||||
#define FG_STATE_FILE "/var/lib/geminux/fuel-gauge.state"
|
||||
#define FG_SYSFS_DIR "/sys/class/power_supply/battery/device"
|
||||
#define MAX_EVDEVS 16
|
||||
#define SPM_ALLOW_MD1_SYSFS "/sys/module/mt6797_spm_sleep/parameters/allow_md1"
|
||||
#define SPM_WAKE_R12_SYSFS "/sys/module/mt6797_spm_sleep/parameters/last_wake_r12"
|
||||
#define SPM_WAKE_MD1_MASK ((1U << 8) | (1U << 25) | (1U << 26))
|
||||
|
||||
typedef struct {
|
||||
int epfd;
|
||||
@@ -70,6 +74,9 @@ typedef struct {
|
||||
bool consys_suspended;
|
||||
bool suspend_ebusy_on_ac;
|
||||
bool low_power_standby;
|
||||
uint32_t suspend_ok_count;
|
||||
uint32_t suspend_fail_count;
|
||||
uint64_t standby_retry_ms;
|
||||
bool airplane_mode;
|
||||
bool air_prev_wifi;
|
||||
bool air_prev_bt_loaded;
|
||||
@@ -245,6 +252,7 @@ static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) {
|
||||
|
||||
static void lock_x11_session(void);
|
||||
static void wake_x11_display_dpms(void);
|
||||
static void blank_x11_display_dpms(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);
|
||||
@@ -263,19 +271,20 @@ static void toggle_backlight(powerd_ctx_t *ctx, bool on) {
|
||||
DIR *d = opendir("/sys/class/backlight");
|
||||
if (!d) {
|
||||
if (on) wake_x11_display_dpms();
|
||||
else blank_x11_display_dpms();
|
||||
return;
|
||||
}
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320];
|
||||
char p[320], pwr[320];
|
||||
snprintf(p, sizeof(p), "/sys/class/backlight/%s/brightness", de->d_name);
|
||||
snprintf(pwr, sizeof(pwr), "/sys/class/backlight/%s/bl_power", de->d_name);
|
||||
if (!on) {
|
||||
save_active_brightness(ctx, p);
|
||||
write_sysfs_str(p, "0");
|
||||
write_sysfs_str(pwr, "4");
|
||||
} else {
|
||||
char pwr[320];
|
||||
snprintf(pwr, sizeof(pwr), "/sys/class/backlight/%s/bl_power", de->d_name);
|
||||
write_sysfs_str(pwr, "0");
|
||||
char restore[32];
|
||||
int val = ctx->saved_brightness > 0 ? ctx->saved_brightness : 128;
|
||||
@@ -286,6 +295,7 @@ static void toggle_backlight(powerd_ctx_t *ctx, bool on) {
|
||||
}
|
||||
closedir(d);
|
||||
if (on) wake_x11_display_dpms();
|
||||
else blank_x11_display_dpms();
|
||||
}
|
||||
|
||||
static void dim_backlight(powerd_ctx_t *ctx) {
|
||||
@@ -539,19 +549,22 @@ static void lock_x11_session(void) {
|
||||
close(pfd[0]);
|
||||
}
|
||||
|
||||
/* Force X11 DPMS On and reset the X11 screen saver inside a double-forked,
|
||||
* privilege-dropped child so XIOError or display restarts never affect root
|
||||
* powerd, and external HDMI/client DPMS states always wake across S3/lid. */
|
||||
static void wake_x11_display_dpms(void) {
|
||||
/* Force X11 DPMS On/Off inside a synchronous, 500 ms timeout-capped,
|
||||
* privilege-dropped child so XIOError or X restarts never stall powerd,
|
||||
* and blanking cuts CRTC/DSI/LCD scanout before suspend/standby. */
|
||||
static void set_x11_display_dpms(bool on) {
|
||||
if (access("/tmp/.X11-unix/X0", F_OK) != 0) return;
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) return;
|
||||
if (pid == 0) {
|
||||
pid_t grand = fork();
|
||||
if (grand < 0) _exit(1);
|
||||
if (grand > 0) _exit(0);
|
||||
|
||||
unblock_signals();
|
||||
setsid();
|
||||
signal(SIGALRM, SIG_DFL);
|
||||
struct itimerval it;
|
||||
memset(&it, 0, sizeof(it));
|
||||
it.it_value.tv_usec = 500000;
|
||||
setitimer(ITIMER_REAL, &it, NULL);
|
||||
|
||||
int devnull = open("/dev/null", O_RDWR);
|
||||
if (devnull >= 0) {
|
||||
dup2(devnull, STDIN_FILENO);
|
||||
@@ -579,19 +592,32 @@ static void wake_x11_display_dpms(void) {
|
||||
|
||||
int ev_base = 0, err_base = 0;
|
||||
if (DPMSQueryExtension(dpy, &ev_base, &err_base) && DPMSCapable(dpy)) {
|
||||
CARD16 power_level = DPMSModeOn;
|
||||
BOOL dpms_enabled = False;
|
||||
if (DPMSInfo(dpy, &power_level, &dpms_enabled) &&
|
||||
dpms_enabled && power_level != DPMSModeOn) {
|
||||
DPMSForceLevel(dpy, DPMSModeOn);
|
||||
if (on) {
|
||||
CARD16 power_level = DPMSModeOn;
|
||||
BOOL dpms_enabled = False;
|
||||
if (DPMSInfo(dpy, &power_level, &dpms_enabled) &&
|
||||
dpms_enabled && power_level != DPMSModeOn) {
|
||||
DPMSForceLevel(dpy, DPMSModeOn);
|
||||
}
|
||||
} else {
|
||||
DPMSEnable(dpy);
|
||||
DPMSForceLevel(dpy, DPMSModeOff);
|
||||
}
|
||||
}
|
||||
XResetScreenSaver(dpy);
|
||||
if (on) XResetScreenSaver(dpy);
|
||||
XSync(dpy, False);
|
||||
XCloseDisplay(dpy);
|
||||
_exit(0);
|
||||
}
|
||||
waitpid(pid, NULL, 0);
|
||||
while (waitpid(pid, NULL, 0) < 0 && errno == EINTR) {}
|
||||
}
|
||||
|
||||
static void wake_x11_display_dpms(void) {
|
||||
set_x11_display_dpms(true);
|
||||
}
|
||||
|
||||
static void blank_x11_display_dpms(void) {
|
||||
set_x11_display_dpms(false);
|
||||
}
|
||||
|
||||
static int run_cmd_sync(const char *const argv[]) {
|
||||
@@ -854,7 +880,15 @@ static void unload_consys_stack(bool include_wlan_core) {
|
||||
* unloading reverse dependents lets mt6797_consys_remove() power off CONN/VCN rails. */
|
||||
static void prepare_consys_for_suspend(powerd_ctx_t *ctx) {
|
||||
reap_consys_restore(ctx, true);
|
||||
reap_radio_worker(ctx, true);
|
||||
if (ctx->airplane_mode) {
|
||||
if (access("/sys/module/mt6797_consys", F_OK) == 0) {
|
||||
unload_consys_stack(true);
|
||||
}
|
||||
char st[16] = {0};
|
||||
if (read_sysfs_line("/sys/class/mt6797_ccci/md1/stage", st, sizeof(st)) == 0 && atoi(st) > 0) {
|
||||
write_sysfs_str("/sys/class/mt6797_ccci/md1/stage", "0");
|
||||
}
|
||||
ctx->consys_suspended = false;
|
||||
return;
|
||||
}
|
||||
@@ -978,11 +1012,18 @@ static void apply_airplane_mode(powerd_ctx_t *ctx, bool enable) {
|
||||
const char *mm_stop[] = {"/sbin/rc-service", "--quiet", "modemmanager", "stop", NULL};
|
||||
run_cmd_sync(mm_stop);
|
||||
send_unix_line(CCCI_SOCK_PATH, "flight on");
|
||||
char md_st[16] = {0};
|
||||
if (read_sysfs_line("/sys/class/mt6797_ccci/md1/stage", md_st, sizeof(md_st)) == 0 &&
|
||||
atoi(md_st) > 0) {
|
||||
write_sysfs_str("/sys/class/mt6797_ccci/md1/stage", "0");
|
||||
}
|
||||
write_text_file(MODEM_STATE_FILE, "AIRPLANE\n", 0666);
|
||||
|
||||
const char *bt_stop[] = {"/sbin/rc-service", "--quiet", "bluetooth", "stop", NULL};
|
||||
const char *nm_off[] = {"/usr/bin/nmcli", "radio", "wifi", "off", NULL};
|
||||
const char *rf_w[] = {"/usr/sbin/rfkill", "block", "wifi", NULL};
|
||||
const char *rf_b[] = {"/usr/sbin/rfkill", "block", "bluetooth", NULL};
|
||||
run_cmd_sync(bt_stop);
|
||||
run_cmd_sync(nm_off);
|
||||
run_cmd_sync(rf_w);
|
||||
run_cmd_sync(rf_b);
|
||||
@@ -1144,16 +1185,36 @@ static void restore_cpus_after_resume(powerd_ctx_t *ctx) {
|
||||
ctx->policy_applied = false;
|
||||
}
|
||||
|
||||
static bool read_spm_allow_md1(void) {
|
||||
char val[16] = {0};
|
||||
if (read_sysfs_line(SPM_ALLOW_MD1_SYSFS, val, sizeof(val)) != 0) return false;
|
||||
return (val[0] == 'Y' || val[0] == 'y' || val[0] == '1');
|
||||
}
|
||||
|
||||
static uint32_t read_spm_last_wake_r12(void) {
|
||||
char val[32] = {0};
|
||||
if (read_sysfs_line(SPM_WAKE_R12_SYSFS, val, sizeof(val)) != 0 || !val[0]) return 0;
|
||||
return (uint32_t)strtoul(val, NULL, 0);
|
||||
}
|
||||
|
||||
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 read_spm_allow_md1();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint64_t next_standby_retry_ms(uint64_t cur) {
|
||||
if (cur < 3000) return 3000;
|
||||
if (cur < 10000) return 10000;
|
||||
if (cur < 30000) return 30000;
|
||||
return 60000;
|
||||
}
|
||||
|
||||
static void exit_low_power_standby(powerd_ctx_t *ctx) {
|
||||
ctx->standby_retry_ms = 0;
|
||||
if (!ctx->low_power_standby) return;
|
||||
ctx->low_power_standby = false;
|
||||
restore_cpus_after_resume(ctx);
|
||||
@@ -1173,6 +1234,7 @@ static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
toggle_backlight(ctx, false);
|
||||
save_fuel_gauge_state(ctx);
|
||||
if (ctx->policy.on_ac_power) {
|
||||
ctx->standby_retry_ms = 0;
|
||||
ctx->suspend_ebusy_on_ac = true;
|
||||
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
|
||||
apply_cpu_sysfs(ctx, false);
|
||||
@@ -1181,7 +1243,6 @@ static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
refresh_lid_state(ctx);
|
||||
bool was_lid_closed = ctx->policy.lid_closed;
|
||||
bool prev_ac = ctx->policy.on_ac_power;
|
||||
exit_low_power_standby(ctx);
|
||||
prepare_consys_for_suspend(ctx);
|
||||
prepare_cpus_for_suspend();
|
||||
bool can_deep = can_enter_deep_mem_sleep(ctx);
|
||||
@@ -1189,6 +1250,12 @@ static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
if (can_deep) {
|
||||
write_sysfs_str("/sys/power/mem_sleep", "deep");
|
||||
suspended = (write_sysfs_str("/sys/power/state", "mem") == 0);
|
||||
if (suspended) {
|
||||
ctx->suspend_ok_count++;
|
||||
ctx->standby_retry_ms = 0;
|
||||
} else {
|
||||
ctx->suspend_fail_count++;
|
||||
}
|
||||
}
|
||||
if (!suspended) {
|
||||
refresh_lid_state(ctx);
|
||||
@@ -1198,12 +1265,18 @@ static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
(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);
|
||||
if (can_enter_deep_mem_sleep(ctx) || ctx->airplane_mode || ctx->radio_worker_pid > 0) {
|
||||
ctx->standby_retry_ms = next_standby_retry_ms(ctx->standby_retry_ms);
|
||||
arm_timer_ms(ctx->tmr_fd, ctx->standby_retry_ms);
|
||||
} else {
|
||||
ctx->standby_retry_ms = 0;
|
||||
arm_timer_ms(ctx->tmr_fd, 0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
ctx->low_power_standby = false;
|
||||
restore_cpus_after_resume(ctx);
|
||||
uint32_t wake_r12 = suspended ? read_spm_last_wake_r12() : 0;
|
||||
bool modem_wake = suspended && ((wake_r12 & SPM_WAKE_MD1_MASK) != 0);
|
||||
bool saw_wake_input = suspended ? wait_post_resume_lid_events(ctx, was_lid_closed, 90)
|
||||
: (was_lid_closed && !ctx->policy.lid_closed);
|
||||
refresh_ac_and_hdmi(ctx);
|
||||
@@ -1218,6 +1291,14 @@ static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
geminux_should_suspend_on_lid(&ctx->policy);
|
||||
bool quiet_timer_resleep = suspended && !ctx->suspend_ebusy_on_ac &&
|
||||
(want_resleep_closed || want_resleep_open);
|
||||
if (!quiet_timer_resleep) {
|
||||
ctx->low_power_standby = false;
|
||||
ctx->standby_retry_ms = 0;
|
||||
restore_cpus_after_resume(ctx);
|
||||
} else {
|
||||
ctx->low_power_standby = true;
|
||||
ctx->standby_retry_ms = 0;
|
||||
}
|
||||
if (!quiet_timer_resleep &&
|
||||
(!ctx->policy.lid_closed || !geminux_should_suspend_on_lid(&ctx->policy) ||
|
||||
ctx->suspend_ebusy_on_ac)) {
|
||||
@@ -1230,14 +1311,13 @@ static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
toggle_backlight(ctx, true);
|
||||
apply_cpu_policy(ctx, true);
|
||||
evaluate_idle(ctx);
|
||||
} else if (quiet_timer_resleep) {
|
||||
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
|
||||
arm_timer_ms(ctx->tmr_fd, modem_wake ? 3500 : 300);
|
||||
} else {
|
||||
toggle_backlight(ctx, false);
|
||||
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
|
||||
if (quiet_timer_resleep) {
|
||||
arm_timer_ms(ctx->tmr_fd, 2000);
|
||||
} else {
|
||||
arm_timer_ms(ctx->tmr_fd, 0);
|
||||
}
|
||||
arm_timer_ms(ctx->tmr_fd, 0);
|
||||
apply_cpu_sysfs(ctx, true);
|
||||
}
|
||||
}
|
||||
@@ -1307,6 +1387,9 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) {
|
||||
} else if (strncmp(buf, "inhibit del ", 12) == 0) {
|
||||
geminux_policy_del_inhibit(&ctx->policy, buf + 12);
|
||||
apply_cpu_policy(ctx, false);
|
||||
if (ctx->policy.lid_closed && geminux_should_suspend_on_lid(&ctx->policy)) {
|
||||
arm_timer_ms(ctx->tmr_fd, 1000);
|
||||
}
|
||||
dprintf(cfd, "OK inhibits=%d\n", ctx->policy.inhibit_count);
|
||||
} else if (strcmp(buf, "lid-suspend on") == 0) {
|
||||
ctx->policy.lid_suspend_enabled = true;
|
||||
@@ -1339,14 +1422,19 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) {
|
||||
apply_airplane_mode(ctx, !ctx->airplane_mode);
|
||||
dprintf(cfd, "OK airplane=%s\n", ctx->airplane_mode ? "on" : "off");
|
||||
} else if (strcmp(buf, "status") == 0) {
|
||||
dprintf(cfd, "profile=%s ac=%d hdmi=%d lid_closed=%d lid_suspend=%d inhibits=%d airplane=%d\n",
|
||||
dprintf(cfd,
|
||||
"profile=%s ac=%d hdmi=%d lid_closed=%d lid_suspend=%d inhibits=%d "
|
||||
"airplane=%d standby=%d suspend_ok=%u suspend_fail=%u\n",
|
||||
geminux_policy_profile_name(ctx->policy.profile),
|
||||
ctx->policy.on_ac_power ? 1 : 0,
|
||||
ctx->policy.hdmi_connected ? 1 : 0,
|
||||
ctx->policy.lid_closed ? 1 : 0,
|
||||
ctx->policy.lid_suspend_enabled ? 1 : 0,
|
||||
ctx->policy.inhibit_count,
|
||||
ctx->airplane_mode ? 1 : 0);
|
||||
ctx->airplane_mode ? 1 : 0,
|
||||
ctx->low_power_standby ? 1 : 0,
|
||||
ctx->suspend_ok_count,
|
||||
ctx->suspend_fail_count);
|
||||
} else {
|
||||
dprintf(cfd, "ERR unknown command\n");
|
||||
}
|
||||
@@ -1470,8 +1558,17 @@ int main(void) {
|
||||
struct signalfd_siginfo si;
|
||||
while (read(ctx.sig_fd, &si, sizeof(si)) > 0) {
|
||||
if (si.ssi_signo == SIGCHLD) {
|
||||
bool had_radio_worker = (ctx.radio_worker_pid > 0);
|
||||
reap_consys_restore(&ctx, false);
|
||||
reap_radio_worker(&ctx, false);
|
||||
if (had_radio_worker && ctx.radio_worker_pid < 0 &&
|
||||
ctx.low_power_standby && can_enter_deep_mem_sleep(&ctx) &&
|
||||
!ctx.policy.on_ac_power && !ctx.policy.hdmi_connected &&
|
||||
ctx.policy.inhibit_count == 0 &&
|
||||
(ctx.policy.lid_closed || ctx.idle_stage == IDLE_STAGE_LOCKED_BLANK)) {
|
||||
ctx.standby_retry_ms = 0;
|
||||
arm_timer_ms(ctx.tmr_fd, 500);
|
||||
}
|
||||
} else if (si.ssi_signo == SIGHUP) {
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
apply_cpu_policy(&ctx, true);
|
||||
|
||||
Reference in new issue
Block a user