Alexandre Belloni | cdf7545 | 2019-03-13 23:02:48 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 2 | /* |
| 3 | * RTC subsystem, sysfs interface |
| 4 | * |
| 5 | * Copyright (C) 2005 Tower Technologies |
| 6 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
Alexandre Belloni | cdf7545 | 2019-03-13 23:02:48 +0100 | [diff] [blame^] | 7 | */ |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/rtc.h> |
| 11 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 12 | #include "rtc-core.h" |
| 13 | |
| 14 | |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 15 | /* device attributes */ |
| 16 | |
David Brownell | 8a0bdfd7 | 2008-02-06 01:38:45 -0800 | [diff] [blame] | 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 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 24 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 25 | name_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 26 | { |
Alexandre Belloni | 77a73f3 | 2017-06-02 13:57:03 +0200 | [diff] [blame] | 27 | return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent), |
| 28 | dev_name(dev->parent)); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 29 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 30 | static DEVICE_ATTR_RO(name); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 31 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 32 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 33 | date_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 34 | { |
| 35 | ssize_t retval; |
| 36 | struct rtc_time tm; |
| 37 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 38 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
Andy Shevchenko | 5548cbf | 2018-12-04 23:23:12 +0200 | [diff] [blame] | 39 | if (retval) |
| 40 | return retval; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 41 | |
Andy Shevchenko | 5548cbf | 2018-12-04 23:23:12 +0200 | [diff] [blame] | 42 | return sprintf(buf, "%ptRd\n", &tm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 43 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 44 | static DEVICE_ATTR_RO(date); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 45 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 46 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 47 | time_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 48 | { |
| 49 | ssize_t retval; |
| 50 | struct rtc_time tm; |
| 51 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 52 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
Andy Shevchenko | 5548cbf | 2018-12-04 23:23:12 +0200 | [diff] [blame] | 53 | if (retval) |
| 54 | return retval; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 55 | |
Andy Shevchenko | 5548cbf | 2018-12-04 23:23:12 +0200 | [diff] [blame] | 56 | return sprintf(buf, "%ptRt\n", &tm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 57 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 58 | static DEVICE_ATTR_RO(time); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 59 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 60 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 61 | since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 62 | { |
| 63 | ssize_t retval; |
| 64 | struct rtc_time tm; |
| 65 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 66 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 67 | if (retval == 0) { |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 68 | time64_t time; |
| 69 | |
| 70 | time = rtc_tm_to_time64(&tm); |
| 71 | retval = sprintf(buf, "%lld\n", time); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return retval; |
| 75 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 76 | static DEVICE_ATTR_RO(since_epoch); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 77 | |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 78 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 79 | 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] | 80 | { |
| 81 | return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq); |
| 82 | } |
| 83 | |
| 84 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 85 | max_user_freq_store(struct device *dev, struct device_attribute *attr, |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 86 | const char *buf, size_t n) |
| 87 | { |
| 88 | struct rtc_device *rtc = to_rtc_device(dev); |
LABBE Corentin | f571287 | 2015-12-17 14:11:04 +0100 | [diff] [blame] | 89 | unsigned long val; |
| 90 | int err; |
| 91 | |
| 92 | err = kstrtoul(buf, 0, &val); |
| 93 | if (err) |
| 94 | return err; |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 95 | |
| 96 | if (val >= 4096 || val == 0) |
| 97 | return -EINVAL; |
| 98 | |
| 99 | rtc->max_user_freq = (int)val; |
| 100 | |
| 101 | return n; |
| 102 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 103 | static DEVICE_ATTR_RW(max_user_freq); |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 104 | |
David Fries | 4c24e29 | 2012-10-04 17:14:12 -0700 | [diff] [blame] | 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 | */ |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 111 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 112 | hctosys_show(struct device *dev, struct device_attribute *attr, char *buf) |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 113 | { |
| 114 | #ifdef CONFIG_RTC_HCTOSYS_DEVICE |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 115 | if (rtc_hctosys_ret == 0 && |
| 116 | strcmp(dev_name(&to_rtc_device(dev)->dev), |
| 117 | CONFIG_RTC_HCTOSYS_DEVICE) == 0) |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 118 | return sprintf(buf, "1\n"); |
| 119 | else |
| 120 | #endif |
| 121 | return sprintf(buf, "0\n"); |
| 122 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 123 | static DEVICE_ATTR_RO(hctosys); |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 124 | |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 125 | static ssize_t |
Dmitry Torokhov | a17ccd1 | 2015-07-23 16:01:07 -0700 | [diff] [blame] | 126 | wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 127 | { |
| 128 | ssize_t retval; |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 129 | time64_t alarm; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 130 | struct rtc_wkalrm alm; |
| 131 | |
David Brownell | 8a0bdfd7 | 2008-02-06 01:38:45 -0800 | [diff] [blame] | 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. |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 135 | * |
David Brownell | 8a0bdfd7 | 2008-02-06 01:38:45 -0800 | [diff] [blame] | 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. |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 139 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 140 | retval = rtc_read_alarm(to_rtc_device(dev), &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 141 | if (retval == 0 && alm.enabled) { |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 142 | alarm = rtc_tm_to_time64(&alm.time); |
| 143 | retval = sprintf(buf, "%lld\n", alarm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return retval; |
| 147 | } |
| 148 | |
| 149 | static ssize_t |
Dmitry Torokhov | a17ccd1 | 2015-07-23 16:01:07 -0700 | [diff] [blame] | 150 | wakealarm_store(struct device *dev, struct device_attribute *attr, |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 151 | const char *buf, size_t n) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 152 | { |
| 153 | ssize_t retval; |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 154 | time64_t now, alarm; |
| 155 | time64_t push = 0; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 156 | struct rtc_wkalrm alm; |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 157 | struct rtc_device *rtc = to_rtc_device(dev); |
LABBE Corentin | 84281c2 | 2016-08-12 14:46:14 +0200 | [diff] [blame] | 158 | const char *buf_ptr; |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 159 | int adjust = 0; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 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 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 164 | retval = rtc_read_time(rtc, &alm.time); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 165 | if (retval < 0) |
| 166 | return retval; |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 167 | now = rtc_tm_to_time64(&alm.time); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 168 | |
LABBE Corentin | 84281c2 | 2016-08-12 14:46:14 +0200 | [diff] [blame] | 169 | buf_ptr = buf; |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 170 | if (*buf_ptr == '+') { |
| 171 | buf_ptr++; |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 172 | if (*buf_ptr == '=') { |
| 173 | buf_ptr++; |
| 174 | push = 1; |
| 175 | } else |
| 176 | adjust = 1; |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 177 | } |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 178 | retval = kstrtos64(buf_ptr, 0, &alarm); |
LABBE Corentin | f571287 | 2015-12-17 14:11:04 +0100 | [diff] [blame] | 179 | if (retval) |
| 180 | return retval; |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 181 | if (adjust) { |
| 182 | alarm += now; |
| 183 | } |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 184 | if (alarm > now || push) { |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 185 | /* Avoid accidentally clobbering active alarms; we can't |
| 186 | * entirely prevent that here, without even the minimal |
| 187 | * locking from the /dev/rtcN api. |
| 188 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 189 | retval = rtc_read_alarm(rtc, &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 190 | if (retval < 0) |
| 191 | return retval; |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 192 | if (alm.enabled) { |
| 193 | if (push) { |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 194 | push = rtc_tm_to_time64(&alm.time); |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 195 | alarm += push; |
| 196 | } else |
| 197 | return -EBUSY; |
| 198 | } else if (push) |
| 199 | return -EINVAL; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 200 | alm.enabled = 1; |
| 201 | } else { |
| 202 | alm.enabled = 0; |
| 203 | |
| 204 | /* Provide a valid future alarm time. Linux isn't EFI, |
| 205 | * this time won't be ignored when disabling the alarm. |
| 206 | */ |
| 207 | alarm = now + 300; |
| 208 | } |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 209 | rtc_time64_to_tm(alarm, &alm.time); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 210 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 211 | retval = rtc_set_alarm(rtc, &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 212 | return (retval < 0) ? retval : n; |
| 213 | } |
Dmitry Torokhov | a17ccd1 | 2015-07-23 16:01:07 -0700 | [diff] [blame] | 214 | static DEVICE_ATTR_RW(wakealarm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 215 | |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 216 | static ssize_t |
| 217 | offset_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 218 | { |
| 219 | ssize_t retval; |
| 220 | long offset; |
| 221 | |
| 222 | retval = rtc_read_offset(to_rtc_device(dev), &offset); |
| 223 | if (retval == 0) |
| 224 | retval = sprintf(buf, "%ld\n", offset); |
| 225 | |
| 226 | return retval; |
| 227 | } |
| 228 | |
| 229 | static ssize_t |
| 230 | offset_store(struct device *dev, struct device_attribute *attr, |
| 231 | const char *buf, size_t n) |
| 232 | { |
| 233 | ssize_t retval; |
| 234 | long offset; |
| 235 | |
| 236 | retval = kstrtol(buf, 10, &offset); |
| 237 | if (retval == 0) |
| 238 | retval = rtc_set_offset(to_rtc_device(dev), offset); |
| 239 | |
| 240 | return (retval < 0) ? retval : n; |
| 241 | } |
| 242 | static DEVICE_ATTR_RW(offset); |
| 243 | |
Alexandre Belloni | 71db049 | 2018-02-17 14:58:40 +0100 | [diff] [blame] | 244 | static ssize_t |
| 245 | range_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 246 | { |
| 247 | return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min, |
| 248 | to_rtc_device(dev)->range_max); |
| 249 | } |
| 250 | static DEVICE_ATTR_RO(range); |
| 251 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 252 | static struct attribute *rtc_attrs[] = { |
| 253 | &dev_attr_name.attr, |
| 254 | &dev_attr_date.attr, |
| 255 | &dev_attr_time.attr, |
| 256 | &dev_attr_since_epoch.attr, |
| 257 | &dev_attr_max_user_freq.attr, |
| 258 | &dev_attr_hctosys.attr, |
| 259 | &dev_attr_wakealarm.attr, |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 260 | &dev_attr_offset.attr, |
Alexandre Belloni | 71db049 | 2018-02-17 14:58:40 +0100 | [diff] [blame] | 261 | &dev_attr_range.attr, |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 262 | NULL, |
| 263 | }; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 264 | |
| 265 | /* The reason to trigger an alarm with no process watching it (via sysfs) |
| 266 | * is its side effect: waking from a system state like suspend-to-RAM or |
| 267 | * suspend-to-disk. So: no attribute unless that side effect is possible. |
| 268 | * (Userspace may disable that mechanism later.) |
| 269 | */ |
Dmitry Torokhov | df100c0 | 2015-07-23 16:01:06 -0700 | [diff] [blame] | 270 | static bool rtc_does_wakealarm(struct rtc_device *rtc) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 271 | { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 272 | if (!device_can_wakeup(rtc->dev.parent)) |
Dmitry Torokhov | df100c0 | 2015-07-23 16:01:06 -0700 | [diff] [blame] | 273 | return false; |
| 274 | |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 275 | return rtc->ops->set_alarm != NULL; |
| 276 | } |
| 277 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 278 | static umode_t rtc_attr_is_visible(struct kobject *kobj, |
| 279 | struct attribute *attr, int n) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 280 | { |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 281 | struct device *dev = container_of(kobj, struct device, kobj); |
| 282 | struct rtc_device *rtc = to_rtc_device(dev); |
| 283 | umode_t mode = attr->mode; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 284 | |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 285 | if (attr == &dev_attr_wakealarm.attr) { |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 286 | if (!rtc_does_wakealarm(rtc)) |
| 287 | mode = 0; |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 288 | } else if (attr == &dev_attr_offset.attr) { |
| 289 | if (!rtc->ops->set_offset) |
| 290 | mode = 0; |
Alexandre Belloni | 71db049 | 2018-02-17 14:58:40 +0100 | [diff] [blame] | 291 | } else if (attr == &dev_attr_range.attr) { |
| 292 | if (!(rtc->range_max - rtc->range_min)) |
| 293 | mode = 0; |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 294 | } |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 295 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 296 | return mode; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 299 | static struct attribute_group rtc_attr_group = { |
| 300 | .is_visible = rtc_attr_is_visible, |
| 301 | .attrs = rtc_attrs, |
| 302 | }; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 303 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 304 | static const struct attribute_group *rtc_attr_groups[] = { |
| 305 | &rtc_attr_group, |
| 306 | NULL |
| 307 | }; |
| 308 | |
| 309 | const struct attribute_group **rtc_get_dev_attribute_groups(void) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 310 | { |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 311 | return rtc_attr_groups; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 312 | } |
Denis Osterland | a0a1a1b | 2018-07-24 11:31:22 +0000 | [diff] [blame] | 313 | |
| 314 | int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps) |
| 315 | { |
| 316 | size_t old_cnt = 0, add_cnt = 0, new_cnt; |
| 317 | const struct attribute_group **groups, **old; |
| 318 | |
| 319 | if (rtc->registered) |
| 320 | return -EINVAL; |
| 321 | if (!grps) |
| 322 | return -EINVAL; |
| 323 | |
| 324 | groups = rtc->dev.groups; |
| 325 | if (groups) |
| 326 | for (; *groups; groups++) |
| 327 | old_cnt++; |
| 328 | |
| 329 | for (groups = grps; *groups; groups++) |
| 330 | add_cnt++; |
| 331 | |
| 332 | new_cnt = old_cnt + add_cnt + 1; |
| 333 | groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL); |
Dan Carpenter | 777d8ae | 2018-08-27 12:22:34 +0300 | [diff] [blame] | 334 | if (!groups) |
| 335 | return -ENOMEM; |
Denis Osterland | a0a1a1b | 2018-07-24 11:31:22 +0000 | [diff] [blame] | 336 | memcpy(groups, rtc->dev.groups, old_cnt * sizeof(*groups)); |
| 337 | memcpy(groups + old_cnt, grps, add_cnt * sizeof(*groups)); |
| 338 | groups[old_cnt + add_cnt] = NULL; |
| 339 | |
| 340 | old = rtc->dev.groups; |
| 341 | rtc->dev.groups = groups; |
| 342 | if (old && old != rtc_attr_groups) |
| 343 | devm_kfree(&rtc->dev, old); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | EXPORT_SYMBOL(rtc_add_groups); |
| 348 | |
| 349 | int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp) |
| 350 | { |
| 351 | const struct attribute_group *groups[] = { grp, NULL }; |
| 352 | |
| 353 | return rtc_add_groups(rtc, groups); |
| 354 | } |
| 355 | EXPORT_SYMBOL(rtc_add_group); |