root/lm-sensors/trunk/prog/pwm/pwmconfig

Revision 5857, 22.7 KB (checked in by khali, 18 months ago)

Drop legacy calls to cut. procfs had several values per file, but
sysfs doesn't.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/bin/bash
2#
3# pwmconfig
4# Tests the pwm outputs of sensors and configures fancontrol
5# Supported Linux kernel versions: 2.6.5 and later
6#
7#    Warning!!! This program will stop your fans, one at a time,
8#    for approximately 5 seconds each!!!
9#    This may cause your processor temperature to rise!!!
10#    Verify that all fans are running at normal speed after this
11#    program has exited!!!
12#
13#    Copyright (C) 2007-2009 Jean Delvare <khali@linux-fr.org>
14#
15#    This program is free software; you can redistribute it and/or modify
16#    it under the terms of the GNU General Public License as published by
17#    the Free Software Foundation; either version 2 of the License, or
18#    (at your option) any later version.
19#
20#    This program is distributed in the hope that it will be useful,
21#    but WITHOUT ANY WARRANTY; without even the implied warranty of
22#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23#    GNU General Public License for more details.
24#
25#    You should have received a copy of the GNU General Public License
26#    along with this program; if not, write to the Free Software
27#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
28#    MA 02110-1301 USA.
29#
30#
31
32REVISION=$(echo '$Revision$' | cut -d' ' -f2)
33REVDATE=$(echo '$Date$' | cut -d' ' -f2)
34PIDFILE="/var/run/fancontrol.pid"
35
36if [ -f "$PIDFILE" ]
37then
38        echo "File $PIDFILE exists. This typically means that the"
39        echo "fancontrol deamon is running. You should stop it before running pwmconfig."
40        echo "If you are certain that fancontrol is not running, then you can delete"
41        echo "$PIDFILE manually."
42        exit 1
43fi
44
45if [ "`id -u`" != "0" ]
46then
47        echo "You need to be root to run this script."
48        exit 1
49fi
50
51echo "# pwmconfig revision $REVISION ($REVDATE)"
52echo 'This program will search your sensors for pulse width modulation (pwm)'
53echo 'controls, and test each one to see if it controls a fan on'
54echo 'your motherboard. Note that many motherboards do not have pwm'
55echo 'circuitry installed, even if your sensor chip supports pwm.'
56echo
57echo 'We will attempt to briefly stop each fan using the pwm controls.'
58echo 'The program will attempt to restore each fan to full speed'
59echo 'after testing. However, it is ** very important ** that you'
60echo 'physically verify that the fans have been to full speed'
61echo 'after the program has completed.'
62echo
63
64DELAY=5 # 3 seconds delay is too short for large fans, thus I increased it to 5
65MAX=255
66
67if [ -d "/sys/class/hwmon" ]
68then
69        SYSFS=2
70        DIR="/sys/class/hwmon"
71        PREFIX='hwmon*'
72elif [ -d "/sys/bus/i2c/devices" ]
73then
74        SYSFS=1
75        DIR="/sys/bus/i2c/devices"
76        PREFIX='*-*'
77else
78        echo $0: 'No sensors found! (modprobe sensor modules?)'
79        exit 1
80fi
81
82cd $DIR
83DEVICES=`echo $PREFIX`
84if [ "$PREFIX" = "$DEVICES" ]
85then
86        echo $0: 'No sensors found! (modprobe sensor modules?)'
87        exit 1
88fi
89
90# We may need to adjust the device path
91if [ "$SYSFS" = "2" ]
92then
93        OLD_DEVICES="$DEVICES"
94        DEVICES=""
95
96        for device in $OLD_DEVICES
97        do
98                if [ ! -r "$device/name" ]
99                then
100                        device="$device/device"
101                fi
102               
103                DEVICES="$DEVICES $device"
104        done
105fi
106
107
108for device in $DEVICES
109do
110        # Find available fan control outputs
111        MATCH=$device/'pwm[1-9]'
112        device_pwm=`echo $MATCH`
113        if [ "$SYSFS" = "1" -a "$MATCH" = "$device_pwm" ]
114        then
115                # Deprecated naming scheme (used in kernels 2.6.5 to 2.6.9)
116                MATCH=$device/'fan[1-9]_pwm'
117                device_pwm=`echo $MATCH`
118        fi
119        if [ "$MATCH" != "$device_pwm" ]
120        then
121                PWM="$PWM $device_pwm"
122        fi
123
124        # Find available fan monitoring inputs
125        MATCH=$device/'fan[1-9]_input'
126        device_fan=`echo $MATCH`
127        if [ "$MATCH" != "$device_fan" ]
128        then
129                FAN="$FAN $device_fan"
130        fi
131done
132
133if [ -z "$PWM" ]
134then
135        echo $0: 'There are no pwm-capable sensor modules installed'
136        exit 1
137fi
138if [ -z "$FAN" ]
139then
140        echo $0: 'There are no fan-capable sensor modules installed'
141        exit 1
142fi
143
144# $1 = padding
145function print_devices()
146{
147        local name
148
149        for device in $DEVICES
150        do
151                name=`cat $device/name 2> /dev/null`
152                [ -z "$name" ] && name="unknown (no name attribute)"
153                echo "$1$device is $name"
154        done
155}
156
157# $1 = pwm file name
158function is_pwm_auto()
159{
160        ENABLE=${1}_enable
161        if [ -f $ENABLE ]
162        then
163                if [ "`cat $ENABLE`" -gt 1 ]
164                then
165                        return 0
166                fi
167        fi
168
169        return 1
170}
171
172# $1 = pwm file name
173function pwmdisable()
174{
175        ENABLE=${1}_enable
176        # No enable file? Just set to max
177        if [ ! -f $ENABLE ]
178        then
179                echo $MAX > $1
180                return 0
181        fi
182
183        # Try pwmN_enable=0
184        echo 0 2>/dev/null > $ENABLE
185        if [ "`cat $ENABLE`" -eq 0 ]
186        then
187                # Success
188                return 0
189        fi
190
191        # It didn't work, try pwmN_enable=1 pwmN=255
192        echo 1 2>/dev/null > $ENABLE
193        if [ "`cat $ENABLE`" -ne 1 ]
194        then
195                echo "$ENABLE stuck to `cat $ENABLE`" >&2
196                return 1
197        fi
198
199        echo $MAX > $1
200        if [ "`cat $1`" -ge 190 ]
201        then
202                # Success
203                return 0
204        fi
205
206        # Nothing worked
207        echo "$1 stuck to `cat $1`" >&2
208        return 1
209}
210
211# $1 = pwm file name
212function pwmenable()
213{
214        ENABLE=${1}_enable
215        if [ -w $ENABLE ]
216        then
217                echo 1 2>/dev/null > $ENABLE
218                if [ $? -ne 0 ]
219                then
220                        return 1
221                fi
222        fi
223        echo $MAX > $1
224}
225
226# $1 = pwm file name; $2 = pwm value 0-255
227function pwmset()
228{
229        echo $2 > $1
230}
231
232echo 'Found the following devices:'
233print_devices "   "
234echo
235
236echo 'Found the following PWM controls:'
237for i in $PWM
238do
239        echo "   $i"
240        if [ -w $i ]
241        then
242                # First check if PWM output is in automatic mode
243                if is_pwm_auto $i
244                then
245                        echo "$i is currently setup for automatic speed control."
246                        echo 'In general, automatic mode is preferred over manual mode, as'
247                        echo 'it is more efficient and it reacts faster. Are you sure that'
248                        echo -n 'you want to setup this output for manual control? (n) '
249                        read X
250                        if [ "$X" = "" -o "$X" != "y" -a "$X" != "Y" ]
251                        then
252                                continue
253                        fi
254                fi
255
256                pwmdisable $i
257                if [ $? -ne 0 ]
258                then
259                        echo "Manual control mode not supported, skipping $i."
260                elif [ "$GOODPWM" = "" ]
261                then
262                        GOODPWM=$i
263                else
264                        GOODPWM="$GOODPWM $i"
265                fi
266        else
267                echo "Can't write to $i, skipping."
268        fi
269done
270
271if [ "$GOODPWM" = "" ]
272then
273        echo 'There are no usable PWM outputs.'
274        exit 1
275fi
276
277echo
278echo "Giving the fans some time to reach full speed..."
279sleep $DELAY
280echo 'Found the following fan sensors:'
281for i in $FAN
282do
283        S=`cat $i`
284        if [ "$S" = "0" -o "$S" = "-1" ]
285        then
286                echo "   $i     current speed: 0 ... skipping!"
287        else
288                echo "   $i     current speed: $S RPM"
289                if [ "$GOODFAN" = "" ]
290                then
291                        GOODFAN=$i
292                        SPEEDS=$S
293                else
294                        GOODFAN="$GOODFAN $i"
295                        SPEEDS="$SPEEDS $S"
296                fi
297        fi
298done
299echo
300
301if [ "$GOODFAN" = "" ]
302then
303        echo 'There are no working fan sensors, all readings are 0.'
304        echo 'Make sure you have a 3-wire fan connected.'
305        echo 'You may also need to increase the fan divisors.'
306        echo 'See doc/fan-divisors for more information.'
307        exit 1
308fi
309
310echo 'Warning!!! This program will stop your fans, one at a time,'
311echo "for approximately $DELAY seconds each!!!"
312echo 'This may cause your processor temperature to rise!!!'
313echo 'If you do not want to do this hit control-C now!!!'
314echo -n 'Hit return to continue: '
315read X
316echo
317
318PLOTTER=gnuplot
319STEP=15
320PDELAY=2
321# Use a smaller step for low PWM values as this is typically where the
322# more important fan speed changes are happening.
323STEP2=2
324STEP2_BELOW=31
325
326function pwmdetail()
327{
328        P=$1
329        F=$2
330        PLOT=
331
332        type $PLOTTER > /dev/null 2>&1
333        if [ $? -eq 0 ]
334        then
335                echo -n "Would you like to generate a graphical plot using $PLOTTER (y)? "
336                read X
337                if [ "$X" = "y" -o "$X" = "Y" -o "$X" = "" ]
338                then
339                        PLOT=y
340                fi
341        else
342                if [ -n "$DISPLAY" ]
343                then
344                        echo "Note: If you had $PLOTTER installed, I could generate a graphical plot."
345                fi
346        fi
347
348        if [ "$PLOT" = "y" ]
349        then
350                TMP1=`mktemp -t pwmtest1.XXXXXXXXXX` || { echo "$0: Cannot create temporary file" >&2; exit 1; }
351                TMP2=`mktemp -t pwmtest2.XXXXXXXXXX` || { rm -f $TMP1 ; echo "$0: Cannot create temporary file" >&2; exit 1; }
352                echo "set xlabel \"PWM: $P\"" > $TMP1
353                echo "set ylabel \"FAN: $F (RPM)\"" >> $TMP1
354                echo 'set nokey' >> $TMP1
355                echo 'set xrange [0:255]' >> $TMP1
356                echo "plot \"$TMP2\" with lines" >> $TMP1
357                echo 'pause -1 "    Hit return to continue..."' >> $TMP1
358                > $TMP2
359        fi
360
361        local threshold=100000
362        let pwm=$MAX
363        pwmenable $P
364        while [ $pwm -ge 0 ]
365        do
366                pwmset $P $pwm
367                sleep $PDELAY
368                if [ $? -ne 0 ]
369                then
370                        pwmdisable $P
371                        echo '^C received, aborting...'
372                        rm -f $TMP1 $TMP2
373                        exit 1
374                fi
375                S=`cat $F`
376                # Fan speed should never increase significantly
377                if [ $S -gt $threshold ]
378                then
379                        echo "    PWM $pwm FAN $S (probably incorrect)"
380                else
381                        echo "    PWM $pwm FAN $S"
382                        let threshold=S*6/5
383                fi
384
385                if [ "$PLOT" = "y" ]
386                then
387                        echo "$pwm $S" >> $TMP2
388                fi
389                if [ "$S" = "0" -o "$S" = "-1" ]
390                then
391                        pwmdisable $P
392                        echo "    Fan Stopped at PWM = $pwm"
393                        if [ $pwm -eq $MAX ]
394                        then
395                                echo "    This fan appears to stop when the PWM is enabled;"
396                                echo "    perhaps the fan input shares a pin with the PWM output"
397                                echo "    on the sensor chip."
398                                echo "    You cannot control this fan with this PWM output."
399                                rm -f $TMP1 $TMP2
400                                echo
401                                return 0
402                        fi
403                        break
404                fi
405                if [ $pwm -lt $STEP2_BELOW ]
406                then
407                        let pwm=$pwm-$STEP2
408                else
409                        let pwm=$pwm-$STEP
410                fi
411        done
412        pwmdisable $P
413        if [ "$PLOT" = "y" ]
414        then
415                $PLOTTER  $TMP1
416                rm -f $TMP1 $TMP2
417        fi
418        echo
419}
420
421for i in $GOODPWM
422do
423        echo Testing pwm control $i ...
424        pwmenable $i
425        if [ $? -ne 0 ]
426        then
427                echo "Manual control mode not supported, skipping."
428                continue
429        fi
430        pwmset $i 0
431        sleep $DELAY
432        if [ $? -ne 0 ]
433        then
434                pwmdisable $i
435                echo '^C received, restoring PWM and aborting...'
436                exit 1
437        fi
438
439        # Sample all current fan speeds at once, so that we can quickly
440        # disable PWM and return all fans to full speed
441        CURRENT_SPEEDS="`cat $GOODFAN`"
442        pwmdisable $i
443
444        let pwmactivecount=0
445        let count=1
446        for j in $GOODFAN
447        do
448                OS=`echo $SPEEDS | cut -d' ' -f$count`
449                S=`echo $CURRENT_SPEEDS | cut -d' ' -f$count`
450                echo "  $j ... speed was $OS now $S"
451                let threshold=2*$OS/3
452                if [ $S -lt $threshold ]
453                then
454                        echo "    It appears that fan $j"
455                        echo "    is controlled by pwm $i"
456#
457# a PWM can control more than one fan....
458#
459                        if [ $pwmactivecount -eq 0 ]
460                        then
461                                let pwmactivecount=1
462                                pwmactive="$i ${pwmactive}"
463                                fanactive="$j ${fanactive}"
464                                fanactive_min="$S ${fanactive_min}"
465
466                                # Give all correlated fans time to return to full speed
467                                sleep $DELAY
468                                if [ $? -ne 0 ]
469                                then
470                                        echo '^C received, aborting...'
471                                        exit 1
472                                fi
473                        else
474                                fanactive="$j+${fanactive}" #not supported yet by fancontrol
475                                fanactive_min="$S+${fanactive_min}"
476                        fi
477                        S=`cat $j`
478                        if [ $S -lt $threshold ]
479                        then
480                                echo "    Fan $j has not returned to speed, please investigate!"
481                        else
482                                echo -n "Would you like to generate a detailed correlation (y)? "
483                                read X
484                                if [ "$X" = "y" -o "$X" = "Y" -o "$X" = "" ]
485                                then
486                                        pwmdetail $i $j
487                                fi
488                        fi
489                else
490                        echo "    no correlation"
491                fi
492                let count=count+1
493        done
494        echo
495        if [ "$pwmactivecount" = "0" ]
496        then
497                echo "No correlations were detected."
498                echo "There is either no fan connected to the output of $i,"
499                echo "or the connected fan has no rpm-signal connected to one of"
500                echo "the tested fan sensors. (Note: not all motherboards have"
501                echo "the pwm outputs connected to the fan connectors,"
502                echo "check out the hardware database on http://www.almico.com/forumindex.php)"
503                echo
504                echo -n "Did you see/hear a fan stopping during the above test (n)? "
505                read X
506                if [ "$X" = "y" -o "$X" = "Y" ]
507                then
508                        pwmactive="$i ${pwmactive}"
509                fi
510                echo
511        fi
512done
513
514
515echo 'Testing is complete.'
516echo 'Please verify that all fans have returned to their normal speed.'
517echo
518echo 'The fancontrol script can automatically respond to temperature changes'
519echo 'of your system by changing fanspeeds.'
520echo -n 'Do you want to set up its configuration file now (y)? '
521
522read X
523if [ "$X" = "n" -o "$X" = "N" ]
524then
525        exit
526fi
527
528for device in $DEVICES
529do
530        # Find available temperature monitoring inputs
531        MATCH=$device/'temp[1-9]_input'
532        device_temp=`echo $MATCH`
533        if [ "$MATCH" != "$device_temp" ]
534        then
535                TEMPS="$TEMPS $device_temp"
536        fi
537done
538
539if [ -z "$TEMPS" ]
540then
541        echo $0: 'There are no temperature-capable sensor modules installed'
542        exit 1
543fi
544
545function DevicePath()
546{
547        if [ -h "$1/device" ]
548        then
549                readlink -f "$1/device" | sed -e 's/^\/sys\///'
550        fi
551}
552
553function DeviceName()
554{
555        if [ -r "$1/name" ]
556        then
557                cat "$1/name" | sed -e 's/[[:space:]=]/_/g'
558        elif [ -r "$1/device/name" ]
559        then
560                cat "$1/device/name" | sed -e 's/[[:space:]=]/_/g'
561        fi
562}
563
564function ValidateDevices()
565{
566        local OLD_DEVPATH="$1" OLD_DEVNAME="$2" outdated=0
567        local entry device name path
568
569        for entry in $OLD_DEVPATH
570        do
571                device=`echo "$entry" | sed -e 's/=[^=]*$//'`
572                path=`echo "$entry" | sed -e 's/^[^=]*=//'`
573
574                if [ "`DevicePath "$device"`" != "$path" ]
575                then
576                        echo "Device path of $device has changed"
577                        outdated=1
578                fi
579        done
580
581        for entry in $OLD_DEVNAME
582        do
583                device=`echo "$entry" | sed -e 's/=[^=]*$//'`
584                name=`echo "$entry" | sed -e 's/^[^=]*=//'`
585
586                if [ "`DeviceName "$device"`" != "$name" ]
587                then
588                        echo "Device name of $device has changed"
589                        outdated=1
590                fi
591        done
592
593        return $outdated
594}
595
596function AskPath()
597{
598        echo -n 'What should be the path to your fancontrol config file (/etc/fancontrol)? '
599
600        read FCCONFIG
601        if [ "$FCCONFIG" = "" ]
602        then
603                FCCONFIG="/etc/fancontrol"
604        fi
605}
606
607AskPath
608
609function ClearConfig()
610{
611        FCTEMPS=""
612        FCFANS=""
613        MINTEMP=""
614        MAXTEMP=""
615        MINSTART=""
616        MINSTOP=""
617        MINPWM=""
618        MAXPWM=""
619}
620
621function LoadConfig()
622{
623        local OLD_DEVPATH OLD_DEVNAME
624
625        # Nothing to do
626        if [ ! -f "$1" ]
627        then
628                ClearConfig
629                return 0
630        fi
631
632        echo "Loading configuration from $1 ..."
633        INTERVAL=`egrep '^INTERVAL=.*$' $1 | sed -e 's/INTERVAL= *//g'`
634        OLD_DEVPATH=`egrep '^DEVPATH=.*$' $1 | sed -e 's/DEVPATH= *//g'`
635        OLD_DEVNAME=`egrep '^DEVNAME=.*$' $1 | sed -e 's/DEVNAME= *//g'`
636        FCTEMPS=`egrep '^FCTEMPS=.*$' $1 | sed -e 's/FCTEMPS= *//g'`
637        FCFANS=`egrep '^FCFANS=.*$' $1 | sed -e 's/FCFANS= *//g'`
638        MINTEMP=`egrep '^MINTEMP=.*$' $1 | sed -e 's/MINTEMP= *//g'`
639        MAXTEMP=`egrep '^MAXTEMP=.*$' $1 | sed -e 's/MAXTEMP= *//g'`
640        MINSTART=`egrep '^MINSTART=.*$' $1 | sed -e 's/MINSTART= *//g'`
641        MINSTOP=`egrep '^MINSTOP=.*$' $1 | sed -e 's/MINSTOP= *//g'`
642        MINPWM=`egrep '^MINPWM=.*$' $1 | sed -e 's/MINPWM= *//g'`
643        MAXPWM=`egrep '^MAXPWM=.*$' $1 | sed -e 's/MAXPWM= *//g'`
644
645        # Check for configuration change
646        if ! ValidateDevices "$OLD_DEVPATH" "$OLD_DEVNAME"
647        then
648                echo "Configuration appears to be outdated, discarded"
649                ClearConfig
650                return 0
651        fi
652}
653
654LoadConfig $FCCONFIG
655
656# $1 = pwm value below which the fan is stopped
657function TestMinStart()
658{
659        echo
660        echo 'Now we increase the PWM value in 10-unit-steps.'
661        echo 'Let the fan stop completely, then press return until the'
662        echo "fan starts spinning. Then enter 'y'."
663        echo 'We will use this value +20 as the starting speed.'
664        let fanok=0
665        let fanval="$1"
666
667        pwmenable $pwms
668        until [ "$fanok" = "1" ]
669        do
670                if [ $fanval -gt 240 ] ; then let fanval=$MAX ; let fanok=1 ; fi
671                echo -n "Setting $pwms to $fanval..."
672                pwmset $pwms $fanval
673                read FANTEST
674                if [ "$FANTEST" != "" ] ; then let fanok=1 ; fi
675                let fanval=fanval+10
676        done
677        pwmdisable $pwms
678
679        let fanval=fanval+20
680        if [ $fanval -gt 240 ] ; then let fanval=$MAX ; fi
681        echo "OK, using $fanval"
682}
683
684# $1 = fan input to read the fan speed from
685function TestMinStop()
686{
687        local faninput=$1
688        local threshold=100000
689        local fanspeed
690
691        echo
692        echo 'Now we decrease the PWM value to figure out the lowest usable value.'
693        echo 'We will use a slightly greater value as the minimum speed.'
694        let fanval=$MAX
695
696        pwmenable $pwms
697        while [ $fanval -ge 0 ]
698        do
699                pwmset $pwms $fanval
700                sleep $PDELAY
701                fanspeed=`cat $faninput`
702                if [ $fanspeed -gt $threshold ]
703                then
704                        echo "    PWM $fanval -> $fanspeed RPM (probably incorrect)"
705                        break
706                else
707                        echo "    PWM $fanval -> $fanspeed RPM"
708                        if [ $fanspeed = "0" -o $fanspeed = "-1" ]
709                        then
710                                break
711                        fi
712                        let threshold=fanspeed*6/5
713                fi
714                if [ $fanval -lt $STEP2_BELOW ]
715                then
716                        let fanval=$fanval-$STEP2
717                else
718                        let fanval=$fanval-$STEP
719                fi
720        done
721        pwmdisable $pwms
722
723        if [ $fanval -lt $STEP2_BELOW ]
724        then
725                let 'fanval=fanval+2*STEP2'
726        else
727                let 'fanval=fanval+STEP'
728        fi
729        echo "OK, using $fanval"
730}
731
732# Remember the path and name of each device with at least one
733# reference (pwm, temp or fan) in the configuration file.
734# This function sets globals DEVPATH and DEVNAME as a side effect.
735function RememberDevices()
736{
737        local used entry device name path tempfandev pwmdev
738        DEVPATH=""
739        DEVNAME=""
740
741        for device in $DEVICES
742        do
743                device=`echo "$device" | sed -e 's/\/.*$//'`
744
745                used=0
746                for entry in $1 $2
747                do
748                        pwmdev=`echo "$entry" | sed -e 's/\/.*$//'`
749                        tempfandev=`echo "$entry" | sed -e 's/^[^=]*=//' -e 's/\/.*$//'`
750
751                        if [ "$device" = "$pwmdev" -o "$device" = "$tempfandev" ]
752                        then
753                                used=1
754                        fi
755                done
756                if [ "$used" -eq 0 ]
757                then
758                        continue
759                fi
760
761                # Record the device path and name. This lets the fancontrol
762                # script check that they didn't change. If they did, then the
763                # configuration file can no longer be trusted.
764                path=`DevicePath "$device"`
765                if [ -z "$DEVPATH" ]
766                then
767                        DEVPATH="$device=$path"
768                else
769                        DEVPATH="$DEVPATH $device=$path"
770                fi
771
772                name=`DeviceName "$device"`
773                if [ -z "$DEVNAME" ]
774                then
775                        DEVNAME="$device=$name"
776                else
777                        DEVNAME="$DEVNAME $device=$name"
778                fi
779        done
780}
781
782function SaveConfig()
783{
784        RememberDevices "$FCTEMPS" "$FCFANS"
785
786        echo
787        echo "Saving configuration to $FCCONFIG..."
788        tmpfile=`mktemp -t pwmcfg.XXXXXXXXXX` || { echo "$0: Cannot create temporary file" >&2; exit 1;  }
789        trap " [ -f \"$tmpfile\" ] && /bin/rm -f -- \"$tmpfile\"" 0 1 2 3 13 15
790        echo "# Configuration file generated by pwmconfig, changes will be lost" >$tmpfile
791        echo "INTERVAL=$INTERVAL" >>$tmpfile
792        echo "DEVPATH=$DEVPATH" >>$tmpfile
793        echo "DEVNAME=$DEVNAME" >>$tmpfile
794        echo "FCTEMPS=$FCTEMPS" >>$tmpfile
795        echo "FCFANS=$FCFANS" >>$tmpfile
796        echo "MINTEMP=$MINTEMP" >>$tmpfile
797        echo "MAXTEMP=$MAXTEMP" >>$tmpfile
798        echo "MINSTART=$MINSTART" >>$tmpfile
799        echo "MINSTOP=$MINSTOP" >>$tmpfile
800        [ -n "$MINPWM" ] && echo "MINPWM=$MINPWM" >>$tmpfile
801        [ -n "$MAXPWM" ] && echo "MAXPWM=$MAXPWM" >>$tmpfile
802        mv $tmpfile $FCCONFIG
803        chmod +r $FCCONFIG
804        #check if file was written correctly
805        echo 'Configuration saved'
806}
807
808INTERVAL=10
809PS3='select (1-n): '
810DEFMINTEMP=20
811DEFMAXTEMP=60
812DEFMINSTART=150
813DEFMINSTOP=100
814
815function filter_cfgvar()
816{
817        echo "$1" | sed -e 's/ /\n/g' \
818                  | egrep "$2" \
819                  | sed -e 's/.*=//g'
820}
821
822# "select" won't repeat the list of options, so we enclose it in a
823# never-ending loop so that it starts over again with each iteration.
824# I admit it's not exactly nice, but I do not have a better idea to
825# keep usability at an acceptable level.
826while [ 1 ] ; do
827echo
828echo 'Select fan output to configure, or other action:'
829select pwms in $pwmactive "Change INTERVAL" "Just quit" "Save and quit" "Show configuration"; do
830        case $pwms in
831        "Change INTERVAL")
832                echo
833                echo "Current interval is $INTERVAL seconds."
834                echo -n "Enter the interval at which fancontrol should update PWM values (in s): "
835                read INTERVAL #check user input here
836                break ;;
837        "Just quit")
838                exit ;;
839        "Save and quit")
840                SaveConfig
841                exit ;;
842        "Show configuration")
843                echo
844                echo "Common Settings:"
845                echo "INTERVAL=$INTERVAL"
846                for pwmo in $pwmactive
847                do
848                        echo
849                        echo "Settings of ${pwmo}:"
850                        echo "  Depends on `filter_cfgvar "$FCTEMPS" "$pwmo"`"
851                        echo "  Controls `filter_cfgvar "$FCFANS" "$pwmo"`"
852                        echo "  MINTEMP=`filter_cfgvar "$MINTEMP" $pwmo`"
853                        echo "  MAXTEMP=`filter_cfgvar "$MAXTEMP" "$pwmo"`"
854                        echo "  MINSTART=`filter_cfgvar "$MINSTART" "$pwmo"`"
855                        echo "  MINSTOP=`filter_cfgvar "$MINSTOP" "$pwmo"`"
856                        XMINP=`filter_cfgvar "$MINPWM" "$pwmo"`
857                        [ -n "$XMINP" ] && echo "  MINPWM=$XMINP"
858                        XMAXP=`filter_cfgvar "$MAXPWM" "$pwmo"`
859                        [ -n "$XMAXP" ] && echo "  MAXPWM=$XMAXP"
860                done
861                echo
862                break ;;
863
864        "`echo ${pwmactive} |sed -e 's/ /\n/g' | egrep "${pwms}"`" )
865                pwmsed=`echo ${pwms} | sed -e 's/\//\\\\\//g'` #escape / for sed
866                echo
867
868                echo 'Devices:'
869                print_devices ""
870                echo
871
872                echo 'Current temperature readings are as follows:'
873                for j in $TEMPS
874                do
875                        S=`cat $j`
876                        let S="$S / 1000"
877                        echo "$j        $S"
878                done
879                FAN=`echo $fanactive|cut -d' ' -f$REPLY`
880                FAN_MIN=`echo $fanactive_min|cut -d' ' -f$REPLY`
881                FCFANS="`echo $FCFANS | sed -e "s/${pwmsed}[^ ]* *//g"` ${pwms}=$FAN"
882                echo
883                echo "Select a temperature sensor as source for ${pwms}:"
884                select tempss in $TEMPS "None (Do not affect this PWM output)"; do
885                        if [ "$tempss" = "None (Do not affect this PWM output)" ]
886                        then
887
888                                break;
889                        else
890                                if [ "$FCTEMPS" = "" ]
891                                then
892                                        FCTEMPS="${pwms}=${tempss}"
893                                else
894                                        FCTEMPS="`echo $FCTEMPS | sed -e "s/${pwmsed}[^ ]* *//g"` ${pwms}=${tempss}"
895                                fi
896                        fi
897                        echo
898                        echo 'Enter the low temperature (degree C)'
899                        echo -n "below which the fan should spin at minimum speed ($DEFMINTEMP): "
900                        read XMT
901                        if [ "$XMT" = "" ]
902                        then
903                                XMT=$DEFMINTEMP
904                        fi
905                        if [ "$MINTEMP" = "" ]
906                        then
907                                MINTEMP="${pwms}=${XMT}"
908                        else
909                                MINTEMP="`echo $MINTEMP | sed -e "s/${pwmsed}[^ ]* *//g"` ${pwms}=${XMT}"
910                        fi
911                        echo
912                        echo 'Enter the high temperature (degree C)'
913                        echo -n "over which the fan should spin at maximum speed ($DEFMAXTEMP): "
914                        read XMT
915                        if [ "$XMT" = "" ]
916                        then
917                                XMT=$DEFMAXTEMP
918                        fi
919                        if [ "$MAXTEMP" = "" ]
920                        then
921                                MAXTEMP="${pwms}=${XMT}"
922                        else
923                                MAXTEMP="`echo $MAXTEMP | sed -e "s/${pwmsed}[^ ]* *//g"` ${pwms}=${XMT}"
924                        fi
925
926                        if [ $FAN_MIN -eq 0 ]
927                        then
928                                echo
929                                echo "Enter the minimum PWM value (0-$MAX)"
930                                echo -n "at which the fan STOPS spinning (press t to test) ($DEFMINSTOP): "
931                                read XMSTOP
932
933                                if [ "$XMSTOP" = "" ]
934                                then
935                                        XMSTOP=$DEFMINSTOP
936                                fi
937                                if [ "$XMSTOP" = "t" -o "$XMSTOP" = "T" ]
938                                then
939                                        TestMinStop $FAN
940                                        XMSTOP=$fanval
941                                fi
942                        else
943                                XMSTOP=0
944                        fi
945                        if [ "$MINSTOP" = "" ]
946                        then
947                                MINSTOP="${pwms}=${XMSTOP}"
948                        else
949                                MINSTOP="`echo $MINSTOP | sed -e "s/${pwmsed}[^ ]* *//g"` ${pwms}=${XMSTOP}"
950                        fi
951
952                        if [ $FAN_MIN -eq 0 ]
953                        then
954                                echo
955                                echo "Enter the minimum PWM value ($XMSTOP-$MAX)"
956                                echo -n "at which the fan STARTS spinning (press t to test) ($DEFMINSTART): "
957                                read XMSTART
958
959                                if [ "$XMSTART" = "" ]
960                                then
961                                        XMSTART=$DEFMINSTART
962                                fi
963                                if [ "$XMSTART" = "t" -o "$XMSTART" = "T" ]
964                                then
965                                        TestMinStart $XMSTOP
966                                        XMSTART=$fanval
967                                fi
968                        else
969                                XMSTART=$DEFMINSTART
970                        fi
971                        if [ "$MINSTART" = "" ]
972                        then
973                                MINSTART="${pwms}=${XMSTART}"
974                        else
975                                MINSTART="`echo $MINSTART | sed -e "s/${pwmsed}[^ ]* *//g"` ${pwms}=${XMSTART}"
976                        fi
977
978                        if [ $XMSTOP -gt 0 ]
979                        then
980                                echo
981                                echo "Enter the PWM value (0-$XMSTOP) to use when the temperature"
982                                echo -n "is below the low temperature limit (0): "
983                                read XMINP
984                        else
985                                XMINP=""
986                        fi
987                        if [ -n "$XMINP" ]
988                        then
989                                if [ "$MINPWM" = "" ]
990                                then
991                                        MINPWM="${pwms}=${XMINP}"
992                                else
993                                        MINPWM="`echo $MINPWM | sed -e "s/${pwmsed}[^ ]* *//g"` ${pwms}=${XMINP}"
994                                fi
995                        fi
996                        echo
997                        echo "Enter the PWM value ($XMSTOP-$MAX) to use when the temperature"
998                        echo -n "is over the high temperature limit ($MAX): "
999                        read XMAXP
1000                        if [ -n "$XMAXP" ]
1001                        then
1002                                if [ "$MAXPWM" = "" ]
1003                                then
1004                                        MAXPWM="${pwms}=${XMAXP}"
1005                                else
1006                                        MAXPWM="`echo $MAXPWM | sed -e "s/${pwmsed}[^ ]* *//g"` ${pwms}=${XMAXP}"
1007                                fi
1008                        fi
1009                        echo
1010                        break;
1011                done
1012                break ;;
1013
1014        *)
1015                echo "No such option. Enter a number."
1016                break ;;
1017        esac
1018done
1019done
Note: See TracBrowser for help on using the browser.