blob: 29ea6753f3bbe54592168dcaa1db07b1a2dbab3d [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
13#include <linux/module.h>
14#include <linux/device.h>
15#include <linux/err.h>
16#include <linux/kdev_t.h>
17#include <linux/idr.h>
18#include <linux/hwmon.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080019#include <linux/gfp.h>
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010020#include <linux/spinlock.h>
Jean Delvare2958b1e2009-06-15 18:39:50 +020021#include <linux/pci.h>
Mark M. Hoffman12364412005-07-15 21:38:08 -040022
23#define HWMON_ID_PREFIX "hwmon"
24#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
25
26static struct class *hwmon_class;
27
28static DEFINE_IDR(hwmon_idr);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010029static DEFINE_SPINLOCK(idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040030
31/**
Tony Jones1beeffe2007-08-20 13:46:20 -070032 * hwmon_device_register - register w/ hwmon
Mark M. Hoffman12364412005-07-15 21:38:08 -040033 * @dev: the device to register
34 *
Tony Jones1beeffe2007-08-20 13:46:20 -070035 * hwmon_device_unregister() must be called when the device is no
Mark M. Hoffman12364412005-07-15 21:38:08 -040036 * longer needed.
37 *
Tony Jones1beeffe2007-08-20 13:46:20 -070038 * Returns the pointer to the new device.
Mark M. Hoffman12364412005-07-15 21:38:08 -040039 */
Tony Jones1beeffe2007-08-20 13:46:20 -070040struct device *hwmon_device_register(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -040041{
Tony Jones1beeffe2007-08-20 13:46:20 -070042 struct device *hwdev;
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010043 int id, err;
Mark M. Hoffman12364412005-07-15 21:38:08 -040044
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010045again:
46 if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0))
Mark M. Hoffman12364412005-07-15 21:38:08 -040047 return ERR_PTR(-ENOMEM);
48
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010049 spin_lock(&idr_lock);
50 err = idr_get_new(&hwmon_idr, NULL, &id);
51 spin_unlock(&idr_lock);
52
53 if (unlikely(err == -EAGAIN))
54 goto again;
55 else if (unlikely(err))
56 return ERR_PTR(err);
Mark M. Hoffman12364412005-07-15 21:38:08 -040057
58 id = id & MAX_ID_MASK;
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -070059 hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL,
60 HWMON_ID_FORMAT, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -040061
Tony Jones1beeffe2007-08-20 13:46:20 -070062 if (IS_ERR(hwdev)) {
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010063 spin_lock(&idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040064 idr_remove(&hwmon_idr, id);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010065 spin_unlock(&idr_lock);
66 }
Mark M. Hoffman12364412005-07-15 21:38:08 -040067
Tony Jones1beeffe2007-08-20 13:46:20 -070068 return hwdev;
Mark M. Hoffman12364412005-07-15 21:38:08 -040069}
70
71/**
72 * hwmon_device_unregister - removes the previously registered class device
73 *
Tony Jones1beeffe2007-08-20 13:46:20 -070074 * @dev: the class device to destroy
Mark M. Hoffman12364412005-07-15 21:38:08 -040075 */
Tony Jones1beeffe2007-08-20 13:46:20 -070076void hwmon_device_unregister(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -040077{
78 int id;
79
Kay Sievers739cf3a2009-01-06 10:44:41 -080080 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
Tony Jones1beeffe2007-08-20 13:46:20 -070081 device_unregister(dev);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010082 spin_lock(&idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040083 idr_remove(&hwmon_idr, id);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010084 spin_unlock(&idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040085 } else
Tony Jones1beeffe2007-08-20 13:46:20 -070086 dev_dbg(dev->parent,
Mark M. Hoffman12364412005-07-15 21:38:08 -040087 "hwmon_device_unregister() failed: bad class ID!\n");
88}
89
Jean Delvare2958b1e2009-06-15 18:39:50 +020090static void __init hwmon_pci_quirks(void)
91{
92#if defined CONFIG_X86 && defined CONFIG_PCI
93 struct pci_dev *sb;
94 u16 base;
95 u8 enable;
96
97 /* Open access to 0x295-0x296 on MSI MS-7031 */
98 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
99 if (sb &&
100 (sb->subsystem_vendor == 0x1462 && /* MSI */
101 sb->subsystem_device == 0x0031)) { /* MS-7031 */
102
103 pci_read_config_byte(sb, 0x48, &enable);
104 pci_read_config_word(sb, 0x64, &base);
105
106 if (base == 0 && !(enable & BIT(2))) {
107 dev_info(&sb->dev,
108 "Opening wide generic port at 0x295\n");
109 pci_write_config_word(sb, 0x64, 0x295);
110 pci_write_config_byte(sb, 0x48, enable | BIT(2));
111 }
112 }
113#endif
114}
115
Mark M. Hoffman12364412005-07-15 21:38:08 -0400116static int __init hwmon_init(void)
117{
Jean Delvare2958b1e2009-06-15 18:39:50 +0200118 hwmon_pci_quirks();
119
Mark M. Hoffman12364412005-07-15 21:38:08 -0400120 hwmon_class = class_create(THIS_MODULE, "hwmon");
121 if (IS_ERR(hwmon_class)) {
122 printk(KERN_ERR "hwmon.c: couldn't create sysfs class\n");
123 return PTR_ERR(hwmon_class);
124 }
125 return 0;
126}
127
128static void __exit hwmon_exit(void)
129{
130 class_destroy(hwmon_class);
131}
132
David Brownell37f54ee2007-02-14 21:15:04 +0100133subsys_initcall(hwmon_init);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400134module_exit(hwmon_exit);
135
136EXPORT_SYMBOL_GPL(hwmon_device_register);
137EXPORT_SYMBOL_GPL(hwmon_device_unregister);
138
139MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
140MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
141MODULE_LICENSE("GPL");
142