Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Sample kset and ktype implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2007 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * This module shows how to create a kset in sysfs called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * /sys/kernel/kset-example
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * Then tree kobjects are created and assigned to this kset, "foo", "baz",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * and "bar".  In those kobjects, attributes of the same name are also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * created and if an integer is written to these files, it can be later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * read out of it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * This is our "object" that we will create a few of and register them with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * sysfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) struct foo_obj {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct kobject kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	int foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	int baz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	int bar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) /* a custom attribute that works just for a struct foo_obj. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) struct foo_attribute {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct attribute attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * The default show function that must be passed to sysfs.  This will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * called by sysfs for whenever a show function is called by the user on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * sysfs file associated with the kobjects we have registered.  We need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * transpose back from a "default" kobject to our custom struct foo_obj and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * then call the show function for that specific object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static ssize_t foo_attr_show(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 			     struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct foo_attribute *attribute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct foo_obj *foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	attribute = to_foo_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	foo = to_foo_obj(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (!attribute->show)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return attribute->show(foo, attribute, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * Just like the default show function above, but this one is for when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * sysfs "store" is requested (when a value is written to a file.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static ssize_t foo_attr_store(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			      struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			      const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct foo_attribute *attribute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct foo_obj *foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	attribute = to_foo_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	foo = to_foo_obj(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (!attribute->store)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	return attribute->store(foo, attribute, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) /* Our custom sysfs_ops that we will associate with our ktype later on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static const struct sysfs_ops foo_sysfs_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	.show = foo_attr_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	.store = foo_attr_store,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * The release function for our object.  This is REQUIRED by the kernel to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * have.  We free the memory held in our object here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * NEVER try to get away with just a "blank" release function to try to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * smarter than the kernel.  Turns out, no one ever is...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void foo_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct foo_obj *foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	foo = to_foo_obj(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	kfree(foo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * The "foo" file where the .foo variable is read from and written to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return sprintf(buf, "%d\n", foo_obj->foo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	ret = kstrtoint(buf, 10, &foo_obj->foo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* Sysfs attributes cannot be world-writable. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static struct foo_attribute foo_attribute =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	__ATTR(foo, 0664, foo_show, foo_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  * More complex function where we determine which variable is being accessed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  * looking at the attribute for the "baz" and "bar" files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		      char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	int var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (strcmp(attr->attr.name, "baz") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		var = foo_obj->baz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		var = foo_obj->bar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return sprintf(buf, "%d\n", var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		       const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	int var, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	ret = kstrtoint(buf, 10, &var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (strcmp(attr->attr.name, "baz") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		foo_obj->baz = var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		foo_obj->bar = var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static struct foo_attribute baz_attribute =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	__ATTR(baz, 0664, b_show, b_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static struct foo_attribute bar_attribute =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	__ATTR(bar, 0664, b_show, b_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  * Create a group of attributes so that we can create and destroy them all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  * at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static struct attribute *foo_default_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	&foo_attribute.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	&baz_attribute.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	&bar_attribute.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	NULL,	/* need to NULL terminate the list of attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) ATTRIBUTE_GROUPS(foo_default);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * Our own ktype for our kobjects.  Here we specify our sysfs ops, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * release function, and the set of default attributes we want created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  * whenever a kobject of this type is registered with the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static struct kobj_type foo_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	.sysfs_ops = &foo_sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	.release = foo_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	.default_groups = foo_default_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static struct kset *example_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static struct foo_obj *foo_obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static struct foo_obj *bar_obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static struct foo_obj *baz_obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static struct foo_obj *create_foo_obj(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct foo_obj *foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	/* allocate the memory for the whole object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	foo = kzalloc(sizeof(*foo), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (!foo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * As we have a kset for this kobject, we need to set it before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * the kobject core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	foo->kobj.kset = example_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	 * Initialize and add the kobject to the kernel.  All the default files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	 * will be created here.  As we have already specified a kset for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	 * kobject, we don't have to set a parent for the kobject, the kobject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	 * will be placed beneath that kset automatically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		kobject_put(&foo->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	 * We are always responsible for sending the uevent that the kobject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	 * was added to the system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	kobject_uevent(&foo->kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	return foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static void destroy_foo_obj(struct foo_obj *foo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	kobject_put(&foo->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static int __init example_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	 * Create a kset with the name of "kset_example",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	 * located under /sys/kernel/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (!example_kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	 * Create three objects and register them with our kset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	foo_obj = create_foo_obj("foo");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (!foo_obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		goto foo_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	bar_obj = create_foo_obj("bar");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (!bar_obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		goto bar_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	baz_obj = create_foo_obj("baz");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (!baz_obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		goto baz_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) baz_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	destroy_foo_obj(bar_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) bar_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	destroy_foo_obj(foo_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) foo_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	kset_unregister(example_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static void __exit example_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	destroy_foo_obj(baz_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	destroy_foo_obj(bar_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	destroy_foo_obj(foo_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	kset_unregister(example_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) module_init(example_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) module_exit(example_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");