Alternative suspend option for IR wakeup, when bl301 tool not possible

Some NG Amlogic boxes can’t wake from suspend by IR remote. The bl301 tool fixes this problem, but it’s not compatible with secure boot devices or encrypted bootloaders, like newer AM6+ and FireTV 2nd gen Cube.

An alternative to CE’s suspend (suspend-to-ram) is to use the suspend-to-idle option, which is one stage above suspend-to-ram. The IR receiver remains active, and can be woken with an IR power button press on any Amlogic device.

Suspend-to-idle puts all but one CPU offline, freezes all user space processes, and puts some other hardware offline. It reduces power consumption, but not to as much of an extent as suspend-to-ram.

This script freezes Kodi, and wakes it with an IR power key press. Wakeup is as fast as is possible, ~2-3sec.

Summary
#!/bin/sh

# Disable anything that might wake from suspend early
# echo disabled > /sys/devices/platform/bt-dev/power/wakeup
# echo disabled > /sys/devices/platform/ff80023c.aocec/power/wakeup
echo disabled > /sys/devices/platform/rtc/power/wakeup
echo disabled > /sys/devices/platform/rtc/input/input1/power/wakeup

# Turn off TV
printf '\x20\x36' > /dev/aocec

# Pause Kodi
pkill -STOP kodi.bin

# Suspend-to-idle
echo freeze > /sys/power/state

# Wakeup
# Turn on TV
printf '\x20\x04' > /dev/aocec

# Reset meson_remote
modprobe -r meson_remote && modprobe meson_remote
remotecfg /storage/.config/remote.conf

# Unpause Kodi
pkill -CONT kodi.bin

This version stops Kodi, freezes all processes, and restarts Kodi on wakeup. Wakeup is slower due to Kodi restarting.

Summary
#!/bin/sh

# Disable anything that might wake from suspend early
# echo disabled > /sys/devices/platform/bt-dev/power/wakeup
# echo disabled > /sys/devices/platform/ff80023c.aocec/power/wakeup
echo disabled > /sys/devices/platform/rtc/power/wakeup
echo disabled > /sys/devices/platform/rtc/input/input1/power/wakeup

systemctl mask systemd-poweroff.service
systemctl stop kodi

# Suspend-to-idle
echo freeze > /sys/power/state

# Wakeup commands
# Reset meson_remote
modprobe -r meson_remote && modprobe meson_remote
remotecfg /storage/.config/remote.conf
systemctl start kodi

sleep 10
systemctl unmask systemd-poweroff.service

The default CE suspend can be seamlessly replaced, by adding the suspend-to-idle script to systemd-suspend.service, and placing it at /storage/.config/system.d/ systemd-suspend.service (samba ConfigFiles/system.d/systemd-suspend.service). Will be active after the next reboot

Summary
[Unit]
Description=System Suspend

[Service]
Type=oneshot
ExecStart=/bin/sh -c "\
    systemctl mask suspend.target; \
    \
    # Disable anything that might wake from suspend early \
    # echo disabled > /sys/devices/platform/bt-dev/power/wakeup \
    # echo disabled > /sys/devices/platform/ff80023c.aocec/power/wakeup \
    echo disabled > /sys/devices/platform/rtc/power/wakeup; \
    echo disabled > /sys/devices/platform/rtc/input/input1/power/wakeup; \
    echo disabled > /sys/devices/platform/ff3f0000.ethernet/power/wakeup; \
    \
    # Turn off TV \
    printf '\x20\x36' > /dev/aocec; \
    \
    # Pause Kodi \
    pkill -STOP kodi.bin; \
    \
    # Suspend-to-idle \
    echo freeze > /sys/power/state; \
    \
    # Wakeup \
    # Turn on TV \
    printf '\x20\x04' > /dev/aocec & \
    \
    # Reset meson_remote \
    modprobe -r meson_remote && modprobe meson_remote; \
    remotecfg /storage/.config/remote.conf; \
    \
    # Unpause Kodi \
    pkill -CONT kodi.bin; \
    \
    systemctl unmask suspend.target; \
    "

I also recommend adding the following line to /storage/.config/autoscript.sh, in case the box is ever powered off completely while in suspend. If the autoscript.sh file wasn’t previously created, you’ll need to create it. Open a plain text file, copy & past the contents below, save it as autostart.sh, copy it to CE over samba to ConfigFiles/autostart.sh

#!/bin/sh

# Make sure suspend is unmasked
systemctl unmask suspend.target

Attached is a copy of systemd-suspend.service. This was tested on the 2nd gen Cube, it’s possible that one or two other pieces of hardware will need to have their wakeup function disabled in the script, to keep other box models from waking from suspend early.

systemd-suspend.service (1.0 KB)

3 Likes

@Gilgamesh The AM6+ probably has one or more pieces of hardware that have a wakeup option, that is causing it to wake early.

can you run this command from SSH and post the output
CoreELEC:~ # find /sys -name wakeup -exec grep -H . {} \;

