3 . /usr/pandora/scripts/op_common.sh
5 xfceuser_home=$(get_user_home)
7 # XXX: better use lockfile (or something), but it's not in current firmware
8 test -e /tmp/op_power.lock && exit 2
9 touch /tmp/op_power.lock
10 highpow="$(cat /etc/pandora/conf/led.conf | grep HighPowerLED: | awk -F\: '{print $2}')"
11 lowpow="$(cat /etc/pandora/conf/led.conf | grep LowPowerLED: | awk -F\: '{print $2}')"
14 return 1 # 0 when debugging, 1 when not
17 test -e $xfceuser_home/.lidconfig && lidconfig=$(cat $xfceuser_home/.lidconfig) # read lid conf. file if it exists
19 if [ -e /tmp/powerstate ]; then
20 powerstate="$(cat /tmp/powerstate)"
25 debug && echo "powerstate=$powerstate"
28 hcistate=$(hciconfig hci0 | grep DOWN)
29 if [ $hcistate ]; then
30 echo "down" > /tmp/hcistate
32 echo "up" > /tmp/hcistate
35 wlstate=$(lsmod | grep -m1 wl1251)
36 if [ -z "$wlstate" ]; then
37 echo "down" > /tmp/wlstate
39 echo "up" > /tmp/wlstate
41 rmmod board_omap3pandora_wifi 2> /dev/null
42 rmmod wl1251_sdio wl1251
47 hcistate=$(cat /tmp/hcistate)
48 if [ "$hcistate" = "up" ]; then
49 hciconfig hci0 up pscan
51 wlstate=$(cat /tmp/wlstate)
52 if [ "$wlstate" = "up" ]; then
53 /etc/init.d/wl1251-init start
55 rm -f /tmp/hcistate /tmp/wlstate
59 echo 0 > /sys/class/graphics/fb0/blank
61 # only bother restoring brightness if it's 0
62 # (old kernel or user messed it up somehow)
63 brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
64 if [ $brightness -gt 0 ]; then
68 maxbright=$(cat $SYSFS_BACKLIGHT/max_brightness)
70 if [ -f /tmp/oldbright ]; then
71 oldbright=$(cat /tmp/oldbright)
73 if [ $oldbright -eq 0 ]; then
74 oldbright=$(cat /etc/pandora/conf/brightness.state)
76 if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then
77 /usr/pandora/scripts/op_bright.sh $oldbright
79 /usr/pandora/scripts/op_bright.sh $maxbright
84 brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
85 if [ $brightness -gt 0 ]; then
86 echo $brightness > /tmp/oldbright
88 kernel_major=`uname -r | cut -c 1`
89 if [ "$kernel_major" = "2" ]; then
90 echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
93 echo 1 > /sys/class/graphics/fb0/blank
96 lowPowerOn(){ #switch from normal to lowpower mode
99 pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
107 cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
108 /usr/pandora/scripts/op_cpuspeed.sh -n 125
111 lowPowerOff(){ # switch from lowpower to normal mode
112 oldspeed=$(cat /tmp/oldspeed)
113 /usr/pandora/scripts/op_cpuspeed.sh -n $oldspeed
119 pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
124 echo $highpow > /sys/class/leds/pandora\:\:power/brightness #power LED bright
127 display_on_with_checks() {
128 # after turning on the display, we don't want lowpower state
129 # (which could be active because of some races)
130 if [ "$powerstate" = "buttonlowpower" -o "$powerstate" = "lidlowpower" -o \
140 # TODO: check if desktop is visible; maybe use layer3?
141 cmd="DISPLAY=:0.0 zenity --info --text \"$1\" --timeout 10"
142 su -c "$cmd" $xfceuser
148 if ! [ -e /sys/power/state ]; then
149 # no kernel suspend support
153 current_now="$(cat /sys/class/power_supply/bq27500-0/current_now)"
155 if [ $current_now -gt 0 ]; then
157 #don't suspend while unit is charging
160 # can't suspend while SGX is in use due to bugs
161 # (prevents low power states and potential lockup)
162 if lsof -t /dev/pvrsrvkm > /dev/null; then
166 if ! grep -q 'mmc_core.removable=0' /proc/cmdline; then
167 # must unmount cards because they will be "ejected" on suspend
168 # (some filesystems may even deadlock if we don't do this due to bugs)
169 mounts="$(grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs echo)"
170 for mnt in $mounts; do
171 if ! umount $mnt; then
172 show_message "Could not unmount $mnt, using partial suspend only"
176 swaps="$(grep "/dev/mmcblk" /proc/swaps | awk '{print $1}' | xargs echo)"
177 for swp in $swaps; do
178 if ! swapoff $swp; then
179 show_message "Could not unmount $swp, using partial suspend only"
184 if [ ! -e /etc/pandora/suspend-warned ]; then
185 show_message "Pandora will now suspend.\n\n\
186 Please do not remove SD cards while pandora is suspended, doing so will corrupt them."
187 touch /etc/pandora/suspend-warned
191 # FIXME: fix the kernel and get rid of this
194 # get rid of modules that prevent suspend due to bugs
195 modules="$(lsmod | awk '{print $1}' | xargs echo)"
196 blacklist="g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
197 g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms
198 ehci_hcd bridgedriver"
200 for mod in $modules; do
201 if echo $blacklist | grep -q "\<$mod\>"; then
202 restore_list="$restore_list $mod"
210 echo mem > /sys/power/state
212 # if we are here, either we already resumed or the suspend failed
213 if [ -n "$restore_list" ]; then
214 for module in $restore_list; do
219 # it has been reported that time sometimes drifts during suspend
220 # so we reload hwclock here (WizardStan)
225 echo $highpow > /sys/class/leds/pandora\:\:power/brightness
227 # wait here a bit to prevent this script from running again (keep op_power.lock)
228 # in case user did resume using the power switch.
236 echo $lowpow > /sys/class/leds/pandora\:\:power/brightness
238 if suspend_real; then
247 if [ "$powerstate" = "on" ]; then
249 echo "resume called unexpectedly" >&2
255 shutdown(){ # warns the user and shuts the pandora down
258 for i in $(seq $time); do
259 precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
261 echo "# Shutdown in $(($time-$i))"
265 countdown | su -c 'DISPLAY=:0.0 zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
266 if [ $? -eq 0 ]; then
267 /sbin/shutdown -h now
269 su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
273 if [[ "$2" == "" ]]; then
274 if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
275 if [[ "$powerstate" == "buttonlowpower" ]]; then
276 (debug && echo "resume") || resume
278 elif [[ "$powerstate" == "on" ]]; then
279 powerstate="buttonlowpower"
280 (debug && echo "suspend") || suspend_
281 elif [[ "$powerstate" == "liddisplayoff" ]]; then
282 powerstate="buttonlowpower"
283 (debug && echo "suspend") || suspend_
285 elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
286 if [[ "$powerstate" == "on" ]]; then
287 (debug && echo "shutdown") || shutdown
290 elif [[ "$2" == "lid" ]]; then
291 if [[ "$1" == 0 ]]; then # lid was opened
292 if [[ "$powerstate" == lid* ]]; then
295 (debug && echo "resume") || resume
299 (debug && echo "display_on") || display_on_with_checks
304 elif [[ "$1" == 1 ]]; then # lid was closed
305 if [[ "$powerstate" == "on" ]]; then
308 (debug && echo "shutdown") || shutdown
311 powerstate="lidlowpower"
312 (debug && echo "suspend") || suspend_
315 (debug && echo "display_off") || display_off
316 powerstate="liddisplayoff"
321 elif [[ "$2" == "screensaver" ]]; then
322 # warning: don't try to interact with X or do real suspend here -
323 # will cause various deadlocks
326 if [[ "$1" == 0 ]]; then # deactivate screensaver
327 display_on_with_checks
329 elif [[ "$1" == 1 ]]; then # activate screensaver
334 debug && echo "powerstate=$powerstate"
335 echo "$powerstate" > /tmp/powerstate
337 rm -f /tmp/op_power.lock