blob: c436e790b21bf7cd89878eb9d92146e1b1c511d6 [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 Stultzc008ba52011-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 Stultz472647d2011-04-29 15:03:10 -070049#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -070050/* rtc timer and device for setting alarm wakeups at suspend */
John Stultzff3ead92011-01-11 09:42:13 -080051static struct rtc_timer rtctimer;
52static struct rtc_device *rtcdev;
John Stultzc008ba52011-06-16 18:27:09 -070053static DEFINE_SPINLOCK(rtcdev_lock);
John Stultzff3ead92011-01-11 09:42:13 -080054
John Stultzc008ba52011-06-16 18:27:09 -070055/**
John Stultzc008ba52011-06-16 18:27:09 -070056 * alarmtimer_get_rtcdev - Return selected rtcdevice
57 *
58 * This function returns the rtc device to use for wakealarms.
59 * If one has not already been chosen, it checks to see if a
60 * functional rtc device is available.
61 */
62static struct rtc_device *alarmtimer_get_rtcdev(void)
63{
John Stultzc008ba52011-06-16 18:27:09 -070064 unsigned long flags;
65 struct rtc_device *ret;
66
67 spin_lock_irqsave(&rtcdev_lock, flags);
John Stultzc008ba52011-06-16 18:27:09 -070068 ret = rtcdev;
69 spin_unlock_irqrestore(&rtcdev_lock, flags);
70
71 return ret;
72}
John Stultz8bc0daf2011-07-14 18:35:13 -070073
74
75static int alarmtimer_rtc_add_device(struct device *dev,
76 struct class_interface *class_intf)
77{
78 unsigned long flags;
79 struct rtc_device *rtc = to_rtc_device(dev);
80
81 if (rtcdev)
82 return -EBUSY;
83
84 if (!rtc->ops->set_alarm)
85 return -1;
86 if (!device_may_wakeup(rtc->dev.parent))
87 return -1;
88
89 spin_lock_irqsave(&rtcdev_lock, flags);
90 if (!rtcdev) {
91 rtcdev = rtc;
92 /* hold a reference so it doesn't go away */
93 get_device(dev);
94 }
95 spin_unlock_irqrestore(&rtcdev_lock, flags);
96 return 0;
97}
98
99static struct class_interface alarmtimer_rtc_interface = {
100 .add_dev = &alarmtimer_rtc_add_device,
101};
102
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200103static int alarmtimer_rtc_interface_setup(void)
John Stultz8bc0daf2011-07-14 18:35:13 -0700104{
105 alarmtimer_rtc_interface.class = rtc_class;
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200106 return class_interface_register(&alarmtimer_rtc_interface);
107}
108static void alarmtimer_rtc_interface_remove(void)
109{
110 class_interface_unregister(&alarmtimer_rtc_interface);
John Stultz8bc0daf2011-07-14 18:35:13 -0700111}
John Stultz1c6b39a2011-06-16 18:47:37 -0700112#else
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200113static inline struct rtc_device *alarmtimer_get_rtcdev(void)
114{
115 return NULL;
116}
117#define rtcdev (NULL)
118static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
119static inline void alarmtimer_rtc_interface_remove(void) { }
John Stultzc008ba52011-06-16 18:27:09 -0700120#endif
John Stultzff3ead92011-01-11 09:42:13 -0800121
John Stultz180bf812011-04-28 12:58:11 -0700122/**
John Stultzff3ead92011-01-11 09:42:13 -0800123 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
124 * @base: pointer to the base where the timer is being run
125 * @alarm: pointer to alarm being enqueued.
126 *
127 * Adds alarm to a alarm_base timerqueue and if necessary sets
128 * an hrtimer to run.
129 *
130 * Must hold base->lock when calling.
131 */
132static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
133{
134 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700135 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
136
John Stultzff3ead92011-01-11 09:42:13 -0800137 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
138 hrtimer_try_to_cancel(&base->timer);
139 hrtimer_start(&base->timer, alarm->node.expires,
140 HRTIMER_MODE_ABS);
141 }
142}
143
John Stultz180bf812011-04-28 12:58:11 -0700144/**
John Stultzff3ead92011-01-11 09:42:13 -0800145 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
146 * @base: pointer to the base where the timer is running
147 * @alarm: pointer to alarm being removed
148 *
149 * Removes alarm to a alarm_base timerqueue and if necessary sets
150 * a new timer to run.
151 *
152 * Must hold base->lock when calling.
153 */
154static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
155{
156 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
157
John Stultza28cde82011-08-10 12:30:21 -0700158 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
159 return;
160
John Stultzff3ead92011-01-11 09:42:13 -0800161 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700162 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
163
John Stultzff3ead92011-01-11 09:42:13 -0800164 if (next == &alarm->node) {
165 hrtimer_try_to_cancel(&base->timer);
166 next = timerqueue_getnext(&base->timerqueue);
167 if (!next)
168 return;
169 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
170 }
171}
172
John Stultz7068b7a2011-04-28 13:29:18 -0700173
John Stultz180bf812011-04-28 12:58:11 -0700174/**
John Stultz7068b7a2011-04-28 13:29:18 -0700175 * alarmtimer_fired - Handles alarm hrtimer being fired.
176 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800177 *
John Stultz180bf812011-04-28 12:58:11 -0700178 * When a alarm timer fires, this runs through the timerqueue to
179 * see which alarms expired, and runs those. If there are more alarm
180 * timers queued for the future, we set the hrtimer to fire when
181 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800182 */
John Stultz7068b7a2011-04-28 13:29:18 -0700183static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800184{
John Stultz7068b7a2011-04-28 13:29:18 -0700185 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
John Stultzff3ead92011-01-11 09:42:13 -0800186 struct timerqueue_node *next;
187 unsigned long flags;
188 ktime_t now;
John Stultz7068b7a2011-04-28 13:29:18 -0700189 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700190 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800191
192 spin_lock_irqsave(&base->lock, flags);
193 now = base->gettime();
194 while ((next = timerqueue_getnext(&base->timerqueue))) {
195 struct alarm *alarm;
196 ktime_t expired = next->expires;
197
198 if (expired.tv64 >= now.tv64)
199 break;
200
201 alarm = container_of(next, struct alarm, node);
202
203 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700204 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultz54da23b2011-08-10 11:08:07 -0700205
John Stultza28cde82011-08-10 12:30:21 -0700206 alarm->state |= ALARMTIMER_STATE_CALLBACK;
John Stultz54da23b2011-08-10 11:08:07 -0700207 spin_unlock_irqrestore(&base->lock, flags);
208 if (alarm->function)
209 restart = alarm->function(alarm, now);
210 spin_lock_irqsave(&base->lock, flags);
John Stultza28cde82011-08-10 12:30:21 -0700211 alarm->state &= ~ALARMTIMER_STATE_CALLBACK;
John Stultz54da23b2011-08-10 11:08:07 -0700212
213 if (restart != ALARMTIMER_NORESTART) {
John Stultzff3ead92011-01-11 09:42:13 -0800214 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700215 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800216 }
John Stultzff3ead92011-01-11 09:42:13 -0800217 }
218
219 if (next) {
John Stultz7068b7a2011-04-28 13:29:18 -0700220 hrtimer_set_expires(&base->timer, next->expires);
221 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800222 }
223 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800224
John Stultz7068b7a2011-04-28 13:29:18 -0700225 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800226
John Stultzff3ead92011-01-11 09:42:13 -0800227}
228
John Stultz472647d2011-04-29 15:03:10 -0700229#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700230/**
John Stultzff3ead92011-01-11 09:42:13 -0800231 * alarmtimer_suspend - Suspend time callback
232 * @dev: unused
233 * @state: unused
234 *
235 * When we are going into suspend, we look through the bases
236 * to see which is the soonest timer to expire. We then
237 * set an rtc timer to fire that far into the future, which
238 * will wake us from suspend.
239 */
240static int alarmtimer_suspend(struct device *dev)
241{
242 struct rtc_time tm;
243 ktime_t min, now;
244 unsigned long flags;
John Stultzc008ba52011-06-16 18:27:09 -0700245 struct rtc_device *rtc;
John Stultzff3ead92011-01-11 09:42:13 -0800246 int i;
247
248 spin_lock_irqsave(&freezer_delta_lock, flags);
249 min = freezer_delta;
250 freezer_delta = ktime_set(0, 0);
251 spin_unlock_irqrestore(&freezer_delta_lock, flags);
252
John Stultz8bc0daf2011-07-14 18:35:13 -0700253 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800254 /* If we have no rtcdev, just return */
John Stultzc008ba52011-06-16 18:27:09 -0700255 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800256 return 0;
257
258 /* Find the soonest timer to expire*/
259 for (i = 0; i < ALARM_NUMTYPE; i++) {
260 struct alarm_base *base = &alarm_bases[i];
261 struct timerqueue_node *next;
262 ktime_t delta;
263
264 spin_lock_irqsave(&base->lock, flags);
265 next = timerqueue_getnext(&base->timerqueue);
266 spin_unlock_irqrestore(&base->lock, flags);
267 if (!next)
268 continue;
269 delta = ktime_sub(next->expires, base->gettime());
270 if (!min.tv64 || (delta.tv64 < min.tv64))
271 min = delta;
272 }
273 if (min.tv64 == 0)
274 return 0;
275
276 /* XXX - Should we enforce a minimum sleep time? */
277 WARN_ON(min.tv64 < NSEC_PER_SEC);
278
279 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba52011-06-16 18:27:09 -0700280 rtc_timer_cancel(rtc, &rtctimer);
281 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800282 now = rtc_tm_to_ktime(tm);
283 now = ktime_add(now, min);
284
John Stultzc008ba52011-06-16 18:27:09 -0700285 rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
John Stultzff3ead92011-01-11 09:42:13 -0800286
287 return 0;
288}
John Stultz472647d2011-04-29 15:03:10 -0700289#else
290static int alarmtimer_suspend(struct device *dev)
291{
292 return 0;
293}
294#endif
John Stultzff3ead92011-01-11 09:42:13 -0800295
John Stultz9a7adcf2011-01-11 09:54:33 -0800296static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
297{
298 ktime_t delta;
299 unsigned long flags;
300 struct alarm_base *base = &alarm_bases[type];
301
302 delta = ktime_sub(absexp, base->gettime());
303
304 spin_lock_irqsave(&freezer_delta_lock, flags);
305 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
306 freezer_delta = delta;
307 spin_unlock_irqrestore(&freezer_delta_lock, flags);
308}
309
310
John Stultz180bf812011-04-28 12:58:11 -0700311/**
John Stultzff3ead92011-01-11 09:42:13 -0800312 * alarm_init - Initialize an alarm structure
313 * @alarm: ptr to alarm to be initialized
314 * @type: the type of the alarm
315 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800316 */
317void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700318 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800319{
320 timerqueue_init(&alarm->node);
John Stultzff3ead92011-01-11 09:42:13 -0800321 alarm->function = function;
322 alarm->type = type;
John Stultza28cde82011-08-10 12:30:21 -0700323 alarm->state = ALARMTIMER_STATE_INACTIVE;
John Stultzff3ead92011-01-11 09:42:13 -0800324}
325
John Stultz180bf812011-04-28 12:58:11 -0700326/**
John Stultzff3ead92011-01-11 09:42:13 -0800327 * alarm_start - Sets an alarm to fire
328 * @alarm: ptr to alarm to set
329 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800330 */
John Stultz9e264762011-08-10 12:09:24 -0700331void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800332{
333 struct alarm_base *base = &alarm_bases[alarm->type];
334 unsigned long flags;
335
336 spin_lock_irqsave(&base->lock, flags);
John Stultza28cde82011-08-10 12:30:21 -0700337 if (alarmtimer_active(alarm))
John Stultzff3ead92011-01-11 09:42:13 -0800338 alarmtimer_remove(base, alarm);
339 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800340 alarmtimer_enqueue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800341 spin_unlock_irqrestore(&base->lock, flags);
342}
343
John Stultz180bf812011-04-28 12:58:11 -0700344/**
John Stultz9082c462011-08-10 12:41:36 -0700345 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800346 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700347 *
348 * Returns 1 if the timer was canceled, 0 if it was not running,
349 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800350 */
John Stultz9082c462011-08-10 12:41:36 -0700351int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800352{
353 struct alarm_base *base = &alarm_bases[alarm->type];
354 unsigned long flags;
John Stultz9082c462011-08-10 12:41:36 -0700355 int ret = -1;
John Stultzff3ead92011-01-11 09:42:13 -0800356 spin_lock_irqsave(&base->lock, flags);
John Stultz9082c462011-08-10 12:41:36 -0700357
358 if (alarmtimer_callback_running(alarm))
359 goto out;
360
361 if (alarmtimer_is_queued(alarm)) {
John Stultzff3ead92011-01-11 09:42:13 -0800362 alarmtimer_remove(base, alarm);
John Stultz9082c462011-08-10 12:41:36 -0700363 ret = 1;
364 } else
365 ret = 0;
366out:
John Stultzff3ead92011-01-11 09:42:13 -0800367 spin_unlock_irqrestore(&base->lock, flags);
John Stultz9082c462011-08-10 12:41:36 -0700368 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800369}
370
371
John Stultz9082c462011-08-10 12:41:36 -0700372/**
373 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
374 * @alarm: ptr to alarm to be canceled
375 *
376 * Returns 1 if the timer was canceled, 0 if it was not active.
377 */
378int alarm_cancel(struct alarm *alarm)
379{
380 for (;;) {
381 int ret = alarm_try_to_cancel(alarm);
382 if (ret >= 0)
383 return ret;
384 cpu_relax();
385 }
386}
387
John Stultzdce75a82011-08-10 11:31:03 -0700388
389u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
390{
391 u64 overrun = 1;
392 ktime_t delta;
393
394 delta = ktime_sub(now, alarm->node.expires);
395
396 if (delta.tv64 < 0)
397 return 0;
398
399 if (unlikely(delta.tv64 >= interval.tv64)) {
400 s64 incr = ktime_to_ns(interval);
401
402 overrun = ktime_divns(delta, incr);
403
404 alarm->node.expires = ktime_add_ns(alarm->node.expires,
405 incr*overrun);
406
407 if (alarm->node.expires.tv64 > now.tv64)
408 return overrun;
409 /*
410 * This (and the ktime_add() below) is the
411 * correction for exact:
412 */
413 overrun++;
414 }
415
416 alarm->node.expires = ktime_add(alarm->node.expires, interval);
417 return overrun;
418}
419
420
421
422
John Stultz180bf812011-04-28 12:58:11 -0700423/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800424 * clock2alarm - helper that converts from clockid to alarmtypes
425 * @clockid: clockid.
John Stultz9a7adcf2011-01-11 09:54:33 -0800426 */
427static enum alarmtimer_type clock2alarm(clockid_t clockid)
428{
429 if (clockid == CLOCK_REALTIME_ALARM)
430 return ALARM_REALTIME;
431 if (clockid == CLOCK_BOOTTIME_ALARM)
432 return ALARM_BOOTTIME;
433 return -1;
434}
435
John Stultz180bf812011-04-28 12:58:11 -0700436/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800437 * alarm_handle_timer - Callback for posix timers
438 * @alarm: alarm that fired
439 *
440 * Posix timer callback for expired alarm timers.
441 */
John Stultz4b413082011-08-10 10:37:59 -0700442static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
443 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800444{
445 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
John Stultz9e264762011-08-10 12:09:24 -0700446 it.alarm.alarmtimer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800447 if (posix_timer_event(ptr, 0) != 0)
448 ptr->it_overrun++;
John Stultz4b413082011-08-10 10:37:59 -0700449
John Stultz54da23b2011-08-10 11:08:07 -0700450 /* Re-add periodic timers */
John Stultz9e264762011-08-10 12:09:24 -0700451 if (ptr->it.alarm.interval.tv64) {
452 ptr->it_overrun += alarm_forward(alarm, now,
453 ptr->it.alarm.interval);
John Stultz54da23b2011-08-10 11:08:07 -0700454 return ALARMTIMER_RESTART;
455 }
John Stultz4b413082011-08-10 10:37:59 -0700456 return ALARMTIMER_NORESTART;
John Stultz9a7adcf2011-01-11 09:54:33 -0800457}
458
John Stultz180bf812011-04-28 12:58:11 -0700459/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800460 * alarm_clock_getres - posix getres interface
461 * @which_clock: clockid
462 * @tp: timespec to fill
463 *
464 * Returns the granularity of underlying alarm base clock
465 */
466static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
467{
468 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
469
John Stultz1c6b39a2011-06-16 18:47:37 -0700470 if (!alarmtimer_get_rtcdev())
471 return -ENOTSUPP;
472
John Stultz9a7adcf2011-01-11 09:54:33 -0800473 return hrtimer_get_res(baseid, tp);
474}
475
476/**
477 * alarm_clock_get - posix clock_get interface
478 * @which_clock: clockid
479 * @tp: timespec to fill.
480 *
481 * Provides the underlying alarm base time.
482 */
483static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
484{
485 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
486
John Stultz1c6b39a2011-06-16 18:47:37 -0700487 if (!alarmtimer_get_rtcdev())
488 return -ENOTSUPP;
489
John Stultz9a7adcf2011-01-11 09:54:33 -0800490 *tp = ktime_to_timespec(base->gettime());
491 return 0;
492}
493
494/**
495 * alarm_timer_create - posix timer_create interface
496 * @new_timer: k_itimer pointer to manage
497 *
498 * Initializes the k_itimer structure.
499 */
500static int alarm_timer_create(struct k_itimer *new_timer)
501{
502 enum alarmtimer_type type;
503 struct alarm_base *base;
504
John Stultz1c6b39a2011-06-16 18:47:37 -0700505 if (!alarmtimer_get_rtcdev())
506 return -ENOTSUPP;
507
John Stultz9a7adcf2011-01-11 09:54:33 -0800508 if (!capable(CAP_WAKE_ALARM))
509 return -EPERM;
510
511 type = clock2alarm(new_timer->it_clock);
512 base = &alarm_bases[type];
John Stultz9e264762011-08-10 12:09:24 -0700513 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800514 return 0;
515}
516
517/**
518 * alarm_timer_get - posix timer_get interface
519 * @new_timer: k_itimer pointer
520 * @cur_setting: itimerspec data to fill
521 *
522 * Copies the itimerspec data out from the k_itimer
523 */
524static void alarm_timer_get(struct k_itimer *timr,
525 struct itimerspec *cur_setting)
526{
John Stultzea7802f2011-08-04 07:51:56 -0700527 memset(cur_setting, 0, sizeof(struct itimerspec));
528
John Stultz9a7adcf2011-01-11 09:54:33 -0800529 cur_setting->it_interval =
John Stultz9e264762011-08-10 12:09:24 -0700530 ktime_to_timespec(timr->it.alarm.interval);
John Stultz9a7adcf2011-01-11 09:54:33 -0800531 cur_setting->it_value =
John Stultz9e264762011-08-10 12:09:24 -0700532 ktime_to_timespec(timr->it.alarm.alarmtimer.node.expires);
John Stultz9a7adcf2011-01-11 09:54:33 -0800533 return;
534}
535
536/**
537 * alarm_timer_del - posix timer_del interface
538 * @timr: k_itimer pointer to be deleted
539 *
540 * Cancels any programmed alarms for the given timer.
541 */
542static int alarm_timer_del(struct k_itimer *timr)
543{
John Stultz1c6b39a2011-06-16 18:47:37 -0700544 if (!rtcdev)
545 return -ENOTSUPP;
546
John Stultz9082c462011-08-10 12:41:36 -0700547 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
548 return TIMER_RETRY;
549
John Stultz9a7adcf2011-01-11 09:54:33 -0800550 return 0;
551}
552
553/**
554 * alarm_timer_set - posix timer_set interface
555 * @timr: k_itimer pointer to be deleted
556 * @flags: timer flags
557 * @new_setting: itimerspec to be used
558 * @old_setting: itimerspec being replaced
559 *
560 * Sets the timer to new_setting, and starts the timer.
561 */
562static int alarm_timer_set(struct k_itimer *timr, int flags,
563 struct itimerspec *new_setting,
564 struct itimerspec *old_setting)
565{
John Stultz1c6b39a2011-06-16 18:47:37 -0700566 if (!rtcdev)
567 return -ENOTSUPP;
568
John Stultz971c90b2011-08-04 07:25:35 -0700569 if (old_setting)
570 alarm_timer_get(timr, old_setting);
John Stultz9a7adcf2011-01-11 09:54:33 -0800571
572 /* If the timer was already set, cancel it */
John Stultz9082c462011-08-10 12:41:36 -0700573 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
574 return TIMER_RETRY;
John Stultz9a7adcf2011-01-11 09:54:33 -0800575
576 /* start the timer */
John Stultz9e264762011-08-10 12:09:24 -0700577 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
578 alarm_start(&timr->it.alarm.alarmtimer,
579 timespec_to_ktime(new_setting->it_value));
John Stultz9a7adcf2011-01-11 09:54:33 -0800580 return 0;
581}
582
583/**
584 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
585 * @alarm: ptr to alarm that fired
586 *
587 * Wakes up the task that set the alarmtimer
588 */
John Stultz4b413082011-08-10 10:37:59 -0700589static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
590 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800591{
592 struct task_struct *task = (struct task_struct *)alarm->data;
593
594 alarm->data = NULL;
595 if (task)
596 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700597 return ALARMTIMER_NORESTART;
John Stultz9a7adcf2011-01-11 09:54:33 -0800598}
599
600/**
601 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
602 * @alarm: ptr to alarmtimer
603 * @absexp: absolute expiration time
604 *
605 * Sets the alarm timer and sleeps until it is fired or interrupted.
606 */
607static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
608{
609 alarm->data = (void *)current;
610 do {
611 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700612 alarm_start(alarm, absexp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800613 if (likely(alarm->data))
614 schedule();
615
616 alarm_cancel(alarm);
617 } while (alarm->data && !signal_pending(current));
618
619 __set_current_state(TASK_RUNNING);
620
621 return (alarm->data == NULL);
622}
623
624
625/**
626 * update_rmtp - Update remaining timespec value
627 * @exp: expiration time
628 * @type: timer type
629 * @rmtp: user pointer to remaining timepsec value
630 *
631 * Helper function that fills in rmtp value with time between
632 * now and the exp value
633 */
634static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
635 struct timespec __user *rmtp)
636{
637 struct timespec rmt;
638 ktime_t rem;
639
640 rem = ktime_sub(exp, alarm_bases[type].gettime());
641
642 if (rem.tv64 <= 0)
643 return 0;
644 rmt = ktime_to_timespec(rem);
645
646 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
647 return -EFAULT;
648
649 return 1;
650
651}
652
653/**
654 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
655 * @restart: ptr to restart block
656 *
657 * Handles restarted clock_nanosleep calls
658 */
659static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
660{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200661 enum alarmtimer_type type = restart->nanosleep.clockid;
John Stultz9a7adcf2011-01-11 09:54:33 -0800662 ktime_t exp;
663 struct timespec __user *rmtp;
664 struct alarm alarm;
665 int ret = 0;
666
667 exp.tv64 = restart->nanosleep.expires;
668 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
669
670 if (alarmtimer_do_nsleep(&alarm, exp))
671 goto out;
672
673 if (freezing(current))
674 alarmtimer_freezerset(exp, type);
675
676 rmtp = restart->nanosleep.rmtp;
677 if (rmtp) {
678 ret = update_rmtp(exp, type, rmtp);
679 if (ret <= 0)
680 goto out;
681 }
682
683
684 /* The other values in restart are already filled in */
685 ret = -ERESTART_RESTARTBLOCK;
686out:
687 return ret;
688}
689
690/**
691 * alarm_timer_nsleep - alarmtimer nanosleep
692 * @which_clock: clockid
693 * @flags: determins abstime or relative
694 * @tsreq: requested sleep time (abs or rel)
695 * @rmtp: remaining sleep time saved
696 *
697 * Handles clock_nanosleep calls against _ALARM clockids
698 */
699static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
700 struct timespec *tsreq, struct timespec __user *rmtp)
701{
702 enum alarmtimer_type type = clock2alarm(which_clock);
703 struct alarm alarm;
704 ktime_t exp;
705 int ret = 0;
706 struct restart_block *restart;
707
John Stultz1c6b39a2011-06-16 18:47:37 -0700708 if (!alarmtimer_get_rtcdev())
709 return -ENOTSUPP;
710
John Stultz9a7adcf2011-01-11 09:54:33 -0800711 if (!capable(CAP_WAKE_ALARM))
712 return -EPERM;
713
714 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
715
716 exp = timespec_to_ktime(*tsreq);
717 /* Convert (if necessary) to absolute time */
718 if (flags != TIMER_ABSTIME) {
719 ktime_t now = alarm_bases[type].gettime();
720 exp = ktime_add(now, exp);
721 }
722
723 if (alarmtimer_do_nsleep(&alarm, exp))
724 goto out;
725
726 if (freezing(current))
727 alarmtimer_freezerset(exp, type);
728
729 /* abs timers don't set remaining time or restart */
730 if (flags == TIMER_ABSTIME) {
731 ret = -ERESTARTNOHAND;
732 goto out;
733 }
734
735 if (rmtp) {
736 ret = update_rmtp(exp, type, rmtp);
737 if (ret <= 0)
738 goto out;
739 }
740
741 restart = &current_thread_info()->restart_block;
742 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200743 restart->nanosleep.clockid = type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800744 restart->nanosleep.expires = exp.tv64;
745 restart->nanosleep.rmtp = rmtp;
746 ret = -ERESTART_RESTARTBLOCK;
747
748out:
749 return ret;
750}
John Stultzff3ead92011-01-11 09:42:13 -0800751
John Stultzff3ead92011-01-11 09:42:13 -0800752
753/* Suspend hook structures */
754static const struct dev_pm_ops alarmtimer_pm_ops = {
755 .suspend = alarmtimer_suspend,
756};
757
758static struct platform_driver alarmtimer_driver = {
759 .driver = {
760 .name = "alarmtimer",
761 .pm = &alarmtimer_pm_ops,
762 }
763};
764
765/**
766 * alarmtimer_init - Initialize alarm timer code
767 *
768 * This function initializes the alarm bases and registers
769 * the posix clock ids.
770 */
771static int __init alarmtimer_init(void)
772{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200773 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800774 int error = 0;
775 int i;
John Stultz9a7adcf2011-01-11 09:54:33 -0800776 struct k_clock alarm_clock = {
777 .clock_getres = alarm_clock_getres,
778 .clock_get = alarm_clock_get,
779 .timer_create = alarm_timer_create,
780 .timer_set = alarm_timer_set,
781 .timer_del = alarm_timer_del,
782 .timer_get = alarm_timer_get,
783 .nsleep = alarm_timer_nsleep,
784 };
785
786 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
787 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
John Stultzff3ead92011-01-11 09:42:13 -0800788
789 /* Initialize alarm bases */
790 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
791 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
792 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
793 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
794 for (i = 0; i < ALARM_NUMTYPE; i++) {
795 timerqueue_init_head(&alarm_bases[i].timerqueue);
796 spin_lock_init(&alarm_bases[i].lock);
797 hrtimer_init(&alarm_bases[i].timer,
798 alarm_bases[i].base_clockid,
799 HRTIMER_MODE_ABS);
800 alarm_bases[i].timer.function = alarmtimer_fired;
John Stultzff3ead92011-01-11 09:42:13 -0800801 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700802
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200803 error = alarmtimer_rtc_interface_setup();
804 if (error)
805 return error;
John Stultzff3ead92011-01-11 09:42:13 -0800806
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200807 error = platform_driver_register(&alarmtimer_driver);
808 if (error)
809 goto out_if;
810
811 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
812 if (IS_ERR(pdev)) {
813 error = PTR_ERR(pdev);
814 goto out_drv;
815 }
816 return 0;
817
818out_drv:
819 platform_driver_unregister(&alarmtimer_driver);
820out_if:
821 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -0800822 return error;
823}
824device_initcall(alarmtimer_init);