Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 1 | /* drivers/rtc/alarm.c |
| 2 | * |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 3 | * Copyright (C) 2007-2009 Google, Inc. |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 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 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 17 | #include <linux/android_alarm.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/miscdevice.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/rtc.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/spinlock.h> |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 24 | #include <linux/wakelock.h> |
| 25 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 26 | #include <asm/mach/time.h> |
| 27 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 28 | #define ANDROID_ALARM_PRINT_ERROR (1U << 0) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 29 | #define ANDROID_ALARM_PRINT_INIT_STATUS (1U << 1) |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 30 | #define ANDROID_ALARM_PRINT_TSET (1U << 2) |
| 31 | #define ANDROID_ALARM_PRINT_CALL (1U << 3) |
| 32 | #define ANDROID_ALARM_PRINT_SUSPEND (1U << 4) |
| 33 | #define ANDROID_ALARM_PRINT_INT (1U << 5) |
| 34 | #define ANDROID_ALARM_PRINT_FLOW (1U << 6) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 35 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 36 | static int debug_mask = ANDROID_ALARM_PRINT_ERROR | \ |
| 37 | ANDROID_ALARM_PRINT_INIT_STATUS; |
| 38 | module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 39 | |
| 40 | #define pr_alarm(debug_level_mask, args...) \ |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 41 | do { \ |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 42 | if (debug_mask & ANDROID_ALARM_PRINT_##debug_level_mask) { \ |
| 43 | pr_info(args); \ |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 44 | } \ |
| 45 | } while (0) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 46 | |
| 47 | #define ANDROID_ALARM_WAKEUP_MASK ( \ |
| 48 | ANDROID_ALARM_RTC_WAKEUP_MASK | \ |
| 49 | ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK) |
| 50 | |
| 51 | /* support old usespace code */ |
| 52 | #define ANDROID_ALARM_SET_OLD _IOW('a', 2, time_t) /* set alarm */ |
| 53 | #define ANDROID_ALARM_SET_AND_WAIT_OLD _IOW('a', 3, time_t) |
| 54 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 55 | struct alarm_queue { |
| 56 | struct rb_root alarms; |
| 57 | struct rb_node *first; |
| 58 | struct hrtimer timer; |
| 59 | ktime_t delta; |
| 60 | bool stopped; |
| 61 | ktime_t stopped_time; |
| 62 | }; |
| 63 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 64 | static struct rtc_device *alarm_rtc_dev; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 65 | static DEFINE_SPINLOCK(alarm_slock); |
| 66 | static DEFINE_MUTEX(alarm_setrtc_mutex); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 67 | static struct wake_lock alarm_rtc_wake_lock; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 68 | static struct platform_device *alarm_platform_dev; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 69 | struct alarm_queue alarms[ANDROID_ALARM_TYPE_COUNT]; |
| 70 | static bool suspended; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 71 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 72 | static void update_timer_locked(struct alarm_queue *base, bool head_removed) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 73 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 74 | struct alarm *alarm; |
| 75 | bool is_wakeup = base == &alarms[ANDROID_ALARM_RTC_WAKEUP] || |
| 76 | base == &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP]; |
| 77 | |
| 78 | if (base->stopped) { |
| 79 | pr_alarm(FLOW, "changed alarm while setting the wall time\n"); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 80 | return; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 83 | if (is_wakeup && !suspended && head_removed) |
| 84 | wake_unlock(&alarm_rtc_wake_lock); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 85 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 86 | if (!base->first) |
| 87 | return; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 88 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 89 | alarm = container_of(base->first, struct alarm, node); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 90 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 91 | pr_alarm(FLOW, "selected alarm, type %d, func %pF at %lld\n", |
| 92 | alarm->type, alarm->function, ktime_to_ns(alarm->expires)); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 93 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 94 | if (is_wakeup && suspended) { |
| 95 | pr_alarm(FLOW, "changed alarm while suspened\n"); |
| 96 | wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ); |
| 97 | return; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 98 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 99 | |
| 100 | hrtimer_try_to_cancel(&base->timer); |
| 101 | base->timer.node.expires = ktime_add(base->delta, alarm->expires); |
| 102 | base->timer._softexpires = ktime_add(base->delta, alarm->softexpires); |
| 103 | hrtimer_start_expires(&base->timer, HRTIMER_MODE_ABS); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 106 | static void alarm_enqueue_locked(struct alarm *alarm) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 107 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 108 | struct alarm_queue *base = &alarms[alarm->type]; |
| 109 | struct rb_node **link = &base->alarms.rb_node; |
| 110 | struct rb_node *parent = NULL; |
| 111 | struct alarm *entry; |
| 112 | int leftmost = 1; |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 113 | bool was_first = false; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 114 | |
| 115 | pr_alarm(FLOW, "added alarm, type %d, func %pF at %lld\n", |
| 116 | alarm->type, alarm->function, ktime_to_ns(alarm->expires)); |
| 117 | |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 118 | if (base->first == &alarm->node) { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 119 | base->first = rb_next(&alarm->node); |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 120 | was_first = true; |
| 121 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 122 | if (!RB_EMPTY_NODE(&alarm->node)) { |
| 123 | rb_erase(&alarm->node, &base->alarms); |
| 124 | RB_CLEAR_NODE(&alarm->node); |
| 125 | } |
| 126 | |
| 127 | while (*link) { |
| 128 | parent = *link; |
| 129 | entry = rb_entry(parent, struct alarm, node); |
| 130 | /* |
| 131 | * We dont care about collisions. Nodes with |
| 132 | * the same expiry time stay together. |
| 133 | */ |
| 134 | if (alarm->expires.tv64 < entry->expires.tv64) { |
| 135 | link = &(*link)->rb_left; |
| 136 | } else { |
| 137 | link = &(*link)->rb_right; |
| 138 | leftmost = 0; |
| 139 | } |
| 140 | } |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 141 | if (leftmost) |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 142 | base->first = &alarm->node; |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 143 | if (leftmost || was_first) |
| 144 | update_timer_locked(base, was_first); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 145 | |
| 146 | rb_link_node(&alarm->node, parent, link); |
| 147 | rb_insert_color(&alarm->node, &base->alarms); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 150 | /** |
| 151 | * alarm_init - initialize an alarm |
| 152 | * @alarm: the alarm to be initialized |
| 153 | * @type: the alarm type to be used |
| 154 | * @function: alarm callback function |
| 155 | */ |
| 156 | void alarm_init(struct alarm *alarm, |
| 157 | enum android_alarm_type type, void (*function)(struct alarm *)) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 158 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 159 | RB_CLEAR_NODE(&alarm->node); |
| 160 | alarm->type = type; |
| 161 | alarm->function = function; |
| 162 | |
| 163 | pr_alarm(FLOW, "created alarm, type %d, func %pF\n", type, function); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * alarm_start_range - (re)start an alarm |
| 169 | * @alarm: the alarm to be added |
| 170 | * @start: earliest expiry time |
| 171 | * @end: expiry time |
| 172 | */ |
| 173 | void alarm_start_range(struct alarm *alarm, ktime_t start, ktime_t end) |
| 174 | { |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 175 | unsigned long flags; |
| 176 | |
| 177 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 178 | alarm->softexpires = start; |
| 179 | alarm->expires = end; |
| 180 | alarm_enqueue_locked(alarm); |
| 181 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * alarm_try_to_cancel - try to deactivate an alarm |
| 186 | * @alarm: alarm to stop |
| 187 | * |
| 188 | * Returns: |
| 189 | * 0 when the alarm was not active |
| 190 | * 1 when the alarm was active |
| 191 | * -1 when the alarm may currently be excuting the callback function and |
| 192 | * cannot be stopped (it may also be inactive) |
| 193 | */ |
| 194 | int alarm_try_to_cancel(struct alarm *alarm) |
| 195 | { |
| 196 | struct alarm_queue *base = &alarms[alarm->type]; |
| 197 | unsigned long flags; |
| 198 | bool first = false; |
| 199 | int ret = 0; |
| 200 | |
| 201 | spin_lock_irqsave(&alarm_slock, flags); |
| 202 | if (!RB_EMPTY_NODE(&alarm->node)) { |
| 203 | pr_alarm(FLOW, "canceled alarm, type %d, func %pF at %lld\n", |
| 204 | alarm->type, alarm->function, |
| 205 | ktime_to_ns(alarm->expires)); |
| 206 | ret = 1; |
| 207 | if (base->first == &alarm->node) { |
| 208 | base->first = rb_next(&alarm->node); |
| 209 | first = true; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 210 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 211 | rb_erase(&alarm->node, &base->alarms); |
| 212 | RB_CLEAR_NODE(&alarm->node); |
| 213 | if (first) |
| 214 | update_timer_locked(base, true); |
| 215 | } else |
| 216 | pr_alarm(FLOW, "tried to cancel alarm, type %d, func %pF\n", |
| 217 | alarm->type, alarm->function); |
| 218 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 219 | if (!ret && hrtimer_callback_running(&base->timer)) |
| 220 | ret = -1; |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * alarm_cancel - cancel an alarm and wait for the handler to finish. |
| 226 | * @alarm: the alarm to be cancelled |
| 227 | * |
| 228 | * Returns: |
| 229 | * 0 when the alarm was not active |
| 230 | * 1 when the alarm was active |
| 231 | */ |
| 232 | int alarm_cancel(struct alarm *alarm) |
| 233 | { |
| 234 | for (;;) { |
| 235 | int ret = alarm_try_to_cancel(alarm); |
| 236 | if (ret >= 0) |
| 237 | return ret; |
| 238 | cpu_relax(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * alarm_set_rtc - set the kernel and rtc walltime |
| 244 | * @new_time: timespec value containing the new time |
| 245 | */ |
| 246 | int alarm_set_rtc(struct timespec new_time) |
| 247 | { |
| 248 | int i; |
| 249 | int ret; |
| 250 | unsigned long flags; |
| 251 | struct rtc_time rtc_new_rtc_time; |
| 252 | struct timespec tmp_time; |
| 253 | |
| 254 | rtc_time_to_tm(new_time.tv_sec, &rtc_new_rtc_time); |
| 255 | |
| 256 | pr_alarm(TSET, "set rtc %ld %ld - rtc %02d:%02d:%02d %02d/%02d/%04d\n", |
| 257 | new_time.tv_sec, new_time.tv_nsec, |
| 258 | rtc_new_rtc_time.tm_hour, rtc_new_rtc_time.tm_min, |
| 259 | rtc_new_rtc_time.tm_sec, rtc_new_rtc_time.tm_mon + 1, |
| 260 | rtc_new_rtc_time.tm_mday, |
| 261 | rtc_new_rtc_time.tm_year + 1900); |
| 262 | |
| 263 | mutex_lock(&alarm_setrtc_mutex); |
| 264 | spin_lock_irqsave(&alarm_slock, flags); |
| 265 | wake_lock(&alarm_rtc_wake_lock); |
| 266 | getnstimeofday(&tmp_time); |
| 267 | for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) { |
| 268 | hrtimer_try_to_cancel(&alarms[i].timer); |
| 269 | alarms[i].stopped = true; |
| 270 | alarms[i].stopped_time = timespec_to_ktime(tmp_time); |
| 271 | } |
| 272 | alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta = |
| 273 | alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta = |
| 274 | ktime_sub(alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta, |
| 275 | timespec_to_ktime(timespec_sub(tmp_time, new_time))); |
| 276 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 277 | ret = do_settimeofday(&new_time); |
| 278 | spin_lock_irqsave(&alarm_slock, flags); |
| 279 | for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) { |
| 280 | alarms[i].stopped = false; |
| 281 | update_timer_locked(&alarms[i], false); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 282 | } |
| 283 | spin_unlock_irqrestore(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 284 | if (ret < 0) { |
| 285 | pr_alarm(ERROR, "alarm_set_rtc: Failed to set time\n"); |
| 286 | goto err; |
| 287 | } |
| 288 | if (!alarm_rtc_dev) { |
| 289 | pr_alarm(ERROR, |
| 290 | "alarm_set_rtc: no RTC, time will be lost on reboot\n"); |
| 291 | goto err; |
| 292 | } |
| 293 | ret = rtc_set_time(alarm_rtc_dev, &rtc_new_rtc_time); |
| 294 | if (ret < 0) |
| 295 | pr_alarm(ERROR, "alarm_set_rtc: " |
| 296 | "Failed to set RTC, time will be lost on reboot\n"); |
| 297 | err: |
| 298 | wake_unlock(&alarm_rtc_wake_lock); |
| 299 | mutex_unlock(&alarm_setrtc_mutex); |
| 300 | return ret; |
| 301 | } |
| 302 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 303 | |
| 304 | void |
| 305 | alarm_update_timedelta(struct timespec tmp_time, struct timespec new_time) |
| 306 | { |
| 307 | int i; |
| 308 | unsigned long flags; |
| 309 | |
| 310 | spin_lock_irqsave(&alarm_slock, flags); |
| 311 | for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) { |
| 312 | hrtimer_try_to_cancel(&alarms[i].timer); |
| 313 | alarms[i].stopped = true; |
| 314 | alarms[i].stopped_time = timespec_to_ktime(tmp_time); |
| 315 | } |
| 316 | alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta = |
| 317 | alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta = |
| 318 | ktime_sub(alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta, |
| 319 | timespec_to_ktime(timespec_sub(tmp_time, new_time))); |
| 320 | for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) { |
| 321 | alarms[i].stopped = false; |
| 322 | update_timer_locked(&alarms[i], false); |
| 323 | } |
| 324 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 325 | } |
| 326 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 327 | /** |
| 328 | * alarm_get_elapsed_realtime - get the elapsed real time in ktime_t format |
| 329 | * |
| 330 | * returns the time in ktime_t format |
| 331 | */ |
| 332 | ktime_t alarm_get_elapsed_realtime(void) |
| 333 | { |
| 334 | ktime_t now; |
| 335 | unsigned long flags; |
| 336 | struct alarm_queue *base = &alarms[ANDROID_ALARM_ELAPSED_REALTIME]; |
| 337 | |
| 338 | spin_lock_irqsave(&alarm_slock, flags); |
| 339 | now = base->stopped ? base->stopped_time : ktime_get_real(); |
| 340 | now = ktime_sub(now, base->delta); |
| 341 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 342 | return now; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static enum hrtimer_restart alarm_timer_triggered(struct hrtimer *timer) |
| 346 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 347 | struct alarm_queue *base; |
| 348 | struct alarm *alarm; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 349 | unsigned long flags; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 350 | ktime_t now; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 351 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 352 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 353 | |
| 354 | base = container_of(timer, struct alarm_queue, timer); |
| 355 | now = base->stopped ? base->stopped_time : hrtimer_cb_get_time(timer); |
| 356 | now = ktime_sub(now, base->delta); |
| 357 | |
| 358 | pr_alarm(INT, "alarm_timer_triggered type %d at %lld\n", |
| 359 | base - alarms, ktime_to_ns(now)); |
| 360 | |
| 361 | while (base->first) { |
| 362 | alarm = container_of(base->first, struct alarm, node); |
| 363 | if (alarm->softexpires.tv64 > now.tv64) { |
| 364 | pr_alarm(FLOW, "don't call alarm, %pF, %lld (s %lld)\n", |
| 365 | alarm->function, ktime_to_ns(alarm->expires), |
| 366 | ktime_to_ns(alarm->softexpires)); |
| 367 | break; |
| 368 | } |
| 369 | base->first = rb_next(&alarm->node); |
| 370 | rb_erase(&alarm->node, &base->alarms); |
| 371 | RB_CLEAR_NODE(&alarm->node); |
| 372 | pr_alarm(CALL, "call alarm, type %d, func %pF, %lld (s %lld)\n", |
| 373 | alarm->type, alarm->function, |
| 374 | ktime_to_ns(alarm->expires), |
| 375 | ktime_to_ns(alarm->softexpires)); |
| 376 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 377 | alarm->function(alarm); |
| 378 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 379 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 380 | if (!base->first) |
| 381 | pr_alarm(FLOW, "no more alarms of type %d\n", base - alarms); |
| 382 | update_timer_locked(base, true); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 383 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 384 | return HRTIMER_NORESTART; |
| 385 | } |
| 386 | |
| 387 | static void alarm_triggered_func(void *p) |
| 388 | { |
| 389 | struct rtc_device *rtc = alarm_rtc_dev; |
| 390 | if (!(rtc->irq_data & RTC_AF)) |
| 391 | return; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 392 | pr_alarm(INT, "rtc alarm triggered\n"); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 393 | wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ); |
| 394 | } |
| 395 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 396 | static int alarm_suspend(struct platform_device *pdev, pm_message_t state) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 397 | { |
| 398 | int err = 0; |
| 399 | unsigned long flags; |
| 400 | struct rtc_wkalrm rtc_alarm; |
| 401 | struct rtc_time rtc_current_rtc_time; |
| 402 | unsigned long rtc_current_time; |
| 403 | unsigned long rtc_alarm_time; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 404 | struct timespec rtc_delta; |
Arve Hjønnevåg | b2a4ab2 | 2010-03-12 20:05:32 -0800 | [diff] [blame] | 405 | struct timespec wall_time; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 406 | struct alarm_queue *wakeup_queue = NULL; |
| 407 | struct alarm_queue *tmp_queue = NULL; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 408 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 409 | pr_alarm(SUSPEND, "alarm_suspend(%p, %d)\n", pdev, state.event); |
| 410 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 411 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 412 | suspended = true; |
| 413 | spin_unlock_irqrestore(&alarm_slock, flags); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 414 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 415 | hrtimer_cancel(&alarms[ANDROID_ALARM_RTC_WAKEUP].timer); |
| 416 | hrtimer_cancel(&alarms[ |
JP Abgrall | 692e468 | 2011-09-02 18:14:12 -0700 | [diff] [blame] | 417 | ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].timer); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 418 | |
| 419 | tmp_queue = &alarms[ANDROID_ALARM_RTC_WAKEUP]; |
| 420 | if (tmp_queue->first) |
| 421 | wakeup_queue = tmp_queue; |
| 422 | tmp_queue = &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP]; |
| 423 | if (tmp_queue->first && (!wakeup_queue || |
| 424 | hrtimer_get_expires(&tmp_queue->timer).tv64 < |
| 425 | hrtimer_get_expires(&wakeup_queue->timer).tv64)) |
| 426 | wakeup_queue = tmp_queue; |
| 427 | if (wakeup_queue) { |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 428 | rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time); |
Arve Hjønnevåg | b2a4ab2 | 2010-03-12 20:05:32 -0800 | [diff] [blame] | 429 | getnstimeofday(&wall_time); |
| 430 | rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time); |
| 431 | set_normalized_timespec(&rtc_delta, |
| 432 | wall_time.tv_sec - rtc_current_time, |
| 433 | wall_time.tv_nsec); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 434 | |
| 435 | rtc_alarm_time = timespec_sub(ktime_to_timespec( |
| 436 | hrtimer_get_expires(&wakeup_queue->timer)), |
| 437 | rtc_delta).tv_sec; |
| 438 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 439 | rtc_time_to_tm(rtc_alarm_time, &rtc_alarm.time); |
| 440 | rtc_alarm.enabled = 1; |
| 441 | rtc_set_alarm(alarm_rtc_dev, &rtc_alarm); |
| 442 | rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time); |
| 443 | rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 444 | pr_alarm(SUSPEND, |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 445 | "rtc alarm set at %ld, now %ld, rtc delta %ld.%09ld\n", |
| 446 | rtc_alarm_time, rtc_current_time, |
| 447 | rtc_delta.tv_sec, rtc_delta.tv_nsec); |
| 448 | if (rtc_current_time + 1 >= rtc_alarm_time) { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 449 | pr_alarm(SUSPEND, "alarm about to go off\n"); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 450 | memset(&rtc_alarm, 0, sizeof(rtc_alarm)); |
| 451 | rtc_alarm.enabled = 0; |
| 452 | rtc_set_alarm(alarm_rtc_dev, &rtc_alarm); |
| 453 | |
| 454 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 455 | suspended = false; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 456 | wake_lock_timeout(&alarm_rtc_wake_lock, 2 * HZ); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 457 | update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP], |
| 458 | false); |
| 459 | update_timer_locked(&alarms[ |
| 460 | ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP], false); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 461 | err = -EBUSY; |
| 462 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 463 | } |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 464 | } |
| 465 | return err; |
| 466 | } |
| 467 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 468 | static int alarm_resume(struct platform_device *pdev) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 469 | { |
| 470 | struct rtc_wkalrm alarm; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 471 | unsigned long flags; |
| 472 | |
| 473 | pr_alarm(SUSPEND, "alarm_resume(%p)\n", pdev); |
| 474 | |
| 475 | memset(&alarm, 0, sizeof(alarm)); |
| 476 | alarm.enabled = 0; |
| 477 | rtc_set_alarm(alarm_rtc_dev, &alarm); |
| 478 | |
| 479 | spin_lock_irqsave(&alarm_slock, flags); |
| 480 | suspended = false; |
| 481 | update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP], false); |
| 482 | update_timer_locked(&alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP], |
| 483 | false); |
| 484 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 485 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static struct rtc_task alarm_rtc_task = { |
| 490 | .func = alarm_triggered_func |
| 491 | }; |
| 492 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 493 | static int rtc_alarm_add_device(struct device *dev, |
| 494 | struct class_interface *class_intf) |
| 495 | { |
| 496 | int err; |
| 497 | struct rtc_device *rtc = to_rtc_device(dev); |
| 498 | |
| 499 | mutex_lock(&alarm_setrtc_mutex); |
| 500 | |
| 501 | if (alarm_rtc_dev) { |
| 502 | err = -EBUSY; |
| 503 | goto err1; |
| 504 | } |
| 505 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 506 | alarm_platform_dev = |
| 507 | platform_device_register_simple("alarm", -1, NULL, 0); |
| 508 | if (IS_ERR(alarm_platform_dev)) { |
| 509 | err = PTR_ERR(alarm_platform_dev); |
| 510 | goto err2; |
| 511 | } |
| 512 | err = rtc_irq_register(rtc, &alarm_rtc_task); |
| 513 | if (err) |
| 514 | goto err3; |
| 515 | alarm_rtc_dev = rtc; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 516 | pr_alarm(INIT_STATUS, "using rtc device, %s, for alarms", rtc->name); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 517 | mutex_unlock(&alarm_setrtc_mutex); |
| 518 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 519 | return 0; |
| 520 | |
| 521 | err3: |
| 522 | platform_device_unregister(alarm_platform_dev); |
| 523 | err2: |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 524 | err1: |
| 525 | mutex_unlock(&alarm_setrtc_mutex); |
| 526 | return err; |
| 527 | } |
| 528 | |
| 529 | static void rtc_alarm_remove_device(struct device *dev, |
| 530 | struct class_interface *class_intf) |
| 531 | { |
| 532 | if (dev == &alarm_rtc_dev->dev) { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 533 | pr_alarm(INIT_STATUS, "lost rtc device for alarms"); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 534 | rtc_irq_unregister(alarm_rtc_dev, &alarm_rtc_task); |
| 535 | platform_device_unregister(alarm_platform_dev); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 536 | alarm_rtc_dev = NULL; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | static struct class_interface rtc_alarm_interface = { |
| 541 | .add_dev = &rtc_alarm_add_device, |
| 542 | .remove_dev = &rtc_alarm_remove_device, |
| 543 | }; |
| 544 | |
| 545 | static struct platform_driver alarm_driver = { |
| 546 | .suspend = alarm_suspend, |
| 547 | .resume = alarm_resume, |
| 548 | .driver = { |
| 549 | .name = "alarm" |
| 550 | } |
| 551 | }; |
| 552 | |
| 553 | static int __init alarm_late_init(void) |
| 554 | { |
| 555 | unsigned long flags; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 556 | struct timespec tmp_time, system_time; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 557 | |
| 558 | /* this needs to run after the rtc is read at boot */ |
| 559 | spin_lock_irqsave(&alarm_slock, flags); |
| 560 | /* We read the current rtc and system time so we can later calulate |
| 561 | * elasped realtime to be (boot_systemtime + rtc - boot_rtc) == |
| 562 | * (rtc - (boot_rtc - boot_systemtime)) |
| 563 | */ |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 564 | getnstimeofday(&tmp_time); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 565 | ktime_get_ts(&system_time); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 566 | alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta = |
| 567 | alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta = |
| 568 | timespec_to_ktime(timespec_sub(tmp_time, system_time)); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 569 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 570 | spin_unlock_irqrestore(&alarm_slock, flags); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 574 | static int __init alarm_driver_init(void) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 575 | { |
| 576 | int err; |
| 577 | int i; |
| 578 | |
| 579 | for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 580 | hrtimer_init(&alarms[i].timer, |
| 581 | CLOCK_REALTIME, HRTIMER_MODE_ABS); |
| 582 | alarms[i].timer.function = alarm_timer_triggered; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 583 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 584 | hrtimer_init(&alarms[ANDROID_ALARM_SYSTEMTIME].timer, |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 585 | CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 586 | alarms[ANDROID_ALARM_SYSTEMTIME].timer.function = alarm_timer_triggered; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 587 | err = platform_driver_register(&alarm_driver); |
| 588 | if (err < 0) |
| 589 | goto err1; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 590 | wake_lock_init(&alarm_rtc_wake_lock, WAKE_LOCK_SUSPEND, "alarm_rtc"); |
| 591 | rtc_alarm_interface.class = rtc_class; |
| 592 | err = class_interface_register(&rtc_alarm_interface); |
| 593 | if (err < 0) |
| 594 | goto err2; |
| 595 | |
| 596 | return 0; |
| 597 | |
| 598 | err2: |
| 599 | wake_lock_destroy(&alarm_rtc_wake_lock); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 600 | platform_driver_unregister(&alarm_driver); |
| 601 | err1: |
| 602 | return err; |
| 603 | } |
| 604 | |
| 605 | static void __exit alarm_exit(void) |
| 606 | { |
| 607 | class_interface_unregister(&rtc_alarm_interface); |
| 608 | wake_lock_destroy(&alarm_rtc_wake_lock); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 609 | platform_driver_unregister(&alarm_driver); |
| 610 | } |
| 611 | |
| 612 | late_initcall(alarm_late_init); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 613 | module_init(alarm_driver_init); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 614 | module_exit(alarm_exit); |
| 615 | |