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>
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
)
30 retval
= rtc_read_time(to_rtc_device(dev
), &tm
);
32 retval
= sprintf(buf
, "%04d-%02d-%02d\n",
33 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
);
39 static ssize_t
rtc_sysfs_show_time(struct class_device
*dev
, char *buf
)
44 retval
= rtc_read_time(to_rtc_device(dev
), &tm
);
46 retval
= sprintf(buf
, "%02d:%02d:%02d\n",
47 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
53 static ssize_t
rtc_sysfs_show_since_epoch(struct class_device
*dev
, char *buf
)
58 retval
= rtc_read_time(to_rtc_device(dev
), &tm
);
61 rtc_tm_to_time(&tm
, &time
);
62 retval
= sprintf(buf
, "%lu\n", time
);
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
),
77 rtc_sysfs_show_wakealarm(struct class_device
*dev
, char *buf
)
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
);
101 rtc_sysfs_set_wakealarm(struct class_device
*dev
, const char *buf
, size_t n
)
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
);
114 rtc_tm_to_time(&alm
.time
, &now
);
116 alarm
= simple_strtoul(buf
, NULL
, 0);
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
);
132 /* Provide a valid future alarm time. Linux isn't EFI,
133 * this time won't be ignored when disabling the alarm.
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
))
155 return rtc
->ops
->set_alarm
!= NULL
;
159 void rtc_sysfs_add_device(struct rtc_device
*rtc
)
163 /* not all RTCs support both alarms and wakeup */
164 if (!rtc_does_wakealarm(rtc
))
167 err
= class_device_create_file(&rtc
->class_dev
,
168 &class_device_attr_wakealarm
);
170 dev_err(rtc
->class_dev
.dev
, "failed to create "
171 "alarm attribute, %d",
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
;