Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 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 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 17 | #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åg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
| 25 | #include <linux/wakelock.h> |
| 26 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 27 | #include <asm/mach/time.h> |
| 28 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 29 | #define ANDROID_ALARM_PRINT_INFO (1U << 0) |
| 30 | #define ANDROID_ALARM_PRINT_IO (1U << 1) |
| 31 | #define ANDROID_ALARM_PRINT_INT (1U << 2) |
| 32 | |
| 33 | static int debug_mask = ANDROID_ALARM_PRINT_INFO; |
| 34 | module_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 | |
| 51 | static int alarm_opened; |
| 52 | static DEFINE_SPINLOCK(alarm_slock); |
| 53 | static struct wake_lock alarm_wake_lock; |
| 54 | static DECLARE_WAIT_QUEUE_HEAD(alarm_wait_queue); |
| 55 | static uint32_t alarm_pending; |
| 56 | static uint32_t alarm_enabled; |
| 57 | static uint32_t wait_pending; |
| 58 | |
| 59 | static struct alarm alarms[ANDROID_ALARM_TYPE_COUNT]; |
| 60 | |
| 61 | static 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; |
Figo Wang | 091c9af | 2013-09-12 20:41:27 +0800 | [diff] [blame] | 101 | if (alarm_type == ANDROID_ALARM_RTC_POWEROFF_WAKEUP) |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame] | 102 | set_power_on_alarm(0); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 103 | 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 | } |
| 122 | from_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)); |
Figo Wang | 091c9af | 2013-09-12 20:41:27 +0800 | [diff] [blame] | 130 | if ((alarm_type == ANDROID_ALARM_RTC_POWEROFF_WAKEUP) && |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame] | 131 | (ANDROID_ALARM_BASE_CMD(cmd) == |
| 132 | ANDROID_ALARM_SET(0))) |
| 133 | set_power_on_alarm(new_alarm_time.tv_sec); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 134 | 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: |
Figo Wang | 091c9af | 2013-09-12 20:41:27 +0800 | [diff] [blame] | 174 | case ANDROID_ALARM_RTC_POWEROFF_WAKEUP: |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 175 | getnstimeofday(&tmp_time); |
| 176 | break; |
| 177 | case ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP: |
| 178 | case ANDROID_ALARM_ELAPSED_REALTIME: |
| 179 | tmp_time = |
| 180 | ktime_to_timespec(alarm_get_elapsed_realtime()); |
| 181 | break; |
| 182 | case ANDROID_ALARM_TYPE_COUNT: |
| 183 | case ANDROID_ALARM_SYSTEMTIME: |
| 184 | ktime_get_ts(&tmp_time); |
| 185 | break; |
| 186 | } |
| 187 | if (copy_to_user((void __user *)arg, &tmp_time, |
| 188 | sizeof(tmp_time))) { |
| 189 | rv = -EFAULT; |
| 190 | goto err1; |
| 191 | } |
| 192 | break; |
| 193 | |
| 194 | default: |
| 195 | rv = -EINVAL; |
| 196 | goto err1; |
| 197 | } |
| 198 | err1: |
| 199 | return rv; |
| 200 | } |
| 201 | |
| 202 | static int alarm_open(struct inode *inode, struct file *file) |
| 203 | { |
| 204 | file->private_data = NULL; |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int alarm_release(struct inode *inode, struct file *file) |
| 209 | { |
| 210 | int i; |
| 211 | unsigned long flags; |
| 212 | |
| 213 | spin_lock_irqsave(&alarm_slock, flags); |
| 214 | if (file->private_data != 0) { |
| 215 | for (i = 0; i < ANDROID_ALARM_TYPE_COUNT; i++) { |
| 216 | uint32_t alarm_type_mask = 1U << i; |
| 217 | if (alarm_enabled & alarm_type_mask) { |
| 218 | pr_alarm(INFO, "alarm_release: clear alarm, " |
| 219 | "pending %d\n", |
| 220 | !!(alarm_pending & alarm_type_mask)); |
| 221 | alarm_enabled &= ~alarm_type_mask; |
| 222 | } |
| 223 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 224 | alarm_cancel(&alarms[i]); |
| 225 | spin_lock_irqsave(&alarm_slock, flags); |
| 226 | } |
| 227 | if (alarm_pending | wait_pending) { |
| 228 | if (alarm_pending) |
| 229 | pr_alarm(INFO, "alarm_release: clear " |
| 230 | "pending alarms %x\n", alarm_pending); |
| 231 | wake_unlock(&alarm_wake_lock); |
| 232 | wait_pending = 0; |
| 233 | alarm_pending = 0; |
| 234 | } |
| 235 | alarm_opened = 0; |
| 236 | } |
| 237 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static void alarm_triggered(struct alarm *alarm) |
| 242 | { |
| 243 | unsigned long flags; |
| 244 | uint32_t alarm_type_mask = 1U << alarm->type; |
| 245 | |
| 246 | pr_alarm(INT, "alarm_triggered type %d\n", alarm->type); |
| 247 | spin_lock_irqsave(&alarm_slock, flags); |
| 248 | if (alarm_enabled & alarm_type_mask) { |
| 249 | wake_lock_timeout(&alarm_wake_lock, 5 * HZ); |
| 250 | alarm_enabled &= ~alarm_type_mask; |
| 251 | alarm_pending |= alarm_type_mask; |
| 252 | wake_up(&alarm_wait_queue); |
| 253 | } |
| 254 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 255 | } |
| 256 | |
| 257 | static const struct file_operations alarm_fops = { |
| 258 | .owner = THIS_MODULE, |
| 259 | .unlocked_ioctl = alarm_ioctl, |
| 260 | .open = alarm_open, |
| 261 | .release = alarm_release, |
| 262 | }; |
| 263 | |
| 264 | static struct miscdevice alarm_device = { |
| 265 | .minor = MISC_DYNAMIC_MINOR, |
| 266 | .name = "alarm", |
| 267 | .fops = &alarm_fops, |
| 268 | }; |
| 269 | |
| 270 | static int __init alarm_dev_init(void) |
| 271 | { |
| 272 | int err; |
| 273 | int i; |
| 274 | |
| 275 | err = misc_register(&alarm_device); |
| 276 | if (err) |
| 277 | return err; |
| 278 | |
| 279 | for (i = 0; i < ANDROID_ALARM_TYPE_COUNT; i++) |
| 280 | alarm_init(&alarms[i], i, alarm_triggered); |
| 281 | wake_lock_init(&alarm_wake_lock, WAKE_LOCK_SUSPEND, "alarm"); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static void __exit alarm_dev_exit(void) |
| 287 | { |
| 288 | misc_deregister(&alarm_device); |
| 289 | wake_lock_destroy(&alarm_wake_lock); |
| 290 | } |
| 291 | |
| 292 | module_init(alarm_dev_init); |
| 293 | module_exit(alarm_dev_exit); |
| 294 | |