Merge master.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[linux-2.6] / drivers / rtc / rtc-sysfs.c
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
15 /* device attributes */
16
17 static ssize_t rtc_sysfs_show_name(struct class_device *dev, char *buf)
18 {
19         return sprintf(buf, "%s\n", to_rtc_device(dev)->name);
20 }
21 static CLASS_DEVICE_ATTR(name, S_IRUGO, rtc_sysfs_show_name, NULL);
22
23 static ssize_t rtc_sysfs_show_date(struct class_device *dev, char *buf)
24 {
25         ssize_t retval;
26         struct rtc_time tm;
27
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);
32         }
33
34         return retval;
35 }
36 static CLASS_DEVICE_ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL);
37
38 static ssize_t rtc_sysfs_show_time(struct class_device *dev, char *buf)
39 {
40         ssize_t retval;
41         struct rtc_time tm;
42
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);
47         }
48
49         return retval;
50 }
51 static CLASS_DEVICE_ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL);
52
53 static ssize_t rtc_sysfs_show_since_epoch(struct class_device *dev, char *buf)
54 {
55         ssize_t retval;
56         struct rtc_time tm;
57
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);
63         }
64
65         return retval;
66 }
67 static CLASS_DEVICE_ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL);
68
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,
75 };
76
77 static struct attribute_group rtc_attr_group = {
78         .attrs = rtc_attrs,
79 };
80
81
82 static ssize_t
83 rtc_sysfs_show_wakealarm(struct class_device *dev, char *buf)
84 {
85         ssize_t retval;
86         unsigned long alarm;
87         struct rtc_wkalrm alm;
88
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.
93          *
94          * REVISIT maybe we should require RTC implementations to
95          * disable the RTC alarm after it triggers, for uniformity.
96          */
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);
101         }
102
103         return retval;
104 }
105
106 static ssize_t
107 rtc_sysfs_set_wakealarm(struct class_device *dev, const char *buf, size_t n)
108 {
109         ssize_t retval;
110         unsigned long now, alarm;
111         struct rtc_wkalrm alm;
112
113         /* Only request alarms that trigger in the future.  Disable them
114          * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
115          */
116         retval = rtc_read_time(dev, &alm.time);
117         if (retval < 0)
118                 return retval;
119         rtc_tm_to_time(&alm.time, &now);
120
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.
126                  */
127                 retval = rtc_read_alarm(dev, &alm);
128                 if (retval < 0)
129                         return retval;
130                 if (alm.enabled)
131                         return -EBUSY;
132
133                 alm.enabled = 1;
134         } else {
135                 alm.enabled = 0;
136
137                 /* Provide a valid future alarm time.  Linux isn't EFI,
138                  * this time won't be ignored when disabling the alarm.
139                  */
140                 alarm = now + 300;
141         }
142         rtc_time_to_tm(alarm, &alm.time);
143
144         retval = rtc_set_alarm(dev, &alm);
145         return (retval < 0) ? retval : n;
146 }
147 static const CLASS_DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR,
148                 rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm);
149
150
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.)
155  */
156 static inline int rtc_does_wakealarm(struct class_device *class_dev)
157 {
158         struct rtc_device *rtc;
159
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;
164 }
165
166
167 static int rtc_sysfs_add_device(struct class_device *class_dev,
168                                         struct class_interface *class_intf)
169 {
170         int err;
171
172         dev_dbg(class_dev->dev, "rtc intf: sysfs\n");
173
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);
186                 }
187         }
188
189         return err;
190 }
191
192 static void rtc_sysfs_remove_device(struct class_device *class_dev,
193                                 struct class_interface *class_intf)
194 {
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);
199 }
200
201 /* interface registration */
202
203 static struct class_interface rtc_sysfs_interface = {
204         .add = &rtc_sysfs_add_device,
205         .remove = &rtc_sysfs_remove_device,
206 };
207
208 static int __init rtc_sysfs_init(void)
209 {
210         return rtc_interface_register(&rtc_sysfs_interface);
211 }
212
213 static void __exit rtc_sysfs_exit(void)
214 {
215         class_interface_unregister(&rtc_sysfs_interface);
216 }
217
218 subsys_initcall(rtc_sysfs_init);
219 module_exit(rtc_sysfs_exit);
220
221 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
222 MODULE_DESCRIPTION("RTC class sysfs interface");
223 MODULE_LICENSE("GPL");