Linux 2.6.25-rc4
[linux-2.6/next.git] / arch / arm / common / rtctime.c
blobf53bca46e23cadb5e35dd847173b5028a7ffa1c8
1 /*
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>
24 #include <asm/rtc.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
46 * Calculate the next alarm time given the requested alarm time mask
47 * and the current time.
49 void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm)
51 unsigned long next_time;
52 unsigned long now_time;
54 next->tm_year = now->tm_year;
55 next->tm_mon = now->tm_mon;
56 next->tm_mday = now->tm_mday;
57 next->tm_hour = alrm->tm_hour;
58 next->tm_min = alrm->tm_min;
59 next->tm_sec = alrm->tm_sec;
61 rtc_tm_to_time(now, &now_time);
62 rtc_tm_to_time(next, &next_time);
64 if (next_time < now_time) {
65 /* Advance one day */
66 next_time += 60 * 60 * 24;
67 rtc_time_to_tm(next_time, next);
70 EXPORT_SYMBOL(rtc_next_alarm_time);
72 static inline int rtc_arm_read_time(struct rtc_ops *ops, struct rtc_time *tm)
74 memset(tm, 0, sizeof(struct rtc_time));
75 return ops->read_time(tm);
78 static inline int rtc_arm_set_time(struct rtc_ops *ops, struct rtc_time *tm)
80 int ret;
82 ret = rtc_valid_tm(tm);
83 if (ret == 0)
84 ret = ops->set_time(tm);
86 return ret;
89 static inline int rtc_arm_read_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
91 int ret = -EINVAL;
92 if (ops->read_alarm) {
93 memset(alrm, 0, sizeof(struct rtc_wkalrm));
94 ret = ops->read_alarm(alrm);
96 return ret;
99 static inline int rtc_arm_set_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
101 int ret = -EINVAL;
102 if (ops->set_alarm)
103 ret = ops->set_alarm(alrm);
104 return ret;
107 void rtc_update(unsigned long num, unsigned long events)
109 spin_lock(&rtc_lock);
110 rtc_irq_data = (rtc_irq_data + (num << 8)) | events;
111 spin_unlock(&rtc_lock);
113 wake_up_interruptible(&rtc_wait);
114 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
116 EXPORT_SYMBOL(rtc_update);
119 static ssize_t
120 rtc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
122 DECLARE_WAITQUEUE(wait, current);
123 unsigned long data;
124 ssize_t ret;
126 if (count < sizeof(unsigned long))
127 return -EINVAL;
129 add_wait_queue(&rtc_wait, &wait);
130 do {
131 __set_current_state(TASK_INTERRUPTIBLE);
133 spin_lock_irq(&rtc_lock);
134 data = rtc_irq_data;
135 rtc_irq_data = 0;
136 spin_unlock_irq(&rtc_lock);
138 if (data != 0) {
139 ret = 0;
140 break;
142 if (file->f_flags & O_NONBLOCK) {
143 ret = -EAGAIN;
144 break;
146 if (signal_pending(current)) {
147 ret = -ERESTARTSYS;
148 break;
150 schedule();
151 } while (1);
152 set_current_state(TASK_RUNNING);
153 remove_wait_queue(&rtc_wait, &wait);
155 if (ret == 0) {
156 ret = put_user(data, (unsigned long __user *)buf);
157 if (ret == 0)
158 ret = sizeof(unsigned long);
160 return ret;
163 static unsigned int rtc_poll(struct file *file, poll_table *wait)
165 unsigned long data;
167 poll_wait(file, &rtc_wait, wait);
169 spin_lock_irq(&rtc_lock);
170 data = rtc_irq_data;
171 spin_unlock_irq(&rtc_lock);
173 return data != 0 ? POLLIN | POLLRDNORM : 0;
176 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
177 unsigned long arg)
179 struct rtc_ops *ops = file->private_data;
180 struct rtc_time tm;
181 struct rtc_wkalrm alrm;
182 void __user *uarg = (void __user *)arg;
183 int ret = -EINVAL;
185 switch (cmd) {
186 case RTC_ALM_READ:
187 ret = rtc_arm_read_alarm(ops, &alrm);
188 if (ret)
189 break;
190 ret = copy_to_user(uarg, &alrm.time, sizeof(tm));
191 if (ret)
192 ret = -EFAULT;
193 break;
195 case RTC_ALM_SET:
196 ret = copy_from_user(&alrm.time, uarg, sizeof(tm));
197 if (ret) {
198 ret = -EFAULT;
199 break;
201 alrm.enabled = 0;
202 alrm.pending = 0;
203 alrm.time.tm_mday = -1;
204 alrm.time.tm_mon = -1;
205 alrm.time.tm_year = -1;
206 alrm.time.tm_wday = -1;
207 alrm.time.tm_yday = -1;
208 alrm.time.tm_isdst = -1;
209 ret = rtc_arm_set_alarm(ops, &alrm);
210 break;
212 case RTC_RD_TIME:
213 ret = rtc_arm_read_time(ops, &tm);
214 if (ret)
215 break;
216 ret = copy_to_user(uarg, &tm, sizeof(tm));
217 if (ret)
218 ret = -EFAULT;
219 break;
221 case RTC_SET_TIME:
222 if (!capable(CAP_SYS_TIME)) {
223 ret = -EACCES;
224 break;
226 ret = copy_from_user(&tm, uarg, sizeof(tm));
227 if (ret) {
228 ret = -EFAULT;
229 break;
231 ret = rtc_arm_set_time(ops, &tm);
232 break;
234 case RTC_EPOCH_SET:
235 #ifndef rtc_epoch
237 * There were no RTC clocks before 1900.
239 if (arg < 1900) {
240 ret = -EINVAL;
241 break;
243 if (!capable(CAP_SYS_TIME)) {
244 ret = -EACCES;
245 break;
247 rtc_epoch = arg;
248 ret = 0;
249 #endif
250 break;
252 case RTC_EPOCH_READ:
253 ret = put_user(rtc_epoch, (unsigned long __user *)uarg);
254 break;
256 case RTC_WKALM_SET:
257 ret = copy_from_user(&alrm, uarg, sizeof(alrm));
258 if (ret) {
259 ret = -EFAULT;
260 break;
262 ret = rtc_arm_set_alarm(ops, &alrm);
263 break;
265 case RTC_WKALM_RD:
266 ret = rtc_arm_read_alarm(ops, &alrm);
267 if (ret)
268 break;
269 ret = copy_to_user(uarg, &alrm, sizeof(alrm));
270 if (ret)
271 ret = -EFAULT;
272 break;
274 default:
275 if (ops->ioctl)
276 ret = ops->ioctl(cmd, arg);
277 break;
279 return ret;
282 static int rtc_open(struct inode *inode, struct file *file)
284 int ret;
286 mutex_lock(&rtc_mutex);
288 if (rtc_inuse) {
289 ret = -EBUSY;
290 } else if (!rtc_ops || !try_module_get(rtc_ops->owner)) {
291 ret = -ENODEV;
292 } else {
293 file->private_data = rtc_ops;
295 ret = rtc_ops->open ? rtc_ops->open() : 0;
296 if (ret == 0) {
297 spin_lock_irq(&rtc_lock);
298 rtc_irq_data = 0;
299 spin_unlock_irq(&rtc_lock);
301 rtc_inuse = 1;
304 mutex_unlock(&rtc_mutex);
306 return ret;
309 static int rtc_release(struct inode *inode, struct file *file)
311 struct rtc_ops *ops = file->private_data;
313 if (ops->release)
314 ops->release();
316 spin_lock_irq(&rtc_lock);
317 rtc_irq_data = 0;
318 spin_unlock_irq(&rtc_lock);
320 module_put(rtc_ops->owner);
321 rtc_inuse = 0;
323 return 0;
326 static int rtc_fasync(int fd, struct file *file, int on)
328 return fasync_helper(fd, file, on, &rtc_async_queue);
331 static const struct file_operations rtc_fops = {
332 .owner = THIS_MODULE,
333 .llseek = no_llseek,
334 .read = rtc_read,
335 .poll = rtc_poll,
336 .ioctl = rtc_ioctl,
337 .open = rtc_open,
338 .release = rtc_release,
339 .fasync = rtc_fasync,
342 static struct miscdevice rtc_miscdev = {
343 .minor = RTC_MINOR,
344 .name = "rtc",
345 .fops = &rtc_fops,
349 static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
351 struct rtc_ops *ops = data;
352 struct rtc_wkalrm alrm;
353 struct rtc_time tm;
354 char *p = page;
356 if (rtc_arm_read_time(ops, &tm) == 0) {
357 p += sprintf(p,
358 "rtc_time\t: %02d:%02d:%02d\n"
359 "rtc_date\t: %04d-%02d-%02d\n"
360 "rtc_epoch\t: %04lu\n",
361 tm.tm_hour, tm.tm_min, tm.tm_sec,
362 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
363 rtc_epoch);
366 if (rtc_arm_read_alarm(ops, &alrm) == 0) {
367 p += sprintf(p, "alrm_time\t: ");
368 if ((unsigned int)alrm.time.tm_hour <= 24)
369 p += sprintf(p, "%02d:", alrm.time.tm_hour);
370 else
371 p += sprintf(p, "**:");
372 if ((unsigned int)alrm.time.tm_min <= 59)
373 p += sprintf(p, "%02d:", alrm.time.tm_min);
374 else
375 p += sprintf(p, "**:");
376 if ((unsigned int)alrm.time.tm_sec <= 59)
377 p += sprintf(p, "%02d\n", alrm.time.tm_sec);
378 else
379 p += sprintf(p, "**\n");
381 p += sprintf(p, "alrm_date\t: ");
382 if ((unsigned int)alrm.time.tm_year <= 200)
383 p += sprintf(p, "%04d-", alrm.time.tm_year + 1900);
384 else
385 p += sprintf(p, "****-");
386 if ((unsigned int)alrm.time.tm_mon <= 11)
387 p += sprintf(p, "%02d-", alrm.time.tm_mon + 1);
388 else
389 p += sprintf(p, "**-");
390 if ((unsigned int)alrm.time.tm_mday <= 31)
391 p += sprintf(p, "%02d\n", alrm.time.tm_mday);
392 else
393 p += sprintf(p, "**\n");
394 p += sprintf(p, "alrm_wakeup\t: %s\n",
395 alrm.enabled ? "yes" : "no");
396 p += sprintf(p, "alrm_pending\t: %s\n",
397 alrm.pending ? "yes" : "no");
400 if (ops->proc)
401 p += ops->proc(p);
403 return p - page;
406 int register_rtc(struct rtc_ops *ops)
408 int ret = -EBUSY;
410 mutex_lock(&rtc_mutex);
411 if (rtc_ops == NULL) {
412 rtc_ops = ops;
414 ret = misc_register(&rtc_miscdev);
415 if (ret == 0)
416 create_proc_read_entry("driver/rtc", 0, NULL,
417 rtc_read_proc, ops);
419 mutex_unlock(&rtc_mutex);
421 return ret;
423 EXPORT_SYMBOL(register_rtc);
425 void unregister_rtc(struct rtc_ops *rtc)
427 mutex_lock(&rtc_mutex);
428 if (rtc == rtc_ops) {
429 remove_proc_entry("driver/rtc", NULL);
430 misc_deregister(&rtc_miscdev);
431 rtc_ops = NULL;
433 mutex_unlock(&rtc_mutex);
435 EXPORT_SYMBOL(unregister_rtc);