All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Holler <holler@ahsoftware.de>
To: linux-kernel@vger.kernel.org
Cc: rtc-linux@googlegroups.com,
	Alessandro Zummo <a.zummo@towertech.it>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Jonathan Cameron <jic23@cam.ac.uk>, Jiri Kosina <jkosina@suse.cz>,
	Alexander Holler <holler@ahsoftware.de>
Subject: [PATCH 3/3] rtc: rtc-hid-sensor-time; add option hctosys to set time at boot
Date: Fri, 19 Apr 2013 17:14:12 +0200	[thread overview]
Message-ID: <1366384452-14367-4-git-send-email-holler@ahsoftware.de> (raw)
In-Reply-To: <1366384452-14367-1-git-send-email-holler@ahsoftware.de>

drivers/rtc/hctosys (CONFIG_RTC_HCTOSYS) doesn't work for
rtc-hid-sensor-time because it will be called in late_init, and thus before
rtc-hid-sensor-time gets loaded. To set the time through rtc-hid-sensor-time
at startup, the module now checks by default if the system time is before
1970-01-02 and sets the system time (once) if this is the case.

To disable this behaviour, set the module option hctosys to zero, e.g. by
using rtc-hid-sensor-time.hctosys=0 at the kernel command line if the
driver is statically linked into the kernel.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>
---
 drivers/rtc/rtc-hid-sensor-time.c | 72 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/drivers/rtc/rtc-hid-sensor-time.c b/drivers/rtc/rtc-hid-sensor-time.c
index 10fc0f0..3184729 100644
--- a/drivers/rtc/rtc-hid-sensor-time.c
+++ b/drivers/rtc/rtc-hid-sensor-time.c
@@ -27,6 +27,12 @@
 /* Usage ID from spec for Time: 0x2000A0 */
 #define DRIVER_NAME "HID-SENSOR-2000a0" /* must be lowercase */
 
+static bool hid_time_hctosys_enabled = 1;
+module_param_named(hctosys, hid_time_hctosys_enabled, bool, 0644);
+MODULE_PARM_DESC(hctosys,
+	"set the system time (once) if it is before 1970-01-02");
+static bool hid_time_time_set_once;
+
 enum hid_time_channel {
 	CHANNEL_SCAN_INDEX_YEAR,
 	CHANNEL_SCAN_INDEX_MONTH,
@@ -62,6 +68,38 @@ static const char * const hid_time_channel_names[TIME_RTC_CHANNEL_MAX] = {
 	"year", "month", "day", "hour", "minute", "second",
 };
 
+static void hid_time_hctosys(struct hid_time_state *time_state)
+{
+	struct timeval tv;
+	struct timespec ts;
+	int rc = rtc_valid_tm(&time_state->last_time);
+	if (rc) {
+		dev_err(time_state->rtc->dev.parent,
+			"hctosys: invalid date/time\n");
+		return;
+	}
+	do_gettimeofday(&tv);
+	if (tv.tv_sec >= 86400) /* 1970-01-02 00:00:00 UTC */
+		/*
+		 * Security: don't let a hot-pluggable device change
+		 * a valid system time.
+		 */
+		return;
+	ts.tv_nsec = NSEC_PER_SEC >> 1;
+	rtc_tm_to_time(&time_state->last_time, &ts.tv_sec);
+	rc = do_settimeofday(&ts);
+	dev_info(time_state->rtc->dev.parent,
+		"hctosys: setting system clock to "
+		"%d-%02d-%02d %02d:%02d:%02d UTC (%u)\n",
+		time_state->last_time.tm_year + 1900,
+		time_state->last_time.tm_mon + 1,
+		time_state->last_time.tm_mday,
+		time_state->last_time.tm_hour,
+		time_state->last_time.tm_min,
+		time_state->last_time.tm_sec,
+		(unsigned int) ts.tv_sec);
+}
+
 /* Callback handler to send event after all samples are received and captured */
 static int hid_time_proc_event(struct hid_sensor_hub_device *hsdev,
 				unsigned usage_id, void *priv)
@@ -73,6 +111,10 @@ static int hid_time_proc_event(struct hid_sensor_hub_device *hsdev,
 	time_state->last_time = time_state->time_buf;
 	spin_unlock_irqrestore(&time_state->lock_last_time, flags);
 	complete(&time_state->comp_last_time);
+	if (unlikely(!hid_time_time_set_once && hid_time_hctosys_enabled)) {
+		hid_time_time_set_once = 1;
+		hid_time_hctosys(time_state);
+	}
 	return 0;
 }
 
@@ -237,6 +279,22 @@ static const struct rtc_class_ops hid_time_rtc_ops = {
 	.read_time = hid_rtc_read_time,
 };
 
+struct hid_time_work_time_state {
+	struct work_struct work;
+	struct hid_time_state *time_state;
+};
+
+static void hid_time_request_report_work(struct work_struct *work)
+{
+	struct hid_time_state *time_state =
+		((struct hid_time_work_time_state *)work)->time_state;
+	/* get a report with all values through requesting one value */
+	sensor_hub_input_attr_get_raw_value(
+		time_state->common_attributes.hsdev, HID_USAGE_SENSOR_TIME,
+		hid_time_addresses[0], time_state->info[0].report_id);
+	kfree(work);
+}
+
 static int hid_time_probe(struct platform_device *pdev)
 {
 	int ret = 0;
@@ -287,6 +345,20 @@ static int hid_time_probe(struct platform_device *pdev)
 		return PTR_ERR(time_state->rtc);
 	}
 
+	if (!hid_time_time_set_once && hid_time_hctosys_enabled) {
+		/*
+		 * Request a HID report to set the time.
+		 * Calling sensor_hub_..._get_raw_value() here directly
+		 * doesn't work, therefor we have to use a work.
+		 */
+		struct hid_time_work_time_state *hdwork =
+			kmalloc(sizeof(struct hid_time_work_time_state),
+				GFP_KERNEL);
+		hdwork->time_state = time_state;
+		INIT_WORK(&hdwork->work, hid_time_request_report_work);
+		schedule_work(&hdwork->work);
+	}
+
 	return ret;
 }
 
