wok-current annotate acpid/stuff/acpi/power-supply.sh @ rev 7879

Up: gobject-introspection (0.10.0)
author Christophe Lincoln <pankso@slitaz.org>
date Mon Jan 03 23:30:56 2011 +0100 (2011-01-03)
parents
children
rev   line source
domcox@1624 1 #!/bin/sh
domcox@1624 2 # /etc/acpi/power-supply.sh - Managing power events for SliTaz
domcox@1624 3 # For Tips & Tricks see http://www.lesswatts.org
domcox@1624 4
domcox@1624 5 # This script turns off power savings mode on ac or when the
domcox@1624 6 # battery almost runs out in a attempt to limit data loss in
domcox@1624 7 # a case of power failure.
domcox@1624 8
domcox@1624 9 ENABLED=1
domcox@1624 10 DISABLED=0
domcox@1624 11
domcox@1624 12 # AC status (from /sys/class/power_supply/online)
domcox@1624 13 ON_LINE=1
domcox@1624 14 OFF_LINE=0
domcox@1624 15
domcox@1624 16 # Battery status
domcox@1624 17 LOW_BAT=0
domcox@1624 18 HIGH_BAT=1
domcox@1624 19
domcox@1624 20 # Determining the power state.
domcox@1624 21
domcox@1624 22 ac_status()
domcox@1624 23 {
domcox@1624 24 POWER_SUPPLY_MAINS=$DISABLED
domcox@1624 25 AC_STATUS=$OFF_LINE
domcox@1624 26 for POWER_SUPPLY in /sys/class/power_supply/* ; do
domcox@1624 27 if [ -f $POWER_SUPPLY/type ] ; then
domcox@1624 28 if [ "$(cat $POWER_SUPPLY/type)" = "Mains" ] ;then
domcox@1624 29 echo -n "Determining power state from $POWER_SUPPLY: "
domcox@1624 30 POWER_SUPPLY_MAINS=$ENABLED
domcox@1624 31 if [ "$(cat $POWER_SUPPLY/online)" = 1 ] ; then
domcox@1624 32 AC_STATUS=$ON_LINE
domcox@1624 33 echo "on-line"
domcox@1624 34 else
domcox@1624 35 echo "off-line"
domcox@1624 36 fi
domcox@1624 37 fi
domcox@1624 38 fi
domcox@1624 39 done
domcox@1624 40 if [ $POWER_SUPPLY_MAINS -eq $DISABLED ] ; then
domcox@1624 41 $AC_STATUS=$ON_LINE
domcox@1624 42 fi
domcox@1624 43 }
domcox@1624 44
domcox@1624 45 # Determining the battery state.
domcox@1624 46
domcox@1624 47 battery_status()
domcox@1624 48 {
domcox@1624 49 BATTERY_STATUS=$LOW_BAT
domcox@1624 50 for BATT in /sys/class/power_supply/* ; do
domcox@1624 51 BATT_TYPE=$(cat $BATT/type)
domcox@1624 52 echo "$BATT is of type $BATT_TYPE."
domcox@1624 53 if [ "$BATT_TYPE" = "Battery" ] ; then
domcox@1624 54 echo " Checking levels for $BATT."
domcox@1624 55 # Only do if the battery is present
domcox@1624 56 if [ $(cat $BATT/present) -eq 1 ] ; then
domcox@1624 57
domcox@1624 58 # Get the remaining capacity.
domcox@1624 59 if [ -f $BATT/charge_now ] ; then
domcox@1624 60 REMAINING=$(cat $BATT/charge_now)
domcox@1624 61 elif [ -f $BATT/energy_now ] ; then
domcox@1624 62 REMAINING=$(cat $BATT/energy_now)
domcox@1624 63 else
domcox@1624 64 REMAINING=0
domcox@1624 65 fi
domcox@1624 66 if [ -z "$REMAINING" -o "$REMAINING" -eq 0 ] ; then
domcox@1624 67 echo " Battery does not report remaining charge. Perhaps it is not present?"
domcox@1624 68 else
domcox@1624 69 echo " Remaining charge: $REMAINING"
domcox@1624 70
domcox@1624 71 # Get the alarm level
domcox@1624 72 ALARM_LEVEL=$(cat $BATT/alarm)
domcox@1624 73 if [ "$ALARM_LEVEL" -eq 0 ] ; then
domcox@1624 74
domcox@1624 75 # Get the full capacity.
domcox@1624 76
domcox@1624 77 if [ -f $BATT/charge_full_design ] ; then
domcox@1624 78 CAPACITY=$(cat $BATT/charge_full_design)
domcox@1624 79 elif [ -f $BATT/energy_full_design ] ; then
domcox@1624 80 CAPACITY=$(cat $BATT/energy_full_design)
domcox@1624 81 else
domcox@1624 82 CAPACITY=0
domcox@1624 83 fi
domcox@1624 84 if [ -z "$CAPACITY" -o "$CAPACITY" -eq 0 ] ; then
domcox@1624 85 echo " Battery does not report design full charge, using non-design full charge."
domcox@1624 86
domcox@1624 87 if [ -f $BATT/charge_full ] ; then
domcox@1624 88 CAPACITY=$(cat $BATT/charge_full)
domcox@1624 89 elif [ -f $BATT/energy_full_design ] ; then
domcox@1624 90 CAPACITY=$(cat $BATT/energy_full)
domcox@1624 91 else
domcox@1624 92 CAPACITY=0
domcox@1624 93 fi
domcox@1624 94 if [ -z "$CAPACITY" -o "$CAPACITY" -eq 0] ; then
domcox@1624 95 echo " Battery does not report non-design full charge."
domcox@1624 96 fi
domcox@1624 97 fi
domcox@1624 98 echo " Full capacity: $CAPACITY"
domcox@1624 99 ALARM_LEVEL=$((CAPACITY*5/100))
domcox@1624 100 fi
domcox@1624 101 echo " Alarm level: $ALARM_LEVEL"
domcox@1624 102 if [ "$ALARM_LEVEL" -ne 0 ] ; then
domcox@1624 103 if [ "$REMAINING" -ge "$ALARM_LEVEL" ] ; then
domcox@1624 104 # this battery does count as having enough charge.
domcox@1624 105 BATTERY_STATUS=$HIGH_BAT
domcox@1624 106 echo " Battery status: high"
domcox@1624 107 else
domcox@1624 108 echo " Battery status: low"
domcox@1624 109 fi
domcox@1624 110 fi
domcox@1624 111 fi
domcox@1624 112 else
domcox@1624 113 echo "Battery is not present."
domcox@1624 114 fi
domcox@1624 115 fi
domcox@1624 116 done
domcox@1624 117 }
domcox@1624 118
domcox@1624 119 online_mode()
domcox@1624 120 {
domcox@1624 121 # Disable laptop mode
domcox@1624 122 # When laptop mode is enabled, the kernel will try to be smart
domcox@1624 123 # about when to do IO, to give the disk and the SATA links as
domcox@1624 124 # much time as possible in a low power state.
domcox@1624 125
domcox@1624 126 if [ -e /proc/sys/vm/laptop_mode ] ; then
domcox@1624 127 echo "Disabling laptop mode"
domcox@1624 128 echo 0 > /proc/sys/vm/laptop_mode
domcox@1624 129 fi
domcox@1624 130
domcox@1624 131 # AC97 audio power saving mode
domcox@1624 132 # The AC97 onboard audio chips support power saving, where the
domcox@1624 133 # analog parts (codec) are powered down when no program is using
domcox@1624 134 # the audio device.
domcox@1624 135
domcox@1624 136 if [ -e /sys/module/snd_ac97_codec/parameters/power_save ] ; then
domcox@1624 137 echo "Disabling AC97 audio power saving mode"
domcox@1624 138 echo 0 > /sys/module/snd_ac97_codec/parameters/power_save
domcox@1624 139 fi
domcox@1624 140
domcox@1624 141 # The VM writeback time
domcox@1624 142 # The VM subsystem caching allows the kernel to group consecutive
domcox@1624 143 # writes into one big write, and to generally optimize the disk IO
domcox@1624 144 # to be the most efficient.
domcox@1624 145
domcox@1624 146 if [ -e /proc/sys/vm/dirty_writeback_centisecs ] ; then
domcox@1624 147 echo "Writeback time reset to 500ms"
domcox@1624 148 echo 500 > /proc/sys/vm/dirty_writeback_centisecs
domcox@1624 149 fi
domcox@1624 150 }
domcox@1624 151
domcox@1624 152 offline_mode()
domcox@1624 153 {
domcox@1624 154 # Enable laptop mode
domcox@1624 155 # When laptop mode is enabled, the kernel will try to be smart
domcox@1624 156 # about when to do IO, to give the disk and the SATA links as
domcox@1624 157 # much time as possible in a low power state.
domcox@1624 158
domcox@1624 159 if [ ! -e /proc/sys/vm/laptop_mode ] ; then
domcox@1624 160 echo "Kernel does not have support for laptop mode."
domcox@1624 161 else
domcox@1624 162 echo "Enabling laptop mode"
domcox@1624 163 echo 5 > /proc/sys/vm/laptop_mode
domcox@1624 164 fi
domcox@1624 165
domcox@1624 166 # AC97 audio power saving mode
domcox@1624 167 # The AC97 onboard audio chips support power saving, where the
domcox@1624 168 # analog parts (codec) are powered down when no program is using
domcox@1624 169 # the audio device.
domcox@1624 170
domcox@1624 171 if [ -e /sys/module/snd_ac97_codec/parameters/power_save ] ; then
domcox@1624 172 echo "Enabling AC97 audio power saving mode"
domcox@1624 173 echo 1 > /sys/module/snd_ac97_codec/parameters/power_save
domcox@1624 174 echo 1 > /dev/dsp
domcox@1624 175 fi
domcox@1624 176
domcox@1624 177 # The VM writeback time
domcox@1624 178 # The VM subsystem caching allows the kernel to group consecutive
domcox@1624 179 # writes into one big write, and to generally optimize the disk IO
domcox@1624 180 # to be the most efficient.
domcox@1624 181
domcox@1624 182 if [ -e /proc/sys/vm/dirty_writeback_centisecs ] ; then
domcox@1624 183 echo "Writeback time set to 1500ms"
domcox@1624 184 echo 1500 > /proc/sys/vm/dirty_writeback_centisecs
domcox@1624 185 fi
domcox@1624 186 }
domcox@1624 187
domcox@1624 188 power_status()
domcox@1624 189 {
domcox@1624 190 if [ $(cat /proc/sys/vm/dirty_writeback_centisecs) -gt 1000 ]; then
domcox@1624 191 POWER_SAVINGS=$ENABLED
domcox@1624 192 echo "power-savings-mode enabled"
domcox@1624 193 else
domcox@1624 194 POWER_SAVINGS=$DISABLED
domcox@1624 195 echo "power-savings-mode disabled"
domcox@1624 196 fi
domcox@1624 197 }
domcox@1624 198
domcox@1624 199 custom_scripts()
domcox@1624 200 {
domcox@1624 201 # Custom scripts in /etc/acpi/ac.d
domcox@1624 202
domcox@1624 203 if [ -d /etc/acpi/ac.d ]; then
domcox@1624 204 for SCRIPT in /etc/acpi/ac.d/*.sh; do
domcox@1624 205 . $SCRIPT $AC_STATUS $BATTERY_STATUS $0
domcox@1624 206 done
domcox@1624 207 fi
domcox@1624 208
domcox@1624 209 # Custom scripts in /etc/acpi/battery.d
domcox@1624 210
domcox@1624 211 if [ -d /etc/acpi/battery.d ]; then
domcox@1624 212 for SCRIPT in /etc/acpi/battery.d/*.sh; do
domcox@1624 213 . $SCRIPT $AC_STATUS $BATTERY_STATUS $0
domcox@1624 214 done
domcox@1624 215 fi
domcox@1624 216 }
domcox@1624 217
domcox@1624 218 ac_status
domcox@1624 219 battery_status
domcox@1624 220 power_status
domcox@1624 221 case "$AC_STATUS+$BATTERY_STATUS" in
domcox@1624 222 "$OFF_LINE+$HIGH_BAT")
domcox@1624 223 if [ $POWER_SAVINGS = $DISABLED ]; then
domcox@1624 224 logger "Start power savings mode"
domcox@1624 225 offline_mode
domcox@1624 226 fi
domcox@1624 227 ;;
domcox@1624 228 *)
domcox@1624 229 if [ $POWER_SAVINGS = $ENABLED ]; then
domcox@1624 230 logger "Stop power savings mode"
domcox@1624 231 online_mode
domcox@1624 232 fi
domcox@1624 233 ;;
domcox@1624 234 esac
domcox@1624 235 custom_scripts