3 * Copyright (C) 2007-2009 Google, Inc.
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.
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.
16 #include <linux/time.h>
17 #include <linux/module.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>
24 #include "android_alarm.h"
26 /* XXX - Hack out wakelocks, while they are out of tree */
31 #define wake_lock_timeout(x, y)
32 #define wake_unlock(x)
33 #define WAKE_LOCK_SUSPEND 0
34 #define wake_lock_init(x, y, z) ((x)->i = 1)
35 #define wake_lock_destroy(x)
37 #define ANDROID_ALARM_PRINT_ERROR (1U << 0)
38 #define ANDROID_ALARM_PRINT_INIT_STATUS (1U << 1)
39 #define ANDROID_ALARM_PRINT_TSET (1U << 2)
40 #define ANDROID_ALARM_PRINT_CALL (1U << 3)
41 #define ANDROID_ALARM_PRINT_SUSPEND (1U << 4)
42 #define ANDROID_ALARM_PRINT_INT (1U << 5)
43 #define ANDROID_ALARM_PRINT_FLOW (1U << 6)
45 static int debug_mask = ANDROID_ALARM_PRINT_ERROR | \
46 ANDROID_ALARM_PRINT_INIT_STATUS;
47 module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
49 #define pr_alarm(debug_level_mask, args...) \
51 if (debug_mask & ANDROID_ALARM_PRINT_##debug_level_mask) { \
56 #define ANDROID_ALARM_WAKEUP_MASK ( \
57 ANDROID_ALARM_RTC_WAKEUP_MASK | \
58 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK)
60 /* support old userspace code */
61 #define ANDROID_ALARM_SET_OLD _IOW('a', 2, time_t) /* set alarm */
62 #define ANDROID_ALARM_SET_AND_WAIT_OLD _IOW('a', 3, time_t)
65 struct rb_root alarms;
66 struct rb_node *first;
72 static struct rtc_device *alarm_rtc_dev;
73 static DEFINE_SPINLOCK(alarm_slock);
74 static DEFINE_MUTEX(alarm_setrtc_mutex);
75 static struct wake_lock alarm_rtc_wake_lock;
76 static struct platform_device *alarm_platform_dev;
77 struct alarm_queue alarms[ANDROID_ALARM_TYPE_COUNT];
78 static bool suspended;
80 static void update_timer_locked(struct alarm_queue *base, bool head_removed)
82 struct android_alarm *alarm;
83 bool is_wakeup = base == &alarms[ANDROID_ALARM_RTC_WAKEUP] ||
84 base == &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP];
87 pr_alarm(FLOW, "changed alarm while setting the wall time\n");
91 if (is_wakeup && !suspended && head_removed)
92 wake_unlock(&alarm_rtc_wake_lock);
97 alarm = container_of(base->first, struct android_alarm, node);
99 pr_alarm(FLOW, "selected alarm, type %d, func %pF at %lld\n",
100 alarm->type, alarm->function, ktime_to_ns(alarm->expires));
102 if (is_wakeup && suspended) {
103 pr_alarm(FLOW, "changed alarm while suspened\n");
104 wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ);
108 hrtimer_try_to_cancel(&base->timer);
109 base->timer.node.expires = alarm->expires;
110 base->timer._softexpires = alarm->softexpires;
111 hrtimer_start_expires(&base->timer, HRTIMER_MODE_ABS);
114 static void alarm_enqueue_locked(struct android_alarm *alarm)
116 struct alarm_queue *base = &alarms[alarm->type];
117 struct rb_node **link = &base->alarms.rb_node;
118 struct rb_node *parent = NULL;
119 struct android_alarm *entry;
121 bool was_first = false;
123 pr_alarm(FLOW, "added alarm, type %d, func %pF at %lld\n",
124 alarm->type, alarm->function, ktime_to_ns(alarm->expires));
126 if (base->first == &alarm->node) {
127 base->first = rb_next(&alarm->node);
130 if (!RB_EMPTY_NODE(&alarm->node)) {
131 rb_erase(&alarm->node, &base->alarms);
132 RB_CLEAR_NODE(&alarm->node);
137 entry = rb_entry(parent, struct android_alarm, node);
139 * We dont care about collisions. Nodes with
140 * the same expiry time stay together.
142 if (alarm->expires.tv64 < entry->expires.tv64) {
143 link = &(*link)->rb_left;
145 link = &(*link)->rb_right;
150 base->first = &alarm->node;
151 if (leftmost || was_first)
152 update_timer_locked(base, was_first);
154 rb_link_node(&alarm->node, parent, link);
155 rb_insert_color(&alarm->node, &base->alarms);
159 * android_alarm_init - initialize an alarm
160 * @alarm: the alarm to be initialized
161 * @type: the alarm type to be used
162 * @function: alarm callback function
164 void android_alarm_init(struct android_alarm *alarm,
165 enum android_alarm_type type, void (*function)(struct android_alarm *))
167 RB_CLEAR_NODE(&alarm->node);
169 alarm->function = function;
171 pr_alarm(FLOW, "created alarm, type %d, func %pF\n", type, function);
176 * android_alarm_start_range - (re)start an alarm
177 * @alarm: the alarm to be added
178 * @start: earliest expiry time
181 void android_alarm_start_range(struct android_alarm *alarm, ktime_t start,
186 spin_lock_irqsave(&alarm_slock, flags);
187 alarm->softexpires = start;
188 alarm->expires = end;
189 alarm_enqueue_locked(alarm);
190 spin_unlock_irqrestore(&alarm_slock, flags);
194 * android_alarm_try_to_cancel - try to deactivate an alarm
195 * @alarm: alarm to stop
198 * 0 when the alarm was not active
199 * 1 when the alarm was active
200 * -1 when the alarm may currently be excuting the callback function and
201 * cannot be stopped (it may also be inactive)
203 int android_alarm_try_to_cancel(struct android_alarm *alarm)
205 struct alarm_queue *base = &alarms[alarm->type];
210 spin_lock_irqsave(&alarm_slock, flags);
211 if (!RB_EMPTY_NODE(&alarm->node)) {
212 pr_alarm(FLOW, "canceled alarm, type %d, func %pF at %lld\n",
213 alarm->type, alarm->function,
214 ktime_to_ns(alarm->expires));
216 if (base->first == &alarm->node) {
217 base->first = rb_next(&alarm->node);
220 rb_erase(&alarm->node, &base->alarms);
221 RB_CLEAR_NODE(&alarm->node);
223 update_timer_locked(base, true);
225 pr_alarm(FLOW, "tried to cancel alarm, type %d, func %pF\n",
226 alarm->type, alarm->function);
227 spin_unlock_irqrestore(&alarm_slock, flags);
228 if (!ret && hrtimer_callback_running(&base->timer))
234 * android_alarm_cancel - cancel an alarm and wait for the handler to finish.
235 * @alarm: the alarm to be cancelled
238 * 0 when the alarm was not active
239 * 1 when the alarm was active
241 int android_alarm_cancel(struct android_alarm *alarm)
244 int ret = android_alarm_try_to_cancel(alarm);
252 * alarm_set_rtc - set the kernel and rtc walltime
253 * @new_time: timespec value containing the new time
255 int android_alarm_set_rtc(struct timespec new_time)
260 struct rtc_time rtc_new_rtc_time;
261 struct timespec tmp_time;
263 rtc_time_to_tm(new_time.tv_sec, &rtc_new_rtc_time);
265 pr_alarm(TSET, "set rtc %ld %ld - rtc %02d:%02d:%02d %02d/%02d/%04d\n",
266 new_time.tv_sec, new_time.tv_nsec,
267 rtc_new_rtc_time.tm_hour, rtc_new_rtc_time.tm_min,
268 rtc_new_rtc_time.tm_sec, rtc_new_rtc_time.tm_mon + 1,
269 rtc_new_rtc_time.tm_mday,
270 rtc_new_rtc_time.tm_year + 1900);
272 mutex_lock(&alarm_setrtc_mutex);
273 spin_lock_irqsave(&alarm_slock, flags);
274 wake_lock(&alarm_rtc_wake_lock);
275 getnstimeofday(&tmp_time);
276 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
277 hrtimer_try_to_cancel(&alarms[i].timer);
278 alarms[i].stopped = true;
279 alarms[i].stopped_time = timespec_to_ktime(tmp_time);
281 spin_unlock_irqrestore(&alarm_slock, flags);
282 ret = do_settimeofday(&new_time);
283 spin_lock_irqsave(&alarm_slock, flags);
284 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
285 alarms[i].stopped = false;
286 update_timer_locked(&alarms[i], false);
288 spin_unlock_irqrestore(&alarm_slock, flags);
290 pr_alarm(ERROR, "alarm_set_rtc: Failed to set time\n");
293 if (!alarm_rtc_dev) {
295 "alarm_set_rtc: no RTC, time will be lost on reboot\n");
298 ret = rtc_set_time(alarm_rtc_dev, &rtc_new_rtc_time);
300 pr_alarm(ERROR, "alarm_set_rtc: "
301 "Failed to set RTC, time will be lost on reboot\n");
303 wake_unlock(&alarm_rtc_wake_lock);
304 mutex_unlock(&alarm_setrtc_mutex);
308 static enum hrtimer_restart alarm_timer_triggered(struct hrtimer *timer)
310 struct alarm_queue *base;
311 struct android_alarm *alarm;
315 spin_lock_irqsave(&alarm_slock, flags);
317 base = container_of(timer, struct alarm_queue, timer);
318 now = base->stopped ? base->stopped_time : hrtimer_cb_get_time(timer);
320 pr_alarm(INT, "alarm_timer_triggered type %td at %lld\n",
321 base - alarms, ktime_to_ns(now));
323 while (base->first) {
324 alarm = container_of(base->first, struct android_alarm, node);
325 if (alarm->softexpires.tv64 > now.tv64) {
326 pr_alarm(FLOW, "don't call alarm, %pF, %lld (s %lld)\n",
327 alarm->function, ktime_to_ns(alarm->expires),
328 ktime_to_ns(alarm->softexpires));
331 base->first = rb_next(&alarm->node);
332 rb_erase(&alarm->node, &base->alarms);
333 RB_CLEAR_NODE(&alarm->node);
334 pr_alarm(CALL, "call alarm, type %d, func %pF, %lld (s %lld)\n",
335 alarm->type, alarm->function,
336 ktime_to_ns(alarm->expires),
337 ktime_to_ns(alarm->softexpires));
338 spin_unlock_irqrestore(&alarm_slock, flags);
339 alarm->function(alarm);
340 spin_lock_irqsave(&alarm_slock, flags);
343 pr_alarm(FLOW, "no more alarms of type %td\n", base - alarms);
344 update_timer_locked(base, true);
345 spin_unlock_irqrestore(&alarm_slock, flags);
346 return HRTIMER_NORESTART;
349 static void alarm_triggered_func(void *p)
351 struct rtc_device *rtc = alarm_rtc_dev;
352 if (!(rtc->irq_data & RTC_AF))
354 pr_alarm(INT, "rtc alarm triggered\n");
355 wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ);
358 static int alarm_suspend(struct platform_device *pdev, pm_message_t state)
362 struct rtc_wkalrm rtc_alarm;
363 struct rtc_time rtc_current_rtc_time;
364 unsigned long rtc_current_time;
365 unsigned long rtc_alarm_time;
366 struct timespec rtc_delta;
367 struct timespec wall_time;
368 struct alarm_queue *wakeup_queue = NULL;
369 struct alarm_queue *tmp_queue = NULL;
371 pr_alarm(SUSPEND, "alarm_suspend(%p, %d)\n", pdev, state.event);
373 spin_lock_irqsave(&alarm_slock, flags);
375 spin_unlock_irqrestore(&alarm_slock, flags);
377 hrtimer_cancel(&alarms[ANDROID_ALARM_RTC_WAKEUP].timer);
378 hrtimer_cancel(&alarms[
379 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].timer);
381 tmp_queue = &alarms[ANDROID_ALARM_RTC_WAKEUP];
382 if (tmp_queue->first)
383 wakeup_queue = tmp_queue;
384 tmp_queue = &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP];
385 if (tmp_queue->first && (!wakeup_queue ||
386 hrtimer_get_expires(&tmp_queue->timer).tv64 <
387 hrtimer_get_expires(&wakeup_queue->timer).tv64))
388 wakeup_queue = tmp_queue;
390 rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time);
391 getnstimeofday(&wall_time);
392 rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time);
393 set_normalized_timespec(&rtc_delta,
394 wall_time.tv_sec - rtc_current_time,
397 rtc_alarm_time = timespec_sub(ktime_to_timespec(
398 hrtimer_get_expires(&wakeup_queue->timer)),
401 rtc_time_to_tm(rtc_alarm_time, &rtc_alarm.time);
402 rtc_alarm.enabled = 1;
403 rtc_set_alarm(alarm_rtc_dev, &rtc_alarm);
404 rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time);
405 rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time);
407 "rtc alarm set at %ld, now %ld, rtc delta %ld.%09ld\n",
408 rtc_alarm_time, rtc_current_time,
409 rtc_delta.tv_sec, rtc_delta.tv_nsec);
410 if (rtc_current_time + 1 >= rtc_alarm_time) {
411 pr_alarm(SUSPEND, "alarm about to go off\n");
412 memset(&rtc_alarm, 0, sizeof(rtc_alarm));
413 rtc_alarm.enabled = 0;
414 rtc_set_alarm(alarm_rtc_dev, &rtc_alarm);
416 spin_lock_irqsave(&alarm_slock, flags);
418 wake_lock_timeout(&alarm_rtc_wake_lock, 2 * HZ);
419 update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP],
421 update_timer_locked(&alarms[
422 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP], false);
424 spin_unlock_irqrestore(&alarm_slock, flags);
430 static int alarm_resume(struct platform_device *pdev)
432 struct rtc_wkalrm alarm;
435 pr_alarm(SUSPEND, "alarm_resume(%p)\n", pdev);
437 memset(&alarm, 0, sizeof(alarm));
439 rtc_set_alarm(alarm_rtc_dev, &alarm);
441 spin_lock_irqsave(&alarm_slock, flags);
443 update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP], false);
444 update_timer_locked(&alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP],
446 spin_unlock_irqrestore(&alarm_slock, flags);
451 static struct rtc_task alarm_rtc_task = {
452 .func = alarm_triggered_func
455 static int rtc_alarm_add_device(struct device *dev,
456 struct class_interface *class_intf)
459 struct rtc_device *rtc = to_rtc_device(dev);
461 mutex_lock(&alarm_setrtc_mutex);
469 platform_device_register_simple("alarm", -1, NULL, 0);
470 if (IS_ERR(alarm_platform_dev)) {
471 err = PTR_ERR(alarm_platform_dev);
474 err = rtc_irq_register(rtc, &alarm_rtc_task);
478 pr_alarm(INIT_STATUS, "using rtc device, %s, for alarms", rtc->name);
479 mutex_unlock(&alarm_setrtc_mutex);
484 platform_device_unregister(alarm_platform_dev);
487 mutex_unlock(&alarm_setrtc_mutex);
491 static void rtc_alarm_remove_device(struct device *dev,
492 struct class_interface *class_intf)
494 if (dev == &alarm_rtc_dev->dev) {
495 pr_alarm(INIT_STATUS, "lost rtc device for alarms");
496 rtc_irq_unregister(alarm_rtc_dev, &alarm_rtc_task);
497 platform_device_unregister(alarm_platform_dev);
498 alarm_rtc_dev = NULL;
502 static struct class_interface rtc_alarm_interface = {
503 .add_dev = &rtc_alarm_add_device,
504 .remove_dev = &rtc_alarm_remove_device,
507 static struct platform_driver alarm_driver = {
508 .suspend = alarm_suspend,
509 .resume = alarm_resume,
515 static int __init alarm_driver_init(void)
520 hrtimer_init(&alarms[ANDROID_ALARM_RTC_WAKEUP].timer,
521 CLOCK_REALTIME, HRTIMER_MODE_ABS);
522 hrtimer_init(&alarms[ANDROID_ALARM_RTC].timer,
523 CLOCK_REALTIME, HRTIMER_MODE_ABS);
524 hrtimer_init(&alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].timer,
525 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
526 hrtimer_init(&alarms[ANDROID_ALARM_ELAPSED_REALTIME].timer,
527 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
528 hrtimer_init(&alarms[ANDROID_ALARM_SYSTEMTIME].timer,
529 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
531 for (i = 0; i < ANDROID_ALARM_TYPE_COUNT; i++)
532 alarms[i].timer.function = alarm_timer_triggered;
534 err = platform_driver_register(&alarm_driver);
537 wake_lock_init(&alarm_rtc_wake_lock, WAKE_LOCK_SUSPEND, "alarm_rtc");
538 rtc_alarm_interface.class = rtc_class;
539 err = class_interface_register(&rtc_alarm_interface);
546 wake_lock_destroy(&alarm_rtc_wake_lock);
547 platform_driver_unregister(&alarm_driver);
552 static void __exit alarm_exit(void)
554 class_interface_unregister(&rtc_alarm_interface);
555 wake_lock_destroy(&alarm_rtc_wake_lock);
556 platform_driver_unregister(&alarm_driver);
559 module_init(alarm_driver_init);
560 module_exit(alarm_exit);