wok view acpid/stuff/acpi/power-supply.sh @ rev 4781

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