Linux RTC
 help / color / mirror / Atom feed
From: Heiner Kallweit <hkallweit1@gmail.com>
To: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: linux-rtc@vger.kernel.org
Subject: [PATCH] rtc: core: let rtc_set_time properly populate element wday
Date: Sat, 26 Aug 2017 21:16:34 +0200	[thread overview]
Message-ID: <3b43da73-ff4e-4519-d1c7-266b17cc5d05@gmail.com> (raw)

Some drivers use element wday of the struct rtc_time which is passed to
callback set_time. However element wday may incorrectly or not be
populated if struct rtc_time is coming from userspace via rtc_dev_ioctl.
rtc_valid_tm() doesn't check element wday.

Therefore convert struct rtc_time to time64_t and then use
rtc_time64_to_tm to generate a struct rtc_time with all elements properly
populated.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 drivers/rtc/interface.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index 8cec9a02..5a53c590 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -59,28 +59,36 @@ EXPORT_SYMBOL_GPL(rtc_read_time);
 
 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
 {
+	time64_t secs64;
 	int err;
 
 	err = rtc_valid_tm(tm);
 	if (err != 0)
 		return err;
 
+	secs64 = rtc_tm_to_time64(tm);
+
 	err = mutex_lock_interruptible(&rtc->ops_lock);
 	if (err)
 		return err;
 
 	if (!rtc->ops)
 		err = -ENODEV;
-	else if (rtc->ops->set_time)
-		err = rtc->ops->set_time(rtc->dev.parent, tm);
-	else if (rtc->ops->set_mmss64) {
-		time64_t secs64 = rtc_tm_to_time64(tm);
-
+	else if (rtc->ops->set_time) {
+		struct rtc_time tmp;
+
+		/*
+		 * element wday of tm may not be set, therefore use
+		 * rtc_time64_to_tm to generate a struct rtc_time
+		 * with all elements being properly populated
+		 */
+		rtc_time64_to_tm(secs64, &tmp);
+		err = rtc->ops->set_time(rtc->dev.parent, &tmp);
+	} else if (rtc->ops->set_mmss64)
 		err = rtc->ops->set_mmss64(rtc->dev.parent, secs64);
-	} else if (rtc->ops->set_mmss) {
-		time64_t secs64 = rtc_tm_to_time64(tm);
+	else if (rtc->ops->set_mmss)
 		err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
-	} else
+	else
 		err = -EINVAL;
 
 	pm_stay_awake(rtc->dev.parent);
-- 
2.14.1

             reply	other threads:[~2017-08-26 19:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-26 19:16 Heiner Kallweit [this message]
2017-08-26 19:56 ` [PATCH] rtc: core: let rtc_set_time properly populate element wday Alexandre Belloni
2017-08-26 19:59   ` Alexandre Belloni
2017-08-27 12:39     ` Heiner Kallweit
2017-08-27 19:35       ` Heiner Kallweit
2017-08-28  7:54         ` Alexandre Belloni
2017-08-28 18:13           ` Heiner Kallweit
2017-08-28 18:54             ` Alexandre Belloni

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=3b43da73-ff4e-4519-d1c7-266b17cc5d05@gmail.com \
    --to=hkallweit1@gmail.com \
    --cc=alexandre.belloni@free-electrons.com \
    --cc=linux-rtc@vger.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