linux-efi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Lee, Chun-Yi" <joeyli.kernel@gmail.com>
To: "Rafael J. Wysocki" <rjw@rjwysocki.net>,
	Alessandro Zummo <a.zummo@towertech.it>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Matt Fleming <matt@console-pimps.org>,
	Matthew Garrett <matthew.garrett@nebula.com>
Cc: Elliott@hp.com, samer.el-haj-mahmoud@hp.com,
	Oliver Neukum <oneukum@suse.de>,
	werner@suse.com, trenn@suse.de, JBeulich@suse.com,
	linux-kernel@vger.kernel.org, rtc-linux@googlegroups.com,
	x86@kernel.org,
	"linux-efi@vger.kernel.org" <linux-efi@vger.kernel.org>,
	linux-acpi@vger.kernel.org, "Lee, Chun-Yi" <jlee@suse.com>
Subject: [RFC PATCH 09/14] efi: move functions of access efi time to header file for sharing
Date: Thu, 19 Dec 2013 15:51:50 +0800	[thread overview]
Message-ID: <1387439515-8926-10-git-send-email-jlee@suse.com> (raw)
In-Reply-To: <1387439515-8926-1-git-send-email-jlee@suse.com>

There have some functions, e.g. compute_yday, compute_wday, convert efi
time... are duplicated in efirtc, rtc-efi and will also used in rtc.c.
So this patch moved those functions of access efi time to efi.h header
file for sharing.

Signed-off-by: Lee, Chun-Yi <jlee@suse.com>
---
 drivers/char/efirtc.c |   98 ------------------------------------
 drivers/rtc/rtc-efi.c |  133 ++----------------------------------------------
 include/linux/efi.h   |  134 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 140 insertions(+), 225 deletions(-)

diff --git a/drivers/char/efirtc.c b/drivers/char/efirtc.c
index e39e740..8a30a02 100644
--- a/drivers/char/efirtc.c
+++ b/drivers/char/efirtc.c
@@ -41,109 +41,11 @@
 
 #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;
-}
-/*
- * 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;
-}
-
-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;
-	}
-}
-
 static long efi_rtc_ioctl(struct file *file, unsigned int cmd,
 							unsigned long arg)
 {
diff --git a/drivers/rtc/rtc-efi.c b/drivers/rtc/rtc-efi.c
index 693ea47..4687022 100644
--- a/drivers/rtc/rtc-efi.c
+++ b/drivers/rtc/rtc-efi.c
@@ -22,95 +22,6 @@
 #include <linux/rtc.h>
 #include <linux/efi.h>
 
-#define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
-/*
- * EFI Epoch is 1/1/1998
- */
-#define EFI_RTC_EPOCH		1998
-
-/*
- * 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 rtc_year_days(eft->day, eft->month - 1, eft->year);
-}
-/*
- * 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) {
-		pr_err("EFI year < 1998, invalid date\n");
-		return -1;
-	}
-
-	for (y = EFI_RTC_EPOCH; y < eft->year; y++)
-		ndays += 365 + (is_leap_year(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;
-#ifdef CONFIG_IA64
-	/* avoid overwrite timezone on non-IA64 platform. e.g. x86_64 */
-	eft->timezone	= EFI_UNSPECIFIED_TIMEZONE;
-#endif
-}
-
-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;
-	}
-}
-
 static int efi_read_gmtoff(struct device *dev, long int *arg)
 {
 	efi_status_t status;
@@ -230,54 +141,22 @@ static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
 	return status == EFI_SUCCESS ? 0 : -EINVAL;
 }
 
-static int efi_read_time(struct device *dev, struct rtc_time *tm)
+static int efi_rtc_read_time(struct device *dev, struct rtc_time *tm)
 {
-	efi_status_t status;
-	efi_time_t eft;
-	efi_time_cap_t cap;
-
-	status = efi.get_time(&eft, &cap);
-
-	if (status != EFI_SUCCESS) {
-		/* should never happen */
-		dev_err(dev, "can't read time\n");
-		return -EINVAL;
-	}
-
-	convert_from_efi_time(&eft, tm);
-
-	return rtc_valid_tm(tm);
+	return efi_read_time(tm);
 }
 
-static int efi_set_time(struct device *dev, struct rtc_time *tm)
+static int efi_rtc_set_time(struct device *dev, struct rtc_time *tm)
 {
-	efi_status_t status;
-	efi_time_t eft;
-#ifdef CONFIG_X86
-	efi_time_cap_t cap;
-
-	/* read time for grab timezone to avoid overwrite it */
-	status = efi.get_time(&eft, &cap);
-
-	if (status != EFI_SUCCESS) {
-		pr_err("efitime: can't read time\n");
-		return -EINVAL;
-	}
-#endif
-
-	convert_to_efi_time(tm, &eft);
-
-	status = efi.set_time(&eft);
-
-	return status == EFI_SUCCESS ? 0 : -EINVAL;
+	return efi_set_time(tm);
 }
 
 static const struct rtc_class_ops efi_rtc_ops = {
 #ifdef CONFIG_X86
 	.ioctl = efi_rtc_ioctl,
 #endif
-	.read_time = efi_read_time,
-	.set_time = efi_set_time,
+	.read_time = efi_rtc_read_time,
+	.set_time = efi_rtc_set_time,
 	.read_alarm = efi_read_alarm,
 	.set_alarm = efi_set_alarm,
 };
