From 34627e46663f3e80479749455e37911ebe8ced2d Mon Sep 17 00:00:00 2001 From: Roland Ramthun Date: Sun, 14 Jan 2018 20:34:40 +0100 Subject: [PATCH] Update fancontrol to save original pwm values Update fancontrol script to save the values of pwmN_enable and pwmN when it starts and restore the values, when it exits. Closes groeck/lm-sensors#47. Signed-off-by: Roland Ramthun [groeck: Split description into multiple lines] Signed-off-by: Guenter Roeck --- prog/pwm/fancontrol | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/prog/pwm/fancontrol b/prog/pwm/fancontrol index 9030438c..be02f1c3 100755 --- a/prog/pwm/fancontrol +++ b/prog/pwm/fancontrol @@ -378,6 +378,11 @@ then fi echo $$ > "$PIDFILE" +# associative arrays to hold pwmN device name as key, and as value the +# pwmN_enable and pwmN values as they were before fancontrol was started +declare -A PWM_ENABLE_ORIG_STATE +declare -A PWM_ORIG_STATE + # $1 = pwm file name function pwmdisable() { @@ -390,6 +395,43 @@ function pwmdisable() return 0 fi + # Try to restore pwmN and pwmN_enable value to the same state as before + # fancontrol start. Restoring the pwmN value is tried first, before the + # pwmN_enable mode switch. + # Some chips seem to need this to properly restore fan operation, + # when activating automatic (2) mode. + if [ ${PWM_ENABLE_ORIG_STATE[${1}]} ] + then + #restore the pwmN value + if [ "$DEBUG" != "" ] + then + echo "Restoring ${1} original value of ${PWM_ORIG_STATE[${1}]}" + fi + echo ${PWM_ORIG_STATE[${1}]} > ${1} 2> /dev/null + # restore the pwmN_enable value, if it is not 1. + # 1 is already set through fancontrol and setting it again might just + # reset the pwmN value. + if [ ${PWM_ENABLE_ORIG_STATE[${1}]} != 1 ] + then + if [ "$DEBUG" != "" ] + then + echo "Restoring $ENABLE original value of ${PWM_ENABLE_ORIG_STATE[${1}]}" + fi + echo ${PWM_ENABLE_ORIG_STATE[${1}]} > $ENABLE 2> /dev/null + # check if setting pwmN_enable value was successful. Checking the + # pwmN value makes no sense, as it might already have been altered + # by the chip. + if [ `cat $ENABLE` = ${PWM_ENABLE_ORIG_STATE[${1}]} ] + then + return 0 + fi + # if pwmN_enable is manual (1), check if restoring the pwmN value worked + elif [ `cat ${1}` = ${PWM_ORIG_STATE[${1}]} ] + then + return 0 + fi + fi + # Try pwmN_enable=0 echo 0 > $ENABLE 2> /dev/null if [ `cat $ENABLE` -eq 0 ] @@ -419,6 +461,22 @@ function pwmenable() if [ -f $ENABLE ] then + # save the original pwmN_control state, e.g. 1 for manual or 2 for auto, + # and the value from pwmN + local PWM_CONTROL_ORIG=`cat $ENABLE` + local PWM_ORIG=`cat ${1}` + if [ "$DEBUG" != "" ] + then + echo "Saving $ENABLE original value as $PWM_CONTROL_ORIG" + echo "Saving ${1} original value as $PWM_ORIG" + fi + #check for degenerate case where these values might be empty + if [ $PWM_CONTROL_ORIG ] && [ $PWM_ORIG ] + then + PWM_ENABLE_ORIG_STATE[${1}]=$PWM_CONTROL_ORIG + PWM_ORIG_STATE[${1}]=$PWM_ORIG + fi + # enable manual control by fancontrol echo 1 > $ENABLE 2> /dev/null if [ $? -ne 0 ] then