Debian Codegraph @

prog/pwm/fancontrol

raw ◀ ▶
1 Debian patch touch this file
1 #!/bin/bash
2 #
3 # Simple script implementing a temperature dependent fan speed control
4 # Supported Linux kernel versions: 2.6.5 and later
5 #
6 # Version 0.71
7 #
8 # Usage: fancontrol [CONFIGFILE]
9 #
10 # Dependencies:
11 # bash, grep, sed, cut, sleep, readlink, lm_sensors :)
12 #
13 # Please send any questions, comments or success stories to
14 # [email protected]
15 # Thanks!
16 #
17 # For configuration instructions and warnings please see fancontrol.txt, which
18 # can be found in the doc/ directory or at the website mentioned above.
19 #
20 #
21 # Copyright 2003 Marius Reiner <[email protected]>
22 # Copyright (C) 2007-2014 Jean Delvare <[email protected]>
23 #
24 # This program is free software; you can redistribute it and/or modify
25 # it under the terms of the GNU General Public License as published by
26 # the Free Software Foundation; either version 2 of the License, or
27 # (at your option) any later version.
28 #
29 # This program is distributed in the hope that it will be useful,
30 # but WITHOUT ANY WARRANTY; without even the implied warranty of
31 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 # GNU General Public License for more details.
33 #
34 # You should have received a copy of the GNU General Public License
35 # along with this program; if not, write to the Free Software
36 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
37 # MA 02110-1301 USA.
38 #
39 #
40
41 PIDFILE="/run/fancontrol.pid"
42
43 #DEBUG=1
44 MAX=255
45
46 function LoadConfig
47 {
48 local fcvcount fcv
49
50 echo "Loading configuration from $1 ..."
51 if [ ! -r "$1" ]
52 then
53 echo "Error: Can't read configuration file" >&2
54 exit 1
55 fi
56
57 # grep configuration from file
58 INTERVAL=$(grep -E '^INTERVAL=.*$' $1 | sed -e 's/INTERVAL=//g')
59 DEVPATH=$(grep -E '^DEVPATH=.*$' $1 | sed -e 's/DEVPATH= *//g')
60 DEVNAME=$(grep -E '^DEVNAME=.*$' $1 | sed -e 's/DEVNAME= *//g')
61 FCTEMPS=$(grep -E '^FCTEMPS=.*$' $1 | sed -e 's/FCTEMPS=//g')
62 MINTEMP=$(grep -E '^MINTEMP=.*$' $1 | sed -e 's/MINTEMP=//g')
63 MAXTEMP=$(grep -E '^MAXTEMP=.*$' $1 | sed -e 's/MAXTEMP=//g')
64 MINSTART=$(grep -E '^MINSTART=.*$' $1 | sed -e 's/MINSTART=//g')
65 MINSTOP=$(grep -E '^MINSTOP=.*$' $1 | sed -e 's/MINSTOP=//g')
66 # optional settings:
67 FCFANS=$(grep -E '^FCFANS=.*$' $1 | sed -e 's/FCFANS=//g')
68 MINPWM=$(grep -E '^MINPWM=.*$' $1 | sed -e 's/MINPWM=//g')
69 MAXPWM=$(grep -E '^MAXPWM=.*$' $1 | sed -e 's/MAXPWM=//g')
70 AVERAGE=$(grep -E '^AVERAGE=.*$' $1 | sed -e 's/AVERAGE=//g')
71
72 # Check whether all mandatory settings are set
73 if [[ -z ${INTERVAL} || -z ${FCTEMPS} || -z ${MINTEMP} || -z ${MAXTEMP} || -z ${MINSTART} || -z ${MINSTOP} ]]
74 then
75 echo "Some mandatory settings missing, please check your config file!" >&2
76 exit 1
77 fi
78 if [ "$INTERVAL" -le 0 ]
79 then
80 echo "Error in configuration file:" >&2
81 echo "INTERVAL must be at least 1" >&2
82 exit 1
83 fi
84
85 # write settings to arrays for easier use and print them
86 echo
87 echo "Common settings:"
88 echo " INTERVAL=$INTERVAL"
89
90 let fcvcount=0
91 for fcv in $FCTEMPS
92 do
93 if ! echo $fcv | grep -E -q '='
94 then
95 echo "Error in configuration file:" >&2
96 echo "FCTEMPS value is improperly formatted" >&2
97 exit 1
98 fi
99
100 AFCPWM[$fcvcount]=$(echo $fcv |cut -d'=' -f1)
101 AFCTEMP[$fcvcount]=$(echo $fcv |cut -d'=' -f2)
102 AFCFAN[$fcvcount]=$(echo $FCFANS |sed -e 's/ /\n/g' |grep -E "${AFCPWM[$fcvcount]}" |cut -d'=' -f2)
103 AFCMINTEMP[$fcvcount]=$(echo $MINTEMP |sed -e 's/ /\n/g' |grep -E "${AFCPWM[$fcvcount]}" |cut -d'=' -f2)
104 AFCMAXTEMP[$fcvcount]=$(echo $MAXTEMP |sed -e 's/ /\n/g' |grep -E "${AFCPWM[$fcvcount]}" |cut -d'=' -f2)
105 AFCMINSTART[$fcvcount]=$(echo $MINSTART |sed -e 's/ /\n/g' |grep -E "${AFCPWM[$fcvcount]}" |cut -d'=' -f2)
106 AFCMINSTOP[$fcvcount]=$(echo $MINSTOP |sed -e 's/ /\n/g' |grep -E "${AFCPWM[$fcvcount]}" |cut -d'=' -f2)
107 AFCMINPWM[$fcvcount]=$(echo $MINPWM |sed -e 's/ /\n/g' |grep -E "${AFCPWM[$fcvcount]}" |cut -d'=' -f2)
108 [ -z "${AFCMINPWM[$fcvcount]}" ] && AFCMINPWM[$fcvcount]=0
109 AFCMAXPWM[$fcvcount]=$(echo $MAXPWM |sed -e 's/ /\n/g' |grep -E "${AFCPWM[$fcvcount]}" |cut -d'=' -f2)
110 [ -z "${AFCMAXPWM[$fcvcount]}" ] && AFCMAXPWM[$fcvcount]=255
111 AFCAVERAGE[$fcvcount]=$(echo $AVERAGE |sed -e 's/ /\n/g' |grep -E "${AFCPWM[$fcvcount]}" |cut -d'=' -f2)
112 [ -z "${AFCAVERAGE[$fcvcount]}" ] && AFCAVERAGE[$fcvcount]=1
113
114 # verify the validity of the settings
115 if [ "${AFCMINTEMP[$fcvcount]}" -ge "${AFCMAXTEMP[$fcvcount]}" ]
116 then
117 echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2
118 echo "MINTEMP must be less than MAXTEMP" >&2
119 exit 1
120 fi
121
122 if [ "${AFCMINPWM[$fvcount]}" -lt "${AFCMAXPWM[$fvcount]}" ]; then
123 # Normal drive:
124 # PWM value 0 -> Fan not spinning
125 # PWM value 255 -> Fan spinning at full speed
126
127 if [ "${AFCMAXPWM[$fcvcount]}" -gt $MAX ]
128 then
129 echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2
130 echo "For normal drive MAXPWM must be at most $MAX" >&2
131 exit 1
132 fi
133
134 if [ "${AFCMINSTOP[$fcvcount]}" -ge "${AFCMAXPWM[$fcvcount]}" ]
135 then
136 echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2
137 echo "For normal drive MINSTOP must be less than MAXPWM" >&2
138 exit 1
139 fi
140
141 if [ "${AFCMINSTOP[$fcvcount]}" -lt "${AFCMINPWM[$fcvcount]}" ]
142 then
143 echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2
144 echo "For normal drive MINSTOP must be greater than or equal to MINPWM" >&2
145 exit 1
146 fi
147
148 if [ "${AFCMINPWM[$fcvcount]}" -lt 0 ]
149 then
150 echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2
151 echo "For normal drive MINPWM must be at least 0" >&2
152 exit 1
153 fi
154
155 elif [ "${AFCMINPWM[$fvcount]}" -gt "${AFCMAXPWM[$fvcount]}" ]; then
156 # Inverted drive:
157 # PWM value 255 -> Fan not spinning
158 # PWM value 0 -> Fan spinning at full speed
159
160 if [ "${AFCMINPWM[$fcvcount]}" -gt $MAX ]
161 then
162 echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2
163 echo "For inverted drive MINPWM must be at most $MAX" >&2
164 exit 1
165 fi
166
167 if [ "${AFCMINSTOP[$fcvcount]}" -lt "${AFCMAXPWM[$fcvcount]}" ]
168 then
169 echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2
170 echo "For inverted drive MINSTOP must be larger than MAXPWM" >&2
171 exit 1
172 fi
173
174 if [ "${AFCMINSTOP[$fcvcount]}" -ge "${AFCMINPWM[$fcvcount]}" ]
175 then
176 echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2
177 echo "For inverted drive MINSTOP must be less than MINPWM" >&2
178 exit 1
179 fi
180
181 if [ "${AFCMAXPWM[$fcvcount]}" -lt 0 ]
182 then
183 echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2
184 echo "For inverted drive MAXPWM must be at least 0" >&2
185 exit 1
186 fi
187
188 else
189 echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2
190 echo "MINPWM and MAXPWM cannot be equal" >&2
191 exit 1
192 fi
193
194 if [ "${AFCAVERAGE[$fcvcount]}" -lt 1 ]
195 then
196 echo "Error in configuration file (${AFCPWM[$fcvcount]}):" >&2
197 echo "AVERAGE must be at least 1" >&2
198 exit 1
199 fi
200 declare -a PREVIOUSTEMP_$fcvcount
201
202 echo
203 echo "Settings for ${AFCPWM[$fcvcount]}:"
204 echo " Depends on ${AFCTEMP[$fcvcount]}"
205 echo " Controls ${AFCFAN[$fcvcount]}"
206 echo " MINTEMP=${AFCMINTEMP[$fcvcount]}"
207 echo " MAXTEMP=${AFCMAXTEMP[$fcvcount]}"
208 echo " MINSTART=${AFCMINSTART[$fcvcount]}"
209 echo " MINSTOP=${AFCMINSTOP[$fcvcount]}"
210 echo " MINPWM=${AFCMINPWM[$fcvcount]}"
211 echo " MAXPWM=${AFCMAXPWM[$fcvcount]}"
212 echo " AVERAGE=${AFCAVERAGE[$fcvcount]}"
213 let fcvcount=fcvcount+1
214 done
215 echo
216 }
217
218 function DevicePath()
219 {
220 if [ -h "$1/device" ]
221 then
222 readlink -f "$1/device" | sed -e 's/^\/sys\///'
223 fi
224 }
225
226 function DeviceName()
227 {
228 if [ -r "$1/name" ]
229 then
230 cat "$1/name" | sed -e 's/[[:space:]=]/_/g'
231 elif [ -r "$1/device/name" ]
232 then
233 cat "$1/device/name" | sed -e 's/[[:space:]=]/_/g'
234 fi
235 }
236
237 function ValidateDevices()
238 {
239 local OLD_DEVPATH="$1" OLD_DEVNAME="$2" outdated=0
240 local entry device name path
241
242 for entry in $OLD_DEVPATH
243 do
244 device=$(echo "$entry" | sed -e 's/=[^=]*$//')
245 path=$(echo "$entry" | sed -e 's/^[^=]*=//')
246
247 if [ "$(DevicePath "$device")" != "$path" ]
248 then
249 echo "Device path of $device has changed" >&2
250 outdated=1
251 fi
252 done
253
254 for entry in $OLD_DEVNAME
255 do
256 device=$(echo "$entry" | sed -e 's/=[^=]*$//')
257 name=$(echo "$entry" | sed -e 's/^[^=]*=//')
258
259 if [ "$(DeviceName "$device")" != "$name" ]
260 then
261 echo "Device name of $device has changed" >&2
262 outdated=1
263 fi
264 done
265
266 return $outdated
267 }
268
269 function FixupDeviceFiles
270 {
271 local DEVICE="$1"
272 local fcvcount pwmo tsen fan
273
274 let fcvcount=0
275 while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
276 do
277 pwmo=${AFCPWM[$fcvcount]}
278 AFCPWM[$fcvcount]=${pwmo//$DEVICE\/device/$DEVICE}
279 if [ "${AFCPWM[$fcvcount]}" != "$pwmo" ]
280 then
281 echo "Adjusing $pwmo -> ${AFCPWM[$fcvcount]}"
282 fi
283 let fcvcount=$fcvcount+1
284 done
285
286 let fcvcount=0
287 while (( $fcvcount < ${#AFCTEMP[@]} )) # go through all temp inputs
288 do
289 tsen=${AFCTEMP[$fcvcount]}
290 AFCTEMP[$fcvcount]=${tsen//$DEVICE\/device/$DEVICE}
291 if [ "${AFCTEMP[$fcvcount]}" != "$tsen" ]
292 then
293 echo "Adjusing $tsen -> ${AFCTEMP[$fcvcount]}"
294 fi
295 let fcvcount=$fcvcount+1
296 done
297
298 let fcvcount=0
299 while (( $fcvcount < ${#AFCFAN[@]} )) # go through all fan inputs
300 do
301 fan=${AFCFAN[$fcvcount]}
302 AFCFAN[$fcvcount]=${fan//$DEVICE\/device/$DEVICE}
303 if [ "${AFCFAN[$fcvcount]}" != "$fan" ]
304 then
305 echo "Adjusing $fan -> ${AFCFAN[$fcvcount]}"
306 fi
307 let fcvcount=$fcvcount+1
308 done
309 }
310
311 # Some drivers moved their attributes from hard device to class device
312 function FixupFiles
313 {
314 local DEVPATH="$1"
315 local entry device
316
317 for entry in $DEVPATH
318 do
319 device=$(echo "$entry" | sed -e 's/=[^=]*$//')
320
321 if [ -e "$device/name" ]
322 then
323 FixupDeviceFiles "$device"
324 fi
325 done
326 }
327
328 # Check that all referenced sysfs files exist
329 function CheckFiles
330 {
331 local outdated=0 fcvcount pwmo tsen fan
332
333 let fcvcount=0
334 while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
335 do
336 pwmo=${AFCPWM[$fcvcount]}
337 if [ ! -w $pwmo ]
338 then
339 echo "Error: file $pwmo doesn't exist or isn't writable" >&2
340 outdated=1
341 fi
342 let fcvcount=$fcvcount+1
343 done
344
345 let fcvcount=0
346 while (( $fcvcount < ${#AFCTEMP[@]} )) # go through all temp inputs
347 do
348 tsen=${AFCTEMP[$fcvcount]}
349 if [ ${tsen::1} == "!" ]
350 then
351 if ! [ -x ${tsen:1} ]
352 then
353 echo "Error: file ${tsen:1} doesn't exist or isn't executable" >&2
354 outdated=1
355 fi
356 else
357 if ! [ -r $tsen ]
358 then
359 echo "Error: file $tsen doesn't exist or isn't readable" >&2
360 outdated=1
361 fi
362 fi
363 let fcvcount=$fcvcount+1
364 done
365
366 let fcvcount=0
367 while (( $fcvcount < ${#AFCFAN[@]} )) # go through all fan inputs
368 do
369 # A given PWM output can control several fans
370 for fan in $(echo ${AFCFAN[$fcvcount]} | sed -e 's/+/ /')
371 do
372 if [ ! -r $fan ]
373 then
374 echo "Error: file $fan doesn't exist or isn't readable" >&2
375 outdated=1
376 fi
377 done
378 let fcvcount=$fcvcount+1
379 done
380
381 if [ $outdated -eq 1 ]
382 then
383 echo >&2
384 echo "At least one referenced file is missing or doesn't have" >&2
385 echo "correct privileges. Either some required kernel" >&2
386 echo "modules haven't been loaded, or your configuration file is outdated." >&2
387 echo "In the latter case, you should run pwmconfig again." >&2
388 fi
389
390 return $outdated
391 }
392
•393 if [ "$1" == "--check" ]
394 then
395 if [ -f "$2" ]
396 then
397 LoadConfig $2
398 else
399 LoadConfig /etc/fancontrol
400 fi
401 exit 0
402 fi
403
404 if [ -f "$1" ]
405 then
406 LoadConfig $1
407 else
408 LoadConfig /etc/fancontrol
409 fi
410
411 # Detect path to sensors
412 if echo "${AFCPWM[0]}" | grep -E -q '^/'
413 then
414 DIR=/
415 elif echo "${AFCPWM[0]}" | grep -E -q '^hwmon[0-9]'
416 then
417 DIR=/sys/class/hwmon
418 elif echo "${AFCPWM[0]}" | grep -E -q '^[1-9]*[0-9]-[0-9abcdef]{4}'
419 then
420 DIR=/sys/bus/i2c/devices
421 else
422 echo "$0: Invalid path to sensors" >&2
423 exit 1
424 fi
425
426 if [ ! -d $DIR ]
427 then
428 echo $0: 'No sensors found! (did you load the necessary modules?)' >&2
429 exit 1
430 fi
431 cd $DIR
432
433 # Check for configuration change
434 if [ "$DIR" != "/" ] && [ -z "$DEVPATH" -o -z "$DEVNAME" ]
435 then
436 echo "Configuration is too old, please run pwmconfig again" >&2
437 exit 1
438 fi
439 if [ "$DIR" = "/" -a -n "$DEVPATH" ]
440 then
441 echo "Unneeded DEVPATH with absolute device paths" >&2
442 exit 1
443 fi
444 if ! ValidateDevices "$DEVPATH" "$DEVNAME"
445 then
446 echo "Configuration appears to be outdated, please run pwmconfig again" >&2
447 exit 1
448 fi
449 if [ "$DIR" = "/sys/class/hwmon" ]
450 then
451 FixupFiles "$DEVPATH"
452 fi
453 CheckFiles || exit 1
454
455 if [ -f "$PIDFILE" ]
456 then
457 echo "File $PIDFILE exists, is fancontrol already running?" >&2
458 exit 1
459 fi
460 echo $$ > "$PIDFILE"
461
462 # associative arrays to hold pwmN device name as key, and as value the
463 # pwmN_enable and pwmN values as they were before fancontrol was started
464 declare -A PWM_ENABLE_ORIG_STATE
465 declare -A PWM_ORIG_STATE
466
467 # $1 = pwm file name
468 function pwmdisable()
469 {
470 local ENABLE=${1}_enable
471
472 # No enable file? Just set to max
473 if [ ! -f $ENABLE ]
474 then
475 echo $MAX > $1
476 return 0
477 fi
478
479 # Try to restore pwmN and pwmN_enable value to the same state as before
480 # fancontrol start. Restoring the pwmN value is tried first, before the
481 # pwmN_enable mode switch.
482 # Some chips seem to need this to properly restore fan operation,
483 # when activating automatic (2) mode.
484 if [ ${PWM_ENABLE_ORIG_STATE[${1}]} ]
485 then
486 #restore the pwmN value
487 if [ "$DEBUG" != "" ]
488 then
489 echo "Restoring ${1} original value of ${PWM_ORIG_STATE[${1}]}"
490 fi
491 echo ${PWM_ORIG_STATE[${1}]} > ${1} 2> /dev/null
492 # restore the pwmN_enable value, if it is not 1.
493 # 1 is already set through fancontrol and setting it again might just
494 # reset the pwmN value.
495 if [ ${PWM_ENABLE_ORIG_STATE[${1}]} != 1 ]
496 then
497 if [ "$DEBUG" != "" ]
498 then
499 echo "Restoring $ENABLE original value of ${PWM_ENABLE_ORIG_STATE[${1}]}"
500 fi
501 echo ${PWM_ENABLE_ORIG_STATE[${1}]} > $ENABLE 2> /dev/null
502 # check if setting pwmN_enable value was successful. Checking the
503 # pwmN value makes no sense, as it might already have been altered
504 # by the chip.
505 if [ "$(cat $ENABLE)" = ${PWM_ENABLE_ORIG_STATE[${1}]} ]
506 then
507 return 0
508 fi
509 # if pwmN_enable is manual (1), check if restoring the pwmN value worked
510 elif [ "$(cat ${1})" = ${PWM_ORIG_STATE[${1}]} ]
511 then
512 return 0
513 fi
514 fi
515
516 # Try pwmN_enable=0
517 echo 0 > $ENABLE 2> /dev/null
518 if [ "$(cat $ENABLE)" -eq 0 ]
519 then
520 # Success
521 return 0
522 fi
523
524 # It didn't work, try pwmN_enable=1 pwmN=255
525 echo 1 > $ENABLE 2> /dev/null
526 echo $MAX > $1
527 if [ "$(cat $ENABLE)" -eq 1 -a "$(cat $1)" -ge 190 ]
528 then
529 # Success
530 return 0
531 fi
532
533 # Nothing worked
534 echo "$ENABLE stuck to" "$(cat $ENABLE)" >&2
535 return 1
536 }
537
538 # $1 = pwm file name
539 function pwmenable()
540 {
541 local ENABLE=${1}_enable
542
543 if [ -f $ENABLE ]
544 then
545 # save the original pwmN_control state, e.g. 1 for manual or 2 for auto,
546 # and the value from pwmN
547 local PWM_CONTROL_ORIG=$(cat $ENABLE)
548 local PWM_ORIG=$(cat ${1})
549 if [ "$DEBUG" != "" ]
550 then
551 echo "Saving $ENABLE original value as $PWM_CONTROL_ORIG"
552 echo "Saving ${1} original value as $PWM_ORIG"
553 fi
554 #check for degenerate case where these values might be empty
555 if [ $PWM_CONTROL_ORIG ] && [ $PWM_ORIG ]
556 then
557 PWM_ENABLE_ORIG_STATE[${1}]=$PWM_CONTROL_ORIG
558 PWM_ORIG_STATE[${1}]=$PWM_ORIG
559 fi
560 # enable manual control by fancontrol
561 echo 1 > $ENABLE 2> /dev/null
562 if [ "$(cat $ENABLE)" -ne 1 ]
563 then
564 return 1
565 fi
566 fi
567 echo $MAX > $1
568 }
569
570 function restorefans()
571 {
572 local status=$1 fcvcount pwmo
573
574 echo 'Aborting, restoring fans...'
575 let fcvcount=0
576 while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
577 do
578 pwmo=${AFCPWM[$fcvcount]}
579 pwmdisable $pwmo
580 let fcvcount=$fcvcount+1
581 done
582 echo 'Verify fans have returned to full speed'
583 rm -f "$PIDFILE"
584 exit $status
585 }
586
587 trap 'restorefans 0' SIGQUIT SIGTERM
588 trap 'restorefans 1' SIGHUP SIGINT
589
590 # main function
591 function UpdateFanSpeeds
592 {
593 local fcvcount
594 local pwmo tsens fan mint maxt minsa minso minpwm maxpwm
595 local tval tlastval pwmpval fanval min_fanval one_fan one_fanval
596 local -i pwmval
597
598 let fcvcount=0
599 while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
600 do
601 #hopefully shorter vars will improve readability:
602 pwmo=${AFCPWM[$fcvcount]}
603 tsens=${AFCTEMP[$fcvcount]}
604 fan=${AFCFAN[$fcvcount]}
605 let mint="${AFCMINTEMP[$fcvcount]}*1000"
606 let maxt="${AFCMAXTEMP[$fcvcount]}*1000"
607 minsa=${AFCMINSTART[$fcvcount]}
608 minso=${AFCMINSTOP[$fcvcount]}
609 minpwm=${AFCMINPWM[$fcvcount]}
610 maxpwm=${AFCMAXPWM[$fcvcount]}
611 avg=${AFCAVERAGE[$fcvcount]}
612
613 if [ ${tsens::1} == "!" ]
614 then
615 tlastval="$(${tsens:1})"
616 else
617 read tlastval < ${tsens}
618 fi
619 if [ $? -ne 0 ]
620 then
621 echo "Error reading temperature from $DIR/$tsens"
622 restorefans 1
623 fi
624
625 read pwmpval < ${pwmo}
626 if [ $? -ne 0 ]
627 then
628 echo "Error reading PWM value from $DIR/$pwmo"
629 restorefans 1
630 fi
631
632 # copy PREVIOUSTEMP_$fcvcount array to prevtemp
633 declare -a 'prevtemp=(${'"PREVIOUSTEMP_$fcvcount"'[@]})'
634 # add new element to the end of the array
635 prevtemp+=($tlastval)
636 # if needed, remove the first element of the array
637 if [ "${#prevtemp[@]}" -gt $avg ]
638 then
639 prevtemp=("${prevtemp[@]:1}")
640 fi
641 # calculate the average value of all elements
642 tval=$(( ( ${prevtemp[@]/%/+}0 ) / ${#prevtemp[@]} ))
643 # copy prevtemp back to PREVIOUSTEMP_$fcvcount
644 eval "PREVIOUSTEMP_$fcvcount=(\"${prevtemp[@]}\")"
645
646 # If fanspeed-sensor output shall be used, do it
647 if [[ -n ${fan} ]]
648 then
649 min_fanval=100000
650 fanval=
651 # A given PWM output can control several fans
652 for one_fan in $(echo $fan | sed -e 's/+/ /')
653 do
654 read one_fanval < ${one_fan}
655 if [ $? -ne 0 ]
656 then
657 echo "Error reading Fan value from $DIR/$one_fan" >&2
658 restorefans 1
659 fi
660
661 # Remember the minimum, it only matters if it is 0
662 if [ $one_fanval -lt $min_fanval ]
663 then
664 min_fanval=$one_fanval
665 fi
666
667 if [ -z "$fanval" ]
668 then
669 fanval=$one_fanval
670 else
671 fanval="$fanval/$one_fanval"
672 fi
673 done
674 else
675 min_fanval=1 # set it to a non zero value, so the rest of the script still works
676 fi
677
678 # debug info
679 if [ "$DEBUG" != "" ]
680 then
681 echo "pwmo=$pwmo"
682 echo "tsens=$tsens"
683 echo "fan=$fan"
684 echo "mint=$mint"
685 echo "maxt=$maxt"
686 echo "minsa=$minsa"
687 echo "minso=$minso"
688 echo "minpwm=$minpwm"
689 echo "maxpwm=$maxpwm"
690 echo "tlastval=$tlastval"
691 echo "prevtemp=${prevtemp[@]}"
692 echo "tval=$tval"
693 echo "pwmpval=$pwmpval"
694 echo "fanval=$fanval"
695 echo "min_fanval=$min_fanval"
696 fi
697
698 if (( $tval <= $mint ))
699 then pwmval=$minpwm # below min temp, use defined min pwm
700 elif (( $tval >= $maxt ))
701 then pwmval=$maxpwm # over max temp, use defined max pwm
702 else
703 # calculate the new value from temperature and settings
704 pwmval="(${tval}-${mint})*(${maxpwm}-${minso})/(${maxt}-${mint})+${minso}"
705 if [ $pwmpval -eq 0 -o $min_fanval -eq 0 ]
706 then # if fan was stopped start it using a safe value
707 echo $minsa > $pwmo
708 # Sleep while still handling signals
709 sleep 1 &
710 wait
711 fi
712 fi
713 echo $pwmval > $pwmo # write new value to pwm output
714 if [ $? -ne 0 ]
715 then
716 echo "Error writing PWM value to $DIR/$pwmo" >&2
717 restorefans 1
718 fi
719 if [ "$DEBUG" != "" ]
720 then
721 echo "new pwmval=$pwmval"
722 fi
723 let fcvcount=$fcvcount+1
724 done
725 }
726
727 echo 'Enabling PWM on fans...'
728 let fcvcount=0
729 while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
730 do
731 pwmo=${AFCPWM[$fcvcount]}
732 pwmenable $pwmo
733 if [ $? -ne 0 ]
734 then
735 echo "Error enabling PWM on $DIR/$pwmo" >&2
736 restorefans 1
737 fi
738 let fcvcount=$fcvcount+1
739 done
740
741 echo 'Starting automatic fan control...'
742
743 # main loop calling the main function at specified intervals
744 while true
745 do
746 UpdateFanSpeeds
747 # Sleep while still handling signals
748 sleep $INTERVAL &
749 wait
750 done