rtc-proc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * RTC subsystem, proc interface
  3. *
  4. * Copyright (C) 2005-06 Tower Technologies
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * based on arch/arm/common/rtctime.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/rtc.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/seq_file.h>
  17. #include "rtc-core.h"
  18. static struct class_device *rtc_dev = NULL;
  19. static DEFINE_MUTEX(rtc_lock);
  20. static int rtc_proc_show(struct seq_file *seq, void *offset)
  21. {
  22. int err;
  23. struct rtc_device *rtc = seq->private;
  24. const struct rtc_class_ops *ops = rtc->ops;
  25. struct rtc_wkalrm alrm;
  26. struct rtc_time tm;
  27. err = rtc_read_time(rtc, &tm);
  28. if (err == 0) {
  29. seq_printf(seq,
  30. "rtc_time\t: %02d:%02d:%02d\n"
  31. "rtc_date\t: %04d-%02d-%02d\n",
  32. tm.tm_hour, tm.tm_min, tm.tm_sec,
  33. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  34. }
  35. err = rtc_read_alarm(rtc, &alrm);
  36. if (err == 0) {
  37. seq_printf(seq, "alrm_time\t: ");
  38. if ((unsigned int)alrm.time.tm_hour <= 24)
  39. seq_printf(seq, "%02d:", alrm.time.tm_hour);
  40. else
  41. seq_printf(seq, "**:");
  42. if ((unsigned int)alrm.time.tm_min <= 59)
  43. seq_printf(seq, "%02d:", alrm.time.tm_min);
  44. else
  45. seq_printf(seq, "**:");
  46. if ((unsigned int)alrm.time.tm_sec <= 59)
  47. seq_printf(seq, "%02d\n", alrm.time.tm_sec);
  48. else
  49. seq_printf(seq, "**\n");
  50. seq_printf(seq, "alrm_date\t: ");
  51. if ((unsigned int)alrm.time.tm_year <= 200)
  52. seq_printf(seq, "%04d-", alrm.time.tm_year + 1900);
  53. else
  54. seq_printf(seq, "****-");
  55. if ((unsigned int)alrm.time.tm_mon <= 11)
  56. seq_printf(seq, "%02d-", alrm.time.tm_mon + 1);
  57. else
  58. seq_printf(seq, "**-");
  59. if (alrm.time.tm_mday && (unsigned int)alrm.time.tm_mday <= 31)
  60. seq_printf(seq, "%02d\n", alrm.time.tm_mday);
  61. else
  62. seq_printf(seq, "**\n");
  63. seq_printf(seq, "alarm_IRQ\t: %s\n",
  64. alrm.enabled ? "yes" : "no");
  65. seq_printf(seq, "alrm_pending\t: %s\n",
  66. alrm.pending ? "yes" : "no");
  67. }
  68. seq_printf(seq, "24hr\t\t: yes\n");
  69. if (ops->proc)
  70. ops->proc(rtc->class_dev.dev, seq);
  71. return 0;
  72. }
  73. static int rtc_proc_open(struct inode *inode, struct file *file)
  74. {
  75. struct rtc_device *rtc = PDE(inode)->data;
  76. if (!try_module_get(THIS_MODULE))
  77. return -ENODEV;
  78. return single_open(file, rtc_proc_show, rtc);
  79. }
  80. static int rtc_proc_release(struct inode *inode, struct file *file)
  81. {
  82. int res = single_release(inode, file);
  83. module_put(THIS_MODULE);
  84. return res;
  85. }
  86. static const struct file_operations rtc_proc_fops = {
  87. .open = rtc_proc_open,
  88. .read = seq_read,
  89. .llseek = seq_lseek,
  90. .release = rtc_proc_release,
  91. };
  92. static int rtc_proc_add_device(struct class_device *class_dev,
  93. struct class_interface *class_intf)
  94. {
  95. mutex_lock(&rtc_lock);
  96. if (rtc_dev == NULL) {
  97. struct proc_dir_entry *ent;
  98. rtc_dev = class_dev;
  99. ent = create_proc_entry("driver/rtc", 0, NULL);
  100. if (ent) {
  101. struct rtc_device *rtc = to_rtc_device(class_dev);
  102. ent->proc_fops = &rtc_proc_fops;
  103. ent->owner = rtc->owner;
  104. ent->data = rtc;
  105. dev_dbg(class_dev->dev, "rtc intf: proc\n");
  106. }
  107. else
  108. rtc_dev = NULL;
  109. }
  110. mutex_unlock(&rtc_lock);
  111. return 0;
  112. }
  113. static void rtc_proc_remove_device(struct class_device *class_dev,
  114. struct class_interface *class_intf)
  115. {
  116. mutex_lock(&rtc_lock);
  117. if (rtc_dev == class_dev) {
  118. remove_proc_entry("driver/rtc", NULL);
  119. rtc_dev = NULL;
  120. }
  121. mutex_unlock(&rtc_lock);
  122. }
  123. static struct class_interface rtc_proc_interface = {
  124. .add = &rtc_proc_add_device,
  125. .remove = &rtc_proc_remove_device,
  126. };
  127. static int __init rtc_proc_init(void)
  128. {
  129. return rtc_interface_register(&rtc_proc_interface);
  130. }
  131. static void __exit rtc_proc_exit(void)
  132. {
  133. class_interface_unregister(&rtc_proc_interface);
  134. }
  135. subsys_initcall(rtc_proc_init);
  136. module_exit(rtc_proc_exit);
  137. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  138. MODULE_DESCRIPTION("RTC class proc interface");
  139. MODULE_LICENSE("GPL");