more changes on original files
[linux-2.4.git] / drivers / macintosh / rtc.c
1 /*
2  * Linux/PowerPC Real Time Clock Driver
3  *
4  * heavily based on:
5  * Linux/SPARC Real Time Clock Driver
6  * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
7  *
8  * This is a little driver that lets a user-level program access
9  * the PPC clocks chip. It is no use unless you
10  * use the modified clock utility.
11  *
12  * Get the modified clock utility from:
13  *   ftp://vger.rutgers.edu/pub/linux/Sparc/userland/clock.c
14  */
15
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/miscdevice.h>
20 #include <linux/slab.h>
21 #include <linux/fcntl.h>
22 #include <linux/poll.h>
23 #include <linux/init.h>
24 #include <linux/mc146818rtc.h>
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
27 #include <asm/machdep.h>
28
29 #include <asm/time.h>
30
31 static int rtc_busy = 0;
32
33 /* Retrieve the current date and time from the real time clock. */
34 void get_rtc_time(struct rtc_time *t)
35 {
36         unsigned long nowtime;
37
38         nowtime = (ppc_md.get_rtc_time)();
39
40         to_tm(nowtime, t);
41
42         t->tm_year -= 1900;
43         t->tm_mon -= 1; /* Make sure userland has a 0-based month */
44 }
45
46 /* Set the current date and time in the real time clock. */
47 void set_rtc_time(struct rtc_time *t)
48 {
49         unsigned long nowtime;
50
51         nowtime = mktime(t->tm_year+1900, t->tm_mon+1, t->tm_mday,
52                         t->tm_hour, t->tm_min, t->tm_sec);
53
54         (ppc_md.set_rtc_time)(nowtime);
55 }
56
57 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
58         unsigned long arg)
59 {
60         struct rtc_time rtc_tm;
61
62         switch (cmd)
63         {
64         case RTC_RD_TIME:
65                 if (ppc_md.get_rtc_time)
66                 {
67                         memset(&rtc_tm, 0, sizeof(struct rtc_time));
68                         get_rtc_time(&rtc_tm);
69
70                         if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
71                                 return -EFAULT;
72
73                         return 0;
74                 }
75                 else
76                         return -EINVAL;
77
78         case RTC_SET_TIME:
79                 if (!capable(CAP_SYS_TIME))
80                         return -EPERM;
81
82                 if (ppc_md.set_rtc_time)
83                 {
84                         if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
85                                 return -EFAULT;
86
87                         set_rtc_time(&rtc_tm);
88
89                         return 0;
90                 }
91                 else
92                         return -EINVAL;
93
94         default:
95                 return -EINVAL;
96         }
97 }
98
99 static int rtc_open(struct inode *inode, struct file *file)
100 {
101         if (rtc_busy)
102                 return -EBUSY;
103
104         rtc_busy = 1;
105
106         MOD_INC_USE_COUNT;
107
108         return 0;
109 }
110
111 static int rtc_release(struct inode *inode, struct file *file)
112 {
113         MOD_DEC_USE_COUNT;
114         rtc_busy = 0;
115         return 0;
116 }
117
118 static struct file_operations rtc_fops = {
119         owner:          THIS_MODULE,
120         llseek:         no_llseek,
121         ioctl:          rtc_ioctl,
122         open:           rtc_open,
123         release:        rtc_release
124 };
125
126 static struct miscdevice rtc_dev = { RTC_MINOR, "rtc", &rtc_fops };
127
128 EXPORT_NO_SYMBOLS;
129
130 static int __init rtc_init(void)
131 {
132         int error;
133
134         error = misc_register(&rtc_dev);
135         if (error) {
136                 printk(KERN_ERR "rtc: unable to get misc minor\n");
137                 return error;
138         }
139
140         return 0;
141 }
142
143 static void __exit rtc_exit(void)
144 {
145         misc_deregister(&rtc_dev);
146 }
147
148 module_init(rtc_init);
149 module_exit(rtc_exit);
150 MODULE_LICENSE("GPL");