rtc: simplified rtc sysfs attribute handling
[linux-2.6/x86.git] / drivers / rtc / rtc-sysfs.c
blob1c2fa0cc9cbb27df9d0921fdde796ef4fbdefbfa
1 /*
2 * RTC subsystem, sysfs interface
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
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.
12 #include <linux/module.h>
13 #include <linux/rtc.h>
15 #include "rtc-core.h"
18 /* device attributes */
20 static ssize_t rtc_sysfs_show_name(struct class_device *dev, char *buf)
22 return sprintf(buf, "%s\n", to_rtc_device(dev)->name);
25 static ssize_t rtc_sysfs_show_date(struct class_device *dev, char *buf)
27 ssize_t retval;
28 struct rtc_time tm;
30 retval = rtc_read_time(to_rtc_device(dev), &tm);
31 if (retval == 0) {
32 retval = sprintf(buf, "%04d-%02d-%02d\n",
33 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
36 return retval;
39 static ssize_t rtc_sysfs_show_time(struct class_device *dev, char *buf)
41 ssize_t retval;
42 struct rtc_time tm;
44 retval = rtc_read_time(to_rtc_device(dev), &tm);
45 if (retval == 0) {
46 retval = sprintf(buf, "%02d:%02d:%02d\n",
47 tm.tm_hour, tm.tm_min, tm.tm_sec);
50 return retval;
53 static ssize_t rtc_sysfs_show_since_epoch(struct class_device *dev, char *buf)
55 ssize_t retval;
56 struct rtc_time tm;
58 retval = rtc_read_time(to_rtc_device(dev), &tm);
59 if (retval == 0) {
60 unsigned long time;
61 rtc_tm_to_time(&tm, &time);
62 retval = sprintf(buf, "%lu\n", time);
65 return retval;
68 static struct class_device_attribute rtc_attrs[] = {
69 __ATTR(name, S_IRUGO, rtc_sysfs_show_name, NULL),
70 __ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL),
71 __ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL),
72 __ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL),
73 { },
76 static ssize_t
77 rtc_sysfs_show_wakealarm(struct class_device *dev, char *buf)
79 ssize_t retval;
80 unsigned long alarm;
81 struct rtc_wkalrm alm;
83 /* Don't show disabled alarms; but the RTC could leave the
84 * alarm enabled after it's already triggered. Alarms are
85 * conceptually one-shot, even though some common hardware
86 * (PCs) doesn't actually work that way.
88 * REVISIT maybe we should require RTC implementations to
89 * disable the RTC alarm after it triggers, for uniformity.
91 retval = rtc_read_alarm(to_rtc_device(dev), &alm);
92 if (retval == 0 && alm.enabled) {
93 rtc_tm_to_time(&alm.time, &alarm);
94 retval = sprintf(buf, "%lu\n", alarm);
97 return retval;
100 static ssize_t
101 rtc_sysfs_set_wakealarm(struct class_device *dev, const char *buf, size_t n)
103 ssize_t retval;
104 unsigned long now, alarm;
105 struct rtc_wkalrm alm;
106 struct rtc_device *rtc = to_rtc_device(dev);
108 /* Only request alarms that trigger in the future. Disable them
109 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
111 retval = rtc_read_time(rtc, &alm.time);
112 if (retval < 0)
113 return retval;
114 rtc_tm_to_time(&alm.time, &now);
116 alarm = simple_strtoul(buf, NULL, 0);
117 if (alarm > now) {
118 /* Avoid accidentally clobbering active alarms; we can't
119 * entirely prevent that here, without even the minimal
120 * locking from the /dev/rtcN api.
122 retval = rtc_read_alarm(rtc, &alm);
123 if (retval < 0)
124 return retval;
125 if (alm.enabled)
126 return -EBUSY;
128 alm.enabled = 1;
129 } else {
130 alm.enabled = 0;
132 /* Provide a valid future alarm time. Linux isn't EFI,
133 * this time won't be ignored when disabling the alarm.
135 alarm = now + 300;
137 rtc_time_to_tm(alarm, &alm.time);
139 retval = rtc_set_alarm(rtc, &alm);
140 return (retval < 0) ? retval : n;
142 static const CLASS_DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR,
143 rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm);
146 /* The reason to trigger an alarm with no process watching it (via sysfs)
147 * is its side effect: waking from a system state like suspend-to-RAM or
148 * suspend-to-disk. So: no attribute unless that side effect is possible.
149 * (Userspace may disable that mechanism later.)
151 static inline int rtc_does_wakealarm(struct rtc_device *rtc)
153 if (!device_can_wakeup(rtc->class_dev.dev))
154 return 0;
155 return rtc->ops->set_alarm != NULL;
159 void rtc_sysfs_add_device(struct rtc_device *rtc)
161 int err;
163 /* not all RTCs support both alarms and wakeup */
164 if (!rtc_does_wakealarm(rtc))
165 return;
167 err = class_device_create_file(&rtc->class_dev,
168 &class_device_attr_wakealarm);
169 if (err)
170 dev_err(rtc->class_dev.dev, "failed to create "
171 "alarm attribute, %d",
172 err);
175 void rtc_sysfs_del_device(struct rtc_device *rtc)
177 /* REVISIT did we add it successfully? */
178 if (rtc_does_wakealarm(rtc))
179 class_device_remove_file(&rtc->class_dev,
180 &class_device_attr_wakealarm);
183 void __init rtc_sysfs_init(struct class *rtc_class)
185 rtc_class->class_dev_attrs = rtc_attrs;