blob: b0a0554cd7dc244a6020d98d45364fac1007bea0 [file] [log] [blame] [edit]
#!/bin/sh
# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
log_msg() {
logger -t "powerd_suspend[${PPID}]" $1
}
# these files are created by powerm | powerd and there existence means
# event has occurred which should cancel suspend
suspend_cancel() {
if [ $cancel -eq 1 ] || [ -e "/var/run/power_manager/lid_opened" ] ||
[ -e "/var/run/power_manager/user_active" ]; then
cancel=1
return 0
else
return 1
fi
}
cleanup_for_resume() {
if [ $force_usb_autosuspend -eq 1 ]; then
# disable autosuspend for USB devices if we set it before suspend.
for i in /sys/bus/usb/devices/*/power/level; do echo on > $i; done
fi
rm -f "/var/run/power_manager/lid_opened"
rm -f "/var/run/power_manager/user_active"
}
parse_args() {
cancel=0
if [ "$1" != "" ] ; then
cancel=$((1 && $1))
fi
}
log_msg "Going to suspend-to-RAM state : args=$@"
parse_args $@
# - logs the time going to suspend (no-op if no RTC).
cp /sys/class/rtc/rtc0/since_epoch /var/log/metrics/suspend-to-ram-time \
2> /dev/null || true
# - announces the event
/usr/bin/dbus-send --type=signal --system / \
org.chromium.PowerManager.PowerStateChanged string:mem
# - stores the current power status
power_status_on_suspend=/tmp/power-status-on-suspend
force_usb_autosuspend=0
/usr/bin/devkit-power -d \
| /bin/grep -Eq '^[[:space:]]+online:[[:space:]]+no$'
if [ $? -eq 0 ]; then
echo OnBattery > $power_status_on_suspend
else
echo OnAC > $power_status_on_suspend
if suspend_cancel; then
log_msg "Cancel suspend at USB autosuspend"
else
# - enables autosuspend for USB devices. This shaves about 300ms
# from resume time.
force_usb_autosuspend=1
for i in /sys/bus/usb/devices/*/power/level; do echo auto > $i; done
fi
fi
# - deletes hwclock snapshot from previous resumes
rm -f /tmp/hwclock-on-resume
# - suspends the cryptohome device
#CRYPTOHOME=/dev/mapper/cryptohome
#/usr/bin/test -b $CRYPTOHOME && /sbin/dmsetup suspend $CRYPTOHOME
log_msg "Explicit sync"
sync
if suspend_cancel; then
log_msg "Cancel suspend at kernel"
else
log_msg "Finalizing suspend"
# - suspends to ram
echo -n mem > /sys/power/state
fi
if [ $cancel -eq 0 ]; then
# On resume:
# - records the hwclock time at resume
/sbin/hwclock --utc --debug > /tmp/hwclock-on-resume &
# - sends UMA metrics on resume
/usr/bin/send_metrics_on_resume &
fi
# - announces the event
/usr/bin/dbus-send --type=signal --system / \
org.chromium.PowerManager.PowerStateChanged string:on &
cleanup_for_resume
# - re-kicks laptop mode since AC<->Battery transition might've happened
# while the system was suspended. Sleep a bit, though, to take care of
# racing with USB stack coming up.
(sleep 10 && /usr/sbin/laptop_mode force) > /dev/null &
# - resumes cryptohome device
#/usr/bin/test -b $CRYPTOHOME && /sbin/dmsetup resume $CRYPTOHOME
log_msg "Resume finished"