1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef _KOBJECT_H_
16#define _KOBJECT_H_
17
18#ifdef __KERNEL__
19
20#include <linux/types.h>
21#include <linux/list.h>
22#include <linux/sysfs.h>
23#include <linux/rwsem.h>
24#include <linux/kref.h>
25#include <linux/kobject_uevent.h>
26#include <asm/atomic.h>
27
28#define KOBJ_NAME_LEN 20
29
30
31extern u64 hotplug_seqnum;
32
33struct kobject {
34 char * k_name;
35 char name[KOBJ_NAME_LEN];
36 struct kref kref;
37 struct list_head entry;
38 struct kobject * parent;
39 struct kset * kset;
40 struct kobj_type * ktype;
41 struct dentry * dentry;
42};
43
44extern int kobject_set_name(struct kobject *, const char *, ...)
45 __attribute__((format(printf,2,3)));
46
47static inline char * kobject_name(struct kobject * kobj)
48{
49 return kobj->k_name;
50}
51
52extern void kobject_init(struct kobject *);
53extern void kobject_cleanup(struct kobject *);
54
55extern int kobject_add(struct kobject *);
56extern void kobject_del(struct kobject *);
57
58extern int kobject_rename(struct kobject *, char *new_name);
59
60extern int kobject_register(struct kobject *);
61extern void kobject_unregister(struct kobject *);
62
63extern struct kobject * kobject_get(struct kobject *);
64extern void kobject_put(struct kobject *);
65
66extern char * kobject_get_path(struct kobject *, int);
67
68struct kobj_type {
69 void (*release)(struct kobject *);
70 struct sysfs_ops * sysfs_ops;
71 struct attribute ** default_attrs;
72};
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93struct kset_hotplug_ops {
94 int (*filter)(struct kset *kset, struct kobject *kobj);
95 char *(*name)(struct kset *kset, struct kobject *kobj);
96 int (*hotplug)(struct kset *kset, struct kobject *kobj, char **envp,
97 int num_envp, char *buffer, int buffer_size);
98};
99
100struct kset {
101 struct subsystem * subsys;
102 struct kobj_type * ktype;
103 struct list_head list;
104 struct kobject kobj;
105 struct kset_hotplug_ops * hotplug_ops;
106};
107
108
109extern void kset_init(struct kset * k);
110extern int kset_add(struct kset * k);
111extern int kset_register(struct kset * k);
112extern void kset_unregister(struct kset * k);
113
114static inline struct kset * to_kset(struct kobject * kobj)
115{
116 return kobj ? container_of(kobj,struct kset,kobj) : NULL;
117}
118
119static inline struct kset * kset_get(struct kset * k)
120{
121 return k ? to_kset(kobject_get(&k->kobj)) : NULL;
122}
123
124static inline void kset_put(struct kset * k)
125{
126 kobject_put(&k->kobj);
127}
128
129static inline struct kobj_type * get_ktype(struct kobject * k)
130{
131 if (k->kset && k->kset->ktype)
132 return k->kset->ktype;
133 else
134 return k->ktype;
135}
136
137extern struct kobject * kset_find_obj(struct kset *, const char *);
138
139
140
141
142
143
144#define set_kset_name(str) .kset = { .kobj = { .name = str } }
145
146
147
148struct subsystem {
149 struct kset kset;
150 struct rw_semaphore rwsem;
151};
152
153#define decl_subsys(_name,_type,_hotplug_ops) \
154struct subsystem _name##_subsys = { \
155 .kset = { \
156 .kobj = { .name = __stringify(_name) }, \
157 .ktype = _type, \
158 .hotplug_ops =_hotplug_ops, \
159 } \
160}
161#define decl_subsys_name(_varname,_name,_type,_hotplug_ops) \
162struct subsystem _varname##_subsys = { \
163 .kset = { \
164 .kobj = { .name = __stringify(_name) }, \
165 .ktype = _type, \
166 .hotplug_ops =_hotplug_ops, \
167 } \
168}
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186#define kobj_set_kset_s(obj,subsys) \
187 (obj)->kobj.kset = &(subsys).kset
188
189
190
191
192
193
194
195
196
197
198
199
200#define kset_set_kset_s(obj,subsys) \
201 (obj)->kset.kobj.kset = &(subsys).kset
202
203
204
205
206
207
208
209
210
211
212
213#define subsys_set_kset(obj,_subsys) \
214 (obj)->subsys.kset.kobj.kset = &(_subsys).kset
215
216extern void subsystem_init(struct subsystem *);
217extern int subsystem_register(struct subsystem *);
218extern void subsystem_unregister(struct subsystem *);
219
220static inline struct subsystem * subsys_get(struct subsystem * s)
221{
222 return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
223}
224
225static inline void subsys_put(struct subsystem * s)
226{
227 kset_put(&s->kset);
228}
229
230struct subsys_attribute {
231 struct attribute attr;
232 ssize_t (*show)(struct subsystem *, char *);
233 ssize_t (*store)(struct subsystem *, const char *, size_t);
234};
235
236extern int subsys_create_file(struct subsystem * , struct subsys_attribute *);
237extern void subsys_remove_file(struct subsystem * , struct subsys_attribute *);
238
239#ifdef CONFIG_HOTPLUG
240void kobject_hotplug(struct kobject *kobj, enum kobject_action action);
241int add_hotplug_env_var(char **envp, int num_envp, int *cur_index,
242 char *buffer, int buffer_size, int *cur_len,
243 const char *format, ...)
244 __attribute__((format (printf, 7, 8)));
245#else
246static inline void kobject_hotplug(struct kobject *kobj, enum kobject_action action) { }
247static inline int add_hotplug_env_var(char **envp, int num_envp, int *cur_index,
248 char *buffer, int buffer_size, int *cur_len,
249 const char *format, ...)
250{ return 0; }
251#endif
252
253#endif
254#endif
255