rtc-sysfs.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 int __devinit rtc_sysfs_add_device(struct class_device *class_dev,
  67. struct class_interface *class_intf)
  68. {
  69. int err;
  70. dev_info(class_dev->dev, "rtc intf: sysfs\n");
  71. err = sysfs_create_group(&class_dev->kobj, &rtc_attr_group);
  72. if (err)
  73. dev_err(class_dev->dev,
  74. "failed to create sysfs attributes\n");
  75. return err;
  76. }
  77. static void rtc_sysfs_remove_device(struct class_device *class_dev,
  78. struct class_interface *class_intf)
  79. {
  80. sysfs_remove_group(&class_dev->kobj, &rtc_attr_group);
  81. }
  82. /* interface registration */
  83. static struct class_interface rtc_sysfs_interface = {
  84. .add = &rtc_sysfs_add_device,
  85. .remove = &rtc_sysfs_remove_device,
  86. };
  87. static int __init rtc_sysfs_init(void)
  88. {
  89. return rtc_interface_register(&rtc_sysfs_interface);
  90. }
  91. static void __exit rtc_sysfs_exit(void)
  92. {
  93. class_interface_unregister(&rtc_sysfs_interface);
  94. }
  95. module_init(rtc_sysfs_init);
  96. module_exit(rtc_sysfs_exit);
  97. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  98. MODULE_DESCRIPTION("RTC class sysfs interface");
  99. MODULE_LICENSE("GPL");