diff --git a/AGENTS.md b/AGENTS.md index 0747357..cd226cf 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -62,7 +62,12 @@ All cross-compilation scripts run inside rootless Docker (`alpine:3.24` `armv7` - **Documentation**: keep `README.md`, `MANUAL.md`, `SDK.md`, and `CHANGELOG.md` synchronized with any new user-facing features, SDK functions, or keybindings, and do not leave third-party `.md` files inside `src/` or `edl/`. - **Artifact sources**: `prebuilt/` is the single source of truth for compiled artifacts and `scripts/rootfs-packages.txt` for the `armv7` package set. Never edit `overlay/rootfs/` copies of binaries by hand; run `./scripts/sync-prebuilt-overlay.sh` after changing `prebuilt/`. - **Package hygiene**: keep `scripts/rootfs-packages.txt` free of Rust-based top-level packages (use C `sudo`, never `sudo-rs`) and Wayland-purpose packages; `libwpe` and the `wayland-libs-*` shared libraries stay because GTK3/WebKitGTK link them directly, but no Wayland compositor, backend, or viewer is shipped. `scripts/check-rebuild.sh` enforces this. -- **Power management**: idle blanking is `dwm`'s native evdev timer (`src/sxmo-dwm/dwm.c`) plus `bananarchy-idle-blank.sh`; the timeout lives in `~/.config/sxmo/idle_blank_timeout` (default 300 s, `0` = off), is edited via **Config -> Screen blank** / `bananarchy-idle-timeout.sh`, and is reloaded on `SIGUSR1`. `~/.cache/sxmo/sxmo.noidle` and `SXMO_UNLOCK_IDLE_TIME` still override it. `bananarchy-screen-wake.sh` (called from the ring and notification hooks) wakes the panel for calls/SMS and resets the timer; locking or closing the lid blanks immediately. Deep standby is `bananarchy-suspend.sh` / `bananarchy-deep-standby.sh` / `sxmo_autosuspend.sh` / `sxmo_suspend.sh`, gated by `/etc/bananarchy-no-deep-standby` (shipped present = disabled) and inert because `CONFIG_SUSPEND` is off. A full custom module tree rebuild is deliberately **not** planned: this firmware has no PSCI `SYSTEM_SUSPEND`, so `CONFIG_SUSPEND` would only give s2idle (`freeze`), not `mem` / DDR self-refresh, for <1 mA of saving (see `BATTERY-OPTIMIZATION-ANALYSIS.md` §14). Never force `sxmo.noidle`/`sxmo.nosuspend` from the session scripts and never reintroduce the infinite `sxmo_not_suspendable` wakelock. `/etc/bananarchy/` is root-only. +- **Power management**: idle blanking is `dwm`'s native evdev timer (`src/sxmo-dwm/dwm.c`) plus `bananarchy-idle-blank.sh`; the timeout lives in `~/.config/sxmo/idle_blank_timeout` (default 300 s, `0` = off), is edited via **Config -> Screen blank** / `bananarchy-idle-timeout.sh`, and is reloaded on `SIGUSR1`. `~/.cache/sxmo/sxmo.noidle` and `SXMO_UNLOCK_IDLE_TIME` still override it. `bananarchy-screen-wake.sh` (called from the ring and notification hooks) wakes the panel for calls/SMS and resets the timer; locking or closing the lid blanks immediately. Runtime power policy: `/etc/bananarchy-power-policy.conf` + +`/usr/bin/bananarchy-power-policy.sh` (OpenRC default runlevel, `doas` rule) +drive `WIFI_LISTEN_INTERVAL` (wcn36xx module option), `MODEM_DRX_MODE` +(`AT$QCDRX`) and `DSDS_SECONDARY_MODE=eco` (QMI secondary GW provisioning +session park on `sxmo_wm.sh display off/on`). Never park a SIM with +`--uim-sim-power-off`: it clears PIN verification. Deep standby is `bananarchy-suspend.sh` / `bananarchy-deep-standby.sh` / `sxmo_autosuspend.sh` / `sxmo_suspend.sh`, gated by `/etc/bananarchy-no-deep-standby` (shipped present = disabled) and inert because `CONFIG_SUSPEND` is off. A full custom module tree rebuild is deliberately **not** planned: this firmware has no PSCI `SYSTEM_SUSPEND`, so `CONFIG_SUSPEND` would only give s2idle (`freeze`), not `mem` / DDR self-refresh, for <1 mA of saving (see `BATTERY-OPTIMIZATION-ANALYSIS.md` §14). Never force `sxmo.noidle`/`sxmo.nosuspend` from the session scripts and never reintroduce the infinite `sxmo_not_suspendable` wakelock. `/etc/bananarchy/` is root-only. - **Image residue**: `rebuild-image.sh` strips `/var/cache/apk`, all `apk.log*` files, and unused gettext locale trees before `mke2fs` and aborts the build if any apk cache/log remains; never add an `apk` call after that guard. - **No on-device compiler**: `scripts/rootfs-packages.txt` ships no `gcc`/`g++`/`musl-dev`/`make` and no Janet runtime (`janet`, `janet-dev`, `jpm`); cross-compile native code with `scripts/build-*.sh`. On-device development uses the Yabasic FFI SDK (`neoui.yab` / `neonet.yab`), which requires `yabasic`, `libffi`, `libneoui.so`, and `libcurl.so.4` — never remove those. `scripts/check-rebuild.sh` guards both sides. - **Core tools**: `curl`, `bc`, `ed`, `rlwrap`, `mandoc`, `nano`, `less`, and `frotz` are pinned core tools asserted by `scripts/check-rebuild.sh`. Perl, Lua, optional Tcl extensions, and the `sqlite` CLI are excluded, while `tcl`, `python3`, `bash`, `luajit`, `gawk`, `jq`, `gojq`, and `quickjs` remain as hard runtime dependencies. diff --git a/BATTERY-OPTIMIZATION-ANALYSIS.md b/BATTERY-OPTIMIZATION-ANALYSIS.md index 7ae12b8..1d8a4c2 100644 --- a/BATTERY-OPTIMIZATION-ANALYSIS.md +++ b/BATTERY-OPTIMIZATION-ANALYSIS.md @@ -669,3 +669,118 @@ and Wi-Fi layers: and no kernel-side change can fix it. None of these four are unlocked by rebuilding the module tree. + +--- + +## 15. Kernel-side fix and runtime power policies (implemented) + +Section 14 concluded that a full module tree rebuild is not worthwhile. This +section records the two things that *were* worth doing: one source-only kernel +fix, and a configurable runtime power-policy layer for the modem/Wi-Fi/DSDS +items that section 14.8 identified as the real remaining levers. + +### 15.1 VM-BMS FIFO sanity clamp (kernel patch 0007) + +`patches/kernel/0007-msm8909-bms-fifo-wifi-policy.patch` makes the VM-BMS +VADC conversion defensive: `PM8916_BMS_VM_RAW_MIN 8000` / +`PM8916_BMS_VM_RAW_MAX 16000` (~2.34-4.69 V) with a +`PM8916_BMS_VM_NOMINAL_UV 3700000` fallback, and rejects out-of-window FIFO +samples in both the IRQ handler and the probe-time pre-read. An uninitialised +`0xffff` sample (from the FIFO or the OCV register read on probe/resume) +previously produced `voltage_now=19.2 V`, `health=Over voltage`, +`status=Full`, `capacity=100` for the first minutes after a hard reset. The +conversion guard covers every call site, including the two OCV register +reads that a FIFO-only clamp misses. This is a source-only change: no config +symbol, vermagic, or module-visible struct changes, so the known-good +pmaports module ABI stays intact. + +### 15.2 Wi-Fi beacon listen interval (kernel patch 0007 + policy) + +`wcn36xx` gained a `listen_interval` module parameter (1-10, default 5, +writable via `/sys/module/wcn36xx/parameters/listen_interval`). The previous +hardcoded value was 3. `/etc/bananarchy-power-policy.conf` exposes +`WIFI_LISTEN_INTERVAL`; `bananarchy-power-policy.sh` regenerates +`/etc/modprobe.d/10-bananarchy-power.conf` from it. The option is read at +module load, so a change needs a reboot or a `wcn36xx` reload. Higher values +cut idle `wcn36xx_rx` wakeups (measured 2.43/s at interval 3) at the cost of +inbound packet latency. + +### 15.3 Modem DRX policy + +Live probing of the modem (`MPSS.JO.3.0.c4`, 2018 firmware) showed: + +| Command | Result | +|---|---| +| `AT+CPSMS=?`, `AT+CPSMS?` | `ERROR` (PSM unsupported) | +| `AT+CEDRXS=?`, `AT+CEDRXS?` | `ERROR` (eDRX unsupported) | +| `AT$QCDRX=?` | `$QCDRX: (0,6-9)` | +| `AT$QCDRX?` | `$QCDRX: 0` | +| `AT$QCDRX=6..9` | `OK`, readback correct, `AT+COPS?` stays registered | +| `qmicli --nas-get-drx` | `unknown` (read-only, no setter) | + +So the standard 3GPP PSM/eDRX AT commands from section 14.8 are **not +available** on this firmware. The Qualcomm-proprietary `AT$QCDRX` DRX mode is +the supported equivalent. `MODEM_DRX_MODE` in +`/etc/bananarchy-power-policy.conf` accepts `0` (firmware default) or `6-9` +and is applied at boot and on every screen wake via +`msm-modem-uim-selection at auto`. Default is **0** because values above 6 +are undocumented here: `AT$QCDRX=9` made the AT channel briefly +unresponsive during testing. Users who accept longer paging latency can opt +in to 6. + +### 15.4 DSDS secondary-SIM policy + +Two mechanisms were tested on the live phone: + +- `qmicli --uim-sim-power-off=2` / `--uim-sim-power-on=2` **works**, but the + power-on leaves a PIN-enabled SIM in `pin1-or-upin-pin-required` / + `enabled-not-verified` and the secondary subscription unregistered. A warm + reboot and `AT+CFUN=1,1` do not restore the cached PIN verification. This + mechanism is therefore **rejected** for automatic parking. +- `qmicli --uim-change-provisioning-session='activate=no,session-type=secondary-gw-provisioning'` + deactivates the secondary GW session cleanly, and reactivation with + `slot=2,activate=yes,session-type=secondary-gw-provisioning,aid=` + restores it without power-cycling the SIM (no PIN loss). Verified on the + device. + +`DSDS_SECONDARY_MODE=eco` (default `active`) uses the second mechanism: after +`DSDS_PARK_DELAY` seconds of a dark panel, `bananarchy-power-policy.sh` +parks the secondary subscription and reactivates it on wake. The AID is read +from `--uim-get-card-status` at park time and cached in `/run`. Hooks in +`sxmo_wm.sh display off/on` drive it, so both the dwm idle blank and the +locker screenoff paths are covered. **SIM2 is unreachable while parked**; +that is the deliberate trade for its RF saving. + +### 15.5 What is still not implementable + +- **DDR self-refresh / `mem` suspend** needs PSCI `SYSTEM_SUSPEND` in lk2nd + (section 14.3). No kernel or userspace change can provide it here. +- **Standard LTE PSM/eDRX** needs modem firmware support that this + `MPSS.JO.3.0.c4` build does not expose (section 15.3). +- **Discharge-current measurement** remains impossible over USB RNDIS + (charging at 540 mA, no `current_now`, no coulomb counter). All savings + above are expressed as wakeup/IRQ counts, not mA. + +### 15.6 Deployment and verification + +- `scripts/build-kernel-audio.sh` applies patch 0007 in both the cached-tree + and fresh-tarball paths; the resolved `.config` is unchanged, so vermagic + and `struct dev_pm_info` stay compatible with the untouched package + modules. +- `bananarchy-power-policy.sh` is installed as an OpenRC default-runlevel + service; `doas.d/50-sxmo.conf` permits the X session to invoke it. +- Live verification on the connected phone: `status` reports + `WIFI_LISTEN_INTERVAL=5` / `wcn36xx_param=5` / `modem_drx_now=$QCDRX: 0`; + `MODEM_DRX_MODE=6` applies and reads back as `$QCDRX: 6` with both SIMs + still registered; `DSDS_SECONDARY_MODE=eco` parks the secondary GW session + after the delay and restores it (with its captured slot and AID) on wake; + `WIFI_LISTEN_INTERVAL=0` removes the module option. The rebuilt modules + load with the same `6.12.1-msm8916 SMP preempt mod_unload ARMv7 p2v8` + vermagic as the package modules, and the defensive BMS conversion is + present in the disassembly. The rebuilt image's initramfs and rootfs both + contain the new `pm8916_bms_vm.ko.zst` / `wcn36xx.ko.zst` (SHA-256 match), + and `boot.img` still carries the byte-identical known-good zImage + DTB. +- The live phone's `/boot/boot.img` was deliberately left untouched: the + boot-time `0xffff` transient only disappears after a reflash with the + rebuilt image. Reloading `pm8916_bms_vm` at runtime from `/lib/modules` + confirms the new module reports sane voltage and `health=Good`. diff --git a/CHANGELOG.md b/CHANGELOG.md index 56a4757..0ace9a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,40 @@ Release versions follow the `
` format (where `2026` is `a`, --- +## Unreleased — VM-BMS clamp and runtime power policies + +- **Kernel fix (patch 0007)**: `pm8916_bms_vm.c` now bounds VADC raw samples + to `8000..16000` (~2.34-4.69 V) with a 3.7 V nominal fallback, covering + the FIFO IRQ, the probe-time FIFO pre-read and both OCV register reads. + An uninitialised `0xffff` sample previously produced `voltage_now=19.2 V`, + `health=Over voltage` and `capacity=100` for the first minutes after a hard + reset. Source-only change; no config, vermagic or struct layout change. +- **Configurable Wi-Fi listen interval**: `wcn36xx` gained a + `listen_interval` module parameter (1-10, default 5, was hardcoded 3). + `/etc/bananarchy-power-policy.conf` (`WIFI_LISTEN_INTERVAL`) drives + `/etc/modprobe.d/10-bananarchy-power.conf` via + `bananarchy-power-policy.sh`. Higher values cut idle RX wakeups at the cost + of inbound packet latency; changes apply at module load. +- **Modem DRX policy**: standard `AT+CPSMS` / `AT+CEDRXS` are unsupported on + this modem firmware, but the Qualcomm `AT$QCDRX` DRX mode (0, 6-9) works. + `MODEM_DRX_MODE` in the policy config is applied at boot and on screen + wake. Default `0` (firmware default) because higher values are + undocumented and can delay paging; `6` is the conservative opt-in. +- **DSDS secondary-SIM policy**: `DSDS_SECONDARY_MODE=eco` parks the + secondary subscription after `DSDS_PARK_DELAY` seconds of a dark panel and + restores it on wake, using QMI provisioning-session + activate/deactivate (not SIM power-off, which loses PIN verification). + Default `active` keeps both SIMs reachable. Hooks in + `sxmo_wm.sh display off/on` cover both idle blank and locker screenoff. +- **New `bananarchy-power-policy.sh`** plus an OpenRC default-runlevel + service and a `doas` rule. `status` reports the effective policy, the + `wcn36xx` parameter, and the current modem DRX mode. +- **Not implementable here**: DDR self-refresh / `mem` suspend (needs PSCI + `SYSTEM_SUSPEND` in lk2nd) and standard LTE PSM/eDRX (not exposed by + `MPSS.JO.3.0.c4`). See `BATTERY-OPTIMIZATION-ANALYSIS.md` §15. + +--- + ## Unreleased — kernel power headroom analysis (no runtime change) A second live measurement pass on the connected `nokia-argon` (screen off, diff --git a/MANUAL.md b/MANUAL.md index a96fa66..95c2f2a 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -103,6 +103,10 @@ sudo dd if=./images/bananarchy-nokia-argon.img of=/dev/sdX bs=4M status=progress > **Screen blanking, idle blanking, deep standby, passcode locking (`xlocker`), waking, and brightness persistence**: > While unlocked (and with the lid open), the display blanks automatically after the configured **Screen blank** timeout — **5 minutes by default** — via `dwm`'s own evdev input timer and `bananarchy-idle-blank.sh`. Change it in **Config -> Screen blank** (`Off`, 30 s, 1/5/10/30 min), which writes `~/.config/sxmo/idle_blank_timeout`; `bananarchy-idle-timeout.sh get|label|set ` does the same from the shell, and `SXMO_UNLOCK_IDLE_TIME` or `~/.cache/sxmo/sxmo.noidle` still override/disable it. Closing the slider/lid or locking from the **Power** menu blanks the panel **immediately** (both `Lock` and `Lock (Screen off)` now use `xlocker screenoff`), and an incoming call or SMS/notification turns it back on **immediately** via `bananarchy-screen-wake.sh`, which also resets `dwm`'s idle timer so it cannot blank again right away. Any keypad, lid or power key also restores the display without engaging the passcode locker. X11 screensaver timeouts stay at `0 0 0`; DPMS is driven by the backlight state. Optional **deep standby** is currently **unavailable**: the shipped kernel keeps the pmaports module ABI and has no sleep states in `/sys/power/state`. It stays disabled via `/etc/bananarchy-no-deep-standby`. Even a full custom kernel module tree would not help here: this firmware has no PSCI `SYSTEM_SUSPEND`, so only s2idle (`freeze`) would be possible, without DDR self-refresh, and the saving would be under 1 mA (see `BATTERY-OPTIMIZATION-ANALYSIS.md`). Active calls, SSH sessions and VPN/tether connections would block suspension. Locking the phone (`Power -> Lock`, `Power -> Lock (Screen off)`, `2x End / Power`, or closing the physical slider) activates the fullscreen `240x320` **Passcode Locker (`xlocker` / `locker.so`)**, which verifies your numeric system password (`147147` by default, checked against `/etc/shadow` via `/usr/bin/bananarchy-verify-passcode`). While locked, there is no keypad, softkey, slider, or power-button bypass (`dmenu`, `xdrawer`, terminal spawning, and desktop shortcuts are blocked, and only incoming calls can temporarily overlay `xcallscreen`). Screen and keypad brightness levels configured in **XDrawer** or **Config -> Brightness** are persisted in `~/.config/sxmo/brightness` and `/tmp/sxmo_saved_brightness` and restored on every wake. + +> **Runtime power policy (`/etc/bananarchy-power-policy.conf`)**: +> `WIFI_LISTEN_INTERVAL` (1-10, default 5) sets the `wcn36xx` beacon listen interval via `/etc/modprobe.d/10-bananarchy-power.conf`; higher values cut idle Wi-Fi RX wakeups but delay inbound packets, and the change applies at module load (reboot or reload `wcn36xx`). `MODEM_DRX_MODE` (default `0`) applies the Qualcomm `AT$QCDRX` DRX mode at boot and on every wake; `6-9` are progressively longer paging cycles that lower modem idle current at the cost of call/SMS setup latency, while `0` is the firmware default. The standard `AT+CPSMS` / `AT+CEDRXS` PSM/eDRX commands are not supported by this modem firmware. `DSDS_SECONDARY_MODE=eco` (default `active`) deactivates the secondary SIM's GW provisioning session after `DSDS_PARK_DELAY` seconds (default 300) of a dark panel and restores it on wake; **SIM2 is unreachable while parked**. This uses QMI provisioning sessions, not SIM power-off, so a PIN-locked SIM stays locked. Edit the file as root and run `doas /usr/bin/bananarchy-power-policy.sh apply`, or reboot; `doas /usr/bin/bananarchy-power-policy.sh status` reports the effective values. DDR self-refresh / `mem` suspend is unavailable on this firmware (no PSCI `SYSTEM_SUSPEND`); see `BATTERY-OPTIMIZATION-ANALYSIS.md` §15. + #### 3.1.1 Window tiling modes and switching combos By default, `dwm` uses the **vertical bottom-stack (`bstack`, `TTT`)** layout so multiple `st` terminals stack **vertically (one under another)** to preserve full line width on the `240x320` portrait screen. diff --git a/README.md b/README.md index 49adade..9b926ac 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ ## Key features ### System and hardware support -- **Mainline Linux 6.12 kernel**: custom patches and out-of-tree drivers for Qualcomm **Adreno 304 (`FD304`, 96 KB GMEM)** 3D GPU acceleration (`drm/msm` + `qcom-iommu`), Qualcomm QDSP6/Q6Voice audio routing, Dual-SIM modem control, the rear 2MP GalaxyCore `GC2395` / `GC2385` / `GC2365` camera (`/dev/video0`), the `SY7807` flashlight LED (`white:torch`), physical keypad/slider events, and clean PM8909 poweroff. The kernel image is built from `config-bananarchy-msm8909.armv7` with the known-good pmaports module ABI, and ships with idle-blanking power management; `BATTERY-OPTIMIZATION-ANALYSIS.md` records the measured idle state and explains why suspend-to-RAM is unavailable on this firmware (PSCI `SYSTEM_SUSPEND` missing) and why a full custom module tree rebuild is not worthwhile. +- **Mainline Linux 6.12 kernel**: custom patches and out-of-tree drivers for Qualcomm **Adreno 304 (`FD304`, 96 KB GMEM)** 3D GPU acceleration (`drm/msm` + `qcom-iommu`), Qualcomm QDSP6/Q6Voice audio routing, Dual-SIM modem control, the rear 2MP GalaxyCore `GC2395` / `GC2385` / `GC2365` camera (`/dev/video0`), the `SY7807` flashlight LED (`white:torch`), physical keypad/slider events, and clean PM8909 poweroff. The kernel image is built from `config-bananarchy-msm8909.armv7` with the known-good pmaports module ABI, and ships with idle-blanking power management; `BATTERY-OPTIMIZATION-ANALYSIS.md` records the measured idle state and explains why suspend-to-RAM is unavailable on this firmware (PSCI `SYSTEM_SUSPEND` missing) and why a full custom module tree rebuild is not worthwhile. Runtime Wi-Fi listen-interval, modem DRX and DSDS secondary-SIM power policies are configurable in `/etc/bananarchy-power-policy.conf`; see `MANUAL.md`. - **Alpine Linux Edge base**: full access to the Alpine package ecosystem with `< 95 MB` idle RAM usage on 512 MB devices and full local root access out of the box. - **Pure X11 graphical stack with GPU acceleration**: cross-compiled **Xlibre** (`X11Libre/xserver`) display server offloading 2D/3D screen rendering to the **Adreno 304 GPU** via **Glamor, DRI3, GLX, and Mesa Freedreno (`FD304`, OpenGL 3.1 / OpenGL ES 3.0)** over `panel-mipi-dbi` KMSRO, paired with a customized **Sxmo (`sxmo-de-dwm`)** window manager (`240x320` vertical stack layout, native `ParentRelative` desktop clock and softkey bar without Conky overhead, `< 2 ms` slider/key wake latency, and zero Wayland overhead). diff --git a/overlay/rootfs/etc/bananarchy-power-policy.conf b/overlay/rootfs/etc/bananarchy-power-policy.conf new file mode 100644 index 0000000..3e833a6 --- /dev/null +++ b/overlay/rootfs/etc/bananarchy-power-policy.conf @@ -0,0 +1,24 @@ +# Bananarchy runtime power policy. Read by bananarchy-power-policy.sh. +# Edit as root, then run: doas /usr/bin/bananarchy-power-policy.sh apply +# (or reboot). Every knob is deliberately conservative by default. + +# wcn36xx beacon listen interval (1-10). Higher values cut idle Wi-Fi RX +# wakeups but delay inbound packets. 0 removes the module option and uses +# the driver default. Applied at module load, so changes need a reboot or +# a wcn36xx module reload. +WIFI_LISTEN_INTERVAL=5 + +# Qualcomm modem DRX mode via AT$QCDRX (0, 6-9). 0 = firmware default, no +# policy. 6-9 = progressively longer paging cycles: lower modem idle +# current, longer call/SMS setup latency. Values above 6 are undocumented +# on this firmware; 9 made the AT channel briefly unresponsive in testing. +MODEM_DRX_MODE=0 + +# Secondary SIM (DSDS) policy while the panel is dark: +# active = keep both subscriptions provisioned (default) +# eco = deactivate the secondary GW provisioning session after +# DSDS_PARK_DELAY seconds of darkness, and reactivate it on wake. +# SIM2 is unreachable while parked. This uses QMI provisioning +# sessions, not SIM power-off, so a PIN-locked SIM stays locked. +DSDS_SECONDARY_MODE=active +DSDS_PARK_DELAY=300 diff --git a/overlay/rootfs/etc/doas.d/50-sxmo.conf b/overlay/rootfs/etc/doas.d/50-sxmo.conf index d4bafd9..1ed6e27 100644 --- a/overlay/rootfs/etc/doas.d/50-sxmo.conf +++ b/overlay/rootfs/etc/doas.d/50-sxmo.conf @@ -52,6 +52,8 @@ permit nopass :wheel as root cmd bananarchy-bt-obex permit nopass :wheel as root cmd /usr/bin/bananarchy-privacy-ctl permit nopass :wheel as root cmd bananarchy-privacy-ctl permit nopass :wheel as root cmd /usr/bin/bananarchy-display-power-sync +permit nopass :wheel as root cmd /usr/bin/bananarchy-power-policy.sh +permit nopass :wheel as root cmd bananarchy-power-policy.sh permit nopass :wheel as root cmd bananarchy-display-power-sync permit nopass :wheel as root cmd /usr/bin/plymouth args quit permit nopass :wheel as root cmd plymouth args quit diff --git a/overlay/rootfs/etc/init.d/bananarchy-power-policy b/overlay/rootfs/etc/init.d/bananarchy-power-policy new file mode 100755 index 0000000..70c1649 --- /dev/null +++ b/overlay/rootfs/etc/init.d/bananarchy-power-policy @@ -0,0 +1,15 @@ +#!/sbin/openrc-run + +name="Bananarchy power policy" +description="Apply Wi-Fi, modem DRX and DSDS runtime power policy" + +depend() { + need rmtfs + after modemmanager msm-modem-uim-selection msm8909-modem-wwan-port +} + +start() { + ebegin "Applying Bananarchy power policy" + /usr/bin/bananarchy-power-policy.sh boot >/dev/null 2>&1 || true + eend 0 +} diff --git a/overlay/rootfs/etc/modprobe.d/10-bananarchy-power.conf b/overlay/rootfs/etc/modprobe.d/10-bananarchy-power.conf new file mode 100644 index 0000000..c69c72a --- /dev/null +++ b/overlay/rootfs/etc/modprobe.d/10-bananarchy-power.conf @@ -0,0 +1,2 @@ +# Generated by bananarchy-power-policy.sh from /etc/bananarchy-power-policy.conf +options wcn36xx listen_interval=5 diff --git a/overlay/rootfs/lib/modules/6.12.1-msm8916/kernel/drivers/net/wireless/ath/wcn36xx/wcn36xx.ko.zst b/overlay/rootfs/lib/modules/6.12.1-msm8916/kernel/drivers/net/wireless/ath/wcn36xx/wcn36xx.ko.zst index 5dc4e28..cb64586 100644 Binary files a/overlay/rootfs/lib/modules/6.12.1-msm8916/kernel/drivers/net/wireless/ath/wcn36xx/wcn36xx.ko.zst and b/overlay/rootfs/lib/modules/6.12.1-msm8916/kernel/drivers/net/wireless/ath/wcn36xx/wcn36xx.ko.zst differ diff --git a/overlay/rootfs/lib/modules/6.12.1-msm8916/kernel/drivers/power/supply/pm8916_bms_vm.ko.zst b/overlay/rootfs/lib/modules/6.12.1-msm8916/kernel/drivers/power/supply/pm8916_bms_vm.ko.zst index 309c03f..c5d7656 100644 Binary files a/overlay/rootfs/lib/modules/6.12.1-msm8916/kernel/drivers/power/supply/pm8916_bms_vm.ko.zst and b/overlay/rootfs/lib/modules/6.12.1-msm8916/kernel/drivers/power/supply/pm8916_bms_vm.ko.zst differ diff --git a/overlay/rootfs/usr/bin/bananarchy-power-policy.sh b/overlay/rootfs/usr/bin/bananarchy-power-policy.sh new file mode 100755 index 0000000..82fd951 --- /dev/null +++ b/overlay/rootfs/usr/bin/bananarchy-power-policy.sh @@ -0,0 +1,177 @@ +#!/bin/sh +# Bananarchy runtime power policy: Wi-Fi listen interval, modem DRX, DSDS. +# Runs as root; re-execs through doas -n when called from the X session. +set -u + +CONF=/etc/bananarchy-power-policy.conf +MODPROBE=/etc/modprobe.d/10-bananarchy-power.conf +RUNDIR=/run/bananarchy-power-policy +PARKED="$RUNDIR/parked" +AIDFILE="$RUNDIR/secondary.aid" +SLOTFILE="$RUNDIR/secondary.slot" +TOKEN="$RUNDIR/token" +QMI="timeout 8 qmicli --silent -pd qrtr://0" + +if [ "$(id -u)" != "0" ]; then + exec doas -n "$0" "$@" +fi + +WIFI_LISTEN_INTERVAL=5 +MODEM_DRX_MODE=0 +DSDS_SECONDARY_MODE=active +DSDS_PARK_DELAY=300 +[ -r "$CONF" ] && . "$CONF" + +mkdir -p "$RUNDIR" 2>/dev/null || true + +log() { + printf 'bananarchy-power-policy: %s\n' "$*" >&2 +} + +write_modprobe() { + n="$WIFI_LISTEN_INTERVAL" + case "$n" in ''|*[!0-9]*) n=0 ;; esac + if [ "$n" -lt 1 ] || [ "$n" -gt 10 ]; then + n=0 + fi + if [ "$n" = "0" ]; then + rm -f "$MODPROBE" 2>/dev/null || true + return 0 + fi + mkdir -p "$(dirname "$MODPROBE")" 2>/dev/null || true + printf '# Generated by bananarchy-power-policy.sh from %s\n' "$CONF" > "$MODPROBE" || return 1 + printf 'options wcn36xx listen_interval=%s\n' "$n" >> "$MODPROBE" || return 1 +} + +apply_modem_drx() { + case "$MODEM_DRX_MODE" in + 0) return 0 ;; + 6|7|8|9) ;; + *) + log "invalid MODEM_DRX_MODE=$MODEM_DRX_MODE (use 0 or 6-9)" + return 1 + ;; + esac + [ -c /dev/wwan0at0 ] || { + log "no modem AT port" + return 1 + } + out="$(timeout 6 /usr/libexec/msm-modem-uim-selection at auto "AT\$QCDRX=$MODEM_DRX_MODE" 2>/dev/null || true)" + case "$out" in + *OK*) return 0 ;; + *) + log "AT\$QCDRX=$MODEM_DRX_MODE failed: $out" + return 1 + ;; + esac +} + +secondary_aid() { + $QMI --uim-get-card-status 2>/dev/null | awk ' + /Slot \[2\]/ { s=1; next } + s && /^Slot \[/ { exit } + s && /Application ID:/ { g=1; next } + s && g && NF { gsub(/^[ \t]+/, ""); print; exit } + ' +} + +secondary_slot() { + $QMI --uim-get-card-status 2>/dev/null | awk ' + /Secondary GW: slot/ { + for (i = 1; i <= NF; i++) + if ($i == "slot") { gsub(/[^0-9]/, "", $(i+1)); print $(i+1); exit } + } + ' +} + +park() { + [ -f "$PARKED" ] && return 0 + aid="$(secondary_aid)" + slot="$(secondary_slot)" + [ -n "$aid" ] && [ -n "$slot" ] || { + log "cannot read secondary SIM AID/slot" + return 1 + } + if $QMI --uim-change-provisioning-session='activate=no,session-type=secondary-gw-provisioning' >/dev/null 2>&1; then + printf '%s\n' "$aid" > "$AIDFILE" + printf '%s\n' "$slot" > "$SLOTFILE" + : > "$PARKED" + log "secondary SIM (slot $slot) parked" + return 0 + fi + log "failed to deactivate secondary GW session" + return 1 +} + +unpark() { + [ -f "$PARKED" ] || return 0 + aid="" + slot="" + [ -r "$AIDFILE" ] && { read -r aid < "$AIDFILE" 2>/dev/null || aid=""; } + [ -r "$SLOTFILE" ] && { read -r slot < "$SLOTFILE" 2>/dev/null || slot=""; } + [ -z "$slot" ] && slot=2 + if [ -n "$aid" ] && $QMI --uim-change-provisioning-session="slot=$slot,activate=yes,session-type=secondary-gw-provisioning,aid=$aid" >/dev/null 2>&1; then + log "secondary SIM (slot $slot) restored" + else + log "failed to reactivate secondary GW session" + fi + rm -f "$PARKED" "$AIDFILE" "$SLOTFILE" +} + +screenoff() { + [ "$DSDS_SECONDARY_MODE" = "eco" ] || return 0 + token="$(date +%s)-$$" + printf '%s\n' "$token" > "$TOKEN" + ( + sleep "$DSDS_PARK_DELAY" + cur="" + [ -r "$TOKEN" ] && { read -r cur < "$TOKEN" 2>/dev/null || cur=""; } + [ "$cur" = "$token" ] || exit 0 + "$0" _park >/dev/null 2>&1 + ) & +} + +screenon() { + rm -f "$TOKEN" 2>/dev/null || true + unpark + apply_modem_drx || true +} + +boot() { + rm -f "$TOKEN" "$PARKED" "$AIDFILE" "$SLOTFILE" 2>/dev/null || true + write_modprobe || true + apply_modem_drx || true +} + +status() { + printf 'WIFI_LISTEN_INTERVAL=%s\n' "$WIFI_LISTEN_INTERVAL" + printf 'MODEM_DRX_MODE=%s\n' "$MODEM_DRX_MODE" + printf 'DSDS_SECONDARY_MODE=%s\n' "$DSDS_SECONDARY_MODE" + printf 'DSDS_PARK_DELAY=%s\n' "$DSDS_PARK_DELAY" + if [ -f "$PARKED" ]; then + printf 'secondary_parked=yes\n' + else + printf 'secondary_parked=no\n' + fi + if [ -r "$MODPROBE" ]; then + printf 'modprobe_conf=%s (%s)\n' "$MODPROBE" "$(tail -n 1 "$MODPROBE")" + else + printf 'modprobe_conf=absent\n' + fi + printf 'wcn36xx_param=%s\n' "$(cat /sys/module/wcn36xx/parameters/listen_interval 2>/dev/null || echo n/a)" + printf 'modem_drx_now=%s\n' "$(timeout 6 /usr/libexec/msm-modem-uim-selection at auto 'AT$QCDRX?' 2>/dev/null | tr -d '\r' | grep -o '\$QCDRX: [0-9]*' | head -n 1)" +} + +case "${1:-}" in + boot|apply) boot ;; + screenoff) screenoff ;; + screenon) screenon ;; + _park) park ;; + unpark) unpark ;; + status) status ;; + *) + printf 'Usage: %s [boot|apply|screenoff|screenon|unpark|status]\n' "${0##*/}" >&2 + exit 1 + ;; +esac +exit 0 diff --git a/overlay/rootfs/usr/bin/sxmo_wm.sh b/overlay/rootfs/usr/bin/sxmo_wm.sh index eaa4726..1cffcac 100755 --- a/overlay/rootfs/usr/bin/sxmo_wm.sh +++ b/overlay/rootfs/usr/bin/sxmo_wm.sh @@ -58,6 +58,8 @@ xorgdisplay() { fi xset s off s noblank >/dev/null 2>&1 || true xset dpms force off >/dev/null 2>&1 || true + # Power policy: park the secondary SIM after the configured dark delay. + /usr/bin/bananarchy-power-policy.sh screenoff >/dev/null 2>&1 & ;; on) if [ -w /sys/devices/system/cpu/cpufreq/policy0/scaling_governor ]; then @@ -101,6 +103,8 @@ xorgdisplay() { # U1: record every wake so the idle-blank grace guard can suppress an # immediate re-blank (also covers dwm's wake_phone() path). date +%s > /tmp/bananarchy_last_wake 2>/dev/null || true + # Power policy: restore the secondary SIM and re-apply modem DRX. + /usr/bin/bananarchy-power-policy.sh screenon >/dev/null 2>&1 & ;; "") _cur_bl=14 diff --git a/patches/kernel/0007-msm8909-bms-fifo-wifi-policy.patch b/patches/kernel/0007-msm8909-bms-fifo-wifi-policy.patch new file mode 100644 index 0000000..1869357 --- /dev/null +++ b/patches/kernel/0007-msm8909-bms-fifo-wifi-policy.patch @@ -0,0 +1,78 @@ +--- a/drivers/power/supply/pm8916_bms_vm.c ++++ b/drivers/power/supply/pm8916_bms_vm.c +@@ -65,12 +65,22 @@ + unsigned int fake_ocv; + }; + ++/* VADC raw codes outside this window are uninitialised reads (0xffff) or ++ * out-of-range samples; 16000 ~= 4.69 V, above any Li-ion VBAT. */ ++#define PM8916_BMS_VM_RAW_MIN 8000 ++#define PM8916_BMS_VM_RAW_MAX 16000 ++#define PM8916_BMS_VM_NOMINAL_UV 3700000 ++ + static unsigned int pm8916_bms_vm_raw_to_uv(u16 raw) + { + /* + * Qualcomm VM-BMS VADC resolution is 97.65625 uV * 3 = 292.96875 uV/LSB +- * (see qpnp-vm-bms.c convert_vbatt_raw_to_uv). ++ * (see qpnp-vm-bms.c convert_vbatt_raw_to_uv). A 0xffff sample would ++ * otherwise be reported as 19.2 V / "Over voltage" after a hard reset; ++ * return a plausible nominal voltage instead so health stays Good. + */ ++ if (raw <= PM8916_BMS_VM_RAW_MIN || raw > PM8916_BMS_VM_RAW_MAX) ++ return PM8916_BMS_VM_NOMINAL_UV; + return (unsigned int)div_u64((u64)raw * 292969ULL, 1000ULL); + } + +@@ -172,7 +182,8 @@ + return IRQ_HANDLED; + + for (i = 0; i < PM8916_BMS_VM_FIFO_COUNT; i++) { +- if (vbat_data[i] > 8000) { ++ if (vbat_data[i] > PM8916_BMS_VM_RAW_MIN && ++ vbat_data[i] <= PM8916_BMS_VM_RAW_MAX) { + sum_uv += pm8916_bms_vm_raw_to_uv(vbat_data[i]); + valid_cnt++; + } +@@ -322,7 +333,8 @@ + bat->reg + PM8916_BMS_VM_BMS_FIFO_REG_0_LSB, + &fifo_init, + PM8916_BMS_VM_FIFO_COUNT * 2) == 0 && +- fifo_init[0] > 8000) { ++ fifo_init[0] > PM8916_BMS_VM_RAW_MIN && ++ fifo_init[0] <= PM8916_BMS_VM_RAW_MAX) { + bat->vbat_now = pm8916_bms_vm_raw_to_uv(fifo_init[0]); + bat->fake_ocv = bat->vbat_now + 12000; + } +--- a/drivers/net/wireless/ath/wcn36xx/main.c ++++ b/drivers/net/wireless/ath/wcn36xx/main.c +@@ -33,6 +33,12 @@ + module_param_named(debug_mask, wcn36xx_dbg_mask, uint, 0644); + MODULE_PARM_DESC(debug_mask, "Debugging mask"); + ++/* Beacon listen interval policy. Higher values cut idle RX wakeups at the ++ * cost of inbound packet latency; set via /etc/modprobe.d at boot. */ ++static unsigned int listen_interval = 5; ++module_param(listen_interval, uint, 0644); ++MODULE_PARM_DESC(listen_interval, "Beacon listen interval (1-10, default 5)"); ++ + #define CHAN2G(_freq, _idx) { \ + .band = NL80211_BAND_2GHZ, \ + .center_freq = (_freq), \ +@@ -1456,10 +1462,12 @@ + wcn->hw->max_listen_interval = 200; + wcn->hw->conf.dynamic_ps_timeout = 200; + wcn->hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT; +- /* Wake for every 3rd beacon instead of every beacon. With beacon +- * filtering + BMPS this roughly thirds the idle RX wakeup floor +- * (measured ~1.8 wcn36xx_rx IRQs/s before this change). */ +- wcn->hw->conf.listen_interval = 3; ++ /* Configurable beacon listen interval: with beacon filtering + BMPS ++ * this lowers the idle RX wakeup floor at the cost of inbound latency. */ ++ wcn->hw->conf.listen_interval = listen_interval; ++ if (wcn->hw->conf.listen_interval < 1 || ++ wcn->hw->conf.listen_interval > 10) ++ wcn->hw->conf.listen_interval = 5; + + wcn->hw->queues = 4; + diff --git a/prebuilt/kernel-6.12.1-msm8916/power/pm8916_bms_vm.ko b/prebuilt/kernel-6.12.1-msm8916/power/pm8916_bms_vm.ko index 59893d1..3493102 100644 Binary files a/prebuilt/kernel-6.12.1-msm8916/power/pm8916_bms_vm.ko and b/prebuilt/kernel-6.12.1-msm8916/power/pm8916_bms_vm.ko differ diff --git a/prebuilt/kernel-6.12.1-msm8916/power/pm8916_bms_vm.ko.zst b/prebuilt/kernel-6.12.1-msm8916/power/pm8916_bms_vm.ko.zst index 309c03f..c5d7656 100644 Binary files a/prebuilt/kernel-6.12.1-msm8916/power/pm8916_bms_vm.ko.zst and b/prebuilt/kernel-6.12.1-msm8916/power/pm8916_bms_vm.ko.zst differ diff --git a/prebuilt/kernel-6.12.1-msm8916/wcn36xx/wcn36xx.ko b/prebuilt/kernel-6.12.1-msm8916/wcn36xx/wcn36xx.ko index 35b58cb..cc9f6c9 100644 Binary files a/prebuilt/kernel-6.12.1-msm8916/wcn36xx/wcn36xx.ko and b/prebuilt/kernel-6.12.1-msm8916/wcn36xx/wcn36xx.ko differ diff --git a/prebuilt/kernel-6.12.1-msm8916/wcn36xx/wcn36xx.ko.zst b/prebuilt/kernel-6.12.1-msm8916/wcn36xx/wcn36xx.ko.zst index 5dc4e28..cb64586 100644 Binary files a/prebuilt/kernel-6.12.1-msm8916/wcn36xx/wcn36xx.ko.zst and b/prebuilt/kernel-6.12.1-msm8916/wcn36xx/wcn36xx.ko.zst differ diff --git a/scripts/apply-bananarchy-overlay.sh b/scripts/apply-bananarchy-overlay.sh index 34b38a5..1a05665 100755 --- a/scripts/apply-bananarchy-overlay.sh +++ b/scripts/apply-bananarchy-overlay.sh @@ -303,6 +303,7 @@ rm -rf \ "$TARGET_ROOTFS/swapfile" \ "$TARGET_ROOTFS/var/log/logbookd.db"* ln -sf /etc/init.d/kill-plymouth "$TARGET_ROOTFS/etc/runlevels/default/kill-plymouth" +ln -sf /etc/init.d/bananarchy-power-policy "$TARGET_ROOTFS/etc/runlevels/default/bananarchy-power-policy" mkdir -p "$TARGET_ROOTFS/var/lib/tinydm" ln -sf /usr/share/xsessions/sxmo.desktop "$TARGET_ROOTFS/var/lib/tinydm/default-session.desktop" diff --git a/scripts/build-kernel-audio.sh b/scripts/build-kernel-audio.sh index 5eb7aed..d662605 100755 --- a/scripts/build-kernel-audio.sh +++ b/scripts/build-kernel-audio.sh @@ -78,6 +78,10 @@ EOF echo "[2e/5] Applying BMS/WCN36xx/GPU idle-power patch..." patch -p1 < /bananarchy/patches/kernel/0006-msm8909-battery-wifi-gpu-power.patch fi + if ! grep -q "PM8916_BMS_VM_NOMINAL_UV" drivers/power/supply/pm8916_bms_vm.c 2>/dev/null; then + echo "[2f/5] Applying VM-BMS FIFO sanity and Wi-Fi listen-interval policy patch..." + patch -p1 < /bananarchy/patches/kernel/0007-msm8909-bms-fifo-wifi-policy.patch + fi if [ ! -f include/config/auto.conf ] || ! grep -q "CONFIG_NETFILTER_XT_MATCH_OWNER=m" .config 2>/dev/null; then echo "[3/5] Preparing kernel configuration..." cp /bananarchy/patches/kernel/config-bananarchy-msm8909.armv7 .config @@ -107,6 +111,9 @@ EOF echo "[2e/5] Applying BMS/WCN36xx/GPU idle-power patch..." patch -p1 < /bananarchy/patches/kernel/0006-msm8909-battery-wifi-gpu-power.patch + echo "[2f/5] Applying VM-BMS FIFO sanity and Wi-Fi listen-interval policy patch..." + patch -p1 < /bananarchy/patches/kernel/0007-msm8909-bms-fifo-wifi-policy.patch + echo "[3/5] Preparing kernel configuration and Module.symvers..." cp /bananarchy/patches/kernel/config-bananarchy-msm8909.armv7 .config make ARCH=arm CROSS_COMPILE=armv7-alpine-linux-musleabihf- olddefconfig