HACK: PM: Port early suspend functionality
Prashant Gaikwad [Mon, 30 Apr 2012 07:23:07 +0000 (12:23 +0530)]
Bug 959487

Change-Id: I899a4780ca3cd537caca819a9d850377302fd74e
Signed-off-by: Prashant Gaikwad <pgaikwad@nvidia.com>
Reviewed-on: http://git-master/r/99587
Tested-by: Varun Wadekar <vwadekar@nvidia.com>
Reviewed-by: Mayuresh Kulkarni <mkulkarni@nvidia.com>
Reviewed-by: Varun Wadekar <vwadekar@nvidia.com>

arch/arm/mach-tegra/pm.c
include/linux/earlysuspend.h [new file with mode: 0644]
kernel/power/Kconfig
kernel/power/Makefile
kernel/power/consoleearlysuspend.c [new file with mode: 0644]
kernel/power/earlysuspend.c [new file with mode: 0644]
kernel/power/fbearlysuspend.c
kernel/power/main.c
kernel/power/power.h
kernel/power/suspend.c

index b0be197..13f3c38 100644 (file)
@@ -32,6 +32,7 @@
 #include <linux/err.h>
 #include <linux/debugfs.h>
 #include <linux/delay.h>
+#include <linux/earlysuspend.h>
 #include <linux/suspend.h>
 #include <linux/slab.h>
 #include <linux/serial_reg.h>
diff --git a/include/linux/earlysuspend.h b/include/linux/earlysuspend.h
new file mode 100644 (file)
index 0000000..8343b81
--- /dev/null
@@ -0,0 +1,56 @@
+/* include/linux/earlysuspend.h
+ *
+ * Copyright (C) 2007-2008 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#ifndef _LINUX_EARLYSUSPEND_H
+#define _LINUX_EARLYSUSPEND_H
+
+#ifdef CONFIG_HAS_EARLYSUSPEND
+#include <linux/list.h>
+#endif
+
+/* The early_suspend structure defines suspend and resume hooks to be called
+ * when the user visible sleep state of the system changes, and a level to
+ * control the order. They can be used to turn off the screen and input
+ * devices that are not used for wakeup.
+ * Suspend handlers are called in low to high level order, resume handlers are
+ * called in the opposite order. If, when calling register_early_suspend,
+ * the suspend handlers have already been called without a matching call to the
+ * resume handlers, the suspend handler will be called directly from
+ * register_early_suspend. This direct call can violate the normal level order.
+ */
+enum {
+       EARLY_SUSPEND_LEVEL_BLANK_SCREEN = 50,
+       EARLY_SUSPEND_LEVEL_STOP_DRAWING = 100,
+       EARLY_SUSPEND_LEVEL_DISABLE_FB = 150,
+};
+struct early_suspend {
+#ifdef CONFIG_HAS_EARLYSUSPEND
+       struct list_head link;
+       int level;
+       void (*suspend)(struct early_suspend *h);
+       void (*resume)(struct early_suspend *h);
+#endif
+};
+
+#ifdef CONFIG_HAS_EARLYSUSPEND
+void register_early_suspend(struct early_suspend *handler);
+void unregister_early_suspend(struct early_suspend *handler);
+#else
+#define register_early_suspend(handler) do { } while (0)
+#define unregister_early_suspend(handler) do { } while (0)
+#endif
+
+#endif
+
index 6677e32..173836c 100644 (file)
@@ -26,6 +26,9 @@ config WAKELOCK
        bool
        default y
 
+config HAS_EARLYSUSPEND
+       bool
+
 config USER_WAKELOCK
        bool "Userspace wake locks"
        depends on PM_SLEEP
@@ -36,6 +39,41 @@ config USER_WAKELOCK
          Write "lockname" to /sys/power/wake_unlock to unlock a user wake
          lock.
 
