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 | |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 14 | /* device attributes */ |
| 15 | |
David Brownell | 8a0bdfd | 2008-02-06 01:38:45 -0800 | [diff] [blame] | 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 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 23 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 24 | name_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 25 | { |
Alexandre Belloni | 77a73f3 | 2017-06-02 13:57:03 +0200 | [diff] [blame] | 26 | return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent), |
| 27 | dev_name(dev->parent)); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 28 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 29 | static DEVICE_ATTR_RO(name); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 30 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 31 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 32 | date_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 33 | { |
| 34 | ssize_t retval; |
| 35 | struct rtc_time tm; |
| 36 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 37 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
Andy Shevchenko | 5548cbf | 2018-12-04 23:23:12 +0200 | [diff] [blame] | 38 | if (retval) |
| 39 | return retval; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 40 | |
Andy Shevchenko | 5548cbf | 2018-12-04 23:23:12 +0200 | [diff] [blame] | 41 | return sprintf(buf, "%ptRd\n", &tm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 42 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 43 | static DEVICE_ATTR_RO(date); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 44 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 45 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 46 | time_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 47 | { |
| 48 | ssize_t retval; |
| 49 | struct rtc_time tm; |
| 50 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 51 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
Andy Shevchenko | 5548cbf | 2018-12-04 23:23:12 +0200 | [diff] [blame] | 52 | if (retval) |
| 53 | return retval; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 54 | |
Andy Shevchenko | 5548cbf | 2018-12-04 23:23:12 +0200 | [diff] [blame] | 55 | return sprintf(buf, "%ptRt\n", &tm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 56 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 57 | static DEVICE_ATTR_RO(time); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 58 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 59 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 60 | since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 61 | { |
| 62 | ssize_t retval; |
| 63 | struct rtc_time tm; |
| 64 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 65 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 66 | if (retval == 0) { |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 67 | time64_t time; |
| 68 | |
| 69 | time = rtc_tm_to_time64(&tm); |
| 70 | retval = sprintf(buf, "%lld\n", time); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return retval; |
| 74 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 75 | static DEVICE_ATTR_RO(since_epoch); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 76 | |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 77 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 78 | 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] | 79 | { |
| 80 | return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq); |
| 81 | } |
| 82 | |
| 83 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 84 | max_user_freq_store(struct device *dev, struct device_attribute *attr, |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 85 | const char *buf, size_t n) |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 86 | { |
| 87 | struct rtc_device *rtc = to_rtc_device(dev); |
LABBE Corentin | f571287 | 2015-12-17 14:11:04 +0100 | [diff] [blame] | 88 | unsigned long val; |
| 89 | int err; |
| 90 | |
| 91 | err = kstrtoul(buf, 0, &val); |
| 92 | if (err) |
| 93 | return err; |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 94 | |
| 95 | if (val >= 4096 || val == 0) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | rtc->max_user_freq = (int)val; |
| 99 | |
| 100 | return n; |
| 101 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 102 | static DEVICE_ATTR_RW(max_user_freq); |
Bryan Kadzban | 06c65eb | 2007-10-16 01:28:22 -0700 | [diff] [blame] | 103 | |
David Fries | 4c24e29 | 2012-10-04 17:14:12 -0700 | [diff] [blame] | 104 | /** |
| 105 | * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time |
Alexandre Belloni | 6f69319 | 2019-11-22 11:22:05 +0100 | [diff] [blame] | 106 | * @dev: The device that the attribute belongs to. |
| 107 | * @attr: The attribute being read. |
| 108 | * @buf: The result buffer. |
David Fries | 4c24e29 | 2012-10-04 17:14:12 -0700 | [diff] [blame] | 109 | * |
Alexandre Belloni | 6f69319 | 2019-11-22 11:22:05 +0100 | [diff] [blame] | 110 | * buf is "1" if the system clock was set by this RTC at the last |
David Fries | 4c24e29 | 2012-10-04 17:14:12 -0700 | [diff] [blame] | 111 | * boot or resume event. |
| 112 | */ |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 113 | static ssize_t |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 114 | hctosys_show(struct device *dev, struct device_attribute *attr, char *buf) |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 115 | { |
| 116 | #ifdef CONFIG_RTC_HCTOSYS_DEVICE |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 117 | if (rtc_hctosys_ret == 0 && |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 118 | strcmp(dev_name(&to_rtc_device(dev)->dev), |
| 119 | CONFIG_RTC_HCTOSYS_DEVICE) == 0) |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 120 | return sprintf(buf, "1\n"); |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 121 | #endif |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 122 | return sprintf(buf, "0\n"); |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 123 | } |
Greg Kroah-Hartman | f21e683 | 2013-07-24 15:05:22 -0700 | [diff] [blame] | 124 | static DEVICE_ATTR_RO(hctosys); |
Matthew Garrett | d8c1acb | 2009-09-22 16:46:32 -0700 | [diff] [blame] | 125 | |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 126 | static ssize_t |
Dmitry Torokhov | a17ccd1 | 2015-07-23 16:01:07 -0700 | [diff] [blame] | 127 | wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 128 | { |
| 129 | ssize_t retval; |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 130 | time64_t alarm; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 131 | struct rtc_wkalrm alm; |
| 132 | |
David Brownell | 8a0bdfd | 2008-02-06 01:38:45 -0800 | [diff] [blame] | 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. |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 136 | * |
David Brownell | 8a0bdfd | 2008-02-06 01:38:45 -0800 | [diff] [blame] | 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. |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 140 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 141 | retval = rtc_read_alarm(to_rtc_device(dev), &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 142 | if (retval == 0 && alm.enabled) { |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 143 | alarm = rtc_tm_to_time64(&alm.time); |
| 144 | retval = sprintf(buf, "%lld\n", alarm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return retval; |
| 148 | } |
| 149 | |
| 150 | static ssize_t |
Dmitry Torokhov | a17ccd1 | 2015-07-23 16:01:07 -0700 | [diff] [blame] | 151 | wakealarm_store(struct device *dev, struct device_attribute *attr, |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 152 | const char *buf, size_t n) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 153 | { |
| 154 | ssize_t retval; |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 155 | time64_t now, alarm; |
| 156 | time64_t push = 0; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 157 | struct rtc_wkalrm alm; |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 158 | struct rtc_device *rtc = to_rtc_device(dev); |
LABBE Corentin | 84281c2 | 2016-08-12 14:46:14 +0200 | [diff] [blame] | 159 | const char *buf_ptr; |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 160 | int adjust = 0; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 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 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 165 | retval = rtc_read_time(rtc, &alm.time); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 166 | if (retval < 0) |
| 167 | return retval; |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 168 | now = rtc_tm_to_time64(&alm.time); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 169 | |
LABBE Corentin | 84281c2 | 2016-08-12 14:46:14 +0200 | [diff] [blame] | 170 | buf_ptr = buf; |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 171 | if (*buf_ptr == '+') { |
| 172 | buf_ptr++; |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 173 | if (*buf_ptr == '=') { |
| 174 | buf_ptr++; |
| 175 | push = 1; |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 176 | } else { |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 177 | adjust = 1; |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 178 | } |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 179 | } |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 180 | retval = kstrtos64(buf_ptr, 0, &alarm); |
LABBE Corentin | f571287 | 2015-12-17 14:11:04 +0100 | [diff] [blame] | 181 | if (retval) |
| 182 | return retval; |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 183 | if (adjust) |
Zhao Yakui | c116bc2 | 2008-04-28 02:11:58 -0700 | [diff] [blame] | 184 | alarm += now; |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 185 | if (alarm > now || push) { |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 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 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 190 | retval = rtc_read_alarm(rtc, &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 191 | if (retval < 0) |
| 192 | return retval; |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 193 | if (alm.enabled) { |
| 194 | if (push) { |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 195 | push = rtc_tm_to_time64(&alm.time); |
Bernie Thompson | 1df0a47 | 2013-07-03 15:07:03 -0700 | [diff] [blame] | 196 | alarm += push; |
| 197 | } else |
| 198 | return -EBUSY; |
| 199 | } else if (push) |
| 200 | return -EINVAL; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 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 | } |
Baolin Wang | 9a06da2 | 2017-11-09 15:09:20 +0800 | [diff] [blame] | 210 | rtc_time64_to_tm(alarm, &alm.time); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 211 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 212 | retval = rtc_set_alarm(rtc, &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 213 | return (retval < 0) ? retval : n; |
| 214 | } |
Dmitry Torokhov | a17ccd1 | 2015-07-23 16:01:07 -0700 | [diff] [blame] | 215 | static DEVICE_ATTR_RW(wakealarm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 216 | |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 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 | |
Alexandre Belloni | 71db049 | 2018-02-17 14:58:40 +0100 | [diff] [blame] | 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 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 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, |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 261 | &dev_attr_offset.attr, |
Alexandre Belloni | 71db049 | 2018-02-17 14:58:40 +0100 | [diff] [blame] | 262 | &dev_attr_range.attr, |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 263 | NULL, |
| 264 | }; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 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 | */ |
Dmitry Torokhov | df100c0 | 2015-07-23 16:01:06 -0700 | [diff] [blame] | 271 | static bool rtc_does_wakealarm(struct rtc_device *rtc) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 272 | { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 273 | if (!device_can_wakeup(rtc->dev.parent)) |
Dmitry Torokhov | df100c0 | 2015-07-23 16:01:06 -0700 | [diff] [blame] | 274 | return false; |
| 275 | |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 276 | return rtc->ops->set_alarm != NULL; |
| 277 | } |
| 278 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 279 | static umode_t rtc_attr_is_visible(struct kobject *kobj, |
| 280 | struct attribute *attr, int n) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 281 | { |
suguosong | ae243ef | 2020-02-25 10:19:23 +0800 | [diff] [blame] | 282 | struct device *dev = kobj_to_dev(kobj); |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 283 | struct rtc_device *rtc = to_rtc_device(dev); |
| 284 | umode_t mode = attr->mode; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 285 | |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 286 | if (attr == &dev_attr_wakealarm.attr) { |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 287 | if (!rtc_does_wakealarm(rtc)) |
| 288 | mode = 0; |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 289 | } else if (attr == &dev_attr_offset.attr) { |
| 290 | if (!rtc->ops->set_offset) |
| 291 | mode = 0; |
Alexandre Belloni | 71db049 | 2018-02-17 14:58:40 +0100 | [diff] [blame] | 292 | } else if (attr == &dev_attr_range.attr) { |
| 293 | if (!(rtc->range_max - rtc->range_min)) |
| 294 | mode = 0; |
Joshua Clayton | 5495a41 | 2016-02-05 12:41:12 -0800 | [diff] [blame] | 295 | } |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 296 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 297 | return mode; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 300 | static struct attribute_group rtc_attr_group = { |
| 301 | .is_visible = rtc_attr_is_visible, |
| 302 | .attrs = rtc_attrs, |
| 303 | }; |
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 | static const struct attribute_group *rtc_attr_groups[] = { |
| 306 | &rtc_attr_group, |
| 307 | NULL |
| 308 | }; |
| 309 | |
| 310 | const struct attribute_group **rtc_get_dev_attribute_groups(void) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 311 | { |
Dmitry Torokhov | 3ee2c40 | 2015-07-23 16:01:08 -0700 | [diff] [blame] | 312 | return rtc_attr_groups; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 313 | } |
Denis Osterland | a0a1a1b | 2018-07-24 11:31:22 +0000 | [diff] [blame] | 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 | |
| 320 | if (rtc->registered) |
| 321 | return -EINVAL; |
| 322 | if (!grps) |
| 323 | return -EINVAL; |
| 324 | |
| 325 | groups = rtc->dev.groups; |
| 326 | if (groups) |
| 327 | for (; *groups; groups++) |
| 328 | old_cnt++; |
| 329 | |
| 330 | for (groups = grps; *groups; groups++) |
| 331 | add_cnt++; |
| 332 | |
| 333 | new_cnt = old_cnt + add_cnt + 1; |
| 334 | groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL); |
Dan Carpenter | 777d8ae | 2018-08-27 12:22:34 +0300 | [diff] [blame] | 335 | if (!groups) |
| 336 | return -ENOMEM; |
Denis Osterland | a0a1a1b | 2018-07-24 11:31:22 +0000 | [diff] [blame] | 337 | memcpy(groups, rtc->dev.groups, old_cnt * sizeof(*groups)); |
| 338 | memcpy(groups + old_cnt, grps, add_cnt * sizeof(*groups)); |
| 339 | groups[old_cnt + add_cnt] = NULL; |
| 340 | |
| 341 | old = rtc->dev.groups; |
| 342 | rtc->dev.groups = groups; |
| 343 | if (old && old != rtc_attr_groups) |
| 344 | devm_kfree(&rtc->dev, old); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | EXPORT_SYMBOL(rtc_add_groups); |
| 349 | |
| 350 | int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp) |
| 351 | { |
| 352 | const struct attribute_group *groups[] = { grp, NULL }; |
| 353 | |
| 354 | return rtc_add_groups(rtc, groups); |
| 355 | } |
| 356 | EXPORT_SYMBOL(rtc_add_group); |