patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / char / rtc.c
1 /*
2  *      Real Time Clock interface for Linux     
3  *
4  *      Copyright (C) 1996 Paul Gortmaker
5  *
6  *      This driver allows use of the real time clock (built into
7  *      nearly all computers) from user space. It exports the /dev/rtc
8  *      interface supporting various ioctl() and also the
9  *      /proc/driver/rtc pseudo-file for status information.
10  *
11  *      The ioctls can be used to set the interrupt behaviour and
12  *      generation rate from the RTC via IRQ 8. Then the /dev/rtc
13  *      interface can be used to make use of these timer interrupts,
14  *      be they interval or alarm based.
15  *
16  *      The /dev/rtc interface will block on reads until an interrupt
17  *      has been received. If a RTC interrupt has already happened,
18  *      it will output an unsigned long and then block. The output value
19  *      contains the interrupt status in the low byte and the number of
20  *      interrupts since the last read in the remaining high bytes. The 
21  *      /dev/rtc interface can also be used with the select(2) call.
22  *
23  *      This program is free software; you can redistribute it and/or
24  *      modify it under the terms of the GNU General Public License
25  *      as published by the Free Software Foundation; either version
26  *      2 of the License, or (at your option) any later version.
27  *
28  *      Based on other minimal char device drivers, like Alan's
29  *      watchdog, Ted's random, etc. etc.
30  *
31  *      1.07    Paul Gortmaker.
32  *      1.08    Miquel van Smoorenburg: disallow certain things on the
33  *              DEC Alpha as the CMOS clock is also used for other things.
34  *      1.09    Nikita Schmidt: epoch support and some Alpha cleanup.
35  *      1.09a   Pete Zaitcev: Sun SPARC
36  *      1.09b   Jeff Garzik: Modularize, init cleanup
37  *      1.09c   Jeff Garzik: SMP cleanup
38  *      1.10    Paul Barton-Davis: add support for async I/O
39  *      1.10a   Andrea Arcangeli: Alpha updates
40  *      1.10b   Andrew Morton: SMP lock fix
41  *      1.10c   Cesar Barros: SMP locking fixes and cleanup
42  *      1.10d   Paul Gortmaker: delete paranoia check in rtc_exit
43  *      1.10e   Maciej W. Rozycki: Handle DECstation's year weirdness.
44  *      1.11    Takashi Iwai: Kernel access functions
45  *                            rtc_register/rtc_unregister/rtc_control
46  *      1.11a   Daniele Bellucci: Audit create_proc_read_entry in rtc_init
47  *      1.12    Venkatesh Pallipadi: Hooks for emulating rtc on HPET base-timer
48  *              CONFIG_HPET_EMULATE_RTC
49  *
50  */
51
52 #define RTC_VERSION             "1.12"
53
54 #define RTC_IO_EXTENT   0x8
55
56 /*
57  *      Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
58  *      interrupts disabled. Due to the index-port/data-port (0x70/0x71)
59  *      design of the RTC, we don't want two different things trying to
60  *      get to it at once. (e.g. the periodic 11 min sync from time.c vs.
61  *      this driver.)
62  */
63
64 #include <linux/config.h>
65 #include <linux/interrupt.h>
66 #include <linux/module.h>
67 #include <linux/kernel.h>
68 #include <linux/types.h>
69 #include <linux/miscdevice.h>
70 #include <linux/ioport.h>
71 #include <linux/fcntl.h>
72 #include <linux/mc146818rtc.h>
73 #include <linux/init.h>
74 #include <linux/poll.h>
75 #include <linux/proc_fs.h>
76 #include <linux/spinlock.h>
77 #include <linux/sysctl.h>
78 #include <linux/wait.h>
79 #include <linux/bcd.h>
80
81 #include <asm/current.h>
82 #include <asm/uaccess.h>
83 #include <asm/system.h>
84
85 #if defined(__i386__)
86 #include <asm/hpet.h>
87 #endif
88
89 #ifdef __sparc__
90 #include <linux/pci.h>
91 #include <asm/ebus.h>
92 #ifdef __sparc_v9__
93 #include <asm/isa.h>
94 #endif
95
96 static unsigned long rtc_port;
97 static int rtc_irq = PCI_IRQ_NONE;
98 #endif
99
100 #ifdef RTC_IRQ
101 static int rtc_has_irq = 1;
102 #endif
103
104 #ifndef CONFIG_HPET_EMULATE_RTC
105 #define is_hpet_enabled()                       0
106 #define hpet_set_alarm_time(hrs, min, sec)      0
107 #define hpet_set_periodic_freq(arg)             0
108 #define hpet_mask_rtc_irq_bit(arg)              0
109 #define hpet_set_rtc_irq_bit(arg)               0
110 #define hpet_rtc_timer_init()                   do { } while (0)
111 #define hpet_rtc_dropped_irq()                  0
112 static inline irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs) {return 0;}
113 #else
114 extern irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs);
115 #endif
116
117 /*
118  *      We sponge a minor off of the misc major. No need slurping
119  *      up another valuable major dev number for this. If you add
120  *      an ioctl, make sure you don't conflict with SPARC's RTC
121  *      ioctls.
122  */
123
124 static struct fasync_struct *rtc_async_queue;
125
126 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
127
128 #ifdef RTC_IRQ
129 static struct timer_list rtc_irq_timer;
130 #endif
131
132 static ssize_t rtc_read(struct file *file, char __user *buf,
133                         size_t count, loff_t *ppos);
134
135 static int rtc_ioctl(struct inode *inode, struct file *file,
136                      unsigned int cmd, unsigned long arg);
137
138 #ifdef RTC_IRQ
139 static unsigned int rtc_poll(struct file *file, poll_table *wait);
140 #endif
141
142 static void get_rtc_alm_time (struct rtc_time *alm_tm);
143 #ifdef RTC_IRQ
144 static void rtc_dropped_irq(unsigned long data);
145
146 static void set_rtc_irq_bit(unsigned char bit);
147 static void mask_rtc_irq_bit(unsigned char bit);
148 #endif
149
150 static int rtc_read_proc(char *page, char **start, off_t off,
151                          int count, int *eof, void *data);
152
153 /*
154  *      Bits in rtc_status. (6 bits of room for future expansion)
155  */
156
157 #define RTC_IS_OPEN             0x01    /* means /dev/rtc is in use     */
158 #define RTC_TIMER_ON            0x02    /* missed irq timer active      */
159
160 /*
161  * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
162  * protected by the big kernel lock. However, ioctl can still disable the timer
163  * in rtc_status and then with del_timer after the interrupt has read
164  * rtc_status but before mod_timer is called, which would then reenable the
165  * timer (but you would need to have an awful timing before you'd trip on it)
166  */
167 static unsigned long rtc_status = 0;    /* bitmapped status byte.       */
168 static unsigned long rtc_freq = 0;      /* Current periodic IRQ rate    */
169 static unsigned long rtc_irq_data = 0;  /* our output to the world      */
170 static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */
171
172 #ifdef RTC_IRQ
173 /*
174  * rtc_task_lock nests inside rtc_lock.
175  */
176 static spinlock_t rtc_task_lock = SPIN_LOCK_UNLOCKED;
177 static rtc_task_t *rtc_callback = NULL;
178 #endif
179
180 /*
181  *      If this driver ever becomes modularised, it will be really nice
182  *      to make the epoch retain its value across module reload...
183  */
184
185 static unsigned long epoch = 1900;      /* year corresponding to 0x00   */
186
187 static const unsigned char days_in_mo[] = 
188 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
189
190 /*
191  * Returns true if a clock update is in progress
192  */
193 static inline unsigned char rtc_is_updating(void)
194 {
195         unsigned char uip;
196
197         spin_lock_irq(&rtc_lock);
198         uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
199         spin_unlock_irq(&rtc_lock);
200         return uip;
201 }
202
203 #ifdef RTC_IRQ
204 /*
205  *      A very tiny interrupt handler. It runs with SA_INTERRUPT set,
206  *      but there is possibility of conflicting with the set_rtc_mmss()
207  *      call (the rtc irq and the timer irq can easily run at the same
208  *      time in two different CPUs). So we need to serialize
209  *      accesses to the chip with the rtc_lock spinlock that each
210  *      architecture should implement in the timer code.
211  *      (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
212  */
213
214 irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
215 {
216         /*
217          *      Can be an alarm interrupt, update complete interrupt,
218          *      or a periodic interrupt. We store the status in the
219          *      low byte and the number of interrupts received since
220          *      the last read in the remainder of rtc_irq_data.
221          */
222
223         spin_lock (&rtc_lock);
224         rtc_irq_data += 0x100;
225         rtc_irq_data &= ~0xff;
226         if (is_hpet_enabled()) {
227                 /*
228                  * In this case it is HPET RTC interrupt handler
229                  * calling us, with the interrupt information
230                  * passed as arg1, instead of irq.
231                  */
232                 rtc_irq_data |= (unsigned long)irq & 0xF0;
233         } else {
234                 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
235         }
236
237         if (rtc_status & RTC_TIMER_ON)
238                 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
239
240         spin_unlock (&rtc_lock);
241
242         /* Now do the rest of the actions */
243         spin_lock(&rtc_task_lock);
244         if (rtc_callback)
245                 rtc_callback->func(rtc_callback->private_data);
246         spin_unlock(&rtc_task_lock);
247         wake_up_interruptible(&rtc_wait);       
248
249         kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
250
251         return IRQ_HANDLED;
252 }
253 #endif
254
255 /*
256  * sysctl-tuning infrastructure.
257  */
258 static ctl_table rtc_table[] = {
259         {
260                 .ctl_name       = 1,
261                 .procname       = "max-user-freq",
262                 .data           = &rtc_max_user_freq,
263                 .maxlen         = sizeof(int),
264                 .mode           = 0644,
265                 .proc_handler   = &proc_dointvec,
266         },
267         { .ctl_name = 0 }
268 };
269
270 static ctl_table rtc_root[] = {
271         {
272                 .ctl_name       = 1,
273                 .procname       = "rtc",
274                 .maxlen         = 0,
275                 .mode           = 0555,
276                 .child          = rtc_table,
277         },
278         { .ctl_name = 0 }
279 };
280
281 static ctl_table dev_root[] = {
282         {
283                 .ctl_name       = CTL_DEV,
284                 .procname       = "dev",
285                 .maxlen         = 0,
286                 .mode           = 0555,
287                 .child          = rtc_root,
288         },
289         { .ctl_name = 0 }
290 };
291
292 static struct ctl_table_header *sysctl_header;
293
294 static int __init init_sysctl(void)
295 {
296     sysctl_header = register_sysctl_table(dev_root, 0);
297     return 0;
298 }
299
300 static void __exit cleanup_sysctl(void)
301 {
302     unregister_sysctl_table(sysctl_header);
303 }
304
305 /*
306  *      Now all the various file operations that we export.
307  */
308
309 static ssize_t rtc_read(struct file *file, char __user *buf,
310                         size_t count, loff_t *ppos)
311 {
312 #ifndef RTC_IRQ
313         return -EIO;
314 #else
315         DECLARE_WAITQUEUE(wait, current);
316         unsigned long data;
317         ssize_t retval;
318         
319         if (rtc_has_irq == 0)
320                 return -EIO;
321
322         if (count < sizeof(unsigned))
323                 return -EINVAL;
324
325         add_wait_queue(&rtc_wait, &wait);
326
327         do {
328                 /* First make it right. Then make it fast. Putting this whole
329                  * block within the parentheses of a while would be too
330                  * confusing. And no, xchg() is not the answer. */
331
332                 __set_current_state(TASK_INTERRUPTIBLE);
333                 
334                 spin_lock_irq (&rtc_lock);
335                 data = rtc_irq_data;
336                 rtc_irq_data = 0;
337                 spin_unlock_irq (&rtc_lock);
338
339                 if (data != 0)
340                         break;
341
342                 if (file->f_flags & O_NONBLOCK) {
343                         retval = -EAGAIN;
344                         goto out;
345                 }
346                 if (signal_pending(current)) {
347                         retval = -ERESTARTSYS;
348                         goto out;
349                 }
350                 schedule();
351         } while (1);
352
353         if (count < sizeof(unsigned long))
354                 retval = put_user(data, (unsigned int __user *)buf) ?: sizeof(int); 
355         else
356                 retval = put_user(data, (unsigned long __user *)buf) ?: sizeof(long);
357  out:
358         current->state = TASK_RUNNING;
359         remove_wait_queue(&rtc_wait, &wait);
360
361         return retval;
362 #endif
363 }
364
365 static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel)
366 {
367         struct rtc_time wtime; 
368
369 #ifdef RTC_IRQ
370         if (rtc_has_irq == 0) {
371                 switch (cmd) {
372                 case RTC_AIE_OFF:
373                 case RTC_AIE_ON:
374                 case RTC_PIE_OFF:
375                 case RTC_PIE_ON:
376                 case RTC_UIE_OFF:
377                 case RTC_UIE_ON:
378                 case RTC_IRQP_READ:
379                 case RTC_IRQP_SET:
380                         return -EINVAL;
381                 };
382         }
383 #endif
384
385         switch (cmd) {
386 #ifdef RTC_IRQ
387         case RTC_AIE_OFF:       /* Mask alarm int. enab. bit    */
388         {
389                 mask_rtc_irq_bit(RTC_AIE);
390                 return 0;
391         }
392         case RTC_AIE_ON:        /* Allow alarm interrupts.      */
393         {
394                 set_rtc_irq_bit(RTC_AIE);
395                 return 0;
396         }
397         case RTC_PIE_OFF:       /* Mask periodic int. enab. bit */
398         {
399                 mask_rtc_irq_bit(RTC_PIE);
400                 if (rtc_status & RTC_TIMER_ON) {
401                         spin_lock_irq (&rtc_lock);
402                         rtc_status &= ~RTC_TIMER_ON;
403                         del_timer(&rtc_irq_timer);
404                         spin_unlock_irq (&rtc_lock);
405                 }
406                 return 0;
407         }
408         case RTC_PIE_ON:        /* Allow periodic ints          */
409         {
410
411                 /*
412                  * We don't really want Joe User enabling more
413                  * than 64Hz of interrupts on a multi-user machine.
414                  */
415                 if (!kernel && (rtc_freq > rtc_max_user_freq) &&
416                         (!capable(CAP_SYS_RESOURCE)))
417                         return -EACCES;
418
419                 if (!(rtc_status & RTC_TIMER_ON)) {
420                         spin_lock_irq (&rtc_lock);
421                         rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
422                         add_timer(&rtc_irq_timer);
423                         rtc_status |= RTC_TIMER_ON;
424                         spin_unlock_irq (&rtc_lock);
425                 }
426                 set_rtc_irq_bit(RTC_PIE);
427                 return 0;
428         }
429         case RTC_UIE_OFF:       /* Mask ints from RTC updates.  */
430         {
431                 mask_rtc_irq_bit(RTC_UIE);
432                 return 0;
433         }
434         case RTC_UIE_ON:        /* Allow ints for RTC updates.  */
435         {
436                 set_rtc_irq_bit(RTC_UIE);
437                 return 0;
438         }
439 #endif
440         case RTC_ALM_READ:      /* Read the present alarm time */
441         {
442                 /*
443                  * This returns a struct rtc_time. Reading >= 0xc0
444                  * means "don't care" or "match all". Only the tm_hour,
445                  * tm_min, and tm_sec values are filled in.
446                  */
447                 memset(&wtime, 0, sizeof(struct rtc_time));
448                 get_rtc_alm_time(&wtime);
449                 break; 
450         }
451         case RTC_ALM_SET:       /* Store a time into the alarm */
452         {
453                 /*
454                  * This expects a struct rtc_time. Writing 0xff means
455                  * "don't care" or "match all". Only the tm_hour,
456                  * tm_min and tm_sec are used.
457                  */
458                 unsigned char hrs, min, sec;
459                 struct rtc_time alm_tm;
460
461                 if (copy_from_user(&alm_tm, (struct rtc_time __user *)arg,
462                                    sizeof(struct rtc_time)))
463                         return -EFAULT;
464
465                 hrs = alm_tm.tm_hour;
466                 min = alm_tm.tm_min;
467                 sec = alm_tm.tm_sec;
468
469                 spin_lock_irq(&rtc_lock);
470                 if (hpet_set_alarm_time(hrs, min, sec)) {
471                         /*
472                          * Fallthru and set alarm time in CMOS too,
473                          * so that we will get proper value in RTC_ALM_READ
474                          */
475                 }
476                 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
477                     RTC_ALWAYS_BCD)
478                 {
479                         if (sec < 60) BIN_TO_BCD(sec);
480                         else sec = 0xff;
481
482                         if (min < 60) BIN_TO_BCD(min);
483                         else min = 0xff;
484
485                         if (hrs < 24) BIN_TO_BCD(hrs);
486                         else hrs = 0xff;
487                 }
488                 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
489                 CMOS_WRITE(min, RTC_MINUTES_ALARM);
490                 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
491                 spin_unlock_irq(&rtc_lock);
492
493                 return 0;
494         }
495         case RTC_RD_TIME:       /* Read the time/date from RTC  */
496         {
497                 memset(&wtime, 0, sizeof(struct rtc_time));
498                 rtc_get_rtc_time(&wtime);
499                 break;
500         }
501         case RTC_SET_TIME:      /* Set the RTC */
502         {
503                 struct rtc_time rtc_tm;
504                 unsigned char mon, day, hrs, min, sec, leap_yr;
505                 unsigned char save_control, save_freq_select;
506                 unsigned int yrs;
507 #ifdef CONFIG_DECSTATION
508                 unsigned int real_yrs;
509 #endif
510
511                 if (!capable(CAP_SYS_TIME))
512                         return -EACCES;
513
514                 if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
515                                    sizeof(struct rtc_time)))
516                         return -EFAULT;
517
518                 yrs = rtc_tm.tm_year + 1900;
519                 mon = rtc_tm.tm_mon + 1;   /* tm_mon starts at zero */
520                 day = rtc_tm.tm_mday;
521                 hrs = rtc_tm.tm_hour;
522                 min = rtc_tm.tm_min;
523                 sec = rtc_tm.tm_sec;
524
525                 if (yrs < 1970)
526                         return -EINVAL;
527
528                 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
529
530                 if ((mon > 12) || (day == 0))
531                         return -EINVAL;
532
533                 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
534                         return -EINVAL;
535                         
536                 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
537                         return -EINVAL;
538
539                 if ((yrs -= epoch) > 255)    /* They are unsigned */
540                         return -EINVAL;
541
542                 spin_lock_irq(&rtc_lock);
543 #ifdef CONFIG_DECSTATION
544                 real_yrs = yrs;
545                 yrs = 72;
546
547                 /*
548                  * We want to keep the year set to 73 until March
549                  * for non-leap years, so that Feb, 29th is handled
550                  * correctly.
551                  */
552                 if (!leap_yr && mon < 3) {
553                         real_yrs--;
554                         yrs = 73;
555                 }
556 #endif
557                 /* These limits and adjustments are independent of
558                  * whether the chip is in binary mode or not.
559                  */
560                 if (yrs > 169) {
561                         spin_unlock_irq(&rtc_lock);
562                         return -EINVAL;
563                 }
564                 if (yrs >= 100)
565                         yrs -= 100;
566
567                 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
568                     || RTC_ALWAYS_BCD) {
569                         BIN_TO_BCD(sec);
570                         BIN_TO_BCD(min);
571                         BIN_TO_BCD(hrs);
572                         BIN_TO_BCD(day);
573                         BIN_TO_BCD(mon);
574                         BIN_TO_BCD(yrs);
575                 }
576
577                 save_control = CMOS_READ(RTC_CONTROL);
578                 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
579                 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
580                 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
581
582 #ifdef CONFIG_DECSTATION
583                 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
584 #endif
585                 CMOS_WRITE(yrs, RTC_YEAR);
586                 CMOS_WRITE(mon, RTC_MONTH);
587                 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
588                 CMOS_WRITE(hrs, RTC_HOURS);
589                 CMOS_WRITE(min, RTC_MINUTES);
590                 CMOS_WRITE(sec, RTC_SECONDS);
591
592                 CMOS_WRITE(save_control, RTC_CONTROL);
593                 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
594
595                 spin_unlock_irq(&rtc_lock);
596                 return 0;
597         }
598 #ifdef RTC_IRQ
599         case RTC_IRQP_READ:     /* Read the periodic IRQ rate.  */
600         {
601                 return put_user(rtc_freq, (unsigned long __user *)arg);
602         }
603         case RTC_IRQP_SET:      /* Set periodic IRQ rate.       */
604         {
605                 int tmp = 0;
606                 unsigned char val;
607
608                 /* 
609                  * The max we can do is 8192Hz.
610                  */
611                 if ((arg < 2) || (arg > 8192))
612                         return -EINVAL;
613                 /*
614                  * We don't really want Joe User generating more
615                  * than 64Hz of interrupts on a multi-user machine.
616                  */
617                 if (!kernel && (arg > rtc_max_user_freq) && (!capable(CAP_SYS_RESOURCE)))
618                         return -EACCES;
619
620                 while (arg > (1<<tmp))
621                         tmp++;
622
623                 /*
624                  * Check that the input was really a power of 2.
625                  */
626                 if (arg != (1<<tmp))
627                         return -EINVAL;
628
629                 spin_lock_irq(&rtc_lock);
630                 if (hpet_set_periodic_freq(arg)) {
631                         spin_unlock_irq(&rtc_lock);
632                         return 0;
633                 }
634                 rtc_freq = arg;
635
636                 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
637                 val |= (16 - tmp);
638                 CMOS_WRITE(val, RTC_FREQ_SELECT);
639                 spin_unlock_irq(&rtc_lock);
640                 return 0;
641         }
642 #endif
643         case RTC_EPOCH_READ:    /* Read the epoch.      */
644         {
645                 return put_user (epoch, (unsigned long __user *)arg);
646         }
647         case RTC_EPOCH_SET:     /* Set the epoch.       */
648         {
649                 /* 
650                  * There were no RTC clocks before 1900.
651                  */
652                 if (arg < 1900)
653                         return -EINVAL;
654
655                 if (!capable(CAP_SYS_TIME))
656                         return -EACCES;
657
658                 epoch = arg;
659                 return 0;
660         }
661         default:
662                 return -ENOTTY;
663         }
664         return copy_to_user((void __user *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
665 }
666
667 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
668                      unsigned long arg)
669 {
670         return rtc_do_ioctl(cmd, arg, 0);
671 }
672
673 /*
674  *      We enforce only one user at a time here with the open/close.
675  *      Also clear the previous interrupt data on an open, and clean
676  *      up things on a close.
677  */
678
679 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
680  * needed here. Or anywhere else in this driver. */
681 static int rtc_open(struct inode *inode, struct file *file)
682 {
683         spin_lock_irq (&rtc_lock);
684
685         if(rtc_status & RTC_IS_OPEN)
686                 goto out_busy;
687
688         rtc_status |= RTC_IS_OPEN;
689
690         rtc_irq_data = 0;
691         spin_unlock_irq (&rtc_lock);
692         return 0;
693
694 out_busy:
695         spin_unlock_irq (&rtc_lock);
696         return -EBUSY;
697 }
698
699 static int rtc_fasync (int fd, struct file *filp, int on)
700
701 {
702         return fasync_helper (fd, filp, on, &rtc_async_queue);
703 }
704
705 static int rtc_release(struct inode *inode, struct file *file)
706 {
707 #ifdef RTC_IRQ
708         unsigned char tmp;
709
710         if (rtc_has_irq == 0)
711                 goto no_irq;
712
713         /*
714          * Turn off all interrupts once the device is no longer
715          * in use, and clear the data.
716          */
717
718         spin_lock_irq(&rtc_lock);
719         if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
720                 tmp = CMOS_READ(RTC_CONTROL);
721                 tmp &=  ~RTC_PIE;
722                 tmp &=  ~RTC_AIE;
723                 tmp &=  ~RTC_UIE;
724                 CMOS_WRITE(tmp, RTC_CONTROL);
725                 CMOS_READ(RTC_INTR_FLAGS);
726         }
727         if (rtc_status & RTC_TIMER_ON) {
728                 rtc_status &= ~RTC_TIMER_ON;
729                 del_timer(&rtc_irq_timer);
730         }
731         spin_unlock_irq(&rtc_lock);
732
733         if (file->f_flags & FASYNC) {
734                 rtc_fasync (-1, file, 0);
735         }
736 no_irq:
737 #endif
738
739         spin_lock_irq (&rtc_lock);
740         rtc_irq_data = 0;
741         rtc_status &= ~RTC_IS_OPEN;
742         spin_unlock_irq (&rtc_lock);
743         return 0;
744 }
745
746 #ifdef RTC_IRQ
747 /* Called without the kernel lock - fine */
748 static unsigned int rtc_poll(struct file *file, poll_table *wait)
749 {
750         unsigned long l;
751
752         if (rtc_has_irq == 0)
753                 return 0;
754
755         poll_wait(file, &rtc_wait, wait);
756
757         spin_lock_irq (&rtc_lock);
758         l = rtc_irq_data;
759         spin_unlock_irq (&rtc_lock);
760
761         if (l != 0)
762                 return POLLIN | POLLRDNORM;
763         return 0;
764 }
765 #endif
766
767 /*
768  * exported stuffs
769  */
770
771 EXPORT_SYMBOL(rtc_register);
772 EXPORT_SYMBOL(rtc_unregister);
773 EXPORT_SYMBOL(rtc_control);
774
775 int rtc_register(rtc_task_t *task)
776 {
777 #ifndef RTC_IRQ
778         return -EIO;
779 #else
780         if (task == NULL || task->func == NULL)
781                 return -EINVAL;
782         spin_lock_irq(&rtc_lock);
783         if (rtc_status & RTC_IS_OPEN) {
784                 spin_unlock_irq(&rtc_lock);
785                 return -EBUSY;
786         }
787         spin_lock(&rtc_task_lock);
788         if (rtc_callback) {
789                 spin_unlock(&rtc_task_lock);
790                 spin_unlock_irq(&rtc_lock);
791                 return -EBUSY;
792         }
793         rtc_status |= RTC_IS_OPEN;
794         rtc_callback = task;
795         spin_unlock(&rtc_task_lock);
796         spin_unlock_irq(&rtc_lock);
797         return 0;
798 #endif
799 }
800
801 int rtc_unregister(rtc_task_t *task)
802 {
803 #ifndef RTC_IRQ
804         return -EIO;
805 #else
806         unsigned char tmp;
807
808         spin_lock_irq(&rtc_lock);
809         spin_lock(&rtc_task_lock);
810         if (rtc_callback != task) {
811                 spin_unlock(&rtc_task_lock);
812                 spin_unlock_irq(&rtc_lock);
813                 return -ENXIO;
814         }
815         rtc_callback = NULL;
816         
817         /* disable controls */
818         if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
819                 tmp = CMOS_READ(RTC_CONTROL);
820                 tmp &= ~RTC_PIE;
821                 tmp &= ~RTC_AIE;
822                 tmp &= ~RTC_UIE;
823                 CMOS_WRITE(tmp, RTC_CONTROL);
824                 CMOS_READ(RTC_INTR_FLAGS);
825         }
826         if (rtc_status & RTC_TIMER_ON) {
827                 rtc_status &= ~RTC_TIMER_ON;
828                 del_timer(&rtc_irq_timer);
829         }
830         rtc_status &= ~RTC_IS_OPEN;
831         spin_unlock(&rtc_task_lock);
832         spin_unlock_irq(&rtc_lock);
833         return 0;
834 #endif
835 }
836
837 int rtc_control(rtc_task_t *task, unsigned int cmd, unsigned long arg)
838 {
839 #ifndef RTC_IRQ
840         return -EIO;
841 #else
842         spin_lock_irq(&rtc_task_lock);
843         if (rtc_callback != task) {
844                 spin_unlock_irq(&rtc_task_lock);
845                 return -ENXIO;
846         }
847         spin_unlock_irq(&rtc_task_lock);
848         return rtc_do_ioctl(cmd, arg, 1);
849 #endif
850 }
851
852
853 /*
854  *      The various file operations we support.
855  */
856
857 static struct file_operations rtc_fops = {
858         .owner          = THIS_MODULE,
859         .llseek         = no_llseek,
860         .read           = rtc_read,
861 #ifdef RTC_IRQ
862         .poll           = rtc_poll,
863 #endif
864         .ioctl          = rtc_ioctl,
865         .open           = rtc_open,
866         .release        = rtc_release,
867         .fasync         = rtc_fasync,
868 };
869
870 static struct miscdevice rtc_dev=
871 {
872         RTC_MINOR,
873         "rtc",
874         &rtc_fops
875 };
876
877 #ifdef RTC_IRQ
878 static irqreturn_t (*rtc_int_handler_ptr)(int irq, void *dev_id, struct pt_regs *regs);
879 #endif
880
881 static int __init rtc_init(void)
882 {
883 #if defined(__alpha__) || defined(__mips__)
884         unsigned int year, ctrl;
885         unsigned long uip_watchdog;
886         char *guess = NULL;
887 #endif
888 #ifdef __sparc__
889         struct linux_ebus *ebus;
890         struct linux_ebus_device *edev;
891 #ifdef __sparc_v9__
892         struct sparc_isa_bridge *isa_br;
893         struct sparc_isa_device *isa_dev;
894 #endif
895 #endif
896
897 #ifdef __sparc__
898         for_each_ebus(ebus) {
899                 for_each_ebusdev(edev, ebus) {
900                         if(strcmp(edev->prom_name, "rtc") == 0) {
901                                 rtc_port = edev->resource[0].start;
902                                 rtc_irq = edev->irqs[0];
903                                 goto found;
904                         }
905                 }
906         }
907 #ifdef __sparc_v9__
908         for_each_isa(isa_br) {
909                 for_each_isadev(isa_dev, isa_br) {
910                         if (strcmp(isa_dev->prom_name, "rtc") == 0) {
911                                 rtc_port = isa_dev->resource.start;
912                                 rtc_irq = isa_dev->irq;
913                                 goto found;
914                         }
915                 }
916         }
917 #endif
918         printk(KERN_ERR "rtc_init: no PC rtc found\n");
919         return -EIO;
920
921 found:
922         if (rtc_irq == PCI_IRQ_NONE) {
923                 rtc_has_irq = 0;
924                 goto no_irq;
925         }
926
927         /*
928          * XXX Interrupt pin #7 in Espresso is shared between RTC and
929          * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
930          * is asking for trouble with add-on boards. Change to SA_SHIRQ.
931          */
932         if (request_irq(rtc_irq, rtc_interrupt, SA_INTERRUPT, "rtc", (void *)&rtc_port)) {
933                 /*
934                  * Standard way for sparc to print irq's is to use
935                  * __irq_itoa(). I think for EBus it's ok to use %d.
936                  */
937                 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
938                 return -EIO;
939         }
940 no_irq:
941 #else
942         if (!request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc")) {
943                 printk(KERN_ERR "rtc: I/O port %d is not free.\n", RTC_PORT (0));
944                 return -EIO;
945         }
946
947 #ifdef RTC_IRQ
948         if (is_hpet_enabled()) {
949                 rtc_int_handler_ptr = hpet_rtc_interrupt;
950         } else {
951                 rtc_int_handler_ptr = rtc_interrupt;
952         }
953
954         if(request_irq(RTC_IRQ, rtc_int_handler_ptr, SA_INTERRUPT, "rtc", NULL)) {
955                 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
956                 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
957                 release_region(RTC_PORT(0), RTC_IO_EXTENT);
958                 return -EIO;
959         }
960         hpet_rtc_timer_init();
961
962 #endif
963
964 #endif /* __sparc__ vs. others */
965
966         if (misc_register(&rtc_dev)) {
967 #ifdef RTC_IRQ
968                 free_irq(RTC_IRQ, NULL);
969 #endif
970                 release_region(RTC_PORT(0), RTC_IO_EXTENT);
971                 return -ENODEV;
972         }
973         if (create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL) == NULL) {
974 #ifdef RTC_IRQ
975                 free_irq(RTC_IRQ, NULL);
976 #endif
977                 release_region(RTC_PORT(0), RTC_IO_EXTENT);
978                 misc_deregister(&rtc_dev);
979                 return -ENOMEM;
980         }
981
982 #if defined(__alpha__) || defined(__mips__)
983         rtc_freq = HZ;
984         
985         /* Each operating system on an Alpha uses its own epoch.
986            Let's try to guess which one we are using now. */
987         
988         uip_watchdog = jiffies;
989         if (rtc_is_updating() != 0)
990                 while (jiffies - uip_watchdog < 2*HZ/100) { 
991                         barrier();
992                         cpu_relax();
993                 }
994         
995         spin_lock_irq(&rtc_lock);
996         year = CMOS_READ(RTC_YEAR);
997         ctrl = CMOS_READ(RTC_CONTROL);
998         spin_unlock_irq(&rtc_lock);
999         
1000         if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1001                 BCD_TO_BIN(year);       /* This should never happen... */
1002         
1003         if (year < 20) {
1004                 epoch = 2000;
1005                 guess = "SRM (post-2000)";
1006         } else if (year >= 20 && year < 48) {
1007                 epoch = 1980;
1008                 guess = "ARC console";
1009         } else if (year >= 48 && year < 72) {
1010                 epoch = 1952;
1011                 guess = "Digital UNIX";
1012 #if defined(__mips__)
1013         } else if (year >= 72 && year < 74) {
1014                 epoch = 2000;
1015                 guess = "Digital DECstation";
1016 #else
1017         } else if (year >= 70) {
1018                 epoch = 1900;
1019                 guess = "Standard PC (1900)";
1020 #endif
1021         }
1022         if (guess)
1023                 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n", guess, epoch);
1024 #endif
1025 #ifdef RTC_IRQ
1026         if (rtc_has_irq == 0)
1027                 goto no_irq2;
1028
1029         init_timer(&rtc_irq_timer);
1030         rtc_irq_timer.function = rtc_dropped_irq;
1031         spin_lock_irq(&rtc_lock);
1032         rtc_freq = 1024;
1033         if (!hpet_set_periodic_freq(rtc_freq)) {
1034                 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
1035                 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
1036         }
1037         spin_unlock_irq(&rtc_lock);
1038 no_irq2:
1039 #endif
1040
1041         (void) init_sysctl();
1042
1043         printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
1044
1045         return 0;
1046 }
1047
1048 static void __exit rtc_exit (void)
1049 {
1050         cleanup_sysctl();
1051         remove_proc_entry ("driver/rtc", NULL);
1052         misc_deregister(&rtc_dev);
1053
1054 #ifdef __sparc__
1055         if (rtc_has_irq)
1056                 free_irq (rtc_irq, &rtc_port);
1057 #else
1058         release_region (RTC_PORT (0), RTC_IO_EXTENT);
1059 #ifdef RTC_IRQ
1060         if (rtc_has_irq)
1061                 free_irq (RTC_IRQ, NULL);
1062 #endif
1063 #endif /* __sparc__ */
1064 }
1065
1066 module_init(rtc_init);
1067 module_exit(rtc_exit);
1068
1069 #ifdef RTC_IRQ
1070 /*
1071  *      At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
1072  *      (usually during an IDE disk interrupt, with IRQ unmasking off)
1073  *      Since the interrupt handler doesn't get called, the IRQ status
1074  *      byte doesn't get read, and the RTC stops generating interrupts.
1075  *      A timer is set, and will call this function if/when that happens.
1076  *      To get it out of this stalled state, we just read the status.
1077  *      At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
1078  *      (You *really* shouldn't be trying to use a non-realtime system 
1079  *      for something that requires a steady > 1KHz signal anyways.)
1080  */
1081
1082 static void rtc_dropped_irq(unsigned long data)
1083 {
1084         unsigned long freq;
1085
1086         spin_lock_irq (&rtc_lock);
1087
1088         if (hpet_rtc_dropped_irq()) {
1089                 spin_unlock_irq(&rtc_lock);
1090                 return;
1091         }
1092
1093         /* Just in case someone disabled the timer from behind our back... */
1094         if (rtc_status & RTC_TIMER_ON)
1095                 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
1096
1097         rtc_irq_data += ((rtc_freq/HZ)<<8);
1098         rtc_irq_data &= ~0xff;
1099         rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);     /* restart */
1100
1101         freq = rtc_freq;
1102
1103         spin_unlock_irq(&rtc_lock);
1104
1105         printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
1106
1107         /* Now we have new data */
1108         wake_up_interruptible(&rtc_wait);
1109
1110         kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
1111 }
1112 #endif
1113
1114 /*
1115  *      Info exported via "/proc/driver/rtc".
1116  */
1117
1118 static int rtc_proc_output (char *buf)
1119 {
1120 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
1121 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
1122         char *p;
1123         struct rtc_time tm;
1124         unsigned char batt, ctrl;
1125         unsigned long freq;
1126
1127         spin_lock_irq(&rtc_lock);
1128         batt = CMOS_READ(RTC_VALID) & RTC_VRT;
1129         ctrl = CMOS_READ(RTC_CONTROL);
1130         freq = rtc_freq;
1131         spin_unlock_irq(&rtc_lock);
1132
1133         p = buf;
1134
1135         rtc_get_rtc_time(&tm);
1136
1137         /*
1138          * There is no way to tell if the luser has the RTC set for local
1139          * time or for Universal Standard Time (GMT). Probably local though.
1140          */
1141         p += sprintf(p,
1142                      "rtc_time\t: %02d:%02d:%02d\n"
1143                      "rtc_date\t: %04d-%02d-%02d\n"
1144                      "rtc_epoch\t: %04lu\n",
1145                      tm.tm_hour, tm.tm_min, tm.tm_sec,
1146                      tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
1147
1148         get_rtc_alm_time(&tm);
1149
1150         /*
1151          * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
1152          * match any value for that particular field. Values that are
1153          * greater than a valid time, but less than 0xc0 shouldn't appear.
1154          */
1155         p += sprintf(p, "alarm\t\t: ");
1156         if (tm.tm_hour <= 24)
1157                 p += sprintf(p, "%02d:", tm.tm_hour);
1158         else
1159                 p += sprintf(p, "**:");
1160
1161         if (tm.tm_min <= 59)
1162                 p += sprintf(p, "%02d:", tm.tm_min);
1163         else
1164                 p += sprintf(p, "**:");
1165
1166         if (tm.tm_sec <= 59)
1167                 p += sprintf(p, "%02d\n", tm.tm_sec);
1168         else
1169                 p += sprintf(p, "**\n");
1170
1171         p += sprintf(p,
1172                      "DST_enable\t: %s\n"
1173                      "BCD\t\t: %s\n"
1174                      "24hr\t\t: %s\n"
1175                      "square_wave\t: %s\n"
1176                      "alarm_IRQ\t: %s\n"
1177                      "update_IRQ\t: %s\n"
1178                      "periodic_IRQ\t: %s\n"
1179                      "periodic_freq\t: %ld\n"
1180                      "batt_status\t: %s\n",
1181                      YN(RTC_DST_EN),
1182                      NY(RTC_DM_BINARY),
1183                      YN(RTC_24H),
1184                      YN(RTC_SQWE),
1185                      YN(RTC_AIE),
1186                      YN(RTC_UIE),
1187                      YN(RTC_PIE),
1188                      freq,
1189                      batt ? "okay" : "dead");
1190
1191         return  p - buf;
1192 #undef YN
1193 #undef NY
1194 }
1195
1196 static int rtc_read_proc(char *page, char **start, off_t off,
1197                          int count, int *eof, void *data)
1198 {
1199         int len = rtc_proc_output (page);
1200         if (len <= off+count) *eof = 1;
1201         *start = page + off;
1202         len -= off;
1203         if (len>count) len = count;
1204         if (len<0) len = 0;
1205         return len;
1206 }
1207
1208 void rtc_get_rtc_time(struct rtc_time *rtc_tm)
1209 {
1210         unsigned long uip_watchdog = jiffies;
1211         unsigned char ctrl;
1212 #ifdef CONFIG_DECSTATION
1213         unsigned int real_year;
1214 #endif
1215
1216         /*
1217          * read RTC once any update in progress is done. The update
1218          * can take just over 2ms. We wait 10 to 20ms. There is no need to
1219          * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
1220          * If you need to know *exactly* when a second has started, enable
1221          * periodic update complete interrupts, (via ioctl) and then 
1222          * immediately read /dev/rtc which will block until you get the IRQ.
1223          * Once the read clears, read the RTC time (again via ioctl). Easy.
1224          */
1225
1226         if (rtc_is_updating() != 0)
1227                 while (jiffies - uip_watchdog < 2*HZ/100) {
1228                         barrier();
1229                         cpu_relax();
1230                 }
1231
1232         /*
1233          * Only the values that we read from the RTC are set. We leave
1234          * tm_wday, tm_yday and tm_isdst untouched. Even though the
1235          * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
1236          * by the RTC when initially set to a non-zero value.
1237          */
1238         spin_lock_irq(&rtc_lock);
1239         rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1240         rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1241         rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1242         rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1243         rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1244         rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1245 #ifdef CONFIG_DECSTATION
1246         real_year = CMOS_READ(RTC_DEC_YEAR);
1247 #endif
1248         ctrl = CMOS_READ(RTC_CONTROL);
1249         spin_unlock_irq(&rtc_lock);
1250
1251         if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1252         {
1253                 BCD_TO_BIN(rtc_tm->tm_sec);
1254                 BCD_TO_BIN(rtc_tm->tm_min);
1255                 BCD_TO_BIN(rtc_tm->tm_hour);
1256                 BCD_TO_BIN(rtc_tm->tm_mday);
1257                 BCD_TO_BIN(rtc_tm->tm_mon);
1258                 BCD_TO_BIN(rtc_tm->tm_year);
1259         }
1260
1261 #ifdef CONFIG_DECSTATION
1262         rtc_tm->tm_year += real_year - 72;
1263 #endif
1264
1265         /*
1266          * Account for differences between how the RTC uses the values
1267          * and how they are defined in a struct rtc_time;
1268          */
1269         if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
1270                 rtc_tm->tm_year += 100;
1271
1272         rtc_tm->tm_mon--;
1273 }
1274
1275 static void get_rtc_alm_time(struct rtc_time *alm_tm)
1276 {
1277         unsigned char ctrl;
1278
1279         /*
1280          * Only the values that we read from the RTC are set. That
1281          * means only tm_hour, tm_min, and tm_sec.
1282          */
1283         spin_lock_irq(&rtc_lock);
1284         alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
1285         alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
1286         alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
1287         ctrl = CMOS_READ(RTC_CONTROL);
1288         spin_unlock_irq(&rtc_lock);
1289
1290         if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1291         {
1292                 BCD_TO_BIN(alm_tm->tm_sec);
1293                 BCD_TO_BIN(alm_tm->tm_min);
1294                 BCD_TO_BIN(alm_tm->tm_hour);
1295         }
1296 }
1297
1298 #ifdef RTC_IRQ
1299 /*
1300  * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
1301  * Rumour has it that if you frob the interrupt enable/disable
1302  * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
1303  * ensure you actually start getting interrupts. Probably for
1304  * compatibility with older/broken chipset RTC implementations.
1305  * We also clear out any old irq data after an ioctl() that
1306  * meddles with the interrupt enable/disable bits.
1307  */
1308
1309 static void mask_rtc_irq_bit(unsigned char bit)
1310 {
1311         unsigned char val;
1312
1313         spin_lock_irq(&rtc_lock);
1314         if (hpet_mask_rtc_irq_bit(bit)) {
1315                 spin_unlock_irq(&rtc_lock);
1316                 return;
1317         }
1318         val = CMOS_READ(RTC_CONTROL);
1319         val &=  ~bit;
1320         CMOS_WRITE(val, RTC_CONTROL);
1321         CMOS_READ(RTC_INTR_FLAGS);
1322
1323         rtc_irq_data = 0;
1324         spin_unlock_irq(&rtc_lock);
1325 }
1326
1327 static void set_rtc_irq_bit(unsigned char bit)
1328 {
1329         unsigned char val;
1330
1331         spin_lock_irq(&rtc_lock);
1332         if (hpet_set_rtc_irq_bit(bit)) {
1333                 spin_unlock_irq(&rtc_lock);
1334                 return;
1335         }
1336         val = CMOS_READ(RTC_CONTROL);
1337         val |= bit;
1338         CMOS_WRITE(val, RTC_CONTROL);
1339         CMOS_READ(RTC_INTR_FLAGS);
1340
1341         rtc_irq_data = 0;
1342         spin_unlock_irq(&rtc_lock);
1343 }
1344 #endif
1345
1346 MODULE_AUTHOR("Paul Gortmaker");
1347 MODULE_LICENSE("GPL");
1348 MODULE_ALIAS_MISCDEV(RTC_MINOR);