Line data Source code
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 :
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>
19 : #include <linux/gfp.h>
20 : #include <linux/spinlock.h>
21 : #include <linux/pci.h>
22 :
23 : #define HWMON_ID_PREFIX "hwmon"
24 : #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
25 :
26 1 : static struct class *hwmon_class;
27 :
28 1 : static DEFINE_IDR(hwmon_idr);
29 1 : static DEFINE_SPINLOCK(idr_lock);
30 :
31 : /**
32 : * hwmon_device_register - register w/ hwmon
33 : * @dev: the device to register
34 : *
35 : * hwmon_device_unregister() must be called when the device is no
36 : * longer needed.
37 : *
38 : * Returns the pointer to the new device.
39 : */
40 : struct device *hwmon_device_register(struct device *dev)
41 : {
42 0 : struct device *hwdev;
43 0 : int id, err;
44 0 :
45 0 : again:
46 0 : if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0))
47 0 : return ERR_PTR(-ENOMEM);
48 0 :
49 0 : spin_lock(&idr_lock);
50 0 : err = idr_get_new(&hwmon_idr, NULL, &id);
51 0 : spin_unlock(&idr_lock);
52 :
53 0 : if (unlikely(err == -EAGAIN))
54 0 : goto again;
55 0 : else if (unlikely(err))
56 0 : return ERR_PTR(err);
57 :
58 0 : id = id & MAX_ID_MASK;
59 0 : hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL,
60 : HWMON_ID_FORMAT, id);
61 :
62 0 : if (IS_ERR(hwdev)) {
63 0 : spin_lock(&idr_lock);
64 0 : idr_remove(&hwmon_idr, id);
65 0 : spin_unlock(&idr_lock);
66 : }
67 :
68 0 : return hwdev;
69 : }
70 :
71 : /**
72 : * hwmon_device_unregister - removes the previously registered class device
73 : *
74 : * @dev: the class device to destroy
75 : */
76 : void hwmon_device_unregister(struct device *dev)
77 : {
78 0 : int id;
79 0 :
80 0 : if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
81 0 : device_unregister(dev);
82 0 : spin_lock(&idr_lock);
83 0 : idr_remove(&hwmon_idr, id);
84 0 : spin_unlock(&idr_lock);
85 : } else
86 : dev_dbg(dev->parent,
87 : "hwmon_device_unregister() failed: bad class ID!\n");
88 0 : }
89 :
90 : static void __init hwmon_pci_quirks(void)
91 : {
92 1 : #if defined CONFIG_X86 && defined CONFIG_PCI
93 1 : struct pci_dev *sb;
94 1 : u16 base;
95 1 : u8 enable;
96 1 :
97 : /* Open access to 0x295-0x296 on MSI MS-7031 */
98 1 : sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
99 4 : if (sb &&
100 : (sb->subsystem_vendor == 0x1462 && /* MSI */
101 : sb->subsystem_device == 0x0031)) { /* MS-7031 */
102 :
103 2 : pci_read_config_byte(sb, 0x48, &enable);
104 2 : pci_read_config_word(sb, 0x64, &base);
105 :
106 4 : if (base == 0 && !(enable & BIT(2))) {
107 4 : dev_info(&sb->dev,
108 : "Opening wide generic port at 0x295\n");
109 2 : pci_write_config_word(sb, 0x64, 0x295);
110 2 : pci_write_config_byte(sb, 0x48, enable | BIT(2));
111 : }
112 : }
113 3 : #endif
114 : }
115 :
116 : static int __init hwmon_init(void)
117 : {
118 5 : hwmon_pci_quirks();
119 1 :
120 3 : hwmon_class = class_create(THIS_MODULE, "hwmon");
121 5 : if (IS_ERR(hwmon_class)) {
122 1 : printk(KERN_ERR "hwmon.c: couldn't create sysfs class\n");
123 3 : return PTR_ERR(hwmon_class);
124 : }
125 1 : return 0;
126 : }
127 :
128 : static void __exit hwmon_exit(void)
129 : {
130 1 : class_destroy(hwmon_class);
131 1 : }
132 :
133 : subsys_initcall(hwmon_init);
134 : module_exit(hwmon_exit);
135 1 :
136 : EXPORT_SYMBOL_GPL(hwmon_device_register);
137 : EXPORT_SYMBOL_GPL(hwmon_device_unregister);
138 :
139 : MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
140 : MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
141 : MODULE_LICENSE("GPL");
142 :
|