rtc-sysfs.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. static ssize_t rtc_sysfs_show_name(struct class_device *dev, char *buf)
  16. {
  17. return sprintf(buf, "%s\n", to_rtc_device(dev)->name);
  18. }
  19. static CLASS_DEVICE_ATTR(name, S_IRUGO, rtc_sysfs_show_name, NULL);
  20. static ssize_t rtc_sysfs_show_date(struct class_device *dev, char *buf)
  21. {
  22. ssize_t retval;
  23. struct rtc_time tm;
  24. retval = rtc_read_time(to_rtc_device(dev), &tm);
  25. if (retval == 0) {
  26. retval = sprintf(buf, "%04d-%02d-%02d\n",
  27. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  28. }
  29. return retval;
  30. }
  31. static CLASS_DEVICE_ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL);
  32. static ssize_t rtc_sysfs_show_time(struct class_device *dev, char *buf)
  33. {
  34. ssize_t retval;
  35. struct rtc_time tm;
  36. retval = rtc_read_time(to_rtc_device(dev), &tm);
  37. if (retval == 0) {
  38. retval = sprintf(buf, "%02d:%02d:%02d\n",
  39. tm.tm_hour, tm.tm_min, tm.tm_sec);
  40. }
  41. return retval;
  42. }
  43. static CLASS_DEVICE_ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL);
  44. static ssize_t rtc_sysfs_show_since_epoch(struct class_device *dev, char *buf)
  45. {
  46. ssize_t retval;
  47. struct rtc_time tm;
  48. retval = rtc_read_time(to_rtc_device(dev), &tm);
  49. if (retval == 0) {
  50. unsigned long time;
  51. rtc_tm_to_time(&tm, &time);
  52. retval = sprintf(buf, "%lu\n", time);
  53. }
  54. return retval;
  55. }
  56. static CLASS_DEVICE_ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL);
  57. static struct attribute *rtc_attrs[] = {
  58. &class_device_attr_name.attr,
  59. &class_device_attr_date.attr,
  60. &class_device_attr_time.attr,
  61. &class_device_attr_since_epoch.attr,
  62. NULL,
  63. };
  64. static struct attribute_group rtc_attr_group = {
  65. .attrs = rtc_attrs,
  66. };
  67. static ssize_t
  68. rtc_sysfs_show_wakealarm(struct class_device *dev, char *buf)
  69. {
  70. ssize_t retval;
  71. unsigned long alarm;
  72. struct rtc_wkalrm alm;
  73. /* Don't show disabled alarms; but the RTC could leave the
  74. * alarm enabled after it's already triggered. Alarms are
  75. * conceptually one-shot, even though some common hardware
  76. * (PCs) doesn't actually work that way.
  77. *
  78. * REVISIT maybe we should require RTC implementations to
  79. * disable the RTC alarm after it triggers, for uniformity.
  80. */
  81. retval = rtc_read_alarm(to_rtc_device(dev), &alm);
  82. if (retval == 0 && alm.enabled) {
  83. rtc_tm_to_time(&alm.time, &alarm);
  84. retval = sprintf(buf, "%lu\n", alarm);
  85. }
  86. return retval;
  87. }
  88. static ssize_t
  89. rtc_sysfs_set_wakealarm(struct class_device *dev, const char *buf, size_t n)
  90. {
  91. ssize_t retval;
  92. unsigned long now, alarm;
  93. struct rtc_wkalrm alm;
  94. struct rtc_device *rtc = to_rtc_device(dev);
  95. /* Only request alarms that trigger in the future. Disable them
  96. * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
  97. */
  98. retval = rtc_read_time(rtc, &alm.time);
  99. if (retval < 0)
  100. return retval;
  101. rtc_tm_to_time(&alm.time, &now);
  102. alarm = simple_strtoul(buf, NULL, 0);
  103. if (alarm > now) {
  104. /* Avoid accidentally clobbering active alarms; we can't
  105. * entirely prevent that here, without even the minimal
  106. * locking from the /dev/rtcN api.
  107. */
  108. retval = rtc_read_alarm(rtc, &alm);
  109. if (retval < 0)
  110. return retval;
  111. if (alm.enabled)
  112. return -EBUSY;
  113. alm.enabled = 1;
  114. } else {
  115. alm.enabled = 0;
  116. /* Provide a valid future alarm time. Linux isn't EFI,
  117. * this time won't be ignored when disabling the alarm.
  118. */
  119. alarm = now + 300;
  120. }
  121. rtc_time_to_tm(alarm, &alm.time);
  122. retval = rtc_set_alarm(rtc, &alm);
  123. return (retval < 0) ? retval : n;
  124. }
  125. static const CLASS_DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR,
  126. rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm);
  127. /* The reason to trigger an alarm with no process watching it (via sysfs)
  128. * is its side effect: waking from a system state like suspend-to-RAM or
  129. * suspend-to-disk. So: no attribute unless that side effect is possible.
  130. * (Userspace may disable that mechanism later.)
  131. */
  132. static inline int rtc_does_wakealarm(struct class_device *class_dev)
  133. {
  134. struct rtc_device *rtc;
  135. if (!device_can_wakeup(class_dev->dev))
  136. return 0;
  137. rtc = to_rtc_device(class_dev);
  138. return rtc->ops->set_alarm != NULL;
  139. }
  140. static int rtc_sysfs_add_device(struct class_device *class_dev,
  141. struct class_interface *class_intf)
  142. {
  143. int err;
  144. dev_dbg(class_dev->dev, "rtc intf: sysfs\n");
  145. err = sysfs_create_group(&class_dev->kobj, &rtc_attr_group);
  146. if (err)
  147. dev_err(class_dev->dev, "failed to create %s\n",
  148. "sysfs attributes");
  149. else if (rtc_does_wakealarm(class_dev)) {
  150. /* not all RTCs support both alarms and wakeup */
  151. err = class_device_create_file(class_dev,
  152. &class_device_attr_wakealarm);
  153. if (err) {
  154. dev_err(class_dev->dev, "failed to create %s\n",
  155. "alarm attribute");
  156. sysfs_remove_group(&class_dev->kobj, &rtc_attr_group);
  157. }
  158. }
  159. return err;
  160. }
  161. static void rtc_sysfs_remove_device(struct class_device *class_dev,
  162. struct class_interface *class_intf)
  163. {
  164. if (rtc_does_wakealarm(class_dev))
  165. class_device_remove_file(class_dev,
  166. &class_device_attr_wakealarm);
  167. sysfs_remove_group(&class_dev->kobj, &rtc_attr_group);
  168. }
  169. /* interface registration */
  170. static struct class_interface rtc_sysfs_interface = {
  171. .add = &rtc_sysfs_add_device,
  172. .remove = &rtc_sysfs_remove_device,
  173. };
  174. static int __init rtc_sysfs_init(void)
  175. {
  176. return rtc_interface_register(&rtc_sysfs_interface);
  177. }
  178. static void __exit rtc_sysfs_exit(void)
  179. {
  180. class_interface_unregister(&rtc_sysfs_interface);
  181. }
  182. subsys_initcall(rtc_sysfs_init);
  183. module_exit(rtc_sysfs_exit);
  184. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  185. MODULE_DESCRIPTION("RTC class sysfs interface");
  186. MODULE_LICENSE("GPL");