hwmon.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
  3. *
  4. * This file defines the sysfs class "hwmon", for use by sensors drivers.
  5. *
  6. * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/kdev_t.h>
  18. #include <linux/idr.h>
  19. #include <linux/hwmon.h>
  20. #include <linux/gfp.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/pci.h>
  23. #define HWMON_ID_PREFIX "hwmon"
  24. #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
  25. struct hwmon_device {
  26. const char *name;
  27. struct device dev;
  28. };
  29. #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
  30. static ssize_t
  31. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  32. {
  33. return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
  34. }
  35. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  36. static struct attribute *hwmon_dev_attrs[] = {
  37. &dev_attr_name.attr,
  38. NULL
  39. };
  40. static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
  41. struct attribute *attr, int n)
  42. {
  43. struct device *dev = container_of(kobj, struct device, kobj);
  44. if (to_hwmon_device(dev)->name == NULL)
  45. return 0;
  46. return attr->mode;
  47. }
  48. static struct attribute_group hwmon_dev_attr_group = {
  49. .attrs = hwmon_dev_attrs,
  50. .is_visible = hwmon_dev_name_is_visible,
  51. };
  52. static const struct attribute_group *hwmon_dev_attr_groups[] = {
  53. &hwmon_dev_attr_group,
  54. NULL
  55. };
  56. static void hwmon_dev_release(struct device *dev)
  57. {
  58. kfree(to_hwmon_device(dev));
  59. }
  60. static struct class hwmon_class = {
  61. .name = "hwmon",
  62. .owner = THIS_MODULE,
  63. .dev_groups = hwmon_dev_attr_groups,
  64. .dev_release = hwmon_dev_release,
  65. };
  66. static DEFINE_IDA(hwmon_ida);
  67. /**
  68. * hwmon_device_register_with_groups - register w/ hwmon
  69. * @dev: the parent device
  70. * @name: hwmon name attribute
  71. * @drvdata: driver data to attach to created device
  72. * @groups: List of attribute groups to create
  73. *
  74. * hwmon_device_unregister() must be called when the device is no
  75. * longer needed.
  76. *
  77. * Returns the pointer to the new device.
  78. */
  79. struct device *
  80. hwmon_device_register_with_groups(struct device *dev, const char *name,
  81. void *drvdata,
  82. const struct attribute_group **groups)
  83. {
  84. struct hwmon_device *hwdev;
  85. int err, id;
  86. id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
  87. if (id < 0)
  88. return ERR_PTR(id);
  89. hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
  90. if (hwdev == NULL) {
  91. err = -ENOMEM;
  92. goto ida_remove;
  93. }
  94. hwdev->name = name;
  95. hwdev->dev.class = &hwmon_class;
  96. hwdev->dev.parent = dev;
  97. hwdev->dev.groups = groups;
  98. hwdev->dev.of_node = dev ? dev->of_node : NULL;
  99. dev_set_drvdata(&hwdev->dev, drvdata);
  100. dev_set_name(&hwdev->dev, HWMON_ID_FORMAT, id);
  101. err = device_register(&hwdev->dev);
  102. if (err)
  103. goto free;
  104. return &hwdev->dev;
  105. free:
  106. kfree(hwdev);
  107. ida_remove:
  108. ida_simple_remove(&hwmon_ida, id);
  109. return ERR_PTR(err);
  110. }
  111. EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
  112. /**
  113. * hwmon_device_register - register w/ hwmon
  114. * @dev: the device to register
  115. *
  116. * hwmon_device_unregister() must be called when the device is no
  117. * longer needed.
  118. *
  119. * Returns the pointer to the new device.
  120. */
  121. struct device *hwmon_device_register(struct device *dev)
  122. {
  123. return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
  124. }
  125. EXPORT_SYMBOL_GPL(hwmon_device_register);
  126. /**
  127. * hwmon_device_unregister - removes the previously registered class device
  128. *
  129. * @dev: the class device to destroy
  130. */
  131. void hwmon_device_unregister(struct device *dev)
  132. {
  133. int id;
  134. if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
  135. device_unregister(dev);
  136. ida_simple_remove(&hwmon_ida, id);
  137. } else
  138. dev_dbg(dev->parent,
  139. "hwmon_device_unregister() failed: bad class ID!\n");
  140. }
  141. EXPORT_SYMBOL_GPL(hwmon_device_unregister);
  142. static void devm_hwmon_release(struct device *dev, void *res)
  143. {
  144. struct device *hwdev = *(struct device **)res;
  145. hwmon_device_unregister(hwdev);
  146. }
  147. /**
  148. * devm_hwmon_device_register_with_groups - register w/ hwmon
  149. * @dev: the parent device
  150. * @name: hwmon name attribute
  151. * @drvdata: driver data to attach to created device
  152. * @groups: List of attribute groups to create
  153. *
  154. * Returns the pointer to the new device. The new device is automatically
  155. * unregistered with the parent device.
  156. */
  157. struct device *
  158. devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
  159. void *drvdata,
  160. const struct attribute_group **groups)
  161. {
  162. struct device **ptr, *hwdev;
  163. if (!dev)
  164. return ERR_PTR(-EINVAL);
  165. ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
  166. if (!ptr)
  167. return ERR_PTR(-ENOMEM);
  168. hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
  169. if (IS_ERR(hwdev))
  170. goto error;
  171. *ptr = hwdev;
  172. devres_add(dev, ptr);
  173. return hwdev;
  174. error:
  175. devres_free(ptr);
  176. return hwdev;
  177. }
  178. EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
  179. static int devm_hwmon_match(struct device *dev, void *res, void *data)
  180. {
  181. struct device **hwdev = res;
  182. return *hwdev == data;
  183. }
  184. /**
  185. * devm_hwmon_device_unregister - removes a previously registered hwmon device
  186. *
  187. * @dev: the parent device of the device to unregister
  188. */
  189. void devm_hwmon_device_unregister(struct device *dev)
  190. {
  191. WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
  192. }
  193. EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
  194. static void __init hwmon_pci_quirks(void)
  195. {
  196. #if defined CONFIG_X86 && defined CONFIG_PCI
  197. struct pci_dev *sb;
  198. u16 base;
  199. u8 enable;
  200. /* Open access to 0x295-0x296 on MSI MS-7031 */
  201. sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
  202. if (sb) {
  203. if (sb->subsystem_vendor == 0x1462 && /* MSI */
  204. sb->subsystem_device == 0x0031) { /* MS-7031 */
  205. pci_read_config_byte(sb, 0x48, &enable);
  206. pci_read_config_word(sb, 0x64, &base);
  207. if (base == 0 && !(enable & BIT(2))) {
  208. dev_info(&sb->dev,
  209. "Opening wide generic port at 0x295\n");
  210. pci_write_config_word(sb, 0x64, 0x295);
  211. pci_write_config_byte(sb, 0x48,
  212. enable | BIT(2));
  213. }
  214. }
  215. pci_dev_put(sb);
  216. }
  217. #endif
  218. }
  219. static int __init hwmon_init(void)
  220. {
  221. int err;
  222. hwmon_pci_quirks();
  223. err = class_register(&hwmon_class);
  224. if (err) {
  225. pr_err("couldn't register hwmon sysfs class\n");
  226. return err;
  227. }
  228. return 0;
  229. }
  230. static void __exit hwmon_exit(void)
  231. {
  232. class_unregister(&hwmon_class);
  233. }
  234. subsys_initcall(hwmon_init);
  235. module_exit(hwmon_exit);
  236. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  237. MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
  238. MODULE_LICENSE("GPL");