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 # This is kernel 3.x only,
29 # but ignored since 2.6 only has lowpower anyways (no suspend)
30 if (ps -A | grep -q hciattac[h]); then
31 touch /tmp/bt-state-up
32 /usr/pandora/scripts/op_bluetooth_work.sh 0
34 rm -f /tmp/bt-state-up
37 wlstate=$(lsmod | grep -m1 wl1251)
38 if [ -z "$wlstate" ]; then
39 echo "down" > /tmp/wlstate
41 echo "up" > /tmp/wlstate
43 rmmod board_omap3pandora_wifi 2> /dev/null
44 rmmod wl1251_sdio wl1251
49 if [ -f /tmp/bt-state-up ]; then
50 /usr/pandora/scripts/op_bluetooth_work.sh 1
51 hciconfig hci0 up pscan
53 wlstate=$(cat /tmp/wlstate)
54 if [ "$wlstate" = "up" ]; then
55 /etc/init.d/wl1251-init start
57 rm -f /tmp/hcistate /tmp/wlstate
61 echo 0 > /sys/class/graphics/fb0/blank
63 # only bother restoring brightness if it's 0
64 # (old kernel or user messed it up somehow)
65 brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
66 if [ $brightness -gt 0 ]; then
70 maxbright=$(cat $SYSFS_BACKLIGHT/max_brightness)
72 if [ -f /tmp/oldbright ]; then
73 oldbright=$(cat /tmp/oldbright)
75 if [ $oldbright -eq 0 ]; then
76 oldbright=$(cat /etc/pandora/conf/brightness.state)
78 if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then
79 /usr/pandora/scripts/op_bright.sh $oldbright
81 /usr/pandora/scripts/op_bright.sh $maxbright
86 brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
87 if [ $brightness -gt 0 ]; then
88 echo $brightness > /tmp/oldbright
90 kernel_major=`uname -r | cut -c 1`
91 if [ "$kernel_major" = "2" ]; then
92 echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
95 echo 1 > /sys/class/graphics/fb0/blank
98 lowPowerOn(){ #switch from normal to lowpower mode
101 pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
109 cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
110 /usr/pandora/scripts/op_cpuspeed.sh -n 125
113 lowPowerOff(){ # switch from lowpower to normal mode
114 oldspeed=$(cat /tmp/oldspeed)
115 /usr/pandora/scripts/op_cpuspeed.sh -n $oldspeed
121 pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
126 echo $highpow > /sys/class/leds/pandora\:\:power/brightness #power LED bright
129 display_on_with_checks() {
130 # after turning on the display, we don't want lowpower state
131 # (which could be active because of some races)
132 if [ "$powerstate" = "buttonlowpower" -o "$powerstate" = "lidlowpower" -o \
142 # TODO: check if desktop is visible; maybe use layer3?
143 cmd="DISPLAY=:0.0 zenity --info --text \"$1\" --timeout 10"
144 su -c "$cmd" $xfceuser
150 if ! [ -e /sys/power/state ]; then
151 # no kernel suspend support
155 current_now="$(cat /sys/class/power_supply/bq27500-0/current_now)"
157 if [ $current_now -gt 0 ]; then
159 #don't suspend while unit is charging
162 # can't suspend while SGX is in use due to bugs
163 # (prevents low power states and potential lockup)
164 if lsof -t /dev/pvrsrvkm > /dev/null; then
168 if ! grep -q 'mmc_core.removable=0' /proc/cmdline; then
169 # must unmount cards because they will be "ejected" on suspend
170 # (some filesystems may even deadlock if we don't do this due to bugs)
171 mounts="$(grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs echo)"
172 for mnt in $mounts; do
173 if ! umount $mnt; then
174 show_message "Could not unmount $mnt, using partial suspend only"
178 swaps="$(grep "/dev/mmcblk" /proc/swaps | awk '{print $1}' | xargs echo)"
179 for swp in $swaps; do
180 if ! swapoff $swp; then
181 show_message "Could not unmount $swp, using partial suspend only"
186 if [ ! -e /etc/pandora/suspend-warned ]; then
187 show_message "Pandora will now suspend.\n\n\
188 Please do not remove SD cards while pandora is suspended, doing so will corrupt them."
189 touch /etc/pandora/suspend-warned
193 # FIXME: fix the kernel and get rid of this
196 # get rid of modules that prevent suspend due to bugs
197 modules="$(lsmod | awk '{print $1}' | xargs echo)"
198 blacklist="g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
199 g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms
200 ehci_hcd bridgedriver"
202 for mod in $modules; do
203 if echo $blacklist | grep -q "\<$mod\>"; then
204 restore_list="$restore_list $mod"
212 echo mem > /sys/power/state
214 # if we are here, either we already resumed or the suspend failed
215 if [ -n "$restore_list" ]; then
216 for module in $restore_list; do
221 # it has been reported that time sometimes drifts during suspend
222 # so we reload hwclock here (WizardStan)
227 echo $highpow > /sys/class/leds/pandora\:\:power/brightness
229 # wait here a bit to prevent this script from running again (keep op_power.lock)
230 # in case user did resume using the power switch.
238 echo $lowpow > /sys/class/leds/pandora\:\:power/brightness
240 if suspend_real; then
249 if [ "$powerstate" = "on" ]; then
251 echo "resume called unexpectedly" >&2
257 shutdown(){ # warns the user and shuts the pandora down
260 for i in $(seq $time); do
261 precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
263 echo "# Shutdown in $(($time-$i))"
267 countdown | su -c 'DISPLAY=:0.0 zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
268 if [ $? -eq 0 ]; then
269 /sbin/shutdown -h now
271 su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
275 if [[ "$2" == "" ]]; then
276 if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
277 if [[ "$powerstate" == "buttonlowpower" ]]; then
278 (debug && echo "resume") || resume
280 elif [[ "$powerstate" == "on" ]]; then
281 powerstate="buttonlowpower"
282 (debug && echo "suspend") || suspend_
283 elif [[ "$powerstate" == "liddisplayoff" ]]; then
284 powerstate="buttonlowpower"
285 (debug && echo "suspend") || suspend_
287 elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
288 if [[ "$powerstate" == "on" ]]; then
289 (debug && echo "shutdown") || shutdown
292 elif [[ "$2" == "lid" ]]; then
293 if [[ "$1" == 0 ]]; then # lid was opened
294 if [[ "$powerstate" == lid* ]]; then
297 (debug && echo "resume") || resume
301 (debug && echo "display_on") || display_on_with_checks
306 elif [[ "$1" == 1 ]]; then # lid was closed
307 if [[ "$powerstate" == "on" ]]; then
310 (debug && echo "shutdown") || shutdown
313 powerstate="lidlowpower"
314 (debug && echo "suspend") || suspend_
317 (debug && echo "display_off") || display_off
318 powerstate="liddisplayoff"
323 elif [[ "$2" == "screensaver" ]]; then
324 # warning: don't try to interact with X or do real suspend here -
325 # will cause various deadlocks
328 if [[ "$1" == 0 ]]; then # deactivate screensaver
329 display_on_with_checks
331 elif [[ "$1" == 1 ]]; then # activate screensaver
336 debug && echo "powerstate=$powerstate"
337 echo "$powerstate" > /tmp/powerstate
339 rm -f /tmp/op_power.lock