Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / char / genrtc.c
blob9a00b28dd507bca485ce3e1b916e78a882aa51d6
1 /*
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
8 * Copyright (C) 1999 Richard Zidlicky
10 * based on Paul Gortmaker's rtc.c device and
11 * Sam Creasey Generic rtc driver
13 * This driver allows use of the real time clock (built into
14 * nearly all computers) from user space. It exports the /dev/rtc
15 * interface supporting various ioctl() and also the /proc/dev/rtc
16 * pseudo-file for status information.
18 * The ioctls can be used to set the interrupt behaviour where
19 * supported.
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.
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.
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>
43 #define RTC_VERSION "1.07"
45 #include <linux/module.h>
46 #include <linux/config.h>
47 #include <linux/errno.h>
48 #include <linux/miscdevice.h>
49 #include <linux/fcntl.h>
51 #include <linux/rtc.h>
52 #include <linux/init.h>
53 #include <linux/poll.h>
54 #include <linux/proc_fs.h>
55 #include <linux/workqueue.h>
57 #include <asm/uaccess.h>
58 #include <asm/system.h>
59 #include <asm/rtc.h>
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.
68 static DECLARE_WAIT_QUEUE_HEAD(gen_rtc_wait);
71 * Bits in gen_rtc_status.
74 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
76 static unsigned char gen_rtc_status; /* bitmapped status byte. */
77 static unsigned long gen_rtc_irq_data; /* our output to the world */
79 /* months start at 0 now */
80 static unsigned char days_in_mo[] =
81 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
83 static int irq_active;
85 #ifdef CONFIG_GEN_RTC_X
86 struct work_struct genrtc_task;
87 static struct timer_list timer_task;
89 static unsigned int oldsecs;
90 static int lostint;
91 static unsigned long tt_exp;
93 static void gen_rtc_timer(unsigned long data);
95 static volatile int stask_active; /* schedule_work */
96 static volatile int ttask_active; /* timer_task */
97 static int stop_rtc_timers; /* don't requeue tasks */
98 static spinlock_t gen_rtc_lock = SPIN_LOCK_UNLOCKED;
100 static void gen_rtc_interrupt(unsigned long arg);
103 * Routine to poll RTC seconds field for change as often as possible,
104 * after first RTC_UIE use timer to reduce polling
106 static void genrtc_troutine(void *data)
108 unsigned int tmp = get_rtc_ss();
110 if (stop_rtc_timers) {
111 stask_active = 0;
112 return;
115 if (oldsecs != tmp){
116 oldsecs = tmp;
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);
125 gen_rtc_interrupt(0);
126 } else if (schedule_work(&genrtc_task) == 0)
127 stask_active = 0;
130 static void gen_rtc_timer(unsigned long data)
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;
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
151 static void gen_rtc_interrupt(unsigned long arg)
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. */
157 gen_rtc_irq_data += 0x100;
158 gen_rtc_irq_data &= ~0xff;
159 gen_rtc_irq_data |= RTC_UIE;
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;
168 wake_up_interruptible(&gen_rtc_wait);
172 * Now all the various file operations that we export.
174 static ssize_t gen_rtc_read(struct file *file, char *buf,
175 size_t count, loff_t *ppos)
177 DECLARE_WAITQUEUE(wait, current);
178 unsigned long data;
179 ssize_t retval;
181 if (count != sizeof (unsigned int) && count != sizeof (unsigned long))
182 return -EINVAL;
184 if (file->f_flags & O_NONBLOCK && !gen_rtc_irq_data)
185 return -EAGAIN;
187 add_wait_queue(&gen_rtc_wait, &wait);
188 retval = -ERESTARTSYS;
190 while (1) {
191 set_current_state(TASK_INTERRUPTIBLE);
192 data = xchg(&gen_rtc_irq_data, 0);
193 if (data)
194 break;
195 if (signal_pending(current))
196 goto out;
197 schedule();
200 /* first test allows optimizer to nuke this case for 32-bit machines */
201 if (sizeof (int) != sizeof (long) && count == sizeof (unsigned int)) {
202 unsigned int uidata = data;
203 retval = put_user(uidata, (unsigned long *)buf);
205 else {
206 retval = put_user(data, (unsigned long *)buf);
208 if (!retval)
209 retval = sizeof(unsigned long);
210 out:
211 current->state = TASK_RUNNING;
212 remove_wait_queue(&gen_rtc_wait, &wait);
214 return retval;
217 static unsigned int gen_rtc_poll(struct file *file,
218 struct poll_table_struct *wait)
220 poll_wait(file, &gen_rtc_wait, wait);
221 if (gen_rtc_irq_data != 0)
222 return POLLIN | POLLRDNORM;
223 return 0;
226 #endif
229 * Used to disable/enable interrupts, only RTC_UIE supported
230 * We also clear out any old irq data after an ioctl() that
231 * meddles with the interrupt enable/disable bits.
234 static inline void gen_clear_rtc_irq_bit(unsigned char bit)
236 #ifdef CONFIG_GEN_RTC_X
237 stop_rtc_timers = 1;
238 if (ttask_active){
239 del_timer_sync(&timer_task);
240 ttask_active = 0;
242 while (stask_active)
243 schedule();
245 spin_lock(&gen_rtc_lock);
246 irq_active = 0;
247 spin_unlock(&gen_rtc_lock);
248 #endif
251 static inline int gen_set_rtc_irq_bit(unsigned char bit)
253 #ifdef CONFIG_GEN_RTC_X
254 spin_lock(&gen_rtc_lock);
255 if ( !irq_active ) {
256 irq_active = 1;
257 stop_rtc_timers = 0;
258 lostint = 0;
259 INIT_WORK(&genrtc_task, genrtc_troutine, NULL);
260 oldsecs = get_rtc_ss();
261 init_timer(&timer_task);
263 stask_active = 1;
264 if (schedule_work(&genrtc_task) == 0){
265 stask_active = 0;
268 spin_unlock(&gen_rtc_lock);
269 gen_rtc_irq_data = 0;
270 return 0;
271 #else
272 return -EINVAL;
273 #endif
276 static int gen_rtc_ioctl(struct inode *inode, struct file *file,
277 unsigned int cmd, unsigned long arg)
279 struct rtc_time wtime;
280 struct rtc_pll_info pll;
282 switch (cmd) {
284 case RTC_PLL_GET:
285 if (get_rtc_pll(&pll))
286 return -EINVAL;
287 else
288 return copy_to_user((void *)arg, &pll, sizeof pll) ? -EFAULT : 0;
290 case RTC_PLL_SET:
291 if (!capable(CAP_SYS_TIME))
292 return -EACCES;
293 if (copy_from_user(&pll, (struct rtc_pll_info*)arg,
294 sizeof(pll)))
295 return -EFAULT;
296 return set_rtc_pll(&pll);
298 case RTC_UIE_OFF: /* disable ints from RTC updates. */
299 gen_clear_rtc_irq_bit(RTC_UIE);
300 return 0;
302 case RTC_UIE_ON: /* enable ints for RTC updates. */
303 return gen_set_rtc_irq_bit(RTC_UIE);
305 case RTC_RD_TIME: /* Read the time/date from RTC */
306 /* this doesn't get week-day, who cares */
307 memset(&wtime, 0, sizeof(wtime));
308 get_rtc_time(&wtime);
310 return copy_to_user((void *)arg, &wtime, sizeof(wtime)) ? -EFAULT : 0;
312 case RTC_SET_TIME: /* Set the RTC */
314 int year;
315 unsigned char leap_yr;
317 if (!capable(CAP_SYS_TIME))
318 return -EACCES;
320 if (copy_from_user(&wtime, (struct rtc_time *)arg,
321 sizeof(wtime)))
322 return -EFAULT;
324 year = wtime.tm_year + 1900;
325 leap_yr = ((!(year % 4) && (year % 100)) ||
326 !(year % 400));
328 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
329 return -EINVAL;
331 if (wtime.tm_mday < 0 || wtime.tm_mday >
332 (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
333 return -EINVAL;
335 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
336 wtime.tm_min < 0 || wtime.tm_min >= 60 ||
337 wtime.tm_sec < 0 || wtime.tm_sec >= 60)
338 return -EINVAL;
340 return set_rtc_time(&wtime);
344 return -EINVAL;
348 * We enforce only one user at a time here with the open/close.
349 * Also clear the previous interrupt data on an open, and clean
350 * up things on a close.
353 static int gen_rtc_open(struct inode *inode, struct file *file)
355 if (gen_rtc_status & RTC_IS_OPEN)
356 return -EBUSY;
358 gen_rtc_status |= RTC_IS_OPEN;
359 gen_rtc_irq_data = 0;
360 irq_active = 0;
362 return 0;
365 static int gen_rtc_release(struct inode *inode, struct file *file)
368 * Turn off all interrupts once the device is no longer
369 * in use and clear the data.
372 gen_clear_rtc_irq_bit(RTC_PIE|RTC_AIE|RTC_UIE);
374 gen_rtc_status &= ~RTC_IS_OPEN;
375 return 0;
378 static int gen_rtc_read_proc(char *page, char **start, off_t off,
379 int count, int *eof, void *data);
383 * The various file operations we support.
386 static struct file_operations gen_rtc_fops = {
387 .owner = THIS_MODULE,
388 #ifdef CONFIG_GEN_RTC_X
389 .read = gen_rtc_read,
390 .poll = gen_rtc_poll,
391 #endif
392 .ioctl = gen_rtc_ioctl,
393 .open = gen_rtc_open,
394 .release = gen_rtc_release,
397 static struct miscdevice rtc_gen_dev =
399 .minor = RTC_MINOR,
400 .name = "rtc",
401 .fops = &gen_rtc_fops,
404 static int __init rtc_generic_init(void)
406 int retval;
408 printk(KERN_INFO "Generic RTC Driver v%s\n", RTC_VERSION);
410 retval = misc_register(&rtc_gen_dev);
411 if(retval < 0)
412 return retval;
414 #ifdef CONFIG_PROC_FS
415 if((create_proc_read_entry ("driver/rtc", 0, 0, gen_rtc_read_proc, NULL)) == NULL){
416 misc_deregister(&rtc_gen_dev);
417 return -ENOMEM;
419 #endif
421 return 0;
424 static void __exit rtc_generic_exit(void)
426 remove_proc_entry ("driver/rtc", NULL);
427 misc_deregister(&rtc_gen_dev);
430 module_init(rtc_generic_init);
431 module_exit(rtc_generic_exit);
435 * Info exported via "/proc/rtc".
438 #ifdef CONFIG_PROC_FS
440 static int gen_rtc_proc_output(char *buf)
442 char *p;
443 struct rtc_time tm;
444 unsigned int flags;
445 struct rtc_pll_info pll;
447 p = buf;
449 flags = get_rtc_time(&tm);
451 p += sprintf(p,
452 "rtc_time\t: %02d:%02d:%02d\n"
453 "rtc_date\t: %04d-%02d-%02d\n"
454 "rtc_epoch\t: %04u\n",
455 tm.tm_hour, tm.tm_min, tm.tm_sec,
456 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 1900);
458 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
460 p += sprintf(p, "alarm\t\t: ");
461 if (tm.tm_hour <= 24)
462 p += sprintf(p, "%02d:", tm.tm_hour);
463 else
464 p += sprintf(p, "**:");
466 if (tm.tm_min <= 59)
467 p += sprintf(p, "%02d:", tm.tm_min);
468 else
469 p += sprintf(p, "**:");
471 if (tm.tm_sec <= 59)
472 p += sprintf(p, "%02d\n", tm.tm_sec);
473 else
474 p += sprintf(p, "**\n");
476 p += sprintf(p,
477 "DST_enable\t: %s\n"
478 "BCD\t\t: %s\n"
479 "24hr\t\t: %s\n"
480 "square_wave\t: %s\n"
481 "alarm_IRQ\t: %s\n"
482 "update_IRQ\t: %s\n"
483 "periodic_IRQ\t: %s\n"
484 "periodic_freq\t: %ld\n"
485 "batt_status\t: %s\n",
486 (flags & RTC_DST_EN) ? "yes" : "no",
487 (flags & RTC_DM_BINARY) ? "no" : "yes",
488 (flags & RTC_24H) ? "yes" : "no",
489 (flags & RTC_SQWE) ? "yes" : "no",
490 (flags & RTC_AIE) ? "yes" : "no",
491 irq_active ? "yes" : "no",
492 (flags & RTC_PIE) ? "yes" : "no",
493 0L /* freq */,
494 (flags & RTC_BATT_BAD) ? "bad" : "okay");
495 if (!get_rtc_pll(&pll))
496 p += sprintf(p,
497 "PLL adjustment\t: %d\n"
498 "PLL max +ve adjustment\t: %d\n"
499 "PLL max -ve adjustment\t: %d\n"
500 "PLL +ve adjustment factor\t: %d\n"
501 "PLL -ve adjustment factor\t: %d\n"
502 "PLL frequency\t: %ld\n",
503 pll.pll_value,
504 pll.pll_max,
505 pll.pll_min,
506 pll.pll_posmult,
507 pll.pll_negmult,
508 pll.pll_clock);
509 return p - buf;
512 static int gen_rtc_read_proc(char *page, char **start, off_t off,
513 int count, int *eof, void *data)
515 int len = gen_rtc_proc_output (page);
516 if (len <= off+count) *eof = 1;
517 *start = page + off;
518 len -= off;
519 if (len>count) len = count;
520 if (len<0) len = 0;
521 return len;
524 #endif /* CONFIG_PROC_FS */
527 MODULE_AUTHOR("Richard Zidlicky");
528 MODULE_LICENSE("GPL");