kobject.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #ifndef _KOBJECT_H_
  15. #define _KOBJECT_H_
  16. #ifdef __KERNEL__
  17. #include <linux/types.h>
  18. #include <linux/list.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/compiler.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/kref.h>
  23. #include <linux/kernel.h>
  24. #include <linux/wait.h>
  25. #include <asm/atomic.h>
  26. #define KOBJ_NAME_LEN 20
  27. #define UEVENT_HELPER_PATH_LEN 256
  28. /* path to the userspace helper executed on an event */
  29. extern char uevent_helper[];
  30. /* counter to tag the uevent, read only except for the kobject core */
  31. extern u64 uevent_seqnum;
  32. /*
  33. * The actions here must match the index to the string array
  34. * in lib/kobject_uevent.c
  35. *
  36. * Do not add new actions here without checking with the driver-core
  37. * maintainers. Action strings are not meant to express subsystem
  38. * or device specific properties. In most cases you want to send a
  39. * kobject_uevent_env(kobj, KOBJ_CHANGE, env) with additional event
  40. * specific variables added to the event environment.
  41. */
  42. enum kobject_action {
  43. KOBJ_ADD,
  44. KOBJ_REMOVE,
  45. KOBJ_CHANGE,
  46. KOBJ_MOVE,
  47. KOBJ_ONLINE,
  48. KOBJ_OFFLINE,
  49. KOBJ_MAX
  50. };
  51. /* The list of strings defining the valid kobject actions as specified above */
  52. extern const char *kobject_actions[];
  53. struct kobject {
  54. const char * k_name;
  55. char name[KOBJ_NAME_LEN];
  56. struct kref kref;
  57. struct list_head entry;
  58. struct kobject * parent;
  59. struct kset * kset;
  60. struct kobj_type * ktype;
  61. struct sysfs_dirent * sd;
  62. wait_queue_head_t poll;
  63. };
  64. extern int kobject_set_name(struct kobject *, const char *, ...)
  65. __attribute__((format(printf,2,3)));
  66. static inline const char * kobject_name(const struct kobject * kobj)
  67. {
  68. return kobj->k_name;
  69. }
  70. extern void kobject_init(struct kobject *);
  71. extern void kobject_cleanup(struct kobject *);
  72. extern int __must_check kobject_add(struct kobject *);
  73. extern int __must_check kobject_shadow_add(struct kobject *kobj,
  74. struct sysfs_dirent *shadow_parent);
  75. extern void kobject_del(struct kobject *);
  76. extern int __must_check kobject_rename(struct kobject *, const char *new_name);
  77. extern int __must_check kobject_shadow_rename(struct kobject *kobj,
  78. struct sysfs_dirent *new_parent,
  79. const char *new_name);
  80. extern int __must_check kobject_move(struct kobject *, struct kobject *);
  81. extern int __must_check kobject_register(struct kobject *);
  82. extern void kobject_unregister(struct kobject *);
  83. extern struct kobject * kobject_get(struct kobject *);
  84. extern void kobject_put(struct kobject *);
  85. extern struct kobject *kobject_kset_add_dir(struct kset *kset,
  86. struct kobject *, const char *);
  87. extern struct kobject *kobject_add_dir(struct kobject *, const char *);
  88. extern char * kobject_get_path(struct kobject *, gfp_t);
  89. struct kobj_type {
  90. void (*release)(struct kobject *);
  91. struct sysfs_ops * sysfs_ops;
  92. struct attribute ** default_attrs;
  93. };
  94. struct kset_uevent_ops {
  95. int (*filter)(struct kset *kset, struct kobject *kobj);
  96. const char *(*name)(struct kset *kset, struct kobject *kobj);
  97. int (*uevent)(struct kset *kset, struct kobject *kobj, char **envp,
  98. int num_envp, char *buffer, int buffer_size);
  99. };
  100. /*
  101. * struct kset - a set of kobjects of a specific type, belonging
  102. * to a specific subsystem.
  103. *
  104. * All kobjects of a kset should be embedded in an identical
  105. * type. This type may have a descriptor, which the kset points
  106. * to. This allows there to exist sets of objects of the same
  107. * type in different subsystems.
  108. *
  109. * A subsystem does not have to be a list of only one type
  110. * of object; multiple ksets can belong to one subsystem. All
  111. * ksets of a subsystem share the subsystem's lock.
  112. *
  113. * Each kset can support specific event variables; it can
  114. * supress the event generation or add subsystem specific
  115. * variables carried with the event.
  116. */
  117. struct kset {
  118. struct kobj_type * ktype;
  119. struct list_head list;
  120. spinlock_t list_lock;
  121. struct kobject kobj;
  122. struct kset_uevent_ops * uevent_ops;
  123. };
  124. extern void kset_init(struct kset * k);
  125. extern int __must_check kset_add(struct kset * k);
  126. extern int __must_check kset_register(struct kset * k);
  127. extern void kset_unregister(struct kset * k);
  128. static inline struct kset * to_kset(struct kobject * kobj)
  129. {
  130. return kobj ? container_of(kobj,struct kset,kobj) : NULL;
  131. }
  132. static inline struct kset * kset_get(struct kset * k)
  133. {
  134. return k ? to_kset(kobject_get(&k->kobj)) : NULL;
  135. }
  136. static inline void kset_put(struct kset * k)
  137. {
  138. kobject_put(&k->kobj);
  139. }
  140. static inline struct kobj_type * get_ktype(struct kobject * k)
  141. {
  142. if (k->kset && k->kset->ktype)
  143. return k->kset->ktype;
  144. else
  145. return k->ktype;
  146. }
  147. extern struct kobject * kset_find_obj(struct kset *, const char *);
  148. /*
  149. * Use this when initializing an embedded kset with no other
  150. * fields to initialize.
  151. */
  152. #define set_kset_name(str) .kset = { .kobj = { .name = str } }
  153. #define decl_subsys(_name,_type,_uevent_ops) \
  154. struct kset _name##_subsys = { \
  155. .kobj = { .name = __stringify(_name) }, \
  156. .ktype = _type, \
  157. .uevent_ops =_uevent_ops, \
  158. }
  159. #define decl_subsys_name(_varname,_name,_type,_uevent_ops) \
  160. struct kset _varname##_subsys = { \
  161. .kobj = { .name = __stringify(_name) }, \
  162. .ktype = _type, \
  163. .uevent_ops =_uevent_ops, \
  164. }
  165. /* The global /sys/kernel/ subsystem for people to chain off of */
  166. extern struct kset kernel_subsys;
  167. /* The global /sys/hypervisor/ subsystem */
  168. extern struct kset hypervisor_subsys;
  169. /*
  170. * Helpers for setting the kset of registered objects.
  171. * Often, a registered object belongs to a kset embedded in a
  172. * subsystem. These do no magic, just make the resulting code
  173. * easier to follow.
  174. */
  175. /**
  176. * kobj_set_kset_s(obj,subsys) - set kset for embedded kobject.
  177. * @obj: ptr to some object type.
  178. * @subsys: a subsystem object (not a ptr).
  179. *
  180. * Can be used for any object type with an embedded ->kobj.
  181. */
  182. #define kobj_set_kset_s(obj,subsys) \
  183. (obj)->kobj.kset = &(subsys)
  184. /**
  185. * kset_set_kset_s(obj,subsys) - set kset for embedded kset.
  186. * @obj: ptr to some object type.
  187. * @subsys: a subsystem object (not a ptr).
  188. *
  189. * Can be used for any object type with an embedded ->kset.
  190. * Sets the kset of @obj's embedded kobject (via its embedded
  191. * kset) to @subsys.kset. This makes @obj a member of that
  192. * kset.
  193. */
  194. #define kset_set_kset_s(obj,subsys) \
  195. (obj)->kset.kobj.kset = &(subsys)
  196. /**
  197. * subsys_set_kset(obj,subsys) - set kset for subsystem
  198. * @obj: ptr to some object type.
  199. * @_subsys: a subsystem object (not a ptr).
  200. *
  201. * Can be used for any object type with an embedded ->subsys.
  202. * Sets the kset of @obj's kobject to @subsys.kset. This makes
  203. * the object a member of that kset.
  204. */
  205. #define subsys_set_kset(obj,_subsys) \
  206. (obj)->subsys.kobj.kset = &(_subsys)
  207. extern void subsystem_init(struct kset *);
  208. extern int __must_check subsystem_register(struct kset *);
  209. extern void subsystem_unregister(struct kset *);
  210. static inline struct kset *subsys_get(struct kset *s)
  211. {
  212. if (s)
  213. return kset_get(s);
  214. return NULL;
  215. }
  216. static inline void subsys_put(struct kset *s)
  217. {
  218. kset_put(s);
  219. }
  220. struct subsys_attribute {
  221. struct attribute attr;
  222. ssize_t (*show)(struct kset *, char *);
  223. ssize_t (*store)(struct kset *, const char *, size_t);
  224. };
  225. extern int __must_check subsys_create_file(struct kset *,
  226. struct subsys_attribute *);
  227. #if defined(CONFIG_HOTPLUG)
  228. int kobject_uevent(struct kobject *kobj, enum kobject_action action);
  229. int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
  230. char *envp[]);
  231. int add_uevent_var(char **envp, int num_envp, int *cur_index,
  232. char *buffer, int buffer_size, int *cur_len,
  233. const char *format, ...)
  234. __attribute__((format (printf, 7, 8)));
  235. #else
  236. static inline int kobject_uevent(struct kobject *kobj, enum kobject_action action)
  237. { return 0; }
  238. static inline int kobject_uevent_env(struct kobject *kobj,
  239. enum kobject_action action,
  240. char *envp[])
  241. { return 0; }
  242. static inline int add_uevent_var(char **envp, int num_envp, int *cur_index,
  243. char *buffer, int buffer_size, int *cur_len,
  244. const char *format, ...)
  245. { return 0; }
  246. #endif
  247. #endif /* __KERNEL__ */
  248. #endif /* _KOBJECT_H_ */