blob: 950fac0d41ff9aef0050b5f75679ef46b05f665f [file] [log] [blame]
Alexandre Bellonicdf75452019-03-13 23:02:48 +01001// SPDX-License-Identifier: GPL-2.0
Alessandro Zummoc5c3e192006-03-27 01:16:39 -08002/*
3 * RTC subsystem, sysfs interface
4 *
5 * Copyright (C) 2005 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
Alexandre Bellonicdf75452019-03-13 23:02:48 +01007 */
Alessandro Zummoc5c3e192006-03-27 01:16:39 -08008
9#include <linux/module.h>
10#include <linux/rtc.h>
11
David Brownellab6a2d72007-05-08 00:33:30 -070012#include "rtc-core.h"
13
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080014/* device attributes */
15
David Brownell8a0bdfd2008-02-06 01:38:45 -080016/*
17 * NOTE: RTC times displayed in sysfs use the RTC's timezone. That's
18 * ideally UTC. However, PCs that also boot to MS-Windows normally use
19 * the local time and change to match daylight savings time. That affects
20 * attributes including date, time, since_epoch, and wakealarm.
21 */
22
David Brownellcd966202007-05-08 00:33:40 -070023static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070024name_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080025{
Alexandre Belloni77a73f32017-06-02 13:57:03 +020026 return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
27 dev_name(dev->parent));
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080028}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070029static DEVICE_ATTR_RO(name);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080030
David Brownellcd966202007-05-08 00:33:40 -070031static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070032date_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080033{
34 ssize_t retval;
35 struct rtc_time tm;
36
David Brownellab6a2d72007-05-08 00:33:30 -070037 retval = rtc_read_time(to_rtc_device(dev), &tm);
Andy Shevchenko5548cbf2018-12-04 23:23:12 +020038 if (retval)
39 return retval;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080040
Andy Shevchenko5548cbf2018-12-04 23:23:12 +020041 return sprintf(buf, "%ptRd\n", &tm);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080042}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070043static DEVICE_ATTR_RO(date);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080044
David Brownellcd966202007-05-08 00:33:40 -070045static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070046time_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080047{
48 ssize_t retval;
49 struct rtc_time tm;
50
David Brownellab6a2d72007-05-08 00:33:30 -070051 retval = rtc_read_time(to_rtc_device(dev), &tm);
Andy Shevchenko5548cbf2018-12-04 23:23:12 +020052 if (retval)
53 return retval;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080054
Andy Shevchenko5548cbf2018-12-04 23:23:12 +020055 return sprintf(buf, "%ptRt\n", &tm);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080056}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070057static DEVICE_ATTR_RO(time);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080058
David Brownellcd966202007-05-08 00:33:40 -070059static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070060since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080061{
62 ssize_t retval;
63 struct rtc_time tm;
64
David Brownellab6a2d72007-05-08 00:33:30 -070065 retval = rtc_read_time(to_rtc_device(dev), &tm);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080066 if (retval == 0) {
Baolin Wang9a06da22017-11-09 15:09:20 +080067 time64_t time;
68
69 time = rtc_tm_to_time64(&tm);
70 retval = sprintf(buf, "%lld\n", time);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080071 }
72
73 return retval;
74}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070075static DEVICE_ATTR_RO(since_epoch);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080076
Bryan Kadzban06c65eb2007-10-16 01:28:22 -070077static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070078max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
Bryan Kadzban06c65eb2007-10-16 01:28:22 -070079{
80 return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
81}
82
83static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070084max_user_freq_store(struct device *dev, struct device_attribute *attr,
Alexandre Belloni606cc432019-03-20 12:59:09 +010085 const char *buf, size_t n)
Bryan Kadzban06c65eb2007-10-16 01:28:22 -070086{
87 struct rtc_device *rtc = to_rtc_device(dev);
LABBE Corentinf5712872015-12-17 14:11:04 +010088 unsigned long val;
89 int err;
90
91 err = kstrtoul(buf, 0, &val);
92 if (err)
93 return err;
Bryan Kadzban06c65eb2007-10-16 01:28:22 -070094
95 if (val >= 4096 || val == 0)
96 return -EINVAL;
97
98 rtc->max_user_freq = (int)val;
99
100 return n;
101}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -0700102static DEVICE_ATTR_RW(max_user_freq);
Bryan Kadzban06c65eb2007-10-16 01:28:22 -0700103
David Fries4c24e292012-10-04 17:14:12 -0700104/**
105 * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
Alexandre Belloni6f693192019-11-22 11:22:05 +0100106 * @dev: The device that the attribute belongs to.
107 * @attr: The attribute being read.
108 * @buf: The result buffer.
David Fries4c24e292012-10-04 17:14:12 -0700109 *
Alexandre Belloni6f693192019-11-22 11:22:05 +0100110 * buf is "1" if the system clock was set by this RTC at the last
David Fries4c24e292012-10-04 17:14:12 -0700111 * boot or resume event.
112 */
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700113static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -0700114hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700115{
116#ifdef CONFIG_RTC_HCTOSYS_DEVICE
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -0800117 if (rtc_hctosys_ret == 0 &&
Alexandre Belloni606cc432019-03-20 12:59:09 +0100118 strcmp(dev_name(&to_rtc_device(dev)->dev),
119 CONFIG_RTC_HCTOSYS_DEVICE) == 0)
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700120 return sprintf(buf, "1\n");
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700121#endif
Alexandre Belloni606cc432019-03-20 12:59:09 +0100122 return sprintf(buf, "0\n");
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700123}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -0700124static DEVICE_ATTR_RO(hctosys);
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700125
David Brownell3925a5c2007-02-12 00:52:47 -0800126static ssize_t
Dmitry Torokhova17ccd12015-07-23 16:01:07 -0700127wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell3925a5c2007-02-12 00:52:47 -0800128{
129 ssize_t retval;
Baolin Wang9a06da22017-11-09 15:09:20 +0800130 time64_t alarm;
David Brownell3925a5c2007-02-12 00:52:47 -0800131 struct rtc_wkalrm alm;
132
David Brownell8a0bdfd2008-02-06 01:38:45 -0800133 /* Don't show disabled alarms. For uniformity, RTC alarms are
134 * conceptually one-shot, even though some common RTCs (on PCs)
135 * don't actually work that way.
David Brownell3925a5c2007-02-12 00:52:47 -0800136 *
David Brownell8a0bdfd2008-02-06 01:38:45 -0800137 * NOTE: RTC implementations where the alarm doesn't match an
138 * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
139 * alarms after they trigger, to ensure one-shot semantics.
David Brownell3925a5c2007-02-12 00:52:47 -0800140 */
David Brownellab6a2d72007-05-08 00:33:30 -0700141 retval = rtc_read_alarm(to_rtc_device(dev), &alm);
David Brownell3925a5c2007-02-12 00:52:47 -0800142 if (retval == 0 && alm.enabled) {
Baolin Wang9a06da22017-11-09 15:09:20 +0800143 alarm = rtc_tm_to_time64(&alm.time);
144 retval = sprintf(buf, "%lld\n", alarm);
David Brownell3925a5c2007-02-12 00:52:47 -0800145 }
146
147 return retval;
148}
149
150static ssize_t
Dmitry Torokhova17ccd12015-07-23 16:01:07 -0700151wakealarm_store(struct device *dev, struct device_attribute *attr,
David Brownellcd966202007-05-08 00:33:40 -0700152 const char *buf, size_t n)
David Brownell3925a5c2007-02-12 00:52:47 -0800153{
154 ssize_t retval;
Baolin Wang9a06da22017-11-09 15:09:20 +0800155 time64_t now, alarm;
156 time64_t push = 0;
David Brownell3925a5c2007-02-12 00:52:47 -0800157 struct rtc_wkalrm alm;
David Brownellab6a2d72007-05-08 00:33:30 -0700158 struct rtc_device *rtc = to_rtc_device(dev);
LABBE Corentin84281c22016-08-12 14:46:14 +0200159 const char *buf_ptr;
Zhao Yakuic116bc22008-04-28 02:11:58 -0700160 int adjust = 0;
David Brownell3925a5c2007-02-12 00:52:47 -0800161
162 /* Only request alarms that trigger in the future. Disable them
163 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
164 */
David Brownellab6a2d72007-05-08 00:33:30 -0700165 retval = rtc_read_time(rtc, &alm.time);
David Brownell3925a5c2007-02-12 00:52:47 -0800166 if (retval < 0)
167 return retval;
Baolin Wang9a06da22017-11-09 15:09:20 +0800168 now = rtc_tm_to_time64(&alm.time);
David Brownell3925a5c2007-02-12 00:52:47 -0800169
LABBE Corentin84281c22016-08-12 14:46:14 +0200170 buf_ptr = buf;
Zhao Yakuic116bc22008-04-28 02:11:58 -0700171 if (*buf_ptr == '+') {
172 buf_ptr++;
Bernie Thompson1df0a472013-07-03 15:07:03 -0700173 if (*buf_ptr == '=') {
174 buf_ptr++;
175 push = 1;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100176 } else {
Bernie Thompson1df0a472013-07-03 15:07:03 -0700177 adjust = 1;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100178 }
Zhao Yakuic116bc22008-04-28 02:11:58 -0700179 }
Baolin Wang9a06da22017-11-09 15:09:20 +0800180 retval = kstrtos64(buf_ptr, 0, &alarm);
LABBE Corentinf5712872015-12-17 14:11:04 +0100181 if (retval)
182 return retval;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100183 if (adjust)
Zhao Yakuic116bc22008-04-28 02:11:58 -0700184 alarm += now;
Bernie Thompson1df0a472013-07-03 15:07:03 -0700185 if (alarm > now || push) {
David Brownell3925a5c2007-02-12 00:52:47 -0800186 /* Avoid accidentally clobbering active alarms; we can't
187 * entirely prevent that here, without even the minimal
188 * locking from the /dev/rtcN api.
189 */
David Brownellab6a2d72007-05-08 00:33:30 -0700190 retval = rtc_read_alarm(rtc, &alm);
David Brownell3925a5c2007-02-12 00:52:47 -0800191 if (retval < 0)
192 return retval;
Bernie Thompson1df0a472013-07-03 15:07:03 -0700193 if (alm.enabled) {
194 if (push) {
Baolin Wang9a06da22017-11-09 15:09:20 +0800195 push = rtc_tm_to_time64(&alm.time);
Bernie Thompson1df0a472013-07-03 15:07:03 -0700196 alarm += push;
197 } else
198 return -EBUSY;
199 } else if (push)
200 return -EINVAL;
David Brownell3925a5c2007-02-12 00:52:47 -0800201 alm.enabled = 1;
202 } else {
203 alm.enabled = 0;
204
205 /* Provide a valid future alarm time. Linux isn't EFI,
206 * this time won't be ignored when disabling the alarm.
207 */
208 alarm = now + 300;
209 }
Baolin Wang9a06da22017-11-09 15:09:20 +0800210 rtc_time64_to_tm(alarm, &alm.time);
David Brownell3925a5c2007-02-12 00:52:47 -0800211
David Brownellab6a2d72007-05-08 00:33:30 -0700212 retval = rtc_set_alarm(rtc, &alm);
David Brownell3925a5c2007-02-12 00:52:47 -0800213 return (retval < 0) ? retval : n;
214}
Dmitry Torokhova17ccd12015-07-23 16:01:07 -0700215static DEVICE_ATTR_RW(wakealarm);
David Brownell3925a5c2007-02-12 00:52:47 -0800216
Joshua Clayton5495a412016-02-05 12:41:12 -0800217static ssize_t
218offset_show(struct device *dev, struct device_attribute *attr, char *buf)
219{
220 ssize_t retval;
221 long offset;
222
223 retval = rtc_read_offset(to_rtc_device(dev), &offset);
224 if (retval == 0)
225 retval = sprintf(buf, "%ld\n", offset);
226
227 return retval;
228}
229
230static ssize_t
231offset_store(struct device *dev, struct device_attribute *attr,
232 const char *buf, size_t n)
233{
234 ssize_t retval;
235 long offset;
236
237 retval = kstrtol(buf, 10, &offset);
238 if (retval == 0)
239 retval = rtc_set_offset(to_rtc_device(dev), offset);
240
241 return (retval < 0) ? retval : n;
242}
243static DEVICE_ATTR_RW(offset);
244
Alexandre Belloni71db0492018-02-17 14:58:40 +0100245static ssize_t
246range_show(struct device *dev, struct device_attribute *attr, char *buf)
247{
248 return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min,
249 to_rtc_device(dev)->range_max);
250}
251static DEVICE_ATTR_RO(range);
252
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700253static struct attribute *rtc_attrs[] = {
254 &dev_attr_name.attr,
255 &dev_attr_date.attr,
256 &dev_attr_time.attr,
257 &dev_attr_since_epoch.attr,
258 &dev_attr_max_user_freq.attr,
259 &dev_attr_hctosys.attr,
260 &dev_attr_wakealarm.attr,
Joshua Clayton5495a412016-02-05 12:41:12 -0800261 &dev_attr_offset.attr,
Alexandre Belloni71db0492018-02-17 14:58:40 +0100262 &dev_attr_range.attr,
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700263 NULL,
264};
David Brownell3925a5c2007-02-12 00:52:47 -0800265
266/* The reason to trigger an alarm with no process watching it (via sysfs)
267 * is its side effect: waking from a system state like suspend-to-RAM or
268 * suspend-to-disk. So: no attribute unless that side effect is possible.
269 * (Userspace may disable that mechanism later.)
270 */
Dmitry Torokhovdf100c02015-07-23 16:01:06 -0700271static bool rtc_does_wakealarm(struct rtc_device *rtc)
David Brownell3925a5c2007-02-12 00:52:47 -0800272{
David Brownellcd966202007-05-08 00:33:40 -0700273 if (!device_can_wakeup(rtc->dev.parent))
Dmitry Torokhovdf100c02015-07-23 16:01:06 -0700274 return false;
275
David Brownell3925a5c2007-02-12 00:52:47 -0800276 return rtc->ops->set_alarm != NULL;
277}
278
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700279static umode_t rtc_attr_is_visible(struct kobject *kobj,
280 struct attribute *attr, int n)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800281{
suguosongae243ef2020-02-25 10:19:23 +0800282 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700283 struct rtc_device *rtc = to_rtc_device(dev);
284 umode_t mode = attr->mode;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800285
Joshua Clayton5495a412016-02-05 12:41:12 -0800286 if (attr == &dev_attr_wakealarm.attr) {
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700287 if (!rtc_does_wakealarm(rtc))
288 mode = 0;
Joshua Clayton5495a412016-02-05 12:41:12 -0800289 } else if (attr == &dev_attr_offset.attr) {
290 if (!rtc->ops->set_offset)
291 mode = 0;
Alexandre Belloni71db0492018-02-17 14:58:40 +0100292 } else if (attr == &dev_attr_range.attr) {
293 if (!(rtc->range_max - rtc->range_min))
294 mode = 0;
Joshua Clayton5495a412016-02-05 12:41:12 -0800295 }
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800296
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700297 return mode;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800298}
299
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700300static struct attribute_group rtc_attr_group = {
301 .is_visible = rtc_attr_is_visible,
302 .attrs = rtc_attrs,
303};
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800304
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700305static const struct attribute_group *rtc_attr_groups[] = {
306 &rtc_attr_group,
307 NULL
308};
309
310const struct attribute_group **rtc_get_dev_attribute_groups(void)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800311{
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700312 return rtc_attr_groups;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800313}
Denis Osterlanda0a1a1b2018-07-24 11:31:22 +0000314
315int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
316{
317 size_t old_cnt = 0, add_cnt = 0, new_cnt;
318 const struct attribute_group **groups, **old;
319
320 if (rtc->registered)
321 return -EINVAL;
322 if (!grps)
323 return -EINVAL;
324
325 groups = rtc->dev.groups;
326 if (groups)
327 for (; *groups; groups++)
328 old_cnt++;
329
330 for (groups = grps; *groups; groups++)
331 add_cnt++;
332
333 new_cnt = old_cnt + add_cnt + 1;
334 groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL);
Dan Carpenter777d8ae2018-08-27 12:22:34 +0300335 if (!groups)
336 return -ENOMEM;
Denis Osterlanda0a1a1b2018-07-24 11:31:22 +0000337 memcpy(groups, rtc->dev.groups, old_cnt * sizeof(*groups));
338 memcpy(groups + old_cnt, grps, add_cnt * sizeof(*groups));
339 groups[old_cnt + add_cnt] = NULL;
340
341 old = rtc->dev.groups;
342 rtc->dev.groups = groups;
343 if (old && old != rtc_attr_groups)
344 devm_kfree(&rtc->dev, old);
345
346 return 0;
347}
348EXPORT_SYMBOL(rtc_add_groups);
349
350int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
351{
352 const struct attribute_group *groups[] = { grp, NULL };
353
354 return rtc_add_groups(rtc, groups);
355}
356EXPORT_SYMBOL(rtc_add_group);