class.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. 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->class_dev.dev = dev;
  62. rtc->class_dev.class = rtc_class;
  63. rtc->class_dev.release = rtc_device_release;
  64. mutex_init(&rtc->ops_lock);
  65. spin_lock_init(&rtc->irq_lock);
  66. spin_lock_init(&rtc->irq_task_lock);
  67. strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
  68. snprintf(rtc->class_dev.class_id, BUS_ID_SIZE, "rtc%d", id);
  69. err = class_device_register(&rtc->class_dev);
  70. if (err)
  71. goto exit_kfree;
  72. dev_info(dev, "rtc core: registered %s as %s\n",
  73. rtc->name, rtc->class_dev.class_id);
  74. return rtc;
  75. exit_kfree:
  76. kfree(rtc);
  77. exit_idr:
  78. idr_remove(&rtc_idr, id);
  79. exit:
  80. dev_err(dev, "rtc core: unable to register %s, err = %d\n",
  81. name, err);
  82. return ERR_PTR(err);
  83. }
  84. EXPORT_SYMBOL_GPL(rtc_device_register);
  85. /**
  86. * rtc_device_unregister - removes the previously registered RTC class device
  87. *
  88. * @rtc: the RTC class device to destroy
  89. */
  90. void rtc_device_unregister(struct rtc_device *rtc)
  91. {
  92. mutex_lock(&rtc->ops_lock);
  93. rtc->ops = NULL;
  94. mutex_unlock(&rtc->ops_lock);
  95. class_device_unregister(&rtc->class_dev);
  96. }
  97. EXPORT_SYMBOL_GPL(rtc_device_unregister);
  98. int rtc_interface_register(struct class_interface *intf)
  99. {
  100. intf->class = rtc_class;
  101. return class_interface_register(intf);
  102. }
  103. EXPORT_SYMBOL_GPL(rtc_interface_register);
  104. static int __init rtc_init(void)
  105. {
  106. rtc_class = class_create(THIS_MODULE, "rtc");
  107. if (IS_ERR(rtc_class)) {
  108. printk(KERN_ERR "%s: couldn't create class\n", __FILE__);
  109. return PTR_ERR(rtc_class);
  110. }
  111. return 0;
  112. }
  113. static void __exit rtc_exit(void)
  114. {
  115. class_destroy(rtc_class);
  116. }
  117. module_init(rtc_init);
  118. module_exit(rtc_exit);
  119. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towerteh.it>");
  120. MODULE_DESCRIPTION("RTC class support");
  121. MODULE_LICENSE("GPL");