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>
25 #include <asm/semaphore.h>
27 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
28 static struct fasync_struct *rtc_async_queue;
31 * rtc_lock protects rtc_irq_data
33 static DEFINE_SPINLOCK(rtc_lock);
34 static unsigned long rtc_irq_data;
37 * rtc_sem protects rtc_inuse and rtc_ops
39 static DEFINE_MUTEX(rtc_mutex);
40 static unsigned long rtc_inuse;
41 static struct rtc_ops *rtc_ops;
43 #define rtc_epoch 1900UL
45 static const unsigned char days_in_month[] = {
46 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
49 #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
50 #define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))
52 static int month_days(unsigned int month, unsigned int year)
54 return days_in_month[month] + (LEAP_YEAR(year) && month == 1);
58 * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
60 void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
62 int days, month, year;
67 tm->tm_wday = (days + 4) % 7;
69 year = 1970 + days / 365;
70 days -= (year - 1970) * 365
71 + LEAPS_THRU_END_OF(year - 1)
72 - LEAPS_THRU_END_OF(1970 - 1);
75 days += 365 + LEAP_YEAR(year);
77 tm->tm_year = year - 1900;
78 tm->tm_yday = days + 1;
80 for (month = 0; month < 11; month++) {
83 newdays = days - month_days(month, year);
89 tm->tm_mday = days + 1;
91 tm->tm_hour = time / 3600;
92 time -= tm->tm_hour * 3600;
93 tm->tm_min = time / 60;
94 tm->tm_sec = time - tm->tm_min * 60;
96 EXPORT_SYMBOL(rtc_time_to_tm);
99 * Does the rtc_time represent a valid date/time?
101 int rtc_valid_tm(struct rtc_time *tm)
103 if (tm->tm_year < 70 ||
106 tm->tm_mday > month_days(tm->tm_mon, tm->tm_year + 1900) ||
114 EXPORT_SYMBOL(rtc_valid_tm);
117 * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
119 int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
121 *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
122 tm->tm_hour, tm->tm_min, tm->tm_sec);
126 EXPORT_SYMBOL(rtc_tm_to_time);
129 * Calculate the next alarm time given the requested alarm time mask
130 * and the current time.
132 * FIXME: for now, we just copy the alarm time because we're lazy (and
133 * is therefore buggy - setting a 10am alarm at 8pm will not result in
134 * the alarm triggering.)
136 void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm)
138 next->tm_year = now->tm_year;
139 next->tm_mon = now->tm_mon;
140 next->tm_mday = now->tm_mday;
141 next->tm_hour = alrm->tm_hour;
142 next->tm_min = alrm->tm_min;
143 next->tm_sec = alrm->tm_sec;
146 static inline int rtc_read_time(struct rtc_ops *ops, struct rtc_time *tm)
148 memset(tm, 0, sizeof(struct rtc_time));
149 return ops->read_time(tm);
152 static inline int rtc_set_time(struct rtc_ops *ops, struct rtc_time *tm)
156 ret = rtc_valid_tm(tm);
158 ret = ops->set_time(tm);
163 static inline int rtc_read_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
166 if (ops->read_alarm) {
167 memset(alrm, 0, sizeof(struct rtc_wkalrm));
168 ret = ops->read_alarm(alrm);
173 static inline int rtc_set_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
177 ret = ops->set_alarm(alrm);
181 void rtc_update(unsigned long num, unsigned long events)
183 spin_lock(&rtc_lock);
184 rtc_irq_data = (rtc_irq_data + (num << 8)) | events;
185 spin_unlock(&rtc_lock);
187 wake_up_interruptible(&rtc_wait);
188 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
190 EXPORT_SYMBOL(rtc_update);
194 rtc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
196 DECLARE_WAITQUEUE(wait, current);
200 if (count < sizeof(unsigned long))
203 add_wait_queue(&rtc_wait, &wait);
205 __set_current_state(TASK_INTERRUPTIBLE);
207 spin_lock_irq(&rtc_lock);
210 spin_unlock_irq(&rtc_lock);
216 if (file->f_flags & O_NONBLOCK) {
220 if (signal_pending(current)) {
226 set_current_state(TASK_RUNNING);
227 remove_wait_queue(&rtc_wait, &wait);
230 ret = put_user(data, (unsigned long __user *)buf);
232 ret = sizeof(unsigned long);
237 static unsigned int rtc_poll(struct file *file, poll_table *wait)
241 poll_wait(file, &rtc_wait, wait);
243 spin_lock_irq(&rtc_lock);
245 spin_unlock_irq(&rtc_lock);
247 return data != 0 ? POLLIN | POLLRDNORM : 0;
250 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
253 struct rtc_ops *ops = file->private_data;
255 struct rtc_wkalrm alrm;
256 void __user *uarg = (void __user *)arg;
261 ret = rtc_read_alarm(ops, &alrm);
264 ret = copy_to_user(uarg, &alrm.time, sizeof(tm));
270 ret = copy_from_user(&alrm.time, uarg, sizeof(tm));
277 alrm.time.tm_mday = -1;
278 alrm.time.tm_mon = -1;
279 alrm.time.tm_year = -1;
280 alrm.time.tm_wday = -1;
281 alrm.time.tm_yday = -1;
282 alrm.time.tm_isdst = -1;
283 ret = rtc_set_alarm(ops, &alrm);
287 ret = rtc_read_time(ops, &tm);
290 ret = copy_to_user(uarg, &tm, sizeof(tm));
296 if (!capable(CAP_SYS_TIME)) {
300 ret = copy_from_user(&tm, uarg, sizeof(tm));
305 ret = rtc_set_time(ops, &tm);
311 * There were no RTC clocks before 1900.
317 if (!capable(CAP_SYS_TIME)) {
327 ret = put_user(rtc_epoch, (unsigned long __user *)uarg);
331 ret = copy_from_user(&alrm, uarg, sizeof(alrm));
336 ret = rtc_set_alarm(ops, &alrm);
340 ret = rtc_read_alarm(ops, &alrm);
343 ret = copy_to_user(uarg, &alrm, sizeof(alrm));
350 ret = ops->ioctl(cmd, arg);
356 static int rtc_open(struct inode *inode, struct file *file)
360 mutex_lock(&rtc_mutex);
364 } else if (!rtc_ops || !try_module_get(rtc_ops->owner)) {
367 file->private_data = rtc_ops;
369 ret = rtc_ops->open ? rtc_ops->open() : 0;
371 spin_lock_irq(&rtc_lock);
373 spin_unlock_irq(&rtc_lock);
378 mutex_unlock(&rtc_mutex);
383 static int rtc_release(struct inode *inode, struct file *file)
385 struct rtc_ops *ops = file->private_data;
390 spin_lock_irq(&rtc_lock);
392 spin_unlock_irq(&rtc_lock);
394 module_put(rtc_ops->owner);
400 static int rtc_fasync(int fd, struct file *file, int on)
402 return fasync_helper(fd, file, on, &rtc_async_queue);
405 static struct file_operations rtc_fops = {
406 .owner = THIS_MODULE,
412 .release = rtc_release,
413 .fasync = rtc_fasync,
416 static struct miscdevice rtc_miscdev = {
423 static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
425 struct rtc_ops *ops = data;
426 struct rtc_wkalrm alrm;
430 if (rtc_read_time(ops, &tm) == 0) {
432 "rtc_time\t: %02d:%02d:%02d\n"
433 "rtc_date\t: %04d-%02d-%02d\n"
434 "rtc_epoch\t: %04lu\n",
435 tm.tm_hour, tm.tm_min, tm.tm_sec,
436 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
440 if (rtc_read_alarm(ops, &alrm) == 0) {
441 p += sprintf(p, "alrm_time\t: ");
442 if ((unsigned int)alrm.time.tm_hour <= 24)
443 p += sprintf(p, "%02d:", alrm.time.tm_hour);
445 p += sprintf(p, "**:");
446 if ((unsigned int)alrm.time.tm_min <= 59)
447 p += sprintf(p, "%02d:", alrm.time.tm_min);
449 p += sprintf(p, "**:");
450 if ((unsigned int)alrm.time.tm_sec <= 59)
451 p += sprintf(p, "%02d\n", alrm.time.tm_sec);
453 p += sprintf(p, "**\n");
455 p += sprintf(p, "alrm_date\t: ");
456 if ((unsigned int)alrm.time.tm_year <= 200)
457 p += sprintf(p, "%04d-", alrm.time.tm_year + 1900);
459 p += sprintf(p, "****-");
460 if ((unsigned int)alrm.time.tm_mon <= 11)
461 p += sprintf(p, "%02d-", alrm.time.tm_mon + 1);
463 p += sprintf(p, "**-");
464 if ((unsigned int)alrm.time.tm_mday <= 31)
465 p += sprintf(p, "%02d\n", alrm.time.tm_mday);
467 p += sprintf(p, "**\n");
468 p += sprintf(p, "alrm_wakeup\t: %s\n",
469 alrm.enabled ? "yes" : "no");
470 p += sprintf(p, "alrm_pending\t: %s\n",
471 alrm.pending ? "yes" : "no");
480 int register_rtc(struct rtc_ops *ops)
484 mutex_lock(&rtc_mutex);
485 if (rtc_ops == NULL) {
488 ret = misc_register(&rtc_miscdev);
490 create_proc_read_entry("driver/rtc", 0, NULL,
493 mutex_unlock(&rtc_mutex);
497 EXPORT_SYMBOL(register_rtc);
499 void unregister_rtc(struct rtc_ops *rtc)
501 mutex_lock(&rtc_mutex);
502 if (rtc == rtc_ops) {
503 remove_proc_entry("driver/rtc", NULL);
504 misc_deregister(&rtc_miscdev);
507 mutex_unlock(&rtc_mutex);
509 EXPORT_SYMBOL(unregister_rtc);