2 * linux/arch/arm/common/rtctime.c
4 * Copyright (C) 2003 Deep Blue Solutions Ltd.
5 * Based on sa1100-rtc.c, Nils Faerber, CIH, Nicolas Pitre.
6 * Based on rtc.c by Paul Gortmaker
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/time.h>
15 #include <linux/rtc.h>
16 #include <linux/poll.h>
17 #include <linux/proc_fs.h>
18 #include <linux/miscdevice.h>
19 #include <linux/spinlock.h>
20 #include <linux/capability.h>
21 #include <linux/device.h>
22 #include <linux/mutex.h>
26 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
27 static struct fasync_struct *rtc_async_queue;
30 * rtc_lock protects rtc_irq_data
32 static DEFINE_SPINLOCK(rtc_lock);
33 static unsigned long rtc_irq_data;
36 * rtc_sem protects rtc_inuse and rtc_ops
38 static DEFINE_MUTEX(rtc_mutex);
39 static unsigned long rtc_inuse;
40 static struct rtc_ops *rtc_ops;
42 #define rtc_epoch 1900UL
45 * Calculate the next alarm time given the requested alarm time mask
46 * and the current time.
48 void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm)
50 unsigned long next_time;
51 unsigned long now_time;
53 next->tm_year = now->tm_year;
54 next->tm_mon = now->tm_mon;
55 next->tm_mday = now->tm_mday;
56 next->tm_hour = alrm->tm_hour;
57 next->tm_min = alrm->tm_min;
58 next->tm_sec = alrm->tm_sec;
60 rtc_tm_to_time(now, &now_time);
61 rtc_tm_to_time(next, &next_time);
63 if (next_time < now_time) {
65 next_time += 60 * 60 * 24;
66 rtc_time_to_tm(next_time, next);
69 EXPORT_SYMBOL(rtc_next_alarm_time);
71 static inline int rtc_arm_read_time(struct rtc_ops *ops, struct rtc_time *tm)
73 memset(tm, 0, sizeof(struct rtc_time));
74 return ops->read_time(tm);
77 static inline int rtc_arm_set_time(struct rtc_ops *ops, struct rtc_time *tm)
81 ret = rtc_valid_tm(tm);
83 ret = ops->set_time(tm);
88 static inline int rtc_arm_read_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
91 if (ops->read_alarm) {
92 memset(alrm, 0, sizeof(struct rtc_wkalrm));
93 ret = ops->read_alarm(alrm);
98 static inline int rtc_arm_set_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
102 ret = ops->set_alarm(alrm);
106 void rtc_update(unsigned long num, unsigned long events)
108 spin_lock(&rtc_lock);
109 rtc_irq_data = (rtc_irq_data + (num << 8)) | events;
110 spin_unlock(&rtc_lock);
112 wake_up_interruptible(&rtc_wait);
113 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
115 EXPORT_SYMBOL(rtc_update);
119 rtc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
121 DECLARE_WAITQUEUE(wait, current);
125 if (count < sizeof(unsigned long))
128 add_wait_queue(&rtc_wait, &wait);
130 __set_current_state(TASK_INTERRUPTIBLE);
132 spin_lock_irq(&rtc_lock);
135 spin_unlock_irq(&rtc_lock);
141 if (file->f_flags & O_NONBLOCK) {
145 if (signal_pending(current)) {
151 set_current_state(TASK_RUNNING);
152 remove_wait_queue(&rtc_wait, &wait);
155 ret = put_user(data, (unsigned long __user *)buf);
157 ret = sizeof(unsigned long);
162 static unsigned int rtc_poll(struct file *file, poll_table *wait)
166 poll_wait(file, &rtc_wait, wait);
168 spin_lock_irq(&rtc_lock);
170 spin_unlock_irq(&rtc_lock);
172 return data != 0 ? POLLIN | POLLRDNORM : 0;
175 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
178 struct rtc_ops *ops = file->private_data;
180 struct rtc_wkalrm alrm;
181 void __user *uarg = (void __user *)arg;
186 ret = rtc_arm_read_alarm(ops, &alrm);
189 ret = copy_to_user(uarg, &alrm.time, sizeof(tm));
195 ret = copy_from_user(&alrm.time, uarg, sizeof(tm));
202 alrm.time.tm_mday = -1;
203 alrm.time.tm_mon = -1;
204 alrm.time.tm_year = -1;
205 alrm.time.tm_wday = -1;
206 alrm.time.tm_yday = -1;
207 alrm.time.tm_isdst = -1;
208 ret = rtc_arm_set_alarm(ops, &alrm);
212 ret = rtc_arm_read_time(ops, &tm);
215 ret = copy_to_user(uarg, &tm, sizeof(tm));
221 if (!capable(CAP_SYS_TIME)) {
225 ret = copy_from_user(&tm, uarg, sizeof(tm));
230 ret = rtc_arm_set_time(ops, &tm);
236 * There were no RTC clocks before 1900.
242 if (!capable(CAP_SYS_TIME)) {
252 ret = put_user(rtc_epoch, (unsigned long __user *)uarg);
256 ret = copy_from_user(&alrm, uarg, sizeof(alrm));
261 ret = rtc_arm_set_alarm(ops, &alrm);
265 ret = rtc_arm_read_alarm(ops, &alrm);
268 ret = copy_to_user(uarg, &alrm, sizeof(alrm));
275 ret = ops->ioctl(cmd, arg);
281 static int rtc_open(struct inode *inode, struct file *file)
285 mutex_lock(&rtc_mutex);
289 } else if (!rtc_ops || !try_module_get(rtc_ops->owner)) {
292 file->private_data = rtc_ops;
294 ret = rtc_ops->open ? rtc_ops->open() : 0;
296 spin_lock_irq(&rtc_lock);
298 spin_unlock_irq(&rtc_lock);
303 mutex_unlock(&rtc_mutex);
308 static int rtc_release(struct inode *inode, struct file *file)
310 struct rtc_ops *ops = file->private_data;
315 spin_lock_irq(&rtc_lock);
317 spin_unlock_irq(&rtc_lock);
319 module_put(rtc_ops->owner);
325 static int rtc_fasync(int fd, struct file *file, int on)
327 return fasync_helper(fd, file, on, &rtc_async_queue);
330 static const struct file_operations rtc_fops = {
331 .owner = THIS_MODULE,
337 .release = rtc_release,
338 .fasync = rtc_fasync,
341 static struct miscdevice rtc_miscdev = {
348 static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
350 struct rtc_ops *ops = data;
351 struct rtc_wkalrm alrm;
355 if (rtc_arm_read_time(ops, &tm) == 0) {
357 "rtc_time\t: %02d:%02d:%02d\n"
358 "rtc_date\t: %04d-%02d-%02d\n"
359 "rtc_epoch\t: %04lu\n",
360 tm.tm_hour, tm.tm_min, tm.tm_sec,
361 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
365 if (rtc_arm_read_alarm(ops, &alrm) == 0) {
366 p += sprintf(p, "alrm_time\t: ");
367 if ((unsigned int)alrm.time.tm_hour <= 24)
368 p += sprintf(p, "%02d:", alrm.time.tm_hour);
370 p += sprintf(p, "**:");
371 if ((unsigned int)alrm.time.tm_min <= 59)
372 p += sprintf(p, "%02d:", alrm.time.tm_min);
374 p += sprintf(p, "**:");
375 if ((unsigned int)alrm.time.tm_sec <= 59)
376 p += sprintf(p, "%02d\n", alrm.time.tm_sec);
378 p += sprintf(p, "**\n");
380 p += sprintf(p, "alrm_date\t: ");
381 if ((unsigned int)alrm.time.tm_year <= 200)
382 p += sprintf(p, "%04d-", alrm.time.tm_year + 1900);
384 p += sprintf(p, "****-");
385 if ((unsigned int)alrm.time.tm_mon <= 11)
386 p += sprintf(p, "%02d-", alrm.time.tm_mon + 1);
388 p += sprintf(p, "**-");
389 if ((unsigned int)alrm.time.tm_mday <= 31)
390 p += sprintf(p, "%02d\n", alrm.time.tm_mday);
392 p += sprintf(p, "**\n");
393 p += sprintf(p, "alrm_wakeup\t: %s\n",
394 alrm.enabled ? "yes" : "no");
395 p += sprintf(p, "alrm_pending\t: %s\n",
396 alrm.pending ? "yes" : "no");
405 int register_rtc(struct rtc_ops *ops)
409 mutex_lock(&rtc_mutex);
410 if (rtc_ops == NULL) {
413 ret = misc_register(&rtc_miscdev);
415 create_proc_read_entry("driver/rtc", 0, NULL,
418 mutex_unlock(&rtc_mutex);
422 EXPORT_SYMBOL(register_rtc);
424 void unregister_rtc(struct rtc_ops *rtc)
426 mutex_lock(&rtc_mutex);
427 if (rtc == rtc_ops) {
428 remove_proc_entry("driver/rtc", NULL);
429 misc_deregister(&rtc_miscdev);
432 mutex_unlock(&rtc_mutex);
434 EXPORT_SYMBOL(unregister_rtc);