hwmon.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #define HWMON_ID_PREFIX "hwmon"
  17. #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
  18. static struct class *hwmon_class;
  19. static DEFINE_IDR(hwmon_idr);
  20. /**
  21. * hwmon_device_register - register w/ hwmon sysfs class
  22. * @dev: the device to register
  23. *
  24. * hwmon_device_unregister() must be called when the class device is no
  25. * longer needed.
  26. *
  27. * Returns the pointer to the new struct class device.
  28. */
  29. struct class_device *hwmon_device_register(struct device *dev)
  30. {
  31. struct class_device *cdev;
  32. int id;
  33. if (idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0)
  34. return ERR_PTR(-ENOMEM);
  35. if (idr_get_new(&hwmon_idr, NULL, &id) < 0)
  36. return ERR_PTR(-ENOMEM);
  37. id = id & MAX_ID_MASK;
  38. cdev = class_device_create(hwmon_class, NULL, MKDEV(0,0), dev,
  39. HWMON_ID_FORMAT, id);
  40. if (IS_ERR(cdev))
  41. idr_remove(&hwmon_idr, id);
  42. return cdev;
  43. }
  44. /**
  45. * hwmon_device_unregister - removes the previously registered class device
  46. *
  47. * @cdev: the class device to destroy
  48. */
  49. void hwmon_device_unregister(struct class_device *cdev)
  50. {
  51. int id;
  52. if (sscanf(cdev->class_id, HWMON_ID_FORMAT, &id) == 1) {
  53. class_device_unregister(cdev);
  54. idr_remove(&hwmon_idr, id);
  55. } else
  56. dev_dbg(cdev->dev,
  57. "hwmon_device_unregister() failed: bad class ID!\n");
  58. }
  59. static int __init hwmon_init(void)
  60. {
  61. hwmon_class = class_create(THIS_MODULE, "hwmon");
  62. if (IS_ERR(hwmon_class)) {
  63. printk(KERN_ERR "hwmon.c: couldn't create sysfs class\n");
  64. return PTR_ERR(hwmon_class);
  65. }
  66. return 0;
  67. }
  68. static void __exit hwmon_exit(void)
  69. {
  70. class_destroy(hwmon_class);
  71. }
  72. module_init(hwmon_init);
  73. module_exit(hwmon_exit);
  74. EXPORT_SYMBOL_GPL(hwmon_device_register);
  75. EXPORT_SYMBOL_GPL(hwmon_device_unregister);
  76. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  77. MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
  78. MODULE_LICENSE("GPL");