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 interrupts and SMP.
23 * TODO (December 1999):
24 * - provide the API to set/get the WakeUp Alarm (different from the
27 * - Add module support
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/miscdevice.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/rtc.h>
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <linux/efi.h>
39 #include <linux/uaccess.h>
42 #define EFI_RTC_VERSION "0.4"
44 #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
46 * EFI Epoch is 1/1/1998
48 #define EFI_RTC_EPOCH 1998
50 static DEFINE_SPINLOCK(efi_rtc_lock);
52 static long efi_rtc_ioctl(struct file *file, unsigned int cmd,
55 #define is_leap(year) \
56 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
58 static const unsigned short int __mon_yday[2][13] =
61 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
63 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
67 * returns day of the year [0-365]
70 compute_yday(efi_time_t *eft)
72 /* efi_time_t.month is in the [1-12] so, we need -1 */
73 return __mon_yday[is_leap(eft->year)][eft->month-1]+ eft->day -1;
76 * returns day of the week [0-6] 0=Sunday
78 * Don't try to provide a year that's before 1998, please !
81 compute_wday(efi_time_t *eft)
86 if ( eft->year < 1998 ) {
87 printk(KERN_ERR "efirtc: EFI year < 1998, invalid date\n");
91 for(y=EFI_RTC_EPOCH; y < eft->year; y++ ) {
92 ndays += 365 + (is_leap(y) ? 1 : 0);
94 ndays += compute_yday(eft);
97 * 4=1/1/1998 was a Thursday
99 return (ndays + 4) % 7;
103 convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
106 eft->year = wtime->tm_year + 1900;
107 eft->month = wtime->tm_mon + 1;
108 eft->day = wtime->tm_mday;
109 eft->hour = wtime->tm_hour;
110 eft->minute = wtime->tm_min;
111 eft->second = wtime->tm_sec;
113 eft->daylight = wtime->tm_isdst ? EFI_ISDST: 0;
114 eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
118 convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
120 memset(wtime, 0, sizeof(*wtime));
121 wtime->tm_sec = eft->second;
122 wtime->tm_min = eft->minute;
123 wtime->tm_hour = eft->hour;
124 wtime->tm_mday = eft->day;
125 wtime->tm_mon = eft->month - 1;
126 wtime->tm_year = eft->year - 1900;
128 /* day of the week [0-6], Sunday=0 */
129 wtime->tm_wday = compute_wday(eft);
131 /* day in the year [1-365]*/
132 wtime->tm_yday = compute_yday(eft);
135 switch (eft->daylight & EFI_ISDST) {
139 case EFI_TIME_ADJUST_DAYLIGHT:
143 wtime->tm_isdst = -1;
147 static long efi_rtc_ioctl(struct file *file, unsigned int cmd,
155 struct rtc_time wtime;
156 struct rtc_wkalrm __user *ewp;
157 unsigned char enabled, pending;
175 spin_lock_irqsave(&efi_rtc_lock, flags);
177 status = efi.get_time(&eft, &cap);
179 spin_unlock_irqrestore(&efi_rtc_lock,flags);
181 if (status != EFI_SUCCESS) {
182 /* should never happen */
183 printk(KERN_ERR "efitime: can't read time\n");
187 convert_from_efi_time(&eft, &wtime);
189 return copy_to_user((void __user *)arg, &wtime,
190 sizeof (struct rtc_time)) ? - EFAULT : 0;
194 if (!capable(CAP_SYS_TIME)) return -EACCES;
196 if (copy_from_user(&wtime, (struct rtc_time __user *)arg,
197 sizeof(struct rtc_time)) )
200 convert_to_efi_time(&wtime, &eft);
202 spin_lock_irqsave(&efi_rtc_lock, flags);
204 status = efi.set_time(&eft);
206 spin_unlock_irqrestore(&efi_rtc_lock,flags);
208 return status == EFI_SUCCESS ? 0 : -EINVAL;
212 if (!capable(CAP_SYS_TIME)) return -EACCES;
214 ewp = (struct rtc_wkalrm __user *)arg;
216 if ( get_user(enabled, &ewp->enabled)
217 || copy_from_user(&wtime, &ewp->time, sizeof(struct rtc_time)) )
220 convert_to_efi_time(&wtime, &eft);
222 spin_lock_irqsave(&efi_rtc_lock, flags);
225 * As of EFI 0.92 with the firmware I have on my
226 * machine this call does not seem to work quite
229 status = efi.set_wakeup_time((efi_bool_t)enabled, &eft);
231 spin_unlock_irqrestore(&efi_rtc_lock,flags);
233 return status == EFI_SUCCESS ? 0 : -EINVAL;
237 spin_lock_irqsave(&efi_rtc_lock, flags);
239 status = efi.get_wakeup_time((efi_bool_t *)&enabled, (efi_bool_t *)&pending, &eft);
241 spin_unlock_irqrestore(&efi_rtc_lock,flags);
243 if (status != EFI_SUCCESS) return -EINVAL;
245 ewp = (struct rtc_wkalrm __user *)arg;
247 if ( put_user(enabled, &ewp->enabled)
248 || put_user(pending, &ewp->pending)) return -EFAULT;
250 convert_from_efi_time(&eft, &wtime);
252 return copy_to_user(&ewp->time, &wtime,
253 sizeof(struct rtc_time)) ? -EFAULT : 0;
259 * We enforce only one user at a time here with the open/close.
260 * Also clear the previous interrupt data on an open, and clean
261 * up things on a close.
264 static int efi_rtc_open(struct inode *inode, struct file *file)
267 * nothing special to do here
268 * We do accept multiple open files at the same time as we
269 * synchronize on the per call operation.
274 static int efi_rtc_close(struct inode *inode, struct file *file)
280 * The various file operations we support.
283 static const struct file_operations efi_rtc_fops = {
284 .owner = THIS_MODULE,
285 .unlocked_ioctl = efi_rtc_ioctl,
286 .open = efi_rtc_open,
287 .release = efi_rtc_close,
291 static struct miscdevice efi_rtc_dev= {
298 * We export RAW EFI information to /proc/driver/efirtc
300 static int efi_rtc_proc_show(struct seq_file *m, void *v)
304 efi_bool_t enabled, pending;
307 memset(&eft, 0, sizeof(eft));
308 memset(&alm, 0, sizeof(alm));
309 memset(&cap, 0, sizeof(cap));
311 spin_lock_irqsave(&efi_rtc_lock, flags);
313 efi.get_time(&eft, &cap);
314 efi.get_wakeup_time(&enabled, &pending, &alm);
316 spin_unlock_irqrestore(&efi_rtc_lock,flags);
319 "Time : %u:%u:%u.%09u\n"
322 eft.hour, eft.minute, eft.second, eft.nanosecond,
323 eft.year, eft.month, eft.day,
326 if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
327 seq_puts(m, "Timezone : unspecified\n");
329 /* XXX fixme: convert to string? */
330 seq_printf(m, "Timezone : %u\n", eft.timezone);
334 "Alarm Time : %u:%u:%u.%09u\n"
335 "Alarm Date : %u-%u-%u\n"
336 "Alarm Daylight : %u\n"
339 alm.hour, alm.minute, alm.second, alm.nanosecond,
340 alm.year, alm.month, alm.day,
342 enabled == 1 ? "yes" : "no",
343 pending == 1 ? "yes" : "no");
345 if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
346 seq_puts(m, "Timezone : unspecified\n");
348 /* XXX fixme: convert to string? */
349 seq_printf(m, "Timezone : %u\n", alm.timezone);
352 * now prints the capabilities
358 cap.resolution, cap.accuracy, cap.sets_to_zero);
363 static int efi_rtc_proc_open(struct inode *inode, struct file *file)
365 return single_open(file, efi_rtc_proc_show, NULL);
368 static const struct file_operations efi_rtc_proc_fops = {
369 .open = efi_rtc_proc_open,
372 .release = single_release,
379 struct proc_dir_entry *dir;
381 printk(KERN_INFO "EFI Time Services Driver v%s\n", EFI_RTC_VERSION);
383 ret = misc_register(&efi_rtc_dev);
385 printk(KERN_ERR "efirtc: can't misc_register on minor=%d\n",
390 dir = proc_create("driver/efirtc", 0, NULL, &efi_rtc_proc_fops);
392 printk(KERN_ERR "efirtc: can't create /proc/driver/efirtc.\n");
393 misc_deregister(&efi_rtc_dev);
405 module_init(efi_rtc_init);
406 module_exit(efi_rtc_exit);
408 MODULE_LICENSE("GPL");