Blame view

include/linux/kobject.h 7.05 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   * kobject.h - generic kernel object infrastructure.
   *
f0e7e1bd7   Greg Kroah-Hartman   kobject: update t...
4
5
   * Copyright (c) 2002-2003 Patrick Mochel
   * Copyright (c) 2002-2003 Open Source Development Labs
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
6
7
   * Copyright (c) 2006-2008 Greg Kroah-Hartman <greg@kroah.com>
   * Copyright (c) 2006-2008 Novell Inc.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
   *
   * This file is released under the GPLv2.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
   * Please read Documentation/kobject.txt before using the kobject
   * interface, ESPECIALLY the parts about reference counts and object
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
13
   * destructors.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
16
17
   */
  
  #ifndef _KOBJECT_H_
  #define _KOBJECT_H_
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
  #include <linux/types.h>
  #include <linux/list.h>
  #include <linux/sysfs.h>
4a7fb6363   Andrew Morton   add __must_check ...
21
  #include <linux/compiler.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
  #include <linux/spinlock.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
  #include <linux/kref.h>
8488a38f4   David Howells   kobject: Break th...
24
  #include <linux/kobject_ns.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
  #include <linux/kernel.h>
4508a7a73   NeilBrown   [PATCH] sysfs: Al...
26
  #include <linux/wait.h>
60063497a   Arun Sharma   atomic: use <linu...
27
  #include <linux/atomic.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28

312c004d3   Kay Sievers   [PATCH] driver co...
29
  #define UEVENT_HELPER_PATH_LEN		256
7eff2e7a8   Kay Sievers   Driver core: chan...
30
31
  #define UEVENT_NUM_ENVP			32	/* number of env pointers */
  #define UEVENT_BUFFER_SIZE		2048	/* buffer for the variables */
0296b2281   Kay Sievers   [PATCH] remove CO...
32
33
  
  /* path to the userspace helper executed on an event */
312c004d3   Kay Sievers   [PATCH] driver co...
34
  extern char uevent_helper[];
0296b2281   Kay Sievers   [PATCH] remove CO...
35

312c004d3   Kay Sievers   [PATCH] driver co...
36
37
  /* counter to tag the uevent, read only except for the kobject core */
  extern u64 uevent_seqnum;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38

60a96a595   Kay Sievers   Driver core: acce...
39
40
41
42
43
44
45
46
47
48
  /*
   * The actions here must match the index to the string array
   * in lib/kobject_uevent.c
   *
   * Do not add new actions here without checking with the driver-core
   * maintainers. Action strings are not meant to express subsystem
   * or device specific properties. In most cases you want to send a
   * kobject_uevent_env(kobj, KOBJ_CHANGE, env) with additional event
   * specific variables added to the event environment.
   */
