sysfs.h 11 KB

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