scsi: libsas: Remove scsi_to_u32()
[linux-2.6-microblaze.git] / drivers / 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 /*
21  * NOTE:  RTC times displayed in sysfs use the RTC's timezone.  That's
22  * ideally UTC.  However, PCs that also boot to MS-Windows normally use
23  * the local time and change to match daylight savings time.  That affects
24  * attributes including date, time, since_epoch, and wakealarm.
25  */
26
27 static ssize_t
28 name_show(struct device *dev, struct device_attribute *attr, char *buf)
29 {
30         return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
31                        dev_name(dev->parent));
32 }
33 static DEVICE_ATTR_RO(name);
34
35 static ssize_t
36 date_show(struct device *dev, struct device_attribute *attr, char *buf)
37 {
38         ssize_t retval;
39         struct rtc_time tm;
40
41         retval = rtc_read_time(to_rtc_device(dev), &tm);
42         if (retval)
43                 return retval;
44
45         return sprintf(buf, "%ptRd\n", &tm);
46 }
47 static DEVICE_ATTR_RO(date);
48
49 static ssize_t
50 time_show(struct device *dev, struct device_attribute *attr, char *buf)
51 {
52         ssize_t retval;
53         struct rtc_time tm;
54
55         retval = rtc_read_time(to_rtc_device(dev), &tm);
56         if (retval)
57                 return retval;
58
59         return sprintf(buf, "%ptRt\n", &tm);
60 }
61 static DEVICE_ATTR_RO(time);
62
63 static ssize_t
64 since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
65 {
66         ssize_t retval;
67         struct rtc_time tm;
68
69         retval = rtc_read_time(to_rtc_device(dev), &tm);
70         if (retval == 0) {
71                 time64_t time;
72
73                 time = rtc_tm_to_time64(&tm);
74                 retval = sprintf(buf, "%lld\n", time);
75         }
76
77         return retval;
78 }
79 static DEVICE_ATTR_RO(since_epoch);
80
81 static ssize_t
82 max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
83 {
84         return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
85 }
86
87 static ssize_t
88 max_user_freq_store(struct device *dev, struct device_attribute *attr,
89                 const char *buf, size_t n)
90 {
91         struct rtc_device *rtc = to_rtc_device(dev);
92         unsigned long val;
93         int err;
94
95         err = kstrtoul(buf, 0, &val);
96         if (err)
97                 return err;
98
99         if (val >= 4096 || val == 0)
100                 return -EINVAL;
101
102         rtc->max_user_freq = (int)val;
103
104         return n;
105 }
106 static DEVICE_ATTR_RW(max_user_freq);
107
108 /**
109  * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
110  *
111  * Returns 1 if the system clock was set by this RTC at the last
112  * boot or resume event.
113  */
114 static ssize_t
115 hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
116 {
117 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
118         if (rtc_hctosys_ret == 0 &&
119                         strcmp(dev_name(&to_rtc_device(dev)->dev),
120                                 CONFIG_RTC_HCTOSYS_DEVICE) == 0)
121                 return sprintf(buf, "1\n");
122         else
123 #endif
124                 return sprintf(buf, "0\n");
125 }
126 static DEVICE_ATTR_RO(hctosys);
127
128 static ssize_t
129 wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
130 {
131         ssize_t retval;
132         time64_t alarm;
133         struct rtc_wkalrm alm;
134
135         /* Don't show disabled alarms.  For uniformity, RTC alarms are
136          * conceptually one-shot, even though some common RTCs (on PCs)
137          * don't actually work that way.
138          *
139          * NOTE: RTC implementations where the alarm doesn't match an
140          * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
141          * alarms after they trigger, to ensure one-shot semantics.
142          */
143         retval = rtc_read_alarm(to_rtc_device(dev), &alm);
144         if (retval == 0 && alm.enabled) {
145                 alarm = rtc_tm_to_time64(&alm.time);
146                 retval = sprintf(buf, "%lld\n", alarm);
147         }
148
149         return retval;
150 }
151
152 static ssize_t
153 wakealarm_store(struct device *dev, struct device_attribute *attr,
154                 const char *buf, size_t n)
155 {
156         ssize_t retval;
157         time64_t now, alarm;
158         time64_t push = 0;
159         struct rtc_wkalrm alm;
160         struct rtc_device *rtc = to_rtc_device(dev);
161         const char *buf_ptr;
162         int adjust = 0;
163
164         /* Only request alarms that trigger in the future.  Disable them
165          * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
166          */
167         retval = rtc_read_time(rtc, &alm.time);
168         if (retval < 0)
169                 return retval;
170         now = rtc_tm_to_time64(&alm.time);
171
172         buf_ptr = buf;
173         if (*buf_ptr == '+') {
174                 buf_ptr++;
175                 if (*buf_ptr == '=') {
176                         buf_ptr++;
177                         push = 1;
178                 } else
179                         adjust = 1;
180         }
181         retval = kstrtos64(buf_ptr, 0, &alarm);
182         if (retval)
183                 return retval;
184         if (adjust) {
185                 alarm += now;
186         }
187         if (alarm > now || push) {
188                 /* Avoid accidentally clobbering active alarms; we can't
189                  * entirely prevent that here, without even the minimal
190                  * locking from the /dev/rtcN api.
191                  */
192                 retval = rtc_read_alarm(rtc, &alm);
193                 if (retval < 0)
194                         return retval;
195                 if (alm.enabled) {
196                         if (push) {
197                                 push = rtc_tm_to_time64(&alm.time);
198                                 alarm += push;
199                         } else
200                                 return -EBUSY;
201                 } else if (push)
202                         return -EINVAL;
203                 alm.enabled = 1;
204         } else {
205                 alm.enabled = 0;
206
207                 /* Provide a valid future alarm time.  Linux isn't EFI,
208                  * this time won't be ignored when disabling the alarm.
209                  */
210                 alarm = now + 300;
211         }
212         rtc_time64_to_tm(alarm, &alm.time);
213
214         retval = rtc_set_alarm(rtc, &alm);
215         return (retval < 0) ? retval : n;
216 }
217 static DEVICE_ATTR_RW(wakealarm);
218
219 static ssize_t
220 offset_show(struct device *dev, struct device_attribute *attr, char *buf)
221 {
222         ssize_t retval;
223         long offset;
224
225         retval = rtc_read_offset(to_rtc_device(dev), &offset);
226         if (retval == 0)
227                 retval = sprintf(buf, "%ld\n", offset);
228
229         return retval;
230 }
231
232 static ssize_t
233 offset_store(struct device *dev, struct device_attribute *attr,
234              const char *buf, size_t n)
235 {
236         ssize_t retval;
237         long offset;
238
239         retval = kstrtol(buf, 10, &offset);
240         if (retval == 0)
241                 retval = rtc_set_offset(to_rtc_device(dev), offset);
242
243         return (retval < 0) ? retval : n;
244 }
245 static DEVICE_ATTR_RW(offset);
246
247 static ssize_t
248 range_show(struct device *dev, struct device_attribute *attr, char *buf)
249 {
250         return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min,
251                        to_rtc_device(dev)->range_max);
252 }
253 static DEVICE_ATTR_RO(range);
254
255 static struct attribute *rtc_attrs[] = {
256         &dev_attr_name.attr,
257         &dev_attr_date.attr,
258         &dev_attr_time.attr,
259         &dev_attr_since_epoch.attr,
260         &dev_attr_max_user_freq.attr,
261         &dev_attr_hctosys.attr,
262         &dev_attr_wakealarm.attr,
263         &dev_attr_offset.attr,
264         &dev_attr_range.attr,
265         NULL,
266 };
267
268 /* The reason to trigger an alarm with no process watching it (via sysfs)
269  * is its side effect:  waking from a system state like suspend-to-RAM or
270  * suspend-to-disk.  So: no attribute unless that side effect is possible.
271  * (Userspace may disable that mechanism later.)
272  */
273 static bool rtc_does_wakealarm(struct rtc_device *rtc)
274 {
275         if (!device_can_wakeup(rtc->dev.parent))
276                 return false;
277
278         return rtc->ops->set_alarm != NULL;
279 }
280
281 static umode_t rtc_attr_is_visible(struct kobject *kobj,
282                                    struct attribute *attr, int n)
283 {
284         struct device *dev = container_of(kobj, struct device, kobj);
285         struct rtc_device *rtc = to_rtc_device(dev);
286         umode_t mode = attr->mode;
287
288         if (attr == &dev_attr_wakealarm.attr) {
289                 if (!rtc_does_wakealarm(rtc))
290                         mode = 0;
291         } else if (attr == &dev_attr_offset.attr) {
292                 if (!rtc->ops->set_offset)
293                         mode = 0;
294         } else if (attr == &dev_attr_range.attr) {
295                 if (!(rtc->range_max - rtc->range_min))
296                         mode = 0;
297         }
298
299         return mode;
300 }
301
302 static struct attribute_group rtc_attr_group = {
303         .is_visible     = rtc_attr_is_visible,
304         .attrs          = rtc_attrs,
305 };
306
307 static const struct attribute_group *rtc_attr_groups[] = {
308         &rtc_attr_group,
309         NULL
310 };
311
312 const struct attribute_group **rtc_get_dev_attribute_groups(void)
313 {
314         return rtc_attr_groups;
315 }
316
317 int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
318 {
319         size_t old_cnt = 0, add_cnt = 0, new_cnt;
320         const struct attribute_group **groups, **old;
321
322         if (rtc->registered)
323                 return -EINVAL;
324         if (!grps)
325                 return -EINVAL;
326
327         groups = rtc->dev.groups;
328         if (groups)
329                 for (; *groups; groups++)
330                         old_cnt++;
331
332         for (groups = grps; *groups; groups++)
333                 add_cnt++;
334
335         new_cnt = old_cnt + add_cnt + 1;
336         groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL);
337         if (!groups)
338                 return -ENOMEM;
339         memcpy(groups, rtc->dev.groups, old_cnt * sizeof(*groups));
340         memcpy(groups + old_cnt, grps, add_cnt * sizeof(*groups));
341         groups[old_cnt + add_cnt] = NULL;
342
343         old = rtc->dev.groups;
344         rtc->dev.groups = groups;
345         if (old && old != rtc_attr_groups)
346                 devm_kfree(&rtc->dev, old);
347
348         return 0;
349 }
350 EXPORT_SYMBOL(rtc_add_groups);
351
352 int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
353 {
354         const struct attribute_group *groups[] = { grp, NULL };
355
356         return rtc_add_groups(rtc, groups);
357 }
358 EXPORT_SYMBOL(rtc_add_group);