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