Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 279 lines (196 sloc) 5.418 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
#! /bin/sh -e
# file://~/bin/fanctrl.sh #
# @author: www.Philippe.COVAL.online.FR - Rev:$Author: rzr $
# Copyright and License : GPL-3+ -- http://RzR.online.fr/license.htm
# URL: http://rzr.online.fr/q/lenovo
# Credits: http://tech.groups.yahoo.com/group/Lenovo/message/18# Mark K
# -*- Mode: Sh -*-
#------------------------------------------------------------------------------
#set -x
url="https://github.com/rzr/acpi_call"

### { Settings
#DEBUG=1

# @param step : delay in seconds
# noticed that it could increase to 16C per 10sec
step=10

# @param crit : Critical temperature
# lower than tjunction=99999 100C 127C ?
# higher than 77C (max when fan is on) or probally 86C (observed when fan off)
crit=86000

# @param low : temp where fan should switch off
# 49C was ok it seems it's hard to go below 43C when ambiant is cool
# default=41C?
# 35C on boot
#low=43000
low=47000

# @param high : temp where fan should turn on
# 55C was ok but may be higher than 43 at least
# should be lower than hdd 65C / still working when 74C , 86C ?
#high=55000
high=57000

LANG=C

### } Settings


diff=0

min=$crit
max=00000
temp=$max
prev=$max

past=$(date +%s)

timeon=0
timeoff=0

state=255


usage_()
{
    cat<<EOF
# Info: Userspace temp/fan regulator
# More: http://rzr.online.fr/q/lenovo

# This script has been created for lenovo-g470 and gX70
# as a workaround for buggy bios (check URL for details)
# I guess it could be easly adapted to upport more bios, firmwares etc
# feel free to update and push it to SCM
# should be started as root else it will try using sudo

EOF
}


log_()
{
    echo "log: $@ ($(uptime))"
}


log_d_()
{
    [ -z $DEBUG ] || log_ "debug: $@"
}


ignore_()
{
    echo "ignore: $@ 2>&1 "> /dev/null
}


watch_()
{
    watch '{ \dmesg | tail ; }'
}


mod_debian_()
{
cat<<EOF
#TODO
url_deb="http://apt.auf.org/pool/auf/a/acpi-call-dkms/acpi-call-dkms_20110625_all.deb"
/etc/local/bin/admin.sh install_url_ "${url_deb}"
sudo aptitude install linux-headers-3.2.0-2-amd64
sudo dkms build -m acpi-call -v 20110625
EOF
}


mod_()
{
    echo "module: $url"

    pwd

lsmod | { grep acpi_call && return 0 ; } || echo "not loaded"

# modprobe -l | grep acpi_call || echo "not available"

    kver="3.2.0-rc6lenovo-g470+"
    kver="$(uname -r)"
    extra="/lib/modules/${kver}/kernel/extra/"
    module="$extra/acpi_call.ko"

    ls "$extra/" || echo "not installed"
    sudo insmod "$module" || echo "not loadable"

    lsmod | grep acpi_call && return 0 || echo "ignored"

    echo "info: about to build"
# read t
    d="/usr/local/src/acpi_call/"

    cd "$d" || { cd /usr/local/src && git clone "$url" ;}
    cd "$d" || return 1;

    make clean || echo "ignored: "
    make

    ko="$d/acpi_call.ko"
    sudo modinfo "$ko"
    sudo insmod "$ko"

    mkdir -pv "$extra"
    cp -v "$ko" "$extra/"
}


tell_()
{
    cat /sys/devices/virtual/thermal/thermal_zone0/temp

    echo '\_TZ_.FN00._STA' \
| sudo tee /proc/acpi/call && sudo cat /proc/acpi/call

    echo '\_TZ_.FN01._STA' \
| sudo tee /proc/acpi/call && sudo cat /proc/acpi/call
}


off_()
{
    echo '\_TZ_.FN00._OFF' | sudo tee /proc/acpi/call \
&& ignore_ sudo cat /proc/acpi/call

    echo '\_TZ_.FN01._OFF' | sudo tee /proc/acpi/call \
&& ignore_ sudo cat /proc/acpi/call
}


on_()
{
    echo '\_TZ_.FN00._ON' | sudo tee /proc/acpi/call \
&& ignore_ sudo cat /proc/acpi/call
    echo '\_TZ_.FN01._ON' | sudo tee /proc/acpi/call \
&& ignore_ sudo cat /proc/acpi/call
}


#TODO: switch fan one after one if needed
switch_()
{
    log_d_ "switch: state=$state temp=$temp";

    [ "_$1" != "_$state" ] || return 0

    past=$(date +%s)

    case $1 in
0)
off_
state="0"
[ $time -lt $timeon ] || timeon=$time
;;
1|*)
# log_ "switcmaxng on the two fans"
on_
state="1"
[ $time -lt $timeoff ] || timeoff=$time
;;
    esac \
2>&1 > /dev/null
# log_ "switch: state=$state temp=$temp";
}


main_()
{
    usage_

    [ "_root" = "_${USER}" ] || { sudo $0 ; return $?; }

    cat /proc/version

    lsmod | grep acpi_call || mod_
    lsmod | grep acpi_call || return 1

# sleep $step

    temp=`cat /sys/devices/virtual/thermal/thermal_zone0/temp`;
    prev=${temp}

    while true; do

[ -z $DEBUG ] || sensors
# hdd
[ -z $DEBUG ] || sudo hddtemp /dev/sda # 41C TZ=46
# sensors | grep temp1 | cut -c 15-19 |sed 's/+//' # 70.0
# temp=$(sensors | grep 'Physical id 0:' | cut -c 15-19 |sed 's/+//')
temp=`cat /sys/devices/virtual/thermal/thermal_zone0/temp`;

[ $min -le $temp ] || min=$temp
[ $max -ge $temp ] || max=$temp

time=$(date +%s)
time=$(expr $time - $past || printf '')

# prev=$(expr $temp - $prev)
# prev=$(expr $prev / $step )


if [ $crit -le $temp ] ; then
echo "error: crittical issue should not happend shutting now !!!"
sudo halt
elif [ $high -le $temp ] ; then
switch_ 1
elif [ $low -ge $temp ] ; then
switch_ 0
# else
# echo "should not happend"
fi

log_ "c=$temp f=$state o=$prev s=$time ($max*$timeon>$min*$timeoff+d=$diff/$step) ($low<$high<$crit)"

sleep $step ;

# echo "updating diff max variation ( $temp - $prev ) during time=$step"
prev=$(expr ${temp} - ${prev} || printf '')

# echo "updating $diff vs $prev ; $?"
[ $prev -lt $diff ] || diff=$prev

prev=$temp

    done
}


#{


[ ! -z $1 ] || main_

$@

#}


#eof
Something went wrong with that request. Please try again.