rtc-sysfs.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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;
  78. int err;
  79. err = kstrtoul(buf, 0, &val);
  80. if (err)
  81. return err;
  82. if (val >= 4096 || val == 0)
  83. return -EINVAL;
  84. rtc->max_user_freq = (int)val;
  85. return n;
  86. }
  87. static DEVICE_ATTR_RW(max_user_freq);
  88. /**
  89. * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
  90. *
  91. * Returns 1 if the system clock was set by this RTC at the last
  92. * boot or resume event.
  93. */
  94. static ssize_t
  95. hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
  96. {
  97. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  98. if (rtc_hctosys_ret == 0 &&
  99. strcmp(dev_name(&to_rtc_device(dev)->dev),
  100. CONFIG_RTC_HCTOSYS_DEVICE) == 0)
  101. return sprintf(buf, "1\n");
  102. else
  103. #endif
  104. return sprintf(buf, "0\n");
  105. }
  106. static DEVICE_ATTR_RO(hctosys);
  107. static ssize_t
  108. wakealarm_show(struct device *dev, struct device_attribute *attr, 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. wakealarm_store(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. unsigned long push = 0;
  135. struct rtc_wkalrm alm;
  136. struct rtc_device *rtc = to_rtc_device(dev);
  137. const 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 = buf;
  147. if (*buf_ptr == '+') {
  148. buf_ptr++;
  149. if (*buf_ptr == '=') {
  150. buf_ptr++;
  151. push = 1;
  152. } else
  153. adjust = 1;
  154. }
  155. retval = kstrtoul(buf_ptr, 0, &alarm);
  156. if (retval)
  157. return retval;
  158. if (adjust) {
  159. alarm += now;
  160. }
  161. if (alarm > now || push) {
  162. /* Avoid accidentally clobbering active alarms; we can't
  163. * entirely prevent that here, without even the minimal
  164. * locking from the /dev/rtcN api.
  165. */
  166. retval = rtc_read_alarm(rtc, &alm);
  167. if (retval < 0)
  168. return retval;
  169. if (alm.enabled) {
  170. if (push) {
  171. rtc_tm_to_time(&alm.time, &push);
  172. alarm += push;
  173. } else
  174. return -EBUSY;
  175. } else if (push)
  176. return -EINVAL;
  177. alm.enabled = 1;
  178. } else {
  179. alm.enabled = 0;
  180. /* Provide a valid future alarm time. Linux isn't EFI,
  181. * this time won't be ignored when disabling the alarm.
  182. */
  183. alarm = now + 300;
  184. }
  185. rtc_time_to_tm(alarm, &alm.time);
  186. retval = rtc_set_alarm(rtc, &alm);
  187. return (retval < 0) ? retval : n;
  188. }
  189. static DEVICE_ATTR_RW(wakealarm);
  190. static ssize_t
  191. offset_show(struct device *dev, struct device_attribute *attr, char *buf)
  192. {
  193. ssize_t retval;
  194. long offset;
  195. retval = rtc_read_offset(to_rtc_device(dev), &offset);
  196. if (retval == 0)
  197. retval = sprintf(buf, "%ld\n", offset);
  198. return retval;
  199. }
  200. static ssize_t
  201. offset_store(struct device *dev, struct device_attribute *attr,
  202. const char *buf, size_t n)
  203. {
  204. ssize_t retval;
  205. long offset;
  206. retval = kstrtol(buf, 10, &offset);
  207. if (retval == 0)
  208. retval = rtc_set_offset(to_rtc_device(dev), offset);
  209. return (retval < 0) ? retval : n;
  210. }
  211. static DEVICE_ATTR_RW(offset);
  212. static struct attribute *rtc_attrs[] = {
  213. &dev_attr_name.attr,
  214. &dev_attr_date.attr,
  215. &dev_attr_time.attr,
  216. &dev_attr_since_epoch.attr,
  217. &dev_attr_max_user_freq.attr,
  218. &dev_attr_hctosys.attr,
  219. &dev_attr_wakealarm.attr,
  220. &dev_attr_offset.attr,
  221. NULL,
  222. };
  223. /* The reason to trigger an alarm with no process watching it (via sysfs)
  224. * is its side effect: waking from a system state like suspend-to-RAM or
  225. * suspend-to-disk. So: no attribute unless that side effect is possible.
  226. * (Userspace may disable that mechanism later.)
  227. */
  228. static bool rtc_does_wakealarm(struct rtc_device *rtc)
  229. {
  230. if (!device_can_wakeup(rtc->dev.parent))
  231. return false;
  232. return rtc->ops->set_alarm != NULL;
  233. }
  234. static umode_t rtc_attr_is_visible(struct kobject *kobj,
  235. struct attribute *attr, int n)
  236. {
  237. struct device *dev = container_of(kobj, struct device, kobj);
  238. struct rtc_device *rtc = to_rtc_device(dev);
  239. umode_t mode = attr->mode;
  240. if (attr == &dev_attr_wakealarm.attr) {
  241. if (!rtc_does_wakealarm(rtc))
  242. mode = 0;
  243. } else if (attr == &dev_attr_offset.attr) {
  244. if (!rtc->ops->set_offset)
  245. mode = 0;
  246. }
  247. return mode;
  248. }
  249. static struct attribute_group rtc_attr_group = {
  250. .is_visible = rtc_attr_is_visible,
  251. .attrs = rtc_attrs,
  252. };
  253. static const struct attribute_group *rtc_attr_groups[] = {
  254. &rtc_attr_group,
  255. NULL
  256. };
  257. const struct attribute_group **rtc_get_dev_attribute_groups(void)
  258. {
  259. return rtc_attr_groups;
  260. }