kobject.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 UEVENT_HELPER_PATH_LEN 256
  27. /* path to the userspace helper executed on an event */
  28. extern char uevent_helper[];
  29. /* counter to tag the uevent, read only except for the kobject core */
  30. extern u64 uevent_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, /* exclusive to core */
  35. KOBJ_REMOVE = (__force kobject_action_t) 0x02, /* exclusive to core */
  36. KOBJ_CHANGE = (__force kobject_action_t) 0x03, /* device state change */
  37. KOBJ_OFFLINE = (__force kobject_action_t) 0x04, /* device offline */
  38. KOBJ_ONLINE = (__force kobject_action_t) 0x05, /* device online */
  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 specific event variables; it can
  85. * supress the event generation or add subsystem specific
  86. * variables carried with the event.
  87. */
  88. struct kset_uevent_ops {
  89. int (*filter)(struct kset *kset, struct kobject *kobj);
  90. const char *(*name)(struct kset *kset, struct kobject *kobj);
  91. int (*uevent)(struct kset *kset, struct kobject *kobj, char **envp,
  92. int num_envp, char *buffer, int buffer_size);
  93. };
  94. struct kset {
  95. struct subsystem * subsys;
  96. struct kobj_type * ktype;
  97. struct list_head list;
  98. spinlock_t list_lock;
  99. struct kobject kobj;
  100. struct kset_uevent_ops * uevent_ops;
  101. };
  102. extern void kset_init(struct kset * k);
  103. extern int kset_add(struct kset * k);
  104. extern int kset_register(struct kset * k);
  105. extern void kset_unregister(struct kset * k);
  106. static inline struct kset * to_kset(struct kobject * kobj)
  107. {
  108. return kobj ? container_of(kobj,struct kset,kobj) : NULL;
  109. }
  110. static inline struct kset * kset_get(struct kset * k)
  111. {
  112. return k ? to_kset(kobject_get(&k->kobj)) : NULL;
  113. }
  114. static inline void kset_put(struct kset * k)
  115. {
  116. kobject_put(&k->kobj);
  117. }
  118. static inline struct kobj_type * get_ktype(struct kobject * k)
  119. {
  120. if (k->kset && k->kset->ktype)
  121. return k->kset->ktype;
  122. else
  123. return k->ktype;
  124. }
  125. extern struct kobject * kset_find_obj(struct kset *, const char *);
  126. /**
  127. * Use this when initializing an embedded kset with no other
  128. * fields to initialize.
  129. */
  130. #define set_kset_name(str) .kset = { .kobj = { .name = str } }
  131. struct subsystem {
  132. struct kset kset;
  133. struct rw_semaphore rwsem;
  134. };
  135. #define decl_subsys(_name,_type,_uevent_ops) \
  136. struct subsystem _name##_subsys = { \
  137. .kset = { \
  138. .kobj = { .name = __stringify(_name) }, \
  139. .ktype = _type, \
  140. .uevent_ops =_uevent_ops, \
  141. } \
  142. }
  143. #define decl_subsys_name(_varname,_name,_type,_uevent_ops) \
  144. struct subsystem _varname##_subsys = { \
  145. .kset = { \
  146. .kobj = { .name = __stringify(_name) }, \
  147. .ktype = _type, \
  148. .uevent_ops =_uevent_ops, \
  149. } \
  150. }
  151. /* The global /sys/kernel/ subsystem for people to chain off of */
  152. extern struct subsystem kernel_subsys;
  153. /**
  154. * Helpers for setting the kset of registered objects.
  155. * Often, a registered object belongs to a kset embedded in a
  156. * subsystem. These do no magic, just make the resulting code
  157. * easier to follow.
  158. */
  159. /**
  160. * kobj_set_kset_s(obj,subsys) - set kset for embedded kobject.
  161. * @obj: ptr to some object type.
  162. * @subsys: a subsystem object (not a ptr).
  163. *
  164. * Can be used for any object type with an embedded ->kobj.
  165. */
  166. #define kobj_set_kset_s(obj,subsys) \
  167. (obj)->kobj.kset = &(subsys).kset
  168. /**
  169. * kset_set_kset_s(obj,subsys) - set kset for embedded kset.
  170. * @obj: ptr to some object type.
  171. * @subsys: a subsystem object (not a ptr).
  172. *
  173. * Can be used for any object type with an embedded ->kset.
  174. * Sets the kset of @obj's embedded kobject (via its embedded
  175. * kset) to @subsys.kset. This makes @obj a member of that
  176. * kset.
  177. */
  178. #define kset_set_kset_s(obj,subsys) \
  179. (obj)->kset.kobj.kset = &(subsys).kset
  180. /**
  181. * subsys_set_kset(obj,subsys) - set kset for subsystem
  182. * @obj: ptr to some object type.
  183. * @subsys: a subsystem object (not a ptr).
  184. *
  185. * Can be used for any object type with an embedded ->subsys.
  186. * Sets the kset of @obj's kobject to @subsys.kset. This makes
  187. * the object a member of that kset.
  188. */
  189. #define subsys_set_kset(obj,_subsys) \
  190. (obj)->subsys.kset.kobj.kset = &(_subsys).kset
  191. extern void subsystem_init(struct subsystem *);
  192. extern int subsystem_register(struct subsystem *);
  193. extern void subsystem_unregister(struct subsystem *);
  194. static inline struct subsystem * subsys_get(struct subsystem * s)
  195. {
  196. return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
  197. }
  198. static inline void subsys_put(struct subsystem * s)
  199. {
  200. kset_put(&s->kset);
  201. }
  202. struct subsys_attribute {
  203. struct attribute attr;
  204. ssize_t (*show)(struct subsystem *, char *);
  205. ssize_t (*store)(struct subsystem *, const char *, size_t);
  206. };
  207. extern int subsys_create_file(struct subsystem * , struct subsys_attribute *);
  208. extern void subsys_remove_file(struct subsystem * , struct subsys_attribute *);
  209. #if defined(CONFIG_HOTPLUG) & defined(CONFIG_NET)
  210. void kobject_uevent(struct kobject *kobj, enum kobject_action action);
  211. int add_uevent_var(char **envp, int num_envp, int *cur_index,
  212. char *buffer, int buffer_size, int *cur_len,
  213. const char *format, ...)
  214. __attribute__((format (printf, 7, 8)));
  215. #else
  216. static inline void kobject_uevent(struct kobject *kobj, enum kobject_action action) { }
  217. static inline int add_uevent_var(char **envp, int num_envp, int *cur_index,
  218. char *buffer, int buffer_size, int *cur_len,
  219. const char *format, ...)
  220. { return 0; }
  221. #endif
  222. #endif /* __KERNEL__ */
  223. #endif /* _KOBJECT_H_ */