/*
* sysfs example
*/
#include <linux/kobject.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/init.h>
/*
* This module shows how to create a kset in sysfs called
* /sys/kernel/ks_a
*/
/*
* This is our "object" that we will create a few of and register them with
* sysfs.
*/
struct fu_obj {
struct kobject kobj;
int par;
};
#define to_fu_obj(x) container_of(x, struct fu_obj, kobj)
/* a custom attribute that works just for a struct fu_obj. */
struct fu_attribute {
struct attribute attr;
ssize_t (*show)(struct fu_obj *fu, struct fu_attribute *attr, char *buf);
ssize_t (*store)(struct fu_obj *fu, struct fu_attribute *attr, const char *buf, size_t count);
};
#define to_fu_attr(x) container_of(x, struct fu_attribute, attr)
/*
* The default show function that must be passed to sysfs. This will be
* called by sysfs for whenever a show function is called by the user on a
* sysfs file associated with the kobjects we have registered. We need to
* transpose back from a "default" kobject to our custom struct fu_obj and
* then call the show function for that specific object.
*/
static ssize_t fu_attr_show(struct kobject *kobj,
struct attribute *attr,
char *buf)
{
struct fu_attribute *attribute;
struct fu_obj *fu;
attribute = to_fu_attr(attr);
fu = to_fu_obj(kobj);
if (!attribute->show)
return -EIO;
return attribute->show(fu, attribute, buf);
}
/*
* Just like the default show function above, but this one is for when the
* sysfs "store" is requested (when a value is written to a file.)
*/
static ssize_t fu_attr_store(struct kobject *kobj,
struct attribute *attr,
const char *buf, size_t len)
{
struct fu_attribute *attribute;
struct fu_obj *fu;
attribute = to_fu_attr(attr);
fu = to_fu_obj(kobj);
if (!attribute->store)
return -EIO;
return attribute->store(fu, attribute, buf, len);
}
/* Our custom sysfs_ops that we will associate with our ktype later on */
static const struct sysfs_ops fu_sysfs_ops = {
.show = fu_attr_show,
.store = fu_attr_store,
};
/*
* The release function for our object. This is REQUIRED by the kernel to
* have. We free the memory held in our object here.
*
* NEVER try to get away with just a "blank" release function to try to be
* smarter than the kernel. Turns out, no one ever is...
*/
static void fu_release(struct kobject *kobj)
{
struct fu_obj *fu;
fu = to_fu_obj(kobj);
kfree(fu);
}
/*
* The "para" file where the .par variable is read from and written to.
*/
static ssize_t para_show(struct fu_obj *fu_obj, struct fu_attribute *attr,
char *buf)
{
return sprintf(buf, "%d\n", fu_obj->par);
}
static ssize_t para_store(struct fu_obj *fu_obj, struct fu_attribute *attr,
const char *buf, size_t count)
{
int ret;
ret = kstrtoint(buf, 10, &fu_obj->par);
if (ret < 0)
return ret;
return count;
}
static struct fu_attribute para_attribute =
__ATTR(para, 0664, para_show, para_store);
/*
* Create a group of attributes so that we can create and destroy them all
* at once.
*/
static struct attribute *fu_default_attrs[] = {
¶_attribute.attr,
NULL, /* need to NULL terminate the list of attributes */
};
/*
* Our own ktype for our kobjects. Here we specify our sysfs ops, the
* release function, and the set of default attributes we want created
* whenever a kobject of this type is registered with the kernel.
*/
static struct kobj_type fu_ktype = {
.sysfs_ops = &fu_sysfs_ops,
.release = fu_release,
.default_attrs = fu_default_attrs,
};
static struct kset *ks_a;
static struct fu_obj *fu_a;
static struct fu_obj *create_fu_obj(const char *name)
{
struct fu_obj *fu;
int retval;
/* allocate the memory for the whole object */
fu = kzalloc(sizeof(*fu), GFP_KERNEL);
if (!fu)
return NULL;
/*
* As we have a kset for this kobject, we need to set it before calling
* the kobject core.
*/
fu->kobj.kset = ks_a;
/*
* Initialize and add the kobject to the kernel. All the default files
* will be created here. As we have already specified a kset for this
* kobject, we don't have to set a parent for the kobject, the kobject
* will be placed beneath that kset automatically.
*/
retval = kobject_init_and_add(&fu->kobj, &fu_ktype, NULL, "%s", name);
if (retval) {
kobject_put(&fu->kobj);
return NULL;
}
return fu;
}
static void destroy_fu_obj(struct fu_obj *fu)
{
kobject_put(&fu->kobj);
}
static int __init example_init(void)
{
/*
* Create a kset with the name of "ks_a",
* located under /sys/kernel/
*/
ks_a = kset_create_and_add("ks_a", NULL, kernel_kobj);
if (!ks_a)
return -ENOMEM;
/*
* Create fu object and register it with our kset
*/
fu_a = create_fu_obj("fu_a");
if (!fu_a)
return -EINVAL;
else
return 0;
}
static void __exit example_exit(void)
{
destroy_fu_obj(fu_a);
kset_unregister(ks_a);
}
module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");