blob: dddd3eb9b3870ef060e84db9d8db4e099303c71d [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. Hoffman12364412005-07-15 21:38:08 -040020
21#define HWMON_ID_PREFIX "hwmon"
22#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
23
24static struct class *hwmon_class;
25
26static DEFINE_IDR(hwmon_idr);
27
28/**
29 * hwmon_device_register - register w/ hwmon sysfs class
30 * @dev: the device to register
31 *
32 * hwmon_device_unregister() must be called when the class device is no
33 * longer needed.
34 *
35 * Returns the pointer to the new struct class device.
36 */
37struct class_device *hwmon_device_register(struct device *dev)
38{
39 struct class_device *cdev;
40 int id;
41
42 if (idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0)
43 return ERR_PTR(-ENOMEM);
44
45 if (idr_get_new(&hwmon_idr, NULL, &id) < 0)
46 return ERR_PTR(-ENOMEM);
47
48 id = id & MAX_ID_MASK;
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -070049 cdev = class_device_create(hwmon_class, NULL, MKDEV(0,0), dev,
Mark M. Hoffman12364412005-07-15 21:38:08 -040050 HWMON_ID_FORMAT, id);
51
52 if (IS_ERR(cdev))
53 idr_remove(&hwmon_idr, id);
54
55 return cdev;
56}
57
58/**
59 * hwmon_device_unregister - removes the previously registered class device
60 *
61 * @cdev: the class device to destroy
62 */
63void hwmon_device_unregister(struct class_device *cdev)
64{
65 int id;
66
67 if (sscanf(cdev->class_id, HWMON_ID_FORMAT, &id) == 1) {
68 class_device_unregister(cdev);
69 idr_remove(&hwmon_idr, id);
70 } else
71 dev_dbg(cdev->dev,
72 "hwmon_device_unregister() failed: bad class ID!\n");
73}
74
75static int __init hwmon_init(void)
76{
77 hwmon_class = class_create(THIS_MODULE, "hwmon");
78 if (IS_ERR(hwmon_class)) {
79 printk(KERN_ERR "hwmon.c: couldn't create sysfs class\n");
80 return PTR_ERR(hwmon_class);
81 }
82 return 0;
83}
84
85static void __exit hwmon_exit(void)
86{
87 class_destroy(hwmon_class);
88}
89
90module_init(hwmon_init);
91module_exit(hwmon_exit);
92
93EXPORT_SYMBOL_GPL(hwmon_device_register);
94EXPORT_SYMBOL_GPL(hwmon_device_unregister);
95
96MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
97MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
98MODULE_LICENSE("GPL");
99