Can you rerun that command with paste at the end
find /sys -name wakeup -exec grep -H . {} \; | paste

Then post the URL, that way I don’t have to transcribe each of the paths

https://paste.coreelec.org/CaterersCyrus

1 Like

ok it’s probably one of these. Cast a wide net to start. Add these all to your script right below

echo disabled > /sys/devices/platform/rtc/input/input1/power/wakeup
echo disabled > /sys/devices/platform/bt-dev/power/wakeup
echo disabled > /sys/devices/platform/ff500000.dwc3/xhci-hcd.0.auto/usb1/power/wakeup
echo disabled > /sys/devices/platform/ff500000.dwc3/xhci-hcd.0.auto/usb1/1-1/power/wakeup
echo disabled > /sys/devices/platform/ff3f0000.ethernet/power/wakeup
echo disabled > /sys/devices/platform/ffe03000.sdio/mmc_host/sdio/sdio:0001/sdio:0001:2/power/wakeup

Then run the script and see if the box stays off. If that works, you can remove a couple of these. My guess would be that it’s ethernet that’s waking it, or maybe mmc.

I’ve put the suspend-to-idle.sh into a CE service file, that can be dropped in. This service makes suspend-to-idle the default CE suspend behavior, so that you don’t have to remap the power menu. The service file is in the OP.

This still only wakes with an IR power key press using an amremote profile. If anyone has any idea how to wake from this state by CEC, BT, or GPIO button, let me know. I’ve not had any luck on that so far.

What is the path for the autoscript.sh file? Is this a typo and you actually meant /storage/.config/autostart.sh (instead of autoscript.sh)?

I stopped using suspend since I needed to restart after waking up or I faced stuttering or slowdowns.I noticed some other users report the same.Would this approach have the same issue?

The default CE suspend (suspend-to-ram), turns off more hardware. On the Cube I had issues with some of the hardware not waking from suspend.

This version of suspend is lighter, I haven’t noticed any performance problems after waking, no video playback issues. Applying the modified suspend (suspend-to-idle) service is pretty easy, and to remove it the suspend service file can be deleted.

As I mentioned in the OP, other boxes might have additional hardware over the Cube, that can wake the box early from suspend. If that happens, then the service can be modified to turn off wakeup on that hardware. My guess is that the AM6+ Ethernet might wake early from suspend, so Ethernet wakeup was disabled in the script.

1 Like

Thank you for your insight.I will give it a shot on the am6b

Yeah, the path is /storage/.config/autostart.sh (samba ConfigFiles/autostart.sh). If you haven’t already created an autostart.sh file previously, you’ll need to create this file.

Open a plain txt file and add this it:

#!/bin/sh

# Make sure suspend is unmasked
systemctl unmask suspend.target

Then save it as autostart.sh and copy it to ConfigFiles over Samba.

1 Like

I am implemented this on the Kinhank G1 (S904X5-J) a few minutes ago. So far I only have 1 video that is stuttering and I am not even sure whether the suspend-to-idle is the culprit. I need to use this on a regular basis to know for sure that it works fine, but so far it is looking good.

Let me know if the Kinhank G1 wakes from suspend on it’s own. The s905x4 might have different paths for some of the hardware with wakeup options than the s922x.

If that happens, run this command from SSH and report:
CoreELEC:~ # find /sys -name wakeup -exec grep -H . {} \;

That will list hardware with wakeup option, its path, and state.

So far not, but when it happens I’ll let you know with the requested information.

@YadaYada

The Kinhank G1 didn’t wake up from sleep without me pressing hte power button. But now I am seeing some stuttering after waking it up.

