patch-2_6_7-vs1_9_1_12
[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 #if defined(__KERNEL__) && !defined(_KOBJECT_H_)
16 #define _KOBJECT_H_
17
18 #include <linux/types.h>
19 #include <linux/list.h>
20 #include <linux/sysfs.h>
21 #include <linux/rwsem.h>
22 #include <asm/atomic.h>
23
24 #define KOBJ_NAME_LEN   20
25
26 struct kobject {
27         char                    * k_name;
28         char                    name[KOBJ_NAME_LEN];
29         atomic_t                refcount;
30         struct list_head        entry;
31         struct kobject          * parent;
32         struct kset             * kset;
33         struct kobj_type        * ktype;
34         struct dentry           * dentry;
35 };
36
37 extern int kobject_set_name(struct kobject *, const char *, ...)
38         __attribute__((format(printf,2,3)));
39
40 static inline char * kobject_name(struct kobject * kobj)
41 {
42         return kobj->k_name;
43 }
44
45 extern void kobject_init(struct kobject *);
46 extern void kobject_cleanup(struct kobject *);
47
48 extern int kobject_add(struct kobject *);
49 extern void kobject_del(struct kobject *);
50
51 extern int kobject_rename(struct kobject *, char *new_name);
52
53 extern int kobject_register(struct kobject *);
54 extern void kobject_unregister(struct kobject *);
55
56 extern struct kobject * kobject_get(struct kobject *);
57 extern void kobject_put(struct kobject *);
58
59 extern void kobject_hotplug(const char *action, struct kobject *);
60
61 struct kobj_type {
62         void (*release)(struct kobject *);
63         struct sysfs_ops        * sysfs_ops;
64         struct attribute        ** default_attrs;
65 };
66
67
68 /**
69  *      kset - a set of kobjects of a specific type, belonging
70  *      to a specific subsystem.
71  *
72  *      All kobjects of a kset should be embedded in an identical 
73  *      type. This type may have a descriptor, which the kset points
74  *      to. This allows there to exist sets of objects of the same
75  *      type in different subsystems.
76  *
77  *      A subsystem does not have to be a list of only one type 
78  *      of object; multiple ksets can belong to one subsystem. All 
79  *      ksets of a subsystem share the subsystem's lock.
80  *
81  *      Each kset can support hotplugging; if it does, it will be given
82  *      the opportunity to filter out specific kobjects from being
83  *      reported, as well as to add its own "data" elements to the
84  *      environment being passed to the hotplug helper.
85  */
86 struct kset_hotplug_ops {
87         int (*filter)(struct kset *kset, struct kobject *kobj);
88         char *(*name)(struct kset *kset, struct kobject *kobj);
89         int (*hotplug)(struct kset *kset, struct kobject *kobj, char **envp,
90                         int num_envp, char *buffer, int buffer_size);
91 };
92
93 struct kset {
94         struct subsystem        * subsys;
95         struct kobj_type        * ktype;
96         struct list_head        list;
97         struct kobject          kobj;
98         struct kset_hotplug_ops * hotplug_ops;
99 };
100
101
102 extern void kset_init(struct kset * k);
103 extern int kset_add(struct kset * k);
104 extern int kset_register(struct kset * k);
105 extern void kset_unregister(struct kset * k);
106
107 static inline struct kset * to_kset(struct kobject * kobj)
108 {
109         return kobj ? container_of(kobj,struct kset,kobj) : NULL;
110 }
111
112 static inline struct kset * kset_get(struct kset * k)
113 {
114         return k ? to_kset(kobject_get(&k->kobj)) : NULL;
115 }
116
117 static inline void kset_put(struct kset * k)
118 {
119         kobject_put(&k->kobj);
120 }
121
122 static inline struct kobj_type * get_ktype(struct kobject * k)
123 {
124         if (k->kset && k->kset->ktype)
125                 return k->kset->ktype;
126         else 
127                 return k->ktype;
128 }
129
130 extern struct kobject * kset_find_obj(struct kset *, const char *);
131
132
133 /**
134  * Use this when initializing an embedded kset with no other 
135  * fields to initialize.
136  */
137 #define set_kset_name(str)      .kset = { .kobj = { .name = str } }
138
139
140
141 struct subsystem {
142         struct kset             kset;
143         struct rw_semaphore     rwsem;
144 };
145
146 #define decl_subsys(_name,_type,_hotplug_ops) \
147 struct subsystem _name##_subsys = { \
148         .kset = { \
149                 .kobj = { .name = __stringify(_name) }, \
150                 .ktype = _type, \
151                 .hotplug_ops =_hotplug_ops, \
152         } \
153 }
154 #define decl_subsys_name(_varname,_name,_type,_hotplug_ops) \
155 struct subsystem _varname##_subsys = { \
156         .kset = { \
157                 .kobj = { .name = __stringify(_name) }, \
158                 .ktype = _type, \
159                 .hotplug_ops =_hotplug_ops, \
160         } \
161 }
162
163
164 /**
165  * Helpers for setting the kset of registered objects.
166  * Often, a registered object belongs to a kset embedded in a 
167  * subsystem. These do no magic, just make the resulting code
168  * easier to follow. 
169  */
170
171 /**
172  *      kobj_set_kset_s(obj,subsys) - set kset for embedded kobject.
173  *      @obj:           ptr to some object type.
174  *      @subsys:        a subsystem object (not a ptr).
175  *
176  *      Can be used for any object type with an embedded ->kobj.
177  */
178
179 #define kobj_set_kset_s(obj,subsys) \
180         (obj)->kobj.kset = &(subsys).kset
181
182 /**
183  *      kset_set_kset_s(obj,subsys) - set kset for embedded kset.
184  *      @obj:           ptr to some object type.
185  *      @subsys:        a subsystem object (not a ptr).
186  *
187  *      Can be used for any object type with an embedded ->kset.
188  *      Sets the kset of @obj's  embedded kobject (via its embedded
189  *      kset) to @subsys.kset. This makes @obj a member of that 
190  *      kset.
191  */
192
193 #define kset_set_kset_s(obj,subsys) \
194         (obj)->kset.kobj.kset = &(subsys).kset
195
196 /**
197  *      subsys_set_kset(obj,subsys) - set kset for subsystem
198  *      @obj:           ptr to some object type.
199  *      @subsys:        a subsystem object (not a ptr).
200  *
201  *      Can be used for any object type with an embedded ->subsys.
202  *      Sets the kset of @obj's kobject to @subsys.kset. This makes
203  *      the object a member of that kset.
204  */
205
206 #define subsys_set_kset(obj,_subsys) \
207         (obj)->subsys.kset.kobj.kset = &(_subsys).kset
208
209 extern void subsystem_init(struct subsystem *);
210 extern int subsystem_register(struct subsystem *);
211 extern void subsystem_unregister(struct subsystem *);
212
213 static inline struct subsystem * subsys_get(struct subsystem * s)
214 {
215         return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
216 }
217
218 static inline void subsys_put(struct subsystem * s)
219 {
220         kset_put(&s->kset);
221 }
222
223 struct subsys_attribute {
224         struct attribute attr;
225         ssize_t (*show)(struct subsystem *, char *);
226         ssize_t (*store)(struct subsystem *, const char *, size_t); 
227 };
228
229 extern int subsys_create_file(struct subsystem * , struct subsys_attribute *);
230 extern void subsys_remove_file(struct subsystem * , struct subsys_attribute *);
231
232
233 #endif /* _KOBJECT_H_ */