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
);
31 * hwmon_device_register - register w/ hwmon
32 * @dev: the device to register
34 * hwmon_device_unregister() must be called when the device is no
37 * Returns the pointer to the new device.
39 struct device
*hwmon_device_register(struct device
*dev
)
45 if (unlikely(idr_pre_get(&hwmon_idr
, GFP_KERNEL
) == 0))
46 return ERR_PTR(-ENOMEM
);
49 err
= idr_get_new(&hwmon_idr
, NULL
, &id
);
50 spin_unlock(&idr_lock
);
52 if (unlikely(err
== -EAGAIN
))
54 else if (unlikely(err
))
57 id
= id
& MAX_ID_MASK
;
58 hwdev
= device_create(hwmon_class
, dev
, MKDEV(0,0), HWMON_ID_FORMAT
, id
);
62 idr_remove(&hwmon_idr
, id
);
63 spin_unlock(&idr_lock
);
70 * hwmon_device_unregister - removes the previously registered class device
72 * @dev: the class device to destroy
74 void hwmon_device_unregister(struct device
*dev
)
78 if (likely(sscanf(dev
->bus_id
, HWMON_ID_FORMAT
, &id
) == 1)) {
79 device_unregister(dev
);
81 idr_remove(&hwmon_idr
, id
);
82 spin_unlock(&idr_lock
);
85 "hwmon_device_unregister() failed: bad class ID!\n");
88 static int __init
hwmon_init(void)
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
);
98 static void __exit
hwmon_exit(void)
100 class_destroy(hwmon_class
);
103 subsys_initcall(hwmon_init
);
104 module_exit(hwmon_exit
);
106 EXPORT_SYMBOL_GPL(hwmon_device_register
);
107 EXPORT_SYMBOL_GPL(hwmon_device_unregister
);
109 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
110 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
111 MODULE_LICENSE("GPL");