Linux 2.3.1pre3
[davej-history.git] / drivers / sbus / char / rtc.c
blob1576a4a0b167e87e4ff0eaeeb9b8117b41e0fc25
1 /* $Id: rtc.c,v 1.13 1998/08/26 10:29:44 davem Exp $
3 * Linux/SPARC Real Time Clock Driver
4 * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
6 * This is a little driver that lets a user-level program access
7 * the SPARC Mostek real time clock chip. It is no use unless you
8 * use the modified clock utility.
10 * Get the modified clock utility from:
11 * ftp://vger.rutgers.edu/pub/linux/Sparc/userland/clock.c
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/miscdevice.h>
18 #include <linux/malloc.h>
19 #include <linux/fcntl.h>
20 #include <linux/poll.h>
21 #include <linux/init.h>
22 #include <asm/mostek.h>
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
25 #include <asm/rtc.h>
27 static int rtc_busy = 0;
29 /* Retrieve the current date and time from the real time clock. */
30 void get_rtc_time(struct rtc_time *t)
32 register struct mostek48t02 *regs = mstk48t02_regs;
33 unsigned long flags;
35 save_flags(flags);
36 cli();
37 regs->creg |= MSTK_CREG_READ;
39 t->sec = MSTK_REG_SEC(regs);
40 t->min = MSTK_REG_MIN(regs);
41 t->hour = MSTK_REG_HOUR(regs);
42 t->dow = MSTK_REG_DOW(regs);
43 t->dom = MSTK_REG_DOM(regs);
44 t->month = MSTK_REG_MONTH(regs);
45 t->year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
47 regs->creg &= ~MSTK_CREG_READ;
48 restore_flags(flags);
51 /* Set the current date and time inthe real time clock. */
52 void set_rtc_time(struct rtc_time *t)
54 register struct mostek48t02 *regs = mstk48t02_regs;
55 unsigned long flags;
57 save_flags(flags);
58 cli();
59 regs->creg |= MSTK_CREG_WRITE;
61 MSTK_SET_REG_SEC(regs,t->sec);
62 MSTK_SET_REG_MIN(regs,t->min);
63 MSTK_SET_REG_HOUR(regs,t->hour);
64 MSTK_SET_REG_DOW(regs,t->dow);
65 MSTK_SET_REG_DOM(regs,t->dom);
66 MSTK_SET_REG_MONTH(regs,t->month);
67 MSTK_SET_REG_YEAR(regs,t->year - MSTK_YEAR_ZERO);
69 regs->creg &= ~MSTK_CREG_WRITE;
70 restore_flags(flags);
73 static long long rtc_lseek(struct file *file, long long offset, int origin)
75 return -ESPIPE;
78 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
79 unsigned long arg)
81 struct rtc_time rtc_tm;
83 switch (cmd)
85 case RTCGET:
86 get_rtc_time(&rtc_tm);
88 copy_to_user_ret((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time), -EFAULT);
90 return 0;
93 case RTCSET:
94 if (!capable(CAP_SYS_TIME))
95 return -EPERM;
97 copy_from_user_ret(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time), -EFAULT);
99 set_rtc_time(&rtc_tm);
101 return 0;
103 default:
104 return -EINVAL;
108 static int rtc_open(struct inode *inode, struct file *file)
110 if (rtc_busy)
111 return -EBUSY;
113 rtc_busy = 1;
115 MOD_INC_USE_COUNT;
117 return 0;
120 static int rtc_release(struct inode *inode, struct file *file)
122 MOD_DEC_USE_COUNT;
123 rtc_busy = 0;
124 return 0;
127 static struct file_operations rtc_fops = {
128 rtc_lseek,
129 NULL, /* rtc_read */
130 NULL, /* rtc_write */
131 NULL, /* rtc_readdir */
132 NULL, /* rtc_poll */
133 rtc_ioctl,
134 NULL, /* rtc_mmap */
135 rtc_open,
136 NULL, /* flush */
137 rtc_release
140 static struct miscdevice rtc_dev = { RTC_MINOR, "rtc", &rtc_fops };
142 EXPORT_NO_SYMBOLS;
144 #ifdef MODULE
145 int init_module(void)
146 #else
147 __initfunc(int rtc_init(void))
148 #endif
150 int error;
152 error = misc_register(&rtc_dev);
153 if (error) {
154 printk(KERN_ERR "rtc: unable to get misc minor\n");
155 return error;
158 return 0;
161 #ifdef MODULE
162 void cleanup_module(void)
164 misc_deregister(&rtc_dev);
166 #endif