blob: 98ecf4e36f2f22ed6e519c136863b4736f33e15d [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}
110#endif
John Stultzff3ead92011-01-11 09:42:13 -0800111
112
John Stultz180bf812011-04-28 12:58:11 -0700113/**
John Stultzff3ead92011-01-11 09:42:13 -0800114 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
115 * @base: pointer to the base where the timer is being run
116 * @alarm: pointer to alarm being enqueued.
117 *
118 * Adds alarm to a alarm_base timerqueue and if necessary sets
119 * an hrtimer to run.
120 *
121 * Must hold base->lock when calling.
122 */
123static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
124{
125 timerqueue_add(&base->timerqueue, &alarm->node);
126 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
127 hrtimer_try_to_cancel(&base->timer);
128 hrtimer_start(&base->timer, alarm->node.expires,
129 HRTIMER_MODE_ABS);
130 }
131}
132
John Stultz180bf812011-04-28 12:58:11 -0700133/**
John Stultzff3ead92011-01-11 09:42:13 -0800134 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
135 * @base: pointer to the base where the timer is running
136 * @alarm: pointer to alarm being removed
137 *
138 * Removes alarm to a alarm_base timerqueue and if necessary sets
139 * a new timer to run.
140 *
141 * Must hold base->lock when calling.
142 */
143static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
144{
145 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
146
147 timerqueue_del(&base->timerqueue, &alarm->node);
148 if (next == &alarm->node) {
149 hrtimer_try_to_cancel(&base->timer);
150 next = timerqueue_getnext(&base->timerqueue);
151 if (!next)
152 return;
153 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
154 }
155}
156
John Stultz7068b7a2011-04-28 13:29:18 -0700157
John Stultz180bf812011-04-28 12:58:11 -0700158/**
John Stultz7068b7a2011-04-28 13:29:18 -0700159 * alarmtimer_fired - Handles alarm hrtimer being fired.
160 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800161 *
John Stultz180bf812011-04-28 12:58:11 -0700162 * When a alarm timer fires, this runs through the timerqueue to
163 * see which alarms expired, and runs those. If there are more alarm
164 * timers queued for the future, we set the hrtimer to fire when
165 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800166 */
John Stultz7068b7a2011-04-28 13:29:18 -0700167static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800168{
John Stultz7068b7a2011-04-28 13:29:18 -0700169 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
John Stultzff3ead92011-01-11 09:42:13 -0800170 struct timerqueue_node *next;
171 unsigned long flags;
172 ktime_t now;
John Stultz7068b7a2011-04-28 13:29:18 -0700173 int ret = HRTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800174
175 spin_lock_irqsave(&base->lock, flags);
176 now = base->gettime();
177 while ((next = timerqueue_getnext(&base->timerqueue))) {
178 struct alarm *alarm;
179 ktime_t expired = next->expires;
180
181 if (expired.tv64 >= now.tv64)
182 break;
183
184 alarm = container_of(next, struct alarm, node);
185
186 timerqueue_del(&base->timerqueue, &alarm->node);
187 alarm->enabled = 0;
188 /* Re-add periodic timers */
189 if (alarm->period.tv64) {
190 alarm->node.expires = ktime_add(expired, alarm->period);
191 timerqueue_add(&base->timerqueue, &alarm->node);
192 alarm->enabled = 1;
193 }
194 spin_unlock_irqrestore(&base->lock, flags);
195 if (alarm->function)
196 alarm->function(alarm);
197 spin_lock_irqsave(&base->lock, flags);
198 }
199
200 if (next) {
John Stultz7068b7a2011-04-28 13:29:18 -0700201 hrtimer_set_expires(&base->timer, next->expires);
202 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800203 }
204 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800205
John Stultz7068b7a2011-04-28 13:29:18 -0700206 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800207
John Stultzff3ead92011-01-11 09:42:13 -0800208}
209
John Stultz472647d2011-04-29 15:03:10 -0700210#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700211/**
John Stultzff3ead92011-01-11 09:42:13 -0800212 * alarmtimer_suspend - Suspend time callback
213 * @dev: unused
214 * @state: unused
215 *
216 * When we are going into suspend, we look through the bases
217 * to see which is the soonest timer to expire. We then
218 * set an rtc timer to fire that far into the future, which
219 * will wake us from suspend.
220 */
221static int alarmtimer_suspend(struct device *dev)
222{
223 struct rtc_time tm;
224 ktime_t min, now;
225 unsigned long flags;
John Stultzc008ba582011-06-16 18:27:09 -0700226 struct rtc_device *rtc;
John Stultzff3ead92011-01-11 09:42:13 -0800227 int i;
228
229 spin_lock_irqsave(&freezer_delta_lock, flags);
230 min = freezer_delta;
231 freezer_delta = ktime_set(0, 0);
232 spin_unlock_irqrestore(&freezer_delta_lock, flags);
233
John Stultzc008ba582011-06-16 18:27:09 -0700234 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800235 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700236 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800237 return 0;
238
239 /* Find the soonest timer to expire*/
240 for (i = 0; i < ALARM_NUMTYPE; i++) {
241 struct alarm_base *base = &alarm_bases[i];
242 struct timerqueue_node *next;
243 ktime_t delta;
244
245 spin_lock_irqsave(&base->lock, flags);
246 next = timerqueue_getnext(&base->timerqueue);
247 spin_unlock_irqrestore(&base->lock, flags);
248 if (!next)
249 continue;
250 delta = ktime_sub(next->expires, base->gettime());
251 if (!min.tv64 || (delta.tv64 < min.tv64))
252 min = delta;
253 }
254 if (min.tv64 == 0)
255 return 0;
256
257 /* XXX - Should we enforce a minimum sleep time? */
258 WARN_ON(min.tv64 < NSEC_PER_SEC);
259
260 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700261 rtc_timer_cancel(rtc, &rtctimer);
262 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800263 now = rtc_tm_to_ktime(tm);
264 now = ktime_add(now, min);
265
John Stultzc008ba582011-06-16 18:27:09 -0700266 rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
John Stultzff3ead92011-01-11 09:42:13 -0800267
268 return 0;
269}
John Stultz472647d2011-04-29 15:03:10 -0700270#else
271static int alarmtimer_suspend(struct device *dev)
272{
273 return 0;
274}
275#endif
John Stultzff3ead92011-01-11 09:42:13 -0800276
John Stultz9a7adcf2011-01-11 09:54:33 -0800277static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
278{
279 ktime_t delta;
280 unsigned long flags;
281 struct alarm_base *base = &alarm_bases[type];
282
283 delta = ktime_sub(absexp, base->gettime());
284
285 spin_lock_irqsave(&freezer_delta_lock, flags);
286 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
287 freezer_delta = delta;
288 spin_unlock_irqrestore(&freezer_delta_lock, flags);
289}
290
291
John Stultz180bf812011-04-28 12:58:11 -0700292/**
John Stultzff3ead92011-01-11 09:42:13 -0800293 * alarm_init - Initialize an alarm structure
294 * @alarm: ptr to alarm to be initialized
295 * @type: the type of the alarm
296 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800297 */
298void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
299 void (*function)(struct alarm *))
300{
301 timerqueue_init(&alarm->node);
302 alarm->period = ktime_set(0, 0);
303 alarm->function = function;
304 alarm->type = type;
305 alarm->enabled = 0;
306}
307
John Stultz180bf812011-04-28 12:58:11 -0700308/**
John Stultzff3ead92011-01-11 09:42:13 -0800309 * alarm_start - Sets an alarm to fire
310 * @alarm: ptr to alarm to set
311 * @start: time to run the alarm
312 * @period: period at which the alarm will recur
John Stultzff3ead92011-01-11 09:42:13 -0800313 */
314void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period)
315{
316 struct alarm_base *base = &alarm_bases[alarm->type];
317 unsigned long flags;
318
319 spin_lock_irqsave(&base->lock, flags);
320 if (alarm->enabled)
321 alarmtimer_remove(base, alarm);
322 alarm->node.expires = start;
323 alarm->period = period;
324 alarmtimer_enqueue(base, alarm);
325 alarm->enabled = 1;
326 spin_unlock_irqrestore(&base->lock, flags);
327}
328
John Stultz180bf812011-04-28 12:58:11 -0700329/**
John Stultzff3ead92011-01-11 09:42:13 -0800330 * alarm_cancel - Tries to cancel an alarm timer
331 * @alarm: ptr to alarm to be canceled
John Stultzff3ead92011-01-11 09:42:13 -0800332 */
333void alarm_cancel(struct alarm *alarm)
334{
335 struct alarm_base *base = &alarm_bases[alarm->type];
336 unsigned long flags;
337
338 spin_lock_irqsave(&base->lock, flags);
339 if (alarm->enabled)
340 alarmtimer_remove(base, alarm);
341 alarm->enabled = 0;
342 spin_unlock_irqrestore(&base->lock, flags);
343}
344
345
John Stultz180bf812011-04-28 12:58:11 -0700346/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800347 * clock2alarm - helper that converts from clockid to alarmtypes
348 * @clockid: clockid.
John Stultz9a7adcf2011-01-11 09:54:33 -0800349 */
350static enum alarmtimer_type clock2alarm(clockid_t clockid)
351{
352 if (clockid == CLOCK_REALTIME_ALARM)
353 return ALARM_REALTIME;
354 if (clockid == CLOCK_BOOTTIME_ALARM)
355 return ALARM_BOOTTIME;
356 return -1;
357}
358
John Stultz180bf812011-04-28 12:58:11 -0700359/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800360 * alarm_handle_timer - Callback for posix timers
361 * @alarm: alarm that fired
362 *
363 * Posix timer callback for expired alarm timers.
364 */
365static void alarm_handle_timer(struct alarm *alarm)
366{
367 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
368 it.alarmtimer);
369 if (posix_timer_event(ptr, 0) != 0)
370 ptr->it_overrun++;
371}
372
John Stultz180bf812011-04-28 12:58:11 -0700373/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800374 * alarm_clock_getres - posix getres interface
375 * @which_clock: clockid
376 * @tp: timespec to fill
377 *
378 * Returns the granularity of underlying alarm base clock
379 */
380static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
381{
382 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
383
384 return hrtimer_get_res(baseid, tp);
385}
386
387/**
388 * alarm_clock_get - posix clock_get interface
389 * @which_clock: clockid
390 * @tp: timespec to fill.
391 *
392 * Provides the underlying alarm base time.
393 */
394static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
395{
396 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
397
398 *tp = ktime_to_timespec(base->gettime());
399 return 0;
400}
401
402/**
403 * alarm_timer_create - posix timer_create interface
404 * @new_timer: k_itimer pointer to manage
405 *
406 * Initializes the k_itimer structure.
407 */
408static int alarm_timer_create(struct k_itimer *new_timer)
409{
410 enum alarmtimer_type type;
411 struct alarm_base *base;
412
413 if (!capable(CAP_WAKE_ALARM))
414 return -EPERM;
415
416 type = clock2alarm(new_timer->it_clock);
417 base = &alarm_bases[type];
418 alarm_init(&new_timer->it.alarmtimer, type, alarm_handle_timer);
419 return 0;
420}
421
422/**
423 * alarm_timer_get - posix timer_get interface
424 * @new_timer: k_itimer pointer
425 * @cur_setting: itimerspec data to fill
426 *
427 * Copies the itimerspec data out from the k_itimer
428 */
429static void alarm_timer_get(struct k_itimer *timr,
430 struct itimerspec *cur_setting)
431{
432 cur_setting->it_interval =
433 ktime_to_timespec(timr->it.alarmtimer.period);
434 cur_setting->it_value =
435 ktime_to_timespec(timr->it.alarmtimer.node.expires);
436 return;
437}
438
439/**
440 * alarm_timer_del - posix timer_del interface
441 * @timr: k_itimer pointer to be deleted
442 *
443 * Cancels any programmed alarms for the given timer.
444 */
445static int alarm_timer_del(struct k_itimer *timr)
446{
447 alarm_cancel(&timr->it.alarmtimer);
448 return 0;
449}
450
451/**
452 * alarm_timer_set - posix timer_set interface
453 * @timr: k_itimer pointer to be deleted
454 * @flags: timer flags
455 * @new_setting: itimerspec to be used
456 * @old_setting: itimerspec being replaced
457 *
458 * Sets the timer to new_setting, and starts the timer.
459 */
460static int alarm_timer_set(struct k_itimer *timr, int flags,
461 struct itimerspec *new_setting,
462 struct itimerspec *old_setting)
463{
464 /* Save old values */
465 old_setting->it_interval =
466 ktime_to_timespec(timr->it.alarmtimer.period);
467 old_setting->it_value =
468 ktime_to_timespec(timr->it.alarmtimer.node.expires);
469
470 /* If the timer was already set, cancel it */
471 alarm_cancel(&timr->it.alarmtimer);
472
473 /* start the timer */
474 alarm_start(&timr->it.alarmtimer,
475 timespec_to_ktime(new_setting->it_value),
476 timespec_to_ktime(new_setting->it_interval));
477 return 0;
478}
479
480/**
481 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
482 * @alarm: ptr to alarm that fired
483 *
484 * Wakes up the task that set the alarmtimer
485 */
486static void alarmtimer_nsleep_wakeup(struct alarm *alarm)
487{
488 struct task_struct *task = (struct task_struct *)alarm->data;
489
490 alarm->data = NULL;
491 if (task)
492 wake_up_process(task);
493}
494
495/**
496 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
497 * @alarm: ptr to alarmtimer
498 * @absexp: absolute expiration time
499 *
500 * Sets the alarm timer and sleeps until it is fired or interrupted.
501 */
502static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
503{
504 alarm->data = (void *)current;
505 do {
506 set_current_state(TASK_INTERRUPTIBLE);
507 alarm_start(alarm, absexp, ktime_set(0, 0));
508 if (likely(alarm->data))
509 schedule();
510
511 alarm_cancel(alarm);
512 } while (alarm->data && !signal_pending(current));
513
514 __set_current_state(TASK_RUNNING);
515
516 return (alarm->data == NULL);
517}
518
519
520/**
521 * update_rmtp - Update remaining timespec value
522 * @exp: expiration time
523 * @type: timer type
524 * @rmtp: user pointer to remaining timepsec value
525 *
526 * Helper function that fills in rmtp value with time between
527 * now and the exp value
528 */
529static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
530 struct timespec __user *rmtp)
531{
532 struct timespec rmt;
533 ktime_t rem;
534
535 rem = ktime_sub(exp, alarm_bases[type].gettime());
536
537 if (rem.tv64 <= 0)
538 return 0;
539 rmt = ktime_to_timespec(rem);
540
541 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
542 return -EFAULT;
543
544 return 1;
545
546}
547
548/**
549 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
550 * @restart: ptr to restart block
551 *
552 * Handles restarted clock_nanosleep calls
553 */
554static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
555{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200556 enum alarmtimer_type type = restart->nanosleep.clockid;
John Stultz9a7adcf2011-01-11 09:54:33 -0800557 ktime_t exp;
558 struct timespec __user *rmtp;
559 struct alarm alarm;
560 int ret = 0;
561
562 exp.tv64 = restart->nanosleep.expires;
563 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
564
565 if (alarmtimer_do_nsleep(&alarm, exp))
566 goto out;
567
568 if (freezing(current))
569 alarmtimer_freezerset(exp, type);
570
571 rmtp = restart->nanosleep.rmtp;
572 if (rmtp) {
573 ret = update_rmtp(exp, type, rmtp);
574 if (ret <= 0)
575 goto out;
576 }
577
578
579 /* The other values in restart are already filled in */
580 ret = -ERESTART_RESTARTBLOCK;
581out:
582 return ret;
583}
584
585/**
586 * alarm_timer_nsleep - alarmtimer nanosleep
587 * @which_clock: clockid
588 * @flags: determins abstime or relative
589 * @tsreq: requested sleep time (abs or rel)
590 * @rmtp: remaining sleep time saved
591 *
592 * Handles clock_nanosleep calls against _ALARM clockids
593 */
594static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
595 struct timespec *tsreq, struct timespec __user *rmtp)
596{
597 enum alarmtimer_type type = clock2alarm(which_clock);
598 struct alarm alarm;
599 ktime_t exp;
600 int ret = 0;
601 struct restart_block *restart;
602
603 if (!capable(CAP_WAKE_ALARM))
604 return -EPERM;
605
606 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
607
608 exp = timespec_to_ktime(*tsreq);
609 /* Convert (if necessary) to absolute time */
610 if (flags != TIMER_ABSTIME) {
611 ktime_t now = alarm_bases[type].gettime();
612 exp = ktime_add(now, exp);
613 }
614
615 if (alarmtimer_do_nsleep(&alarm, exp))
616 goto out;
617
618 if (freezing(current))
619 alarmtimer_freezerset(exp, type);
620
621 /* abs timers don't set remaining time or restart */
622 if (flags == TIMER_ABSTIME) {
623 ret = -ERESTARTNOHAND;
624 goto out;
625 }
626
627 if (rmtp) {
628 ret = update_rmtp(exp, type, rmtp);
629 if (ret <= 0)
630 goto out;
631 }
632
633 restart = &current_thread_info()->restart_block;
634 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200635 restart->nanosleep.clockid = type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800636 restart->nanosleep.expires = exp.tv64;
637 restart->nanosleep.rmtp = rmtp;
638 ret = -ERESTART_RESTARTBLOCK;
639
640out:
641 return ret;
642}
John Stultzff3ead92011-01-11 09:42:13 -0800643
John Stultzff3ead92011-01-11 09:42:13 -0800644
645/* Suspend hook structures */
646static const struct dev_pm_ops alarmtimer_pm_ops = {
647 .suspend = alarmtimer_suspend,
648};
649
650static struct platform_driver alarmtimer_driver = {
651 .driver = {
652 .name = "alarmtimer",
653 .pm = &alarmtimer_pm_ops,
654 }
655};
656
657/**
658 * alarmtimer_init - Initialize alarm timer code
659 *
660 * This function initializes the alarm bases and registers
661 * the posix clock ids.
662 */
663static int __init alarmtimer_init(void)
664{
665 int error = 0;
666 int i;
John Stultz9a7adcf2011-01-11 09:54:33 -0800667 struct k_clock alarm_clock = {
668 .clock_getres = alarm_clock_getres,
669 .clock_get = alarm_clock_get,
670 .timer_create = alarm_timer_create,
671 .timer_set = alarm_timer_set,
672 .timer_del = alarm_timer_del,
673 .timer_get = alarm_timer_get,
674 .nsleep = alarm_timer_nsleep,
675 };
676
677 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
678 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
John Stultzff3ead92011-01-11 09:42:13 -0800679
680 /* Initialize alarm bases */
681 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
682 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
683 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
684 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
685 for (i = 0; i < ALARM_NUMTYPE; i++) {
686 timerqueue_init_head(&alarm_bases[i].timerqueue);
687 spin_lock_init(&alarm_bases[i].lock);
688 hrtimer_init(&alarm_bases[i].timer,
689 alarm_bases[i].base_clockid,
690 HRTIMER_MODE_ABS);
691 alarm_bases[i].timer.function = alarmtimer_fired;
John Stultzff3ead92011-01-11 09:42:13 -0800692 }
693 error = platform_driver_register(&alarmtimer_driver);
694 platform_device_register_simple("alarmtimer", -1, NULL, 0);
695
696 return error;
697}
698device_initcall(alarmtimer_init);
699