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 if [ -x /usr/bin/op_lidstate -a ! -e /etc/pandora/my_lid_is_broken ]; then
34 if [ $(op_lidstate) = "0" ]; then
35 # lid is open - return true
38 # lid is closed - return false
42 # op_lidstate program not found, report lid open
43 # this should cause less trouble
49 # if lid is open switch powerstate to "on"
50 # if lid is closed switch powerstate to "liddisplayoff"
51 # this prevents screen off lock when shoulder butttons
52 # are pressed while the lid is closed
56 powerstate="liddisplayoff"
61 # This is kernel 3.x only,
62 # but ignored since 2.6 only has lowpower anyways (no suspend)
63 if (ps -A | grep -q hciattac[h]); then
64 touch /tmp/bt-state-up
65 /usr/pandora/scripts/op_bluetooth_work.sh 0
67 rm -f /tmp/bt-state-up
70 wlstate=$(lsmod | grep -m1 wl1251)
71 if [ -z "$wlstate" ]; then
72 echo "down" > /tmp/wlstate
74 echo "up" > /tmp/wlstate
76 rmmod board_omap3pandora_wifi 2> /dev/null
77 rmmod wl1251_sdio wl1251
82 if [ -f /tmp/bt-state-up ]; then
83 /usr/pandora/scripts/op_bluetooth_work.sh 1
84 hciconfig hci0 up pscan
86 wlstate=$(cat /tmp/wlstate)
87 if [ "$wlstate" = "up" ]; then
88 /etc/init.d/wl1251-init start
90 rm -f /tmp/hcistate /tmp/wlstate
94 # only turn on display if lid is open
95 if ! is_lid_open; then
96 if [[ "$1" != "force" ]]; then
101 echo 0 > /sys/class/graphics/fb0/blank
103 # only bother restoring brightness if it's 0
104 # (old kernel or user messed it up somehow)
105 brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
106 if [ $brightness -gt 0 ]; then
110 maxbright=$(cat $SYSFS_BACKLIGHT/max_brightness)
112 if [ -f /tmp/oldbright ]; then
113 oldbright=$(cat /tmp/oldbright)
115 if [ $oldbright -eq 0 ]; then
116 oldbright=$(cat /etc/pandora/conf/brightness.state)
118 if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then
119 /usr/pandora/scripts/op_bright.sh $oldbright
121 /usr/pandora/scripts/op_bright.sh $maxbright
126 brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
127 if [ $brightness -gt 0 ]; then
128 echo $brightness > /tmp/oldbright
130 kernel_major=`uname -r | cut -c 1`
131 if [ "$kernel_major" = "2" ]; then
132 echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
135 echo 1 > /sys/class/graphics/fb0/blank
138 lowPowerOn(){ #switch from normal to lowpower mode
141 pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
149 cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
150 /usr/pandora/scripts/op_cpuspeed.sh -n 125
153 lowPowerOff(){ # switch from lowpower to normal mode
154 oldspeed=$(cat /tmp/oldspeed)
155 /usr/pandora/scripts/op_cpuspeed.sh -n $oldspeed
161 pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
166 echo $led_high > /sys/class/leds/pandora\:\:power/brightness #power LED bright
169 display_on_with_checks() {
170 # after turning on the display, we don't want lowpower state
171 # (which could be active because of some races)
172 if [ "$powerstate" = "buttonlowpower" -o "$powerstate" = "lidlowpower" -o \
178 echo $led_high > /sys/class/leds/pandora\:\:power/brightness
183 # TODO: check if desktop is visible; maybe use layer3?
184 cmd="DISPLAY=:0.0 zenity --info --text \"$1\" --timeout 10"
185 su -c "$cmd" $xfceuser
191 if ! [ -e /sys/power/state ]; then
192 # no kernel suspend support
196 current_now="$(cat /sys/class/power_supply/bq27500-0/current_now)"
198 if [ $current_now -gt 0 ]; then
200 #don't suspend while unit is charging
203 # can't suspend while SGX is in use due to bugs
204 # (prevents low power states and potential lockup)
205 if lsof -t /dev/pvrsrvkm > /dev/null; then
209 if ! grep -q 'mmc_core.removable=0' /proc/cmdline; then
210 # must unmount cards because they will be "ejected" on suspend
211 # (some filesystems may even deadlock if we don't do this due to bugs)
212 mounts="$(grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs echo)"
213 for mnt in $mounts; do
214 if ! umount $mnt; then
215 show_message "Could not unmount $mnt, using partial suspend only"
219 swaps="$(grep "/dev/mmcblk" /proc/swaps | awk '{print $1}' | xargs echo)"
220 for swp in $swaps; do
221 if ! swapoff $swp; then
222 show_message "Could not unmount $swp, using partial suspend only"
227 if [ ! -e /etc/pandora/suspend-warned ]; then
228 show_message "Pandora will now suspend.\n\n\
229 Please do not remove SD cards while pandora is suspended, doing so will corrupt them."
230 touch /etc/pandora/suspend-warned
234 # FIXME: fix the kernel and get rid of this
237 # get rid of modules that prevent suspend due to bugs
238 modules="$(lsmod | awk '{print $1}' | xargs echo)"
239 blacklist="g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
240 g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms
241 ehci_hcd bridgedriver"
243 for mod in $modules; do
244 if echo $blacklist | grep -q "\<$mod\>"; then
245 restore_list="$restore_list $mod"
253 echo mem > /sys/power/state
255 # if we are here, either we already resumed or the suspend failed
256 if [ -n "$restore_list" ]; then
257 for module in $restore_list; do
262 # it has been reported that time sometimes drifts during suspend
263 # so we reload hwclock here (WizardStan)
268 echo $led_high > /sys/class/leds/pandora\:\:power/brightness
270 # wait here a bit to prevent this script from running again (keep op_power.lock)
271 # in case user did resume using the power switch.
279 echo $led_low > /sys/class/leds/pandora\:\:power/brightness
281 if suspend_real; then
289 resume() { # only called on 2.6 kernels or when charging
290 if [ "$powerstate" = "on" ]; then
292 echo "resume called unexpectedly" >&2
298 shutdown(){ # warns the user and shuts the pandora down
301 for i in $(seq $time); do
302 precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
304 echo "# Shutdown in $(($time-$i))"
308 countdown | su -c 'DISPLAY=:0.0 zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
309 if [ $? -eq 0 ]; then
310 /sbin/shutdown -h now
312 su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
316 if [[ "$2" == "" ]]; then
317 if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
318 if [[ "$powerstate" == "buttonlowpower" ]]; then
319 (debug && echo "resume") || resume
321 elif [[ "$powerstate" == "on" ]]; then
322 powerstate="buttonlowpower"
323 (debug && echo "suspend") || suspend_
324 elif [[ "$powerstate" == "liddisplayoff" ]]; then
325 powerstate="buttonlowpower"
326 (debug && echo "suspend") || suspend_
328 elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
329 if [[ "$powerstate" == "on" ]]; then
330 (debug && echo "shutdown") || shutdown
333 elif [[ "$2" == "lid" ]]; then
334 if [[ "$1" == 0 ]]; then # lid was opened
335 if [[ "$powerstate" == lid* ]]; then
338 (debug && echo "resume") || resume
342 (debug && echo "display_on") || display_on_with_checks
347 elif [[ "$1" == 1 ]]; then # lid was closed
348 if [[ "$powerstate" == "on" ]]; then
351 (debug && echo "shutdown") || shutdown
354 powerstate="lidlowpower"
355 (debug && echo "suspend") || suspend_
358 (debug && echo "display_off") || display_off
359 echo $led_idle > /sys/class/leds/pandora\:\:power/brightness
360 powerstate="liddisplayoff"
365 elif [[ "$2" == "screensaver" ]]; then
366 # warning: don't try to interact with X or do real suspend here -
367 # will cause various deadlocks
370 if [[ "$1" == 0 ]]; then # deactivate screensaver
371 display_on_with_checks
373 elif [[ "$1" == 1 ]]; then # activate screensaver
375 echo $led_idle > /sys/class/leds/pandora\:\:power/brightness
379 debug && echo "powerstate=$powerstate"
380 echo "$powerstate" > /tmp/powerstate
382 rm -f /tmp/op_power.lock