1

I'm attempting to learn how to write a sysfs module, and am confused at the basic initialization. In this document looking at the kobject.h file, there are several different functions related to creating a sysfs entry.

From the looks of it, the function "kobject_init_and_add" seems like the right thing to use, which takes the following:

 90 int kobject_init_and_add(struct kobject *kobj,
 91                          struct kobj_type *ktype, struct kobject *parent,
 92                          const char *fmt, ...);

struct kobject and struct kobj_type are straightforward enough, but I don't understand what the *parent kobject and *fmt cstring are supposed to be.

Further, after initializing these objects, I would need to remove them at the exit_module function, but there are two options that seem possible: kobject_del and kobject_puts. What are the differences between these?

Part of my confusion comes from the fact that while googling for the answer, I see tutorials which says to use functions like kobject_register instead, but that function doesn't actually exist.

2 Answers 2

2

Yes there are lot of example on mainline kernel which you can refers for your implementatin. For Your doubts I am adding the some example code"

Module Probe/init function

static struct kobject   *module_kobject;
module_kobject=kobject_create_and_add("module_status",NULL);
sysfs_create_group(module_kobject,&module_attr);

Module Remove/exit function

sysfs_remove_group(module_kobject,&module_attr);
kobject_put(module_kobject);

If you want to expose more than one attribute on the user space; than you need to define the group as well

static struct attribute_group module_attr={
.attrs = module_attribute,
};

There is some more implementation and function you may need like:

static ssize_t module_show_status(struct kobject *kobj,struct kobj_attribute *attr,char *buf);
static ssize_t module_store__status(struct kobject *kobj,struct kobj_attribute *attr,const char *buf,size_t len);

I think you can start your sysfs module implementation based on the above code and Feel free for any help.

Sign up to request clarification or add additional context in comments.

4 Comments

So for attributes like the &module_attr in the sysfs_create_group example, where is that &module_attr being defined? Because that seems to be where I am having trouble. I had read you could use the __ATTR macro but that is causing errors for me.
You can define like the below structure static struct attribute_group module_attr={ .attrs = module_attribute, };
Understood, my question is, does the 'module_attribute' you use there need to be defined in advance, or just declared?
You need to define the attribute of module_attribute, I am just posting the way you need to implement in your code: static ssize_t module_show_minimum_value(struct kobject *kobj,struct kobj_attribute *attr,char *buf){ // Add your code }; static struct kobj_attribute module_attribute= __ATTR(min_value,0777,module_show_minimum_value ,NULL); static struct attribute_group module_attr={ .attrs = module_attribute, };
0

There are many kernel modules that create sysfs entries. For example, http://lxr.free-electrons.com/source/net/bridge/br_sysfs_br.c This module uses kobject_create_and_add(), which gets as a parameter a kobject instance, created by sysfs_create_group(). I believe that looking into such module, and trying to code step by step, following the patterns in that module, can help. Also look in http://lxr.free-electrons.com/source/Documentation/kobject.txt

Rami Rosen

1 Comment

Thank you, I will try this. Are you aware of any such example I could look at for instantiating an attribute? The sysfs you link above has an instance where several attributes are bundled together into an attribute group, and then assigned, but I don't quite understand how to create those attributes to begin with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.