blob: 80e7484e6e31610ee87fdc7815cd2b80b96737de [file] [log] [blame]
Nicholas Flintham1e3d3112013-04-10 10:48:38 +01001/*
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 * Copyright (c) 2006-2008 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (c) 2006-2008 Novell Inc.
8 *
9 * This file is released under the GPLv2.
10 *
11 * Please read Documentation/kobject.txt before using the kobject
12 * interface, ESPECIALLY the parts about reference counts and object
13 * destructors.
14 */
15
16#ifndef _KOBJECT_H_
17#define _KOBJECT_H_
18
19#include <linux/types.h>
20#include <linux/list.h>
21#include <linux/sysfs.h>
22#include <linux/compiler.h>
23#include <linux/spinlock.h>
24#include <linux/kref.h>
25#include <linux/kobject_ns.h>
26#include <linux/kernel.h>
27#include <linux/wait.h>
28#include <linux/atomic.h>
29
30#define UEVENT_HELPER_PATH_LEN 256
31#define UEVENT_NUM_ENVP 32
32#define UEVENT_BUFFER_SIZE 2048
33
34extern char uevent_helper[];
35
36extern u64 uevent_seqnum;
37
38enum kobject_action {
39 KOBJ_ADD,
40 KOBJ_REMOVE,
41 KOBJ_CHANGE,
42 KOBJ_MOVE,
43 KOBJ_ONLINE,
44 KOBJ_OFFLINE,
45 KOBJ_MAX
46};
47
48struct kobject {
49 const char *name;
50 struct list_head entry;
51 struct kobject *parent;
52 struct kset *kset;
53 struct kobj_type *ktype;
54 struct sysfs_dirent *sd;
55 struct kref kref;
56 unsigned int state_initialized:1;
57 unsigned int state_in_sysfs:1;
58 unsigned int state_add_uevent_sent:1;
59 unsigned int state_remove_uevent_sent:1;
60 unsigned int uevent_suppress:1;
61};
62
63extern __printf(2, 3)
64int kobject_set_name(struct kobject *kobj, const char *name, ...);
65extern int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
66 va_list vargs);
67
68static inline const char *kobject_name(const struct kobject *kobj)
69{
70 return kobj->name;
71}
72
73extern void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
74extern __printf(3, 4) __must_check
75int kobject_add(struct kobject *kobj, struct kobject *parent,
76 const char *fmt, ...);
77extern __printf(4, 5) __must_check
78int kobject_init_and_add(struct kobject *kobj,
79 struct kobj_type *ktype, struct kobject *parent,
80 const char *fmt, ...);
81
82extern void kobject_del(struct kobject *kobj);
83
84extern struct kobject * __must_check kobject_create(void);
85extern struct kobject * __must_check kobject_create_and_add(const char *name,
86 struct kobject *parent);
87
88extern int __must_check kobject_rename(struct kobject *, const char *new_name);
89extern int __must_check kobject_move(struct kobject *, struct kobject *);
90
91extern struct kobject *kobject_get(struct kobject *kobj);
92extern void kobject_put(struct kobject *kobj);
93
94extern char *kobject_get_path(struct kobject *kobj, gfp_t flag);
95
96struct kobj_type {
97 void (*release)(struct kobject *kobj);
98 const struct sysfs_ops *sysfs_ops;
99 struct attribute **default_attrs;
100 const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
101 const void *(*namespace)(struct kobject *kobj);
102};
103
104struct kobj_uevent_env {
105 char *envp[UEVENT_NUM_ENVP];
106 int envp_idx;
107 char buf[UEVENT_BUFFER_SIZE];
108 int buflen;
109};
110
111struct kset_uevent_ops {
112 int (* const filter)(struct kset *kset, struct kobject *kobj);
113 const char *(* const name)(struct kset *kset, struct kobject *kobj);
114 int (* const uevent)(struct kset *kset, struct kobject *kobj,
115 struct kobj_uevent_env *env);
116};
117
118struct kobj_attribute {
119 struct attribute attr;
120 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
121 char *buf);
122 ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
123 const char *buf, size_t count);
124};
125
126extern const struct sysfs_ops kobj_sysfs_ops;
127
128struct sock;
129
130struct kset {
131 struct list_head list;
132 spinlock_t list_lock;
133 struct kobject kobj;
134 const struct kset_uevent_ops *uevent_ops;
135};
136
137extern void kset_init(struct kset *kset);
138extern int __must_check kset_register(struct kset *kset);
139extern void kset_unregister(struct kset *kset);
140extern struct kset * __must_check kset_create_and_add(const char *name,
141 const struct kset_uevent_ops *u,
142 struct kobject *parent_kobj);
143
144static inline struct kset *to_kset(struct kobject *kobj)
145{
146 return kobj ? container_of(kobj, struct kset, kobj) : NULL;
147}
148
149static inline struct kset *kset_get(struct kset *k)
150{
151 return k ? to_kset(kobject_get(&k->kobj)) : NULL;
152}
153
154static inline void kset_put(struct kset *k)
155{
156 kobject_put(&k->kobj);
157}
158
159static inline struct kobj_type *get_ktype(struct kobject *kobj)
160{
161 return kobj->ktype;
162}
163
164extern struct kobject *kset_find_obj(struct kset *, const char *);
165
166extern struct kobject *kernel_kobj;
167extern struct kobject *mm_kobj;
168extern struct kobject *hypervisor_kobj;
169extern struct kobject *power_kobj;
170extern struct kobject *firmware_kobj;
171
172#if defined(CONFIG_HOTPLUG)
173int kobject_uevent(struct kobject *kobj, enum kobject_action action);
174int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
175 char *envp[]);
176
177__printf(2, 3)
178int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...);
179
180int kobject_action_type(const char *buf, size_t count,
181 enum kobject_action *type);
182#else
183static inline int kobject_uevent(struct kobject *kobj,
184 enum kobject_action action)
185{ return 0; }
186static inline int kobject_uevent_env(struct kobject *kobj,
187 enum kobject_action action,
188 char *envp[])
189{ return 0; }
190
191static inline __printf(2, 3)
192int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
193{ return 0; }
194
195static inline int kobject_action_type(const char *buf, size_t count,
196 enum kobject_action *type)
197{ return -EINVAL; }
198#endif
199
200#endif