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: Guenter Roeck <linux@roeck-us.net>
This commit is contained in:
Родитель
522928a87f
Коммит
bab2243ce1
|
@ -15,6 +15,7 @@
|
|||
#include <linux/module.h>
|
||||
#include <linux/device.h>
|
||||
#include <linux/err.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/kdev_t.h>
|
||||
#include <linux/idr.h>
|
||||
#include <linux/hwmon.h>
|
||||
|
@ -25,10 +26,110 @@
|
|||
#define HWMON_ID_PREFIX "hwmon"
|
||||
#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);
|
||||
|
||||
/**
|
||||
* hwmon_device_register_with_groups - register w/ hwmon
|
||||
* @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
|
||||
* longer needed.
|
||||
*
|
||||
* Returns the pointer to the new device.
|
||||
*/
|
||||
struct device *
|
||||
hwmon_device_register_with_groups(struct device *dev, const char *name,
|
||||
void *drvdata,
|
||||
const struct attribute_group **groups)
|
||||
{
|
||||
struct hwmon_device *hwdev;
|
||||
int err, id;
|
||||
|
||||
id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
|
||||
if (id < 0)
|
||||
return ERR_PTR(id);
|
||||
|
||||
hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
|
||||
if (hwdev == NULL) {
|
||||
err = -ENOMEM;
|
||||
goto ida_remove;
|
||||
}
|
||||
|
||||
hwdev->name = name;
|
||||
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);
|
||||
|
||||
/**
|
||||
* hwmon_device_register - register w/ hwmon
|
||||
* @dev: the device to register
|
||||
|
@ -40,20 +141,7 @@ static DEFINE_IDA(hwmon_ida);
|
|||
*/
|
||||
struct device *hwmon_device_register(struct device *dev)
|
||||
{
|
||||
struct device *hwdev;
|
||||
int id;
|
||||
|
||||
id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
|
||||
if (id < 0)
|
||||
return ERR_PTR(id);
|
||||
|
||||
hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL,
|
||||
HWMON_ID_FORMAT, id);
|
||||
|
||||
if (IS_ERR(hwdev))
|
||||
ida_simple_remove(&hwmon_ida, id);
|
||||
|
||||
return hwdev;
|
||||
return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(hwmon_device_register);
|
||||
|
||||
|
@ -105,19 +193,21 @@ static void __init hwmon_pci_quirks(void)
|
|||
|
||||
static int __init hwmon_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
hwmon_pci_quirks();
|
||||
|
||||
hwmon_class = class_create(THIS_MODULE, "hwmon");
|
||||
if (IS_ERR(hwmon_class)) {
|
||||
pr_err("couldn't create sysfs class\n");
|
||||
return PTR_ERR(hwmon_class);
|
||||
err = class_register(&hwmon_class);
|
||||
if (err) {
|
||||
pr_err("couldn't register hwmon sysfs class\n");
|
||||
return err;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit hwmon_exit(void)
|
||||
{
|
||||
class_destroy(hwmon_class);
|
||||
class_unregister(&hwmon_class);
|
||||
}
|
||||
|
||||
subsys_initcall(hwmon_init);
|
||||
|
|
|
@ -15,8 +15,13 @@
|
|||
#define _HWMON_H_
|
||||
|
||||
struct device;
|
||||
struct attribute_group;
|
||||
|
||||
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);
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче