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 return (type
== ANDROID_ALARM_RTC_WAKEUP
||
71 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
);
84 static int devalarm_try_to_cancel(struct devalarm
*alrm
)
86 if (is_wakeup(alrm
->type
))
87 return alarm_try_to_cancel(&alrm
->u
.alrm
);
88 return hrtimer_try_to_cancel(&alrm
->u
.hrt
);
91 static void devalarm_cancel(struct devalarm
*alrm
)
93 if (is_wakeup(alrm
->type
))
94 alarm_cancel(&alrm
->u
.alrm
);
96 hrtimer_cancel(&alrm
->u
.hrt
);
100 static long alarm_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
104 struct timespec new_alarm_time
;
105 struct timespec new_rtc_time
;
106 struct timespec tmp_time
;
107 struct rtc_time new_rtc_tm
;
108 struct rtc_device
*rtc_dev
;
109 enum android_alarm_type alarm_type
= ANDROID_ALARM_IOCTL_TO_TYPE(cmd
);
110 uint32_t alarm_type_mask
= 1U << alarm_type
;
112 if (alarm_type
>= ANDROID_ALARM_TYPE_COUNT
)
115 if (ANDROID_ALARM_BASE_CMD(cmd
) != ANDROID_ALARM_GET_TIME(0)) {
116 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
)
118 if (file
->private_data
== NULL
&&
119 cmd
!= ANDROID_ALARM_SET_RTC
) {
120 spin_lock_irqsave(&alarm_slock
, flags
);
122 spin_unlock_irqrestore(&alarm_slock
, flags
);
126 file
->private_data
= (void *)1;
127 spin_unlock_irqrestore(&alarm_slock
, flags
);
131 switch (ANDROID_ALARM_BASE_CMD(cmd
)) {
132 case ANDROID_ALARM_CLEAR(0):
133 spin_lock_irqsave(&alarm_slock
, flags
);
134 alarm_dbg(IO
, "alarm %d clear\n", alarm_type
);
135 devalarm_try_to_cancel(&alarms
[alarm_type
]);
137 alarm_pending
&= ~alarm_type_mask
;
138 if (!alarm_pending
&& !wait_pending
)
139 __pm_relax(&alarm_wake_lock
);
141 alarm_enabled
&= ~alarm_type_mask
;
142 spin_unlock_irqrestore(&alarm_slock
, flags
);
145 case ANDROID_ALARM_SET_OLD
:
146 case ANDROID_ALARM_SET_AND_WAIT_OLD
:
147 if (get_user(new_alarm_time
.tv_sec
, (int __user
*)arg
)) {
151 new_alarm_time
.tv_nsec
= 0;
152 goto from_old_alarm_set
;
154 case ANDROID_ALARM_SET_AND_WAIT(0):
155 case ANDROID_ALARM_SET(0):
156 if (copy_from_user(&new_alarm_time
, (void __user
*)arg
,
157 sizeof(new_alarm_time
))) {
162 spin_lock_irqsave(&alarm_slock
, flags
);
163 alarm_dbg(IO
, "alarm %d set %ld.%09ld\n",
165 new_alarm_time
.tv_sec
, new_alarm_time
.tv_nsec
);
166 alarm_enabled
|= alarm_type_mask
;
167 devalarm_start(&alarms
[alarm_type
],
168 timespec_to_ktime(new_alarm_time
));
169 spin_unlock_irqrestore(&alarm_slock
, flags
);
170 if (ANDROID_ALARM_BASE_CMD(cmd
) != ANDROID_ALARM_SET_AND_WAIT(0)
171 && cmd
!= ANDROID_ALARM_SET_AND_WAIT_OLD
)
174 case ANDROID_ALARM_WAIT
:
175 spin_lock_irqsave(&alarm_slock
, flags
);
176 alarm_dbg(IO
, "alarm wait\n");
177 if (!alarm_pending
&& wait_pending
) {
178 __pm_relax(&alarm_wake_lock
);
181 spin_unlock_irqrestore(&alarm_slock
, flags
);
182 rv
= wait_event_interruptible(alarm_wait_queue
, alarm_pending
);
185 spin_lock_irqsave(&alarm_slock
, flags
);
189 spin_unlock_irqrestore(&alarm_slock
, flags
);
191 case ANDROID_ALARM_SET_RTC
:
192 if (copy_from_user(&new_rtc_time
, (void __user
*)arg
,
193 sizeof(new_rtc_time
))) {
197 rtc_time_to_tm(new_rtc_time
.tv_sec
, &new_rtc_tm
);
198 rtc_dev
= alarmtimer_get_rtcdev();
199 rv
= do_settimeofday(&new_rtc_time
);
203 rv
= rtc_set_time(rtc_dev
, &new_rtc_tm
);
204 spin_lock_irqsave(&alarm_slock
, flags
);
205 alarm_pending
|= ANDROID_ALARM_TIME_CHANGE_MASK
;
206 wake_up(&alarm_wait_queue
);
207 spin_unlock_irqrestore(&alarm_slock
, flags
);
211 case ANDROID_ALARM_GET_TIME(0):
212 switch (alarm_type
) {
213 case ANDROID_ALARM_RTC_WAKEUP
:
214 case ANDROID_ALARM_RTC
:
215 getnstimeofday(&tmp_time
);
217 case ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP
:
218 case ANDROID_ALARM_ELAPSED_REALTIME
:
219 get_monotonic_boottime(&tmp_time
);
221 case ANDROID_ALARM_SYSTEMTIME
:
222 ktime_get_ts(&tmp_time
);
228 if (copy_to_user((void __user
*)arg
, &tmp_time
,
242 static int alarm_open(struct inode
*inode
, struct file
*file
)
244 file
->private_data
= NULL
;
248 static int alarm_release(struct inode
*inode
, struct file
*file
)
253 spin_lock_irqsave(&alarm_slock
, flags
);
254 if (file
->private_data
) {
255 for (i
= 0; i
< ANDROID_ALARM_TYPE_COUNT
; i
++) {
256 uint32_t alarm_type_mask
= 1U << i
;
257 if (alarm_enabled
& alarm_type_mask
) {
259 "%s: clear alarm, pending %d\n",
261 !!(alarm_pending
& alarm_type_mask
));
262 alarm_enabled
&= ~alarm_type_mask
;
264 spin_unlock_irqrestore(&alarm_slock
, flags
);
265 devalarm_cancel(&alarms
[i
]);
266 spin_lock_irqsave(&alarm_slock
, flags
);
268 if (alarm_pending
| wait_pending
) {
270 alarm_dbg(INFO
, "%s: clear pending alarms %x\n",
271 __func__
, alarm_pending
);
272 __pm_relax(&alarm_wake_lock
);
278 spin_unlock_irqrestore(&alarm_slock
, flags
);
282 static void devalarm_triggered(struct devalarm
*alarm
)
285 uint32_t alarm_type_mask
= 1U << alarm
->type
;
287 alarm_dbg(INT
, "%s: type %d\n", __func__
, alarm
->type
);
288 spin_lock_irqsave(&alarm_slock
, flags
);
289 if (alarm_enabled
& alarm_type_mask
) {
290 __pm_wakeup_event(&alarm_wake_lock
, 5000); /* 5secs */
291 alarm_enabled
&= ~alarm_type_mask
;
292 alarm_pending
|= alarm_type_mask
;
293 wake_up(&alarm_wait_queue
);
295 spin_unlock_irqrestore(&alarm_slock
, flags
);
299 static enum hrtimer_restart
devalarm_hrthandler(struct hrtimer
*hrt
)
301 struct devalarm
*devalrm
= container_of(hrt
, struct devalarm
, u
.hrt
);
303 devalarm_triggered(devalrm
);
304 return HRTIMER_NORESTART
;
307 static enum alarmtimer_restart
devalarm_alarmhandler(struct alarm
*alrm
,
310 struct devalarm
*devalrm
= container_of(alrm
, struct devalarm
, u
.alrm
);
312 devalarm_triggered(devalrm
);
313 return ALARMTIMER_NORESTART
;
317 static const struct file_operations alarm_fops
= {
318 .owner
= THIS_MODULE
,
319 .unlocked_ioctl
= alarm_ioctl
,
321 .release
= alarm_release
,
324 static struct miscdevice alarm_device
= {
325 .minor
= MISC_DYNAMIC_MINOR
,
330 static int __init
alarm_dev_init(void)
335 err
= misc_register(&alarm_device
);
339 alarm_init(&alarms
[ANDROID_ALARM_RTC_WAKEUP
].u
.alrm
,
340 ALARM_REALTIME
, devalarm_alarmhandler
);
341 hrtimer_init(&alarms
[ANDROID_ALARM_RTC
].u
.hrt
,
342 CLOCK_REALTIME
, HRTIMER_MODE_ABS
);
343 alarm_init(&alarms
[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP
].u
.alrm
,
344 ALARM_BOOTTIME
, devalarm_alarmhandler
);
345 hrtimer_init(&alarms
[ANDROID_ALARM_ELAPSED_REALTIME
].u
.hrt
,
346 CLOCK_BOOTTIME
, HRTIMER_MODE_ABS
);
347 hrtimer_init(&alarms
[ANDROID_ALARM_SYSTEMTIME
].u
.hrt
,
348 CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
350 for (i
= 0; i
< ANDROID_ALARM_TYPE_COUNT
; i
++) {
353 alarms
[i
].u
.hrt
.function
= devalarm_hrthandler
;
356 wakeup_source_init(&alarm_wake_lock
, "alarm");
360 static void __exit
alarm_dev_exit(void)
362 misc_deregister(&alarm_device
);
363 wakeup_source_trash(&alarm_wake_lock
);
366 module_init(alarm_dev_init
);
367 module_exit(alarm_dev_exit
);