sysfs.h 8.7 KB

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