blob: 5b14cc29b6a685550830548f5976337c77caddae [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 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 Stultzc008ba582011-06-16 18:27:09 -070053static DEFINE_SPINLOCK(rtcdev_lock);
John Stultzff3ead92011-01-11 09:42:13 -080054
John Stultzc008ba582011-06-16 18:27:09 -070055/**
56 * has_wakealarm - check rtc device has wakealarm ability
57 * @dev: current device
58 * @name_ptr: name to be returned
59 *
60 * This helper function checks to see if the rtc device can wake
61 * from suspend.
62 */
63static int has_wakealarm(struct device *dev, void *name_ptr)
64{
65 struct rtc_device *candidate = to_rtc_device(dev);
66
67 if (!candidate->ops->set_alarm)
68 return 0;
69 if (!device_may_wakeup(candidate->dev.parent))
70 return 0;
71
72 *(const char **)name_ptr = dev_name(dev);
73 return 1;
74}
75
76/**
77 * alarmtimer_get_rtcdev - Return selected rtcdevice
78 *
79 * This function returns the rtc device to use for wakealarms.
80 * If one has not already been chosen, it checks to see if a
81 * functional rtc device is available.
82 */
83static struct rtc_device *alarmtimer_get_rtcdev(void)
84{
85 struct device *dev;
86 char *str;
87 unsigned long flags;
88 struct rtc_device *ret;
89
90 spin_lock_irqsave(&rtcdev_lock, flags);
91 if (!rtcdev) {
92 /* Find an rtc device and init the rtc_timer */
93 dev = class_find_device(rtc_class, NULL, &str, has_wakealarm);
94 /* If we have a device then str is valid. See has_wakealarm() */
95 if (dev) {
96 rtcdev = rtc_class_open(str);
97 /*
98 * Drop the reference we got in class_find_device,
99 * rtc_open takes its own.
100 */
101 put_device(dev);
102 rtc_timer_init(&rtctimer, NULL, NULL);
103 }
104 }
105 ret = rtcdev;
106 spin_unlock_irqrestore(&rtcdev_lock, flags);
107
108 return ret;
109}
John Stultz1c6b39a2011-06-16 18:47:37 -0700110#else
111#define alarmtimer_get_rtcdev() (0)
112#define rtcdev (0)
John Stultzc008ba582011-06-16 18:27:09 -0700113#endif
John Stultzff3ead92011-01-11 09:42:13 -0800114
115
John Stultz180bf812011-04-28 12:58:11 -0700116/**
John Stultzff3ead92011-01-11 09:42:13 -0800117 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
118 * @base: pointer to the base where the timer is being run
119 * @alarm: pointer to alarm being enqueued.
120 *
121 * Adds alarm to a alarm_base timerqueue and if necessary sets
122 * an hrtimer to run.
123 *
124 * Must hold base->lock when calling.
125 */
126static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
127{
128 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700129 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
130
John Stultzff3ead92011-01-11 09:42:13 -0800131 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
132 hrtimer_try_to_cancel(&base->timer);
133 hrtimer_start(&base->timer, alarm->node.expires,
134 HRTIMER_MODE_ABS);
135 }
136}
137
John Stultz180bf812011-04-28 12:58:11 -0700138/**
John Stultzff3ead92011-01-11 09:42:13 -0800139 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
140 * @base: pointer to the base where the timer is running
141 * @alarm: pointer to alarm being removed
142 *
143 * Removes alarm to a alarm_base timerqueue and if necessary sets
144 * a new timer to run.
145 *
146 * Must hold base->lock when calling.
147 */
148static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
149{
150 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
151
John Stultza28cde82011-08-10 12:30:21 -0700152 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
153 return;
154
John Stultzff3ead92011-01-11 09:42:13 -0800155 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700156 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
157
John Stultzff3ead92011-01-11 09:42:13 -0800158 if (next == &alarm->node) {
159 hrtimer_try_to_cancel(&base->timer);
160 next = timerqueue_getnext(&base->timerqueue);
161 if (!next)
162 return;
163 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
164 }
165}
166
John Stultz7068b7a2011-04-28 13:29:18 -0700167
John Stultz180bf812011-04-28 12:58:11 -0700168/**
John Stultz7068b7a2011-04-28 13:29:18 -0700169 * alarmtimer_fired - Handles alarm hrtimer being fired.
170 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800171 *
John Stultz180bf812011-04-28 12:58:11 -0700172 * When a alarm timer fires, this runs through the timerqueue to
173 * see which alarms expired, and runs those. If there are more alarm
174 * timers queued for the future, we set the hrtimer to fire when
175 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800176 */
John Stultz7068b7a2011-04-28 13:29:18 -0700177static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800178{
John Stultz7068b7a2011-04-28 13:29:18 -0700179 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
John Stultzff3ead92011-01-11 09:42:13 -0800180 struct timerqueue_node *next;
181 unsigned long flags;
182 ktime_t now;
John Stultz7068b7a2011-04-28 13:29:18 -0700183 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700184 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800185
186 spin_lock_irqsave(&base->lock, flags);
187 now = base->gettime();
188 while ((next = timerqueue_getnext(&base->timerqueue))) {
189 struct alarm *alarm;
190 ktime_t expired = next->expires;
191
192 if (expired.tv64 >= now.tv64)
193 break;
194
195 alarm = container_of(next, struct alarm, node);
196
197 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700198 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultz54da23b2011-08-10 11:08:07 -0700199
John Stultza28cde82011-08-10 12:30:21 -0700200 alarm->state |= ALARMTIMER_STATE_CALLBACK;
John Stultz54da23b2011-08-10 11:08:07 -0700201 spin_unlock_irqrestore(&base->lock, flags);
202 if (alarm->function)
203 restart = alarm->function(alarm, now);
204 spin_lock_irqsave(&base->lock, flags);
John Stultza28cde82011-08-10 12:30:21 -0700205 alarm->state &= ~ALARMTIMER_STATE_CALLBACK;
John Stultz54da23b2011-08-10 11:08:07 -0700206
207 if (restart != ALARMTIMER_NORESTART) {
John Stultzff3ead92011-01-11 09:42:13 -0800208 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700209 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800210 }
John Stultzff3ead92011-01-11 09:42:13 -0800211 }
212
213 if (next) {
John Stultz7068b7a2011-04-28 13:29:18 -0700214 hrtimer_set_expires(&base->timer, next->expires);
215 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800216 }
217 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800218
John Stultz7068b7a2011-04-28 13:29:18 -0700219 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800220
John Stultzff3ead92011-01-11 09:42:13 -0800221}
222
John Stultz472647d2011-04-29 15:03:10 -0700223#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700224/**
John Stultzff3ead92011-01-11 09:42:13 -0800225 * alarmtimer_suspend - Suspend time callback
226 * @dev: unused
227 * @state: unused
228 *
229 * When we are going into suspend, we look through the bases
230 * to see which is the soonest timer to expire. We then
231 * set an rtc timer to fire that far into the future, which
232 * will wake us from suspend.
233 */
234static int alarmtimer_suspend(struct device *dev)
235{
236 struct rtc_time tm;
237 ktime_t min, now;
238 unsigned long flags;
John Stultzc008ba582011-06-16 18:27:09 -0700239 struct rtc_device *rtc;
John Stultzff3ead92011-01-11 09:42:13 -0800240 int i;
241
242 spin_lock_irqsave(&freezer_delta_lock, flags);
243 min = freezer_delta;
244 freezer_delta = ktime_set(0, 0);
245 spin_unlock_irqrestore(&freezer_delta_lock, flags);
246
John Stultz1c6b39a2011-06-16 18:47:37 -0700247 rtc = rtcdev;
John Stultzff3ead92011-01-11 09:42:13 -0800248 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700249 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800250 return 0;
251
252 /* Find the soonest timer to expire*/
253 for (i = 0; i < ALARM_NUMTYPE; i++) {
254 struct alarm_base *base = &alarm_bases[i];
255 struct timerqueue_node *next;
256 ktime_t delta;
257
258 spin_lock_irqsave(&base->lock, flags);
259 next = timerqueue_getnext(&base->timerqueue);
260 spin_unlock_irqrestore(&base->lock, flags);
261 if (!next)
262 continue;
263 delta = ktime_sub(next->expires, base->gettime());
264 if (!min.tv64 || (delta.tv64 < min.tv64))
265 min = delta;
266 }
267 if (min.tv64 == 0)
268 return 0;
269
270 /* XXX - Should we enforce a minimum sleep time? */
271 WARN_ON(min.tv64 < NSEC_PER_SEC);
272
273 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700274 rtc_timer_cancel(rtc, &rtctimer);
275 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800276 now = rtc_tm_to_ktime(tm);
277 now = ktime_add(now, min);
278
John Stultzc008ba582011-06-16 18:27:09 -0700279 rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
John Stultzff3ead92011-01-11 09:42:13 -0800280
281 return 0;
282}
John Stultz472647d2011-04-29 15:03:10 -0700283#else
284static int alarmtimer_suspend(struct device *dev)
285{
286 return 0;
287}
288#endif
John Stultzff3ead92011-01-11 09:42:13 -0800289
John Stultz9a7adcf2011-01-11 09:54:33 -0800290static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
291{
292 ktime_t delta;
293 unsigned long flags;
294 struct alarm_base *base = &alarm_bases[type];
295
296 delta = ktime_sub(absexp, base->gettime());
297
298 spin_lock_irqsave(&freezer_delta_lock, flags);
299 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
300 freezer_delta = delta;
301 spin_unlock_irqrestore(&freezer_delta_lock, flags);
302}
303
304
John Stultz180bf812011-04-28 12:58:11 -0700305/**
John Stultzff3ead92011-01-11 09:42:13 -0800306 * alarm_init - Initialize an alarm structure
307 * @alarm: ptr to alarm to be initialized
308 * @type: the type of the alarm
309 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800310 */
311void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700312 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800313{
314 timerqueue_init(&alarm->node);
John Stultzff3ead92011-01-11 09:42:13 -0800315 alarm->function = function;
316 alarm->type = type;
John Stultza28cde82011-08-10 12:30:21 -0700317 alarm->state = ALARMTIMER_STATE_INACTIVE;
John Stultzff3ead92011-01-11 09:42:13 -0800318}
319
John Stultz180bf812011-04-28 12:58:11 -0700320/**
John Stultzff3ead92011-01-11 09:42:13 -0800321 * alarm_start - Sets an alarm to fire
322 * @alarm: ptr to alarm to set
323 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800324 */
John Stultz9e264762011-08-10 12:09:24 -0700325void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800326{
327 struct alarm_base *base = &alarm_bases[alarm->type];
328 unsigned long flags;
329
330 spin_lock_irqsave(&base->lock, flags);
John Stultza28cde82011-08-10 12:30:21 -0700331 if (alarmtimer_active(alarm))
John Stultzff3ead92011-01-11 09:42:13 -0800332 alarmtimer_remove(base, alarm);
333 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800334 alarmtimer_enqueue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800335 spin_unlock_irqrestore(&base->lock, flags);
336}
337
John Stultz180bf812011-04-28 12:58:11 -0700338/**
John Stultzff3ead92011-01-11 09:42:13 -0800339 * alarm_cancel - Tries to cancel an alarm timer
340 * @alarm: ptr to alarm to be canceled
John Stultzff3ead92011-01-11 09:42:13 -0800341 */
342void alarm_cancel(struct alarm *alarm)
343{
344 struct alarm_base *base = &alarm_bases[alarm->type];
345 unsigned long flags;
346
347 spin_lock_irqsave(&base->lock, flags);
John Stultza28cde82011-08-10 12:30:21 -0700348 if (alarmtimer_is_queued(alarm))
John Stultzff3ead92011-01-11 09:42:13 -0800349 alarmtimer_remove(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800350 spin_unlock_irqrestore(&base->lock, flags);
351}
352
353
John Stultzdce75a82011-08-10 11:31:03 -0700354
355u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
356{
357 u64 overrun = 1;
358 ktime_t delta;
359
360 delta = ktime_sub(now, alarm->node.expires);
361
362 if (delta.tv64 < 0)
363 return 0;
364
365 if (unlikely(delta.tv64 >= interval.tv64)) {
366 s64 incr = ktime_to_ns(interval);
367
368 overrun = ktime_divns(delta, incr);
369
370 alarm->node.expires = ktime_add_ns(alarm->node.expires,
371 incr*overrun);
372
373 if (alarm->node.expires.tv64 > now.tv64)
374 return overrun;
375 /*
376 * This (and the ktime_add() below) is the
377 * correction for exact:
378 */
379 overrun++;
380 }
381
382 alarm->node.expires = ktime_add(alarm->node.expires, interval);
383 return overrun;
384}
385
386
387
388
John Stultz180bf812011-04-28 12:58:11 -0700389/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800390 * clock2alarm - helper that converts from clockid to alarmtypes
391 * @clockid: clockid.
John Stultz9a7adcf2011-01-11 09:54:33 -0800392 */
393static enum alarmtimer_type clock2alarm(clockid_t clockid)
394{
395 if (clockid == CLOCK_REALTIME_ALARM)
396 return ALARM_REALTIME;
397 if (clockid == CLOCK_BOOTTIME_ALARM)
398 return ALARM_BOOTTIME;
399 return -1;
400}
401
John Stultz180bf812011-04-28 12:58:11 -0700402/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800403 * alarm_handle_timer - Callback for posix timers
404 * @alarm: alarm that fired
405 *
406 * Posix timer callback for expired alarm timers.
407 */
John Stultz4b413082011-08-10 10:37:59 -0700408static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
409 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800410{
411 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
John Stultz9e264762011-08-10 12:09:24 -0700412 it.alarm.alarmtimer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800413 if (posix_timer_event(ptr, 0) != 0)
414 ptr->it_overrun++;
John Stultz4b413082011-08-10 10:37:59 -0700415
John Stultz54da23b2011-08-10 11:08:07 -0700416 /* Re-add periodic timers */
John Stultz9e264762011-08-10 12:09:24 -0700417 if (ptr->it.alarm.interval.tv64) {
418 ptr->it_overrun += alarm_forward(alarm, now,
419 ptr->it.alarm.interval);
John Stultz54da23b2011-08-10 11:08:07 -0700420 return ALARMTIMER_RESTART;
421 }
John Stultz4b413082011-08-10 10:37:59 -0700422 return ALARMTIMER_NORESTART;
John Stultz9a7adcf2011-01-11 09:54:33 -0800423}
424
John Stultz180bf812011-04-28 12:58:11 -0700425/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800426 * alarm_clock_getres - posix getres interface
427 * @which_clock: clockid
428 * @tp: timespec to fill
429 *
430 * Returns the granularity of underlying alarm base clock
431 */
432static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
433{
434 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
435
John Stultz1c6b39a2011-06-16 18:47:37 -0700436 if (!alarmtimer_get_rtcdev())
437 return -ENOTSUPP;
438
John Stultz9a7adcf2011-01-11 09:54:33 -0800439 return hrtimer_get_res(baseid, tp);
440}
441
442/**
443 * alarm_clock_get - posix clock_get interface
444 * @which_clock: clockid
445 * @tp: timespec to fill.
446 *
447 * Provides the underlying alarm base time.
448 */
449static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
450{
451 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
452
John Stultz1c6b39a2011-06-16 18:47:37 -0700453 if (!alarmtimer_get_rtcdev())
454 return -ENOTSUPP;
455
John Stultz9a7adcf2011-01-11 09:54:33 -0800456 *tp = ktime_to_timespec(base->gettime());
457 return 0;
458}
459
460/**
461 * alarm_timer_create - posix timer_create interface
462 * @new_timer: k_itimer pointer to manage
463 *
464 * Initializes the k_itimer structure.
465 */
466static int alarm_timer_create(struct k_itimer *new_timer)
467{
468 enum alarmtimer_type type;
469 struct alarm_base *base;
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 if (!capable(CAP_WAKE_ALARM))
475 return -EPERM;
476
477 type = clock2alarm(new_timer->it_clock);
478 base = &alarm_bases[type];
John Stultz9e264762011-08-10 12:09:24 -0700479 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800480 return 0;
481}
482
483/**
484 * alarm_timer_get - posix timer_get interface
485 * @new_timer: k_itimer pointer
486 * @cur_setting: itimerspec data to fill
487 *
488 * Copies the itimerspec data out from the k_itimer
489 */
490static void alarm_timer_get(struct k_itimer *timr,
491 struct itimerspec *cur_setting)
492{
John Stultzea7802f2011-08-04 07:51:56 -0700493 memset(cur_setting, 0, sizeof(struct itimerspec));
494
John Stultz9a7adcf2011-01-11 09:54:33 -0800495 cur_setting->it_interval =
John Stultz9e264762011-08-10 12:09:24 -0700496 ktime_to_timespec(timr->it.alarm.interval);
John Stultz9a7adcf2011-01-11 09:54:33 -0800497 cur_setting->it_value =
John Stultz9e264762011-08-10 12:09:24 -0700498 ktime_to_timespec(timr->it.alarm.alarmtimer.node.expires);
John Stultz9a7adcf2011-01-11 09:54:33 -0800499 return;
500}
501
502/**
503 * alarm_timer_del - posix timer_del interface
504 * @timr: k_itimer pointer to be deleted
505 *
506 * Cancels any programmed alarms for the given timer.
507 */
508static int alarm_timer_del(struct k_itimer *timr)
509{
John Stultz1c6b39a2011-06-16 18:47:37 -0700510 if (!rtcdev)
511 return -ENOTSUPP;
512
John Stultz9e264762011-08-10 12:09:24 -0700513 alarm_cancel(&timr->it.alarm.alarmtimer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800514 return 0;
515}
516
517/**
518 * alarm_timer_set - posix timer_set interface
519 * @timr: k_itimer pointer to be deleted
520 * @flags: timer flags
521 * @new_setting: itimerspec to be used
522 * @old_setting: itimerspec being replaced
523 *
524 * Sets the timer to new_setting, and starts the timer.
525 */
526static int alarm_timer_set(struct k_itimer *timr, int flags,
527 struct itimerspec *new_setting,
528 struct itimerspec *old_setting)
529{
John Stultz1c6b39a2011-06-16 18:47:37 -0700530 if (!rtcdev)
531 return -ENOTSUPP;
532
John Stultz971c90b2011-08-04 07:25:35 -0700533 if (old_setting)
534 alarm_timer_get(timr, old_setting);
John Stultz9a7adcf2011-01-11 09:54:33 -0800535
536 /* If the timer was already set, cancel it */
John Stultz9e264762011-08-10 12:09:24 -0700537 alarm_cancel(&timr->it.alarm.alarmtimer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800538
539 /* start the timer */
John Stultz9e264762011-08-10 12:09:24 -0700540 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
541 alarm_start(&timr->it.alarm.alarmtimer,
542 timespec_to_ktime(new_setting->it_value));
John Stultz9a7adcf2011-01-11 09:54:33 -0800543 return 0;
544}
545
546/**
547 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
548 * @alarm: ptr to alarm that fired
549 *
550 * Wakes up the task that set the alarmtimer
551 */
John Stultz4b413082011-08-10 10:37:59 -0700552static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
553 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800554{
555 struct task_struct *task = (struct task_struct *)alarm->data;
556
557 alarm->data = NULL;
558 if (task)
559 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700560 return ALARMTIMER_NORESTART;
John Stultz9a7adcf2011-01-11 09:54:33 -0800561}
562
563/**
564 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
565 * @alarm: ptr to alarmtimer
566 * @absexp: absolute expiration time
567 *
568 * Sets the alarm timer and sleeps until it is fired or interrupted.
569 */
570static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
571{
572 alarm->data = (void *)current;
573 do {
574 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700575 alarm_start(alarm, absexp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800576 if (likely(alarm->data))
577 schedule();
578
579 alarm_cancel(alarm);
580 } while (alarm->data && !signal_pending(current));
581
582 __set_current_state(TASK_RUNNING);
583
584 return (alarm->data == NULL);
585}
586
587
588/**
589 * update_rmtp - Update remaining timespec value
590 * @exp: expiration time
591 * @type: timer type
592 * @rmtp: user pointer to remaining timepsec value
593 *
594 * Helper function that fills in rmtp value with time between
595 * now and the exp value
596 */
597static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
598 struct timespec __user *rmtp)
599{
600 struct timespec rmt;
601 ktime_t rem;
602
603 rem = ktime_sub(exp, alarm_bases[type].gettime());
604
605 if (rem.tv64 <= 0)
606 return 0;
607 rmt = ktime_to_timespec(rem);
608
609 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
610 return -EFAULT;
611
612 return 1;
613
614}
615
616/**
617 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
618 * @restart: ptr to restart block
619 *
620 * Handles restarted clock_nanosleep calls
621 */
622static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
623{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200624 enum alarmtimer_type type = restart->nanosleep.clockid;
John Stultz9a7adcf2011-01-11 09:54:33 -0800625 ktime_t exp;
626 struct timespec __user *rmtp;
627 struct alarm alarm;
628 int ret = 0;
629
630 exp.tv64 = restart->nanosleep.expires;
631 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
632
633 if (alarmtimer_do_nsleep(&alarm, exp))
634 goto out;
635
636 if (freezing(current))
637 alarmtimer_freezerset(exp, type);
638
639 rmtp = restart->nanosleep.rmtp;
640 if (rmtp) {
641 ret = update_rmtp(exp, type, rmtp);
642 if (ret <= 0)
643 goto out;
644 }
645
646
647 /* The other values in restart are already filled in */
648 ret = -ERESTART_RESTARTBLOCK;
649out:
650 return ret;
651}
652
653/**
654 * alarm_timer_nsleep - alarmtimer nanosleep
655 * @which_clock: clockid
656 * @flags: determins abstime or relative
657 * @tsreq: requested sleep time (abs or rel)
658 * @rmtp: remaining sleep time saved
659 *
660 * Handles clock_nanosleep calls against _ALARM clockids
661 */
662static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
663 struct timespec *tsreq, struct timespec __user *rmtp)
664{
665 enum alarmtimer_type type = clock2alarm(which_clock);
666 struct alarm alarm;
667 ktime_t exp;
668 int ret = 0;
669 struct restart_block *restart;
670
John Stultz1c6b39a2011-06-16 18:47:37 -0700671 if (!alarmtimer_get_rtcdev())
672 return -ENOTSUPP;
673
John Stultz9a7adcf2011-01-11 09:54:33 -0800674 if (!capable(CAP_WAKE_ALARM))
675 return -EPERM;
676
677 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
678
679 exp = timespec_to_ktime(*tsreq);
680 /* Convert (if necessary) to absolute time */
681 if (flags != TIMER_ABSTIME) {
682 ktime_t now = alarm_bases[type].gettime();
683 exp = ktime_add(now, exp);
684 }
685
686 if (alarmtimer_do_nsleep(&alarm, exp))
687 goto out;
688
689 if (freezing(current))
690 alarmtimer_freezerset(exp, type);
691
692 /* abs timers don't set remaining time or restart */
693 if (flags == TIMER_ABSTIME) {
694 ret = -ERESTARTNOHAND;
695 goto out;
696 }
697
698 if (rmtp) {
699 ret = update_rmtp(exp, type, rmtp);
700 if (ret <= 0)
701 goto out;
702 }
703
704 restart = &current_thread_info()->restart_block;
705 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200706 restart->nanosleep.clockid = type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800707 restart->nanosleep.expires = exp.tv64;
708 restart->nanosleep.rmtp = rmtp;
709 ret = -ERESTART_RESTARTBLOCK;
710
711out:
712 return ret;
713}
John Stultzff3ead92011-01-11 09:42:13 -0800714
John Stultzff3ead92011-01-11 09:42:13 -0800715
716/* Suspend hook structures */
717static const struct dev_pm_ops alarmtimer_pm_ops = {
718 .suspend = alarmtimer_suspend,
719};
720
721static struct platform_driver alarmtimer_driver = {
722 .driver = {
723 .name = "alarmtimer",
724 .pm = &alarmtimer_pm_ops,
725 }
726};
727
728/**
729 * alarmtimer_init - Initialize alarm timer code
730 *
731 * This function initializes the alarm bases and registers
732 * the posix clock ids.
733 */
734static int __init alarmtimer_init(void)
735{
736 int error = 0;
737 int i;
John Stultz9a7adcf2011-01-11 09:54:33 -0800738 struct k_clock alarm_clock = {
739 .clock_getres = alarm_clock_getres,
740 .clock_get = alarm_clock_get,
741 .timer_create = alarm_timer_create,
742 .timer_set = alarm_timer_set,
743 .timer_del = alarm_timer_del,
744 .timer_get = alarm_timer_get,
745 .nsleep = alarm_timer_nsleep,
746 };
747
748 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
749 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
John Stultzff3ead92011-01-11 09:42:13 -0800750
751 /* Initialize alarm bases */
752 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
753 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
754 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
755 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
756 for (i = 0; i < ALARM_NUMTYPE; i++) {
757 timerqueue_init_head(&alarm_bases[i].timerqueue);
758 spin_lock_init(&alarm_bases[i].lock);
759 hrtimer_init(&alarm_bases[i].timer,
760 alarm_bases[i].base_clockid,
761 HRTIMER_MODE_ABS);
762 alarm_bases[i].timer.function = alarmtimer_fired;
John Stultzff3ead92011-01-11 09:42:13 -0800763 }
764 error = platform_driver_register(&alarmtimer_driver);
765 platform_device_register_simple("alarmtimer", -1, NULL, 0);
766
767 return error;
768}
769device_initcall(alarmtimer_init);
770