kobject.h 7.0 KB

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