blob: 1d60e9784a57771aaafc95548e391dc8feef20aa [file] [log] [blame]
Arve Hjønnevågfa565402009-05-04 14:09:15 -07001/* drivers/rtc/alarm-dev.c
2 *
3 * Copyright (C) 2007-2009 Google, Inc.
4 *
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.
8 *
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.
13 *
14 */
15
Steve Mucklef132c6c2012-06-06 18:30:57 -070016#include <linux/module.h>
Arve Hjønnevågfa565402009-05-04 14:09:15 -070017#include <linux/android_alarm.h>
18#include <linux/device.h>
19#include <linux/miscdevice.h>
20#include <linux/fs.h>
21#include <linux/platform_device.h>
22#include <linux/sched.h>
23#include <linux/spinlock.h>
Arve Hjønnevågfa565402009-05-04 14:09:15 -070024#include <linux/uaccess.h>
25#include <linux/wakelock.h>
26
Steve Mucklef132c6c2012-06-06 18:30:57 -070027#include <asm/mach/time.h>
28
Arve Hjønnevågfa565402009-05-04 14:09:15 -070029#define ANDROID_ALARM_PRINT_INFO (1U << 0)
30#define ANDROID_ALARM_PRINT_IO (1U << 1)
31#define ANDROID_ALARM_PRINT_INT (1U << 2)
32
33static int debug_mask = ANDROID_ALARM_PRINT_INFO;
34module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
35
36#define pr_alarm(debug_level_mask, args...) \
37 do { \
38 if (debug_mask & ANDROID_ALARM_PRINT_##debug_level_mask) { \
39 pr_info(args); \
40 } \
41 } while (0)
42
43#define ANDROID_ALARM_WAKEUP_MASK ( \
44 ANDROID_ALARM_RTC_WAKEUP_MASK | \
45 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK)
46
47/* support old usespace code */
48#define ANDROID_ALARM_SET_OLD _IOW('a', 2, time_t) /* set alarm */
49#define ANDROID_ALARM_SET_AND_WAIT_OLD _IOW('a', 3, time_t)
50
51static int alarm_opened;
52static DEFINE_SPINLOCK(alarm_slock);
53static struct wake_lock alarm_wake_lock;
54static DECLARE_WAIT_QUEUE_HEAD(alarm_wait_queue);
55static uint32_t alarm_pending;
56static uint32_t alarm_enabled;
57static uint32_t wait_pending;
58
59static struct alarm alarms[ANDROID_ALARM_TYPE_COUNT];
60
61static long alarm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
62{
63 int rv = 0;
64 unsigned long flags;
65 struct timespec new_alarm_time;
66 struct timespec new_rtc_time;
67 struct timespec tmp_time;
68 enum android_alarm_type alarm_type = ANDROID_ALARM_IOCTL_TO_TYPE(cmd);
69 uint32_t alarm_type_mask = 1U << alarm_type;
70
71 if (alarm_type >= ANDROID_ALARM_TYPE_COUNT)
72 return -EINVAL;
73
74 if (ANDROID_ALARM_BASE_CMD(cmd) != ANDROID_ALARM_GET_TIME(0)) {
75 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
76 return -EPERM;
77 if (file->private_data == NULL &&
78 cmd != ANDROID_ALARM_SET_RTC) {
79 spin_lock_irqsave(&alarm_slock, flags);
80 if (alarm_opened) {
81 spin_unlock_irqrestore(&alarm_slock, flags);
82 return -EBUSY;
83 }
84 alarm_opened = 1;
85 file->private_data = (void *)1;
86 spin_unlock_irqrestore(&alarm_slock, flags);
87 }
88 }
89
90 switch (ANDROID_ALARM_BASE_CMD(cmd)) {
91 case ANDROID_ALARM_CLEAR(0):
92 spin_lock_irqsave(&alarm_slock, flags);
93 pr_alarm(IO, "alarm %d clear\n", alarm_type);
94 alarm_try_to_cancel(&alarms[alarm_type]);
95 if (alarm_pending) {
96 alarm_pending &= ~alarm_type_mask;
97 if (!alarm_pending && !wait_pending)
98 wake_unlock(&alarm_wake_lock);
99 }
100 alarm_enabled &= ~alarm_type_mask;
Ashay Jaiswalc1b2e472013-05-17 15:15:02 +0530101 if (alarm_type == ANDROID_ALARM_RTC_WAKEUP)
102 set_power_on_alarm(0);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700103 spin_unlock_irqrestore(&alarm_slock, flags);
104 break;
105
106 case ANDROID_ALARM_SET_OLD:
107 case ANDROID_ALARM_SET_AND_WAIT_OLD:
108 if (get_user(new_alarm_time.tv_sec, (int __user *)arg)) {
109 rv = -EFAULT;
110 goto err1;
111 }
112 new_alarm_time.tv_nsec = 0;
113 goto from_old_alarm_set;
114
115 case ANDROID_ALARM_SET_AND_WAIT(0):
116 case ANDROID_ALARM_SET(0):
117 if (copy_from_user(&new_alarm_time, (void __user *)arg,
118 sizeof(new_alarm_time))) {
119 rv = -EFAULT;
120 goto err1;
121 }
122from_old_alarm_set:
123 spin_lock_irqsave(&alarm_slock, flags);
124 pr_alarm(IO, "alarm %d set %ld.%09ld\n", alarm_type,
125 new_alarm_time.tv_sec, new_alarm_time.tv_nsec);
126 alarm_enabled |= alarm_type_mask;
127 alarm_start_range(&alarms[alarm_type],
128 timespec_to_ktime(new_alarm_time),
129 timespec_to_ktime(new_alarm_time));
Ashay Jaiswalc1b2e472013-05-17 15:15:02 +0530130 if ((alarm_type == ANDROID_ALARM_RTC_WAKEUP) &&
131 (ANDROID_ALARM_BASE_CMD(cmd) ==
132 ANDROID_ALARM_SET(0)))
133 set_power_on_alarm(new_alarm_time.tv_sec);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700134 spin_unlock_irqrestore(&alarm_slock, flags);
135 if (ANDROID_ALARM_BASE_CMD(cmd) != ANDROID_ALARM_SET_AND_WAIT(0)
136 && cmd != ANDROID_ALARM_SET_AND_WAIT_OLD)
137 break;
138 /* fall though */
139 case ANDROID_ALARM_WAIT:
140 spin_lock_irqsave(&alarm_slock, flags);
141 pr_alarm(IO, "alarm wait\n");
142 if (!alarm_pending && wait_pending) {
143 wake_unlock(&alarm_wake_lock);
144 wait_pending = 0;
145 }
146 spin_unlock_irqrestore(&alarm_slock, flags);
147 rv = wait_event_interruptible(alarm_wait_queue, alarm_pending);
148 if (rv)
149 goto err1;
150 spin_lock_irqsave(&alarm_slock, flags);
151 rv = alarm_pending;
152 wait_pending = 1;
153 alarm_pending = 0;
154 spin_unlock_irqrestore(&alarm_slock, flags);
155 break;
156 case ANDROID_ALARM_SET_RTC:
157 if (copy_from_user(&new_rtc_time, (void __user *)arg,
158 sizeof(new_rtc_time))) {
159 rv = -EFAULT;
160 goto err1;
161 }
162 rv = alarm_set_rtc(new_rtc_time);
163 spin_lock_irqsave(&alarm_slock, flags);
164 alarm_pending |= ANDROID_ALARM_TIME_CHANGE_MASK;
165 wake_up(&alarm_wait_queue);
166 spin_unlock_irqrestore(&alarm_slock, flags);
167 if (rv < 0)
168 goto err1;
169 break;
170 case ANDROID_ALARM_GET_TIME(0):
171 switch (alarm_type) {
172 case ANDROID_ALARM_RTC_WAKEUP:
173 case ANDROID_ALARM_RTC:
174 getnstimeofday(&tmp_time);
175 break;
176 case ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP:
177 case ANDROID_ALARM_ELAPSED_REALTIME:
178 tmp_time =
179 ktime_to_timespec(alarm_get_elapsed_realtime());
180 break;
181 case ANDROID_ALARM_TYPE_COUNT:
182 case ANDROID_ALARM_SYSTEMTIME:
183 ktime_get_ts(&tmp_time);
184 break;
185 }
186 if (copy_to_user((void __user *)arg, &tmp_time,
187 sizeof(tmp_time))) {
188 rv = -EFAULT;
189 goto err1;
190 }
191 break;
192
193 default:
194 rv = -EINVAL;
195 goto err1;
196 }
197err1:
198 return rv;
199}
200
201static int alarm_open(struct inode *inode, struct file *file)
202{
203 file->private_data = NULL;
204 return 0;
205}
206
207static int alarm_release(struct inode *inode, struct file *file)
208{
209 int i;
210 unsigned long flags;
211
212 spin_lock_irqsave(&alarm_slock, flags);
213 if (file->private_data != 0) {
214 for (i = 0; i < ANDROID_ALARM_TYPE_COUNT; i++) {
215 uint32_t alarm_type_mask = 1U << i;
216 if (alarm_enabled & alarm_type_mask) {
217 pr_alarm(INFO, "alarm_release: clear alarm, "
218 "pending %d\n",
219 !!(alarm_pending & alarm_type_mask));
220 alarm_enabled &= ~alarm_type_mask;
221 }
222 spin_unlock_irqrestore(&alarm_slock, flags);
223 alarm_cancel(&alarms[i]);
224 spin_lock_irqsave(&alarm_slock, flags);
225 }
226 if (alarm_pending | wait_pending) {
227 if (alarm_pending)
228 pr_alarm(INFO, "alarm_release: clear "
229 "pending alarms %x\n", alarm_pending);
230 wake_unlock(&alarm_wake_lock);
231 wait_pending = 0;
232 alarm_pending = 0;
233 }
234 alarm_opened = 0;
235 }
236 spin_unlock_irqrestore(&alarm_slock, flags);
237 return 0;
238}
239
240static void alarm_triggered(struct alarm *alarm)
241{
242 unsigned long flags;
243 uint32_t alarm_type_mask = 1U << alarm->type;
244
245 pr_alarm(INT, "alarm_triggered type %d\n", alarm->type);
246 spin_lock_irqsave(&alarm_slock, flags);
247 if (alarm_enabled & alarm_type_mask) {
248 wake_lock_timeout(&alarm_wake_lock, 5 * HZ);
249 alarm_enabled &= ~alarm_type_mask;
250 alarm_pending |= alarm_type_mask;
251 wake_up(&alarm_wait_queue);
252 }
253 spin_unlock_irqrestore(&alarm_slock, flags);
254}
255
256static const struct file_operations alarm_fops = {
257 .owner = THIS_MODULE,
258 .unlocked_ioctl = alarm_ioctl,
259 .open = alarm_open,
260 .release = alarm_release,
261};
262
263static struct miscdevice alarm_device = {
264 .minor = MISC_DYNAMIC_MINOR,
265 .name = "alarm",
266 .fops = &alarm_fops,
267};
268
269static int __init alarm_dev_init(void)
270{
271 int err;
272 int i;
273
274 err = misc_register(&alarm_device);
275 if (err)
276 return err;
277
278 for (i = 0; i < ANDROID_ALARM_TYPE_COUNT; i++)
279 alarm_init(&alarms[i], i, alarm_triggered);
280 wake_lock_init(&alarm_wake_lock, WAKE_LOCK_SUSPEND, "alarm");
281
282 return 0;
283}
284
285static void __exit alarm_dev_exit(void)
286{
287 misc_deregister(&alarm_device);
288 wake_lock_destroy(&alarm_wake_lock);
289}
290
291module_init(alarm_dev_init);
292module_exit(alarm_dev_exit);
293