+config EARLYSUSPEND
+       bool "Early suspend"
+       depends on WAKELOCK
+       default y
+       select HAS_EARLYSUSPEND
+       ---help---
+         Call early suspend handlers when the user requested sleep state
+         changes.
+
+choice
+       prompt "User-space screen access"
+       default FB_EARLYSUSPEND if !FRAMEBUFFER_CONSOLE
+       default CONSOLE_EARLYSUSPEND
+       depends on HAS_EARLYSUSPEND
+
+       config NO_USER_SPACE_SCREEN_ACCESS_CONTROL
+               bool "None"
+
+       config CONSOLE_EARLYSUSPEND
+               bool "Console switch on early-suspend"
+               depends on HAS_EARLYSUSPEND && VT
+               ---help---
+                 Register early suspend handler to perform a console switch to
+                 when user-space should stop drawing to the screen and a switch
+                 back when it should resume.
+
+       config FB_EARLYSUSPEND
+               bool "Sysfs interface"
+               depends on HAS_EARLYSUSPEND
+               ---help---
+                 Register early suspend handler that notifies and waits for
+                 user-space through sysfs when user-space should stop drawing
+                 to the screen and notifies user-space when it should resume.
+endchoice
+
 config HIBERNATE_CALLBACKS
        bool
 
index 06f9472..26dd34b 100644 (file)
@@ -10,7 +10,9 @@ obj-$(CONFIG_PM_TEST_SUSPEND) += suspend_test.o
 obj-$(CONFIG_HIBERNATION)      += hibernate.o snapshot.o swap.o user.o \
                                   block_io.o
 obj-$(CONFIG_USER_WAKELOCK)    += userwakelock.o
+obj-$(CONFIG_EARLYSUSPEND)     += earlysuspend.o
+obj-$(CONFIG_CONSOLE_EARLYSUSPEND)     += consoleearlysuspend.o
+obj-$(CONFIG_FB_EARLYSUSPEND)  += fbearlysuspend.o
 obj-$(CONFIG_SUSPEND_TIME)     += suspend_time.o
 
-obj-y   += fbearlysuspend.o
 obj-$(CONFIG_MAGIC_SYSRQ)      += poweroff.o
