rtc-sysfs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 ssize_t
  85. rtc_sysfs_show_hctosys(struct device *dev, struct device_attribute *attr,
  86. char *buf)
  87. {
  88. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  89. if (strcmp(dev_name(&to_rtc_device(dev)->dev),
  90. CONFIG_RTC_HCTOSYS_DEVICE) == 0)
  91. return sprintf(buf, "1\n");
  92. else
  93. #endif
  94. return sprintf(buf, "0\n");
  95. }
  96. static struct device_attribute rtc_attrs[] = {
  97. __ATTR(name, S_IRUGO, rtc_sysfs_show_name, NULL),
  98. __ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL),
  99. __ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL),
  100. __ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL),
  101. __ATTR(max_user_freq, S_IRUGO | S_IWUSR, rtc_sysfs_show_max_user_freq,
  102. rtc_sysfs_set_max_user_freq),
  103. __ATTR(hctosys, S_IRUGO, rtc_sysfs_show_hctosys, NULL),
  104. { },
  105. };
  106. static ssize_t
  107. rtc_sysfs_show_wakealarm(struct device *dev, struct device_attribute *attr,
  108. char *buf)
  109. {
  110. ssize_t retval;
  111. unsigned long alarm;
  112. struct rtc_wkalrm alm;
  113. /* Don't show disabled alarms. For uniformity, RTC alarms are
  114. * conceptually one-shot, even though some common RTCs (on PCs)
  115. * don't actually work that way.
  116. *
  117. * NOTE: RTC implementations where the alarm doesn't match an
  118. * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
  119. * alarms after they trigger, to ensure one-shot semantics.
  120. */
  121. retval = rtc_read_alarm(to_rtc_device(dev), &alm);
  122. if (retval == 0 && alm.enabled) {
  123. rtc_tm_to_time(&alm.time, &alarm);
  124. retval = sprintf(buf, "%lu\n", alarm);
  125. }
  126. return retval;
  127. }
  128. static ssize_t
  129. rtc_sysfs_set_wakealarm(struct device *dev, struct device_attribute *attr,
  130. const char *buf, size_t n)
  131. {
  132. ssize_t retval;
  133. unsigned long now, alarm;
  134. struct rtc_wkalrm alm;
  135. struct rtc_device *rtc = to_rtc_device(dev);
  136. char *buf_ptr;
  137. int adjust = 0;
  138. /* Only request alarms that trigger in the future. Disable them
  139. * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
  140. */
  141. retval = rtc_read_time(rtc, &alm.time);
  142. if (retval < 0)
  143. return retval;
  144. rtc_tm_to_time(&alm.time, &now);
  145. buf_ptr = (char *)buf;
  146. if (*buf_ptr == '+') {
  147. buf_ptr++;
  148. adjust = 1;
  149. }
  150. alarm = simple_strtoul(buf_ptr, NULL, 0);
  151. if (adjust) {
  152. alarm += now;
  153. }
  154. if (alarm > now) {
  155. /* Avoid accidentally clobbering active alarms; we can't
  156. * entirely prevent that here, without even the minimal
  157. * locking from the /dev/rtcN api.
  158. */
  159. retval = rtc_read_alarm(rtc, &alm);
  160. if (retval < 0)
  161. return retval;
  162. if (alm.enabled)
  163. return -EBUSY;
  164. alm.enabled = 1;
  165. } else {
  166. alm.enabled = 0;
  167. /* Provide a valid future alarm time. Linux isn't EFI,
  168. * this time won't be ignored when disabling the alarm.
  169. */
  170. alarm = now + 300;
  171. }
  172. rtc_time_to_tm(alarm, &alm.time);
  173. retval = rtc_set_alarm(rtc, &alm);
  174. return (retval < 0) ? retval : n;
  175. }
  176. static DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR,
  177. rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm);
  178. /* The reason to trigger an alarm with no process watching it (via sysfs)
  179. * is its side effect: waking from a system state like suspend-to-RAM or
  180. * suspend-to-disk. So: no attribute unless that side effect is possible.
  181. * (Userspace may disable that mechanism later.)
  182. */
  183. static inline int rtc_does_wakealarm(struct rtc_device *rtc)
  184. {
  185. if (!device_can_wakeup(rtc->dev.parent))
  186. return 0;
  187. return rtc->ops->set_alarm != NULL;
  188. }
  189. void rtc_sysfs_add_device(struct rtc_device *rtc)
  190. {
  191. int err;
  192. /* not all RTCs support both alarms and wakeup */
  193. if (!rtc_does_wakealarm(rtc))
  194. return;
  195. err = device_create_file(&rtc->dev, &dev_attr_wakealarm);
  196. if (err)
  197. dev_err(rtc->dev.parent,
  198. "failed to create alarm attribute, %d\n", err);
  199. }
  200. void rtc_sysfs_del_device(struct rtc_device *rtc)
  201. {
  202. /* REVISIT did we add it successfully? */
  203. if (rtc_does_wakealarm(rtc))
  204. device_remove_file(&rtc->dev, &dev_attr_wakealarm);
  205. }
  206. void __init rtc_sysfs_init(struct class *rtc_class)
  207. {
  208. rtc_class->dev_attrs = rtc_attrs;
  209. }