blob: dc62568b7dde9fe12f4ddd6fb5cc997f6f299865 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Joe Perches8dfba4d2008-02-03 17:11:42 +020021 * to interrupts and SMP.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
31#include <linux/errno.h>
32#include <linux/miscdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
34#include <linux/rtc.h>
35#include <linux/proc_fs.h>
David Howells788416b2013-04-10 16:21:08 +010036#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/efi.h>
Alan Cox76528a42008-07-25 01:48:12 -070038#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define EFI_RTC_VERSION "0.4"
42
43#define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
44/*
45 * EFI Epoch is 1/1/1998
46 */
47#define EFI_RTC_EPOCH 1998
48
49static DEFINE_SPINLOCK(efi_rtc_lock);
50
Alan Cox76528a42008-07-25 01:48:12 -070051static long efi_rtc_ioctl(struct file *file, unsigned int cmd,
52 unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define is_leap(year) \
55 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
56
57static const unsigned short int __mon_yday[2][13] =
58{
59 /* Normal years. */
60 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
61 /* Leap years. */
62 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
63};
64
65/*
66 * returns day of the year [0-365]
67 */
68static inline int
69compute_yday(efi_time_t *eft)
70{
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;
73}
74/*
75 * returns day of the week [0-6] 0=Sunday
76 *
77 * Don't try to provide a year that's before 1998, please !
78 */
79static int
80compute_wday(efi_time_t *eft)
81{
82 int y;
83 int ndays = 0;
84
85 if ( eft->year < 1998 ) {
86 printk(KERN_ERR "efirtc: EFI year < 1998, invalid date\n");
87 return -1;
88 }
89
90 for(y=EFI_RTC_EPOCH; y < eft->year; y++ ) {
91 ndays += 365 + (is_leap(y) ? 1 : 0);
92 }
93 ndays += compute_yday(eft);
94
95 /*
96 * 4=1/1/1998 was a Thursday
97 */
98 return (ndays + 4) % 7;
99}
100
101static void
102convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
103{
104
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;
111 eft->nanosecond = 0;
112 eft->daylight = wtime->tm_isdst ? EFI_ISDST: 0;
113 eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
114}
115
116static void
117convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
118{
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;
126
127 /* day of the week [0-6], Sunday=0 */
128 wtime->tm_wday = compute_wday(eft);
129
130 /* day in the year [1-365]*/
131 wtime->tm_yday = compute_yday(eft);
132
133
134 switch (eft->daylight & EFI_ISDST) {
135 case EFI_ISDST:
136 wtime->tm_isdst = 1;
137 break;
138 case EFI_TIME_ADJUST_DAYLIGHT:
139 wtime->tm_isdst = 0;
140 break;
141 default:
142 wtime->tm_isdst = -1;
143 }
144}
145
Alan Cox76528a42008-07-25 01:48:12 -0700146static long efi_rtc_ioctl(struct file *file, unsigned int cmd,
147 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149
150 efi_status_t status;
151 unsigned long flags;
152 efi_time_t eft;
153 efi_time_cap_t cap;
154 struct rtc_time wtime;
155 struct rtc_wkalrm __user *ewp;
156 unsigned char enabled, pending;
157
158 switch (cmd) {
159 case RTC_UIE_ON:
160 case RTC_UIE_OFF:
161 case RTC_PIE_ON:
162 case RTC_PIE_OFF:
163 case RTC_AIE_ON:
164 case RTC_AIE_OFF:
165 case RTC_ALM_SET:
166 case RTC_ALM_READ:
167 case RTC_IRQP_READ:
168 case RTC_IRQP_SET:
169 case RTC_EPOCH_READ:
170 case RTC_EPOCH_SET:
171 return -EINVAL;
172
173 case RTC_RD_TIME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 spin_lock_irqsave(&efi_rtc_lock, flags);
175
176 status = efi.get_time(&eft, &cap);
177
178 spin_unlock_irqrestore(&efi_rtc_lock,flags);
Thomas Gleixnera5ee6dc2009-10-10 15:14:03 +0200179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (status != EFI_SUCCESS) {
181 /* should never happen */
182 printk(KERN_ERR "efitime: can't read time\n");
183 return -EINVAL;
184 }
185
186 convert_from_efi_time(&eft, &wtime);
187
188 return copy_to_user((void __user *)arg, &wtime,
189 sizeof (struct rtc_time)) ? - EFAULT : 0;
190
191 case RTC_SET_TIME:
192
193 if (!capable(CAP_SYS_TIME)) return -EACCES;
194
195 if (copy_from_user(&wtime, (struct rtc_time __user *)arg,
196 sizeof(struct rtc_time)) )
197 return -EFAULT;
198
199 convert_to_efi_time(&wtime, &eft);
200
201 spin_lock_irqsave(&efi_rtc_lock, flags);
202
203 status = efi.set_time(&eft);
204
205 spin_unlock_irqrestore(&efi_rtc_lock,flags);
206
207 return status == EFI_SUCCESS ? 0 : -EINVAL;
208
209 case RTC_WKALM_SET:
210
211 if (!capable(CAP_SYS_TIME)) return -EACCES;
212
213 ewp = (struct rtc_wkalrm __user *)arg;
214
215 if ( get_user(enabled, &ewp->enabled)
216 || copy_from_user(&wtime, &ewp->time, sizeof(struct rtc_time)) )
217 return -EFAULT;
218
219 convert_to_efi_time(&wtime, &eft);
220
221 spin_lock_irqsave(&efi_rtc_lock, flags);
222 /*
223 * XXX Fixme:
224 * As of EFI 0.92 with the firmware I have on my
225 * machine this call does not seem to work quite
226 * right
227 */
228 status = efi.set_wakeup_time((efi_bool_t)enabled, &eft);
229
230 spin_unlock_irqrestore(&efi_rtc_lock,flags);
231
232 return status == EFI_SUCCESS ? 0 : -EINVAL;
233
234 case RTC_WKALM_RD:
235
236 spin_lock_irqsave(&efi_rtc_lock, flags);
237
238 status = efi.get_wakeup_time((efi_bool_t *)&enabled, (efi_bool_t *)&pending, &eft);
239
240 spin_unlock_irqrestore(&efi_rtc_lock,flags);
241
242 if (status != EFI_SUCCESS) return -EINVAL;
243
244 ewp = (struct rtc_wkalrm __user *)arg;
245
246 if ( put_user(enabled, &ewp->enabled)
247 || put_user(pending, &ewp->pending)) return -EFAULT;
248
249 convert_from_efi_time(&eft, &wtime);
250
251 return copy_to_user(&ewp->time, &wtime,
252 sizeof(struct rtc_time)) ? -EFAULT : 0;
253 }
Alan Cox76528a42008-07-25 01:48:12 -0700254 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257/*
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.
261 */
262
Alan Cox76528a42008-07-25 01:48:12 -0700263static int efi_rtc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 /*
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.
269 */
270 return 0;
271}
272
Alan Cox76528a42008-07-25 01:48:12 -0700273static int efi_rtc_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 return 0;
276}
277
278/*
279 * The various file operations we support.
280 */
281
Arjan van de Ven62322d22006-07-03 00:24:21 -0700282static const struct file_operations efi_rtc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 .owner = THIS_MODULE,
Alan Cox76528a42008-07-25 01:48:12 -0700284 .unlocked_ioctl = efi_rtc_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 .open = efi_rtc_open,
286 .release = efi_rtc_close,
John Kacurf29627c2009-12-15 16:46:06 -0800287 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288};
289
Alan Cox76528a42008-07-25 01:48:12 -0700290static struct miscdevice efi_rtc_dev= {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 EFI_RTC_MINOR,
292 "efirtc",
293 &efi_rtc_fops
294};
295
296/*
297 * We export RAW EFI information to /proc/driver/efirtc
298 */
David Howells788416b2013-04-10 16:21:08 +0100299static int efi_rtc_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 efi_time_t eft, alm;
302 efi_time_cap_t cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 efi_bool_t enabled, pending;
304 unsigned long flags;
305
306 memset(&eft, 0, sizeof(eft));
307 memset(&alm, 0, sizeof(alm));
308 memset(&cap, 0, sizeof(cap));
309
310 spin_lock_irqsave(&efi_rtc_lock, flags);
311
312 efi.get_time(&eft, &cap);
313 efi.get_wakeup_time(&enabled, &pending, &alm);
314
315 spin_unlock_irqrestore(&efi_rtc_lock,flags);
316
David Howells788416b2013-04-10 16:21:08 +0100317 seq_printf(m,
318 "Time : %u:%u:%u.%09u\n"
319 "Date : %u-%u-%u\n"
320 "Daylight : %u\n",
321 eft.hour, eft.minute, eft.second, eft.nanosecond,
322 eft.year, eft.month, eft.day,
323 eft.daylight);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
David Howells788416b2013-04-10 16:21:08 +0100326 seq_puts(m, "Timezone : unspecified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 else
328 /* XXX fixme: convert to string? */
David Howells788416b2013-04-10 16:21:08 +0100329 seq_printf(m, "Timezone : %u\n", eft.timezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331
David Howells788416b2013-04-10 16:21:08 +0100332 seq_printf(m,
333 "Alarm Time : %u:%u:%u.%09u\n"
334 "Alarm Date : %u-%u-%u\n"
335 "Alarm Daylight : %u\n"
336 "Enabled : %s\n"
337 "Pending : %s\n",
338 alm.hour, alm.minute, alm.second, alm.nanosecond,
339 alm.year, alm.month, alm.day,
340 alm.daylight,
341 enabled == 1 ? "yes" : "no",
342 pending == 1 ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
David Howells788416b2013-04-10 16:21:08 +0100345 seq_puts(m, "Timezone : unspecified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 else
347 /* XXX fixme: convert to string? */
David Howells788416b2013-04-10 16:21:08 +0100348 seq_printf(m, "Timezone : %u\n", alm.timezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /*
351 * now prints the capabilities
352 */
David Howells788416b2013-04-10 16:21:08 +0100353 seq_printf(m,
354 "Resolution : %u\n"
355 "Accuracy : %u\n"
356 "SetstoZero : %u\n",
357 cap.resolution, cap.accuracy, cap.sets_to_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
David Howells788416b2013-04-10 16:21:08 +0100359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
David Howells788416b2013-04-10 16:21:08 +0100362static int efi_rtc_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
David Howells788416b2013-04-10 16:21:08 +0100364 return single_open(file, efi_rtc_proc_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
David Howells788416b2013-04-10 16:21:08 +0100367static const struct file_operations efi_rtc_proc_fops = {
368 .open = efi_rtc_proc_open,
369 .read = seq_read,
370 .llseek = seq_lseek,
Al Viro2e7718c2013-05-05 00:12:29 -0400371 .release = single_release,
David Howells788416b2013-04-10 16:21:08 +0100372};
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374static int __init
375efi_rtc_init(void)
376{
377 int ret;
378 struct proc_dir_entry *dir;
379
380 printk(KERN_INFO "EFI Time Services Driver v%s\n", EFI_RTC_VERSION);
381
382 ret = misc_register(&efi_rtc_dev);
383 if (ret) {
384 printk(KERN_ERR "efirtc: can't misc_register on minor=%d\n",
385 EFI_RTC_MINOR);
386 return ret;
387 }
388
David Howells788416b2013-04-10 16:21:08 +0100389 dir = proc_create("driver/efirtc", 0, NULL, &efi_rtc_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (dir == NULL) {
391 printk(KERN_ERR "efirtc: can't create /proc/driver/efirtc.\n");
392 misc_deregister(&efi_rtc_dev);
393 return -1;
394 }
395 return 0;
396}
Paul Gortmaker9b7ebe02015-08-08 16:35:02 -0400397device_initcall(efi_rtc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Paul Gortmaker9b7ebe02015-08-08 16:35:02 -0400399/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400MODULE_LICENSE("GPL");
Paul Gortmaker9b7ebe02015-08-08 16:35:02 -0400401*/