Arve Hjønnevåg | c1783f5 | 2008-10-07 20:48:01 -0700 | [diff] [blame] | 1 | /* kernel/power/earlysuspend.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/earlysuspend.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/rtc.h> |
| 20 | #include <linux/syscalls.h> /* sys_sync */ |
| 21 | #include <linux/wakelock.h> |
| 22 | #include <linux/workqueue.h> |
| 23 | |
| 24 | #include "power.h" |
| 25 | |
| 26 | enum { |
| 27 | DEBUG_USER_STATE = 1U << 0, |
| 28 | DEBUG_SUSPEND = 1U << 2, |
Erik Gilling | fec502d | 2011-07-21 14:07:45 -0700 | [diff] [blame^] | 29 | DEBUG_VERBOSE = 1U << 3, |
Arve Hjønnevåg | c1783f5 | 2008-10-07 20:48:01 -0700 | [diff] [blame] | 30 | }; |
| 31 | static int debug_mask = DEBUG_USER_STATE; |
| 32 | module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 33 | |
| 34 | static DEFINE_MUTEX(early_suspend_lock); |
| 35 | static LIST_HEAD(early_suspend_handlers); |
| 36 | static void early_suspend(struct work_struct *work); |
| 37 | static void late_resume(struct work_struct *work); |
| 38 | static DECLARE_WORK(early_suspend_work, early_suspend); |
| 39 | static DECLARE_WORK(late_resume_work, late_resume); |
| 40 | static DEFINE_SPINLOCK(state_lock); |
| 41 | enum { |
| 42 | SUSPEND_REQUESTED = 0x1, |
| 43 | SUSPENDED = 0x2, |
| 44 | SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED, |
| 45 | }; |
| 46 | static int state; |
| 47 | |
| 48 | void register_early_suspend(struct early_suspend *handler) |
| 49 | { |
| 50 | struct list_head *pos; |
| 51 | |
| 52 | mutex_lock(&early_suspend_lock); |
| 53 | list_for_each(pos, &early_suspend_handlers) { |
| 54 | struct early_suspend *e; |
| 55 | e = list_entry(pos, struct early_suspend, link); |
| 56 | if (e->level > handler->level) |
| 57 | break; |
| 58 | } |
| 59 | list_add_tail(&handler->link, pos); |
| 60 | if ((state & SUSPENDED) && handler->suspend) |
| 61 | handler->suspend(handler); |
| 62 | mutex_unlock(&early_suspend_lock); |
| 63 | } |
| 64 | EXPORT_SYMBOL(register_early_suspend); |
| 65 | |
| 66 | void unregister_early_suspend(struct early_suspend *handler) |
| 67 | { |
| 68 | mutex_lock(&early_suspend_lock); |
| 69 | list_del(&handler->link); |
| 70 | mutex_unlock(&early_suspend_lock); |
| 71 | } |
| 72 | EXPORT_SYMBOL(unregister_early_suspend); |
| 73 | |
| 74 | static void early_suspend(struct work_struct *work) |
| 75 | { |
| 76 | struct early_suspend *pos; |
| 77 | unsigned long irqflags; |
| 78 | int abort = 0; |
| 79 | |
| 80 | mutex_lock(&early_suspend_lock); |
| 81 | spin_lock_irqsave(&state_lock, irqflags); |
| 82 | if (state == SUSPEND_REQUESTED) |
| 83 | state |= SUSPENDED; |
| 84 | else |
| 85 | abort = 1; |
| 86 | spin_unlock_irqrestore(&state_lock, irqflags); |
| 87 | |
| 88 | if (abort) { |
| 89 | if (debug_mask & DEBUG_SUSPEND) |
| 90 | pr_info("early_suspend: abort, state %d\n", state); |
| 91 | mutex_unlock(&early_suspend_lock); |
| 92 | goto abort; |
| 93 | } |
| 94 | |
| 95 | if (debug_mask & DEBUG_SUSPEND) |
| 96 | pr_info("early_suspend: call handlers\n"); |
| 97 | list_for_each_entry(pos, &early_suspend_handlers, link) { |
Erik Gilling | fec502d | 2011-07-21 14:07:45 -0700 | [diff] [blame^] | 98 | if (pos->suspend != NULL) { |
| 99 | if (debug_mask & DEBUG_VERBOSE) |
| 100 | pr_info("early_suspend: calling %pf\n", pos->suspend); |
Arve Hjønnevåg | c1783f5 | 2008-10-07 20:48:01 -0700 | [diff] [blame] | 101 | pos->suspend(pos); |
Erik Gilling | fec502d | 2011-07-21 14:07:45 -0700 | [diff] [blame^] | 102 | } |
Arve Hjønnevåg | c1783f5 | 2008-10-07 20:48:01 -0700 | [diff] [blame] | 103 | } |
| 104 | mutex_unlock(&early_suspend_lock); |
| 105 | |
| 106 | if (debug_mask & DEBUG_SUSPEND) |
| 107 | pr_info("early_suspend: sync\n"); |
| 108 | |
| 109 | sys_sync(); |
| 110 | abort: |
| 111 | spin_lock_irqsave(&state_lock, irqflags); |
| 112 | if (state == SUSPEND_REQUESTED_AND_SUSPENDED) |
| 113 | wake_unlock(&main_wake_lock); |
| 114 | spin_unlock_irqrestore(&state_lock, irqflags); |
| 115 | } |
| 116 | |
| 117 | static void late_resume(struct work_struct *work) |
| 118 | { |
| 119 | struct early_suspend *pos; |
| 120 | unsigned long irqflags; |
| 121 | int abort = 0; |
| 122 | |
| 123 | mutex_lock(&early_suspend_lock); |
| 124 | spin_lock_irqsave(&state_lock, irqflags); |
| 125 | if (state == SUSPENDED) |
| 126 | state &= ~SUSPENDED; |
| 127 | else |
| 128 | abort = 1; |
| 129 | spin_unlock_irqrestore(&state_lock, irqflags); |
| 130 | |
| 131 | if (abort) { |
| 132 | if (debug_mask & DEBUG_SUSPEND) |
| 133 | pr_info("late_resume: abort, state %d\n", state); |
| 134 | goto abort; |
| 135 | } |
| 136 | if (debug_mask & DEBUG_SUSPEND) |
| 137 | pr_info("late_resume: call handlers\n"); |
Erik Gilling | fec502d | 2011-07-21 14:07:45 -0700 | [diff] [blame^] | 138 | list_for_each_entry_reverse(pos, &early_suspend_handlers, link) { |
| 139 | if (pos->resume != NULL) { |
| 140 | if (debug_mask & DEBUG_VERBOSE) |
| 141 | pr_info("late_resume: calling %pf\n", pos->resume); |
| 142 | |
Arve Hjønnevåg | c1783f5 | 2008-10-07 20:48:01 -0700 | [diff] [blame] | 143 | pos->resume(pos); |
Erik Gilling | fec502d | 2011-07-21 14:07:45 -0700 | [diff] [blame^] | 144 | } |
| 145 | } |
Arve Hjønnevåg | c1783f5 | 2008-10-07 20:48:01 -0700 | [diff] [blame] | 146 | if (debug_mask & DEBUG_SUSPEND) |
| 147 | pr_info("late_resume: done\n"); |
| 148 | abort: |
| 149 | mutex_unlock(&early_suspend_lock); |
| 150 | } |
| 151 | |
| 152 | void request_suspend_state(suspend_state_t new_state) |
| 153 | { |
| 154 | unsigned long irqflags; |
| 155 | int old_sleep; |
| 156 | |
| 157 | spin_lock_irqsave(&state_lock, irqflags); |
| 158 | old_sleep = state & SUSPEND_REQUESTED; |
| 159 | if (debug_mask & DEBUG_USER_STATE) { |
| 160 | struct timespec ts; |
| 161 | struct rtc_time tm; |
| 162 | getnstimeofday(&ts); |
| 163 | rtc_time_to_tm(ts.tv_sec, &tm); |
| 164 | pr_info("request_suspend_state: %s (%d->%d) at %lld " |
| 165 | "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", |
| 166 | new_state != PM_SUSPEND_ON ? "sleep" : "wakeup", |
| 167 | requested_suspend_state, new_state, |
| 168 | ktime_to_ns(ktime_get()), |
| 169 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 170 | tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec); |
| 171 | } |
| 172 | if (!old_sleep && new_state != PM_SUSPEND_ON) { |
| 173 | state |= SUSPEND_REQUESTED; |
| 174 | queue_work(suspend_work_queue, &early_suspend_work); |
| 175 | } else if (old_sleep && new_state == PM_SUSPEND_ON) { |
| 176 | state &= ~SUSPEND_REQUESTED; |
| 177 | wake_lock(&main_wake_lock); |
| 178 | queue_work(suspend_work_queue, &late_resume_work); |
| 179 | } |
| 180 | requested_suspend_state = new_state; |
| 181 | spin_unlock_irqrestore(&state_lock, irqflags); |
| 182 | } |
| 183 | |
| 184 | suspend_state_t get_suspend_state(void) |
| 185 | { |
| 186 | return requested_suspend_state; |
| 187 | } |