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