rtc: simplified rtc sysfs attribute handling
[deliverable/linux.git] / 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 #include "rtc-core.h"
16
17
18 /* device attributes */
19
20 static ssize_t rtc_sysfs_show_name(struct class_device *dev, char *buf)
21 {
22 return sprintf(buf, "%s\n", to_rtc_device(dev)->name);
23 }
24
25 static ssize_t rtc_sysfs_show_date(struct class_device *dev, char *buf)
26 {
27 ssize_t retval;
28 struct rtc_time tm;
29
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);
34 }
35
36 return retval;
37 }
38
39 static ssize_t rtc_sysfs_show_time(struct class_device *dev, char *buf)
40 {
41 ssize_t retval;
42 struct rtc_time tm;
43
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);
48 }
49
50 return retval;
51 }
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(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);
63 }
64
65 return retval;
66 }
67
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 { },
74 };
75
76 static ssize_t
77 rtc_sysfs_show_wakealarm(struct class_device *dev, char *buf)
78 {
79 ssize_t retval;
80 unsigned long alarm;
81 struct rtc_wkalrm alm;
82
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.
87 *
88 * REVISIT maybe we should require RTC implementations to
89 * disable the RTC alarm after it triggers, for uniformity.
90 */
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);
95 }
96
97 return retval;
98 }
99
100 static ssize_t
101 rtc_sysfs_set_wakealarm(struct class_device *dev, const char *buf, size_t n)
102 {
103 ssize_t retval;
104 unsigned long now, alarm;
105 struct rtc_wkalrm alm;
106 struct rtc_device *rtc = to_rtc_device(dev);
107
108 /* Only request alarms that trigger in the future. Disable them
109 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
110 */
111 retval = rtc_read_time(rtc, &alm.time);
112 if (retval < 0)
113 return retval;
114 rtc_tm_to_time(&alm.time, &now);
115
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.
121 */
122 retval = rtc_read_alarm(rtc, &alm);
123 if (retval < 0)
124 return retval;
125 if (alm.enabled)
126 return -EBUSY;
127
128 alm.enabled = 1;
129 } else {
130 alm.enabled = 0;
131
132 /* Provide a valid future alarm time. Linux isn't EFI,
133 * this time won't be ignored when disabling the alarm.
134 */
135 alarm = now + 300;
136 }
137 rtc_time_to_tm(alarm, &alm.time);
138
139 retval = rtc_set_alarm(rtc, &alm);
140 return (retval < 0) ? retval : n;
141 }
142 static const CLASS_DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR,
143 rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm);
144
145
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.)
150 */
151 static inline int rtc_does_wakealarm(struct rtc_device *rtc)
152 {
153 if (!device_can_wakeup(rtc->class_dev.dev))
154 return 0;
155 return rtc->ops->set_alarm != NULL;
156 }
157
158
159 void rtc_sysfs_add_device(struct rtc_device *rtc)
160 {
161 int err;
162
163 /* not all RTCs support both alarms and wakeup */
164 if (!rtc_does_wakealarm(rtc))
165 return;
166
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);
173 }
174
175 void rtc_sysfs_del_device(struct rtc_device *rtc)
176 {
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);
181 }
182
183 void __init rtc_sysfs_init(struct class *rtc_class)
184 {
185 rtc_class->class_dev_attrs = rtc_attrs;
186 }
This page took 0.033637 seconds and 5 git commands to generate.