wok diff acpid/stuff/acpi/power-supply.sh @ rev 2210

postgresql: update url
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 10 17:01:04 2009 +0000 (2009-02-10)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/acpid/stuff/acpi/power-supply.sh	Tue Feb 10 17:01:04 2009 +0000
     1.3 @@ -0,0 +1,235 @@
     1.4 +#!/bin/sh
     1.5 +# /etc/acpi/power-supply.sh - Managing power events for SliTaz
     1.6 +# For Tips & Tricks see http://www.lesswatts.org
     1.7 +
     1.8 +# This script turns off power savings mode on ac or when the 
     1.9 +# battery almost runs out in a attempt to limit data loss in
    1.10 +# a case of power failure.
    1.11 +
    1.12 +ENABLED=1
    1.13 +DISABLED=0
    1.14 +
    1.15 +# AC status (from /sys/class/power_supply/online)
    1.16 +ON_LINE=1
    1.17 +OFF_LINE=0
    1.18 +
    1.19 +# Battery status 
    1.20 +LOW_BAT=0
    1.21 +HIGH_BAT=1
    1.22 +
    1.23 +# Determining the power state.
    1.24 +
    1.25 +ac_status()
    1.26 +{
    1.27 +	POWER_SUPPLY_MAINS=$DISABLED
    1.28 +	AC_STATUS=$OFF_LINE
    1.29 +	for POWER_SUPPLY in /sys/class/power_supply/* ; do
    1.30 +		if [ -f $POWER_SUPPLY/type ] ; then
    1.31 +			if [ "$(cat $POWER_SUPPLY/type)" = "Mains" ] ;then
    1.32 +				echo -n "Determining power state from $POWER_SUPPLY: "
    1.33 +				POWER_SUPPLY_MAINS=$ENABLED
    1.34 +				if [ "$(cat $POWER_SUPPLY/online)" = 1 ] ; then
    1.35 +					AC_STATUS=$ON_LINE
    1.36 +					echo "on-line"
    1.37 +				else
    1.38 +					echo "off-line"
    1.39 +				fi
    1.40 +			fi
    1.41 +		fi
    1.42 +	done
    1.43 +	if [ $POWER_SUPPLY_MAINS -eq $DISABLED ] ; then
    1.44 +		$AC_STATUS=$ON_LINE
    1.45 +	fi
    1.46 +}
    1.47 +
    1.48 +# Determining the battery state.
    1.49 +
    1.50 +battery_status()
    1.51 +{
    1.52 +	BATTERY_STATUS=$LOW_BAT
    1.53 +	for BATT in /sys/class/power_supply/* ; do
    1.54 +		BATT_TYPE=$(cat $BATT/type)
    1.55 +		echo "$BATT is of type $BATT_TYPE."
    1.56 +		if [ "$BATT_TYPE" = "Battery" ] ; then
    1.57 +			echo "  Checking levels for $BATT."
    1.58 +			# Only do if the battery is present
    1.59 +	        if [ $(cat $BATT/present) -eq 1 ] ; then
    1.60 +
    1.61 +				# Get the remaining capacity.
    1.62 +				if [ -f $BATT/charge_now ] ; then
    1.63 +					REMAINING=$(cat $BATT/charge_now)
    1.64 +				elif [ -f $BATT/energy_now ] ; then
    1.65 +					REMAINING=$(cat $BATT/energy_now)
    1.66 +				else
    1.67 +					REMAINING=0
    1.68 +				fi
    1.69 +				if [ -z "$REMAINING" -o "$REMAINING" -eq 0 ] ; then
    1.70 +					echo "  Battery does not report remaining charge. Perhaps it is not present?"
    1.71 +				else
    1.72 +					echo "  Remaining charge: $REMAINING"
    1.73 +
    1.74 +					# Get the alarm level
    1.75 +					ALARM_LEVEL=$(cat $BATT/alarm)
    1.76 +					if [ "$ALARM_LEVEL" -eq 0 ] ; then
    1.77 +
    1.78 +						# Get the full capacity.
    1.79 +
    1.80 +						if [ -f $BATT/charge_full_design ] ; then
    1.81 +							CAPACITY=$(cat $BATT/charge_full_design)
    1.82 +						elif [ -f $BATT/energy_full_design ] ; then
    1.83 +							CAPACITY=$(cat $BATT/energy_full_design)
    1.84 +						else
    1.85 +							CAPACITY=0
    1.86 +						fi
    1.87 +						if [ -z "$CAPACITY" -o "$CAPACITY" -eq 0 ] ; then
    1.88 +							echo "  Battery does not report design full charge, using non-design full charge."
    1.89 +
    1.90 +							if [ -f $BATT/charge_full ] ; then
    1.91 +								CAPACITY=$(cat $BATT/charge_full)
    1.92 +							elif [ -f $BATT/energy_full_design ] ; then
    1.93 +								CAPACITY=$(cat $BATT/energy_full)
    1.94 +							else
    1.95 +								CAPACITY=0
    1.96 +							fi
    1.97 +							if [ -z "$CAPACITY" -o "$CAPACITY" -eq 0] ; then
    1.98 +								echo "  Battery does not report non-design full charge."
    1.99 +							fi
   1.100 +						fi
   1.101 +						echo "  Full capacity: $CAPACITY"
   1.102 +						ALARM_LEVEL=$((CAPACITY*5/100))
   1.103 +					fi
   1.104 +					echo "  Alarm level: $ALARM_LEVEL"
   1.105 +					if [ "$ALARM_LEVEL" -ne 0 ] ; then
   1.106 +						if [ "$REMAINING" -ge "$ALARM_LEVEL" ] ; then
   1.107 +							# this battery does count as having enough charge.
   1.108 +							BATTERY_STATUS=$HIGH_BAT
   1.109 +							echo "  Battery status: high"
   1.110 +						else
   1.111 +							echo "  Battery status: low"
   1.112 +						fi
   1.113 +					fi
   1.114 +				fi
   1.115 +			else
   1.116 +				echo "Battery is not present."
   1.117 +			fi
   1.118 +		fi
   1.119 +	done
   1.120 +}
   1.121 +
   1.122 +online_mode()
   1.123 +{
   1.124 +	# Disable laptop mode
   1.125 +	# When laptop mode is enabled, the kernel will try to be smart
   1.126 +	# about when to do IO, to give the disk and the SATA links as
   1.127 +	# much time as possible in a low power state.
   1.128 +
   1.129 +	if [ -e /proc/sys/vm/laptop_mode ] ; then
   1.130 +		echo "Disabling laptop mode"
   1.131 +		echo 0 > /proc/sys/vm/laptop_mode
   1.132 +	fi
   1.133 +
   1.134 +	# AC97 audio power saving mode
   1.135 +	# The AC97 onboard audio chips support power saving, where the
   1.136 +	# analog parts (codec) are powered down when no program is using
   1.137 +	# the audio device.
   1.138 +
   1.139 +	if [ -e /sys/module/snd_ac97_codec/parameters/power_save ] ; then
   1.140 +		echo "Disabling AC97 audio power saving mode"
   1.141 +		echo 0 > /sys/module/snd_ac97_codec/parameters/power_save
   1.142 +	fi
   1.143 +
   1.144 +	# The VM writeback time
   1.145 +	# The VM subsystem caching allows the kernel to group consecutive
   1.146 +	# writes into one big write, and to generally optimize the disk IO
   1.147 +	# to be the most efficient. 
   1.148 +
   1.149 +	if [ -e /proc/sys/vm/dirty_writeback_centisecs ] ; then
   1.150 +		echo "Writeback time reset to 500ms"
   1.151 +		echo 500 > /proc/sys/vm/dirty_writeback_centisecs
   1.152 +	fi
   1.153 +}
   1.154 +
   1.155 +offline_mode()
   1.156 +{
   1.157 +	# Enable laptop mode
   1.158 +	# When laptop mode is enabled, the kernel will try to be smart
   1.159 +	# about when to do IO, to give the disk and the SATA links as
   1.160 +	# much time as possible in a low power state. 
   1.161 +
   1.162 +	if [ ! -e /proc/sys/vm/laptop_mode ] ; then
   1.163 +		echo "Kernel does not have support for laptop mode." 
   1.164 +	else
   1.165 +		echo "Enabling laptop mode"
   1.166 +		echo 5 > /proc/sys/vm/laptop_mode
   1.167 +	fi
   1.168 +
   1.169 +	# AC97 audio power saving mode
   1.170 +	# The AC97 onboard audio chips support power saving, where the
   1.171 +	# analog parts (codec) are powered down when no program is using
   1.172 +	# the audio device.
   1.173 +
   1.174 +	if [ -e /sys/module/snd_ac97_codec/parameters/power_save ] ; then
   1.175 +		echo "Enabling AC97 audio power saving mode"
   1.176 +		echo 1 > /sys/module/snd_ac97_codec/parameters/power_save
   1.177 +		echo 1 > /dev/dsp
   1.178 +	fi
   1.179 +
   1.180 +	# The VM writeback time
   1.181 +	# The VM subsystem caching allows the kernel to group consecutive
   1.182 +	# writes into one big write, and to generally optimize the disk IO
   1.183 +	# to be the most efficient.
   1.184 +
   1.185 +	if [ -e /proc/sys/vm/dirty_writeback_centisecs ] ; then
   1.186 +		echo "Writeback time set to 1500ms"
   1.187 +		echo 1500 > /proc/sys/vm/dirty_writeback_centisecs
   1.188 +	fi
   1.189 +}
   1.190 +
   1.191 +power_status()
   1.192 +{
   1.193 +	if [ $(cat /proc/sys/vm/dirty_writeback_centisecs) -gt 1000 ]; then
   1.194 +		POWER_SAVINGS=$ENABLED
   1.195 +		echo "power-savings-mode enabled"
   1.196 +	else
   1.197 +		POWER_SAVINGS=$DISABLED
   1.198 +		echo "power-savings-mode disabled"
   1.199 +	fi
   1.200 +}
   1.201 +
   1.202 +custom_scripts()
   1.203 +{
   1.204 +	# Custom scripts in /etc/acpi/ac.d
   1.205 +
   1.206 +	if [ -d /etc/acpi/ac.d ]; then
   1.207 +		for SCRIPT in /etc/acpi/ac.d/*.sh; do
   1.208 +			. $SCRIPT $AC_STATUS $BATTERY_STATUS $0
   1.209 +		done
   1.210 +	fi
   1.211 +
   1.212 +	# Custom scripts in /etc/acpi/battery.d
   1.213 +
   1.214 +	if [ -d /etc/acpi/battery.d ]; then
   1.215 +		for SCRIPT in /etc/acpi/battery.d/*.sh; do
   1.216 +			. $SCRIPT $AC_STATUS $BATTERY_STATUS $0
   1.217 +		done
   1.218 +	fi
   1.219 +}
   1.220 +
   1.221 +ac_status
   1.222 +battery_status
   1.223 +power_status
   1.224 +case "$AC_STATUS+$BATTERY_STATUS" in
   1.225 +	"$OFF_LINE+$HIGH_BAT")
   1.226 +		if [ $POWER_SAVINGS = $DISABLED ]; then 
   1.227 +			logger "Start power savings mode"
   1.228 +			offline_mode
   1.229 +		fi
   1.230 +	;;
   1.231 +	*)
   1.232 +		if [ $POWER_SAVINGS = $ENABLED ]; then
   1.233 +			logger "Stop power savings mode"
   1.234 +			online_mode
   1.235 +		fi
   1.236 +	;;
   1.237 +esac
   1.238 +custom_scripts
   1.239 \ No newline at end of file