^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * RTC subsystem, sysfs interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2005 Tower Technologies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Alessandro Zummo <a.zummo@towertech.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "rtc-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /* device attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * NOTE: RTC times displayed in sysfs use the RTC's timezone. That's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * ideally UTC. However, PCs that also boot to MS-Windows normally use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * the local time and change to match daylight savings time. That affects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * attributes including date, time, since_epoch, and wakealarm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) name_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) dev_name(dev->parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static DEVICE_ATTR_RO(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) date_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) retval = rtc_read_time(to_rtc_device(dev), &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return sprintf(buf, "%ptRd\n", &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static DEVICE_ATTR_RO(date);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) time_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) retval = rtc_read_time(to_rtc_device(dev), &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return sprintf(buf, "%ptRt\n", &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static DEVICE_ATTR_RO(time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) retval = rtc_read_time(to_rtc_device(dev), &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (retval == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) time64_t time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) time = rtc_tm_to_time64(&tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) retval = sprintf(buf, "%lld\n", time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static DEVICE_ATTR_RO(since_epoch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) max_user_freq_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) const char *buf, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct rtc_device *rtc = to_rtc_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) err = kstrtoul(buf, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (val >= 4096 || val == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) rtc->max_user_freq = (int)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static DEVICE_ATTR_RW(max_user_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * @dev: The device that the attribute belongs to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * @attr: The attribute being read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * @buf: The result buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * buf is "1" if the system clock was set by this RTC at the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * boot or resume event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #ifdef CONFIG_RTC_HCTOSYS_DEVICE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (rtc_hctosys_ret == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) strcmp(dev_name(&to_rtc_device(dev)->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) CONFIG_RTC_HCTOSYS_DEVICE) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return sprintf(buf, "1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return sprintf(buf, "0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static DEVICE_ATTR_RO(hctosys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) time64_t alarm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct rtc_wkalrm alm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /* Don't show disabled alarms. For uniformity, RTC alarms are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * conceptually one-shot, even though some common RTCs (on PCs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * don't actually work that way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * NOTE: RTC implementations where the alarm doesn't match an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * alarms after they trigger, to ensure one-shot semantics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) retval = rtc_read_alarm(to_rtc_device(dev), &alm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (retval == 0 && alm.enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) alarm = rtc_tm_to_time64(&alm.time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) retval = sprintf(buf, "%lld\n", alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) wakealarm_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) const char *buf, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) time64_t now, alarm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) time64_t push = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct rtc_wkalrm alm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct rtc_device *rtc = to_rtc_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) const char *buf_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int adjust = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* Only request alarms that trigger in the future. Disable them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) retval = rtc_read_time(rtc, &alm.time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) now = rtc_tm_to_time64(&alm.time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) buf_ptr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (*buf_ptr == '+') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) buf_ptr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (*buf_ptr == '=') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) buf_ptr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) push = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) adjust = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) retval = kstrtos64(buf_ptr, 0, &alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (adjust)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) alarm += now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (alarm > now || push) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /* Avoid accidentally clobbering active alarms; we can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * entirely prevent that here, without even the minimal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * locking from the /dev/rtcN api.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) retval = rtc_read_alarm(rtc, &alm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (alm.enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (push) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) push = rtc_tm_to_time64(&alm.time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) alarm += push;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) } else if (push)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) alm.enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) alm.enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* Provide a valid future alarm time. Linux isn't EFI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * this time won't be ignored when disabling the alarm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) alarm = now + 300;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) rtc_time64_to_tm(alarm, &alm.time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) retval = rtc_set_alarm(rtc, &alm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return (retval < 0) ? retval : n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static DEVICE_ATTR_RW(wakealarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) offset_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) retval = rtc_read_offset(to_rtc_device(dev), &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (retval == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) retval = sprintf(buf, "%ld\n", offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) offset_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) const char *buf, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) retval = kstrtol(buf, 10, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (retval == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) retval = rtc_set_offset(to_rtc_device(dev), offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return (retval < 0) ? retval : n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static DEVICE_ATTR_RW(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) range_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) to_rtc_device(dev)->range_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static DEVICE_ATTR_RO(range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static struct attribute *rtc_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) &dev_attr_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) &dev_attr_date.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) &dev_attr_time.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) &dev_attr_since_epoch.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) &dev_attr_max_user_freq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) &dev_attr_hctosys.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) &dev_attr_wakealarm.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) &dev_attr_offset.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) &dev_attr_range.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) /* The reason to trigger an alarm with no process watching it (via sysfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * is its side effect: waking from a system state like suspend-to-RAM or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * suspend-to-disk. So: no attribute unless that side effect is possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * (Userspace may disable that mechanism later.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static bool rtc_does_wakealarm(struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (!device_can_wakeup(rtc->dev.parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return rtc->ops->set_alarm != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static umode_t rtc_attr_is_visible(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct attribute *attr, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct device *dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct rtc_device *rtc = to_rtc_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) umode_t mode = attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (attr == &dev_attr_wakealarm.attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (!rtc_does_wakealarm(rtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) } else if (attr == &dev_attr_offset.attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (!rtc->ops->set_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) } else if (attr == &dev_attr_range.attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (!(rtc->range_max - rtc->range_min))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static struct attribute_group rtc_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) .is_visible = rtc_attr_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) .attrs = rtc_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static const struct attribute_group *rtc_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) &rtc_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) const struct attribute_group **rtc_get_dev_attribute_groups(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return rtc_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) size_t old_cnt = 0, add_cnt = 0, new_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) const struct attribute_group **groups, **old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (rtc->registered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (!grps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) groups = rtc->dev.groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) for (; *groups; groups++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) old_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) for (groups = grps; *groups; groups++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) add_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) new_cnt = old_cnt + add_cnt + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (!groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) memcpy(groups, rtc->dev.groups, old_cnt * sizeof(*groups));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) memcpy(groups + old_cnt, grps, add_cnt * sizeof(*groups));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) groups[old_cnt + add_cnt] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) old = rtc->dev.groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) rtc->dev.groups = groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (old && old != rtc_attr_groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) devm_kfree(&rtc->dev, old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) EXPORT_SYMBOL(rtc_add_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) const struct attribute_group *groups[] = { grp, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return rtc_add_groups(rtc, groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) EXPORT_SYMBOL(rtc_add_group);