sysfs.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * sysfs.h - definitions for the device driver filesystem
  3. *
  4. * Copyright (c) 2001,2002 Patrick Mochel
  5. * Copyright (c) 2004 Silicon Graphics, Inc.
  6. * Copyright (c) 2007 SUSE Linux Products GmbH
  7. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  8. *
  9. * Please see Documentation/filesystems/sysfs.txt for more information.
  10. */
  11. #ifndef _SYSFS_H_
  12. #define _SYSFS_H_
  13. #include <linux/compiler.h>
  14. #include <linux/errno.h>
  15. #include <linux/list.h>
  16. #include <linux/lockdep.h>
  17. #include <linux/kobject_ns.h>
  18. #include <asm/atomic.h>
  19. struct kobject;
  20. struct module;
  21. enum kobj_ns_type;
  22. struct attribute {
  23. const char *name;
  24. mode_t mode;
  25. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  26. struct lock_class_key *key;
  27. struct lock_class_key skey;
  28. #endif
  29. };
  30. /**
  31. * sysfs_attr_init - initialize a dynamically allocated sysfs attribute
  32. * @attr: struct attribute to initialize
  33. *
  34. * Initialize a dynamically allocated struct attribute so we can
  35. * make lockdep happy. This is a new requirement for attributes
  36. * and initially this is only needed when lockdep is enabled.
  37. * Lockdep gives a nice error when your attribute is added to
  38. * sysfs if you don't have this.
  39. */
  40. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  41. #define sysfs_attr_init(attr) \
  42. do { \
  43. static struct lock_class_key __key; \
  44. \
  45. (attr)->key = &__key; \
  46. } while(0)
  47. #else
  48. #define sysfs_attr_init(attr) do {} while(0)
  49. #endif
  50. struct attribute_group {
  51. const char *name;
  52. mode_t (*is_visible)(struct kobject *,
  53. struct attribute *, int);
  54. struct attribute **attrs;
  55. };
  56. /**
  57. * Use these macros to make defining attributes easier. See include/linux/device.h
  58. * for examples..
  59. */
  60. #define __ATTR(_name,_mode,_show,_store) { \
  61. .attr = {.name = __stringify(_name), .mode = _mode }, \
  62. .show = _show, \
  63. .store = _store, \
  64. }
  65. #define __ATTR_RO(_name) { \
  66. .attr = { .name = __stringify(_name), .mode = 0444 }, \
  67. .show = _name##_show, \
  68. }
  69. #define __ATTR_NULL { .attr = { .name = NULL } }
  70. #define attr_name(_attr) (_attr).attr.name
  71. struct file;
  72. struct vm_area_struct;
  73. struct bin_attribute {
  74. struct attribute attr;
  75. size_t size;
  76. void *private;
  77. ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
  78. char *, loff_t, size_t);
  79. ssize_t (*write)(struct file *,struct kobject *, struct bin_attribute *,
  80. char *, loff_t, size_t);
  81. int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
  82. struct vm_area_struct *vma);
  83. };
  84. /**
  85. * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
  86. * @attr: struct bin_attribute to initialize
  87. *
  88. * Initialize a dynamically allocated struct bin_attribute so we
  89. * can make lockdep happy. This is a new requirement for
  90. * attributes and initially this is only needed when lockdep is
  91. * enabled. Lockdep gives a nice error when your attribute is
  92. * added to sysfs if you don't have this.
  93. */
  94. #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
  95. struct sysfs_ops {
  96. ssize_t (*show)(struct kobject *, struct attribute *,char *);
  97. ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);
  98. };
  99. struct sysfs_dirent;
  100. #ifdef CONFIG_SYSFS
  101. int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
  102. void *data, struct module *owner);
  103. int __must_check sysfs_create_dir(struct kobject *kobj);
  104. void sysfs_remove_dir(struct kobject *kobj);
  105. int __must_check sysfs_rename_dir(struct kobject *kobj, const char *new_name);
  106. int __must_check sysfs_move_dir(struct kobject *kobj,
  107. struct kobject *new_parent_kobj);
  108. int __must_check sysfs_create_file(struct kobject *kobj,
  109. const struct attribute *attr);
  110. int __must_check sysfs_create_files(struct kobject *kobj,
  111. const struct attribute **attr);
  112. int __must_check sysfs_chmod_file(struct kobject *kobj,
  113. const struct attribute *attr, mode_t mode);
  114. void sysfs_remove_file(struct kobject *kobj, const struct attribute *attr);
  115. void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr);
  116. int __must_check sysfs_create_bin_file(struct kobject *kobj,
  117. const struct bin_attribute *attr);
  118. void sysfs_remove_bin_file(struct kobject *kobj,
  119. const struct bin_attribute *attr);
  120. int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
  121. const char *name);
  122. int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
  123. struct kobject *target,
  124. const char *name);
  125. void sysfs_remove_link(struct kobject *kobj, const char *name);
  126. int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
  127. const char *old_name, const char *new_name);
  128. void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
  129. const char *name);
  130. int __must_check sysfs_create_group(struct kobject *kobj,
  131. const struct attribute_group *grp);
  132. int sysfs_update_group(struct kobject *kobj,
  133. const struct attribute_group *grp);
  134. void sysfs_remove_group(struct kobject *kobj,
  135. const struct attribute_group *grp);
  136. int sysfs_add_file_to_group(struct kobject *kobj,
  137. const struct attribute *attr, const char *group);
  138. void sysfs_remove_file_from_group(struct kobject *kobj,
  139. const struct attribute *attr, const char *group);
  140. int sysfs_merge_group(struct kobject *kobj,
  141. const struct attribute_group *grp);
  142. void sysfs_unmerge_group(struct kobject *kobj,
  143. const struct attribute_group *grp);
  144. void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
  145. void sysfs_notify_dirent(struct sysfs_dirent *sd);
  146. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  147. const void *ns,
  148. const unsigned char *name);
  149. struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd);
  150. void sysfs_put(struct sysfs_dirent *sd);
  151. /* Called to clear a ns tag when it is no longer valid */
  152. void sysfs_exit_ns(enum kobj_ns_type type, const void *tag);
  153. int __must_check sysfs_init(void);
  154. #else /* CONFIG_SYSFS */
  155. static inline int sysfs_schedule_callback(struct kobject *kobj,
  156. void (*func)(void *), void *data, struct module *owner)
  157. {
  158. return -ENOSYS;
  159. }
  160. static inline int sysfs_create_dir(struct kobject *kobj)
  161. {
  162. return 0;
  163. }
  164. static inline void sysfs_remove_dir(struct kobject *kobj)
  165. {
  166. }
  167. static inline int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
  168. {
  169. return 0;
  170. }
  171. static inline int sysfs_move_dir(struct kobject *kobj,
  172. struct kobject *new_parent_kobj)
  173. {
  174. return 0;
  175. }
  176. static inline int sysfs_create_file(struct kobject *kobj,
  177. const struct attribute *attr)
  178. {
  179. return 0;
  180. }
  181. static inline int sysfs_create_files(struct kobject *kobj,
  182. const struct attribute **attr)
  183. {
  184. return 0;
  185. }
  186. static inline int sysfs_chmod_file(struct kobject *kobj,
  187. const struct attribute *attr, mode_t mode)
  188. {
  189. return 0;
  190. }
  191. static inline void sysfs_remove_file(struct kobject *kobj,
  192. const struct attribute *attr)
  193. {
  194. }
  195. static inline void sysfs_remove_files(struct kobject *kobj,
  196. const struct attribute **attr)
  197. {
  198. }
  199. static inline int sysfs_create_bin_file(struct kobject *kobj,
  200. const struct bin_attribute *attr)
  201. {
  202. return 0;
  203. }
  204. static inline void sysfs_remove_bin_file(struct kobject *kobj,
  205. const struct bin_attribute *attr)
  206. {
  207. }
  208. static inline int sysfs_create_link(struct kobject *kobj,
  209. struct kobject *target, const char *name)
  210. {
  211. return 0;
  212. }
  213. static inline int sysfs_create_link_nowarn(struct kobject *kobj,
  214. struct kobject *target,
  215. const char *name)
  216. {
  217. return 0;
  218. }
  219. static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
  220. {
  221. }
  222. static inline int sysfs_rename_link(struct kobject *k, struct kobject *t,
  223. const char *old_name, const char *new_name)
  224. {
  225. return 0;
  226. }
  227. static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
  228. const char *name)
  229. {
  230. }
  231. static inline int sysfs_create_group(struct kobject *kobj,
  232. const struct attribute_group *grp)
  233. {
  234. return 0;
  235. }
  236. static inline int sysfs_update_group(struct kobject *kobj,
  237. const struct attribute_group *grp)
  238. {
  239. return 0;
  240. }
  241. static inline void sysfs_remove_group(struct kobject *kobj,
  242. const struct attribute_group *grp)
  243. {
  244. }
  245. static inline int sysfs_add_file_to_group(struct kobject *kobj,
  246. const struct attribute *attr, const char *group)
  247. {
  248. return 0;
  249. }
  250. static inline void sysfs_remove_file_from_group(struct kobject *kobj,
  251. const struct attribute *attr, const char *group)
  252. {
  253. }
  254. static inline int sysfs_merge_group(struct kobject *kobj,
  255. const struct attribute_group *grp)
  256. {
  257. return 0;
  258. }
  259. static inline void sysfs_unmerge_group(struct kobject *kobj,
  260. const struct attribute_group *grp)
  261. {
  262. }
  263. static inline void sysfs_notify(struct kobject *kobj, const char *dir,
  264. const char *attr)
  265. {
  266. }
  267. static inline void sysfs_notify_dirent(struct sysfs_dirent *sd)
  268. {
  269. }
  270. static inline
  271. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  272. const void *ns,
  273. const unsigned char *name)
  274. {
  275. return NULL;
  276. }
  277. static inline struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd)
  278. {
  279. return NULL;
  280. }
  281. static inline void sysfs_put(struct sysfs_dirent *sd)
  282. {
  283. }
  284. static inline void sysfs_exit_ns(int type, const void *tag)
  285. {
  286. }
  287. static inline int __must_check sysfs_init(void)
  288. {
  289. return 0;
  290. }
  291. #endif /* CONFIG_SYSFS */
  292. #endif /* _SYSFS_H_ */