Release 4.12 drivers/char/efirtc.c
  
  
  
/*
 * 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
 *        to interrupts and SMP.
 *
 * TODO (December 1999):
 *      - provide the API to set/get the WakeUp Alarm (different from the
 *        rtc.c alarm).
 *      - SMP testing
 *      - Add module support
 */
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/init.h>
#include <linux/rtc.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/efi.h>
#include <linux/uaccess.h>
#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);
static long efi_rtc_ioctl(struct file *file, unsigned int cmd,
							unsigned long arg);
#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;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Linus Torvalds (pre-git) | 36 | 100.00% | 1 | 100.00% | 
| Total | 36 | 100.00% | 1 | 100.00% | 
/*
 * 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\n");
		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;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Linus Torvalds (pre-git) | 87 | 100.00% | 1 | 100.00% | 
| Total | 87 | 100.00% | 1 | 100.00% | 
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;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Linus Torvalds (pre-git) | 91 | 100.00% | 1 | 100.00% | 
| Total | 91 | 100.00% | 1 | 100.00% | 
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;
	}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Linus Torvalds (pre-git) | 125 | 90.58% | 1 | 50.00% | 
| Andrew Morton | 13 | 9.42% | 1 | 50.00% | 
| Total | 138 | 100.00% | 2 | 100.00% | 
static long efi_rtc_ioctl(struct file *file, unsigned int cmd,
							unsigned long arg)
{
	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:
			spin_lock_irqsave(&efi_rtc_lock, flags);
			status = efi.get_time(&eft, &cap);
			spin_unlock_irqrestore(&efi_rtc_lock,flags);
			if (status != EFI_SUCCESS) {
				/* should never happen */
				printk(KERN_ERR "efitime: can't read time\n");
				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;
	}
	return -ENOTTY;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Linus Torvalds (pre-git) | 490 | 98.20% | 3 | 60.00% | 
| David Mosberger-Tang | 6 | 1.20% | 1 | 20.00% | 
| Alan Cox | 3 | 0.60% | 1 | 20.00% | 
| Total | 499 | 100.00% | 5 | 100.00% | 
/*
 *      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.
 */
static int efi_rtc_open(struct inode *inode, struct file *file)
{
	/*
         * 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;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Linus Torvalds (pre-git) | 20 | 100.00% | 1 | 100.00% | 
| Total | 20 | 100.00% | 1 | 100.00% | 
static int efi_rtc_close(struct inode *inode, struct file *file)
{
	return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Linus Torvalds (pre-git) | 19 | 100.00% | 1 | 100.00% | 
| Total | 19 | 100.00% | 1 | 100.00% | 
/*
 *      The various file operations we support.
 */
static const struct file_operations efi_rtc_fops = {
	.owner		= THIS_MODULE,
	.unlocked_ioctl	= efi_rtc_ioctl,
	.open		= efi_rtc_open,
	.release	= efi_rtc_close,
	.llseek		= no_llseek,
};
static struct miscdevice efi_rtc_dev= {
	EFI_RTC_MINOR,
	"efirtc",
	&efi_rtc_fops
};
/*
 *      We export RAW EFI information to /proc/driver/efirtc
 */
static int efi_rtc_proc_show(struct seq_file *m, void *v)
{
	efi_time_t 	eft, alm;
	efi_time_cap_t	cap;
	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);
	seq_printf(m,
		   "Time           : %u:%u:%u.%09u\n"
		   "Date           : %u-%u-%u\n"
		   "Daylight       : %u\n",
		   eft.hour, eft.minute, eft.second, eft.nanosecond, 
		   eft.year, eft.month, eft.day,
		   eft.daylight);
	if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
		seq_puts(m, "Timezone       : unspecified\n");
	else
		/* XXX fixme: convert to string? */
		seq_printf(m, "Timezone       : %u\n", eft.timezone);
		
	seq_printf(m,
		   "Alarm Time     : %u:%u:%u.%09u\n"
		   "Alarm Date     : %u-%u-%u\n"
		   "Alarm Daylight : %u\n"
		   "Enabled        : %s\n"
		   "Pending        : %s\n",
		   alm.hour, alm.minute, alm.second, alm.nanosecond, 
		   alm.year, alm.month, alm.day, 
		   alm.daylight,
		   enabled == 1 ? "yes" : "no",
		   pending == 1 ? "yes" : "no");
	if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
		seq_puts(m, "Timezone       : unspecified\n");
	else
		/* XXX fixme: convert to string? */
		seq_printf(m, "Timezone       : %u\n", alm.timezone);
	/*
         * now prints the capabilities
         */
	seq_printf(m,
		   "Resolution     : %u\n"
		   "Accuracy       : %u\n"
		   "SetstoZero     : %u\n",
		   cap.resolution, cap.accuracy, cap.sets_to_zero);
	return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Linus Torvalds (pre-git) | 185 | 63.14% | 1 | 20.00% | 
| David Mosberger-Tang | 43 | 14.68% | 1 | 20.00% | 
| Andrew Morton | 39 | 13.31% | 1 | 20.00% | 
| David Howells | 23 | 7.85% | 1 | 20.00% | 
| Stéphane Eranian | 3 | 1.02% | 1 | 20.00% | 
| Total | 293 | 100.00% | 5 | 100.00% | 
static int efi_rtc_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, efi_rtc_proc_show, NULL);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| David Howells | 16 | 61.54% | 1 | 50.00% | 
| Linus Torvalds (pre-git) | 10 | 38.46% | 1 | 50.00% | 
| Total | 26 | 100.00% | 2 | 100.00% | 
static const struct file_operations efi_rtc_proc_fops = {
	.open		= efi_rtc_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};
