hwmon.c 2.3 KB

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