diff --git a/kernel/power/consoleearlysuspend.c b/kernel/power/consoleearlysuspend.c
new file mode 100644 (file)
index 0000000..a3edcb2
--- /dev/null
@@ -0,0 +1,78 @@
+/* kernel/power/consoleearlysuspend.c
+ *
+ * Copyright (C) 2005-2008 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <linux/console.h>
+#include <linux/earlysuspend.h>
+#include <linux/kbd_kern.h>
+#include <linux/module.h>
+#include <linux/vt_kern.h>
+#include <linux/wait.h>
+
+#define EARLY_SUSPEND_CONSOLE  (MAX_NR_CONSOLES-1)
+
+static int orig_fgconsole;
+static void console_early_suspend(struct early_suspend *h)
+{
+       acquire_console_sem();
+       orig_fgconsole = fg_console;
+       if (vc_allocate(EARLY_SUSPEND_CONSOLE))
+               goto err;
+       if (set_console(EARLY_SUSPEND_CONSOLE))
+               goto err;
+       release_console_sem();
+
+       if (vt_waitactive(EARLY_SUSPEND_CONSOLE + 1))
+               pr_warning("console_early_suspend: Can't switch VCs.\n");
+       return;
+err:
+       pr_warning("console_early_suspend: Can't set console\n");
+       release_console_sem();
+}
+
+static void console_late_resume(struct early_suspend *h)
+{
+       int ret;
+       acquire_console_sem();
+       ret = set_console(orig_fgconsole);
+       release_console_sem();
+       if (ret) {
+               pr_warning("console_late_resume: Can't set console.\n");
+               return;
+       }
+
+       if (vt_waitactive(orig_fgconsole + 1))
+               pr_warning("console_late_resume: Can't switch VCs.\n");
+}
+
+static struct early_suspend console_early_suspend_desc = {
+       .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
+       .suspend = console_early_suspend,
+       .resume = console_late_resume,
+};
+
+static int __init console_early_suspend_init(void)
+{
+       register_early_suspend(&console_early_suspend_desc);
+       return 0;
+}
+
+static void  __exit console_early_suspend_exit(void)
+{
+       unregister_early_suspend(&console_early_suspend_desc);
+}
+
+module_init(console_early_suspend_init);
+module_exit(console_early_suspend_exit);
+
diff --git a/kernel/power/earlysuspend.c b/kernel/power/earlysuspend.c
new file mode 100644 (file)
index 0000000..cff3281
--- /dev/null
@@ -0,0 +1,190 @@
+/* kernel/power/earlysuspend.c
+ *
+ * Copyright (C) 2005-2008 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <linux/earlysuspend.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/rtc.h>
+#include <linux/syscalls.h> /* sys_sync */
+#include <linux/wakelock.h>
+#include <linux/workqueue.h>
+
+#include "power.h"
+
+enum {
+       DEBUG_USER_STATE = 1U << 0,
+       DEBUG_SUSPEND = 1U << 2,
+       DEBUG_VERBOSE = 1U << 3,
+};
+static int debug_mask = DEBUG_USER_STATE;
+module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
+
+suspend_state_t requested_suspend_state = PM_SUSPEND_MEM;
+static DEFINE_MUTEX(early_suspend_lock);
+static LIST_HEAD(early_suspend_handlers);
+static void early_suspend(struct work_struct *work);
+static void late_resume(struct work_struct *work);
+static void suspend(struct work_struct *work);
+static DECLARE_WORK(early_suspend_work, early_suspend);
+static DECLARE_WORK(late_resume_work, late_resume);
+static DECLARE_WORK(suspend_work, suspend);
+static DEFINE_SPINLOCK(state_lock);
+enum {
+       SUSPEND_REQUESTED = 0x1,
+       SUSPENDED = 0x2,
+       SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED,
+};
+static int state;
+
+void register_early_suspend(struct early_suspend *handler)
+{
+       struct list_head *pos;
+
+       mutex_lock(&early_suspend_lock);
+       list_for_each(pos, &early_suspend_handlers) {
+               struct early_suspend *e;
+               e = list_entry(pos, struct early_suspend, link);
+               if (e->level > handler->level)
+                       break;
+       }
+       list_add_tail(&handler->link, pos);
+       if ((state & SUSPENDED) && handler->suspend)
+               handler->suspend(handler);
+       mutex_unlock(&early_suspend_lock);
+}
+EXPORT_SYMBOL(register_early_suspend);
+
+void unregister_early_suspend(struct early_suspend *handler)
+{
+       mutex_lock(&early_suspend_lock);
+       list_del(&handler->link);
+       mutex_unlock(&early_suspend_lock);
+}
+EXPORT_SYMBOL(unregister_early_suspend);
+
+static void suspend(struct work_struct *work)
+{
+        pm_suspend(requested_suspend_state);
+}
+
+static void early_suspend(struct work_struct *work)
+{
+       struct early_suspend *pos;
+       unsigned long irqflags;
+       int abort = 0;
+
+       mutex_lock(&early_suspend_lock);
+       spin_lock_irqsave(&state_lock, irqflags);
+       if (state == SUSPEND_REQUESTED)
+               state |= SUSPENDED;
+       else
+               abort = 1;
+       spin_unlock_irqrestore(&state_lock, irqflags);
+
+       if (abort) {
+               if (debug_mask & DEBUG_SUSPEND)
+                       pr_info("early_suspend: abort, state %d\n", state);
+               mutex_unlock(&early_suspend_lock);
+               return;
+       }
+
+       if (debug_mask & DEBUG_SUSPEND)
+               pr_info("early_suspend: call handlers\n");
+       list_for_each_entry(pos, &early_suspend_handlers, link) {
+               if (pos->suspend != NULL) {
+                       if (debug_mask & DEBUG_VERBOSE)
+                               pr_info("early_suspend: calling %pf\n", pos->suspend);
+                       pos->suspend(pos);
+               }
+       }
+       mutex_unlock(&early_suspend_lock);
+
+       if (debug_mask & DEBUG_SUSPEND)
+               pr_info("early_suspend: sync\n");
+
+       sys_sync();
+       queue_work(suspend_work_queue, &suspend_work);
+}
+
+static void late_resume(struct work_struct *work)
+{
+       struct early_suspend *pos;
+       unsigned long irqflags;
+       int abort = 0;
+
+       mutex_lock(&early_suspend_lock);
+       spin_lock_irqsave(&state_lock, irqflags);
+       if (state == SUSPENDED)
+               state &= ~SUSPENDED;
+       else
+               abort = 1;
+       spin_unlock_irqrestore(&state_lock, irqflags);
+
+       if (abort) {
+               if (debug_mask & DEBUG_SUSPEND)
+                       pr_info("late_resume: abort, state %d\n", state);
+               goto abort;
+       }
+       if (debug_mask & DEBUG_SUSPEND)
+               pr_info("late_resume: call handlers\n");
+       list_for_each_entry_reverse(pos, &early_suspend_handlers, link) {
+               if (pos->resume != NULL) {
+                       if (debug_mask & DEBUG_VERBOSE)
+                               pr_info("late_resume: calling %pf\n", pos->resume);
+
+                       pos->resume(pos);
+               }
+       }
+       if (debug_mask & DEBUG_SUSPEND)
+               pr_info("late_resume: done\n");
+abort:
+       mutex_unlock(&early_suspend_lock);
+}
+
+void request_suspend_state(suspend_state_t new_state)
+{
+       unsigned long irqflags;
+       int old_sleep;
+
+       spin_lock_irqsave(&state_lock, irqflags);
+       old_sleep = state & SUSPEND_REQUESTED;
+       if (debug_mask & DEBUG_USER_STATE) {
+               struct timespec ts;
+               struct rtc_time tm;
+               getnstimeofday(&ts);
+               rtc_time_to_tm(ts.tv_sec, &tm);
+               pr_info("request_suspend_state: %s (%d->%d) at %lld "
+                       "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
+                       new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",
+                       requested_suspend_state, new_state,
+                       ktime_to_ns(ktime_get()),
+                       tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
+                       tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
+       }
+       if (!old_sleep && new_state != PM_SUSPEND_ON) {
+               state |= SUSPEND_REQUESTED;
+               queue_work(suspend_work_queue, &early_suspend_work);
+       } else if (old_sleep && new_state == PM_SUSPEND_ON) {
+               state &= ~SUSPEND_REQUESTED;
+               queue_work(suspend_work_queue, &late_resume_work);
+       }
+       requested_suspend_state = new_state;
+       spin_unlock_irqrestore(&state_lock, irqflags);
+}
+
+suspend_state_t get_suspend_state(void)
+{
+       return requested_suspend_state;
+}
index 571b51a..1513765 100644 (file)
@@ -13,6 +13,7 @@
  *
  */
 
+#include <linux/earlysuspend.h>
 #include <linux/module.h>
 #include <linux/wait.h>
 
@@ -26,6 +27,42 @@ static enum {
        FB_STATE_DRAWING_OK,
 } fb_state;
 
+/* tell userspace to stop drawing, wait for it to stop */
+static void stop_drawing_early_suspend(struct early_suspend *h)
+{
+       int ret;
+       unsigned long irq_flags;
+
+       spin_lock_irqsave(&fb_state_lock, irq_flags);
+       fb_state = FB_STATE_REQUEST_STOP_DRAWING;
+       spin_unlock_irqrestore(&fb_state_lock, irq_flags);
+
+       wake_up_all(&fb_state_wq);
+       ret = wait_event_timeout(fb_state_wq,
+                                fb_state == FB_STATE_STOPPED_DRAWING,
+                                HZ);
+       if (unlikely(fb_state != FB_STATE_STOPPED_DRAWING))
+               pr_warning("stop_drawing_early_suspend: timeout waiting for "
+                          "userspace to stop drawing\n");
+}
+
+/* tell userspace to start drawing */
+static void start_drawing_late_resume(struct early_suspend *h)
+{
+       unsigned long irq_flags;
+
+       spin_lock_irqsave(&fb_state_lock, irq_flags);
+       fb_state = FB_STATE_DRAWING_OK;
+       spin_unlock_irqrestore(&fb_state_lock, irq_flags);
+       wake_up(&fb_state_wq);
+}
+
+static struct early_suspend stop_drawing_early_suspend_desc = {
+       .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
+       .suspend = stop_drawing_early_suspend,
+       .resume = start_drawing_late_resume,
+};
+
 static ssize_t wait_for_fb_sleep_show(struct kobject *kobj,
                                      struct kobj_attribute *attr, char *buf)
 {
@@ -101,11 +138,13 @@ static int __init android_power_init(void)
                return ret;
        }
 
+       register_early_suspend(&stop_drawing_early_suspend_desc);
        return 0;
 }
 
 static void  __exit android_power_exit(void)
 {
+       unregister_early_suspend(&stop_drawing_early_suspend_desc);
        sysfs_remove_group(power_kobj, &attr_group);
 }
 
