Commit bab2243c authored by Guenter Roeck's avatar Guenter Roeck

hwmon: Introduce hwmon_device_register_with_groups

hwmon_device_register_with_groups() lets callers register a hwmon device
together with all sysfs attributes in a single call.

When using hwmon_device_register_with_groups(), hwmon attributes are attached
to the hwmon device directly and no longer with its parent device.
Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
parent 522928a8
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/slab.h>
#include <linux/kdev_t.h> #include <linux/kdev_t.h>
#include <linux/idr.h> #include <linux/idr.h>
#include <linux/hwmon.h> #include <linux/hwmon.h>
...@@ -25,35 +26,122 @@ ...@@ -25,35 +26,122 @@
#define HWMON_ID_PREFIX "hwmon" #define HWMON_ID_PREFIX "hwmon"
#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
static struct class *hwmon_class; struct hwmon_device {
const char *name;
struct device dev;
};
#define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
static ssize_t
show_name(struct device *dev, struct device_attribute *attr, char *buf)
{
return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
}
static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
static struct attribute *hwmon_dev_attrs[] = {
&dev_attr_name.attr,
NULL
};
static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
struct attribute *attr, int n)
{
struct device *dev = container_of(kobj, struct device, kobj);
if (to_hwmon_device(dev)->name == NULL)
return 0;
return attr->mode;
}
static struct attribute_group hwmon_dev_attr_group = {
.attrs = hwmon_dev_attrs,
.is_visible = hwmon_dev_name_is_visible,
};
static const struct attribute_group *hwmon_dev_attr_groups[] = {
&hwmon_dev_attr_group,
NULL
};
static void hwmon_dev_release(struct device *dev)
{
kfree(to_hwmon_device(dev));
}
static struct class hwmon_class = {
.name = "hwmon",
.owner = THIS_MODULE,
.dev_groups = hwmon_dev_attr_groups,
.dev_release = hwmon_dev_release,
};
static DEFINE_IDA(hwmon_ida); static DEFINE_IDA(hwmon_ida);
/** /**
* hwmon_device_register - register w/ hwmon * hwmon_device_register_with_groups - register w/ hwmon
* @dev: the device to register * @dev: the parent device
* @name: hwmon name attribute
* @drvdata: driver data to attach to created device
* @groups: List of attribute groups to create
* *
* hwmon_device_unregister() must be called when the device is no * hwmon_device_unregister() must be called when the device is no
* longer needed. * longer needed.
* *
* Returns the pointer to the new device. * Returns the pointer to the new device.
*/ */
struct device *hwmon_device_register(struct device *dev) struct device *
hwmon_device_register_with_groups(struct device *dev, const char *name,
void *drvdata,
const struct attribute_group **groups)
{ {
struct device *hwdev; struct hwmon_device *hwdev;
int id; int err, id;
id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
if (id < 0) if (id < 0)
return ERR_PTR(id); return ERR_PTR(id);
hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL, hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
HWMON_ID_FORMAT, id); if (hwdev == NULL) {
err = -ENOMEM;
goto ida_remove;
}
if (IS_ERR(hwdev)) hwdev->name = name;
ida_simple_remove(&hwmon_ida, id); hwdev->dev.class = &hwmon_class;
hwdev->dev.parent = dev;
hwdev->dev.groups = groups;
hwdev->dev.of_node = dev ? dev->of_node : NULL;
dev_set_drvdata(&hwdev->dev, drvdata);
dev_set_name(&hwdev->dev, HWMON_ID_FORMAT, id);
err = device_register(&hwdev->dev);
if (err)
goto free;
return &hwdev->dev;
free:
kfree(hwdev);
ida_remove:
ida_simple_remove(&hwmon_ida, id);
return ERR_PTR(err);
}
EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
return hwdev; /**
* hwmon_device_register - register w/ hwmon
* @dev: the device to register
*
* hwmon_device_unregister() must be called when the device is no
* longer needed.
*
* Returns the pointer to the new device.
*/
struct device *hwmon_device_register(struct device *dev)
{
return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
} }
EXPORT_SYMBOL_GPL(hwmon_device_register); EXPORT_SYMBOL_GPL(hwmon_device_register);
...@@ -105,19 +193,21 @@ static void __init hwmon_pci_quirks(void) ...@@ -105,19 +193,21 @@ static void __init hwmon_pci_quirks(void)
static int __init hwmon_init(void) static int __init hwmon_init(void)
{ {
int err;
hwmon_pci_quirks(); hwmon_pci_quirks();
hwmon_class = class_create(THIS_MODULE, "hwmon"); err = class_register(&hwmon_class);
if (IS_ERR(hwmon_class)) { if (err) {
pr_err("couldn't create sysfs class\n"); pr_err("couldn't register hwmon sysfs class\n");
return PTR_ERR(hwmon_class); return err;
} }
return 0; return 0;
} }
static void __exit hwmon_exit(void) static void __exit hwmon_exit(void)
{ {
class_destroy(hwmon_class); class_unregister(&hwmon_class);
} }
subsys_initcall(hwmon_init); subsys_initcall(hwmon_init);
......
...@@ -15,8 +15,13 @@ ...@@ -15,8 +15,13 @@
#define _HWMON_H_ #define _HWMON_H_
struct device; struct device;
struct attribute_group;
struct device *hwmon_device_register(struct device *dev); struct device *hwmon_device_register(struct device *dev);
struct device *
hwmon_device_register_with_groups(struct device *dev, const char *name,
void *drvdata,
const struct attribute_group **groups);
void hwmon_device_unregister(struct device *dev); void hwmon_device_unregister(struct device *dev);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment