Blame view

drivers/char/efirtc.c 9.25 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  /*
   * EFI Time Services Driver for Linux
   *
   * Copyright (C) 1999 Hewlett-Packard Co
   * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com>
   *
   * Based on skeleton from the drivers/char/rtc.c driver by P. Gortmaker
   *
   * This code provides an architected & portable interface to the real time
   * clock by using EFI instead of direct bit fiddling. The functionalities are 
   * quite different from the rtc.c driver. The only way to talk to the device 
   * is by using ioctl(). There is a /proc interface which provides the raw 
   * information.
   *
   * Please note that we have kept the API as close as possible to the
   * legacy RTC. The standard /sbin/hwclock program should work normally 
   * when used to get/set the time.
   *
   * NOTES:
   *	- Locking is required for safe execution of EFI calls with regards
8dfba4d71   Joe Perches   drivers/char/: Sp...
21
   *	  to interrupts and SMP.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
24
25
26
27
28
   *
   * TODO (December 1999):
   * 	- provide the API to set/get the WakeUp Alarm (different from the
   *	  rtc.c alarm).
   *	- SMP testing
   * 	- Add module support
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
31
  #include <linux/types.h>
  #include <linux/errno.h>
  #include <linux/miscdevice.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
  #include <linux/init.h>
  #include <linux/rtc.h>
  #include <linux/proc_fs.h>
788416bce   David Howells   efirtc: Don't use...
35
  #include <linux/seq_file.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
  #include <linux/efi.h>
76528a42e   Alan Cox   efirtc: push down...
37
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
41
42
43
44
45
46
47
48
  
  #define EFI_RTC_VERSION		"0.4"
  
  #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
  /*
   * EFI Epoch is 1/1/1998
   */
  #define EFI_RTC_EPOCH		1998
  
  static DEFINE_SPINLOCK(efi_rtc_lock);
