blob: c397e71f938d0f79fbcbf29869fbb1d40cf09306 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/common/rtctime.c
3 *
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
7 *
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.
11 */
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/device.h>
21
22#include <asm/rtc.h>
23#include <asm/semaphore.h>
24
25static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
26static struct fasync_struct *rtc_async_queue;
27
28/*
29 * rtc_lock protects rtc_irq_data
30 */
31static DEFINE_SPINLOCK(rtc_lock);
32static unsigned long rtc_irq_data;
33
34/*
35 * rtc_sem protects rtc_inuse and rtc_ops
36 */
37static DECLARE_MUTEX(rtc_sem);
38static unsigned long rtc_inuse;
39static struct rtc_ops *rtc_ops;
40
41#define rtc_epoch 1900UL
42
43static const unsigned char days_in_month[] = {
44 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
45};
46
47#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
48#define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))
49
50static int month_days(unsigned int month, unsigned int year)
51{
52 return days_in_month[month] + (LEAP_YEAR(year) && month == 1);
53}
54
55/*
56 * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
57 */
58void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
59{
60 int days, month, year;
61
62 days = time / 86400;
63 time -= days * 86400;
64
65 tm->tm_wday = (days + 4) % 7;
66
67 year = 1970 + days / 365;
68 days -= (year - 1970) * 365
69 + LEAPS_THRU_END_OF(year - 1)
70 - LEAPS_THRU_END_OF(1970 - 1);
71 if (days < 0) {
72 year -= 1;
73 days += 365 + LEAP_YEAR(year);
74 }
75 tm->tm_year = year - 1900;
76 tm->tm_yday = days + 1;
77
78 for (month = 0; month < 11; month++) {
79 int newdays;
80
81 newdays = days - month_days(month, year);
82 if (newdays < 0)
83 break;
84 days = newdays;
85 }
86 tm->tm_mon = month;
87 tm->tm_mday = days + 1;
88
89 tm->tm_hour = time / 3600;
90 time -= tm->tm_hour * 3600;
91 tm->tm_min = time / 60;
92 tm->tm_sec = time - tm->tm_min * 60;
93}
94EXPORT_SYMBOL(rtc_time_to_tm);
95
96/*
97 * Does the rtc_time represent a valid date/time?
98 */
99int rtc_valid_tm(struct rtc_time *tm)
100{
101 if (tm->tm_year < 70 ||
102 tm->tm_mon >= 12 ||
103 tm->tm_mday < 1 ||
104 tm->tm_mday > month_days(tm->tm_mon, tm->tm_year + 1900) ||
105 tm->tm_hour >= 24 ||
106 tm->tm_min >= 60 ||
107 tm->tm_sec >= 60)
108 return -EINVAL;
109
110 return 0;
111}
112EXPORT_SYMBOL(rtc_valid_tm);
113
114/*
115 * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
116 */
117int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
118{
119 *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
120 tm->tm_hour, tm->tm_min, tm->tm_sec);
121
122 return 0;
123}
124EXPORT_SYMBOL(rtc_tm_to_time);
125
126/*
127 * Calculate the next alarm time given the requested alarm time mask
128 * and the current time.
129 *
130 * FIXME: for now, we just copy the alarm time because we're lazy (and
131 * is therefore buggy - setting a 10am alarm at 8pm will not result in
132 * the alarm triggering.)
133 */
134void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm)
135{
136 next->tm_year = now->tm_year;
137 next->tm_mon = now->tm_mon;
138 next->tm_mday = now->tm_mday;
139 next->tm_hour = alrm->tm_hour;
140 next->tm_min = alrm->tm_min;
141 next->tm_sec = alrm->tm_sec;
142}
143
144static inline void rtc_read_time(struct rtc_ops *ops, struct rtc_time *tm)
145{
146 memset(tm, 0, sizeof(struct rtc_time));
147 ops->read_time(tm);
148}
149
150static inline int rtc_set_time(struct rtc_ops *ops, struct rtc_time *tm)
151{
152 int ret;
153
154 ret = rtc_valid_tm(tm);
155 if (ret == 0)
156 ret = ops->set_time(tm);
157
158 return ret;
159}
160
161static inline int rtc_read_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
162{
163 int ret = -EINVAL;
164 if (ops->read_alarm) {
165 memset(alrm, 0, sizeof(struct rtc_wkalrm));
166 ops->read_alarm(alrm);
167 ret = 0;
168 }
169 return ret;
170}
171
172static inline int rtc_set_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
173{
174 int ret = -EINVAL;
175 if (ops->set_alarm)
176 ret = ops->set_alarm(alrm);
177 return ret;
178}
179
180void rtc_update(unsigned long num, unsigned long events)
181{
182 spin_lock(&rtc_lock);
183 rtc_irq_data = (rtc_irq_data + (num << 8)) | events;
184 spin_unlock(&rtc_lock);
185
186 wake_up_interruptible(&rtc_wait);
187 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
188}
189EXPORT_SYMBOL(rtc_update);
190
191
192static ssize_t
193rtc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
194{
195 DECLARE_WAITQUEUE(wait, current);
196 unsigned long data;
197 ssize_t ret;
198
199 if (count < sizeof(unsigned long))
200 return -EINVAL;
201
202 add_wait_queue(&rtc_wait, &wait);
203 do {
204 __set_current_state(TASK_INTERRUPTIBLE);
205
206 spin_lock_irq(&rtc_lock);
207 data = rtc_irq_data;
208 rtc_irq_data = 0;
209 spin_unlock_irq(&rtc_lock);
210
211 if (data != 0) {
212 ret = 0;
213 break;
214 }
215 if (file->f_flags & O_NONBLOCK) {
216 ret = -EAGAIN;
217 break;
218 }
219 if (signal_pending(current)) {
220 ret = -ERESTARTSYS;
221 break;
222 }
223 schedule();
224 } while (1);
225 set_current_state(TASK_RUNNING);
226 remove_wait_queue(&rtc_wait, &wait);
227
228 if (ret == 0) {
229 ret = put_user(data, (unsigned long __user *)buf);
230 if (ret == 0)
231 ret = sizeof(unsigned long);
232 }
233 return ret;
234}
235
236static unsigned int rtc_poll(struct file *file, poll_table *wait)
237{
238 unsigned long data;
239
240 poll_wait(file, &rtc_wait, wait);
241
242 spin_lock_irq(&rtc_lock);
243 data = rtc_irq_data;
244 spin_unlock_irq(&rtc_lock);
245
246 return data != 0 ? POLLIN | POLLRDNORM : 0;
247}
248
249static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
250 unsigned long arg)
251{
252 struct rtc_ops *ops = file->private_data;
253 struct rtc_time tm;
254 struct rtc_wkalrm alrm;
255 void __user *uarg = (void __user *)arg;
256 int ret = -EINVAL;
257
258 switch (cmd) {
259 case RTC_ALM_READ:
260 ret = rtc_read_alarm(ops, &alrm);
261 if (ret)
262 break;
263 ret = copy_to_user(uarg, &alrm.time, sizeof(tm));
264 if (ret)
265 ret = -EFAULT;
266 break;
267
268 case RTC_ALM_SET:
269 ret = copy_from_user(&alrm.time, uarg, sizeof(tm));
270 if (ret) {
271 ret = -EFAULT;
272 break;
273 }
274 alrm.enabled = 0;
275 alrm.pending = 0;
276 alrm.time.tm_mday = -1;
277 alrm.time.tm_mon = -1;
278 alrm.time.tm_year = -1;
279 alrm.time.tm_wday = -1;
280 alrm.time.tm_yday = -1;
281 alrm.time.tm_isdst = -1;
282 ret = rtc_set_alarm(ops, &alrm);
283 break;
284
285 case RTC_RD_TIME:
286 rtc_read_time(ops, &tm);
287 ret = copy_to_user(uarg, &tm, sizeof(tm));
288 if (ret)
289 ret = -EFAULT;
290 break;
291
292 case RTC_SET_TIME:
293 if (!capable(CAP_SYS_TIME)) {
294 ret = -EACCES;
295 break;
296 }
297 ret = copy_from_user(&tm, uarg, sizeof(tm));
298 if (ret) {
299 ret = -EFAULT;
300 break;
301 }
302 ret = rtc_set_time(ops, &tm);
303 break;
304
305 case RTC_EPOCH_SET:
306#ifndef rtc_epoch
307 /*
308 * There were no RTC clocks before 1900.
309 */
310 if (arg < 1900) {
311 ret = -EINVAL;
312 break;
313 }
314 if (!capable(CAP_SYS_TIME)) {
315 ret = -EACCES;
316 break;
317 }
318 rtc_epoch = arg;
319 ret = 0;
320#endif
321 break;
322
323 case RTC_EPOCH_READ:
324 ret = put_user(rtc_epoch, (unsigned long __user *)uarg);
325 break;
326
327 case RTC_WKALM_SET:
328 ret = copy_from_user(&alrm, uarg, sizeof(alrm));
329 if (ret) {
330 ret = -EFAULT;
331 break;
332 }
333 ret = rtc_set_alarm(ops, &alrm);
334 break;
335
336 case RTC_WKALM_RD:
337 ret = rtc_read_alarm(ops, &alrm);
338 if (ret)
339 break;
340 ret = copy_to_user(uarg, &alrm, sizeof(alrm));
341 if (ret)
342 ret = -EFAULT;
343 break;
344
345 default:
346 if (ops->ioctl)
347 ret = ops->ioctl(cmd, arg);
348 break;
349 }
350 return ret;
351}
352
353static int rtc_open(struct inode *inode, struct file *file)
354{
355 int ret;
356
357 down(&rtc_sem);
358
359 if (rtc_inuse) {
360 ret = -EBUSY;
361 } else if (!rtc_ops || !try_module_get(rtc_ops->owner)) {
362 ret = -ENODEV;
363 } else {
364 file->private_data = rtc_ops;
365
366 ret = rtc_ops->open ? rtc_ops->open() : 0;
367 if (ret == 0) {
368 spin_lock_irq(&rtc_lock);
369 rtc_irq_data = 0;
370 spin_unlock_irq(&rtc_lock);
371
372 rtc_inuse = 1;
373 }
374 }
375 up(&rtc_sem);
376
377 return ret;
378}
379
380static int rtc_release(struct inode *inode, struct file *file)
381{
382 struct rtc_ops *ops = file->private_data;
383
384 if (ops->release)
385 ops->release();
386
387 spin_lock_irq(&rtc_lock);
388 rtc_irq_data = 0;
389 spin_unlock_irq(&rtc_lock);
390
391 module_put(rtc_ops->owner);
392 rtc_inuse = 0;
393
394 return 0;
395}
396
397static int rtc_fasync(int fd, struct file *file, int on)
398{
399 return fasync_helper(fd, file, on, &rtc_async_queue);
400}
401
402static struct file_operations rtc_fops = {
403 .owner = THIS_MODULE,
404 .llseek = no_llseek,
405 .read = rtc_read,
406 .poll = rtc_poll,
407 .ioctl = rtc_ioctl,
408 .open = rtc_open,
409 .release = rtc_release,
410 .fasync = rtc_fasync,
411};
412
413static struct miscdevice rtc_miscdev = {
414 .minor = RTC_MINOR,
415 .name = "rtc",
416 .fops = &rtc_fops,
417};
418
419
420static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
421{
422 struct rtc_ops *ops = data;
423 struct rtc_wkalrm alrm;
424 struct rtc_time tm;
425 char *p = page;
426
427 rtc_read_time(ops, &tm);
428
429 p += sprintf(p,
430 "rtc_time\t: %02d:%02d:%02d\n"
431 "rtc_date\t: %04d-%02d-%02d\n"
432 "rtc_epoch\t: %04lu\n",
433 tm.tm_hour, tm.tm_min, tm.tm_sec,
434 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
435 rtc_epoch);
436
437 if (rtc_read_alarm(ops, &alrm) == 0) {
438 p += sprintf(p, "alrm_time\t: ");
439 if ((unsigned int)alrm.time.tm_hour <= 24)
440 p += sprintf(p, "%02d:", alrm.time.tm_hour);
441 else
442 p += sprintf(p, "**:");
443 if ((unsigned int)alrm.time.tm_min <= 59)
444 p += sprintf(p, "%02d:", alrm.time.tm_min);
445 else
446 p += sprintf(p, "**:");
447 if ((unsigned int)alrm.time.tm_sec <= 59)
448 p += sprintf(p, "%02d\n", alrm.time.tm_sec);
449 else
450 p += sprintf(p, "**\n");
451
452 p += sprintf(p, "alrm_date\t: ");
453 if ((unsigned int)alrm.time.tm_year <= 200)
454 p += sprintf(p, "%04d-", alrm.time.tm_year + 1900);
455 else
456 p += sprintf(p, "****-");
457 if ((unsigned int)alrm.time.tm_mon <= 11)
458 p += sprintf(p, "%02d-", alrm.time.tm_mon + 1);
459 else
460 p += sprintf(p, "**-");
461 if ((unsigned int)alrm.time.tm_mday <= 31)
462 p += sprintf(p, "%02d\n", alrm.time.tm_mday);
463 else
464 p += sprintf(p, "**\n");
465 p += sprintf(p, "alrm_wakeup\t: %s\n",
466 alrm.enabled ? "yes" : "no");
467 p += sprintf(p, "alrm_pending\t: %s\n",
468 alrm.pending ? "yes" : "no");
469 }
470
471 if (ops->proc)
472 p += ops->proc(p);
473
474 return p - page;
475}
476
477int register_rtc(struct rtc_ops *ops)
478{
479 int ret = -EBUSY;
480
481 down(&rtc_sem);
482 if (rtc_ops == NULL) {
483 rtc_ops = ops;
484
485 ret = misc_register(&rtc_miscdev);
486 if (ret == 0)
487 create_proc_read_entry("driver/rtc", 0, NULL,
488 rtc_read_proc, ops);
489 }
490 up(&rtc_sem);
491
492 return ret;
493}
494EXPORT_SYMBOL(register_rtc);
495
496void unregister_rtc(struct rtc_ops *rtc)
497{
498 down(&rtc_sem);
499 if (rtc == rtc_ops) {
500 remove_proc_entry("driver/rtc", NULL);
501 misc_deregister(&rtc_miscdev);
502 rtc_ops = NULL;
503 }
504 up(&rtc_sem);
505}
506EXPORT_SYMBOL(unregister_rtc);