rtc-sysfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include <linux/module.h>
  12. #include <linux/rtc.h>
  13. #include "rtc-core.h"
  14. /* device attributes */
  15. /*
  16. * NOTE: RTC times displayed in sysfs use the RTC's timezone. That's
  17. * ideally UTC. However, PCs that also boot to MS-Windows normally use
  18. * the local time and change to match daylight savings time. That affects
  19. * attributes including date, time, since_epoch, and wakealarm.
  20. */
  21. static ssize_t
  22. rtc_sysfs_show_name(struct device *dev, struct device_attribute *attr,
  23. char *buf)
  24. {
  25. return sprintf(buf, "%s\n", to_rtc_device(dev)->name);
  26. }
  27. static ssize_t
  28. rtc_sysfs_show_date(struct device *dev, struct device_attribute *attr,
  29. char *buf)
  30. {
  31. ssize_t retval;
  32. struct rtc_time tm;
  33. retval = rtc_read_time(to_rtc_device(dev), &tm);
  34. if (retval == 0) {
  35. retval = sprintf(buf, "%04d-%02d-%02d\n",
  36. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  37. }
  38. return retval;
  39. }
  40. static ssize_t
  41. rtc_sysfs_show_time(struct device *dev, struct device_attribute *attr,
  42. char *buf)
  43. {
  44. ssize_t retval;
  45. struct rtc_time tm;
  46. retval = rtc_read_time(to_rtc_device(dev), &tm);
  47. if (retval == 0) {
  48. retval = sprintf(buf, "%02d:%02d:%02d\n",
  49. tm.tm_hour, tm.tm_min, tm.tm_sec);
  50. }
  51. return retval;
  52. }
  53. static ssize_t
  54. rtc_sysfs_show_since_epoch(struct device *dev, struct device_attribute *attr,
  55. char *buf)
  56. {
  57. ssize_t retval;
  58. struct rtc_time tm;
  59. retval = rtc_read_time(to_rtc_device(dev), &tm);
  60. if (retval == 0) {
  61. unsigned long time;
  62. rtc_tm_to_time(&tm, &time);
  63. retval = sprintf(buf, "%lu\n", time);
  64. }
  65. return retval;
  66. }
  67. static ssize_t
  68. rtc_sysfs_show_max_user_freq(struct device *dev, struct device_attribute *attr,
  69. char *buf)
  70. {
  71. return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
  72. }
  73. static ssize_t
  74. rtc_sysfs_set_max_user_freq(struct device *dev, struct device_attribute *attr,
  75. const char *buf, size_t n)
  76. {
  77. struct rtc_device *rtc = to_rtc_device(dev);
  78. unsigned long val = simple_strtoul(buf, NULL, 0);
  79. if (val >= 4096 || val == 0)
  80. return -EINVAL;
  81. rtc->max_user_freq = (int)val;
  82. return n;
  83. }
  84. static struct device_attribute rtc_attrs[] = {
  85. __ATTR(name, S_IRUGO, rtc_sysfs_show_name, NULL),
  86. __ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL),
  87. __ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL),
  88. __ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL),
  89. __ATTR(max_user_freq, S_IRUGO | S_IWUSR, rtc_sysfs_show_max_user_freq,
  90. rtc_sysfs_set_max_user_freq),
  91. { },
  92. };
  93. static ssize_t
  94. rtc_sysfs_show_wakealarm(struct device *dev, struct device_attribute *attr,
  95. char *buf)
  96. {
  97. ssize_t retval;
  98. unsigned long alarm;
  99. struct rtc_wkalrm alm;
  100. /* Don't show disabled alarms. For uniformity, RTC alarms are
  101. * conceptually one-shot, even though some common RTCs (on PCs)
  102. * don't actually work that way.
  103. *
  104. * NOTE: RTC implementations where the alarm doesn't match an
  105. * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
  106. * alarms after they trigger, to ensure one-shot semantics.
  107. */
  108. retval = rtc_read_alarm(to_rtc_device(dev), &alm);
  109. if (retval == 0 && alm.enabled) {
  110. rtc_tm_to_time(&alm.time, &alarm);
  111. retval = sprintf(buf, "%lu\n", alarm);
  112. }
  113. return retval;
  114. }
  115. static ssize_t
  116. rtc_sysfs_set_wakealarm(struct device *dev, struct device_attribute *attr,
  117. const char *buf, size_t n)
  118. {
  119. ssize_t retval;
  120. unsigned long now, alarm;
  121. struct rtc_wkalrm alm;
  122. struct rtc_device *rtc = to_rtc_device(dev);
  123. /* Only request alarms that trigger in the future. Disable them
  124. * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
  125. */
  126. retval = rtc_read_time(rtc, &alm.time);
  127. if (retval < 0)
  128. return retval;
  129. rtc_tm_to_time(&alm.time, &now);
  130. alarm = simple_strtoul(buf, NULL, 0);
  131. if (alarm > now) {
  132. /* Avoid accidentally clobbering active alarms; we can't
  133. * entirely prevent that here, without even the minimal
  134. * locking from the /dev/rtcN api.
  135. */
  136. retval = rtc_read_alarm(rtc, &alm);
  137. if (retval < 0)
  138. return retval;
  139. if (alm.enabled)
  140. return -EBUSY;
  141. alm.enabled = 1;
  142. } else {
  143. alm.enabled = 0;
  144. /* Provide a valid future alarm time. Linux isn't EFI,
  145. * this time won't be ignored when disabling the alarm.
  146. */
  147. alarm = now + 300;
  148. }
  149. rtc_time_to_tm(alarm, &alm.time);
  150. retval = rtc_set_alarm(rtc, &alm);
  151. return (retval < 0) ? retval : n;
  152. }
  153. static DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR,
  154. rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm);
  155. /* The reason to trigger an alarm with no process watching it (via sysfs)
  156. * is its side effect: waking from a system state like suspend-to-RAM or
  157. * suspend-to-disk. So: no attribute unless that side effect is possible.
  158. * (Userspace may disable that mechanism later.)
  159. */
  160. static inline int rtc_does_wakealarm(struct rtc_device *rtc)
  161. {
  162. if (!device_can_wakeup(rtc->dev.parent))
  163. return 0;
  164. return rtc->ops->set_alarm != NULL;
  165. }
  166. void rtc_sysfs_add_device(struct rtc_device *rtc)
  167. {
  168. int err;
  169. /* not all RTCs support both alarms and wakeup */
  170. if (!rtc_does_wakealarm(rtc))
  171. return;
  172. err = device_create_file(&rtc->dev, &dev_attr_wakealarm);
  173. if (err)
  174. dev_err(rtc->dev.parent,
  175. "failed to create alarm attribute, %d\n", err);
  176. }
  177. void rtc_sysfs_del_device(struct rtc_device *rtc)
  178. {
  179. /* REVISIT did we add it successfully? */
  180. if (rtc_does_wakealarm(rtc))
  181. device_remove_file(&rtc->dev, &dev_attr_wakealarm);
  182. }
  183. void __init rtc_sysfs_init(struct class *rtc_class)
  184. {
  185. rtc_class->dev_attrs = rtc_attrs;
  186. }