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 <linux/kernel.h>
27 #include <asm/atomic.h>
29 #define KOBJ_NAME_LEN 20
31 /* counter to tag the hotplug event, read only except for the kobject core */
32 extern u64 hotplug_seqnum;
36 char name[KOBJ_NAME_LEN];
38 struct list_head entry;
39 struct kobject * parent;
41 struct kobj_type * ktype;
42 struct dentry * dentry;
45 extern int kobject_set_name(struct kobject *, const char *, ...)
46 __attribute__((format(printf,2,3)));
48 static inline char * kobject_name(struct kobject * kobj)
53 extern void kobject_init(struct kobject *);
54 extern void kobject_cleanup(struct kobject *);
56 extern int kobject_add(struct kobject *);
57 extern void kobject_del(struct kobject *);
59 extern int kobject_rename(struct kobject *, char *new_name);
61 extern int kobject_register(struct kobject *);
62 extern void kobject_unregister(struct kobject *);
64 extern struct kobject * kobject_get(struct kobject *);
65 extern void kobject_put(struct kobject *);
67 extern char * kobject_get_path(struct kobject *, int);
70 void (*release)(struct kobject *);
71 struct sysfs_ops * sysfs_ops;
72 struct attribute ** default_attrs;
77 * kset - a set of kobjects of a specific type, belonging
78 * to a specific subsystem.
80 * All kobjects of a kset should be embedded in an identical
81 * type. This type may have a descriptor, which the kset points
82 * to. This allows there to exist sets of objects of the same
83 * type in different subsystems.
85 * A subsystem does not have to be a list of only one type
86 * of object; multiple ksets can belong to one subsystem. All
87 * ksets of a subsystem share the subsystem's lock.
89 * Each kset can support hotplugging; if it does, it will be given
90 * the opportunity to filter out specific kobjects from being
91 * reported, as well as to add its own "data" elements to the
92 * environment being passed to the hotplug helper.
94 struct kset_hotplug_ops {
95 int (*filter)(struct kset *kset, struct kobject *kobj);
96 char *(*name)(struct kset *kset, struct kobject *kobj);
97 int (*hotplug)(struct kset *kset, struct kobject *kobj, char **envp,
98 int num_envp, char *buffer, int buffer_size);
102 struct subsystem * subsys;
103 struct kobj_type * ktype;
104 struct list_head list;
106 struct kset_hotplug_ops * hotplug_ops;
110 extern void kset_init(struct kset * k);
111 extern int kset_add(struct kset * k);
112 extern int kset_register(struct kset * k);
113 extern void kset_unregister(struct kset * k);
115 static inline struct kset * to_kset(struct kobject * kobj)
117 return kobj ? container_of(kobj,struct kset,kobj) : NULL;
120 static inline struct kset * kset_get(struct kset * k)
122 return k ? to_kset(kobject_get(&k->kobj)) : NULL;
125 static inline void kset_put(struct kset * k)
127 kobject_put(&k->kobj);
130 static inline struct kobj_type * get_ktype(struct kobject * k)
132 if (k->kset && k->kset->ktype)
133 return k->kset->ktype;
138 extern struct kobject * kset_find_obj(struct kset *, const char *);
142 * Use this when initializing an embedded kset with no other
143 * fields to initialize.
145 #define set_kset_name(str) .kset = { .kobj = { .name = str } }
151 struct rw_semaphore rwsem;
154 #define decl_subsys(_name,_type,_hotplug_ops) \
155 struct subsystem _name##_subsys = { \
157 .kobj = { .name = __stringify(_name) }, \
159 .hotplug_ops =_hotplug_ops, \
162 #define decl_subsys_name(_varname,_name,_type,_hotplug_ops) \
163 struct subsystem _varname##_subsys = { \
165 .kobj = { .name = __stringify(_name) }, \
167 .hotplug_ops =_hotplug_ops, \
171 /* The global /sys/kernel/ subsystem for people to chain off of */
172 extern struct subsystem kernel_subsys;
175 * Helpers for setting the kset of registered objects.
176 * Often, a registered object belongs to a kset embedded in a
177 * subsystem. These do no magic, just make the resulting code
182 * kobj_set_kset_s(obj,subsys) - set kset for embedded kobject.
183 * @obj: ptr to some object type.
184 * @subsys: a subsystem object (not a ptr).
186 * Can be used for any object type with an embedded ->kobj.
189 #define kobj_set_kset_s(obj,subsys) \
190 (obj)->kobj.kset = &(subsys).kset
193 * kset_set_kset_s(obj,subsys) - set kset for embedded kset.
194 * @obj: ptr to some object type.
195 * @subsys: a subsystem object (not a ptr).
197 * Can be used for any object type with an embedded ->kset.
198 * Sets the kset of @obj's embedded kobject (via its embedded
199 * kset) to @subsys.kset. This makes @obj a member of that
203 #define kset_set_kset_s(obj,subsys) \
204 (obj)->kset.kobj.kset = &(subsys).kset
207 * subsys_set_kset(obj,subsys) - set kset for subsystem
208 * @obj: ptr to some object type.
209 * @subsys: a subsystem object (not a ptr).
211 * Can be used for any object type with an embedded ->subsys.
212 * Sets the kset of @obj's kobject to @subsys.kset. This makes
213 * the object a member of that kset.
216 #define subsys_set_kset(obj,_subsys) \
217 (obj)->subsys.kset.kobj.kset = &(_subsys).kset
219 extern void subsystem_init(struct subsystem *);
220 extern int subsystem_register(struct subsystem *);
221 extern void subsystem_unregister(struct subsystem *);
223 static inline struct subsystem * subsys_get(struct subsystem * s)
225 return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
228 static inline void subsys_put(struct subsystem * s)
233 struct subsys_attribute {
234 struct attribute attr;
235 ssize_t (*show)(struct subsystem *, char *);
236 ssize_t (*store)(struct subsystem *, const char *, size_t);
239 extern int subsys_create_file(struct subsystem * , struct subsys_attribute *);
240 extern void subsys_remove_file(struct subsystem * , struct subsys_attribute *);
242 #ifdef CONFIG_HOTPLUG
243 void kobject_hotplug(struct kobject *kobj, enum kobject_action action);
244 int add_hotplug_env_var(char **envp, int num_envp, int *cur_index,
245 char *buffer, int buffer_size, int *cur_len,
246 const char *format, ...)
247 __attribute__((format (printf, 7, 8)));
249 static inline void kobject_hotplug(struct kobject *kobj, enum kobject_action action) { }
250 static inline int add_hotplug_env_var(char **envp, int num_envp, int *cur_index,
251 char *buffer, int buffer_size, int *cur_len,
252 const char *format, ...)
256 #endif /* __KERNEL__ */
257 #endif /* _KOBJECT_H_ */