Debian Codegraph @

prog/pwm/fancontrol

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 [ -f "$1" ]
394 then
395 LoadConfig $1
396 else
397 LoadConfig /etc/fancontrol
398 fi
399
400 # Detect path to sensors
401 if echo "${AFCPWM[0]}" | grep -E -q '^/'
402 then
403 DIR=/
404 elif echo "${AFCPWM[0]}" | grep -E -q '^hwmon[0-9]'
405 then
406 DIR=/sys/class/hwmon
407 elif echo "${AFCPWM[0]}" | grep -E -q '^[1-9]*[0-9]-[0-9abcdef]{4}'
408 then
409 DIR=/sys/bus/i2c/devices
410 else
411 echo "$0: Invalid path to sensors" >&2
412 exit 1
413 fi
414
415 if [ ! -d $DIR ]
416 then
417 echo $0: 'No sensors found! (did you load the necessary modules?)' >&2
418 exit 1
419 fi
420 cd $DIR
421
422 # Check for configuration change
423 if [ "$DIR" != "/" ] && [ -z "$DEVPATH" -o -z "$DEVNAME" ]
424 then
425 echo "Configuration is too old, please run pwmconfig again" >&2
426 exit 1
427 fi
428 if [ "$DIR" = "/" -a -n "$DEVPATH" ]
429 then
430 echo "Unneeded DEVPATH with absolute device paths" >&2
431 exit 1
432 fi
433 if ! ValidateDevices "$DEVPATH" "$DEVNAME"
434 then
435 echo "Configuration appears to be outdated, please run pwmconfig again" >&2
436 exit 1
437 fi
438 if [ "$DIR" = "/sys/class/hwmon" ]
439 then
440 FixupFiles "$DEVPATH"
441 fi
442 CheckFiles || exit 1
443
444 if [ -f "$PIDFILE" ]
445 then
446 echo "File $PIDFILE exists, is fancontrol already running?" >&2
447 exit 1
448 fi
449 echo $$ > "$PIDFILE"
450
451 # associative arrays to hold pwmN device name as key, and as value the
452 # pwmN_enable and pwmN values as they were before fancontrol was started
453 declare -A PWM_ENABLE_ORIG_STATE
454 declare -A PWM_ORIG_STATE
455
456 # $1 = pwm file name
457 function pwmdisable()
458 {
459 local ENABLE=${1}_enable
460
461 # No enable file? Just set to max
462 if [ ! -f $ENABLE ]
463 then
464 echo $MAX > $1
465 return 0
466 fi
467
468 # Try to restore pwmN and pwmN_enable value to the same state as before
469 # fancontrol start. Restoring the pwmN value is tried first, before the
470 # pwmN_enable mode switch.
471 # Some chips seem to need this to properly restore fan operation,
472 # when activating automatic (2) mode.
473 if [ ${PWM_ENABLE_ORIG_STATE[${1}]} ]
474 then
475 #restore the pwmN value
476 if [ "$DEBUG" != "" ]
477 then
478 echo "Restoring ${1} original value of ${PWM_ORIG_STATE[${1}]}"
479 fi
480 echo ${PWM_ORIG_STATE[${1}]} > ${1} 2> /dev/null
481 # restore the pwmN_enable value, if it is not 1.
482 # 1 is already set through fancontrol and setting it again might just
483 # reset the pwmN value.
484 if [ ${PWM_ENABLE_ORIG_STATE[${1}]} != 1 ]
485 then
486 if [ "$DEBUG" != "" ]
487 then
488 echo "Restoring $ENABLE original value of ${PWM_ENABLE_ORIG_STATE[${1}]}"
489 fi
490 echo ${PWM_ENABLE_ORIG_STATE[${1}]} > $ENABLE 2> /dev/null
491 # check if setting pwmN_enable value was successful. Checking the
492 # pwmN value makes no sense, as it might already have been altered
493 # by the chip.
494 if [ "$(cat $ENABLE)" = ${PWM_ENABLE_ORIG_STATE[${1}]} ]
495 then
496 return 0
497 fi
498 # if pwmN_enable is manual (1), check if restoring the pwmN value worked
499 elif [ "$(cat ${1})" = ${PWM_ORIG_STATE[${1}]} ]
500 then
501 return 0
502 fi
503 fi
504
505 # Try pwmN_enable=0
506 echo 0 > $ENABLE 2> /dev/null
507 if [ "$(cat $ENABLE)" -eq 0 ]
508 then
509 # Success
510 return 0
511 fi
512
513 # It didn't work, try pwmN_enable=1 pwmN=255
514 echo 1 > $ENABLE 2> /dev/null
515 echo $MAX > $1
516 if [ "$(cat $ENABLE)" -eq 1 -a "$(cat $1)" -ge 190 ]
517 then
518 # Success
519 return 0
520 fi
521
522 # Nothing worked
523 echo "$ENABLE stuck to" "$(cat $ENABLE)" >&2
524 return 1
525 }
526
527 # $1 = pwm file name
528 function pwmenable()
529 {
530 local ENABLE=${1}_enable
531
532 if [ -f $ENABLE ]
533 then
534 # save the original pwmN_control state, e.g. 1 for manual or 2 for auto,
535 # and the value from pwmN
536 local PWM_CONTROL_ORIG=$(cat $ENABLE)
537 local PWM_ORIG=$(cat ${1})
538 if [ "$DEBUG" != "" ]
539 then
540 echo "Saving $ENABLE original value as $PWM_CONTROL_ORIG"
541 echo "Saving ${1} original value as $PWM_ORIG"
542 fi
543 #check for degenerate case where these values might be empty
544 if [ $PWM_CONTROL_ORIG ] && [ $PWM_ORIG ]
545 then
546 PWM_ENABLE_ORIG_STATE[${1}]=$PWM_CONTROL_ORIG
547 PWM_ORIG_STATE[${1}]=$PWM_ORIG
548 fi
549 # enable manual control by fancontrol
550 echo 1 > $ENABLE 2> /dev/null
551 if [ "$(cat $ENABLE)" -ne 1 ]
552 then
553 return 1
554 fi
555 fi
556 echo $MAX > $1
557 }
558
559 function restorefans()
560 {
561 local status=$1 fcvcount pwmo
562
563 echo 'Aborting, restoring fans...'
564 let fcvcount=0
565 while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
566 do
567 pwmo=${AFCPWM[$fcvcount]}
568 pwmdisable $pwmo
569 let fcvcount=$fcvcount+1
570 done
571 echo 'Verify fans have returned to full speed'
572 rm -f "$PIDFILE"
573 exit $status
574 }
575
576 trap 'restorefans 0' SIGQUIT SIGTERM
577 trap 'restorefans 1' SIGHUP SIGINT
578
579 # main function
580 function UpdateFanSpeeds
581 {
582 local fcvcount
583 local pwmo tsens fan mint maxt minsa minso minpwm maxpwm
584 local tval tlastval pwmpval fanval min_fanval one_fan one_fanval
585 local -i pwmval
586
587 let fcvcount=0
588 while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
589 do
590 #hopefully shorter vars will improve readability:
591 pwmo=${AFCPWM[$fcvcount]}
592 tsens=${AFCTEMP[$fcvcount]}
593 fan=${AFCFAN[$fcvcount]}
594 let mint="${AFCMINTEMP[$fcvcount]}*1000"
595 let maxt="${AFCMAXTEMP[$fcvcount]}*1000"
596 minsa=${AFCMINSTART[$fcvcount]}
597 minso=${AFCMINSTOP[$fcvcount]}
598 minpwm=${AFCMINPWM[$fcvcount]}
599 maxpwm=${AFCMAXPWM[$fcvcount]}
600 avg=${AFCAVERAGE[$fcvcount]}
601
602 if [ ${tsens::1} == "!" ]
603 then
604 tlastval="$(${tsens:1})"
605 else
606 read tlastval < ${tsens}
607 fi
608 if [ $? -ne 0 ]
609 then
610 echo "Error reading temperature from $DIR/$tsens"
611 restorefans 1
612 fi
613
614 read pwmpval < ${pwmo}
615 if [ $? -ne 0 ]
616 then
617 echo "Error reading PWM value from $DIR/$pwmo"
618 restorefans 1
619 fi
620
621 # copy PREVIOUSTEMP_$fcvcount array to prevtemp
622 declare -a 'prevtemp=(${'"PREVIOUSTEMP_$fcvcount"'[@]})'
623 # add new element to the end of the array
624 prevtemp+=($tlastval)
625 # if needed, remove the first element of the array
626 if [ "${#prevtemp[@]}" -gt $avg ]
627 then
628 prevtemp=("${prevtemp[@]:1}")
629 fi
630 # calculate the average value of all elements
631 tval=$(( ( ${prevtemp[@]/%/+}0 ) / ${#prevtemp[@]} ))
632 # copy prevtemp back to PREVIOUSTEMP_$fcvcount
633 eval "PREVIOUSTEMP_$fcvcount=(\"${prevtemp[@]}\")"
634
635 # If fanspeed-sensor output shall be used, do it
636 if [[ -n ${fan} ]]
637 then
638 min_fanval=100000
639 fanval=
640 # A given PWM output can control several fans
641 for one_fan in $(echo $fan | sed -e 's/+/ /')
642 do
643 read one_fanval < ${one_fan}
644 if [ $? -ne 0 ]
645 then
646 echo "Error reading Fan value from $DIR/$one_fan" >&2
647 restorefans 1
648 fi
649
650 # Remember the minimum, it only matters if it is 0
651 if [ $one_fanval -lt $min_fanval ]
652 then
653 min_fanval=$one_fanval
654 fi
655
656 if [ -z "$fanval" ]
657 then
658 fanval=$one_fanval
659 else
660 fanval="$fanval/$one_fanval"
661 fi
662 done
663 else
664 min_fanval=1 # set it to a non zero value, so the rest of the script still works
665 fi
666
667 # debug info
668 if [ "$DEBUG" != "" ]
669 then
670 echo "pwmo=$pwmo"
671 echo "tsens=$tsens"
672 echo "fan=$fan"
673 echo "mint=$mint"
674 echo "maxt=$maxt"
675 echo "minsa=$minsa"
676 echo "minso=$minso"
677 echo "minpwm=$minpwm"
678 echo "maxpwm=$maxpwm"
679 echo "tlastval=$tlastval"
680 echo "prevtemp=${prevtemp[@]}"
681 echo "tval=$tval"
682 echo "pwmpval=$pwmpval"
683 echo "fanval=$fanval"
684 echo "min_fanval=$min_fanval"
685 fi
686
687 if (( $tval <= $mint ))
688 then pwmval=$minpwm # below min temp, use defined min pwm
689 elif (( $tval >= $maxt ))
690 then pwmval=$maxpwm # over max temp, use defined max pwm
691 else
692 # calculate the new value from temperature and settings
693 pwmval="(${tval}-${mint})*(${maxpwm}-${minso})/(${maxt}-${mint})+${minso}"
694 if [ $pwmpval -eq 0 -o $min_fanval -eq 0 ]
695 then # if fan was stopped start it using a safe value
696 echo $minsa > $pwmo
697 # Sleep while still handling signals
698 sleep 1 &
699 wait
700 fi
701 fi
702 echo $pwmval > $pwmo # write new value to pwm output
703 if [ $? -ne 0 ]
704 then
705 echo "Error writing PWM value to $DIR/$pwmo" >&2
706 restorefans 1
707 fi
708 if [ "$DEBUG" != "" ]
709 then
710 echo "new pwmval=$pwmval"
711 fi
712 let fcvcount=$fcvcount+1
713 done
714 }
715
716 echo 'Enabling PWM on fans...'
717 let fcvcount=0
718 while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
719 do
720 pwmo=${AFCPWM[$fcvcount]}
721 pwmenable $pwmo
722 if [ $? -ne 0 ]
723 then
724 echo "Error enabling PWM on $DIR/$pwmo" >&2
725 restorefans 1
726 fi
727 let fcvcount=$fcvcount+1
728 done
729
730 echo 'Starting automatic fan control...'
731
732 # main loop calling the main function at specified intervals
733 while true
734 do
735 UpdateFanSpeeds
736 # Sleep while still handling signals
737 sleep $INTERVAL &
738 wait
739 done