1
2
3
4
5
6
7
8
9
10
11
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
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>
21#include <linux/gfp.h>
22#include <linux/spinlock.h>
23#include <linux/pci.h>
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_IDA(hwmon_ida);
31
32
33
34
35
36
37
38
39
40
41struct device *hwmon_device_register(struct device *dev)
42{
43 struct device *hwdev;
44 int id;
45
46 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
47 if (id < 0)
48 return ERR_PTR(id);
49
50 hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL,
51 HWMON_ID_FORMAT, id);
52
53 if (IS_ERR(hwdev))
54 ida_simple_remove(&hwmon_ida, id);
55
56 return hwdev;
57}
58EXPORT_SYMBOL_GPL(hwmon_device_register);
59
60
61
62
63
64
65void hwmon_device_unregister(struct device *dev)
66{
67 int id;
68
69 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
70 device_unregister(dev);
71 ida_simple_remove(&hwmon_ida, id);
72 } else
73 dev_dbg(dev->parent,
74 "hwmon_device_unregister() failed: bad class ID!\n");
75}
76EXPORT_SYMBOL_GPL(hwmon_device_unregister);
77
78static void __init hwmon_pci_quirks(void)
79{
80#if defined CONFIG_X86 && defined CONFIG_PCI
81 struct pci_dev *sb;
82 u16 base;
83 u8 enable;
84
85
86 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
87 if (sb &&
88 (sb->subsystem_vendor == 0x1462 &&
89 sb->subsystem_device == 0x0031)) {
90
91 pci_read_config_byte(sb, 0x48, &enable);
92 pci_read_config_word(sb, 0x64, &base);
93
94 if (base == 0 && !(enable & BIT(2))) {
95 dev_info(&sb->dev,
96 "Opening wide generic port at 0x295\n");
97 pci_write_config_word(sb, 0x64, 0x295);
98 pci_write_config_byte(sb, 0x48, enable | BIT(2));
99 }
100 }
101#endif
102}
103
104static int __init hwmon_init(void)
105{
106 hwmon_pci_quirks();
107
108 hwmon_class = class_create(THIS_MODULE, "hwmon");
109 if (IS_ERR(hwmon_class)) {
110 pr_err("couldn't create sysfs class\n");
111 return PTR_ERR(hwmon_class);
112 }
113 return 0;
114}
115
116static void __exit hwmon_exit(void)
117{
118 class_destroy(hwmon_class);
119}
120
121subsys_initcall(hwmon_init);
122module_exit(hwmon_exit);
123
124MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
125MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
126MODULE_LICENSE("GPL");
127
128