Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / drivers / hwmon / hwmon.c
CommitLineData
1236441f
MH
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
c95df1ae
JP
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
1236441f
MH
15#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>
8c65b4a6 21#include <linux/gfp.h>
ded2b666 22#include <linux/spinlock.h>
2958b1ec 23#include <linux/pci.h>
1236441f
MH
24
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);
ded2b666 31static DEFINE_SPINLOCK(idr_lock);
1236441f
MH
32
33/**
1beeffe4 34 * hwmon_device_register - register w/ hwmon
1236441f
MH
35 * @dev: the device to register
36 *
1beeffe4 37 * hwmon_device_unregister() must be called when the device is no
1236441f
MH
38 * longer needed.
39 *
1beeffe4 40 * Returns the pointer to the new device.
1236441f 41 */
1beeffe4 42struct device *hwmon_device_register(struct device *dev)
1236441f 43{
1beeffe4 44 struct device *hwdev;
ded2b666 45 int id, err;
1236441f 46
ded2b666
MH
47again:
48 if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0))
1236441f
MH
49 return ERR_PTR(-ENOMEM);
50
ded2b666
MH
51 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);
1236441f
MH
59
60 id = id & MAX_ID_MASK;
a9b12619
GKH
61 hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL,
62 HWMON_ID_FORMAT, id);
1236441f 63
1beeffe4 64 if (IS_ERR(hwdev)) {
ded2b666 65 spin_lock(&idr_lock);
1236441f 66 idr_remove(&hwmon_idr, id);
ded2b666
MH
67 spin_unlock(&idr_lock);
68 }
1236441f 69
1beeffe4 70 return hwdev;
1236441f
MH
71}
72
73/**
74 * hwmon_device_unregister - removes the previously registered class device
75 *
1beeffe4 76 * @dev: the class device to destroy
1236441f 77 */
1beeffe4 78void hwmon_device_unregister(struct device *dev)
1236441f
MH
79{
80 int id;
81
739cf3a2 82 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
1beeffe4 83 device_unregister(dev);
ded2b666 84 spin_lock(&idr_lock);
1236441f 85 idr_remove(&hwmon_idr, id);
ded2b666 86 spin_unlock(&idr_lock);
1236441f 87 } else
1beeffe4 88 dev_dbg(dev->parent,
1236441f
MH
89 "hwmon_device_unregister() failed: bad class ID!\n");
90}
91
2958b1ec
JD
92static 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
1236441f
MH
118static int __init hwmon_init(void)
119{
2958b1ec
JD
120 hwmon_pci_quirks();
121
1236441f
MH
122 hwmon_class = class_create(THIS_MODULE, "hwmon");
123 if (IS_ERR(hwmon_class)) {
c95df1ae 124 pr_err("couldn't create sysfs class\n");
1236441f
MH
125 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
37f54ee5 135subsys_initcall(hwmon_init);
1236441f
MH
136module_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
This page took 0.528367 seconds and 5 git commands to generate.