hwmon: Convert from class_device to device
[deliverable/linux.git] / drivers / hwmon / hwmon.c
CommitLineData
1236441f
MH
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>
8c65b4a6 19#include <linux/gfp.h>
ded2b666 20#include <linux/spinlock.h>
1236441f
MH
21
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);
ded2b666 28static DEFINE_SPINLOCK(idr_lock);
1236441f
MH
29
30/**
1beeffe4 31 * hwmon_device_register - register w/ hwmon
1236441f
MH
32 * @dev: the device to register
33 *
1beeffe4 34 * hwmon_device_unregister() must be called when the device is no
1236441f
MH
35 * longer needed.
36 *
1beeffe4 37 * Returns the pointer to the new device.
1236441f 38 */
1beeffe4 39struct device *hwmon_device_register(struct device *dev)
1236441f 40{
1beeffe4 41 struct device *hwdev;
ded2b666 42 int id, err;
1236441f 43
ded2b666
MH
44again:
45 if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0))
1236441f
MH
46 return ERR_PTR(-ENOMEM);
47
ded2b666
MH
48 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);
1236441f
MH
56
57 id = id & MAX_ID_MASK;
1beeffe4 58 hwdev = device_create(hwmon_class, dev, MKDEV(0,0), HWMON_ID_FORMAT, id);
1236441f 59
1beeffe4 60 if (IS_ERR(hwdev)) {
ded2b666 61 spin_lock(&idr_lock);
1236441f 62 idr_remove(&hwmon_idr, id);
ded2b666
MH
63 spin_unlock(&idr_lock);
64 }
1236441f 65
1beeffe4 66 return hwdev;
1236441f
MH
67}
68
69/**
70 * hwmon_device_unregister - removes the previously registered class device
71 *
1beeffe4 72 * @dev: the class device to destroy
1236441f 73 */
1beeffe4 74void hwmon_device_unregister(struct device *dev)
1236441f
MH
75{
76 int id;
77
1beeffe4
TJ
78 if (likely(sscanf(dev->bus_id, HWMON_ID_FORMAT, &id) == 1)) {
79 device_unregister(dev);
ded2b666 80 spin_lock(&idr_lock);
1236441f 81 idr_remove(&hwmon_idr, id);
ded2b666 82 spin_unlock(&idr_lock);
1236441f 83 } else
1beeffe4 84 dev_dbg(dev->parent,
1236441f
MH
85 "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
37f54ee5 103subsys_initcall(hwmon_init);
1236441f
MH
104module_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
This page took 0.223782 seconds and 5 git commands to generate.