2 * EFI Time Services Driver for Linux
4 * Copyright (C) 1999 Hewlett-Packard Co
5 * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com>
7 * Based on skeleton from the drivers/char/rtc.c driver by P. Gortmaker
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
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.
20 * - Locking is required for safe execution of EFI calls with regards
21 * to interrrupts and SMP.
23 * TODO (December 1999):
24 * - provide the API to set/get the WakeUp Alarm (different from the
27 * - Add module support
31 #include <linux/types.h>
32 #include <linux/errno.h>
33 #include <linux/miscdevice.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/rtc.h>
37 #include <linux/proc_fs.h>
38 #include <linux/efi.h>
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
43 #define EFI_RTC_VERSION "0.4"
45 #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
47 * EFI Epoch is 1/1/1998
49 #define EFI_RTC_EPOCH 1998
51 static DEFINE_SPINLOCK(efi_rtc_lock);
53 static int efi_rtc_ioctl(struct inode *inode, struct file *file,
54 unsigned int cmd, unsigned long arg);
56 #define is_leap(year) \
57 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
59 static const unsigned short int __mon_yday[2][13] =
62 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
64 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
68 * returns day of the year [0-365]
71 compute_yday(efi_time_t *eft)
73 /* efi_time_t.month is in the [1-12] so, we need -1 */
74 return __mon_yday[is_leap(eft->year)][eft->month-1]+ eft->day -1;
77 * returns day of the week [0-6] 0=Sunday
79 * Don't try to provide a year that's before 1998, please !
82 compute_wday(efi_time_t *eft)
87 if ( eft->year < 1998 ) {
88 printk(KERN_ERR "efirtc: EFI year < 1998, invalid date\n");
92 for(y=EFI_RTC_EPOCH; y < eft->year; y++ ) {
93 ndays += 365 + (is_leap(y) ? 1 : 0);
95 ndays += compute_yday(eft);
98 * 4=1/1/1998 was a Thursday
100 return (ndays + 4) % 7;
104 convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
107 eft->year = wtime->tm_year + 1900;
108 eft->month = wtime->tm_mon + 1;
109 eft->day = wtime->tm_mday;
110 eft->hour = wtime->tm_hour;
111 eft->minute = wtime->tm_min;
112 eft->second = wtime->tm_sec;
114 eft->daylight = wtime->tm_isdst ? EFI_ISDST: 0;
115 eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
119 convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
121 memset(wtime, 0, sizeof(*wtime));
122 wtime->tm_sec = eft->second;
123 wtime->tm_min = eft->minute;
124 wtime->tm_hour = eft->hour;
125 wtime->tm_mday = eft->day;
126 wtime->tm_mon = eft->month - 1;
127 wtime->tm_year = eft->year - 1900;
129 /* day of the week [0-6], Sunday=0 */
130 wtime->tm_wday = compute_wday(eft);
132 /* day in the year [1-365]*/
133 wtime->tm_yday = compute_yday(eft);
136 switch (eft->daylight & EFI_ISDST) {
140 case EFI_TIME_ADJUST_DAYLIGHT:
144 wtime->tm_isdst = -1;
149 efi_rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
157 struct rtc_time wtime;
158 struct rtc_wkalrm __user *ewp;
159 unsigned char enabled, pending;
178 spin_lock_irqsave(&efi_rtc_lock, flags);
180 status = efi.get_time(&eft, &cap);
182 spin_unlock_irqrestore(&efi_rtc_lock,flags);
184 if (status != EFI_SUCCESS) {
185 /* should never happen */
186 printk(KERN_ERR "efitime: can't read time\n");
190 convert_from_efi_time(&eft, &wtime);
192 return copy_to_user((void __user *)arg, &wtime,
193 sizeof (struct rtc_time)) ? - EFAULT : 0;
197 if (!capable(CAP_SYS_TIME)) return -EACCES;
199 if (copy_from_user(&wtime, (struct rtc_time __user *)arg,
200 sizeof(struct rtc_time)) )
203 convert_to_efi_time(&wtime, &eft);
205 spin_lock_irqsave(&efi_rtc_lock, flags);
207 status = efi.set_time(&eft);
209 spin_unlock_irqrestore(&efi_rtc_lock,flags);
211 return status == EFI_SUCCESS ? 0 : -EINVAL;
215 if (!capable(CAP_SYS_TIME)) return -EACCES;
217 ewp = (struct rtc_wkalrm __user *)arg;
219 if ( get_user(enabled, &ewp->enabled)
220 || copy_from_user(&wtime, &ewp->time, sizeof(struct rtc_time)) )
223 convert_to_efi_time(&wtime, &eft);
225 spin_lock_irqsave(&efi_rtc_lock, flags);
228 * As of EFI 0.92 with the firmware I have on my
229 * machine this call does not seem to work quite
232 status = efi.set_wakeup_time((efi_bool_t)enabled, &eft);
234 spin_unlock_irqrestore(&efi_rtc_lock,flags);
236 return status == EFI_SUCCESS ? 0 : -EINVAL;
240 spin_lock_irqsave(&efi_rtc_lock, flags);
242 status = efi.get_wakeup_time((efi_bool_t *)&enabled, (efi_bool_t *)&pending, &eft);
244 spin_unlock_irqrestore(&efi_rtc_lock,flags);
246 if (status != EFI_SUCCESS) return -EINVAL;
248 ewp = (struct rtc_wkalrm __user *)arg;
250 if ( put_user(enabled, &ewp->enabled)
251 || put_user(pending, &ewp->pending)) return -EFAULT;
253 convert_from_efi_time(&eft, &wtime);
255 return copy_to_user(&ewp->time, &wtime,
256 sizeof(struct rtc_time)) ? -EFAULT : 0;
262 * We enforce only one user at a time here with the open/close.
263 * Also clear the previous interrupt data on an open, and clean
264 * up things on a close.
268 efi_rtc_open(struct inode *inode, struct file *file)
271 * nothing special to do here
272 * We do accept multiple open files at the same time as we
273 * synchronize on the per call operation.
279 efi_rtc_close(struct inode *inode, struct file *file)
285 * The various file operations we support.
288 static struct file_operations efi_rtc_fops = {
289 .owner = THIS_MODULE,
290 .ioctl = efi_rtc_ioctl,
291 .open = efi_rtc_open,
292 .release = efi_rtc_close,
295 static struct miscdevice efi_rtc_dev=
303 * We export RAW EFI information to /proc/driver/efirtc
306 efi_rtc_get_status(char *buf)
311 efi_bool_t enabled, pending;
314 memset(&eft, 0, sizeof(eft));
315 memset(&alm, 0, sizeof(alm));
316 memset(&cap, 0, sizeof(cap));
318 spin_lock_irqsave(&efi_rtc_lock, flags);
320 efi.get_time(&eft, &cap);
321 efi.get_wakeup_time(&enabled, &pending, &alm);
323 spin_unlock_irqrestore(&efi_rtc_lock,flags);
326 "Time : %u:%u:%u.%09u\n"
329 eft.hour, eft.minute, eft.second, eft.nanosecond,
330 eft.year, eft.month, eft.day,
333 if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
334 p += sprintf(p, "Timezone : unspecified\n");
336 /* XXX fixme: convert to string? */
337 p += sprintf(p, "Timezone : %u\n", eft.timezone);
341 "Alarm Time : %u:%u:%u.%09u\n"
342 "Alarm Date : %u-%u-%u\n"
343 "Alarm Daylight : %u\n"
346 alm.hour, alm.minute, alm.second, alm.nanosecond,
347 alm.year, alm.month, alm.day,
349 enabled == 1 ? "yes" : "no",
350 pending == 1 ? "yes" : "no");
352 if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
353 p += sprintf(p, "Timezone : unspecified\n");
355 /* XXX fixme: convert to string? */
356 p += sprintf(p, "Timezone : %u\n", alm.timezone);
359 * now prints the capabilities
365 cap.resolution, cap.accuracy, cap.sets_to_zero);
371 efi_rtc_read_proc(char *page, char **start, off_t off,
372 int count, int *eof, void *data)
374 int len = efi_rtc_get_status(page);
375 if (len <= off+count) *eof = 1;
378 if (len>count) len = count;
387 struct proc_dir_entry *dir;
389 printk(KERN_INFO "EFI Time Services Driver v%s\n", EFI_RTC_VERSION);
391 ret = misc_register(&efi_rtc_dev);
393 printk(KERN_ERR "efirtc: can't misc_register on minor=%d\n",
398 dir = create_proc_read_entry ("driver/efirtc", 0, NULL,
399 efi_rtc_read_proc, NULL);
401 printk(KERN_ERR "efirtc: can't create /proc/driver/efirtc.\n");
402 misc_deregister(&efi_rtc_dev);
414 module_init(efi_rtc_init);
415 module_exit(efi_rtc_exit);
417 MODULE_LICENSE("GPL");