#!/bin/sh
# s10e-deep prepare | resume ERRNO: s10e-sleepd's deep_hook on the Galaxy S10e, around each SoC sleep
# (exynos9820-sleep mode 3, "mem" with mem_sleep "deep"; s2idle needs none of this).
#
# The SoC sleep takes the power of most of the SoC; the PMU's sleep pin table (U-Boot v51) holds the pads. What the
# drivers cannot carry through it yet is handled here (2026-11-09, kernel 7.2.15 #55):
#   - UFS: spm_lvl 5 (link off, device powered down) for the sleep, 3 again after it.
#   - The sensor hub (bcm4775-shub on spi1.0): unbound for the sleep, bound after it (its firmware boots in ~2 s).
#   - Wi-Fi: the BCM4375 stays powered (WL_REG_ON holds its level through the sleep, U-Boot v51), associated and
#     armed (WoWLAN "any"); its host-wake line (gpa0-7) could wake the SoC but is not a wake-up source (below). The
#     RC takes the link down with PERST# for the sleep and trains it again after (kernel #57: through #56 it never
#     did, PERST# itself was still held in pad retention), and brcmfmac picks the running firmware up where it was
#     (#58: through #57 it rang the firmware's D0 doorbell through the wrong BAR0 window, and the firmware stopped
#     answering a few sleeps later). Should Wi-Fi not answer after a sleep anyway, the RC and brcmfmac are reloaded:
#     the firmware boots again and NetworkManager reconnects, ~10 s.
#     An early wake-up (ERRNO 95: the firmware refused, an interrupt was pending) leaves the link up.
#   - Wake-up sources: the modem's mailboxes (WAKEUP_INT_EN 0x1500: a call, an SMS) and the EINTs of the PMIC (RTC,
#     gpa0-1) and the power key (gpa2-4): eint_wake 0x100002. Not Wi-Fi's host wake (gpa0-7, bit 7) for now: every
#     frame for the phone raised it (TCP ACKs, SIP OPTIONS), and with the reload below each such wake cost ~10 s
#     awake; pushes over Wi-Fi need a filter in the dongle first (2026-11-09). Not the touchscreen (gpa1-0): with the screen
#     off the s6sy761 raises its line every ~6-8 s, which ended every SoC sleep after ~8.5 s (2026-11-09), so a double
#     tap does not wake a deep sleep. Not the max77705 (gpa1-6) either: plugging or unplugging the charger does not
#     wake it (it charges asleep all the same).
#   - The HW watchdog: keep_in_suspend N (the driver stops it for the sleep and starts it after; kept running, the
#     SoC sleep resets the block and it bites ~5 s after the wake).
P=/sys/module/exynos9820_sleep/parameters
U=/sys/bus/platform/devices/13d60000.ufs
SHUB=/sys/bus/spi/drivers/bcm4775-shub
log() { echo "s10e-deep: $*"; }

case "$1" in
prepare)
	echo 3 > $P/mode
	echo $((0x1500)) > $P/wakeup_int_en
	echo $((0x100002)) > $P/eint_wake
	echo N > /sys/module/s3c2410_wdt/parameters/keep_in_suspend
	echo 5 > $U/spm_lvl
	[ -e $SHUB/spi1.0 ] && echo spi1.0 > $SHUB/unbind
	echo deep > /sys/power/mem_sleep
	;;
resume)
	echo 3 > $U/spm_lvl
	[ -e $SHUB/spi1.0 ] || echo spi1.0 > $SHUB/bind
	if [ "${2:-1}" = 0 ]; then
		# The SoC slept. The link trains again and brcmfmac resumes the dongle hot: still associated, nothing to
		# do. Only if the firmware does not answer does Wi-Fi come back by a reload. Answering means over the bus:
		# `iw link`'s "Connected" is cfg80211's cache. Associated, `station dump` lists the AP only with the
		# firmware's assoclist and sta_info; not associated, `iw info` shows a txpower only from the firmware.
		# A command the firmware does not answer takes 2 s; two tries, by the wall clock.
		end=$(($(date +%s) + 5))
		while [ "$(date +%s)" -lt $end ]; do
			if timeout 3 iw dev wlp1s0 link 2>/dev/null | grep -q '^Connected'; then
				timeout 5 iw dev wlp1s0 station dump 2>/dev/null | grep -q '^Station'
			else
				timeout 5 iw dev wlp1s0 info 2>/dev/null | grep -q txpower
			fi && { log "Wi-Fi kept through the sleep"; exit 0; }
			sleep 0.5
		done
		t0=$(cut -d' ' -f1 /proc/uptime)
		rmmod brcmfmac_wcc brcmfmac 2>/dev/null
		rmmod pcie_exynos9820 2>/dev/null
		modprobe pcie_exynos9820 || log "modprobe pcie_exynos9820 failed"
		# NetworkManager reconnects on its own; until then a sleep would leave the phone off the network (the
		# dongle would not be associated, nothing on Wi-Fi could wake it). 20 s at most, by the wall clock.
		end=$(($(date +%s) + 20))
		while [ "$(date +%s)" -lt $end ]; do
			nmcli -t -f DEVICE,STATE device 2>/dev/null | grep -q '^wlp1s0:connected$' && break
			sleep 0.5
		done
		st=$(nmcli -t -f DEVICE,STATE device 2>/dev/null | grep '^wlp1s0:' | cut -d: -f2)
		log "Wi-Fi reloaded: ${st:-no wlp1s0} after $(awk -v t="$t0" '{printf "%.1f", $1 - t}' /proc/uptime) s"
	fi
	;;
*)
	echo "usage: s10e-deep prepare | resume ERRNO" >&2
	exit 2
	;;
esac
exit 0
