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