blob: eb0e63ef297ff28112620fb6b68c10bfd7579ee6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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#ifndef _KOBJECT_H_
16#define _KOBJECT_H_
17
18#ifdef __KERNEL__
19
20#include <linux/types.h>
21#include <linux/list.h>
22#include <linux/sysfs.h>
Andrew Morton4a7fb632006-08-14 22:43:17 -070023#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kref.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
NeilBrown4508a7a2006-03-20 17:53:53 +110027#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/atomic.h>
29
Kay Sievers312c0042005-11-16 09:00:00 +010030#define KOBJ_NAME_LEN 20
31#define UEVENT_HELPER_PATH_LEN 256
Kay Sievers0296b222005-11-11 05:33:52 +010032
33/* path to the userspace helper executed on an event */
Kay Sievers312c0042005-11-16 09:00:00 +010034extern char uevent_helper[];
Kay Sievers0296b222005-11-11 05:33:52 +010035
Kay Sievers312c0042005-11-16 09:00:00 +010036/* counter to tag the uevent, read only except for the kobject core */
37extern u64 uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Kay Sievers0296b222005-11-11 05:33:52 +010039/* the actions here must match the proper string in lib/kobject_uevent.c */
40typedef int __bitwise kobject_action_t;
41enum kobject_action {
Kay Sievers5f123fb2005-11-11 14:43:07 +010042 KOBJ_ADD = (__force kobject_action_t) 0x01, /* exclusive to core */
43 KOBJ_REMOVE = (__force kobject_action_t) 0x02, /* exclusive to core */
44 KOBJ_CHANGE = (__force kobject_action_t) 0x03, /* device state change */
Adrian Bunk3106d462007-04-06 12:21:45 +020045 KOBJ_OFFLINE = (__force kobject_action_t) 0x04, /* device offline */
46 KOBJ_ONLINE = (__force kobject_action_t) 0x05, /* device online */
47 KOBJ_MOVE = (__force kobject_action_t) 0x06, /* device move */
Kay Sievers0296b222005-11-11 05:33:52 +010048};
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050struct kobject {
Dmitry Torokhovf3b4f3c2005-04-26 02:32:00 -050051 const char * k_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 char name[KOBJ_NAME_LEN];
53 struct kref kref;
54 struct list_head entry;
55 struct kobject * parent;
56 struct kset * kset;
57 struct kobj_type * ktype;
58 struct dentry * dentry;
NeilBrown4508a7a2006-03-20 17:53:53 +110059 wait_queue_head_t poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
62extern int kobject_set_name(struct kobject *, const char *, ...)
63 __attribute__((format(printf,2,3)));
64
Dmitry Torokhovf3b4f3c2005-04-26 02:32:00 -050065static inline const char * kobject_name(const struct kobject * kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 return kobj->k_name;
68}
69
70extern void kobject_init(struct kobject *);
71extern void kobject_cleanup(struct kobject *);
72
Andrew Morton4a7fb632006-08-14 22:43:17 -070073extern int __must_check kobject_add(struct kobject *);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070074extern int __must_check kobject_shadow_add(struct kobject *, struct dentry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075extern void kobject_del(struct kobject *);
76
Andrew Morton4a7fb632006-08-14 22:43:17 -070077extern int __must_check kobject_rename(struct kobject *, const char *new_name);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070078extern int __must_check kobject_shadow_rename(struct kobject *kobj,
79 struct dentry *new_parent,
80 const char *new_name);
Cornelia Huck8a824722006-11-20 17:07:51 +010081extern int __must_check kobject_move(struct kobject *, struct kobject *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Andrew Morton4a7fb632006-08-14 22:43:17 -070083extern int __must_check kobject_register(struct kobject *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084extern void kobject_unregister(struct kobject *);
85
86extern struct kobject * kobject_get(struct kobject *);
87extern void kobject_put(struct kobject *);
88
Kay Sievers86406242007-03-14 03:25:56 +010089extern struct kobject *kobject_kset_add_dir(struct kset *kset,
90 struct kobject *, const char *);
Jun'ichi Nomura74231722006-03-13 17:14:25 -050091extern struct kobject *kobject_add_dir(struct kobject *, const char *);
92
Al Virofd4f2df2005-10-21 03:18:50 -040093extern char * kobject_get_path(struct kobject *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95struct kobj_type {
96 void (*release)(struct kobject *);
97 struct sysfs_ops * sysfs_ops;
98 struct attribute ** default_attrs;
99};
100
101
102/**
103 * kset - a set of kobjects of a specific type, belonging
104 * to a specific subsystem.
105 *
106 * All kobjects of a kset should be embedded in an identical
107 * type. This type may have a descriptor, which the kset points
108 * to. This allows there to exist sets of objects of the same
109 * type in different subsystems.
110 *
111 * A subsystem does not have to be a list of only one type
112 * of object; multiple ksets can belong to one subsystem. All
113 * ksets of a subsystem share the subsystem's lock.
114 *
Kay Sievers312c0042005-11-16 09:00:00 +0100115 * Each kset can support specific event variables; it can
116 * supress the event generation or add subsystem specific
117 * variables carried with the event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
Kay Sievers312c0042005-11-16 09:00:00 +0100119struct kset_uevent_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 int (*filter)(struct kset *kset, struct kobject *kobj);
Dmitry Torokhov419cab32005-04-26 02:32:54 -0500121 const char *(*name)(struct kset *kset, struct kobject *kobj);
Kay Sievers312c0042005-11-16 09:00:00 +0100122 int (*uevent)(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int num_envp, char *buffer, int buffer_size);
124};
125
126struct kset {
127 struct subsystem * subsys;
128 struct kobj_type * ktype;
129 struct list_head list;
130 spinlock_t list_lock;
131 struct kobject kobj;
Kay Sievers312c0042005-11-16 09:00:00 +0100132 struct kset_uevent_ops * uevent_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135
136extern void kset_init(struct kset * k);
Andrew Morton4a7fb632006-08-14 22:43:17 -0700137extern int __must_check kset_add(struct kset * k);
138extern int __must_check kset_register(struct kset * k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139extern void kset_unregister(struct kset * k);
140
141static inline struct kset * to_kset(struct kobject * kobj)
142{
143 return kobj ? container_of(kobj,struct kset,kobj) : NULL;
144}
145
146static inline struct kset * kset_get(struct kset * k)
147{
148 return k ? to_kset(kobject_get(&k->kobj)) : NULL;
149}
150
151static inline void kset_put(struct kset * k)
152{
153 kobject_put(&k->kobj);
154}
155
156static inline struct kobj_type * get_ktype(struct kobject * k)
157{
158 if (k->kset && k->kset->ktype)
159 return k->kset->ktype;
160 else
161 return k->ktype;
162}
163
164extern struct kobject * kset_find_obj(struct kset *, const char *);
165
166
167/**
168 * Use this when initializing an embedded kset with no other
169 * fields to initialize.
170 */
171#define set_kset_name(str) .kset = { .kobj = { .name = str } }
172
173
174
175struct subsystem {
176 struct kset kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Kay Sievers312c0042005-11-16 09:00:00 +0100179#define decl_subsys(_name,_type,_uevent_ops) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180struct subsystem _name##_subsys = { \
181 .kset = { \
182 .kobj = { .name = __stringify(_name) }, \
183 .ktype = _type, \
Kay Sievers312c0042005-11-16 09:00:00 +0100184 .uevent_ops =_uevent_ops, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 } \
186}
Kay Sievers312c0042005-11-16 09:00:00 +0100187#define decl_subsys_name(_varname,_name,_type,_uevent_ops) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188struct subsystem _varname##_subsys = { \
189 .kset = { \
190 .kobj = { .name = __stringify(_name) }, \
191 .ktype = _type, \
Kay Sievers312c0042005-11-16 09:00:00 +0100192 .uevent_ops =_uevent_ops, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 } \
194}
195
196/* The global /sys/kernel/ subsystem for people to chain off of */
197extern struct subsystem kernel_subsys;
Michael Holzheu40394832006-05-09 12:53:49 +0200198/* The global /sys/hypervisor/ subsystem */
199extern struct subsystem hypervisor_subsys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201/**
202 * Helpers for setting the kset of registered objects.
203 * Often, a registered object belongs to a kset embedded in a
204 * subsystem. These do no magic, just make the resulting code
205 * easier to follow.
206 */
207
208/**
209 * kobj_set_kset_s(obj,subsys) - set kset for embedded kobject.
210 * @obj: ptr to some object type.
211 * @subsys: a subsystem object (not a ptr).
212 *
213 * Can be used for any object type with an embedded ->kobj.
214 */
215
216#define kobj_set_kset_s(obj,subsys) \
217 (obj)->kobj.kset = &(subsys).kset
218
219/**
220 * kset_set_kset_s(obj,subsys) - set kset for embedded kset.
221 * @obj: ptr to some object type.
222 * @subsys: a subsystem object (not a ptr).
223 *
224 * Can be used for any object type with an embedded ->kset.
225 * Sets the kset of @obj's embedded kobject (via its embedded
226 * kset) to @subsys.kset. This makes @obj a member of that
227 * kset.
228 */
229
230#define kset_set_kset_s(obj,subsys) \
231 (obj)->kset.kobj.kset = &(subsys).kset
232
233/**
234 * subsys_set_kset(obj,subsys) - set kset for subsystem
235 * @obj: ptr to some object type.
236 * @subsys: a subsystem object (not a ptr).
237 *
238 * Can be used for any object type with an embedded ->subsys.
239 * Sets the kset of @obj's kobject to @subsys.kset. This makes
240 * the object a member of that kset.
241 */
242
243#define subsys_set_kset(obj,_subsys) \
244 (obj)->subsys.kset.kobj.kset = &(_subsys).kset
245
246extern void subsystem_init(struct subsystem *);
Andrew Morton4a7fb632006-08-14 22:43:17 -0700247extern int __must_check subsystem_register(struct subsystem *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248extern void subsystem_unregister(struct subsystem *);
249
250static inline struct subsystem * subsys_get(struct subsystem * s)
251{
252 return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
253}
254
255static inline void subsys_put(struct subsystem * s)
256{
257 kset_put(&s->kset);
258}
259
260struct subsys_attribute {
261 struct attribute attr;
262 ssize_t (*show)(struct subsystem *, char *);
263 ssize_t (*store)(struct subsystem *, const char *, size_t);
264};
265
Andrew Morton4a7fb632006-08-14 22:43:17 -0700266extern int __must_check subsys_create_file(struct subsystem * ,
267 struct subsys_attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200269#if defined(CONFIG_HOTPLUG)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800270int kobject_uevent(struct kobject *kobj, enum kobject_action action);
271int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Cornelia Huck8a824722006-11-20 17:07:51 +0100272 char *envp[]);
Kay Sievers0296b222005-11-11 05:33:52 +0100273
Kay Sievers312c0042005-11-16 09:00:00 +0100274int add_uevent_var(char **envp, int num_envp, int *cur_index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 char *buffer, int buffer_size, int *cur_len,
276 const char *format, ...)
277 __attribute__((format (printf, 7, 8)));
278#else
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800279static inline int kobject_uevent(struct kobject *kobj, enum kobject_action action)
280{ return 0; }
281static inline int kobject_uevent_env(struct kobject *kobj,
Cornelia Huck8a824722006-11-20 17:07:51 +0100282 enum kobject_action action,
283 char *envp[])
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800284{ return 0; }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100285
Kay Sievers312c0042005-11-16 09:00:00 +0100286static inline int add_uevent_var(char **envp, int num_envp, int *cur_index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 char *buffer, int buffer_size, int *cur_len,
288 const char *format, ...)
289{ return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290#endif
291
292#endif /* __KERNEL__ */
293#endif /* _KOBJECT_H_ */