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/init.h>
34 #include <linux/rtc.h>
35 #include <linux/proc_fs.h>
36 #include <linux/seq_file.h>
37 #include <linux/efi.h>
38 #include <linux/uaccess.h>
41 #define EFI_RTC_VERSION "0.4"
43 #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
45 * EFI Epoch is 1/1/1998
47 #define EFI_RTC_EPOCH 1998
49 static DEFINE_SPINLOCK(efi_rtc_lock
);
51 static long efi_rtc_ioctl(struct file
*file
, unsigned int cmd
,
54 #define is_leap(year) \
55 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
57 static const unsigned short int __mon_yday
[2][13] =
60 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
62 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
66 * returns day of the year [0-365]
69 compute_yday(efi_time_t
*eft
)
71 /* efi_time_t.month is in the [1-12] so, we need -1 */
72 return __mon_yday
[is_leap(eft
->year
)][eft
->month
-1]+ eft
->day
-1;
75 * returns day of the week [0-6] 0=Sunday
77 * Don't try to provide a year that's before 1998, please !
80 compute_wday(efi_time_t
*eft
)
85 if ( eft
->year
< 1998 ) {
86 printk(KERN_ERR
"efirtc: EFI year < 1998, invalid date\n");
90 for(y
=EFI_RTC_EPOCH
; y
< eft
->year
; y
++ ) {
91 ndays
+= 365 + (is_leap(y
) ? 1 : 0);
93 ndays
+= compute_yday(eft
);
96 * 4=1/1/1998 was a Thursday
98 return (ndays
+ 4) % 7;
102 convert_to_efi_time(struct rtc_time
*wtime
, efi_time_t
*eft
)
105 eft
->year
= wtime
->tm_year
+ 1900;
106 eft
->month
= wtime
->tm_mon
+ 1;
107 eft
->day
= wtime
->tm_mday
;
108 eft
->hour
= wtime
->tm_hour
;
109 eft
->minute
= wtime
->tm_min
;
110 eft
->second
= wtime
->tm_sec
;
112 eft
->daylight
= wtime
->tm_isdst
? EFI_ISDST
: 0;
113 eft
->timezone
= EFI_UNSPECIFIED_TIMEZONE
;
117 convert_from_efi_time(efi_time_t
*eft
, struct rtc_time
*wtime
)
119 memset(wtime
, 0, sizeof(*wtime
));
120 wtime
->tm_sec
= eft
->second
;
121 wtime
->tm_min
= eft
->minute
;
122 wtime
->tm_hour
= eft
->hour
;
123 wtime
->tm_mday
= eft
->day
;
124 wtime
->tm_mon
= eft
->month
- 1;
125 wtime
->tm_year
= eft
->year
- 1900;
127 /* day of the week [0-6], Sunday=0 */
128 wtime
->tm_wday
= compute_wday(eft
);
130 /* day in the year [1-365]*/
131 wtime
->tm_yday
= compute_yday(eft
);
134 switch (eft
->daylight
& EFI_ISDST
) {
138 case EFI_TIME_ADJUST_DAYLIGHT
:
142 wtime
->tm_isdst
= -1;
146 static long efi_rtc_ioctl(struct file
*file
, unsigned int cmd
,
154 struct rtc_time wtime
;
155 struct rtc_wkalrm __user
*ewp
;
156 unsigned char enabled
, pending
;
174 spin_lock_irqsave(&efi_rtc_lock
, flags
);
176 status
= efi
.get_time(&eft
, &cap
);
178 spin_unlock_irqrestore(&efi_rtc_lock
,flags
);
180 if (status
!= EFI_SUCCESS
) {
181 /* should never happen */
182 printk(KERN_ERR
"efitime: can't read time\n");
186 convert_from_efi_time(&eft
, &wtime
);
188 return copy_to_user((void __user
*)arg
, &wtime
,
189 sizeof (struct rtc_time
)) ? - EFAULT
: 0;
193 if (!capable(CAP_SYS_TIME
)) return -EACCES
;
195 if (copy_from_user(&wtime
, (struct rtc_time __user
*)arg
,
196 sizeof(struct rtc_time
)) )
199 convert_to_efi_time(&wtime
, &eft
);
201 spin_lock_irqsave(&efi_rtc_lock
, flags
);
203 status
= efi
.set_time(&eft
);
205 spin_unlock_irqrestore(&efi_rtc_lock
,flags
);
207 return status
== EFI_SUCCESS
? 0 : -EINVAL
;
211 if (!capable(CAP_SYS_TIME
)) return -EACCES
;
213 ewp
= (struct rtc_wkalrm __user
*)arg
;
215 if ( get_user(enabled
, &ewp
->enabled
)
216 || copy_from_user(&wtime
, &ewp
->time
, sizeof(struct rtc_time
)) )
219 convert_to_efi_time(&wtime
, &eft
);
221 spin_lock_irqsave(&efi_rtc_lock
, flags
);
224 * As of EFI 0.92 with the firmware I have on my
225 * machine this call does not seem to work quite
228 status
= efi
.set_wakeup_time((efi_bool_t
)enabled
, &eft
);
230 spin_unlock_irqrestore(&efi_rtc_lock
,flags
);
232 return status
== EFI_SUCCESS
? 0 : -EINVAL
;
236 spin_lock_irqsave(&efi_rtc_lock
, flags
);
238 status
= efi
.get_wakeup_time((efi_bool_t
*)&enabled
, (efi_bool_t
*)&pending
, &eft
);
240 spin_unlock_irqrestore(&efi_rtc_lock
,flags
);
242 if (status
!= EFI_SUCCESS
) return -EINVAL
;
244 ewp
= (struct rtc_wkalrm __user
*)arg
;
246 if ( put_user(enabled
, &ewp
->enabled
)
247 || put_user(pending
, &ewp
->pending
)) return -EFAULT
;
249 convert_from_efi_time(&eft
, &wtime
);
251 return copy_to_user(&ewp
->time
, &wtime
,
252 sizeof(struct rtc_time
)) ? -EFAULT
: 0;
258 * We enforce only one user at a time here with the open/close.
259 * Also clear the previous interrupt data on an open, and clean
260 * up things on a close.
263 static int efi_rtc_open(struct inode
*inode
, struct file
*file
)
266 * nothing special to do here
267 * We do accept multiple open files at the same time as we
268 * synchronize on the per call operation.
273 static int efi_rtc_close(struct inode
*inode
, struct file
*file
)
279 * The various file operations we support.
282 static const struct file_operations efi_rtc_fops
= {
283 .owner
= THIS_MODULE
,
284 .unlocked_ioctl
= efi_rtc_ioctl
,
285 .open
= efi_rtc_open
,
286 .release
= efi_rtc_close
,
290 static struct miscdevice efi_rtc_dev
= {
297 * We export RAW EFI information to /proc/driver/efirtc
299 static int efi_rtc_proc_show(struct seq_file
*m
, void *v
)
303 efi_bool_t enabled
, pending
;
306 memset(&eft
, 0, sizeof(eft
));
307 memset(&alm
, 0, sizeof(alm
));
308 memset(&cap
, 0, sizeof(cap
));
310 spin_lock_irqsave(&efi_rtc_lock
, flags
);
312 efi
.get_time(&eft
, &cap
);
313 efi
.get_wakeup_time(&enabled
, &pending
, &alm
);
315 spin_unlock_irqrestore(&efi_rtc_lock
,flags
);
318 "Time : %u:%u:%u.%09u\n"
321 eft
.hour
, eft
.minute
, eft
.second
, eft
.nanosecond
,
322 eft
.year
, eft
.month
, eft
.day
,
325 if (eft
.timezone
== EFI_UNSPECIFIED_TIMEZONE
)
326 seq_puts(m
, "Timezone : unspecified\n");
328 /* XXX fixme: convert to string? */
329 seq_printf(m
, "Timezone : %u\n", eft
.timezone
);
333 "Alarm Time : %u:%u:%u.%09u\n"
334 "Alarm Date : %u-%u-%u\n"
335 "Alarm Daylight : %u\n"
338 alm
.hour
, alm
.minute
, alm
.second
, alm
.nanosecond
,
339 alm
.year
, alm
.month
, alm
.day
,
341 enabled
== 1 ? "yes" : "no",
342 pending
== 1 ? "yes" : "no");
344 if (eft
.timezone
== EFI_UNSPECIFIED_TIMEZONE
)
345 seq_puts(m
, "Timezone : unspecified\n");
347 /* XXX fixme: convert to string? */
348 seq_printf(m
, "Timezone : %u\n", alm
.timezone
);
351 * now prints the capabilities
357 cap
.resolution
, cap
.accuracy
, cap
.sets_to_zero
);
365 struct proc_dir_entry
*dir
;
367 printk(KERN_INFO
"EFI Time Services Driver v%s\n", EFI_RTC_VERSION
);
369 ret
= misc_register(&efi_rtc_dev
);
371 printk(KERN_ERR
"efirtc: can't misc_register on minor=%d\n",
376 dir
= proc_create_single("driver/efirtc", 0, NULL
, efi_rtc_proc_show
);
378 printk(KERN_ERR
"efirtc: can't create /proc/driver/efirtc.\n");
379 misc_deregister(&efi_rtc_dev
);
384 device_initcall(efi_rtc_init
);
387 MODULE_LICENSE("GPL");