Revert "power: earlysuspend: suspend if user space is still asleep after resume"
[linux-2.6.git] / kernel / power / earlysuspend.c
1 /* kernel/power/earlysuspend.c
2  *
3  * Copyright (C) 2005-2008 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/earlysuspend.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/rtc.h>
20 #include <linux/syscalls.h> /* sys_sync */
21 #include <linux/wakelock.h>
22 #include <linux/workqueue.h>
23
24 #include "power.h"
25
26 enum {
27         DEBUG_USER_STATE = 1U << 0,
28         DEBUG_SUSPEND = 1U << 2,
29         DEBUG_VERBOSE = 1U << 3,
30 };
31 static int debug_mask = DEBUG_USER_STATE;
32 module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
33
34 suspend_state_t requested_suspend_state = PM_SUSPEND_MEM;
35 static DEFINE_MUTEX(early_suspend_lock);
36 static LIST_HEAD(early_suspend_handlers);
37 static void early_suspend(struct work_struct *work);
38 static void late_resume(struct work_struct *work);
39 static void suspend(struct work_struct *work);
40 static DECLARE_WORK(early_suspend_work, early_suspend);
41 static DECLARE_WORK(late_resume_work, late_resume);
42 static DECLARE_WORK(suspend_work, suspend);
43 static DEFINE_SPINLOCK(state_lock);
44 enum {
45         SUSPEND_REQUESTED = 0x1,
46         SUSPENDED = 0x2,
47         SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED,
48 };
49 static int state;
50
51 void register_early_suspend(struct early_suspend *handler)
52 {
53         struct list_head *pos;
54
55         mutex_lock(&early_suspend_lock);
56         list_for_each(pos, &early_suspend_handlers) {
57                 struct early_suspend *e;
58                 e = list_entry(pos, struct early_suspend, link);
59                 if (e->level > handler->level)
60                         break;
61         }
62         list_add_tail(&handler->link, pos);
63         if ((state & SUSPENDED) && handler->suspend)
64                 handler->suspend(handler);
65         mutex_unlock(&early_suspend_lock);
66 }
67 EXPORT_SYMBOL(register_early_suspend);
68
69 void unregister_early_suspend(struct early_suspend *handler)
70 {
71         mutex_lock(&early_suspend_lock);
72         list_del(&handler->link);
73         mutex_unlock(&early_suspend_lock);
74 }
75 EXPORT_SYMBOL(unregister_early_suspend);
76
77 static void suspend(struct work_struct *work)
78 {
79          pm_suspend(requested_suspend_state);
80 }
81
82 static void early_suspend(struct work_struct *work)
83 {
84         struct early_suspend *pos;
85         unsigned long irqflags;
86         int abort = 0;
87
88         mutex_lock(&early_suspend_lock);
89         spin_lock_irqsave(&state_lock, irqflags);
90         if (state == SUSPEND_REQUESTED)
91                 state |= SUSPENDED;
92         else
93                 abort = 1;
94         spin_unlock_irqrestore(&state_lock, irqflags);
95
96         if (abort) {
97                 if (debug_mask & DEBUG_SUSPEND)
98                         pr_info("early_suspend: abort, state %d\n", state);
99                 mutex_unlock(&early_suspend_lock);
100                 return;
101         }
102
103         if (debug_mask & DEBUG_SUSPEND)
104                 pr_info("early_suspend: call handlers\n");
105         list_for_each_entry(pos, &early_suspend_handlers, link) {
106                 if (pos->suspend != NULL) {
107                         if (debug_mask & DEBUG_VERBOSE)
108                                 pr_info("early_suspend: calling %pf\n", pos->suspend);
109                         pos->suspend(pos);
110                 }
111         }
112         mutex_unlock(&early_suspend_lock);
113
114         if (debug_mask & DEBUG_SUSPEND)
115                 pr_info("early_suspend: sync\n");
116
117         sys_sync();
118         queue_work(suspend_work_queue, &suspend_work);
119 }
120
121 static void late_resume(struct work_struct *work)
122 {
123         struct early_suspend *pos;
124         unsigned long irqflags;
125         int abort = 0;
126
127         mutex_lock(&early_suspend_lock);
128         spin_lock_irqsave(&state_lock, irqflags);
129         if (state == SUSPENDED)
130                 state &= ~SUSPENDED;
131         else
132                 abort = 1;
133         spin_unlock_irqrestore(&state_lock, irqflags);
134
135         if (abort) {
136                 if (debug_mask & DEBUG_SUSPEND)
137                         pr_info("late_resume: abort, state %d\n", state);
138                 goto abort;
139         }
140         if (debug_mask & DEBUG_SUSPEND)
141                 pr_info("late_resume: call handlers\n");
142         list_for_each_entry_reverse(pos, &early_suspend_handlers, link) {
143                 if (pos->resume != NULL) {
144                         if (debug_mask & DEBUG_VERBOSE)
145                                 pr_info("late_resume: calling %pf\n", pos->resume);
146
147                         pos->resume(pos);
148                 }
149         }
150         if (debug_mask & DEBUG_SUSPEND)
151                 pr_info("late_resume: done\n");
152 abort:
153         mutex_unlock(&early_suspend_lock);
154 }
155
156 void request_suspend_state(suspend_state_t new_state)
157 {
158         unsigned long irqflags;
159         int old_sleep;
160
161         spin_lock_irqsave(&state_lock, irqflags);
162         old_sleep = state & SUSPEND_REQUESTED;
163         if (debug_mask & DEBUG_USER_STATE) {
164                 struct timespec ts;
165                 struct rtc_time tm;
166                 getnstimeofday(&ts);
167                 rtc_time_to_tm(ts.tv_sec, &tm);
168                 pr_info("request_suspend_state: %s (%d->%d) at %lld "
169                         "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
170                         new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",
171                         requested_suspend_state, new_state,
172                         ktime_to_ns(ktime_get()),
173                         tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
174                         tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
175         }
176         if (!old_sleep && new_state != PM_SUSPEND_ON) {
177                 state |= SUSPEND_REQUESTED;
178                 queue_work(suspend_work_queue, &early_suspend_work);
179         } else if (old_sleep && new_state == PM_SUSPEND_ON) {
180                 state &= ~SUSPEND_REQUESTED;
181                 queue_work(suspend_work_queue, &late_resume_work);
182         }
183         requested_suspend_state = new_state;
184         spin_unlock_irqrestore(&state_lock, irqflags);
185 }
186
187 suspend_state_t get_suspend_state(void)
188 {
189         return requested_suspend_state;
190 }