[SPARC64]: Privatize sun4u_get_pte() and fix name.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-sysfs.c
blob899ab8c514facbda5965be87bff5422b8aa0ebf0
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 /* device attributes */
17 static ssize_t rtc_sysfs_show_name(struct class_device *dev, char *buf)
19 return sprintf(buf, "%s\n", to_rtc_device(dev)->name);
21 static CLASS_DEVICE_ATTR(name, S_IRUGO, rtc_sysfs_show_name, NULL);
23 static ssize_t rtc_sysfs_show_date(struct class_device *dev, char *buf)
25 ssize_t retval;
26 struct rtc_time tm;
28 retval = rtc_read_time(dev, &tm);
29 if (retval == 0) {
30 retval = sprintf(buf, "%04d-%02d-%02d\n",
31 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
34 return retval;
36 static CLASS_DEVICE_ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL);
38 static ssize_t rtc_sysfs_show_time(struct class_device *dev, char *buf)
40 ssize_t retval;
41 struct rtc_time tm;
43 retval = rtc_read_time(dev, &tm);
44 if (retval == 0) {
45 retval = sprintf(buf, "%02d:%02d:%02d\n",
46 tm.tm_hour, tm.tm_min, tm.tm_sec);
49 return retval;
51 static CLASS_DEVICE_ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL);
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(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;
67 static CLASS_DEVICE_ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL);
69 static struct attribute *rtc_attrs[] = {
70 &class_device_attr_name.attr,
71 &class_device_attr_date.attr,
72 &class_device_attr_time.attr,
73 &class_device_attr_since_epoch.attr,
74 NULL,
77 static struct attribute_group rtc_attr_group = {
78 .attrs = rtc_attrs,
82 static ssize_t
83 rtc_sysfs_show_wakealarm(struct class_device *dev, char *buf)
85 ssize_t retval;
86 unsigned long alarm;
87 struct rtc_wkalrm alm;
89 /* Don't show disabled alarms; but the RTC could leave the
90 * alarm enabled after it's already triggered. Alarms are
91 * conceptually one-shot, even though some common hardware
92 * (PCs) doesn't actually work that way.
94 * REVISIT maybe we should require RTC implementations to
95 * disable the RTC alarm after it triggers, for uniformity.
97 retval = rtc_read_alarm(dev, &alm);
98 if (retval == 0 && alm.enabled) {
99 rtc_tm_to_time(&alm.time, &alarm);
100 retval = sprintf(buf, "%lu\n", alarm);
103 return retval;
106 static ssize_t
107 rtc_sysfs_set_wakealarm(struct class_device *dev, const char *buf, size_t n)
109 ssize_t retval;
110 unsigned long now, alarm;
111 struct rtc_wkalrm alm;
113 /* Only request alarms that trigger in the future. Disable them
114 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
116 retval = rtc_read_time(dev, &alm.time);
117 if (retval < 0)
118 return retval;
119 rtc_tm_to_time(&alm.time, &now);
121 alarm = simple_strtoul(buf, NULL, 0);
122 if (alarm > now) {
123 /* Avoid accidentally clobbering active alarms; we can't
124 * entirely prevent that here, without even the minimal
125 * locking from the /dev/rtcN api.
127 retval = rtc_read_alarm(dev, &alm);
128 if (retval < 0)
129 return retval;
130 if (alm.enabled)
131 return -EBUSY;
133 alm.enabled = 1;
134 } else {
135 alm.enabled = 0;
137 /* Provide a valid future alarm time. Linux isn't EFI,
138 * this time won't be ignored when disabling the alarm.
140 alarm = now + 300;
142 rtc_time_to_tm(alarm, &alm.time);
144 retval = rtc_set_alarm(dev, &alm);
145 return (retval < 0) ? retval : n;
147 static const CLASS_DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR,
148 rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm);
151 /* The reason to trigger an alarm with no process watching it (via sysfs)
152 * is its side effect: waking from a system state like suspend-to-RAM or
153 * suspend-to-disk. So: no attribute unless that side effect is possible.
154 * (Userspace may disable that mechanism later.)
156 static inline int rtc_does_wakealarm(struct class_device *class_dev)
158 struct rtc_device *rtc;
160 if (!device_can_wakeup(class_dev->dev))
161 return 0;
162 rtc = to_rtc_device(class_dev);
163 return rtc->ops->set_alarm != NULL;
167 static int rtc_sysfs_add_device(struct class_device *class_dev,
168 struct class_interface *class_intf)
170 int err;
172 dev_dbg(class_dev->dev, "rtc intf: sysfs\n");
174 err = sysfs_create_group(&class_dev->kobj, &rtc_attr_group);
175 if (err)
176 dev_err(class_dev->dev, "failed to create %s\n",
177 "sysfs attributes");
178 else if (rtc_does_wakealarm(class_dev)) {
179 /* not all RTCs support both alarms and wakeup */
180 err = class_device_create_file(class_dev,
181 &class_device_attr_wakealarm);
182 if (err) {
183 dev_err(class_dev->dev, "failed to create %s\n",
184 "alarm attribute");
185 sysfs_remove_group(&class_dev->kobj, &rtc_attr_group);
189 return err;
192 static void rtc_sysfs_remove_device(struct class_device *class_dev,
193 struct class_interface *class_intf)
195 if (rtc_does_wakealarm(class_dev))
196 class_device_remove_file(class_dev,
197 &class_device_attr_wakealarm);
198 sysfs_remove_group(&class_dev->kobj, &rtc_attr_group);
201 /* interface registration */
203 static struct class_interface rtc_sysfs_interface = {
204 .add = &rtc_sysfs_add_device,
205 .remove = &rtc_sysfs_remove_device,
208 static int __init rtc_sysfs_init(void)
210 return rtc_interface_register(&rtc_sysfs_interface);
213 static void __exit rtc_sysfs_exit(void)
215 class_interface_unregister(&rtc_sysfs_interface);
218 subsys_initcall(rtc_sysfs_init);
219 module_exit(rtc_sysfs_exit);
221 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
222 MODULE_DESCRIPTION("RTC class sysfs interface");
223 MODULE_LICENSE("GPL");