blob: 54e7145c54145b32b6d2290144d8f9dcea0db6e6 [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
Todd Poynor59a93c22012-08-09 00:37:27 -070049static struct wakeup_source *ws;
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 */
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +010053static struct rtc_timer rtctimer;
John Stultzff3ead92011-01-11 09:42:13 -080054static struct rtc_device *rtcdev;
John Stultzc008ba582011-06-16 18:27:09 -070055static DEFINE_SPINLOCK(rtcdev_lock);
John Stultzff3ead92011-01-11 09:42:13 -080056
John Stultzc008ba582011-06-16 18:27:09 -070057/**
John Stultzc008ba582011-06-16 18:27:09 -070058 * alarmtimer_get_rtcdev - Return selected rtcdevice
59 *
60 * This function returns the rtc device to use for wakealarms.
61 * If one has not already been chosen, it checks to see if a
62 * functional rtc device is available.
63 */
John Stultz57c498f2012-04-20 12:31:45 -070064struct rtc_device *alarmtimer_get_rtcdev(void)
John Stultzc008ba582011-06-16 18:27:09 -070065{
John Stultzc008ba582011-06-16 18:27:09 -070066 unsigned long flags;
67 struct rtc_device *ret;
68
69 spin_lock_irqsave(&rtcdev_lock, flags);
John Stultzc008ba582011-06-16 18:27:09 -070070 ret = rtcdev;
71 spin_unlock_irqrestore(&rtcdev_lock, flags);
72
73 return ret;
74}
John Stultz8bc0daf2011-07-14 18:35:13 -070075
76
77static int alarmtimer_rtc_add_device(struct device *dev,
78 struct class_interface *class_intf)
79{
80 unsigned long flags;
81 struct rtc_device *rtc = to_rtc_device(dev);
82
83 if (rtcdev)
84 return -EBUSY;
85
86 if (!rtc->ops->set_alarm)
87 return -1;
88 if (!device_may_wakeup(rtc->dev.parent))
89 return -1;
90
91 spin_lock_irqsave(&rtcdev_lock, flags);
92 if (!rtcdev) {
93 rtcdev = rtc;
94 /* hold a reference so it doesn't go away */
95 get_device(dev);
96 }
97 spin_unlock_irqrestore(&rtcdev_lock, flags);
98 return 0;
99}
100
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100101static inline void alarmtimer_rtc_timer_init(void)
102{
103 rtc_timer_init(&rtctimer, NULL, NULL);
104}
105
John Stultz8bc0daf2011-07-14 18:35:13 -0700106static struct class_interface alarmtimer_rtc_interface = {
107 .add_dev = &alarmtimer_rtc_add_device,
108};
109
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200110static int alarmtimer_rtc_interface_setup(void)
John Stultz8bc0daf2011-07-14 18:35:13 -0700111{
112 alarmtimer_rtc_interface.class = rtc_class;
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200113 return class_interface_register(&alarmtimer_rtc_interface);
114}
115static void alarmtimer_rtc_interface_remove(void)
116{
117 class_interface_unregister(&alarmtimer_rtc_interface);
John Stultz8bc0daf2011-07-14 18:35:13 -0700118}
John Stultz1c6b39a2011-06-16 18:47:37 -0700119#else
John Stultz57c498f2012-04-20 12:31:45 -0700120struct rtc_device *alarmtimer_get_rtcdev(void)
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200121{
122 return NULL;
123}
124#define rtcdev (NULL)
125static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
126static inline void alarmtimer_rtc_interface_remove(void) { }
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100127static inline void alarmtimer_rtc_timer_init(void) { }
John Stultzc008ba582011-06-16 18:27:09 -0700128#endif
John Stultzff3ead92011-01-11 09:42:13 -0800129
John Stultz180bf812011-04-28 12:58:11 -0700130/**
John Stultzff3ead92011-01-11 09:42:13 -0800131 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
132 * @base: pointer to the base where the timer is being run
133 * @alarm: pointer to alarm being enqueued.
134 *
135 * Adds alarm to a alarm_base timerqueue and if necessary sets
136 * an hrtimer to run.
137 *
138 * Must hold base->lock when calling.
139 */
140static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
141{
142 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700143 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
144
John Stultzff3ead92011-01-11 09:42:13 -0800145 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
146 hrtimer_try_to_cancel(&base->timer);
147 hrtimer_start(&base->timer, alarm->node.expires,
148 HRTIMER_MODE_ABS);
149 }
150}
151
John Stultz180bf812011-04-28 12:58:11 -0700152/**
John Stultzff3ead92011-01-11 09:42:13 -0800153 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
154 * @base: pointer to the base where the timer is running
155 * @alarm: pointer to alarm being removed
156 *
157 * Removes alarm to a alarm_base timerqueue and if necessary sets
158 * a new timer to run.
159 *
160 * Must hold base->lock when calling.
161 */
162static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
163{
164 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
165
John Stultza28cde82011-08-10 12:30:21 -0700166 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
167 return;
168
John Stultzff3ead92011-01-11 09:42:13 -0800169 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700170 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
171
John Stultzff3ead92011-01-11 09:42:13 -0800172 if (next == &alarm->node) {
173 hrtimer_try_to_cancel(&base->timer);
174 next = timerqueue_getnext(&base->timerqueue);
175 if (!next)
176 return;
177 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
178 }
179}
180
John Stultz7068b7a2011-04-28 13:29:18 -0700181
John Stultz180bf812011-04-28 12:58:11 -0700182/**
John Stultz7068b7a2011-04-28 13:29:18 -0700183 * alarmtimer_fired - Handles alarm hrtimer being fired.
184 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800185 *
John Stultz180bf812011-04-28 12:58:11 -0700186 * When a alarm timer fires, this runs through the timerqueue to
187 * see which alarms expired, and runs those. If there are more alarm
188 * timers queued for the future, we set the hrtimer to fire when
189 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800190 */
John Stultz7068b7a2011-04-28 13:29:18 -0700191static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800192{
John Stultz7068b7a2011-04-28 13:29:18 -0700193 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
John Stultzff3ead92011-01-11 09:42:13 -0800194 struct timerqueue_node *next;
195 unsigned long flags;
196 ktime_t now;
John Stultz7068b7a2011-04-28 13:29:18 -0700197 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700198 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800199
200 spin_lock_irqsave(&base->lock, flags);
201 now = base->gettime();
202 while ((next = timerqueue_getnext(&base->timerqueue))) {
203 struct alarm *alarm;
204 ktime_t expired = next->expires;
205
Thomas Gleixnerc9c024b2011-12-05 21:20:23 +0100206 if (expired.tv64 > now.tv64)
John Stultzff3ead92011-01-11 09:42:13 -0800207 break;
208
209 alarm = container_of(next, struct alarm, node);
210
211 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700212 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultz54da23b2011-08-10 11:08:07 -0700213
John Stultza28cde82011-08-10 12:30:21 -0700214 alarm->state |= ALARMTIMER_STATE_CALLBACK;
John Stultz54da23b2011-08-10 11:08:07 -0700215 spin_unlock_irqrestore(&base->lock, flags);
216 if (alarm->function)
217 restart = alarm->function(alarm, now);
218 spin_lock_irqsave(&base->lock, flags);
John Stultza28cde82011-08-10 12:30:21 -0700219 alarm->state &= ~ALARMTIMER_STATE_CALLBACK;
John Stultz54da23b2011-08-10 11:08:07 -0700220
221 if (restart != ALARMTIMER_NORESTART) {
John Stultzff3ead92011-01-11 09:42:13 -0800222 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700223 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800224 }
John Stultzff3ead92011-01-11 09:42:13 -0800225 }
226
227 if (next) {
John Stultz7068b7a2011-04-28 13:29:18 -0700228 hrtimer_set_expires(&base->timer, next->expires);
229 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800230 }
231 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800232
John Stultz7068b7a2011-04-28 13:29:18 -0700233 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800234
John Stultzff3ead92011-01-11 09:42:13 -0800235}
236
John Stultz472647d2011-04-29 15:03:10 -0700237#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700238/**
John Stultzff3ead92011-01-11 09:42:13 -0800239 * alarmtimer_suspend - Suspend time callback
240 * @dev: unused
241 * @state: unused
242 *
243 * When we are going into suspend, we look through the bases
244 * to see which is the soonest timer to expire. We then
245 * set an rtc timer to fire that far into the future, which
246 * will wake us from suspend.
247 */
248static int alarmtimer_suspend(struct device *dev)
249{
250 struct rtc_time tm;
251 ktime_t min, now;
252 unsigned long flags;
John Stultzc008ba582011-06-16 18:27:09 -0700253 struct rtc_device *rtc;
John Stultzff3ead92011-01-11 09:42:13 -0800254 int i;
Todd Poynor59a93c22012-08-09 00:37:27 -0700255 int ret;
John Stultzff3ead92011-01-11 09:42:13 -0800256
257 spin_lock_irqsave(&freezer_delta_lock, flags);
258 min = freezer_delta;
259 freezer_delta = ktime_set(0, 0);
260 spin_unlock_irqrestore(&freezer_delta_lock, flags);
261
John Stultz8bc0daf2011-07-14 18:35:13 -0700262 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800263 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700264 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800265 return 0;
266
267 /* Find the soonest timer to expire*/
268 for (i = 0; i < ALARM_NUMTYPE; i++) {
269 struct alarm_base *base = &alarm_bases[i];
270 struct timerqueue_node *next;
271 ktime_t delta;
272
273 spin_lock_irqsave(&base->lock, flags);
274 next = timerqueue_getnext(&base->timerqueue);
275 spin_unlock_irqrestore(&base->lock, flags);
276 if (!next)
277 continue;
278 delta = ktime_sub(next->expires, base->gettime());
279 if (!min.tv64 || (delta.tv64 < min.tv64))
280 min = delta;
281 }
282 if (min.tv64 == 0)
283 return 0;
284
Todd Poynor59a93c22012-08-09 00:37:27 -0700285 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
286 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
287 return -EBUSY;
288 }
John Stultzff3ead92011-01-11 09:42:13 -0800289
290 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700291 rtc_timer_cancel(rtc, &rtctimer);
292 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800293 now = rtc_tm_to_ktime(tm);
294 now = ktime_add(now, min);
295
Todd Poynor59a93c22012-08-09 00:37:27 -0700296 /* Set alarm, if in the past reject suspend briefly to handle */
297 ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
298 if (ret < 0)
299 __pm_wakeup_event(ws, MSEC_PER_SEC);
300 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800301}
John Stultz472647d2011-04-29 15:03:10 -0700302#else
303static int alarmtimer_suspend(struct device *dev)
304{
305 return 0;
306}
307#endif
John Stultzff3ead92011-01-11 09:42:13 -0800308
John Stultz9a7adcf52011-01-11 09:54:33 -0800309static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
310{
311 ktime_t delta;
312 unsigned long flags;
313 struct alarm_base *base = &alarm_bases[type];
314
315 delta = ktime_sub(absexp, base->gettime());
316
317 spin_lock_irqsave(&freezer_delta_lock, flags);
318 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
319 freezer_delta = delta;
320 spin_unlock_irqrestore(&freezer_delta_lock, flags);
321}
322
323
John Stultz180bf812011-04-28 12:58:11 -0700324/**
John Stultzff3ead92011-01-11 09:42:13 -0800325 * alarm_init - Initialize an alarm structure
326 * @alarm: ptr to alarm to be initialized
327 * @type: the type of the alarm
328 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800329 */
330void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700331 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800332{
333 timerqueue_init(&alarm->node);
John Stultzff3ead92011-01-11 09:42:13 -0800334 alarm->function = function;
335 alarm->type = type;
John Stultza28cde82011-08-10 12:30:21 -0700336 alarm->state = ALARMTIMER_STATE_INACTIVE;
John Stultzff3ead92011-01-11 09:42:13 -0800337}
338
John Stultz180bf812011-04-28 12:58:11 -0700339/**
John Stultzff3ead92011-01-11 09:42:13 -0800340 * alarm_start - Sets an alarm to fire
341 * @alarm: ptr to alarm to set
342 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800343 */
John Stultz9e264762011-08-10 12:09:24 -0700344void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800345{
346 struct alarm_base *base = &alarm_bases[alarm->type];
347 unsigned long flags;
348
349 spin_lock_irqsave(&base->lock, flags);
John Stultza28cde82011-08-10 12:30:21 -0700350 if (alarmtimer_active(alarm))
John Stultzff3ead92011-01-11 09:42:13 -0800351 alarmtimer_remove(base, alarm);
352 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800353 alarmtimer_enqueue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800354 spin_unlock_irqrestore(&base->lock, flags);
355}
356
John Stultz180bf812011-04-28 12:58:11 -0700357/**
John Stultz9082c462011-08-10 12:41:36 -0700358 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800359 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700360 *
361 * Returns 1 if the timer was canceled, 0 if it was not running,
362 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800363 */
John Stultz9082c462011-08-10 12:41:36 -0700364int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800365{
366 struct alarm_base *base = &alarm_bases[alarm->type];
367 unsigned long flags;
John Stultz9082c462011-08-10 12:41:36 -0700368 int ret = -1;
John Stultzff3ead92011-01-11 09:42:13 -0800369 spin_lock_irqsave(&base->lock, flags);
John Stultz9082c462011-08-10 12:41:36 -0700370
371 if (alarmtimer_callback_running(alarm))
372 goto out;
373
374 if (alarmtimer_is_queued(alarm)) {
John Stultzff3ead92011-01-11 09:42:13 -0800375 alarmtimer_remove(base, alarm);
John Stultz9082c462011-08-10 12:41:36 -0700376 ret = 1;
377 } else
378 ret = 0;
379out:
John Stultzff3ead92011-01-11 09:42:13 -0800380 spin_unlock_irqrestore(&base->lock, flags);
John Stultz9082c462011-08-10 12:41:36 -0700381 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800382}
383
384
John Stultz9082c462011-08-10 12:41:36 -0700385/**
386 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
387 * @alarm: ptr to alarm to be canceled
388 *
389 * Returns 1 if the timer was canceled, 0 if it was not active.
390 */
391int alarm_cancel(struct alarm *alarm)
392{
393 for (;;) {
394 int ret = alarm_try_to_cancel(alarm);
395 if (ret >= 0)
396 return ret;
397 cpu_relax();
398 }
399}
400
John Stultzdce75a82011-08-10 11:31:03 -0700401
402u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
403{
404 u64 overrun = 1;
405 ktime_t delta;
406
407 delta = ktime_sub(now, alarm->node.expires);
408
409 if (delta.tv64 < 0)
410 return 0;
411
412 if (unlikely(delta.tv64 >= interval.tv64)) {
413 s64 incr = ktime_to_ns(interval);
414
415 overrun = ktime_divns(delta, incr);
416
417 alarm->node.expires = ktime_add_ns(alarm->node.expires,
418 incr*overrun);
419
420 if (alarm->node.expires.tv64 > now.tv64)
421 return overrun;
422 /*
423 * This (and the ktime_add() below) is the
424 * correction for exact:
425 */
426 overrun++;
427 }
428
429 alarm->node.expires = ktime_add(alarm->node.expires, interval);
430 return overrun;
431}
432
433
434
435
John Stultz180bf812011-04-28 12:58:11 -0700436/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800437 * clock2alarm - helper that converts from clockid to alarmtypes
438 * @clockid: clockid.
John Stultz9a7adcf52011-01-11 09:54:33 -0800439 */
440static enum alarmtimer_type clock2alarm(clockid_t clockid)
441{
442 if (clockid == CLOCK_REALTIME_ALARM)
443 return ALARM_REALTIME;
444 if (clockid == CLOCK_BOOTTIME_ALARM)
445 return ALARM_BOOTTIME;
446 return -1;
447}
448
John Stultz180bf812011-04-28 12:58:11 -0700449/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800450 * alarm_handle_timer - Callback for posix timers
451 * @alarm: alarm that fired
452 *
453 * Posix timer callback for expired alarm timers.
454 */
John Stultz4b413082011-08-10 10:37:59 -0700455static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
456 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800457{
458 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
John Stultz9e264762011-08-10 12:09:24 -0700459 it.alarm.alarmtimer);
John Stultz9a7adcf52011-01-11 09:54:33 -0800460 if (posix_timer_event(ptr, 0) != 0)
461 ptr->it_overrun++;
John Stultz4b413082011-08-10 10:37:59 -0700462
John Stultz54da23b2011-08-10 11:08:07 -0700463 /* Re-add periodic timers */
John Stultz9e264762011-08-10 12:09:24 -0700464 if (ptr->it.alarm.interval.tv64) {
465 ptr->it_overrun += alarm_forward(alarm, now,
466 ptr->it.alarm.interval);
John Stultz54da23b2011-08-10 11:08:07 -0700467 return ALARMTIMER_RESTART;
468 }
John Stultz4b413082011-08-10 10:37:59 -0700469 return ALARMTIMER_NORESTART;
John Stultz9a7adcf52011-01-11 09:54:33 -0800470}
471
John Stultz180bf812011-04-28 12:58:11 -0700472/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800473 * alarm_clock_getres - posix getres interface
474 * @which_clock: clockid
475 * @tp: timespec to fill
476 *
477 * Returns the granularity of underlying alarm base clock
478 */
479static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
480{
481 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
482
John Stultz1c6b39a2011-06-16 18:47:37 -0700483 if (!alarmtimer_get_rtcdev())
484 return -ENOTSUPP;
485
John Stultz9a7adcf52011-01-11 09:54:33 -0800486 return hrtimer_get_res(baseid, tp);
487}
488
489/**
490 * alarm_clock_get - posix clock_get interface
491 * @which_clock: clockid
492 * @tp: timespec to fill.
493 *
494 * Provides the underlying alarm base time.
495 */
496static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
497{
498 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
499
John Stultz1c6b39a2011-06-16 18:47:37 -0700500 if (!alarmtimer_get_rtcdev())
501 return -ENOTSUPP;
502
John Stultz9a7adcf52011-01-11 09:54:33 -0800503 *tp = ktime_to_timespec(base->gettime());
504 return 0;
505}
506
507/**
508 * alarm_timer_create - posix timer_create interface
509 * @new_timer: k_itimer pointer to manage
510 *
511 * Initializes the k_itimer structure.
512 */
513static int alarm_timer_create(struct k_itimer *new_timer)
514{
515 enum alarmtimer_type type;
516 struct alarm_base *base;
517
John Stultz1c6b39a2011-06-16 18:47:37 -0700518 if (!alarmtimer_get_rtcdev())
519 return -ENOTSUPP;
520
John Stultz9a7adcf52011-01-11 09:54:33 -0800521 if (!capable(CAP_WAKE_ALARM))
522 return -EPERM;
523
524 type = clock2alarm(new_timer->it_clock);
525 base = &alarm_bases[type];
John Stultz9e264762011-08-10 12:09:24 -0700526 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf52011-01-11 09:54:33 -0800527 return 0;
528}
529
530/**
531 * alarm_timer_get - posix timer_get interface
532 * @new_timer: k_itimer pointer
533 * @cur_setting: itimerspec data to fill
534 *
535 * Copies the itimerspec data out from the k_itimer
536 */
537static void alarm_timer_get(struct k_itimer *timr,
538 struct itimerspec *cur_setting)
539{
John Stultzea7802f2011-08-04 07:51:56 -0700540 memset(cur_setting, 0, sizeof(struct itimerspec));
541
John Stultz9a7adcf52011-01-11 09:54:33 -0800542 cur_setting->it_interval =
John Stultz9e264762011-08-10 12:09:24 -0700543 ktime_to_timespec(timr->it.alarm.interval);
John Stultz9a7adcf52011-01-11 09:54:33 -0800544 cur_setting->it_value =
John Stultz9e264762011-08-10 12:09:24 -0700545 ktime_to_timespec(timr->it.alarm.alarmtimer.node.expires);
John Stultz9a7adcf52011-01-11 09:54:33 -0800546 return;
547}
548
549/**
550 * alarm_timer_del - posix timer_del interface
551 * @timr: k_itimer pointer to be deleted
552 *
553 * Cancels any programmed alarms for the given timer.
554 */
555static int alarm_timer_del(struct k_itimer *timr)
556{
John Stultz1c6b39a2011-06-16 18:47:37 -0700557 if (!rtcdev)
558 return -ENOTSUPP;
559
John Stultz9082c462011-08-10 12:41:36 -0700560 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
561 return TIMER_RETRY;
562
John Stultz9a7adcf52011-01-11 09:54:33 -0800563 return 0;
564}
565
566/**
567 * alarm_timer_set - posix timer_set interface
568 * @timr: k_itimer pointer to be deleted
569 * @flags: timer flags
570 * @new_setting: itimerspec to be used
571 * @old_setting: itimerspec being replaced
572 *
573 * Sets the timer to new_setting, and starts the timer.
574 */
575static int alarm_timer_set(struct k_itimer *timr, int flags,
576 struct itimerspec *new_setting,
577 struct itimerspec *old_setting)
578{
John Stultz1c6b39a2011-06-16 18:47:37 -0700579 if (!rtcdev)
580 return -ENOTSUPP;
581
John Stultz971c90b2011-08-04 07:25:35 -0700582 if (old_setting)
583 alarm_timer_get(timr, old_setting);
John Stultz9a7adcf52011-01-11 09:54:33 -0800584
585 /* If the timer was already set, cancel it */
John Stultz9082c462011-08-10 12:41:36 -0700586 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
587 return TIMER_RETRY;
John Stultz9a7adcf52011-01-11 09:54:33 -0800588
589 /* start the timer */
John Stultz9e264762011-08-10 12:09:24 -0700590 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
591 alarm_start(&timr->it.alarm.alarmtimer,
592 timespec_to_ktime(new_setting->it_value));
John Stultz9a7adcf52011-01-11 09:54:33 -0800593 return 0;
594}
595
596/**
597 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
598 * @alarm: ptr to alarm that fired
599 *
600 * Wakes up the task that set the alarmtimer
601 */
John Stultz4b413082011-08-10 10:37:59 -0700602static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
603 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800604{
605 struct task_struct *task = (struct task_struct *)alarm->data;
606
607 alarm->data = NULL;
608 if (task)
609 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700610 return ALARMTIMER_NORESTART;
John Stultz9a7adcf52011-01-11 09:54:33 -0800611}
612
613/**
614 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
615 * @alarm: ptr to alarmtimer
616 * @absexp: absolute expiration time
617 *
618 * Sets the alarm timer and sleeps until it is fired or interrupted.
619 */
620static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
621{
622 alarm->data = (void *)current;
623 do {
624 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700625 alarm_start(alarm, absexp);
John Stultz9a7adcf52011-01-11 09:54:33 -0800626 if (likely(alarm->data))
627 schedule();
628
629 alarm_cancel(alarm);
630 } while (alarm->data && !signal_pending(current));
631
632 __set_current_state(TASK_RUNNING);
633
634 return (alarm->data == NULL);
635}
636
637
638/**
639 * update_rmtp - Update remaining timespec value
640 * @exp: expiration time
641 * @type: timer type
642 * @rmtp: user pointer to remaining timepsec value
643 *
644 * Helper function that fills in rmtp value with time between
645 * now and the exp value
646 */
647static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
648 struct timespec __user *rmtp)
649{
650 struct timespec rmt;
651 ktime_t rem;
652
653 rem = ktime_sub(exp, alarm_bases[type].gettime());
654
655 if (rem.tv64 <= 0)
656 return 0;
657 rmt = ktime_to_timespec(rem);
658
659 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
660 return -EFAULT;
661
662 return 1;
663
664}
665
666/**
667 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
668 * @restart: ptr to restart block
669 *
670 * Handles restarted clock_nanosleep calls
671 */
672static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
673{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200674 enum alarmtimer_type type = restart->nanosleep.clockid;
John Stultz9a7adcf52011-01-11 09:54:33 -0800675 ktime_t exp;
676 struct timespec __user *rmtp;
677 struct alarm alarm;
678 int ret = 0;
679
680 exp.tv64 = restart->nanosleep.expires;
681 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
682
683 if (alarmtimer_do_nsleep(&alarm, exp))
684 goto out;
685
686 if (freezing(current))
687 alarmtimer_freezerset(exp, type);
688
689 rmtp = restart->nanosleep.rmtp;
690 if (rmtp) {
691 ret = update_rmtp(exp, type, rmtp);
692 if (ret <= 0)
693 goto out;
694 }
695
696
697 /* The other values in restart are already filled in */
698 ret = -ERESTART_RESTARTBLOCK;
699out:
700 return ret;
701}
702
703/**
704 * alarm_timer_nsleep - alarmtimer nanosleep
705 * @which_clock: clockid
706 * @flags: determins abstime or relative
707 * @tsreq: requested sleep time (abs or rel)
708 * @rmtp: remaining sleep time saved
709 *
710 * Handles clock_nanosleep calls against _ALARM clockids
711 */
712static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
713 struct timespec *tsreq, struct timespec __user *rmtp)
714{
715 enum alarmtimer_type type = clock2alarm(which_clock);
716 struct alarm alarm;
717 ktime_t exp;
718 int ret = 0;
719 struct restart_block *restart;
720
John Stultz1c6b39a2011-06-16 18:47:37 -0700721 if (!alarmtimer_get_rtcdev())
722 return -ENOTSUPP;
723
John Stultz9a7adcf52011-01-11 09:54:33 -0800724 if (!capable(CAP_WAKE_ALARM))
725 return -EPERM;
726
727 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
728
729 exp = timespec_to_ktime(*tsreq);
730 /* Convert (if necessary) to absolute time */
731 if (flags != TIMER_ABSTIME) {
732 ktime_t now = alarm_bases[type].gettime();
733 exp = ktime_add(now, exp);
734 }
735
736 if (alarmtimer_do_nsleep(&alarm, exp))
737 goto out;
738
739 if (freezing(current))
740 alarmtimer_freezerset(exp, type);
741
742 /* abs timers don't set remaining time or restart */
743 if (flags == TIMER_ABSTIME) {
744 ret = -ERESTARTNOHAND;
745 goto out;
746 }
747
748 if (rmtp) {
749 ret = update_rmtp(exp, type, rmtp);
750 if (ret <= 0)
751 goto out;
752 }
753
754 restart = &current_thread_info()->restart_block;
755 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200756 restart->nanosleep.clockid = type;
John Stultz9a7adcf52011-01-11 09:54:33 -0800757 restart->nanosleep.expires = exp.tv64;
758 restart->nanosleep.rmtp = rmtp;
759 ret = -ERESTART_RESTARTBLOCK;
760
761out:
762 return ret;
763}
John Stultzff3ead92011-01-11 09:42:13 -0800764
John Stultzff3ead92011-01-11 09:42:13 -0800765
766/* Suspend hook structures */
767static const struct dev_pm_ops alarmtimer_pm_ops = {
768 .suspend = alarmtimer_suspend,
769};
770
771static struct platform_driver alarmtimer_driver = {
772 .driver = {
773 .name = "alarmtimer",
774 .pm = &alarmtimer_pm_ops,
775 }
776};
777
778/**
779 * alarmtimer_init - Initialize alarm timer code
780 *
781 * This function initializes the alarm bases and registers
782 * the posix clock ids.
783 */
784static int __init alarmtimer_init(void)
785{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200786 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800787 int error = 0;
788 int i;
John Stultz9a7adcf52011-01-11 09:54:33 -0800789 struct k_clock alarm_clock = {
790 .clock_getres = alarm_clock_getres,
791 .clock_get = alarm_clock_get,
792 .timer_create = alarm_timer_create,
793 .timer_set = alarm_timer_set,
794 .timer_del = alarm_timer_del,
795 .timer_get = alarm_timer_get,
796 .nsleep = alarm_timer_nsleep,
797 };
798
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100799 alarmtimer_rtc_timer_init();
John Stultzad30dfa2012-03-23 15:52:25 -0700800
John Stultz9a7adcf52011-01-11 09:54:33 -0800801 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
802 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
John Stultzff3ead92011-01-11 09:42:13 -0800803
804 /* Initialize alarm bases */
805 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
806 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
807 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
808 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
809 for (i = 0; i < ALARM_NUMTYPE; i++) {
810 timerqueue_init_head(&alarm_bases[i].timerqueue);
811 spin_lock_init(&alarm_bases[i].lock);
812 hrtimer_init(&alarm_bases[i].timer,
813 alarm_bases[i].base_clockid,
814 HRTIMER_MODE_ABS);
815 alarm_bases[i].timer.function = alarmtimer_fired;
John Stultzff3ead92011-01-11 09:42:13 -0800816 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700817
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200818 error = alarmtimer_rtc_interface_setup();
819 if (error)
820 return error;
John Stultzff3ead92011-01-11 09:42:13 -0800821
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200822 error = platform_driver_register(&alarmtimer_driver);
823 if (error)
824 goto out_if;
825
826 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
827 if (IS_ERR(pdev)) {
828 error = PTR_ERR(pdev);
829 goto out_drv;
830 }
Todd Poynor59a93c22012-08-09 00:37:27 -0700831 ws = wakeup_source_register("alarmtimer");
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200832 return 0;
833
834out_drv:
835 platform_driver_unregister(&alarmtimer_driver);
836out_if:
837 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -0800838 return error;
839}
840device_initcall(alarmtimer_init);