rtc-sysfs.c 6.6 KB

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