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 /* support old userspace code */
46 #define ANDROID_ALARM_SET_OLD _IOW('a', 2, time_t) /* set alarm */
47 #define ANDROID_ALARM_SET_AND_WAIT_OLD _IOW('a', 3, time_t)
49 static int alarm_opened
;
50 static DEFINE_SPINLOCK(alarm_slock
);
51 static struct wakeup_source alarm_wake_lock
;
52 static DECLARE_WAIT_QUEUE_HEAD(alarm_wait_queue
);
53 static uint32_t alarm_pending
;
54 static uint32_t alarm_enabled
;
55 static uint32_t wait_pending
;
62 enum android_alarm_type type
;
65 static struct devalarm alarms
[ANDROID_ALARM_TYPE_COUNT
];
68 static int is_wakeup(enum android_alarm_type type
)
70 if (type
== ANDROID_ALARM_RTC_WAKEUP
||
71 type
== ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP
)
77 static void devalarm_start(struct devalarm
*alrm
, ktime_t exp
)
79 if (is_wakeup(alrm
->type
))
80 alarm_start(&alrm
->u
.alrm
, exp
);
82 hrtimer_start(&alrm
->u
.hrt
, exp
, HRTIMER_MODE_ABS
);
86 static int devalarm_try_to_cancel(struct devalarm
*alrm
)
89 if (is_wakeup(alrm
->type
))
90 ret
= alarm_try_to_cancel(&alrm
->u
.alrm
);
92 ret
= hrtimer_try_to_cancel(&alrm
->u
.hrt
);
96 static void devalarm_cancel(struct devalarm
*alrm
)
98 if (is_wakeup(alrm
->type
))
99 alarm_cancel(&alrm
->u
.alrm
);
101 hrtimer_cancel(&alrm
->u
.hrt
);
105 static long alarm_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
109 struct timespec new_alarm_time
;
110 struct timespec new_rtc_time
;
111 struct timespec tmp_time
;
112 struct rtc_time new_rtc_tm
;
113 struct rtc_device
*rtc_dev
;
114 enum android_alarm_type alarm_type
= ANDROID_ALARM_IOCTL_TO_TYPE(cmd
);
115 uint32_t alarm_type_mask
= 1U << alarm_type
;
117 if (alarm_type
>= ANDROID_ALARM_TYPE_COUNT
)
120 if (ANDROID_ALARM_BASE_CMD(cmd
) != ANDROID_ALARM_GET_TIME(0)) {
121 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
)
123 if (file
->private_data
== NULL
&&
124 cmd
!= ANDROID_ALARM_SET_RTC
) {
125 spin_lock_irqsave(&alarm_slock
, flags
);
127 spin_unlock_irqrestore(&alarm_slock
, flags
);
131 file
->private_data
= (void *)1;
132 spin_unlock_irqrestore(&alarm_slock
, flags
);
136 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
137 case ANDROID_ALARM_CLEAR(0):
138 spin_lock_irqsave(&alarm_slock
, flags
);
139 alarm_dbg(IO
, "alarm %d clear\n", alarm_type
);
140 devalarm_try_to_cancel(&alarms
[alarm_type
]);
142 alarm_pending
&= ~alarm_type_mask
;
143 if (!alarm_pending
&& !wait_pending
)
144 __pm_relax(&alarm_wake_lock
);
146 alarm_enabled
&= ~alarm_type_mask
;
147 spin_unlock_irqrestore(&alarm_slock
, flags
);
150 case ANDROID_ALARM_SET_OLD
:
151 case ANDROID_ALARM_SET_AND_WAIT_OLD
:
152 if (get_user(new_alarm_time
.tv_sec
, (int __user
*)arg
)) {
156 new_alarm_time
.tv_nsec
= 0;
157 goto from_old_alarm_set
;
159 case ANDROID_ALARM_SET_AND_WAIT(0):
160 case ANDROID_ALARM_SET(0):
161 if (copy_from_user(&new_alarm_time
, (void __user
*)arg
,
162 sizeof(new_alarm_time
))) {
167 spin_lock_irqsave(&alarm_slock
, flags
);
168 alarm_dbg(IO
, "alarm %d set %ld.%09ld\n",
170 new_alarm_time
.tv_sec
, new_alarm_time
.tv_nsec
);
171 alarm_enabled
|= alarm_type_mask
;
172 devalarm_start(&alarms
[alarm_type
],
173 timespec_to_ktime(new_alarm_time
));
174 spin_unlock_irqrestore(&alarm_slock
, flags
);
175 if (ANDROID_ALARM_BASE_CMD(cmd
) != ANDROID_ALARM_SET_AND_WAIT(0)
176 && cmd
!= ANDROID_ALARM_SET_AND_WAIT_OLD
)
179 case ANDROID_ALARM_WAIT
:
180 spin_lock_irqsave(&alarm_slock
, flags
);
181 alarm_dbg(IO
, "alarm wait\n");
182 if (!alarm_pending
&& wait_pending
) {
183 __pm_relax(&alarm_wake_lock
);
186 spin_unlock_irqrestore(&alarm_slock
, flags
);
187 rv
= wait_event_interruptible(alarm_wait_queue
, alarm_pending
);
190 spin_lock_irqsave(&alarm_slock
, flags
);
194 spin_unlock_irqrestore(&alarm_slock
, flags
);
196 case ANDROID_ALARM_SET_RTC
:
197 if (copy_from_user(&new_rtc_time
, (void __user
*)arg
,
198 sizeof(new_rtc_time
))) {
202 rtc_time_to_tm(new_rtc_time
.tv_sec
, &new_rtc_tm
);
203 rtc_dev
= alarmtimer_get_rtcdev();
204 rv
= do_settimeofday(&new_rtc_time
);
208 rv
= rtc_set_time(rtc_dev
, &new_rtc_tm
);
209 spin_lock_irqsave(&alarm_slock
, flags
);
210 alarm_pending
|= ANDROID_ALARM_TIME_CHANGE_MASK
;
211 wake_up(&alarm_wait_queue
);
212 spin_unlock_irqrestore(&alarm_slock
, flags
);
216 case ANDROID_ALARM_GET_TIME(0):
217 switch (alarm_type
) {
218 case ANDROID_ALARM_RTC_WAKEUP
:
219 case ANDROID_ALARM_RTC
:
220 getnstimeofday(&tmp_time
);
222 case ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP
:
223 case ANDROID_ALARM_ELAPSED_REALTIME
:
224 get_monotonic_boottime(&tmp_time
);
226 case ANDROID_ALARM_TYPE_COUNT
:
227 case ANDROID_ALARM_SYSTEMTIME
:
228 ktime_get_ts(&tmp_time
);
231 if (copy_to_user((void __user
*)arg
, &tmp_time
,
245 static int alarm_open(struct inode
*inode
, struct file
*file
)
247 file
->private_data
= NULL
;
251 static int alarm_release(struct inode
*inode
, struct file
*file
)
256 spin_lock_irqsave(&alarm_slock
, flags
);
257 if (file
->private_data
) {
258 for (i
= 0; i
< ANDROID_ALARM_TYPE_COUNT
; i
++) {
259 uint32_t alarm_type_mask
= 1U << i
;
260 if (alarm_enabled
& alarm_type_mask
) {
262 "%s: clear alarm, pending %d\n",
264 !!(alarm_pending
& alarm_type_mask
));
265 alarm_enabled
&= ~alarm_type_mask
;
267 spin_unlock_irqrestore(&alarm_slock
, flags
);
268 devalarm_cancel(&alarms
[i
]);
269 spin_lock_irqsave(&alarm_slock
, flags
);
271 if (alarm_pending
| wait_pending
) {
273 alarm_dbg(INFO
, "%s: clear pending alarms %x\n",
274 __func__
, alarm_pending
);
275 __pm_relax(&alarm_wake_lock
);
281 spin_unlock_irqrestore(&alarm_slock
, flags
);
285 static void devalarm_triggered(struct devalarm
*alarm
)
288 uint32_t alarm_type_mask
= 1U << alarm
->type
;
290 alarm_dbg(INT
, "%s: type %d\n", __func__
, alarm
->type
);
291 spin_lock_irqsave(&alarm_slock
, flags
);
292 if (alarm_enabled
& alarm_type_mask
) {
293 __pm_wakeup_event(&alarm_wake_lock
, 5000); /* 5secs */
294 alarm_enabled
&= ~alarm_type_mask
;
295 alarm_pending
|= alarm_type_mask
;
296 wake_up(&alarm_wait_queue
);
298 spin_unlock_irqrestore(&alarm_slock
, flags
);
302 static enum hrtimer_restart
devalarm_hrthandler(struct hrtimer
*hrt
)
304 struct devalarm
*devalrm
= container_of(hrt
, struct devalarm
, u
.hrt
);
306 devalarm_triggered(devalrm
);
307 return HRTIMER_NORESTART
;
310 static enum alarmtimer_restart
devalarm_alarmhandler(struct alarm
*alrm
,
313 struct devalarm
*devalrm
= container_of(alrm
, struct devalarm
, u
.alrm
);
315 devalarm_triggered(devalrm
);
316 return ALARMTIMER_NORESTART
;
320 static const struct file_operations alarm_fops
= {
321 .owner
= THIS_MODULE
,
322 .unlocked_ioctl
= alarm_ioctl
,
324 .release
= alarm_release
,
327 static struct miscdevice alarm_device
= {
328 .minor
= MISC_DYNAMIC_MINOR
,
333 static int __init
alarm_dev_init(void)
338 err
= misc_register(&alarm_device
);
342 alarm_init(&alarms
[ANDROID_ALARM_RTC_WAKEUP
].u
.alrm
,
343 ALARM_REALTIME
, devalarm_alarmhandler
);
344 hrtimer_init(&alarms
[ANDROID_ALARM_RTC
].u
.hrt
,
345 CLOCK_REALTIME
, HRTIMER_MODE_ABS
);
346 alarm_init(&alarms
[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP
].u
.alrm
,
347 ALARM_BOOTTIME
, devalarm_alarmhandler
);
348 hrtimer_init(&alarms
[ANDROID_ALARM_ELAPSED_REALTIME
].u
.hrt
,
349 CLOCK_BOOTTIME
, HRTIMER_MODE_ABS
);
350 hrtimer_init(&alarms
[ANDROID_ALARM_SYSTEMTIME
].u
.hrt
,
351 CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
353 for (i
= 0; i
< ANDROID_ALARM_TYPE_COUNT
; i
++) {
356 alarms
[i
].u
.hrt
.function
= devalarm_hrthandler
;
359 wakeup_source_init(&alarm_wake_lock
, "alarm");
363 static void __exit
alarm_dev_exit(void)
365 misc_deregister(&alarm_device
);
366 wakeup_source_trash(&alarm_wake_lock
);
369 module_init(alarm_dev_init
);
370 module_exit(alarm_dev_exit
);