static int __init 
efi_rtc_init(void)
{
	int ret;
	struct proc_dir_entry *dir;
	printk(KERN_INFO "EFI Time Services Driver v%s\n", EFI_RTC_VERSION);
	ret = misc_register(&efi_rtc_dev);
	if (ret) {
		printk(KERN_ERR "efirtc: can't misc_register on minor=%d\n",
				EFI_RTC_MINOR);
		return ret;
	}
	dir = proc_create("driver/efirtc", 0, NULL, &efi_rtc_proc_fops);
	if (dir == NULL) {
		printk(KERN_ERR "efirtc: can't create /proc/driver/efirtc.\n");
		misc_deregister(&efi_rtc_dev);
		return -1;
	}
	return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Linus Torvalds (pre-git) | 34 | 37.36% | 1 | 20.00% | 
| Stéphane Eranian | 32 | 35.16% | 1 | 20.00% | 
| Dave Jones | 21 | 23.08% | 1 | 20.00% | 
| David Howells | 3 | 3.30% | 1 | 20.00% | 
| David Mosberger-Tang | 1 | 1.10% | 1 | 20.00% | 
| Total | 91 | 100.00% | 5 | 100.00% | 
device_initcall(efi_rtc_init);
/*
MODULE_LICENSE("GPL");
*/
Overall Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Linus Torvalds (pre-git) | 1275 | 83.22% | 5 | 23.81% | 
| David Howells | 68 | 4.44% | 1 | 4.76% | 
| Andrew Morton | 53 | 3.46% | 2 | 9.52% | 
| David Mosberger-Tang | 51 | 3.33% | 3 | 14.29% | 
| Stéphane Eranian | 36 | 2.35% | 1 | 4.76% | 
| Dave Jones | 21 | 1.37% | 1 | 4.76% | 
| Art Haas | 8 | 0.52% | 1 | 4.76% | 
| Alan Cox | 6 | 0.39% | 1 | 4.76% | 
| John Kacur | 5 | 0.33% | 1 | 4.76% | 
| Thomas Gleixner | 4 | 0.26% | 1 | 4.76% | 
| Paul Gortmaker | 2 | 0.13% | 1 | 4.76% | 
| Arjan van de Ven | 1 | 0.07% | 1 | 4.76% | 
| Joe Perches | 1 | 0.07% | 1 | 4.76% | 
| Al Viro | 1 | 0.07% | 1 | 4.76% | 
| Total | 1532 | 100.00% | 21 | 100.00% | 
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.