blob: bfcaebc9ce429bd5d4c9fee6cd9d2b706fa1d624 [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;
101 spin_unlock_irqrestore(&alarm_slock, flags);
102 break;
103
104 case ANDROID_ALARM_SET_OLD:
105 case ANDROID_ALARM_SET_AND_WAIT_OLD:
106 if (get_user(new_alarm_time.tv_sec, (int __user *)arg)) {
107 rv = -EFAULT;
108 goto err1;
109 }
110 new_alarm_time.tv_nsec = 0;
111 goto from_old_alarm_set;
112
113 case ANDROID_ALARM_SET_AND_WAIT(0):
114 case ANDROID_ALARM_SET(0):
115 if (copy_from_user(&new_alarm_time, (void __user *)arg,
116 sizeof(new_alarm_time))) {
117 rv = -EFAULT;
118 goto err1;
119 }
120from_old_alarm_set:
121 spin_lock_irqsave(&alarm_slock, flags);
122 pr_alarm(IO, "alarm %d set %ld.%09ld\n", alarm_type,
123 new_alarm_time.tv_sec, new_alarm_time.tv_nsec);
124 alarm_enabled |= alarm_type_mask;
125 alarm_start_range(&alarms[alarm_type],
126 timespec_to_ktime(new_alarm_time),
127 timespec_to_ktime(new_alarm_time));
128 spin_unlock_irqrestore(&alarm_slock, flags);
129 if (ANDROID_ALARM_BASE_CMD(cmd) != ANDROID_ALARM_SET_AND_WAIT(0)
130 && cmd != ANDROID_ALARM_SET_AND_WAIT_OLD)
131 break;
132 /* fall though */
133 case ANDROID_ALARM_WAIT:
134 spin_lock_irqsave(&alarm_slock, flags);
135 pr_alarm(IO, "alarm wait\n");
136 if (!alarm_pending && wait_pending) {
137 wake_unlock(&alarm_wake_lock);
138 wait_pending = 0;
139 }
140 spin_unlock_irqrestore(&alarm_slock, flags);
141 rv = wait_event_interruptible(alarm_wait_queue, alarm_pending);
142 if (rv)
143 goto err1;
144 spin_lock_irqsave(&alarm_slock, flags);
145 rv = alarm_pending;
146 wait_pending = 1;
147 alarm_pending = 0;
148 spin_unlock_irqrestore(&alarm_slock, flags);
149 break;
150 case ANDROID_ALARM_SET_RTC:
151 if (copy_from_user(&new_rtc_time, (void __user *)arg,
152 sizeof(new_rtc_time))) {
153 rv = -EFAULT;
154 goto err1;
155 }
156 rv = alarm_set_rtc(new_rtc_time);
157 spin_lock_irqsave(&alarm_slock, flags);
158 alarm_pending |= ANDROID_ALARM_TIME_CHANGE_MASK;
159 wake_up(&alarm_wait_queue);
160 spin_unlock_irqrestore(&alarm_slock, flags);
161 if (rv < 0)
162 goto err1;
163 break;
164 case ANDROID_ALARM_GET_TIME(0):
165 switch (alarm_type) {
166 case ANDROID_ALARM_RTC_WAKEUP:
167 case ANDROID_ALARM_RTC:
168 getnstimeofday(&tmp_time);
169 break;
170 case ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP:
171 case ANDROID_ALARM_ELAPSED_REALTIME:
172 tmp_time =
173 ktime_to_timespec(alarm_get_elapsed_realtime());
174 break;
175 case ANDROID_ALARM_TYPE_COUNT:
176 case ANDROID_ALARM_SYSTEMTIME:
177 ktime_get_ts(&tmp_time);
178 break;
179 }
180 if (copy_to_user((void __user *)arg, &tmp_time,
181 sizeof(tmp_time))) {
182 rv = -EFAULT;
183 goto err1;
184 }
185 break;
186
187 default:
188 rv = -EINVAL;
189 goto err1;
190 }
191err1:
192 return rv;
193}
194
195static int alarm_open(struct inode *inode, struct file *file)
196{
197 file->private_data = NULL;
198 return 0;
199}
200
201static int alarm_release(struct inode *inode, struct file *file)
202{
203 int i;
204 unsigned long flags;
205
206 spin_lock_irqsave(&alarm_slock, flags);
207 if (file->private_data != 0) {
208 for (i = 0; i < ANDROID_ALARM_TYPE_COUNT; i++) {
209 uint32_t alarm_type_mask = 1U << i;
210 if (alarm_enabled & alarm_type_mask) {
211 pr_alarm(INFO, "alarm_release: clear alarm, "
212 "pending %d\n",
213 !!(alarm_pending & alarm_type_mask));
214 alarm_enabled &= ~alarm_type_mask;
215 }
216 spin_unlock_irqrestore(&alarm_slock, flags);
217 alarm_cancel(&alarms[i]);
218 spin_lock_irqsave(&alarm_slock, flags);
219 }
220 if (alarm_pending | wait_pending) {
221 if (alarm_pending)
222 pr_alarm(INFO, "alarm_release: clear "
223 "pending alarms %x\n", alarm_pending);
224 wake_unlock(&alarm_wake_lock);
225 wait_pending = 0;
226 alarm_pending = 0;
227 }
228 alarm_opened = 0;
229 }
230 spin_unlock_irqrestore(&alarm_slock, flags);
231 return 0;
232}
233
234static void alarm_triggered(struct alarm *alarm)
235{
236 unsigned long flags;
237 uint32_t alarm_type_mask = 1U << alarm->type;
238
239 pr_alarm(INT, "alarm_triggered type %d\n", alarm->type);
240 spin_lock_irqsave(&alarm_slock, flags);
241 if (alarm_enabled & alarm_type_mask) {
242 wake_lock_timeout(&alarm_wake_lock, 5 * HZ);
243 alarm_enabled &= ~alarm_type_mask;
244 alarm_pending |= alarm_type_mask;
245 wake_up(&alarm_wait_queue);
246 }
247 spin_unlock_irqrestore(&alarm_slock, flags);
248}
249
250static const struct file_operations alarm_fops = {
251 .owner = THIS_MODULE,
252 .unlocked_ioctl = alarm_ioctl,
253 .open = alarm_open,
254 .release = alarm_release,
255};
256
257static struct miscdevice alarm_device = {
258 .minor = MISC_DYNAMIC_MINOR,
259 .name = "alarm",
260 .fops = &alarm_fops,
261};
262
263static int __init alarm_dev_init(void)
264{
265 int err;
266 int i;
267
268 err = misc_register(&alarm_device);
269 if (err)
270 return err;
271
272 for (i = 0; i < ANDROID_ALARM_TYPE_COUNT; i++)
273 alarm_init(&alarms[i], i, alarm_triggered);
274 wake_lock_init(&alarm_wake_lock, WAKE_LOCK_SUSPEND, "alarm");
275
276 return 0;
277}
278
279static void __exit alarm_dev_exit(void)
280{
281 misc_deregister(&alarm_device);
282 wake_lock_destroy(&alarm_wake_lock);
283}
284
285module_init(alarm_dev_init);
286module_exit(alarm_dev_exit);
287