From patchwork Mon Feb 26 08:33:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 129611 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp3365364lja; Mon, 26 Feb 2018 00:35:29 -0800 (PST) X-Google-Smtp-Source: AG47ELu8OW3WhissARJCd1QYaJehXA9fZHbJ66YJstMk4vwMEoK1vaXdYbMu6lQZVPXlNrmGhi+h X-Received: by 2002:a17:902:4001:: with SMTP id b1-v6mr1553911pld.28.1519634129692; Mon, 26 Feb 2018 00:35:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519634129; cv=none; d=google.com; s=arc-20160816; b=ZPfQNnPl/lPlX0kDF0rjrztlqN5zyFABtizWa1k979IlqgXB9bNFVHE7Q06MNqSFQw IWfnb3O26aYzEJFbHaEFZ+r9xNHffpTvOPtrEl33hY8N96rj4W0oNPPYk5wouFatHW7U +CvvJZ1+5xoX92QYd0EAFSqCrhqVxpohFpAYipJ6kOjQOBOkiSnNQDcaWifxtrVUTssf mcLNXTUo4d/oDhpX4NpAula67fxFL0oWapTaFVh0iYmroGMnMOPeWBMea/6uZffLl0gU J5OQHhjwBk6V3Vaa6NFGjE1NWSVIIYqjRxImrn23H2J6mVltV+xi8n3SWyLRYdIrS3ZO 40ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=suHtErjr+ylroZwu3YfJDocdRxAyU8MOsGzy5vbOC3M=; b=s0AnxZF+mWBgzkd0iwMcNArRKg7fV6bZzOW6m4BIzfCaJ2olN+a756GarwiqzhjRFL CJW1oC7lcrtM7nUlz0Gv0LpjhcHwpcEtoLVbdDdktyJFKTiXsDuSkP4qsRsyE42yNckY NWgtyMl3mbQcxQeJn+RBicyxWb6uAK5/qvQPNDVlPj7biIz9mxKIvH1jiyNK82UijvLH HXezxAMKBudCCUcvIfWDhNgpufpdpSo4cEfc0XPpqMl6gz3RGTwHDrrF/GI/9NRyNeQK qwqp6ATlgdI9vWRsgqDk+gKjqsL5M1mge1WhsGTLC+N9vniSnJfdpwQQo+Cs9ZpPckKt 3Kgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EwtmKHo4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b2si296930pgc.281.2018.02.26.00.35.29; Mon, 26 Feb 2018 00:35:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EwtmKHo4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752261AbeBZIfZ (ORCPT + 28 others); Mon, 26 Feb 2018 03:35:25 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:42240 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751570AbeBZIed (ORCPT ); Mon, 26 Feb 2018 03:34:33 -0500 Received: by mail-pf0-f195.google.com with SMTP id a16so404340pfn.9 for ; Mon, 26 Feb 2018 00:34:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=suHtErjr+ylroZwu3YfJDocdRxAyU8MOsGzy5vbOC3M=; b=EwtmKHo4vOJ6fpthUEKSsKwKlFHQx+5CkxapBZJXPa1wIZmUyfZkpY71F5DAJI0HoR xa2igzSPurIPWUuud0znsPwZINWbdYkzwjBluVrs2DJ6K3pD3OrdQa+MIkgB8QlAVGMU PDymEdzR6mUju5IaHHVuaWAjqVJujVqglZztY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=suHtErjr+ylroZwu3YfJDocdRxAyU8MOsGzy5vbOC3M=; b=iLCwQnhihU08yyrcAS+ET2QSFEaa3C9+de1BoDM2WdfhaAzlaNH3mWSu9ZAWth54+v WXbL1Wiqo/TEL3J5c9K1sgBGcI5AXZG0ol7N7J6s3e1lXMEowFF2XioXlY3SHcEPhF9s NOsBJ0pehCFhTWo3M1h2Vt71LaN8iGHD1WNVO6pZwXBDFZU3u+z36MZtwjPHyFKTu/6C btlrexnAE3QkSDhpIZSXzhDEWdQll/6IoCNbLV4dJ0Y/pxFVhTSvPT2FF9V92eLZRkSB Z1JNn0iJmb8wzpXRc/ZGdnIFBMbhONre+zOGpy7SW1dgf7lta5/R6PQ6V6oAPh+JXzJp vTDA== X-Gm-Message-State: APf1xPDAA0YNiPxZQJRkpC213u0NiYPuJagAfoZyEYZ5g+jw/ijnMAFN eP71wWevIUjioEuaYZot1W63cfmZNYQ= X-Received: by 10.99.42.72 with SMTP id q69mr217191pgq.168.1519634073086; Mon, 26 Feb 2018 00:34:33 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id e83sm17734791pfk.148.2018.02.26.00.34.30 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Feb 2018 00:34:32 -0800 (PST) From: Baolin Wang To: a.zummo@towertech.it, alexandre.belloni@bootlin.com Cc: arnd@arndb.de, broonie@kernel.org, linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org, baolin.wang@linaro.org, alexandre.belloni@free-electrons.com Subject: [RESEND PATCH 1/3] rtc: Use time64_t to save range_max of RTC Date: Mon, 26 Feb 2018 16:33:56 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We need use rtc->range_max to valid if the time values are valid, and the time values are saved by time64_t type. So change the rtc->range_max to time64_t type for comparison correctly. Signed-off-by: Baolin Wang --- include/linux/rtc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- 1.7.9.5 diff --git a/include/linux/rtc.h b/include/linux/rtc.h index bdfc0c4..8560282 100644 --- a/include/linux/rtc.h +++ b/include/linux/rtc.h @@ -152,7 +152,7 @@ struct rtc_device { struct bin_attribute *nvram; time64_t range_min; - timeu64_t range_max; + time64_t range_max; #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL struct work_struct uie_task; From patchwork Mon Feb 26 08:33:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 129610 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp3364952lja; Mon, 26 Feb 2018 00:34:57 -0800 (PST) X-Google-Smtp-Source: AH8x227/FNZG7KUFB+GCydiRxtUJa6e3SHYIcyd5ML3xCWqiMlXZ8nkFD0VL9NumDc8rgHPUOE6o X-Received: by 2002:a17:902:50e:: with SMTP id 14-v6mr9838427plf.360.1519634097348; Mon, 26 Feb 2018 00:34:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519634097; cv=none; d=google.com; s=arc-20160816; b=YJNzIBmwGOz9CcPqSzqDNXbup8l3wgshlulg/tTU1mYqjDruDUuIz/pDLnM4ejrHT+ P8WeFG0JZhRVGKdyEuIChqUc8vKI336LVF2yPmlyRx6/JiGb0xWkqpLiFKTeDJqexFF8 PW234YvKJy+CuyVMMCyo+s/bo3TN+cAXf9RrSMz4RtBfe3YfVTz4j2mJMM2JAwHWK4Ad ZPctnHHXl8Tl6NEsRTXIuCwk1eSc/lIAV7ckPgw74F4E91ajUPg9Tn+yCCxGhIJeUG5X 5HRFbGBvkzZWpg1j36fRt9a1s2yLncLNfs6w/s/e1u3MrslrzklQLbq8pdXmDkIf9I4f 27xA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=IzVnQsiaJpsvgArC4tqNOrZgNLdSwmSWbuPGX6C6DK8=; b=mq0a6iAFMZKU4nLKS789BoYoZ6PCnlpJBZyZ+hroEeUQX2JivHg7UodicQdH850BlL OohuA9R7VZ+AsOqmMWoARorE5/rtH3+ipeSMkmWjd/uAt096G5EXmEejd3xq06f1pji1 UpOMYRwTol6Satx5ajj5+fjCzJcpYXIumJR1LRWhPx1d7/uKWReSLrZU4XerhwSzQf0K MVW71v5nPcEzhONXbsXJnmxkw2GyYhn3JyVbMxq0nfXgaCso6mLmgM37NQsx/s4rfRg7 BIEgX2s4dA6XN4wpsRnVDhE7GHK1GPD9F9qm+cVkj2LnLbfM7uI0h47CuM9SxMeqbaey 1Ybg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PXU2r1K3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c14si983370pgu.341.2018.02.26.00.34.57; Mon, 26 Feb 2018 00:34:57 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PXU2r1K3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752662AbeBZIet (ORCPT + 28 others); Mon, 26 Feb 2018 03:34:49 -0500 Received: from mail-pf0-f193.google.com ([209.85.192.193]:39182 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752607AbeBZIeg (ORCPT ); Mon, 26 Feb 2018 03:34:36 -0500 Received: by mail-pf0-f193.google.com with SMTP id c143so6232220pfb.6 for ; Mon, 26 Feb 2018 00:34:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=IzVnQsiaJpsvgArC4tqNOrZgNLdSwmSWbuPGX6C6DK8=; b=PXU2r1K3x38Rryp2XwZQ+M2RkvUE1+irtsoINeT++qsoPuLAy4EaqcvRS31Bp8exBd VWakgwhd4PXvBH5CQSS0Lp95EaDdUs9RjCoeo3hQrZ73snAY5Iq+UmGJasLZ6nt4BNE9 2CHJXokhO9aAHYY1oVrrNeltalnwKx2PVfzts= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=IzVnQsiaJpsvgArC4tqNOrZgNLdSwmSWbuPGX6C6DK8=; b=fPjcycirddcuuW2pDkWF9DSDldY7XxmmlgT4f2UP018eBGIMOIfTQFnIYfp0AO5jL+ c0DBvKLus3A8k7ZAe5V4P3Sjbf3j2XURWmWzHyyeuWJrrSgby+sZk+WSu0zcU7AWzMgz gUgJ+G9LVT8fBTHvsx2g8PZ1D+vXJLj5W8uzmeXDN2WQxIqJqt7eCPikD1Dog4Fb+Xmy ZPfcdstHVOcKe0opYUvySKg20KVwv2B9/ESrVFMxdXYEI1al1WFKHaqaXJu7WTvAYKwy Z05EcrCUzJnL2byT8Trt59kboCeeH/wwUE8XsJgAeiUlQO497ehi32unAtIOD+pBzElI wsng== X-Gm-Message-State: APf1xPABr0YUE4QpnRnAA9YvMM51UMc3hdwDqQG3MFMH+kA+Rnpp5ZxP i8cRdW1EUDdp4AvwGGbbp1kjgg== X-Received: by 10.101.69.134 with SMTP id o6mr8014642pgq.340.1519634075650; Mon, 26 Feb 2018 00:34:35 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id e83sm17734791pfk.148.2018.02.26.00.34.33 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Feb 2018 00:34:35 -0800 (PST) From: Baolin Wang To: a.zummo@towertech.it, alexandre.belloni@bootlin.com Cc: arnd@arndb.de, broonie@kernel.org, linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org, baolin.wang@linaro.org, alexandre.belloni@free-electrons.com Subject: [RESEND PATCH 2/3] rtc: Factor out the RTC range validation into rtc_valid_range() Date: Mon, 26 Feb 2018 16:33:57 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The RTC range validation code can be factored into rtc_valid_range() function to avoid duplicate code. Signed-off-by: Baolin Wang --- drivers/rtc/interface.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) -- 1.7.9.5 diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index c068dae..25aebc5 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c @@ -23,6 +23,18 @@ static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer); static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer); +static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm) +{ + if (rtc->range_min != rtc->range_max) { + time64_t time = rtc_tm_to_time64(tm); + + if (time < rtc->range_min || time > rtc->range_max) + return -ERANGE; + } + + return 0; +} + static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) { int err; @@ -70,12 +82,9 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) if (err != 0) return err; - if (rtc->range_min != rtc->range_max) { - time64_t time = rtc_tm_to_time64(tm); - - if (time < rtc->range_min || time > rtc->range_max) - return -ERANGE; - } + err = rtc_valid_range(rtc, tm); + if (err) + return err; err = mutex_lock_interruptible(&rtc->ops_lock); if (err) @@ -381,12 +390,9 @@ int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) if (err != 0) return err; - if (rtc->range_min != rtc->range_max) { - time64_t time = rtc_tm_to_time64(&alarm->time); - - if (time < rtc->range_min || time > rtc->range_max) - return -ERANGE; - } + err = rtc_valid_range(rtc, &alarm->time); + if (err) + return err; err = mutex_lock_interruptible(&rtc->ops_lock); if (err) From patchwork Mon Feb 26 08:33:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 129609 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp3364865lja; Mon, 26 Feb 2018 00:34:48 -0800 (PST) X-Google-Smtp-Source: AH8x224bb8yo8Xy8f+HMmJHwJvgKVbXf4FhRNPhVwpORDDbdGxn2ZXoBdl0u8OC2cYzln/34QzX0 X-Received: by 2002:a17:902:fa2:: with SMTP id 31-v6mr10099404plz.346.1519634088461; Mon, 26 Feb 2018 00:34:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519634088; cv=none; d=google.com; s=arc-20160816; b=QYt3IPrx7Hz0bF9L+3H6SPGhFofiEn5GD2Q72tjj3z2ndOvP3fiHm1hn412G1pZJat G9ULctdnj9lMUi3Ic/k3tPM35Hz+uyYR1rUT8NV7R9m8AlxsopnUjEFyJ0eHag4axLbQ JP6skVE/Uafn8HTraEI7lJL/3itaJnNOmb0AEZh4BsuSEcsbRfM6arj/Tqw0NeYcNNjs dqbjhp0k4gl91axcNWlWabDokeFikN9+fnRZ94ykzlOTKjZPrTf7rrF8rNmWDrJoLc8H LRM4ytX4Id3f/319IBgpF298VywLu+rWRJSFIPVDwf6sPDoFBT7nD4BAPp65XdPBlpxD hwhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=n4IjGvTYiAFzl5mzdjpCmPflLxOVDvnuwBYFmB6lcsY=; b=qvBic6nI1XkCl6I7VLFrFHlZLAA3xaVMm6WOs61vAIec4essHBxEPWsgSWPZo1g+sj JIKl/jHujkcAwNE4tBqnWHG4qVBkgMYz/KHsXHMCpsBGMzF5eQgFrrEXAnXuZE+SBY7a IaqgCiuVC+x36S3b2XYYm7QgG1R5/Pcb4kzohcdF7fo0WwqK91/T8Jzg9EnqIARQj/SV U3oXcV0hdNsL0tBOk4f39Mv8Y1rW9f9Fe1c91HYB85QaE+XiSfEX/8cTCjgLeweFaPrM HXlFhhaep2b11iBrarrPtYUvxxiwQ55aw9x6Vhz3Phev1XeZ2PjkOKOeU71jcD3iNDJU orew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Z6pV4qo1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c14si983370pgu.341.2018.02.26.00.34.48; Mon, 26 Feb 2018 00:34:48 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Z6pV4qo1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752660AbeBZIeo (ORCPT + 28 others); Mon, 26 Feb 2018 03:34:44 -0500 Received: from mail-pg0-f67.google.com ([74.125.83.67]:42467 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751999AbeBZIej (ORCPT ); Mon, 26 Feb 2018 03:34:39 -0500 Received: by mail-pg0-f67.google.com with SMTP id y8so5914226pgr.9 for ; Mon, 26 Feb 2018 00:34:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=n4IjGvTYiAFzl5mzdjpCmPflLxOVDvnuwBYFmB6lcsY=; b=Z6pV4qo1a0hPSBbKjLc0FzGkOjbjexnUIbO4hTx5BqVegxxk54VZuVe3XWAJy/DC/K bjNsFqLT3xmZ4XU5y68jTs/0/AjO+hnrWzTUuCTGcpfrwVvmCdLGu2rjQIQN2YlwFRp3 t9g10i1dZbPik1TPY2v1Bfc8ZuXGT6DwCImsM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=n4IjGvTYiAFzl5mzdjpCmPflLxOVDvnuwBYFmB6lcsY=; b=iK+QduU9umXzUj4Y7NEz7NBv0oYWvx+Vb9Rt4S7SnWi45aqqbFyUe5opDn8J5VuDdy IfPEP1KbO2VDoMOV0Nm7hE7zT2nulOaGx/a2i8RNYp4vGJAliXgaz4dBqMjr+UlPHB/x 9AT50n3JIQPpxfLOJ5mgIvSCFsfmnUJXWgbGHJdPoQIm4/HsXzq9tXtj9m5twJ2SkXly o2XUKMvSozvG+hIuBPcYzrR7y1bL1DcfNGK+HPzpIMvaBUKfVqXrUt81LBz4Bs2Dqidi 0utj5XhiGfNgeJpjf9GKhajG3j2jNg/rm+D0gA2QCyYYcqTcMmBw2PloLI5cgiYltDXG LgWw== X-Gm-Message-State: APf1xPAxwwV5o4NG4WuYuvbQB3+zGIpf3vLHn9cJ0GsG2Bu9j7yZzvRi krBFxaq5nXziELIJV/I685wPmg== X-Received: by 10.99.122.12 with SMTP id v12mr7915120pgc.128.1519634078355; Mon, 26 Feb 2018 00:34:38 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id e83sm17734791pfk.148.2018.02.26.00.34.35 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Feb 2018 00:34:37 -0800 (PST) From: Baolin Wang To: a.zummo@towertech.it, alexandre.belloni@bootlin.com Cc: arnd@arndb.de, broonie@kernel.org, linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org, baolin.wang@linaro.org, alexandre.belloni@free-electrons.com Subject: [RESEND PATCH 3/3] rtc: Add one offset seconds to expand RTC range Date: Mon, 26 Feb 2018 16:33:58 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org >From our investigation for all RTC drivers, 1 driver will be expired before year 2017, 7 drivers will be expired before year 2038, 23 drivers will be expired before year 2069, 72 drivers will be expired before 2100 and 104 drivers will be expired before 2106. Especially for these early expired drivers, we need to expand the RTC range to make the RTC can still work after the expired year. So we can expand the RTC range by adding one offset to the time when reading from hardware, and subtracting it when writing back. For example, if you have an RTC that can do 100 years, and currently is configured to be based in Jan 1 1970, so it can represents times from 1970 to 2069. Then if you change the start year from 1970 to 2000, which means it can represents times from 2000 to 2099. By adding or subtracting the offset produced by moving the wrap point, all times between 1970 and 1999 from RTC hardware could get interpreted as times from 2070 to 2099, but the interpretation of dates between 2000 and 2069 would not change. Signed-off-by: Baolin Wang --- drivers/rtc/class.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++ drivers/rtc/interface.c | 58 ++++++++++++++++++++++++++++++++++++++- include/linux/rtc.h | 3 ++ 3 files changed, 130 insertions(+), 1 deletion(-) -- 1.7.9.5 diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c index 722d683..086a42b 100644 --- a/drivers/rtc/class.c +++ b/drivers/rtc/class.c @@ -211,6 +211,73 @@ static int rtc_device_get_id(struct device *dev) return id; } +static void rtc_device_get_offset(struct rtc_device *rtc) +{ + time64_t range_secs; + u32 start_year; + int ret; + + /* + * If RTC driver did not implement the range of RTC hardware device, + * then we can not expand the RTC range by adding or subtracting one + * offset. + */ + if (rtc->range_min == rtc->range_max) + return; + + ret = device_property_read_u32(rtc->dev.parent, "start-year", + &start_year); + if (!ret) { + rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0); + rtc->set_start_time = true; + } + + /* + * If user did not implement the start time for RTC driver, then no + * need to expand the RTC range. + */ + if (!rtc->set_start_time) + return; + + range_secs = rtc->range_max - rtc->range_min + 1; + + /* + * If the start_secs is larger than the maximum seconds (rtc->range_max) + * supported by RTC hardware or the maximum seconds of new expanded + * range (start_secs + rtc->range_max - rtc->range_min) is less than + * rtc->range_min, which means the minimum seconds (rtc->range_min) of + * RTC hardware will be mapped to start_secs by adding one offset, so + * the offset seconds calculation formula should be: + * rtc->offset_secs = rtc->start_secs - rtc->range_min; + * + * If the start_secs is larger than the minimum seconds (rtc->range_min) + * supported by RTC hardware, then there is one region is overlapped + * between the original RTC hardware range and the new expanded range, + * and this overlapped region do not need to be mapped into the new + * expanded range due to it is valid for RTC device. So the minimum + * seconds of RTC hardware (rtc->range_min) should be mapped to + * rtc->range_max + 1, then the offset seconds formula should be: + * rtc->offset_secs = rtc->range_max - rtc->range_min + 1; + * + * If the start_secs is less than the minimum seconds (rtc->range_min), + * which is similar to case 2. So the start_secs should be mapped to + * start_secs + rtc->range_max - rtc->range_min + 1, then the + * offset seconds formula should be: + * rtc->offset_secs = -(rtc->range_max - rtc->range_min + 1); + * + * Otherwise the offset seconds should be 0. + */ + if (rtc->start_secs > rtc->range_max || + rtc->start_secs + range_secs - 1 < rtc->range_min) + rtc->offset_secs = rtc->start_secs - rtc->range_min; + else if (rtc->start_secs > rtc->range_min) + rtc->offset_secs = range_secs; + else if (rtc->start_secs < rtc->range_min) + rtc->offset_secs = -range_secs; + else + rtc->offset_secs = 0; +} + /** * rtc_device_register - register w/ RTC class * @dev: the device to register @@ -247,6 +314,8 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev, dev_set_name(&rtc->dev, "rtc%d", id); + rtc_device_get_offset(rtc); + /* Check to see if there is an ALARM already set in hw */ err = __rtc_read_alarm(rtc, &alrm); @@ -435,6 +504,7 @@ int __rtc_register_device(struct module *owner, struct rtc_device *rtc) return -EINVAL; rtc->owner = owner; + rtc_device_get_offset(rtc); /* Check to see if there is an ALARM already set in hw */ err = __rtc_read_alarm(rtc, &alrm); diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index 25aebc5..6a0340b 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c @@ -23,12 +23,61 @@ static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer); static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer); +static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm) +{ + time64_t secs; + + if (!rtc->offset_secs) + return; + + secs = rtc_tm_to_time64(tm); + + /* + * Since the reading time values from RTC device are always in the RTC + * original valid range, but we need to skip the overlapped region + * between expanded range and original range, which is no need to add + * the offset. + */ + if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) || + (rtc->start_secs < rtc->range_min && + secs <= (rtc->start_secs + rtc->range_max - rtc->range_min))) + return; + + rtc_time64_to_tm(secs + rtc->offset_secs, tm); +} + +static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm) +{ + time64_t secs; + + if (!rtc->offset_secs) + return; + + secs = rtc_tm_to_time64(tm); + + /* + * If the setting time values are in the valid range of RTC hardware + * device, then no need to subtract the offset when setting time to RTC + * device. Otherwise we need to subtract the offset to make the time + * values are valid for RTC hardware device. + */ + if (secs >= rtc->range_min && secs <= rtc->range_max) + return; + + rtc_time64_to_tm(secs - rtc->offset_secs, tm); +} + static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm) { if (rtc->range_min != rtc->range_max) { time64_t time = rtc_tm_to_time64(tm); + time64_t range_min = rtc->set_start_time ? rtc->start_secs : + rtc->range_min; + time64_t range_max = rtc->set_start_time ? + (rtc->start_secs + rtc->range_max - rtc->range_min) : + rtc->range_max; - if (time < rtc->range_min || time > rtc->range_max) + if (time < range_min || time > range_max) return -ERANGE; } @@ -51,6 +100,8 @@ static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) return err; } + rtc_add_offset(rtc, tm); + err = rtc_valid_tm(tm); if (err < 0) dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n"); @@ -86,6 +137,8 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) if (err) return err; + rtc_subtract_offset(rtc, tm); + err = mutex_lock_interruptible(&rtc->ops_lock); if (err) return err; @@ -143,6 +196,7 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *al mutex_unlock(&rtc->ops_lock); + rtc_add_offset(rtc, &alarm->time); trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err); return err; } @@ -355,6 +409,8 @@ static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) err = rtc_valid_tm(&alarm->time); if (err) return err; + + rtc_subtract_offset(rtc, &alarm->time); scheduled = rtc_tm_to_time64(&alarm->time); /* Make sure we're not setting alarms in the past */ diff --git a/include/linux/rtc.h b/include/linux/rtc.h index 8560282..f63add6 100644 --- a/include/linux/rtc.h +++ b/include/linux/rtc.h @@ -153,6 +153,9 @@ struct rtc_device { time64_t range_min; time64_t range_max; + time64_t start_secs; + time64_t offset_secs; + bool set_start_time; #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL struct work_struct uie_task;