rtc-sysfs.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 ssize_t
  104. wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
  105. {
  106. ssize_t retval;
  107. unsigned long alarm;
  108. struct rtc_wkalrm alm;
  109. /* Don't show disabled alarms. For uniformity, RTC alarms are
  110. * conceptually one-shot, even though some common RTCs (on PCs)
  111. * don't actually work that way.
  112. *
  113. * NOTE: RTC implementations where the alarm doesn't match an
  114. * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
  115. * alarms after they trigger, to ensure one-shot semantics.
  116. */
  117. retval = rtc_read_alarm(to_rtc_device(dev), &alm);
  118. if (retval == 0 && alm.enabled) {
  119. rtc_tm_to_time(&alm.time, &alarm);
  120. retval = sprintf(buf, "%lu\n", alarm);
  121. }
  122. return retval;
  123. }
  124. static ssize_t
  125. wakealarm_store(struct device *dev, struct device_attribute *attr,
  126. const char *buf, size_t n)
  127. {
  128. ssize_t retval;
  129. unsigned long now, alarm;
  130. unsigned long push = 0;
  131. struct rtc_wkalrm alm;
  132. struct rtc_device *rtc = to_rtc_device(dev);
  133. char *buf_ptr;
  134. int adjust = 0;
  135. /* Only request alarms that trigger in the future. Disable them
  136. * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
  137. */
  138. retval = rtc_read_time(rtc, &alm.time);
  139. if (retval < 0)
  140. return retval;
  141. rtc_tm_to_time(&alm.time, &now);
  142. buf_ptr = (char *)buf;
  143. if (*buf_ptr == '+') {
  144. buf_ptr++;
  145. if (*buf_ptr == '=') {
  146. buf_ptr++;
  147. push = 1;
  148. } else
  149. adjust = 1;
  150. }
  151. alarm = simple_strtoul(buf_ptr, NULL, 0);
  152. if (adjust) {
  153. alarm += now;
  154. }
  155. if (alarm > now || push) {
  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. if (push) {
  165. rtc_tm_to_time(&alm.time, &push);
  166. alarm += push;
  167. } else
  168. return -EBUSY;
  169. } else if (push)
  170. return -EINVAL;
  171. alm.enabled = 1;
  172. } else {
  173. alm.enabled = 0;
  174. /* Provide a valid future alarm time. Linux isn't EFI,
  175. * this time won't be ignored when disabling the alarm.
  176. */
  177. alarm = now + 300;
  178. }
  179. rtc_time_to_tm(alarm, &alm.time);
  180. retval = rtc_set_alarm(rtc, &alm);
  181. return (retval < 0) ? retval : n;
  182. }
  183. static DEVICE_ATTR_RW(wakealarm);
  184. static struct attribute *rtc_attrs[] = {
  185. &dev_attr_name.attr,
  186. &dev_attr_date.attr,
  187. &dev_attr_time.attr,
  188. &dev_attr_since_epoch.attr,
  189. &dev_attr_max_user_freq.attr,
  190. &dev_attr_hctosys.attr,
  191. &dev_attr_wakealarm.attr,
  192. NULL,
  193. };
  194. /* The reason to trigger an alarm with no process watching it (via sysfs)
  195. * is its side effect: waking from a system state like suspend-to-RAM or
  196. * suspend-to-disk. So: no attribute unless that side effect is possible.
  197. * (Userspace may disable that mechanism later.)
  198. */
  199. static bool rtc_does_wakealarm(struct rtc_device *rtc)
  200. {
  201. if (!device_can_wakeup(rtc->dev.parent))
  202. return false;
  203. return rtc->ops->set_alarm != NULL;
  204. }
  205. static umode_t rtc_attr_is_visible(struct kobject *kobj,
  206. struct attribute *attr, int n)
  207. {
  208. struct device *dev = container_of(kobj, struct device, kobj);
  209. struct rtc_device *rtc = to_rtc_device(dev);
  210. umode_t mode = attr->mode;
  211. if (attr == &dev_attr_wakealarm.attr)
  212. if (!rtc_does_wakealarm(rtc))
  213. mode = 0;
  214. return mode;
  215. }
  216. static struct attribute_group rtc_attr_group = {
  217. .is_visible = rtc_attr_is_visible,
  218. .attrs = rtc_attrs,
  219. };
  220. static const struct attribute_group *rtc_attr_groups[] = {
  221. &rtc_attr_group,
  222. NULL
  223. };
  224. const struct attribute_group **rtc_get_dev_attribute_groups(void)
  225. {
  226. return rtc_attr_groups;
  227. }