index 7f3c91a..c8a4b77 100644 (file)
@@ -21,6 +21,7 @@
 DEFINE_MUTEX(pm_mutex);
 
 #ifdef CONFIG_PM_SLEEP
+struct workqueue_struct *suspend_work_queue;
 
 /* Routines for PM-transition notifications */
 
@@ -273,7 +274,11 @@ static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
                           const char *buf, size_t n)
 {
 #ifdef CONFIG_SUSPEND
+#ifdef CONFIG_EARLYSUSPEND
+       suspend_state_t state = PM_SUSPEND_ON;
+#else
        suspend_state_t state = PM_SUSPEND_STANDBY;
+#endif
        const char * const *s;
 #endif
        char *p;
@@ -291,11 +296,18 @@ static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
 
 #ifdef CONFIG_SUSPEND
        for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
-               if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
-                       error = pm_suspend(state);
+               if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
                        break;
-               }
        }
+       if (state < PM_SUSPEND_MAX && *s)
+#ifdef CONFIG_EARLYSUSPEND
+               if (state == PM_SUSPEND_ON || valid_state(state)) {
+                       error = 0;
+                       request_suspend_state(state);
+               }
+#else
+               error = enter_state(state);
+#endif
 #endif
 
  Exit:
@@ -445,15 +457,28 @@ static inline int pm_start_workqueue(void) { return 0; }
 
 static int __init pm_init(void)
 {
-       int error = pm_start_workqueue();
-       if (error)
-               return error;
+       int ret;
+
+       suspend_work_queue = create_singlethread_workqueue("suspend");
+        if (suspend_work_queue == NULL)
+                return -ENOMEM;
+
+       ret = pm_start_workqueue();
+       if (ret)
+               goto error;
+
        hibernate_image_size_init();
        hibernate_reserved_size_init();
        power_kobj = kobject_create_and_add("power", NULL);
-       if (!power_kobj)
-               return -ENOMEM;
+       if (!power_kobj) {
+               ret = -ENOMEM;
+               goto error;
+       }
        return sysfs_create_group(power_kobj, &attr_group);
+
+error:
+       destroy_workqueue(suspend_work_queue);
+       return ret;
 }
 
 core_initcall(pm_init);
