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