initial commit with v2.6.9
[linux-2.6.9-moxart.git] / include / linux / kobject.h
blob331d25b9cc4149ed9fbf05144c36802957647c68
1 /*
2 * kobject.h - generic kernel object infrastructure.
4 * Copyright (c) 2002-2003 Patrick Mochel
5 * Copyright (c) 2002-2003 Open Source Development Labs
7 * This file is released under the GPLv2.
9 *
10 * Please read Documentation/kobject.txt before using the kobject
11 * interface, ESPECIALLY the parts about reference counts and object
12 * destructors.
15 #ifndef _KOBJECT_H_
16 #define _KOBJECT_H_
18 #ifdef __KERNEL__
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 <asm/atomic.h>
27 #define KOBJ_NAME_LEN 20
29 struct kobject {
30 char * k_name;
31 char name[KOBJ_NAME_LEN];
32 struct kref kref;
33 struct list_head entry;
34 struct kobject * parent;
35 struct kset * kset;
36 struct kobj_type * ktype;
37 struct dentry * dentry;
40 extern int kobject_set_name(struct kobject *, const char *, ...)
41 __attribute__((format(printf,2,3)));
43 static inline char * kobject_name(struct kobject * kobj)
45 return kobj->k_name;
48 extern void kobject_init(struct kobject *);
49 extern void kobject_cleanup(struct kobject *);
51 extern int kobject_add(struct kobject *);
52 extern void kobject_del(struct kobject *);
54 extern int kobject_rename(struct kobject *, char *new_name);
56 extern int kobject_register(struct kobject *);
57 extern void kobject_unregister(struct kobject *);
59 extern struct kobject * kobject_get(struct kobject *);
60 extern void kobject_put(struct kobject *);
62 extern void kobject_hotplug(const char *action, struct kobject *);
64 extern char * kobject_get_path(struct kset *, struct kobject *, int);
66 struct kobj_type {
67 void (*release)(struct kobject *);
68 struct sysfs_ops * sysfs_ops;
69 struct attribute ** default_attrs;
73 /**
74 * kset - a set of kobjects of a specific type, belonging
75 * to a specific subsystem.
77 * All kobjects of a kset should be embedded in an identical
78 * type. This type may have a descriptor, which the kset points
79 * to. This allows there to exist sets of objects of the same
80 * type in different subsystems.
82 * A subsystem does not have to be a list of only one type
83 * of object; multiple ksets can belong to one subsystem. All
84 * ksets of a subsystem share the subsystem's lock.
86 * Each kset can support hotplugging; if it does, it will be given
87 * the opportunity to filter out specific kobjects from being
88 * reported, as well as to add its own "data" elements to the
89 * environment being passed to the hotplug helper.
91 struct kset_hotplug_ops {
92 int (*filter)(struct kset *kset, struct kobject *kobj);
93 char *(*name)(struct kset *kset, struct kobject *kobj);
94 int (*hotplug)(struct kset *kset, struct kobject *kobj, char **envp,
95 int num_envp, char *buffer, int buffer_size);
98 struct kset {
99 struct subsystem * subsys;
100 struct kobj_type * ktype;
101 struct list_head list;
102 struct kobject kobj;
103 struct kset_hotplug_ops * hotplug_ops;
107 extern void kset_init(struct kset * k);
108 extern int kset_add(struct kset * k);
109 extern int kset_register(struct kset * k);
110 extern void kset_unregister(struct kset * k);
112 static inline struct kset * to_kset(struct kobject * kobj)
114 return kobj ? container_of(kobj,struct kset,kobj) : NULL;
117 static inline struct kset * kset_get(struct kset * k)
119 return k ? to_kset(kobject_get(&k->kobj)) : NULL;
122 static inline void kset_put(struct kset * k)
124 kobject_put(&k->kobj);
127 static inline struct kobj_type * get_ktype(struct kobject * k)
129 if (k->kset && k->kset->ktype)
130 return k->kset->ktype;
131 else
132 return k->ktype;
135 extern struct kobject * kset_find_obj(struct kset *, const char *);
139 * Use this when initializing an embedded kset with no other
140 * fields to initialize.
142 #define set_kset_name(str) .kset = { .kobj = { .name = str } }
146 struct subsystem {
147 struct kset kset;
148 struct rw_semaphore rwsem;
151 #define decl_subsys(_name,_type,_hotplug_ops) \
152 struct subsystem _name##_subsys = { \
153 .kset = { \
154 .kobj = { .name = __stringify(_name) }, \
155 .ktype = _type, \
156 .hotplug_ops =_hotplug_ops, \
159 #define decl_subsys_name(_varname,_name,_type,_hotplug_ops) \
160 struct subsystem _varname##_subsys = { \
161 .kset = { \
162 .kobj = { .name = __stringify(_name) }, \
163 .ktype = _type, \
164 .hotplug_ops =_hotplug_ops, \
170 * Helpers for setting the kset of registered objects.
171 * Often, a registered object belongs to a kset embedded in a
172 * subsystem. These do no magic, just make the resulting code
173 * easier to follow.
177 * kobj_set_kset_s(obj,subsys) - set kset for embedded kobject.
178 * @obj: ptr to some object type.
179 * @subsys: a subsystem object (not a ptr).
181 * Can be used for any object type with an embedded ->kobj.
184 #define kobj_set_kset_s(obj,subsys) \
185 (obj)->kobj.kset = &(subsys).kset
188 * kset_set_kset_s(obj,subsys) - set kset for embedded kset.
189 * @obj: ptr to some object type.
190 * @subsys: a subsystem object (not a ptr).
192 * Can be used for any object type with an embedded ->kset.
193 * Sets the kset of @obj's embedded kobject (via its embedded
194 * kset) to @subsys.kset. This makes @obj a member of that
195 * kset.
198 #define kset_set_kset_s(obj,subsys) \
199 (obj)->kset.kobj.kset = &(subsys).kset
202 * subsys_set_kset(obj,subsys) - set kset for subsystem
203 * @obj: ptr to some object type.
204 * @subsys: a subsystem object (not a ptr).
206 * Can be used for any object type with an embedded ->subsys.
207 * Sets the kset of @obj's kobject to @subsys.kset. This makes
208 * the object a member of that kset.
211 #define subsys_set_kset(obj,_subsys) \
212 (obj)->subsys.kset.kobj.kset = &(_subsys).kset
214 extern void subsystem_init(struct subsystem *);
215 extern int subsystem_register(struct subsystem *);
216 extern void subsystem_unregister(struct subsystem *);
218 static inline struct subsystem * subsys_get(struct subsystem * s)
220 return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
223 static inline void subsys_put(struct subsystem * s)
225 kset_put(&s->kset);
228 struct subsys_attribute {
229 struct attribute attr;
230 ssize_t (*show)(struct subsystem *, char *);
231 ssize_t (*store)(struct subsystem *, const char *, size_t);
234 extern int subsys_create_file(struct subsystem * , struct subsys_attribute *);
235 extern void subsys_remove_file(struct subsystem * , struct subsys_attribute *);
237 #endif /* __KERNEL__ */
238 #endif /* _KOBJECT_H_ */