blob: aac0985a572b86811bae33317d7d4176e924d317 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Real Time Clock interface for
3 * - q40 and other m68k machines,
4 * - HP PARISC machines
5 * - PowerPC machines
6 * emulate some RTC irq capabilities in software
7 *
8 * Copyright (C) 1999 Richard Zidlicky
9 *
10 * based on Paul Gortmaker's rtc.c device and
11 * Sam Creasey Generic rtc driver
12 *
13 * This driver allows use of the real time clock (built into
14 * nearly all computers) from user space. It exports the /dev/rtc
Robert P. J. Dayc685ce02007-05-09 07:21:11 +020015 * interface supporting various ioctl() and also the /proc/driver/rtc
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * pseudo-file for status information.
17 *
18 * The ioctls can be used to set the interrupt behaviour where
19 * supported.
20 *
21 * The /dev/rtc interface will block on reads until an interrupt
22 * has been received. If a RTC interrupt has already happened,
23 * it will output an unsigned long and then block. The output value
24 * contains the interrupt status in the low byte and the number of
25 * interrupts since the last read in the remaining high bytes. The
26 * /dev/rtc interface can also be used with the select(2) call.
27 *
28 * This program is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU General Public License
30 * as published by the Free Software Foundation; either version
31 * 2 of the License, or (at your option) any later version.
32 *
33
34 * 1.01 fix for 2.3.X rz@linux-m68k.org
35 * 1.02 merged with code from genrtc.c rz@linux-m68k.org
36 * 1.03 make it more portable zippel@linux-m68k.org
37 * 1.04 removed useless timer code rz@linux-m68k.org
38 * 1.05 portable RTC_UIE emulation rz@linux-m68k.org
39 * 1.06 set_rtc_time can return an error trini@kernel.crashing.org
40 * 1.07 ported to HP PARISC (hppa) Helge Deller <deller@gmx.de>
41 */
42
43#define RTC_VERSION "1.07"
44
45#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/errno.h>
47#include <linux/miscdevice.h>
48#include <linux/fcntl.h>
49
50#include <linux/rtc.h>
51#include <linux/init.h>
52#include <linux/poll.h>
53#include <linux/proc_fs.h>
Arnd Bergmann742a2fe2008-05-20 19:15:55 +020054#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/workqueue.h>
56
57#include <asm/uaccess.h>
58#include <asm/system.h>
59#include <asm/rtc.h>
60
61/*
62 * We sponge a minor off of the misc major. No need slurping
63 * up another valuable major dev number for this. If you add
64 * an ioctl, make sure you don't conflict with SPARC's RTC
65 * ioctls.
66 */
67
68static DECLARE_WAIT_QUEUE_HEAD(gen_rtc_wait);
69
70/*
71 * Bits in gen_rtc_status.
72 */
73
74#define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
75
76static unsigned char gen_rtc_status; /* bitmapped status byte. */
77static unsigned long gen_rtc_irq_data; /* our output to the world */
78
79/* months start at 0 now */
80static unsigned char days_in_mo[] =
81{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
82
83static int irq_active;
84
85#ifdef CONFIG_GEN_RTC_X
86static struct work_struct genrtc_task;
87static struct timer_list timer_task;
88
89static unsigned int oldsecs;
90static int lostint;
91static unsigned long tt_exp;
92
93static void gen_rtc_timer(unsigned long data);
94
95static volatile int stask_active; /* schedule_work */
96static volatile int ttask_active; /* timer_task */
97static int stop_rtc_timers; /* don't requeue tasks */
98static DEFINE_SPINLOCK(gen_rtc_lock);
99
100static void gen_rtc_interrupt(unsigned long arg);
101
102/*
103 * Routine to poll RTC seconds field for change as often as possible,
104 * after first RTC_UIE use timer to reduce polling
105 */
David Howellsc4028952006-11-22 14:57:56 +0000106static void genrtc_troutine(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 unsigned int tmp = get_rtc_ss();
109
110 if (stop_rtc_timers) {
111 stask_active = 0;
112 return;
113 }
114
115 if (oldsecs != tmp){
116 oldsecs = tmp;
117
118 timer_task.function = gen_rtc_timer;
119 timer_task.expires = jiffies + HZ - (HZ/10);
120 tt_exp=timer_task.expires;
121 ttask_active=1;
122 stask_active=0;
123 add_timer(&timer_task);
124
125 gen_rtc_interrupt(0);
126 } else if (schedule_work(&genrtc_task) == 0)
127 stask_active = 0;
128}
129
130static void gen_rtc_timer(unsigned long data)
131{
132 lostint = get_rtc_ss() - oldsecs ;
133 if (lostint<0)
134 lostint = 60 - lostint;
135 if (time_after(jiffies, tt_exp))
136 printk(KERN_INFO "genrtc: timer task delayed by %ld jiffies\n",
137 jiffies-tt_exp);
138 ttask_active=0;
139 stask_active=1;
140 if ((schedule_work(&genrtc_task) == 0))
141 stask_active = 0;
142}
143
144/*
145 * call gen_rtc_interrupt function to signal an RTC_UIE,
146 * arg is unused.
147 * Could be invoked either from a real interrupt handler or
148 * from some routine that periodically (eg 100HZ) monitors
149 * whether RTC_SECS changed
150 */
151static void gen_rtc_interrupt(unsigned long arg)
152{
153 /* We store the status in the low byte and the number of
154 * interrupts received since the last read in the remainder
155 * of rtc_irq_data. */
156
157 gen_rtc_irq_data += 0x100;
158 gen_rtc_irq_data &= ~0xff;
159 gen_rtc_irq_data |= RTC_UIE;
160
161 if (lostint){
162 printk("genrtc: system delaying clock ticks?\n");
163 /* increment count so that userspace knows something is wrong */
164 gen_rtc_irq_data += ((lostint-1)<<8);
165 lostint = 0;
166 }
167
168 wake_up_interruptible(&gen_rtc_wait);
169}
170
171/*
172 * Now all the various file operations that we export.
173 */
174static ssize_t gen_rtc_read(struct file *file, char __user *buf,
175 size_t count, loff_t *ppos)
176{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned long data;
178 ssize_t retval;
179
180 if (count != sizeof (unsigned int) && count != sizeof (unsigned long))
181 return -EINVAL;
182
183 if (file->f_flags & O_NONBLOCK && !gen_rtc_irq_data)
184 return -EAGAIN;
185
Jiri Slabye0955e12007-07-15 23:40:19 -0700186 retval = wait_event_interruptible(gen_rtc_wait,
187 (data = xchg(&gen_rtc_irq_data, 0)));
188 if (retval)
189 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /* first test allows optimizer to nuke this case for 32-bit machines */
192 if (sizeof (int) != sizeof (long) && count == sizeof (unsigned int)) {
193 unsigned int uidata = data;
Atsushi Nemotof3537ea2006-05-01 12:16:17 -0700194 retval = put_user(uidata, (unsigned int __user *)buf) ?:
195 sizeof(unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197 else {
Atsushi Nemotof3537ea2006-05-01 12:16:17 -0700198 retval = put_user(data, (unsigned long __user *)buf) ?:
199 sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Jiri Slabye0955e12007-07-15 23:40:19 -0700201out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return retval;
203}
204
205static unsigned int gen_rtc_poll(struct file *file,
206 struct poll_table_struct *wait)
207{
208 poll_wait(file, &gen_rtc_wait, wait);
209 if (gen_rtc_irq_data != 0)
210 return POLLIN | POLLRDNORM;
211 return 0;
212}
213
214#endif
215
216/*
217 * Used to disable/enable interrupts, only RTC_UIE supported
218 * We also clear out any old irq data after an ioctl() that
219 * meddles with the interrupt enable/disable bits.
220 */
221
222static inline void gen_clear_rtc_irq_bit(unsigned char bit)
223{
224#ifdef CONFIG_GEN_RTC_X
225 stop_rtc_timers = 1;
226 if (ttask_active){
227 del_timer_sync(&timer_task);
228 ttask_active = 0;
229 }
230 while (stask_active)
231 schedule();
232
233 spin_lock(&gen_rtc_lock);
234 irq_active = 0;
235 spin_unlock(&gen_rtc_lock);
236#endif
237}
238
239static inline int gen_set_rtc_irq_bit(unsigned char bit)
240{
241#ifdef CONFIG_GEN_RTC_X
242 spin_lock(&gen_rtc_lock);
243 if ( !irq_active ) {
244 irq_active = 1;
245 stop_rtc_timers = 0;
246 lostint = 0;
David Howellsc4028952006-11-22 14:57:56 +0000247 INIT_WORK(&genrtc_task, genrtc_troutine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 oldsecs = get_rtc_ss();
249 init_timer(&timer_task);
250
251 stask_active = 1;
252 if (schedule_work(&genrtc_task) == 0){
253 stask_active = 0;
254 }
255 }
256 spin_unlock(&gen_rtc_lock);
257 gen_rtc_irq_data = 0;
258 return 0;
259#else
260 return -EINVAL;
261#endif
262}
263
264static int gen_rtc_ioctl(struct inode *inode, struct file *file,
265 unsigned int cmd, unsigned long arg)
266{
267 struct rtc_time wtime;
268 struct rtc_pll_info pll;
269 void __user *argp = (void __user *)arg;
270
271 switch (cmd) {
272
273 case RTC_PLL_GET:
274 if (get_rtc_pll(&pll))
275 return -EINVAL;
276 else
277 return copy_to_user(argp, &pll, sizeof pll) ? -EFAULT : 0;
278
279 case RTC_PLL_SET:
280 if (!capable(CAP_SYS_TIME))
281 return -EACCES;
282 if (copy_from_user(&pll, argp, sizeof(pll)))
283 return -EFAULT;
284 return set_rtc_pll(&pll);
285
286 case RTC_UIE_OFF: /* disable ints from RTC updates. */
287 gen_clear_rtc_irq_bit(RTC_UIE);
288 return 0;
289
290 case RTC_UIE_ON: /* enable ints for RTC updates. */
291 return gen_set_rtc_irq_bit(RTC_UIE);
292
293 case RTC_RD_TIME: /* Read the time/date from RTC */
294 /* this doesn't get week-day, who cares */
295 memset(&wtime, 0, sizeof(wtime));
296 get_rtc_time(&wtime);
297
298 return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
299
300 case RTC_SET_TIME: /* Set the RTC */
301 {
302 int year;
303 unsigned char leap_yr;
304
305 if (!capable(CAP_SYS_TIME))
306 return -EACCES;
307
308 if (copy_from_user(&wtime, argp, sizeof(wtime)))
309 return -EFAULT;
310
311 year = wtime.tm_year + 1900;
312 leap_yr = ((!(year % 4) && (year % 100)) ||
313 !(year % 400));
314
315 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
316 return -EINVAL;
317
318 if (wtime.tm_mday < 0 || wtime.tm_mday >
319 (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
320 return -EINVAL;
321
322 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
323 wtime.tm_min < 0 || wtime.tm_min >= 60 ||
324 wtime.tm_sec < 0 || wtime.tm_sec >= 60)
325 return -EINVAL;
326
327 return set_rtc_time(&wtime);
328 }
329 }
330
331 return -EINVAL;
332}
333
334/*
335 * We enforce only one user at a time here with the open/close.
336 * Also clear the previous interrupt data on an open, and clean
337 * up things on a close.
338 */
339
340static int gen_rtc_open(struct inode *inode, struct file *file)
341{
Arnd Bergmann742a2fe2008-05-20 19:15:55 +0200342 lock_kernel();
343 if (gen_rtc_status & RTC_IS_OPEN) {
344 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return -EBUSY;
Arnd Bergmann742a2fe2008-05-20 19:15:55 +0200346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 gen_rtc_status |= RTC_IS_OPEN;
349 gen_rtc_irq_data = 0;
350 irq_active = 0;
Arnd Bergmann742a2fe2008-05-20 19:15:55 +0200351 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 return 0;
354}
355
356static int gen_rtc_release(struct inode *inode, struct file *file)
357{
358 /*
359 * Turn off all interrupts once the device is no longer
360 * in use and clear the data.
361 */
362
363 gen_clear_rtc_irq_bit(RTC_PIE|RTC_AIE|RTC_UIE);
364
365 gen_rtc_status &= ~RTC_IS_OPEN;
366 return 0;
367}
368
369
370#ifdef CONFIG_PROC_FS
371
372/*
Robert P. J. Dayc685ce02007-05-09 07:21:11 +0200373 * Info exported via "/proc/driver/rtc".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 */
375
376static int gen_rtc_proc_output(char *buf)
377{
378 char *p;
379 struct rtc_time tm;
380 unsigned int flags;
381 struct rtc_pll_info pll;
382
383 p = buf;
384
385 flags = get_rtc_time(&tm);
386
387 p += sprintf(p,
388 "rtc_time\t: %02d:%02d:%02d\n"
389 "rtc_date\t: %04d-%02d-%02d\n"
390 "rtc_epoch\t: %04u\n",
391 tm.tm_hour, tm.tm_min, tm.tm_sec,
392 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 1900);
393
394 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
395
396 p += sprintf(p, "alarm\t\t: ");
397 if (tm.tm_hour <= 24)
398 p += sprintf(p, "%02d:", tm.tm_hour);
399 else
400 p += sprintf(p, "**:");
401
402 if (tm.tm_min <= 59)
403 p += sprintf(p, "%02d:", tm.tm_min);
404 else
405 p += sprintf(p, "**:");
406
407 if (tm.tm_sec <= 59)
408 p += sprintf(p, "%02d\n", tm.tm_sec);
409 else
410 p += sprintf(p, "**\n");
411
412 p += sprintf(p,
413 "DST_enable\t: %s\n"
414 "BCD\t\t: %s\n"
415 "24hr\t\t: %s\n"
416 "square_wave\t: %s\n"
417 "alarm_IRQ\t: %s\n"
418 "update_IRQ\t: %s\n"
419 "periodic_IRQ\t: %s\n"
420 "periodic_freq\t: %ld\n"
421 "batt_status\t: %s\n",
422 (flags & RTC_DST_EN) ? "yes" : "no",
423 (flags & RTC_DM_BINARY) ? "no" : "yes",
424 (flags & RTC_24H) ? "yes" : "no",
425 (flags & RTC_SQWE) ? "yes" : "no",
426 (flags & RTC_AIE) ? "yes" : "no",
427 irq_active ? "yes" : "no",
428 (flags & RTC_PIE) ? "yes" : "no",
429 0L /* freq */,
430 (flags & RTC_BATT_BAD) ? "bad" : "okay");
431 if (!get_rtc_pll(&pll))
432 p += sprintf(p,
433 "PLL adjustment\t: %d\n"
434 "PLL max +ve adjustment\t: %d\n"
435 "PLL max -ve adjustment\t: %d\n"
436 "PLL +ve adjustment factor\t: %d\n"
437 "PLL -ve adjustment factor\t: %d\n"
438 "PLL frequency\t: %ld\n",
439 pll.pll_value,
440 pll.pll_max,
441 pll.pll_min,
442 pll.pll_posmult,
443 pll.pll_negmult,
444 pll.pll_clock);
445 return p - buf;
446}
447
448static int gen_rtc_read_proc(char *page, char **start, off_t off,
449 int count, int *eof, void *data)
450{
451 int len = gen_rtc_proc_output (page);
452 if (len <= off+count) *eof = 1;
453 *start = page + off;
454 len -= off;
455 if (len>count) len = count;
456 if (len<0) len = 0;
457 return len;
458}
459
460static int __init gen_rtc_proc_init(void)
461{
462 struct proc_dir_entry *r;
463
464 r = create_proc_read_entry("driver/rtc", 0, NULL, gen_rtc_read_proc, NULL);
465 if (!r)
466 return -ENOMEM;
467 return 0;
468}
469#else
470static inline int gen_rtc_proc_init(void) { return 0; }
471#endif /* CONFIG_PROC_FS */
472
473
474/*
475 * The various file operations we support.
476 */
477
Arjan van de Ven62322d22006-07-03 00:24:21 -0700478static const struct file_operations gen_rtc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 .owner = THIS_MODULE,
480#ifdef CONFIG_GEN_RTC_X
481 .read = gen_rtc_read,
482 .poll = gen_rtc_poll,
483#endif
484 .ioctl = gen_rtc_ioctl,
485 .open = gen_rtc_open,
486 .release = gen_rtc_release,
487};
488
489static struct miscdevice rtc_gen_dev =
490{
491 .minor = RTC_MINOR,
492 .name = "rtc",
493 .fops = &gen_rtc_fops,
494};
495
496static int __init rtc_generic_init(void)
497{
498 int retval;
499
500 printk(KERN_INFO "Generic RTC Driver v%s\n", RTC_VERSION);
501
502 retval = misc_register(&rtc_gen_dev);
503 if (retval < 0)
504 return retval;
505
506 retval = gen_rtc_proc_init();
507 if (retval) {
508 misc_deregister(&rtc_gen_dev);
509 return retval;
510 }
511
512 return 0;
513}
514
515static void __exit rtc_generic_exit(void)
516{
517 remove_proc_entry ("driver/rtc", NULL);
518 misc_deregister(&rtc_gen_dev);
519}
520
521
522module_init(rtc_generic_init);
523module_exit(rtc_generic_exit);
524
525MODULE_AUTHOR("Richard Zidlicky");
526MODULE_LICENSE("GPL");
527MODULE_ALIAS_MISCDEV(RTC_MINOR);