diff options
author | Alexandre Belloni <alexandre.belloni@bootlin.com> | 2018-02-17 08:58:40 -0500 |
---|---|---|
committer | Alexandre Belloni <alexandre.belloni@bootlin.com> | 2018-03-17 09:20:54 -0400 |
commit | 71db049e7355f31604e2c04b6cabb71d02bd487d (patch) | |
tree | 19743f8478631b847a11cf09690a6b4f28927e76 | |
parent | 236b7187034e87bd46eb535ab4f276267ef66ee4 (diff) |
rtc: Add RTC range
Add a way for drivers to inform the core of the supported date/time range.
The core can then check whether the date/time or alarm is in the range
before calling ->set_time, ->set_mmss or ->set_alarm. It returns -ERANGE
when the time is out of range.
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
-rw-r--r-- | Documentation/ABI/testing/sysfs-class-rtc | 8 | ||||
-rw-r--r-- | drivers/rtc/interface.c | 14 | ||||
-rw-r--r-- | drivers/rtc/rtc-sysfs.c | 12 | ||||
-rw-r--r-- | include/linux/rtc.h | 3 |
4 files changed, 37 insertions, 0 deletions
diff --git a/Documentation/ABI/testing/sysfs-class-rtc b/Documentation/ABI/testing/sysfs-class-rtc index 792a38300336..95984289a4ee 100644 --- a/Documentation/ABI/testing/sysfs-class-rtc +++ b/Documentation/ABI/testing/sysfs-class-rtc | |||
@@ -43,6 +43,14 @@ Contact: linux-rtc@vger.kernel.org | |||
43 | Description: | 43 | Description: |
44 | (RO) The name of the RTC corresponding to this sysfs directory | 44 | (RO) The name of the RTC corresponding to this sysfs directory |
45 | 45 | ||
46 | What: /sys/class/rtc/rtcX/range | ||
47 | Date: January 2018 | ||
48 | KernelVersion: 4.16 | ||
49 | Contact: linux-rtc@vger.kernel.org | ||
50 | Description: | ||
51 | Valid time range for the RTC, as seconds from epoch, formatted | ||
52 | as [min, max] | ||
53 | |||
46 | What: /sys/class/rtc/rtcX/since_epoch | 54 | What: /sys/class/rtc/rtcX/since_epoch |
47 | Date: March 2006 | 55 | Date: March 2006 |
48 | KernelVersion: 2.6.17 | 56 | KernelVersion: 2.6.17 |
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index 7e253be19ba7..c068daebeec2 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c | |||
@@ -70,6 +70,13 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) | |||
70 | if (err != 0) | 70 | if (err != 0) |
71 | return err; | 71 | return err; |
72 | 72 | ||
73 | if (rtc->range_min != rtc->range_max) { | ||
74 | time64_t time = rtc_tm_to_time64(tm); | ||
75 | |||
76 | if (time < rtc->range_min || time > rtc->range_max) | ||
77 | return -ERANGE; | ||
78 | } | ||
79 | |||
73 | err = mutex_lock_interruptible(&rtc->ops_lock); | 80 | err = mutex_lock_interruptible(&rtc->ops_lock); |
74 | if (err) | 81 | if (err) |
75 | return err; | 82 | return err; |
@@ -374,6 +381,13 @@ int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) | |||
374 | if (err != 0) | 381 | if (err != 0) |
375 | return err; | 382 | return err; |
376 | 383 | ||
384 | if (rtc->range_min != rtc->range_max) { | ||
385 | time64_t time = rtc_tm_to_time64(&alarm->time); | ||
386 | |||
387 | if (time < rtc->range_min || time > rtc->range_max) | ||
388 | return -ERANGE; | ||
389 | } | ||
390 | |||
377 | err = mutex_lock_interruptible(&rtc->ops_lock); | 391 | err = mutex_lock_interruptible(&rtc->ops_lock); |
378 | if (err) | 392 | if (err) |
379 | return err; | 393 | return err; |
diff --git a/drivers/rtc/rtc-sysfs.c b/drivers/rtc/rtc-sysfs.c index 92ff2edb86a6..454da38c6012 100644 --- a/drivers/rtc/rtc-sysfs.c +++ b/drivers/rtc/rtc-sysfs.c | |||
@@ -248,6 +248,14 @@ offset_store(struct device *dev, struct device_attribute *attr, | |||
248 | } | 248 | } |
249 | static DEVICE_ATTR_RW(offset); | 249 | static DEVICE_ATTR_RW(offset); |
250 | 250 | ||
251 | static ssize_t | ||
252 | range_show(struct device *dev, struct device_attribute *attr, char *buf) | ||
253 | { | ||
254 | return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min, | ||
255 | to_rtc_device(dev)->range_max); | ||
256 | } | ||
257 | static DEVICE_ATTR_RO(range); | ||
258 | |||
251 | static struct attribute *rtc_attrs[] = { | 259 | static struct attribute *rtc_attrs[] = { |
252 | &dev_attr_name.attr, | 260 | &dev_attr_name.attr, |
253 | &dev_attr_date.attr, | 261 | &dev_attr_date.attr, |
@@ -257,6 +265,7 @@ static struct attribute *rtc_attrs[] = { | |||
257 | &dev_attr_hctosys.attr, | 265 | &dev_attr_hctosys.attr, |
258 | &dev_attr_wakealarm.attr, | 266 | &dev_attr_wakealarm.attr, |
259 | &dev_attr_offset.attr, | 267 | &dev_attr_offset.attr, |
268 | &dev_attr_range.attr, | ||
260 | NULL, | 269 | NULL, |
261 | }; | 270 | }; |
262 | 271 | ||
@@ -286,6 +295,9 @@ static umode_t rtc_attr_is_visible(struct kobject *kobj, | |||
286 | } else if (attr == &dev_attr_offset.attr) { | 295 | } else if (attr == &dev_attr_offset.attr) { |
287 | if (!rtc->ops->set_offset) | 296 | if (!rtc->ops->set_offset) |
288 | mode = 0; | 297 | mode = 0; |
298 | } else if (attr == &dev_attr_range.attr) { | ||
299 | if (!(rtc->range_max - rtc->range_min)) | ||
300 | mode = 0; | ||
289 | } | 301 | } |
290 | 302 | ||
291 | return mode; | 303 | return mode; |
diff --git a/include/linux/rtc.h b/include/linux/rtc.h index 3b65b201169c..c78528c394e5 100644 --- a/include/linux/rtc.h +++ b/include/linux/rtc.h | |||
@@ -150,6 +150,9 @@ struct rtc_device { | |||
150 | bool nvram_old_abi; | 150 | bool nvram_old_abi; |
151 | struct bin_attribute *nvram; | 151 | struct bin_attribute *nvram; |
152 | 152 | ||
153 | time64_t range_min; | ||
154 | timeu64_t range_max; | ||
155 | |||
153 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL | 156 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
154 | struct work_struct uie_task; | 157 | struct work_struct uie_task; |
155 | struct timer_list uie_timer; | 158 | struct timer_list uie_timer; |