upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / include / linux / kobject.h
1 /*
2  * kobject.h - generic kernel object infrastructure.
3  *
4  * Copyright (c) 2002-2003      Patrick Mochel
5  * Copyright (c) 2002-2003      Open Source Development Labs
6  *
7  * This file is released under the GPLv2.
8  *
9  * 
10  * Please read Documentation/kobject.txt before using the kobject
11  * interface, ESPECIALLY the parts about reference counts and object
12  * destructors. 
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 /* counter to tag the hotplug event, read only except for the kobject core */
31 extern u64 hotplug_seqnum;
32
33 struct 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
44 extern int kobject_set_name(struct kobject *, const char *, ...)
45         __attribute__((format(printf,2,3)));
46
47 static inline char * kobject_name(struct kobject * kobj)
48 {
49         return kobj->k_name;
50 }
51
52 extern void kobject_init(struct kobject *);
53 extern void kobject_cleanup(struct kobject *);
54
55 extern int kobject_add(struct kobject *);
56 extern void kobject_del(struct kobject *);
57
58 extern int kobject_rename(struct kobject *, char *new_name);
59
60 extern int kobject_register(struct kobject *);
61 extern void kobject_unregister(struct kobject *);
62
63 extern struct kobject * kobject_get(struct kobject *);
64 extern void kobject_put(struct kobject *);
65
66 extern char * kobject_get_path(struct kobject *, int);
67
68 struct kobj_type {
69         void (*release)(struct kobject *);
70         struct sysfs_ops        * sysfs_ops;
71         struct attribute        ** default_attrs;
72 };
73
74
75 /**
76  *      kset - a set of kobjects of a specific type, belonging
77  *      to a specific subsystem.
78  *
79  *      All kobjects of a kset should be embedded in an identical 
80  *      type. This type may have a descriptor, which the kset points
81  *      to. This allows there to exist sets of objects of the same
82  *      type in different subsystems.
83  *
84  *      A subsystem does not have to be a list of only one type 
85  *      of object; multiple ksets can belong to one subsystem. All 
86  *      ksets of a subsystem share the subsystem's lock.
87  *
88  *      Each kset can support hotplugging; if it does, it will be given
89  *      the opportunity to filter out specific kobjects from being
90  *      reported, as well as to add its own "data" elements to the
91  *      environment being passed to the hotplug helper.
92  */
93 struct 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
100 struct 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
109 extern void kset_init(struct kset * k);
110 extern int kset_add(struct kset * k);
111 extern int kset_register(struct kset * k);
112 extern void kset_unregister(struct kset * k);
113
114 static inline struct kset * to_kset(struct kobject * kobj)
115 {
116         return kobj ? container_of(kobj,struct kset,kobj) : NULL;
117 }
118
119 static inline struct kset * kset_get(struct kset * k)
120 {
121         return k ? to_kset(kobject_get(&k->kobj)) : NULL;
122 }
123
124 static inline void kset_put(struct kset * k)
125 {
126         kobject_put(&k->kobj);
127 }
128
129 static 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
137 extern struct kobject * kset_find_obj(struct kset *, const char *);
138
139
140 /**
141  * Use this when initializing an embedded kset with no other 
142  * fields to initialize.
143  */
144 #define set_kset_name(str)      .kset = { .kobj = { .name = str } }
145
146
147
148 struct subsystem {
149         struct kset             kset;
150         struct rw_semaphore     rwsem;
151 };
152
153 #define decl_subsys(_name,_type,_hotplug_ops) \
154 struct 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) \
162 struct subsystem _varname##_subsys = { \
163         .kset = { \
164                 .kobj = { .name = __stringify(_name) }, \
165                 .ktype = _type, \
166                 .hotplug_ops =_hotplug_ops, \
167         } \
168 }
169
170
171 /**
172  * Helpers for setting the kset of registered objects.
173  * Often, a registered object belongs to a kset embedded in a 
174  * subsystem. These do no magic, just make the resulting code
175  * easier to follow. 
176  */
177
178 /**
179  *      kobj_set_kset_s(obj,subsys) - set kset for embedded kobject.
180  *      @obj:           ptr to some object type.
181  *      @subsys:        a subsystem object (not a ptr).
182  *
183  *      Can be used for any object type with an embedded ->kobj.
184  */
185
186 #define kobj_set_kset_s(obj,subsys) \
187         (obj)->kobj.kset = &(subsys).kset
188
189 /**
190  *      kset_set_kset_s(obj,subsys) - set kset for embedded kset.
191  *      @obj:           ptr to some object type.
192  *      @subsys:        a subsystem object (not a ptr).
193  *
194  *      Can be used for any object type with an embedded ->kset.
195  *      Sets the kset of @obj's  embedded kobject (via its embedded
196  *      kset) to @subsys.kset. This makes @obj a member of that 
197  *      kset.
198  */
199
200 #define kset_set_kset_s(obj,subsys) \
201         (obj)->kset.kobj.kset = &(subsys).kset
202
203 /**
204  *      subsys_set_kset(obj,subsys) - set kset for subsystem
205  *      @obj:           ptr to some object type.
206  *      @subsys:        a subsystem object (not a ptr).
207  *
208  *      Can be used for any object type with an embedded ->subsys.
209  *      Sets the kset of @obj's kobject to @subsys.kset. This makes
210  *      the object a member of that kset.
211  */
212
213 #define subsys_set_kset(obj,_subsys) \
214         (obj)->subsys.kset.kobj.kset = &(_subsys).kset
215
216 extern void subsystem_init(struct subsystem *);
217 extern int subsystem_register(struct subsystem *);
218 extern void subsystem_unregister(struct subsystem *);
219
220 static inline struct subsystem * subsys_get(struct subsystem * s)
221 {
222         return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
223 }
224
225 static inline void subsys_put(struct subsystem * s)
226 {
227         kset_put(&s->kset);
228 }
229
230 struct 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
236 extern int subsys_create_file(struct subsystem * , struct subsys_attribute *);
237 extern void subsys_remove_file(struct subsystem * , struct subsys_attribute *);
238
239 #ifdef CONFIG_HOTPLUG
240 void kobject_hotplug(struct kobject *kobj, enum kobject_action action);
241 int 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
246 static inline void kobject_hotplug(struct kobject *kobj, enum kobject_action action) { }
247 static 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 /* __KERNEL__ */
254 #endif /* _KOBJECT_H_ */