Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
alarm-dev.c
Go to the documentation of this file.
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 <linux/alarmtimer.h>
26 #include "android_alarm.h"
27 
28 #define ANDROID_ALARM_PRINT_INFO (1U << 0)
29 #define ANDROID_ALARM_PRINT_IO (1U << 1)
30 #define ANDROID_ALARM_PRINT_INT (1U << 2)
31 
33 module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
34 
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)
40 
41 #define ANDROID_ALARM_WAKEUP_MASK ( \
42  ANDROID_ALARM_RTC_WAKEUP_MASK | \
43  ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK)
44 
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)
48 
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;
56 
57 struct devalarm {
58  union {
59  struct hrtimer hrt;
60  struct alarm alrm;
61  } u;
63 };
64 
65 static struct devalarm alarms[ANDROID_ALARM_TYPE_COUNT];
66 
67 
68 static int is_wakeup(enum android_alarm_type type)
69 {
70  return (type == ANDROID_ALARM_RTC_WAKEUP ||
72 }
73 
74 
75 static void devalarm_start(struct devalarm *alrm, ktime_t exp)
76 {
77  if (is_wakeup(alrm->type))
78  alarm_start(&alrm->u.alrm, exp);
79  else
80  hrtimer_start(&alrm->u.hrt, exp, HRTIMER_MODE_ABS);
81 }
82 
83 
84 static int devalarm_try_to_cancel(struct devalarm *alrm)
85 {
86  if (is_wakeup(alrm->type))
87  return alarm_try_to_cancel(&alrm->u.alrm);
88  return hrtimer_try_to_cancel(&alrm->u.hrt);
89 }
90 
91 static void devalarm_cancel(struct devalarm *alrm)
92 {
93  if (is_wakeup(alrm->type))
94  alarm_cancel(&alrm->u.alrm);
95  else
96  hrtimer_cancel(&alrm->u.hrt);
97 }
98 
99 
100 static long alarm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
101 {
102  int rv = 0;
103  unsigned long flags;
104  struct timespec new_alarm_time;
105  struct timespec new_rtc_time;
106  struct timespec tmp_time;
107  struct rtc_time new_rtc_tm;
108  struct rtc_device *rtc_dev;
109  enum android_alarm_type alarm_type = ANDROID_ALARM_IOCTL_TO_TYPE(cmd);
110  uint32_t alarm_type_mask = 1U << alarm_type;
111 
112  if (alarm_type >= ANDROID_ALARM_TYPE_COUNT)
113  return -EINVAL;
114 
116  if ((file->f_flags & O_ACCMODE) == O_RDONLY)
117  return -EPERM;
118  if (file->private_data == NULL &&
119  cmd != ANDROID_ALARM_SET_RTC) {
120  spin_lock_irqsave(&alarm_slock, flags);
121  if (alarm_opened) {
122  spin_unlock_irqrestore(&alarm_slock, flags);
123  return -EBUSY;
124  }
125  alarm_opened = 1;
126  file->private_data = (void *)1;
127  spin_unlock_irqrestore(&alarm_slock, flags);
128  }
129  }
130 
131  switch (ANDROID_ALARM_BASE_CMD(cmd)) {
132  case ANDROID_ALARM_CLEAR(0):
133  spin_lock_irqsave(&alarm_slock, flags);
134  alarm_dbg(IO, "alarm %d clear\n", alarm_type);
135  devalarm_try_to_cancel(&alarms[alarm_type]);
136  if (alarm_pending) {
137  alarm_pending &= ~alarm_type_mask;
138  if (!alarm_pending && !wait_pending)
139  __pm_relax(&alarm_wake_lock);
140  }
141  alarm_enabled &= ~alarm_type_mask;
142  spin_unlock_irqrestore(&alarm_slock, flags);
143  break;
144 
147  if (get_user(new_alarm_time.tv_sec, (int __user *)arg)) {
148  rv = -EFAULT;
149  goto err1;
150  }
151  new_alarm_time.tv_nsec = 0;
152  goto from_old_alarm_set;
153 
155  case ANDROID_ALARM_SET(0):
156  if (copy_from_user(&new_alarm_time, (void __user *)arg,
157  sizeof(new_alarm_time))) {
158  rv = -EFAULT;
159  goto err1;
160  }
161 from_old_alarm_set:
162  spin_lock_irqsave(&alarm_slock, flags);
163  alarm_dbg(IO, "alarm %d set %ld.%09ld\n",
164  alarm_type,
165  new_alarm_time.tv_sec, new_alarm_time.tv_nsec);
166  alarm_enabled |= alarm_type_mask;
167  devalarm_start(&alarms[alarm_type],
168  timespec_to_ktime(new_alarm_time));
169  spin_unlock_irqrestore(&alarm_slock, flags);
172  break;
173  /* fall though */
174  case ANDROID_ALARM_WAIT:
175  spin_lock_irqsave(&alarm_slock, flags);
176  alarm_dbg(IO, "alarm wait\n");
177  if (!alarm_pending && wait_pending) {
178  __pm_relax(&alarm_wake_lock);
179  wait_pending = 0;
180  }
181  spin_unlock_irqrestore(&alarm_slock, flags);
182  rv = wait_event_interruptible(alarm_wait_queue, alarm_pending);
183  if (rv)
184  goto err1;
185  spin_lock_irqsave(&alarm_slock, flags);
186  rv = alarm_pending;
187  wait_pending = 1;
188  alarm_pending = 0;
189  spin_unlock_irqrestore(&alarm_slock, flags);
190  break;
192  if (copy_from_user(&new_rtc_time, (void __user *)arg,
193  sizeof(new_rtc_time))) {
194  rv = -EFAULT;
195  goto err1;
196  }
197  rtc_time_to_tm(new_rtc_time.tv_sec, &new_rtc_tm);
198  rtc_dev = alarmtimer_get_rtcdev();
199  rv = do_settimeofday(&new_rtc_time);
200  if (rv < 0)
201  goto err1;
202  if (rtc_dev)
203  rv = rtc_set_time(rtc_dev, &new_rtc_tm);
204  spin_lock_irqsave(&alarm_slock, flags);
205  alarm_pending |= ANDROID_ALARM_TIME_CHANGE_MASK;
206  wake_up(&alarm_wait_queue);
207  spin_unlock_irqrestore(&alarm_slock, flags);
208  if (rv < 0)
209  goto err1;
210  break;
211  case ANDROID_ALARM_GET_TIME(0):
212  switch (alarm_type) {
214  case ANDROID_ALARM_RTC:
215  getnstimeofday(&tmp_time);
216  break;
219  get_monotonic_boottime(&tmp_time);
220  break;
222  ktime_get_ts(&tmp_time);
223  break;
224  default:
225  rv = -EINVAL;
226  goto err1;
227  }
228  if (copy_to_user((void __user *)arg, &tmp_time,
229  sizeof(tmp_time))) {
230  rv = -EFAULT;
231  goto err1;
232  }
233  break;
234 
235  default:
236  rv = -EINVAL;
237  }
238 err1:
239  return rv;
240 }
241 
242 static int alarm_open(struct inode *inode, struct file *file)
243 {
244  file->private_data = NULL;
245  return 0;
246 }
247 
248 static int alarm_release(struct inode *inode, struct file *file)
249 {
250  int i;
251  unsigned long flags;
252 
253  spin_lock_irqsave(&alarm_slock, flags);
254  if (file->private_data) {
255  for (i = 0; i < ANDROID_ALARM_TYPE_COUNT; i++) {
256  uint32_t alarm_type_mask = 1U << i;
257  if (alarm_enabled & alarm_type_mask) {
258  alarm_dbg(INFO,
259  "%s: clear alarm, pending %d\n",
260  __func__,
261  !!(alarm_pending & alarm_type_mask));
262  alarm_enabled &= ~alarm_type_mask;
263  }
264  spin_unlock_irqrestore(&alarm_slock, flags);
265  devalarm_cancel(&alarms[i]);
266  spin_lock_irqsave(&alarm_slock, flags);
267  }
268  if (alarm_pending | wait_pending) {
269  if (alarm_pending)
270  alarm_dbg(INFO, "%s: clear pending alarms %x\n",
271  __func__, alarm_pending);
272  __pm_relax(&alarm_wake_lock);
273  wait_pending = 0;
274  alarm_pending = 0;
275  }
276  alarm_opened = 0;
277  }
278  spin_unlock_irqrestore(&alarm_slock, flags);
279  return 0;
280 }
281 
282 static void devalarm_triggered(struct devalarm *alarm)
283 {
284  unsigned long flags;
285  uint32_t alarm_type_mask = 1U << alarm->type;
286 
287  alarm_dbg(INT, "%s: type %d\n", __func__, alarm->type);
288  spin_lock_irqsave(&alarm_slock, flags);
289  if (alarm_enabled & alarm_type_mask) {
290  __pm_wakeup_event(&alarm_wake_lock, 5000); /* 5secs */
291  alarm_enabled &= ~alarm_type_mask;
292  alarm_pending |= alarm_type_mask;
293  wake_up(&alarm_wait_queue);
294  }
295  spin_unlock_irqrestore(&alarm_slock, flags);
296 }
297 
298 
299 static enum hrtimer_restart devalarm_hrthandler(struct hrtimer *hrt)
300 {
301  struct devalarm *devalrm = container_of(hrt, struct devalarm, u.hrt);
302 
303  devalarm_triggered(devalrm);
304  return HRTIMER_NORESTART;
305 }
306 
307 static enum alarmtimer_restart devalarm_alarmhandler(struct alarm *alrm,
308  ktime_t now)
309 {
310  struct devalarm *devalrm = container_of(alrm, struct devalarm, u.alrm);
311 
312  devalarm_triggered(devalrm);
313  return ALARMTIMER_NORESTART;
314 }
315 
316 
317 static const struct file_operations alarm_fops = {
318  .owner = THIS_MODULE,
319  .unlocked_ioctl = alarm_ioctl,
320  .open = alarm_open,
321  .release = alarm_release,
322 };
323 
324 static struct miscdevice alarm_device = {
325  .minor = MISC_DYNAMIC_MINOR,
326  .name = "alarm",
327  .fops = &alarm_fops,
328 };
329 
330 static int __init alarm_dev_init(void)
331 {
332  int err;
333  int i;
334 
335  err = misc_register(&alarm_device);
336  if (err)
337  return err;
338 
339  alarm_init(&alarms[ANDROID_ALARM_RTC_WAKEUP].u.alrm,
340  ALARM_REALTIME, devalarm_alarmhandler);
341  hrtimer_init(&alarms[ANDROID_ALARM_RTC].u.hrt,
344  ALARM_BOOTTIME, devalarm_alarmhandler);
349 
350  for (i = 0; i < ANDROID_ALARM_TYPE_COUNT; i++) {
351  alarms[i].type = i;
352  if (!is_wakeup(i))
353  alarms[i].u.hrt.function = devalarm_hrthandler;
354  }
355 
356  wakeup_source_init(&alarm_wake_lock, "alarm");
357  return 0;
358 }
359 
360 static void __exit alarm_dev_exit(void)
361 {
362  misc_deregister(&alarm_device);
363  wakeup_source_trash(&alarm_wake_lock);
364 }
365 
366 module_init(alarm_dev_init);
367 module_exit(alarm_dev_exit);
368