Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / efirtc.c
blob9ad85c687978102e853ba369b16166d80ca19e52
1 /*
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 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.
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.
19 * NOTES:
20 * - Locking is required for safe execution of EFI calls with regards
21 * to interrrupts and SMP.
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
31 #include <linux/types.h>
32 #include <linux/errno.h>
33 #include <linux/miscdevice.h>
34 #include <linux/init.h>
35 #include <linux/rtc.h>
36 #include <linux/proc_fs.h>
38 #include <asm/efi.h>
39 #include <asm/uaccess.h>
40 #include <asm/system.h>
42 #define EFI_RTC_VERSION "0.2"
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 spinlock_t efi_rtc_lock = SPIN_LOCK_UNLOCKED;
52 static int efi_rtc_ioctl(struct inode *inode, struct file *file,
53 unsigned int cmd, unsigned long arg);
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] =
60 /* Normal years. */
61 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
62 /* Leap years. */
63 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
67 * returns day of the year [0-365]
69 static inline int
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 !
80 static int
81 compute_wday(efi_time_t *eft)
83 int y;
84 int ndays = 0;
86 if ( eft->year < 1998 ) {
87 printk(KERN_ERR "efirtc: EFI year < 1998, invalid date\n");
88 return -1;
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;
102 static void
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;
112 eft->nanosecond = 0;
113 eft->daylight = wtime->tm_isdst ? EFI_ISDST: 0;
114 eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
117 static void
118 convert_from_efi_time(efi_time_t *eft, struct rtc_time *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) {
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;
146 static int
147 efi_rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
148 unsigned long arg)
151 efi_status_t status;
152 unsigned long flags;
153 efi_time_t eft;
154 efi_time_cap_t cap;
155 struct rtc_time wtime;
156 struct rtc_wkalrm *ewp;
157 unsigned char enabled, pending;
159 switch (cmd) {
160 case RTC_UIE_ON:
161 case RTC_UIE_OFF:
162 case RTC_PIE_ON:
163 case RTC_PIE_OFF:
164 case RTC_AIE_ON:
165 case RTC_AIE_OFF:
166 case RTC_ALM_SET:
167 case RTC_ALM_READ:
168 case RTC_IRQP_READ:
169 case RTC_IRQP_SET:
170 case RTC_EPOCH_READ:
171 case RTC_EPOCH_SET:
172 return -EINVAL;
174 case RTC_RD_TIME:
176 spin_lock_irqsave(&efi_rtc_lock, flags);
178 status = efi.get_time(&eft, &cap);
180 spin_unlock_irqrestore(&efi_rtc_lock,flags);
182 if (status != EFI_SUCCESS) {
183 /* should never happen */
184 printk(KERN_ERR "efitime: can't read time\n");
185 return -EINVAL;
188 convert_from_efi_time(&eft, &wtime);
190 return copy_to_user((void *)arg, &wtime, sizeof (struct rtc_time)) ? - EFAULT : 0;
192 case RTC_SET_TIME:
194 if (!capable(CAP_SYS_TIME)) return -EACCES;
196 if (copy_from_user(&wtime, (struct rtc_time *)arg, sizeof(struct rtc_time)) )
197 return -EFAULT;
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;
209 case RTC_WKALM_SET:
211 if (!capable(CAP_SYS_TIME)) return -EACCES;
213 ewp = (struct rtc_wkalrm *)arg;
215 if ( get_user(enabled, &ewp->enabled)
216 || copy_from_user(&wtime, &ewp->time, sizeof(struct rtc_time)) )
217 return -EFAULT;
219 convert_to_efi_time(&wtime, &eft);
221 spin_lock_irqsave(&efi_rtc_lock, flags);
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
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;
234 case RTC_WKALM_RD:
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 *)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((void *)&ewp->time, &wtime, sizeof(struct rtc_time));
253 return -EINVAL;
257 * We enforce only one user at a time here with the open/close.
258 * Also clear the previous interrupt data on an open, and clean
259 * up things on a close.
262 static int
263 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.
270 return 0;
273 static int
274 efi_rtc_close(struct inode *inode, struct file *file)
276 return 0;
280 * The various file operations we support.
283 static struct file_operations efi_rtc_fops = {
284 owner: THIS_MODULE,
285 ioctl: efi_rtc_ioctl,
286 open: efi_rtc_open,
287 release: efi_rtc_close,
290 static struct miscdevice efi_rtc_dev=
292 EFI_RTC_MINOR,
293 "efirtc",
294 &efi_rtc_fops
298 * We export RAW EFI information to /proc/efirtc
300 static int
301 efi_rtc_get_status(char *buf)
303 efi_time_t eft, alm;
304 efi_time_cap_t cap;
305 char *p = buf;
306 efi_bool_t enabled, pending;
307 unsigned long flags;
309 spin_lock_irqsave(&efi_rtc_lock, flags);
311 efi.get_time(&eft, &cap);
312 efi.get_wakeup_time(&enabled, &pending, &alm);
314 spin_unlock_irqrestore(&efi_rtc_lock,flags);
316 p += sprintf(p,
317 "Time :\n"
318 "Year : %u\n"
319 "Month : %u\n"
320 "Day : %u\n"
321 "Hour : %u\n"
322 "Minute : %u\n"
323 "Second : %u\n"
324 "Nanosecond: %u\n"
325 "Daylight : %u\n",
326 eft.year, eft.month, eft.day, eft.hour, eft.minute,
327 eft.second, eft.nanosecond, eft.daylight);
329 if ( eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
330 p += sprintf(p, "Timezone : unspecified\n");
331 else
332 /* XXX fixme: convert to string? */
333 p += sprintf(p, "Timezone : %u\n", eft.timezone);
336 p += sprintf(p,
337 "\nWakeup Alm:\n"
338 "Enabled : %s\n"
339 "Pending : %s\n"
340 "Year : %u\n"
341 "Month : %u\n"
342 "Day : %u\n"
343 "Hour : %u\n"
344 "Minute : %u\n"
345 "Second : %u\n"
346 "Nanosecond: %u\n"
347 "Daylight : %u\n",
348 enabled == 1 ? "Yes" : "No",
349 pending == 1 ? "Yes" : "No",
350 alm.year, alm.month, alm.day, alm.hour, alm.minute,
351 alm.second, alm.nanosecond, alm.daylight);
353 if ( eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
354 p += sprintf(p, "Timezone : unspecified\n");
355 else
356 /* XXX fixme: convert to string? */
357 p += sprintf(p, "Timezone : %u\n", eft.timezone);
360 * now prints the capabilities
362 p += sprintf(p,
363 "\nClock Cap :\n"
364 "Resolution: %u\n"
365 "Accuracy : %u\n"
366 "SetstoZero: %u\n",
367 cap.resolution, cap.accuracy, cap.sets_to_zero);
369 return p - buf;
372 static int
373 efi_rtc_read_proc(char *page, char **start, off_t off,
374 int count, int *eof, void *data)
376 int len = efi_rtc_get_status(page);
377 if (len <= off+count) *eof = 1;
378 *start = page + off;
379 len -= off;
380 if (len>count) len = count;
381 if (len<0) len = 0;
382 return len;
385 static int __init
386 efi_rtc_init(void)
388 printk(KERN_INFO "EFI Time Services Driver v%s\n", EFI_RTC_VERSION);
390 misc_register(&efi_rtc_dev);
392 create_proc_read_entry ("efirtc", 0, NULL, efi_rtc_read_proc, NULL);
394 return 0;
397 static int __exit
398 efi_rtc_exit(void)
400 /* not yet used */
401 return 0;
404 module_init(efi_rtc_init);
405 module_exit(efi_rtc_exit);