Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
422 giacomo 1
/*
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
#if defined(__KERNEL__) && !defined(_KOBJECT_H_)
16
#define _KOBJECT_H_
17
 
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>
23
 
24
#define KOBJ_NAME_LEN   20
25
 
26
struct kobject {
27
        char                    * k_name;
28
        char                    name[KOBJ_NAME_LEN];
29
        atomic_t                refcount;
30
        struct list_head        entry;
31
        struct kobject          * parent;
32
        struct kset             * kset;
33
        struct kobj_type        * ktype;
34
        struct dentry           * dentry;
35
};
36
 
37
extern int kobject_set_name(struct kobject *, const char *, ...)
38
        __attribute__((format(printf,2,3)));
39
 
40
static inline char * kobject_name(struct kobject * kobj)
41
{
42
        return kobj->k_name;
43
}
44
 
45
extern void kobject_init(struct kobject *);
46
extern void kobject_cleanup(struct kobject *);
47
 
48
extern int kobject_add(struct kobject *);
49
extern void kobject_del(struct kobject *);
50
 
51
extern void kobject_rename(struct kobject *, char *new_name);
52
 
53
extern int kobject_register(struct kobject *);
54
extern void kobject_unregister(struct kobject *);
55
 
56
extern struct kobject * kobject_get(struct kobject *);
57
extern void kobject_put(struct kobject *);
58
 
59
 
60
struct kobj_type {
61
        void (*release)(struct kobject *);
62
        struct sysfs_ops        * sysfs_ops;
63
        struct attribute        ** default_attrs;
64
};
65
 
66
 
67
/**
68
 *      kset - a set of kobjects of a specific type, belonging
69
 *      to a specific subsystem.
70
 *
71
 *      All kobjects of a kset should be embedded in an identical
72
 *      type. This type may have a descriptor, which the kset points
73
 *      to. This allows there to exist sets of objects of the same
74
 *      type in different subsystems.
75
 *
76
 *      A subsystem does not have to be a list of only one type
77
 *      of object; multiple ksets can belong to one subsystem. All
78
 *      ksets of a subsystem share the subsystem's lock.
79
 *
80
 *      Each kset can support hotplugging; if it does, it will be given
81
 *      the opportunity to filter out specific kobjects from being
82
 *      reported, as well as to add its own "data" elements to the
83
 *      environment being passed to the hotplug helper.
84
 */
85
struct kset_hotplug_ops {
86
        int (*filter)(struct kset *kset, struct kobject *kobj);
87
        char *(*name)(struct kset *kset, struct kobject *kobj);
88
        int (*hotplug)(struct kset *kset, struct kobject *kobj, char **envp,
89
                        int num_envp, char *buffer, int buffer_size);
90
};
91
 
92
struct kset {
93
        struct subsystem        * subsys;
94
        struct kobj_type        * ktype;
95
        struct list_head        list;
96
        struct kobject          kobj;
97
        struct kset_hotplug_ops * hotplug_ops;
98
};
99
 
100
 
101
extern void kset_init(struct kset * k);
102
extern int kset_add(struct kset * k);
103
extern int kset_register(struct kset * k);
104
extern void kset_unregister(struct kset * k);
105
 
106
static inline struct kset * to_kset(struct kobject * kobj)
107
{
108
        return kobj ? container_of(kobj,struct kset,kobj) : NULL;
109
}
110
 
111
static inline struct kset * kset_get(struct kset * k)
112
{
113
        return k ? to_kset(kobject_get(&k->kobj)) : NULL;
114
}
115
 
116
static inline void kset_put(struct kset * k)
117
{
118
        kobject_put(&k->kobj);
119
}
120
 
121
static inline struct kobj_type * get_ktype(struct kobject * k)
122
{
123
        if (k->kset && k->kset->ktype)
124
                return k->kset->ktype;
125
        else
126
                return k->ktype;
127
}
128
 
129
extern struct kobject * kset_find_obj(struct kset *, const char *);
130
 
131
 
132
/**
133
 * Use this when initializing an embedded kset with no other
134
 * fields to initialize.
135
 */
136
#define set_kset_name(str)      .kset = { .kobj = { .name = str } }
137
 
138
 
139
 
140
struct subsystem {
141
        struct kset             kset;
142
        struct rw_semaphore     rwsem;
143
};
144
 
145
#define decl_subsys(_name,_type,_hotplug_ops) \
146
struct subsystem _name##_subsys = { \
147
        .kset = { \
148
                .kobj = { .name = __stringify(_name) }, \
149
                .ktype = _type, \
150
                .hotplug_ops =_hotplug_ops, \
151
        } \
152
}
153
 
154
 
155
/**
156
 * Helpers for setting the kset of registered objects.
157
 * Often, a registered object belongs to a kset embedded in a
158
 * subsystem. These do no magic, just make the resulting code
159
 * easier to follow.
160
 */
161
 
162
/**
163
 *      kobj_set_kset_s(obj,subsys) - set kset for embedded kobject.
164
 *      @obj:           ptr to some object type.
165
 *      @subsys:        a subsystem object (not a ptr).
166
 *
167
 *      Can be used for any object type with an embedded ->kobj.
168
 */
169
 
170
#define kobj_set_kset_s(obj,subsys) \
171
        (obj)->kobj.kset = &(subsys).kset
172
 
173
/**
174
 *      kset_set_kset_s(obj,subsys) - set kset for embedded kset.
175
 *      @obj:           ptr to some object type.
176
 *      @subsys:        a subsystem object (not a ptr).
177
 *
178
 *      Can be used for any object type with an embedded ->kset.
179
 *      Sets the kset of @obj's  embedded kobject (via its embedded
180
 *      kset) to @subsys.kset. This makes @obj a member of that
181
 *      kset.
182
 */
183
 
184
#define kset_set_kset_s(obj,subsys) \
185
        (obj)->kset.kobj.kset = &(subsys).kset
186
 
187
/**
188
 *      subsys_set_kset(obj,subsys) - set kset for subsystem
189
 *      @obj:           ptr to some object type.
190
 *      @subsys:        a subsystem object (not a ptr).
191
 *
192
 *      Can be used for any object type with an embedded ->subsys.
193
 *      Sets the kset of @obj's kobject to @subsys.kset. This makes
194
 *      the object a member of that kset.
195
 */
196
 
197
#define subsys_set_kset(obj,_subsys) \
198
        (obj)->subsys.kset.kobj.kset = &(_subsys).kset
199
 
200
extern void subsystem_init(struct subsystem *);
201
extern int subsystem_register(struct subsystem *);
202
extern void subsystem_unregister(struct subsystem *);
203
 
204
static inline struct subsystem * subsys_get(struct subsystem * s)
205
{
206
        return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
207
}
208
 
209
static inline void subsys_put(struct subsystem * s)
210
{
211
        kset_put(&s->kset);
212
}
213
 
214
struct subsys_attribute {
215
        struct attribute attr;
216
        ssize_t (*show)(struct subsystem *, char *);
217
        ssize_t (*store)(struct subsystem *, const char *, size_t);
218
};
219
 
220
extern int subsys_create_file(struct subsystem * , struct subsys_attribute *);
221
extern void subsys_remove_file(struct subsystem * , struct subsys_attribute *);
222
 
223
 
224
#endif /* _KOBJECT_H_ */