hwmon.c 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
    hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring

    This file defines the sysfs class "hwmon", for use by sensors drivers.

    Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of the License.
*/

#include <linux/module.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/kdev_t.h>
#include <linux/idr.h>
#include <linux/hwmon.h>
19
#include <linux/gfp.h>
20
#include <linux/spinlock.h>
21 22 23 24 25 26 27

#define HWMON_ID_PREFIX "hwmon"
#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"

static struct class *hwmon_class;

static DEFINE_IDR(hwmon_idr);
28
static DEFINE_SPINLOCK(idr_lock);
29 30

/**
31
 * hwmon_device_register - register w/ hwmon
32 33
 * @dev: the device to register
 *
34
 * hwmon_device_unregister() must be called when the device is no
35 36
 * longer needed.
 *
37
 * Returns the pointer to the new device.
38
 */
39
struct device *hwmon_device_register(struct device *dev)
40
{
41
	struct device *hwdev;
42
	int id, err;
43

44 45
again:
	if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0))
46 47
		return ERR_PTR(-ENOMEM);

48 49 50 51 52 53 54 55
	spin_lock(&idr_lock);
	err = idr_get_new(&hwmon_idr, NULL, &id);
	spin_unlock(&idr_lock);

	if (unlikely(err == -EAGAIN))
		goto again;
	else if (unlikely(err))
		return ERR_PTR(err);
56 57

	id = id & MAX_ID_MASK;
58 59
	hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL,
			      HWMON_ID_FORMAT, id);
60

61
	if (IS_ERR(hwdev)) {
62
		spin_lock(&idr_lock);
63
		idr_remove(&hwmon_idr, id);
64 65
		spin_unlock(&idr_lock);
	}
66

67
	return hwdev;
68 69 70 71 72
}

/**
 * hwmon_device_unregister - removes the previously registered class device
 *
73
 * @dev: the class device to destroy
74
 */
75
void hwmon_device_unregister(struct device *dev)
76 77 78
{
	int id;

79 80
	if (likely(sscanf(dev->bus_id, HWMON_ID_FORMAT, &id) == 1)) {
		device_unregister(dev);
81
		spin_lock(&idr_lock);
82
		idr_remove(&hwmon_idr, id);
83
		spin_unlock(&idr_lock);
84
	} else
85
		dev_dbg(dev->parent,
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
			"hwmon_device_unregister() failed: bad class ID!\n");
}

static int __init hwmon_init(void)
{
	hwmon_class = class_create(THIS_MODULE, "hwmon");
	if (IS_ERR(hwmon_class)) {
		printk(KERN_ERR "hwmon.c: couldn't create sysfs class\n");
		return PTR_ERR(hwmon_class);
	}
	return 0;
}

static void __exit hwmon_exit(void)
{
	class_destroy(hwmon_class);
}

D
David Brownell 已提交
104
subsys_initcall(hwmon_init);
105 106 107 108 109 110 111 112 113
module_exit(hwmon_exit);

EXPORT_SYMBOL_GPL(hwmon_device_register);
EXPORT_SYMBOL_GPL(hwmon_device_unregister);

MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
MODULE_LICENSE("GPL");