Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 1 | /* 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 | |
| 27 | enum { |
| 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 | }; |
| 34 | static int debug_mask = DEBUG_EXIT_SUSPEND | DEBUG_WAKEUP; |
| 35 | module_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 | |
| 43 | static DEFINE_SPINLOCK(list_lock); |
| 44 | static LIST_HEAD(inactive_locks); |
| 45 | static struct list_head active_wake_locks[WAKE_LOCK_TYPE_COUNT]; |
| 46 | static int current_event_num; |
Pratik Patel | 529461b | 2011-07-21 19:13:34 -0700 | [diff] [blame] | 47 | static int suspend_sys_sync_count; |
| 48 | static DEFINE_SPINLOCK(suspend_sys_sync_lock); |
| 49 | static struct workqueue_struct *suspend_sys_sync_work_queue; |
| 50 | static DECLARE_COMPLETION(suspend_sys_sync_comp); |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 51 | struct workqueue_struct *suspend_work_queue; |
| 52 | struct wake_lock main_wake_lock; |
| 53 | suspend_state_t requested_suspend_state = PM_SUSPEND_MEM; |
| 54 | static struct wake_lock unknown_wakeup; |
| 55 | |
| 56 | #ifdef CONFIG_WAKELOCK_STAT |
| 57 | static struct wake_lock deleted_wake_locks; |
| 58 | static ktime_t last_sleep_time_update; |
| 59 | static int wait_for_wakeup; |
| 60 | |
| 61 | int get_expired_time(struct wake_lock *lock, ktime_t *expire_time) |
| 62 | { |
| 63 | struct timespec ts; |
| 64 | struct timespec kt; |
| 65 | struct timespec tomono; |
| 66 | struct timespec delta; |
Colin Cross | 28e23cf | 2011-03-30 12:37:49 -0700 | [diff] [blame] | 67 | struct timespec sleep; |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 68 | long timeout; |
| 69 | |
| 70 | if (!(lock->flags & WAKE_LOCK_AUTO_EXPIRE)) |
| 71 | return 0; |
Colin Cross | 28e23cf | 2011-03-30 12:37:49 -0700 | [diff] [blame] | 72 | get_xtime_and_monotonic_and_sleep_offset(&kt, &tomono, &sleep); |
| 73 | timeout = lock->expires - jiffies; |
| 74 | if (timeout > 0) |
| 75 | return 0; |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 76 | jiffies_to_timespec(-timeout, &delta); |
| 77 | set_normalized_timespec(&ts, kt.tv_sec + tomono.tv_sec - delta.tv_sec, |
| 78 | kt.tv_nsec + tomono.tv_nsec - delta.tv_nsec); |
| 79 | *expire_time = timespec_to_ktime(ts); |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | |
Arve Hjønnevåg | 1b07495 | 2009-12-02 18:22:00 -0800 | [diff] [blame] | 84 | static int print_lock_stat(struct seq_file *m, struct wake_lock *lock) |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 85 | { |
| 86 | int lock_count = lock->stat.count; |
| 87 | int expire_count = lock->stat.expire_count; |
| 88 | ktime_t active_time = ktime_set(0, 0); |
| 89 | ktime_t total_time = lock->stat.total_time; |
| 90 | ktime_t max_time = lock->stat.max_time; |
Erik Gilling | 10f0138 | 2009-08-25 20:09:12 -0700 | [diff] [blame] | 91 | |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 92 | ktime_t prevent_suspend_time = lock->stat.prevent_suspend_time; |
| 93 | if (lock->flags & WAKE_LOCK_ACTIVE) { |
| 94 | ktime_t now, add_time; |
| 95 | int expired = get_expired_time(lock, &now); |
| 96 | if (!expired) |
| 97 | now = ktime_get(); |
| 98 | add_time = ktime_sub(now, lock->stat.last_time); |
| 99 | lock_count++; |
| 100 | if (!expired) |
| 101 | active_time = add_time; |
| 102 | else |
| 103 | expire_count++; |
| 104 | total_time = ktime_add(total_time, add_time); |
| 105 | if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) |
| 106 | prevent_suspend_time = ktime_add(prevent_suspend_time, |
| 107 | ktime_sub(now, last_sleep_time_update)); |
| 108 | if (add_time.tv64 > max_time.tv64) |
| 109 | max_time = add_time; |
| 110 | } |
| 111 | |
Arve Hjønnevåg | 1b07495 | 2009-12-02 18:22:00 -0800 | [diff] [blame] | 112 | return seq_printf(m, |
Erik Gilling | 10f0138 | 2009-08-25 20:09:12 -0700 | [diff] [blame] | 113 | "\"%s\"\t%d\t%d\t%d\t%lld\t%lld\t%lld\t%lld\t%lld\n", |
| 114 | lock->name, lock_count, expire_count, |
| 115 | lock->stat.wakeup_count, ktime_to_ns(active_time), |
| 116 | ktime_to_ns(total_time), |
| 117 | ktime_to_ns(prevent_suspend_time), ktime_to_ns(max_time), |
| 118 | ktime_to_ns(lock->stat.last_time)); |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Arve Hjønnevåg | 1b07495 | 2009-12-02 18:22:00 -0800 | [diff] [blame] | 121 | static int wakelock_stats_show(struct seq_file *m, void *unused) |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 122 | { |
| 123 | unsigned long irqflags; |
| 124 | struct wake_lock *lock; |
Arve Hjønnevåg | 1b07495 | 2009-12-02 18:22:00 -0800 | [diff] [blame] | 125 | int ret; |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 126 | int type; |
| 127 | |
| 128 | spin_lock_irqsave(&list_lock, irqflags); |
| 129 | |
Arve Hjønnevåg | 1b07495 | 2009-12-02 18:22:00 -0800 | [diff] [blame] | 130 | ret = seq_puts(m, "name\tcount\texpire_count\twake_count\tactive_since" |
Erik Gilling | 10f0138 | 2009-08-25 20:09:12 -0700 | [diff] [blame] | 131 | "\ttotal_time\tsleep_time\tmax_time\tlast_change\n"); |
Arve Hjønnevåg | 1b07495 | 2009-12-02 18:22:00 -0800 | [diff] [blame] | 132 | list_for_each_entry(lock, &inactive_locks, link) |
| 133 | ret = print_lock_stat(m, lock); |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 134 | for (type = 0; type < WAKE_LOCK_TYPE_COUNT; type++) { |
| 135 | list_for_each_entry(lock, &active_wake_locks[type], link) |
Arve Hjønnevåg | 1b07495 | 2009-12-02 18:22:00 -0800 | [diff] [blame] | 136 | ret = print_lock_stat(m, lock); |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 137 | } |
| 138 | spin_unlock_irqrestore(&list_lock, irqflags); |
Arve Hjønnevåg | 1b07495 | 2009-12-02 18:22:00 -0800 | [diff] [blame] | 139 | return 0; |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void wake_unlock_stat_locked(struct wake_lock *lock, int expired) |
| 143 | { |
| 144 | ktime_t duration; |
| 145 | ktime_t now; |
| 146 | if (!(lock->flags & WAKE_LOCK_ACTIVE)) |
| 147 | return; |
| 148 | if (get_expired_time(lock, &now)) |
| 149 | expired = 1; |
| 150 | else |
| 151 | now = ktime_get(); |
| 152 | lock->stat.count++; |
| 153 | if (expired) |
| 154 | lock->stat.expire_count++; |
| 155 | duration = ktime_sub(now, lock->stat.last_time); |
| 156 | lock->stat.total_time = ktime_add(lock->stat.total_time, duration); |
| 157 | if (ktime_to_ns(duration) > ktime_to_ns(lock->stat.max_time)) |
| 158 | lock->stat.max_time = duration; |
| 159 | lock->stat.last_time = ktime_get(); |
| 160 | if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) { |
| 161 | duration = ktime_sub(now, last_sleep_time_update); |
| 162 | lock->stat.prevent_suspend_time = ktime_add( |
| 163 | lock->stat.prevent_suspend_time, duration); |
| 164 | lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static void update_sleep_wait_stats_locked(int done) |
| 169 | { |
| 170 | struct wake_lock *lock; |
| 171 | ktime_t now, etime, elapsed, add; |
| 172 | int expired; |
| 173 | |
| 174 | now = ktime_get(); |
| 175 | elapsed = ktime_sub(now, last_sleep_time_update); |
| 176 | list_for_each_entry(lock, &active_wake_locks[WAKE_LOCK_SUSPEND], link) { |
| 177 | expired = get_expired_time(lock, &etime); |
| 178 | if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) { |
| 179 | if (expired) |
| 180 | add = ktime_sub(etime, last_sleep_time_update); |
| 181 | else |
| 182 | add = elapsed; |
| 183 | lock->stat.prevent_suspend_time = ktime_add( |
| 184 | lock->stat.prevent_suspend_time, add); |
| 185 | } |
| 186 | if (done || expired) |
| 187 | lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND; |
| 188 | else |
| 189 | lock->flags |= WAKE_LOCK_PREVENTING_SUSPEND; |
| 190 | } |
| 191 | last_sleep_time_update = now; |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | |
| 196 | static void expire_wake_lock(struct wake_lock *lock) |
| 197 | { |
| 198 | #ifdef CONFIG_WAKELOCK_STAT |
| 199 | wake_unlock_stat_locked(lock, 1); |
| 200 | #endif |
| 201 | lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE); |
| 202 | list_del(&lock->link); |
| 203 | list_add(&lock->link, &inactive_locks); |
| 204 | if (debug_mask & (DEBUG_WAKE_LOCK | DEBUG_EXPIRE)) |
| 205 | pr_info("expired wake lock %s\n", lock->name); |
| 206 | } |
| 207 | |
Mike Chan | 97a0a74 | 2009-08-25 18:10:32 -0700 | [diff] [blame] | 208 | /* Caller must acquire the list_lock spinlock */ |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 209 | static void print_active_locks(int type) |
| 210 | { |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 211 | struct wake_lock *lock; |
Mike Chan | af62b25 | 2010-02-16 14:18:55 -0800 | [diff] [blame] | 212 | bool print_expired = true; |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 213 | |
| 214 | BUG_ON(type >= WAKE_LOCK_TYPE_COUNT); |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 215 | list_for_each_entry(lock, &active_wake_locks[type], link) { |
| 216 | if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) { |
| 217 | long timeout = lock->expires - jiffies; |
Mike Chan | af62b25 | 2010-02-16 14:18:55 -0800 | [diff] [blame] | 218 | if (timeout > 0) |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 219 | pr_info("active wake lock %s, time left %ld\n", |
| 220 | lock->name, timeout); |
Mike Chan | af62b25 | 2010-02-16 14:18:55 -0800 | [diff] [blame] | 221 | else if (print_expired) |
| 222 | pr_info("wake lock %s, expired\n", lock->name); |
| 223 | } else { |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 224 | pr_info("active wake lock %s\n", lock->name); |
Colin Cross | 0c7841c | 2010-08-21 17:27:02 -0700 | [diff] [blame] | 225 | if (!(debug_mask & DEBUG_EXPIRE)) |
Mike Chan | af62b25 | 2010-02-16 14:18:55 -0800 | [diff] [blame] | 226 | print_expired = false; |
| 227 | } |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 228 | } |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static long has_wake_lock_locked(int type) |
| 232 | { |
| 233 | struct wake_lock *lock, *n; |
| 234 | long max_timeout = 0; |
| 235 | |
| 236 | BUG_ON(type >= WAKE_LOCK_TYPE_COUNT); |
| 237 | list_for_each_entry_safe(lock, n, &active_wake_locks[type], link) { |
| 238 | if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) { |
| 239 | long timeout = lock->expires - jiffies; |
| 240 | if (timeout <= 0) |
| 241 | expire_wake_lock(lock); |
| 242 | else if (timeout > max_timeout) |
| 243 | max_timeout = timeout; |
| 244 | } else |
| 245 | return -1; |
| 246 | } |
| 247 | return max_timeout; |
| 248 | } |
| 249 | |
| 250 | long has_wake_lock(int type) |
| 251 | { |
| 252 | long ret; |
| 253 | unsigned long irqflags; |
| 254 | spin_lock_irqsave(&list_lock, irqflags); |
| 255 | ret = has_wake_lock_locked(type); |
Todd Poynor | ca64b0c | 2011-08-08 16:06:54 -0700 | [diff] [blame] | 256 | if (ret && (debug_mask & DEBUG_WAKEUP) && type == WAKE_LOCK_SUSPEND) |
Mike Chan | af62b25 | 2010-02-16 14:18:55 -0800 | [diff] [blame] | 257 | print_active_locks(type); |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 258 | spin_unlock_irqrestore(&list_lock, irqflags); |
| 259 | return ret; |
| 260 | } |
| 261 | |
Pratik Patel | 529461b | 2011-07-21 19:13:34 -0700 | [diff] [blame] | 262 | static void suspend_sys_sync(struct work_struct *work) |
| 263 | { |
| 264 | if (debug_mask & DEBUG_SUSPEND) |
| 265 | pr_info("PM: Syncing filesystems...\n"); |
| 266 | |
| 267 | sys_sync(); |
| 268 | |
| 269 | if (debug_mask & DEBUG_SUSPEND) |
| 270 | pr_info("sync done.\n"); |
| 271 | |
| 272 | spin_lock(&suspend_sys_sync_lock); |
| 273 | suspend_sys_sync_count--; |
| 274 | spin_unlock(&suspend_sys_sync_lock); |
| 275 | } |
| 276 | static DECLARE_WORK(suspend_sys_sync_work, suspend_sys_sync); |
| 277 | |
| 278 | void suspend_sys_sync_queue(void) |
| 279 | { |
| 280 | int ret; |
| 281 | |
| 282 | spin_lock(&suspend_sys_sync_lock); |
| 283 | ret = queue_work(suspend_sys_sync_work_queue, &suspend_sys_sync_work); |
| 284 | if (ret) |
| 285 | suspend_sys_sync_count++; |
| 286 | spin_unlock(&suspend_sys_sync_lock); |
| 287 | } |
| 288 | |
| 289 | static bool suspend_sys_sync_abort; |
| 290 | static void suspend_sys_sync_handler(unsigned long); |
| 291 | static DEFINE_TIMER(suspend_sys_sync_timer, suspend_sys_sync_handler, 0, 0); |
| 292 | /* value should be less then half of input event wake lock timeout value |
| 293 | * which is currently set to 5*HZ (see drivers/input/evdev.c) |
| 294 | */ |
| 295 | #define SUSPEND_SYS_SYNC_TIMEOUT (HZ/4) |
| 296 | static void suspend_sys_sync_handler(unsigned long arg) |
| 297 | { |
| 298 | if (suspend_sys_sync_count == 0) { |
| 299 | complete(&suspend_sys_sync_comp); |
| 300 | } else if (has_wake_lock(WAKE_LOCK_SUSPEND)) { |
| 301 | suspend_sys_sync_abort = true; |
| 302 | complete(&suspend_sys_sync_comp); |
| 303 | } else { |
| 304 | mod_timer(&suspend_sys_sync_timer, jiffies + |
| 305 | SUSPEND_SYS_SYNC_TIMEOUT); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | int suspend_sys_sync_wait(void) |
| 310 | { |
| 311 | suspend_sys_sync_abort = false; |
| 312 | |
| 313 | if (suspend_sys_sync_count != 0) { |
| 314 | mod_timer(&suspend_sys_sync_timer, jiffies + |
| 315 | SUSPEND_SYS_SYNC_TIMEOUT); |
| 316 | wait_for_completion(&suspend_sys_sync_comp); |
| 317 | } |
| 318 | if (suspend_sys_sync_abort) { |
| 319 | pr_info("suspend aborted....while waiting for sys_sync\n"); |
| 320 | return -EAGAIN; |
| 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 326 | static void suspend(struct work_struct *work) |
| 327 | { |
| 328 | int ret; |
| 329 | int entry_event_num; |
| 330 | |
| 331 | if (has_wake_lock(WAKE_LOCK_SUSPEND)) { |
| 332 | if (debug_mask & DEBUG_SUSPEND) |
| 333 | pr_info("suspend: abort suspend\n"); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | entry_event_num = current_event_num; |
| 338 | sys_sync(); |
| 339 | if (debug_mask & DEBUG_SUSPEND) |
| 340 | pr_info("suspend: enter suspend\n"); |
| 341 | ret = pm_suspend(requested_suspend_state); |
| 342 | if (debug_mask & DEBUG_EXIT_SUSPEND) { |
| 343 | struct timespec ts; |
| 344 | struct rtc_time tm; |
| 345 | getnstimeofday(&ts); |
| 346 | rtc_time_to_tm(ts.tv_sec, &tm); |
| 347 | pr_info("suspend: exit suspend, ret = %d " |
| 348 | "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", ret, |
| 349 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 350 | tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec); |
| 351 | } |
| 352 | if (current_event_num == entry_event_num) { |
| 353 | if (debug_mask & DEBUG_SUSPEND) |
| 354 | pr_info("suspend: pm_suspend returned with no event\n"); |
| 355 | wake_lock_timeout(&unknown_wakeup, HZ / 2); |
| 356 | } |
| 357 | } |
| 358 | static DECLARE_WORK(suspend_work, suspend); |
| 359 | |
| 360 | static void expire_wake_locks(unsigned long data) |
| 361 | { |
| 362 | long has_lock; |
| 363 | unsigned long irqflags; |
| 364 | if (debug_mask & DEBUG_EXPIRE) |
| 365 | pr_info("expire_wake_locks: start\n"); |
Mike Chan | 97a0a74 | 2009-08-25 18:10:32 -0700 | [diff] [blame] | 366 | spin_lock_irqsave(&list_lock, irqflags); |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 367 | if (debug_mask & DEBUG_SUSPEND) |
| 368 | print_active_locks(WAKE_LOCK_SUSPEND); |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 369 | has_lock = has_wake_lock_locked(WAKE_LOCK_SUSPEND); |
| 370 | if (debug_mask & DEBUG_EXPIRE) |
| 371 | pr_info("expire_wake_locks: done, has_lock %ld\n", has_lock); |
| 372 | if (has_lock == 0) |
| 373 | queue_work(suspend_work_queue, &suspend_work); |
| 374 | spin_unlock_irqrestore(&list_lock, irqflags); |
| 375 | } |
| 376 | static DEFINE_TIMER(expire_timer, expire_wake_locks, 0, 0); |
| 377 | |
| 378 | static int power_suspend_late(struct device *dev) |
| 379 | { |
| 380 | int ret = has_wake_lock(WAKE_LOCK_SUSPEND) ? -EAGAIN : 0; |
| 381 | #ifdef CONFIG_WAKELOCK_STAT |
Todd Poynor | ed27e53 | 2011-08-08 17:26:49 -0700 | [diff] [blame] | 382 | wait_for_wakeup = !ret; |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 383 | #endif |
| 384 | if (debug_mask & DEBUG_SUSPEND) |
| 385 | pr_info("power_suspend_late return %d\n", ret); |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | static struct dev_pm_ops power_driver_pm_ops = { |
| 390 | .suspend_noirq = power_suspend_late, |
| 391 | }; |
| 392 | |
| 393 | static struct platform_driver power_driver = { |
| 394 | .driver.name = "power", |
| 395 | .driver.pm = &power_driver_pm_ops, |
| 396 | }; |
| 397 | static struct platform_device power_device = { |
| 398 | .name = "power", |
| 399 | }; |
| 400 | |
| 401 | void wake_lock_init(struct wake_lock *lock, int type, const char *name) |
| 402 | { |
| 403 | unsigned long irqflags = 0; |
| 404 | |
| 405 | if (name) |
| 406 | lock->name = name; |
| 407 | BUG_ON(!lock->name); |
| 408 | |
| 409 | if (debug_mask & DEBUG_WAKE_LOCK) |
| 410 | pr_info("wake_lock_init name=%s\n", lock->name); |
| 411 | #ifdef CONFIG_WAKELOCK_STAT |
| 412 | lock->stat.count = 0; |
| 413 | lock->stat.expire_count = 0; |
| 414 | lock->stat.wakeup_count = 0; |
| 415 | lock->stat.total_time = ktime_set(0, 0); |
| 416 | lock->stat.prevent_suspend_time = ktime_set(0, 0); |
| 417 | lock->stat.max_time = ktime_set(0, 0); |
| 418 | lock->stat.last_time = ktime_set(0, 0); |
| 419 | #endif |
| 420 | lock->flags = (type & WAKE_LOCK_TYPE_MASK) | WAKE_LOCK_INITIALIZED; |
| 421 | |
| 422 | INIT_LIST_HEAD(&lock->link); |
| 423 | spin_lock_irqsave(&list_lock, irqflags); |
| 424 | list_add(&lock->link, &inactive_locks); |
| 425 | spin_unlock_irqrestore(&list_lock, irqflags); |
| 426 | } |
| 427 | EXPORT_SYMBOL(wake_lock_init); |
| 428 | |
| 429 | void wake_lock_destroy(struct wake_lock *lock) |
| 430 | { |
| 431 | unsigned long irqflags; |
| 432 | if (debug_mask & DEBUG_WAKE_LOCK) |
| 433 | pr_info("wake_lock_destroy name=%s\n", lock->name); |
| 434 | spin_lock_irqsave(&list_lock, irqflags); |
| 435 | lock->flags &= ~WAKE_LOCK_INITIALIZED; |
| 436 | #ifdef CONFIG_WAKELOCK_STAT |
| 437 | if (lock->stat.count) { |
| 438 | deleted_wake_locks.stat.count += lock->stat.count; |
| 439 | deleted_wake_locks.stat.expire_count += lock->stat.expire_count; |
| 440 | deleted_wake_locks.stat.total_time = |
| 441 | ktime_add(deleted_wake_locks.stat.total_time, |
| 442 | lock->stat.total_time); |
| 443 | deleted_wake_locks.stat.prevent_suspend_time = |
| 444 | ktime_add(deleted_wake_locks.stat.prevent_suspend_time, |
| 445 | lock->stat.prevent_suspend_time); |
| 446 | deleted_wake_locks.stat.max_time = |
| 447 | ktime_add(deleted_wake_locks.stat.max_time, |
| 448 | lock->stat.max_time); |
| 449 | } |
| 450 | #endif |
| 451 | list_del(&lock->link); |
| 452 | spin_unlock_irqrestore(&list_lock, irqflags); |
| 453 | } |
| 454 | EXPORT_SYMBOL(wake_lock_destroy); |
| 455 | |
| 456 | static void wake_lock_internal( |
| 457 | struct wake_lock *lock, long timeout, int has_timeout) |
| 458 | { |
| 459 | int type; |
| 460 | unsigned long irqflags; |
| 461 | long expire_in; |
| 462 | |
| 463 | spin_lock_irqsave(&list_lock, irqflags); |
| 464 | type = lock->flags & WAKE_LOCK_TYPE_MASK; |
| 465 | BUG_ON(type >= WAKE_LOCK_TYPE_COUNT); |
| 466 | BUG_ON(!(lock->flags & WAKE_LOCK_INITIALIZED)); |
| 467 | #ifdef CONFIG_WAKELOCK_STAT |
| 468 | if (type == WAKE_LOCK_SUSPEND && wait_for_wakeup) { |
| 469 | if (debug_mask & DEBUG_WAKEUP) |
| 470 | pr_info("wakeup wake lock: %s\n", lock->name); |
| 471 | wait_for_wakeup = 0; |
| 472 | lock->stat.wakeup_count++; |
| 473 | } |
| 474 | if ((lock->flags & WAKE_LOCK_AUTO_EXPIRE) && |
| 475 | (long)(lock->expires - jiffies) <= 0) { |
| 476 | wake_unlock_stat_locked(lock, 0); |
| 477 | lock->stat.last_time = ktime_get(); |
| 478 | } |
| 479 | #endif |
| 480 | if (!(lock->flags & WAKE_LOCK_ACTIVE)) { |
| 481 | lock->flags |= WAKE_LOCK_ACTIVE; |
| 482 | #ifdef CONFIG_WAKELOCK_STAT |
| 483 | lock->stat.last_time = ktime_get(); |
| 484 | #endif |
| 485 | } |
| 486 | list_del(&lock->link); |
| 487 | if (has_timeout) { |
| 488 | if (debug_mask & DEBUG_WAKE_LOCK) |
| 489 | pr_info("wake_lock: %s, type %d, timeout %ld.%03lu\n", |
| 490 | lock->name, type, timeout / HZ, |
| 491 | (timeout % HZ) * MSEC_PER_SEC / HZ); |
| 492 | lock->expires = jiffies + timeout; |
| 493 | lock->flags |= WAKE_LOCK_AUTO_EXPIRE; |
| 494 | list_add_tail(&lock->link, &active_wake_locks[type]); |
| 495 | } else { |
| 496 | if (debug_mask & DEBUG_WAKE_LOCK) |
| 497 | pr_info("wake_lock: %s, type %d\n", lock->name, type); |
| 498 | lock->expires = LONG_MAX; |
| 499 | lock->flags &= ~WAKE_LOCK_AUTO_EXPIRE; |
| 500 | list_add(&lock->link, &active_wake_locks[type]); |
| 501 | } |
| 502 | if (type == WAKE_LOCK_SUSPEND) { |
| 503 | current_event_num++; |
| 504 | #ifdef CONFIG_WAKELOCK_STAT |
| 505 | if (lock == &main_wake_lock) |
| 506 | update_sleep_wait_stats_locked(1); |
| 507 | else if (!wake_lock_active(&main_wake_lock)) |
| 508 | update_sleep_wait_stats_locked(0); |
| 509 | #endif |
| 510 | if (has_timeout) |
| 511 | expire_in = has_wake_lock_locked(type); |
| 512 | else |
| 513 | expire_in = -1; |
| 514 | if (expire_in > 0) { |
| 515 | if (debug_mask & DEBUG_EXPIRE) |
| 516 | pr_info("wake_lock: %s, start expire timer, " |
| 517 | "%ld\n", lock->name, expire_in); |
| 518 | mod_timer(&expire_timer, jiffies + expire_in); |
| 519 | } else { |
| 520 | if (del_timer(&expire_timer)) |
| 521 | if (debug_mask & DEBUG_EXPIRE) |
| 522 | pr_info("wake_lock: %s, stop expire timer\n", |
| 523 | lock->name); |
| 524 | if (expire_in == 0) |
| 525 | queue_work(suspend_work_queue, &suspend_work); |
| 526 | } |
| 527 | } |
| 528 | spin_unlock_irqrestore(&list_lock, irqflags); |
| 529 | } |
| 530 | |
| 531 | void wake_lock(struct wake_lock *lock) |
| 532 | { |
| 533 | wake_lock_internal(lock, 0, 0); |
| 534 | } |
| 535 | EXPORT_SYMBOL(wake_lock); |
| 536 | |
| 537 | void wake_lock_timeout(struct wake_lock *lock, long timeout) |
| 538 | { |
| 539 | wake_lock_internal(lock, timeout, 1); |
| 540 | } |
| 541 | EXPORT_SYMBOL(wake_lock_timeout); |
| 542 | |
| 543 | void wake_unlock(struct wake_lock *lock) |
| 544 | { |
| 545 | int type; |
| 546 | unsigned long irqflags; |
| 547 | spin_lock_irqsave(&list_lock, irqflags); |
| 548 | type = lock->flags & WAKE_LOCK_TYPE_MASK; |
| 549 | #ifdef CONFIG_WAKELOCK_STAT |
| 550 | wake_unlock_stat_locked(lock, 0); |
| 551 | #endif |
| 552 | if (debug_mask & DEBUG_WAKE_LOCK) |
| 553 | pr_info("wake_unlock: %s\n", lock->name); |
| 554 | lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE); |
| 555 | list_del(&lock->link); |
| 556 | list_add(&lock->link, &inactive_locks); |
| 557 | if (type == WAKE_LOCK_SUSPEND) { |
| 558 | long has_lock = has_wake_lock_locked(type); |
| 559 | if (has_lock > 0) { |
| 560 | if (debug_mask & DEBUG_EXPIRE) |
| 561 | pr_info("wake_unlock: %s, start expire timer, " |
| 562 | "%ld\n", lock->name, has_lock); |
| 563 | mod_timer(&expire_timer, jiffies + has_lock); |
| 564 | } else { |
| 565 | if (del_timer(&expire_timer)) |
| 566 | if (debug_mask & DEBUG_EXPIRE) |
| 567 | pr_info("wake_unlock: %s, stop expire " |
| 568 | "timer\n", lock->name); |
| 569 | if (has_lock == 0) |
| 570 | queue_work(suspend_work_queue, &suspend_work); |
| 571 | } |
| 572 | if (lock == &main_wake_lock) { |
| 573 | if (debug_mask & DEBUG_SUSPEND) |
| 574 | print_active_locks(WAKE_LOCK_SUSPEND); |
| 575 | #ifdef CONFIG_WAKELOCK_STAT |
| 576 | update_sleep_wait_stats_locked(0); |
| 577 | #endif |
| 578 | } |
| 579 | } |
| 580 | spin_unlock_irqrestore(&list_lock, irqflags); |
| 581 | } |
| 582 | EXPORT_SYMBOL(wake_unlock); |
| 583 | |
| 584 | int wake_lock_active(struct wake_lock *lock) |
| 585 | { |
| 586 | return !!(lock->flags & WAKE_LOCK_ACTIVE); |
| 587 | } |
| 588 | EXPORT_SYMBOL(wake_lock_active); |
| 589 | |
Arve Hjønnevåg | 1b07495 | 2009-12-02 18:22:00 -0800 | [diff] [blame] | 590 | static int wakelock_stats_open(struct inode *inode, struct file *file) |
| 591 | { |
| 592 | return single_open(file, wakelock_stats_show, NULL); |
| 593 | } |
| 594 | |
| 595 | static const struct file_operations wakelock_stats_fops = { |
| 596 | .owner = THIS_MODULE, |
| 597 | .open = wakelock_stats_open, |
| 598 | .read = seq_read, |
| 599 | .llseek = seq_lseek, |
| 600 | .release = single_release, |
| 601 | }; |
| 602 | |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 603 | static int __init wakelocks_init(void) |
| 604 | { |
| 605 | int ret; |
| 606 | int i; |
| 607 | |
| 608 | for (i = 0; i < ARRAY_SIZE(active_wake_locks); i++) |
| 609 | INIT_LIST_HEAD(&active_wake_locks[i]); |
| 610 | |
| 611 | #ifdef CONFIG_WAKELOCK_STAT |
| 612 | wake_lock_init(&deleted_wake_locks, WAKE_LOCK_SUSPEND, |
| 613 | "deleted_wake_locks"); |
| 614 | #endif |
| 615 | wake_lock_init(&main_wake_lock, WAKE_LOCK_SUSPEND, "main"); |
| 616 | wake_lock(&main_wake_lock); |
| 617 | wake_lock_init(&unknown_wakeup, WAKE_LOCK_SUSPEND, "unknown_wakeups"); |
| 618 | |
| 619 | ret = platform_device_register(&power_device); |
| 620 | if (ret) { |
| 621 | pr_err("wakelocks_init: platform_device_register failed\n"); |
| 622 | goto err_platform_device_register; |
| 623 | } |
| 624 | ret = platform_driver_register(&power_driver); |
| 625 | if (ret) { |
| 626 | pr_err("wakelocks_init: platform_driver_register failed\n"); |
| 627 | goto err_platform_driver_register; |
| 628 | } |
| 629 | |
| 630 | suspend_work_queue = create_singlethread_workqueue("suspend"); |
| 631 | if (suspend_work_queue == NULL) { |
| 632 | ret = -ENOMEM; |
| 633 | goto err_suspend_work_queue; |
| 634 | } |
| 635 | |
| 636 | #ifdef CONFIG_WAKELOCK_STAT |
Arve Hjønnevåg | 1b07495 | 2009-12-02 18:22:00 -0800 | [diff] [blame] | 637 | proc_create("wakelocks", S_IRUGO, NULL, &wakelock_stats_fops); |
Arve Hjønnevåg | fe6cd63 | 2008-09-09 22:14:34 -0700 | [diff] [blame] | 638 | #endif |
| 639 | |
| 640 | return 0; |
| 641 | |
| 642 | err_suspend_work_queue: |
| 643 | platform_driver_unregister(&power_driver); |
| 644 | err_platform_driver_register: |
| 645 | platform_device_unregister(&power_device); |
| 646 | err_platform_device_register: |
| 647 | wake_lock_destroy(&unknown_wakeup); |
| 648 | wake_lock_destroy(&main_wake_lock); |
| 649 | #ifdef CONFIG_WAKELOCK_STAT |
| 650 | wake_lock_destroy(&deleted_wake_locks); |
| 651 | #endif |
| 652 | return ret; |
| 653 | } |
| 654 | |
| 655 | static void __exit wakelocks_exit(void) |
| 656 | { |
| 657 | #ifdef CONFIG_WAKELOCK_STAT |
| 658 | remove_proc_entry("wakelocks", NULL); |
| 659 | #endif |
| 660 | destroy_workqueue(suspend_work_queue); |
| 661 | platform_driver_unregister(&power_driver); |
| 662 | platform_device_unregister(&power_device); |
| 663 | wake_lock_destroy(&unknown_wakeup); |
| 664 | wake_lock_destroy(&main_wake_lock); |
| 665 | #ifdef CONFIG_WAKELOCK_STAT |
| 666 | wake_lock_destroy(&deleted_wake_locks); |
| 667 | #endif |
| 668 | } |
| 669 | |
| 670 | core_initcall(wakelocks_init); |
| 671 | module_exit(wakelocks_exit); |