find /sys -name wakeup -exec grep -H . {} \; 
/sys/kernel/irq/55/wakeup:disabled
/sys/kernel/irq/17/wakeup:disabled
/sys/kernel/irq/45/wakeup:disabled
/sys/kernel/irq/35/wakeup:disabled
/sys/kernel/irq/7/wakeup:disabled
/sys/kernel/irq/63/wakeup:disabled
/sys/kernel/irq/25/wakeup:disabled
/sys/kernel/irq/53/wakeup:disabled
/sys/kernel/irq/15/wakeup:disabled
/sys/kernel/irq/43/wakeup:disabled
/sys/kernel/irq/71/wakeup:disabled
/sys/kernel/irq/33/wakeup:disabled
/sys/kernel/irq/5/wakeup:disabled
/sys/kernel/irq/61/wakeup:disabled
/sys/kernel/irq/23/wakeup:disabled
/sys/kernel/irq/51/wakeup:disabled
/sys/kernel/irq/13/wakeup:disabled
/sys/kernel/irq/41/wakeup:disabled
/sys/kernel/irq/31/wakeup:disabled
/sys/kernel/irq/3/wakeup:disabled
/sys/kernel/irq/21/wakeup:disabled
/sys/kernel/irq/11/wakeup:disabled
/sys/kernel/irq/68/wakeup:disabled
/sys/kernel/irq/1/wakeup:disabled
/sys/kernel/irq/58/wakeup:disabled
/sys/kernel/irq/48/wakeup:disabled
/sys/kernel/irq/38/wakeup:disabled
/sys/kernel/irq/66/wakeup:disabled
/sys/kernel/irq/28/wakeup:disabled
/sys/kernel/irq/56/wakeup:disabled
/sys/kernel/irq/18/wakeup:disabled
/sys/kernel/irq/46/wakeup:disabled
/sys/kernel/irq/36/wakeup:disabled
/sys/kernel/irq/8/wakeup:disabled
/sys/kernel/irq/64/wakeup:disabled
/sys/kernel/irq/26/wakeup:disabled
/sys/kernel/irq/54/wakeup:disabled
/sys/kernel/irq/16/wakeup:disabled
/sys/kernel/irq/44/wakeup:disabled
/sys/kernel/irq/72/wakeup:enabled
/sys/kernel/irq/34/wakeup:disabled
/sys/kernel/irq/6/wakeup:disabled
/sys/kernel/irq/62/wakeup:disabled
/sys/kernel/irq/24/wakeup:disabled
/sys/kernel/irq/52/wakeup:disabled
/sys/kernel/irq/14/wakeup:disabled
/sys/kernel/irq/42/wakeup:disabled
/sys/kernel/irq/70/wakeup:disabled
/sys/kernel/irq/32/wakeup:disabled
/sys/kernel/irq/4/wakeup:disabled
/sys/kernel/irq/60/wakeup:disabled
/sys/kernel/irq/22/wakeup:disabled
/sys/kernel/irq/50/wakeup:disabled
/sys/kernel/irq/12/wakeup:disabled
/sys/kernel/irq/40/wakeup:disabled
/sys/kernel/irq/69/wakeup:disabled
/sys/kernel/irq/30/wakeup:disabled
/sys/kernel/irq/2/wakeup:disabled
/sys/kernel/irq/59/wakeup:disabled
/sys/kernel/irq/20/wakeup:disabled
/sys/kernel/irq/49/wakeup:disabled
/sys/kernel/irq/10/wakeup:disabled
/sys/kernel/irq/39/wakeup:disabled
/sys/kernel/irq/67/wakeup:disabled
/sys/kernel/irq/29/wakeup:disabled
/sys/kernel/irq/57/wakeup:disabled
/sys/kernel/irq/19/wakeup:disabled
/sys/kernel/irq/47/wakeup:disabled
/sys/kernel/irq/37/wakeup:disabled
/sys/kernel/irq/9/wakeup:disabled
/sys/kernel/irq/65/wakeup:disabled
/sys/kernel/irq/27/wakeup:disabled
/sys/devices/platform/aml_bt/power/wakeup:enabled
/sys/devices/platform/soc/f5000000.pcie/pci0000:00/0000:00:00.0/power/wakeup:disabled
/sys/devices/platform/soc/f5000000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/power/wakeup:disabled
/sys/devices/platform/soc/soc:usb@fde00000/fde00000.dwc3/xhci-hcd.0.auto/usb1/power/wakeup:enabled
/sys/devices/platform/soc/soc:usb@fde00000/fde00000.dwc3/xhci-hcd.0.auto/usb1/1-2/power/wakeup:enabled
/sys/devices/platform/soc/soc:usb@fde00000/fde00000.dwc3/xhci-hcd.0.auto/usb2/power/wakeup:disabled
/sys/devices/platform/soc/fe000000.apb4/fe07a000.serial/tty/ttyS0/power/wakeup:disabled
/sys/devices/platform/soc/fe330000.audiobus/fe330000.audiobus:vad/power/wakeup:enabled
/sys/devices/platform/soc/fdc00000.ethernet/power/wakeup:enabled
/sys/devices/platform/soc/fe080000.serial/tty/ttyS1/power/wakeup:disabled
/sys/devices/platform/fe084040.meson-remote/power/wakeup:enabled
/sys/devices/platform/fe044000.aocec/power/wakeup:enabled

Is the video stuttering when you wakeup, and immediately start a video? or do you continue to see stuttering even 30sec after wakeup?

When the board first unfreezes it does jump back on the internet and there is a bit more CPU activity for 10-15sec after the wake.

The stuttering would be there until I reboot, i don’t believe it has anything to do with initial background activity after restart.

Can you try using suspend, and if when you wakeup you see video stuttering, only restart Kodi, not the G1. I’m wondering if this is a Kodi issue. To restart Kodi, SSH in. First run dmesg | paste and share, maybe there is something obvious. Then run systemctl restart kodi. Depending on the skin you’re using, there might be a restart kodi option in the power menu.

Restarting Kodi also solves the stuttering.

The suspend script pauses Kodi and after wakeup lets Kodi run again. Instead of doing this, it could kill Kodi before suspend and start Kodi after wakeup. That would solve the issue in a kind of hard way.