Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * RTC subsystem, sysfs interface
4 *
5 * Copyright (C) 2005 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 */
8
9#include <linux/kstrtox.h>
10#include <linux/module.h>
11#include <linux/rtc.h>
12
13#include "rtc-core.h"
14
15/* device attributes */
16
17/*
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
24static ssize_t
25name_show(struct device *dev, struct device_attribute *attr, char *buf)
26{
27 return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
28 dev_name(dev->parent));
29}
30static DEVICE_ATTR_RO(name);
31
32static ssize_t
33date_show(struct device *dev, struct device_attribute *attr, char *buf)
34{
35 ssize_t retval;
36 struct rtc_time tm;
37
38 retval = rtc_read_time(to_rtc_device(dev), &tm);
39 if (retval)
40 return retval;
41
42 return sprintf(buf, "%ptRd\n", &tm);
43}
44static DEVICE_ATTR_RO(date);
45
46static ssize_t
47time_show(struct device *dev, struct device_attribute *attr, char *buf)
48{
49 ssize_t retval;
50 struct rtc_time tm;
51
52 retval = rtc_read_time(to_rtc_device(dev), &tm);
53 if (retval)
54 return retval;
55
56 return sprintf(buf, "%ptRt\n", &tm);
57}
58static DEVICE_ATTR_RO(time);
59
60static ssize_t
61since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
62{
63 ssize_t retval;
64 struct rtc_time tm;
65
66 retval = rtc_read_time(to_rtc_device(dev), &tm);
67 if (retval == 0) {
68 time64_t time;
69
70 time = rtc_tm_to_time64(&tm);
71 retval = sprintf(buf, "%lld\n", time);
72 }
73
74 return retval;
75}
76static DEVICE_ATTR_RO(since_epoch);
77
78static ssize_t
79max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
80{
81 return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
82}
83
84static ssize_t
85max_user_freq_store(struct device *dev, struct device_attribute *attr,
86 const char *buf, size_t n)
87{
88 struct rtc_device *rtc = to_rtc_device(dev);
89 unsigned long val;
90 int err;
91
92 err = kstrtoul(buf, 0, &val);
93 if (err)
94 return err;
95
96 if (val >= 4096 || val == 0)
97 return -EINVAL;
98
99 rtc->max_user_freq = (int)val;
100
101 return n;
102}
103static DEVICE_ATTR_RW(max_user_freq);
104
105/**
106 * hctosys_show - indicate if the given RTC set the system time
107 * @dev: The device that the attribute belongs to.
108 * @attr: The attribute being read.
109 * @buf: The result buffer.
110 *
111 * buf is "1" if the system clock was set by this RTC at the last
112 * boot or resume event.
113 */
114static ssize_t
115hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
116{
117#ifdef CONFIG_RTC_HCTOSYS_DEVICE
118 if (rtc_hctosys_ret == 0 &&
119 strcmp(dev_name(&to_rtc_device(dev)->dev),
120 CONFIG_RTC_HCTOSYS_DEVICE) == 0)
121 return sprintf(buf, "1\n");
122#endif
123 return sprintf(buf, "0\n");
124}
125static DEVICE_ATTR_RO(hctosys);
126
127static ssize_t
128wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
129{
130 ssize_t retval;
131 time64_t alarm;
132 struct rtc_wkalrm alm;
133
134 /* Don't show disabled alarms. For uniformity, RTC alarms are
135 * conceptually one-shot, even though some common RTCs (on PCs)
136 * don't actually work that way.
137 *
138 * NOTE: RTC implementations where the alarm doesn't match an
139 * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
140 * alarms after they trigger, to ensure one-shot semantics.
141 */
142 retval = rtc_read_alarm(to_rtc_device(dev), &alm);
143 if (retval == 0 && alm.enabled) {
144 alarm = rtc_tm_to_time64(&alm.time);
145 retval = sprintf(buf, "%lld\n", alarm);
146 }
147
148 return retval;
149}
150
151static ssize_t
152wakealarm_store(struct device *dev, struct device_attribute *attr,
153 const char *buf, size_t n)
154{
155 ssize_t retval;
156 time64_t now, alarm;
157 time64_t push = 0;
158 struct rtc_wkalrm alm;
159 struct rtc_device *rtc = to_rtc_device(dev);
160 const char *buf_ptr;
161 int adjust = 0;
162
163 /* Only request alarms that trigger in the future. Disable them
164 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
165 */
166 retval = rtc_read_time(rtc, &alm.time);
167 if (retval < 0)
168 return retval;
169 now = rtc_tm_to_time64(&alm.time);
170
171 buf_ptr = buf;
172 if (*buf_ptr == '+') {
173 buf_ptr++;
174 if (*buf_ptr == '=') {
175 buf_ptr++;
176 push = 1;
177 } else {
178 adjust = 1;
179 }
180 }
181 retval = kstrtos64(buf_ptr, 0, &alarm);
182 if (retval)
183 return retval;
184 if (adjust)
185 alarm += now;
186 if (alarm > now || push) {
187 /* Avoid accidentally clobbering active alarms; we can't
188 * entirely prevent that here, without even the minimal
189 * locking from the /dev/rtcN api.
190 */
191 retval = rtc_read_alarm(rtc, &alm);
192 if (retval < 0)
193 return retval;
194 if (alm.enabled) {
195 if (push) {
196 push = rtc_tm_to_time64(&alm.time);
197 alarm += push;
198 } else
199 return -EBUSY;
200 } else if (push)
201 return -EINVAL;
202 alm.enabled = 1;
203 } else {
204 alm.enabled = 0;
205
206 /* Provide a valid future alarm time. Linux isn't EFI,
207 * this time won't be ignored when disabling the alarm.
208 */
209 alarm = now + 300;
210 }
211 rtc_time64_to_tm(alarm, &alm.time);
212
213 retval = rtc_set_alarm(rtc, &alm);
214 return (retval < 0) ? retval : n;
215}
216static DEVICE_ATTR_RW(wakealarm);
217
218static ssize_t
219offset_show(struct device *dev, struct device_attribute *attr, char *buf)
220{
221 ssize_t retval;
222 long offset;
223
224 retval = rtc_read_offset(to_rtc_device(dev), &offset);
225 if (retval == 0)
226 retval = sprintf(buf, "%ld\n", offset);
227
228 return retval;
229}
230
231static ssize_t
232offset_store(struct device *dev, struct device_attribute *attr,
233 const char *buf, size_t n)
234{
235 ssize_t retval;
236 long offset;
237
238 retval = kstrtol(buf, 10, &offset);
239 if (retval == 0)
240 retval = rtc_set_offset(to_rtc_device(dev), offset);
241
242 return (retval < 0) ? retval : n;
243}
244static DEVICE_ATTR_RW(offset);
245
246static ssize_t
247range_show(struct device *dev, struct device_attribute *attr, char *buf)
248{
249 return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min,
250 to_rtc_device(dev)->range_max);
251}
252static DEVICE_ATTR_RO(range);
253
254static struct attribute *rtc_attrs[] = {
255 &dev_attr_name.attr,
256 &dev_attr_date.attr,
257 &dev_attr_time.attr,
258 &dev_attr_since_epoch.attr,
259 &dev_attr_max_user_freq.attr,
260 &dev_attr_hctosys.attr,
261 &dev_attr_wakealarm.attr,
262 &dev_attr_offset.attr,
263 &dev_attr_range.attr,
264 NULL,
265};
266
267/* The reason to trigger an alarm with no process watching it (via sysfs)
268 * is its side effect: waking from a system state like suspend-to-RAM or
269 * suspend-to-disk. So: no attribute unless that side effect is possible.
270 * (Userspace may disable that mechanism later.)
271 */
272static bool rtc_does_wakealarm(struct rtc_device *rtc)
273{
274 if (!device_can_wakeup(rtc->dev.parent))
275 return false;
276
277 return !!test_bit(RTC_FEATURE_ALARM, rtc->features);
278}
279
280static umode_t rtc_attr_is_visible(struct kobject *kobj,
281 struct attribute *attr, int n)
282{
283 struct device *dev = kobj_to_dev(kobj);
284 struct rtc_device *rtc = to_rtc_device(dev);
285 umode_t mode = attr->mode;
286
287 if (attr == &dev_attr_wakealarm.attr) {
288 if (!rtc_does_wakealarm(rtc))
289 mode = 0;
290 } else if (attr == &dev_attr_offset.attr) {
291 if (!rtc->ops->set_offset)
292 mode = 0;
293 } else if (attr == &dev_attr_range.attr) {
294 if (!(rtc->range_max - rtc->range_min))
295 mode = 0;
296 }
297
298 return mode;
299}
300
301static struct attribute_group rtc_attr_group = {
302 .is_visible = rtc_attr_is_visible,
303 .attrs = rtc_attrs,
304};
305
306static const struct attribute_group *rtc_attr_groups[] = {
307 &rtc_attr_group,
308 NULL
309};
310
311const struct attribute_group **rtc_get_dev_attribute_groups(void)
312{
313 return rtc_attr_groups;
314}
315
316int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
317{
318 size_t old_cnt = 0, add_cnt = 0, new_cnt;
319 const struct attribute_group **groups, **old;
320
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);
334 if (!groups)
335 return -ENOMEM;
336 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);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * RTC subsystem, sysfs interface
4 *
5 * Copyright (C) 2005 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 */
8
9#include <linux/module.h>
10#include <linux/rtc.h>
11
12#include "rtc-core.h"
13
14/* device attributes */
15
16/*
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
23static ssize_t
24name_show(struct device *dev, struct device_attribute *attr, char *buf)
25{
26 return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
27 dev_name(dev->parent));
28}
29static DEVICE_ATTR_RO(name);
30
31static ssize_t
32date_show(struct device *dev, struct device_attribute *attr, char *buf)
33{
34 ssize_t retval;
35 struct rtc_time tm;
36
37 retval = rtc_read_time(to_rtc_device(dev), &tm);
38 if (retval)
39 return retval;
40
41 return sprintf(buf, "%ptRd\n", &tm);
42}
43static DEVICE_ATTR_RO(date);
44
45static ssize_t
46time_show(struct device *dev, struct device_attribute *attr, char *buf)
47{
48 ssize_t retval;
49 struct rtc_time tm;
50
51 retval = rtc_read_time(to_rtc_device(dev), &tm);
52 if (retval)
53 return retval;
54
55 return sprintf(buf, "%ptRt\n", &tm);
56}
57static DEVICE_ATTR_RO(time);
58
59static ssize_t
60since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
61{
62 ssize_t retval;
63 struct rtc_time tm;
64
65 retval = rtc_read_time(to_rtc_device(dev), &tm);
66 if (retval == 0) {
67 time64_t time;
68
69 time = rtc_tm_to_time64(&tm);
70 retval = sprintf(buf, "%lld\n", time);
71 }
72
73 return retval;
74}
75static DEVICE_ATTR_RO(since_epoch);
76
77static ssize_t
78max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
79{
80 return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
81}
82
83static ssize_t
84max_user_freq_store(struct device *dev, struct device_attribute *attr,
85 const char *buf, size_t n)
86{
87 struct rtc_device *rtc = to_rtc_device(dev);
88 unsigned long val;
89 int err;
90
91 err = kstrtoul(buf, 0, &val);
92 if (err)
93 return err;
94
95 if (val >= 4096 || val == 0)
96 return -EINVAL;
97
98 rtc->max_user_freq = (int)val;
99
100 return n;
101}
102static DEVICE_ATTR_RW(max_user_freq);
103
104/**
105 * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
106 * @dev: The device that the attribute belongs to.
107 * @attr: The attribute being read.
108 * @buf: The result buffer.
109 *
110 * buf is "1" if the system clock was set by this RTC at the last
111 * boot or resume event.
112 */
113static ssize_t
114hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
115{
116#ifdef CONFIG_RTC_HCTOSYS_DEVICE
117 if (rtc_hctosys_ret == 0 &&
118 strcmp(dev_name(&to_rtc_device(dev)->dev),
119 CONFIG_RTC_HCTOSYS_DEVICE) == 0)
120 return sprintf(buf, "1\n");
121#endif
122 return sprintf(buf, "0\n");
123}
124static DEVICE_ATTR_RO(hctosys);
125
126static ssize_t
127wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
128{
129 ssize_t retval;
130 time64_t alarm;
131 struct rtc_wkalrm alm;
132
133 /* 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.
136 *
137 * 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.
140 */
141 retval = rtc_read_alarm(to_rtc_device(dev), &alm);
142 if (retval == 0 && alm.enabled) {
143 alarm = rtc_tm_to_time64(&alm.time);
144 retval = sprintf(buf, "%lld\n", alarm);
145 }
146
147 return retval;
148}
149
150static ssize_t
151wakealarm_store(struct device *dev, struct device_attribute *attr,
152 const char *buf, size_t n)
153{
154 ssize_t retval;
155 time64_t now, alarm;
156 time64_t push = 0;
157 struct rtc_wkalrm alm;
158 struct rtc_device *rtc = to_rtc_device(dev);
159 const char *buf_ptr;
160 int adjust = 0;
161
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 */
165 retval = rtc_read_time(rtc, &alm.time);
166 if (retval < 0)
167 return retval;
168 now = rtc_tm_to_time64(&alm.time);
169
170 buf_ptr = buf;
171 if (*buf_ptr == '+') {
172 buf_ptr++;
173 if (*buf_ptr == '=') {
174 buf_ptr++;
175 push = 1;
176 } else {
177 adjust = 1;
178 }
179 }
180 retval = kstrtos64(buf_ptr, 0, &alarm);
181 if (retval)
182 return retval;
183 if (adjust)
184 alarm += now;
185 if (alarm > now || push) {
186 /* 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 */
190 retval = rtc_read_alarm(rtc, &alm);
191 if (retval < 0)
192 return retval;
193 if (alm.enabled) {
194 if (push) {
195 push = rtc_tm_to_time64(&alm.time);
196 alarm += push;
197 } else
198 return -EBUSY;
199 } else if (push)
200 return -EINVAL;
201 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 }
210 rtc_time64_to_tm(alarm, &alm.time);
211
212 retval = rtc_set_alarm(rtc, &alm);
213 return (retval < 0) ? retval : n;
214}
215static DEVICE_ATTR_RW(wakealarm);
216
217static 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
245static 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
253static 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,
261 &dev_attr_offset.attr,
262 &dev_attr_range.attr,
263 NULL,
264};
265
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 */
271static bool rtc_does_wakealarm(struct rtc_device *rtc)
272{
273 if (!device_can_wakeup(rtc->dev.parent))
274 return false;
275
276 return rtc->ops->set_alarm != NULL;
277}
278
279static umode_t rtc_attr_is_visible(struct kobject *kobj,
280 struct attribute *attr, int n)
281{
282 struct device *dev = kobj_to_dev(kobj);
283 struct rtc_device *rtc = to_rtc_device(dev);
284 umode_t mode = attr->mode;
285
286 if (attr == &dev_attr_wakealarm.attr) {
287 if (!rtc_does_wakealarm(rtc))
288 mode = 0;
289 } else if (attr == &dev_attr_offset.attr) {
290 if (!rtc->ops->set_offset)
291 mode = 0;
292 } else if (attr == &dev_attr_range.attr) {
293 if (!(rtc->range_max - rtc->range_min))
294 mode = 0;
295 }
296
297 return mode;
298}
299
300static struct attribute_group rtc_attr_group = {
301 .is_visible = rtc_attr_is_visible,
302 .attrs = rtc_attrs,
303};
304
305static 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)
311{
312 return rtc_attr_groups;
313}
314
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);
335 if (!groups)
336 return -ENOMEM;
337 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);