diff --git a/include/linux/efi.h b/include/linux/efi.h
index fc8fa0e..3859f3e 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -132,6 +132,12 @@ typedef int (*efi_freemem_callback_t) (u64 start, u64 end, void *arg);
 #define EFI_TIME_ADJUST_DAYLIGHT 0x1
 #define EFI_TIME_IN_DAYLIGHT     0x2
 #define EFI_UNSPECIFIED_TIMEZONE 0x07ff
+#define EFI_ISDST		 (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
+
+/*
+ * EFI Epoch is 1/1/1998
+ */
+#define EFI_RTC_EPOCH           1998
 
 typedef struct {
 	u16 year;
@@ -154,6 +160,90 @@ typedef struct {
 } efi_time_cap_t;
 
 /*
+ * 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 rtc_year_days(eft->day, eft->month - 1, eft->year);
+}
+
+/*
+ * returns day of the week [0-6] 0=Sunday
+ *
+ * Don't try to provide a year that's before 1998, please !
+ */
+static inline int
+compute_wday(efi_time_t *eft)
+{
+	int y;
+	int ndays = 0;
+
+	if (eft->year < 1998) {
+		pr_err("EFI year < 1998, invalid date\n");
+		return -1;
+	}
+
+	for (y = EFI_RTC_EPOCH; y < eft->year; y++)
+		ndays += 365 + (is_leap_year(y) ? 1 : 0);
+
+	ndays += compute_yday(eft);
+
+	/*
+	 * 4=1/1/1998 was a Thursday
+	 */
+	return (ndays + 4) % 7;
+}
+
+static inline 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;
+	}
+}
+
+static inline 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;
+#ifdef CONFIG_IA64
+	/* avoid overwrite timezone on non-IA64 platform. e.g. x86_64 */
+	eft->timezone   = EFI_UNSPECIFIED_TIMEZONE;
+#endif
+}
+
+
+/*
  * EFI Boot Services table
  */
 typedef struct {
@@ -585,6 +675,50 @@ efi_guid_unparse(efi_guid_t *guid, char *out)
         return out;
 }
 
+static inline int
+efi_set_time(struct rtc_time *tm)
+{
+	efi_status_t status;
+	efi_time_t eft;
+#ifdef CONFIG_X86
+	efi_time_cap_t cap;
+
+	/* read time for grab timezone to avoid overwrite it */
+	status = efi.get_time(&eft, &cap);
+
+	if (status != EFI_SUCCESS) {
+		pr_err("efi: can't read time\n");
+		return -EINVAL;
+	}
+#endif
+
+	convert_to_efi_time(tm, &eft);
+
+	status = efi.set_time(&eft);
+
+	return status == EFI_SUCCESS ? 0 : -EINVAL;
+}
+
+static inline int
+efi_read_time(struct rtc_time *tm)
+{
+	efi_status_t status;
+	efi_time_t eft;
+	efi_time_cap_t cap;
+
+	status = efi.get_time(&eft, &cap);
+
+	if (status != EFI_SUCCESS) {
+		/* should never happen */
+		pr_err("efi: can't read time\n");
+		return -EINVAL;
+	}
+
+	convert_from_efi_time(&eft, tm);
+
+	return rtc_valid_tm(tm);
+}
+
 extern void efi_init (void);
 extern void *efi_get_pal_addr (void);
 extern void efi_map_pal_code (void);
