serial: 8250_dw: Do not use readl/writel before checking port iotype
[linux/fpc-iii.git] / samples / kobject / kset-example.c
bloba55bff52bde3f51ff34b25c5a4af46cca4607a28
1 /*
2 * Sample kset and ktype implementation
4 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2007 Novell Inc.
7 * Released under the GPL version 2 only.
9 */
10 #include <linux/kobject.h>
11 #include <linux/string.h>
12 #include <linux/sysfs.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
18 * This module shows how to create a kset in sysfs called
19 * /sys/kernel/kset-example
20 * Then tree kobjects are created and assigned to this kset, "foo", "baz",
21 * and "bar". In those kobjects, attributes of the same name are also
22 * created and if an integer is written to these files, it can be later
23 * read out of it.
28 * This is our "object" that we will create a few of and register them with
29 * sysfs.
31 struct foo_obj {
32 struct kobject kobj;
33 int foo;
34 int baz;
35 int bar;
37 #define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
39 /* a custom attribute that works just for a struct foo_obj. */
40 struct foo_attribute {
41 struct attribute attr;
42 ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf);
43 ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count);
45 #define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
48 * The default show function that must be passed to sysfs. This will be
49 * called by sysfs for whenever a show function is called by the user on a
50 * sysfs file associated with the kobjects we have registered. We need to
51 * transpose back from a "default" kobject to our custom struct foo_obj and
52 * then call the show function for that specific object.
54 static ssize_t foo_attr_show(struct kobject *kobj,
55 struct attribute *attr,
56 char *buf)
58 struct foo_attribute *attribute;
59 struct foo_obj *foo;
61 attribute = to_foo_attr(attr);
62 foo = to_foo_obj(kobj);
64 if (!attribute->show)
65 return -EIO;
67 return attribute->show(foo, attribute, buf);
71 * Just like the default show function above, but this one is for when the
72 * sysfs "store" is requested (when a value is written to a file.)
74 static ssize_t foo_attr_store(struct kobject *kobj,
75 struct attribute *attr,
76 const char *buf, size_t len)
78 struct foo_attribute *attribute;
79 struct foo_obj *foo;
81 attribute = to_foo_attr(attr);
82 foo = to_foo_obj(kobj);
84 if (!attribute->store)
85 return -EIO;
87 return attribute->store(foo, attribute, buf, len);
90 /* Our custom sysfs_ops that we will associate with our ktype later on */
91 static const struct sysfs_ops foo_sysfs_ops = {
92 .show = foo_attr_show,
93 .store = foo_attr_store,
97 * The release function for our object. This is REQUIRED by the kernel to
98 * have. We free the memory held in our object here.
100 * NEVER try to get away with just a "blank" release function to try to be
101 * smarter than the kernel. Turns out, no one ever is...
103 static void foo_release(struct kobject *kobj)
105 struct foo_obj *foo;
107 foo = to_foo_obj(kobj);
108 kfree(foo);
112 * The "foo" file where the .foo variable is read from and written to.
114 static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
115 char *buf)
117 return sprintf(buf, "%d\n", foo_obj->foo);
120 static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
121 const char *buf, size_t count)
123 int ret;
125 ret = kstrtoint(buf, 10, &foo_obj->foo);
126 if (ret < 0)
127 return ret;
129 return count;
132 /* Sysfs attributes cannot be world-writable. */
133 static struct foo_attribute foo_attribute =
134 __ATTR(foo, 0664, foo_show, foo_store);
137 * More complex function where we determine which variable is being accessed by
138 * looking at the attribute for the "baz" and "bar" files.
140 static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
141 char *buf)
143 int var;
145 if (strcmp(attr->attr.name, "baz") == 0)
146 var = foo_obj->baz;
147 else
148 var = foo_obj->bar;
149 return sprintf(buf, "%d\n", var);
152 static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
153 const char *buf, size_t count)
155 int var, ret;
157 ret = kstrtoint(buf, 10, &var);
158 if (ret < 0)
159 return ret;
161 if (strcmp(attr->attr.name, "baz") == 0)
162 foo_obj->baz = var;
163 else
164 foo_obj->bar = var;
165 return count;
168 static struct foo_attribute baz_attribute =
169 __ATTR(baz, 0664, b_show, b_store);
170 static struct foo_attribute bar_attribute =
171 __ATTR(bar, 0664, b_show, b_store);
174 * Create a group of attributes so that we can create and destroy them all
175 * at once.
177 static struct attribute *foo_default_attrs[] = {
178 &foo_attribute.attr,
179 &baz_attribute.attr,
180 &bar_attribute.attr,
181 NULL, /* need to NULL terminate the list of attributes */
185 * Our own ktype for our kobjects. Here we specify our sysfs ops, the
186 * release function, and the set of default attributes we want created
187 * whenever a kobject of this type is registered with the kernel.
189 static struct kobj_type foo_ktype = {
190 .sysfs_ops = &foo_sysfs_ops,
191 .release = foo_release,
192 .default_attrs = foo_default_attrs,
195 static struct kset *example_kset;
196 static struct foo_obj *foo_obj;
197 static struct foo_obj *bar_obj;
198 static struct foo_obj *baz_obj;
200 static struct foo_obj *create_foo_obj(const char *name)
202 struct foo_obj *foo;
203 int retval;
205 /* allocate the memory for the whole object */
206 foo = kzalloc(sizeof(*foo), GFP_KERNEL);
207 if (!foo)
208 return NULL;
211 * As we have a kset for this kobject, we need to set it before calling
212 * the kobject core.
214 foo->kobj.kset = example_kset;
217 * Initialize and add the kobject to the kernel. All the default files
218 * will be created here. As we have already specified a kset for this
219 * kobject, we don't have to set a parent for the kobject, the kobject
220 * will be placed beneath that kset automatically.
222 retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
223 if (retval) {
224 kobject_put(&foo->kobj);
225 return NULL;
229 * We are always responsible for sending the uevent that the kobject
230 * was added to the system.
232 kobject_uevent(&foo->kobj, KOBJ_ADD);
234 return foo;
237 static void destroy_foo_obj(struct foo_obj *foo)
239 kobject_put(&foo->kobj);
242 static int __init example_init(void)
245 * Create a kset with the name of "kset_example",
246 * located under /sys/kernel/
248 example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
249 if (!example_kset)
250 return -ENOMEM;
253 * Create three objects and register them with our kset
255 foo_obj = create_foo_obj("foo");
256 if (!foo_obj)
257 goto foo_error;
259 bar_obj = create_foo_obj("bar");
260 if (!bar_obj)
261 goto bar_error;
263 baz_obj = create_foo_obj("baz");
264 if (!baz_obj)
265 goto baz_error;
267 return 0;
269 baz_error:
270 destroy_foo_obj(bar_obj);
271 bar_error:
272 destroy_foo_obj(foo_obj);
273 foo_error:
274 kset_unregister(example_kset);
275 return -EINVAL;
278 static void __exit example_exit(void)
280 destroy_foo_obj(baz_obj);
281 destroy_foo_obj(bar_obj);
282 destroy_foo_obj(foo_obj);
283 kset_unregister(example_kset);
286 module_init(example_init);
287 module_exit(example_exit);
288 MODULE_LICENSE("GPL v2");
289 MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");