Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame^] | 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 | * 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 | |
| 34 | extern char uevent_helper[]; |
| 35 | |
| 36 | extern u64 uevent_seqnum; |
| 37 | |
| 38 | enum 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 | |
| 48 | struct 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 | |
| 63 | extern __printf(2, 3) |
| 64 | int kobject_set_name(struct kobject *kobj, const char *name, ...); |
| 65 | extern int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, |
| 66 | va_list vargs); |
| 67 | |
| 68 | static inline const char *kobject_name(const struct kobject *kobj) |
| 69 | { |
| 70 | return kobj->name; |
| 71 | } |
| 72 | |
| 73 | extern void kobject_init(struct kobject *kobj, struct kobj_type *ktype); |
| 74 | extern __printf(3, 4) __must_check |
| 75 | int kobject_add(struct kobject *kobj, struct kobject *parent, |
| 76 | const char *fmt, ...); |
| 77 | extern __printf(4, 5) __must_check |
| 78 | int kobject_init_and_add(struct kobject *kobj, |
| 79 | struct kobj_type *ktype, struct kobject *parent, |
| 80 | const char *fmt, ...); |
| 81 | |
| 82 | extern void kobject_del(struct kobject *kobj); |
| 83 | |
| 84 | extern struct kobject * __must_check kobject_create(void); |
| 85 | extern struct kobject * __must_check kobject_create_and_add(const char *name, |
| 86 | struct kobject *parent); |
| 87 | |
| 88 | extern int __must_check kobject_rename(struct kobject *, const char *new_name); |
| 89 | extern int __must_check kobject_move(struct kobject *, struct kobject *); |
| 90 | |
| 91 | extern struct kobject *kobject_get(struct kobject *kobj); |
| 92 | extern void kobject_put(struct kobject *kobj); |
| 93 | |
| 94 | extern char *kobject_get_path(struct kobject *kobj, gfp_t flag); |
| 95 | |
| 96 | struct 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 | |
| 104 | struct kobj_uevent_env { |
| 105 | char *envp[UEVENT_NUM_ENVP]; |
| 106 | int envp_idx; |
| 107 | char buf[UEVENT_BUFFER_SIZE]; |
| 108 | int buflen; |
| 109 | }; |
| 110 | |
| 111 | struct 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 | |
| 118 | struct 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 | |
| 126 | extern const struct sysfs_ops kobj_sysfs_ops; |
| 127 | |
| 128 | struct sock; |
| 129 | |
| 130 | struct 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 | |
| 137 | extern void kset_init(struct kset *kset); |
| 138 | extern int __must_check kset_register(struct kset *kset); |
| 139 | extern void kset_unregister(struct kset *kset); |
| 140 | extern struct kset * __must_check kset_create_and_add(const char *name, |
| 141 | const struct kset_uevent_ops *u, |
| 142 | struct kobject *parent_kobj); |
| 143 | |
| 144 | static inline struct kset *to_kset(struct kobject *kobj) |
| 145 | { |
| 146 | return kobj ? container_of(kobj, struct kset, kobj) : NULL; |
| 147 | } |
| 148 | |
| 149 | static inline struct kset *kset_get(struct kset *k) |
| 150 | { |
| 151 | return k ? to_kset(kobject_get(&k->kobj)) : NULL; |
| 152 | } |
| 153 | |
| 154 | static inline void kset_put(struct kset *k) |
| 155 | { |
| 156 | kobject_put(&k->kobj); |
| 157 | } |
| 158 | |
| 159 | static inline struct kobj_type *get_ktype(struct kobject *kobj) |
| 160 | { |
| 161 | return kobj->ktype; |
| 162 | } |
| 163 | |
| 164 | extern struct kobject *kset_find_obj(struct kset *, const char *); |
| 165 | |
| 166 | extern struct kobject *kernel_kobj; |
| 167 | extern struct kobject *mm_kobj; |
| 168 | extern struct kobject *hypervisor_kobj; |
| 169 | extern struct kobject *power_kobj; |
| 170 | extern struct kobject *firmware_kobj; |
| 171 | |
| 172 | #if defined(CONFIG_HOTPLUG) |
| 173 | int kobject_uevent(struct kobject *kobj, enum kobject_action action); |
| 174 | int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, |
| 175 | char *envp[]); |
| 176 | |
| 177 | __printf(2, 3) |
| 178 | int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...); |
| 179 | |
| 180 | int kobject_action_type(const char *buf, size_t count, |
| 181 | enum kobject_action *type); |
| 182 | #else |
| 183 | static inline int kobject_uevent(struct kobject *kobj, |
| 184 | enum kobject_action action) |
| 185 | { return 0; } |
| 186 | static inline int kobject_uevent_env(struct kobject *kobj, |
| 187 | enum kobject_action action, |
| 188 | char *envp[]) |
| 189 | { return 0; } |
| 190 | |
| 191 | static inline __printf(2, 3) |
| 192 | int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...) |
| 193 | { return 0; } |
| 194 | |
| 195 | static inline int kobject_action_type(const char *buf, size_t count, |
| 196 | enum kobject_action *type) |
| 197 | { return -EINVAL; } |
| 198 | #endif |
| 199 | |
| 200 | #endif |