[PATCH] x86_64 early quirks: fix early_qrk[] section tag
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / char / genrtc.c
blob23b25ada65ea3c5b02405b1cf955afd17051d5f1
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/errno.h>
47 #include <linux/miscdevice.h>
48 #include <linux/fcntl.h>
50 #include <linux/rtc.h>
51 #include <linux/init.h>
52 #include <linux/poll.h>
53 #include <linux/proc_fs.h>
54 #include <linux/workqueue.h>
56 #include <asm/uaccess.h>
57 #include <asm/system.h>
58 #include <asm/rtc.h>
61 * We sponge a minor off of the misc major. No need slurping
62 * up another valuable major dev number for this. If you add
63 * an ioctl, make sure you don't conflict with SPARC's RTC
64 * ioctls.
67 static DECLARE_WAIT_QUEUE_HEAD(gen_rtc_wait);
70 * Bits in gen_rtc_status.
73 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
75 static unsigned char gen_rtc_status; /* bitmapped status byte. */
76 static unsigned long gen_rtc_irq_data; /* our output to the world */
78 /* months start at 0 now */
79 static unsigned char days_in_mo[] =
80 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
82 static int irq_active;
84 #ifdef CONFIG_GEN_RTC_X
85 static struct work_struct genrtc_task;
86 static struct timer_list timer_task;
88 static unsigned int oldsecs;
89 static int lostint;
90 static unsigned long tt_exp;
92 static void gen_rtc_timer(unsigned long data);
94 static volatile int stask_active; /* schedule_work */
95 static volatile int ttask_active; /* timer_task */
96 static int stop_rtc_timers; /* don't requeue tasks */
97 static DEFINE_SPINLOCK(gen_rtc_lock);
99 static void gen_rtc_interrupt(unsigned long arg);
102 * Routine to poll RTC seconds field for change as often as possible,
103 * after first RTC_UIE use timer to reduce polling
105 static void genrtc_troutine(struct work_struct *work)
107 unsigned int tmp = get_rtc_ss();
109 if (stop_rtc_timers) {
110 stask_active = 0;
111 return;
114 if (oldsecs != tmp){
115 oldsecs = tmp;
117 timer_task.function = gen_rtc_timer;
118 timer_task.expires = jiffies + HZ - (HZ/10);
119 tt_exp=timer_task.expires;
120 ttask_active=1;
121 stask_active=0;
122 add_timer(&timer_task);
124 gen_rtc_interrupt(0);
125 } else if (schedule_work(&genrtc_task) == 0)
126 stask_active = 0;
129 static void gen_rtc_timer(unsigned long data)
131 lostint = get_rtc_ss() - oldsecs ;
132 if (lostint<0)
133 lostint = 60 - lostint;
134 if (time_after(jiffies, tt_exp))
135 printk(KERN_INFO "genrtc: timer task delayed by %ld jiffies\n",
136 jiffies-tt_exp);
137 ttask_active=0;
138 stask_active=1;
139 if ((schedule_work(&genrtc_task) == 0))
140 stask_active = 0;
144 * call gen_rtc_interrupt function to signal an RTC_UIE,
145 * arg is unused.
146 * Could be invoked either from a real interrupt handler or
147 * from some routine that periodically (eg 100HZ) monitors
148 * whether RTC_SECS changed
150 static void gen_rtc_interrupt(unsigned long arg)
152 /* We store the status in the low byte and the number of
153 * interrupts received since the last read in the remainder
154 * of rtc_irq_data. */
156 gen_rtc_irq_data += 0x100;
157 gen_rtc_irq_data &= ~0xff;
158 gen_rtc_irq_data |= RTC_UIE;
160 if (lostint){
161 printk("genrtc: system delaying clock ticks?\n");
162 /* increment count so that userspace knows something is wrong */
163 gen_rtc_irq_data += ((lostint-1)<<8);
164 lostint = 0;
167 wake_up_interruptible(&gen_rtc_wait);
171 * Now all the various file operations that we export.
173 static ssize_t gen_rtc_read(struct file *file, char __user *buf,
174 size_t count, loff_t *ppos)
176 DECLARE_WAITQUEUE(wait, current);
177 unsigned long data;
178 ssize_t retval;
180 if (count != sizeof (unsigned int) && count != sizeof (unsigned long))
181 return -EINVAL;
183 if (file->f_flags & O_NONBLOCK && !gen_rtc_irq_data)
184 return -EAGAIN;
186 add_wait_queue(&gen_rtc_wait, &wait);
187 retval = -ERESTARTSYS;
189 while (1) {
190 set_current_state(TASK_INTERRUPTIBLE);
191 data = xchg(&gen_rtc_irq_data, 0);
192 if (data)
193 break;
194 if (signal_pending(current))
195 goto out;
196 schedule();
199 /* first test allows optimizer to nuke this case for 32-bit machines */
200 if (sizeof (int) != sizeof (long) && count == sizeof (unsigned int)) {
201 unsigned int uidata = data;
202 retval = put_user(uidata, (unsigned int __user *)buf) ?:
203 sizeof(unsigned int);
205 else {
206 retval = put_user(data, (unsigned long __user *)buf) ?:
207 sizeof(unsigned long);
209 out:
210 current->state = TASK_RUNNING;
211 remove_wait_queue(&gen_rtc_wait, &wait);
213 return retval;
216 static unsigned int gen_rtc_poll(struct file *file,
217 struct poll_table_struct *wait)
219 poll_wait(file, &gen_rtc_wait, wait);
220 if (gen_rtc_irq_data != 0)
221 return POLLIN | POLLRDNORM;
222 return 0;
225 #endif
228 * Used to disable/enable interrupts, only RTC_UIE supported
229 * We also clear out any old irq data after an ioctl() that
230 * meddles with the interrupt enable/disable bits.
233 static inline void gen_clear_rtc_irq_bit(unsigned char bit)
235 #ifdef CONFIG_GEN_RTC_X
236 stop_rtc_timers = 1;
237 if (ttask_active){
238 del_timer_sync(&timer_task);
239 ttask_active = 0;
241 while (stask_active)
242 schedule();
244 spin_lock(&gen_rtc_lock);
245 irq_active = 0;
246 spin_unlock(&gen_rtc_lock);
247 #endif
250 static inline int gen_set_rtc_irq_bit(unsigned char bit)
252 #ifdef CONFIG_GEN_RTC_X
253 spin_lock(&gen_rtc_lock);
254 if ( !irq_active ) {
255 irq_active = 1;
256 stop_rtc_timers = 0;
257 lostint = 0;
258 INIT_WORK(&genrtc_task, genrtc_troutine);
259 oldsecs = get_rtc_ss();
260 init_timer(&timer_task);
262 stask_active = 1;
263 if (schedule_work(&genrtc_task) == 0){
264 stask_active = 0;
267 spin_unlock(&gen_rtc_lock);
268 gen_rtc_irq_data = 0;
269 return 0;
270 #else
271 return -EINVAL;
272 #endif
275 static int gen_rtc_ioctl(struct inode *inode, struct file *file,
276 unsigned int cmd, unsigned long arg)
278 struct rtc_time wtime;
279 struct rtc_pll_info pll;
280 void __user *argp = (void __user *)arg;
282 switch (cmd) {
284 case RTC_PLL_GET:
285 if (get_rtc_pll(&pll))
286 return -EINVAL;
287 else
288 return copy_to_user(argp, &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, argp, sizeof(pll)))
294 return -EFAULT;
295 return set_rtc_pll(&pll);
297 case RTC_UIE_OFF: /* disable ints from RTC updates. */
298 gen_clear_rtc_irq_bit(RTC_UIE);
299 return 0;
301 case RTC_UIE_ON: /* enable ints for RTC updates. */
302 return gen_set_rtc_irq_bit(RTC_UIE);
304 case RTC_RD_TIME: /* Read the time/date from RTC */
305 /* this doesn't get week-day, who cares */
306 memset(&wtime, 0, sizeof(wtime));
307 get_rtc_time(&wtime);
309 return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
311 case RTC_SET_TIME: /* Set the RTC */
313 int year;
314 unsigned char leap_yr;
316 if (!capable(CAP_SYS_TIME))
317 return -EACCES;
319 if (copy_from_user(&wtime, argp, sizeof(wtime)))
320 return -EFAULT;
322 year = wtime.tm_year + 1900;
323 leap_yr = ((!(year % 4) && (year % 100)) ||
324 !(year % 400));
326 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
327 return -EINVAL;
329 if (wtime.tm_mday < 0 || wtime.tm_mday >
330 (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
331 return -EINVAL;
333 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
334 wtime.tm_min < 0 || wtime.tm_min >= 60 ||
335 wtime.tm_sec < 0 || wtime.tm_sec >= 60)
336 return -EINVAL;
338 return set_rtc_time(&wtime);
342 return -EINVAL;
346 * We enforce only one user at a time here with the open/close.
347 * Also clear the previous interrupt data on an open, and clean
348 * up things on a close.
351 static int gen_rtc_open(struct inode *inode, struct file *file)
353 if (gen_rtc_status & RTC_IS_OPEN)
354 return -EBUSY;
356 gen_rtc_status |= RTC_IS_OPEN;
357 gen_rtc_irq_data = 0;
358 irq_active = 0;
360 return 0;
363 static int gen_rtc_release(struct inode *inode, struct file *file)
366 * Turn off all interrupts once the device is no longer
367 * in use and clear the data.
370 gen_clear_rtc_irq_bit(RTC_PIE|RTC_AIE|RTC_UIE);
372 gen_rtc_status &= ~RTC_IS_OPEN;
373 return 0;
377 #ifdef CONFIG_PROC_FS
380 * Info exported via "/proc/rtc".
383 static int gen_rtc_proc_output(char *buf)
385 char *p;
386 struct rtc_time tm;
387 unsigned int flags;
388 struct rtc_pll_info pll;
390 p = buf;
392 flags = get_rtc_time(&tm);
394 p += sprintf(p,
395 "rtc_time\t: %02d:%02d:%02d\n"
396 "rtc_date\t: %04d-%02d-%02d\n"
397 "rtc_epoch\t: %04u\n",
398 tm.tm_hour, tm.tm_min, tm.tm_sec,
399 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 1900);
401 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
403 p += sprintf(p, "alarm\t\t: ");
404 if (tm.tm_hour <= 24)
405 p += sprintf(p, "%02d:", tm.tm_hour);
406 else
407 p += sprintf(p, "**:");
409 if (tm.tm_min <= 59)
410 p += sprintf(p, "%02d:", tm.tm_min);
411 else
412 p += sprintf(p, "**:");
414 if (tm.tm_sec <= 59)
415 p += sprintf(p, "%02d\n", tm.tm_sec);
416 else
417 p += sprintf(p, "**\n");
419 p += sprintf(p,
420 "DST_enable\t: %s\n"
421 "BCD\t\t: %s\n"
422 "24hr\t\t: %s\n"
423 "square_wave\t: %s\n"
424 "alarm_IRQ\t: %s\n"
425 "update_IRQ\t: %s\n"
426 "periodic_IRQ\t: %s\n"
427 "periodic_freq\t: %ld\n"
428 "batt_status\t: %s\n",
429 (flags & RTC_DST_EN) ? "yes" : "no",
430 (flags & RTC_DM_BINARY) ? "no" : "yes",
431 (flags & RTC_24H) ? "yes" : "no",
432 (flags & RTC_SQWE) ? "yes" : "no",
433 (flags & RTC_AIE) ? "yes" : "no",
434 irq_active ? "yes" : "no",
435 (flags & RTC_PIE) ? "yes" : "no",
436 0L /* freq */,
437 (flags & RTC_BATT_BAD) ? "bad" : "okay");
438 if (!get_rtc_pll(&pll))
439 p += sprintf(p,
440 "PLL adjustment\t: %d\n"
441 "PLL max +ve adjustment\t: %d\n"
442 "PLL max -ve adjustment\t: %d\n"
443 "PLL +ve adjustment factor\t: %d\n"
444 "PLL -ve adjustment factor\t: %d\n"
445 "PLL frequency\t: %ld\n",
446 pll.pll_value,
447 pll.pll_max,
448 pll.pll_min,
449 pll.pll_posmult,
450 pll.pll_negmult,
451 pll.pll_clock);
452 return p - buf;
455 static int gen_rtc_read_proc(char *page, char **start, off_t off,
456 int count, int *eof, void *data)
458 int len = gen_rtc_proc_output (page);
459 if (len <= off+count) *eof = 1;
460 *start = page + off;
461 len -= off;
462 if (len>count) len = count;
463 if (len<0) len = 0;
464 return len;
467 static int __init gen_rtc_proc_init(void)
469 struct proc_dir_entry *r;
471 r = create_proc_read_entry("driver/rtc", 0, NULL, gen_rtc_read_proc, NULL);
472 if (!r)
473 return -ENOMEM;
474 return 0;
476 #else
477 static inline int gen_rtc_proc_init(void) { return 0; }
478 #endif /* CONFIG_PROC_FS */
482 * The various file operations we support.
485 static const struct file_operations gen_rtc_fops = {
486 .owner = THIS_MODULE,
487 #ifdef CONFIG_GEN_RTC_X
488 .read = gen_rtc_read,
489 .poll = gen_rtc_poll,
490 #endif
491 .ioctl = gen_rtc_ioctl,
492 .open = gen_rtc_open,
493 .release = gen_rtc_release,
496 static struct miscdevice rtc_gen_dev =
498 .minor = RTC_MINOR,
499 .name = "rtc",
500 .fops = &gen_rtc_fops,
503 static int __init rtc_generic_init(void)
505 int retval;
507 printk(KERN_INFO "Generic RTC Driver v%s\n", RTC_VERSION);
509 retval = misc_register(&rtc_gen_dev);
510 if (retval < 0)
511 return retval;
513 retval = gen_rtc_proc_init();
514 if (retval) {
515 misc_deregister(&rtc_gen_dev);
516 return retval;
519 return 0;
522 static void __exit rtc_generic_exit(void)
524 remove_proc_entry ("driver/rtc", NULL);
525 misc_deregister(&rtc_gen_dev);
529 module_init(rtc_generic_init);
530 module_exit(rtc_generic_exit);
532 MODULE_AUTHOR("Richard Zidlicky");
533 MODULE_LICENSE("GPL");
534 MODULE_ALIAS_MISCDEV(RTC_MINOR);