hwmon.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/kdev_t.h>
  14. #include <linux/idr.h>
  15. #include <linux/hwmon.h>
  16. #include <linux/gfp.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/pci.h>
  19. #define HWMON_ID_PREFIX "hwmon"
  20. #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
  21. static struct class *hwmon_class;
  22. static DEFINE_IDR(hwmon_idr);
  23. static DEFINE_SPINLOCK(idr_lock);
  24. /**
  25. * hwmon_device_register - register w/ hwmon
  26. * @dev: the device to register
  27. *
  28. * hwmon_device_unregister() must be called when the device is no
  29. * longer needed.
  30. *
  31. * Returns the pointer to the new device.
  32. */
  33. struct device *hwmon_device_register(struct device *dev)
  34. {
  35. struct device *hwdev;
  36. int id, err;
  37. again:
  38. if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0))
  39. return ERR_PTR(-ENOMEM);
  40. spin_lock(&idr_lock);
  41. err = idr_get_new(&hwmon_idr, NULL, &id);
  42. spin_unlock(&idr_lock);
  43. if (unlikely(err == -EAGAIN))
  44. goto again;
  45. else if (unlikely(err))
  46. return ERR_PTR(err);
  47. id = id & MAX_ID_MASK;
  48. hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL,
  49. HWMON_ID_FORMAT, id);
  50. if (IS_ERR(hwdev)) {
  51. spin_lock(&idr_lock);
  52. idr_remove(&hwmon_idr, id);
  53. spin_unlock(&idr_lock);
  54. }
  55. return hwdev;
  56. }
  57. /**
  58. * hwmon_device_unregister - removes the previously registered class device
  59. *
  60. * @dev: the class device to destroy
  61. */
  62. void hwmon_device_unregister(struct device *dev)
  63. {
  64. int id;
  65. if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
  66. device_unregister(dev);
  67. spin_lock(&idr_lock);
  68. idr_remove(&hwmon_idr, id);
  69. spin_unlock(&idr_lock);
  70. } else
  71. dev_dbg(dev->parent,
  72. "hwmon_device_unregister() failed: bad class ID!\n");
  73. }
  74. static void __init hwmon_pci_quirks(void)
  75. {
  76. #if defined CONFIG_X86 && defined CONFIG_PCI
  77. struct pci_dev *sb;
  78. u16 base;
  79. u8 enable;
  80. /* Open access to 0x295-0x296 on MSI MS-7031 */
  81. sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
  82. if (sb &&
  83. (sb->subsystem_vendor == 0x1462 && /* MSI */
  84. sb->subsystem_device == 0x0031)) { /* MS-7031 */
  85. pci_read_config_byte(sb, 0x48, &enable);
  86. pci_read_config_word(sb, 0x64, &base);
  87. if (base == 0 && !(enable & BIT(2))) {
  88. dev_info(&sb->dev,
  89. "Opening wide generic port at 0x295\n");
  90. pci_write_config_word(sb, 0x64, 0x295);
  91. pci_write_config_byte(sb, 0x48, enable | BIT(2));
  92. }
  93. }
  94. #endif
  95. }
  96. static int __init hwmon_init(void)
  97. {
  98. hwmon_pci_quirks();
  99. hwmon_class = class_create(THIS_MODULE, "hwmon");
  100. if (IS_ERR(hwmon_class)) {
  101. pr_err("couldn't create sysfs class\n");
  102. return PTR_ERR(hwmon_class);
  103. }
  104. return 0;
  105. }
  106. static void __exit hwmon_exit(void)
  107. {
  108. class_destroy(hwmon_class);
  109. }
  110. subsys_initcall(hwmon_init);
  111. module_exit(hwmon_exit);
  112. EXPORT_SYMBOL_GPL(hwmon_device_register);
  113. EXPORT_SYMBOL_GPL(hwmon_device_unregister);
  114. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  115. MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
  116. MODULE_LICENSE("GPL");