Suspend and saa7164

From MythTV Official Wiki
Jump to: navigation, search

Introduction

The saa7164 module does not support suspend to RAM or disk. This module is used by Hauppauge devices such as WinTV-HVR2200, WinTV-HVR2250, WinTV-HVR2255 and generally the Hauppauge WinTV-HVR22xx family. You can see if the module is in use from a terminal by entering the command: lsmod | grep saa7164

The symptoms are that after a resume the tuners will not work. You cannot watch LiveTV and mythbackend may crash or lock up.

In order to suspend you need to remove this module, which means that it must not be in use. This means you need to close mythbackend before you can suspend.

The following outlines one approach, using Ubuntu 15.10. You may wish to make improvements.

Script to close mythbackend

Create the following script named closemyth.sh in folder /usr/local/bin

until [ $? -ne 0 ]
do
  sleep 5
  killall -2 mythbackend
done
rmmod saa7164
if [ $? -eq 0 ]
then
  bash /home/m/sleep.sh
  modprobe saa7164
fi
mythbackend

To make it executable, enter the command: sudo chmod a+x /usr/local/bin/closemyth.sh

Explanation

  • The until loop sends a request to mythbackend to terminate gracefully. We loop until we don't find anything to kill - that means mythtvbackend has ended
  • We remove the saa7164 module - if this fails for some reason (perhaps the tuner is in use), then we will not sleep and instead restart the backend
  • We run our desired suspend command (stored in folder /home/m/sleep.sh - you may change that)
  • the next two lines are run after resuming from the suspend - we add the saa7164 module back, and then restart mythbackend

Allow the script to run as root without a password

  • Create the following as a file mythtv in /etc/sudoers.d

m ALL = NOPASSWD: /usr/sbin/rtcwake, /usr/local/bin/closemyth.sh

where m is the user name of the logged in user

Make sure you set permissions correctly on this file: sudo chmod 0440 /etc/sudoers.d/mythtv

Note that we will use rtcwake as the command to suspend the computer - you may use another command. See ACPI_Wakeup for details.

Manual test

First create the sleep command script in whatever folder you are using - eg /home/m/sleep.sh above

sudo /usr/sbin/rtcwake -m mem -s 60

This will sleep for 60 seconds to memory ( a suspend to RAM) - replace mem with disk if you want to suspend to disk - mem is faster to respond but does use more power

Now you should be able to enter the command:

closemyth.sh

and your machine should suspend for 60 seconds. After it resumes check that the tuners are working and that you can watch LiveTV.

Automate

After a successful test we can automate the process. From a terminal enter: mythtv-setup (You can continue with the backend running)

  • Choose 1. General
  • Next your way to the Shutdown/Wakeup options page
  • Chose Idle Shutdown timeout (eg 60 seconds)
  • Choose maximum wait for recording (eg 5 minutes - probably longer if you chose to suspend to disk, rather than RAM)
  • Choose startup before recording (eg 180 seconds) - you need enough time for the resume to complete before recording starts - this is longer if you suspend to disk
  • Wakeup time format - use time_t
  • Command to set wakeup time - this is where we actually store the sleep command to be executed by the closemyth.sh script - so you need to choose a writeable folder - eg the home folder. If the username is m, the command is:

echo sudo /usr/sbin/rtcwake -m mem -t $time> /home/m/sleep.sh

  • Server halt command

sudo /usr/local/bin/closemyth.sh

  • Now Next and Finish your way out to save your settings.

Conclusion

There are probably better and more elegant ways to do this. Feel free to edit this page to make improvements and report your experiences or problems.