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