Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[linux/fpc-iii.git] / drivers / hwmon / hwmon.c
blob106fa01cdb6069717c61ee6a596e94d1ad38bf98
1 /*
2 hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
4 This file defines the sysfs class "hwmon", for use by sensors drivers.
6 Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
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.
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>
22 #define HWMON_ID_PREFIX "hwmon"
23 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
25 static struct class *hwmon_class;
27 static DEFINE_IDR(hwmon_idr);
28 static DEFINE_SPINLOCK(idr_lock);
30 /**
31 * hwmon_device_register - register w/ hwmon sysfs class
32 * @dev: the device to register
34 * hwmon_device_unregister() must be called when the class device is no
35 * longer needed.
37 * Returns the pointer to the new struct class device.
39 struct class_device *hwmon_device_register(struct device *dev)
41 struct class_device *cdev;
42 int id, err;
44 again:
45 if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0))
46 return ERR_PTR(-ENOMEM);
48 spin_lock(&idr_lock);
49 err = idr_get_new(&hwmon_idr, NULL, &id);
50 spin_unlock(&idr_lock);
52 if (unlikely(err == -EAGAIN))
53 goto again;
54 else if (unlikely(err))
55 return ERR_PTR(err);
57 id = id & MAX_ID_MASK;
58 cdev = class_device_create(hwmon_class, NULL, MKDEV(0,0), dev,
59 HWMON_ID_FORMAT, id);
61 if (IS_ERR(cdev)) {
62 spin_lock(&idr_lock);
63 idr_remove(&hwmon_idr, id);
64 spin_unlock(&idr_lock);
67 return cdev;
70 /**
71 * hwmon_device_unregister - removes the previously registered class device
73 * @cdev: the class device to destroy
75 void hwmon_device_unregister(struct class_device *cdev)
77 int id;
79 if (likely(sscanf(cdev->class_id, HWMON_ID_FORMAT, &id) == 1)) {
80 class_device_unregister(cdev);
81 spin_lock(&idr_lock);
82 idr_remove(&hwmon_idr, id);
83 spin_unlock(&idr_lock);
84 } else
85 dev_dbg(cdev->dev,
86 "hwmon_device_unregister() failed: bad class ID!\n");
89 static int __init hwmon_init(void)
91 hwmon_class = class_create(THIS_MODULE, "hwmon");
92 if (IS_ERR(hwmon_class)) {
93 printk(KERN_ERR "hwmon.c: couldn't create sysfs class\n");
94 return PTR_ERR(hwmon_class);
96 return 0;
99 static void __exit hwmon_exit(void)
101 class_destroy(hwmon_class);
104 module_init(hwmon_init);
105 module_exit(hwmon_exit);
107 EXPORT_SYMBOL_GPL(hwmon_device_register);
108 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
110 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
111 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
112 MODULE_LICENSE("GPL");