1 /*
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 
15 #include "rtc-core.h"
16 
17 
18 /* device attributes */
19 
20 /*
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 
27 static ssize_t
name_show(struct device * dev,struct device_attribute * attr,char * buf)28 name_show(struct device *dev, struct device_attribute *attr, char *buf)
29 {
30 	return sprintf(buf, "%s\n", to_rtc_device(dev)->name);
31 }
32 static DEVICE_ATTR_RO(name);
33 
34 static ssize_t
date_show(struct device * dev,struct device_attribute * attr,char * buf)35 date_show(struct device *dev, struct device_attribute *attr, char *buf)
36 {
37 	ssize_t retval;
38 	struct rtc_time tm;
39 
40 	retval = rtc_read_time(to_rtc_device(dev), &tm);
41 	if (retval == 0) {
42 		retval = sprintf(buf, "%04d-%02d-%02d\n",
43 			tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
44 	}
45 
46 	return retval;
47 }
48 static DEVICE_ATTR_RO(date);
49 
50 static ssize_t
time_show(struct device * dev,struct device_attribute * attr,char * buf)51 time_show(struct device *dev, struct device_attribute *attr, char *buf)
52 {
53 	ssize_t retval;
54 	struct rtc_time tm;
55 
56 	retval = rtc_read_time(to_rtc_device(dev), &tm);
57 	if (retval == 0) {
58 		retval = sprintf(buf, "%02d:%02d:%02d\n",
59 			tm.tm_hour, tm.tm_min, tm.tm_sec);
60 	}
61 
62 	return retval;
63 }
64 static DEVICE_ATTR_RO(time);
65 
66 static ssize_t
since_epoch_show(struct device * dev,struct device_attribute * attr,char * buf)67 since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
68 {
69 	ssize_t retval;
70 	struct rtc_time tm;
71 
72 	retval = rtc_read_time(to_rtc_device(dev), &tm);
73 	if (retval == 0) {
74 		unsigned long time;
75 		rtc_tm_to_time(&tm, &time);
76 		retval = sprintf(buf, "%lu\n", time);
77 	}
78 
79 	return retval;
80 }
81 static DEVICE_ATTR_RO(since_epoch);
82 
83 static ssize_t
max_user_freq_show(struct device * dev,struct device_attribute * attr,char * buf)84 max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
85 {
86 	return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
87 }
88 
89 static ssize_t
max_user_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)90 max_user_freq_store(struct device *dev, struct device_attribute *attr,
91 		const char *buf, size_t n)
92 {
93 	struct rtc_device *rtc = to_rtc_device(dev);
94 	unsigned long val = simple_strtoul(buf, NULL, 0);
95 
96 	if (val >= 4096 || val == 0)
97 		return -EINVAL;
98 
99 	rtc->max_user_freq = (int)val;
100 
101 	return n;
102 }
103 static DEVICE_ATTR_RW(max_user_freq);
104 
105 /**
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  */
111 static ssize_t
hctosys_show(struct device * dev,struct device_attribute * attr,char * buf)112 hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
113 {
114 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
115 	if (rtc_hctosys_ret == 0 &&
116 			strcmp(dev_name(&to_rtc_device(dev)->dev),
117 				CONFIG_RTC_HCTOSYS_DEVICE) == 0)
118 		return sprintf(buf, "1\n");
119 	else
120 #endif
121 		return sprintf(buf, "0\n");
122 }
123 static DEVICE_ATTR_RO(hctosys);
124 
125 static ssize_t
wakealarm_show(struct device * dev,struct device_attribute * attr,char * buf)126 wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
127 {
128 	ssize_t retval;
129 	unsigned long alarm;
130 	struct rtc_wkalrm alm;
131 
132 	/* 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.
135 	 *
136 	 * 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.
139 	 */
140 	retval = rtc_read_alarm(to_rtc_device(dev), &alm);
141 	if (retval == 0 && alm.enabled) {
142 		rtc_tm_to_time(&alm.time, &alarm);
143 		retval = sprintf(buf, "%lu\n", alarm);
144 	}
145 
146 	return retval;
147 }
148 
149 static ssize_t
wakealarm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)150 wakealarm_store(struct device *dev, struct device_attribute *attr,
151 		const char *buf, size_t n)
152 {
153 	ssize_t retval;
154 	unsigned long now, alarm;
155 	unsigned long push = 0;
156 	struct rtc_wkalrm alm;
157 	struct rtc_device *rtc = to_rtc_device(dev);
158 	char *buf_ptr;
159 	int adjust = 0;
160 
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 	 */
164 	retval = rtc_read_time(rtc, &alm.time);
165 	if (retval < 0)
166 		return retval;
167 	rtc_tm_to_time(&alm.time, &now);
168 
169 	buf_ptr = (char *)buf;
170 	if (*buf_ptr == '+') {
171 		buf_ptr++;
172 		if (*buf_ptr == '=') {
173 			buf_ptr++;
174 			push = 1;
175 		} else
176 			adjust = 1;
177 	}
178 	alarm = simple_strtoul(buf_ptr, NULL, 0);
179 	if (adjust) {
180 		alarm += now;
181 	}
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 				rtc_tm_to_time(&alm.time, &push);
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_time_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 struct attribute *rtc_attrs[] = {
215 	&dev_attr_name.attr,
216 	&dev_attr_date.attr,
217 	&dev_attr_time.attr,
218 	&dev_attr_since_epoch.attr,
219 	&dev_attr_max_user_freq.attr,
220 	&dev_attr_hctosys.attr,
221 	&dev_attr_wakealarm.attr,
222 	NULL,
223 };
224 
225 /* The reason to trigger an alarm with no process watching it (via sysfs)
226  * is its side effect:  waking from a system state like suspend-to-RAM or
227  * suspend-to-disk.  So: no attribute unless that side effect is possible.
228  * (Userspace may disable that mechanism later.)
229  */
rtc_does_wakealarm(struct rtc_device * rtc)230 static bool rtc_does_wakealarm(struct rtc_device *rtc)
231 {
232 	if (!device_can_wakeup(rtc->dev.parent))
233 		return false;
234 
235 	return rtc->ops->set_alarm != NULL;
236 }
237 
rtc_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)238 static umode_t rtc_attr_is_visible(struct kobject *kobj,
239 				   struct attribute *attr, int n)
240 {
241 	struct device *dev = container_of(kobj, struct device, kobj);
242 	struct rtc_device *rtc = to_rtc_device(dev);
243 	umode_t mode = attr->mode;
244 
245 	if (attr == &dev_attr_wakealarm.attr)
246 		if (!rtc_does_wakealarm(rtc))
247 			mode = 0;
248 
249 	return mode;
250 }
251 
252 static struct attribute_group rtc_attr_group = {
253 	.is_visible	= rtc_attr_is_visible,
254 	.attrs		= rtc_attrs,
255 };
256 
257 static const struct attribute_group *rtc_attr_groups[] = {
258 	&rtc_attr_group,
259 	NULL
260 };
261 
rtc_get_dev_attribute_groups(void)262 const struct attribute_group **rtc_get_dev_attribute_groups(void)
263 {
264 	return rtc_attr_groups;
265 }
266