blob: 3db28450a3b34504facc27cc8a9611a8e53569cd [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>
Mark M. Hoffman12364412005-07-15 21:38:08 -040021
22#define HWMON_ID_PREFIX "hwmon"
23#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
24
25static struct class *hwmon_class;
26
27static DEFINE_IDR(hwmon_idr);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010028static DEFINE_SPINLOCK(idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040029
30/**
Tony Jones1beeffe2007-08-20 13:46:20 -070031 * hwmon_device_register - register w/ hwmon
Mark M. Hoffman12364412005-07-15 21:38:08 -040032 * @dev: the device to register
33 *
Tony Jones1beeffe2007-08-20 13:46:20 -070034 * hwmon_device_unregister() must be called when the device is no
Mark M. Hoffman12364412005-07-15 21:38:08 -040035 * longer needed.
36 *
Tony Jones1beeffe2007-08-20 13:46:20 -070037 * Returns the pointer to the new device.
Mark M. Hoffman12364412005-07-15 21:38:08 -040038 */
Tony Jones1beeffe2007-08-20 13:46:20 -070039struct device *hwmon_device_register(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -040040{
Tony Jones1beeffe2007-08-20 13:46:20 -070041 struct device *hwdev;
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010042 int id, err;
Mark M. Hoffman12364412005-07-15 21:38:08 -040043
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010044again:
45 if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0))
Mark M. Hoffman12364412005-07-15 21:38:08 -040046 return ERR_PTR(-ENOMEM);
47
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010048 spin_lock(&idr_lock);
49 err = idr_get_new(&hwmon_idr, NULL, &id);
50 spin_unlock(&idr_lock);
51
52 if (unlikely(err == -EAGAIN))
53 goto again;
54 else if (unlikely(err))
55 return ERR_PTR(err);
Mark M. Hoffman12364412005-07-15 21:38:08 -040056
57 id = id & MAX_ID_MASK;
Tony Jones1beeffe2007-08-20 13:46:20 -070058 hwdev = device_create(hwmon_class, dev, MKDEV(0,0), HWMON_ID_FORMAT, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -040059
Tony Jones1beeffe2007-08-20 13:46:20 -070060 if (IS_ERR(hwdev)) {
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010061 spin_lock(&idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040062 idr_remove(&hwmon_idr, id);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010063 spin_unlock(&idr_lock);
64 }
Mark M. Hoffman12364412005-07-15 21:38:08 -040065
Tony Jones1beeffe2007-08-20 13:46:20 -070066 return hwdev;
Mark M. Hoffman12364412005-07-15 21:38:08 -040067}
68
69/**
70 * hwmon_device_unregister - removes the previously registered class device
71 *
Tony Jones1beeffe2007-08-20 13:46:20 -070072 * @dev: the class device to destroy
Mark M. Hoffman12364412005-07-15 21:38:08 -040073 */
Tony Jones1beeffe2007-08-20 13:46:20 -070074void hwmon_device_unregister(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -040075{
76 int id;
77
Tony Jones1beeffe2007-08-20 13:46:20 -070078 if (likely(sscanf(dev->bus_id, HWMON_ID_FORMAT, &id) == 1)) {
79 device_unregister(dev);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010080 spin_lock(&idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040081 idr_remove(&hwmon_idr, id);
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010082 spin_unlock(&idr_lock);
Mark M. Hoffman12364412005-07-15 21:38:08 -040083 } else
Tony Jones1beeffe2007-08-20 13:46:20 -070084 dev_dbg(dev->parent,
Mark M. Hoffman12364412005-07-15 21:38:08 -040085 "hwmon_device_unregister() failed: bad class ID!\n");
86}
87
88static int __init hwmon_init(void)
89{
90 hwmon_class = class_create(THIS_MODULE, "hwmon");
91 if (IS_ERR(hwmon_class)) {
92 printk(KERN_ERR "hwmon.c: couldn't create sysfs class\n");
93 return PTR_ERR(hwmon_class);
94 }
95 return 0;
96}
97
98static void __exit hwmon_exit(void)
99{
100 class_destroy(hwmon_class);
101}
102
David Brownell37f54ee2007-02-14 21:15:04 +0100103subsys_initcall(hwmon_init);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400104module_exit(hwmon_exit);
105
106EXPORT_SYMBOL_GPL(hwmon_device_register);
107EXPORT_SYMBOL_GPL(hwmon_device_unregister);
108
109MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
110MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
111MODULE_LICENSE("GPL");
112