rtc-proc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. 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 ((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, "alrm_wakeup\t: %s\n",
  63. alrm.enabled ? "yes" : "no");
  64. seq_printf(seq, "alrm_pending\t: %s\n",
  65. alrm.pending ? "yes" : "no");
  66. }
  67. if (ops->proc)
  68. ops->proc(class_dev->dev, seq);
  69. return 0;
  70. }
  71. static int rtc_proc_open(struct inode *inode, struct file *file)
  72. {
  73. struct class_device *class_dev = PDE(inode)->data;
  74. if (!try_module_get(THIS_MODULE))
  75. return -ENODEV;
  76. return single_open(file, rtc_proc_show, class_dev);
  77. }
  78. static int rtc_proc_release(struct inode *inode, struct file *file)
  79. {
  80. int res = single_release(inode, file);
  81. module_put(THIS_MODULE);
  82. return res;
  83. }
  84. static struct file_operations rtc_proc_fops = {
  85. .open = rtc_proc_open,
  86. .read = seq_read,
  87. .llseek = seq_lseek,
  88. .release = rtc_proc_release,
  89. };
  90. static int rtc_proc_add_device(struct class_device *class_dev,
  91. struct class_interface *class_intf)
  92. {
  93. mutex_lock(&rtc_lock);
  94. if (rtc_dev == NULL) {
  95. struct proc_dir_entry *ent;
  96. rtc_dev = class_dev;
  97. ent = create_proc_entry("driver/rtc", 0, NULL);
  98. if (ent) {
  99. struct rtc_device *rtc = to_rtc_device(class_dev);
  100. ent->proc_fops = &rtc_proc_fops;
  101. ent->owner = rtc->owner;
  102. ent->data = class_dev;
  103. dev_info(class_dev->dev, "rtc intf: proc\n");
  104. }
  105. else
  106. rtc_dev = NULL;
  107. }
  108. mutex_unlock(&rtc_lock);
  109. return 0;
  110. }
  111. static void rtc_proc_remove_device(struct class_device *class_dev,
  112. struct class_interface *class_intf)
  113. {
  114. mutex_lock(&rtc_lock);
  115. if (rtc_dev == class_dev) {
  116. remove_proc_entry("driver/rtc", NULL);
  117. rtc_dev = NULL;
  118. }
  119. mutex_unlock(&rtc_lock);
  120. }
  121. static struct class_interface rtc_proc_interface = {
  122. .add = &rtc_proc_add_device,
  123. .remove = &rtc_proc_remove_device,
  124. };
  125. static int __init rtc_proc_init(void)
  126. {
  127. return rtc_interface_register(&rtc_proc_interface);
  128. }
  129. static void __exit rtc_proc_exit(void)
  130. {
  131. class_interface_unregister(&rtc_proc_interface);
  132. }
  133. module_init(rtc_proc_init);
  134. module_exit(rtc_proc_exit);
  135. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  136. MODULE_DESCRIPTION("RTC class proc interface");
  137. MODULE_LICENSE("GPL");