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 * is_wakeup() - Checks to see if this alarm can wake the device
65 * @type: The type of alarm being checked
67 * Return: 1 if this is a wakeup alarm, otherwise 0
69 static int is_wakeup(enum android_alarm_type type
)
71 return type
== ANDROID_ALARM_RTC_WAKEUP
||
72 type
== ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP
;
75 static void devalarm_start(struct devalarm
*alrm
, ktime_t exp
)
77 if (is_wakeup(alrm
->type
))
78 alarm_start(&alrm
->u
.alrm
, exp
);
80 hrtimer_start(&alrm
->u
.hrt
, exp
, HRTIMER_MODE_ABS
);
83 static int devalarm_try_to_cancel(struct devalarm
*alrm
)
85 if (is_wakeup(alrm
->type
))
86 return alarm_try_to_cancel(&alrm
->u
.alrm
);
87 return hrtimer_try_to_cancel(&alrm
->u
.hrt
);
90 static void devalarm_cancel(struct devalarm
*alrm
)
92 if (is_wakeup(alrm
->type
))
93 alarm_cancel(&alrm
->u
.alrm
);
95 hrtimer_cancel(&alrm
->u
.hrt
);
98 static void alarm_clear(enum android_alarm_type alarm_type
)
100 uint32_t alarm_type_mask
= 1U << alarm_type
;
103 spin_lock_irqsave(&alarm_slock
, flags
);
104 alarm_dbg(IO
, "alarm %d clear\n", alarm_type
);
105 devalarm_try_to_cancel(&alarms
[alarm_type
]);
107 alarm_pending
&= ~alarm_type_mask
;
108 if (!alarm_pending
&& !wait_pending
)
109 __pm_relax(&alarm_wake_lock
);
111 alarm_enabled
&= ~alarm_type_mask
;
112 spin_unlock_irqrestore(&alarm_slock
, flags
);
115 static void alarm_set(enum android_alarm_type alarm_type
,
118 uint32_t alarm_type_mask
= 1U << alarm_type
;
121 spin_lock_irqsave(&alarm_slock
, flags
);
122 alarm_dbg(IO
, "alarm %d set %ld.%09ld\n",
123 alarm_type
, ts
->tv_sec
, ts
->tv_nsec
);
124 alarm_enabled
|= alarm_type_mask
;
125 devalarm_start(&alarms
[alarm_type
], timespec_to_ktime(*ts
));
126 spin_unlock_irqrestore(&alarm_slock
, flags
);
129 static int alarm_wait(void)
134 spin_lock_irqsave(&alarm_slock
, flags
);
135 alarm_dbg(IO
, "alarm wait\n");
136 if (!alarm_pending
&& wait_pending
) {
137 __pm_relax(&alarm_wake_lock
);
140 spin_unlock_irqrestore(&alarm_slock
, flags
);
142 rv
= wait_event_interruptible(alarm_wait_queue
, alarm_pending
);
146 spin_lock_irqsave(&alarm_slock
, flags
);
150 spin_unlock_irqrestore(&alarm_slock
, flags
);
155 static int alarm_set_rtc(struct timespec
*ts
)
157 struct rtc_time new_rtc_tm
;
158 struct rtc_device
*rtc_dev
;
162 rtc_time_to_tm(ts
->tv_sec
, &new_rtc_tm
);
163 rtc_dev
= alarmtimer_get_rtcdev();
164 rv
= do_settimeofday(ts
);
168 rv
= rtc_set_time(rtc_dev
, &new_rtc_tm
);
170 spin_lock_irqsave(&alarm_slock
, flags
);
171 alarm_pending
|= ANDROID_ALARM_TIME_CHANGE_MASK
;
172 wake_up(&alarm_wait_queue
);
173 spin_unlock_irqrestore(&alarm_slock
, flags
);
178 static int alarm_get_time(enum android_alarm_type alarm_type
,
183 switch (alarm_type
) {
184 case ANDROID_ALARM_RTC_WAKEUP
:
185 case ANDROID_ALARM_RTC
:
188 case ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP
:
189 case ANDROID_ALARM_ELAPSED_REALTIME
:
190 get_monotonic_boottime(ts
);
192 case ANDROID_ALARM_SYSTEMTIME
:
201 static long alarm_do_ioctl(struct file
*file
, unsigned int cmd
,
206 enum android_alarm_type alarm_type
= ANDROID_ALARM_IOCTL_TO_TYPE(cmd
);
208 if (alarm_type
>= ANDROID_ALARM_TYPE_COUNT
)
211 if (ANDROID_ALARM_BASE_CMD(cmd
) != ANDROID_ALARM_GET_TIME(0)) {
212 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
)
214 if (file
->private_data
== NULL
&&
215 cmd
!= ANDROID_ALARM_SET_RTC
) {
216 spin_lock_irqsave(&alarm_slock
, flags
);
218 spin_unlock_irqrestore(&alarm_slock
, flags
);
222 file
->private_data
= (void *)1;
223 spin_unlock_irqrestore(&alarm_slock
, flags
);
227 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
228 case ANDROID_ALARM_CLEAR(0):
229 alarm_clear(alarm_type
);
231 case ANDROID_ALARM_SET(0):
232 alarm_set(alarm_type
, ts
);
234 case ANDROID_ALARM_SET_AND_WAIT(0):
235 alarm_set(alarm_type
, ts
);
237 case ANDROID_ALARM_WAIT
:
240 case ANDROID_ALARM_SET_RTC
:
241 rv
= alarm_set_rtc(ts
);
243 case ANDROID_ALARM_GET_TIME(0):
244 rv
= alarm_get_time(alarm_type
, ts
);
253 static long alarm_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
259 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
260 case ANDROID_ALARM_SET_AND_WAIT(0):
261 case ANDROID_ALARM_SET(0):
262 case ANDROID_ALARM_SET_RTC
:
263 if (copy_from_user(&ts
, (void __user
*)arg
, sizeof(ts
)))
268 rv
= alarm_do_ioctl(file
, cmd
, &ts
);
272 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
273 case ANDROID_ALARM_GET_TIME(0):
274 if (copy_to_user((void __user
*)arg
, &ts
, sizeof(ts
)))
283 static long alarm_compat_ioctl(struct file
*file
, unsigned int cmd
,
290 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
291 case ANDROID_ALARM_SET_AND_WAIT_COMPAT(0):
292 case ANDROID_ALARM_SET_COMPAT(0):
293 case ANDROID_ALARM_SET_RTC_COMPAT
:
294 if (compat_get_timespec(&ts
, (void __user
*)arg
))
297 case ANDROID_ALARM_GET_TIME_COMPAT(0):
298 cmd
= ANDROID_ALARM_COMPAT_TO_NORM(cmd
);
302 rv
= alarm_do_ioctl(file
, cmd
, &ts
);
306 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
307 case ANDROID_ALARM_GET_TIME(0): /* NOTE: we modified cmd above */
308 if (compat_put_timespec(&ts
, (void __user
*)arg
))
317 static int alarm_open(struct inode
*inode
, struct file
*file
)
319 file
->private_data
= NULL
;
323 static int alarm_release(struct inode
*inode
, struct file
*file
)
328 spin_lock_irqsave(&alarm_slock
, flags
);
329 if (file
->private_data
) {
330 for (i
= 0; i
< ANDROID_ALARM_TYPE_COUNT
; i
++) {
331 uint32_t alarm_type_mask
= 1U << i
;
332 if (alarm_enabled
& alarm_type_mask
) {
334 "%s: clear alarm, pending %d\n",
336 !!(alarm_pending
& alarm_type_mask
));
337 alarm_enabled
&= ~alarm_type_mask
;
339 spin_unlock_irqrestore(&alarm_slock
, flags
);
340 devalarm_cancel(&alarms
[i
]);
341 spin_lock_irqsave(&alarm_slock
, flags
);
343 if (alarm_pending
| wait_pending
) {
345 alarm_dbg(INFO
, "%s: clear pending alarms %x\n",
346 __func__
, alarm_pending
);
347 __pm_relax(&alarm_wake_lock
);
353 spin_unlock_irqrestore(&alarm_slock
, flags
);
357 static void devalarm_triggered(struct devalarm
*alarm
)
360 uint32_t alarm_type_mask
= 1U << alarm
->type
;
362 alarm_dbg(INT
, "%s: type %d\n", __func__
, alarm
->type
);
363 spin_lock_irqsave(&alarm_slock
, flags
);
364 if (alarm_enabled
& alarm_type_mask
) {
365 __pm_wakeup_event(&alarm_wake_lock
, 5000); /* 5secs */
366 alarm_enabled
&= ~alarm_type_mask
;
367 alarm_pending
|= alarm_type_mask
;
368 wake_up(&alarm_wait_queue
);
370 spin_unlock_irqrestore(&alarm_slock
, flags
);
373 static enum hrtimer_restart
devalarm_hrthandler(struct hrtimer
*hrt
)
375 struct devalarm
*devalrm
= container_of(hrt
, struct devalarm
, u
.hrt
);
377 devalarm_triggered(devalrm
);
378 return HRTIMER_NORESTART
;
381 static enum alarmtimer_restart
devalarm_alarmhandler(struct alarm
*alrm
,
384 struct devalarm
*devalrm
= container_of(alrm
, struct devalarm
, u
.alrm
);
386 devalarm_triggered(devalrm
);
387 return ALARMTIMER_NORESTART
;
391 static const struct file_operations alarm_fops
= {
392 .owner
= THIS_MODULE
,
393 .unlocked_ioctl
= alarm_ioctl
,
395 .release
= alarm_release
,
397 .compat_ioctl
= alarm_compat_ioctl
,
401 static struct miscdevice alarm_device
= {
402 .minor
= MISC_DYNAMIC_MINOR
,
407 static int __init
alarm_dev_init(void)
412 err
= misc_register(&alarm_device
);
416 alarm_init(&alarms
[ANDROID_ALARM_RTC_WAKEUP
].u
.alrm
,
417 ALARM_REALTIME
, devalarm_alarmhandler
);
418 hrtimer_init(&alarms
[ANDROID_ALARM_RTC
].u
.hrt
,
419 CLOCK_REALTIME
, HRTIMER_MODE_ABS
);
420 alarm_init(&alarms
[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP
].u
.alrm
,
421 ALARM_BOOTTIME
, devalarm_alarmhandler
);
422 hrtimer_init(&alarms
[ANDROID_ALARM_ELAPSED_REALTIME
].u
.hrt
,
423 CLOCK_BOOTTIME
, HRTIMER_MODE_ABS
);
424 hrtimer_init(&alarms
[ANDROID_ALARM_SYSTEMTIME
].u
.hrt
,
425 CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
427 for (i
= 0; i
< ANDROID_ALARM_TYPE_COUNT
; i
++) {
430 alarms
[i
].u
.hrt
.function
= devalarm_hrthandler
;
433 wakeup_source_init(&alarm_wake_lock
, "alarm");
437 static void __exit
alarm_dev_exit(void)
439 misc_deregister(&alarm_device
);
440 wakeup_source_trash(&alarm_wake_lock
);
443 module_init(alarm_dev_init
);
444 module_exit(alarm_dev_exit
);