rtc-proc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. static struct class_device *rtc_dev = NULL;
  18. static DEFINE_MUTEX(rtc_lock);
  19. static int rtc_proc_show(struct seq_file *seq, void *offset)
  20. {
  21. int err;
  22. struct class_device *class_dev = seq->private;
  23. const struct rtc_class_ops *ops = to_rtc_device(class_dev)->ops;
  24. struct rtc_wkalrm alrm;
  25. struct rtc_time tm;
  26. err = rtc_read_time(class_dev, &tm);
  27. if (err == 0) {
  28. seq_printf(seq,
  29. "rtc_time\t: %02d:%02d:%02d\n"
  30. "rtc_date\t: %04d-%02d-%02d\n",
  31. tm.tm_hour, tm.tm_min, tm.tm_sec,
  32. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  33. }
  34. err = rtc_read_alarm(class_dev, &alrm);
  35. if (err == 0) {
  36. seq_printf(seq, "alrm_time\t: ");
  37. if ((unsigned int)alrm.time.tm_hour <= 24)
  38. seq_printf(seq, "%02d:", alrm.time.tm_hour);
  39. else
  40. seq_printf(seq, "**:");
  41. if ((unsigned int)alrm.time.tm_min <= 59)
  42. seq_printf(seq, "%02d:", alrm.time.tm_min);
  43. else
  44. seq_printf(seq, "**:");
  45. if ((unsigned int)alrm.time.tm_sec <= 59)
  46. seq_printf(seq, "%02d\n", alrm.time.tm_sec);
  47. else
  48. seq_printf(seq, "**\n");
  49. seq_printf(seq, "alrm_date\t: ");
  50. if ((unsigned int)alrm.time.tm_year <= 200)
  51. seq_printf(seq, "%04d-", alrm.time.tm_year + 1900);
  52. else
  53. seq_printf(seq, "****-");
  54. if ((unsigned int)alrm.time.tm_mon <= 11)
  55. seq_printf(seq, "%02d-", alrm.time.tm_mon + 1);
  56. else
  57. seq_printf(seq, "**-");
  58. if (alrm.time.tm_mday && (unsigned int)alrm.time.tm_mday <= 31)
  59. seq_printf(seq, "%02d\n", alrm.time.tm_mday);
  60. else
  61. seq_printf(seq, "**\n");
  62. seq_printf(seq, "alarm_IRQ\t: %s\n",
  63. alrm.enabled ? "yes" : "no");
  64. seq_printf(seq, "alrm_pending\t: %s\n",
  65. alrm.pending ? "yes" : "no");
  66. }
  67. seq_printf(seq, "24hr\t\t: yes\n");
  68. if (ops->proc)
  69. ops->proc(class_dev->dev, seq);
  70. return 0;
  71. }
  72. static int rtc_proc_open(struct inode *inode, struct file *file)
  73. {
  74. struct class_device *class_dev = PDE(inode)->data;
  75. if (!try_module_get(THIS_MODULE))
  76. return -ENODEV;
  77. return single_open(file, rtc_proc_show, class_dev);
  78. }
  79. static int rtc_proc_release(struct inode *inode, struct file *file)
  80. {
  81. int res = single_release(inode, file);
  82. module_put(THIS_MODULE);
  83. return res;
  84. }
  85. static const struct file_operations rtc_proc_fops = {
  86. .open = rtc_proc_open,
  87. .read = seq_read,
  88. .llseek = seq_lseek,
  89. .release = rtc_proc_release,
  90. };
  91. static int rtc_proc_add_device(struct class_device *class_dev,
  92. struct class_interface *class_intf)
  93. {
  94. mutex_lock(&rtc_lock);
  95. if (rtc_dev == NULL) {
  96. struct proc_dir_entry *ent;
  97. rtc_dev = class_dev;
  98. ent = create_proc_entry("driver/rtc", 0, NULL);
  99. if (ent) {
  100. struct rtc_device *rtc = to_rtc_device(class_dev);
  101. ent->proc_fops = &rtc_proc_fops;
  102. ent->owner = rtc->owner;
  103. ent->data = class_dev;
  104. dev_dbg(class_dev->dev, "rtc intf: proc\n");
  105. }
  106. else
  107. rtc_dev = NULL;
  108. }
  109. mutex_unlock(&rtc_lock);
  110. return 0;
  111. }
  112. static void rtc_proc_remove_device(struct class_device *class_dev,
  113. struct class_interface *class_intf)
  114. {
  115. mutex_lock(&rtc_lock);
  116. if (rtc_dev == class_dev) {
  117. remove_proc_entry("driver/rtc", NULL);
  118. rtc_dev = NULL;
  119. }
  120. mutex_unlock(&rtc_lock);
  121. }
  122. static struct class_interface rtc_proc_interface = {
  123. .add = &rtc_proc_add_device,
  124. .remove = &rtc_proc_remove_device,
  125. };
  126. static int __init rtc_proc_init(void)
  127. {
  128. return rtc_interface_register(&rtc_proc_interface);
  129. }
  130. static void __exit rtc_proc_exit(void)
  131. {
  132. class_interface_unregister(&rtc_proc_interface);
  133. }
  134. subsys_initcall(rtc_proc_init);
  135. module_exit(rtc_proc_exit);
  136. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  137. MODULE_DESCRIPTION("RTC class proc interface");
  138. MODULE_LICENSE("GPL");