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 | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 20 | static ssize_t |
| 21 | rtc_sysfs_show_name(struct device *dev, struct device_attribute *attr, |
| 22 | char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 23 | { |
| 24 | return sprintf(buf, "%s\n", to_rtc_device(dev)->name); |
| 25 | } |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 26 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 27 | static ssize_t |
| 28 | rtc_sysfs_show_date(struct device *dev, struct device_attribute *attr, |
| 29 | char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 30 | { |
| 31 | ssize_t retval; |
| 32 | struct rtc_time tm; |
| 33 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 34 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 35 | if (retval == 0) { |
| 36 | retval = sprintf(buf, "%04d-%02d-%02d\n", |
| 37 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); |
| 38 | } |
| 39 | |
| 40 | return retval; |
| 41 | } |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 42 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 43 | static ssize_t |
| 44 | rtc_sysfs_show_time(struct device *dev, struct device_attribute *attr, |
| 45 | char *buf) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 46 | { |
| 47 | ssize_t retval; |
| 48 | struct rtc_time tm; |
| 49 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 50 | retval = rtc_read_time(to_rtc_device(dev), &tm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 51 | if (retval == 0) { |
| 52 | retval = sprintf(buf, "%02d:%02d:%02d\n", |
| 53 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 54 | } |
| 55 | |
| 56 | return retval; |
| 57 | } |
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 |
| 60 | rtc_sysfs_show_since_epoch(struct device *dev, struct device_attribute *attr, |
| 61 | 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) { |
| 68 | unsigned long time; |
| 69 | rtc_tm_to_time(&tm, &time); |
| 70 | retval = sprintf(buf, "%lu\n", time); |
| 71 | } |
| 72 | |
| 73 | return retval; |
| 74 | } |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 75 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 76 | static struct device_attribute rtc_attrs[] = { |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 77 | __ATTR(name, S_IRUGO, rtc_sysfs_show_name, NULL), |
| 78 | __ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL), |
| 79 | __ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL), |
| 80 | __ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL), |
| 81 | { }, |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 84 | static ssize_t |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 85 | rtc_sysfs_show_wakealarm(struct device *dev, struct device_attribute *attr, |
| 86 | char *buf) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 87 | { |
| 88 | ssize_t retval; |
| 89 | unsigned long alarm; |
| 90 | struct rtc_wkalrm alm; |
| 91 | |
| 92 | /* Don't show disabled alarms; but the RTC could leave the |
| 93 | * alarm enabled after it's already triggered. Alarms are |
| 94 | * conceptually one-shot, even though some common hardware |
| 95 | * (PCs) doesn't actually work that way. |
| 96 | * |
| 97 | * REVISIT maybe we should require RTC implementations to |
| 98 | * disable the RTC alarm after it triggers, for uniformity. |
| 99 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 100 | retval = rtc_read_alarm(to_rtc_device(dev), &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 101 | if (retval == 0 && alm.enabled) { |
| 102 | rtc_tm_to_time(&alm.time, &alarm); |
| 103 | retval = sprintf(buf, "%lu\n", alarm); |
| 104 | } |
| 105 | |
| 106 | return retval; |
| 107 | } |
| 108 | |
| 109 | static ssize_t |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 110 | rtc_sysfs_set_wakealarm(struct device *dev, struct device_attribute *attr, |
| 111 | const char *buf, size_t n) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 112 | { |
| 113 | ssize_t retval; |
| 114 | unsigned long now, alarm; |
| 115 | struct rtc_wkalrm alm; |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 116 | struct rtc_device *rtc = to_rtc_device(dev); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 117 | |
| 118 | /* Only request alarms that trigger in the future. Disable them |
| 119 | * by writing another time, e.g. 0 meaning Jan 1 1970 UTC. |
| 120 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 121 | retval = rtc_read_time(rtc, &alm.time); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 122 | if (retval < 0) |
| 123 | return retval; |
| 124 | rtc_tm_to_time(&alm.time, &now); |
| 125 | |
| 126 | alarm = simple_strtoul(buf, NULL, 0); |
| 127 | if (alarm > now) { |
| 128 | /* Avoid accidentally clobbering active alarms; we can't |
| 129 | * entirely prevent that here, without even the minimal |
| 130 | * locking from the /dev/rtcN api. |
| 131 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 132 | retval = rtc_read_alarm(rtc, &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 133 | if (retval < 0) |
| 134 | return retval; |
| 135 | if (alm.enabled) |
| 136 | return -EBUSY; |
| 137 | |
| 138 | alm.enabled = 1; |
| 139 | } else { |
| 140 | alm.enabled = 0; |
| 141 | |
| 142 | /* Provide a valid future alarm time. Linux isn't EFI, |
| 143 | * this time won't be ignored when disabling the alarm. |
| 144 | */ |
| 145 | alarm = now + 300; |
| 146 | } |
| 147 | rtc_time_to_tm(alarm, &alm.time); |
| 148 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 149 | retval = rtc_set_alarm(rtc, &alm); |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 150 | return (retval < 0) ? retval : n; |
| 151 | } |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 152 | static DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR, |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 153 | rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm); |
| 154 | |
| 155 | |
| 156 | /* The reason to trigger an alarm with no process watching it (via sysfs) |
| 157 | * is its side effect: waking from a system state like suspend-to-RAM or |
| 158 | * suspend-to-disk. So: no attribute unless that side effect is possible. |
| 159 | * (Userspace may disable that mechanism later.) |
| 160 | */ |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 161 | static inline int rtc_does_wakealarm(struct rtc_device *rtc) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 162 | { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 163 | if (!device_can_wakeup(rtc->dev.parent)) |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 164 | return 0; |
David Brownell | 3925a5c | 2007-02-12 00:52:47 -0800 | [diff] [blame] | 165 | return rtc->ops->set_alarm != NULL; |
| 166 | } |
| 167 | |
| 168 | |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 169 | void rtc_sysfs_add_device(struct rtc_device *rtc) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 170 | { |
| 171 | int err; |
| 172 | |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 173 | /* not all RTCs support both alarms and wakeup */ |
| 174 | if (!rtc_does_wakealarm(rtc)) |
| 175 | return; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 176 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 177 | err = device_create_file(&rtc->dev, &dev_attr_wakealarm); |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 178 | if (err) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 179 | dev_err(rtc->dev.parent, "failed to create " |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 180 | "alarm attribute, %d", |
| 181 | err); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 182 | } |
| 183 | |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 184 | void rtc_sysfs_del_device(struct rtc_device *rtc) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 185 | { |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 186 | /* REVISIT did we add it successfully? */ |
| 187 | if (rtc_does_wakealarm(rtc)) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 188 | device_remove_file(&rtc->dev, &dev_attr_wakealarm); |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 189 | } |
| 190 | |
David Brownell | 446ecbd | 2007-05-08 00:33:33 -0700 | [diff] [blame] | 191 | void __init rtc_sysfs_init(struct class *rtc_class) |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 192 | { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 193 | rtc_class->dev_attrs = rtc_attrs; |
Alessandro Zummo | c5c3e19 | 2006-03-27 01:16:39 -0800 | [diff] [blame] | 194 | } |