blob: e364550eb9a7fad8ef4efed7ca2f200116832e9c [file] [log] [blame]
Alessandro Zummoc5c3e192006-03-27 01:16:39 -08001/*
2 * RTC subsystem, sysfs interface
3 *
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10*/
11
12#include <linux/module.h>
13#include <linux/rtc.h>
14
David Brownellab6a2d72007-05-08 00:33:30 -070015#include "rtc-core.h"
16
17
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080018/* device attributes */
19
David Brownell8a0bdfd72008-02-06 01:38:45 -080020/*
21 * NOTE: RTC times displayed in sysfs use the RTC's timezone. That's
22 * ideally UTC. However, PCs that also boot to MS-Windows normally use
23 * the local time and change to match daylight savings time. That affects
24 * attributes including date, time, since_epoch, and wakealarm.
25 */
26
David Brownellcd966202007-05-08 00:33:40 -070027static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070028name_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080029{
Alexandre Belloni77a73f32017-06-02 13:57:03 +020030 return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
31 dev_name(dev->parent));
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080032}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070033static DEVICE_ATTR_RO(name);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080034
David Brownellcd966202007-05-08 00:33:40 -070035static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070036date_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080037{
38 ssize_t retval;
39 struct rtc_time tm;
40
David Brownellab6a2d72007-05-08 00:33:30 -070041 retval = rtc_read_time(to_rtc_device(dev), &tm);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080042 if (retval == 0) {
43 retval = sprintf(buf, "%04d-%02d-%02d\n",
44 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
45 }
46
47 return retval;
48}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070049static DEVICE_ATTR_RO(date);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080050
David Brownellcd966202007-05-08 00:33:40 -070051static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070052time_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080053{
54 ssize_t retval;
55 struct rtc_time tm;
56
David Brownellab6a2d72007-05-08 00:33:30 -070057 retval = rtc_read_time(to_rtc_device(dev), &tm);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080058 if (retval == 0) {
59 retval = sprintf(buf, "%02d:%02d:%02d\n",
60 tm.tm_hour, tm.tm_min, tm.tm_sec);
61 }
62
63 return retval;
64}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070065static DEVICE_ATTR_RO(time);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080066
David Brownellcd966202007-05-08 00:33:40 -070067static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070068since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080069{
70 ssize_t retval;
71 struct rtc_time tm;
72
David Brownellab6a2d72007-05-08 00:33:30 -070073 retval = rtc_read_time(to_rtc_device(dev), &tm);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080074 if (retval == 0) {
75 unsigned long time;
76 rtc_tm_to_time(&tm, &time);
77 retval = sprintf(buf, "%lu\n", time);
78 }
79
80 return retval;
81}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070082static DEVICE_ATTR_RO(since_epoch);
Alessandro Zummoc5c3e192006-03-27 01:16:39 -080083
Bryan Kadzban06c65eb2007-10-16 01:28:22 -070084static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070085max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
Bryan Kadzban06c65eb2007-10-16 01:28:22 -070086{
87 return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
88}
89
90static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -070091max_user_freq_store(struct device *dev, struct device_attribute *attr,
Bryan Kadzban06c65eb2007-10-16 01:28:22 -070092 const char *buf, size_t n)
93{
94 struct rtc_device *rtc = to_rtc_device(dev);
LABBE Corentinf5712872015-12-17 14:11:04 +010095 unsigned long val;
96 int err;
97
98 err = kstrtoul(buf, 0, &val);
99 if (err)
100 return err;
Bryan Kadzban06c65eb2007-10-16 01:28:22 -0700101
102 if (val >= 4096 || val == 0)
103 return -EINVAL;
104
105 rtc->max_user_freq = (int)val;
106
107 return n;
108}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -0700109static DEVICE_ATTR_RW(max_user_freq);
Bryan Kadzban06c65eb2007-10-16 01:28:22 -0700110
David Fries4c24e292012-10-04 17:14:12 -0700111/**
112 * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
113 *
114 * Returns 1 if the system clock was set by this RTC at the last
115 * boot or resume event.
116 */
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700117static ssize_t
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -0700118hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700119{
120#ifdef CONFIG_RTC_HCTOSYS_DEVICE
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -0800121 if (rtc_hctosys_ret == 0 &&
122 strcmp(dev_name(&to_rtc_device(dev)->dev),
123 CONFIG_RTC_HCTOSYS_DEVICE) == 0)
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700124 return sprintf(buf, "1\n");
125 else
126#endif
127 return sprintf(buf, "0\n");
128}
Greg Kroah-Hartmanf21e6832013-07-24 15:05:22 -0700129static DEVICE_ATTR_RO(hctosys);
Matthew Garrettd8c1acb2009-09-22 16:46:32 -0700130
David Brownell3925a5c2007-02-12 00:52:47 -0800131static ssize_t
Dmitry Torokhova17ccd12015-07-23 16:01:07 -0700132wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell3925a5c2007-02-12 00:52:47 -0800133{
134 ssize_t retval;
135 unsigned long alarm;
136 struct rtc_wkalrm alm;
137
David Brownell8a0bdfd72008-02-06 01:38:45 -0800138 /* Don't show disabled alarms. For uniformity, RTC alarms are
139 * conceptually one-shot, even though some common RTCs (on PCs)
140 * don't actually work that way.
David Brownell3925a5c2007-02-12 00:52:47 -0800141 *
David Brownell8a0bdfd72008-02-06 01:38:45 -0800142 * NOTE: RTC implementations where the alarm doesn't match an
143 * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
144 * alarms after they trigger, to ensure one-shot semantics.
David Brownell3925a5c2007-02-12 00:52:47 -0800145 */
David Brownellab6a2d72007-05-08 00:33:30 -0700146 retval = rtc_read_alarm(to_rtc_device(dev), &alm);
David Brownell3925a5c2007-02-12 00:52:47 -0800147 if (retval == 0 && alm.enabled) {
148 rtc_tm_to_time(&alm.time, &alarm);
149 retval = sprintf(buf, "%lu\n", alarm);
150 }
151
152 return retval;
153}
154
155static ssize_t
Dmitry Torokhova17ccd12015-07-23 16:01:07 -0700156wakealarm_store(struct device *dev, struct device_attribute *attr,
David Brownellcd966202007-05-08 00:33:40 -0700157 const char *buf, size_t n)
David Brownell3925a5c2007-02-12 00:52:47 -0800158{
159 ssize_t retval;
160 unsigned long now, alarm;
Bernie Thompson1df0a472013-07-03 15:07:03 -0700161 unsigned long push = 0;
David Brownell3925a5c2007-02-12 00:52:47 -0800162 struct rtc_wkalrm alm;
David Brownellab6a2d72007-05-08 00:33:30 -0700163 struct rtc_device *rtc = to_rtc_device(dev);
LABBE Corentin84281c22016-08-12 14:46:14 +0200164 const char *buf_ptr;
Zhao Yakuic116bc22008-04-28 02:11:58 -0700165 int adjust = 0;
David Brownell3925a5c2007-02-12 00:52:47 -0800166
167 /* Only request alarms that trigger in the future. Disable them
168 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
169 */
David Brownellab6a2d72007-05-08 00:33:30 -0700170 retval = rtc_read_time(rtc, &alm.time);
David Brownell3925a5c2007-02-12 00:52:47 -0800171 if (retval < 0)
172 return retval;
173 rtc_tm_to_time(&alm.time, &now);
174
LABBE Corentin84281c22016-08-12 14:46:14 +0200175 buf_ptr = buf;
Zhao Yakuic116bc22008-04-28 02:11:58 -0700176 if (*buf_ptr == '+') {
177 buf_ptr++;
Bernie Thompson1df0a472013-07-03 15:07:03 -0700178 if (*buf_ptr == '=') {
179 buf_ptr++;
180 push = 1;
181 } else
182 adjust = 1;
Zhao Yakuic116bc22008-04-28 02:11:58 -0700183 }
LABBE Corentinf5712872015-12-17 14:11:04 +0100184 retval = kstrtoul(buf_ptr, 0, &alarm);
185 if (retval)
186 return retval;
Zhao Yakuic116bc22008-04-28 02:11:58 -0700187 if (adjust) {
188 alarm += now;
189 }
Bernie Thompson1df0a472013-07-03 15:07:03 -0700190 if (alarm > now || push) {
David Brownell3925a5c2007-02-12 00:52:47 -0800191 /* Avoid accidentally clobbering active alarms; we can't
192 * entirely prevent that here, without even the minimal
193 * locking from the /dev/rtcN api.
194 */
David Brownellab6a2d72007-05-08 00:33:30 -0700195 retval = rtc_read_alarm(rtc, &alm);
David Brownell3925a5c2007-02-12 00:52:47 -0800196 if (retval < 0)
197 return retval;
Bernie Thompson1df0a472013-07-03 15:07:03 -0700198 if (alm.enabled) {
199 if (push) {
200 rtc_tm_to_time(&alm.time, &push);
201 alarm += push;
202 } else
203 return -EBUSY;
204 } else if (push)
205 return -EINVAL;
David Brownell3925a5c2007-02-12 00:52:47 -0800206 alm.enabled = 1;
207 } else {
208 alm.enabled = 0;
209
210 /* Provide a valid future alarm time. Linux isn't EFI,
211 * this time won't be ignored when disabling the alarm.
212 */
213 alarm = now + 300;
214 }
215 rtc_time_to_tm(alarm, &alm.time);
216
David Brownellab6a2d72007-05-08 00:33:30 -0700217 retval = rtc_set_alarm(rtc, &alm);
David Brownell3925a5c2007-02-12 00:52:47 -0800218 return (retval < 0) ? retval : n;
219}
Dmitry Torokhova17ccd12015-07-23 16:01:07 -0700220static DEVICE_ATTR_RW(wakealarm);
David Brownell3925a5c2007-02-12 00:52:47 -0800221
Joshua Clayton5495a412016-02-05 12:41:12 -0800222static ssize_t
223offset_show(struct device *dev, struct device_attribute *attr, char *buf)
224{
225 ssize_t retval;
226 long offset;
227
228 retval = rtc_read_offset(to_rtc_device(dev), &offset);
229 if (retval == 0)
230 retval = sprintf(buf, "%ld\n", offset);
231
232 return retval;
233}
234
235static ssize_t
236offset_store(struct device *dev, struct device_attribute *attr,
237 const char *buf, size_t n)
238{
239 ssize_t retval;
240 long offset;
241
242 retval = kstrtol(buf, 10, &offset);
243 if (retval == 0)
244 retval = rtc_set_offset(to_rtc_device(dev), offset);
245
246 return (retval < 0) ? retval : n;
247}
248static DEVICE_ATTR_RW(offset);
249
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700250static 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,
Joshua Clayton5495a412016-02-05 12:41:12 -0800258 &dev_attr_offset.attr,
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700259 NULL,
260};
David Brownell3925a5c2007-02-12 00:52:47 -0800261
262/* The reason to trigger an alarm with no process watching it (via sysfs)
263 * is its side effect: waking from a system state like suspend-to-RAM or
264 * suspend-to-disk. So: no attribute unless that side effect is possible.
265 * (Userspace may disable that mechanism later.)
266 */
Dmitry Torokhovdf100c02015-07-23 16:01:06 -0700267static bool rtc_does_wakealarm(struct rtc_device *rtc)
David Brownell3925a5c2007-02-12 00:52:47 -0800268{
David Brownellcd966202007-05-08 00:33:40 -0700269 if (!device_can_wakeup(rtc->dev.parent))
Dmitry Torokhovdf100c02015-07-23 16:01:06 -0700270 return false;
271
David Brownell3925a5c2007-02-12 00:52:47 -0800272 return rtc->ops->set_alarm != NULL;
273}
274
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700275static umode_t rtc_attr_is_visible(struct kobject *kobj,
276 struct attribute *attr, int n)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800277{
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700278 struct device *dev = container_of(kobj, struct device, kobj);
279 struct rtc_device *rtc = to_rtc_device(dev);
280 umode_t mode = attr->mode;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800281
Joshua Clayton5495a412016-02-05 12:41:12 -0800282 if (attr == &dev_attr_wakealarm.attr) {
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700283 if (!rtc_does_wakealarm(rtc))
284 mode = 0;
Joshua Clayton5495a412016-02-05 12:41:12 -0800285 } else if (attr == &dev_attr_offset.attr) {
286 if (!rtc->ops->set_offset)
287 mode = 0;
288 }
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800289
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700290 return mode;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800291}
292
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700293static struct attribute_group rtc_attr_group = {
294 .is_visible = rtc_attr_is_visible,
295 .attrs = rtc_attrs,
296};
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800297
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700298static const struct attribute_group *rtc_attr_groups[] = {
299 &rtc_attr_group,
300 NULL
301};
302
303const struct attribute_group **rtc_get_dev_attribute_groups(void)
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800304{
Dmitry Torokhov3ee2c402015-07-23 16:01:08 -0700305 return rtc_attr_groups;
Alessandro Zummoc5c3e192006-03-27 01:16:39 -0800306}