rtc-sysfs.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. char *buf_ptr;
  124. int adjust = 0;
  125. /* Only request alarms that trigger in the future. Disable them
  126. * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
  127. */
  128. retval = rtc_read_time(rtc, &alm.time);
  129. if (retval < 0)
  130. return retval;
  131. rtc_tm_to_time(&alm.time, &now);
  132. buf_ptr = (char *)buf;
  133. if (*buf_ptr == '+') {
  134. buf_ptr++;
  135. adjust = 1;
  136. }
  137. alarm = simple_strtoul(buf_ptr, NULL, 0);
  138. if (adjust) {
  139. alarm += now;
  140. }
  141. if (alarm > now) {
  142. /* Avoid accidentally clobbering active alarms; we can't
  143. * entirely prevent that here, without even the minimal
  144. * locking from the /dev/rtcN api.
  145. */
  146. retval = rtc_read_alarm(rtc, &alm);
  147. if (retval < 0)
  148. return retval;
  149. if (alm.enabled)
  150. return -EBUSY;
  151. alm.enabled = 1;
  152. } else {
  153. alm.enabled = 0;
  154. /* Provide a valid future alarm time. Linux isn't EFI,
  155. * this time won't be ignored when disabling the alarm.
  156. */
  157. alarm = now + 300;
  158. }
  159. rtc_time_to_tm(alarm, &alm.time);
  160. retval = rtc_set_alarm(rtc, &alm);
  161. return (retval < 0) ? retval : n;
  162. }
  163. static DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR,
  164. rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm);
  165. /* The reason to trigger an alarm with no process watching it (via sysfs)
  166. * is its side effect: waking from a system state like suspend-to-RAM or
  167. * suspend-to-disk. So: no attribute unless that side effect is possible.
  168. * (Userspace may disable that mechanism later.)
  169. */
  170. static inline int rtc_does_wakealarm(struct rtc_device *rtc)
  171. {
  172. if (!device_can_wakeup(rtc->dev.parent))
  173. return 0;
  174. return rtc->ops->set_alarm != NULL;
  175. }
  176. void rtc_sysfs_add_device(struct rtc_device *rtc)
  177. {
  178. int err;
  179. /* not all RTCs support both alarms and wakeup */
  180. if (!rtc_does_wakealarm(rtc))
  181. return;
  182. err = device_create_file(&rtc->dev, &dev_attr_wakealarm);
  183. if (err)
  184. dev_err(rtc->dev.parent,
  185. "failed to create alarm attribute, %d\n", err);
  186. }
  187. void rtc_sysfs_del_device(struct rtc_device *rtc)
  188. {
  189. /* REVISIT did we add it successfully? */
  190. if (rtc_does_wakealarm(rtc))
  191. device_remove_file(&rtc->dev, &dev_attr_wakealarm);
  192. }
  193. void __init rtc_sysfs_init(struct class *rtc_class)
  194. {
  195. rtc_class->dev_attrs = rtc_attrs;
  196. }