-- 
1.8.1.4


  parent reply	other threads:[~2013-04-19 15:16 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-19 15:14 [PATCH 0/3] rtc: rtc-hid-sensor-time Alexander Holler
2013-04-19 15:14 ` [PATCH 1/3 RESEND] rtc: rtc-hid-sensor-time: allow full years (16bit) in HID reports Alexander Holler
2013-04-19 15:14 ` [PATCH 2/3] rtc: rtc-hid-sensor-time: allow 16 and 32 bit values for all attributes Alexander Holler
2013-04-19 15:14 ` Alexander Holler [this message]
2013-04-22 23:38   ` [PATCH 3/3] rtc: rtc-hid-sensor-time; add option hctosys to set time at boot Andrew Morton
2013-04-23  8:51     ` Alexander Holler
2013-04-23 10:08       ` Alexander Holler
2013-04-23 10:13         ` Alexander Holler
2013-04-23 10:17           ` Alexander Holler
2013-04-23 15:47             ` Alexander Holler
2013-04-24 21:14               ` Andrew Morton
2013-04-25  6:55                 ` Alexander Holler
2013-05-05 11:21             ` [PATCH 0/4] rtc: rtc-hid-sensor-time: some changes Alexander Holler
2013-05-05 11:21               ` [PATCH 1/4] rtc: rtc-hid-sensor-time: allow full years (16bit) in HID reports Alexander Holler
2013-05-05 11:21               ` [PATCH 2/4] rtc: rtc-hid-sensor-time: allow 16 and 32 bit values for all attributes Alexander Holler
2013-05-05 11:21               ` [PATCH 3/4] rtc: rtc-hid-sensor-time: add option hctosys to set time at boot Alexander Holler
2013-05-21 21:42                 ` Andrew Morton
2013-05-21 22:02                 ` John Stultz
2013-05-21 23:15                   ` Alexander Holler
2013-05-28 19:37                     ` John Stultz
2013-05-29  4:42                       ` Alexander Holler
2013-06-04 13:41                         ` Alexander Holler
2013-06-05 17:15                           ` [PATCH 0/3] RFC: timekeeping: rtc: change hctosys mechanism Alexander Holler
2013-06-05 17:15                             ` [PATCH 1/3] RFC: timekeeping: introduce flag systime_was_set Alexander Holler
2013-06-05 17:15                             ` [PATCH 2/3] RFC: timekeeping: rtc: Introduce new kernel parameter hctosys Alexander Holler
2013-06-05 17:15                             ` [PATCH 3/3] RFC: timekeeping: rtc: remove CONFIG_RTC_HCTOSYS and RTC_HCTOSYS_DEVICE Alexander Holler
2013-06-06 10:51                             ` [PATCH 0/3 v2] RFC: timekeeping: rtc: change hctosys mechanism Alexander Holler
2013-06-06 10:51                               ` [PATCH 1/3 RESEND] RFC: timekeeping: introduce flag systime_was_set Alexander Holler
2013-06-06 10:51                               ` [PATCH 2/3 v2] RFC: timekeeping: rtc: Introduce new kernel parameter hctosys Alexander Holler
2013-06-13 19:39                                 ` Alexander Holler
2013-06-14 16:52                                   ` [PATCH 0/9 v3] RFC: timekeeping: rtc: change hctosys mechanism Alexander Holler
2013-06-14 16:52                                     ` [PATCH 1/9 RESEND] rtc: rtc-hid-sensor-time: allow full years (16bit) in HID reports Alexander Holler
2013-06-14 16:52                                     ` [PATCH 2/9 RESEND] rtc: rtc-hid-sensor-time: allow 16 and 32 bit values for all attributes Alexander Holler
2013-06-14 16:52                                     ` [PATCH 3/9] rtc: rtc-hid-sensor-time: delay registering as rtc into a work Alexander Holler
2013-06-20 10:39                                       ` [PATCH 3/9 v2] " Alexander Holler
2013-06-26 19:55                                         ` Andrew Morton
2013-06-26 21:34                                           ` [rtc-linux] " Alexander Holler
2013-06-26 22:07                                             ` Greg KH
2013-06-26 23:51                                               ` Alexander Holler
2013-07-06  8:55                                                 ` Alexander Holler
2013-07-06 18:21                                                   ` Jiri Kosina
2013-07-07  7:35                                                     ` Alexander Holler
2013-07-08  9:12                                                       ` [PATCH 0/2] rtc: rtc-hid-sensor-time: enable HID input processing early Alexander Holler
2013-07-08  9:12                                                         ` [PATCH 1/2] rtc: rtc-hid-sensor-time: improve error handling when rtc register fails Alexander Holler
2013-07-08  9:12                                                         ` [PATCH 2/2] rtc: rtc-hid-sensor-time: enable HID input processing early Alexander Holler
2013-06-28  1:29                                             ` [rtc-linux] Re: [PATCH 3/9 v2] rtc: rtc-hid-sensor-time: delay registering as rtc into a work Alexander Holler
2013-06-14 16:52                                     ` [PATCH 4/9 RESEND] RFC: timekeeping: introduce flag systime_was_set Alexander Holler
2013-06-14 17:41                                       ` John Stultz
2013-06-14 18:05                                         ` [rtc-linux] " Alexander Holler
2013-06-14 18:28                                           ` John Stultz
2013-06-15  6:01                                             ` Alexander Holler
2013-06-17 18:10                                               ` John Stultz
2013-06-20 10:15                                                 ` Alexander Holler
2013-06-20 17:27                                                   ` John Stultz
2013-06-20 18:45                                                     ` Alexander Holler
2013-06-20 19:28                                                       ` John Stultz
2013-06-20 23:10                                                         ` Alexander Holler
2013-06-14 16:52                                     ` [PATCH 5/9 v3] RFC: timekeeping: rtc: Introduce new kernel parameter hctosys Alexander Holler
2013-06-14 19:24                                       ` John Stultz
2013-06-14 16:52                                     ` [PATCH 6/9 v3] RFC: timekeeping: rtc: remove CONFIG_RTC_HCTOSYS and RTC_HCTOSYS_DEVICE Alexander Holler
2013-06-14 19:11                                       ` John Stultz
2013-06-22  8:00                                         ` Alexander Holler
2013-06-14 16:52                                     ` [PATCH 7/9] RFC: rtc: implement rtc_read_timeval() Alexander Holler
2013-06-14 17:23                                       ` John Stultz
2013-06-14 17:43                                         ` Alexander Holler
2013-06-14 19:18                                           ` John Stultz
2013-06-14 17:28                                       ` John Stultz
2013-06-14 16:52                                     ` [PATCH 8/9] RFC: rtc: hctosys: support rtc_read_timeval() for high precision clocks Alexander Holler
2013-06-14 19:20                                       ` John Stultz
2013-06-14 16:52                                     ` [PATCH 9/9] RFC: rtc: rtc-hid-sensor-time: add support for rtc_read_timeval() Alexander Holler
2013-06-14 17:27                                     ` [PATCH 0/9 v3] RFC: timekeeping: rtc: change hctosys mechanism John Stultz
2013-06-06 10:51                               ` [PATCH 3/3 v2] RFC: timekeeping: rtc: remove CONFIG_RTC_HCTOSYS and RTC_HCTOSYS_DEVICE Alexander Holler
2013-06-04  9:38                 ` [PATCH] rtc: rtc-hid-sensor-time: fix possible bug on driver_remove Alexander Holler
2013-06-08  8:56                   ` Alexander Holler
2013-05-05 11:21               ` [PATCH 4/4] rtc: rtc-hid-sensor-time: add support for milliseconds Alexander Holler
2013-04-20 23:46 ` [PATCH 0/3] rtc: rtc-hid-sensor-time Jiri Kosina
2013-04-21  6:38   ` Alexander Holler

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=1366384452-14367-4-git-send-email-holler@ahsoftware.de \
    --to=holler@ahsoftware.de \
    --cc=a.zummo@towertech.it \
    --cc=jic23@cam.ac.uk \
    --cc=jkosina@suse.cz \
    --cc=lars@metafoo.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rtc-linux@googlegroups.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.