blob: a61e7815a2a9153f7ad82e8dc2ff49273763c19a [file] [log] [blame]
Mark M. Hoffman12364412005-07-15 21:38:08 -04001/*
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
Joe Perchesc95df1a2010-10-20 06:51:37 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Mark M. Hoffman12364412005-07-15 21:38:08 -040015#include <linux/module.h>
16#include <linux/device.h>
17#include <linux/err.h>
18#include <linux/kdev_t.h>
19#include <linux/idr.h>
20#include <linux/hwmon.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080021#include <linux/gfp.h>
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010022#include <linux/spinlock.h>
Jean Delvare2958b1e2009-06-15 18:39:50 +020023#include <linux/pci.h>
Mark M. Hoffman12364412005-07-15 21:38:08 -040024
25#define HWMON_ID_PREFIX "hwmon"
26#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
27
28static struct class *hwmon_class;
29
30static DEFINE_IDR(hwmon_idr);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010031static DEFINE_SPINLOCK(idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040032
33/**
Tony Jones1beeffe2007-08-20 13:46:20 -070034 * hwmon_device_register - register w/ hwmon
Mark M. Hoffman12364412005-07-15 21:38:08 -040035 * @dev: the device to register
36 *
Tony Jones1beeffe2007-08-20 13:46:20 -070037 * hwmon_device_unregister() must be called when the device is no
Mark M. Hoffman12364412005-07-15 21:38:08 -040038 * longer needed.
39 *
Tony Jones1beeffe2007-08-20 13:46:20 -070040 * Returns the pointer to the new device.
Mark M. Hoffman12364412005-07-15 21:38:08 -040041 */
Tony Jones1beeffe2007-08-20 13:46:20 -070042struct device *hwmon_device_register(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -040043{
Tony Jones1beeffe2007-08-20 13:46:20 -070044 struct device *hwdev;
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010045 int id, err;
Mark M. Hoffman12364412005-07-15 21:38:08 -040046
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010047again:
48 if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0))
Mark M. Hoffman12364412005-07-15 21:38:08 -040049 return ERR_PTR(-ENOMEM);
50
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010051 spin_lock(&idr_lock);
52 err = idr_get_new(&hwmon_idr, NULL, &id);
53 spin_unlock(&idr_lock);
54
55 if (unlikely(err == -EAGAIN))
56 goto again;
57 else if (unlikely(err))
58 return ERR_PTR(err);
Mark M. Hoffman12364412005-07-15 21:38:08 -040059
60 id = id & MAX_ID_MASK;
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -070061 hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL,
62 HWMON_ID_FORMAT, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -040063
Tony Jones1beeffe2007-08-20 13:46:20 -070064 if (IS_ERR(hwdev)) {
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010065 spin_lock(&idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040066 idr_remove(&hwmon_idr, id);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010067 spin_unlock(&idr_lock);
68 }
Mark M. Hoffman12364412005-07-15 21:38:08 -040069
Tony Jones1beeffe2007-08-20 13:46:20 -070070 return hwdev;
Mark M. Hoffman12364412005-07-15 21:38:08 -040071}
72
73/**
74 * hwmon_device_unregister - removes the previously registered class device
75 *
Tony Jones1beeffe2007-08-20 13:46:20 -070076 * @dev: the class device to destroy
Mark M. Hoffman12364412005-07-15 21:38:08 -040077 */
Tony Jones1beeffe2007-08-20 13:46:20 -070078void hwmon_device_unregister(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -040079{
80 int id;
81
Kay Sievers739cf3a2009-01-06 10:44:41 -080082 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
Tony Jones1beeffe2007-08-20 13:46:20 -070083 device_unregister(dev);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010084 spin_lock(&idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040085 idr_remove(&hwmon_idr, id);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010086 spin_unlock(&idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040087 } else
Tony Jones1beeffe2007-08-20 13:46:20 -070088 dev_dbg(dev->parent,
Mark M. Hoffman12364412005-07-15 21:38:08 -040089 "hwmon_device_unregister() failed: bad class ID!\n");
90}
91
Jean Delvare2958b1e2009-06-15 18:39:50 +020092static void __init hwmon_pci_quirks(void)
93{
94#if defined CONFIG_X86 && defined CONFIG_PCI
95 struct pci_dev *sb;
96 u16 base;
97 u8 enable;
98
99 /* Open access to 0x295-0x296 on MSI MS-7031 */
100 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
101 if (sb &&
102 (sb->subsystem_vendor == 0x1462 && /* MSI */
103 sb->subsystem_device == 0x0031)) { /* MS-7031 */
104
105 pci_read_config_byte(sb, 0x48, &enable);
106 pci_read_config_word(sb, 0x64, &base);
107
108 if (base == 0 && !(enable & BIT(2))) {
109 dev_info(&sb->dev,
110 "Opening wide generic port at 0x295\n");
111 pci_write_config_word(sb, 0x64, 0x295);
112 pci_write_config_byte(sb, 0x48, enable | BIT(2));
113 }
114 }
115#endif
116}
117
Mark M. Hoffman12364412005-07-15 21:38:08 -0400118static int __init hwmon_init(void)
119{
Jean Delvare2958b1e2009-06-15 18:39:50 +0200120 hwmon_pci_quirks();
121
Mark M. Hoffman12364412005-07-15 21:38:08 -0400122 hwmon_class = class_create(THIS_MODULE, "hwmon");
123 if (IS_ERR(hwmon_class)) {
Joe Perchesc95df1a2010-10-20 06:51:37 +0000124 pr_err("couldn't create sysfs class\n");
Mark M. Hoffman12364412005-07-15 21:38:08 -0400125 return PTR_ERR(hwmon_class);
126 }
127 return 0;
128}
129
130static void __exit hwmon_exit(void)
131{
132 class_destroy(hwmon_class);
133}
134
David Brownell37f54ee2007-02-14 21:15:04 +0100135subsys_initcall(hwmon_init);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400136module_exit(hwmon_exit);
137
138EXPORT_SYMBOL_GPL(hwmon_device_register);
139EXPORT_SYMBOL_GPL(hwmon_device_unregister);
140
141MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
142MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
143MODULE_LICENSE("GPL");
144