-- 
1.6.4.2

  parent reply	other threads:[~2013-12-19  7:51 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-19  7:51 [RFC PATCH 00/14] Support timezone of ACPI TAD and EFI TIME Lee, Chun-Yi
2013-12-19  7:51 ` [PATCH 01/14] rtc-efi: fix decrease day twice when computing year days Lee, Chun-Yi
     [not found] ` <1387439515-8926-1-git-send-email-jlee-IBi9RG/b67k@public.gmane.org>
2013-12-19  7:51   ` [PATCH 03/14] rtc: block registration of rtc-cmos when CMOS RTC Not Present Lee, Chun-Yi
2013-12-19  7:51 ` [RFC PATCH 04/14] ACPI: Add ACPI 5.0 Time and Alarm Device driver Lee, Chun-Yi
     [not found]   ` <1387439515-8926-5-git-send-email-jlee-IBi9RG/b67k@public.gmane.org>
2013-12-19 15:22     ` H. Peter Anvin
     [not found]       ` <52B30F43.1060306-YMNOUZJC4hwAvxtiuMwx3w@public.gmane.org>
2013-12-20  5:41         ` joeyli
     [not found]           ` <1387518099.3539.4453.camel-ONCj+Eqt86TasUa73XJKwA@public.gmane.org>
2014-01-01  0:42             ` H. Peter Anvin
     [not found]               ` <52C3647B.7000708-YMNOUZJC4hwAvxtiuMwx3w@public.gmane.org>
2014-01-06  8:58                 ` joeyli
2014-01-07  5:37                   ` H. Peter Anvin
2014-01-07 10:40                     ` joeyli
2014-01-07 16:35                       ` H. Peter Anvin
2014-01-08 14:59                         ` joeyli
     [not found]                           ` <1389193142.3539.6123.camel-ONCj+Eqt86TasUa73XJKwA@public.gmane.org>
2014-01-08 17:56                             ` H. Peter Anvin
     [not found]                               ` <52CD9139.2070302-YMNOUZJC4hwAvxtiuMwx3w@public.gmane.org>
2014-01-09  3:47                                 ` joeyli
     [not found]                                   ` <1389239259.24105.2.camel-ONCj+Eqt86TasUa73XJKwA@public.gmane.org>
2014-01-09  4:00                                     ` H. Peter Anvin
2014-01-09 12:16                                   ` Rafael J. Wysocki
2014-01-02  8:09     ` Lan Tianyu
2014-01-06  9:20       ` joeyli
2013-12-19  7:51 ` [RFC PATCH 05/14] rtc: Add RTC driver of ACPI Time and Alarm Device Lee, Chun-Yi
2013-12-19  7:51 ` [RFC PATCH 06/14] rtc-efi: register rtc-efi device when EFI enabled Lee, Chun-Yi
2013-12-19 14:09   ` Matt Fleming
2013-12-20  4:24     ` joeyli
     [not found]       ` <1387513491.3539.4345.camel-ONCj+Eqt86TasUa73XJKwA@public.gmane.org>
2013-12-20  4:30         ` H. Peter Anvin
2013-12-20 10:37           ` Borislav Petkov
     [not found]             ` <20131220103755.GA14784-fF5Pk5pvG8Y@public.gmane.org>
2013-12-20 15:14               ` Matthew Garrett
2013-12-20 21:04                 ` H. Peter Anvin
     [not found]                   ` <52B4B0ED.7030600-YMNOUZJC4hwAvxtiuMwx3w@public.gmane.org>
2013-12-21  1:24                     ` joeyli
2013-12-21  1:51                       ` H. Peter Anvin
2013-12-21 15:34                         ` joeyli
2013-12-20 16:48               ` H. Peter Anvin
2013-12-19  7:51 ` [RFC PATCH 07/14] rtc-efi: add GMTOFF support to rtc_efi Lee, Chun-Yi
     [not found]   ` <1387439515-8926-8-git-send-email-jlee-IBi9RG/b67k@public.gmane.org>
2013-12-20 15:11     ` Matthew Garrett
2013-12-21  3:56       ` joeyli
2013-12-19  7:51 ` [RFC PATCH 08/14] rtc-efi: set uie_unsupported for indicate rtc-efi doesn't support UIE mode Lee, Chun-Yi
2013-12-19  7:51 ` Lee, Chun-Yi [this message]
2013-12-19  7:51 ` [RFC PATCH 10/14] rtc: improve and move week day computing function to rtc header Lee, Chun-Yi
2013-12-19  7:51 ` [RFC PATCH 11/14] rtc: switch to get/set rtc time to efi functions if CMOS RTC Not Present git set Lee, Chun-Yi
2013-12-19  7:51 ` [RFC PATCH 12/14] efi: adjust system time base on timezone from EFI time services Lee, Chun-Yi
2013-12-19  7:51 ` [RFC PATCH 13/14] Documentation/RTC: add document of ACPI TAD and EFI TIME driver Lee, Chun-Yi
2013-12-19  7:51 ` [TEST PATCH 14/14] acpi: add early parameter to set CMOS RTC Not Present bit for testing Lee, Chun-Yi
     [not found] ` <1387448416-11672-1-git-send-email-jlee@suse.com>
     [not found]   ` <1387448416-11672-1-git-send-email-jlee-IBi9RG/b67k@public.gmane.org>
2013-12-19 10:49     ` [PATCH 02/14] x86-64/efi: Use EFI to deal with platform wall clock (again) Matt Fleming
     [not found]       ` <20131219104908.GE3145-HNK1S37rvNbeXh+fF434Mdi2O/JbrIOy@public.gmane.org>
2013-12-19 13:32         ` joeyli
     [not found]           ` <1387459939.3539.4092.camel-ONCj+Eqt86TasUa73XJKwA@public.gmane.org>
2013-12-20 11:29             ` One Thousand Gnomes
2013-12-23 23:25               ` H. Peter Anvin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1387439515-8926-10-git-send-email-jlee@suse.com \
    --to=joeyli.kernel@gmail.com \
    --cc=Elliott@hp.com \
    --cc=JBeulich@suse.com \
    --cc=a.zummo@towertech.it \
    --cc=hpa@zytor.com \
    --cc=jlee@suse.com \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matt@console-pimps.org \
    --cc=matthew.garrett@nebula.com \
    --cc=oneukum@suse.de \
    --cc=rjw@rjwysocki.net \
    --cc=rtc-linux@googlegroups.com \
    --cc=samer.el-haj-mahmoud@hp.com \
    --cc=trenn@suse.de \
    --cc=werner@suse.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).