Blame view

samples/kobject/kobject-example.c 3.37 KB
d9d16e16a   Greg Kroah-Hartman   kobject: add SPDX...
1
  // SPDX-License-Identifier: GPL-2.0
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
2
3
4
5
6
  /*
   * Sample kobject implementation
   *
   * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
   * Copyright (C) 2007 Novell Inc.
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
   */
  #include <linux/kobject.h>
  #include <linux/string.h>
  #include <linux/sysfs.h>
  #include <linux/module.h>
  #include <linux/init.h>
  
  /*
   * 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.
   */
  
  static int foo;
  static int baz;
  static int 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 sprintf(buf, "%d
  ", foo);
  }
  
  static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
  			 const char *buf, size_t count)
  {
5fd637e7a   Rastislav Barlik   samples/kobject: ...
38
39
40
41
42
  	int ret;
  
  	ret = kstrtoint(buf, 10, &foo);
  	if (ret < 0)
  		return ret;
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
43
44
  	return count;
  }
de5109898   Rusty Russell   samples/kobject/:...
45
  /* Sysfs attributes cannot be world-writable. */
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
46
  static struct kobj_attribute foo_attribute =
de5109898   Rusty Russell   samples/kobject/:...
47
  	__ATTR(foo, 0664, foo_show, foo_store);
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
48
49
  
  /*
a115bc070   Radu Voicilas   kobject-example: ...
50
   * More complex function where we determine which variable is being accessed by
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
   * 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 sprintf(buf, "%d
  ", var);
  }
  
  static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
  		       const char *buf, size_t count)
  {
5fd637e7a   Rastislav Barlik   samples/kobject: ...
69
70
71
72
73
  	int var, ret;
  
  	ret = kstrtoint(buf, 10, &var);
  	if (ret < 0)
  		return ret;
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
74

40efcb05f   Greg Kroah-Hartman   kobject: add samp...
75
76
77
78
79
80
81
82
  	if (strcmp(attr->attr.name, "baz") == 0)
  		baz = var;
  	else
  		bar = var;
  	return count;
  }
  
  static struct kobj_attribute baz_attribute =
de5109898   Rusty Russell   samples/kobject/:...
83
  	__ATTR(baz, 0664, b_show, b_store);
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
84
  static struct kobj_attribute bar_attribute =
de5109898   Rusty Russell   samples/kobject/:...
85
  	__ATTR(bar, 0664, b_show, b_store);
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
86
87
88
  
  
  /*
a115bc070   Radu Voicilas   kobject-example: ...
89
   * Create a group of attributes so that we can create and destroy them all
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
   * at once.
   */
  static struct 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.
   */
  static struct attribute_group attr_group = {
  	.attrs = attrs,
  };
  
  static struct kobject *example_kobj;
7ec7fb394   Qinghuang Feng   samples: mark {st...
110
  static int __init example_init(void)
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  {
  	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);
  
  	return retval;
  }
7ec7fb394   Qinghuang Feng   samples: mark {st...
134
  static void __exit example_exit(void)
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
135
136
137
138
139
140
  {
  	kobject_put(example_kobj);
  }
  
  module_init(example_init);
  module_exit(example_exit);
07afb6ace   Greg Kroah-Hartman   samples/kobject: ...
141
  MODULE_LICENSE("GPL v2");
40efcb05f   Greg Kroah-Hartman   kobject: add samp...
142
  MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");