/* * This module shows how to create a simple subdirectory in sysfs called * /sys/kernel/kobject_example In that directory, 3 files are created: * "foo", "baz", and "bar". If an integer is written to these files, it can be * later read out of it.
*/
staticint foo; staticint baz; staticint bar;
/* * The "foo" file where a static variable is read from and written to.
*/ static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{ return sysfs_emit(buf, "%d\n", foo);
}
/* * More complex function where we determine which variable is being accessed by * looking at the attribute for the "baz" and "bar" files.
*/ static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{ int var;
if (strcmp(attr->attr.name, "baz") == 0)
var = baz; else
var = bar; return sysfs_emit(buf, "%d\n", var);
}
static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, constchar *buf, size_t count)
{ int var, ret;
ret = kstrtoint(buf, 10, &var); if (ret < 0) return ret;
if (strcmp(attr->attr.name, "baz") == 0)
baz = var; else
bar = var; return count;
}
/* * Create a group of attributes so that we can create and destroy them all * at once.
*/ staticstruct attribute *attrs[] = {
&foo_attribute.attr,
&baz_attribute.attr,
&bar_attribute.attr,
NULL, /* need to NULL terminate the list of attributes */
};
/* * An unnamed attribute group will put all of the attributes directly in * the kobject directory. If we specify a name, a subdirectory will be * created for the attributes with the directory being the name of the * attribute group.
*/ staticconststruct attribute_group attr_group = {
.attrs = attrs,
};
staticstruct kobject *example_kobj;
staticint __init example_init(void)
{ int retval;
/* * Create a simple kobject with the name of "kobject_example", * located under /sys/kernel/ * * As this is a simple directory, no uevent will be sent to * userspace. That is why this function should not be used for * any type of dynamic kobjects, where the name and number are * not known ahead of time.
*/
example_kobj = kobject_create_and_add("kobject_example", kernel_kobj); if (!example_kobj) return -ENOMEM;
/* Create the files associated with this kobject */
retval = sysfs_create_group(example_kobj, &attr_group); if (retval)
kobject_put(example_kobj);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.