index c4ecb81..3944423 100644 (file)
@@ -177,11 +177,13 @@ extern const char *const pm_states[];
 
 extern bool valid_state(suspend_state_t state);
 extern int suspend_devices_and_enter(suspend_state_t state);
+extern int enter_state(suspend_state_t state);
 #else /* !CONFIG_SUSPEND */
 static inline int suspend_devices_and_enter(suspend_state_t state)
 {
        return -ENOSYS;
 }
+static inline int enter_state(suspend_state_t state) { return -ENOSYS; }
 static inline bool valid_state(suspend_state_t state) { return false; }
 #endif /* !CONFIG_SUSPEND */
 
@@ -265,6 +267,13 @@ static inline void suspend_thaw_processes(void)
 }
 #endif
 
+#ifdef CONFIG_WAKELOCK
+/* kernel/power/wakelock.c */
+extern struct workqueue_struct *suspend_work_queue;
+extern struct wake_lock main_wake_lock;
+extern suspend_state_t requested_suspend_state;
+#endif
+
 #ifdef CONFIG_USER_WAKELOCK
 ssize_t wake_lock_show(struct kobject *kobj, struct kobj_attribute *attr,
                        char *buf);
@@ -275,3 +284,9 @@ ssize_t wake_unlock_show(struct kobject *kobj, struct kobj_attribute *attr,
 ssize_t  wake_unlock_store(struct kobject *kobj, struct kobj_attribute *attr,
                        const char *buf, size_t n);
 #endif
+
+#ifdef CONFIG_EARLYSUSPEND
+/* kernel/power/earlysuspend.c */
+void request_suspend_state(suspend_state_t state);
+suspend_state_t get_suspend_state(void);
+#endif
index 396d262..74eb3de 100644 (file)
@@ -29,6 +29,9 @@
 #include "power.h"
 
 const char *const pm_states[PM_SUSPEND_MAX] = {
+#ifdef CONFIG_EARLYSUSPEND
+       [PM_SUSPEND_ON]         = "on",
+#endif
        [PM_SUSPEND_STANDBY]    = "standby",
        [PM_SUSPEND_MEM]        = "mem",
 };
@@ -265,7 +268,7 @@ static void suspend_finish(void)
  * Fail if that's not the case.  Otherwise, prepare for system suspend, make the
  * system enter the given sleep state and clean up after wakeup.
  */
-static int enter_state(suspend_state_t state)
+int enter_state(suspend_state_t state)
 {
        int error;