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