kobject.h 8.1 KB

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