class.c 3.5 KB

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