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