76528a42e   Alan Cox   efirtc: push down...
49
50
  static long efi_rtc_ioctl(struct file *file, unsigned int cmd,
  							unsigned long arg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  
  #define is_leap(year) \
            ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  
  static const unsigned short int __mon_yday[2][13] =
  {
  	/* Normal years.  */
  	{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  	/* Leap years.  */  
  	{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  };
  
  /*
   * returns day of the year [0-365]
   */
  static inline int
  compute_yday(efi_time_t *eft)
  {
  	/* efi_time_t.month is in the [1-12] so, we need -1 */
  	return  __mon_yday[is_leap(eft->year)][eft->month-1]+ eft->day -1;
  }
  /*
   * returns day of the week [0-6] 0=Sunday
   *
   * Don't try to provide a year that's before 1998, please !
   */
  static int
  compute_wday(efi_time_t *eft)
  {
  	int y;
  	int ndays = 0;
  
  	if ( eft->year < 1998 ) {
  		printk(KERN_ERR "efirtc: EFI year < 1998, invalid date
  ");
  		return -1;
  	}
  
  	for(y=EFI_RTC_EPOCH; y < eft->year; y++ ) {
  		ndays += 365 + (is_leap(y) ? 1 : 0);
  	}
  	ndays += compute_yday(eft);
  
  	/*
  	 * 4=1/1/1998 was a Thursday
  	 */
  	return (ndays + 4) % 7;
  }
  
  static void
  convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
  {
  
  	eft->year	= wtime->tm_year + 1900;
  	eft->month	= wtime->tm_mon + 1; 
  	eft->day	= wtime->tm_mday;
  	eft->hour	= wtime->tm_hour;
  	eft->minute	= wtime->tm_min;
  	eft->second 	= wtime->tm_sec;
  	eft->nanosecond = 0; 
  	eft->daylight	= wtime->tm_isdst ? EFI_ISDST: 0;
  	eft->timezone	= EFI_UNSPECIFIED_TIMEZONE;
  }
  
  static void
  convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
  {
  	memset(wtime, 0, sizeof(*wtime));
  	wtime->tm_sec  = eft->second;
  	wtime->tm_min  = eft->minute;
  	wtime->tm_hour = eft->hour;
  	wtime->tm_mday = eft->day;
  	wtime->tm_mon  = eft->month - 1;
  	wtime->tm_year = eft->year - 1900;
  
  	/* day of the week [0-6], Sunday=0 */
  	wtime->tm_wday = compute_wday(eft);
  
  	/* day in the year [1-365]*/
  	wtime->tm_yday = compute_yday(eft);
  
  
  	switch (eft->daylight & EFI_ISDST) {
  		case EFI_ISDST:
  			wtime->tm_isdst = 1;
  			break;
  		case EFI_TIME_ADJUST_DAYLIGHT:
  			wtime->tm_isdst = 0;
  			break;
  		default:
  			wtime->tm_isdst = -1;
  	}
  }
76528a42e   Alan Cox   efirtc: push down...
144
145
  static long efi_rtc_ioctl(struct file *file, unsigned int cmd,
  							unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  {
  
  	efi_status_t	status;
  	unsigned long	flags;
  	efi_time_t	eft;
  	efi_time_cap_t	cap;
  	struct rtc_time	wtime;
  	struct rtc_wkalrm __user *ewp;
  	unsigned char	enabled, pending;
  
  	switch (cmd) {
  		case RTC_UIE_ON:
  		case RTC_UIE_OFF:
  		case RTC_PIE_ON:
  		case RTC_PIE_OFF:
  		case RTC_AIE_ON:
  		case RTC_AIE_OFF:
  		case RTC_ALM_SET:
  		case RTC_ALM_READ:
  		case RTC_IRQP_READ:
  		case RTC_IRQP_SET:
  		case RTC_EPOCH_READ:
  		case RTC_EPOCH_SET:
  			return -EINVAL;
  
  		case RTC_RD_TIME:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172
173
174
175
176
  			spin_lock_irqsave(&efi_rtc_lock, flags);
  
  			status = efi.get_time(&eft, &cap);
  
  			spin_unlock_irqrestore(&efi_rtc_lock,flags);
a5ee6dc9e   Thomas Gleixner   rtc: Remove BKL f...
177

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  			if (status != EFI_SUCCESS) {
  				/* should never happen */
  				printk(KERN_ERR "efitime: can't read time
  ");
  				return -EINVAL;
  			}
  
  			convert_from_efi_time(&eft, &wtime);
  
   			return copy_to_user((void __user *)arg, &wtime,
  					    sizeof (struct rtc_time)) ? - EFAULT : 0;
  
  		case RTC_SET_TIME:
  
  			if (!capable(CAP_SYS_TIME)) return -EACCES;
  
  			if (copy_from_user(&wtime, (struct rtc_time __user *)arg,
  					   sizeof(struct rtc_time)) )
  				return -EFAULT;
  
  			convert_to_efi_time(&wtime, &eft);
  
  			spin_lock_irqsave(&efi_rtc_lock, flags);
  
  			status = efi.set_time(&eft);
  
  			spin_unlock_irqrestore(&efi_rtc_lock,flags);
  
  			return status == EFI_SUCCESS ? 0 : -EINVAL;
  
  		case RTC_WKALM_SET:
  
  			if (!capable(CAP_SYS_TIME)) return -EACCES;
  
  			ewp = (struct rtc_wkalrm __user *)arg;
  
  			if (  get_user(enabled, &ewp->enabled)
  			   || copy_from_user(&wtime, &ewp->time, sizeof(struct rtc_time)) )
  				return -EFAULT;
  
  			convert_to_efi_time(&wtime, &eft);
  
  			spin_lock_irqsave(&efi_rtc_lock, flags);
  			/*
  			 * XXX Fixme:
  			 * As of EFI 0.92 with the firmware I have on my
  			 * machine this call does not seem to work quite
  			 * right
  			 */
  			status = efi.set_wakeup_time((efi_bool_t)enabled, &eft);
  
  			spin_unlock_irqrestore(&efi_rtc_lock,flags);
  
  			return status == EFI_SUCCESS ? 0 : -EINVAL;
  
  		case RTC_WKALM_RD:
  
  			spin_lock_irqsave(&efi_rtc_lock, flags);
  
  			status = efi.get_wakeup_time((efi_bool_t *)&enabled, (efi_bool_t *)&pending, &eft);
  
  			spin_unlock_irqrestore(&efi_rtc_lock,flags);
  
  			if (status != EFI_SUCCESS) return -EINVAL;
  
  			ewp = (struct rtc_wkalrm __user *)arg;
  
  			if (  put_user(enabled, &ewp->enabled)
  			   || put_user(pending, &ewp->pending)) return -EFAULT;
  
  			convert_from_efi_time(&eft, &wtime);
  
  			return copy_to_user(&ewp->time, &wtime,
  					    sizeof(struct rtc_time)) ? -EFAULT : 0;
  	}
76528a42e   Alan Cox   efirtc: push down...
253
  	return -ENOTTY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
256
257
258
259
260
  }
  
  /*
   *	We enforce only one user at a time here with the open/close.
   *	Also clear the previous interrupt data on an open, and clean
   *	up things on a close.
   */
76528a42e   Alan Cox   efirtc: push down...
261
  static int efi_rtc_open(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
263
264
265
266
267
268
269
  {
  	/*
  	 * nothing special to do here
  	 * We do accept multiple open files at the same time as we
  	 * synchronize on the per call operation.
  	 */
  	return 0;
  }
76528a42e   Alan Cox   efirtc: push down...
270
  static int efi_rtc_close(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
272
273
274
275
276
277
  {
  	return 0;
  }
  
  /*
   *	The various file operations we support.
   */
62322d255   Arjan van de Ven   [PATCH] make more...
278
  static const struct file_operations efi_rtc_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
  	.owner		= THIS_MODULE,
76528a42e   Alan Cox   efirtc: push down...
280
  	.unlocked_ioctl	= efi_rtc_ioctl,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
282
  	.open		= efi_rtc_open,
  	.release	= efi_rtc_close,
f29627c2a   John Kacur   efirtc: explicitl...
283
  	.llseek		= no_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
  };
76528a42e   Alan Cox   efirtc: push down...
285
  static struct miscdevice efi_rtc_dev= {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286
287
288
289
290
291
292
293
  	EFI_RTC_MINOR,
  	"efirtc",
  	&efi_rtc_fops
  };
  
  /*
   *	We export RAW EFI information to /proc/driver/efirtc
   */
788416bce   David Howells   efirtc: Don't use...
294
  static int efi_rtc_proc_show(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
296
297
  {
  	efi_time_t 	eft, alm;
  	efi_time_cap_t	cap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
299
300
301
302
303
304
305
306
307
308
309
310
  	efi_bool_t	enabled, pending;	
  	unsigned long	flags;
  
  	memset(&eft, 0, sizeof(eft));
  	memset(&alm, 0, sizeof(alm));
  	memset(&cap, 0, sizeof(cap));
  
  	spin_lock_irqsave(&efi_rtc_lock, flags);
  
  	efi.get_time(&eft, &cap);
  	efi.get_wakeup_time(&enabled, &pending, &alm);
  
  	spin_unlock_irqrestore(&efi_rtc_lock,flags);
788416bce   David Howells   efirtc: Don't use...
311
312
313
314
315
316
317
318
319
320
  	seq_printf(m,
  		   "Time           : %u:%u:%u.%09u
  "
  		   "Date           : %u-%u-%u
  "
  		   "Daylight       : %u
  ",
  		   eft.hour, eft.minute, eft.second, eft.nanosecond, 
  		   eft.year, eft.month, eft.day,
  		   eft.daylight);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
322
  
  	if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
788416bce   David Howells   efirtc: Don't use...
323
324
  		seq_puts(m, "Timezone       : unspecified
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325
326
  	else
  		/* XXX fixme: convert to string? */
788416bce   David Howells   efirtc: Don't use...
327
328
  		seq_printf(m, "Timezone       : %u
  ", eft.timezone);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
329
  		
788416bce   David Howells   efirtc: Don't use...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
  	seq_printf(m,
  		   "Alarm Time     : %u:%u:%u.%09u
  "
  		   "Alarm Date     : %u-%u-%u
  "
  		   "Alarm Daylight : %u
  "
  		   "Enabled        : %s
  "
  		   "Pending        : %s
  ",
  		   alm.hour, alm.minute, alm.second, alm.nanosecond, 
  		   alm.year, alm.month, alm.day, 
  		   alm.daylight,
  		   enabled == 1 ? "yes" : "no",
  		   pending == 1 ? "yes" : "no");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
347
  
  	if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
788416bce   David Howells   efirtc: Don't use...
348
349
  		seq_puts(m, "Timezone       : unspecified
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
350
351
  	else
  		/* XXX fixme: convert to string? */
788416bce   David Howells   efirtc: Don't use...
352
353
  		seq_printf(m, "Timezone       : %u
  ", alm.timezone);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
355
356
357
  
  	/*
  	 * now prints the capabilities
  	 */
788416bce   David Howells   efirtc: Don't use...
358
359
360
361
362
363
364
365
  	seq_printf(m,
  		   "Resolution     : %u
  "
  		   "Accuracy       : %u
  "
  		   "SetstoZero     : %u
  ",
  		   cap.resolution, cap.accuracy, cap.sets_to_zero);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366

788416bce   David Howells   efirtc: Don't use...
367
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
  }
788416bce   David Howells   efirtc: Don't use...
369
  static int efi_rtc_proc_open(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
  {
788416bce   David Howells   efirtc: Don't use...
371
  	return single_open(file, efi_rtc_proc_show, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
  }
788416bce   David Howells   efirtc: Don't use...
373
374
375
376
  static const struct file_operations efi_rtc_proc_fops = {
  	.open		= efi_rtc_proc_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
2e7718cf7   Al Viro   rtc: single_open(...
377
  	.release	= single_release,
788416bce   David Howells   efirtc: Don't use...
378
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  static int __init 
  efi_rtc_init(void)
  {
  	int ret;
  	struct proc_dir_entry *dir;
  
  	printk(KERN_INFO "EFI Time Services Driver v%s
  ", EFI_RTC_VERSION);
  
  	ret = misc_register(&efi_rtc_dev);
  	if (ret) {
  		printk(KERN_ERR "efirtc: can't misc_register on minor=%d
  ",
  				EFI_RTC_MINOR);
  		return ret;
  	}
788416bce   David Howells   efirtc: Don't use...
395
  	dir = proc_create("driver/efirtc", 0, NULL, &efi_rtc_proc_fops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396
397
398
399
400
401
402
403
  	if (dir == NULL) {
  		printk(KERN_ERR "efirtc: can't create /proc/driver/efirtc.
  ");
  		misc_deregister(&efi_rtc_dev);
  		return -1;
  	}
  	return 0;
  }
9b7ebe012   Paul Gortmaker   drivers/char: mak...
404
  device_initcall(efi_rtc_init);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
405

9b7ebe012   Paul Gortmaker   drivers/char: mak...
406
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
  MODULE_LICENSE("GPL");
9b7ebe012   Paul Gortmaker   drivers/char: mak...
408
  */