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