blob: 0d3dac557df56ba0673e90ce126ea37aaf9fa36c [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
14
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080015/* device attributes */
16
David Brownell8a0bdfd72008-02-06 01:38:45 -080017/*
18 * NOTE: RTC times displayed in sysfs use the RTC's timezone. That's
19 * ideally UTC. However, PCs that also boot to MS-Windows normally use
20 * the local time and change to match daylight savings time. That affects
21 * attributes including date, time, since_epoch, and wakealarm.
22 */
23
David Brownellcd966202007-05-08 00:33:40 -070024static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070025name_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080026{
Alexandre Belloni77a73f32017-06-02 13:57:03 +020027 return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
28 dev_name(dev->parent));
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080029}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070030static DEVICE_ATTR_RO(name);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080031
David Brownellcd966202007-05-08 00:33:40 -070032static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070033date_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080034{
35 ssize_t retval;
36 struct rtc_time tm;
37
David Brownellab6a2d72007-05-08 00:33:30 -070038 retval = rtc_read_time(to_rtc_device(dev), &tm);
Andy Shevchenko5548cbf2018-12-04 23:23:12 +020039 if (retval)
40 return retval;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080041
Andy Shevchenko5548cbf2018-12-04 23:23:12 +020042 return sprintf(buf, "%ptRd\n", &tm);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080043}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070044static DEVICE_ATTR_RO(date);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080045
David Brownellcd966202007-05-08 00:33:40 -070046static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070047time_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080048{
49 ssize_t retval;
50 struct rtc_time tm;
51
David Brownellab6a2d72007-05-08 00:33:30 -070052 retval = rtc_read_time(to_rtc_device(dev), &tm);
Andy Shevchenko5548cbf2018-12-04 23:23:12 +020053 if (retval)
54 return retval;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080055
Andy Shevchenko5548cbf2018-12-04 23:23:12 +020056 return sprintf(buf, "%ptRt\n", &tm);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080057}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070058static DEVICE_ATTR_RO(time);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080059
David Brownellcd966202007-05-08 00:33:40 -070060static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070061since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080062{
63 ssize_t retval;
64 struct rtc_time tm;
65
David Brownellab6a2d72007-05-08 00:33:30 -070066 retval = rtc_read_time(to_rtc_device(dev), &tm);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080067 if (retval == 0) {
Baolin Wang9a06da22017-11-09 15:09:20 +080068 time64_t time;
69
70 time = rtc_tm_to_time64(&tm);
71 retval = sprintf(buf, "%lld\n", time);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080072 }
73
74 return retval;
75}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070076static DEVICE_ATTR_RO(since_epoch);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080077
Bryan Kadzban06c65eb2007-10-16 01:28:22 -070078static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070079max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
Bryan Kadzban06c65eb2007-10-16 01:28:22 -070080{
81 return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
82}
83
84static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070085max_user_freq_store(struct device *dev, struct device_attribute *attr,
Bryan Kadzban06c65eb2007-10-16 01:28:22 -070086 const char *buf, size_t n)
87{
88 struct rtc_device *rtc = to_rtc_device(dev);
LABBE Corentinf5712872015-12-17 14:11:04 +010089 unsigned long val;
90 int err;
91
92 err = kstrtoul(buf, 0, &val);
93 if (err)
94 return err;
Bryan Kadzban06c65eb2007-10-16 01:28:22 -070095
96 if (val >= 4096 || val == 0)
97 return -EINVAL;
98
99 rtc->max_user_freq = (int)val;
100
101 return n;
102}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -0700103static DEVICE_ATTR_RW(max_user_freq);
Bryan Kadzban06c65eb2007-10-16 01:28:22 -0700104
David Fries4c24e292012-10-04 17:14:12 -0700105/**
106 * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
107 *
108 * Returns 1 if the system clock was set by this RTC at the last
109 * boot or resume event.
110 */
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700111static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -0700112hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700113{
114#ifdef CONFIG_RTC_HCTOSYS_DEVICE
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -0800115 if (rtc_hctosys_ret == 0 &&
116 strcmp(dev_name(&to_rtc_device(dev)->dev),
117 CONFIG_RTC_HCTOSYS_DEVICE) == 0)
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700118 return sprintf(buf, "1\n");
119 else
120#endif
121 return sprintf(buf, "0\n");
122}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -0700123static DEVICE_ATTR_RO(hctosys);
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700124
David Brownell3925a5c2007-02-12 00:52:47 -0800125static ssize_t
Dmitry Torokhova17ccd12015-07-23 16:01:07 -0700126wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell3925a5c2007-02-12 00:52:47 -0800127{
128 ssize_t retval;
Baolin Wang9a06da22017-11-09 15:09:20 +0800129 time64_t alarm;
David Brownell3925a5c2007-02-12 00:52:47 -0800130 struct rtc_wkalrm alm;
131
David Brownell8a0bdfd72008-02-06 01:38:45 -0800132 /* Don't show disabled alarms. For uniformity, RTC alarms are
133 * conceptually one-shot, even though some common RTCs (on PCs)
134 * don't actually work that way.
David Brownell3925a5c2007-02-12 00:52:47 -0800135 *
David Brownell8a0bdfd72008-02-06 01:38:45 -0800136 * NOTE: RTC implementations where the alarm doesn't match an
137 * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
138 * alarms after they trigger, to ensure one-shot semantics.
David Brownell3925a5c2007-02-12 00:52:47 -0800139 */
David Brownellab6a2d72007-05-08 00:33:30 -0700140 retval = rtc_read_alarm(to_rtc_device(dev), &alm);
David Brownell3925a5c2007-02-12 00:52:47 -0800141 if (retval == 0 && alm.enabled) {
Baolin Wang9a06da22017-11-09 15:09:20 +0800142 alarm = rtc_tm_to_time64(&alm.time);
143 retval = sprintf(buf, "%lld\n", alarm);
David Brownell3925a5c2007-02-12 00:52:47 -0800144 }
145
146 return retval;
147}
148
149static ssize_t
Dmitry Torokhova17ccd12015-07-23 16:01:07 -0700150wakealarm_store(struct device *dev, struct device_attribute *attr,
David Brownellcd966202007-05-08 00:33:40 -0700151 const char *buf, size_t n)
David Brownell3925a5c2007-02-12 00:52:47 -0800152{
153 ssize_t retval;
Baolin Wang9a06da22017-11-09 15:09:20 +0800154 time64_t now, alarm;
155 time64_t push = 0;
David Brownell3925a5c2007-02-12 00:52:47 -0800156 struct rtc_wkalrm alm;
David Brownellab6a2d72007-05-08 00:33:30 -0700157 struct rtc_device *rtc = to_rtc_device(dev);
LABBE Corentin84281c22016-08-12 14:46:14 +0200158 const char *buf_ptr;
Zhao Yakuic116bc22008-04-28 02:11:58 -0700159 int adjust = 0;
David Brownell3925a5c2007-02-12 00:52:47 -0800160
161 /* Only request alarms that trigger in the future. Disable them
162 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
163 */
David Brownellab6a2d72007-05-08 00:33:30 -0700164 retval = rtc_read_time(rtc, &alm.time);
David Brownell3925a5c2007-02-12 00:52:47 -0800165 if (retval < 0)
166 return retval;
Baolin Wang9a06da22017-11-09 15:09:20 +0800167 now = rtc_tm_to_time64(&alm.time);
David Brownell3925a5c2007-02-12 00:52:47 -0800168
LABBE Corentin84281c22016-08-12 14:46:14 +0200169 buf_ptr = buf;
Zhao Yakuic116bc22008-04-28 02:11:58 -0700170 if (*buf_ptr == '+') {
171 buf_ptr++;
Bernie Thompson1df0a472013-07-03 15:07:03 -0700172 if (*buf_ptr == '=') {
173 buf_ptr++;
174 push = 1;
175 } else
176 adjust = 1;
Zhao Yakuic116bc22008-04-28 02:11:58 -0700177 }
Baolin Wang9a06da22017-11-09 15:09:20 +0800178 retval = kstrtos64(buf_ptr, 0, &alarm);
LABBE Corentinf5712872015-12-17 14:11:04 +0100179 if (retval)
180 return retval;
Zhao Yakuic116bc22008-04-28 02:11:58 -0700181 if (adjust) {
182 alarm += now;
183 }
Bernie Thompson1df0a472013-07-03 15:07:03 -0700184 if (alarm > now || push) {
David Brownell3925a5c2007-02-12 00:52:47 -0800185 /* Avoid accidentally clobbering active alarms; we can't
186 * entirely prevent that here, without even the minimal
187 * locking from the /dev/rtcN api.
188 */
David Brownellab6a2d72007-05-08 00:33:30 -0700189 retval = rtc_read_alarm(rtc, &alm);
David Brownell3925a5c2007-02-12 00:52:47 -0800190 if (retval < 0)
191 return retval;
Bernie Thompson1df0a472013-07-03 15:07:03 -0700192 if (alm.enabled) {
193 if (push) {
Baolin Wang9a06da22017-11-09 15:09:20 +0800194 push = rtc_tm_to_time64(&alm.time);
Bernie Thompson1df0a472013-07-03 15:07:03 -0700195 alarm += push;
196 } else
197 return -EBUSY;
198 } else if (push)
199 return -EINVAL;
David Brownell3925a5c2007-02-12 00:52:47 -0800200 alm.enabled = 1;
201 } else {
202 alm.enabled = 0;
203
204 /* Provide a valid future alarm time. Linux isn't EFI,
205 * this time won't be ignored when disabling the alarm.
206 */
207 alarm = now + 300;
208 }
Baolin Wang9a06da22017-11-09 15:09:20 +0800209 rtc_time64_to_tm(alarm, &alm.time);
David Brownell3925a5c2007-02-12 00:52:47 -0800210
David Brownellab6a2d72007-05-08 00:33:30 -0700211 retval = rtc_set_alarm(rtc, &alm);
David Brownell3925a5c2007-02-12 00:52:47 -0800212 return (retval < 0) ? retval : n;
213}
Dmitry Torokhova17ccd12015-07-23 16:01:07 -0700214static DEVICE_ATTR_RW(wakealarm);
David Brownell3925a5c2007-02-12 00:52:47 -0800215
Joshua Clayton5495a412016-02-05 12:41:12 -0800216static ssize_t
217offset_show(struct device *dev, struct device_attribute *attr, char *buf)
218{
219 ssize_t retval;
220 long offset;
221
222 retval = rtc_read_offset(to_rtc_device(dev), &offset);
223 if (retval == 0)
224 retval = sprintf(buf, "%ld\n", offset);
225
226 return retval;
227}
228
229static ssize_t
230offset_store(struct device *dev, struct device_attribute *attr,
231 const char *buf, size_t n)
232{
233 ssize_t retval;
234 long offset;
235
236 retval = kstrtol(buf, 10, &offset);
237 if (retval == 0)
238 retval = rtc_set_offset(to_rtc_device(dev), offset);
239
240 return (retval < 0) ? retval : n;
241}
242static DEVICE_ATTR_RW(offset);
243
Alexandre Belloni71db0492018-02-17 14:58:40 +0100244static ssize_t
245range_show(struct device *dev, struct device_attribute *attr, char *buf)
246{
247 return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min,
248 to_rtc_device(dev)->range_max);
249}
250static DEVICE_ATTR_RO(range);
251
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700252static struct attribute *rtc_attrs[] = {
253 &dev_attr_name.attr,
254 &dev_attr_date.attr,
255 &dev_attr_time.attr,
256 &dev_attr_since_epoch.attr,
257 &dev_attr_max_user_freq.attr,
258 &dev_attr_hctosys.attr,
259 &dev_attr_wakealarm.attr,
Joshua Clayton5495a412016-02-05 12:41:12 -0800260 &dev_attr_offset.attr,
Alexandre Belloni71db0492018-02-17 14:58:40 +0100261 &dev_attr_range.attr,
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700262 NULL,
263};
David Brownell3925a5c2007-02-12 00:52:47 -0800264
265/* The reason to trigger an alarm with no process watching it (via sysfs)
266 * is its side effect: waking from a system state like suspend-to-RAM or
267 * suspend-to-disk. So: no attribute unless that side effect is possible.
268 * (Userspace may disable that mechanism later.)
269 */
Dmitry Torokhovdf100c02015-07-23 16:01:06 -0700270static bool rtc_does_wakealarm(struct rtc_device *rtc)
David Brownell3925a5c2007-02-12 00:52:47 -0800271{
David Brownellcd966202007-05-08 00:33:40 -0700272 if (!device_can_wakeup(rtc->dev.parent))
Dmitry Torokhovdf100c02015-07-23 16:01:06 -0700273 return false;
274
David Brownell3925a5c2007-02-12 00:52:47 -0800275 return rtc->ops->set_alarm != NULL;
276}
277
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700278static umode_t rtc_attr_is_visible(struct kobject *kobj,
279 struct attribute *attr, int n)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800280{
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700281 struct device *dev = container_of(kobj, struct device, kobj);
282 struct rtc_device *rtc = to_rtc_device(dev);
283 umode_t mode = attr->mode;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800284
Joshua Clayton5495a412016-02-05 12:41:12 -0800285 if (attr == &dev_attr_wakealarm.attr) {
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700286 if (!rtc_does_wakealarm(rtc))
287 mode = 0;
Joshua Clayton5495a412016-02-05 12:41:12 -0800288 } else if (attr == &dev_attr_offset.attr) {
289 if (!rtc->ops->set_offset)
290 mode = 0;
Alexandre Belloni71db0492018-02-17 14:58:40 +0100291 } else if (attr == &dev_attr_range.attr) {
292 if (!(rtc->range_max - rtc->range_min))
293 mode = 0;
Joshua Clayton5495a412016-02-05 12:41:12 -0800294 }
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800295
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700296 return mode;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800297}
298
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700299static struct attribute_group rtc_attr_group = {
300 .is_visible = rtc_attr_is_visible,
301 .attrs = rtc_attrs,
302};
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800303
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700304static const struct attribute_group *rtc_attr_groups[] = {
305 &rtc_attr_group,
306 NULL
307};
308
309const struct attribute_group **rtc_get_dev_attribute_groups(void)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800310{
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700311 return rtc_attr_groups;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800312}
Denis Osterlanda0a1a1b2018-07-24 11:31:22 +0000313
314int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
315{
316 size_t old_cnt = 0, add_cnt = 0, new_cnt;
317 const struct attribute_group **groups, **old;
318
319 if (rtc->registered)
320 return -EINVAL;
321 if (!grps)
322 return -EINVAL;
323
324 groups = rtc->dev.groups;
325 if (groups)
326 for (; *groups; groups++)
327 old_cnt++;
328
329 for (groups = grps; *groups; groups++)
330 add_cnt++;
331
332 new_cnt = old_cnt + add_cnt + 1;
333 groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL);
Dan Carpenter777d8ae2018-08-27 12:22:34 +0300334 if (!groups)
335 return -ENOMEM;
Denis Osterlanda0a1a1b2018-07-24 11:31:22 +0000336 memcpy(groups, rtc->dev.groups, old_cnt * sizeof(*groups));
337 memcpy(groups + old_cnt, grps, add_cnt * sizeof(*groups));
338 groups[old_cnt + add_cnt] = NULL;
339
340 old = rtc->dev.groups;
341 rtc->dev.groups = groups;
342 if (old && old != rtc_attr_groups)
343 devm_kfree(&rtc->dev, old);
344
345 return 0;
346}
347EXPORT_SYMBOL(rtc_add_groups);
348
349int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
350{
351 const struct attribute_group *groups[] = { grp, NULL };
352
353 return rtc_add_groups(rtc, groups);
354}
355EXPORT_SYMBOL(rtc_add_group);