linux-efi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Prarit Bhargava <prarit-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
To: linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Cc: Prarit Bhargava <prarit-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>,
	Thomas Gleixner <tglx-hfZtesqFncYOwBW4kG4KsQ@public.gmane.org>,
	John Stultz <john.stultz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>,
	x86-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
	Matt Fleming
	<matt.fleming-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>,
	David Vrabel
	<david.vrabel-Sxgqhf6Nn4DQT0dZR+AlfA@public.gmane.org>,
	Andrew Morton
	<akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>,
	Andi Kleen <ak-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>,
	linux-efi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: [RFE PATCH 2/2] rtc, add write functionality to sysfs
Date: Thu, 14 Feb 2013 12:02:55 -0500	[thread overview]
Message-ID: <1360861375-24131-3-git-send-email-prarit@redhat.com> (raw)
In-Reply-To: <1360861375-24131-1-git-send-email-prarit-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>

/sys/class/rtc/rtcX/date and /sys/class/rtc/rtcX/time currently have
read-only access.  This patch introduces write functionality which will
set the rtc time.

Usage: echo YYYY-MM-DD > /sys/class/rtc/rtcX/date
       echo HH:MM:SS > /sys/class/rtc/rtcX/time

Signed-off-by: Prarit Bhargava <prarit-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
Cc: Thomas Gleixner <tglx-hfZtesqFncYOwBW4kG4KsQ@public.gmane.org>
Cc: John Stultz <john.stultz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
Cc: x86-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org
Cc: Matt Fleming <matt.fleming-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
Cc: David Vrabel <david.vrabel-Sxgqhf6Nn4DQT0dZR+AlfA@public.gmane.org>
Cc: Andrew Morton <akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>
Cc: Andi Kleen <ak-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
Cc: linux-efi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
---
 drivers/rtc/rtc-sysfs.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 84 insertions(+), 2 deletions(-)

diff --git a/drivers/rtc/rtc-sysfs.c b/drivers/rtc/rtc-sysfs.c
index b70e2bb..87d63d7 100644
--- a/drivers/rtc/rtc-sysfs.c
+++ b/drivers/rtc/rtc-sysfs.c
@@ -48,6 +48,46 @@ rtc_sysfs_show_date(struct device *dev, struct device_attribute *attr,
 }
 
 static ssize_t
