/*
* class.c - basic device class management
*
* Copyright (c) 2002-3 Patrick Mochel
* Copyright (c) 2002-3 Open Source Development Labs
* Copyright (c) 2003-2004 Greg Kroah-Hartman
* Copyright (c) 2003-2004 IBM Corp.
*
* This file is released under the GPLv2
*
*/
#include <linux/device.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/kdev_t.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/genhd.h>
#include "base.h"
#define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
#define to_class(obj) container_of(obj, struct class, subsys.kobj)
static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct class_attribute *class_attr = to_class_attr(attr);
struct class *dc = to_class(kobj);
ssize_t ret = -EIO;
if (class_attr->show)
ret = class_attr->show(dc, buf);
return ret;
}
static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
struct class_attribute *class_attr = to_class_attr(attr);
struct class *dc = to_class(kobj);
ssize_t ret = -EIO;
if (class_attr->store)
ret = class_attr->store(dc, buf, count);
return ret;
}
static void class_release(struct kobject *kobj)
{
struct class *class = to_class(kobj);
pr_debug("class '%s': release.\n", class->name);
if (class->class_release)
class->class_release(class);
else
pr_debug("class '%s' does not have a release() function, "
"be careful\n", class->name);
}
static struct sysfs_ops class_sysfs_ops = {
.show = class_attr_show,
.store = class_attr_store,
};
static struct kobj_type class_ktype = {
.sysfs_ops = &class_sysfs_ops,
.release = class_release,
};
/* Hotplug events for classes go to the class_obj subsys */
static struct kset *class_kset;
int class_create_file(struct class *cls, const struct class_attribute *attr)
{
int error;
if (cls)
error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
else
error = -EINVAL;
return error;
}
void class_remove_file(struct class *cls, const struct class_attribute *attr)
{
if (cls)
sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
}
static struct class *class_get(struct class *cls)
{
if (cls)
return container_of(kset_get(&cls->subsys),
struct class, subsys);
return NULL;
}
static void class_put(struct class *cls)
{
if (cls)
kset_put(&cls->subsys);
}
static int add_class_attrs(struct class *cls)
{
int i;
int error = 0;
if (cls->class_attrs) {
for (i = 0; attr_name(cls->class_attrs[i]); i++) {
error = class_create_file(cls, &cls->class_attrs[i]);
if (error)
goto error;
}
}
done:
return error;
error:
while (--i >= 0)
class_remove_file(cls, &cls->class_attrs[i]);
goto done;
}
static void remove_class_attrs(struct class *cls)
{
int i;
if (cls->class_attrs) {
for (i = 0; attr_name(cls->class_attrs[i]); i++)
class_remove_file(cls, &cls->class_attrs[i]);
}
}
int class_register(struct class *cls)
{
int error;
pr_debug("device class '%s': registering\n", cls->name);
INIT_LIST_HEAD(&