Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / staging / android / alarm-dev.c
blob5b7064005188d9ac9299ee24e79e9c163ef785df
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>
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 <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, ...) \
36 do { \
37 if (debug_mask & ANDROID_ALARM_PRINT_##debug_level_mask) \
38 pr_info(fmt, ##__VA_ARGS__); \
39 } while (0)
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;
57 struct devalarm {
58 union {
59 struct hrtimer hrt;
60 struct alarm alrm;
61 } u;
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)
72 return 1;
73 return 0;
77 static void devalarm_start(struct devalarm *alrm, ktime_t exp)
79 if (is_wakeup(alrm->type))
80 alarm_start(&alrm->u.alrm, exp);
81 else
82 hrtimer_start(&alrm->u.hrt, exp, HRTIMER_MODE_ABS);
86 static int devalarm_try_to_cancel(struct devalarm *alrm)
88 int ret;
89 if (is_wakeup(alrm->type))
90 ret = alarm_try_to_cancel(&alrm->u.alrm);
91 else
92 ret = hrtimer_try_to_cancel(&alrm->u.hrt);
93 return ret;
96 static void devalarm_cancel(struct devalarm *alrm)
98 if (is_wakeup(alrm->type))
99 alarm_cancel(&alrm->u.alrm);
100 else
101 hrtimer_cancel(&alrm->u.hrt);
105 static long alarm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
107 int rv = 0;
108 unsigned long flags;
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)
118 return -EINVAL;
120 if (ANDROID_ALARM_BASE_CMD(cmd) != ANDROID_ALARM_GET_TIME(0)) {
121 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
122 return -EPERM;
123 if (file->private_data == NULL &&
124 cmd != ANDROID_ALARM_SET_RTC) {
125 spin_lock_irqsave(&alarm_slock, flags);
126 if (alarm_opened) {
127 spin_unlock_irqrestore(&alarm_slock, flags);
128 return -EBUSY;
130 alarm_opened = 1;
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]);
141 if (alarm_pending) {
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);
148 break;
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)) {
153 rv = -EFAULT;
154 goto err1;
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))) {
163 rv = -EFAULT;
164 goto err1;
166 from_old_alarm_set:
167 spin_lock_irqsave(&alarm_slock, flags);
168 alarm_dbg(IO, "alarm %d set %ld.%09ld\n",
169 alarm_type,
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)
177 break;
178 /* fall though */
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);
184 wait_pending = 0;
186 spin_unlock_irqrestore(&alarm_slock, flags);
187 rv = wait_event_interruptible(alarm_wait_queue, alarm_pending);
188 if (rv)
189 goto err1;
190 spin_lock_irqsave(&alarm_slock, flags);
191 rv = alarm_pending;
192 wait_pending = 1;
193 alarm_pending = 0;
194 spin_unlock_irqrestore(&alarm_slock, flags);
195 break;
196 case ANDROID_ALARM_SET_RTC:
197 if (copy_from_user(&new_rtc_time, (void __user *)arg,
198 sizeof(new_rtc_time))) {
199 rv = -EFAULT;
200 goto err1;
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);
205 if (rv < 0)
206 goto err1;
207 if (rtc_dev)
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);
213 if (rv < 0)
214 goto err1;
215 break;
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);
221 break;
222 case ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP:
223 case ANDROID_ALARM_ELAPSED_REALTIME:
224 get_monotonic_boottime(&tmp_time);
225 break;
226 case ANDROID_ALARM_TYPE_COUNT:
227 case ANDROID_ALARM_SYSTEMTIME:
228 ktime_get_ts(&tmp_time);
229 break;
231 if (copy_to_user((void __user *)arg, &tmp_time,
232 sizeof(tmp_time))) {
233 rv = -EFAULT;
234 goto err1;
236 break;
238 default:
239 rv = -EINVAL;
241 err1:
242 return rv;
245 static int alarm_open(struct inode *inode, struct file *file)
247 file->private_data = NULL;
248 return 0;
251 static int alarm_release(struct inode *inode, struct file *file)
253 int i;
254 unsigned long flags;
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) {
261 alarm_dbg(INFO,
262 "%s: clear alarm, pending %d\n",
263 __func__,
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) {
272 if (alarm_pending)
273 alarm_dbg(INFO, "%s: clear pending alarms %x\n",
274 __func__, alarm_pending);
275 __pm_relax(&alarm_wake_lock);
276 wait_pending = 0;
277 alarm_pending = 0;
279 alarm_opened = 0;
281 spin_unlock_irqrestore(&alarm_slock, flags);
282 return 0;
285 static void devalarm_triggered(struct devalarm *alarm)
287 unsigned long flags;
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,
311 ktime_t now)
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,
323 .open = alarm_open,
324 .release = alarm_release,
327 static struct miscdevice alarm_device = {
328 .minor = MISC_DYNAMIC_MINOR,
329 .name = "alarm",
330 .fops = &alarm_fops,
333 static int __init alarm_dev_init(void)
335 int err;
336 int i;
338 err = misc_register(&alarm_device);
339 if (err)
340 return err;
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++) {
354 alarms[i].type = i;
355 if (!is_wakeup(i))
356 alarms[i].u.hrt.function = devalarm_hrthandler;
359 wakeup_source_init(&alarm_wake_lock, "alarm");
360 return 0;
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);