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