sysfs.h 9.8 KB

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