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
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>
28 #define KOBJ_NAME_LEN 20
30 /* counter to tag the hotplug event, read only except for the kobject core */
31 extern u64 hotplug_seqnum;
35 char name[KOBJ_NAME_LEN];
37 struct list_head entry;
38 struct kobject * parent;
40 struct kobj_type * ktype;
41 struct dentry * dentry;
44 extern int kobject_set_name(struct kobject *, const char *, ...)
45 __attribute__((format(printf,2,3)));
47 static inline char * kobject_name(struct kobject * kobj)
52 extern void kobject_init(struct kobject *);
53 extern void kobject_cleanup(struct kobject *);
55 extern int kobject_add(struct kobject *);
56 extern void kobject_del(struct kobject *);
58 extern int kobject_rename(struct kobject *, char *new_name);
60 extern int kobject_register(struct kobject *);
61 extern void kobject_unregister(struct kobject *);
63 extern struct kobject * kobject_get(struct kobject *);
64 extern void kobject_put(struct kobject *);
66 extern char * kobject_get_path(struct kobject *, int);
69 void (*release)(struct kobject *);
70 struct sysfs_ops * sysfs_ops;
71 struct attribute ** default_attrs;
76 * kset - a set of kobjects of a specific type, belonging
77 * to a specific subsystem.
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.
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.
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.
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);
101 struct subsystem * subsys;
102 struct kobj_type * ktype;
103 struct list_head list;
105 struct kset_hotplug_ops * hotplug_ops;
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);
114 static inline struct kset * to_kset(struct kobject * kobj)
116 return kobj ? container_of(kobj,struct kset,kobj) : NULL;
119 static inline struct kset * kset_get(struct kset * k)
121 return k ? to_kset(kobject_get(&k->kobj)) : NULL;
124 static inline void kset_put(struct kset * k)
126 kobject_put(&k->kobj);
129 static inline struct kobj_type * get_ktype(struct kobject * k)
131 if (k->kset && k->kset->ktype)
132 return k->kset->ktype;
137 extern struct kobject * kset_find_obj(struct kset *, const char *);
141 * Use this when initializing an embedded kset with no other
142 * fields to initialize.
144 #define set_kset_name(str) .kset = { .kobj = { .name = str } }
150 struct rw_semaphore rwsem;
153 #define decl_subsys(_name,_type,_hotplug_ops) \
154 struct subsystem _name##_subsys = { \
156 .kobj = { .name = __stringify(_name) }, \
158 .hotplug_ops =_hotplug_ops, \
161 #define decl_subsys_name(_varname,_name,_type,_hotplug_ops) \
162 struct subsystem _varname##_subsys = { \
164 .kobj = { .name = __stringify(_name) }, \
166 .hotplug_ops =_hotplug_ops, \
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
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).
183 * Can be used for any object type with an embedded ->kobj.
186 #define kobj_set_kset_s(obj,subsys) \
187 (obj)->kobj.kset = &(subsys).kset
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).
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
200 #define kset_set_kset_s(obj,subsys) \
201 (obj)->kset.kobj.kset = &(subsys).kset
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).
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.
213 #define subsys_set_kset(obj,_subsys) \
214 (obj)->subsys.kset.kobj.kset = &(_subsys).kset
216 extern void subsystem_init(struct subsystem *);
217 extern int subsystem_register(struct subsystem *);
218 extern void subsystem_unregister(struct subsystem *);
220 static inline struct subsystem * subsys_get(struct subsystem * s)
222 return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
225 static inline void subsys_put(struct subsystem * s)
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);
236 extern int subsys_create_file(struct subsystem * , struct subsys_attribute *);
237 extern void subsys_remove_file(struct subsystem * , struct subsys_attribute *);
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)));
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, ...)
253 #endif /* __KERNEL__ */
254 #endif /* _KOBJECT_H_ */