From patchwork Fri Apr 19 15:14:12 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Holler X-Patchwork-Id: 238032 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-we0-x23e.google.com (mail-we0-x23e.google.com [IPv6:2a00:1450:400c:c03::23e]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 8055D2C01CA for ; Sat, 20 Apr 2013 01:15:40 +1000 (EST) Received: by mail-we0-f190.google.com with SMTP id p43sf116692wea.17 for ; Fri, 19 Apr 2013 08:15:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=x-received:mime-version:x-beenthere:x-received:received-spf :x-spam-checker-version:x-spam-level:x-spam-status:from:to:cc :subject:date:message-id:x-mailer:in-reply-to:references :x-original-sender:x-original-authentication-results:reply-to :precedence:mailing-list:list-id:x-google-group-id:list-post :list-help:list-archive:sender:list-subscribe:list-unsubscribe :content-type; bh=ohqy5i5ArF/abwmgXAnj3MIwWjmB5WQPg6E7SpXyk54=; b=i6l63dQFUICnNtEC/zJuX1IVwZKzi95GhVhEkdy1iId/jlwKfV8MzIhUNWzGwfoNYB x2QZ5fDLmWJcjswf/vjviNtAfMqPDHWF0gRUtZSA5ffzDx6y4R2TGWCSFfXuVGt/edXd BGFf/EEOnzcL+CweNxIQ0BEdJWuSDHtWl9Csiungv3/qxgmzZ362x51NOLtSvWDXYXQN HQxeb1WtfaZtGuvhJETSuMGR8+P9Y0MLzm4QVKkNxF6kQMzBFisLrt7UdopBmb6tYAIt TWWQ9qBTKLYpxOHNj2zhKjKYL+P0FoUvJMHXszKjRxY7RkM5cmO8pEVd/VTdjTeprV79 EC0Q== X-Received: by 10.180.24.72 with SMTP id s8mr2732843wif.15.1366384537073; Fri, 19 Apr 2013 08:15:37 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: rtc-linux@googlegroups.com Received: by 10.180.78.169 with SMTP id c9ls246234wix.13.gmail; Fri, 19 Apr 2013 08:15:36 -0700 (PDT) X-Received: by 10.15.95.2 with SMTP id bc2mr7567143eeb.4.1366384536729; Fri, 19 Apr 2013 08:15:36 -0700 (PDT) Received: from mail.ahsoftware.de (h1446028.stratoserver.net. [85.214.92.142]) by gmr-mx.google.com with ESMTP id d43si3986110eeu.1.2013.04.19.08.15.36; Fri, 19 Apr 2013 08:15:36 -0700 (PDT) Received-SPF: neutral (google.com: 85.214.92.142 is neither permitted nor denied by best guess record for domain of holler@ahsoftware.de) client-ip=85.214.92.142; Received: by mail.ahsoftware.de (Postfix, from userid 65534) id 75EDC888C43; Fri, 19 Apr 2013 17:15:36 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.ahsoftware.de X-Spam-Level: X-Spam-Status: No, score=-101.0 required=5.0 tests=ALL_TRUSTED, USER_IN_WHITELIST autolearn=disabled version=3.3.1 Received: from eiche.ahsoftware (p57B20182.dip0.t-ipconnect.de [87.178.1.130]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.ahsoftware.de (Postfix) with ESMTPSA id 88CE8888BAD for ; Fri, 19 Apr 2013 17:15:35 +0200 (CEST) Received: by eiche.ahsoftware (Postfix, from userid 65534) id CC9FF7FA1F; Fri, 19 Apr 2013 17:15:34 +0200 (CEST) Received: from krabat.ahsoftware (unknown [192.168.207.2]) by eiche.ahsoftware (Postfix) with ESMTP id 1A3E0802F3; Fri, 19 Apr 2013 15:14:36 +0000 (UTC) From: Alexander Holler To: linux-kernel@vger.kernel.org Cc: rtc-linux@googlegroups.com, Alessandro Zummo , Lars-Peter Clausen , Jonathan Cameron , Jiri Kosina , Alexander Holler Subject: [rtc-linux] [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 Message-Id: <1366384452-14367-4-git-send-email-holler@ahsoftware.de> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1366384452-14367-1-git-send-email-holler@ahsoftware.de> References: <1366384452-14367-1-git-send-email-holler@ahsoftware.de> X-Original-Sender: holler@ahsoftware.de X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 85.214.92.142 is neither permitted nor denied by best guess record for domain of holler@ahsoftware.de) smtp.mail=holler@ahsoftware.de Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: X-Google-Group-Id: 712029733259 List-Post: , List-Help: , List-Archive: Sender: rtc-linux@googlegroups.com List-Subscribe: , List-Unsubscribe: , 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 --- 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; }