Pull ia64-mutex-primitives into release branch
[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 void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm)
133 {
134         unsigned long next_time;
135         unsigned long now_time;
136
137         next->tm_year = now->tm_year;
138         next->tm_mon = now->tm_mon;
139         next->tm_mday = now->tm_mday;
140         next->tm_hour = alrm->tm_hour;
141         next->tm_min = alrm->tm_min;
142         next->tm_sec = alrm->tm_sec;
143
144         rtc_tm_to_time(now, &now_time);
145         rtc_tm_to_time(next, &next_time);
146
147         if (next_time < now_time) {
148                 /* Advance one day */
149                 next_time += 60 * 60 * 24;
150                 rtc_time_to_tm(next_time, next);
151         }
152 }
153
154 static inline int rtc_read_time(struct rtc_ops *ops, struct rtc_time *tm)
155 {
156         memset(tm, 0, sizeof(struct rtc_time));
157         return ops->read_time(tm);
158 }
159
160 static inline int rtc_set_time(struct rtc_ops *ops, struct rtc_time *tm)
161 {
162         int ret;
163
164         ret = rtc_valid_tm(tm);
165         if (ret == 0)
166                 ret = ops->set_time(tm);
167
168         return ret;
169 }
170
171 static inline int rtc_read_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
172 {
173         int ret = -EINVAL;
174         if (ops->read_alarm) {
175                 memset(alrm, 0, sizeof(struct rtc_wkalrm));
176                 ret = ops->read_alarm(alrm);
177         }
178         return ret;
179 }
180
181 static inline int rtc_set_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
182 {
183         int ret = -EINVAL;
184         if (ops->set_alarm)
185                 ret = ops->set_alarm(alrm);
186         return ret;
187 }
188
189 void rtc_update(unsigned long num, unsigned long events)
190 {
191         spin_lock(&rtc_lock);
192         rtc_irq_data = (rtc_irq_data + (num << 8)) | events;
193         spin_unlock(&rtc_lock);
194
195         wake_up_interruptible(&rtc_wait);
196         kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
197 }
198 EXPORT_SYMBOL(rtc_update);
199
200
201 static ssize_t
202 rtc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
203 {
204         DECLARE_WAITQUEUE(wait, current);
205         unsigned long data;
206         ssize_t ret;
207
208         if (count < sizeof(unsigned long))
209                 return -EINVAL;
210
211         add_wait_queue(&rtc_wait, &wait);
212         do {
213                 __set_current_state(TASK_INTERRUPTIBLE);
214
215                 spin_lock_irq(&rtc_lock);
216                 data = rtc_irq_data;
217                 rtc_irq_data = 0;
218                 spin_unlock_irq(&rtc_lock);
219
220                 if (data != 0) {
221                         ret = 0;
222                         break;
223                 }
224                 if (file->f_flags & O_NONBLOCK) {
225                         ret = -EAGAIN;
226                         break;
227                 }
228                 if (signal_pending(current)) {
229                         ret = -ERESTARTSYS;
230                         break;
231                 }
232                 schedule();
233         } while (1);
234         set_current_state(TASK_RUNNING);
235         remove_wait_queue(&rtc_wait, &wait);
236
237         if (ret == 0) {
238                 ret = put_user(data, (unsigned long __user *)buf);
239                 if (ret == 0)
240                         ret = sizeof(unsigned long);
241         }
242         return ret;
243 }
244
245 static unsigned int rtc_poll(struct file *file, poll_table *wait)
246 {
247         unsigned long data;
248
249         poll_wait(file, &rtc_wait, wait);
250
251         spin_lock_irq(&rtc_lock);
252         data = rtc_irq_data;
253         spin_unlock_irq(&rtc_lock);
254
255         return data != 0 ? POLLIN | POLLRDNORM : 0;
256 }
257
258 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
259                      unsigned long arg)
260 {
261         struct rtc_ops *ops = file->private_data;
262         struct rtc_time tm;
263         struct rtc_wkalrm alrm;
264         void __user *uarg = (void __user *)arg;
265         int ret = -EINVAL;
266
267         switch (cmd) {
268         case RTC_ALM_READ:
269                 ret = rtc_read_alarm(ops, &alrm);
270                 if (ret)
271                         break;
272                 ret = copy_to_user(uarg, &alrm.time, sizeof(tm));
273                 if (ret)
274                         ret = -EFAULT;
275                 break;
276
277         case RTC_ALM_SET:
278                 ret = copy_from_user(&alrm.time, uarg, sizeof(tm));
279                 if (ret) {
280                         ret = -EFAULT;
281                         break;
282                 }
283                 alrm.enabled = 0;
284                 alrm.pending = 0;
285                 alrm.time.tm_mday = -1;
286                 alrm.time.tm_mon = -1;
287                 alrm.time.tm_year = -1;
288                 alrm.time.tm_wday = -1;
289                 alrm.time.tm_yday = -1;
290                 alrm.time.tm_isdst = -1;
291                 ret = rtc_set_alarm(ops, &alrm);
292                 break;
293
294         case RTC_RD_TIME:
295                 ret = rtc_read_time(ops, &tm);
296                 if (ret)
297                         break;
298                 ret = copy_to_user(uarg, &tm, sizeof(tm));
299                 if (ret)
300                         ret = -EFAULT;
301                 break;
302
303         case RTC_SET_TIME:
304                 if (!capable(CAP_SYS_TIME)) {
305                         ret = -EACCES;
306                         break;
307                 }
308                 ret = copy_from_user(&tm, uarg, sizeof(tm));
309                 if (ret) {
310                         ret = -EFAULT;
311                         break;
312                 }
313                 ret = rtc_set_time(ops, &tm);
314                 break;
315
316         case RTC_EPOCH_SET:
317 #ifndef rtc_epoch
318                 /*
319                  * There were no RTC clocks before 1900.
320                  */
321                 if (arg < 1900) {
322                         ret = -EINVAL;
323                         break;
324                 }
325                 if (!capable(CAP_SYS_TIME)) {
326                         ret = -EACCES;
327                         break;
328                 }
329                 rtc_epoch = arg;
330                 ret = 0;
331 #endif
332                 break;
333
334         case RTC_EPOCH_READ:
335                 ret = put_user(rtc_epoch, (unsigned long __user *)uarg);
336                 break;
337
338         case RTC_WKALM_SET:
339                 ret = copy_from_user(&alrm, uarg, sizeof(alrm));
340                 if (ret) {
341                         ret = -EFAULT;
342                         break;
343                 }
344                 ret = rtc_set_alarm(ops, &alrm);
345                 break;
346
347         case RTC_WKALM_RD:
348                 ret = rtc_read_alarm(ops, &alrm);
349                 if (ret)
350                         break;
351                 ret = copy_to_user(uarg, &alrm, sizeof(alrm));
352                 if (ret)
353                         ret = -EFAULT;
354                 break;
355
356         default:
357                 if (ops->ioctl)
358                         ret = ops->ioctl(cmd, arg);
359                 break;
360         }
361         return ret;
362 }
363
364 static int rtc_open(struct inode *inode, struct file *file)
365 {
366         int ret;
367
368         mutex_lock(&rtc_mutex);
369
370         if (rtc_inuse) {
371                 ret = -EBUSY;
372         } else if (!rtc_ops || !try_module_get(rtc_ops->owner)) {
373                 ret = -ENODEV;
374         } else {
375                 file->private_data = rtc_ops;
376
377                 ret = rtc_ops->open ? rtc_ops->open() : 0;
378                 if (ret == 0) {
379                         spin_lock_irq(&rtc_lock);
380                         rtc_irq_data = 0;
381                         spin_unlock_irq(&rtc_lock);
382
383                         rtc_inuse = 1;
384                 }
385         }
386         mutex_unlock(&rtc_mutex);
387
388         return ret;
389 }
390
391 static int rtc_release(struct inode *inode, struct file *file)
392 {
393         struct rtc_ops *ops = file->private_data;
394
395         if (ops->release)
396                 ops->release();
397
398         spin_lock_irq(&rtc_lock);
399         rtc_irq_data = 0;
400         spin_unlock_irq(&rtc_lock);
401
402         module_put(rtc_ops->owner);
403         rtc_inuse = 0;
404
405         return 0;
406 }
407
408 static int rtc_fasync(int fd, struct file *file, int on)
409 {
410         return fasync_helper(fd, file, on, &rtc_async_queue);
411 }
412
413 static struct file_operations rtc_fops = {
414         .owner          = THIS_MODULE,
415         .llseek         = no_llseek,
416         .read           = rtc_read,
417         .poll           = rtc_poll,
418         .ioctl          = rtc_ioctl,
419         .open           = rtc_open,
420         .release        = rtc_release,
421         .fasync         = rtc_fasync,
422 };
423
424 static struct miscdevice rtc_miscdev = {
425         .minor          = RTC_MINOR,
426         .name           = "rtc",
427         .fops           = &rtc_fops,
428 };
429
430
431 static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
432 {
433         struct rtc_ops *ops = data;
434         struct rtc_wkalrm alrm;
435         struct rtc_time tm;
436         char *p = page;
437
438         if (rtc_read_time(ops, &tm) == 0) {
439                 p += sprintf(p,
440                         "rtc_time\t: %02d:%02d:%02d\n"
441                         "rtc_date\t: %04d-%02d-%02d\n"
442                         "rtc_epoch\t: %04lu\n",
443                         tm.tm_hour, tm.tm_min, tm.tm_sec,
444                         tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
445                         rtc_epoch);
446         }
447
448         if (rtc_read_alarm(ops, &alrm) == 0) {
449                 p += sprintf(p, "alrm_time\t: ");
450                 if ((unsigned int)alrm.time.tm_hour <= 24)
451                         p += sprintf(p, "%02d:", alrm.time.tm_hour);
452                 else
453                         p += sprintf(p, "**:");
454                 if ((unsigned int)alrm.time.tm_min <= 59)
455                         p += sprintf(p, "%02d:", alrm.time.tm_min);
456                 else
457                         p += sprintf(p, "**:");
458                 if ((unsigned int)alrm.time.tm_sec <= 59)
459                         p += sprintf(p, "%02d\n", alrm.time.tm_sec);
460                 else
461                         p += sprintf(p, "**\n");
462
463                 p += sprintf(p, "alrm_date\t: ");
464                 if ((unsigned int)alrm.time.tm_year <= 200)
465                         p += sprintf(p, "%04d-", alrm.time.tm_year + 1900);
466                 else
467                         p += sprintf(p, "****-");
468                 if ((unsigned int)alrm.time.tm_mon <= 11)
469                         p += sprintf(p, "%02d-", alrm.time.tm_mon + 1);
470                 else
471                         p += sprintf(p, "**-");
472                 if ((unsigned int)alrm.time.tm_mday <= 31)
473                         p += sprintf(p, "%02d\n", alrm.time.tm_mday);
474                 else
475                         p += sprintf(p, "**\n");
476                 p += sprintf(p, "alrm_wakeup\t: %s\n",
477                              alrm.enabled ? "yes" : "no");
478                 p += sprintf(p, "alrm_pending\t: %s\n",
479                              alrm.pending ? "yes" : "no");
480         }
481
482         if (ops->proc)
483                 p += ops->proc(p);
484
485         return p - page;
486 }
487
488 int register_rtc(struct rtc_ops *ops)
489 {
490         int ret = -EBUSY;
491
492         mutex_lock(&rtc_mutex);
493         if (rtc_ops == NULL) {
494                 rtc_ops = ops;
495
496                 ret = misc_register(&rtc_miscdev);
497                 if (ret == 0)
498                         create_proc_read_entry("driver/rtc", 0, NULL,
499                                                rtc_read_proc, ops);
500         }
501         mutex_unlock(&rtc_mutex);
502
503         return ret;
504 }
505 EXPORT_SYMBOL(register_rtc);
506
507 void unregister_rtc(struct rtc_ops *rtc)
508 {
509         mutex_lock(&rtc_mutex);
510         if (rtc == rtc_ops) {
511                 remove_proc_entry("driver/rtc", NULL);
512                 misc_deregister(&rtc_miscdev);
513                 rtc_ops = NULL;
514         }
515         mutex_unlock(&rtc_mutex);
516 }
517 EXPORT_SYMBOL(unregister_rtc);