Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 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> |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 19 | #include <linux/gfp.h> |
Mark M. Hoffman | ded2b66 | 2006-03-05 23:13:47 +0100 | [diff] [blame] | 20 | #include <linux/spinlock.h> |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 21 | |
| 22 | #define HWMON_ID_PREFIX "hwmon" |
| 23 | #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" |
| 24 | |
| 25 | static struct class *hwmon_class; |
| 26 | |
| 27 | static DEFINE_IDR(hwmon_idr); |
Mark M. Hoffman | ded2b66 | 2006-03-05 23:13:47 +0100 | [diff] [blame] | 28 | static DEFINE_SPINLOCK(idr_lock); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 29 | |
| 30 | /** |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 31 | * hwmon_device_register - register w/ hwmon |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 32 | * @dev: the device to register |
| 33 | * |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 34 | * hwmon_device_unregister() must be called when the device is no |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 35 | * longer needed. |
| 36 | * |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 37 | * Returns the pointer to the new device. |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 38 | */ |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 39 | struct device *hwmon_device_register(struct device *dev) |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 40 | { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 41 | struct device *hwdev; |
Mark M. Hoffman | ded2b66 | 2006-03-05 23:13:47 +0100 | [diff] [blame] | 42 | int id, err; |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 43 | |
Mark M. Hoffman | ded2b66 | 2006-03-05 23:13:47 +0100 | [diff] [blame] | 44 | again: |
| 45 | if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0)) |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 46 | return ERR_PTR(-ENOMEM); |
| 47 | |
Mark M. Hoffman | ded2b66 | 2006-03-05 23:13:47 +0100 | [diff] [blame] | 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); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 56 | |
| 57 | id = id & MAX_ID_MASK; |
Greg Kroah-Hartman | a9b1261 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 58 | hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL, |
| 59 | HWMON_ID_FORMAT, id); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 60 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 61 | if (IS_ERR(hwdev)) { |
Mark M. Hoffman | ded2b66 | 2006-03-05 23:13:47 +0100 | [diff] [blame] | 62 | spin_lock(&idr_lock); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 63 | idr_remove(&hwmon_idr, id); |
Mark M. Hoffman | ded2b66 | 2006-03-05 23:13:47 +0100 | [diff] [blame] | 64 | spin_unlock(&idr_lock); |
| 65 | } |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 66 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 67 | return hwdev; |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * hwmon_device_unregister - removes the previously registered class device |
| 72 | * |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 73 | * @dev: the class device to destroy |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 74 | */ |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 75 | void hwmon_device_unregister(struct device *dev) |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 76 | { |
| 77 | int id; |
| 78 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 79 | if (likely(sscanf(dev->bus_id, HWMON_ID_FORMAT, &id) == 1)) { |
| 80 | device_unregister(dev); |
Mark M. Hoffman | ded2b66 | 2006-03-05 23:13:47 +0100 | [diff] [blame] | 81 | spin_lock(&idr_lock); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 82 | idr_remove(&hwmon_idr, id); |
Mark M. Hoffman | ded2b66 | 2006-03-05 23:13:47 +0100 | [diff] [blame] | 83 | spin_unlock(&idr_lock); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 84 | } else |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 85 | dev_dbg(dev->parent, |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 86 | "hwmon_device_unregister() failed: bad class ID!\n"); |
| 87 | } |
| 88 | |
| 89 | static int __init hwmon_init(void) |
| 90 | { |
| 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); |
| 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static void __exit hwmon_exit(void) |
| 100 | { |
| 101 | class_destroy(hwmon_class); |
| 102 | } |
| 103 | |
David Brownell | 37f54ee | 2007-02-14 21:15:04 +0100 | [diff] [blame] | 104 | subsys_initcall(hwmon_init); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 105 | module_exit(hwmon_exit); |
| 106 | |
| 107 | EXPORT_SYMBOL_GPL(hwmon_device_register); |
| 108 | EXPORT_SYMBOL_GPL(hwmon_device_unregister); |
| 109 | |
| 110 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
| 111 | MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); |
| 112 | MODULE_LICENSE("GPL"); |
| 113 | |