]>
Commit | Line | Data |
---|---|---|
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 | ||
29 | ||
30 | static struct alarm_base { | |
31 | spinlock_t lock; | |
32 | struct timerqueue_head timerqueue; | |
33 | struct hrtimer timer; | |
34 | ktime_t (*gettime)(void); | |
35 | clockid_t base_clockid; | |
36 | struct work_struct irqwork; | |
37 | } alarm_bases[ALARM_NUMTYPE]; | |
38 | ||
39 | static struct rtc_timer rtctimer; | |
40 | static struct rtc_device *rtcdev; | |
41 | ||
42 | static ktime_t freezer_delta; | |
43 | static DEFINE_SPINLOCK(freezer_delta_lock); | |
44 | ||
45 | ||
46 | /************************************************************************** | |
47 | * alarmtimer management code | |
48 | */ | |
49 | ||
50 | /* | |
51 | * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue | |
52 | * @base: pointer to the base where the timer is being run | |
53 | * @alarm: pointer to alarm being enqueued. | |
54 | * | |
55 | * Adds alarm to a alarm_base timerqueue and if necessary sets | |
56 | * an hrtimer to run. | |
57 | * | |
58 | * Must hold base->lock when calling. | |
59 | */ | |
60 | static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm) | |
61 | { | |
62 | timerqueue_add(&base->timerqueue, &alarm->node); | |
63 | if (&alarm->node == timerqueue_getnext(&base->timerqueue)) { | |
64 | hrtimer_try_to_cancel(&base->timer); | |
65 | hrtimer_start(&base->timer, alarm->node.expires, | |
66 | HRTIMER_MODE_ABS); | |
67 | } | |
68 | } | |
69 | ||
70 | /* | |
71 | * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue | |
72 | * @base: pointer to the base where the timer is running | |
73 | * @alarm: pointer to alarm being removed | |
74 | * | |
75 | * Removes alarm to a alarm_base timerqueue and if necessary sets | |
76 | * a new timer to run. | |
77 | * | |
78 | * Must hold base->lock when calling. | |
79 | */ | |
80 | static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm) | |
81 | { | |
82 | struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue); | |
83 | ||
84 | timerqueue_del(&base->timerqueue, &alarm->node); | |
85 | if (next == &alarm->node) { | |
86 | hrtimer_try_to_cancel(&base->timer); | |
87 | next = timerqueue_getnext(&base->timerqueue); | |
88 | if (!next) | |
89 | return; | |
90 | hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS); | |
91 | } | |
92 | } | |
93 | ||
94 | /* | |
95 | * alarmtimer_do_work - Handles alarm being fired. | |
96 | * @work: pointer to workqueue being run | |
97 | * | |
98 | * When a timer fires, this runs through the timerqueue to see | |
99 | * which alarm timers, and run those that expired. If there are | |
100 | * more alarm timers queued, we set the hrtimer to fire in the | |
101 | * future. | |
102 | */ | |
103 | void alarmtimer_do_work(struct work_struct *work) | |
104 | { | |
105 | struct alarm_base *base = container_of(work, struct alarm_base, | |
106 | irqwork); | |
107 | struct timerqueue_node *next; | |
108 | unsigned long flags; | |
109 | ktime_t now; | |
110 | ||
111 | spin_lock_irqsave(&base->lock, flags); | |
112 | now = base->gettime(); | |
113 | while ((next = timerqueue_getnext(&base->timerqueue))) { | |
114 | struct alarm *alarm; | |
115 | ktime_t expired = next->expires; | |
116 | ||
117 | if (expired.tv64 >= now.tv64) | |
118 | break; | |
119 | ||
120 | alarm = container_of(next, struct alarm, node); | |
121 | ||
122 | timerqueue_del(&base->timerqueue, &alarm->node); | |
123 | alarm->enabled = 0; | |
124 | /* Re-add periodic timers */ | |
125 | if (alarm->period.tv64) { | |
126 | alarm->node.expires = ktime_add(expired, alarm->period); | |
127 | timerqueue_add(&base->timerqueue, &alarm->node); | |
128 | alarm->enabled = 1; | |
129 | } | |
130 | spin_unlock_irqrestore(&base->lock, flags); | |
131 | if (alarm->function) | |
132 | alarm->function(alarm); | |
133 | spin_lock_irqsave(&base->lock, flags); | |
134 | } | |
135 | ||
136 | if (next) { | |
137 | hrtimer_start(&base->timer, next->expires, | |
138 | HRTIMER_MODE_ABS); | |
139 | } | |
140 | spin_unlock_irqrestore(&base->lock, flags); | |
141 | } | |
142 | ||
143 | ||
144 | /* | |
145 | * alarmtimer_fired - Handles alarm hrtimer being fired. | |
146 | * @timer: pointer to hrtimer being run | |
147 | * | |
148 | * When a timer fires, this schedules the do_work function to | |
149 | * be run. | |
150 | */ | |
151 | static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer) | |
152 | { | |
153 | struct alarm_base *base = container_of(timer, struct alarm_base, timer); | |
154 | schedule_work(&base->irqwork); | |
155 | return HRTIMER_NORESTART; | |
156 | } | |
157 | ||
158 | ||
159 | /* | |
160 | * alarmtimer_suspend - Suspend time callback | |
161 | * @dev: unused | |
162 | * @state: unused | |
163 | * | |
164 | * When we are going into suspend, we look through the bases | |
165 | * to see which is the soonest timer to expire. We then | |
166 | * set an rtc timer to fire that far into the future, which | |
167 | * will wake us from suspend. | |
168 | */ | |
169 | static int alarmtimer_suspend(struct device *dev) | |
170 | { | |
171 | struct rtc_time tm; | |
172 | ktime_t min, now; | |
173 | unsigned long flags; | |
174 | int i; | |
175 | ||
176 | spin_lock_irqsave(&freezer_delta_lock, flags); | |
177 | min = freezer_delta; | |
178 | freezer_delta = ktime_set(0, 0); | |
179 | spin_unlock_irqrestore(&freezer_delta_lock, flags); | |
180 | ||
181 | /* If we have no rtcdev, just return */ | |
182 | if (!rtcdev) | |
183 | return 0; | |
184 | ||
185 | /* Find the soonest timer to expire*/ | |
186 | for (i = 0; i < ALARM_NUMTYPE; i++) { | |
187 | struct alarm_base *base = &alarm_bases[i]; | |
188 | struct timerqueue_node *next; | |
189 | ktime_t delta; | |
190 | ||
191 | spin_lock_irqsave(&base->lock, flags); | |
192 | next = timerqueue_getnext(&base->timerqueue); | |
193 | spin_unlock_irqrestore(&base->lock, flags); | |
194 | if (!next) | |
195 | continue; | |
196 | delta = ktime_sub(next->expires, base->gettime()); | |
197 | if (!min.tv64 || (delta.tv64 < min.tv64)) | |
198 | min = delta; | |
199 | } | |
200 | if (min.tv64 == 0) | |
201 | return 0; | |
202 | ||
203 | /* XXX - Should we enforce a minimum sleep time? */ | |
204 | WARN_ON(min.tv64 < NSEC_PER_SEC); | |
205 | ||
206 | /* Setup an rtc timer to fire that far in the future */ | |
207 | rtc_timer_cancel(rtcdev, &rtctimer); | |
208 | rtc_read_time(rtcdev, &tm); | |
209 | now = rtc_tm_to_ktime(tm); | |
210 | now = ktime_add(now, min); | |
211 | ||
212 | rtc_timer_start(rtcdev, &rtctimer, now, ktime_set(0, 0)); | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
217 | ||
9a7adcf5 JS |
218 | static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type) |
219 | { | |
220 | ktime_t delta; | |
221 | unsigned long flags; | |
222 | struct alarm_base *base = &alarm_bases[type]; | |
223 | ||
224 | delta = ktime_sub(absexp, base->gettime()); | |
225 | ||
226 | spin_lock_irqsave(&freezer_delta_lock, flags); | |
227 | if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64)) | |
228 | freezer_delta = delta; | |
229 | spin_unlock_irqrestore(&freezer_delta_lock, flags); | |
230 | } | |
231 | ||
232 | ||
ff3ead96 JS |
233 | /************************************************************************** |
234 | * alarm kernel interface code | |
235 | */ | |
236 | ||
237 | /* | |
238 | * alarm_init - Initialize an alarm structure | |
239 | * @alarm: ptr to alarm to be initialized | |
240 | * @type: the type of the alarm | |
241 | * @function: callback that is run when the alarm fires | |
242 | * | |
243 | * In-kernel interface to initializes the alarm structure. | |
244 | */ | |
245 | void alarm_init(struct alarm *alarm, enum alarmtimer_type type, | |
246 | void (*function)(struct alarm *)) | |
247 | { | |
248 | timerqueue_init(&alarm->node); | |
249 | alarm->period = ktime_set(0, 0); | |
250 | alarm->function = function; | |
251 | alarm->type = type; | |
252 | alarm->enabled = 0; | |
253 | } | |
254 | ||
255 | /* | |
256 | * alarm_start - Sets an alarm to fire | |
257 | * @alarm: ptr to alarm to set | |
258 | * @start: time to run the alarm | |
259 | * @period: period at which the alarm will recur | |
260 | * | |
261 | * In-kernel interface set an alarm timer. | |
262 | */ | |
263 | void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period) | |
264 | { | |
265 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
266 | unsigned long flags; | |
267 | ||
268 | spin_lock_irqsave(&base->lock, flags); | |
269 | if (alarm->enabled) | |
270 | alarmtimer_remove(base, alarm); | |
271 | alarm->node.expires = start; | |
272 | alarm->period = period; | |
273 | alarmtimer_enqueue(base, alarm); | |
274 | alarm->enabled = 1; | |
275 | spin_unlock_irqrestore(&base->lock, flags); | |
276 | } | |
277 | ||
278 | /* | |
279 | * alarm_cancel - Tries to cancel an alarm timer | |
280 | * @alarm: ptr to alarm to be canceled | |
281 | * | |
282 | * In-kernel interface to cancel an alarm timer. | |
283 | */ | |
284 | void alarm_cancel(struct alarm *alarm) | |
285 | { | |
286 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
287 | unsigned long flags; | |
288 | ||
289 | spin_lock_irqsave(&base->lock, flags); | |
290 | if (alarm->enabled) | |
291 | alarmtimer_remove(base, alarm); | |
292 | alarm->enabled = 0; | |
293 | spin_unlock_irqrestore(&base->lock, flags); | |
294 | } | |
295 | ||
296 | ||
9a7adcf5 JS |
297 | /************************************************************************** |
298 | * alarm posix interface code | |
299 | */ | |
300 | ||
301 | /* | |
302 | * clock2alarm - helper that converts from clockid to alarmtypes | |
303 | * @clockid: clockid. | |
304 | * | |
305 | * Helper function that converts from clockids to alarmtypes | |
306 | */ | |
307 | static enum alarmtimer_type clock2alarm(clockid_t clockid) | |
308 | { | |
309 | if (clockid == CLOCK_REALTIME_ALARM) | |
310 | return ALARM_REALTIME; | |
311 | if (clockid == CLOCK_BOOTTIME_ALARM) | |
312 | return ALARM_BOOTTIME; | |
313 | return -1; | |
314 | } | |
315 | ||
316 | /* | |
317 | * alarm_handle_timer - Callback for posix timers | |
318 | * @alarm: alarm that fired | |
319 | * | |
320 | * Posix timer callback for expired alarm timers. | |
321 | */ | |
322 | static void alarm_handle_timer(struct alarm *alarm) | |
323 | { | |
324 | struct k_itimer *ptr = container_of(alarm, struct k_itimer, | |
325 | it.alarmtimer); | |
326 | if (posix_timer_event(ptr, 0) != 0) | |
327 | ptr->it_overrun++; | |
328 | } | |
329 | ||
330 | /* | |
331 | * alarm_clock_getres - posix getres interface | |
332 | * @which_clock: clockid | |
333 | * @tp: timespec to fill | |
334 | * | |
335 | * Returns the granularity of underlying alarm base clock | |
336 | */ | |
337 | static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp) | |
338 | { | |
339 | clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid; | |
340 | ||
341 | return hrtimer_get_res(baseid, tp); | |
342 | } | |
343 | ||
344 | /** | |
345 | * alarm_clock_get - posix clock_get interface | |
346 | * @which_clock: clockid | |
347 | * @tp: timespec to fill. | |
348 | * | |
349 | * Provides the underlying alarm base time. | |
350 | */ | |
351 | static int alarm_clock_get(clockid_t which_clock, struct timespec *tp) | |
352 | { | |
353 | struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)]; | |
354 | ||
355 | *tp = ktime_to_timespec(base->gettime()); | |
356 | return 0; | |
357 | } | |
358 | ||
359 | /** | |
360 | * alarm_timer_create - posix timer_create interface | |
361 | * @new_timer: k_itimer pointer to manage | |
362 | * | |
363 | * Initializes the k_itimer structure. | |
364 | */ | |
365 | static int alarm_timer_create(struct k_itimer *new_timer) | |
366 | { | |
367 | enum alarmtimer_type type; | |
368 | struct alarm_base *base; | |
369 | ||
370 | if (!capable(CAP_WAKE_ALARM)) | |
371 | return -EPERM; | |
372 | ||
373 | type = clock2alarm(new_timer->it_clock); | |
374 | base = &alarm_bases[type]; | |
375 | alarm_init(&new_timer->it.alarmtimer, type, alarm_handle_timer); | |
376 | return 0; | |
377 | } | |
378 | ||
379 | /** | |
380 | * alarm_timer_get - posix timer_get interface | |
381 | * @new_timer: k_itimer pointer | |
382 | * @cur_setting: itimerspec data to fill | |
383 | * | |
384 | * Copies the itimerspec data out from the k_itimer | |
385 | */ | |
386 | static void alarm_timer_get(struct k_itimer *timr, | |
387 | struct itimerspec *cur_setting) | |
388 | { | |
389 | cur_setting->it_interval = | |
390 | ktime_to_timespec(timr->it.alarmtimer.period); | |
391 | cur_setting->it_value = | |
392 | ktime_to_timespec(timr->it.alarmtimer.node.expires); | |
393 | return; | |
394 | } | |
395 | ||
396 | /** | |
397 | * alarm_timer_del - posix timer_del interface | |
398 | * @timr: k_itimer pointer to be deleted | |
399 | * | |
400 | * Cancels any programmed alarms for the given timer. | |
401 | */ | |
402 | static int alarm_timer_del(struct k_itimer *timr) | |
403 | { | |
404 | alarm_cancel(&timr->it.alarmtimer); | |
405 | return 0; | |
406 | } | |
407 | ||
408 | /** | |
409 | * alarm_timer_set - posix timer_set interface | |
410 | * @timr: k_itimer pointer to be deleted | |
411 | * @flags: timer flags | |
412 | * @new_setting: itimerspec to be used | |
413 | * @old_setting: itimerspec being replaced | |
414 | * | |
415 | * Sets the timer to new_setting, and starts the timer. | |
416 | */ | |
417 | static int alarm_timer_set(struct k_itimer *timr, int flags, | |
418 | struct itimerspec *new_setting, | |
419 | struct itimerspec *old_setting) | |
420 | { | |
421 | /* Save old values */ | |
422 | old_setting->it_interval = | |
423 | ktime_to_timespec(timr->it.alarmtimer.period); | |
424 | old_setting->it_value = | |
425 | ktime_to_timespec(timr->it.alarmtimer.node.expires); | |
426 | ||
427 | /* If the timer was already set, cancel it */ | |
428 | alarm_cancel(&timr->it.alarmtimer); | |
429 | ||
430 | /* start the timer */ | |
431 | alarm_start(&timr->it.alarmtimer, | |
432 | timespec_to_ktime(new_setting->it_value), | |
433 | timespec_to_ktime(new_setting->it_interval)); | |
434 | return 0; | |
435 | } | |
436 | ||
437 | /** | |
438 | * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep | |
439 | * @alarm: ptr to alarm that fired | |
440 | * | |
441 | * Wakes up the task that set the alarmtimer | |
442 | */ | |
443 | static void alarmtimer_nsleep_wakeup(struct alarm *alarm) | |
444 | { | |
445 | struct task_struct *task = (struct task_struct *)alarm->data; | |
446 | ||
447 | alarm->data = NULL; | |
448 | if (task) | |
449 | wake_up_process(task); | |
450 | } | |
451 | ||
452 | /** | |
453 | * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation | |
454 | * @alarm: ptr to alarmtimer | |
455 | * @absexp: absolute expiration time | |
456 | * | |
457 | * Sets the alarm timer and sleeps until it is fired or interrupted. | |
458 | */ | |
459 | static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp) | |
460 | { | |
461 | alarm->data = (void *)current; | |
462 | do { | |
463 | set_current_state(TASK_INTERRUPTIBLE); | |
464 | alarm_start(alarm, absexp, ktime_set(0, 0)); | |
465 | if (likely(alarm->data)) | |
466 | schedule(); | |
467 | ||
468 | alarm_cancel(alarm); | |
469 | } while (alarm->data && !signal_pending(current)); | |
470 | ||
471 | __set_current_state(TASK_RUNNING); | |
472 | ||
473 | return (alarm->data == NULL); | |
474 | } | |
475 | ||
476 | ||
477 | /** | |
478 | * update_rmtp - Update remaining timespec value | |
479 | * @exp: expiration time | |
480 | * @type: timer type | |
481 | * @rmtp: user pointer to remaining timepsec value | |
482 | * | |
483 | * Helper function that fills in rmtp value with time between | |
484 | * now and the exp value | |
485 | */ | |
486 | static int update_rmtp(ktime_t exp, enum alarmtimer_type type, | |
487 | struct timespec __user *rmtp) | |
488 | { | |
489 | struct timespec rmt; | |
490 | ktime_t rem; | |
491 | ||
492 | rem = ktime_sub(exp, alarm_bases[type].gettime()); | |
493 | ||
494 | if (rem.tv64 <= 0) | |
495 | return 0; | |
496 | rmt = ktime_to_timespec(rem); | |
497 | ||
498 | if (copy_to_user(rmtp, &rmt, sizeof(*rmtp))) | |
499 | return -EFAULT; | |
500 | ||
501 | return 1; | |
502 | ||
503 | } | |
504 | ||
505 | /** | |
506 | * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep | |
507 | * @restart: ptr to restart block | |
508 | * | |
509 | * Handles restarted clock_nanosleep calls | |
510 | */ | |
511 | static long __sched alarm_timer_nsleep_restart(struct restart_block *restart) | |
512 | { | |
513 | enum alarmtimer_type type = restart->nanosleep.index; | |
514 | ktime_t exp; | |
515 | struct timespec __user *rmtp; | |
516 | struct alarm alarm; | |
517 | int ret = 0; | |
518 | ||
519 | exp.tv64 = restart->nanosleep.expires; | |
520 | alarm_init(&alarm, type, alarmtimer_nsleep_wakeup); | |
521 | ||
522 | if (alarmtimer_do_nsleep(&alarm, exp)) | |
523 | goto out; | |
524 | ||
525 | if (freezing(current)) | |
526 | alarmtimer_freezerset(exp, type); | |
527 | ||
528 | rmtp = restart->nanosleep.rmtp; | |
529 | if (rmtp) { | |
530 | ret = update_rmtp(exp, type, rmtp); | |
531 | if (ret <= 0) | |
532 | goto out; | |
533 | } | |
534 | ||
535 | ||
536 | /* The other values in restart are already filled in */ | |
537 | ret = -ERESTART_RESTARTBLOCK; | |
538 | out: | |
539 | return ret; | |
540 | } | |
541 | ||
542 | /** | |
543 | * alarm_timer_nsleep - alarmtimer nanosleep | |
544 | * @which_clock: clockid | |
545 | * @flags: determins abstime or relative | |
546 | * @tsreq: requested sleep time (abs or rel) | |
547 | * @rmtp: remaining sleep time saved | |
548 | * | |
549 | * Handles clock_nanosleep calls against _ALARM clockids | |
550 | */ | |
551 | static int alarm_timer_nsleep(const clockid_t which_clock, int flags, | |
552 | struct timespec *tsreq, struct timespec __user *rmtp) | |
553 | { | |
554 | enum alarmtimer_type type = clock2alarm(which_clock); | |
555 | struct alarm alarm; | |
556 | ktime_t exp; | |
557 | int ret = 0; | |
558 | struct restart_block *restart; | |
559 | ||
560 | if (!capable(CAP_WAKE_ALARM)) | |
561 | return -EPERM; | |
562 | ||
563 | alarm_init(&alarm, type, alarmtimer_nsleep_wakeup); | |
564 | ||
565 | exp = timespec_to_ktime(*tsreq); | |
566 | /* Convert (if necessary) to absolute time */ | |
567 | if (flags != TIMER_ABSTIME) { | |
568 | ktime_t now = alarm_bases[type].gettime(); | |
569 | exp = ktime_add(now, exp); | |
570 | } | |
571 | ||
572 | if (alarmtimer_do_nsleep(&alarm, exp)) | |
573 | goto out; | |
574 | ||
575 | if (freezing(current)) | |
576 | alarmtimer_freezerset(exp, type); | |
577 | ||
578 | /* abs timers don't set remaining time or restart */ | |
579 | if (flags == TIMER_ABSTIME) { | |
580 | ret = -ERESTARTNOHAND; | |
581 | goto out; | |
582 | } | |
583 | ||
584 | if (rmtp) { | |
585 | ret = update_rmtp(exp, type, rmtp); | |
586 | if (ret <= 0) | |
587 | goto out; | |
588 | } | |
589 | ||
590 | restart = ¤t_thread_info()->restart_block; | |
591 | restart->fn = alarm_timer_nsleep_restart; | |
592 | restart->nanosleep.index = type; | |
593 | restart->nanosleep.expires = exp.tv64; | |
594 | restart->nanosleep.rmtp = rmtp; | |
595 | ret = -ERESTART_RESTARTBLOCK; | |
596 | ||
597 | out: | |
598 | return ret; | |
599 | } | |
ff3ead96 JS |
600 | |
601 | /************************************************************************** | |
602 | * alarmtimer initialization code | |
603 | */ | |
604 | ||
605 | /* Suspend hook structures */ | |
606 | static const struct dev_pm_ops alarmtimer_pm_ops = { | |
607 | .suspend = alarmtimer_suspend, | |
608 | }; | |
609 | ||
610 | static struct platform_driver alarmtimer_driver = { | |
611 | .driver = { | |
612 | .name = "alarmtimer", | |
613 | .pm = &alarmtimer_pm_ops, | |
614 | } | |
615 | }; | |
616 | ||
617 | /** | |
618 | * alarmtimer_init - Initialize alarm timer code | |
619 | * | |
620 | * This function initializes the alarm bases and registers | |
621 | * the posix clock ids. | |
622 | */ | |
623 | static int __init alarmtimer_init(void) | |
624 | { | |
625 | int error = 0; | |
626 | int i; | |
9a7adcf5 JS |
627 | struct k_clock alarm_clock = { |
628 | .clock_getres = alarm_clock_getres, | |
629 | .clock_get = alarm_clock_get, | |
630 | .timer_create = alarm_timer_create, | |
631 | .timer_set = alarm_timer_set, | |
632 | .timer_del = alarm_timer_del, | |
633 | .timer_get = alarm_timer_get, | |
634 | .nsleep = alarm_timer_nsleep, | |
635 | }; | |
636 | ||
637 | posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock); | |
638 | posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock); | |
ff3ead96 JS |
639 | |
640 | /* Initialize alarm bases */ | |
641 | alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME; | |
642 | alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real; | |
643 | alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME; | |
644 | alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime; | |
645 | for (i = 0; i < ALARM_NUMTYPE; i++) { | |
646 | timerqueue_init_head(&alarm_bases[i].timerqueue); | |
647 | spin_lock_init(&alarm_bases[i].lock); | |
648 | hrtimer_init(&alarm_bases[i].timer, | |
649 | alarm_bases[i].base_clockid, | |
650 | HRTIMER_MODE_ABS); | |
651 | alarm_bases[i].timer.function = alarmtimer_fired; | |
652 | INIT_WORK(&alarm_bases[i].irqwork, alarmtimer_do_work); | |
653 | } | |
654 | error = platform_driver_register(&alarmtimer_driver); | |
655 | platform_device_register_simple("alarmtimer", -1, NULL, 0); | |
656 | ||
657 | return error; | |
658 | } | |
659 | device_initcall(alarmtimer_init); | |
660 | ||
661 | /** | |
662 | * has_wakealarm - check rtc device has wakealarm ability | |
663 | * @dev: current device | |
664 | * @name_ptr: name to be returned | |
665 | * | |
666 | * This helper function checks to see if the rtc device can wake | |
667 | * from suspend. | |
668 | */ | |
669 | static int __init has_wakealarm(struct device *dev, void *name_ptr) | |
670 | { | |
671 | struct rtc_device *candidate = to_rtc_device(dev); | |
672 | ||
673 | if (!candidate->ops->set_alarm) | |
674 | return 0; | |
675 | if (!device_may_wakeup(candidate->dev.parent)) | |
676 | return 0; | |
677 | ||
678 | *(const char **)name_ptr = dev_name(dev); | |
679 | return 1; | |
680 | } | |
681 | ||
682 | /** | |
683 | * alarmtimer_init_late - Late initializing of alarmtimer code | |
684 | * | |
685 | * This function locates a rtc device to use for wakealarms. | |
686 | * Run as late_initcall to make sure rtc devices have been | |
687 | * registered. | |
688 | */ | |
689 | static int __init alarmtimer_init_late(void) | |
690 | { | |
691 | char *str; | |
692 | ||
693 | /* Find an rtc device and init the rtc_timer */ | |
694 | class_find_device(rtc_class, NULL, &str, has_wakealarm); | |
695 | if (str) | |
696 | rtcdev = rtc_class_open(str); | |
697 | if (!rtcdev) { | |
698 | printk(KERN_WARNING "No RTC device found, ALARM timers will" | |
699 | " not wake from suspend"); | |
700 | } | |
701 | rtc_timer_init(&rtctimer, NULL, NULL); | |
702 | ||
703 | return 0; | |
704 | } | |
705 | late_initcall(alarmtimer_init_late); |