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
11 led_high="$(cat /etc/pandora/conf/led.conf | grep HighPowerLED: | awk -F\: '{print $2}')"
12 led_low="$(cat /etc/pandora/conf/led.conf | grep LowPowerLED: | awk -F\: '{print $2}')"
13 led_idle="$(cat /etc/pandora/conf/led.conf | grep IdleLED: | awk -F\: '{print $2}')"
14 test -z "$led_high" && led_high=255
15 test -z "$led_low" && led_low=16
16 test -z "$led_idle" && led_idle=48
19 return 1 # 0 when debugging, 1 when not
22 test -e $xfceuser_home/.lidconfig && lidconfig=$(cat $xfceuser_home/.lidconfig) # read lid conf. file if it exists
24 if [ -e /tmp/powerstate ]; then
25 powerstate="$(cat /tmp/powerstate)"
30 debug && echo "powerstate=$powerstate"
33 # This is kernel 3.x only,
34 # but ignored since 2.6 only has lowpower anyways (no suspend)
35 if (ps -A | grep -q hciattac[h]); then
36 touch /tmp/bt-state-up
37 /usr/pandora/scripts/op_bluetooth_work.sh 0
39 rm -f /tmp/bt-state-up
42 wlstate=$(lsmod | grep -m1 wl1251)
43 if [ -z "$wlstate" ]; then
44 echo "down" > /tmp/wlstate
46 echo "up" > /tmp/wlstate
48 rmmod board_omap3pandora_wifi 2> /dev/null
49 rmmod wl1251_sdio wl1251
54 if [ -f /tmp/bt-state-up ]; then
55 /usr/pandora/scripts/op_bluetooth_work.sh 1
56 hciconfig hci0 up pscan
58 wlstate=$(cat /tmp/wlstate)
59 if [ "$wlstate" = "up" ]; then
60 /etc/init.d/wl1251-init start
62 rm -f /tmp/hcistate /tmp/wlstate
66 echo 0 > /sys/class/graphics/fb0/blank
68 # only bother restoring brightness if it's 0
69 # (old kernel or user messed it up somehow)
70 brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
71 if [ $brightness -gt 0 ]; then
75 maxbright=$(cat $SYSFS_BACKLIGHT/max_brightness)
77 if [ -f /tmp/oldbright ]; then
78 oldbright=$(cat /tmp/oldbright)
80 if [ $oldbright -eq 0 ]; then
81 oldbright=$(cat /etc/pandora/conf/brightness.state)
83 if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then
84 /usr/pandora/scripts/op_bright.sh $oldbright
86 /usr/pandora/scripts/op_bright.sh $maxbright
91 brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
92 if [ $brightness -gt 0 ]; then
93 echo $brightness > /tmp/oldbright
95 kernel_major=`uname -r | cut -c 1`
96 if [ "$kernel_major" = "2" ]; then
97 echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
100 echo 1 > /sys/class/graphics/fb0/blank
103 lowPowerOn(){ #switch from normal to lowpower mode
106 pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
114 cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
115 /usr/pandora/scripts/op_cpuspeed.sh -n 125
118 lowPowerOff(){ # switch from lowpower to normal mode
119 oldspeed=$(cat /tmp/oldspeed)
120 /usr/pandora/scripts/op_cpuspeed.sh -n $oldspeed
126 pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
131 echo $led_high > /sys/class/leds/pandora\:\:power/brightness #power LED bright
134 display_on_with_checks() {
135 # after turning on the display, we don't want lowpower state
136 # (which could be active because of some races)
137 if [ "$powerstate" = "buttonlowpower" -o "$powerstate" = "lidlowpower" -o \
143 echo $led_high > /sys/class/leds/pandora\:\:power/brightness
148 # TODO: check if desktop is visible; maybe use layer3?
149 cmd="DISPLAY=:0.0 zenity --info --text \"$1\" --timeout 10"
150 su -c "$cmd" $xfceuser
156 if ! [ -e /sys/power/state ]; then
157 # no kernel suspend support
161 current_now="$(cat /sys/class/power_supply/bq27500-0/current_now)"
163 if [ $current_now -gt 0 ]; then
165 #don't suspend while unit is charging
168 # can't suspend while SGX is in use due to bugs
169 # (prevents low power states and potential lockup)
170 if lsof -t /dev/pvrsrvkm > /dev/null; then
174 if ! grep -q 'mmc_core.removable=0' /proc/cmdline; then
175 # must unmount cards because they will be "ejected" on suspend
176 # (some filesystems may even deadlock if we don't do this due to bugs)
177 mounts="$(grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs echo)"
178 for mnt in $mounts; do
179 if ! umount $mnt; then
180 show_message "Could not unmount $mnt, using partial suspend only"
184 swaps="$(grep "/dev/mmcblk" /proc/swaps | awk '{print $1}' | xargs echo)"
185 for swp in $swaps; do
186 if ! swapoff $swp; then
187 show_message "Could not unmount $swp, using partial suspend only"
192 if [ ! -e /etc/pandora/suspend-warned ]; then
193 show_message "Pandora will now suspend.\n\n\
194 Please do not remove SD cards while pandora is suspended, doing so will corrupt them."
195 touch /etc/pandora/suspend-warned
199 # FIXME: fix the kernel and get rid of this
202 # get rid of modules that prevent suspend due to bugs
203 modules="$(lsmod | awk '{print $1}' | xargs echo)"
204 blacklist="g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
205 g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms
206 ehci_hcd bridgedriver"
208 for mod in $modules; do
209 if echo $blacklist | grep -q "\<$mod\>"; then
210 restore_list="$restore_list $mod"
218 echo mem > /sys/power/state
220 # if we are here, either we already resumed or the suspend failed
221 if [ -n "$restore_list" ]; then
222 for module in $restore_list; do
227 # it has been reported that time sometimes drifts during suspend
228 # so we reload hwclock here (WizardStan)
233 echo $led_high > /sys/class/leds/pandora\:\:power/brightness
235 # wait here a bit to prevent this script from running again (keep op_power.lock)
236 # in case user did resume using the power switch.
244 echo $led_low > /sys/class/leds/pandora\:\:power/brightness
246 if suspend_real; then
255 if [ "$powerstate" = "on" ]; then
257 echo "resume called unexpectedly" >&2
263 shutdown(){ # warns the user and shuts the pandora down
266 for i in $(seq $time); do
267 precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
269 echo "# Shutdown in $(($time-$i))"
273 countdown | su -c 'DISPLAY=:0.0 zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
274 if [ $? -eq 0 ]; then
275 /sbin/shutdown -h now
277 su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
281 if [[ "$2" == "" ]]; then
282 if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
283 if [[ "$powerstate" == "buttonlowpower" ]]; then
284 (debug && echo "resume") || resume
286 elif [[ "$powerstate" == "on" ]]; then
287 powerstate="buttonlowpower"
288 (debug && echo "suspend") || suspend_
289 elif [[ "$powerstate" == "liddisplayoff" ]]; then
290 powerstate="buttonlowpower"
291 (debug && echo "suspend") || suspend_
293 elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
294 if [[ "$powerstate" == "on" ]]; then
295 (debug && echo "shutdown") || shutdown
298 elif [[ "$2" == "lid" ]]; then
299 if [[ "$1" == 0 ]]; then # lid was opened
300 if [[ "$powerstate" == lid* ]]; then
303 (debug && echo "resume") || resume
307 (debug && echo "display_on") || display_on_with_checks
312 elif [[ "$1" == 1 ]]; then # lid was closed
313 if [[ "$powerstate" == "on" ]]; then
316 (debug && echo "shutdown") || shutdown
319 powerstate="lidlowpower"
320 (debug && echo "suspend") || suspend_
323 (debug && echo "display_off") || display_off
324 echo $led_idle > /sys/class/leds/pandora\:\:power/brightness
325 powerstate="liddisplayoff"
330 elif [[ "$2" == "screensaver" ]]; then
331 # warning: don't try to interact with X or do real suspend here -
332 # will cause various deadlocks
335 if [[ "$1" == 0 ]]; then # deactivate screensaver
336 display_on_with_checks
338 elif [[ "$1" == 1 ]]; then # activate screensaver
340 echo $led_idle > /sys/class/leds/pandora\:\:power/brightness
344 debug && echo "powerstate=$powerstate"
345 echo "$powerstate" > /tmp/powerstate
347 rm -f /tmp/op_power.lock