blob: c16548807f1e1fad9628b1fc3678601e71c6f774 [file] [log] [blame]
John Stultzff3ead92011-01-11 09:42:13 -08001/*
2 * Alarmtimer interface
3 *
4 * This interface provides a timer which is similarto hrtimers,
5 * but triggers a RTC alarm if the box is suspend.
6 *
7 * This interface is influenced by the Android RTC Alarm timer
8 * interface.
9 *
10 * Copyright (C) 2010 IBM Corperation
11 *
12 * Author: John Stultz <john.stultz@linaro.org>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18#include <linux/time.h>
19#include <linux/hrtimer.h>
20#include <linux/timerqueue.h>
21#include <linux/rtc.h>
22#include <linux/alarmtimer.h>
23#include <linux/mutex.h>
24#include <linux/platform_device.h>
25#include <linux/posix-timers.h>
26#include <linux/workqueue.h>
27#include <linux/freezer.h>
28
John Stultz180bf812011-04-28 12:58:11 -070029/**
30 * struct alarm_base - Alarm timer bases
31 * @lock: Lock for syncrhonized access to the base
32 * @timerqueue: Timerqueue head managing the list of events
33 * @timer: hrtimer used to schedule events while running
34 * @gettime: Function to read the time correlating to the base
35 * @base_clockid: clockid for the base
John Stultz180bf812011-04-28 12:58:11 -070036 */
John Stultzff3ead92011-01-11 09:42:13 -080037static struct alarm_base {
38 spinlock_t lock;
39 struct timerqueue_head timerqueue;
40 struct hrtimer timer;
41 ktime_t (*gettime)(void);
42 clockid_t base_clockid;
John Stultzff3ead92011-01-11 09:42:13 -080043} alarm_bases[ALARM_NUMTYPE];
44
John Stultzc008ba582011-06-16 18:27:09 -070045/* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
46static ktime_t freezer_delta;
47static DEFINE_SPINLOCK(freezer_delta_lock);
48
John Stultzad30dfa2012-03-23 15:52:25 -070049static struct rtc_timer rtctimer;
50
John Stultz472647d2011-04-29 15:03:10 -070051#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -070052/* rtc timer and device for setting alarm wakeups at suspend */
John Stultzff3ead92011-01-11 09:42:13 -080053static struct rtc_device *rtcdev;
John Stultzc008ba582011-06-16 18:27:09 -070054static DEFINE_SPINLOCK(rtcdev_lock);
John Stultzff3ead92011-01-11 09:42:13 -080055
John Stultzc008ba582011-06-16 18:27:09 -070056/**
John Stultzc008ba582011-06-16 18:27:09 -070057 * alarmtimer_get_rtcdev - Return selected rtcdevice
58 *
59 * This function returns the rtc device to use for wakealarms.
60 * If one has not already been chosen, it checks to see if a
61 * functional rtc device is available.
62 */
63static struct rtc_device *alarmtimer_get_rtcdev(void)
64{
John Stultzc008ba582011-06-16 18:27:09 -070065 unsigned long flags;
66 struct rtc_device *ret;
67
68 spin_lock_irqsave(&rtcdev_lock, flags);
John Stultzc008ba582011-06-16 18:27:09 -070069 ret = rtcdev;
70 spin_unlock_irqrestore(&rtcdev_lock, flags);
71
72 return ret;
73}
John Stultz8bc0daf2011-07-14 18:35:13 -070074
75
76static int alarmtimer_rtc_add_device(struct device *dev,
77 struct class_interface *class_intf)
78{
79 unsigned long flags;
80 struct rtc_device *rtc = to_rtc_device(dev);
81
82 if (rtcdev)
83 return -EBUSY;
84
85 if (!rtc->ops->set_alarm)
86 return -1;
87 if (!device_may_wakeup(rtc->dev.parent))
88 return -1;
89
90 spin_lock_irqsave(&rtcdev_lock, flags);
91 if (!rtcdev) {
92 rtcdev = rtc;
93 /* hold a reference so it doesn't go away */
94 get_device(dev);
95 }
96 spin_unlock_irqrestore(&rtcdev_lock, flags);
97 return 0;
98}
99
100static struct class_interface alarmtimer_rtc_interface = {
101 .add_dev = &alarmtimer_rtc_add_device,
102};
103
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200104static int alarmtimer_rtc_interface_setup(void)
John Stultz8bc0daf2011-07-14 18:35:13 -0700105{
106 alarmtimer_rtc_interface.class = rtc_class;
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200107 return class_interface_register(&alarmtimer_rtc_interface);
108}
109static void alarmtimer_rtc_interface_remove(void)
110{
111 class_interface_unregister(&alarmtimer_rtc_interface);
John Stultz8bc0daf2011-07-14 18:35:13 -0700112}
John Stultz1c6b39a2011-06-16 18:47:37 -0700113#else
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200114static inline struct rtc_device *alarmtimer_get_rtcdev(void)
115{
116 return NULL;
117}
118#define rtcdev (NULL)
119static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
120static inline void alarmtimer_rtc_interface_remove(void) { }
John Stultzc008ba582011-06-16 18:27:09 -0700121#endif
John Stultzff3ead92011-01-11 09:42:13 -0800122
John Stultz180bf812011-04-28 12:58:11 -0700123/**
John Stultzff3ead92011-01-11 09:42:13 -0800124 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
125 * @base: pointer to the base where the timer is being run
126 * @alarm: pointer to alarm being enqueued.
127 *
128 * Adds alarm to a alarm_base timerqueue and if necessary sets
129 * an hrtimer to run.
130 *
131 * Must hold base->lock when calling.
132 */
133static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
134{
135 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700136 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
137
John Stultzff3ead92011-01-11 09:42:13 -0800138 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
139 hrtimer_try_to_cancel(&base->timer);
140 hrtimer_start(&base->timer, alarm->node.expires,
141 HRTIMER_MODE_ABS);
142 }
143}
144
John Stultz180bf812011-04-28 12:58:11 -0700145/**
John Stultzff3ead92011-01-11 09:42:13 -0800146 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
147 * @base: pointer to the base where the timer is running
148 * @alarm: pointer to alarm being removed
149 *
150 * Removes alarm to a alarm_base timerqueue and if necessary sets
151 * a new timer to run.
152 *
153 * Must hold base->lock when calling.
154 */
155static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
156{
157 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
158
John Stultza28cde82011-08-10 12:30:21 -0700159 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
160 return;
161
John Stultzff3ead92011-01-11 09:42:13 -0800162 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700163 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
164
John Stultzff3ead92011-01-11 09:42:13 -0800165 if (next == &alarm->node) {
166 hrtimer_try_to_cancel(&base->timer);
167 next = timerqueue_getnext(&base->timerqueue);
168 if (!next)
169 return;
170 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
171 }
172}
173
John Stultz7068b7a2011-04-28 13:29:18 -0700174
John Stultz180bf812011-04-28 12:58:11 -0700175/**
John Stultz7068b7a2011-04-28 13:29:18 -0700176 * alarmtimer_fired - Handles alarm hrtimer being fired.
177 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800178 *
John Stultz180bf812011-04-28 12:58:11 -0700179 * When a alarm timer fires, this runs through the timerqueue to
180 * see which alarms expired, and runs those. If there are more alarm
181 * timers queued for the future, we set the hrtimer to fire when
182 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800183 */
John Stultz7068b7a2011-04-28 13:29:18 -0700184static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800185{
John Stultz7068b7a2011-04-28 13:29:18 -0700186 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
John Stultzff3ead92011-01-11 09:42:13 -0800187 struct timerqueue_node *next;
188 unsigned long flags;
189 ktime_t now;
John Stultz7068b7a2011-04-28 13:29:18 -0700190 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700191 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800192
193 spin_lock_irqsave(&base->lock, flags);
194 now = base->gettime();
195 while ((next = timerqueue_getnext(&base->timerqueue))) {
196 struct alarm *alarm;
197 ktime_t expired = next->expires;
198
Thomas Gleixnerc9c024b2011-12-05 21:20:23 +0100199 if (expired.tv64 > now.tv64)
John Stultzff3ead92011-01-11 09:42:13 -0800200 break;
201
202 alarm = container_of(next, struct alarm, node);
203
204 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700205 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultz54da23b2011-08-10 11:08:07 -0700206
John Stultza28cde82011-08-10 12:30:21 -0700207 alarm->state |= ALARMTIMER_STATE_CALLBACK;
John Stultz54da23b2011-08-10 11:08:07 -0700208 spin_unlock_irqrestore(&base->lock, flags);
209 if (alarm->function)
210 restart = alarm->function(alarm, now);
211 spin_lock_irqsave(&base->lock, flags);
John Stultza28cde82011-08-10 12:30:21 -0700212 alarm->state &= ~ALARMTIMER_STATE_CALLBACK;
John Stultz54da23b2011-08-10 11:08:07 -0700213
214 if (restart != ALARMTIMER_NORESTART) {
John Stultzff3ead92011-01-11 09:42:13 -0800215 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700216 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800217 }
John Stultzff3ead92011-01-11 09:42:13 -0800218 }
219
220 if (next) {
John Stultz7068b7a2011-04-28 13:29:18 -0700221 hrtimer_set_expires(&base->timer, next->expires);
222 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800223 }
224 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800225
John Stultz7068b7a2011-04-28 13:29:18 -0700226 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800227
John Stultzff3ead92011-01-11 09:42:13 -0800228}
229
John Stultz472647d2011-04-29 15:03:10 -0700230#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700231/**
John Stultzff3ead92011-01-11 09:42:13 -0800232 * alarmtimer_suspend - Suspend time callback
233 * @dev: unused
234 * @state: unused
235 *
236 * When we are going into suspend, we look through the bases
237 * to see which is the soonest timer to expire. We then
238 * set an rtc timer to fire that far into the future, which
239 * will wake us from suspend.
240 */
241static int alarmtimer_suspend(struct device *dev)
242{
243 struct rtc_time tm;
244 ktime_t min, now;
245 unsigned long flags;
John Stultzc008ba582011-06-16 18:27:09 -0700246 struct rtc_device *rtc;
John Stultzff3ead92011-01-11 09:42:13 -0800247 int i;
248
249 spin_lock_irqsave(&freezer_delta_lock, flags);
250 min = freezer_delta;
251 freezer_delta = ktime_set(0, 0);
252 spin_unlock_irqrestore(&freezer_delta_lock, flags);
253
John Stultz8bc0daf2011-07-14 18:35:13 -0700254 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800255 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700256 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800257 return 0;
258
259 /* Find the soonest timer to expire*/
260 for (i = 0; i < ALARM_NUMTYPE; i++) {
261 struct alarm_base *base = &alarm_bases[i];
262 struct timerqueue_node *next;
263 ktime_t delta;
264
265 spin_lock_irqsave(&base->lock, flags);
266 next = timerqueue_getnext(&base->timerqueue);
267 spin_unlock_irqrestore(&base->lock, flags);
268 if (!next)
269 continue;
270 delta = ktime_sub(next->expires, base->gettime());
271 if (!min.tv64 || (delta.tv64 < min.tv64))
272 min = delta;
273 }
274 if (min.tv64 == 0)
275 return 0;
276
277 /* XXX - Should we enforce a minimum sleep time? */
278 WARN_ON(min.tv64 < NSEC_PER_SEC);
279
280 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700281 rtc_timer_cancel(rtc, &rtctimer);
282 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800283 now = rtc_tm_to_ktime(tm);
284 now = ktime_add(now, min);
285
John Stultzc008ba582011-06-16 18:27:09 -0700286 rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
John Stultzff3ead92011-01-11 09:42:13 -0800287
288 return 0;
289}
John Stultz472647d2011-04-29 15:03:10 -0700290#else
291static int alarmtimer_suspend(struct device *dev)
292{
293 return 0;
294}
295#endif
John Stultzff3ead92011-01-11 09:42:13 -0800296
John Stultz9a7adcf2011-01-11 09:54:33 -0800297static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
298{
299 ktime_t delta;
300 unsigned long flags;
301 struct alarm_base *base = &alarm_bases[type];
302
303 delta = ktime_sub(absexp, base->gettime());
304
305 spin_lock_irqsave(&freezer_delta_lock, flags);
306 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
307 freezer_delta = delta;
308 spin_unlock_irqrestore(&freezer_delta_lock, flags);
309}
310
311
John Stultz180bf812011-04-28 12:58:11 -0700312/**
John Stultzff3ead92011-01-11 09:42:13 -0800313 * alarm_init - Initialize an alarm structure
314 * @alarm: ptr to alarm to be initialized
315 * @type: the type of the alarm
316 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800317 */
318void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700319 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800320{
321 timerqueue_init(&alarm->node);
John Stultzff3ead92011-01-11 09:42:13 -0800322 alarm->function = function;
323 alarm->type = type;
John Stultza28cde82011-08-10 12:30:21 -0700324 alarm->state = ALARMTIMER_STATE_INACTIVE;
John Stultzff3ead92011-01-11 09:42:13 -0800325}
326
John Stultz180bf812011-04-28 12:58:11 -0700327/**
John Stultzff3ead92011-01-11 09:42:13 -0800328 * alarm_start - Sets an alarm to fire
329 * @alarm: ptr to alarm to set
330 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800331 */
John Stultz9e264762011-08-10 12:09:24 -0700332void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800333{
334 struct alarm_base *base = &alarm_bases[alarm->type];
335 unsigned long flags;
336
337 spin_lock_irqsave(&base->lock, flags);
John Stultza28cde82011-08-10 12:30:21 -0700338 if (alarmtimer_active(alarm))
John Stultzff3ead92011-01-11 09:42:13 -0800339 alarmtimer_remove(base, alarm);
340 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800341 alarmtimer_enqueue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800342 spin_unlock_irqrestore(&base->lock, flags);
343}
344
John Stultz180bf812011-04-28 12:58:11 -0700345/**
John Stultz9082c462011-08-10 12:41:36 -0700346 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800347 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700348 *
349 * Returns 1 if the timer was canceled, 0 if it was not running,
350 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800351 */
John Stultz9082c462011-08-10 12:41:36 -0700352int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800353{
354 struct alarm_base *base = &alarm_bases[alarm->type];
355 unsigned long flags;
John Stultz9082c462011-08-10 12:41:36 -0700356 int ret = -1;
John Stultzff3ead92011-01-11 09:42:13 -0800357 spin_lock_irqsave(&base->lock, flags);
John Stultz9082c462011-08-10 12:41:36 -0700358
359 if (alarmtimer_callback_running(alarm))
360 goto out;
361
362 if (alarmtimer_is_queued(alarm)) {
John Stultzff3ead92011-01-11 09:42:13 -0800363 alarmtimer_remove(base, alarm);
John Stultz9082c462011-08-10 12:41:36 -0700364 ret = 1;
365 } else
366 ret = 0;
367out:
John Stultzff3ead92011-01-11 09:42:13 -0800368 spin_unlock_irqrestore(&base->lock, flags);
John Stultz9082c462011-08-10 12:41:36 -0700369 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800370}
371
372
John Stultz9082c462011-08-10 12:41:36 -0700373/**
374 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
375 * @alarm: ptr to alarm to be canceled
376 *
377 * Returns 1 if the timer was canceled, 0 if it was not active.
378 */
379int alarm_cancel(struct alarm *alarm)
380{
381 for (;;) {
382 int ret = alarm_try_to_cancel(alarm);
383 if (ret >= 0)
384 return ret;
385 cpu_relax();
386 }
387}
388
John Stultzdce75a82011-08-10 11:31:03 -0700389
390u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
391{
392 u64 overrun = 1;
393 ktime_t delta;
394
395 delta = ktime_sub(now, alarm->node.expires);
396
397 if (delta.tv64 < 0)
398 return 0;
399
400 if (unlikely(delta.tv64 >= interval.tv64)) {
401 s64 incr = ktime_to_ns(interval);
402
403 overrun = ktime_divns(delta, incr);
404
405 alarm->node.expires = ktime_add_ns(alarm->node.expires,
406 incr*overrun);
407
408 if (alarm->node.expires.tv64 > now.tv64)
409 return overrun;
410 /*
411 * This (and the ktime_add() below) is the
412 * correction for exact:
413 */
414 overrun++;
415 }
416
417 alarm->node.expires = ktime_add(alarm->node.expires, interval);
418 return overrun;
419}
420
421
422
423
John Stultz180bf812011-04-28 12:58:11 -0700424/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800425 * clock2alarm - helper that converts from clockid to alarmtypes
426 * @clockid: clockid.
John Stultz9a7adcf2011-01-11 09:54:33 -0800427 */
428static enum alarmtimer_type clock2alarm(clockid_t clockid)
429{
430 if (clockid == CLOCK_REALTIME_ALARM)
431 return ALARM_REALTIME;
432 if (clockid == CLOCK_BOOTTIME_ALARM)
433 return ALARM_BOOTTIME;
434 return -1;
435}
436
John Stultz180bf812011-04-28 12:58:11 -0700437/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800438 * alarm_handle_timer - Callback for posix timers
439 * @alarm: alarm that fired
440 *
441 * Posix timer callback for expired alarm timers.
442 */
John Stultz4b413082011-08-10 10:37:59 -0700443static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
444 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800445{
446 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
John Stultz9e264762011-08-10 12:09:24 -0700447 it.alarm.alarmtimer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800448 if (posix_timer_event(ptr, 0) != 0)
449 ptr->it_overrun++;
John Stultz4b413082011-08-10 10:37:59 -0700450
John Stultz54da23b2011-08-10 11:08:07 -0700451 /* Re-add periodic timers */
John Stultz9e264762011-08-10 12:09:24 -0700452 if (ptr->it.alarm.interval.tv64) {
453 ptr->it_overrun += alarm_forward(alarm, now,
454 ptr->it.alarm.interval);
John Stultz54da23b2011-08-10 11:08:07 -0700455 return ALARMTIMER_RESTART;
456 }
John Stultz4b413082011-08-10 10:37:59 -0700457 return ALARMTIMER_NORESTART;
John Stultz9a7adcf2011-01-11 09:54:33 -0800458}
459
John Stultz180bf812011-04-28 12:58:11 -0700460/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800461 * alarm_clock_getres - posix getres interface
462 * @which_clock: clockid
463 * @tp: timespec to fill
464 *
465 * Returns the granularity of underlying alarm base clock
466 */
467static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
468{
469 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
470
John Stultz1c6b39a2011-06-16 18:47:37 -0700471 if (!alarmtimer_get_rtcdev())
472 return -ENOTSUPP;
473
John Stultz9a7adcf2011-01-11 09:54:33 -0800474 return hrtimer_get_res(baseid, tp);
475}
476
477/**
478 * alarm_clock_get - posix clock_get interface
479 * @which_clock: clockid
480 * @tp: timespec to fill.
481 *
482 * Provides the underlying alarm base time.
483 */
484static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
485{
486 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
487
John Stultz1c6b39a2011-06-16 18:47:37 -0700488 if (!alarmtimer_get_rtcdev())
489 return -ENOTSUPP;
490
John Stultz9a7adcf2011-01-11 09:54:33 -0800491 *tp = ktime_to_timespec(base->gettime());
492 return 0;
493}
494
495/**
496 * alarm_timer_create - posix timer_create interface
497 * @new_timer: k_itimer pointer to manage
498 *
499 * Initializes the k_itimer structure.
500 */
501static int alarm_timer_create(struct k_itimer *new_timer)
502{
503 enum alarmtimer_type type;
504 struct alarm_base *base;
505
John Stultz1c6b39a2011-06-16 18:47:37 -0700506 if (!alarmtimer_get_rtcdev())
507 return -ENOTSUPP;
508
John Stultz9a7adcf2011-01-11 09:54:33 -0800509 if (!capable(CAP_WAKE_ALARM))
510 return -EPERM;
511
512 type = clock2alarm(new_timer->it_clock);
513 base = &alarm_bases[type];
John Stultz9e264762011-08-10 12:09:24 -0700514 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800515 return 0;
516}
517
518/**
519 * alarm_timer_get - posix timer_get interface
520 * @new_timer: k_itimer pointer
521 * @cur_setting: itimerspec data to fill
522 *
523 * Copies the itimerspec data out from the k_itimer
524 */
525static void alarm_timer_get(struct k_itimer *timr,
526 struct itimerspec *cur_setting)
527{
John Stultzea7802f2011-08-04 07:51:56 -0700528 memset(cur_setting, 0, sizeof(struct itimerspec));
529
John Stultz9a7adcf2011-01-11 09:54:33 -0800530 cur_setting->it_interval =
John Stultz9e264762011-08-10 12:09:24 -0700531 ktime_to_timespec(timr->it.alarm.interval);
John Stultz9a7adcf2011-01-11 09:54:33 -0800532 cur_setting->it_value =
John Stultz9e264762011-08-10 12:09:24 -0700533 ktime_to_timespec(timr->it.alarm.alarmtimer.node.expires);
John Stultz9a7adcf2011-01-11 09:54:33 -0800534 return;
535}
536
537/**
538 * alarm_timer_del - posix timer_del interface
539 * @timr: k_itimer pointer to be deleted
540 *
541 * Cancels any programmed alarms for the given timer.
542 */
543static int alarm_timer_del(struct k_itimer *timr)
544{
John Stultz1c6b39a2011-06-16 18:47:37 -0700545 if (!rtcdev)
546 return -ENOTSUPP;
547
John Stultz9082c462011-08-10 12:41:36 -0700548 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
549 return TIMER_RETRY;
550
John Stultz9a7adcf2011-01-11 09:54:33 -0800551 return 0;
552}
553
554/**
555 * alarm_timer_set - posix timer_set interface
556 * @timr: k_itimer pointer to be deleted
557 * @flags: timer flags
558 * @new_setting: itimerspec to be used
559 * @old_setting: itimerspec being replaced
560 *
561 * Sets the timer to new_setting, and starts the timer.
562 */
563static int alarm_timer_set(struct k_itimer *timr, int flags,
564 struct itimerspec *new_setting,
565 struct itimerspec *old_setting)
566{
John Stultz1c6b39a2011-06-16 18:47:37 -0700567 if (!rtcdev)
568 return -ENOTSUPP;
569
John Stultz971c90b2011-08-04 07:25:35 -0700570 if (old_setting)
571 alarm_timer_get(timr, old_setting);
John Stultz9a7adcf2011-01-11 09:54:33 -0800572
573 /* If the timer was already set, cancel it */
John Stultz9082c462011-08-10 12:41:36 -0700574 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
575 return TIMER_RETRY;
John Stultz9a7adcf2011-01-11 09:54:33 -0800576
577 /* start the timer */
John Stultz9e264762011-08-10 12:09:24 -0700578 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
579 alarm_start(&timr->it.alarm.alarmtimer,
580 timespec_to_ktime(new_setting->it_value));
John Stultz9a7adcf2011-01-11 09:54:33 -0800581 return 0;
582}
583
584/**
585 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
586 * @alarm: ptr to alarm that fired
587 *
588 * Wakes up the task that set the alarmtimer
589 */
John Stultz4b413082011-08-10 10:37:59 -0700590static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
591 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800592{
593 struct task_struct *task = (struct task_struct *)alarm->data;
594
595 alarm->data = NULL;
596 if (task)
597 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700598 return ALARMTIMER_NORESTART;
John Stultz9a7adcf2011-01-11 09:54:33 -0800599}
600
601/**
602 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
603 * @alarm: ptr to alarmtimer
604 * @absexp: absolute expiration time
605 *
606 * Sets the alarm timer and sleeps until it is fired or interrupted.
607 */
608static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
609{
610 alarm->data = (void *)current;
611 do {
612 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700613 alarm_start(alarm, absexp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800614 if (likely(alarm->data))
615 schedule();
616
617 alarm_cancel(alarm);
618 } while (alarm->data && !signal_pending(current));
619
620 __set_current_state(TASK_RUNNING);
621
622 return (alarm->data == NULL);
623}
624
625
626/**
627 * update_rmtp - Update remaining timespec value
628 * @exp: expiration time
629 * @type: timer type
630 * @rmtp: user pointer to remaining timepsec value
631 *
632 * Helper function that fills in rmtp value with time between
633 * now and the exp value
634 */
635static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
636 struct timespec __user *rmtp)
637{
638 struct timespec rmt;
639 ktime_t rem;
640
641 rem = ktime_sub(exp, alarm_bases[type].gettime());
642
643 if (rem.tv64 <= 0)
644 return 0;
645 rmt = ktime_to_timespec(rem);
646
647 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
648 return -EFAULT;
649
650 return 1;
651
652}
653
654/**
655 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
656 * @restart: ptr to restart block
657 *
658 * Handles restarted clock_nanosleep calls
659 */
660static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
661{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200662 enum alarmtimer_type type = restart->nanosleep.clockid;
John Stultz9a7adcf2011-01-11 09:54:33 -0800663 ktime_t exp;
664 struct timespec __user *rmtp;
665 struct alarm alarm;
666 int ret = 0;
667
668 exp.tv64 = restart->nanosleep.expires;
669 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
670
671 if (alarmtimer_do_nsleep(&alarm, exp))
672 goto out;
673
674 if (freezing(current))
675 alarmtimer_freezerset(exp, type);
676
677 rmtp = restart->nanosleep.rmtp;
678 if (rmtp) {
679 ret = update_rmtp(exp, type, rmtp);
680 if (ret <= 0)
681 goto out;
682 }
683
684
685 /* The other values in restart are already filled in */
686 ret = -ERESTART_RESTARTBLOCK;
687out:
688 return ret;
689}
690
691/**
692 * alarm_timer_nsleep - alarmtimer nanosleep
693 * @which_clock: clockid
694 * @flags: determins abstime or relative
695 * @tsreq: requested sleep time (abs or rel)
696 * @rmtp: remaining sleep time saved
697 *
698 * Handles clock_nanosleep calls against _ALARM clockids
699 */
700static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
701 struct timespec *tsreq, struct timespec __user *rmtp)
702{
703 enum alarmtimer_type type = clock2alarm(which_clock);
704 struct alarm alarm;
705 ktime_t exp;
706 int ret = 0;
707 struct restart_block *restart;
708
John Stultz1c6b39a2011-06-16 18:47:37 -0700709 if (!alarmtimer_get_rtcdev())
710 return -ENOTSUPP;
711
John Stultz9a7adcf2011-01-11 09:54:33 -0800712 if (!capable(CAP_WAKE_ALARM))
713 return -EPERM;
714
715 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
716
717 exp = timespec_to_ktime(*tsreq);
718 /* Convert (if necessary) to absolute time */
719 if (flags != TIMER_ABSTIME) {
720 ktime_t now = alarm_bases[type].gettime();
721 exp = ktime_add(now, exp);
722 }
723
724 if (alarmtimer_do_nsleep(&alarm, exp))
725 goto out;
726
727 if (freezing(current))
728 alarmtimer_freezerset(exp, type);
729
730 /* abs timers don't set remaining time or restart */
731 if (flags == TIMER_ABSTIME) {
732 ret = -ERESTARTNOHAND;
733 goto out;
734 }
735
736 if (rmtp) {
737 ret = update_rmtp(exp, type, rmtp);
738 if (ret <= 0)
739 goto out;
740 }
741
742 restart = &current_thread_info()->restart_block;
743 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200744 restart->nanosleep.clockid = type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800745 restart->nanosleep.expires = exp.tv64;
746 restart->nanosleep.rmtp = rmtp;
747 ret = -ERESTART_RESTARTBLOCK;
748
749out:
750 return ret;
751}
John Stultzff3ead92011-01-11 09:42:13 -0800752
John Stultzff3ead92011-01-11 09:42:13 -0800753
754/* Suspend hook structures */
755static const struct dev_pm_ops alarmtimer_pm_ops = {
756 .suspend = alarmtimer_suspend,
757};
758
759static struct platform_driver alarmtimer_driver = {
760 .driver = {
761 .name = "alarmtimer",
762 .pm = &alarmtimer_pm_ops,
763 }
764};
765
766/**
767 * alarmtimer_init - Initialize alarm timer code
768 *
769 * This function initializes the alarm bases and registers
770 * the posix clock ids.
771 */
772static int __init alarmtimer_init(void)
773{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200774 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800775 int error = 0;
776 int i;
John Stultz9a7adcf2011-01-11 09:54:33 -0800777 struct k_clock alarm_clock = {
778 .clock_getres = alarm_clock_getres,
779 .clock_get = alarm_clock_get,
780 .timer_create = alarm_timer_create,
781 .timer_set = alarm_timer_set,
782 .timer_del = alarm_timer_del,
783 .timer_get = alarm_timer_get,
784 .nsleep = alarm_timer_nsleep,
785 };
786
John Stultzad30dfa2012-03-23 15:52:25 -0700787 rtc_timer_init(&rtctimer, NULL, NULL);
788
John Stultz9a7adcf2011-01-11 09:54:33 -0800789 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
790 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
John Stultzff3ead92011-01-11 09:42:13 -0800791
792 /* Initialize alarm bases */
793 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
794 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
795 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
796 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
797 for (i = 0; i < ALARM_NUMTYPE; i++) {
798 timerqueue_init_head(&alarm_bases[i].timerqueue);
799 spin_lock_init(&alarm_bases[i].lock);
800 hrtimer_init(&alarm_bases[i].timer,
801 alarm_bases[i].base_clockid,
802 HRTIMER_MODE_ABS);
803 alarm_bases[i].timer.function = alarmtimer_fired;
John Stultzff3ead92011-01-11 09:42:13 -0800804 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700805
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200806 error = alarmtimer_rtc_interface_setup();
807 if (error)
808 return error;
John Stultzff3ead92011-01-11 09:42:13 -0800809
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200810 error = platform_driver_register(&alarmtimer_driver);
811 if (error)
812 goto out_if;
813
814 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
815 if (IS_ERR(pdev)) {
816 error = PTR_ERR(pdev);
817 goto out_drv;
818 }
819 return 0;
820
821out_drv:
822 platform_driver_unregister(&alarmtimer_driver);
823out_if:
824 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -0800825 return error;
826}
827device_initcall(alarmtimer_init);