rtc-sysfs.c 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * RTC subsystem, sysfs interface
 *
 * Copyright (C) 2005 Tower Technologies
 * Author: Alessandro Zummo <a.zummo@towertech.it>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/

#include <linux/module.h>
#include <linux/rtc.h>

15 16 17
#include "rtc-core.h"


18 19
/* device attributes */

D
David Brownell 已提交
20 21 22 23 24 25 26
/*
 * NOTE:  RTC times displayed in sysfs use the RTC's timezone.  That's
 * ideally UTC.  However, PCs that also boot to MS-Windows normally use
 * the local time and change to match daylight savings time.  That affects
 * attributes including date, time, since_epoch, and wakealarm.
 */

27
static ssize_t
28
name_show(struct device *dev, struct device_attribute *attr, char *buf)
29 30 31
{
	return sprintf(buf, "%s\n", to_rtc_device(dev)->name);
}
32
static DEVICE_ATTR_RO(name);
33

34
static ssize_t
35
date_show(struct device *dev, struct device_attribute *attr, char *buf)
36 37 38 39
{
	ssize_t retval;
	struct rtc_time tm;

40
	retval = rtc_read_time(to_rtc_device(dev), &tm);
41 42 43 44 45 46 47
	if (retval == 0) {
		retval = sprintf(buf, "%04d-%02d-%02d\n",
			tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
	}

	return retval;
}
48
static DEVICE_ATTR_RO(date);
49

50
static ssize_t
51
time_show(struct device *dev, struct device_attribute *attr, char *buf)
52 53 54 55
{
	ssize_t retval;
	struct rtc_time tm;

56
	retval = rtc_read_time(to_rtc_device(dev), &tm);
57 58 59 60 61 62 63
	if (retval == 0) {
		retval = sprintf(buf, "%02d:%02d:%02d\n",
			tm.tm_hour, tm.tm_min, tm.tm_sec);
	}

	return retval;
}
64
static DEVICE_ATTR_RO(time);
65

66
static ssize_t
67
since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
68 69 70 71
{
	ssize_t retval;
	struct rtc_time tm;

72
	retval = rtc_read_time(to_rtc_device(dev), &tm);
73 74 75 76 77 78 79 80
	if (retval == 0) {
		unsigned long time;
		rtc_tm_to_time(&tm, &time);
		retval = sprintf(buf, "%lu\n", time);
	}

	return retval;
}
81
static DEVICE_ATTR_RO(since_epoch);
82

B
Bryan Kadzban 已提交
83
static ssize_t
84
max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
B
Bryan Kadzban 已提交
85 86 87 88 89
{
	return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
}

static ssize_t
90
max_user_freq_store(struct device *dev, struct device_attribute *attr,
B
Bryan Kadzban 已提交
91 92 93 94 95 96 97 98 99 100 101 102
		const char *buf, size_t n)
{
	struct rtc_device *rtc = to_rtc_device(dev);
	unsigned long val = simple_strtoul(buf, NULL, 0);

	if (val >= 4096 || val == 0)
		return -EINVAL;

	rtc->max_user_freq = (int)val;

	return n;
}
103
static DEVICE_ATTR_RW(max_user_freq);
B
Bryan Kadzban 已提交
104

105 106 107 108 109 110
/**
 * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
 *
 * Returns 1 if the system clock was set by this RTC at the last
 * boot or resume event.
 */
111
static ssize_t
112
hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
113 114
{
#ifdef CONFIG_RTC_HCTOSYS_DEVICE
115 116 117
	if (rtc_hctosys_ret == 0 &&
			strcmp(dev_name(&to_rtc_device(dev)->dev),
				CONFIG_RTC_HCTOSYS_DEVICE) == 0)
118 119 120 121 122
		return sprintf(buf, "1\n");
	else
#endif
		return sprintf(buf, "0\n");
}
123 124 125 126 127 128 129 130 131 132
static DEVICE_ATTR_RO(hctosys);

static struct attribute *rtc_attrs[] = {
	&dev_attr_name.attr,
	&dev_attr_date.attr,
	&dev_attr_time.attr,
	&dev_attr_since_epoch.attr,
	&dev_attr_max_user_freq.attr,
	&dev_attr_hctosys.attr,
	NULL,
133
};
134
ATTRIBUTE_GROUPS(rtc);
135

136
static ssize_t
137 138
rtc_sysfs_show_wakealarm(struct device *dev, struct device_attribute *attr,
		char *buf)
139 140 141 142 143
{
	ssize_t retval;
	unsigned long alarm;
	struct rtc_wkalrm alm;

D
David Brownell 已提交
144 145 146
	/* Don't show disabled alarms.  For uniformity, RTC alarms are
	 * conceptually one-shot, even though some common RTCs (on PCs)
	 * don't actually work that way.
147
	 *
D
David Brownell 已提交
148 149 150
	 * NOTE: RTC implementations where the alarm doesn't match an
	 * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
	 * alarms after they trigger, to ensure one-shot semantics.
151
	 */
152
	retval = rtc_read_alarm(to_rtc_device(dev), &alm);
153 154 155 156 157 158 159 160 161
	if (retval == 0 && alm.enabled) {
		rtc_tm_to_time(&alm.time, &alarm);
		retval = sprintf(buf, "%lu\n", alarm);
	}

	return retval;
}

static ssize_t
162 163
rtc_sysfs_set_wakealarm(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
164 165 166
{
	ssize_t retval;
	unsigned long now, alarm;
167
	unsigned long push = 0;
168
	struct rtc_wkalrm alm;
169
	struct rtc_device *rtc = to_rtc_device(dev);
170 171
	char *buf_ptr;
	int adjust = 0;
172 173 174 175

	/* Only request alarms that trigger in the future.  Disable them
	 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
	 */
176
	retval = rtc_read_time(rtc, &alm.time);
177 178 179 180
	if (retval < 0)
		return retval;
	rtc_tm_to_time(&alm.time, &now);

181 182 183
	buf_ptr = (char *)buf;
	if (*buf_ptr == '+') {
		buf_ptr++;
184 185 186 187 188
		if (*buf_ptr == '=') {
			buf_ptr++;
			push = 1;
		} else
			adjust = 1;
189 190 191 192 193
	}
	alarm = simple_strtoul(buf_ptr, NULL, 0);
	if (adjust) {
		alarm += now;
	}
194
	if (alarm > now || push) {
195 196 197 198
		/* Avoid accidentally clobbering active alarms; we can't
		 * entirely prevent that here, without even the minimal
		 * locking from the /dev/rtcN api.
		 */
199
		retval = rtc_read_alarm(rtc, &alm);
200 201
		if (retval < 0)
			return retval;
202 203 204 205 206 207 208 209
		if (alm.enabled) {
			if (push) {
				rtc_tm_to_time(&alm.time, &push);
				alarm += push;
			} else
				return -EBUSY;
		} else if (push)
			return -EINVAL;
210 211 212 213 214 215 216 217 218 219 220
		alm.enabled = 1;
	} else {
		alm.enabled = 0;

		/* Provide a valid future alarm time.  Linux isn't EFI,
		 * this time won't be ignored when disabling the alarm.
		 */
		alarm = now + 300;
	}
	rtc_time_to_tm(alarm, &alm.time);

221
	retval = rtc_set_alarm(rtc, &alm);
222 223
	return (retval < 0) ? retval : n;
}
224
static DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR,
225 226 227 228 229 230 231 232
		rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm);


/* The reason to trigger an alarm with no process watching it (via sysfs)
 * is its side effect:  waking from a system state like suspend-to-RAM or
 * suspend-to-disk.  So: no attribute unless that side effect is possible.
 * (Userspace may disable that mechanism later.)
 */
233
static inline int rtc_does_wakealarm(struct rtc_device *rtc)
234
{
235
	if (!device_can_wakeup(rtc->dev.parent))
236 237 238 239 240
		return 0;
	return rtc->ops->set_alarm != NULL;
}


241
void rtc_sysfs_add_device(struct rtc_device *rtc)
242 243 244
{
	int err;

245 246 247
	/* not all RTCs support both alarms and wakeup */
	if (!rtc_does_wakealarm(rtc))
		return;
248

249
	err = device_create_file(&rtc->dev, &dev_attr_wakealarm);
250
	if (err)
251 252
		dev_err(rtc->dev.parent,
			"failed to create alarm attribute, %d\n", err);
253 254
}

255
void rtc_sysfs_del_device(struct rtc_device *rtc)
256
{
257 258
	/* REVISIT did we add it successfully? */
	if (rtc_does_wakealarm(rtc))
259
		device_remove_file(&rtc->dev, &dev_attr_wakealarm);
260 261
}

262
void __init rtc_sysfs_init(struct class *rtc_class)
263
{
264
	rtc_class->dev_groups = rtc_groups;
265
}