Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * RTC subsystem, sysfs interface
0004  *
0005  * Copyright (C) 2005 Tower Technologies
0006  * Author: Alessandro Zummo <a.zummo@towertech.it>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/rtc.h>
0011 
0012 #include "rtc-core.h"
0013 
0014 /* device attributes */
0015 
0016 /*
0017  * NOTE:  RTC times displayed in sysfs use the RTC's timezone.  That's
0018  * ideally UTC.  However, PCs that also boot to MS-Windows normally use
0019  * the local time and change to match daylight savings time.  That affects
0020  * attributes including date, time, since_epoch, and wakealarm.
0021  */
0022 
0023 static ssize_t
0024 name_show(struct device *dev, struct device_attribute *attr, char *buf)
0025 {
0026     return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
0027                dev_name(dev->parent));
0028 }
0029 static DEVICE_ATTR_RO(name);
0030 
0031 static ssize_t
0032 date_show(struct device *dev, struct device_attribute *attr, char *buf)
0033 {
0034     ssize_t retval;
0035     struct rtc_time tm;
0036 
0037     retval = rtc_read_time(to_rtc_device(dev), &tm);
0038     if (retval)
0039         return retval;
0040 
0041     return sprintf(buf, "%ptRd\n", &tm);
0042 }
0043 static DEVICE_ATTR_RO(date);
0044 
0045 static ssize_t
0046 time_show(struct device *dev, struct device_attribute *attr, char *buf)
0047 {
0048     ssize_t retval;
0049     struct rtc_time tm;
0050 
0051     retval = rtc_read_time(to_rtc_device(dev), &tm);
0052     if (retval)
0053         return retval;
0054 
0055     return sprintf(buf, "%ptRt\n", &tm);
0056 }
0057 static DEVICE_ATTR_RO(time);
0058 
0059 static ssize_t
0060 since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
0061 {
0062     ssize_t retval;
0063     struct rtc_time tm;
0064 
0065     retval = rtc_read_time(to_rtc_device(dev), &tm);
0066     if (retval == 0) {
0067         time64_t time;
0068 
0069         time = rtc_tm_to_time64(&tm);
0070         retval = sprintf(buf, "%lld\n", time);
0071     }
0072 
0073     return retval;
0074 }
0075 static DEVICE_ATTR_RO(since_epoch);
0076 
0077 static ssize_t
0078 max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
0079 {
0080     return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
0081 }
0082 
0083 static ssize_t
0084 max_user_freq_store(struct device *dev, struct device_attribute *attr,
0085             const char *buf, size_t n)
0086 {
0087     struct rtc_device *rtc = to_rtc_device(dev);
0088     unsigned long val;
0089     int err;
0090 
0091     err = kstrtoul(buf, 0, &val);
0092     if (err)
0093         return err;
0094 
0095     if (val >= 4096 || val == 0)
0096         return -EINVAL;
0097 
0098     rtc->max_user_freq = (int)val;
0099 
0100     return n;
0101 }
0102 static DEVICE_ATTR_RW(max_user_freq);
0103 
0104 /**
0105  * hctosys_show - indicate if the given RTC set the system time
0106  * @dev: The device that the attribute belongs to.
0107  * @attr: The attribute being read.
0108  * @buf: The result buffer.
0109  *
0110  * buf is "1" if the system clock was set by this RTC at the last
0111  * boot or resume event.
0112  */
0113 static ssize_t
0114 hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
0115 {
0116 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
0117     if (rtc_hctosys_ret == 0 &&
0118         strcmp(dev_name(&to_rtc_device(dev)->dev),
0119            CONFIG_RTC_HCTOSYS_DEVICE) == 0)
0120         return sprintf(buf, "1\n");
0121 #endif
0122     return sprintf(buf, "0\n");
0123 }
0124 static DEVICE_ATTR_RO(hctosys);
0125 
0126 static ssize_t
0127 wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
0128 {
0129     ssize_t retval;
0130     time64_t alarm;
0131     struct rtc_wkalrm alm;
0132 
0133     /* Don't show disabled alarms.  For uniformity, RTC alarms are
0134      * conceptually one-shot, even though some common RTCs (on PCs)
0135      * don't actually work that way.
0136      *
0137      * NOTE: RTC implementations where the alarm doesn't match an
0138      * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
0139      * alarms after they trigger, to ensure one-shot semantics.
0140      */
0141     retval = rtc_read_alarm(to_rtc_device(dev), &alm);
0142     if (retval == 0 && alm.enabled) {
0143         alarm = rtc_tm_to_time64(&alm.time);
0144         retval = sprintf(buf, "%lld\n", alarm);
0145     }
0146 
0147     return retval;
0148 }
0149 
0150 static ssize_t
0151 wakealarm_store(struct device *dev, struct device_attribute *attr,
0152         const char *buf, size_t n)
0153 {
0154     ssize_t retval;
0155     time64_t now, alarm;
0156     time64_t push = 0;
0157     struct rtc_wkalrm alm;
0158     struct rtc_device *rtc = to_rtc_device(dev);
0159     const char *buf_ptr;
0160     int adjust = 0;
0161 
0162     /* Only request alarms that trigger in the future.  Disable them
0163      * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
0164      */
0165     retval = rtc_read_time(rtc, &alm.time);
0166     if (retval < 0)
0167         return retval;
0168     now = rtc_tm_to_time64(&alm.time);
0169 
0170     buf_ptr = buf;
0171     if (*buf_ptr == '+') {
0172         buf_ptr++;
0173         if (*buf_ptr == '=') {
0174             buf_ptr++;
0175             push = 1;
0176         } else {
0177             adjust = 1;
0178         }
0179     }
0180     retval = kstrtos64(buf_ptr, 0, &alarm);
0181     if (retval)
0182         return retval;
0183     if (adjust)
0184         alarm += now;
0185     if (alarm > now || push) {
0186         /* Avoid accidentally clobbering active alarms; we can't
0187          * entirely prevent that here, without even the minimal
0188          * locking from the /dev/rtcN api.
0189          */
0190         retval = rtc_read_alarm(rtc, &alm);
0191         if (retval < 0)
0192             return retval;
0193         if (alm.enabled) {
0194             if (push) {
0195                 push = rtc_tm_to_time64(&alm.time);
0196                 alarm += push;
0197             } else
0198                 return -EBUSY;
0199         } else if (push)
0200             return -EINVAL;
0201         alm.enabled = 1;
0202     } else {
0203         alm.enabled = 0;
0204 
0205         /* Provide a valid future alarm time.  Linux isn't EFI,
0206          * this time won't be ignored when disabling the alarm.
0207          */
0208         alarm = now + 300;
0209     }
0210     rtc_time64_to_tm(alarm, &alm.time);
0211 
0212     retval = rtc_set_alarm(rtc, &alm);
0213     return (retval < 0) ? retval : n;
0214 }
0215 static DEVICE_ATTR_RW(wakealarm);
0216 
0217 static ssize_t
0218 offset_show(struct device *dev, struct device_attribute *attr, char *buf)
0219 {
0220     ssize_t retval;
0221     long offset;
0222 
0223     retval = rtc_read_offset(to_rtc_device(dev), &offset);
0224     if (retval == 0)
0225         retval = sprintf(buf, "%ld\n", offset);
0226 
0227     return retval;
0228 }
0229 
0230 static ssize_t
0231 offset_store(struct device *dev, struct device_attribute *attr,
0232          const char *buf, size_t n)
0233 {
0234     ssize_t retval;
0235     long offset;
0236 
0237     retval = kstrtol(buf, 10, &offset);
0238     if (retval == 0)
0239         retval = rtc_set_offset(to_rtc_device(dev), offset);
0240 
0241     return (retval < 0) ? retval : n;
0242 }
0243 static DEVICE_ATTR_RW(offset);
0244 
0245 static ssize_t
0246 range_show(struct device *dev, struct device_attribute *attr, char *buf)
0247 {
0248     return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min,
0249                to_rtc_device(dev)->range_max);
0250 }
0251 static DEVICE_ATTR_RO(range);
0252 
0253 static struct attribute *rtc_attrs[] = {
0254     &dev_attr_name.attr,
0255     &dev_attr_date.attr,
0256     &dev_attr_time.attr,
0257     &dev_attr_since_epoch.attr,
0258     &dev_attr_max_user_freq.attr,
0259     &dev_attr_hctosys.attr,
0260     &dev_attr_wakealarm.attr,
0261     &dev_attr_offset.attr,
0262     &dev_attr_range.attr,
0263     NULL,
0264 };
0265 
0266 /* The reason to trigger an alarm with no process watching it (via sysfs)
0267  * is its side effect:  waking from a system state like suspend-to-RAM or
0268  * suspend-to-disk.  So: no attribute unless that side effect is possible.
0269  * (Userspace may disable that mechanism later.)
0270  */
0271 static bool rtc_does_wakealarm(struct rtc_device *rtc)
0272 {
0273     if (!device_can_wakeup(rtc->dev.parent))
0274         return false;
0275 
0276     return !!test_bit(RTC_FEATURE_ALARM, rtc->features);
0277 }
0278 
0279 static umode_t rtc_attr_is_visible(struct kobject *kobj,
0280                    struct attribute *attr, int n)
0281 {
0282     struct device *dev = kobj_to_dev(kobj);
0283     struct rtc_device *rtc = to_rtc_device(dev);
0284     umode_t mode = attr->mode;
0285 
0286     if (attr == &dev_attr_wakealarm.attr) {
0287         if (!rtc_does_wakealarm(rtc))
0288             mode = 0;
0289     } else if (attr == &dev_attr_offset.attr) {
0290         if (!rtc->ops->set_offset)
0291             mode = 0;
0292     } else if (attr == &dev_attr_range.attr) {
0293         if (!(rtc->range_max - rtc->range_min))
0294             mode = 0;
0295     }
0296 
0297     return mode;
0298 }
0299 
0300 static struct attribute_group rtc_attr_group = {
0301     .is_visible = rtc_attr_is_visible,
0302     .attrs      = rtc_attrs,
0303 };
0304 
0305 static const struct attribute_group *rtc_attr_groups[] = {
0306     &rtc_attr_group,
0307     NULL
0308 };
0309 
0310 const struct attribute_group **rtc_get_dev_attribute_groups(void)
0311 {
0312     return rtc_attr_groups;
0313 }
0314 
0315 int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
0316 {
0317     size_t old_cnt = 0, add_cnt = 0, new_cnt;
0318     const struct attribute_group **groups, **old;
0319 
0320     if (!grps)
0321         return -EINVAL;
0322 
0323     groups = rtc->dev.groups;
0324     if (groups)
0325         for (; *groups; groups++)
0326             old_cnt++;
0327 
0328     for (groups = grps; *groups; groups++)
0329         add_cnt++;
0330 
0331     new_cnt = old_cnt + add_cnt + 1;
0332     groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL);
0333     if (!groups)
0334         return -ENOMEM;
0335     memcpy(groups, rtc->dev.groups, old_cnt * sizeof(*groups));
0336     memcpy(groups + old_cnt, grps, add_cnt * sizeof(*groups));
0337     groups[old_cnt + add_cnt] = NULL;
0338 
0339     old = rtc->dev.groups;
0340     rtc->dev.groups = groups;
0341     if (old && old != rtc_attr_groups)
0342         devm_kfree(&rtc->dev, old);
0343 
0344     return 0;
0345 }
0346 EXPORT_SYMBOL(rtc_add_groups);
0347 
0348 int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
0349 {
0350     const struct attribute_group *groups[] = { grp, NULL };
0351 
0352     return rtc_add_groups(rtc, groups);
0353 }
0354 EXPORT_SYMBOL(rtc_add_group);