hwmon.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
  3. This file defines the sysfs class "hwmon", for use by sensors drivers.
  4. Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; version 2 of the License.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/idr.h>
  14. #include <linux/hwmon.h>
  15. #include <linux/gfp.h>
  16. #include <linux/spinlock.h>
  17. #define HWMON_ID_PREFIX "hwmon"
  18. #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
  19. static struct class *hwmon_class;
  20. static DEFINE_IDR(hwmon_idr);
  21. static DEFINE_SPINLOCK(idr_lock);
  22. /**
  23. * hwmon_device_register - register w/ hwmon sysfs class
  24. * @dev: the device to register
  25. *
  26. * hwmon_device_unregister() must be called when the class device is no
  27. * longer needed.
  28. *
  29. * Returns the pointer to the new struct class device.
  30. */
  31. struct class_device *hwmon_device_register(struct device *dev)
  32. {
  33. struct class_device *cdev;
  34. int id, err;
  35. again:
  36. if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0))
  37. return ERR_PTR(-ENOMEM);
  38. spin_lock(&idr_lock);
  39. err = idr_get_new(&hwmon_idr, NULL, &id);
  40. spin_unlock(&idr_lock);
  41. if (unlikely(err == -EAGAIN))
  42. goto again;
  43. else if (unlikely(err))
  44. return ERR_PTR(err);
  45. id = id & MAX_ID_MASK;
  46. cdev = class_device_create(hwmon_class, NULL, MKDEV(0,0), dev,
  47. HWMON_ID_FORMAT, id);
  48. if (IS_ERR(cdev)) {
  49. spin_lock(&idr_lock);
  50. idr_remove(&hwmon_idr, id);
  51. spin_unlock(&idr_lock);
  52. }
  53. return cdev;
  54. }
  55. /**
  56. * hwmon_device_unregister - removes the previously registered class device
  57. *
  58. * @cdev: the class device to destroy
  59. */
  60. void hwmon_device_unregister(struct class_device *cdev)
  61. {
  62. int id;
  63. if (likely(sscanf(cdev->class_id, HWMON_ID_FORMAT, &id) == 1)) {
  64. class_device_unregister(cdev);
  65. spin_lock(&idr_lock);
  66. idr_remove(&hwmon_idr, id);
  67. spin_unlock(&idr_lock);
  68. } else
  69. dev_dbg(cdev->dev,
  70. "hwmon_device_unregister() failed: bad class ID!\n");
  71. }
  72. static int __init hwmon_init(void)
  73. {
  74. hwmon_class = class_create(THIS_MODULE, "hwmon");
  75. if (IS_ERR(hwmon_class)) {
  76. printk(KERN_ERR "hwmon.c: couldn't create sysfs class\n");
  77. return PTR_ERR(hwmon_class);
  78. }
  79. return 0;
  80. }
  81. static void __exit hwmon_exit(void)
  82. {
  83. class_destroy(hwmon_class);
  84. }
  85. module_init(hwmon_init);
  86. module_exit(hwmon_exit);
  87. EXPORT_SYMBOL_GPL(hwmon_device_register);
  88. EXPORT_SYMBOL_GPL(hwmon_device_unregister);
  89. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  90. MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
  91. MODULE_LICENSE("GPL");