class.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * RTC subsystem, base class
  3. *
  4. * Copyright (C) 2005 Tower Technologies
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * class skeleton from drivers/hwmon/hwmon.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/kdev_t.h>
  16. #include <linux/idr.h>
  17. #include "rtc-core.h"
  18. static DEFINE_IDR(rtc_idr);
  19. static DEFINE_MUTEX(idr_lock);
  20. struct class *rtc_class;
  21. static void rtc_device_release(struct class_device *class_dev)
  22. {
  23. struct rtc_device *rtc = to_rtc_device(class_dev);
  24. mutex_lock(&idr_lock);
  25. idr_remove(&rtc_idr, rtc->id);
  26. mutex_unlock(&idr_lock);
  27. kfree(rtc);
  28. }
  29. /**
  30. * rtc_device_register - register w/ RTC class
  31. * @dev: the device to register
  32. *
  33. * rtc_device_unregister() must be called when the class device is no
  34. * longer needed.
  35. *
  36. * Returns the pointer to the new struct class device.
  37. */
  38. struct rtc_device *rtc_device_register(const char *name, struct device *dev,
  39. const struct rtc_class_ops *ops,
  40. struct module *owner)
  41. {
  42. struct rtc_device *rtc;
  43. int id, err;
  44. if (idr_pre_get(&rtc_idr, GFP_KERNEL) == 0) {
  45. err = -ENOMEM;
  46. goto exit;
  47. }
  48. mutex_lock(&idr_lock);
  49. err = idr_get_new(&rtc_idr, NULL, &id);
  50. mutex_unlock(&idr_lock);
  51. if (err < 0)
  52. goto exit;
  53. id = id & MAX_ID_MASK;
  54. rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
  55. if (rtc == NULL) {
  56. err = -ENOMEM;
  57. goto exit_idr;
  58. }
  59. rtc->id = id;
  60. rtc->ops = ops;
  61. rtc->owner = owner;
  62. rtc->max_user_freq = 64;
  63. rtc->class_dev.dev = dev;
  64. rtc->class_dev.class = rtc_class;
  65. rtc->class_dev.release = rtc_device_release;
  66. mutex_init(&rtc->ops_lock);
  67. spin_lock_init(&rtc->irq_lock);
  68. spin_lock_init(&rtc->irq_task_lock);
  69. strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
  70. snprintf(rtc->class_dev.class_id, BUS_ID_SIZE, "rtc%d", id);
  71. err = class_device_register(&rtc->class_dev);
  72. if (err)
  73. goto exit_kfree;
  74. rtc_dev_add_device(rtc);
  75. rtc_sysfs_add_device(rtc);
  76. rtc_proc_add_device(rtc);
  77. dev_info(dev, "rtc core: registered %s as %s\n",
  78. rtc->name, rtc->class_dev.class_id);
  79. return rtc;
  80. exit_kfree:
  81. kfree(rtc);
  82. exit_idr:
  83. mutex_lock(&idr_lock);
  84. idr_remove(&rtc_idr, id);
  85. mutex_unlock(&idr_lock);
  86. exit:
  87. dev_err(dev, "rtc core: unable to register %s, err = %d\n",
  88. name, err);
  89. return ERR_PTR(err);
  90. }
  91. EXPORT_SYMBOL_GPL(rtc_device_register);
  92. /**
  93. * rtc_device_unregister - removes the previously registered RTC class device
  94. *
  95. * @rtc: the RTC class device to destroy
  96. */
  97. void rtc_device_unregister(struct rtc_device *rtc)
  98. {
  99. if (class_device_get(&rtc->class_dev) != NULL) {
  100. mutex_lock(&rtc->ops_lock);
  101. /* remove innards of this RTC, then disable it, before
  102. * letting any rtc_class_open() users access it again
  103. */
  104. rtc_sysfs_del_device(rtc);
  105. rtc_dev_del_device(rtc);
  106. rtc_proc_del_device(rtc);
  107. class_device_unregister(&rtc->class_dev);
  108. rtc->ops = NULL;
  109. mutex_unlock(&rtc->ops_lock);
  110. class_device_put(&rtc->class_dev);
  111. }
  112. }
  113. EXPORT_SYMBOL_GPL(rtc_device_unregister);
  114. static int __init rtc_init(void)
  115. {
  116. rtc_class = class_create(THIS_MODULE, "rtc");
  117. if (IS_ERR(rtc_class)) {
  118. printk(KERN_ERR "%s: couldn't create class\n", __FILE__);
  119. return PTR_ERR(rtc_class);
  120. }
  121. rtc_dev_init();
  122. rtc_sysfs_init(rtc_class);
  123. return 0;
  124. }
  125. static void __exit rtc_exit(void)
  126. {
  127. rtc_dev_exit();
  128. class_destroy(rtc_class);
  129. }
  130. subsys_initcall(rtc_init);
  131. module_exit(rtc_exit);
  132. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  133. MODULE_DESCRIPTION("RTC class support");
  134. MODULE_LICENSE("GPL");