rtc-sysfs.c 5.6 KB

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