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.
10 * Please read Documentation/kobject.txt before using the kobject
11 * interface, ESPECIALLY the parts about reference counts and object
15 #if defined(__KERNEL__) && !defined(_KOBJECT_H_)
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>
24 #define KOBJ_NAME_LEN 20
28 char name[KOBJ_NAME_LEN];
30 struct list_head entry;
31 struct kobject * parent;
33 struct kobj_type * ktype;
34 struct dentry * dentry;
37 extern int kobject_set_name(struct kobject *, const char *, ...)
38 __attribute__((format(printf,2,3)));
40 static inline char * kobject_name(struct kobject * kobj)
45 extern void kobject_init(struct kobject *);
46 extern void kobject_cleanup(struct kobject *);
48 extern int kobject_add(struct kobject *);
49 extern void kobject_del(struct kobject *);
51 extern void kobject_rename(struct kobject *, char *new_name);
53 extern int kobject_register(struct kobject *);
54 extern void kobject_unregister(struct kobject *);
56 extern struct kobject * kobject_get(struct kobject *);
57 extern void kobject_put(struct kobject *);
59 extern void kobject_hotplug(const char *action, struct kobject *);
62 void (*release)(struct kobject *);
63 struct sysfs_ops * sysfs_ops;
64 struct attribute ** default_attrs;
69 * kset - a set of kobjects of a specific type, belonging
70 * to a specific subsystem.
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.
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.
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.
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);
94 struct subsystem * subsys;
95 struct kobj_type * ktype;
96 struct list_head list;
98 struct kset_hotplug_ops * hotplug_ops;
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);
107 static inline struct kset * to_kset(struct kobject * kobj)
109 return kobj ? container_of(kobj,struct kset,kobj) : NULL;
112 static inline struct kset * kset_get(struct kset * k)
114 return k ? to_kset(kobject_get(&k->kobj)) : NULL;
117 static inline void kset_put(struct kset * k)
119 kobject_put(&k->kobj);
122 static inline struct kobj_type * get_ktype(struct kobject * k)
124 if (k->kset && k->kset->ktype)
125 return k->kset->ktype;
130 extern struct kobject * kset_find_obj(struct kset *, const char *);
134 * Use this when initializing an embedded kset with no other
135 * fields to initialize.
137 #define set_kset_name(str) .kset = { .kobj = { .name = str } }
143 struct rw_semaphore rwsem;
146 #define decl_subsys(_name,_type,_hotplug_ops) \
147 struct subsystem _name##_subsys = { \
149 .kobj = { .name = __stringify(_name) }, \
151 .hotplug_ops =_hotplug_ops, \
154 #define decl_subsys_name(_varname,_name,_type,_hotplug_ops) \
155 struct subsystem _varname##_subsys = { \
157 .kobj = { .name = __stringify(_name) }, \
159 .hotplug_ops =_hotplug_ops, \
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
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).
176 * Can be used for any object type with an embedded ->kobj.
179 #define kobj_set_kset_s(obj,subsys) \
180 (obj)->kobj.kset = &(subsys).kset
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).
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
193 #define kset_set_kset_s(obj,subsys) \
194 (obj)->kset.kobj.kset = &(subsys).kset
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).
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.
206 #define subsys_set_kset(obj,_subsys) \
207 (obj)->subsys.kset.kobj.kset = &(_subsys).kset
209 extern void subsystem_init(struct subsystem *);
210 extern int subsystem_register(struct subsystem *);
211 extern void subsystem_unregister(struct subsystem *);
213 static inline struct subsystem * subsys_get(struct subsystem * s)
215 return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
218 static inline void subsys_put(struct subsystem * s)
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);
229 extern int subsys_create_file(struct subsystem * , struct subsys_attribute *);
230 extern void subsys_remove_file(struct subsystem * , struct subsys_attribute *);
233 #endif /* _KOBJECT_H_ */