Merge branch 'core/topology' of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[pandora-kernel.git] / drivers / char / efirtc.c
1 /*
2  * EFI Time Services Driver for Linux
3  *
4  * Copyright (C) 1999 Hewlett-Packard Co
5  * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com>
6  *
7  * Based on skeleton from the drivers/char/rtc.c driver by P. Gortmaker
8  *
9  * This code provides an architected & portable interface to the real time
10  * clock by using EFI instead of direct bit fiddling. The functionalities are 
11  * quite different from the rtc.c driver. The only way to talk to the device 
12  * is by using ioctl(). There is a /proc interface which provides the raw 
13  * information.
14  *
15  * Please note that we have kept the API as close as possible to the
16  * legacy RTC. The standard /sbin/hwclock program should work normally 
17  * when used to get/set the time.
18  *
19  * NOTES:
20  *      - Locking is required for safe execution of EFI calls with regards
21  *        to interrupts and SMP.
22  *
23  * TODO (December 1999):
24  *      - provide the API to set/get the WakeUp Alarm (different from the
25  *        rtc.c alarm).
26  *      - SMP testing
27  *      - Add module support
28  */
29
30
31 #include <linux/smp_lock.h>
32 #include <linux/types.h>
33 #include <linux/errno.h>
34 #include <linux/miscdevice.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/rtc.h>
38 #include <linux/proc_fs.h>
39 #include <linux/efi.h>
40
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
43
44 #define EFI_RTC_VERSION         "0.4"
45
46 #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
47 /*
48  * EFI Epoch is 1/1/1998
49  */
50 #define EFI_RTC_EPOCH           1998
51
52 static DEFINE_SPINLOCK(efi_rtc_lock);
53
54 static int efi_rtc_ioctl(struct inode *inode, struct file *file,
55                      unsigned int cmd, unsigned long arg);
56
57 #define is_leap(year) \
58           ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
59
60 static const unsigned short int __mon_yday[2][13] =
61 {
62         /* Normal years.  */
63         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
64         /* Leap years.  */  
65         { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
66 };
67
68 /*
69  * returns day of the year [0-365]
70  */
71 static inline int
72 compute_yday(efi_time_t *eft)
73 {
74         /* efi_time_t.month is in the [1-12] so, we need -1 */
75         return  __mon_yday[is_leap(eft->year)][eft->month-1]+ eft->day -1;
76 }
77 /*
78  * returns day of the week [0-6] 0=Sunday
79  *
80  * Don't try to provide a year that's before 1998, please !
81  */
82 static int
83 compute_wday(efi_time_t *eft)
84 {
85         int y;
86         int ndays = 0;
87
88         if ( eft->year < 1998 ) {
89                 printk(KERN_ERR "efirtc: EFI year < 1998, invalid date\n");
90                 return -1;
91         }
92
93         for(y=EFI_RTC_EPOCH; y < eft->year; y++ ) {
94                 ndays += 365 + (is_leap(y) ? 1 : 0);
95         }
96         ndays += compute_yday(eft);
97
98         /*
99          * 4=1/1/1998 was a Thursday
100          */
101         return (ndays + 4) % 7;
102 }
103
104 static void
105 convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
106 {
107
108         eft->year       = wtime->tm_year + 1900;
109         eft->month      = wtime->tm_mon + 1; 
110         eft->day        = wtime->tm_mday;
111         eft->hour       = wtime->tm_hour;
112         eft->minute     = wtime->tm_min;
113         eft->second     = wtime->tm_sec;
114         eft->nanosecond = 0; 
115         eft->daylight   = wtime->tm_isdst ? EFI_ISDST: 0;
116         eft->timezone   = EFI_UNSPECIFIED_TIMEZONE;
117 }
118
119 static void
120 convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
121 {
122         memset(wtime, 0, sizeof(*wtime));
123         wtime->tm_sec  = eft->second;
124         wtime->tm_min  = eft->minute;
125         wtime->tm_hour = eft->hour;
126         wtime->tm_mday = eft->day;
127         wtime->tm_mon  = eft->month - 1;
128         wtime->tm_year = eft->year - 1900;
129
130         /* day of the week [0-6], Sunday=0 */
131         wtime->tm_wday = compute_wday(eft);
132
133         /* day in the year [1-365]*/
134         wtime->tm_yday = compute_yday(eft);
135
136
137         switch (eft->daylight & EFI_ISDST) {
138                 case EFI_ISDST:
139                         wtime->tm_isdst = 1;
140                         break;
141                 case EFI_TIME_ADJUST_DAYLIGHT:
142                         wtime->tm_isdst = 0;
143                         break;
144                 default:
145                         wtime->tm_isdst = -1;
146         }
147 }
148
149 static int
150 efi_rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
151                      unsigned long arg)
152 {
153
154         efi_status_t    status;
155         unsigned long   flags;
156         efi_time_t      eft;
157         efi_time_cap_t  cap;
158         struct rtc_time wtime;
159         struct rtc_wkalrm __user *ewp;
160         unsigned char   enabled, pending;
161
162         switch (cmd) {
163                 case RTC_UIE_ON:
164                 case RTC_UIE_OFF:
165                 case RTC_PIE_ON:
166                 case RTC_PIE_OFF:
167                 case RTC_AIE_ON:
168                 case RTC_AIE_OFF:
169                 case RTC_ALM_SET:
170                 case RTC_ALM_READ:
171                 case RTC_IRQP_READ:
172                 case RTC_IRQP_SET:
173                 case RTC_EPOCH_READ:
174                 case RTC_EPOCH_SET:
175                         return -EINVAL;
176
177                 case RTC_RD_TIME:
178
179                         spin_lock_irqsave(&efi_rtc_lock, flags);
180
181                         status = efi.get_time(&eft, &cap);
182
183                         spin_unlock_irqrestore(&efi_rtc_lock,flags);
184
185                         if (status != EFI_SUCCESS) {
186                                 /* should never happen */
187                                 printk(KERN_ERR "efitime: can't read time\n");
188                                 return -EINVAL;
189                         }
190
191                         convert_from_efi_time(&eft, &wtime);
192
193                         return copy_to_user((void __user *)arg, &wtime,
194                                             sizeof (struct rtc_time)) ? - EFAULT : 0;
195
196                 case RTC_SET_TIME:
197
198                         if (!capable(CAP_SYS_TIME)) return -EACCES;
199
200                         if (copy_from_user(&wtime, (struct rtc_time __user *)arg,
201                                            sizeof(struct rtc_time)) )
202                                 return -EFAULT;
203
204                         convert_to_efi_time(&wtime, &eft);
205
206                         spin_lock_irqsave(&efi_rtc_lock, flags);
207
208                         status = efi.set_time(&eft);
209
210                         spin_unlock_irqrestore(&efi_rtc_lock,flags);
211
212                         return status == EFI_SUCCESS ? 0 : -EINVAL;
213
214                 case RTC_WKALM_SET:
215
216                         if (!capable(CAP_SYS_TIME)) return -EACCES;
217
218                         ewp = (struct rtc_wkalrm __user *)arg;
219
220                         if (  get_user(enabled, &ewp->enabled)
221                            || copy_from_user(&wtime, &ewp->time, sizeof(struct rtc_time)) )
222                                 return -EFAULT;
223
224                         convert_to_efi_time(&wtime, &eft);
225
226                         spin_lock_irqsave(&efi_rtc_lock, flags);
227                         /*
228                          * XXX Fixme:
229                          * As of EFI 0.92 with the firmware I have on my
230                          * machine this call does not seem to work quite
231                          * right
232                          */
233                         status = efi.set_wakeup_time((efi_bool_t)enabled, &eft);
234
235                         spin_unlock_irqrestore(&efi_rtc_lock,flags);
236
237                         return status == EFI_SUCCESS ? 0 : -EINVAL;
238
239                 case RTC_WKALM_RD:
240
241                         spin_lock_irqsave(&efi_rtc_lock, flags);
242
243                         status = efi.get_wakeup_time((efi_bool_t *)&enabled, (efi_bool_t *)&pending, &eft);
244
245                         spin_unlock_irqrestore(&efi_rtc_lock,flags);
246
247                         if (status != EFI_SUCCESS) return -EINVAL;
248
249                         ewp = (struct rtc_wkalrm __user *)arg;
250
251                         if (  put_user(enabled, &ewp->enabled)
252                            || put_user(pending, &ewp->pending)) return -EFAULT;
253
254                         convert_from_efi_time(&eft, &wtime);
255
256                         return copy_to_user(&ewp->time, &wtime,
257                                             sizeof(struct rtc_time)) ? -EFAULT : 0;
258         }
259         return -EINVAL;
260 }
261
262 /*
263  *      We enforce only one user at a time here with the open/close.
264  *      Also clear the previous interrupt data on an open, and clean
265  *      up things on a close.
266  */
267
268 static int
269 efi_rtc_open(struct inode *inode, struct file *file)
270 {
271         /*
272          * nothing special to do here
273          * We do accept multiple open files at the same time as we
274          * synchronize on the per call operation.
275          */
276         cycle_kernel_lock();
277         return 0;
278 }
279
280 static int
281 efi_rtc_close(struct inode *inode, struct file *file)
282 {
283         return 0;
284 }
285
286 /*
287  *      The various file operations we support.
288  */
289
290 static const struct file_operations efi_rtc_fops = {
291         .owner          = THIS_MODULE,
292         .ioctl          = efi_rtc_ioctl,
293         .open           = efi_rtc_open,
294         .release        = efi_rtc_close,
295 };
296
297 static struct miscdevice efi_rtc_dev=
298 {
299         EFI_RTC_MINOR,
300         "efirtc",
301         &efi_rtc_fops
302 };
303
304 /*
305  *      We export RAW EFI information to /proc/driver/efirtc
306  */
307 static int
308 efi_rtc_get_status(char *buf)
309 {
310         efi_time_t      eft, alm;
311         efi_time_cap_t  cap;
312         char            *p = buf;
313         efi_bool_t      enabled, pending;       
314         unsigned long   flags;
315
316         memset(&eft, 0, sizeof(eft));
317         memset(&alm, 0, sizeof(alm));
318         memset(&cap, 0, sizeof(cap));
319
320         spin_lock_irqsave(&efi_rtc_lock, flags);
321
322         efi.get_time(&eft, &cap);
323         efi.get_wakeup_time(&enabled, &pending, &alm);
324
325         spin_unlock_irqrestore(&efi_rtc_lock,flags);
326
327         p += sprintf(p,
328                      "Time           : %u:%u:%u.%09u\n"
329                      "Date           : %u-%u-%u\n"
330                      "Daylight       : %u\n",
331                      eft.hour, eft.minute, eft.second, eft.nanosecond, 
332                      eft.year, eft.month, eft.day,
333                      eft.daylight);
334
335         if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
336                 p += sprintf(p, "Timezone       : unspecified\n");
337         else
338                 /* XXX fixme: convert to string? */
339                 p += sprintf(p, "Timezone       : %u\n", eft.timezone);
340                 
341
342         p += sprintf(p,
343                      "Alarm Time     : %u:%u:%u.%09u\n"
344                      "Alarm Date     : %u-%u-%u\n"
345                      "Alarm Daylight : %u\n"
346                      "Enabled        : %s\n"
347                      "Pending        : %s\n",
348                      alm.hour, alm.minute, alm.second, alm.nanosecond, 
349                      alm.year, alm.month, alm.day, 
350                      alm.daylight,
351                      enabled == 1 ? "yes" : "no",
352                      pending == 1 ? "yes" : "no");
353
354         if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
355                 p += sprintf(p, "Timezone       : unspecified\n");
356         else
357                 /* XXX fixme: convert to string? */
358                 p += sprintf(p, "Timezone       : %u\n", alm.timezone);
359
360         /*
361          * now prints the capabilities
362          */
363         p += sprintf(p,
364                      "Resolution     : %u\n"
365                      "Accuracy       : %u\n"
366                      "SetstoZero     : %u\n",
367                       cap.resolution, cap.accuracy, cap.sets_to_zero);
368
369         return  p - buf;
370 }
371
372 static int
373 efi_rtc_read_proc(char *page, char **start, off_t off,
374                                  int count, int *eof, void *data)
375 {
376         int len = efi_rtc_get_status(page);
377         if (len <= off+count) *eof = 1;
378         *start = page + off;
379         len -= off;
380         if (len>count) len = count;
381         if (len<0) len = 0;
382         return len;
383 }
384
385 static int __init 
386 efi_rtc_init(void)
387 {
388         int ret;
389         struct proc_dir_entry *dir;
390
391         printk(KERN_INFO "EFI Time Services Driver v%s\n", EFI_RTC_VERSION);
392
393         ret = misc_register(&efi_rtc_dev);
394         if (ret) {
395                 printk(KERN_ERR "efirtc: can't misc_register on minor=%d\n",
396                                 EFI_RTC_MINOR);
397                 return ret;
398         }
399
400         dir = create_proc_read_entry ("driver/efirtc", 0, NULL,
401                                       efi_rtc_read_proc, NULL);
402         if (dir == NULL) {
403                 printk(KERN_ERR "efirtc: can't create /proc/driver/efirtc.\n");
404                 misc_deregister(&efi_rtc_dev);
405                 return -1;
406         }
407         return 0;
408 }
409
410 static void __exit
411 efi_rtc_exit(void)
412 {
413         /* not yet used */
414 }
415
416 module_init(efi_rtc_init);
417 module_exit(efi_rtc_exit);
418
419 MODULE_LICENSE("GPL");