Merge branch 'timers/clockevents' of git://git.linaro.org/people/dlezcano/clockevents...
[deliverable/linux.git] / kernel / time / alarmtimer.c
CommitLineData
ff3ead96
JS
1/*
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
180bf812
JS
29/**
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
180bf812 36 */
ff3ead96
JS
37static struct alarm_base {
38 spinlock_t lock;
39 struct timerqueue_head timerqueue;
ff3ead96
JS
40 ktime_t (*gettime)(void);
41 clockid_t base_clockid;
ff3ead96
JS
42} alarm_bases[ALARM_NUMTYPE];
43
c008ba58
JS
44/* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
45static ktime_t freezer_delta;
46static DEFINE_SPINLOCK(freezer_delta_lock);
47
59a93c27
TP
48static struct wakeup_source *ws;
49
472647dc 50#ifdef CONFIG_RTC_CLASS
180bf812 51/* rtc timer and device for setting alarm wakeups at suspend */
c5e14e76 52static struct rtc_timer rtctimer;
ff3ead96 53static struct rtc_device *rtcdev;
c008ba58 54static DEFINE_SPINLOCK(rtcdev_lock);
ff3ead96 55
c008ba58
JS
56/**
57 * 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 */
57c498fa 63struct rtc_device *alarmtimer_get_rtcdev(void)
c008ba58 64{
c008ba58
JS
65 unsigned long flags;
66 struct rtc_device *ret;
67
68 spin_lock_irqsave(&rtcdev_lock, flags);
c008ba58
JS
69 ret = rtcdev;
70 spin_unlock_irqrestore(&rtcdev_lock, flags);
71
72 return ret;
73}
8bc0dafb
JS
74
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
c5e14e76
TG
100static inline void alarmtimer_rtc_timer_init(void)
101{
102 rtc_timer_init(&rtctimer, NULL, NULL);
103}
104
8bc0dafb
JS
105static struct class_interface alarmtimer_rtc_interface = {
106 .add_dev = &alarmtimer_rtc_add_device,
107};
108
4523f6ad 109static int alarmtimer_rtc_interface_setup(void)
8bc0dafb
JS
110{
111 alarmtimer_rtc_interface.class = rtc_class;
4523f6ad
TG
112 return class_interface_register(&alarmtimer_rtc_interface);
113}
114static void alarmtimer_rtc_interface_remove(void)
115{
116 class_interface_unregister(&alarmtimer_rtc_interface);
8bc0dafb 117}
1c6b39ad 118#else
57c498fa 119struct rtc_device *alarmtimer_get_rtcdev(void)
4523f6ad
TG
120{
121 return NULL;
122}
123#define rtcdev (NULL)
124static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
125static inline void alarmtimer_rtc_interface_remove(void) { }
c5e14e76 126static inline void alarmtimer_rtc_timer_init(void) { }
c008ba58 127#endif
ff3ead96 128
180bf812 129/**
ff3ead96
JS
130 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
131 * @base: pointer to the base where the timer is being run
132 * @alarm: pointer to alarm being enqueued.
133 *
dae373be 134 * Adds alarm to a alarm_base timerqueue
ff3ead96
JS
135 *
136 * Must hold base->lock when calling.
137 */
138static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
139{
dae373be
JS
140 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
141 timerqueue_del(&base->timerqueue, &alarm->node);
142
ff3ead96 143 timerqueue_add(&base->timerqueue, &alarm->node);
a28cde81 144 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
ff3ead96
JS
145}
146
180bf812 147/**
a65bcc12 148 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
ff3ead96
JS
149 * @base: pointer to the base where the timer is running
150 * @alarm: pointer to alarm being removed
151 *
dae373be 152 * Removes alarm to a alarm_base timerqueue
ff3ead96
JS
153 *
154 * Must hold base->lock when calling.
155 */
a65bcc12 156static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
ff3ead96 157{
a28cde81
JS
158 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
159 return;
160
ff3ead96 161 timerqueue_del(&base->timerqueue, &alarm->node);
a28cde81 162 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
ff3ead96
JS
163}
164
7068b7a1 165
180bf812 166/**
7068b7a1
JS
167 * alarmtimer_fired - Handles alarm hrtimer being fired.
168 * @timer: pointer to hrtimer being run
ff3ead96 169 *
180bf812
JS
170 * When a alarm timer fires, this runs through the timerqueue to
171 * see which alarms expired, and runs those. If there are more alarm
172 * timers queued for the future, we set the hrtimer to fire when
173 * when the next future alarm timer expires.
ff3ead96 174 */
7068b7a1 175static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
ff3ead96 176{
dae373be
JS
177 struct alarm *alarm = container_of(timer, struct alarm, timer);
178 struct alarm_base *base = &alarm_bases[alarm->type];
ff3ead96 179 unsigned long flags;
7068b7a1 180 int ret = HRTIMER_NORESTART;
54da23b7 181 int restart = ALARMTIMER_NORESTART;
ff3ead96
JS
182
183 spin_lock_irqsave(&base->lock, flags);
a65bcc12 184 alarmtimer_dequeue(base, alarm);
dae373be 185 spin_unlock_irqrestore(&base->lock, flags);
54da23b7 186
dae373be
JS
187 if (alarm->function)
188 restart = alarm->function(alarm, base->gettime());
ff3ead96 189
dae373be
JS
190 spin_lock_irqsave(&base->lock, flags);
191 if (restart != ALARMTIMER_NORESTART) {
192 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
193 alarmtimer_enqueue(base, alarm);
7068b7a1 194 ret = HRTIMER_RESTART;
ff3ead96
JS
195 }
196 spin_unlock_irqrestore(&base->lock, flags);
ff3ead96 197
7068b7a1 198 return ret;
ff3ead96 199
ff3ead96
JS
200}
201
6cffe00f
TP
202ktime_t alarm_expires_remaining(const struct alarm *alarm)
203{
204 struct alarm_base *base = &alarm_bases[alarm->type];
205 return ktime_sub(alarm->node.expires, base->gettime());
206}
207
472647dc 208#ifdef CONFIG_RTC_CLASS
180bf812 209/**
ff3ead96
JS
210 * alarmtimer_suspend - Suspend time callback
211 * @dev: unused
212 * @state: unused
213 *
214 * When we are going into suspend, we look through the bases
215 * to see which is the soonest timer to expire. We then
216 * set an rtc timer to fire that far into the future, which
217 * will wake us from suspend.
218 */
219static int alarmtimer_suspend(struct device *dev)
220{
221 struct rtc_time tm;
222 ktime_t min, now;
223 unsigned long flags;
c008ba58 224 struct rtc_device *rtc;
ff3ead96 225 int i;
59a93c27 226 int ret;
ff3ead96
JS
227
228 spin_lock_irqsave(&freezer_delta_lock, flags);
229 min = freezer_delta;
230 freezer_delta = ktime_set(0, 0);
231 spin_unlock_irqrestore(&freezer_delta_lock, flags);
232
8bc0dafb 233 rtc = alarmtimer_get_rtcdev();
ff3ead96 234 /* If we have no rtcdev, just return */
c008ba58 235 if (!rtc)
ff3ead96
JS
236 return 0;
237
238 /* Find the soonest timer to expire*/
239 for (i = 0; i < ALARM_NUMTYPE; i++) {
240 struct alarm_base *base = &alarm_bases[i];
241 struct timerqueue_node *next;
242 ktime_t delta;
243
244 spin_lock_irqsave(&base->lock, flags);
245 next = timerqueue_getnext(&base->timerqueue);
246 spin_unlock_irqrestore(&base->lock, flags);
247 if (!next)
248 continue;
249 delta = ktime_sub(next->expires, base->gettime());
250 if (!min.tv64 || (delta.tv64 < min.tv64))
251 min = delta;
252 }
253 if (min.tv64 == 0)
254 return 0;
255
59a93c27
TP
256 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
257 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
258 return -EBUSY;
259 }
ff3ead96
JS
260
261 /* Setup an rtc timer to fire that far in the future */
c008ba58
JS
262 rtc_timer_cancel(rtc, &rtctimer);
263 rtc_read_time(rtc, &tm);
ff3ead96
JS
264 now = rtc_tm_to_ktime(tm);
265 now = ktime_add(now, min);
266
59a93c27
TP
267 /* Set alarm, if in the past reject suspend briefly to handle */
268 ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
269 if (ret < 0)
270 __pm_wakeup_event(ws, MSEC_PER_SEC);
271 return ret;
ff3ead96 272}
472647dc
JS
273#else
274static int alarmtimer_suspend(struct device *dev)
275{
276 return 0;
277}
278#endif
ff3ead96 279
9a7adcf5
JS
280static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
281{
282 ktime_t delta;
283 unsigned long flags;
284 struct alarm_base *base = &alarm_bases[type];
285
286 delta = ktime_sub(absexp, base->gettime());
287
288 spin_lock_irqsave(&freezer_delta_lock, flags);
289 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
290 freezer_delta = delta;
291 spin_unlock_irqrestore(&freezer_delta_lock, flags);
292}
293
294
180bf812 295/**
ff3ead96
JS
296 * alarm_init - Initialize an alarm structure
297 * @alarm: ptr to alarm to be initialized
298 * @type: the type of the alarm
299 * @function: callback that is run when the alarm fires
ff3ead96
JS
300 */
301void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
4b41308d 302 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
ff3ead96
JS
303{
304 timerqueue_init(&alarm->node);
dae373be
JS
305 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
306 HRTIMER_MODE_ABS);
307 alarm->timer.function = alarmtimer_fired;
ff3ead96
JS
308 alarm->function = function;
309 alarm->type = type;
a28cde81 310 alarm->state = ALARMTIMER_STATE_INACTIVE;
ff3ead96
JS
311}
312
180bf812 313/**
6cffe00f 314 * alarm_start - Sets an absolute alarm to fire
ff3ead96
JS
315 * @alarm: ptr to alarm to set
316 * @start: time to run the alarm
ff3ead96 317 */
dae373be 318int alarm_start(struct alarm *alarm, ktime_t start)
ff3ead96
JS
319{
320 struct alarm_base *base = &alarm_bases[alarm->type];
321 unsigned long flags;
dae373be 322 int ret;
ff3ead96
JS
323
324 spin_lock_irqsave(&base->lock, flags);
ff3ead96 325 alarm->node.expires = start;
ff3ead96 326 alarmtimer_enqueue(base, alarm);
dae373be
JS
327 ret = hrtimer_start(&alarm->timer, alarm->node.expires,
328 HRTIMER_MODE_ABS);
ff3ead96 329 spin_unlock_irqrestore(&base->lock, flags);
dae373be 330 return ret;
ff3ead96
JS
331}
332
6cffe00f
TP
333/**
334 * alarm_start_relative - Sets a relative alarm to fire
335 * @alarm: ptr to alarm to set
336 * @start: time relative to now to run the alarm
337 */
338int alarm_start_relative(struct alarm *alarm, ktime_t start)
339{
340 struct alarm_base *base = &alarm_bases[alarm->type];
341
342 start = ktime_add(start, base->gettime());
343 return alarm_start(alarm, start);
344}
345
346void alarm_restart(struct alarm *alarm)
347{
348 struct alarm_base *base = &alarm_bases[alarm->type];
349 unsigned long flags;
350
351 spin_lock_irqsave(&base->lock, flags);
352 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
353 hrtimer_restart(&alarm->timer);
354 alarmtimer_enqueue(base, alarm);
355 spin_unlock_irqrestore(&base->lock, flags);
356}
357
180bf812 358/**
9082c465 359 * alarm_try_to_cancel - Tries to cancel an alarm timer
ff3ead96 360 * @alarm: ptr to alarm to be canceled
9082c465
JS
361 *
362 * Returns 1 if the timer was canceled, 0 if it was not running,
363 * and -1 if the callback was running
ff3ead96 364 */
9082c465 365int alarm_try_to_cancel(struct alarm *alarm)
ff3ead96
JS
366{
367 struct alarm_base *base = &alarm_bases[alarm->type];
368 unsigned long flags;
dae373be 369 int ret;
9082c465 370
dae373be
JS
371 spin_lock_irqsave(&base->lock, flags);
372 ret = hrtimer_try_to_cancel(&alarm->timer);
373 if (ret >= 0)
a65bcc12 374 alarmtimer_dequeue(base, alarm);
ff3ead96 375 spin_unlock_irqrestore(&base->lock, flags);
9082c465 376 return ret;
ff3ead96
JS
377}
378
379
9082c465
JS
380/**
381 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
382 * @alarm: ptr to alarm to be canceled
383 *
384 * Returns 1 if the timer was canceled, 0 if it was not active.
385 */
386int alarm_cancel(struct alarm *alarm)
387{
388 for (;;) {
389 int ret = alarm_try_to_cancel(alarm);
390 if (ret >= 0)
391 return ret;
392 cpu_relax();
393 }
394}
395
dce75a8c
JS
396
397u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
398{
399 u64 overrun = 1;
400 ktime_t delta;
401
402 delta = ktime_sub(now, alarm->node.expires);
403
404 if (delta.tv64 < 0)
405 return 0;
406
407 if (unlikely(delta.tv64 >= interval.tv64)) {
408 s64 incr = ktime_to_ns(interval);
409
410 overrun = ktime_divns(delta, incr);
411
412 alarm->node.expires = ktime_add_ns(alarm->node.expires,
413 incr*overrun);
414
415 if (alarm->node.expires.tv64 > now.tv64)
416 return overrun;
417 /*
418 * This (and the ktime_add() below) is the
419 * correction for exact:
420 */
421 overrun++;
422 }
423
424 alarm->node.expires = ktime_add(alarm->node.expires, interval);
425 return overrun;
426}
427
6cffe00f
TP
428u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
429{
430 struct alarm_base *base = &alarm_bases[alarm->type];
431
432 return alarm_forward(alarm, base->gettime(), interval);
433}
dce75a8c
JS
434
435
436
180bf812 437/**
9a7adcf5
JS
438 * clock2alarm - helper that converts from clockid to alarmtypes
439 * @clockid: clockid.
9a7adcf5
JS
440 */
441static enum alarmtimer_type clock2alarm(clockid_t clockid)
442{
443 if (clockid == CLOCK_REALTIME_ALARM)
444 return ALARM_REALTIME;
445 if (clockid == CLOCK_BOOTTIME_ALARM)
446 return ALARM_BOOTTIME;
447 return -1;
448}
449
180bf812 450/**
9a7adcf5
JS
451 * alarm_handle_timer - Callback for posix timers
452 * @alarm: alarm that fired
453 *
454 * Posix timer callback for expired alarm timers.
455 */
4b41308d
JS
456static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
457 ktime_t now)
9a7adcf5
JS
458{
459 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
9e264762 460 it.alarm.alarmtimer);
9a7adcf5
JS
461 if (posix_timer_event(ptr, 0) != 0)
462 ptr->it_overrun++;
4b41308d 463
54da23b7 464 /* Re-add periodic timers */
9e264762
JS
465 if (ptr->it.alarm.interval.tv64) {
466 ptr->it_overrun += alarm_forward(alarm, now,
467 ptr->it.alarm.interval);
54da23b7
JS
468 return ALARMTIMER_RESTART;
469 }
4b41308d 470 return ALARMTIMER_NORESTART;
9a7adcf5
JS
471}
472
180bf812 473/**
9a7adcf5
JS
474 * alarm_clock_getres - posix getres interface
475 * @which_clock: clockid
476 * @tp: timespec to fill
477 *
478 * Returns the granularity of underlying alarm base clock
479 */
480static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
481{
482 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
483
1c6b39ad
JS
484 if (!alarmtimer_get_rtcdev())
485 return -ENOTSUPP;
486
9a7adcf5
JS
487 return hrtimer_get_res(baseid, tp);
488}
489
490/**
491 * alarm_clock_get - posix clock_get interface
492 * @which_clock: clockid
493 * @tp: timespec to fill.
494 *
495 * Provides the underlying alarm base time.
496 */
497static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
498{
499 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
500
1c6b39ad
JS
501 if (!alarmtimer_get_rtcdev())
502 return -ENOTSUPP;
503
9a7adcf5
JS
504 *tp = ktime_to_timespec(base->gettime());
505 return 0;
506}
507
508/**
509 * alarm_timer_create - posix timer_create interface
510 * @new_timer: k_itimer pointer to manage
511 *
512 * Initializes the k_itimer structure.
513 */
514static int alarm_timer_create(struct k_itimer *new_timer)
515{
516 enum alarmtimer_type type;
517 struct alarm_base *base;
518
1c6b39ad
JS
519 if (!alarmtimer_get_rtcdev())
520 return -ENOTSUPP;
521
9a7adcf5
JS
522 if (!capable(CAP_WAKE_ALARM))
523 return -EPERM;
524
525 type = clock2alarm(new_timer->it_clock);
526 base = &alarm_bases[type];
9e264762 527 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
9a7adcf5
JS
528 return 0;
529}
530
531/**
532 * alarm_timer_get - posix timer_get interface
533 * @new_timer: k_itimer pointer
534 * @cur_setting: itimerspec data to fill
535 *
536 * Copies the itimerspec data out from the k_itimer
537 */
538static void alarm_timer_get(struct k_itimer *timr,
539 struct itimerspec *cur_setting)
540{
ea7802f6
JS
541 memset(cur_setting, 0, sizeof(struct itimerspec));
542
9a7adcf5 543 cur_setting->it_interval =
9e264762 544 ktime_to_timespec(timr->it.alarm.interval);
9a7adcf5 545 cur_setting->it_value =
9e264762 546 ktime_to_timespec(timr->it.alarm.alarmtimer.node.expires);
9a7adcf5
JS
547 return;
548}
549
550/**
551 * alarm_timer_del - posix timer_del interface
552 * @timr: k_itimer pointer to be deleted
553 *
554 * Cancels any programmed alarms for the given timer.
555 */
556static int alarm_timer_del(struct k_itimer *timr)
557{
1c6b39ad
JS
558 if (!rtcdev)
559 return -ENOTSUPP;
560
9082c465
JS
561 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
562 return TIMER_RETRY;
563
9a7adcf5
JS
564 return 0;
565}
566
567/**
568 * alarm_timer_set - posix timer_set interface
569 * @timr: k_itimer pointer to be deleted
570 * @flags: timer flags
571 * @new_setting: itimerspec to be used
572 * @old_setting: itimerspec being replaced
573 *
574 * Sets the timer to new_setting, and starts the timer.
575 */
576static int alarm_timer_set(struct k_itimer *timr, int flags,
577 struct itimerspec *new_setting,
578 struct itimerspec *old_setting)
579{
1c6b39ad
JS
580 if (!rtcdev)
581 return -ENOTSUPP;
582
971c90bf
JS
583 if (old_setting)
584 alarm_timer_get(timr, old_setting);
9a7adcf5
JS
585
586 /* If the timer was already set, cancel it */
9082c465
JS
587 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
588 return TIMER_RETRY;
9a7adcf5
JS
589
590 /* start the timer */
9e264762
JS
591 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
592 alarm_start(&timr->it.alarm.alarmtimer,
593 timespec_to_ktime(new_setting->it_value));
9a7adcf5
JS
594 return 0;
595}
596
597/**
598 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
599 * @alarm: ptr to alarm that fired
600 *
601 * Wakes up the task that set the alarmtimer
602 */
4b41308d
JS
603static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
604 ktime_t now)
9a7adcf5
JS
605{
606 struct task_struct *task = (struct task_struct *)alarm->data;
607
608 alarm->data = NULL;
609 if (task)
610 wake_up_process(task);
4b41308d 611 return ALARMTIMER_NORESTART;
9a7adcf5
JS
612}
613
614/**
615 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
616 * @alarm: ptr to alarmtimer
617 * @absexp: absolute expiration time
618 *
619 * Sets the alarm timer and sleeps until it is fired or interrupted.
620 */
621static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
622{
623 alarm->data = (void *)current;
624 do {
625 set_current_state(TASK_INTERRUPTIBLE);
9e264762 626 alarm_start(alarm, absexp);
9a7adcf5
JS
627 if (likely(alarm->data))
628 schedule();
629
630 alarm_cancel(alarm);
631 } while (alarm->data && !signal_pending(current));
632
633 __set_current_state(TASK_RUNNING);
634
635 return (alarm->data == NULL);
636}
637
638
639/**
640 * update_rmtp - Update remaining timespec value
641 * @exp: expiration time
642 * @type: timer type
643 * @rmtp: user pointer to remaining timepsec value
644 *
645 * Helper function that fills in rmtp value with time between
646 * now and the exp value
647 */
648static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
649 struct timespec __user *rmtp)
650{
651 struct timespec rmt;
652 ktime_t rem;
653
654 rem = ktime_sub(exp, alarm_bases[type].gettime());
655
656 if (rem.tv64 <= 0)
657 return 0;
658 rmt = ktime_to_timespec(rem);
659
660 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
661 return -EFAULT;
662
663 return 1;
664
665}
666
667/**
668 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
669 * @restart: ptr to restart block
670 *
671 * Handles restarted clock_nanosleep calls
672 */
673static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
674{
ab8177bc 675 enum alarmtimer_type type = restart->nanosleep.clockid;
9a7adcf5
JS
676 ktime_t exp;
677 struct timespec __user *rmtp;
678 struct alarm alarm;
679 int ret = 0;
680
681 exp.tv64 = restart->nanosleep.expires;
682 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
683
684 if (alarmtimer_do_nsleep(&alarm, exp))
685 goto out;
686
687 if (freezing(current))
688 alarmtimer_freezerset(exp, type);
689
690 rmtp = restart->nanosleep.rmtp;
691 if (rmtp) {
692 ret = update_rmtp(exp, type, rmtp);
693 if (ret <= 0)
694 goto out;
695 }
696
697
698 /* The other values in restart are already filled in */
699 ret = -ERESTART_RESTARTBLOCK;
700out:
701 return ret;
702}
703
704/**
705 * alarm_timer_nsleep - alarmtimer nanosleep
706 * @which_clock: clockid
707 * @flags: determins abstime or relative
708 * @tsreq: requested sleep time (abs or rel)
709 * @rmtp: remaining sleep time saved
710 *
711 * Handles clock_nanosleep calls against _ALARM clockids
712 */
713static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
714 struct timespec *tsreq, struct timespec __user *rmtp)
715{
716 enum alarmtimer_type type = clock2alarm(which_clock);
717 struct alarm alarm;
718 ktime_t exp;
719 int ret = 0;
720 struct restart_block *restart;
721
1c6b39ad
JS
722 if (!alarmtimer_get_rtcdev())
723 return -ENOTSUPP;
724
9a7adcf5
JS
725 if (!capable(CAP_WAKE_ALARM))
726 return -EPERM;
727
728 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
729
730 exp = timespec_to_ktime(*tsreq);
731 /* Convert (if necessary) to absolute time */
732 if (flags != TIMER_ABSTIME) {
733 ktime_t now = alarm_bases[type].gettime();
734 exp = ktime_add(now, exp);
735 }
736
737 if (alarmtimer_do_nsleep(&alarm, exp))
738 goto out;
739
740 if (freezing(current))
741 alarmtimer_freezerset(exp, type);
742
743 /* abs timers don't set remaining time or restart */
744 if (flags == TIMER_ABSTIME) {
745 ret = -ERESTARTNOHAND;
746 goto out;
747 }
748
749 if (rmtp) {
750 ret = update_rmtp(exp, type, rmtp);
751 if (ret <= 0)
752 goto out;
753 }
754
755 restart = &current_thread_info()->restart_block;
756 restart->fn = alarm_timer_nsleep_restart;
ab8177bc 757 restart->nanosleep.clockid = type;
9a7adcf5
JS
758 restart->nanosleep.expires = exp.tv64;
759 restart->nanosleep.rmtp = rmtp;
760 ret = -ERESTART_RESTARTBLOCK;
761
762out:
763 return ret;
764}
ff3ead96 765
ff3ead96
JS
766
767/* Suspend hook structures */
768static const struct dev_pm_ops alarmtimer_pm_ops = {
769 .suspend = alarmtimer_suspend,
770};
771
772static struct platform_driver alarmtimer_driver = {
773 .driver = {
774 .name = "alarmtimer",
775 .pm = &alarmtimer_pm_ops,
776 }
777};
778
779/**
780 * alarmtimer_init - Initialize alarm timer code
781 *
782 * This function initializes the alarm bases and registers
783 * the posix clock ids.
784 */
785static int __init alarmtimer_init(void)
786{
4523f6ad 787 struct platform_device *pdev;
ff3ead96
JS
788 int error = 0;
789 int i;
9a7adcf5
JS
790 struct k_clock alarm_clock = {
791 .clock_getres = alarm_clock_getres,
792 .clock_get = alarm_clock_get,
793 .timer_create = alarm_timer_create,
794 .timer_set = alarm_timer_set,
795 .timer_del = alarm_timer_del,
796 .timer_get = alarm_timer_get,
797 .nsleep = alarm_timer_nsleep,
798 };
799
c5e14e76 800 alarmtimer_rtc_timer_init();
ad30dfa9 801
9a7adcf5
JS
802 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
803 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
ff3ead96
JS
804
805 /* Initialize alarm bases */
806 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
807 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
808 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
809 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
810 for (i = 0; i < ALARM_NUMTYPE; i++) {
811 timerqueue_init_head(&alarm_bases[i].timerqueue);
812 spin_lock_init(&alarm_bases[i].lock);
ff3ead96 813 }
8bc0dafb 814
4523f6ad
TG
815 error = alarmtimer_rtc_interface_setup();
816 if (error)
817 return error;
818
ff3ead96 819 error = platform_driver_register(&alarmtimer_driver);
4523f6ad
TG
820 if (error)
821 goto out_if;
ff3ead96 822
4523f6ad
TG
823 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
824 if (IS_ERR(pdev)) {
825 error = PTR_ERR(pdev);
826 goto out_drv;
827 }
59a93c27 828 ws = wakeup_source_register("alarmtimer");
4523f6ad
TG
829 return 0;
830
831out_drv:
832 platform_driver_unregister(&alarmtimer_driver);
833out_if:
834 alarmtimer_rtc_interface_remove();
ff3ead96
JS
835 return error;
836}
837device_initcall(alarmtimer_init);
This page took 0.159154 seconds and 5 git commands to generate.