kobject.h 8.2 KB

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