rtc-sysfs.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. name_show(struct device *dev, struct device_attribute *attr, char *buf)
  23. {
  24. return sprintf(buf, "%s\n", to_rtc_device(dev)->name);
  25. }
  26. static DEVICE_ATTR_RO(name);
  27. static ssize_t
  28. date_show(struct device *dev, struct device_attribute *attr, char *buf)
  29. {
  30. ssize_t retval;
  31. struct rtc_time tm;
  32. retval = rtc_read_time(to_rtc_device(dev), &tm);
  33. if (retval == 0) {
  34. retval = sprintf(buf, "%04d-%02d-%02d\n",
  35. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  36. }
  37. return retval;
  38. }
  39. static DEVICE_ATTR_RO(date);
  40. static ssize_t
  41. time_show(struct device *dev, struct device_attribute *attr, char *buf)
  42. {
  43. ssize_t retval;
  44. struct rtc_time tm;
  45. retval = rtc_read_time(to_rtc_device(dev), &tm);
  46. if (retval == 0) {
  47. retval = sprintf(buf, "%02d:%02d:%02d\n",
  48. tm.tm_hour, tm.tm_min, tm.tm_sec);
  49. }
  50. return retval;
  51. }
  52. static DEVICE_ATTR_RO(time);
  53. static ssize_t
  54. since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
  55. {
  56. ssize_t retval;
  57. struct rtc_time tm;
  58. retval = rtc_read_time(to_rtc_device(dev), &tm);
  59. if (retval == 0) {
  60. unsigned long time;
  61. rtc_tm_to_time(&tm, &time);
  62. retval = sprintf(buf, "%lu\n", time);
  63. }
  64. return retval;
  65. }
  66. static DEVICE_ATTR_RO(since_epoch);
  67. static ssize_t
  68. max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
  69. {
  70. return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
  71. }
  72. static ssize_t
  73. max_user_freq_store(struct device *dev, struct device_attribute *attr,
  74. const char *buf, size_t n)
  75. {
  76. struct rtc_device *rtc = to_rtc_device(dev);
  77. unsigned long val = simple_strtoul(buf, NULL, 0);
  78. if (val >= 4096 || val == 0)
  79. return -EINVAL;
  80. rtc->max_user_freq = (int)val;
  81. return n;
  82. }
  83. static DEVICE_ATTR_RW(max_user_freq);
  84. /**
  85. * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
  86. *
  87. * Returns 1 if the system clock was set by this RTC at the last
  88. * boot or resume event.
  89. */
  90. static ssize_t
  91. hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
  92. {
  93. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  94. if (rtc_hctosys_ret == 0 &&
  95. strcmp(dev_name(&to_rtc_device(dev)->dev),
  96. CONFIG_RTC_HCTOSYS_DEVICE) == 0)
  97. return sprintf(buf, "1\n");
  98. else
  99. #endif
  100. return sprintf(buf, "0\n");
  101. }
  102. static DEVICE_ATTR_RO(hctosys);
  103. static struct attribute *rtc_attrs[] = {
  104. &dev_attr_name.attr,
  105. &dev_attr_date.attr,
  106. &dev_attr_time.attr,
  107. &dev_attr_since_epoch.attr,
  108. &dev_attr_max_user_freq.attr,
  109. &dev_attr_hctosys.attr,
  110. NULL,
  111. };
  112. ATTRIBUTE_GROUPS(rtc);
  113. static ssize_t
  114. rtc_sysfs_show_wakealarm(struct device *dev, struct device_attribute *attr,
  115. char *buf)
  116. {
  117. ssize_t retval;
  118. unsigned long alarm;
  119. struct rtc_wkalrm alm;
  120. /* Don't show disabled alarms. For uniformity, RTC alarms are
  121. * conceptually one-shot, even though some common RTCs (on PCs)
  122. * don't actually work that way.
  123. *
  124. * NOTE: RTC implementations where the alarm doesn't match an
  125. * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
  126. * alarms after they trigger, to ensure one-shot semantics.
  127. */
  128. retval = rtc_read_alarm(to_rtc_device(dev), &alm);
  129. if (retval == 0 && alm.enabled) {
  130. rtc_tm_to_time(&alm.time, &alarm);
  131. retval = sprintf(buf, "%lu\n", alarm);
  132. }
  133. return retval;
  134. }
  135. static ssize_t
  136. rtc_sysfs_set_wakealarm(struct device *dev, struct device_attribute *attr,
  137. const char *buf, size_t n)
  138. {
  139. ssize_t retval;
  140. unsigned long now, alarm;
  141. unsigned long push = 0;
  142. struct rtc_wkalrm alm;
  143. struct rtc_device *rtc = to_rtc_device(dev);
  144. char *buf_ptr;
  145. int adjust = 0;
  146. /* Only request alarms that trigger in the future. Disable them
  147. * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
  148. */
  149. retval = rtc_read_time(rtc, &alm.time);
  150. if (retval < 0)
  151. return retval;
  152. rtc_tm_to_time(&alm.time, &now);
  153. buf_ptr = (char *)buf;
  154. if (*buf_ptr == '+') {
  155. buf_ptr++;
  156. if (*buf_ptr == '=') {
  157. buf_ptr++;
  158. push = 1;
  159. } else
  160. adjust = 1;
  161. }
  162. alarm = simple_strtoul(buf_ptr, NULL, 0);
  163. if (adjust) {
  164. alarm += now;
  165. }
  166. if (alarm > now || push) {
  167. /* Avoid accidentally clobbering active alarms; we can't
  168. * entirely prevent that here, without even the minimal
  169. * locking from the /dev/rtcN api.
  170. */
  171. retval = rtc_read_alarm(rtc, &alm);
  172. if (retval < 0)
  173. return retval;
  174. if (alm.enabled) {
  175. if (push) {
  176. rtc_tm_to_time(&alm.time, &push);
  177. alarm += push;
  178. } else
  179. return -EBUSY;
  180. } else if (push)
  181. return -EINVAL;
  182. alm.enabled = 1;
  183. } else {
  184. alm.enabled = 0;
  185. /* Provide a valid future alarm time. Linux isn't EFI,
  186. * this time won't be ignored when disabling the alarm.
  187. */
  188. alarm = now + 300;
  189. }
  190. rtc_time_to_tm(alarm, &alm.time);
  191. retval = rtc_set_alarm(rtc, &alm);
  192. return (retval < 0) ? retval : n;
  193. }
  194. static DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR,
  195. rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm);
  196. /* The reason to trigger an alarm with no process watching it (via sysfs)
  197. * is its side effect: waking from a system state like suspend-to-RAM or
  198. * suspend-to-disk. So: no attribute unless that side effect is possible.
  199. * (Userspace may disable that mechanism later.)
  200. */
  201. static inline int rtc_does_wakealarm(struct rtc_device *rtc)
  202. {
  203. if (!device_can_wakeup(rtc->dev.parent))
  204. return 0;
  205. return rtc->ops->set_alarm != NULL;
  206. }
  207. void rtc_sysfs_add_device(struct rtc_device *rtc)
  208. {
  209. int err;
  210. /* not all RTCs support both alarms and wakeup */
  211. if (!rtc_does_wakealarm(rtc))
  212. return;
  213. err = device_create_file(&rtc->dev, &dev_attr_wakealarm);
  214. if (err)
  215. dev_err(rtc->dev.parent,
  216. "failed to create alarm attribute, %d\n", err);
  217. }
  218. void rtc_sysfs_del_device(struct rtc_device *rtc)
  219. {
  220. /* REVISIT did we add it successfully? */
  221. if (rtc_does_wakealarm(rtc))
  222. device_remove_file(&rtc->dev, &dev_attr_wakealarm);
  223. }
  224. void __init rtc_sysfs_init(struct class *rtc_class)
  225. {
  226. rtc_class->dev_groups = rtc_groups;
  227. }