sysfs.h 9.7 KB

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