Commit | Line | Data |
---|---|---|
cdf7545a | 1 | // SPDX-License-Identifier: GPL-2.0 |
c5c3e192 AZ |
2 | /* |
3 | * RTC subsystem, sysfs interface | |
4 | * | |
5 | * Copyright (C) 2005 Tower Technologies | |
6 | * Author: Alessandro Zummo <a.zummo@towertech.it> | |
cdf7545a | 7 | */ |
c5c3e192 AZ |
8 | |
9 | #include <linux/module.h> | |
10 | #include <linux/rtc.h> | |
11 | ||
ab6a2d70 DB |
12 | #include "rtc-core.h" |
13 | ||
c5c3e192 AZ |
14 | /* device attributes */ |
15 | ||
8a0bdfd7 DB |
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 | ||
cd966209 | 23 | static ssize_t |
f21e6835 | 24 | name_show(struct device *dev, struct device_attribute *attr, char *buf) |
c5c3e192 | 25 | { |
77a73f3c AB |
26 | return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent), |
27 | dev_name(dev->parent)); | |
c5c3e192 | 28 | } |
f21e6835 | 29 | static DEVICE_ATTR_RO(name); |
c5c3e192 | 30 | |
cd966209 | 31 | static ssize_t |
f21e6835 | 32 | date_show(struct device *dev, struct device_attribute *attr, char *buf) |
c5c3e192 AZ |
33 | { |
34 | ssize_t retval; | |
35 | struct rtc_time tm; | |
36 | ||
ab6a2d70 | 37 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
5548cbf7 AS |
38 | if (retval) |
39 | return retval; | |
c5c3e192 | 40 | |
5548cbf7 | 41 | return sprintf(buf, "%ptRd\n", &tm); |
c5c3e192 | 42 | } |
f21e6835 | 43 | static DEVICE_ATTR_RO(date); |
c5c3e192 | 44 | |
cd966209 | 45 | static ssize_t |
f21e6835 | 46 | time_show(struct device *dev, struct device_attribute *attr, char *buf) |
c5c3e192 AZ |
47 | { |
48 | ssize_t retval; | |
49 | struct rtc_time tm; | |
50 | ||
ab6a2d70 | 51 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
5548cbf7 AS |
52 | if (retval) |
53 | return retval; | |
c5c3e192 | 54 | |
5548cbf7 | 55 | return sprintf(buf, "%ptRt\n", &tm); |
c5c3e192 | 56 | } |
f21e6835 | 57 | static DEVICE_ATTR_RO(time); |
c5c3e192 | 58 | |
cd966209 | 59 | static ssize_t |
f21e6835 | 60 | since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf) |
c5c3e192 AZ |
61 | { |
62 | ssize_t retval; | |
63 | struct rtc_time tm; | |
64 | ||
ab6a2d70 | 65 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
c5c3e192 | 66 | if (retval == 0) { |
9a06da2e BW |
67 | time64_t time; |
68 | ||
69 | time = rtc_tm_to_time64(&tm); | |
70 | retval = sprintf(buf, "%lld\n", time); | |
c5c3e192 AZ |
71 | } |
72 | ||
73 | return retval; | |
74 | } | |
f21e6835 | 75 | static DEVICE_ATTR_RO(since_epoch); |
c5c3e192 | 76 | |
06c65eb4 | 77 | static ssize_t |
f21e6835 | 78 | max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf) |
06c65eb4 BK |
79 | { |
80 | return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq); | |
81 | } | |
82 | ||
83 | static ssize_t | |
f21e6835 | 84 | max_user_freq_store(struct device *dev, struct device_attribute *attr, |
606cc43c | 85 | const char *buf, size_t n) |
06c65eb4 BK |
86 | { |
87 | struct rtc_device *rtc = to_rtc_device(dev); | |
f571287b LC |
88 | unsigned long val; |
89 | int err; | |
90 | ||
91 | err = kstrtoul(buf, 0, &val); | |
92 | if (err) | |
93 | return err; | |
06c65eb4 BK |
94 | |
95 | if (val >= 4096 || val == 0) | |
96 | return -EINVAL; | |
97 | ||
98 | rtc->max_user_freq = (int)val; | |
99 | ||
100 | return n; | |
101 | } | |
f21e6835 | 102 | static DEVICE_ATTR_RW(max_user_freq); |
06c65eb4 | 103 | |
4c24e29e DF |
104 | /** |
105 | * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time | |
6f693192 AB |
106 | * @dev: The device that the attribute belongs to. |
107 | * @attr: The attribute being read. | |
108 | * @buf: The result buffer. | |
4c24e29e | 109 | * |
6f693192 | 110 | * buf is "1" if the system clock was set by this RTC at the last |
4c24e29e DF |
111 | * boot or resume event. |
112 | */ | |
d8c1acb1 | 113 | static ssize_t |
f21e6835 | 114 | hctosys_show(struct device *dev, struct device_attribute *attr, char *buf) |
d8c1acb1 MG |
115 | { |
116 | #ifdef CONFIG_RTC_HCTOSYS_DEVICE | |
d0ab4a4d | 117 | if (rtc_hctosys_ret == 0 && |
606cc43c AB |
118 | strcmp(dev_name(&to_rtc_device(dev)->dev), |
119 | CONFIG_RTC_HCTOSYS_DEVICE) == 0) | |
d8c1acb1 | 120 | return sprintf(buf, "1\n"); |
d8c1acb1 | 121 | #endif |
606cc43c | 122 | return sprintf(buf, "0\n"); |
d8c1acb1 | 123 | } |
f21e6835 GKH |
124 | static DEVICE_ATTR_RO(hctosys); |
125 | ||
3925a5ce | 126 | static ssize_t |
a17ccd1c | 127 | wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf) |
3925a5ce DB |
128 | { |
129 | ssize_t retval; | |
9a06da2e | 130 | time64_t alarm; |
3925a5ce DB |
131 | struct rtc_wkalrm alm; |
132 | ||
8a0bdfd7 DB |
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. | |
3925a5ce | 136 | * |
8a0bdfd7 DB |
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. | |
3925a5ce | 140 | */ |
ab6a2d70 | 141 | retval = rtc_read_alarm(to_rtc_device(dev), &alm); |
3925a5ce | 142 | if (retval == 0 && alm.enabled) { |
9a06da2e BW |
143 | alarm = rtc_tm_to_time64(&alm.time); |
144 | retval = sprintf(buf, "%lld\n", alarm); | |
3925a5ce DB |
145 | } |
146 | ||
147 | return retval; | |
148 | } | |
149 | ||
150 | static ssize_t | |
a17ccd1c | 151 | wakealarm_store(struct device *dev, struct device_attribute *attr, |
cd966209 | 152 | const char *buf, size_t n) |
3925a5ce DB |
153 | { |
154 | ssize_t retval; | |
9a06da2e BW |
155 | time64_t now, alarm; |
156 | time64_t push = 0; | |
3925a5ce | 157 | struct rtc_wkalrm alm; |
ab6a2d70 | 158 | struct rtc_device *rtc = to_rtc_device(dev); |
84281c2d | 159 | const char *buf_ptr; |
c116bc2a | 160 | int adjust = 0; |
3925a5ce DB |
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 | */ | |
ab6a2d70 | 165 | retval = rtc_read_time(rtc, &alm.time); |
3925a5ce DB |
166 | if (retval < 0) |
167 | return retval; | |
9a06da2e | 168 | now = rtc_tm_to_time64(&alm.time); |
3925a5ce | 169 | |
84281c2d | 170 | buf_ptr = buf; |
c116bc2a ZY |
171 | if (*buf_ptr == '+') { |
172 | buf_ptr++; | |
1df0a471 BT |
173 | if (*buf_ptr == '=') { |
174 | buf_ptr++; | |
175 | push = 1; | |
606cc43c | 176 | } else { |
1df0a471 | 177 | adjust = 1; |
606cc43c | 178 | } |
c116bc2a | 179 | } |
9a06da2e | 180 | retval = kstrtos64(buf_ptr, 0, &alarm); |
f571287b LC |
181 | if (retval) |
182 | return retval; | |
606cc43c | 183 | if (adjust) |
c116bc2a | 184 | alarm += now; |
1df0a471 | 185 | if (alarm > now || push) { |
3925a5ce DB |
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 | */ | |
ab6a2d70 | 190 | retval = rtc_read_alarm(rtc, &alm); |
3925a5ce DB |
191 | if (retval < 0) |
192 | return retval; | |
1df0a471 BT |
193 | if (alm.enabled) { |
194 | if (push) { | |
9a06da2e | 195 | push = rtc_tm_to_time64(&alm.time); |
1df0a471 BT |
196 | alarm += push; |
197 | } else | |
198 | return -EBUSY; | |
199 | } else if (push) | |
200 | return -EINVAL; | |
3925a5ce DB |
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 | } | |
9a06da2e | 210 | rtc_time64_to_tm(alarm, &alm.time); |
3925a5ce | 211 | |
ab6a2d70 | 212 | retval = rtc_set_alarm(rtc, &alm); |
3925a5ce DB |
213 | return (retval < 0) ? retval : n; |
214 | } | |
a17ccd1c | 215 | static DEVICE_ATTR_RW(wakealarm); |
3925a5ce | 216 | |
5495a415 JC |
217 | static ssize_t |
218 | offset_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 | ||
230 | static ssize_t | |
231 | offset_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 | } | |
243 | static DEVICE_ATTR_RW(offset); | |
244 | ||
71db049e AB |
245 | static ssize_t |
246 | range_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 | } | |
251 | static DEVICE_ATTR_RO(range); | |
252 | ||
3ee2c40b DT |
253 | static 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, | |
5495a415 | 261 | &dev_attr_offset.attr, |
71db049e | 262 | &dev_attr_range.attr, |
3ee2c40b DT |
263 | NULL, |
264 | }; | |
3925a5ce DB |
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 | */ | |
df100c01 | 271 | static bool rtc_does_wakealarm(struct rtc_device *rtc) |
3925a5ce | 272 | { |
cd966209 | 273 | if (!device_can_wakeup(rtc->dev.parent)) |
df100c01 DT |
274 | return false; |
275 | ||
4d0185e6 | 276 | return !!test_bit(RTC_FEATURE_ALARM, rtc->features); |
3925a5ce DB |
277 | } |
278 | ||
3ee2c40b DT |
279 | static umode_t rtc_attr_is_visible(struct kobject *kobj, |
280 | struct attribute *attr, int n) | |
c5c3e192 | 281 | { |
ae243ef0 | 282 | struct device *dev = kobj_to_dev(kobj); |
3ee2c40b DT |
283 | struct rtc_device *rtc = to_rtc_device(dev); |
284 | umode_t mode = attr->mode; | |
c5c3e192 | 285 | |
5495a415 | 286 | if (attr == &dev_attr_wakealarm.attr) { |
3ee2c40b DT |
287 | if (!rtc_does_wakealarm(rtc)) |
288 | mode = 0; | |
5495a415 JC |
289 | } else if (attr == &dev_attr_offset.attr) { |
290 | if (!rtc->ops->set_offset) | |
291 | mode = 0; | |
71db049e AB |
292 | } else if (attr == &dev_attr_range.attr) { |
293 | if (!(rtc->range_max - rtc->range_min)) | |
294 | mode = 0; | |
5495a415 | 295 | } |
c5c3e192 | 296 | |
3ee2c40b | 297 | return mode; |
c5c3e192 AZ |
298 | } |
299 | ||
3ee2c40b DT |
300 | static struct attribute_group rtc_attr_group = { |
301 | .is_visible = rtc_attr_is_visible, | |
302 | .attrs = rtc_attrs, | |
303 | }; | |
304 | ||
305 | static const struct attribute_group *rtc_attr_groups[] = { | |
306 | &rtc_attr_group, | |
307 | NULL | |
308 | }; | |
c5c3e192 | 309 | |
3ee2c40b | 310 | const struct attribute_group **rtc_get_dev_attribute_groups(void) |
c5c3e192 | 311 | { |
3ee2c40b | 312 | return rtc_attr_groups; |
c5c3e192 | 313 | } |
a0a1a1ba DO |
314 | |
315 | int 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 | ||
a0a1a1ba DO |
320 | if (!grps) |
321 | return -EINVAL; | |
322 | ||
323 | groups = rtc->dev.groups; | |
324 | if (groups) | |
325 | for (; *groups; groups++) | |
326 | old_cnt++; | |
327 | ||
328 | for (groups = grps; *groups; groups++) | |
329 | add_cnt++; | |
330 | ||
331 | new_cnt = old_cnt + add_cnt + 1; | |
332 | groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL); | |
777d8ae5 DC |
333 | if (!groups) |
334 | return -ENOMEM; | |
a0a1a1ba DO |
335 | memcpy(groups, rtc->dev.groups, old_cnt * sizeof(*groups)); |
336 | memcpy(groups + old_cnt, grps, add_cnt * sizeof(*groups)); | |
337 | groups[old_cnt + add_cnt] = NULL; | |
338 | ||
339 | old = rtc->dev.groups; | |
340 | rtc->dev.groups = groups; | |
341 | if (old && old != rtc_attr_groups) | |
342 | devm_kfree(&rtc->dev, old); | |
343 | ||
344 | return 0; | |
345 | } | |
346 | EXPORT_SYMBOL(rtc_add_groups); | |
347 | ||
348 | int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp) | |
349 | { | |
350 | const struct attribute_group *groups[] = { grp, NULL }; | |
351 | ||
352 | return rtc_add_groups(rtc, groups); | |
353 | } | |
354 | EXPORT_SYMBOL(rtc_add_group); |