hwmon.c 6.8 KB

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