Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 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 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 15 | #include "rtc-core.h" |
| 16 | |
| 17 | |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 18 | /* device attributes */ |
| 19 | |
David Brownell | 8a0bdfd7 | 2008-02-06 01:38:45 -0800 | [diff] [blame] | 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 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 27 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 28 | name_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 29 | { |
Alexandre Belloni | 77a73f3 | 2017-06-02 13:57:03 +0200 | [diff] [blame^] | 30 | return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent), |
| 31 | dev_name(dev->parent)); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 32 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 33 | static DEVICE_ATTR_RO(name); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 34 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 35 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 36 | date_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 37 | { |
| 38 | ssize_t retval; |
| 39 | struct rtc_time tm; |
| 40 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 41 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 42 | 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-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 49 | static DEVICE_ATTR_RO(date); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 50 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 51 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 52 | time_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 53 | { |
| 54 | ssize_t retval; |
| 55 | struct rtc_time tm; |
| 56 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 57 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 58 | 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-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 65 | static DEVICE_ATTR_RO(time); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 66 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 67 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 68 | since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 69 | { |
| 70 | ssize_t retval; |
| 71 | struct rtc_time tm; |
| 72 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 73 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 74 | 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-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 82 | static DEVICE_ATTR_RO(since_epoch); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 83 | |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 84 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 85 | max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf) |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 86 | { |
| 87 | return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq); |
| 88 | } |
| 89 | |
| 90 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 91 | max_user_freq_store(struct device *dev, struct device_attribute *attr, |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 92 | const char *buf, size_t n) |
| 93 | { |
| 94 | struct rtc_device *rtc = to_rtc_device(dev); |
LABBE Corentin | f571287 | 2015-12-17 14:11:04 +0100 | [diff] [blame] | 95 | unsigned long val; |
| 96 | int err; |
| 97 | |
| 98 | err = kstrtoul(buf, 0, &val); |
| 99 | if (err) |
| 100 | return err; |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 101 | |
| 102 | if (val >= 4096 || val == 0) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | rtc->max_user_freq = (int)val; |
| 106 | |
| 107 | return n; |
| 108 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 109 | static DEVICE_ATTR_RW(max_user_freq); |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 110 | |
David Fries | 4c24e29 | 2012-10-04 17:14:12 -0700 | [diff] [blame] | 111 | /** |
| 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 Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 117 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 118 | hctosys_show(struct device *dev, struct device_attribute *attr, char *buf) |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 119 | { |
| 120 | #ifdef CONFIG_RTC_HCTOSYS_DEVICE |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 121 | if (rtc_hctosys_ret == 0 && |
| 122 | strcmp(dev_name(&to_rtc_device(dev)->dev), |
| 123 | CONFIG_RTC_HCTOSYS_DEVICE) == 0) |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 124 | return sprintf(buf, "1\n"); |
| 125 | else |
| 126 | #endif |
| 127 | return sprintf(buf, "0\n"); |
| 128 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 129 | static DEVICE_ATTR_RO(hctosys); |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 130 | |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 131 | static ssize_t |
Dmitry Torokhov | a17ccd1 | 2015-07-23 16:01:07 -0700 | [diff] [blame] | 132 | wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 133 | { |
| 134 | ssize_t retval; |
| 135 | unsigned long alarm; |
| 136 | struct rtc_wkalrm alm; |
| 137 | |
David Brownell | 8a0bdfd7 | 2008-02-06 01:38:45 -0800 | [diff] [blame] | 138 | /* 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 Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 141 | * |
David Brownell | 8a0bdfd7 | 2008-02-06 01:38:45 -0800 | [diff] [blame] | 142 | * 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 Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 145 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 146 | retval = rtc_read_alarm(to_rtc_device(dev), &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 147 | 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 | |
| 155 | static ssize_t |
Dmitry Torokhov | a17ccd1 | 2015-07-23 16:01:07 -0700 | [diff] [blame] | 156 | wakealarm_store(struct device *dev, struct device_attribute *attr, |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 157 | const char *buf, size_t n) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 158 | { |
| 159 | ssize_t retval; |
| 160 | unsigned long now, alarm; |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 161 | unsigned long push = 0; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 162 | struct rtc_wkalrm alm; |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 163 | struct rtc_device *rtc = to_rtc_device(dev); |
LABBE Corentin | 84281c2 | 2016-08-12 14:46:14 +0200 | [diff] [blame] | 164 | const char *buf_ptr; |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 165 | int adjust = 0; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 166 | |
| 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 Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 170 | retval = rtc_read_time(rtc, &alm.time); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 171 | if (retval < 0) |
| 172 | return retval; |
| 173 | rtc_tm_to_time(&alm.time, &now); |
| 174 | |
LABBE Corentin | 84281c2 | 2016-08-12 14:46:14 +0200 | [diff] [blame] | 175 | buf_ptr = buf; |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 176 | if (*buf_ptr == '+') { |
| 177 | buf_ptr++; |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 178 | if (*buf_ptr == '=') { |
| 179 | buf_ptr++; |
| 180 | push = 1; |
| 181 | } else |
| 182 | adjust = 1; |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 183 | } |
LABBE Corentin | f571287 | 2015-12-17 14:11:04 +0100 | [diff] [blame] | 184 | retval = kstrtoul(buf_ptr, 0, &alarm); |
| 185 | if (retval) |
| 186 | return retval; |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 187 | if (adjust) { |
| 188 | alarm += now; |
| 189 | } |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 190 | if (alarm > now || push) { |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 191 | /* 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 Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 195 | retval = rtc_read_alarm(rtc, &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 196 | if (retval < 0) |
| 197 | return retval; |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 198 | 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 Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 206 | 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 Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 217 | retval = rtc_set_alarm(rtc, &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 218 | return (retval < 0) ? retval : n; |
| 219 | } |
Dmitry Torokhov | a17ccd1 | 2015-07-23 16:01:07 -0700 | [diff] [blame] | 220 | static DEVICE_ATTR_RW(wakealarm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 221 | |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 222 | static ssize_t |
| 223 | offset_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 | |
| 235 | static ssize_t |
| 236 | offset_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 | } |
| 248 | static DEVICE_ATTR_RW(offset); |
| 249 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 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, |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 258 | &dev_attr_offset.attr, |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 259 | NULL, |
| 260 | }; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 261 | |
| 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 Torokhov | df100c0 | 2015-07-23 16:01:06 -0700 | [diff] [blame] | 267 | static bool rtc_does_wakealarm(struct rtc_device *rtc) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 268 | { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 269 | if (!device_can_wakeup(rtc->dev.parent)) |
Dmitry Torokhov | df100c0 | 2015-07-23 16:01:06 -0700 | [diff] [blame] | 270 | return false; |
| 271 | |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 272 | return rtc->ops->set_alarm != NULL; |
| 273 | } |
| 274 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 275 | static umode_t rtc_attr_is_visible(struct kobject *kobj, |
| 276 | struct attribute *attr, int n) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 277 | { |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 278 | 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 Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 281 | |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 282 | if (attr == &dev_attr_wakealarm.attr) { |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 283 | if (!rtc_does_wakealarm(rtc)) |
| 284 | mode = 0; |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 285 | } else if (attr == &dev_attr_offset.attr) { |
| 286 | if (!rtc->ops->set_offset) |
| 287 | mode = 0; |
| 288 | } |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 289 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 290 | return mode; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 293 | static struct attribute_group rtc_attr_group = { |
| 294 | .is_visible = rtc_attr_is_visible, |
| 295 | .attrs = rtc_attrs, |
| 296 | }; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 297 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 298 | static const struct attribute_group *rtc_attr_groups[] = { |
| 299 | &rtc_attr_group, |
| 300 | NULL |
| 301 | }; |
| 302 | |
| 303 | const struct attribute_group **rtc_get_dev_attribute_groups(void) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 304 | { |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 305 | return rtc_attr_groups; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 306 | } |