| 1 | From eef7f012d480142a3eccff91e0ae73ecb13220b3 Mon Sep 17 00:00:00 2001 |
| 2 | From: =?utf-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?= <arve@android.com> |
| 3 | Date: Tue, 9 Sep 2008 22:14:34 -0700 |
| 4 | Subject: [PATCH 066/134] PM: Implement wakelock api. |
| 5 | MIME-Version: 1.0 |
| 6 | Content-Type: text/plain; charset=utf-8 |
| 7 | Content-Transfer-Encoding: 8bit |
| 8 | |
| 9 | PM: wakelock: Replace expire work with a timer |
| 10 | |
| 11 | The expire work function did not work in the normal case. |
| 12 | |
| 13 | Signed-off-by: Arve Hjønnevåg <arve@android.com> |
| 14 | --- |
| 15 | kernel/power/Kconfig | 19 ++ |
| 16 | kernel/power/Makefile | 1 + |
| 17 | kernel/power/power.h | 7 + |
| 18 | kernel/power/wakelock.c | 598 +++++++++++++++++++++++++++++++++++++++++++++++ |
| 19 | 4 files changed, 625 insertions(+), 0 deletions(-) |
| 20 | create mode 100644 kernel/power/wakelock.c |
| 21 | |
| 22 | --- a/kernel/power/Kconfig |
| 23 | +++ b/kernel/power/Kconfig |
| 24 | @@ -116,6 +116,25 @@ config SUSPEND_FREEZER |
| 25 | |
| 26 | Turning OFF this setting is NOT recommended! If in doubt, say Y. |
| 27 | |
| 28 | +config HAS_WAKELOCK |
| 29 | + bool |
| 30 | + |
| 31 | +config WAKELOCK |
| 32 | + bool "Wake lock" |
| 33 | + depends on PM && RTC_CLASS |
| 34 | + default n |
| 35 | + select HAS_WAKELOCK |
| 36 | + ---help--- |
| 37 | + Enable wakelocks. When user space request a sleep state the |
| 38 | + sleep request will be delayed until no wake locks are held. |
| 39 | + |
| 40 | +config WAKELOCK_STAT |
| 41 | + bool "Wake lock stats" |
| 42 | + depends on WAKELOCK |
| 43 | + default y |
| 44 | + ---help--- |
| 45 | + Report wake lock stats in /proc/wakelocks |
| 46 | + |
| 47 | config HIBERNATION |
| 48 | bool "Hibernation (aka 'suspend to disk')" |
| 49 | depends on PM && SWAP && ARCH_HIBERNATION_POSSIBLE |
| 50 | --- a/kernel/power/Makefile |
| 51 | +++ b/kernel/power/Makefile |
| 52 | @@ -6,6 +6,7 @@ endif |
| 53 | obj-$(CONFIG_PM) += main.o |
| 54 | obj-$(CONFIG_PM_SLEEP) += console.o |
| 55 | obj-$(CONFIG_FREEZER) += process.o |
| 56 | +obj-$(CONFIG_WAKELOCK) += wakelock.o |
| 57 | obj-$(CONFIG_HIBERNATION) += swsusp.o disk.o snapshot.o swap.o user.o |
| 58 | |
| 59 | obj-$(CONFIG_MAGIC_SYSRQ) += poweroff.o |
| 60 | --- a/kernel/power/power.h |
| 61 | +++ b/kernel/power/power.h |
| 62 | @@ -223,3 +223,10 @@ static inline void suspend_thaw_processe |
| 63 | { |
| 64 | } |
| 65 | #endif |
| 66 | + |
| 67 | +#ifdef CONFIG_WAKELOCK |
| 68 | +/* kernel/power/wakelock.c */ |
| 69 | +extern struct workqueue_struct *suspend_work_queue; |
| 70 | +extern struct wake_lock main_wake_lock; |
| 71 | +extern suspend_state_t requested_suspend_state; |
| 72 | +#endif |
| 73 | --- /dev/null |
| 74 | +++ b/kernel/power/wakelock.c |
| 75 | @@ -0,0 +1,598 @@ |
| 76 | +/* kernel/power/wakelock.c |
| 77 | + * |
| 78 | + * Copyright (C) 2005-2008 Google, Inc. |
| 79 | + * |
| 80 | + * This software is licensed under the terms of the GNU General Public |
| 81 | + * License version 2, as published by the Free Software Foundation, and |
| 82 | + * may be copied, distributed, and modified under those terms. |
| 83 | + * |
| 84 | + * This program is distributed in the hope that it will be useful, |
| 85 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 86 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 87 | + * GNU General Public License for more details. |
| 88 | + * |
| 89 | + */ |
| 90 | + |
| 91 | +#include <linux/module.h> |
| 92 | +#include <linux/platform_device.h> |
| 93 | +#include <linux/rtc.h> |
| 94 | +#include <linux/suspend.h> |
| 95 | +#include <linux/syscalls.h> /* sys_sync */ |
| 96 | +#include <linux/wakelock.h> |
| 97 | +#ifdef CONFIG_WAKELOCK_STAT |
| 98 | +#include <linux/proc_fs.h> |
| 99 | +#endif |
| 100 | +#include "power.h" |
| 101 | + |
| 102 | +enum { |
| 103 | + DEBUG_EXIT_SUSPEND = 1U << 0, |
| 104 | + DEBUG_WAKEUP = 1U << 1, |
| 105 | + DEBUG_SUSPEND = 1U << 2, |
| 106 | + DEBUG_EXPIRE = 1U << 3, |
| 107 | + DEBUG_WAKE_LOCK = 1U << 4, |
| 108 | +}; |
| 109 | +static int debug_mask = DEBUG_EXIT_SUSPEND | DEBUG_WAKEUP; |
| 110 | +module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 111 | + |
| 112 | +#define WAKE_LOCK_TYPE_MASK (0x0f) |
| 113 | +#define WAKE_LOCK_INITIALIZED (1U << 8) |
| 114 | +#define WAKE_LOCK_ACTIVE (1U << 9) |
| 115 | +#define WAKE_LOCK_AUTO_EXPIRE (1U << 10) |
| 116 | +#define WAKE_LOCK_PREVENTING_SUSPEND (1U << 11) |
| 117 | + |
| 118 | +static DEFINE_SPINLOCK(list_lock); |
| 119 | +static LIST_HEAD(inactive_locks); |
| 120 | +static struct list_head active_wake_locks[WAKE_LOCK_TYPE_COUNT]; |
| 121 | +static int current_event_num; |
| 122 | +struct workqueue_struct *suspend_work_queue; |
| 123 | +struct wake_lock main_wake_lock; |
| 124 | +suspend_state_t requested_suspend_state = PM_SUSPEND_MEM; |
| 125 | +static struct wake_lock unknown_wakeup; |
| 126 | + |
| 127 | +#ifdef CONFIG_WAKELOCK_STAT |
| 128 | +static struct wake_lock deleted_wake_locks; |
| 129 | +static ktime_t last_sleep_time_update; |
| 130 | +static int wait_for_wakeup; |
| 131 | + |
| 132 | +int get_expired_time(struct wake_lock *lock, ktime_t *expire_time) |
| 133 | +{ |
| 134 | + struct timespec ts; |
| 135 | + struct timespec kt; |
| 136 | + struct timespec tomono; |
| 137 | + struct timespec delta; |
| 138 | + unsigned long seq; |
| 139 | + long timeout; |
| 140 | + |
| 141 | + if (!(lock->flags & WAKE_LOCK_AUTO_EXPIRE)) |
| 142 | + return 0; |
| 143 | + do { |
| 144 | + seq = read_seqbegin(&xtime_lock); |
| 145 | + timeout = lock->expires - jiffies; |
| 146 | + if (timeout > 0) |
| 147 | + return 0; |
| 148 | + kt = current_kernel_time(); |
| 149 | + tomono = wall_to_monotonic; |
| 150 | + } while (read_seqretry(&xtime_lock, seq)); |
| 151 | + jiffies_to_timespec(-timeout, &delta); |
| 152 | + set_normalized_timespec(&ts, kt.tv_sec + tomono.tv_sec - delta.tv_sec, |
| 153 | + kt.tv_nsec + tomono.tv_nsec - delta.tv_nsec); |
| 154 | + *expire_time = timespec_to_ktime(ts); |
| 155 | + return 1; |
| 156 | +} |
| 157 | + |
| 158 | + |
| 159 | +static int print_lock_stat(char *buf, struct wake_lock *lock) |
| 160 | +{ |
| 161 | + int lock_count = lock->stat.count; |
| 162 | + int expire_count = lock->stat.expire_count; |
| 163 | + ktime_t active_time = ktime_set(0, 0); |
| 164 | + ktime_t total_time = lock->stat.total_time; |
| 165 | + ktime_t max_time = lock->stat.max_time; |
| 166 | + ktime_t prevent_suspend_time = lock->stat.prevent_suspend_time; |
| 167 | + if (lock->flags & WAKE_LOCK_ACTIVE) { |
| 168 | + ktime_t now, add_time; |
| 169 | + int expired = get_expired_time(lock, &now); |
| 170 | + if (!expired) |
| 171 | + now = ktime_get(); |
| 172 | + add_time = ktime_sub(now, lock->stat.last_time); |
| 173 | + lock_count++; |
| 174 | + if (!expired) |
| 175 | + active_time = add_time; |
| 176 | + else |
| 177 | + expire_count++; |
| 178 | + total_time = ktime_add(total_time, add_time); |
| 179 | + if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) |
| 180 | + prevent_suspend_time = ktime_add(prevent_suspend_time, |
| 181 | + ktime_sub(now, last_sleep_time_update)); |
| 182 | + if (add_time.tv64 > max_time.tv64) |
| 183 | + max_time = add_time; |
| 184 | + } |
| 185 | + |
| 186 | + return sprintf(buf, "\"%s\"\t%d\t%d\t%d\t%lld\t%lld\t%lld\t%lld\t" |
| 187 | + "%lld\n", lock->name, lock_count, expire_count, |
| 188 | + lock->stat.wakeup_count, ktime_to_ns(active_time), |
| 189 | + ktime_to_ns(total_time), |
| 190 | + ktime_to_ns(prevent_suspend_time), ktime_to_ns(max_time), |
| 191 | + ktime_to_ns(lock->stat.last_time)); |
| 192 | +} |
| 193 | + |
| 194 | + |
| 195 | +static int wakelocks_read_proc(char *page, char **start, off_t off, |
| 196 | + int count, int *eof, void *data) |
| 197 | +{ |
| 198 | + unsigned long irqflags; |
| 199 | + struct wake_lock *lock; |
| 200 | + int len = 0; |
| 201 | + char *p = page; |
| 202 | + int type; |
| 203 | + |
| 204 | + spin_lock_irqsave(&list_lock, irqflags); |
| 205 | + |
| 206 | + p += sprintf(p, "name\tcount\texpire_count\twake_count\tactive_since" |
| 207 | + "\ttotal_time\tsleep_time\tmax_time\tlast_change\n"); |
| 208 | + list_for_each_entry(lock, &inactive_locks, link) { |
| 209 | + p += print_lock_stat(p, lock); |
| 210 | + } |
| 211 | + for (type = 0; type < WAKE_LOCK_TYPE_COUNT; type++) { |
| 212 | + list_for_each_entry(lock, &active_wake_locks[type], link) |
| 213 | + p += print_lock_stat(p, lock); |
| 214 | + } |
| 215 | + spin_unlock_irqrestore(&list_lock, irqflags); |
| 216 | + |
| 217 | + *start = page + off; |
| 218 | + |
| 219 | + len = p - page; |
| 220 | + if (len > off) |
| 221 | + len -= off; |
| 222 | + else |
| 223 | + len = 0; |
| 224 | + |
| 225 | + return len < count ? len : count; |
| 226 | +} |
| 227 | + |
| 228 | +static void wake_unlock_stat_locked(struct wake_lock *lock, int expired) |
| 229 | +{ |
| 230 | + ktime_t duration; |
| 231 | + ktime_t now; |
| 232 | + if (!(lock->flags & WAKE_LOCK_ACTIVE)) |
| 233 | + return; |
| 234 | + if (get_expired_time(lock, &now)) |
| 235 | + expired = 1; |
| 236 | + else |
| 237 | + now = ktime_get(); |
| 238 | + lock->stat.count++; |
| 239 | + if (expired) |
| 240 | + lock->stat.expire_count++; |
| 241 | + duration = ktime_sub(now, lock->stat.last_time); |
| 242 | + lock->stat.total_time = ktime_add(lock->stat.total_time, duration); |
| 243 | + if (ktime_to_ns(duration) > ktime_to_ns(lock->stat.max_time)) |
| 244 | + lock->stat.max_time = duration; |
| 245 | + lock->stat.last_time = ktime_get(); |
| 246 | + if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) { |
| 247 | + duration = ktime_sub(now, last_sleep_time_update); |
| 248 | + lock->stat.prevent_suspend_time = ktime_add( |
| 249 | + lock->stat.prevent_suspend_time, duration); |
| 250 | + lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND; |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +static void update_sleep_wait_stats_locked(int done) |
| 255 | +{ |
| 256 | + struct wake_lock *lock; |
| 257 | + ktime_t now, etime, elapsed, add; |
| 258 | + int expired; |
| 259 | + |
| 260 | + now = ktime_get(); |
| 261 | + elapsed = ktime_sub(now, last_sleep_time_update); |
| 262 | + list_for_each_entry(lock, &active_wake_locks[WAKE_LOCK_SUSPEND], link) { |
| 263 | + expired = get_expired_time(lock, &etime); |
| 264 | + if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) { |
| 265 | + if (expired) |
| 266 | + add = ktime_sub(etime, last_sleep_time_update); |
| 267 | + else |
| 268 | + add = elapsed; |
| 269 | + lock->stat.prevent_suspend_time = ktime_add( |
| 270 | + lock->stat.prevent_suspend_time, add); |
| 271 | + } |
| 272 | + if (done || expired) |
| 273 | + lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND; |
| 274 | + else |
| 275 | + lock->flags |= WAKE_LOCK_PREVENTING_SUSPEND; |
| 276 | + } |
| 277 | + last_sleep_time_update = now; |
| 278 | +} |
| 279 | +#endif |
| 280 | + |
| 281 | + |
| 282 | +static void expire_wake_lock(struct wake_lock *lock) |
| 283 | +{ |
| 284 | +#ifdef CONFIG_WAKELOCK_STAT |
| 285 | + wake_unlock_stat_locked(lock, 1); |
| 286 | +#endif |
| 287 | + lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE); |
| 288 | + list_del(&lock->link); |
| 289 | + list_add(&lock->link, &inactive_locks); |
| 290 | + if (debug_mask & (DEBUG_WAKE_LOCK | DEBUG_EXPIRE)) |
| 291 | + pr_info("expired wake lock %s\n", lock->name); |
| 292 | +} |
| 293 | + |
| 294 | +static void print_active_locks(int type) |
| 295 | +{ |
| 296 | + unsigned long irqflags; |
| 297 | + struct wake_lock *lock; |
| 298 | + |
| 299 | + BUG_ON(type >= WAKE_LOCK_TYPE_COUNT); |
| 300 | + spin_lock_irqsave(&list_lock, irqflags); |
| 301 | + list_for_each_entry(lock, &active_wake_locks[type], link) { |
| 302 | + if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) { |
| 303 | + long timeout = lock->expires - jiffies; |
| 304 | + if (timeout <= 0) |
| 305 | + pr_info("wake lock %s, expired\n", lock->name); |
| 306 | + else |
| 307 | + pr_info("active wake lock %s, time left %ld\n", |
| 308 | + lock->name, timeout); |
| 309 | + } else |
| 310 | + pr_info("active wake lock %s\n", lock->name); |
| 311 | + } |
| 312 | + spin_unlock_irqrestore(&list_lock, irqflags); |
| 313 | +} |
| 314 | + |
| 315 | +static long has_wake_lock_locked(int type) |
| 316 | +{ |
| 317 | + struct wake_lock *lock, *n; |
| 318 | + long max_timeout = 0; |
| 319 | + |
| 320 | + BUG_ON(type >= WAKE_LOCK_TYPE_COUNT); |
| 321 | + list_for_each_entry_safe(lock, n, &active_wake_locks[type], link) { |
| 322 | + if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) { |
| 323 | + long timeout = lock->expires - jiffies; |
| 324 | + if (timeout <= 0) |
| 325 | + expire_wake_lock(lock); |
| 326 | + else if (timeout > max_timeout) |
| 327 | + max_timeout = timeout; |
| 328 | + } else |
| 329 | + return -1; |
| 330 | + } |
| 331 | + return max_timeout; |
| 332 | +} |
| 333 | + |
| 334 | +long has_wake_lock(int type) |
| 335 | +{ |
| 336 | + long ret; |
| 337 | + unsigned long irqflags; |
| 338 | + spin_lock_irqsave(&list_lock, irqflags); |
| 339 | + ret = has_wake_lock_locked(type); |
| 340 | + spin_unlock_irqrestore(&list_lock, irqflags); |
| 341 | + return ret; |
| 342 | +} |
| 343 | + |
| 344 | +static void suspend(struct work_struct *work) |
| 345 | +{ |
| 346 | + int ret; |
| 347 | + int entry_event_num; |
| 348 | + |
| 349 | + if (has_wake_lock(WAKE_LOCK_SUSPEND)) { |
| 350 | + if (debug_mask & DEBUG_SUSPEND) |
| 351 | + pr_info("suspend: abort suspend\n"); |
| 352 | + return; |
| 353 | + } |
| 354 | + |
| 355 | + entry_event_num = current_event_num; |
| 356 | + sys_sync(); |
| 357 | + if (debug_mask & DEBUG_SUSPEND) |
| 358 | + pr_info("suspend: enter suspend\n"); |
| 359 | + ret = pm_suspend(requested_suspend_state); |
| 360 | + if (debug_mask & DEBUG_EXIT_SUSPEND) { |
| 361 | + struct timespec ts; |
| 362 | + struct rtc_time tm; |
| 363 | + getnstimeofday(&ts); |
| 364 | + rtc_time_to_tm(ts.tv_sec, &tm); |
| 365 | + pr_info("suspend: exit suspend, ret = %d " |
| 366 | + "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", ret, |
| 367 | + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 368 | + tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec); |
| 369 | + } |
| 370 | + if (current_event_num == entry_event_num) { |
| 371 | + if (debug_mask & DEBUG_SUSPEND) |
| 372 | + pr_info("suspend: pm_suspend returned with no event\n"); |
| 373 | + wake_lock_timeout(&unknown_wakeup, HZ / 2); |
| 374 | + } |
| 375 | +} |
| 376 | +static DECLARE_WORK(suspend_work, suspend); |
| 377 | + |
| 378 | +static void expire_wake_locks(unsigned long data) |
| 379 | +{ |
| 380 | + long has_lock; |
| 381 | + unsigned long irqflags; |
| 382 | + if (debug_mask & DEBUG_EXPIRE) |
| 383 | + pr_info("expire_wake_locks: start\n"); |
| 384 | + if (debug_mask & DEBUG_SUSPEND) |
| 385 | + print_active_locks(WAKE_LOCK_SUSPEND); |
| 386 | + spin_lock_irqsave(&list_lock, irqflags); |
| 387 | + has_lock = has_wake_lock_locked(WAKE_LOCK_SUSPEND); |
| 388 | + if (debug_mask & DEBUG_EXPIRE) |
| 389 | + pr_info("expire_wake_locks: done, has_lock %ld\n", has_lock); |
| 390 | + if (has_lock == 0) |
| 391 | + queue_work(suspend_work_queue, &suspend_work); |
| 392 | + spin_unlock_irqrestore(&list_lock, irqflags); |
| 393 | +} |
| 394 | +static DEFINE_TIMER(expire_timer, expire_wake_locks, 0, 0); |
| 395 | + |
| 396 | +static int power_suspend_late(struct platform_device *pdev, pm_message_t state) |
| 397 | +{ |
| 398 | + int ret = has_wake_lock(WAKE_LOCK_SUSPEND) ? -EAGAIN : 0; |
| 399 | +#ifdef CONFIG_WAKELOCK_STAT |
| 400 | + wait_for_wakeup = 1; |
| 401 | +#endif |
| 402 | + if (debug_mask & DEBUG_SUSPEND) |
| 403 | + pr_info("power_suspend_late return %d\n", ret); |
| 404 | + return ret; |
| 405 | +} |
| 406 | + |
| 407 | +static struct platform_driver power_driver = { |
| 408 | + .driver.name = "power", |
| 409 | + .suspend_late = power_suspend_late, |
| 410 | +}; |
| 411 | +static struct platform_device power_device = { |
| 412 | + .name = "power", |
| 413 | +}; |
| 414 | + |
| 415 | +void wake_lock_init(struct wake_lock *lock, int type, const char *name) |
| 416 | +{ |
| 417 | + unsigned long irqflags = 0; |
| 418 | + |
| 419 | + if (name) |
| 420 | + lock->name = name; |
| 421 | + BUG_ON(!lock->name); |
| 422 | + |
| 423 | + if (debug_mask & DEBUG_WAKE_LOCK) |
| 424 | + pr_info("wake_lock_init name=%s\n", lock->name); |
| 425 | +#ifdef CONFIG_WAKELOCK_STAT |
| 426 | + lock->stat.count = 0; |
| 427 | + lock->stat.expire_count = 0; |
| 428 | + lock->stat.wakeup_count = 0; |
| 429 | + lock->stat.total_time = ktime_set(0, 0); |
| 430 | + lock->stat.prevent_suspend_time = ktime_set(0, 0); |
| 431 | + lock->stat.max_time = ktime_set(0, 0); |
| 432 | + lock->stat.last_time = ktime_set(0, 0); |
| 433 | +#endif |
| 434 | + lock->flags = (type & WAKE_LOCK_TYPE_MASK) | WAKE_LOCK_INITIALIZED; |
| 435 | + |
| 436 | + INIT_LIST_HEAD(&lock->link); |
| 437 | + spin_lock_irqsave(&list_lock, irqflags); |
| 438 | + list_add(&lock->link, &inactive_locks); |
| 439 | + spin_unlock_irqrestore(&list_lock, irqflags); |
| 440 | +} |
| 441 | +EXPORT_SYMBOL(wake_lock_init); |
| 442 | + |
| 443 | +void wake_lock_destroy(struct wake_lock *lock) |
| 444 | +{ |
| 445 | + unsigned long irqflags; |
| 446 | + if (debug_mask & DEBUG_WAKE_LOCK) |
| 447 | + pr_info("wake_lock_destroy name=%s\n", lock->name); |
| 448 | + spin_lock_irqsave(&list_lock, irqflags); |
| 449 | + lock->flags &= ~WAKE_LOCK_INITIALIZED; |
| 450 | +#ifdef CONFIG_WAKELOCK_STAT |
| 451 | + if (lock->stat.count) { |
| 452 | + deleted_wake_locks.stat.count += lock->stat.count; |
| 453 | + deleted_wake_locks.stat.expire_count += lock->stat.expire_count; |
| 454 | + deleted_wake_locks.stat.total_time = |
| 455 | + ktime_add(deleted_wake_locks.stat.total_time, |
| 456 | + lock->stat.total_time); |
| 457 | + deleted_wake_locks.stat.prevent_suspend_time = |
| 458 | + ktime_add(deleted_wake_locks.stat.prevent_suspend_time, |
| 459 | + lock->stat.prevent_suspend_time); |
| 460 | + deleted_wake_locks.stat.max_time = |
| 461 | + ktime_add(deleted_wake_locks.stat.max_time, |
| 462 | + lock->stat.max_time); |
| 463 | + } |
| 464 | +#endif |
| 465 | + list_del(&lock->link); |
| 466 | + spin_unlock_irqrestore(&list_lock, irqflags); |
| 467 | +} |
| 468 | +EXPORT_SYMBOL(wake_lock_destroy); |
| 469 | + |
| 470 | +static void wake_lock_internal( |
| 471 | + struct wake_lock *lock, long timeout, int has_timeout) |
| 472 | +{ |
| 473 | + int type; |
| 474 | + unsigned long irqflags; |
| 475 | + long expire_in; |
| 476 | + |
| 477 | + spin_lock_irqsave(&list_lock, irqflags); |
| 478 | + type = lock->flags & WAKE_LOCK_TYPE_MASK; |
| 479 | + BUG_ON(type >= WAKE_LOCK_TYPE_COUNT); |
| 480 | + BUG_ON(!(lock->flags & WAKE_LOCK_INITIALIZED)); |
| 481 | +#ifdef CONFIG_WAKELOCK_STAT |
| 482 | + if (type == WAKE_LOCK_SUSPEND && wait_for_wakeup) { |
| 483 | + if (debug_mask & DEBUG_WAKEUP) |
| 484 | + pr_info("wakeup wake lock: %s\n", lock->name); |
| 485 | + wait_for_wakeup = 0; |
| 486 | + lock->stat.wakeup_count++; |
| 487 | + } |
| 488 | + if ((lock->flags & WAKE_LOCK_AUTO_EXPIRE) && |
| 489 | + (long)(lock->expires - jiffies) <= 0) { |
| 490 | + wake_unlock_stat_locked(lock, 0); |
| 491 | + lock->stat.last_time = ktime_get(); |
| 492 | + } |
| 493 | +#endif |
| 494 | + if (!(lock->flags & WAKE_LOCK_ACTIVE)) { |
| 495 | + lock->flags |= WAKE_LOCK_ACTIVE; |
| 496 | +#ifdef CONFIG_WAKELOCK_STAT |
| 497 | + lock->stat.last_time = ktime_get(); |
| 498 | +#endif |
| 499 | + } |
| 500 | + list_del(&lock->link); |
| 501 | + if (has_timeout) { |
| 502 | + if (debug_mask & DEBUG_WAKE_LOCK) |
| 503 | + pr_info("wake_lock: %s, type %d, timeout %ld.%03lu\n", |
| 504 | + lock->name, type, timeout / HZ, |
| 505 | + (timeout % HZ) * MSEC_PER_SEC / HZ); |
| 506 | + lock->expires = jiffies + timeout; |
| 507 | + lock->flags |= WAKE_LOCK_AUTO_EXPIRE; |
| 508 | + list_add_tail(&lock->link, &active_wake_locks[type]); |
| 509 | + } else { |
| 510 | + if (debug_mask & DEBUG_WAKE_LOCK) |
| 511 | + pr_info("wake_lock: %s, type %d\n", lock->name, type); |
| 512 | + lock->expires = LONG_MAX; |
| 513 | + lock->flags &= ~WAKE_LOCK_AUTO_EXPIRE; |
| 514 | + list_add(&lock->link, &active_wake_locks[type]); |
| 515 | + } |
| 516 | + if (type == WAKE_LOCK_SUSPEND) { |
| 517 | + current_event_num++; |
| 518 | +#ifdef CONFIG_WAKELOCK_STAT |
| 519 | + if (lock == &main_wake_lock) |
| 520 | + update_sleep_wait_stats_locked(1); |
| 521 | + else if (!wake_lock_active(&main_wake_lock)) |
| 522 | + update_sleep_wait_stats_locked(0); |
| 523 | +#endif |
| 524 | + if (has_timeout) |
| 525 | + expire_in = has_wake_lock_locked(type); |
| 526 | + else |
| 527 | + expire_in = -1; |
| 528 | + if (expire_in > 0) { |
| 529 | + if (debug_mask & DEBUG_EXPIRE) |
| 530 | + pr_info("wake_lock: %s, start expire timer, " |
| 531 | + "%ld\n", lock->name, expire_in); |
| 532 | + mod_timer(&expire_timer, jiffies + expire_in); |
| 533 | + } else { |
| 534 | + if (del_timer(&expire_timer)) |
| 535 | + if (debug_mask & DEBUG_EXPIRE) |
| 536 | + pr_info("wake_lock: %s, stop expire timer\n", |
| 537 | + lock->name); |
| 538 | + if (expire_in == 0) |
| 539 | + queue_work(suspend_work_queue, &suspend_work); |
| 540 | + } |
| 541 | + } |
| 542 | + spin_unlock_irqrestore(&list_lock, irqflags); |
| 543 | +} |
| 544 | + |
| 545 | +void wake_lock(struct wake_lock *lock) |
| 546 | +{ |
| 547 | + wake_lock_internal(lock, 0, 0); |
| 548 | +} |
| 549 | +EXPORT_SYMBOL(wake_lock); |
| 550 | + |
| 551 | +void wake_lock_timeout(struct wake_lock *lock, long timeout) |
| 552 | +{ |
| 553 | + wake_lock_internal(lock, timeout, 1); |
| 554 | +} |
| 555 | +EXPORT_SYMBOL(wake_lock_timeout); |
| 556 | + |
| 557 | +void wake_unlock(struct wake_lock *lock) |
| 558 | +{ |
| 559 | + int type; |
| 560 | + unsigned long irqflags; |
| 561 | + spin_lock_irqsave(&list_lock, irqflags); |
| 562 | + type = lock->flags & WAKE_LOCK_TYPE_MASK; |
| 563 | +#ifdef CONFIG_WAKELOCK_STAT |
| 564 | + wake_unlock_stat_locked(lock, 0); |
| 565 | +#endif |
| 566 | + if (debug_mask & DEBUG_WAKE_LOCK) |
| 567 | + pr_info("wake_unlock: %s\n", lock->name); |
| 568 | + lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE); |
| 569 | + list_del(&lock->link); |
| 570 | + list_add(&lock->link, &inactive_locks); |
| 571 | + if (type == WAKE_LOCK_SUSPEND) { |
| 572 | + long has_lock = has_wake_lock_locked(type); |
| 573 | + if (has_lock > 0) { |
| 574 | + if (debug_mask & DEBUG_EXPIRE) |
| 575 | + pr_info("wake_unlock: %s, start expire timer, " |
| 576 | + "%ld\n", lock->name, has_lock); |
| 577 | + mod_timer(&expire_timer, jiffies + has_lock); |
| 578 | + } else { |
| 579 | + if (del_timer(&expire_timer)) |
| 580 | + if (debug_mask & DEBUG_EXPIRE) |
| 581 | + pr_info("wake_unlock: %s, stop expire " |
| 582 | + "timer\n", lock->name); |
| 583 | + if (has_lock == 0) |
| 584 | + queue_work(suspend_work_queue, &suspend_work); |
| 585 | + } |
| 586 | + if (lock == &main_wake_lock) { |
| 587 | + if (debug_mask & DEBUG_SUSPEND) |
| 588 | + print_active_locks(WAKE_LOCK_SUSPEND); |
| 589 | +#ifdef CONFIG_WAKELOCK_STAT |
| 590 | + update_sleep_wait_stats_locked(0); |
| 591 | +#endif |
| 592 | + } |
| 593 | + } |
| 594 | + spin_unlock_irqrestore(&list_lock, irqflags); |
| 595 | +} |
| 596 | +EXPORT_SYMBOL(wake_unlock); |
| 597 | + |
| 598 | +int wake_lock_active(struct wake_lock *lock) |
| 599 | +{ |
| 600 | + return !!(lock->flags & WAKE_LOCK_ACTIVE); |
| 601 | +} |
| 602 | +EXPORT_SYMBOL(wake_lock_active); |
| 603 | + |
| 604 | +static int __init wakelocks_init(void) |
| 605 | +{ |
| 606 | + int ret; |
| 607 | + int i; |
| 608 | + |
| 609 | + for (i = 0; i < ARRAY_SIZE(active_wake_locks); i++) |
| 610 | + INIT_LIST_HEAD(&active_wake_locks[i]); |
| 611 | + |
| 612 | +#ifdef CONFIG_WAKELOCK_STAT |
| 613 | + wake_lock_init(&deleted_wake_locks, WAKE_LOCK_SUSPEND, |
| 614 | + "deleted_wake_locks"); |
| 615 | +#endif |
| 616 | + wake_lock_init(&main_wake_lock, WAKE_LOCK_SUSPEND, "main"); |
| 617 | + wake_lock(&main_wake_lock); |
| 618 | + wake_lock_init(&unknown_wakeup, WAKE_LOCK_SUSPEND, "unknown_wakeups"); |
| 619 | + |
| 620 | + ret = platform_device_register(&power_device); |
| 621 | + if (ret) { |
| 622 | + pr_err("wakelocks_init: platform_device_register failed\n"); |
| 623 | + goto err_platform_device_register; |
| 624 | + } |
| 625 | + ret = platform_driver_register(&power_driver); |
| 626 | + if (ret) { |
| 627 | + pr_err("wakelocks_init: platform_driver_register failed\n"); |
| 628 | + goto err_platform_driver_register; |
| 629 | + } |
| 630 | + |
| 631 | + suspend_work_queue = create_singlethread_workqueue("suspend"); |
| 632 | + if (suspend_work_queue == NULL) { |
| 633 | + ret = -ENOMEM; |
| 634 | + goto err_suspend_work_queue; |
| 635 | + } |
| 636 | + |
| 637 | +#ifdef CONFIG_WAKELOCK_STAT |
| 638 | + create_proc_read_entry("wakelocks", S_IRUGO, NULL, |
| 639 | + wakelocks_read_proc, NULL); |
| 640 | +#endif |
| 641 | + |
| 642 | + return 0; |
| 643 | + |
| 644 | +err_suspend_work_queue: |
| 645 | + platform_driver_unregister(&power_driver); |
| 646 | +err_platform_driver_register: |
| 647 | + platform_device_unregister(&power_device); |
| 648 | +err_platform_device_register: |
| 649 | + wake_lock_destroy(&unknown_wakeup); |
| 650 | + wake_lock_destroy(&main_wake_lock); |
| 651 | +#ifdef CONFIG_WAKELOCK_STAT |
| 652 | + wake_lock_destroy(&deleted_wake_locks); |
| 653 | +#endif |
| 654 | + return ret; |
| 655 | +} |
| 656 | + |
| 657 | +static void __exit wakelocks_exit(void) |
| 658 | +{ |
| 659 | +#ifdef CONFIG_WAKELOCK_STAT |
| 660 | + remove_proc_entry("wakelocks", NULL); |
| 661 | +#endif |
| 662 | + destroy_workqueue(suspend_work_queue); |
| 663 | + platform_driver_unregister(&power_driver); |
| 664 | + platform_device_unregister(&power_device); |
| 665 | + wake_lock_destroy(&unknown_wakeup); |
| 666 | + wake_lock_destroy(&main_wake_lock); |
| 667 | +#ifdef CONFIG_WAKELOCK_STAT |
| 668 | + wake_lock_destroy(&deleted_wake_locks); |
| 669 | +#endif |
| 670 | +} |
| 671 | + |
| 672 | +core_initcall(wakelocks_init); |
| 673 | +module_exit(wakelocks_exit); |
| 674 | |