blob: 9f77f61a29d4e0b5d9200dc942985572b3f50ddf [file] [log] [blame]
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -07001/* kernel/power/wakelock.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/module.h>
17#include <linux/platform_device.h>
18#include <linux/rtc.h>
19#include <linux/suspend.h>
20#include <linux/syscalls.h> /* sys_sync */
21#include <linux/wakelock.h>
22#ifdef CONFIG_WAKELOCK_STAT
23#include <linux/proc_fs.h>
24#endif
25#include "power.h"
26
27enum {
28 DEBUG_EXIT_SUSPEND = 1U << 0,
29 DEBUG_WAKEUP = 1U << 1,
30 DEBUG_SUSPEND = 1U << 2,
31 DEBUG_EXPIRE = 1U << 3,
32 DEBUG_WAKE_LOCK = 1U << 4,
33};
34static int debug_mask = DEBUG_EXIT_SUSPEND | DEBUG_WAKEUP;
35module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
36
37#define WAKE_LOCK_TYPE_MASK (0x0f)
38#define WAKE_LOCK_INITIALIZED (1U << 8)
39#define WAKE_LOCK_ACTIVE (1U << 9)
40#define WAKE_LOCK_AUTO_EXPIRE (1U << 10)
41#define WAKE_LOCK_PREVENTING_SUSPEND (1U << 11)
42
43static DEFINE_SPINLOCK(list_lock);
44static LIST_HEAD(inactive_locks);
45static struct list_head active_wake_locks[WAKE_LOCK_TYPE_COUNT];
46static int current_event_num;
Pratik Patel529461b2011-07-21 19:13:34 -070047static int suspend_sys_sync_count;
48static DEFINE_SPINLOCK(suspend_sys_sync_lock);
49static struct workqueue_struct *suspend_sys_sync_work_queue;
50static DECLARE_COMPLETION(suspend_sys_sync_comp);
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -070051struct workqueue_struct *suspend_work_queue;
52struct wake_lock main_wake_lock;
53suspend_state_t requested_suspend_state = PM_SUSPEND_MEM;
54static struct wake_lock unknown_wakeup;
Abhijeet Dharmapurikar57b34bb2011-09-26 17:10:02 -070055/* flag to warn/bug if wakelocks are taken after suspend_noirq */
56static int msm_suspend_check_done;
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -070057
58#ifdef CONFIG_WAKELOCK_STAT
59static struct wake_lock deleted_wake_locks;
60static ktime_t last_sleep_time_update;
61static int wait_for_wakeup;
62
63int get_expired_time(struct wake_lock *lock, ktime_t *expire_time)
64{
65 struct timespec ts;
66 struct timespec kt;
67 struct timespec tomono;
68 struct timespec delta;
Colin Cross28e23cf2011-03-30 12:37:49 -070069 struct timespec sleep;
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -070070 long timeout;
71
72 if (!(lock->flags & WAKE_LOCK_AUTO_EXPIRE))
73 return 0;
Colin Cross28e23cf2011-03-30 12:37:49 -070074 get_xtime_and_monotonic_and_sleep_offset(&kt, &tomono, &sleep);
75 timeout = lock->expires - jiffies;
76 if (timeout > 0)
77 return 0;
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -070078 jiffies_to_timespec(-timeout, &delta);
79 set_normalized_timespec(&ts, kt.tv_sec + tomono.tv_sec - delta.tv_sec,
80 kt.tv_nsec + tomono.tv_nsec - delta.tv_nsec);
81 *expire_time = timespec_to_ktime(ts);
82 return 1;
83}
84
85
Arve Hjønnevåg1b074952009-12-02 18:22:00 -080086static int print_lock_stat(struct seq_file *m, struct wake_lock *lock)
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -070087{
88 int lock_count = lock->stat.count;
89 int expire_count = lock->stat.expire_count;
90 ktime_t active_time = ktime_set(0, 0);
91 ktime_t total_time = lock->stat.total_time;
92 ktime_t max_time = lock->stat.max_time;
Erik Gilling10f01382009-08-25 20:09:12 -070093
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -070094 ktime_t prevent_suspend_time = lock->stat.prevent_suspend_time;
95 if (lock->flags & WAKE_LOCK_ACTIVE) {
96 ktime_t now, add_time;
97 int expired = get_expired_time(lock, &now);
98 if (!expired)
99 now = ktime_get();
100 add_time = ktime_sub(now, lock->stat.last_time);
101 lock_count++;
102 if (!expired)
103 active_time = add_time;
104 else
105 expire_count++;
106 total_time = ktime_add(total_time, add_time);
107 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND)
108 prevent_suspend_time = ktime_add(prevent_suspend_time,
109 ktime_sub(now, last_sleep_time_update));
110 if (add_time.tv64 > max_time.tv64)
111 max_time = add_time;
112 }
113
Arve Hjønnevåg1b074952009-12-02 18:22:00 -0800114 return seq_printf(m,
Erik Gilling10f01382009-08-25 20:09:12 -0700115 "\"%s\"\t%d\t%d\t%d\t%lld\t%lld\t%lld\t%lld\t%lld\n",
116 lock->name, lock_count, expire_count,
117 lock->stat.wakeup_count, ktime_to_ns(active_time),
118 ktime_to_ns(total_time),
119 ktime_to_ns(prevent_suspend_time), ktime_to_ns(max_time),
120 ktime_to_ns(lock->stat.last_time));
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700121}
122
Arve Hjønnevåg1b074952009-12-02 18:22:00 -0800123static int wakelock_stats_show(struct seq_file *m, void *unused)
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700124{
125 unsigned long irqflags;
126 struct wake_lock *lock;
Arve Hjønnevåg1b074952009-12-02 18:22:00 -0800127 int ret;
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700128 int type;
129
130 spin_lock_irqsave(&list_lock, irqflags);
131
Arve Hjønnevåg1b074952009-12-02 18:22:00 -0800132 ret = seq_puts(m, "name\tcount\texpire_count\twake_count\tactive_since"
Erik Gilling10f01382009-08-25 20:09:12 -0700133 "\ttotal_time\tsleep_time\tmax_time\tlast_change\n");
Arve Hjønnevåg1b074952009-12-02 18:22:00 -0800134 list_for_each_entry(lock, &inactive_locks, link)
135 ret = print_lock_stat(m, lock);
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700136 for (type = 0; type < WAKE_LOCK_TYPE_COUNT; type++) {
137 list_for_each_entry(lock, &active_wake_locks[type], link)
Arve Hjønnevåg1b074952009-12-02 18:22:00 -0800138 ret = print_lock_stat(m, lock);
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700139 }
140 spin_unlock_irqrestore(&list_lock, irqflags);
Arve Hjønnevåg1b074952009-12-02 18:22:00 -0800141 return 0;
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700142}
143
144static void wake_unlock_stat_locked(struct wake_lock *lock, int expired)
145{
146 ktime_t duration;
147 ktime_t now;
148 if (!(lock->flags & WAKE_LOCK_ACTIVE))
149 return;
150 if (get_expired_time(lock, &now))
151 expired = 1;
152 else
153 now = ktime_get();
154 lock->stat.count++;
155 if (expired)
156 lock->stat.expire_count++;
157 duration = ktime_sub(now, lock->stat.last_time);
158 lock->stat.total_time = ktime_add(lock->stat.total_time, duration);
159 if (ktime_to_ns(duration) > ktime_to_ns(lock->stat.max_time))
160 lock->stat.max_time = duration;
161 lock->stat.last_time = ktime_get();
162 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
163 duration = ktime_sub(now, last_sleep_time_update);
164 lock->stat.prevent_suspend_time = ktime_add(
165 lock->stat.prevent_suspend_time, duration);
166 lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
167 }
168}
169
170static void update_sleep_wait_stats_locked(int done)
171{
172 struct wake_lock *lock;
173 ktime_t now, etime, elapsed, add;
174 int expired;
175
176 now = ktime_get();
177 elapsed = ktime_sub(now, last_sleep_time_update);
178 list_for_each_entry(lock, &active_wake_locks[WAKE_LOCK_SUSPEND], link) {
179 expired = get_expired_time(lock, &etime);
180 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
181 if (expired)
182 add = ktime_sub(etime, last_sleep_time_update);
183 else
184 add = elapsed;
185 lock->stat.prevent_suspend_time = ktime_add(
186 lock->stat.prevent_suspend_time, add);
187 }
188 if (done || expired)
189 lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
190 else
191 lock->flags |= WAKE_LOCK_PREVENTING_SUSPEND;
192 }
193 last_sleep_time_update = now;
194}
195#endif
196
197
198static void expire_wake_lock(struct wake_lock *lock)
199{
200#ifdef CONFIG_WAKELOCK_STAT
201 wake_unlock_stat_locked(lock, 1);
202#endif
203 lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
204 list_del(&lock->link);
205 list_add(&lock->link, &inactive_locks);
206 if (debug_mask & (DEBUG_WAKE_LOCK | DEBUG_EXPIRE))
207 pr_info("expired wake lock %s\n", lock->name);
208}
209
Mike Chan97a0a742009-08-25 18:10:32 -0700210/* Caller must acquire the list_lock spinlock */
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700211static void print_active_locks(int type)
212{
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700213 struct wake_lock *lock;
Mike Chanaf62b252010-02-16 14:18:55 -0800214 bool print_expired = true;
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700215
216 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700217 list_for_each_entry(lock, &active_wake_locks[type], link) {
218 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
219 long timeout = lock->expires - jiffies;
Mike Chanaf62b252010-02-16 14:18:55 -0800220 if (timeout > 0)
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700221 pr_info("active wake lock %s, time left %ld\n",
222 lock->name, timeout);
Mike Chanaf62b252010-02-16 14:18:55 -0800223 else if (print_expired)
224 pr_info("wake lock %s, expired\n", lock->name);
225 } else {
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700226 pr_info("active wake lock %s\n", lock->name);
Colin Cross0c7841c2010-08-21 17:27:02 -0700227 if (!(debug_mask & DEBUG_EXPIRE))
Mike Chanaf62b252010-02-16 14:18:55 -0800228 print_expired = false;
229 }
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700230 }
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700231}
232
233static long has_wake_lock_locked(int type)
234{
235 struct wake_lock *lock, *n;
236 long max_timeout = 0;
237
238 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
239 list_for_each_entry_safe(lock, n, &active_wake_locks[type], link) {
240 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
241 long timeout = lock->expires - jiffies;
242 if (timeout <= 0)
243 expire_wake_lock(lock);
244 else if (timeout > max_timeout)
245 max_timeout = timeout;
246 } else
247 return -1;
248 }
249 return max_timeout;
250}
251
252long has_wake_lock(int type)
253{
254 long ret;
255 unsigned long irqflags;
256 spin_lock_irqsave(&list_lock, irqflags);
257 ret = has_wake_lock_locked(type);
Todd Poynorca64b0c2011-08-08 16:06:54 -0700258 if (ret && (debug_mask & DEBUG_WAKEUP) && type == WAKE_LOCK_SUSPEND)
Mike Chanaf62b252010-02-16 14:18:55 -0800259 print_active_locks(type);
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700260 spin_unlock_irqrestore(&list_lock, irqflags);
261 return ret;
262}
263
Pratik Patel529461b2011-07-21 19:13:34 -0700264static void suspend_sys_sync(struct work_struct *work)
265{
266 if (debug_mask & DEBUG_SUSPEND)
267 pr_info("PM: Syncing filesystems...\n");
268
269 sys_sync();
270
271 if (debug_mask & DEBUG_SUSPEND)
272 pr_info("sync done.\n");
273
274 spin_lock(&suspend_sys_sync_lock);
275 suspend_sys_sync_count--;
276 spin_unlock(&suspend_sys_sync_lock);
277}
278static DECLARE_WORK(suspend_sys_sync_work, suspend_sys_sync);
279
280void suspend_sys_sync_queue(void)
281{
282 int ret;
283
284 spin_lock(&suspend_sys_sync_lock);
285 ret = queue_work(suspend_sys_sync_work_queue, &suspend_sys_sync_work);
286 if (ret)
287 suspend_sys_sync_count++;
288 spin_unlock(&suspend_sys_sync_lock);
289}
290
291static bool suspend_sys_sync_abort;
292static void suspend_sys_sync_handler(unsigned long);
293static DEFINE_TIMER(suspend_sys_sync_timer, suspend_sys_sync_handler, 0, 0);
294/* value should be less then half of input event wake lock timeout value
295 * which is currently set to 5*HZ (see drivers/input/evdev.c)
296 */
297#define SUSPEND_SYS_SYNC_TIMEOUT (HZ/4)
298static void suspend_sys_sync_handler(unsigned long arg)
299{
300 if (suspend_sys_sync_count == 0) {
301 complete(&suspend_sys_sync_comp);
302 } else if (has_wake_lock(WAKE_LOCK_SUSPEND)) {
303 suspend_sys_sync_abort = true;
304 complete(&suspend_sys_sync_comp);
305 } else {
306 mod_timer(&suspend_sys_sync_timer, jiffies +
307 SUSPEND_SYS_SYNC_TIMEOUT);
308 }
309}
310
311int suspend_sys_sync_wait(void)
312{
313 suspend_sys_sync_abort = false;
314
315 if (suspend_sys_sync_count != 0) {
316 mod_timer(&suspend_sys_sync_timer, jiffies +
317 SUSPEND_SYS_SYNC_TIMEOUT);
318 wait_for_completion(&suspend_sys_sync_comp);
319 }
320 if (suspend_sys_sync_abort) {
321 pr_info("suspend aborted....while waiting for sys_sync\n");
322 return -EAGAIN;
323 }
324
325 return 0;
326}
327
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700328static void suspend(struct work_struct *work)
329{
330 int ret;
331 int entry_event_num;
332
333 if (has_wake_lock(WAKE_LOCK_SUSPEND)) {
334 if (debug_mask & DEBUG_SUSPEND)
335 pr_info("suspend: abort suspend\n");
336 return;
337 }
338
339 entry_event_num = current_event_num;
340 sys_sync();
341 if (debug_mask & DEBUG_SUSPEND)
342 pr_info("suspend: enter suspend\n");
343 ret = pm_suspend(requested_suspend_state);
344 if (debug_mask & DEBUG_EXIT_SUSPEND) {
345 struct timespec ts;
346 struct rtc_time tm;
347 getnstimeofday(&ts);
348 rtc_time_to_tm(ts.tv_sec, &tm);
349 pr_info("suspend: exit suspend, ret = %d "
350 "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", ret,
351 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
352 tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
353 }
354 if (current_event_num == entry_event_num) {
355 if (debug_mask & DEBUG_SUSPEND)
356 pr_info("suspend: pm_suspend returned with no event\n");
357 wake_lock_timeout(&unknown_wakeup, HZ / 2);
358 }
359}
360static DECLARE_WORK(suspend_work, suspend);
361
362static void expire_wake_locks(unsigned long data)
363{
364 long has_lock;
365 unsigned long irqflags;
366 if (debug_mask & DEBUG_EXPIRE)
367 pr_info("expire_wake_locks: start\n");
Mike Chan97a0a742009-08-25 18:10:32 -0700368 spin_lock_irqsave(&list_lock, irqflags);
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700369 if (debug_mask & DEBUG_SUSPEND)
370 print_active_locks(WAKE_LOCK_SUSPEND);
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700371 has_lock = has_wake_lock_locked(WAKE_LOCK_SUSPEND);
372 if (debug_mask & DEBUG_EXPIRE)
373 pr_info("expire_wake_locks: done, has_lock %ld\n", has_lock);
374 if (has_lock == 0)
375 queue_work(suspend_work_queue, &suspend_work);
376 spin_unlock_irqrestore(&list_lock, irqflags);
377}
378static DEFINE_TIMER(expire_timer, expire_wake_locks, 0, 0);
379
380static int power_suspend_late(struct device *dev)
381{
382 int ret = has_wake_lock(WAKE_LOCK_SUSPEND) ? -EAGAIN : 0;
383#ifdef CONFIG_WAKELOCK_STAT
Todd Poynored27e532011-08-08 17:26:49 -0700384 wait_for_wakeup = !ret;
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700385#endif
386 if (debug_mask & DEBUG_SUSPEND)
387 pr_info("power_suspend_late return %d\n", ret);
Abhijeet Dharmapurikar57b34bb2011-09-26 17:10:02 -0700388
389 msm_suspend_check_done = 1;
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700390 return ret;
391}
392
Abhijeet Dharmapurikar57b34bb2011-09-26 17:10:02 -0700393static int power_resume_early(struct device *dev)
394{
395 msm_suspend_check_done = 0;
396 return 0;
397}
398
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700399static struct dev_pm_ops power_driver_pm_ops = {
400 .suspend_noirq = power_suspend_late,
Abhijeet Dharmapurikar57b34bb2011-09-26 17:10:02 -0700401 .resume_noirq = power_resume_early,
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700402};
403
404static struct platform_driver power_driver = {
405 .driver.name = "power",
406 .driver.pm = &power_driver_pm_ops,
407};
408static struct platform_device power_device = {
409 .name = "power",
410};
411
412void wake_lock_init(struct wake_lock *lock, int type, const char *name)
413{
414 unsigned long irqflags = 0;
415
416 if (name)
417 lock->name = name;
418 BUG_ON(!lock->name);
419
420 if (debug_mask & DEBUG_WAKE_LOCK)
421 pr_info("wake_lock_init name=%s\n", lock->name);
422#ifdef CONFIG_WAKELOCK_STAT
423 lock->stat.count = 0;
424 lock->stat.expire_count = 0;
425 lock->stat.wakeup_count = 0;
426 lock->stat.total_time = ktime_set(0, 0);
427 lock->stat.prevent_suspend_time = ktime_set(0, 0);
428 lock->stat.max_time = ktime_set(0, 0);
429 lock->stat.last_time = ktime_set(0, 0);
430#endif
431 lock->flags = (type & WAKE_LOCK_TYPE_MASK) | WAKE_LOCK_INITIALIZED;
432
433 INIT_LIST_HEAD(&lock->link);
434 spin_lock_irqsave(&list_lock, irqflags);
435 list_add(&lock->link, &inactive_locks);
436 spin_unlock_irqrestore(&list_lock, irqflags);
437}
438EXPORT_SYMBOL(wake_lock_init);
439
440void wake_lock_destroy(struct wake_lock *lock)
441{
442 unsigned long irqflags;
443 if (debug_mask & DEBUG_WAKE_LOCK)
444 pr_info("wake_lock_destroy name=%s\n", lock->name);
445 spin_lock_irqsave(&list_lock, irqflags);
446 lock->flags &= ~WAKE_LOCK_INITIALIZED;
447#ifdef CONFIG_WAKELOCK_STAT
448 if (lock->stat.count) {
449 deleted_wake_locks.stat.count += lock->stat.count;
450 deleted_wake_locks.stat.expire_count += lock->stat.expire_count;
451 deleted_wake_locks.stat.total_time =
452 ktime_add(deleted_wake_locks.stat.total_time,
453 lock->stat.total_time);
454 deleted_wake_locks.stat.prevent_suspend_time =
455 ktime_add(deleted_wake_locks.stat.prevent_suspend_time,
456 lock->stat.prevent_suspend_time);
457 deleted_wake_locks.stat.max_time =
458 ktime_add(deleted_wake_locks.stat.max_time,
459 lock->stat.max_time);
460 }
461#endif
462 list_del(&lock->link);
463 spin_unlock_irqrestore(&list_lock, irqflags);
464}
465EXPORT_SYMBOL(wake_lock_destroy);
466
467static void wake_lock_internal(
468 struct wake_lock *lock, long timeout, int has_timeout)
469{
470 int type;
471 unsigned long irqflags;
472 long expire_in;
473
474 spin_lock_irqsave(&list_lock, irqflags);
475 type = lock->flags & WAKE_LOCK_TYPE_MASK;
476 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
477 BUG_ON(!(lock->flags & WAKE_LOCK_INITIALIZED));
478#ifdef CONFIG_WAKELOCK_STAT
479 if (type == WAKE_LOCK_SUSPEND && wait_for_wakeup) {
480 if (debug_mask & DEBUG_WAKEUP)
481 pr_info("wakeup wake lock: %s\n", lock->name);
482 wait_for_wakeup = 0;
483 lock->stat.wakeup_count++;
484 }
485 if ((lock->flags & WAKE_LOCK_AUTO_EXPIRE) &&
486 (long)(lock->expires - jiffies) <= 0) {
487 wake_unlock_stat_locked(lock, 0);
488 lock->stat.last_time = ktime_get();
489 }
490#endif
491 if (!(lock->flags & WAKE_LOCK_ACTIVE)) {
492 lock->flags |= WAKE_LOCK_ACTIVE;
493#ifdef CONFIG_WAKELOCK_STAT
494 lock->stat.last_time = ktime_get();
495#endif
496 }
497 list_del(&lock->link);
498 if (has_timeout) {
499 if (debug_mask & DEBUG_WAKE_LOCK)
500 pr_info("wake_lock: %s, type %d, timeout %ld.%03lu\n",
501 lock->name, type, timeout / HZ,
502 (timeout % HZ) * MSEC_PER_SEC / HZ);
503 lock->expires = jiffies + timeout;
504 lock->flags |= WAKE_LOCK_AUTO_EXPIRE;
505 list_add_tail(&lock->link, &active_wake_locks[type]);
506 } else {
507 if (debug_mask & DEBUG_WAKE_LOCK)
508 pr_info("wake_lock: %s, type %d\n", lock->name, type);
509 lock->expires = LONG_MAX;
510 lock->flags &= ~WAKE_LOCK_AUTO_EXPIRE;
511 list_add(&lock->link, &active_wake_locks[type]);
512 }
513 if (type == WAKE_LOCK_SUSPEND) {
514 current_event_num++;
515#ifdef CONFIG_WAKELOCK_STAT
516 if (lock == &main_wake_lock)
517 update_sleep_wait_stats_locked(1);
518 else if (!wake_lock_active(&main_wake_lock))
519 update_sleep_wait_stats_locked(0);
520#endif
521 if (has_timeout)
522 expire_in = has_wake_lock_locked(type);
523 else
524 expire_in = -1;
525 if (expire_in > 0) {
526 if (debug_mask & DEBUG_EXPIRE)
527 pr_info("wake_lock: %s, start expire timer, "
528 "%ld\n", lock->name, expire_in);
529 mod_timer(&expire_timer, jiffies + expire_in);
530 } else {
531 if (del_timer(&expire_timer))
532 if (debug_mask & DEBUG_EXPIRE)
533 pr_info("wake_lock: %s, stop expire timer\n",
534 lock->name);
535 if (expire_in == 0)
536 queue_work(suspend_work_queue, &suspend_work);
537 }
538 }
539 spin_unlock_irqrestore(&list_lock, irqflags);
540}
541
542void wake_lock(struct wake_lock *lock)
543{
Abhijeet Dharmapurikar57b34bb2011-09-26 17:10:02 -0700544 /*
545 * if wake lock is being called too late in the suspend sequence,
546 * call bug so we get to analyze the callstack
547 */
548 BUG_ON(msm_suspend_check_done);
549
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700550 wake_lock_internal(lock, 0, 0);
551}
552EXPORT_SYMBOL(wake_lock);
553
554void wake_lock_timeout(struct wake_lock *lock, long timeout)
555{
Abhijeet Dharmapurikar57b34bb2011-09-26 17:10:02 -0700556 /*
557 * if wake lock is being called too late in the suspend sequence,
558 * call bug so we get to analyze the callstack
559 */
560 BUG_ON(msm_suspend_check_done);
561
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700562 wake_lock_internal(lock, timeout, 1);
563}
564EXPORT_SYMBOL(wake_lock_timeout);
565
566void wake_unlock(struct wake_lock *lock)
567{
568 int type;
569 unsigned long irqflags;
570 spin_lock_irqsave(&list_lock, irqflags);
571 type = lock->flags & WAKE_LOCK_TYPE_MASK;
572#ifdef CONFIG_WAKELOCK_STAT
573 wake_unlock_stat_locked(lock, 0);
574#endif
575 if (debug_mask & DEBUG_WAKE_LOCK)
576 pr_info("wake_unlock: %s\n", lock->name);
577 lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
578 list_del(&lock->link);
579 list_add(&lock->link, &inactive_locks);
580 if (type == WAKE_LOCK_SUSPEND) {
581 long has_lock = has_wake_lock_locked(type);
582 if (has_lock > 0) {
583 if (debug_mask & DEBUG_EXPIRE)
584 pr_info("wake_unlock: %s, start expire timer, "
585 "%ld\n", lock->name, has_lock);
586 mod_timer(&expire_timer, jiffies + has_lock);
587 } else {
588 if (del_timer(&expire_timer))
589 if (debug_mask & DEBUG_EXPIRE)
590 pr_info("wake_unlock: %s, stop expire "
591 "timer\n", lock->name);
592 if (has_lock == 0)
593 queue_work(suspend_work_queue, &suspend_work);
594 }
595 if (lock == &main_wake_lock) {
596 if (debug_mask & DEBUG_SUSPEND)
597 print_active_locks(WAKE_LOCK_SUSPEND);
598#ifdef CONFIG_WAKELOCK_STAT
599 update_sleep_wait_stats_locked(0);
600#endif
601 }
602 }
603 spin_unlock_irqrestore(&list_lock, irqflags);
604}
605EXPORT_SYMBOL(wake_unlock);
606
607int wake_lock_active(struct wake_lock *lock)
608{
609 return !!(lock->flags & WAKE_LOCK_ACTIVE);
610}
611EXPORT_SYMBOL(wake_lock_active);
612
Arve Hjønnevåg1b074952009-12-02 18:22:00 -0800613static int wakelock_stats_open(struct inode *inode, struct file *file)
614{
615 return single_open(file, wakelock_stats_show, NULL);
616}
617
618static const struct file_operations wakelock_stats_fops = {
619 .owner = THIS_MODULE,
620 .open = wakelock_stats_open,
621 .read = seq_read,
622 .llseek = seq_lseek,
623 .release = single_release,
624};
625
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700626static int __init wakelocks_init(void)
627{
628 int ret;
629 int i;
630
631 for (i = 0; i < ARRAY_SIZE(active_wake_locks); i++)
632 INIT_LIST_HEAD(&active_wake_locks[i]);
633
634#ifdef CONFIG_WAKELOCK_STAT
635 wake_lock_init(&deleted_wake_locks, WAKE_LOCK_SUSPEND,
636 "deleted_wake_locks");
637#endif
638 wake_lock_init(&main_wake_lock, WAKE_LOCK_SUSPEND, "main");
639 wake_lock(&main_wake_lock);
640 wake_lock_init(&unknown_wakeup, WAKE_LOCK_SUSPEND, "unknown_wakeups");
641
642 ret = platform_device_register(&power_device);
643 if (ret) {
644 pr_err("wakelocks_init: platform_device_register failed\n");
645 goto err_platform_device_register;
646 }
647 ret = platform_driver_register(&power_driver);
648 if (ret) {
649 pr_err("wakelocks_init: platform_driver_register failed\n");
650 goto err_platform_driver_register;
651 }
652
653 suspend_work_queue = create_singlethread_workqueue("suspend");
654 if (suspend_work_queue == NULL) {
655 ret = -ENOMEM;
656 goto err_suspend_work_queue;
657 }
658
659#ifdef CONFIG_WAKELOCK_STAT
Arve Hjønnevåg1b074952009-12-02 18:22:00 -0800660 proc_create("wakelocks", S_IRUGO, NULL, &wakelock_stats_fops);
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -0700661#endif
662
663 return 0;
664
665err_suspend_work_queue:
666 platform_driver_unregister(&power_driver);
667err_platform_driver_register:
668 platform_device_unregister(&power_device);
669err_platform_device_register:
670 wake_lock_destroy(&unknown_wakeup);
671 wake_lock_destroy(&main_wake_lock);
672#ifdef CONFIG_WAKELOCK_STAT
673 wake_lock_destroy(&deleted_wake_locks);
674#endif
675 return ret;
676}
677
678static void __exit wakelocks_exit(void)
679{
680#ifdef CONFIG_WAKELOCK_STAT
681 remove_proc_entry("wakelocks", NULL);
682#endif
683 destroy_workqueue(suspend_work_queue);
684 platform_driver_unregister(&power_driver);
685 platform_device_unregister(&power_device);
686 wake_lock_destroy(&unknown_wakeup);
687 wake_lock_destroy(&main_wake_lock);
688#ifdef CONFIG_WAKELOCK_STAT
689 wake_lock_destroy(&deleted_wake_locks);
690#endif
691}
692
693core_initcall(wakelocks_init);
694module_exit(wakelocks_exit);