rtc-sysfs.c 6.1 KB

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