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 a 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 from 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
21 * to interrrupts and SMP.
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
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
39 #include <asm/efi.h>
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
42
43 #define EFI_RTC_VERSION "0.2"
44
45 #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
46 /*
47 * EFI Epoch is 1/1/1998
48 */
49 #define EFI_RTC_EPOCH 1998
50
51 static spinlock_t efi_rtc_lock = SPIN_LOCK_UNLOCKED;
52
53 static int efi_rtc_ioctl(struct inode *inode, struct file *file,
54 unsigned int cmd, unsigned long arg);
55
56 #define is_leap(year) \
57 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
58
59 static const unsigned short int __mon_yday[2][13] =
60 {
61 /* Normal years. */
62 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
63 /* Leap years. */
64 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
65 };
66
67 /*
68 * returns day of the year [0-365]
69 */
70 static inline int
71 compute_yday(efi_time_t *eft)
72 {
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;
75 }
76 /*
77 * returns day of the week [0-6] 0=Sunday
78 *
79 * Don't try to provide a year that's before 1998, please !
80 */
81 static int
82 compute_wday(efi_time_t *eft)
83 {
84 int y;
85 int ndays = 0;
86
87 if ( eft->year < 1998 ) {
88 printk(KERN_ERR "efirtc: EFI year < 1998, invalid date\n");
89 return -1;
90 }
91
92 for(y=EFI_RTC_EPOCH; y < eft->year; y++ ) {
93 ndays += 365 + (is_leap(y) ? 1 : 0);
94 }
95 ndays += compute_yday(eft);
96
97 /*
98 * 4=1/1/1998 was a Thursday
99 */
100 return (ndays + 4) % 7;
101 }
102
103 static void
104 convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
105 {
106
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;
113 eft->nanosecond = 0;
114 eft->daylight = wtime->tm_isdst ? EFI_ISDST: 0;
115 eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
116 }
117
118 static void
119 convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
120 {
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;
127
128 /* day of the week [0-6], Sunday=0 */
129 wtime->tm_wday = compute_wday(eft);
130
131 /* day in the year [1-365]*/
132 wtime->tm_yday = compute_yday(eft);
133
134
135 switch (eft->daylight & EFI_ISDST) {
136 case EFI_ISDST:
137 wtime->tm_isdst = 1;
138 break;
139 case EFI_TIME_ADJUST_DAYLIGHT:
140 wtime->tm_isdst = 0;
141 break;
142 default:
143 wtime->tm_isdst = -1;
144 }
145 }
146
147 static int
148 efi_rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
149 unsigned long arg)
150 {
151
152 efi_status_t status;
153 unsigned long flags;
154 efi_time_t eft;
155 efi_time_cap_t cap;
156 struct rtc_time wtime;
157 struct rtc_wkalrm *ewp;
158 unsigned char enabled, pending;
159
160 switch (cmd) {
161 case RTC_UIE_ON:
162 case RTC_UIE_OFF:
163 case RTC_PIE_ON:
164 case RTC_PIE_OFF:
165 case RTC_AIE_ON:
166 case RTC_AIE_OFF:
167 case RTC_ALM_SET:
168 case RTC_ALM_READ:
169 case RTC_IRQP_READ:
170 case RTC_IRQP_SET:
171 case RTC_EPOCH_READ:
172 case RTC_EPOCH_SET:
173 return -EINVAL;
174
175 case RTC_RD_TIME:
176
177 spin_lock_irqsave(&efi_rtc_lock, flags);
178
179 status = efi.get_time(&eft, &cap);
180
181 spin_unlock_irqrestore(&efi_rtc_lock,flags);
182
183 if (status != EFI_SUCCESS) {
184 /* should never happen */
185 printk(KERN_ERR "efitime: can't read time\n");
186 return -EINVAL;
187 }
188
189 convert_from_efi_time(&eft, &wtime);
190
191 return copy_to_user((void *)arg, &wtime, sizeof (struct rtc_time)) ? - EFAULT : 0;
192
193 case RTC_SET_TIME:
194
195 if (!capable(CAP_SYS_TIME)) return -EACCES;
196
197 if (copy_from_user(&wtime, (struct rtc_time *)arg, sizeof(struct rtc_time)) )
198 return -EFAULT;
199
200 convert_to_efi_time(&wtime, &eft);
201
202 spin_lock_irqsave(&efi_rtc_lock, flags);
203
204 status = efi.set_time(&eft);
205
206 spin_unlock_irqrestore(&efi_rtc_lock,flags);
207
208 return status == EFI_SUCCESS ? 0 : -EINVAL;
209
210 case RTC_WKALM_SET:
211
212 if (!capable(CAP_SYS_TIME)) return -EACCES;
213
214 ewp = (struct rtc_wkalrm *)arg;
215
216 if ( get_user(enabled, &ewp->enabled)
217 || copy_from_user(&wtime, &ewp->time, sizeof(struct rtc_time)) )
218 return -EFAULT;
219
220 convert_to_efi_time(&wtime, &eft);
221
222 spin_lock_irqsave(&efi_rtc_lock, flags);
223 /*
224 * XXX Fixme:
225 * As of EFI 0.92 with the firmware I have on my
226 * machine this call does not seem to work quite
227 * right
228 */
229 status = efi.set_wakeup_time((efi_bool_t)enabled, &eft);
230
231 spin_unlock_irqrestore(&efi_rtc_lock,flags);
232
233 return status == EFI_SUCCESS ? 0 : -EINVAL;
234
235 case RTC_WKALM_RD:
236
237 spin_lock_irqsave(&efi_rtc_lock, flags);
238
239 status = efi.get_wakeup_time((efi_bool_t *)&enabled, (efi_bool_t *)&pending, &eft);
240
241 spin_unlock_irqrestore(&efi_rtc_lock,flags);
242
243 if (status != EFI_SUCCESS) return -EINVAL;
244
245 ewp = (struct rtc_wkalrm *)arg;
246
247 if ( put_user(enabled, &ewp->enabled)
248 || put_user(pending, &ewp->pending)) return -EFAULT;
249
250 convert_from_efi_time(&eft, &wtime);
251
252 return copy_to_user((void *)&ewp->time, &wtime, sizeof(struct rtc_time)) ? -EFAULT : 0;
253 }
254 return -EINVAL;
255 }
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
263 static int
264 efi_rtc_open(struct inode *inode, struct file *file)
265 {
266 /*
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.
270 */
271 return 0;
272 }
273
274 static int
275 efi_rtc_close(struct inode *inode, struct file *file)
276 {
277 return 0;
278 }
279
280 /*
281 * The various file operations we support.
282 */
283
284 static struct file_operations efi_rtc_fops = {
285 owner: THIS_MODULE,
286 ioctl: efi_rtc_ioctl,
287 open: efi_rtc_open,
288 release: efi_rtc_close,
289 };
290
291 static struct miscdevice efi_rtc_dev=
292 {
293 EFI_RTC_MINOR,
294 "efirtc",
295 &efi_rtc_fops
296 };
297
298 /*
299 * We export RAW EFI information to /proc/efirtc
300 */
301 static int
302 efi_rtc_get_status(char *buf)
303 {
304 efi_time_t eft, alm;
305 efi_time_cap_t cap;
306 char *p = buf;
307 efi_bool_t enabled, pending;
308 unsigned long flags;
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
317 p += sprintf(p,
318 "Time :\n"
319 "Year : %u\n"
320 "Month : %u\n"
321 "Day : %u\n"
322 "Hour : %u\n"
323 "Minute : %u\n"
324 "Second : %u\n"
325 "Nanosecond: %u\n"
326 "Daylight : %u\n",
327 eft.year, eft.month, eft.day, eft.hour, eft.minute,
328 eft.second, eft.nanosecond, eft.daylight);
329
330 if ( eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
331 p += sprintf(p, "Timezone : unspecified\n");
332 else
333 /* XXX fixme: convert to string? */
334 p += sprintf(p, "Timezone : %u\n", eft.timezone);
335
336
337 p += sprintf(p,
338 "\nWakeup Alm:\n"
339 "Enabled : %s\n"
340 "Pending : %s\n"
341 "Year : %u\n"
342 "Month : %u\n"
343 "Day : %u\n"
344 "Hour : %u\n"
345 "Minute : %u\n"
346 "Second : %u\n"
347 "Nanosecond: %u\n"
348 "Daylight : %u\n",
349 enabled == 1 ? "Yes" : "No",
350 pending == 1 ? "Yes" : "No",
351 alm.year, alm.month, alm.day, alm.hour, alm.minute,
352 alm.second, alm.nanosecond, alm.daylight);
353
354 if ( eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
355 p += sprintf(p, "Timezone : unspecified\n");
356 else
357 /* XXX fixme: convert to string? */
358 p += sprintf(p, "Timezone : %u\n", eft.timezone);
359
360 /*
361 * now prints the capabilities
362 */
363 p += sprintf(p,
364 "\nClock Cap :\n"
365 "Resolution: %u\n"
366 "Accuracy : %u\n"
367 "SetstoZero: %u\n",
368 cap.resolution, cap.accuracy, cap.sets_to_zero);
369
370 return p - buf;
371 }
372
373 static int
374 efi_rtc_read_proc(char *page, char **start, off_t off,
375 int count, int *eof, void *data)
376 {
377 int len = efi_rtc_get_status(page);
378 if (len <= off+count) *eof = 1;
379 *start = page + off;
380 len -= off;
381 if (len>count) len = count;
382 if (len<0) len = 0;
383 return len;
384 }
385
386 static int __init
387 efi_rtc_init(void)
388 {
389 printk(KERN_INFO "EFI Time Services Driver v%s\n", EFI_RTC_VERSION);
390
391 misc_register(&efi_rtc_dev);
392
393 create_proc_read_entry ("efirtc", 0, NULL, efi_rtc_read_proc, NULL);
394
395 return 0;
396 }
397
398 static void __exit
399 efi_rtc_exit(void)
400 {
401 /* not yet used */
402 }
403
404 module_init(efi_rtc_init);
405 module_exit(efi_rtc_exit);
406
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.