+rtc_sysfs_store_date(struct device *dev, struct device_attribute *attr,
+		     const char *buf, size_t count)
+{
+	ssize_t retval;
+	struct rtc_time tm;
+	char tmp[11] = "\0", *tmpptr = tmp;
+	char *cyear, *cmonth, *cday;
+	int year, month, day;
+
+	if (!capable(CAP_SYS_TIME))
+		return -EPERM;
+
+	/* from rtc_sysfs_show_date(): date string format is YYYY-MM-DD */
+	if (strlen(buf) != 11)
+		return -EINVAL;
+
+	strncpy(tmp, buf, 11);
+	cyear = strsep(&tmpptr, "-");
+	sscanf(cyear, "%i", &year);
+	cmonth = strsep(&tmpptr, "-");
+	sscanf(cmonth, "%i", &month);
+	cday = strsep(&tmpptr, "-");
+	sscanf(cday, "%i", &day);
+
+	retval = rtc_read_time(to_rtc_device(dev), &tm);
+	if (retval)
+		return -ENODEV;
+
+	tm.tm_year = year - 1900;
+	tm.tm_mon = month - 1;
+	tm.tm_mday = day;
+
+	retval = rtc_set_time(to_rtc_device(dev), &tm);
+	if (retval)
+		return retval;
+
+	return count;
+}
+
+static ssize_t
 rtc_sysfs_show_time(struct device *dev, struct device_attribute *attr,
 		char *buf)
 {
@@ -64,6 +104,46 @@ rtc_sysfs_show_time(struct device *dev, struct device_attribute *attr,
 }
 
 static ssize_t
+rtc_sysfs_store_time(struct device *dev, struct device_attribute *attr,
+		     const char *buf, size_t count)
+{
+	ssize_t retval;
+	struct rtc_time tm;
+	char tmp[9] = "\0", *tmpptr = tmp;
+	char *chour, *cmin, *csec;
+	int hour, min, sec;
+
+	if (!capable(CAP_SYS_TIME))
+		return -EPERM;
+
+	/* from rtc_sysfs_show_time(): time string format is HH:MM:SS */
+	if (strlen(buf) != 9)
+		return -EINVAL;
+
+	strncpy(tmp, buf, 9);
+	chour = strsep(&tmpptr, ":");
+	sscanf(chour, "%i", &hour);
+	cmin = strsep(&tmpptr, ":");
+	sscanf(cmin, "%i", &min);
+	csec = strsep(&tmpptr, ":");
+	sscanf(csec, "%i", &sec);
+
+	retval = rtc_read_time(to_rtc_device(dev), &tm);
+	if (retval)
+		return -ENODEV;
+
+	tm.tm_hour = hour;
+	tm.tm_min = min;
+	tm.tm_sec = sec;
+
+	retval = rtc_set_time(to_rtc_device(dev), &tm);
+	if (retval)
+		return retval;
+
+	return count;
+}
+
+static ssize_t
 rtc_sysfs_show_since_epoch(struct device *dev, struct device_attribute *attr,
 		char *buf)
 {
@@ -124,8 +204,10 @@ rtc_sysfs_show_hctosys(struct device *dev, struct device_attribute *attr,
 
 static struct device_attribute rtc_attrs[] = {
 	__ATTR(name, S_IRUGO, rtc_sysfs_show_name, NULL),
-	__ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL),
-	__ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL),
+	__ATTR(date, S_IRUGO | S_IWUGO, rtc_sysfs_show_date,
+	       rtc_sysfs_store_date),
+	__ATTR(time, S_IRUGO | S_IWUGO, rtc_sysfs_show_time,
+	       rtc_sysfs_store_time),
 	__ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL),
 	__ATTR(max_user_freq, S_IRUGO | S_IWUSR, rtc_sysfs_show_max_user_freq,
 			rtc_sysfs_set_max_user_freq),
-- 
1.8.1.2

  parent reply	other threads:[~2013-02-14 17:02 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-14 17:02 [RFE PATCH 0/2] x86, rtc, ntp, Enable full rtc synchronization Prarit Bhargava
2013-02-14 17:02 ` [RFE PATCH 1/2] x86, rtc, ntp, Do full rtc synchronization with ntp Prarit Bhargava
     [not found]   ` <1360861375-24131-2-git-send-email-prarit-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2013-02-22 20:42     ` John Stultz
     [not found]       ` <5127D853.6030703-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2013-02-24 17:12         ` Prarit Bhargava
     [not found]           ` <512A4A19.2090800-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2013-02-25 15:04             ` Alessandro Zummo
     [not found] ` <1360861375-24131-1-git-send-email-prarit-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2013-02-14 17:02   ` Prarit Bhargava [this message]
     [not found]     ` <1360861375-24131-3-git-send-email-prarit-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2013-02-22 20:43       ` [RFE PATCH 2/2] rtc, add write functionality to sysfs John Stultz
     [not found]         ` <5127D884.6020701-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2013-02-22 20:55           ` Prarit Bhargava
2013-02-22 21:05             ` John Stultz
     [not found]               ` <5127DDAF.2000403-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2013-02-23 23:11                 ` Alessandro Zummo
     [not found]                   ` <B22B05C4-B8D2-44B9-971B-37913DE59EF6-BfzFCNDTiLLj+vYz1yj4TQ@public.gmane.org>
2013-02-24 17:03                     ` Prarit Bhargava
     [not found]                       ` <512A47C5.3010500-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2013-02-25 14:58                         ` Alessandro Zummo
     [not found]                           ` <20130225155812.0758e093-7UIfgwKH8t5lecnr3/Sf3RLwuzhV/fVh@public.gmane.org>
2013-02-28 14:50                             ` Prarit Bhargava
2013-02-28 18:51                       ` John Stultz

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=1360861375-24131-3-git-send-email-prarit@redhat.com \
    --to=prarit-h+wxahxf7alqt0dzr+alfa@public.gmane.org \
    --cc=ak-VuQAYsv1563Yd54FQh9/CA@public.gmane.org \
    --cc=akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org \
    --cc=david.vrabel-Sxgqhf6Nn4DQT0dZR+AlfA@public.gmane.org \
    --cc=john.stultz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
    --cc=linux-efi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=matt.fleming-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org \
    --cc=tglx-hfZtesqFncYOwBW4kG4KsQ@public.gmane.org \
    --cc=x86-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.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).