0296b2281   Kay Sievers   [PATCH] remove CO...
49
  enum kobject_action {
60a96a595   Kay Sievers   Driver core: acce...
50
51
52
53
54
55
56
  	KOBJ_ADD,
  	KOBJ_REMOVE,
  	KOBJ_CHANGE,
  	KOBJ_MOVE,
  	KOBJ_ONLINE,
  	KOBJ_OFFLINE,
  	KOBJ_MAX
0296b2281   Kay Sievers   [PATCH] remove CO...
57
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
  struct kobject {
af5ca3f4e   Kay Sievers   Driver core: chan...
59
  	const char		*name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
  	struct list_head	entry;
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
61
62
63
64
  	struct kobject		*parent;
  	struct kset		*kset;
  	struct kobj_type	*ktype;
  	struct sysfs_dirent	*sd;
a231934bd   Richard Kennedy   kobject: reorder ...
65
  	struct kref		kref;
0f4dafc05   Kay Sievers   Kobject: auto-cle...
66
  	unsigned int state_initialized:1;
0f4dafc05   Kay Sievers   Kobject: auto-cle...
67
68
69
  	unsigned int state_in_sysfs:1;
  	unsigned int state_add_uevent_sent:1;
  	unsigned int state_remove_uevent_sent:1;
f67f129e5   Ming Lei   Driver core: impl...
70
  	unsigned int uevent_suppress:1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
  };
b9075fa96   Joe Perches   treewide: use __p...
72
73
  extern __printf(2, 3)
  int kobject_set_name(struct kobject *kobj, const char *name, ...);
1fa5ae857   Kay Sievers   driver core: get ...
74
75
  extern int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
  				  va_list vargs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76

79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
77
  static inline const char *kobject_name(const struct kobject *kobj)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
  {
af5ca3f4e   Kay Sievers   Driver core: chan...
79
  	return kobj->name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
  }
f9cb074bf   Greg Kroah-Hartman   Kobject: rename k...
81
  extern void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
b9075fa96   Joe Perches   treewide: use __p...
82
83
84
85
86
87
88
  extern __printf(3, 4) __must_check
  int kobject_add(struct kobject *kobj, struct kobject *parent,
  		const char *fmt, ...);
  extern __printf(4, 5) __must_check
  int kobject_init_and_add(struct kobject *kobj,
  			 struct kobj_type *ktype, struct kobject *parent,
  			 const char *fmt, ...);
c11c4154e   Greg Kroah-Hartman   kobject: add kobj...
89

79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
90
  extern void kobject_del(struct kobject *kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91

43968d2f1   Greg Kroah-Hartman   kobject: get rid ...
92
  extern struct kobject * __must_check kobject_create(void);
3f9e3ee9d   Greg Kroah-Hartman   kobject: add kobj...
93
94
  extern struct kobject * __must_check kobject_create_and_add(const char *name,
  						struct kobject *parent);
4a7fb6363   Andrew Morton   add __must_check ...
95
  extern int __must_check kobject_rename(struct kobject *, const char *new_name);
8a82472f8   Cornelia Huck   driver core: Intr...
96
  extern int __must_check kobject_move(struct kobject *, struct kobject *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97

79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
98
99
  extern struct kobject *kobject_get(struct kobject *kobj);
  extern void kobject_put(struct kobject *kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100

79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
101
  extern char *kobject_get_path(struct kobject *kobj, gfp_t flag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
103
  
  struct kobj_type {
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
104
  	void (*release)(struct kobject *kobj);
52cf25d0a   Emese Revfy   Driver core: Cons...
105
  	const struct sysfs_ops *sysfs_ops;
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
106
  	struct attribute **default_attrs;
bc451f205   Eric W. Biederman   kobj: Add basic i...
107
108
  	const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
  	const void *(*namespace)(struct kobject *kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
  };
7eff2e7a8   Kay Sievers   Driver core: chan...
110
111
112
113
114
115
  struct kobj_uevent_env {
  	char *envp[UEVENT_NUM_ENVP];
  	int envp_idx;
  	char buf[UEVENT_BUFFER_SIZE];
  	int buflen;
  };
a56156489   Randy Dunlap   kset: kernel-doc ...
116
  struct kset_uevent_ops {
9cd43611c   Emese Revfy   kobject: Constify...
117
118
119
  	int (* const filter)(struct kset *kset, struct kobject *kobj);
  	const char *(* const name)(struct kset *kset, struct kobject *kobj);
  	int (* const uevent)(struct kset *kset, struct kobject *kobj,
7eff2e7a8   Kay Sievers   Driver core: chan...
120
  		      struct kobj_uevent_env *env);
a56156489   Randy Dunlap   kset: kernel-doc ...
121
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122

23b5212cc   Kay Sievers   Driver Core: add ...
123
124
125
126
127
128
129
  struct kobj_attribute {
  	struct attribute attr;
  	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
  			char *buf);
  	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
  			 const char *buf, size_t count);
  };
52cf25d0a   Emese Revfy   Driver core: Cons...
130
  extern const struct sysfs_ops kobj_sysfs_ops;
23b5212cc   Kay Sievers   Driver Core: add ...
131

bc451f205   Eric W. Biederman   kobj: Add basic i...
132
  struct sock;
be867b194   Serge E. Hallyn   sysfs: Comment sy...
133

6adf7554b   Greg Kroah-Hartman   kset: add some ke...
134
135
  /**
   * struct kset - a set of kobjects of a specific type, belonging to a specific subsystem.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
   *
6adf7554b   Greg Kroah-Hartman   kset: add some ke...
137
138
139
140
141
   * A kset defines a group of kobjects.  They can be individually
   * different "types" but overall these kobjects all want to be grouped
   * together and operated on in the same manner.  ksets are used to
   * define the attribute callbacks and other common events that happen to
   * a kobject.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
   *
6adf7554b   Greg Kroah-Hartman   kset: add some ke...
143
144
145
146
147
148
149
   * @list: the list of all kobjects for this kset
   * @list_lock: a lock for iterating over the kobjects
   * @kobj: the embedded kobject for this kset (recursion, isn't it fun...)
   * @uevent_ops: the set of uevent operations for this kset.  These are
   * called whenever a kobject has something happen to it so that the kset
   * can add new environment variables, or filter out the uevents if so
   * desired.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
  struct kset {
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
152
153
154
  	struct list_head list;
  	spinlock_t list_lock;
  	struct kobject kobj;
9cd43611c   Emese Revfy   kobject: Constify...
155
  	const struct kset_uevent_ops *uevent_ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
  };
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
157
158
159
  extern void kset_init(struct kset *kset);
  extern int __must_check kset_register(struct kset *kset);
  extern void kset_unregister(struct kset *kset);
b727c7028   Greg Kroah-Hartman   kset: add kset_cr...
160
  extern struct kset * __must_check kset_create_and_add(const char *name,
9cd43611c   Emese Revfy   kobject: Constify...
161
  						const struct kset_uevent_ops *u,
b727c7028   Greg Kroah-Hartman   kset: add kset_cr...
162
  						struct kobject *parent_kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163

79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
164
  static inline struct kset *to_kset(struct kobject *kobj)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
  {
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
166
  	return kobj ? container_of(kobj, struct kset, kobj) : NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
  }
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
168
  static inline struct kset *kset_get(struct kset *k)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
170
171
  {
  	return k ? to_kset(kobject_get(&k->kobj)) : NULL;
  }
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
172
  static inline void kset_put(struct kset *k)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
174
175
  {
  	kobject_put(&k->kobj);
  }
3514faca1   Greg Kroah-Hartman   kobject: remove s...
176
  static inline struct kobj_type *get_ktype(struct kobject *kobj)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
  {
3514faca1   Greg Kroah-Hartman   kobject: remove s...
178
  	return kobj->ktype;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
  }
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
180
  extern struct kobject *kset_find_obj(struct kset *, const char *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181

0ff21e466   Greg Kroah-Hartman   kobject: convert ...
182
183
  /* The global /sys/kernel/ kobject for people to chain off of */
  extern struct kobject *kernel_kobj;
ff7ea79cf   Nishanth Aravamudan   mm: create /sys/k...
184
185
  /* The global /sys/kernel/mm/ kobject for people to chain off of */
  extern struct kobject *mm_kobj;
2d72fc00a   Greg Kroah-Hartman   kobject: convert ...
186
187
  /* The global /sys/hypervisor/ kobject for people to chain off of */
  extern struct kobject *hypervisor_kobj;
d76e15fb2   Greg Kroah-Hartman   driver core: make...
188
189
  /* The global /sys/power/ kobject for people to chain off of */
  extern struct kobject *power_kobj;
f62ed9e33   Greg Kroah-Hartman   firmware: change ...
190
191
  /* The global /sys/firmware/ kobject for people to chain off of */
  extern struct kobject *firmware_kobj;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192

4d17ffda3   Kay Sievers   [PATCH] Kobject: ...
193
  #if defined(CONFIG_HOTPLUG)
542cfce6f   Aneesh Kumar K.V   kobject: kobject_...
194
195
  int kobject_uevent(struct kobject *kobj, enum kobject_action action);
  int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
8a82472f8   Cornelia Huck   driver core: Intr...
196
  			char *envp[]);
0296b2281   Kay Sievers   [PATCH] remove CO...
197

b9075fa96   Joe Perches   treewide: use __p...
198
199
  __printf(2, 3)
  int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...);
5c5daf657   Kay Sievers   Driver core: excl...
200
201
202
  
  int kobject_action_type(const char *buf, size_t count,
  			enum kobject_action *type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
  #else
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
204
205
  static inline int kobject_uevent(struct kobject *kobj,
  				 enum kobject_action action)
542cfce6f   Aneesh Kumar K.V   kobject: kobject_...
206
207
  { return 0; }
  static inline int kobject_uevent_env(struct kobject *kobj,
8a82472f8   Cornelia Huck   driver core: Intr...
208
209
  				      enum kobject_action action,
  				      char *envp[])
542cfce6f   Aneesh Kumar K.V   kobject: kobject_...
210
  { return 0; }
5f123fbd8   Kay Sievers   [PATCH] merge kob...
211

b9075fa96   Joe Perches   treewide: use __p...
212
  static inline __printf(2, 3)
b38360a28   Randy Dunlap   kobject.h: fix bu...
213
  int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
  { return 0; }
5c5daf657   Kay Sievers   Driver core: excl...
215
216
  
  static inline int kobject_action_type(const char *buf, size_t count,
79a6ee42f   Greg Kroah-Hartman   Kobject: fix codi...
217
  				      enum kobject_action *type)
5c5daf657   Kay Sievers   Driver core: excl...
218
  { return -EINVAL; }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
219
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
  #endif /* _KOBJECT_H_ */