VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / sbus / char / rtc.c
1 /* $Id: rtc.c,v 1.28 2001/10/08 22:19:51 davem Exp $
2  *
3  * Linux/SPARC Real Time Clock Driver
4  * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
5  *
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.
9  *
10  * Get the modified clock utility from:
11  *   ftp://vger.kernel.org/pub/linux/Sparc/userland/clock.c
12  */
13
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/miscdevice.h>
18 #include <linux/slab.h>
19 #include <linux/fcntl.h>
20 #include <linux/poll.h>
21 #include <linux/init.h>
22 #include <linux/smp_lock.h>
23 #include <asm/mostek.h>
24 #include <asm/system.h>
25 #include <asm/uaccess.h>
26 #include <asm/rtc.h>
27
28 static int rtc_busy = 0;
29
30 /* Retrieve the current date and time from the real time clock. */
31 static void get_rtc_time(struct rtc_time *t)
32 {
33         unsigned long regs = mstk48t02_regs;
34         u8 tmp;
35
36         spin_lock_irq(&mostek_lock);
37
38         tmp = mostek_read(regs + MOSTEK_CREG);
39         tmp |= MSTK_CREG_READ;
40         mostek_write(regs + MOSTEK_CREG, tmp);
41
42         t->sec = MSTK_REG_SEC(regs);
43         t->min = MSTK_REG_MIN(regs);
44         t->hour = MSTK_REG_HOUR(regs);
45         t->dow = MSTK_REG_DOW(regs);
46         t->dom = MSTK_REG_DOM(regs);
47         t->month = MSTK_REG_MONTH(regs);
48         t->year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
49
50         tmp = mostek_read(regs + MOSTEK_CREG);
51         tmp &= ~MSTK_CREG_READ;
52         mostek_write(regs + MOSTEK_CREG, tmp);
53
54         spin_unlock_irq(&mostek_lock);
55 }
56
57 /* Set the current date and time inthe real time clock. */
58 void set_rtc_time(struct rtc_time *t)
59 {
60         unsigned long regs = mstk48t02_regs;
61         u8 tmp;
62
63         spin_lock_irq(&mostek_lock);
64
65         tmp = mostek_read(regs + MOSTEK_CREG);
66         tmp |= MSTK_CREG_WRITE;
67         mostek_write(regs + MOSTEK_CREG, tmp);
68
69         MSTK_SET_REG_SEC(regs,t->sec);
70         MSTK_SET_REG_MIN(regs,t->min);
71         MSTK_SET_REG_HOUR(regs,t->hour);
72         MSTK_SET_REG_DOW(regs,t->dow);
73         MSTK_SET_REG_DOM(regs,t->dom);
74         MSTK_SET_REG_MONTH(regs,t->month);
75         MSTK_SET_REG_YEAR(regs,t->year - MSTK_YEAR_ZERO);
76
77         tmp = mostek_read(regs + MOSTEK_CREG);
78         tmp &= ~MSTK_CREG_WRITE;
79         mostek_write(regs + MOSTEK_CREG, tmp);
80
81         spin_unlock_irq(&mostek_lock);
82 }
83
84 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
85         unsigned long arg)
86 {
87         struct rtc_time rtc_tm;
88         void __user *argp = (void __user *)arg;
89
90         switch (cmd)
91         {
92         case RTCGET:
93                 memset(&rtc_tm, 0, sizeof(struct rtc_time));
94                 get_rtc_time(&rtc_tm);
95
96                 if (copy_to_user(argp, &rtc_tm, sizeof(struct rtc_time)))
97                         return -EFAULT;
98
99                 return 0;
100
101
102         case RTCSET:
103                 if (!capable(CAP_SYS_TIME))
104                         return -EPERM;
105
106                 if (copy_from_user(&rtc_tm, argp, sizeof(struct rtc_time)))
107                         return -EFAULT;
108
109                 set_rtc_time(&rtc_tm);
110
111                 return 0;
112
113         default:
114                 return -EINVAL;
115         }
116 }
117
118 static int rtc_open(struct inode *inode, struct file *file)
119 {
120         int ret;
121
122         spin_lock_irq(&mostek_lock);
123         if (rtc_busy) {
124                 ret = -EBUSY;
125         } else {
126                 rtc_busy = 1;
127                 ret = 0;
128         }
129         spin_unlock_irq(&mostek_lock);
130
131         return ret;
132 }
133
134 static int rtc_release(struct inode *inode, struct file *file)
135 {
136         rtc_busy = 0;
137
138         return 0;
139 }
140
141 static struct file_operations rtc_fops = {
142         .owner =        THIS_MODULE,
143         .llseek =       no_llseek,
144         .ioctl =        rtc_ioctl,
145         .open =         rtc_open,
146         .release =      rtc_release,
147 };
148
149 static struct miscdevice rtc_dev = { RTC_MINOR, "rtc", &rtc_fops };
150
151 static int __init rtc_sun_init(void)
152 {
153         int error;
154
155         /* It is possible we are being driven by some other RTC chip
156          * and thus another RTC driver is handling things.
157          */
158         if (mstk48t02_regs == 0)
159                 return -ENODEV;
160
161         error = misc_register(&rtc_dev);
162         if (error) {
163                 printk(KERN_ERR "rtc: unable to get misc minor for Mostek\n");
164                 return error;
165         }
166
167         return 0;
168 }
169
170 static void __exit rtc_sun_cleanup(void)
171 {
172         misc_deregister(&rtc_dev);
173 }
174
175 module_init(rtc_sun_init);
176 module_exit(rtc_sun_cleanup);
177 MODULE_LICENSE("GPL");