1 /* drivers/rtc/alarm-dev.c
3 * Copyright (C) 2007-2009 Google, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/time.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/miscdevice.h>
21 #include <linux/platform_device.h>
22 #include <linux/sched.h>
23 #include <linux/spinlock.h>
24 #include <linux/uaccess.h>
25 #include <linux/alarmtimer.h>
26 #include "android_alarm.h"
28 #define ANDROID_ALARM_PRINT_INFO (1U << 0)
29 #define ANDROID_ALARM_PRINT_IO (1U << 1)
30 #define ANDROID_ALARM_PRINT_INT (1U << 2)
32 static int debug_mask
= ANDROID_ALARM_PRINT_INFO
;
33 module_param_named(debug_mask
, debug_mask
, int, S_IRUGO
| S_IWUSR
| S_IWGRP
);
35 #define alarm_dbg(debug_level_mask, fmt, ...) \
37 if (debug_mask & ANDROID_ALARM_PRINT_##debug_level_mask) \
38 pr_info(fmt, ##__VA_ARGS__); \
41 #define ANDROID_ALARM_WAKEUP_MASK ( \
42 ANDROID_ALARM_RTC_WAKEUP_MASK | \
43 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK)
45 static int alarm_opened
;
46 static DEFINE_SPINLOCK(alarm_slock
);
47 static struct wakeup_source alarm_wake_lock
;
48 static DECLARE_WAIT_QUEUE_HEAD(alarm_wait_queue
);
49 static uint32_t alarm_pending
;
50 static uint32_t alarm_enabled
;
51 static uint32_t wait_pending
;
58 enum android_alarm_type type
;
61 static struct devalarm alarms
[ANDROID_ALARM_TYPE_COUNT
];
64 static int is_wakeup(enum android_alarm_type type
)
66 return (type
== ANDROID_ALARM_RTC_WAKEUP
||
67 type
== ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP
);
71 static void devalarm_start(struct devalarm
*alrm
, ktime_t exp
)
73 if (is_wakeup(alrm
->type
))
74 alarm_start(&alrm
->u
.alrm
, exp
);
76 hrtimer_start(&alrm
->u
.hrt
, exp
, HRTIMER_MODE_ABS
);
80 static int devalarm_try_to_cancel(struct devalarm
*alrm
)
82 if (is_wakeup(alrm
->type
))
83 return alarm_try_to_cancel(&alrm
->u
.alrm
);
84 return hrtimer_try_to_cancel(&alrm
->u
.hrt
);
87 static void devalarm_cancel(struct devalarm
*alrm
)
89 if (is_wakeup(alrm
->type
))
90 alarm_cancel(&alrm
->u
.alrm
);
92 hrtimer_cancel(&alrm
->u
.hrt
);
95 static void alarm_clear(enum android_alarm_type alarm_type
)
97 uint32_t alarm_type_mask
= 1U << alarm_type
;
100 spin_lock_irqsave(&alarm_slock
, flags
);
101 alarm_dbg(IO
, "alarm %d clear\n", alarm_type
);
102 devalarm_try_to_cancel(&alarms
[alarm_type
]);
104 alarm_pending
&= ~alarm_type_mask
;
105 if (!alarm_pending
&& !wait_pending
)
106 __pm_relax(&alarm_wake_lock
);
108 alarm_enabled
&= ~alarm_type_mask
;
109 spin_unlock_irqrestore(&alarm_slock
, flags
);
113 static void alarm_set(enum android_alarm_type alarm_type
,
116 uint32_t alarm_type_mask
= 1U << alarm_type
;
119 spin_lock_irqsave(&alarm_slock
, flags
);
120 alarm_dbg(IO
, "alarm %d set %ld.%09ld\n",
121 alarm_type
, ts
->tv_sec
, ts
->tv_nsec
);
122 alarm_enabled
|= alarm_type_mask
;
123 devalarm_start(&alarms
[alarm_type
], timespec_to_ktime(*ts
));
124 spin_unlock_irqrestore(&alarm_slock
, flags
);
127 static int alarm_wait(void)
132 spin_lock_irqsave(&alarm_slock
, flags
);
133 alarm_dbg(IO
, "alarm wait\n");
134 if (!alarm_pending
&& wait_pending
) {
135 __pm_relax(&alarm_wake_lock
);
138 spin_unlock_irqrestore(&alarm_slock
, flags
);
140 rv
= wait_event_interruptible(alarm_wait_queue
, alarm_pending
);
144 spin_lock_irqsave(&alarm_slock
, flags
);
148 spin_unlock_irqrestore(&alarm_slock
, flags
);
153 static int alarm_set_rtc(struct timespec
*ts
)
155 struct rtc_time new_rtc_tm
;
156 struct rtc_device
*rtc_dev
;
160 rtc_time_to_tm(ts
->tv_sec
, &new_rtc_tm
);
161 rtc_dev
= alarmtimer_get_rtcdev();
162 rv
= do_settimeofday(ts
);
166 rv
= rtc_set_time(rtc_dev
, &new_rtc_tm
);
168 spin_lock_irqsave(&alarm_slock
, flags
);
169 alarm_pending
|= ANDROID_ALARM_TIME_CHANGE_MASK
;
170 wake_up(&alarm_wait_queue
);
171 spin_unlock_irqrestore(&alarm_slock
, flags
);
176 static int alarm_get_time(enum android_alarm_type alarm_type
,
181 switch (alarm_type
) {
182 case ANDROID_ALARM_RTC_WAKEUP
:
183 case ANDROID_ALARM_RTC
:
186 case ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP
:
187 case ANDROID_ALARM_ELAPSED_REALTIME
:
188 get_monotonic_boottime(ts
);
190 case ANDROID_ALARM_SYSTEMTIME
:
199 static long alarm_do_ioctl(struct file
*file
, unsigned int cmd
,
204 enum android_alarm_type alarm_type
= ANDROID_ALARM_IOCTL_TO_TYPE(cmd
);
206 if (alarm_type
>= ANDROID_ALARM_TYPE_COUNT
)
209 if (ANDROID_ALARM_BASE_CMD(cmd
) != ANDROID_ALARM_GET_TIME(0)) {
210 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
)
212 if (file
->private_data
== NULL
&&
213 cmd
!= ANDROID_ALARM_SET_RTC
) {
214 spin_lock_irqsave(&alarm_slock
, flags
);
216 spin_unlock_irqrestore(&alarm_slock
, flags
);
220 file
->private_data
= (void *)1;
221 spin_unlock_irqrestore(&alarm_slock
, flags
);
225 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
226 case ANDROID_ALARM_CLEAR(0):
227 alarm_clear(alarm_type
);
229 case ANDROID_ALARM_SET(0):
230 alarm_set(alarm_type
, ts
);
232 case ANDROID_ALARM_SET_AND_WAIT(0):
233 alarm_set(alarm_type
, ts
);
235 case ANDROID_ALARM_WAIT
:
238 case ANDROID_ALARM_SET_RTC
:
239 rv
= alarm_set_rtc(ts
);
241 case ANDROID_ALARM_GET_TIME(0):
242 rv
= alarm_get_time(alarm_type
, ts
);
251 static long alarm_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
257 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
258 case ANDROID_ALARM_SET_AND_WAIT(0):
259 case ANDROID_ALARM_SET(0):
260 case ANDROID_ALARM_SET_RTC
:
261 if (copy_from_user(&ts
, (void __user
*)arg
, sizeof(ts
)))
266 rv
= alarm_do_ioctl(file
, cmd
, &ts
);
270 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
271 case ANDROID_ALARM_GET_TIME(0):
272 if (copy_to_user((void __user
*)arg
, &ts
, sizeof(ts
)))
280 static long alarm_compat_ioctl(struct file
*file
, unsigned int cmd
,
287 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
288 case ANDROID_ALARM_SET_AND_WAIT_COMPAT(0):
289 case ANDROID_ALARM_SET_COMPAT(0):
290 case ANDROID_ALARM_SET_RTC_COMPAT
:
291 if (compat_get_timespec(&ts
, (void __user
*)arg
))
294 case ANDROID_ALARM_GET_TIME_COMPAT(0):
295 cmd
= ANDROID_ALARM_COMPAT_TO_NORM(cmd
);
299 rv
= alarm_do_ioctl(file
, cmd
, &ts
);
303 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
304 case ANDROID_ALARM_GET_TIME(0): /* NOTE: we modified cmd above */
305 if (compat_put_timespec(&ts
, (void __user
*)arg
))
314 static int alarm_open(struct inode
*inode
, struct file
*file
)
316 file
->private_data
= NULL
;
320 static int alarm_release(struct inode
*inode
, struct file
*file
)
325 spin_lock_irqsave(&alarm_slock
, flags
);
326 if (file
->private_data
) {
327 for (i
= 0; i
< ANDROID_ALARM_TYPE_COUNT
; i
++) {
328 uint32_t alarm_type_mask
= 1U << i
;
329 if (alarm_enabled
& alarm_type_mask
) {
331 "%s: clear alarm, pending %d\n",
333 !!(alarm_pending
& alarm_type_mask
));
334 alarm_enabled
&= ~alarm_type_mask
;
336 spin_unlock_irqrestore(&alarm_slock
, flags
);
337 devalarm_cancel(&alarms
[i
]);
338 spin_lock_irqsave(&alarm_slock
, flags
);
340 if (alarm_pending
| wait_pending
) {
342 alarm_dbg(INFO
, "%s: clear pending alarms %x\n",
343 __func__
, alarm_pending
);
344 __pm_relax(&alarm_wake_lock
);
350 spin_unlock_irqrestore(&alarm_slock
, flags
);
354 static void devalarm_triggered(struct devalarm
*alarm
)
357 uint32_t alarm_type_mask
= 1U << alarm
->type
;
359 alarm_dbg(INT
, "%s: type %d\n", __func__
, alarm
->type
);
360 spin_lock_irqsave(&alarm_slock
, flags
);
361 if (alarm_enabled
& alarm_type_mask
) {
362 __pm_wakeup_event(&alarm_wake_lock
, 5000); /* 5secs */
363 alarm_enabled
&= ~alarm_type_mask
;
364 alarm_pending
|= alarm_type_mask
;
365 wake_up(&alarm_wait_queue
);
367 spin_unlock_irqrestore(&alarm_slock
, flags
);
371 static enum hrtimer_restart
devalarm_hrthandler(struct hrtimer
*hrt
)
373 struct devalarm
*devalrm
= container_of(hrt
, struct devalarm
, u
.hrt
);
375 devalarm_triggered(devalrm
);
376 return HRTIMER_NORESTART
;
379 static enum alarmtimer_restart
devalarm_alarmhandler(struct alarm
*alrm
,
382 struct devalarm
*devalrm
= container_of(alrm
, struct devalarm
, u
.alrm
);
384 devalarm_triggered(devalrm
);
385 return ALARMTIMER_NORESTART
;
389 static const struct file_operations alarm_fops
= {
390 .owner
= THIS_MODULE
,
391 .unlocked_ioctl
= alarm_ioctl
,
393 .release
= alarm_release
,
395 .compat_ioctl
= alarm_compat_ioctl
,
399 static struct miscdevice alarm_device
= {
400 .minor
= MISC_DYNAMIC_MINOR
,
405 static int __init
alarm_dev_init(void)
410 err
= misc_register(&alarm_device
);
414 alarm_init(&alarms
[ANDROID_ALARM_RTC_WAKEUP
].u
.alrm
,
415 ALARM_REALTIME
, devalarm_alarmhandler
);
416 hrtimer_init(&alarms
[ANDROID_ALARM_RTC
].u
.hrt
,
417 CLOCK_REALTIME
, HRTIMER_MODE_ABS
);
418 alarm_init(&alarms
[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP
].u
.alrm
,
419 ALARM_BOOTTIME
, devalarm_alarmhandler
);
420 hrtimer_init(&alarms
[ANDROID_ALARM_ELAPSED_REALTIME
].u
.hrt
,
421 CLOCK_BOOTTIME
, HRTIMER_MODE_ABS
);
422 hrtimer_init(&alarms
[ANDROID_ALARM_SYSTEMTIME
].u
.hrt
,
423 CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
425 for (i
= 0; i
< ANDROID_ALARM_TYPE_COUNT
; i
++) {
428 alarms
[i
].u
.hrt
.function
= devalarm_hrthandler
;
431 wakeup_source_init(&alarm_wake_lock
, "alarm");
435 static void __exit
alarm_dev_exit(void)
437 misc_deregister(&alarm_device
);
438 wakeup_source_trash(&alarm_wake_lock
);
441 module_init(alarm_dev_init
);
442 module_exit(alarm_dev_exit
);