Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | |
8dfba4d7 | 21 | * to interrupts and SMP. |
1da177e4 LT |
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 | ||
89c7de08 | 31 | #include <linux/smp_lock.h> |
1da177e4 LT |
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> | |
76528a42 AC |
40 | #include <linux/smp_lock.h> |
41 | #include <linux/uaccess.h> | |
1da177e4 | 42 | |
1da177e4 LT |
43 | #include <asm/system.h> |
44 | ||
45 | #define EFI_RTC_VERSION "0.4" | |
46 | ||
47 | #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT) | |
48 | /* | |
49 | * EFI Epoch is 1/1/1998 | |
50 | */ | |
51 | #define EFI_RTC_EPOCH 1998 | |
52 | ||
53 | static DEFINE_SPINLOCK(efi_rtc_lock); | |
54 | ||
76528a42 AC |
55 | static long efi_rtc_ioctl(struct file *file, unsigned int cmd, |
56 | unsigned long arg); | |
1da177e4 LT |
57 | |
58 | #define is_leap(year) \ | |
59 | ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) | |
60 | ||
61 | static const unsigned short int __mon_yday[2][13] = | |
62 | { | |
63 | /* Normal years. */ | |
64 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, | |
65 | /* Leap years. */ | |
66 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } | |
67 | }; | |
68 | ||
69 | /* | |
70 | * returns day of the year [0-365] | |
71 | */ | |
72 | static inline int | |
73 | compute_yday(efi_time_t *eft) | |
74 | { | |
75 | /* efi_time_t.month is in the [1-12] so, we need -1 */ | |
76 | return __mon_yday[is_leap(eft->year)][eft->month-1]+ eft->day -1; | |
77 | } | |
78 | /* | |
79 | * returns day of the week [0-6] 0=Sunday | |
80 | * | |
81 | * Don't try to provide a year that's before 1998, please ! | |
82 | */ | |
83 | static int | |
84 | compute_wday(efi_time_t *eft) | |
85 | { | |
86 | int y; | |
87 | int ndays = 0; | |
88 | ||
89 | if ( eft->year < 1998 ) { | |
90 | printk(KERN_ERR "efirtc: EFI year < 1998, invalid date\n"); | |
91 | return -1; | |
92 | } | |
93 | ||
94 | for(y=EFI_RTC_EPOCH; y < eft->year; y++ ) { | |
95 | ndays += 365 + (is_leap(y) ? 1 : 0); | |
96 | } | |
97 | ndays += compute_yday(eft); | |
98 | ||
99 | /* | |
100 | * 4=1/1/1998 was a Thursday | |
101 | */ | |
102 | return (ndays + 4) % 7; | |
103 | } | |
104 | ||
105 | static void | |
106 | convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft) | |
107 | { | |
108 | ||
109 | eft->year = wtime->tm_year + 1900; | |
110 | eft->month = wtime->tm_mon + 1; | |
111 | eft->day = wtime->tm_mday; | |
112 | eft->hour = wtime->tm_hour; | |
113 | eft->minute = wtime->tm_min; | |
114 | eft->second = wtime->tm_sec; | |
115 | eft->nanosecond = 0; | |
116 | eft->daylight = wtime->tm_isdst ? EFI_ISDST: 0; | |
117 | eft->timezone = EFI_UNSPECIFIED_TIMEZONE; | |
118 | } | |
119 | ||
120 | static void | |
121 | convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime) | |
122 | { | |
123 | memset(wtime, 0, sizeof(*wtime)); | |
124 | wtime->tm_sec = eft->second; | |
125 | wtime->tm_min = eft->minute; | |
126 | wtime->tm_hour = eft->hour; | |
127 | wtime->tm_mday = eft->day; | |
128 | wtime->tm_mon = eft->month - 1; | |
129 | wtime->tm_year = eft->year - 1900; | |
130 | ||
131 | /* day of the week [0-6], Sunday=0 */ | |
132 | wtime->tm_wday = compute_wday(eft); | |
133 | ||
134 | /* day in the year [1-365]*/ | |
135 | wtime->tm_yday = compute_yday(eft); | |
136 | ||
137 | ||
138 | switch (eft->daylight & EFI_ISDST) { | |
139 | case EFI_ISDST: | |
140 | wtime->tm_isdst = 1; | |
141 | break; | |
142 | case EFI_TIME_ADJUST_DAYLIGHT: | |
143 | wtime->tm_isdst = 0; | |
144 | break; | |
145 | default: | |
146 | wtime->tm_isdst = -1; | |
147 | } | |
148 | } | |
149 | ||
76528a42 AC |
150 | static long efi_rtc_ioctl(struct file *file, unsigned int cmd, |
151 | unsigned long arg) | |
1da177e4 LT |
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: | |
76528a42 | 178 | lock_kernel(); |
1da177e4 LT |
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); | |
76528a42 | 184 | unlock_kernel(); |
1da177e4 LT |
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 | ||
76528a42 | 206 | lock_kernel(); |
1da177e4 LT |
207 | spin_lock_irqsave(&efi_rtc_lock, flags); |
208 | ||
209 | status = efi.set_time(&eft); | |
210 | ||
211 | spin_unlock_irqrestore(&efi_rtc_lock,flags); | |
76528a42 | 212 | unlock_kernel(); |
1da177e4 LT |
213 | |
214 | return status == EFI_SUCCESS ? 0 : -EINVAL; | |
215 | ||
216 | case RTC_WKALM_SET: | |
217 | ||
218 | if (!capable(CAP_SYS_TIME)) return -EACCES; | |
219 | ||
220 | ewp = (struct rtc_wkalrm __user *)arg; | |
221 | ||
222 | if ( get_user(enabled, &ewp->enabled) | |
223 | || copy_from_user(&wtime, &ewp->time, sizeof(struct rtc_time)) ) | |
224 | return -EFAULT; | |
225 | ||
226 | convert_to_efi_time(&wtime, &eft); | |
227 | ||
76528a42 | 228 | lock_kernel(); |
1da177e4 LT |
229 | spin_lock_irqsave(&efi_rtc_lock, flags); |
230 | /* | |
231 | * XXX Fixme: | |
232 | * As of EFI 0.92 with the firmware I have on my | |
233 | * machine this call does not seem to work quite | |
234 | * right | |
235 | */ | |
236 | status = efi.set_wakeup_time((efi_bool_t)enabled, &eft); | |
237 | ||
238 | spin_unlock_irqrestore(&efi_rtc_lock,flags); | |
76528a42 | 239 | unlock_kernel(); |
1da177e4 LT |
240 | |
241 | return status == EFI_SUCCESS ? 0 : -EINVAL; | |
242 | ||
243 | case RTC_WKALM_RD: | |
244 | ||
76528a42 | 245 | lock_kernel(); |
1da177e4 LT |
246 | spin_lock_irqsave(&efi_rtc_lock, flags); |
247 | ||
248 | status = efi.get_wakeup_time((efi_bool_t *)&enabled, (efi_bool_t *)&pending, &eft); | |
249 | ||
250 | spin_unlock_irqrestore(&efi_rtc_lock,flags); | |
76528a42 | 251 | unlock_kernel(); |
1da177e4 LT |
252 | |
253 | if (status != EFI_SUCCESS) return -EINVAL; | |
254 | ||
255 | ewp = (struct rtc_wkalrm __user *)arg; | |
256 | ||
257 | if ( put_user(enabled, &ewp->enabled) | |
258 | || put_user(pending, &ewp->pending)) return -EFAULT; | |
259 | ||
260 | convert_from_efi_time(&eft, &wtime); | |
261 | ||
262 | return copy_to_user(&ewp->time, &wtime, | |
263 | sizeof(struct rtc_time)) ? -EFAULT : 0; | |
264 | } | |
76528a42 | 265 | return -ENOTTY; |
1da177e4 LT |
266 | } |
267 | ||
268 | /* | |
269 | * We enforce only one user at a time here with the open/close. | |
270 | * Also clear the previous interrupt data on an open, and clean | |
271 | * up things on a close. | |
272 | */ | |
273 | ||
76528a42 | 274 | static int efi_rtc_open(struct inode *inode, struct file *file) |
1da177e4 LT |
275 | { |
276 | /* | |
277 | * nothing special to do here | |
278 | * We do accept multiple open files at the same time as we | |
279 | * synchronize on the per call operation. | |
280 | */ | |
89c7de08 | 281 | cycle_kernel_lock(); |
1da177e4 LT |
282 | return 0; |
283 | } | |
284 | ||
76528a42 | 285 | static int efi_rtc_close(struct inode *inode, struct file *file) |
1da177e4 LT |
286 | { |
287 | return 0; | |
288 | } | |
289 | ||
290 | /* | |
291 | * The various file operations we support. | |
292 | */ | |
293 | ||
62322d25 | 294 | static const struct file_operations efi_rtc_fops = { |
1da177e4 | 295 | .owner = THIS_MODULE, |
76528a42 | 296 | .unlocked_ioctl = efi_rtc_ioctl, |
1da177e4 LT |
297 | .open = efi_rtc_open, |
298 | .release = efi_rtc_close, | |
299 | }; | |
300 | ||
76528a42 | 301 | static struct miscdevice efi_rtc_dev= { |
1da177e4 LT |
302 | EFI_RTC_MINOR, |
303 | "efirtc", | |
304 | &efi_rtc_fops | |
305 | }; | |
306 | ||
307 | /* | |
308 | * We export RAW EFI information to /proc/driver/efirtc | |
309 | */ | |
310 | static int | |
311 | efi_rtc_get_status(char *buf) | |
312 | { | |
313 | efi_time_t eft, alm; | |
314 | efi_time_cap_t cap; | |
315 | char *p = buf; | |
316 | efi_bool_t enabled, pending; | |
317 | unsigned long flags; | |
318 | ||
319 | memset(&eft, 0, sizeof(eft)); | |
320 | memset(&alm, 0, sizeof(alm)); | |
321 | memset(&cap, 0, sizeof(cap)); | |
322 | ||
323 | spin_lock_irqsave(&efi_rtc_lock, flags); | |
324 | ||
325 | efi.get_time(&eft, &cap); | |
326 | efi.get_wakeup_time(&enabled, &pending, &alm); | |
327 | ||
328 | spin_unlock_irqrestore(&efi_rtc_lock,flags); | |
329 | ||
330 | p += sprintf(p, | |
331 | "Time : %u:%u:%u.%09u\n" | |
332 | "Date : %u-%u-%u\n" | |
333 | "Daylight : %u\n", | |
334 | eft.hour, eft.minute, eft.second, eft.nanosecond, | |
335 | eft.year, eft.month, eft.day, | |
336 | eft.daylight); | |
337 | ||
338 | if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE) | |
339 | p += sprintf(p, "Timezone : unspecified\n"); | |
340 | else | |
341 | /* XXX fixme: convert to string? */ | |
342 | p += sprintf(p, "Timezone : %u\n", eft.timezone); | |
343 | ||
344 | ||
345 | p += sprintf(p, | |
346 | "Alarm Time : %u:%u:%u.%09u\n" | |
347 | "Alarm Date : %u-%u-%u\n" | |
348 | "Alarm Daylight : %u\n" | |
349 | "Enabled : %s\n" | |
350 | "Pending : %s\n", | |
351 | alm.hour, alm.minute, alm.second, alm.nanosecond, | |
352 | alm.year, alm.month, alm.day, | |
353 | alm.daylight, | |
354 | enabled == 1 ? "yes" : "no", | |
355 | pending == 1 ? "yes" : "no"); | |
356 | ||
357 | if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE) | |
358 | p += sprintf(p, "Timezone : unspecified\n"); | |
359 | else | |
360 | /* XXX fixme: convert to string? */ | |
361 | p += sprintf(p, "Timezone : %u\n", alm.timezone); | |
362 | ||
363 | /* | |
364 | * now prints the capabilities | |
365 | */ | |
366 | p += sprintf(p, | |
367 | "Resolution : %u\n" | |
368 | "Accuracy : %u\n" | |
369 | "SetstoZero : %u\n", | |
370 | cap.resolution, cap.accuracy, cap.sets_to_zero); | |
371 | ||
372 | return p - buf; | |
373 | } | |
374 | ||
375 | static int | |
376 | efi_rtc_read_proc(char *page, char **start, off_t off, | |
377 | int count, int *eof, void *data) | |
378 | { | |
379 | int len = efi_rtc_get_status(page); | |
380 | if (len <= off+count) *eof = 1; | |
381 | *start = page + off; | |
382 | len -= off; | |
383 | if (len>count) len = count; | |
384 | if (len<0) len = 0; | |
385 | return len; | |
386 | } | |
387 | ||
388 | static int __init | |
389 | efi_rtc_init(void) | |
390 | { | |
391 | int ret; | |
392 | struct proc_dir_entry *dir; | |
393 | ||
394 | printk(KERN_INFO "EFI Time Services Driver v%s\n", EFI_RTC_VERSION); | |
395 | ||
396 | ret = misc_register(&efi_rtc_dev); | |
397 | if (ret) { | |
398 | printk(KERN_ERR "efirtc: can't misc_register on minor=%d\n", | |
399 | EFI_RTC_MINOR); | |
400 | return ret; | |
401 | } | |
402 | ||
403 | dir = create_proc_read_entry ("driver/efirtc", 0, NULL, | |
404 | efi_rtc_read_proc, NULL); | |
405 | if (dir == NULL) { | |
406 | printk(KERN_ERR "efirtc: can't create /proc/driver/efirtc.\n"); | |
407 | misc_deregister(&efi_rtc_dev); | |
408 | return -1; | |
409 | } | |
410 | return 0; | |
411 | } | |
412 | ||
413 | static void __exit | |
414 | efi_rtc_exit(void) | |
415 | { | |
416 | /* not yet used */ | |
417 | } | |
418 | ||
419 | module_init(efi_rtc_init); | |
420 | module_exit(efi_rtc_exit); | |
421 | ||
422 | MODULE_LICENSE("GPL"); |