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