sysfs.h 8.8 KB

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